rspack-plugin-mock 1.1.0 → 1.3.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 +10 -2
- package/README.zh-CN.md +13 -5
- package/dist/helper.d.mts +126 -0
- package/dist/helper.mjs +4 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.mjs +1 -0
- package/dist/json5-loader.cjs +1 -17
- package/dist/options-BUfaThYe.mjs +43 -0
- package/dist/rsbuild.d.mts +7 -0
- package/dist/rsbuild.mjs +1 -0
- package/dist/server-4ETytB7L.d.mts +89 -0
- package/dist/server.d.mts +3 -0
- package/dist/server.mjs +1 -0
- package/dist/types-Bt_OTa7I.d.mts +701 -0
- package/dist/ws-BM06pHhL.mjs +1 -0
- package/package.json +46 -57
- 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/helper.cjs +0 -129
- package/dist/helper.d.cts +0 -122
- package/dist/helper.d.ts +0 -122
- package/dist/helper.js +0 -129
- package/dist/index.cjs +0 -103
- package/dist/index.d.cts +0 -25
- package/dist/index.d.ts +0 -25
- package/dist/index.js +0 -103
- package/dist/mockWebsocket-DBgZBsdo.d.ts +0 -76
- package/dist/mockWebsocket-Ki_cShTv.d.cts +0 -76
- package/dist/rsbuild.cjs +0 -202
- package/dist/rsbuild.d.cts +0 -15
- package/dist/rsbuild.d.ts +0 -15
- package/dist/rsbuild.js +0 -202
- package/dist/server.cjs +0 -18
- package/dist/server.d.cts +0 -31
- package/dist/server.d.ts +0 -31
- package/dist/server.js +0 -18
- package/dist/types-Aw0AciTG.d.cts +0 -570
- package/dist/types-Aw0AciTG.d.ts +0 -570
package/dist/helper.cjs
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/core/defineMock.ts
|
|
2
|
-
var _utils = require('@pengzhanbo/utils');
|
|
3
|
-
function defineMock(config) {
|
|
4
|
-
return config;
|
|
5
|
-
}
|
|
6
|
-
function createDefineMock(transformer) {
|
|
7
|
-
const define = (config) => {
|
|
8
|
-
if (_utils.isArray.call(void 0, config))
|
|
9
|
-
config = config.map((item) => transformer(item) || item);
|
|
10
|
-
else
|
|
11
|
-
config = transformer(config) || config;
|
|
12
|
-
return config;
|
|
13
|
-
};
|
|
14
|
-
return define;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// src/core/defineMockData.ts
|
|
18
|
-
|
|
19
|
-
var mockDataCache = /* @__PURE__ */ new Map();
|
|
20
|
-
var responseCache = /* @__PURE__ */ new WeakMap();
|
|
21
|
-
var staleInterval = 70;
|
|
22
|
-
var CacheImpl = class {
|
|
23
|
-
|
|
24
|
-
// 初始化数据的备份,用于 判断 传入的初始化数据是否发生变更
|
|
25
|
-
#initialValue;
|
|
26
|
-
#lastUpdate;
|
|
27
|
-
constructor(value) {
|
|
28
|
-
this.value = value;
|
|
29
|
-
this.#initialValue = _utils.deepClone.call(void 0, value);
|
|
30
|
-
this.#lastUpdate = Date.now();
|
|
31
|
-
}
|
|
32
|
-
hotUpdate(value) {
|
|
33
|
-
if (Date.now() - this.#lastUpdate < staleInterval)
|
|
34
|
-
return;
|
|
35
|
-
if (!_utils.deepEqual.call(void 0, value, this.#initialValue)) {
|
|
36
|
-
this.value = value;
|
|
37
|
-
this.#initialValue = _utils.deepClone.call(void 0, value);
|
|
38
|
-
this.#lastUpdate = Date.now();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
function defineMockData(key, initialData) {
|
|
43
|
-
if (!mockDataCache.has(key))
|
|
44
|
-
mockDataCache.set(key, new CacheImpl(initialData));
|
|
45
|
-
const cache = mockDataCache.get(key);
|
|
46
|
-
cache.hotUpdate(initialData);
|
|
47
|
-
if (responseCache.has(cache))
|
|
48
|
-
return responseCache.get(cache);
|
|
49
|
-
const res = [
|
|
50
|
-
() => cache.value,
|
|
51
|
-
(val) => {
|
|
52
|
-
if (_utils.isFunction.call(void 0, val))
|
|
53
|
-
val = _nullishCoalesce(val(cache.value), () => ( cache.value));
|
|
54
|
-
cache.value = val;
|
|
55
|
-
}
|
|
56
|
-
];
|
|
57
|
-
Object.defineProperty(res, "value", {
|
|
58
|
-
get() {
|
|
59
|
-
return cache.value;
|
|
60
|
-
},
|
|
61
|
-
set(val) {
|
|
62
|
-
cache.value = val;
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
responseCache.set(cache, res);
|
|
66
|
-
return res;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// src/core/sse.ts
|
|
70
|
-
var _stream = require('stream');
|
|
71
|
-
var SSEStream = class extends _stream.Transform {
|
|
72
|
-
constructor(req) {
|
|
73
|
-
super({ objectMode: true });
|
|
74
|
-
req.socket.setKeepAlive(true);
|
|
75
|
-
req.socket.setNoDelay(true);
|
|
76
|
-
req.socket.setTimeout(0);
|
|
77
|
-
}
|
|
78
|
-
pipe(destination, options) {
|
|
79
|
-
if (destination.writeHead) {
|
|
80
|
-
destination.writeHead(200, {
|
|
81
|
-
"Content-Type": "text/event-stream; charset=utf-8",
|
|
82
|
-
"Transfer-Encoding": "identity",
|
|
83
|
-
"Cache-Control": "no-cache",
|
|
84
|
-
"Connection": "keep-alive"
|
|
85
|
-
});
|
|
86
|
-
_optionalChain([destination, 'access', _ => _.flushHeaders, 'optionalCall', _2 => _2()]);
|
|
87
|
-
}
|
|
88
|
-
destination.write(":ok\n\n");
|
|
89
|
-
return super.pipe(destination, options);
|
|
90
|
-
}
|
|
91
|
-
_transform(message, encoding, callback) {
|
|
92
|
-
if (message.comment)
|
|
93
|
-
this.push(`: ${message.comment}
|
|
94
|
-
`);
|
|
95
|
-
if (message.event)
|
|
96
|
-
this.push(`event: ${message.event}
|
|
97
|
-
`);
|
|
98
|
-
if (message.id)
|
|
99
|
-
this.push(`id: ${message.id}
|
|
100
|
-
`);
|
|
101
|
-
if (message.retry)
|
|
102
|
-
this.push(`retry: ${message.retry}
|
|
103
|
-
`);
|
|
104
|
-
if (message.data)
|
|
105
|
-
this.push(dataString(message.data));
|
|
106
|
-
this.push("\n");
|
|
107
|
-
callback();
|
|
108
|
-
}
|
|
109
|
-
write(message, ...args) {
|
|
110
|
-
return super.write(message, ...args);
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
function dataString(data) {
|
|
114
|
-
if (typeof data === "object")
|
|
115
|
-
return dataString(JSON.stringify(data));
|
|
116
|
-
return data.split(/\r\n|\r|\n/).map((line) => `data: ${line}
|
|
117
|
-
`).join("");
|
|
118
|
-
}
|
|
119
|
-
function createSSEStream(req, res) {
|
|
120
|
-
const sse = new SSEStream(req);
|
|
121
|
-
sse.pipe(res);
|
|
122
|
-
return sse;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
exports.createDefineMock = createDefineMock; exports.createSSEStream = createSSEStream; exports.defineMock = defineMock; exports.defineMockData = defineMockData;
|
package/dist/helper.d.cts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.cjs';
|
|
2
|
-
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, c as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.cjs';
|
|
3
|
-
import { OutgoingHttpHeaders, IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
-
import { Transform } from 'node:stream';
|
|
5
|
-
import 'co-body';
|
|
6
|
-
import 'cookies';
|
|
7
|
-
import 'cors';
|
|
8
|
-
import 'formidable';
|
|
9
|
-
import 'node:buffer';
|
|
10
|
-
import 'ws';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* mock config Type helper
|
|
14
|
-
*
|
|
15
|
-
* mock配置 类型帮助函数
|
|
16
|
-
* @param config see config docs:
|
|
17
|
-
* {@link https://vite-plugin-mock-dev-server.netlify.app/en/guide/mock-config en-US DOC} |
|
|
18
|
-
* {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config zh-CN DOC}
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* Mock Http Request
|
|
22
|
-
* ```ts
|
|
23
|
-
* export default defineMock({
|
|
24
|
-
* url: '/api/example',
|
|
25
|
-
* method: ['GET', 'POST'],
|
|
26
|
-
* body: { a: 1 },
|
|
27
|
-
* })
|
|
28
|
-
* ```
|
|
29
|
-
* ```ts
|
|
30
|
-
* export default defineMock({
|
|
31
|
-
* url: '/api/example',
|
|
32
|
-
* method: 'GET',
|
|
33
|
-
* body: ({ query }) => ({ a: 1, b: query.b }),
|
|
34
|
-
* })
|
|
35
|
-
* ```
|
|
36
|
-
* @example
|
|
37
|
-
* Mock WebSocket
|
|
38
|
-
* ```ts
|
|
39
|
-
* export default defineMock({
|
|
40
|
-
* url: '/socket.io',
|
|
41
|
-
* ws: true,
|
|
42
|
-
* setup(wss) {
|
|
43
|
-
* wss.on('connection', (ws) => {
|
|
44
|
-
* ws.on('message', (rawData) => console.log(rawData))
|
|
45
|
-
* ws.send('data')
|
|
46
|
-
* })
|
|
47
|
-
* },
|
|
48
|
-
* })
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
declare function defineMock(config: MockHttpItem): MockHttpItem;
|
|
52
|
-
declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
|
|
53
|
-
declare function defineMock(config: MockOptions): MockOptions;
|
|
54
|
-
/**
|
|
55
|
-
* Return a custom defineMock function to support preprocessing of mock config.
|
|
56
|
-
*
|
|
57
|
-
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
58
|
-
* @param transformer preprocessing function
|
|
59
|
-
* @example
|
|
60
|
-
* ```ts
|
|
61
|
-
* const definePostMock = createDefineMock((mock) => {
|
|
62
|
-
* mock.url = '/api/post/' + mock.url
|
|
63
|
-
* })
|
|
64
|
-
* export default definePostMock({
|
|
65
|
-
* url: 'list',
|
|
66
|
-
* body: [{ title: '1' }, { title: '2' }],
|
|
67
|
-
* })
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
71
|
-
|
|
72
|
-
type MockData<T = any> = readonly [
|
|
73
|
-
/**
|
|
74
|
-
* getter
|
|
75
|
-
*/
|
|
76
|
-
() => T,
|
|
77
|
-
/**
|
|
78
|
-
* setter
|
|
79
|
-
*/
|
|
80
|
-
(val: T | ((val: T) => T | void)) => void
|
|
81
|
-
] & {
|
|
82
|
-
value: T;
|
|
83
|
-
};
|
|
84
|
-
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
|
|
85
|
-
|
|
86
|
-
interface SSEMessage {
|
|
87
|
-
data?: string | object;
|
|
88
|
-
comment?: string;
|
|
89
|
-
event?: string;
|
|
90
|
-
id?: string;
|
|
91
|
-
retry?: number;
|
|
92
|
-
}
|
|
93
|
-
interface WriteHeaders {
|
|
94
|
-
writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
|
|
95
|
-
flushHeaders?: () => void;
|
|
96
|
-
}
|
|
97
|
-
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
98
|
-
/**
|
|
99
|
-
* Transforms "messages" to W3C event stream content.
|
|
100
|
-
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
101
|
-
* A message is an object with one or more of the following properties:
|
|
102
|
-
* - data (String or object, which gets turned into JSON)
|
|
103
|
-
* - event
|
|
104
|
-
* - id
|
|
105
|
-
* - retry
|
|
106
|
-
* - comment
|
|
107
|
-
*
|
|
108
|
-
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
109
|
-
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
110
|
-
*/
|
|
111
|
-
declare class SSEStream extends Transform {
|
|
112
|
-
constructor(req: IncomingMessage);
|
|
113
|
-
pipe<T extends HeaderStream>(destination: T, options?: {
|
|
114
|
-
end?: boolean;
|
|
115
|
-
}): T;
|
|
116
|
-
_transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
|
|
117
|
-
write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
|
|
118
|
-
write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
|
|
119
|
-
}
|
|
120
|
-
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
121
|
-
|
|
122
|
-
export { type HeaderStream, type MockData, MockHttpItem, MockOptions, MockWebsocketItem, type SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData };
|
package/dist/helper.d.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.js';
|
|
2
|
-
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, c as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.js';
|
|
3
|
-
import { OutgoingHttpHeaders, IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
-
import { Transform } from 'node:stream';
|
|
5
|
-
import 'co-body';
|
|
6
|
-
import 'cookies';
|
|
7
|
-
import 'cors';
|
|
8
|
-
import 'formidable';
|
|
9
|
-
import 'node:buffer';
|
|
10
|
-
import 'ws';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* mock config Type helper
|
|
14
|
-
*
|
|
15
|
-
* mock配置 类型帮助函数
|
|
16
|
-
* @param config see config docs:
|
|
17
|
-
* {@link https://vite-plugin-mock-dev-server.netlify.app/en/guide/mock-config en-US DOC} |
|
|
18
|
-
* {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config zh-CN DOC}
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* Mock Http Request
|
|
22
|
-
* ```ts
|
|
23
|
-
* export default defineMock({
|
|
24
|
-
* url: '/api/example',
|
|
25
|
-
* method: ['GET', 'POST'],
|
|
26
|
-
* body: { a: 1 },
|
|
27
|
-
* })
|
|
28
|
-
* ```
|
|
29
|
-
* ```ts
|
|
30
|
-
* export default defineMock({
|
|
31
|
-
* url: '/api/example',
|
|
32
|
-
* method: 'GET',
|
|
33
|
-
* body: ({ query }) => ({ a: 1, b: query.b }),
|
|
34
|
-
* })
|
|
35
|
-
* ```
|
|
36
|
-
* @example
|
|
37
|
-
* Mock WebSocket
|
|
38
|
-
* ```ts
|
|
39
|
-
* export default defineMock({
|
|
40
|
-
* url: '/socket.io',
|
|
41
|
-
* ws: true,
|
|
42
|
-
* setup(wss) {
|
|
43
|
-
* wss.on('connection', (ws) => {
|
|
44
|
-
* ws.on('message', (rawData) => console.log(rawData))
|
|
45
|
-
* ws.send('data')
|
|
46
|
-
* })
|
|
47
|
-
* },
|
|
48
|
-
* })
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
declare function defineMock(config: MockHttpItem): MockHttpItem;
|
|
52
|
-
declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
|
|
53
|
-
declare function defineMock(config: MockOptions): MockOptions;
|
|
54
|
-
/**
|
|
55
|
-
* Return a custom defineMock function to support preprocessing of mock config.
|
|
56
|
-
*
|
|
57
|
-
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
58
|
-
* @param transformer preprocessing function
|
|
59
|
-
* @example
|
|
60
|
-
* ```ts
|
|
61
|
-
* const definePostMock = createDefineMock((mock) => {
|
|
62
|
-
* mock.url = '/api/post/' + mock.url
|
|
63
|
-
* })
|
|
64
|
-
* export default definePostMock({
|
|
65
|
-
* url: 'list',
|
|
66
|
-
* body: [{ title: '1' }, { title: '2' }],
|
|
67
|
-
* })
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
71
|
-
|
|
72
|
-
type MockData<T = any> = readonly [
|
|
73
|
-
/**
|
|
74
|
-
* getter
|
|
75
|
-
*/
|
|
76
|
-
() => T,
|
|
77
|
-
/**
|
|
78
|
-
* setter
|
|
79
|
-
*/
|
|
80
|
-
(val: T | ((val: T) => T | void)) => void
|
|
81
|
-
] & {
|
|
82
|
-
value: T;
|
|
83
|
-
};
|
|
84
|
-
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
|
|
85
|
-
|
|
86
|
-
interface SSEMessage {
|
|
87
|
-
data?: string | object;
|
|
88
|
-
comment?: string;
|
|
89
|
-
event?: string;
|
|
90
|
-
id?: string;
|
|
91
|
-
retry?: number;
|
|
92
|
-
}
|
|
93
|
-
interface WriteHeaders {
|
|
94
|
-
writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
|
|
95
|
-
flushHeaders?: () => void;
|
|
96
|
-
}
|
|
97
|
-
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
98
|
-
/**
|
|
99
|
-
* Transforms "messages" to W3C event stream content.
|
|
100
|
-
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
101
|
-
* A message is an object with one or more of the following properties:
|
|
102
|
-
* - data (String or object, which gets turned into JSON)
|
|
103
|
-
* - event
|
|
104
|
-
* - id
|
|
105
|
-
* - retry
|
|
106
|
-
* - comment
|
|
107
|
-
*
|
|
108
|
-
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
109
|
-
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
110
|
-
*/
|
|
111
|
-
declare class SSEStream extends Transform {
|
|
112
|
-
constructor(req: IncomingMessage);
|
|
113
|
-
pipe<T extends HeaderStream>(destination: T, options?: {
|
|
114
|
-
end?: boolean;
|
|
115
|
-
}): T;
|
|
116
|
-
_transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
|
|
117
|
-
write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
|
|
118
|
-
write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
|
|
119
|
-
}
|
|
120
|
-
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
121
|
-
|
|
122
|
-
export { type HeaderStream, type MockData, MockHttpItem, MockOptions, MockWebsocketItem, type SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData };
|
package/dist/helper.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
// src/core/defineMock.ts
|
|
2
|
-
import { isArray } from "@pengzhanbo/utils";
|
|
3
|
-
function defineMock(config) {
|
|
4
|
-
return config;
|
|
5
|
-
}
|
|
6
|
-
function createDefineMock(transformer) {
|
|
7
|
-
const define = (config) => {
|
|
8
|
-
if (isArray(config))
|
|
9
|
-
config = config.map((item) => transformer(item) || item);
|
|
10
|
-
else
|
|
11
|
-
config = transformer(config) || config;
|
|
12
|
-
return config;
|
|
13
|
-
};
|
|
14
|
-
return define;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// src/core/defineMockData.ts
|
|
18
|
-
import { deepClone, deepEqual, isFunction } from "@pengzhanbo/utils";
|
|
19
|
-
var mockDataCache = /* @__PURE__ */ new Map();
|
|
20
|
-
var responseCache = /* @__PURE__ */ new WeakMap();
|
|
21
|
-
var staleInterval = 70;
|
|
22
|
-
var CacheImpl = class {
|
|
23
|
-
value;
|
|
24
|
-
// 初始化数据的备份,用于 判断 传入的初始化数据是否发生变更
|
|
25
|
-
#initialValue;
|
|
26
|
-
#lastUpdate;
|
|
27
|
-
constructor(value) {
|
|
28
|
-
this.value = value;
|
|
29
|
-
this.#initialValue = deepClone(value);
|
|
30
|
-
this.#lastUpdate = Date.now();
|
|
31
|
-
}
|
|
32
|
-
hotUpdate(value) {
|
|
33
|
-
if (Date.now() - this.#lastUpdate < staleInterval)
|
|
34
|
-
return;
|
|
35
|
-
if (!deepEqual(value, this.#initialValue)) {
|
|
36
|
-
this.value = value;
|
|
37
|
-
this.#initialValue = deepClone(value);
|
|
38
|
-
this.#lastUpdate = Date.now();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
function defineMockData(key, initialData) {
|
|
43
|
-
if (!mockDataCache.has(key))
|
|
44
|
-
mockDataCache.set(key, new CacheImpl(initialData));
|
|
45
|
-
const cache = mockDataCache.get(key);
|
|
46
|
-
cache.hotUpdate(initialData);
|
|
47
|
-
if (responseCache.has(cache))
|
|
48
|
-
return responseCache.get(cache);
|
|
49
|
-
const res = [
|
|
50
|
-
() => cache.value,
|
|
51
|
-
(val) => {
|
|
52
|
-
if (isFunction(val))
|
|
53
|
-
val = val(cache.value) ?? cache.value;
|
|
54
|
-
cache.value = val;
|
|
55
|
-
}
|
|
56
|
-
];
|
|
57
|
-
Object.defineProperty(res, "value", {
|
|
58
|
-
get() {
|
|
59
|
-
return cache.value;
|
|
60
|
-
},
|
|
61
|
-
set(val) {
|
|
62
|
-
cache.value = val;
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
responseCache.set(cache, res);
|
|
66
|
-
return res;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// src/core/sse.ts
|
|
70
|
-
import { Transform } from "node:stream";
|
|
71
|
-
var SSEStream = class extends Transform {
|
|
72
|
-
constructor(req) {
|
|
73
|
-
super({ objectMode: true });
|
|
74
|
-
req.socket.setKeepAlive(true);
|
|
75
|
-
req.socket.setNoDelay(true);
|
|
76
|
-
req.socket.setTimeout(0);
|
|
77
|
-
}
|
|
78
|
-
pipe(destination, options) {
|
|
79
|
-
if (destination.writeHead) {
|
|
80
|
-
destination.writeHead(200, {
|
|
81
|
-
"Content-Type": "text/event-stream; charset=utf-8",
|
|
82
|
-
"Transfer-Encoding": "identity",
|
|
83
|
-
"Cache-Control": "no-cache",
|
|
84
|
-
"Connection": "keep-alive"
|
|
85
|
-
});
|
|
86
|
-
destination.flushHeaders?.();
|
|
87
|
-
}
|
|
88
|
-
destination.write(":ok\n\n");
|
|
89
|
-
return super.pipe(destination, options);
|
|
90
|
-
}
|
|
91
|
-
_transform(message, encoding, callback) {
|
|
92
|
-
if (message.comment)
|
|
93
|
-
this.push(`: ${message.comment}
|
|
94
|
-
`);
|
|
95
|
-
if (message.event)
|
|
96
|
-
this.push(`event: ${message.event}
|
|
97
|
-
`);
|
|
98
|
-
if (message.id)
|
|
99
|
-
this.push(`id: ${message.id}
|
|
100
|
-
`);
|
|
101
|
-
if (message.retry)
|
|
102
|
-
this.push(`retry: ${message.retry}
|
|
103
|
-
`);
|
|
104
|
-
if (message.data)
|
|
105
|
-
this.push(dataString(message.data));
|
|
106
|
-
this.push("\n");
|
|
107
|
-
callback();
|
|
108
|
-
}
|
|
109
|
-
write(message, ...args) {
|
|
110
|
-
return super.write(message, ...args);
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
function dataString(data) {
|
|
114
|
-
if (typeof data === "object")
|
|
115
|
-
return dataString(JSON.stringify(data));
|
|
116
|
-
return data.split(/\r\n|\r|\n/).map((line) => `data: ${line}
|
|
117
|
-
`).join("");
|
|
118
|
-
}
|
|
119
|
-
function createSSEStream(req, res) {
|
|
120
|
-
const sse = new SSEStream(req);
|
|
121
|
-
sse.pipe(res);
|
|
122
|
-
return sse;
|
|
123
|
-
}
|
|
124
|
-
export {
|
|
125
|
-
createDefineMock,
|
|
126
|
-
createSSEStream,
|
|
127
|
-
defineMock,
|
|
128
|
-
defineMockData
|
|
129
|
-
};
|
package/dist/index.cjs
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var _chunkM7F5AAOFcjs = require('./chunk-M7F5AAOF.cjs');
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var _chunkHTVJXQRMcjs = require('./chunk-HTVJXQRM.cjs');
|
|
13
|
-
|
|
14
|
-
// src/rspack.ts
|
|
15
|
-
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
16
|
-
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
17
|
-
var _utils = require('@pengzhanbo/utils');
|
|
18
|
-
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
19
|
-
var PLUGIN_NAME = "rspack-plugin-mock";
|
|
20
|
-
var MockServerPlugin = class {
|
|
21
|
-
constructor(options = {}) {
|
|
22
|
-
this.options = options;
|
|
23
|
-
}
|
|
24
|
-
apply(compiler) {
|
|
25
|
-
const compilerOptions = compiler.options;
|
|
26
|
-
const options = resolvePluginOptions2(compiler, this.options);
|
|
27
|
-
if (_process2.default.env.NODE_ENV !== "production") {
|
|
28
|
-
const mockCompiler = _chunkM7F5AAOFcjs.createMockCompiler.call(void 0, options);
|
|
29
|
-
const mockMiddleware = _chunkM7F5AAOFcjs.createMockMiddleware.call(void 0, mockCompiler, options);
|
|
30
|
-
const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
|
|
31
|
-
const waitServerForMockWebSocket = _chunkHTVJXQRMcjs.waitingFor.call(void 0, (server) => {
|
|
32
|
-
_chunkHTVJXQRMcjs.mockWebSocket.call(void 0, mockCompiler, server, options);
|
|
33
|
-
});
|
|
34
|
-
compilerOptions.devServer = {
|
|
35
|
-
...compilerOptions.devServer,
|
|
36
|
-
setupMiddlewares: (middlewares, devServer) => {
|
|
37
|
-
middlewares = _optionalChain([setupMiddlewares, 'optionalCall', _3 => _3(middlewares, devServer)]) || middlewares;
|
|
38
|
-
const reload = () => {
|
|
39
|
-
if (_optionalChain([devServer, 'access', _4 => _4.webSocketServer, 'optionalAccess', _5 => _5.clients]))
|
|
40
|
-
devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
|
|
41
|
-
};
|
|
42
|
-
middlewares = mockMiddleware(middlewares, reload) || middlewares;
|
|
43
|
-
waitServerForMockWebSocket(() => devServer.server);
|
|
44
|
-
return middlewares;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const wsPrefix = _utils.toArray.call(void 0, options.wsPrefix);
|
|
48
|
-
if (_optionalChain([compilerOptions, 'access', _6 => _6.devServer, 'optionalAccess', _7 => _7.proxy, 'optionalAccess', _8 => _8.length])) {
|
|
49
|
-
const proxy = compilerOptions.devServer.proxy;
|
|
50
|
-
compilerOptions.devServer.proxy = proxy.filter((item) => {
|
|
51
|
-
if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
|
|
52
|
-
return !_utils.toArray.call(void 0, item.context).filter(_utils.isString).some((context) => wsPrefix.includes(context));
|
|
53
|
-
}
|
|
54
|
-
return true;
|
|
55
|
-
}).map((item) => {
|
|
56
|
-
if (typeof item !== "function" && !item.ws) {
|
|
57
|
-
const onProxyReq = item.onProxyReq;
|
|
58
|
-
item.onProxyReq = (proxyReq, req, ...args) => {
|
|
59
|
-
_optionalChain([onProxyReq, 'optionalCall', _9 => _9(proxyReq, req, ...args)]);
|
|
60
|
-
_chunkHTVJXQRMcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
return item;
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
|
|
67
|
-
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
|
|
68
|
-
} else if (options.build !== false) {
|
|
69
|
-
compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkM7F5AAOFcjs.buildMockServer.call(void 0,
|
|
70
|
-
options,
|
|
71
|
-
compilerOptions.output.path || _path2.default.resolve(_process2.default.cwd(), "dist")
|
|
72
|
-
));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
function resolvePluginOptions2(compiler, options) {
|
|
77
|
-
const compilerOptions = compiler.options;
|
|
78
|
-
const alias = _optionalChain([compilerOptions, 'access', _10 => _10.resolve, 'optionalAccess', _11 => _11.alias]) || {};
|
|
79
|
-
const context = compilerOptions.context;
|
|
80
|
-
const definePluginInstance = _optionalChain([compilerOptions, 'access', _12 => _12.plugins, 'optionalAccess', _13 => _13.find, 'call', _14 => _14(
|
|
81
|
-
(plugin) => plugin instanceof _core2.default.DefinePlugin
|
|
82
|
-
)]);
|
|
83
|
-
const proxies = (_optionalChain([compilerOptions, 'access', _15 => _15.devServer, 'optionalAccess', _16 => _16.proxy]) || []).flatMap((item) => {
|
|
84
|
-
if (typeof item !== "function" && item.context && !item.ws) {
|
|
85
|
-
return item.context;
|
|
86
|
-
}
|
|
87
|
-
return [];
|
|
88
|
-
});
|
|
89
|
-
return _chunkM7F5AAOFcjs.resolvePluginOptions.call(void 0, options, {
|
|
90
|
-
alias,
|
|
91
|
-
context,
|
|
92
|
-
plugins: _utils.toArray.call(void 0, definePluginInstance),
|
|
93
|
-
proxies
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
exports.MockCompiler = _chunkM7F5AAOFcjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunkM7F5AAOFcjs.createMockCompiler; exports.createMockMiddleware = _chunkM7F5AAOFcjs.createMockMiddleware; exports.mockWebSocket = _chunkHTVJXQRMcjs.mockWebSocket; exports.resolvePluginOptions = resolvePluginOptions2;
|
package/dist/index.d.cts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { R as ResolvePluginOptions } from './mockWebsocket-Ki_cShTv.cjs';
|
|
2
|
-
export { e as Middleware, b as MiddlewareOptions, a as MockCompiler, M as MockCompilerOptions, f as MockSocketOptions, S as Server, c as createMockCompiler, d as createMockMiddleware, m as mockWebSocket } from './mockWebsocket-Ki_cShTv.cjs';
|
|
3
|
-
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
4
|
-
import { c as MockServerPluginOptions } from './types-Aw0AciTG.cjs';
|
|
5
|
-
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, M as MockHttpItem, d as MockMatchPriority, e as MockMatchSpecialPriority, b as MockOptions, g as MockRequest, h as MockResponse, a as MockWebsocketItem, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.cjs';
|
|
6
|
-
import 'node:http';
|
|
7
|
-
import 'node:http2';
|
|
8
|
-
import 'node:fs';
|
|
9
|
-
import 'node:events';
|
|
10
|
-
import 'co-body';
|
|
11
|
-
import 'cookies';
|
|
12
|
-
import 'cors';
|
|
13
|
-
import 'formidable';
|
|
14
|
-
import 'node:buffer';
|
|
15
|
-
import 'node:stream';
|
|
16
|
-
import 'ws';
|
|
17
|
-
|
|
18
|
-
declare class MockServerPlugin implements RspackPluginInstance {
|
|
19
|
-
options: MockServerPluginOptions;
|
|
20
|
-
constructor(options?: MockServerPluginOptions);
|
|
21
|
-
apply(compiler: Compiler): void;
|
|
22
|
-
}
|
|
23
|
-
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
24
|
-
|
|
25
|
-
export { MockServerPlugin, MockServerPluginOptions, resolvePluginOptions };
|
package/dist/index.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { R as ResolvePluginOptions } from './mockWebsocket-DBgZBsdo.js';
|
|
2
|
-
export { e as Middleware, b as MiddlewareOptions, a as MockCompiler, M as MockCompilerOptions, f as MockSocketOptions, S as Server, c as createMockCompiler, d as createMockMiddleware, m as mockWebSocket } from './mockWebsocket-DBgZBsdo.js';
|
|
3
|
-
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
4
|
-
import { c as MockServerPluginOptions } from './types-Aw0AciTG.js';
|
|
5
|
-
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, M as MockHttpItem, d as MockMatchPriority, e as MockMatchSpecialPriority, b as MockOptions, g as MockRequest, h as MockResponse, a as MockWebsocketItem, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.js';
|
|
6
|
-
import 'node:http';
|
|
7
|
-
import 'node:http2';
|
|
8
|
-
import 'node:fs';
|
|
9
|
-
import 'node:events';
|
|
10
|
-
import 'co-body';
|
|
11
|
-
import 'cookies';
|
|
12
|
-
import 'cors';
|
|
13
|
-
import 'formidable';
|
|
14
|
-
import 'node:buffer';
|
|
15
|
-
import 'node:stream';
|
|
16
|
-
import 'ws';
|
|
17
|
-
|
|
18
|
-
declare class MockServerPlugin implements RspackPluginInstance {
|
|
19
|
-
options: MockServerPluginOptions;
|
|
20
|
-
constructor(options?: MockServerPluginOptions);
|
|
21
|
-
apply(compiler: Compiler): void;
|
|
22
|
-
}
|
|
23
|
-
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
24
|
-
|
|
25
|
-
export { MockServerPlugin, MockServerPluginOptions, resolvePluginOptions };
|