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