vite-plugin-mock-dev-server 2.0.7 → 2.1.1
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/helper.d.ts +286 -0
- package/dist/helper.js +4 -0
- package/dist/{types-C8ZwTU-4.d.mts → index-HOrR1VyK.d.ts} +503 -172
- package/dist/index.d.ts +4 -0
- package/dist/index.js +48 -0
- package/dist/server-DnEpYtlk.js +2 -0
- package/dist/server-ypJ-wM5S.d.ts +270 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +1 -0
- package/dist/types.d.ts +2 -0
- package/package.json +28 -23
- package/dist/helper.d.mts +0 -126
- package/dist/helper.mjs +0 -4
- package/dist/index.d.mts +0 -9
- package/dist/index.mjs +0 -37
- package/dist/server-BEkc6AaK.mjs +0 -1
- package/dist/server-lwszq3fl.d.mts +0 -89
- package/dist/server.d.mts +0 -2
- package/dist/server.mjs +0 -1
- package/dist/types.d.mts +0 -2
- /package/dist/{types.mjs → types.js} +0 -0
package/dist/helper.d.ts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { C as MockRequest, D as GetCookieOption, E as CookiesOption, O as SetCookieOption, S as Method, T as ResponseBody, _ as RecordedRequest, a as MockErrorConfig, b as ExtraRequest, c as LogLevel, d as MockMatchSpecialPriority, f as MockServerPluginOptions, g as RecordedReq, h as RecordedMeta, i as WebSocketSetupContext, l as LogType, m as RecordOptions, n as MockOptions, o as MockHttpItem, p as ServerBuildOption, r as MockWebsocketItem, s as BodyParserOptions, t as FormidableFile, u as MockMatchPriority, v as RecordedRes, w as MockResponse, x as Headers, y as ResolvedRecordOptions } from "./index-HOrR1VyK.js";
|
|
2
|
+
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http";
|
|
3
|
+
import { Transform } from "node:stream";
|
|
4
|
+
|
|
5
|
+
//#region src/helpers/createSSEStream.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Server-sent events message interface
|
|
8
|
+
*
|
|
9
|
+
* Server-sent events 消息接口
|
|
10
|
+
*/
|
|
11
|
+
interface SSEMessage {
|
|
12
|
+
/**
|
|
13
|
+
* Message data
|
|
14
|
+
*
|
|
15
|
+
* 消息数据
|
|
16
|
+
*/
|
|
17
|
+
data?: string | object;
|
|
18
|
+
/**
|
|
19
|
+
* Comment
|
|
20
|
+
*
|
|
21
|
+
* 注释
|
|
22
|
+
*/
|
|
23
|
+
comment?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Event name
|
|
26
|
+
*
|
|
27
|
+
* 事件名称
|
|
28
|
+
*/
|
|
29
|
+
event?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Event ID
|
|
32
|
+
*
|
|
33
|
+
* 事件 ID
|
|
34
|
+
*/
|
|
35
|
+
id?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Retry interval
|
|
38
|
+
*
|
|
39
|
+
* 重试间隔
|
|
40
|
+
*/
|
|
41
|
+
retry?: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Write headers interface
|
|
45
|
+
*
|
|
46
|
+
* 写入头信息接口
|
|
47
|
+
*/
|
|
48
|
+
interface WriteHeaders {
|
|
49
|
+
/**
|
|
50
|
+
* Write HTTP headers
|
|
51
|
+
*
|
|
52
|
+
* 写入 HTTP 头信息
|
|
53
|
+
*/
|
|
54
|
+
writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
|
|
55
|
+
/**
|
|
56
|
+
* Flush headers
|
|
57
|
+
*
|
|
58
|
+
* 刷新头信息
|
|
59
|
+
*/
|
|
60
|
+
flushHeaders?: () => void;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Header stream type
|
|
64
|
+
*
|
|
65
|
+
* 头信息流类型
|
|
66
|
+
*/
|
|
67
|
+
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
68
|
+
/**
|
|
69
|
+
* Transforms "messages" to W3C event stream content.
|
|
70
|
+
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
71
|
+
* A message is an object with one or more of the following properties:
|
|
72
|
+
* - data (String or object, which gets turned into JSON)
|
|
73
|
+
* - event
|
|
74
|
+
* - id
|
|
75
|
+
* - retry
|
|
76
|
+
* - comment
|
|
77
|
+
*
|
|
78
|
+
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
79
|
+
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
80
|
+
*
|
|
81
|
+
* 将 "messages" 转换为 W3C 事件流内容。
|
|
82
|
+
* 参见 https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
83
|
+
* 消息是一个具有以下一个或多个属性的对象:
|
|
84
|
+
* - data (字符串或对象,会被转换为 JSON)
|
|
85
|
+
* - event
|
|
86
|
+
* - id
|
|
87
|
+
* - retry
|
|
88
|
+
* - comment
|
|
89
|
+
*
|
|
90
|
+
* 如果使用 HTTP 请求构造,它将优化套接字以进行流式传输。
|
|
91
|
+
* 如果此流被管道传输到 HTTP 响应,它将设置适当的头信息。
|
|
92
|
+
*/
|
|
93
|
+
declare class SSEStream extends Transform {
|
|
94
|
+
/**
|
|
95
|
+
* Constructor
|
|
96
|
+
*
|
|
97
|
+
* 构造函数
|
|
98
|
+
*
|
|
99
|
+
* @param req - HTTP request object / HTTP 请求对象
|
|
100
|
+
*/
|
|
101
|
+
constructor(req: IncomingMessage);
|
|
102
|
+
/**
|
|
103
|
+
* Pipe the stream to a destination
|
|
104
|
+
*
|
|
105
|
+
* 将流管道传输到目标
|
|
106
|
+
*
|
|
107
|
+
* @template T - Type of destination stream / 目标流的类型
|
|
108
|
+
* @param destination - Destination stream / 目标流
|
|
109
|
+
* @param options - Pipe options / 管道选项
|
|
110
|
+
* @param options.end - Whether to end the stream after piping / 是否在管道传输后结束流
|
|
111
|
+
* @returns Destination stream / 目标流
|
|
112
|
+
*/
|
|
113
|
+
pipe<T extends HeaderStream>(destination: T, options?: {
|
|
114
|
+
end?: boolean;
|
|
115
|
+
}): T;
|
|
116
|
+
/**
|
|
117
|
+
* Transform message to SSE format
|
|
118
|
+
*
|
|
119
|
+
* 将消息转换为 SSE 格式
|
|
120
|
+
*
|
|
121
|
+
* @param message - SSE message / SSE 消息
|
|
122
|
+
* @param encoding - Encoding / 编码
|
|
123
|
+
* @param callback - Callback function / 回调函数
|
|
124
|
+
*/
|
|
125
|
+
_transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
|
|
126
|
+
/**
|
|
127
|
+
* Write message to the stream
|
|
128
|
+
*
|
|
129
|
+
* 向流写入消息
|
|
130
|
+
*
|
|
131
|
+
* @param message - SSE message / SSE 消息
|
|
132
|
+
* @param encoding - Encoding / 编码
|
|
133
|
+
* @param cb - Callback function / 回调函数
|
|
134
|
+
* @returns Whether the write was successful / 写入是否成功
|
|
135
|
+
*/
|
|
136
|
+
write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Write message to the stream
|
|
139
|
+
*
|
|
140
|
+
* 向流写入消息
|
|
141
|
+
*
|
|
142
|
+
* @param message - SSE message / SSE 消息
|
|
143
|
+
* @param cb - Callback function / 回调函数
|
|
144
|
+
* @returns Whether the write was successful / 写入是否成功
|
|
145
|
+
*/
|
|
146
|
+
write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Destroy the stream
|
|
149
|
+
*
|
|
150
|
+
* 销毁流
|
|
151
|
+
*
|
|
152
|
+
* @param error - Error object / 错误对象
|
|
153
|
+
* @returns This stream / 此流
|
|
154
|
+
*/
|
|
155
|
+
destroy(error?: Error): this;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Create a Server-sent events write stream for simulating EventSource
|
|
159
|
+
*
|
|
160
|
+
* 创建一个 Server-sent events 写入流,用于支持模拟 EventSource
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* import { createSSEStream, defineMock } from 'vite-plugin-mock-dev-server'
|
|
165
|
+
*
|
|
166
|
+
* export default defineMock({
|
|
167
|
+
* url: '/api',
|
|
168
|
+
* response: (req, res) => {
|
|
169
|
+
* const sse = createSSEStream(req, res)
|
|
170
|
+
* sse.write({ event: 'message', data: { message: 'hello world' } })
|
|
171
|
+
* }
|
|
172
|
+
* })
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @param req - HTTP request object / HTTP 请求对象
|
|
176
|
+
* @param res - HTTP response object / HTTP 响应对象
|
|
177
|
+
* @returns SSE stream instance / SSE 流实例
|
|
178
|
+
*/
|
|
179
|
+
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/helpers/defineMock.d.ts
|
|
182
|
+
/**
|
|
183
|
+
* Mock config Type helper
|
|
184
|
+
*
|
|
185
|
+
* mock配置 类型帮助函数
|
|
186
|
+
* @param config see config docs:
|
|
187
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config en-US DOC} |
|
|
188
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/zh/guide/mock-config zh-CN DOC}
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* Mock Http Request
|
|
192
|
+
* ```ts
|
|
193
|
+
* export default defineMock({
|
|
194
|
+
* url: '/api/example',
|
|
195
|
+
* method: ['GET', 'POST'],
|
|
196
|
+
* body: { a: 1 },
|
|
197
|
+
* })
|
|
198
|
+
* ```
|
|
199
|
+
* ```ts
|
|
200
|
+
* export default defineMock({
|
|
201
|
+
* url: '/api/example',
|
|
202
|
+
* method: 'GET',
|
|
203
|
+
* body: ({ query }) => ({ a: 1, b: query.b }),
|
|
204
|
+
* })
|
|
205
|
+
* ```
|
|
206
|
+
* @example
|
|
207
|
+
* Mock WebSocket
|
|
208
|
+
* ```ts
|
|
209
|
+
* export default defineMock({
|
|
210
|
+
* url: '/socket.io',
|
|
211
|
+
* ws: true,
|
|
212
|
+
* setup(wss) {
|
|
213
|
+
* wss.on('connection', (ws) => {
|
|
214
|
+
* ws.on('message', (rawData) => console.log(rawData))
|
|
215
|
+
* ws.send('data')
|
|
216
|
+
* })
|
|
217
|
+
* },
|
|
218
|
+
* })
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
declare function defineMock(config: MockHttpItem): MockHttpItem;
|
|
222
|
+
declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
|
|
223
|
+
declare function defineMock(config: MockOptions): MockOptions;
|
|
224
|
+
/**
|
|
225
|
+
* Return a custom defineMock function to support preprocessing of mock config.
|
|
226
|
+
*
|
|
227
|
+
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
228
|
+
* @param transformer preprocessing function
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* const definePostMock = createDefineMock((mock) => {
|
|
232
|
+
* mock.url = '/api/post/' + mock.url
|
|
233
|
+
* })
|
|
234
|
+
* export default definePostMock({
|
|
235
|
+
* url: 'list',
|
|
236
|
+
* body: [{ title: '1' }, { title: '2' }],
|
|
237
|
+
* })
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
241
|
+
//#endregion
|
|
242
|
+
//#region src/helpers/defineMockData.d.ts
|
|
243
|
+
/**
|
|
244
|
+
* Options for defineMockData
|
|
245
|
+
*
|
|
246
|
+
* defineMockData 的选项
|
|
247
|
+
*/
|
|
248
|
+
interface DefineMockDataOptions {
|
|
249
|
+
/**
|
|
250
|
+
* Whether to persist the data value on HMR (Hot Module Replacement).
|
|
251
|
+
*
|
|
252
|
+
* 热更新时是否保持数据值
|
|
253
|
+
*
|
|
254
|
+
* @default false
|
|
255
|
+
*/
|
|
256
|
+
persistOnHMR?: boolean;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Mock data type with getter, setter, and value property
|
|
260
|
+
*
|
|
261
|
+
* 带有 getter、setter 和 value 属性的 Mock 数据类型
|
|
262
|
+
*
|
|
263
|
+
* @template T - Type of mock data / Mock 数据的类型
|
|
264
|
+
*/
|
|
265
|
+
type MockData<T = any> = readonly [() => T, (val: T | ((val: T) => T | void)) => void] & {
|
|
266
|
+
/**
|
|
267
|
+
* Current value
|
|
268
|
+
*
|
|
269
|
+
* 当前值
|
|
270
|
+
*/
|
|
271
|
+
value: T;
|
|
272
|
+
};
|
|
273
|
+
/**
|
|
274
|
+
* Define mock data with memory-based sharing mechanism
|
|
275
|
+
*
|
|
276
|
+
* 定义带有基于内存的共享机制的 Mock 数据
|
|
277
|
+
*
|
|
278
|
+
* @template T - Type of mock data / Mock 数据的类型
|
|
279
|
+
* @param key - Unique key for mock data / Mock 数据的唯一键
|
|
280
|
+
* @param initialData - Initial data value / 初始数据值
|
|
281
|
+
* @param options - Options / 选项
|
|
282
|
+
* @returns MockData object with getter, setter, and value property / 带有 getter、setter 和 value 属性的 MockData 对象
|
|
283
|
+
*/
|
|
284
|
+
declare function defineMockData<T = any>(key: string, initialData: T, options?: DefineMockDataOptions): MockData<T>;
|
|
285
|
+
//#endregion
|
|
286
|
+
export { BodyParserOptions, CookiesOption, DefineMockDataOptions, ExtraRequest, FormidableFile, GetCookieOption, HeaderStream, Headers, LogLevel, LogType, Method, MockData, MockErrorConfig, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, RecordOptions, RecordedMeta, RecordedReq, RecordedRequest, RecordedRes, ResolvedRecordOptions, ResponseBody, SSEMessage, ServerBuildOption, SetCookieOption, WebSocketSetupContext, createDefineMock, createSSEStream, defineMock, defineMockData };
|
package/dist/helper.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{deepClone as e,isArray as t,isFunction as n}from"@pengzhanbo/utils";import{createHash as r}from"node:crypto";import{Transform as i}from"node:stream";var a=class extends i{constructor(e){super({objectMode:!0}),e.socket.setKeepAlive(!0),e.socket.setNoDelay(!0),e.socket.setTimeout(0)}pipe(e,t){return e.writeHead&&(e.writeHead(200,{"Content-Type":`text/event-stream; charset=utf-8`,"Transfer-Encoding":`identity`,"Cache-Control":`no-cache`,Connection:`keep-alive`}),e.flushHeaders?.()),e.write(`:ok
|
|
2
|
+
|
|
3
|
+
`),super.pipe(e,t)}_transform(e,t,n){e.comment&&this.push(`: ${e.comment}\n`),e.event&&this.push(`event: ${e.event}\n`),e.id&&this.push(`id: ${e.id}\n`),e.retry&&this.push(`retry: ${e.retry}\n`),e.data&&this.push(o(e.data)),this.push(`
|
|
4
|
+
`),n()}write(e,...t){return super.write(e,...t)}destroy(e){return e&&this.write({event:`error`,data:e.message}),this.end(),this}};function o(e){return typeof e==`object`?o(JSON.stringify(e)):e.split(/\r\n|\r|\n/).map(e=>`data: ${e}\n`).join(``)}function s(e,t){let n=new a(e);return n.pipe(t),n}function c(e){return e}function l(e){return n=>(n=t(n)?n.map(t=>e(t)||t):e(n)||n,n)}const u=new Map,d=new WeakMap;var f=class{value;#e;#t;#n;constructor(e,t){this.value=e,this.#e=m(e),this.#t=Date.now(),this.#n=t??!1}hotUpdate(t){if(this.#n||Date.now()-this.#t<70)return;let n=m(t);this.#e!==n&&(this.value=e(t),this.#e=n,this.#t=Date.now())}setPersistOnHMR(e){this.#n||=e}};function p(e,t,r){let i=u.get(e);if(i)i.setPersistOnHMR(r?.persistOnHMR??!1);else{let n=new f(t,r?.persistOnHMR),a=u.get(e);a?i=a:(u.set(e,n),i=n)}if(i.hotUpdate(t),d.has(i))return d.get(i);let a=[()=>i.value,e=>{n(e)&&(e=e(i.value)??i.value),i.value=e}];return Object.defineProperty(a,`value`,{get(){return i.value},set(e){i.value=e}}),d.set(i,a),a}function m(e){return r(`sha256`).update(JSON.stringify(e)).digest(`hex`)}export{l as createDefineMock,s as createSSEStream,c as defineMock,p as defineMockData};
|