rspack-plugin-mock 1.2.0 → 2.0.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 +17 -4
- package/README.zh-CN.md +20 -7
- package/dist/helper.d.ts +59 -43
- package/dist/helper.js +103 -67
- package/dist/index.d.ts +4 -4
- package/dist/index.js +25 -21
- package/dist/json5-loader.mjs +8 -0
- package/dist/{resolvePluginOptions-DlkIkykz.js → options-Bd0PSZeT.js} +226 -256
- package/dist/rsbuild.d.ts +1 -1
- package/dist/rsbuild.js +41 -124
- package/dist/server-_doRwsZm.d.ts +89 -0
- package/dist/server.d.ts +2 -27
- package/dist/server.js +2 -3
- package/dist/{types-6lajtJPx.d.cts → types-GT6M6WuI.d.ts} +139 -10
- package/dist/ws-BcLCWVaK.js +1003 -0
- package/package.json +35 -47
- package/dist/chunk-CUT6urMc.cjs +0 -30
- package/dist/helper.cjs +0 -140
- package/dist/helper.d.cts +0 -110
- package/dist/index.cjs +0 -86
- package/dist/index.d.cts +0 -13
- package/dist/json5-loader.cjs +0 -32
- package/dist/logger-C0V8Cvvd.cjs +0 -800
- package/dist/logger-C48_LmdS.js +0 -710
- package/dist/mockWebsocket-DkVHpZCx.d.cts +0 -85
- package/dist/mockWebsocket-qLVAe-RI.d.ts +0 -85
- package/dist/resolvePluginOptions-Da5uqlBx.cjs +0 -506
- package/dist/rsbuild.cjs +0 -176
- package/dist/rsbuild.d.cts +0 -7
- package/dist/server.cjs +0 -9
- package/dist/server.d.cts +0 -27
- package/dist/types-DPzh7nJq.d.ts +0 -572
package/dist/rsbuild.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { buildMockServer, createMockCompiler,
|
|
3
|
-
import { isArray, toArray } from "@pengzhanbo/utils";
|
|
1
|
+
import { n as baseMiddleware, r as rewriteRequest, t as mockWebSocket } from "./ws-BcLCWVaK.js";
|
|
2
|
+
import { n as buildMockServer, r as createMockCompiler, t as resolvePluginOptions } from "./options-Bd0PSZeT.js";
|
|
3
|
+
import { isArray, objectEntries, toArray } from "@pengzhanbo/utils";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import process from "node:process";
|
|
6
|
-
import color from "picocolors";
|
|
7
6
|
import rspack from "@rspack/core";
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
|
|
7
|
+
import ansis from "ansis";
|
|
8
|
+
import { Socket } from "node:net";
|
|
11
9
|
//#region src/rsbuild.ts
|
|
12
10
|
function pluginMockServer(options = {}) {
|
|
13
11
|
return {
|
|
@@ -20,7 +18,7 @@ function pluginMockServer(options = {}) {
|
|
|
20
18
|
context: api.context.rootPath,
|
|
21
19
|
plugins: [new rspack.DefinePlugin(rsbuildConfig.source?.define || {})]
|
|
22
20
|
});
|
|
23
|
-
if (
|
|
21
|
+
if (api.context.action === "build") {
|
|
24
22
|
if (resolvedOptions.build) api.onAfterBuild(async () => {
|
|
25
23
|
const config = api.getNormalizedConfig();
|
|
26
24
|
await buildMockServer(resolvedOptions, path.resolve(process.cwd(), config.output.distPath.root || "dist"));
|
|
@@ -28,132 +26,54 @@ function pluginMockServer(options = {}) {
|
|
|
28
26
|
return;
|
|
29
27
|
}
|
|
30
28
|
const mockCompiler = createMockCompiler(resolvedOptions);
|
|
31
|
-
api.modifyRsbuildConfig((config) => {
|
|
32
|
-
updateServerProxyConfigByHttpMock(config);
|
|
33
|
-
const mockMiddleware = createMockMiddleware(mockCompiler, resolvedOptions);
|
|
34
|
-
config.dev ??= {};
|
|
35
|
-
config.dev.setupMiddlewares ??= [];
|
|
36
|
-
config.dev.setupMiddlewares.push((middlewares, server$1) => {
|
|
37
|
-
mockMiddleware(middlewares, () => server$1.sockWrite("static-changed"));
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
let port = 3079;
|
|
41
|
-
const shouldMockWs = toArray(resolvedOptions.wsPrefix).length > 0;
|
|
42
|
-
if (shouldMockWs) api.modifyRsbuildConfig(async (config) => {
|
|
43
|
-
const defaultPort = (config.server?.port || port) + 1;
|
|
44
|
-
port = await getPortPromise({ port: defaultPort });
|
|
45
|
-
updateServerProxyConfigByWSMock(config, options.wsPrefix || [], port);
|
|
46
|
-
});
|
|
47
|
-
let server;
|
|
48
|
-
function startMockServer() {
|
|
49
|
-
mockCompiler.run();
|
|
50
|
-
if (shouldMockWs) {
|
|
51
|
-
server = createServer();
|
|
52
|
-
mockWebSocket(mockCompiler, server, resolvedOptions);
|
|
53
|
-
server.listen(port);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function close() {
|
|
57
|
-
mockCompiler.close();
|
|
58
|
-
server?.close();
|
|
59
|
-
}
|
|
60
29
|
api.onAfterCreateCompiler(({ compiler }) => {
|
|
61
|
-
if ("compilers" in compiler) compiler.compilers.forEach((compiler
|
|
62
|
-
mockCompiler.updateAlias(compiler
|
|
30
|
+
if ("compilers" in compiler) compiler.compilers.forEach((compiler) => {
|
|
31
|
+
mockCompiler.updateAlias(compiler.options.resolve?.alias || {});
|
|
63
32
|
});
|
|
64
33
|
else mockCompiler.updateAlias(compiler.options.resolve?.alias || {});
|
|
65
34
|
});
|
|
66
|
-
api.
|
|
67
|
-
|
|
68
|
-
|
|
35
|
+
api.modifyRsbuildConfig((config) => updateServerProxyConfigByHttpMock(config));
|
|
36
|
+
function initMockServer(server) {
|
|
37
|
+
server.middlewares.use(baseMiddleware(mockCompiler, resolvedOptions));
|
|
38
|
+
if (resolvedOptions.reload && api.context.action === "dev") mockCompiler.on("update", () => server.sockWrite("static-changed"));
|
|
39
|
+
if (toArray(resolvedOptions.wsPrefix).length > 0) mockWebSocket(mockCompiler, server.httpServer, resolvedOptions);
|
|
40
|
+
mockCompiler.run();
|
|
41
|
+
}
|
|
42
|
+
api.onBeforeStartDevServer(({ server }) => initMockServer(server));
|
|
43
|
+
api.onBeforeStartPreviewServer(({ server }) => initMockServer(server));
|
|
44
|
+
api.onExit(() => mockCompiler.close());
|
|
69
45
|
}
|
|
70
46
|
};
|
|
71
47
|
}
|
|
72
48
|
function onProxyError(err, _req, res) {
|
|
73
|
-
console.error(
|
|
74
|
-
res.statusCode = 500;
|
|
49
|
+
console.error(ansis.red(err?.stack || err.message));
|
|
50
|
+
if (!(res instanceof Socket)) res.statusCode = 500;
|
|
75
51
|
res.end();
|
|
76
52
|
}
|
|
53
|
+
function wrapProxyOptions(options) {
|
|
54
|
+
const { on, ...rest } = options;
|
|
55
|
+
return {
|
|
56
|
+
...rest,
|
|
57
|
+
on: {
|
|
58
|
+
...on,
|
|
59
|
+
proxyReq: (proxyReq, req, ...args) => {
|
|
60
|
+
on?.proxyReq?.(proxyReq, req, ...args);
|
|
61
|
+
rewriteRequest(proxyReq, req);
|
|
62
|
+
},
|
|
63
|
+
error: on?.error || onProxyError
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
77
67
|
function updateServerProxyConfigByHttpMock(config) {
|
|
78
68
|
if (!config.server?.proxy) return;
|
|
79
|
-
if (isArray(config.server.proxy)) config.server.proxy = config.server.proxy.map((item) =>
|
|
80
|
-
|
|
81
|
-
const onProxyReq = item.onProxyReq;
|
|
82
|
-
const onError = item.onError;
|
|
83
|
-
return {
|
|
84
|
-
...item,
|
|
85
|
-
onError: onError || onProxyError,
|
|
86
|
-
onProxyReq: (proxyReq, req, ...args) => {
|
|
87
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
88
|
-
rewriteRequest(proxyReq, req);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
return item;
|
|
93
|
-
});
|
|
94
|
-
else if ("target" in config.server.proxy) {
|
|
95
|
-
const onProxyReq = config.server.proxy.onProxyReq;
|
|
96
|
-
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
97
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
98
|
-
rewriteRequest(proxyReq, req);
|
|
99
|
-
};
|
|
100
|
-
config.server.proxy.onError ??= onProxyError;
|
|
101
|
-
} else if (config.server.proxy) {
|
|
69
|
+
if (isArray(config.server.proxy)) config.server.proxy = config.server.proxy.map((item) => item.ws ? item : wrapProxyOptions(item));
|
|
70
|
+
else if (config.server.proxy) {
|
|
102
71
|
const proxy = config.server.proxy;
|
|
103
72
|
Object.keys(proxy).forEach((key) => {
|
|
104
73
|
const target = proxy[key];
|
|
105
74
|
const options = typeof target === "string" ? { target } : target;
|
|
106
75
|
if (options.ws) return;
|
|
107
|
-
|
|
108
|
-
proxy[key] = {
|
|
109
|
-
...rest,
|
|
110
|
-
onProxyReq: (proxyReq, req, ...args) => {
|
|
111
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
112
|
-
rewriteRequest(proxyReq, req);
|
|
113
|
-
},
|
|
114
|
-
onError: onError || onProxyError
|
|
115
|
-
};
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
function updateServerProxyConfigByWSMock(config, wsPrefix, port) {
|
|
120
|
-
config.server ??= {};
|
|
121
|
-
const proxy = config.server.proxy ??= {};
|
|
122
|
-
const wsTarget = `ws://localhost:${port}`;
|
|
123
|
-
const prefix = toArray(wsPrefix);
|
|
124
|
-
const has = (context) => typeof context === "string" && prefix.includes(context);
|
|
125
|
-
const used = /* @__PURE__ */ new Set();
|
|
126
|
-
function updateProxy(item) {
|
|
127
|
-
if (isArray(item.context)) item.context = item.context.filter(has);
|
|
128
|
-
else if (has(item.context)) {
|
|
129
|
-
used.add(item.context);
|
|
130
|
-
item.target = wsTarget;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
if (isArray(proxy)) {
|
|
134
|
-
for (const item of proxy) if (typeof item !== "function" && item.context && item.ws) updateProxy(item);
|
|
135
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => proxy.push({
|
|
136
|
-
context,
|
|
137
|
-
target: wsTarget
|
|
138
|
-
}));
|
|
139
|
-
} else if ("target" in proxy) {
|
|
140
|
-
if (proxy.ws) {
|
|
141
|
-
updateProxy(proxy);
|
|
142
|
-
const list = config.server.proxy = [proxy];
|
|
143
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => list.push({
|
|
144
|
-
context,
|
|
145
|
-
target: wsTarget
|
|
146
|
-
}));
|
|
147
|
-
}
|
|
148
|
-
} else {
|
|
149
|
-
Object.entries(proxy).forEach(([, opt]) => {
|
|
150
|
-
if (typeof opt !== "string" && opt.ws) updateProxy(opt);
|
|
151
|
-
});
|
|
152
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
153
|
-
proxy[context] = {
|
|
154
|
-
target: wsTarget,
|
|
155
|
-
ws: true
|
|
156
|
-
};
|
|
76
|
+
proxy[key] = wrapProxyOptions(options);
|
|
157
77
|
});
|
|
158
78
|
}
|
|
159
79
|
}
|
|
@@ -162,14 +82,11 @@ function resolveConfigProxies(config) {
|
|
|
162
82
|
const proxy = config.server.proxy ??= {};
|
|
163
83
|
const proxies = [];
|
|
164
84
|
if (isArray(proxy)) {
|
|
165
|
-
for (const item of proxy) if (
|
|
166
|
-
} else
|
|
167
|
-
if (!
|
|
168
|
-
} else Object.entries(proxy).forEach(([context, opt]) => {
|
|
169
|
-
if (typeof opt === "string" || !opt.ws) proxies.push(context);
|
|
85
|
+
for (const item of proxy) if (item.pathFilter && !item.ws) proxies.push(...toArray(item.pathFilter));
|
|
86
|
+
} else objectEntries(proxy).forEach(([pathFilter, opt]) => {
|
|
87
|
+
if (typeof opt === "string" || !opt.ws) proxies.push(pathFilter);
|
|
170
88
|
});
|
|
171
89
|
return proxies;
|
|
172
90
|
}
|
|
173
|
-
|
|
174
91
|
//#endregion
|
|
175
|
-
export { pluginMockServer };
|
|
92
|
+
export { pluginMockServer };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { g as ServerBuildOption, i as LogLevel, m as MockWebsocketItem, p as MockServerPluginOptions, s as MockHttpItem, u as MockOptions } from "./types-GT6M6WuI.js";
|
|
2
|
+
import { Compiler, RspackPluginInstance } from "@rspack/core";
|
|
3
|
+
import { Matcher } from "picomatch";
|
|
4
|
+
import Debug from "debug";
|
|
5
|
+
import EventEmitter from "node:events";
|
|
6
|
+
import { FSWatcher } from "chokidar";
|
|
7
|
+
import { CorsOptions } from "cors";
|
|
8
|
+
import * as http$1 from "node:http";
|
|
9
|
+
import http, { Server } from "node:http";
|
|
10
|
+
import { Http2SecureServer } from "node:http2";
|
|
11
|
+
|
|
12
|
+
//#region src/utils/logger.d.ts
|
|
13
|
+
interface Logger {
|
|
14
|
+
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
15
|
+
info: (msg: string, level?: boolean | LogLevel) => void;
|
|
16
|
+
warn: (msg: string, level?: boolean | LogLevel) => void;
|
|
17
|
+
error: (msg: string, level?: boolean | LogLevel) => void;
|
|
18
|
+
}
|
|
19
|
+
declare const logLevels: Record<LogLevel, number>;
|
|
20
|
+
declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/options.d.ts
|
|
23
|
+
interface ResolvedCompilerOptions {
|
|
24
|
+
alias: Record<string, false | string | (string | false)[]>;
|
|
25
|
+
proxies: (string | ((pathname: string, req: http.IncomingMessage) => boolean))[];
|
|
26
|
+
wsProxies: (string | ((pathname: string, req: http.IncomingMessage) => boolean))[];
|
|
27
|
+
plugins: RspackPluginInstance[];
|
|
28
|
+
context?: string;
|
|
29
|
+
cors: false | CorsOptions;
|
|
30
|
+
}
|
|
31
|
+
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, "build">> & ResolvedCompilerOptions & {
|
|
32
|
+
logger: Logger;
|
|
33
|
+
build: false | ServerBuildOption;
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/compiler/processData.d.ts
|
|
37
|
+
declare function processRawData(rawData: (readonly [any, string])[]): (MockHttpItem | MockWebsocketItem | MockOptions)[];
|
|
38
|
+
declare function processMockData(mockList: (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
39
|
+
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/compiler/mockCompiler.d.ts
|
|
42
|
+
declare class MockCompiler extends EventEmitter {
|
|
43
|
+
options: ResolvePluginOptions;
|
|
44
|
+
cwd: string;
|
|
45
|
+
mockWatcher!: FSWatcher;
|
|
46
|
+
entryFile!: string;
|
|
47
|
+
deps: string[];
|
|
48
|
+
isESM: boolean;
|
|
49
|
+
private _mockData;
|
|
50
|
+
private watchInfo?;
|
|
51
|
+
compiler?: Compiler | null;
|
|
52
|
+
constructor(options: ResolvePluginOptions);
|
|
53
|
+
get mockData(): Record<string, MockOptions>;
|
|
54
|
+
run(): Promise<void>;
|
|
55
|
+
close(): void;
|
|
56
|
+
updateAlias(alias: Record<string, false | string | (string | false)[]>): void;
|
|
57
|
+
updateMockEntry(): Promise<void>;
|
|
58
|
+
watchMockFiles(isMatch: Matcher): void;
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/core/mockMiddleware.d.ts
|
|
62
|
+
interface BaseMiddlewareOptions extends Pick<MockServerPluginOptions, "formidableOptions" | "cookiesOptions" | "bodyParserOptions" | "priority"> {
|
|
63
|
+
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
64
|
+
logger: Logger;
|
|
65
|
+
cors: false | CorsOptions;
|
|
66
|
+
}
|
|
67
|
+
declare function baseMiddleware(compiler: MockCompiler, {
|
|
68
|
+
formidableOptions,
|
|
69
|
+
bodyParserOptions,
|
|
70
|
+
proxies,
|
|
71
|
+
cookiesOptions,
|
|
72
|
+
logger,
|
|
73
|
+
priority,
|
|
74
|
+
cors: corsOptions
|
|
75
|
+
}: BaseMiddlewareOptions): (req: http$1.IncomingMessage, res: http$1.ServerResponse, next: (err?: any) => void) => void;
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/core/ws.d.ts
|
|
78
|
+
interface MockSocketOptions {
|
|
79
|
+
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
80
|
+
cookiesOptions: MockServerPluginOptions["cookiesOptions"];
|
|
81
|
+
logger: Logger;
|
|
82
|
+
}
|
|
83
|
+
declare function mockWebSocket(compiler: MockCompiler, httpServer: Server | Http2SecureServer | null, {
|
|
84
|
+
wsProxies: proxies,
|
|
85
|
+
cookiesOptions,
|
|
86
|
+
logger
|
|
87
|
+
}: MockSocketOptions): void;
|
|
88
|
+
//#endregion
|
|
89
|
+
export { processMockData as a, Logger as c, baseMiddleware as i, createLogger as l, mockWebSocket as n, processRawData as o, BaseMiddlewareOptions as r, sortByValidator as s, MockSocketOptions as t, logLevels as u };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,27 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
//#region src/core/baseMiddleware.d.ts
|
|
5
|
-
interface BaseMiddlewareOptions {
|
|
6
|
-
formidableOptions: MockServerPluginOptions["formidableOptions"];
|
|
7
|
-
cookiesOptions: MockServerPluginOptions["cookiesOptions"];
|
|
8
|
-
bodyParserOptions: MockServerPluginOptions["bodyParserOptions"];
|
|
9
|
-
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
10
|
-
logger: Logger;
|
|
11
|
-
priority: MockServerPluginOptions["priority"];
|
|
12
|
-
}
|
|
13
|
-
declare function baseMiddleware(compiler: MockCompiler, {
|
|
14
|
-
formidableOptions,
|
|
15
|
-
bodyParserOptions,
|
|
16
|
-
proxies,
|
|
17
|
-
cookiesOptions,
|
|
18
|
-
logger,
|
|
19
|
-
priority
|
|
20
|
-
}: BaseMiddlewareOptions): Middleware;
|
|
21
|
-
//#endregion
|
|
22
|
-
//#region src/core/transform.d.ts
|
|
23
|
-
declare function transformRawData(rawData: (readonly [any, string])[]): (MockHttpItem | MockWebsocketItem | MockOptions)[];
|
|
24
|
-
declare function transformMockData(mockList: (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
25
|
-
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
26
|
-
//#endregion
|
|
27
|
-
export { BaseMiddlewareOptions, Logger, MockSocketOptions, baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData };
|
|
1
|
+
import { a as processMockData, c as Logger, i as baseMiddleware, l as createLogger, n as mockWebSocket, o as processRawData, r as BaseMiddlewareOptions, s as sortByValidator, t as MockSocketOptions, u as logLevels } from "./server-_doRwsZm.js";
|
|
2
|
+
export { BaseMiddlewareOptions, Logger, MockSocketOptions, baseMiddleware, createLogger, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
package/dist/server.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export { baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData };
|
|
1
|
+
import { a as processRawData, d as logLevels, i as processMockData, n as baseMiddleware, o as sortByValidator, t as mockWebSocket, u as createLogger } from "./ws-BcLCWVaK.js";
|
|
2
|
+
export { baseMiddleware, createLogger, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
|
@@ -1,14 +1,135 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
3
|
import { CorsOptions } from "cors";
|
|
4
|
+
import { Options } from "co-body";
|
|
4
5
|
import formidable from "formidable";
|
|
6
|
+
import http, { IncomingMessage, ServerResponse } from "node:http";
|
|
5
7
|
import { Buffer } from "node:buffer";
|
|
6
|
-
import http from "node:http";
|
|
7
|
-
import { Readable } from "node:stream";
|
|
8
8
|
import { WebSocketServer } from "ws";
|
|
9
9
|
|
|
10
|
+
//#region src/cookies/Keygrip.d.ts
|
|
11
|
+
declare class Keygrip {
|
|
12
|
+
private algorithm!;
|
|
13
|
+
private encoding!;
|
|
14
|
+
private keys;
|
|
15
|
+
constructor(keys: string[], algorithm?: string, encoding?: crypto.BinaryToTextEncoding);
|
|
16
|
+
sign(data: string, key?: string): string;
|
|
17
|
+
index(data: string, digest: string): number;
|
|
18
|
+
verify(data: string, digest: string): boolean;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/cookies/types.d.ts
|
|
22
|
+
interface CookiesOption {
|
|
23
|
+
keys?: string[] | Keygrip;
|
|
24
|
+
secure?: boolean;
|
|
25
|
+
}
|
|
26
|
+
interface SetCookieOption {
|
|
27
|
+
/**
|
|
28
|
+
* a number representing the milliseconds from `Date.now()` for expiry
|
|
29
|
+
*
|
|
30
|
+
* 表示从 `Date.now()` 起至过期的毫秒数
|
|
31
|
+
*/
|
|
32
|
+
maxAge?: number;
|
|
33
|
+
/**
|
|
34
|
+
* a Date object indicating the cookie's expiration
|
|
35
|
+
* date (expires at the end of session by default).
|
|
36
|
+
*
|
|
37
|
+
* 一个指示cookie过期时间的 Date 对象(默认在会话结束时过期)。
|
|
38
|
+
*/
|
|
39
|
+
expires?: Date;
|
|
40
|
+
/**
|
|
41
|
+
* a string indicating the path of the cookie (`/` by default).
|
|
42
|
+
*
|
|
43
|
+
* 一个指示cookie路径的字符串(默认为 `/`)。
|
|
44
|
+
*/
|
|
45
|
+
path?: string;
|
|
46
|
+
/**
|
|
47
|
+
* a string indicating the domain of the cookie (no default).
|
|
48
|
+
*
|
|
49
|
+
* 表示 Cookie 域的字符串(无默认值)。
|
|
50
|
+
*/
|
|
51
|
+
domain?: string;
|
|
52
|
+
/**
|
|
53
|
+
* a boolean indicating whether the cookie is only to be sent
|
|
54
|
+
* over HTTPS (false by default for HTTP, true by default for HTTPS).
|
|
55
|
+
*
|
|
56
|
+
* 一个布尔值,指示该 Cookie 是否仅通过 HTTPS 发送(HTTP 默认为 false,HTTPS 默认为 true)。
|
|
57
|
+
*/
|
|
58
|
+
secure?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* a boolean indicating whether the cookie is only to be sent over HTTP(S),
|
|
61
|
+
* and not made available to client JavaScript (true by default).
|
|
62
|
+
*
|
|
63
|
+
* 一个布尔值,指示该 cookie 是否仅通过HTTP(S)发送,而不对客户端JavaScript开放(默认为true)。
|
|
64
|
+
*/
|
|
65
|
+
httpOnly?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* a boolean or string indicating whether the cookie is a "same site" cookie (false by default).
|
|
68
|
+
* This can be set to 'strict', 'lax', or true (which maps to 'strict').
|
|
69
|
+
*
|
|
70
|
+
* 一个布尔值或字符串,用于指示该cookie是否为“同站”cookie(默认为false)。
|
|
71
|
+
* 可将其设置为'strict'、'lax'或true(true会映射为'strict')。
|
|
72
|
+
*/
|
|
73
|
+
sameSite?: "strict" | "lax" | "none" | boolean;
|
|
74
|
+
/**
|
|
75
|
+
* a boolean indicating whether the cookie is to be signed (false by default).
|
|
76
|
+
* If this is true, another cookie of the same name with the .sig suffix
|
|
77
|
+
* appended will also be sent, with a 27-byte url-safe base64 SHA1 value
|
|
78
|
+
* representing the hash of cookie-name=cookie-value against the first Keygrip key.
|
|
79
|
+
* This signature key is used to detect tampering the next time a cookie is received.
|
|
80
|
+
*
|
|
81
|
+
* 一个布尔值,指示cookie是否需签名(默认为false)。
|
|
82
|
+
* 若设为true,将同时发送另一个同名但附加 `.sig` 后缀的 cookie,其值为 27 字节的URL安全型 base64 SHA1哈希值,
|
|
83
|
+
* 该哈希由cookie名称=cookie值的字符串与首个 Keygrip 密钥计算生成。
|
|
84
|
+
* 此签名密钥用于在下次接收cookie时检测数据是否被篡改。
|
|
85
|
+
*/
|
|
86
|
+
signed?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* a boolean indicating whether to overwrite previously set
|
|
89
|
+
* cookies of the same name (false by default). If this is true,
|
|
90
|
+
* all cookies set during the same request with the same
|
|
91
|
+
* name (regardless of path or domain) are filtered out of
|
|
92
|
+
* the Set-Cookie header when setting this cookie.
|
|
93
|
+
*
|
|
94
|
+
* 一个布尔值,指示是否覆盖先前设置的同名Cookie(默认为false)。
|
|
95
|
+
* 若设为true,当设置此Cookie时,在同一请求期间设置的所有同名Cookie(无论路径或域)
|
|
96
|
+
* 都将从Set-Cookie标头中过滤掉。
|
|
97
|
+
*/
|
|
98
|
+
overwrite?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* a string indicating the cookie priority.
|
|
101
|
+
* This can be set to 'low', 'medium', or 'high'.
|
|
102
|
+
*
|
|
103
|
+
* 表示Cookie优先级的字符串。可设置为'low'、'medium'或'high'。
|
|
104
|
+
*/
|
|
105
|
+
priority?: "low" | "medium" | "high";
|
|
106
|
+
/**
|
|
107
|
+
* a boolean indicating whether to partition the cookie in Chrome
|
|
108
|
+
* for the CHIPS Update (false by default). If this is true,
|
|
109
|
+
* Cookies from embedded sites will be partitioned
|
|
110
|
+
* and only readable from the same top level site from which it was created.
|
|
111
|
+
*
|
|
112
|
+
* 一个布尔值,指示是否在Chrome中为CHIPS更新对Cookie进行分区(默认为false)。
|
|
113
|
+
* 若设为true,来自嵌入式站点的Cookie将被分区,且仅可从创建它的同一顶级站点读取。
|
|
114
|
+
*/
|
|
115
|
+
partitioned?: boolean;
|
|
116
|
+
}
|
|
117
|
+
interface GetCookieOption {
|
|
118
|
+
signed: boolean;
|
|
119
|
+
}
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/cookies/Cookies.d.ts
|
|
122
|
+
declare class Cookies {
|
|
123
|
+
request: IncomingMessage;
|
|
124
|
+
response: ServerResponse<IncomingMessage>;
|
|
125
|
+
secure: boolean | undefined;
|
|
126
|
+
keys: Keygrip | undefined;
|
|
127
|
+
constructor(req: IncomingMessage, res: ServerResponse<IncomingMessage>, options?: CookiesOption);
|
|
128
|
+
set(name: string, value?: string | null, options?: SetCookieOption): this;
|
|
129
|
+
get(name: string, options?: GetCookieOption): string | void;
|
|
130
|
+
}
|
|
131
|
+
//#endregion
|
|
10
132
|
//#region src/types.d.ts
|
|
11
|
-
|
|
12
133
|
/**
|
|
13
134
|
* Configure plugin
|
|
14
135
|
*
|
|
@@ -48,6 +169,14 @@ interface MockServerPluginOptions {
|
|
|
48
169
|
*/
|
|
49
170
|
cwd?: string;
|
|
50
171
|
/**
|
|
172
|
+
* The directory to store mock files
|
|
173
|
+
*
|
|
174
|
+
* 存储 mock 文件的目录
|
|
175
|
+
*
|
|
176
|
+
* @default 'mock'
|
|
177
|
+
*/
|
|
178
|
+
dir?: string;
|
|
179
|
+
/**
|
|
51
180
|
* glob string matching mock includes files
|
|
52
181
|
*
|
|
53
182
|
* glob 字符串匹配 mock 包含的文件
|
|
@@ -97,7 +226,7 @@ interface MockServerPluginOptions {
|
|
|
97
226
|
* cookies options
|
|
98
227
|
* @see [cookies](https://github.com/pillarjs/cookies#new-cookiesrequest-response--options)
|
|
99
228
|
*/
|
|
100
|
-
cookiesOptions?:
|
|
229
|
+
cookiesOptions?: CookiesOption;
|
|
101
230
|
/**
|
|
102
231
|
* Configure to `co-body`
|
|
103
232
|
*
|
|
@@ -278,7 +407,7 @@ interface ExtraRequest {
|
|
|
278
407
|
* 获取 请求中携带的 cookie
|
|
279
408
|
* @see [cookies](https://github.com/pillarjs/cookies#cookiesgetname--options)
|
|
280
409
|
*/
|
|
281
|
-
getCookie:
|
|
410
|
+
getCookie: Cookies["get"];
|
|
282
411
|
}
|
|
283
412
|
type MockRequest = http.IncomingMessage & ExtraRequest;
|
|
284
413
|
type MockResponse = http.ServerResponse<http.IncomingMessage> & {
|
|
@@ -288,11 +417,11 @@ type MockResponse = http.ServerResponse<http.IncomingMessage> & {
|
|
|
288
417
|
* 向请求响应中设置 cookie
|
|
289
418
|
* @see [cookies](https://github.com/pillarjs/cookies#cookiessetname--values--options)
|
|
290
419
|
*/
|
|
291
|
-
setCookie:
|
|
420
|
+
setCookie: Cookies["set"];
|
|
292
421
|
};
|
|
293
422
|
type ResponseBodyFn = (request: MockRequest) => ResponseBody | Promise<ResponseBody>;
|
|
294
423
|
type ResponseHeaderFn = (request: MockRequest) => Headers | Promise<Headers>;
|
|
295
|
-
type CookieValue = string | [string,
|
|
424
|
+
type CookieValue = string | [string, SetCookieOption];
|
|
296
425
|
type ResponseCookies = Record<string, CookieValue>;
|
|
297
426
|
type ResponseCookiesFn = (request: MockRequest) => ResponseCookies | Promise<ResponseCookies>;
|
|
298
427
|
interface MockBaseItem {
|
|
@@ -569,4 +698,4 @@ type FormidableFile = formidable.File | formidable.File[];
|
|
|
569
698
|
type LogType = "info" | "warn" | "error" | "debug";
|
|
570
699
|
type LogLevel = LogType | "silent";
|
|
571
700
|
//#endregion
|
|
572
|
-
export {
|
|
701
|
+
export { WebSocketSetupContext as _, LogType as a, MockMatchPriority as c, MockRequest as d, MockResponse as f, ServerBuildOption as g, ResponseBody as h, LogLevel as i, MockMatchSpecialPriority as l, MockWebsocketItem as m, ExtraRequest as n, Method as o, MockServerPluginOptions as p, FormidableFile as r, MockHttpItem as s, BodyParserOptions as t, MockOptions as u };
|