@zimic/interceptor 1.3.6-canary.2 → 1.3.6-canary.4
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/dist/{chunk-2YIGHTHM.js → chunk-SNMNOHD6.js} +5 -2
- package/dist/{chunk-2YIGHTHM.js.map → chunk-SNMNOHD6.js.map} +1 -1
- package/dist/{chunk-4XT4ZOJ2.mjs → chunk-WG7EXKWT.mjs} +5 -2
- package/dist/{chunk-4XT4ZOJ2.mjs.map → chunk-WG7EXKWT.mjs.map} +1 -1
- package/dist/cli.js +17 -17
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +2 -2
- package/dist/cli.mjs.map +1 -1
- package/dist/http.js +73 -69
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +73 -49
- package/dist/http.mjs.map +1 -1
- package/dist/scripts/postinstall.js +6 -52
- package/dist/scripts/postinstall.js.map +1 -1
- package/dist/scripts/postinstall.mjs +7 -52
- package/dist/scripts/postinstall.mjs.map +1 -1
- package/dist/server.js +6 -6
- package/dist/server.mjs +1 -1
- package/package.json +3 -3
- package/src/http/interceptor/HttpInterceptorStore.ts +1 -0
- package/src/http/interceptor/LocalHttpInterceptor.ts +2 -6
- package/src/http/interceptorWorker/HttpInterceptorWorker.ts +4 -0
- package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +82 -43
- package/src/http/interceptorWorker/types/msw.ts +2 -2
package/dist/http.mjs
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { InvalidFormDataError as InvalidFormDataError$1, InvalidJSONError as InvalidJSONError$1, HTTP_METHODS, HttpHeaders, HttpSearchParams, HttpFormData, parseHttpBody } from '@zimic/http';
|
|
2
2
|
import color2 from 'picocolors';
|
|
3
3
|
import { http, bypass, passthrough } from 'msw';
|
|
4
|
-
import * as mswBrowser from 'msw/browser';
|
|
5
|
-
import * as mswNode from 'msw/node';
|
|
6
4
|
import ClientSocket from 'isomorphic-ws';
|
|
7
5
|
|
|
8
6
|
// src/http/index.ts
|
|
@@ -1713,8 +1711,14 @@ var HttpInterceptorImplementation = class {
|
|
|
1713
1711
|
}
|
|
1714
1712
|
};
|
|
1715
1713
|
var HttpInterceptorImplementation_default = HttpInterceptorImplementation;
|
|
1716
|
-
var
|
|
1717
|
-
|
|
1714
|
+
var importMSWNode = createCachedDynamicImport_default(() => import('msw/node'));
|
|
1715
|
+
var importMSWBrowser = createCachedDynamicImport_default(() => import('msw/browser'));
|
|
1716
|
+
var LocalHttpInterceptorWorker = class _LocalHttpInterceptorWorker extends HttpInterceptorWorker_default {
|
|
1717
|
+
// Re-creating MSW workers may cause issues, so we should keep a single worker instance, even if all interceptor
|
|
1718
|
+
// workers are stopped. See https://github.com/mswjs/msw/issues/2597.
|
|
1719
|
+
static mswWorker;
|
|
1720
|
+
static isMSWWorkerRunning = false;
|
|
1721
|
+
mswHttpHandler;
|
|
1718
1722
|
httpHandlersByMethod = {
|
|
1719
1723
|
GET: [],
|
|
1720
1724
|
POST: [],
|
|
@@ -1727,52 +1731,66 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
|
1727
1731
|
constructor(_options) {
|
|
1728
1732
|
super();
|
|
1729
1733
|
}
|
|
1734
|
+
get class() {
|
|
1735
|
+
return _LocalHttpInterceptorWorker;
|
|
1736
|
+
}
|
|
1730
1737
|
get type() {
|
|
1731
1738
|
return "local";
|
|
1732
1739
|
}
|
|
1733
|
-
get
|
|
1734
|
-
if (!this.
|
|
1740
|
+
get mswWorkerOrThrow() {
|
|
1741
|
+
if (!this.class.mswWorker) {
|
|
1735
1742
|
throw new NotRunningHttpInterceptorError_default();
|
|
1736
1743
|
}
|
|
1737
|
-
return this.
|
|
1744
|
+
return this.class.mswWorker;
|
|
1738
1745
|
}
|
|
1739
|
-
|
|
1740
|
-
this.
|
|
1741
|
-
return this.
|
|
1746
|
+
async getMSWWorkerOrCreate() {
|
|
1747
|
+
this.class.mswWorker ??= await this.createMSWWorker();
|
|
1748
|
+
return this.class.mswWorker;
|
|
1742
1749
|
}
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
const
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
if (isServerSide() && "setupServer" in mswNode) {
|
|
1750
|
-
return mswNode.setupServer(mswHttpHandler);
|
|
1750
|
+
async createMSWWorker() {
|
|
1751
|
+
if (isServerSide()) {
|
|
1752
|
+
const mswNode = await importMSWNode();
|
|
1753
|
+
if ("setupServer" in mswNode) {
|
|
1754
|
+
return mswNode.setupServer();
|
|
1755
|
+
}
|
|
1751
1756
|
}
|
|
1752
|
-
if (isClientSide()
|
|
1753
|
-
|
|
1757
|
+
if (isClientSide()) {
|
|
1758
|
+
const mswBrowser = await importMSWBrowser();
|
|
1759
|
+
if ("setupWorker" in mswBrowser) {
|
|
1760
|
+
return mswBrowser.setupWorker();
|
|
1761
|
+
}
|
|
1754
1762
|
}
|
|
1755
1763
|
throw new UnknownHttpInterceptorPlatformError_default();
|
|
1756
1764
|
}
|
|
1757
1765
|
async start() {
|
|
1758
1766
|
await super.sharedStart(async () => {
|
|
1759
|
-
const
|
|
1767
|
+
const mswWorker = await this.getMSWWorkerOrCreate();
|
|
1760
1768
|
const sharedOptions = {
|
|
1761
1769
|
onUnhandledRequest: "bypass"
|
|
1762
1770
|
};
|
|
1763
|
-
|
|
1771
|
+
this.mswHttpHandler = http.all("*", async (context) => {
|
|
1772
|
+
const request = context.request;
|
|
1773
|
+
const response = await this.createResponseForRequest(request);
|
|
1774
|
+
return response;
|
|
1775
|
+
});
|
|
1776
|
+
mswWorker.use(this.mswHttpHandler);
|
|
1777
|
+
if (this.isInternalBrowserWorker(mswWorker)) {
|
|
1764
1778
|
this.platform = "browser";
|
|
1765
|
-
|
|
1779
|
+
if (!this.class.isMSWWorkerRunning) {
|
|
1780
|
+
await this.startInBrowser(mswWorker, sharedOptions);
|
|
1781
|
+
this.class.isMSWWorkerRunning = true;
|
|
1782
|
+
}
|
|
1766
1783
|
} else {
|
|
1767
1784
|
this.platform = "node";
|
|
1768
|
-
this.startInNode(
|
|
1785
|
+
this.startInNode(mswWorker, sharedOptions);
|
|
1786
|
+
this.class.isMSWWorkerRunning = true;
|
|
1769
1787
|
}
|
|
1770
1788
|
this.isRunning = true;
|
|
1771
1789
|
});
|
|
1772
1790
|
}
|
|
1773
|
-
async startInBrowser(
|
|
1791
|
+
async startInBrowser(mswWorker, sharedOptions) {
|
|
1774
1792
|
try {
|
|
1775
|
-
await
|
|
1793
|
+
await mswWorker.start({ ...sharedOptions, quiet: true });
|
|
1776
1794
|
} catch (error) {
|
|
1777
1795
|
this.handleBrowserWorkerStartError(error);
|
|
1778
1796
|
}
|
|
@@ -1780,36 +1798,35 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
|
1780
1798
|
handleBrowserWorkerStartError(error) {
|
|
1781
1799
|
if (UnregisteredBrowserServiceWorkerError_default.matchesRawError(error)) {
|
|
1782
1800
|
throw new UnregisteredBrowserServiceWorkerError_default();
|
|
1801
|
+
} else {
|
|
1802
|
+
throw error;
|
|
1783
1803
|
}
|
|
1784
|
-
throw error;
|
|
1785
1804
|
}
|
|
1786
|
-
startInNode(
|
|
1787
|
-
|
|
1805
|
+
startInNode(mswWorker, sharedOptions) {
|
|
1806
|
+
mswWorker.listen(sharedOptions);
|
|
1788
1807
|
}
|
|
1789
1808
|
async stop() {
|
|
1790
|
-
await super.sharedStop(() => {
|
|
1791
|
-
const
|
|
1792
|
-
if (this.isInternalBrowserWorker(internalWorker)) {
|
|
1793
|
-
this.stopInBrowser(internalWorker);
|
|
1794
|
-
} else {
|
|
1795
|
-
this.stopInNode(internalWorker);
|
|
1796
|
-
}
|
|
1809
|
+
await super.sharedStop(async () => {
|
|
1810
|
+
const mswWorker = await this.getMSWWorkerOrCreate();
|
|
1797
1811
|
this.clearHandlers();
|
|
1798
|
-
|
|
1812
|
+
const newMSWHandlers = mswWorker.listHandlers().filter((handler) => handler !== this.mswHttpHandler);
|
|
1813
|
+
mswWorker.resetHandlers(...newMSWHandlers);
|
|
1814
|
+
if (this.isInternalBrowserWorker(mswWorker)) ; else {
|
|
1815
|
+
this.stopInNode(mswWorker);
|
|
1816
|
+
this.class.isMSWWorkerRunning = false;
|
|
1817
|
+
}
|
|
1818
|
+
this.mswHttpHandler = void 0;
|
|
1799
1819
|
this.isRunning = false;
|
|
1800
1820
|
});
|
|
1801
1821
|
}
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
}
|
|
1805
|
-
stopInNode(internalWorker) {
|
|
1806
|
-
internalWorker.close();
|
|
1822
|
+
stopInNode(mswWorker) {
|
|
1823
|
+
mswWorker.close();
|
|
1807
1824
|
}
|
|
1808
1825
|
isInternalBrowserWorker(worker) {
|
|
1809
1826
|
return "start" in worker && "stop" in worker;
|
|
1810
1827
|
}
|
|
1811
1828
|
hasInternalBrowserWorker() {
|
|
1812
|
-
return this.isInternalBrowserWorker(this.
|
|
1829
|
+
return this.isInternalBrowserWorker(this.mswWorkerOrThrow);
|
|
1813
1830
|
}
|
|
1814
1831
|
hasInternalNodeWorker() {
|
|
1815
1832
|
return !this.hasInternalBrowserWorker();
|
|
@@ -2640,12 +2657,8 @@ var LocalHttpInterceptor = class {
|
|
|
2640
2657
|
this.implementation = new HttpInterceptorImplementation_default({
|
|
2641
2658
|
store: this.store,
|
|
2642
2659
|
baseURL,
|
|
2643
|
-
createWorker: () => {
|
|
2644
|
-
|
|
2645
|
-
},
|
|
2646
|
-
deleteWorker: () => {
|
|
2647
|
-
this.store.deleteLocalWorker();
|
|
2648
|
-
},
|
|
2660
|
+
createWorker: () => this.store.getOrCreateLocalWorker({}),
|
|
2661
|
+
deleteWorker: () => this.store.deleteLocalWorker(),
|
|
2649
2662
|
Handler: LocalHttpRequestHandler_default,
|
|
2650
2663
|
onUnhandledRequest: options.onUnhandledRequest,
|
|
2651
2664
|
requestSaving: options.requestSaving
|
|
@@ -2868,15 +2881,26 @@ var InvalidJSONError = class extends InvalidJSONError$1 {
|
|
|
2868
2881
|
};
|
|
2869
2882
|
/* istanbul ignore else -- @preserve
|
|
2870
2883
|
* The else is a fallback for when the error is not an instance of Error. */
|
|
2884
|
+
/* istanbul ignore next -- @preserve
|
|
2885
|
+
* This if statement only runs if concurrent calls to stop() are made, which is an edge case that is hard to
|
|
2886
|
+
* reliably reproduce in tests. */
|
|
2871
2887
|
/* istanbul ignore if -- @preserve
|
|
2872
2888
|
* This is just a type guard to ensure the value is valid. In practice, this condition should never be true. */
|
|
2873
2889
|
/* istanbul ignore next -- @preserve
|
|
2874
2890
|
* Ignoring because there will always be a handler for the given method and path at this point. */
|
|
2875
2891
|
/* istanbul ignore if -- @preserve
|
|
2876
2892
|
* Trying to access the internal worker when it does not exist should not happen. */
|
|
2893
|
+
/* istanbul ignore else -- @preserve
|
|
2894
|
+
* We still check if we actually imported the server module in case our `isServerSide()` check returns true, but
|
|
2895
|
+
* the environment actually resolves the browser module. */
|
|
2877
2896
|
/* istanbul ignore else -- @preserve */
|
|
2897
|
+
/* istanbul ignore else -- @preserve
|
|
2898
|
+
* We still check if we actually imported the browser module in case our `isClientSide()` check returns true, but
|
|
2899
|
+
* the environment actually resolves the server module. */
|
|
2878
2900
|
/* istanbul ignore next -- @preserve
|
|
2879
2901
|
* Ignoring because checking unknown platforms is not configured in our test setup. */
|
|
2902
|
+
/* istanbul ignore else -- @preserve
|
|
2903
|
+
* Since we start the internal worker once and do not stop it, tests may not be able exercise this branch. */
|
|
2880
2904
|
/* istanbul ignore next -- @preserve
|
|
2881
2905
|
* Ignoring as Node.js >=20 provides a global crypto and the import fallback won't run. */
|
|
2882
2906
|
/* istanbul ignore else -- @preserve
|