@depup/msw 3.0.1-depup.0 → 3.0.3-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/changes.json +7 -3
- package/lib/_chunks/cookie-store.js +3 -2
- package/lib/_chunks/cookie-store.js.map +1 -1
- package/lib/_chunks/interceptor-source.d.ts +5 -1
- package/lib/_chunks/interceptor-source.js +18 -5
- package/lib/_chunks/interceptor-source.js.map +1 -1
- package/lib/_chunks/request-handler.d.ts +3 -2
- package/lib/browser/index.d.ts +4 -9
- package/lib/browser/index.js +21 -14
- package/lib/browser/index.js.map +1 -1
- package/lib/graphql/index.d.ts +19 -4
- package/lib/graphql/index.js +24 -12
- package/lib/graphql/index.js.map +1 -1
- package/lib/iife/index.js +66 -33
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/vite/index.js +10 -7
- package/lib/vite/index.js.map +1 -1
- package/lib/vite/runtime.d.ts +2 -3
- package/lib/vite/runtime.js +70 -68
- package/lib/vite/runtime.js.map +1 -1
- package/package.json +12 -9
- package/src/browser/default-network-options.ts +1 -1
- package/src/browser/setup-worker.ts +1 -4
- package/src/browser/sources/fallback-http-source.ts +11 -8
- package/src/browser/sources/service-worker-source.ts +12 -5
- package/src/browser/utils/should-invalidate-worker.test.ts +0 -16
- package/src/core/experimental/define-network.ts +1 -1
- package/src/core/experimental/handlers-controller.test.ts +61 -0
- package/src/core/experimental/handlers-controller.ts +39 -6
- package/src/core/experimental/sources/network-source.ts +8 -1
- package/src/core/handlers/handler.ts +3 -2
- package/src/graphql/graphql-subscription.ts +30 -12
- package/src/vite/plugin.test.ts +1 -1
- package/src/vite/plugin.ts +13 -10
- package/src/vite/runtime.ts +85 -85
- package/src/vite/tsconfig.build.json +1 -1
- package/src/vite/options.d.ts +0 -5
|
@@ -32,8 +32,9 @@ export abstract class Handler {
|
|
|
32
32
|
/**
|
|
33
33
|
* Release the resources held by this handler.
|
|
34
34
|
*
|
|
35
|
-
* @note This method is invoked automatically when the
|
|
36
|
-
* disabled (e.g. `server.close()`)
|
|
35
|
+
* @note This method is invoked automatically when the handler leaves
|
|
36
|
+
* the network: when the network is disabled (e.g. `server.close()`)
|
|
37
|
+
* or when a reset removes it (e.g. `server.resetHandlers()`). Override it in the handlers that
|
|
37
38
|
* hold onto anything beyond a single frame, like timers, connections,
|
|
38
39
|
* or event listeners. Returning a promise makes the network await
|
|
39
40
|
* this handler's disposal before it tears itself down.
|
|
@@ -585,24 +585,32 @@ export class GraphQLSubscriptionTransportHandler extends GraphQLWebSocketHandler
|
|
|
585
585
|
}
|
|
586
586
|
|
|
587
587
|
/**
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
|
|
588
|
+
* Remove the given handler from the subscribers of every session
|
|
589
|
+
* this transport participates in. The handler no longer matches
|
|
590
|
+
* subscriptions on the connections opened while it was registered.
|
|
591
|
+
*/
|
|
592
|
+
public unsubscribe(handler: WebSocketHandler): void {
|
|
593
|
+
for (const connection of connections.values()) {
|
|
594
|
+
connection.subscribers.delete(handler)
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* End the active subscriptions of the sessions this transport
|
|
600
|
+
* participates in. The sessions and their subscribers are left intact:
|
|
601
|
+
* the handlers that survive the reset keep serving the connections
|
|
602
|
+
* opened before it, while the removed handlers unsubscribe themselves
|
|
603
|
+
* on disposal. The sessions own the protocol listeners for as long as
|
|
604
|
+
* the client stays connected.
|
|
592
605
|
*
|
|
593
606
|
* @note This method is invoked automatically when the handlers
|
|
594
607
|
* controller resets the handlers (e.g. `server.resetHandlers()`).
|
|
595
608
|
*/
|
|
596
609
|
public reset(): void {
|
|
597
610
|
for (const connection of connections.values()) {
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
if (entry.transport === this) {
|
|
602
|
-
connection.subscribers.delete(handler)
|
|
603
|
-
ownsConnection = true
|
|
604
|
-
}
|
|
605
|
-
}
|
|
611
|
+
const ownsConnection = Array.from(connection.subscribers.values()).some(
|
|
612
|
+
(entry) => entry.transport === this,
|
|
613
|
+
)
|
|
606
614
|
|
|
607
615
|
// Resetting the handlers detaches the resolvers from their
|
|
608
616
|
// subscriptions, so run their cleanups instead of dropping them.
|
|
@@ -935,6 +943,16 @@ export class GraphQLSubscriptionHandler<
|
|
|
935
943
|
this.isUsed = false
|
|
936
944
|
}
|
|
937
945
|
|
|
946
|
+
/**
|
|
947
|
+
* Stop serving the connections this handler was bound to.
|
|
948
|
+
* @note This method is invoked automatically when the handler
|
|
949
|
+
* is removed from the network, either by resetting the handlers
|
|
950
|
+
* (e.g. `server.resetHandlers()`) or by disabling the network.
|
|
951
|
+
*/
|
|
952
|
+
public dispose(): void {
|
|
953
|
+
this.#transport.unsubscribe(this)
|
|
954
|
+
}
|
|
955
|
+
|
|
938
956
|
/**
|
|
939
957
|
* @note Individual subscription handlers stay silent. The subscription
|
|
940
958
|
* transport owns the GraphQL-aware logging for the entire connection
|
package/src/vite/plugin.test.ts
CHANGED
|
@@ -784,7 +784,7 @@ test('serves only the worker in worker-only mode with an absolute base URL', asy
|
|
|
784
784
|
server.environments.client.pluginContainer.resolveId('virtual:msw'),
|
|
785
785
|
).resolves.toBeNull()
|
|
786
786
|
await expect(
|
|
787
|
-
server.environments.ssr.pluginContainer.resolveId('virtual:msw
|
|
787
|
+
server.environments.ssr.pluginContainer.resolveId('virtual:msw'),
|
|
788
788
|
).resolves.toBeNull()
|
|
789
789
|
})
|
|
790
790
|
|
package/src/vite/plugin.ts
CHANGED
|
@@ -5,7 +5,6 @@ import type { Plugin } from 'vite'
|
|
|
5
5
|
const WORKER_FILENAME = 'mockServiceWorker.js'
|
|
6
6
|
const WORKER_SCRIPT_PATH = new URL('../mockServiceWorker.js', import.meta.url)
|
|
7
7
|
const VIRTUAL_MODULE_ID = 'virtual:msw'
|
|
8
|
-
const VIRTUAL_OPTIONS_ID = 'virtual:msw/options'
|
|
9
8
|
const RUNTIME_PATH = fileURLToPath(new URL('./runtime.js', import.meta.url))
|
|
10
9
|
|
|
11
10
|
export interface MswPluginOptions {
|
|
@@ -53,33 +52,37 @@ export function msw(options: MswPluginOptions = {}): Plugin {
|
|
|
53
52
|
|
|
54
53
|
return {
|
|
55
54
|
name: 'msw',
|
|
56
|
-
|
|
55
|
+
resolveId(id) {
|
|
57
56
|
if (mode === 'worker-only') {
|
|
58
57
|
return
|
|
59
58
|
}
|
|
60
59
|
|
|
61
|
-
if (id === VIRTUAL_OPTIONS_ID) {
|
|
62
|
-
return `\0${VIRTUAL_OPTIONS_ID}`
|
|
63
|
-
}
|
|
64
|
-
|
|
65
60
|
if (id === VIRTUAL_MODULE_ID) {
|
|
66
|
-
return
|
|
61
|
+
return `\0${VIRTUAL_MODULE_ID}`
|
|
67
62
|
}
|
|
68
63
|
},
|
|
69
64
|
load(id) {
|
|
70
|
-
if (mode === 'worker-only' || id !== `\0${
|
|
65
|
+
if (mode === 'worker-only' || id !== `\0${VIRTUAL_MODULE_ID}`) {
|
|
71
66
|
return
|
|
72
67
|
}
|
|
73
68
|
|
|
69
|
+
// Tools like Vitest import the runtime natively when it's installed
|
|
70
|
+
// in "node_modules". Provide it with the environment's options from here
|
|
71
|
+
// so it never has to import a virtual module that only Vite can resolve.
|
|
74
72
|
if (this.environment.config.consumer === 'server') {
|
|
75
|
-
return `
|
|
73
|
+
return `
|
|
74
|
+
import { defaultNetworkOptions } from 'msw/node'
|
|
75
|
+
import { createNetwork } from ${JSON.stringify(RUNTIME_PATH)}
|
|
76
|
+
export const network = await createNetwork(defaultNetworkOptions)
|
|
77
|
+
`
|
|
76
78
|
}
|
|
77
79
|
|
|
78
80
|
environmentsUsingNetwork.add(this.environment.name)
|
|
79
81
|
|
|
80
82
|
return `
|
|
81
83
|
import { createDefaultNetworkOptions } from 'msw/browser'
|
|
82
|
-
|
|
84
|
+
import { createNetwork } from ${JSON.stringify(RUNTIME_PATH)}
|
|
85
|
+
export const network = await createNetwork(createDefaultNetworkOptions(${JSON.stringify(workerUrl)}))
|
|
83
86
|
`
|
|
84
87
|
},
|
|
85
88
|
configResolved(config) {
|
package/src/vite/runtime.ts
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
1
|
/// <reference types="vite/client" />
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
defineNetwork,
|
|
5
|
-
NetworkReadyState,
|
|
6
|
-
type NetworkApi,
|
|
7
|
-
} from 'msw/experimental'
|
|
8
|
-
import { defaultNetworkOptions } from 'virtual:msw/options'
|
|
3
|
+
import { defineNetwork, NetworkReadyState } from 'msw/experimental'
|
|
9
4
|
|
|
10
5
|
const hot = import.meta.hot
|
|
11
6
|
const hotData: { disposeNetwork?: () => Promise<void> } | undefined = hot?.data
|
|
12
7
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
export async function createNetwork(
|
|
9
|
+
options: Parameters<typeof defineNetwork>[0],
|
|
10
|
+
): Promise<ReturnType<typeof defineNetwork>> {
|
|
11
|
+
// SSR imports can re-evaluate the network before the old HMR listener runs.
|
|
12
|
+
while (hotData?.disposeNetwork) {
|
|
13
|
+
const disposePrevious = hotData.disposeNetwork
|
|
14
|
+
await disposePrevious()
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
if (hotData.disposeNetwork === disposePrevious) {
|
|
17
|
+
break
|
|
18
|
+
}
|
|
20
19
|
}
|
|
21
|
-
}
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
defineNetwork<typeof defaultNetworkOptions.sources>(defaultNetworkOptions)
|
|
21
|
+
const network = defineNetwork(options)
|
|
25
22
|
|
|
26
|
-
if (hot && hotData) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
if (hot && hotData) {
|
|
24
|
+
let resumeAfterUpdate = false
|
|
25
|
+
let retired = false
|
|
26
|
+
let pendingOperation: Promise<void> | undefined
|
|
27
|
+
const configure = network.configure
|
|
28
|
+
const enable = network.enable
|
|
29
|
+
const disable = network.disable
|
|
33
30
|
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
const runOperation = (action: () => void | Promise<void>) => {
|
|
32
|
+
const result = pendingOperation ? pendingOperation.then(action) : action()
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
if (result instanceof Promise) {
|
|
35
|
+
const operation = result.finally(() => {
|
|
36
|
+
if (pendingOperation === operation) {
|
|
37
|
+
pendingOperation = undefined
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
pendingOperation = operation
|
|
44
41
|
|
|
45
|
-
|
|
42
|
+
return operation
|
|
43
|
+
}
|
|
46
44
|
}
|
|
47
|
-
}
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
const stop = () => {
|
|
47
|
+
if (network.readyState === NetworkReadyState.ENABLED) {
|
|
48
|
+
return disable()
|
|
49
|
+
}
|
|
52
50
|
}
|
|
53
|
-
}
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
52
|
+
// An SSR entry may be re-evaluated while this module remains cached.
|
|
53
|
+
network.configure = (options) => {
|
|
54
|
+
void runOperation(() => {
|
|
55
|
+
if (retired) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
61
58
|
|
|
62
|
-
|
|
59
|
+
const stopped = stop()
|
|
63
60
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
if (stopped instanceof Promise) {
|
|
62
|
+
return stopped.then(() => configure(options))
|
|
63
|
+
}
|
|
67
64
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
65
|
+
configure(options)
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
network.enable = async () => {
|
|
69
|
+
await runOperation(() => {
|
|
70
|
+
if (!retired && network.readyState === NetworkReadyState.DISABLED) {
|
|
71
|
+
return enable()
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
network.disable = async () => {
|
|
76
|
+
await runOperation(stop)
|
|
77
|
+
}
|
|
81
78
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
79
|
+
const disableNetwork = async () => {
|
|
80
|
+
await network.disable()
|
|
81
|
+
}
|
|
85
82
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
83
|
+
const beforeUpdate = async () => {
|
|
84
|
+
resumeAfterUpdate = network.readyState === NetworkReadyState.ENABLED
|
|
85
|
+
await disableNetwork()
|
|
86
|
+
}
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
88
|
+
const afterUpdate = async () => {
|
|
89
|
+
const shouldResume = resumeAfterUpdate
|
|
90
|
+
resumeAfterUpdate = false
|
|
94
91
|
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
if (shouldResume && network.readyState === NetworkReadyState.DISABLED) {
|
|
93
|
+
await network.enable()
|
|
94
|
+
}
|
|
97
95
|
}
|
|
98
|
-
}
|
|
99
96
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
97
|
+
hot.on('vite:beforeUpdate', beforeUpdate)
|
|
98
|
+
hot.on('vite:afterUpdate', afterUpdate)
|
|
99
|
+
hot.on('vite:beforeFullReload', disableNetwork)
|
|
100
|
+
|
|
101
|
+
const disposeNetwork = async () => {
|
|
102
|
+
retired = true
|
|
103
|
+
resumeAfterUpdate = false
|
|
103
104
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
hot.off('vite:beforeUpdate', beforeUpdate)
|
|
106
|
+
hot.off('vite:afterUpdate', afterUpdate)
|
|
107
|
+
hot.off('vite:beforeFullReload', disableNetwork)
|
|
107
108
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
hot.off('vite:beforeFullReload', disableNetwork)
|
|
109
|
+
await disableNetwork()
|
|
110
|
+
}
|
|
111
111
|
|
|
112
|
-
|
|
112
|
+
hotData.disposeNetwork = disposeNetwork
|
|
113
|
+
hot.dispose(disposeNetwork)
|
|
113
114
|
}
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
hot.dispose(disposeNetwork)
|
|
116
|
+
return network
|
|
117
117
|
}
|