Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback mode #688

Merged
merged 8 commits into from
May 23, 2021
Merged

Fallback mode #688

merged 8 commits into from
May 23, 2021

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Apr 12, 2021

Changes

  • Unifies response retrieval by introducing a new handleRequest function that encapsulates:
    • Response lookup via getResponse.
    • Life-cycle events emitting in the proper phases.
    • Unhandled request callback invocation.
  • Removes mocked response headers serialization into a HeadersList. Instead, serializes mock response headers into a FlatHeadersObject so that their shape is the same across regular start (where serialization is necessary to transmit the headers to the worker) and the fallback start (where serialization is only necessary to respond properly).
  • Adds support for the "fallback mode"—alternative interception mechanism that's automatically enabled in browser environments when Service Worker API isn't supported (legacy browsers) or cannot run (static local builds).
  • Splits the Rollup configuration so each build target uses its own set of plugins. This way the browser bundles are configured properly (see the note below).

Motivation

  • Add support for the browsers that don't support Service Workers.
  • Allows MSW execution in statically built apps (i.e. static Storybook build), where running a Service Worker from a file:// is forbidden.

GitHub

Roadmap

  • Find a way to enable and control the interceptor when calling worker.start() and worker.stop() without disrupting the public API while keeping the internals clean.
  • Bundle the debug transient dependency of interceptors property (currently pulls in util and tty dependencies being bundled for UMD and ESM targets).

@kettanaito kettanaito marked this pull request as draft April 12, 2021 09:14
@kettanaito kettanaito changed the title WIP Fallback mode Apr 12, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 12, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit d8e1652:

Sandbox Source
MSW React Configuration

emitter: StrictEventEmitter<ServerLifecycleEventsMap>,
handleRequestOptions?: HandleRequestOptions<ResponseType>,
): Promise<ResponseType | undefined> {
emitter.emit('request:start', request)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request cookies must not be set here, as they are already set in parseIsomorpchicRequest.

If cookies are set here once more, they will get overridden in Node.js, as request cookies in Node.js are resolved to {} (no location to check if the cookies should be inherited).

// Merge both direct request cookies and the cookies inherited
// from other same-origin requests in the cookie store.
mockedRequest.cookies = {
...mockedRequest.cookies,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprisingly, there was no test that checks if the request cookies are properly set given the combination of its own cookies and those inherited from the cookie store. Previously, such combination resulted in the own request cookies being overridden, with the store cookies taking priority.

Here, I swap the cookies priority, with the request's own cookies potentially overriding the store (inherited) cookies, as that seems like something you'd expect.

})
},
onMockedResponse(response) {
channel.send({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important to signal the worker ignoring the response's delay, as the worker resolves the response respecting that delay. Otherwise, the delay would get doubled.

@@ -1,37 +1,51 @@
import * as https from 'https'
import { rest } from 'msw'
import { setupServer } from 'msw/node'
import { setupServer, SetupServerApi } from 'msw/node'
import { ServerApi, createServer } from '@open-draft/test-server'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using this pull request as an opportunity to remove external HTTP servers dependency from tests and replace it with the locally running server that represents a production one (which mustn't be hit in most test cases).

@kettanaito kettanaito added the BREAKING CHANGE Pull request introducing breaking changes. label Apr 21, 2021
@kettanaito kettanaito force-pushed the feature/fallback-mode branch 3 times, most recently from ded0247 to 3634836 Compare April 27, 2021 00:40
@kettanaito kettanaito force-pushed the feature/fallback-mode branch 3 times, most recently from cc2107b to 3cbbb57 Compare May 13, 2021 12:02
@kettanaito
Copy link
Member Author

kettanaito commented May 22, 2021

Bundling debug

The debug dependency has a dynamic import that loads either Browser and Node versions of the library on runtime:

https://github.com/visionmedia/debug/blob/d177f2bc36d3b8b8e9b1b006727ef5e04f98eac7/src/index.js#L6-L10

When being bundled, though, it loaded the Node version all the time, resulting in broken browser builds.

Solution

  • Configure browser builds (UMD, IIFE) to prioritize the "browser" field in mainFields option (decides which dependency entries are loaded first).
  • Set browser: true and preferBuiltins: false for the browser builds.

Links

@kettanaito kettanaito marked this pull request as ready for review May 22, 2021 10:09
@kettanaito kettanaito merged commit c96ffa3 into master May 23, 2021
@kettanaito kettanaito deleted the feature/fallback-mode branch May 23, 2021 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE Pull request introducing breaking changes. DX feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unify response retrieval and the related event handling RFC: Fallback mode
1 participant