rspack-plugin-mock 1.0.1 → 1.2.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/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/helper.cjs +124 -113
- package/dist/helper.d.cts +92 -104
- package/dist/helper.d.ts +92 -104
- package/dist/helper.js +120 -113
- package/dist/index.cjs +82 -99
- package/dist/index.d.cts +10 -22
- package/dist/index.d.ts +10 -22
- package/dist/index.js +74 -97
- package/dist/json5-loader.cjs +30 -15
- package/dist/logger-C0V8Cvvd.cjs +800 -0
- package/dist/logger-C48_LmdS.js +710 -0
- package/dist/mockWebsocket-DkVHpZCx.d.cts +85 -0
- package/dist/mockWebsocket-qLVAe-RI.d.ts +85 -0
- package/dist/resolvePluginOptions-Da5uqlBx.cjs +506 -0
- package/dist/resolvePluginOptions-DlkIkykz.js +476 -0
- package/dist/rsbuild.cjs +164 -190
- package/dist/rsbuild.d.cts +5 -13
- package/dist/rsbuild.d.ts +5 -13
- package/dist/rsbuild.js +161 -188
- package/dist/server.cjs +9 -18
- package/dist/server.d.cts +21 -25
- package/dist/server.d.ts +21 -25
- package/dist/server.js +3 -18
- package/dist/types-6lajtJPx.d.cts +572 -0
- package/dist/types-DPzh7nJq.d.ts +572 -0
- package/package.json +28 -28
- package/dist/chunk-HTVJXQRM.cjs +0 -906
- package/dist/chunk-HV5L72CY.js +0 -557
- package/dist/chunk-M7F5AAOF.cjs +0 -557
- package/dist/chunk-OGWV5ZGG.js +0 -906
- package/dist/mockWebsocket-DBgZBsdo.d.ts +0 -76
- package/dist/mockWebsocket-Ki_cShTv.d.cts +0 -76
- package/dist/types-Aw0AciTG.d.cts +0 -570
- package/dist/types-Aw0AciTG.d.ts +0 -570
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
|
|
25
|
+
Object.defineProperty(exports, '__toESM', {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return __toESM;
|
|
29
|
+
}
|
|
30
|
+
});
|
package/dist/helper.cjs
CHANGED
|
@@ -1,129 +1,140 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const __pengzhanbo_utils = require_chunk.__toESM(require("@pengzhanbo/utils"));
|
|
3
|
+
const node_stream = require_chunk.__toESM(require("node:stream"));
|
|
4
|
+
|
|
5
|
+
//#region src/core/defineMock.ts
|
|
3
6
|
function defineMock(config) {
|
|
4
|
-
|
|
7
|
+
return config;
|
|
5
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Return a custom defineMock function to support preprocessing of mock config.
|
|
11
|
+
*
|
|
12
|
+
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
13
|
+
* @param transformer preprocessing function
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const definePostMock = createDefineMock((mock) => {
|
|
17
|
+
* mock.url = '/api/post/' + mock.url
|
|
18
|
+
* })
|
|
19
|
+
* export default definePostMock({
|
|
20
|
+
* url: 'list',
|
|
21
|
+
* body: [{ title: '1' }, { title: '2' }],
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
6
25
|
function createDefineMock(transformer) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
14
|
-
return define;
|
|
26
|
+
const define = (config) => {
|
|
27
|
+
if ((0, __pengzhanbo_utils.isArray)(config)) config = config.map((item) => transformer(item) || item);
|
|
28
|
+
else config = transformer(config) || config;
|
|
29
|
+
return config;
|
|
30
|
+
};
|
|
31
|
+
return define;
|
|
15
32
|
}
|
|
16
33
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/core/defineMockData.ts
|
|
36
|
+
const mockDataCache = /* @__PURE__ */ new Map();
|
|
37
|
+
const responseCache = /* @__PURE__ */ new WeakMap();
|
|
38
|
+
const staleInterval = 70;
|
|
22
39
|
var CacheImpl = class {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
}
|
|
40
|
+
value;
|
|
41
|
+
#initialValue;
|
|
42
|
+
#lastUpdate;
|
|
43
|
+
constructor(value) {
|
|
44
|
+
this.value = value;
|
|
45
|
+
this.#initialValue = (0, __pengzhanbo_utils.deepClone)(value);
|
|
46
|
+
this.#lastUpdate = Date.now();
|
|
47
|
+
}
|
|
48
|
+
hotUpdate(value) {
|
|
49
|
+
if (Date.now() - this.#lastUpdate < staleInterval) return;
|
|
50
|
+
if (!(0, __pengzhanbo_utils.deepEqual)(value, this.#initialValue)) {
|
|
51
|
+
this.value = value;
|
|
52
|
+
this.#initialValue = (0, __pengzhanbo_utils.deepClone)(value);
|
|
53
|
+
this.#lastUpdate = Date.now();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
41
56
|
};
|
|
42
57
|
function defineMockData(key, initialData) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
set(val) {
|
|
62
|
-
cache.value = val;
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
responseCache.set(cache, res);
|
|
66
|
-
return res;
|
|
58
|
+
if (!mockDataCache.has(key)) mockDataCache.set(key, new CacheImpl(initialData));
|
|
59
|
+
const cache = mockDataCache.get(key);
|
|
60
|
+
cache.hotUpdate(initialData);
|
|
61
|
+
if (responseCache.has(cache)) return responseCache.get(cache);
|
|
62
|
+
const res = [() => cache.value, (val) => {
|
|
63
|
+
if ((0, __pengzhanbo_utils.isFunction)(val)) val = val(cache.value) ?? cache.value;
|
|
64
|
+
cache.value = val;
|
|
65
|
+
}];
|
|
66
|
+
Object.defineProperty(res, "value", {
|
|
67
|
+
get() {
|
|
68
|
+
return cache.value;
|
|
69
|
+
},
|
|
70
|
+
set(val) {
|
|
71
|
+
cache.value = val;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
responseCache.set(cache, res);
|
|
75
|
+
return res;
|
|
67
76
|
}
|
|
68
77
|
|
|
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
|
-
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/core/sse.ts
|
|
80
|
+
/**
|
|
81
|
+
* Transforms "messages" to W3C event stream content.
|
|
82
|
+
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
83
|
+
* A message is an object with one or more of the following properties:
|
|
84
|
+
* - data (String or object, which gets turned into JSON)
|
|
85
|
+
* - event
|
|
86
|
+
* - id
|
|
87
|
+
* - retry
|
|
88
|
+
* - comment
|
|
89
|
+
*
|
|
90
|
+
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
91
|
+
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
92
|
+
*/
|
|
93
|
+
var SSEStream = class extends node_stream.Transform {
|
|
94
|
+
constructor(req) {
|
|
95
|
+
super({ objectMode: true });
|
|
96
|
+
req.socket.setKeepAlive(true);
|
|
97
|
+
req.socket.setNoDelay(true);
|
|
98
|
+
req.socket.setTimeout(0);
|
|
99
|
+
}
|
|
100
|
+
pipe(destination, options) {
|
|
101
|
+
if (destination.writeHead) {
|
|
102
|
+
destination.writeHead(200, {
|
|
103
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
104
|
+
"Transfer-Encoding": "identity",
|
|
105
|
+
"Cache-Control": "no-cache",
|
|
106
|
+
"Connection": "keep-alive"
|
|
107
|
+
});
|
|
108
|
+
destination.flushHeaders?.();
|
|
109
|
+
}
|
|
110
|
+
destination.write(":ok\n\n");
|
|
111
|
+
return super.pipe(destination, options);
|
|
112
|
+
}
|
|
113
|
+
_transform(message, encoding, callback) {
|
|
114
|
+
if (message.comment) this.push(`: ${message.comment}\n`);
|
|
115
|
+
if (message.event) this.push(`event: ${message.event}\n`);
|
|
116
|
+
if (message.id) this.push(`id: ${message.id}\n`);
|
|
117
|
+
if (message.retry) this.push(`retry: ${message.retry}\n`);
|
|
118
|
+
if (message.data) this.push(dataString(message.data));
|
|
119
|
+
this.push("\n");
|
|
120
|
+
callback();
|
|
121
|
+
}
|
|
122
|
+
write(message, ...args) {
|
|
123
|
+
return super.write(message, ...args);
|
|
124
|
+
}
|
|
112
125
|
};
|
|
113
126
|
function dataString(data) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return data.split(/\r\n|\r|\n/).map((line) => `data: ${line}
|
|
117
|
-
`).join("");
|
|
127
|
+
if (typeof data === "object") return dataString(JSON.stringify(data));
|
|
128
|
+
return data.split(/\r\n|\r|\n/).map((line) => `data: ${line}\n`).join("");
|
|
118
129
|
}
|
|
119
130
|
function createSSEStream(req, res) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
131
|
+
const sse = new SSEStream(req);
|
|
132
|
+
sse.pipe(res);
|
|
133
|
+
return sse;
|
|
123
134
|
}
|
|
124
135
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
exports.
|
|
136
|
+
//#endregion
|
|
137
|
+
exports.createDefineMock = createDefineMock;
|
|
138
|
+
exports.createSSEStream = createSSEStream;
|
|
139
|
+
exports.defineMock = defineMock;
|
|
140
|
+
exports.defineMockData = defineMockData;
|
package/dist/helper.d.cts
CHANGED
|
@@ -1,122 +1,110 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import 'cookies';
|
|
7
|
-
import 'cors';
|
|
8
|
-
import 'formidable';
|
|
9
|
-
import 'node:buffer';
|
|
10
|
-
import 'ws';
|
|
1
|
+
import { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext } from "./types-6lajtJPx.cjs";
|
|
2
|
+
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http";
|
|
3
|
+
import { Transform } from "node:stream";
|
|
4
|
+
|
|
5
|
+
//#region src/core/defineMock.d.ts
|
|
11
6
|
|
|
12
7
|
/**
|
|
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
|
-
|
|
8
|
+
* mock config Type helper
|
|
9
|
+
*
|
|
10
|
+
* mock配置 类型帮助函数
|
|
11
|
+
* @param config see config docs:
|
|
12
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/en/guide/mock-config en-US DOC} |
|
|
13
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config zh-CN DOC}
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* Mock Http Request
|
|
17
|
+
* ```ts
|
|
18
|
+
* export default defineMock({
|
|
19
|
+
* url: '/api/example',
|
|
20
|
+
* method: ['GET', 'POST'],
|
|
21
|
+
* body: { a: 1 },
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
* ```ts
|
|
25
|
+
* export default defineMock({
|
|
26
|
+
* url: '/api/example',
|
|
27
|
+
* method: 'GET',
|
|
28
|
+
* body: ({ query }) => ({ a: 1, b: query.b }),
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
* @example
|
|
32
|
+
* Mock WebSocket
|
|
33
|
+
* ```ts
|
|
34
|
+
* export default defineMock({
|
|
35
|
+
* url: '/socket.io',
|
|
36
|
+
* ws: true,
|
|
37
|
+
* setup(wss) {
|
|
38
|
+
* wss.on('connection', (ws) => {
|
|
39
|
+
* ws.on('message', (rawData) => console.log(rawData))
|
|
40
|
+
* ws.send('data')
|
|
41
|
+
* })
|
|
42
|
+
* },
|
|
43
|
+
* })
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
51
46
|
declare function defineMock(config: MockHttpItem): MockHttpItem;
|
|
52
47
|
declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
|
|
53
48
|
declare function defineMock(config: MockOptions): MockOptions;
|
|
54
49
|
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
50
|
+
* Return a custom defineMock function to support preprocessing of mock config.
|
|
51
|
+
*
|
|
52
|
+
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
53
|
+
* @param transformer preprocessing function
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const definePostMock = createDefineMock((mock) => {
|
|
57
|
+
* mock.url = '/api/post/' + mock.url
|
|
58
|
+
* })
|
|
59
|
+
* export default definePostMock({
|
|
60
|
+
* url: 'list',
|
|
61
|
+
* body: [{ title: '1' }, { title: '2' }],
|
|
62
|
+
* })
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
70
65
|
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
*/
|
|
76
|
-
() => T,
|
|
77
|
-
/**
|
|
78
|
-
* setter
|
|
79
|
-
*/
|
|
80
|
-
(val: T | ((val: T) => T | void)) => void
|
|
81
|
-
] & {
|
|
82
|
-
value: T;
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/core/defineMockData.d.ts
|
|
68
|
+
type MockData<T = any> = readonly [() => T, (val: T | ((val: T) => T | void)) => void] & {
|
|
69
|
+
value: T;
|
|
83
70
|
};
|
|
84
71
|
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
|
|
85
|
-
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/core/sse.d.ts
|
|
86
74
|
interface SSEMessage {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
75
|
+
data?: string | object;
|
|
76
|
+
comment?: string;
|
|
77
|
+
event?: string;
|
|
78
|
+
id?: string;
|
|
79
|
+
retry?: number;
|
|
92
80
|
}
|
|
93
81
|
interface WriteHeaders {
|
|
94
|
-
|
|
95
|
-
|
|
82
|
+
writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
|
|
83
|
+
flushHeaders?: () => void;
|
|
96
84
|
}
|
|
97
85
|
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
98
86
|
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
87
|
+
* Transforms "messages" to W3C event stream content.
|
|
88
|
+
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
89
|
+
* A message is an object with one or more of the following properties:
|
|
90
|
+
* - data (String or object, which gets turned into JSON)
|
|
91
|
+
* - event
|
|
92
|
+
* - id
|
|
93
|
+
* - retry
|
|
94
|
+
* - comment
|
|
95
|
+
*
|
|
96
|
+
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
97
|
+
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
98
|
+
*/
|
|
111
99
|
declare class SSEStream extends Transform {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
100
|
+
constructor(req: IncomingMessage);
|
|
101
|
+
pipe<T extends HeaderStream>(destination: T, options?: {
|
|
102
|
+
end?: boolean;
|
|
103
|
+
}): T;
|
|
104
|
+
_transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
|
|
105
|
+
write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
|
|
106
|
+
write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
|
|
119
107
|
}
|
|
120
108
|
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
121
|
-
|
|
122
|
-
export {
|
|
109
|
+
//#endregion
|
|
110
|
+
export { BodyParserOptions, ExtraRequest, FormidableFile, HeaderStream, LogLevel, LogType, Method, MockData, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, SSEMessage, ServerBuildOption, WebSocketSetupContext, createDefineMock, createSSEStream, defineMock, defineMockData };
|