rspack-plugin-mock 2.0.0 → 2.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/README.md +406 -65
- package/README.zh-CN.md +406 -66
- package/dist/helper.d.ts +124 -87
- package/dist/helper.js +45 -71
- package/dist/index-BjZK24gW.d.ts +1065 -0
- package/dist/index.d.ts +9 -7
- package/dist/index.js +78 -68
- package/dist/json5-loader.mjs +2 -2
- package/dist/{ws-BcLCWVaK.js → logger-DTZcb53u.js} +519 -432
- package/dist/{options-Bd0PSZeT.js → options-C3NAM6gv.js} +59 -27
- package/dist/rsbuild.d.ts +4 -4
- package/dist/rsbuild.js +30 -42
- package/dist/server-DurHqymI.d.ts +97 -0
- package/dist/server.d.ts +2 -2
- package/dist/server.js +2 -2
- package/package.json +38 -51
- package/dist/server-_doRwsZm.d.ts +0 -89
- package/dist/types-GT6M6WuI.d.ts +0 -701
package/dist/helper.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { _ as
|
|
1
|
+
import { A as ResponseBody, C as HandleFunction, D as MockResponse, E as MockRequest, M as CookiesOption, N as GetCookieOption, O as NextFunction, P as SetCookieOption, S as ExtraRequest, T as Method, _ as RecordedMeta, a as MockWebsocketItem, b as RecordedRes, c as MockHttpItem, d as LogType, f as MockMatchPriority, g as RecordOptions, h as ServerBuildOption, i as MockOptions, j as SimpleHandleFunction, k as NextHandleFunction, l as BodyParserOptions, m as MockServerPluginOptions, n as HttpProxyPlugin, o as WebSocketSetupContext, p as MockMatchSpecialPriority, r as PathFilter, s as MockErrorConfig, t as FormidableFile, u as LogLevel, v as RecordedReq, w as Headers, x as ResolvedRecordOptions, y as RecordedRequest } from "./index-BjZK24gW.js";
|
|
2
2
|
import { Transform } from "node:stream";
|
|
3
3
|
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http";
|
|
4
|
-
|
|
5
|
-
//#region src/helper/createSSEStream.d.ts
|
|
4
|
+
//#region src/helpers/createSSEStream.d.ts
|
|
6
5
|
interface SSEMessage {
|
|
7
6
|
data?: string | object;
|
|
8
7
|
comment?: string;
|
|
@@ -16,18 +15,18 @@ interface WriteHeaders {
|
|
|
16
15
|
}
|
|
17
16
|
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
18
17
|
/**
|
|
19
|
-
* Transforms "messages" to W3C event stream content.
|
|
20
|
-
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
21
|
-
* A message is an object with one or more of the following properties:
|
|
22
|
-
* - data (String or object, which gets turned into JSON)
|
|
23
|
-
* - event
|
|
24
|
-
* - id
|
|
25
|
-
* - retry
|
|
26
|
-
* - comment
|
|
27
|
-
*
|
|
28
|
-
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
29
|
-
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
30
|
-
*/
|
|
18
|
+
* Transforms "messages" to W3C event stream content.
|
|
19
|
+
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
20
|
+
* A message is an object with one or more of the following properties:
|
|
21
|
+
* - data (String or object, which gets turned into JSON)
|
|
22
|
+
* - event
|
|
23
|
+
* - id
|
|
24
|
+
* - retry
|
|
25
|
+
* - comment
|
|
26
|
+
*
|
|
27
|
+
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
28
|
+
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
29
|
+
*/
|
|
31
30
|
declare class SSEStream extends Transform {
|
|
32
31
|
constructor(req: IncomingMessage);
|
|
33
32
|
pipe<T extends HeaderStream>(destination: T, options?: {
|
|
@@ -39,88 +38,126 @@ declare class SSEStream extends Transform {
|
|
|
39
38
|
destroy(error?: Error): this;
|
|
40
39
|
}
|
|
41
40
|
/**
|
|
42
|
-
* 创建一个 Server-sent events 写入流,用于支持模拟 EventSource
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* import { createSSEStream, defineMock } from 'vite-plugin-mock-dev-server'
|
|
47
|
-
*
|
|
48
|
-
* export default defineMock({
|
|
49
|
-
* url: '/api',
|
|
50
|
-
* response: (req, res) => {
|
|
51
|
-
* const sse = createSSEStream(req, res)
|
|
52
|
-
* sse.write({ event: 'message', data: { message: 'hello world' } })
|
|
53
|
-
* }
|
|
54
|
-
* })
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
41
|
+
* 创建一个 Server-sent events 写入流,用于支持模拟 EventSource
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { createSSEStream, defineMock } from 'vite-plugin-mock-dev-server'
|
|
46
|
+
*
|
|
47
|
+
* export default defineMock({
|
|
48
|
+
* url: '/api',
|
|
49
|
+
* response: (req, res) => {
|
|
50
|
+
* const sse = createSSEStream(req, res)
|
|
51
|
+
* sse.write({ event: 'message', data: { message: 'hello world' } })
|
|
52
|
+
* }
|
|
53
|
+
* })
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
57
56
|
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
58
57
|
//#endregion
|
|
59
|
-
//#region src/
|
|
58
|
+
//#region src/helpers/defineMock.d.ts
|
|
60
59
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* mock配置 类型帮助函数
|
|
64
|
-
* @param config see config docs:
|
|
65
|
-
* {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config en-US DOC} |
|
|
66
|
-
* {@link https://vite-plugin-mock-dev-server.netlify.app/zh/guide/mock-config zh-CN DOC}
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* Mock Http Request
|
|
70
|
-
* ```ts
|
|
71
|
-
* export default defineMock({
|
|
72
|
-
* url: '/api/example',
|
|
73
|
-
* method: ['GET', 'POST'],
|
|
74
|
-
* body: { a: 1 },
|
|
75
|
-
* })
|
|
76
|
-
* ```
|
|
77
|
-
* ```ts
|
|
78
|
-
* export default defineMock({
|
|
79
|
-
* url: '/api/example',
|
|
80
|
-
* method: 'GET',
|
|
81
|
-
* body: ({ query }) => ({ a: 1, b: query.b }),
|
|
82
|
-
* })
|
|
83
|
-
* ```
|
|
84
|
-
* @example
|
|
85
|
-
* Mock WebSocket
|
|
86
|
-
* ```ts
|
|
87
|
-
* export default defineMock({
|
|
88
|
-
* url: '/socket.io',
|
|
89
|
-
* ws: true,
|
|
90
|
-
* setup(wss) {
|
|
91
|
-
* wss.on('connection', (ws) => {
|
|
92
|
-
* ws.on('message', (rawData) => console.log(rawData))
|
|
93
|
-
* ws.send('data')
|
|
94
|
-
* })
|
|
95
|
-
* },
|
|
96
|
-
* })
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
60
|
+
* Mock config Type helper
|
|
61
|
+
*
|
|
62
|
+
* mock配置 类型帮助函数
|
|
63
|
+
* @param config see config docs:
|
|
64
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config en-US DOC} |
|
|
65
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/zh/guide/mock-config zh-CN DOC}
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* Mock Http Request
|
|
69
|
+
* ```ts
|
|
70
|
+
* export default defineMock({
|
|
71
|
+
* url: '/api/example',
|
|
72
|
+
* method: ['GET', 'POST'],
|
|
73
|
+
* body: { a: 1 },
|
|
74
|
+
* })
|
|
75
|
+
* ```
|
|
76
|
+
* ```ts
|
|
77
|
+
* export default defineMock({
|
|
78
|
+
* url: '/api/example',
|
|
79
|
+
* method: 'GET',
|
|
80
|
+
* body: ({ query }) => ({ a: 1, b: query.b }),
|
|
81
|
+
* })
|
|
82
|
+
* ```
|
|
83
|
+
* @example
|
|
84
|
+
* Mock WebSocket
|
|
85
|
+
* ```ts
|
|
86
|
+
* export default defineMock({
|
|
87
|
+
* url: '/socket.io',
|
|
88
|
+
* ws: true,
|
|
89
|
+
* setup(wss) {
|
|
90
|
+
* wss.on('connection', (ws) => {
|
|
91
|
+
* ws.on('message', (rawData) => console.log(rawData))
|
|
92
|
+
* ws.send('data')
|
|
93
|
+
* })
|
|
94
|
+
* },
|
|
95
|
+
* })
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
99
98
|
declare function defineMock(config: MockHttpItem): MockHttpItem;
|
|
100
99
|
declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
|
|
101
100
|
declare function defineMock(config: MockOptions): MockOptions;
|
|
102
101
|
/**
|
|
103
|
-
* Return a custom defineMock function to support preprocessing of mock config.
|
|
104
|
-
*
|
|
105
|
-
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
106
|
-
* @param transformer preprocessing function
|
|
107
|
-
* @example
|
|
108
|
-
* ```ts
|
|
109
|
-
* const definePostMock = createDefineMock((mock) => {
|
|
110
|
-
* mock.url = '/api/post/' + mock.url
|
|
111
|
-
* })
|
|
112
|
-
* export default definePostMock({
|
|
113
|
-
* url: 'list',
|
|
114
|
-
* body: [{ title: '1' }, { title: '2' }],
|
|
115
|
-
* })
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
102
|
+
* Return a custom defineMock function to support preprocessing of mock config.
|
|
103
|
+
*
|
|
104
|
+
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
105
|
+
* @param transformer preprocessing function
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const definePostMock = createDefineMock((mock) => {
|
|
109
|
+
* mock.url = '/api/post/' + mock.url
|
|
110
|
+
* })
|
|
111
|
+
* export default definePostMock({
|
|
112
|
+
* url: 'list',
|
|
113
|
+
* body: [{ title: '1' }, { title: '2' }],
|
|
114
|
+
* })
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
118
117
|
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
119
118
|
//#endregion
|
|
120
|
-
//#region src/
|
|
119
|
+
//#region src/helpers/defineMockData.d.ts
|
|
120
|
+
/**
|
|
121
|
+
* Options for defineMockData
|
|
122
|
+
*
|
|
123
|
+
* defineMockData 的选项
|
|
124
|
+
*/
|
|
125
|
+
interface DefineMockDataOptions {
|
|
126
|
+
/**
|
|
127
|
+
* Whether to persist the data value on HMR (Hot Module Replacement).
|
|
128
|
+
*
|
|
129
|
+
* 热更新时是否保持数据值
|
|
130
|
+
*
|
|
131
|
+
* @default false
|
|
132
|
+
*/
|
|
133
|
+
persistOnHMR?: boolean;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Mock data type with getter, setter, and value property
|
|
137
|
+
*
|
|
138
|
+
* 带有 getter、setter 和 value 属性的 Mock 数据类型
|
|
139
|
+
*
|
|
140
|
+
* @template T - Type of mock data / Mock 数据的类型
|
|
141
|
+
*/
|
|
121
142
|
type MockData<T = any> = readonly [() => T, (val: T | ((val: T) => T | void)) => void] & {
|
|
143
|
+
/**
|
|
144
|
+
* Current value
|
|
145
|
+
*
|
|
146
|
+
* 当前值
|
|
147
|
+
*/
|
|
122
148
|
value: T;
|
|
123
149
|
};
|
|
124
|
-
|
|
150
|
+
/**
|
|
151
|
+
* Define mock data with memory-based sharing mechanism
|
|
152
|
+
*
|
|
153
|
+
* 定义带有基于内存的共享机制的 Mock 数据
|
|
154
|
+
*
|
|
155
|
+
* @template T - Type of mock data / Mock 数据的类型
|
|
156
|
+
* @param key - Unique key for mock data / Mock 数据的唯一键
|
|
157
|
+
* @param initialData - Initial data value / 初始数据值
|
|
158
|
+
* @param options - Options / 选项
|
|
159
|
+
* @returns MockData object with getter, setter, and value property / 带有 getter、setter 和 value 属性的 MockData 对象
|
|
160
|
+
*/
|
|
161
|
+
declare function defineMockData<T = any>(key: string, initialData: T, options?: DefineMockDataOptions): MockData<T>;
|
|
125
162
|
//#endregion
|
|
126
|
-
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 };
|
|
163
|
+
export { type BodyParserOptions, type CookiesOption, DefineMockDataOptions, type ExtraRequest, type FormidableFile, type GetCookieOption, type HandleFunction, HeaderStream, type Headers, type HttpProxyPlugin, type LogLevel, type LogType, type Method, MockData, type MockErrorConfig, type MockHttpItem, type MockMatchPriority, type MockMatchSpecialPriority, type MockOptions, type MockRequest, type MockResponse, type MockServerPluginOptions, type MockWebsocketItem, type NextFunction, type NextHandleFunction, type PathFilter, type RecordOptions, type RecordedMeta, type RecordedReq, type RecordedRequest, type RecordedRes, type ResolvedRecordOptions, type ResponseBody, SSEMessage, type ServerBuildOption, type SetCookieOption, type SimpleHandleFunction, type WebSocketSetupContext, createDefineMock, createSSEStream, defineMock, defineMockData };
|
package/dist/helper.js
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
|
+
import { deepClone, isArray, isFunction } from "@pengzhanbo/utils";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
1
3
|
import { Transform } from "node:stream";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Transforms "messages" to W3C event stream content.
|
|
6
|
-
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
7
|
-
* A message is an object with one or more of the following properties:
|
|
8
|
-
* - data (String or object, which gets turned into JSON)
|
|
9
|
-
* - event
|
|
10
|
-
* - id
|
|
11
|
-
* - retry
|
|
12
|
-
* - comment
|
|
13
|
-
*
|
|
14
|
-
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
15
|
-
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
16
|
-
*/
|
|
4
|
+
|
|
5
|
+
|
|
17
6
|
var SSEStream = class extends Transform {
|
|
18
7
|
constructor(req) {
|
|
19
8
|
super({ objectMode: true });
|
|
@@ -59,48 +48,18 @@ function dataString(data) {
|
|
|
59
48
|
if (typeof data === "object") return dataString(JSON.stringify(data));
|
|
60
49
|
return data.split(/\r\n|\r|\n/).map((line) => `data: ${line}\n`).join("");
|
|
61
50
|
}
|
|
62
|
-
|
|
63
|
-
* 创建一个 Server-sent events 写入流,用于支持模拟 EventSource
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```ts
|
|
67
|
-
* import { createSSEStream, defineMock } from 'vite-plugin-mock-dev-server'
|
|
68
|
-
*
|
|
69
|
-
* export default defineMock({
|
|
70
|
-
* url: '/api',
|
|
71
|
-
* response: (req, res) => {
|
|
72
|
-
* const sse = createSSEStream(req, res)
|
|
73
|
-
* sse.write({ event: 'message', data: { message: 'hello world' } })
|
|
74
|
-
* }
|
|
75
|
-
* })
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
51
|
+
|
|
78
52
|
function createSSEStream(req, res) {
|
|
79
53
|
const sse = new SSEStream(req);
|
|
80
54
|
sse.pipe(res);
|
|
81
55
|
return sse;
|
|
82
56
|
}
|
|
83
|
-
|
|
84
|
-
|
|
57
|
+
|
|
58
|
+
|
|
85
59
|
function defineMock(config) {
|
|
86
60
|
return config;
|
|
87
61
|
}
|
|
88
|
-
|
|
89
|
-
* Return a custom defineMock function to support preprocessing of mock config.
|
|
90
|
-
*
|
|
91
|
-
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
92
|
-
* @param transformer preprocessing function
|
|
93
|
-
* @example
|
|
94
|
-
* ```ts
|
|
95
|
-
* const definePostMock = createDefineMock((mock) => {
|
|
96
|
-
* mock.url = '/api/post/' + mock.url
|
|
97
|
-
* })
|
|
98
|
-
* export default definePostMock({
|
|
99
|
-
* url: 'list',
|
|
100
|
-
* body: [{ title: '1' }, { title: '2' }],
|
|
101
|
-
* })
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
62
|
+
|
|
104
63
|
function createDefineMock(transformer) {
|
|
105
64
|
const define = (config) => {
|
|
106
65
|
if (isArray(config)) config = config.map((item) => transformer(item) || item);
|
|
@@ -109,48 +68,60 @@ function createDefineMock(transformer) {
|
|
|
109
68
|
};
|
|
110
69
|
return define;
|
|
111
70
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
* 为此,插件提供了一种基于 memory 的数据共享机制。
|
|
121
|
-
*/
|
|
122
|
-
const mockDataCache = /* @__PURE__ */ new Map();
|
|
123
|
-
const responseCache = /* @__PURE__ */ new WeakMap();
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
const mockDataCache = new Map();
|
|
76
|
+
|
|
77
|
+
const responseCache = new WeakMap();
|
|
78
|
+
|
|
124
79
|
const staleInterval = 70;
|
|
80
|
+
|
|
125
81
|
var CacheImpl = class {
|
|
82
|
+
|
|
126
83
|
value;
|
|
127
|
-
|
|
84
|
+
|
|
85
|
+
#hash;
|
|
86
|
+
|
|
128
87
|
#lastUpdate;
|
|
129
|
-
|
|
88
|
+
|
|
89
|
+
#persistOnHMR;
|
|
90
|
+
|
|
91
|
+
constructor(value, persistOnHMR) {
|
|
130
92
|
this.value = value;
|
|
131
|
-
this.#
|
|
93
|
+
this.#hash = getHash(value);
|
|
132
94
|
this.#lastUpdate = Date.now();
|
|
95
|
+
this.#persistOnHMR = persistOnHMR ?? false;
|
|
133
96
|
}
|
|
97
|
+
|
|
134
98
|
hotUpdate(value) {
|
|
99
|
+
if (this.#persistOnHMR) return;
|
|
135
100
|
if (Date.now() - this.#lastUpdate < staleInterval) return;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
this
|
|
101
|
+
const hash = getHash(value);
|
|
102
|
+
if (this.#hash !== hash) {
|
|
103
|
+
this.value = deepClone(value);
|
|
104
|
+
this.#hash = hash;
|
|
139
105
|
this.#lastUpdate = Date.now();
|
|
140
106
|
}
|
|
141
107
|
}
|
|
108
|
+
|
|
109
|
+
setPersistOnHMR(persistOnHMR) {
|
|
110
|
+
if (!this.#persistOnHMR) this.#persistOnHMR = persistOnHMR;
|
|
111
|
+
}
|
|
142
112
|
};
|
|
143
|
-
|
|
113
|
+
|
|
114
|
+
function defineMockData(key, initialData, options) {
|
|
144
115
|
let cache = mockDataCache.get(key);
|
|
145
116
|
if (!cache) {
|
|
146
|
-
const newCache = new CacheImpl(initialData);
|
|
117
|
+
const newCache = new CacheImpl(initialData, options?.persistOnHMR);
|
|
147
118
|
const existing = mockDataCache.get(key);
|
|
148
119
|
if (existing) cache = existing;
|
|
149
120
|
else {
|
|
150
121
|
mockDataCache.set(key, newCache);
|
|
151
122
|
cache = newCache;
|
|
152
123
|
}
|
|
153
|
-
}
|
|
124
|
+
} else cache.setPersistOnHMR(options?.persistOnHMR ?? false);
|
|
154
125
|
cache.hotUpdate(initialData);
|
|
155
126
|
if (responseCache.has(cache)) return responseCache.get(cache);
|
|
156
127
|
const res = [() => cache.value, (val) => {
|
|
@@ -168,5 +139,8 @@ function defineMockData(key, initialData) {
|
|
|
168
139
|
responseCache.set(cache, res);
|
|
169
140
|
return res;
|
|
170
141
|
}
|
|
171
|
-
|
|
142
|
+
function getHash(data) {
|
|
143
|
+
return createHash("sha256").update(JSON.stringify(data)).digest("hex");
|
|
144
|
+
}
|
|
145
|
+
|
|
172
146
|
export { createDefineMock, createSSEStream, defineMock, defineMockData };
|