@zimic/interceptor
Version:
Next-gen TypeScript-first HTTP intercepting and mocking
70 lines (68 loc) • 3.13 kB
JavaScript
import { __require } from '../chunk-BJTO5JO5.mjs';
import fs from 'fs';
import path from 'path';
var MSW_ROOT_DIRECTORY = path.join(__require.resolve("msw"), "..", "..", "..");
var MSW_PACKAGE_PATH = path.join(MSW_ROOT_DIRECTORY, "package.json");
var MSW_CORE_DIRECTORY = path.join(MSW_ROOT_DIRECTORY, "lib", "core");
async function patchMSWExports() {
const mswPackageContentAsString = await fs.promises.readFile(MSW_PACKAGE_PATH, "utf-8");
const mswPackageContent = JSON.parse(mswPackageContentAsString);
const browserExports = mswPackageContent.exports["./browser"];
const nodeExports = mswPackageContent.exports["./node"];
browserExports.node = nodeExports.node;
nodeExports.browser = browserExports.browser;
const patchedMSWPackageContentAsString = JSON.stringify(mswPackageContent, null, 2);
await fs.promises.writeFile(MSW_PACKAGE_PATH, patchedMSWPackageContentAsString);
}
async function patchMSWWebSocketBroadcastChannel() {
await Promise.all(
["ws.js", "ws.mjs"].map(async (webSocketFileName) => {
const mswWebSocketPath = path.join(MSW_CORE_DIRECTORY, webSocketFileName);
const mswWebSocketContent = await fs.promises.readFile(mswWebSocketPath, "utf-8");
const patchedMSWWebSocketContent = mswWebSocketContent.replace(
[
'const webSocketChannel = new BroadcastChannel("msw:websocket-client-manager");',
"if (isBroadcastChannelWithUnref(webSocketChannel)) {",
" webSocketChannel.unref();",
"}"
].join("\n"),
"let webSocketChannel;"
).replace(
[
" );",
" const clientManager = new import_WebSocketClientManager.WebSocketClientManager(webSocketChannel);"
].join("\n"),
[
" );",
" if (!webSocketChannel) {",
' webSocketChannel = new BroadcastChannel("msw:websocket-client-manager");',
" if (isBroadcastChannelWithUnref(webSocketChannel)) {",
" webSocketChannel.unref();",
" }",
" }",
" const clientManager = new import_WebSocketClientManager.WebSocketClientManager(webSocketChannel);"
].join("\n")
).replace(
[" );", " const clientManager = new WebSocketClientManager(webSocketChannel);"].join("\n"),
[
" );",
" if (!webSocketChannel) {",
' webSocketChannel = new BroadcastChannel("msw:websocket-client-manager");',
" if (isBroadcastChannelWithUnref(webSocketChannel)) {",
" webSocketChannel.unref();",
" }",
" }",
" const clientManager = new WebSocketClientManager(webSocketChannel);"
].join("\n")
);
await fs.promises.writeFile(mswWebSocketPath, patchedMSWWebSocketContent);
})
);
}
async function postinstall() {
await Promise.all([patchMSWExports(), patchMSWWebSocketBroadcastChannel()]);
}
var postinstallPromise = postinstall();
export { MSW_CORE_DIRECTORY, MSW_PACKAGE_PATH, MSW_ROOT_DIRECTORY, postinstallPromise };
//# sourceMappingURL=postinstall.mjs.map
//# sourceMappingURL=postinstall.mjs.map