Spaces:
Running
Running
File size: 5,445 Bytes
d4b85c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
// src/node/SetupServerApi.ts
import { AsyncLocalStorage } from "node:async_hooks";
import { ClientRequestInterceptor } from "@mswjs/interceptors/ClientRequest";
import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
import { FetchInterceptor } from "@mswjs/interceptors/fetch";
// src/node/SetupServerCommonApi.ts
import { invariant } from "outvariant";
import {
BatchInterceptor,
InterceptorReadyState
} from "@mswjs/interceptors";
import { SetupApi } from '../core/SetupApi.mjs';
import { handleRequest } from '../core/utils/handleRequest.mjs';
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
import { InternalError, devUtils } from '../core/utils/internal/devUtils.mjs';
import { handleWebSocketEvent } from '../core/ws/handleWebSocketEvent.mjs';
import { webSocketInterceptor } from '../core/ws/webSocketInterceptor.mjs';
import { isHandlerKind } from '../core/utils/internal/isHandlerKind.mjs';
var DEFAULT_LISTEN_OPTIONS = {
onUnhandledRequest: "warn"
};
var SetupServerCommonApi = class extends SetupApi {
interceptor;
resolvedOptions;
constructor(interceptors, handlers) {
super(...handlers);
this.interceptor = new BatchInterceptor({
name: "setup-server",
interceptors: interceptors.map((Interceptor) => new Interceptor())
});
this.resolvedOptions = {};
}
/**
* Subscribe to all requests that are using the interceptor object
*/
init() {
this.interceptor.on(
"request",
async ({ request, requestId, controller }) => {
const response = await handleRequest(
request,
requestId,
this.handlersController.currentHandlers().filter(isHandlerKind("RequestHandler")),
this.resolvedOptions,
this.emitter,
{
onPassthroughResponse(request2) {
const acceptHeader = request2.headers.get("accept");
if (acceptHeader) {
const nextAcceptHeader = acceptHeader.replace(
/(,\s+)?msw\/passthrough/,
""
);
if (nextAcceptHeader) {
request2.headers.set("accept", nextAcceptHeader);
} else {
request2.headers.delete("accept");
}
}
}
}
);
if (response) {
controller.respondWith(response);
}
return;
}
);
this.interceptor.on("unhandledException", ({ error }) => {
if (error instanceof InternalError) {
throw error;
}
});
this.interceptor.on(
"response",
({ response, isMockedResponse, request, requestId }) => {
this.emitter.emit(
isMockedResponse ? "response:mocked" : "response:bypass",
{
response,
request,
requestId
}
);
}
);
handleWebSocketEvent({
getUnhandledRequestStrategy: () => {
return this.resolvedOptions.onUnhandledRequest;
},
getHandlers: () => {
return this.handlersController.currentHandlers();
},
onMockedConnection: () => {
},
onPassthroughConnection: () => {
}
});
}
listen(options = {}) {
this.resolvedOptions = mergeRight(
DEFAULT_LISTEN_OPTIONS,
options
);
this.interceptor.apply();
this.init();
this.subscriptions.push(() => this.interceptor.dispose());
webSocketInterceptor.apply();
this.subscriptions.push(() => webSocketInterceptor.dispose());
invariant(
[InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(
this.interceptor.readyState
),
devUtils.formatMessage(
'Failed to start "setupServer": the interceptor failed to apply. This is likely an issue with the library and you should report it at "%s".'
),
"https://github.com/mswjs/msw/issues/new/choose"
);
}
close() {
this.dispose();
}
};
// src/node/SetupServerApi.ts
var store = new AsyncLocalStorage();
var AsyncHandlersController = class {
rootContext;
constructor(initialHandlers) {
this.rootContext = { initialHandlers, handlers: [] };
}
get context() {
return store.getStore() || this.rootContext;
}
prepend(runtimeHandlers) {
this.context.handlers.unshift(...runtimeHandlers);
}
reset(nextHandlers) {
const context = this.context;
context.handlers = [];
context.initialHandlers = nextHandlers.length > 0 ? nextHandlers : context.initialHandlers;
}
currentHandlers() {
const { initialHandlers, handlers } = this.context;
return handlers.concat(initialHandlers);
}
};
var SetupServerApi = class extends SetupServerCommonApi {
constructor(handlers) {
super(
[ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],
handlers
);
this.handlersController = new AsyncHandlersController(handlers);
}
boundary(callback) {
return (...args) => {
return store.run(
{
initialHandlers: this.handlersController.currentHandlers(),
handlers: []
},
callback,
...args
);
};
}
close() {
super.close();
store.disable();
}
};
// src/node/setupServer.ts
var setupServer = (...handlers) => {
return new SetupServerApi(handlers);
};
export {
SetupServerApi,
setupServer
};
//# sourceMappingURL=index.mjs.map |