vite-plugin-mock-dev-server 1.9.3 → 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/dist/{helper-DHb-Bj_j.js → helper-BbR8Si2U.js} +85 -69
- package/dist/helper.d.ts +3 -3
- package/dist/helper.js +1 -2
- package/dist/{helper-BGOKvYrM.d.ts → index-CJc2Oax2.d.ts} +57 -42
- package/dist/index.d.ts +4 -10
- package/dist/index.js +442 -325
- package/dist/{server-C-u7jwot.js → server-D-sv_WW9.js} +460 -391
- package/dist/{server-CmsjYpLV.d.ts → server-tcT2vAaP.d.ts} +32 -37
- package/dist/server.d.ts +3 -3
- package/dist/server.js +2 -3
- package/dist/{types-BbbTJG0b.d.ts → types-BtCJqeLH.d.ts} +12 -4
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +14 -20
- package/dist/dist-CAA1v47s.js +0 -204
- package/dist/dist-DrfpZ4UT.cjs +0 -323
- package/dist/helper-D4jXMDFX.d.cts +0 -111
- package/dist/helper-FbtDOlwA.cjs +0 -167
- package/dist/helper.cjs +0 -7
- package/dist/helper.d.cts +0 -3
- package/dist/index.cjs +0 -731
- package/dist/index.d.cts +0 -15
- package/dist/server-BwOfV_62.cjs +0 -810
- package/dist/server-DIZZ3-ar.d.cts +0 -93
- package/dist/server.cjs +0 -10
- package/dist/server.d.cts +0 -3
- package/dist/types-DpbHkRjL.d.cts +0 -573
|
@@ -1,7 +1,89 @@
|
|
|
1
|
-
import { deepClone, deepEqual, isArray, isFunction } from "
|
|
1
|
+
import { deepClone, deepEqual, isArray, isFunction } from "@pengzhanbo/utils";
|
|
2
2
|
import { Transform } from "node:stream";
|
|
3
3
|
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/helper/createSSEStream.ts
|
|
5
|
+
/**
|
|
6
|
+
* Transforms "messages" to W3C event stream content.
|
|
7
|
+
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
8
|
+
* A message is an object with one or more of the following properties:
|
|
9
|
+
* - data (String or object, which gets turned into JSON)
|
|
10
|
+
* - event
|
|
11
|
+
* - id
|
|
12
|
+
* - retry
|
|
13
|
+
* - comment
|
|
14
|
+
*
|
|
15
|
+
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
16
|
+
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
17
|
+
*/
|
|
18
|
+
var SSEStream = class extends Transform {
|
|
19
|
+
constructor(req) {
|
|
20
|
+
super({ objectMode: true });
|
|
21
|
+
req.socket.setKeepAlive(true);
|
|
22
|
+
req.socket.setNoDelay(true);
|
|
23
|
+
req.socket.setTimeout(0);
|
|
24
|
+
}
|
|
25
|
+
pipe(destination, options) {
|
|
26
|
+
if (destination.writeHead) {
|
|
27
|
+
destination.writeHead(200, {
|
|
28
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
29
|
+
"Transfer-Encoding": "identity",
|
|
30
|
+
"Cache-Control": "no-cache",
|
|
31
|
+
"Connection": "keep-alive"
|
|
32
|
+
});
|
|
33
|
+
destination.flushHeaders?.();
|
|
34
|
+
}
|
|
35
|
+
destination.write(":ok\n\n");
|
|
36
|
+
return super.pipe(destination, options);
|
|
37
|
+
}
|
|
38
|
+
_transform(message, encoding, callback) {
|
|
39
|
+
if (message.comment) this.push(`: ${message.comment}\n`);
|
|
40
|
+
if (message.event) this.push(`event: ${message.event}\n`);
|
|
41
|
+
if (message.id) this.push(`id: ${message.id}\n`);
|
|
42
|
+
if (message.retry) this.push(`retry: ${message.retry}\n`);
|
|
43
|
+
if (message.data) this.push(dataString(message.data));
|
|
44
|
+
this.push("\n");
|
|
45
|
+
callback();
|
|
46
|
+
}
|
|
47
|
+
write(message, ...args) {
|
|
48
|
+
return super.write(message, ...args);
|
|
49
|
+
}
|
|
50
|
+
destroy(error) {
|
|
51
|
+
if (error) this.write({
|
|
52
|
+
event: "error",
|
|
53
|
+
data: error.message
|
|
54
|
+
});
|
|
55
|
+
this.end();
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
function dataString(data) {
|
|
60
|
+
if (typeof data === "object") return dataString(JSON.stringify(data));
|
|
61
|
+
return data.split(/\r\n|\r|\n/).map((line) => `data: ${line}\n`).join("");
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 创建一个 Server-sent events 写入流,用于支持模拟 EventSource
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { createSSEStream, defineMock } from 'vite-plugin-mock-dev-server'
|
|
69
|
+
*
|
|
70
|
+
* export default defineMock({
|
|
71
|
+
* url: '/api',
|
|
72
|
+
* response: (req, res) => {
|
|
73
|
+
* const sse = createSSEStream(req, res)
|
|
74
|
+
* sse.write({ event: 'message', data: { message: 'hello world' } })
|
|
75
|
+
* }
|
|
76
|
+
* })
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
function createSSEStream(req, res) {
|
|
80
|
+
const sse = new SSEStream(req);
|
|
81
|
+
sse.pipe(res);
|
|
82
|
+
return sse;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/helper/defineMock.ts
|
|
5
87
|
function defineMock(config) {
|
|
6
88
|
return config;
|
|
7
89
|
}
|
|
@@ -31,7 +113,7 @@ function createDefineMock(transformer) {
|
|
|
31
113
|
}
|
|
32
114
|
|
|
33
115
|
//#endregion
|
|
34
|
-
//#region src/
|
|
116
|
+
//#region src/helper/defineMockData.ts
|
|
35
117
|
const mockDataCache = /* @__PURE__ */ new Map();
|
|
36
118
|
const responseCache = /* @__PURE__ */ new WeakMap();
|
|
37
119
|
const staleInterval = 70;
|
|
@@ -74,71 +156,5 @@ function defineMockData(key, initialData) {
|
|
|
74
156
|
return res;
|
|
75
157
|
}
|
|
76
158
|
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/core/sse.ts
|
|
79
|
-
/**
|
|
80
|
-
* Transforms "messages" to W3C event stream content.
|
|
81
|
-
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
82
|
-
* A message is an object with one or more of the following properties:
|
|
83
|
-
* - data (String or object, which gets turned into JSON)
|
|
84
|
-
* - event
|
|
85
|
-
* - id
|
|
86
|
-
* - retry
|
|
87
|
-
* - comment
|
|
88
|
-
*
|
|
89
|
-
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
90
|
-
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
91
|
-
*/
|
|
92
|
-
var SSEStream = class extends Transform {
|
|
93
|
-
constructor(req) {
|
|
94
|
-
super({ objectMode: true });
|
|
95
|
-
req.socket.setKeepAlive(true);
|
|
96
|
-
req.socket.setNoDelay(true);
|
|
97
|
-
req.socket.setTimeout(0);
|
|
98
|
-
}
|
|
99
|
-
pipe(destination, options) {
|
|
100
|
-
if (destination.writeHead) {
|
|
101
|
-
destination.writeHead(200, {
|
|
102
|
-
"Content-Type": "text/event-stream; charset=utf-8",
|
|
103
|
-
"Transfer-Encoding": "identity",
|
|
104
|
-
"Cache-Control": "no-cache",
|
|
105
|
-
"Connection": "keep-alive"
|
|
106
|
-
});
|
|
107
|
-
destination.flushHeaders?.();
|
|
108
|
-
}
|
|
109
|
-
destination.write(":ok\n\n");
|
|
110
|
-
return super.pipe(destination, options);
|
|
111
|
-
}
|
|
112
|
-
_transform(message, encoding, callback) {
|
|
113
|
-
if (message.comment) this.push(`: ${message.comment}\n`);
|
|
114
|
-
if (message.event) this.push(`event: ${message.event}\n`);
|
|
115
|
-
if (message.id) this.push(`id: ${message.id}\n`);
|
|
116
|
-
if (message.retry) this.push(`retry: ${message.retry}\n`);
|
|
117
|
-
if (message.data) this.push(dataString(message.data));
|
|
118
|
-
this.push("\n");
|
|
119
|
-
callback();
|
|
120
|
-
}
|
|
121
|
-
write(message, ...args) {
|
|
122
|
-
return super.write(message, ...args);
|
|
123
|
-
}
|
|
124
|
-
destroy(error) {
|
|
125
|
-
if (error) this.write({
|
|
126
|
-
event: "error",
|
|
127
|
-
data: error.message
|
|
128
|
-
});
|
|
129
|
-
this.end();
|
|
130
|
-
return this;
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
function dataString(data) {
|
|
134
|
-
if (typeof data === "object") return dataString(JSON.stringify(data));
|
|
135
|
-
return data.split(/\r\n|\r|\n/).map((line) => `data: ${line}\n`).join("");
|
|
136
|
-
}
|
|
137
|
-
function createSSEStream(req, res) {
|
|
138
|
-
const sse = new SSEStream(req);
|
|
139
|
-
sse.pipe(res);
|
|
140
|
-
return sse;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
159
|
//#endregion
|
|
144
160
|
export { createDefineMock, createSSEStream, defineMock, defineMockData };
|
package/dist/helper.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { MockHttpItem, MockOptions, MockRequest, MockWebsocketItem } from "./types-
|
|
2
|
-
import { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from "./
|
|
3
|
-
export { HeaderStream, MockData, MockHttpItem, MockOptions, MockRequest, MockWebsocketItem, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData };
|
|
1
|
+
import { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext } from "./types-BtCJqeLH.js";
|
|
2
|
+
import { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from "./index-CJc2Oax2.js";
|
|
3
|
+
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 };
|
package/dist/helper.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import { createDefineMock, createSSEStream, defineMock, defineMockData } from "./helper-DHb-Bj_j.js";
|
|
1
|
+
import { createDefineMock, createSSEStream, defineMock, defineMockData } from "./helper-BbR8Si2U.js";
|
|
3
2
|
|
|
4
3
|
export { createDefineMock, createSSEStream, defineMock, defineMockData };
|
|
@@ -1,9 +1,62 @@
|
|
|
1
|
-
import { MockHttpItem, MockOptions, MockWebsocketItem } from "./types-
|
|
1
|
+
import { MockHttpItem, MockOptions, MockWebsocketItem } from "./types-BtCJqeLH.js";
|
|
2
2
|
import { Transform } from "node:stream";
|
|
3
3
|
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http";
|
|
4
4
|
|
|
5
|
-
//#region src/
|
|
6
|
-
|
|
5
|
+
//#region src/helper/createSSEStream.d.ts
|
|
6
|
+
interface SSEMessage {
|
|
7
|
+
data?: string | object;
|
|
8
|
+
comment?: string;
|
|
9
|
+
event?: string;
|
|
10
|
+
id?: string;
|
|
11
|
+
retry?: number;
|
|
12
|
+
}
|
|
13
|
+
interface WriteHeaders {
|
|
14
|
+
writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
|
|
15
|
+
flushHeaders?: () => void;
|
|
16
|
+
}
|
|
17
|
+
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
18
|
+
/**
|
|
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
|
+
*/
|
|
31
|
+
declare class SSEStream extends Transform {
|
|
32
|
+
constructor(req: IncomingMessage);
|
|
33
|
+
pipe<T extends HeaderStream>(destination: T, options?: {
|
|
34
|
+
end?: boolean;
|
|
35
|
+
}): T;
|
|
36
|
+
_transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
|
|
37
|
+
write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
|
|
38
|
+
write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
|
|
39
|
+
destroy(error?: Error): this;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
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
|
+
*/
|
|
57
|
+
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/helper/defineMock.d.ts
|
|
7
60
|
/**
|
|
8
61
|
* mock config Type helper
|
|
9
62
|
*
|
|
@@ -64,48 +117,10 @@ declare function defineMock(config: MockOptions): MockOptions;
|
|
|
64
117
|
*/
|
|
65
118
|
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
66
119
|
//#endregion
|
|
67
|
-
//#region src/
|
|
120
|
+
//#region src/helper/defineMockData.d.ts
|
|
68
121
|
type MockData<T = any> = readonly [() => T, (val: T | ((val: T) => T | void)) => void] & {
|
|
69
122
|
value: T;
|
|
70
123
|
};
|
|
71
124
|
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
|
|
72
125
|
//#endregion
|
|
73
|
-
//#region src/core/sse.d.ts
|
|
74
|
-
interface SSEMessage {
|
|
75
|
-
data?: string | object;
|
|
76
|
-
comment?: string;
|
|
77
|
-
event?: string;
|
|
78
|
-
id?: string;
|
|
79
|
-
retry?: number;
|
|
80
|
-
}
|
|
81
|
-
interface WriteHeaders {
|
|
82
|
-
writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
|
|
83
|
-
flushHeaders?: () => void;
|
|
84
|
-
}
|
|
85
|
-
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
86
|
-
/**
|
|
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
|
-
*/
|
|
99
|
-
declare class SSEStream extends Transform {
|
|
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;
|
|
107
|
-
destroy(error?: Error): this;
|
|
108
|
-
}
|
|
109
|
-
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
110
|
-
//#endregion
|
|
111
126
|
export { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
import { FormidableFile, MockHttpItem, MockOptions, MockRequest, MockServerPluginOptions, MockWebsocketItem } from "./types-
|
|
2
|
-
import { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from "./
|
|
3
|
-
import {
|
|
1
|
+
import { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext } from "./types-BtCJqeLH.js";
|
|
2
|
+
import { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from "./index-CJc2Oax2.js";
|
|
3
|
+
import { CreateMockMiddlewareOptions, Logger, createLogger, createMockMiddleware, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator } from "./server-tcT2vAaP.js";
|
|
4
4
|
import { Plugin } from "vite";
|
|
5
5
|
|
|
6
6
|
//#region src/plugin.d.ts
|
|
7
7
|
declare function mockDevServerPlugin(options?: MockServerPluginOptions): Plugin[];
|
|
8
8
|
//#endregion
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @deprecated use named export instead
|
|
12
|
-
*/
|
|
13
|
-
declare function mockDevServerPluginWithDefaultExportWasDeprecated(options?: MockServerPluginOptions): Plugin[];
|
|
14
|
-
//#endregion
|
|
15
|
-
export { BaseMiddlewareOptions, type FormidableFile, HeaderStream, Logger, MockData, type MockHttpItem, type MockOptions, type MockRequest, type MockServerPluginOptions, type MockWebsocketItem, SSEMessage, baseMiddleware, createDefineMock, createLogger, createSSEStream, mockDevServerPluginWithDefaultExportWasDeprecated as default, defineMock, defineMockData, logLevels, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData, transformRawData };
|
|
9
|
+
export { BodyParserOptions, CreateMockMiddlewareOptions, ExtraRequest, type FormidableFile, HeaderStream, LogLevel, LogType, Logger, Method, MockData, type MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, type MockOptions, type MockRequest, MockResponse, type MockServerPluginOptions, type MockWebsocketItem, ResponseBody, SSEMessage, ServerBuildOption, WebSocketSetupContext, createDefineMock, createLogger, createMockMiddleware, createSSEStream, defineMock, defineMockData, logLevels, mockDevServerPlugin, mockWebSocket, processMockData, processRawData, sortByValidator };
|