vite-plugin-mock-dev-server 1.7.3 → 1.8.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/{chunk-VMBOC7DG.js → chunk-4RQVTS77.js} +58 -1
- package/dist/chunk-JNSDHVCK.cjs +130 -0
- package/dist/helper.cjs +4 -2
- package/dist/helper.d.cts +39 -3
- package/dist/helper.d.ts +39 -3
- package/dist/helper.js +3 -1
- package/dist/index.cjs +5 -3
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -2
- package/package.json +2 -2
- package/dist/chunk-FND5XIG2.cjs +0 -73
|
@@ -66,8 +66,65 @@ function defineMockData(key, initialData) {
|
|
|
66
66
|
return res;
|
|
67
67
|
}
|
|
68
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
|
+
|
|
69
125
|
export {
|
|
70
126
|
defineMock,
|
|
71
127
|
createDefineMock,
|
|
72
|
-
defineMockData
|
|
128
|
+
defineMockData,
|
|
129
|
+
createSSEStream
|
|
73
130
|
};
|
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
|
|
130
|
+
exports.defineMock = defineMock; exports.createDefineMock = createDefineMock; exports.defineMockData = defineMockData; exports.createSSEStream = createSSEStream;
|
package/dist/helper.cjs
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var _chunkFND5XIG2cjs = require('./chunk-FND5XIG2.cjs');
|
|
6
5
|
|
|
6
|
+
var _chunkJNSDHVCKcjs = require('./chunk-JNSDHVCK.cjs');
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
exports.createDefineMock = _chunkJNSDHVCKcjs.createDefineMock; exports.createSSEStream = _chunkJNSDHVCKcjs.createSSEStream; exports.defineMock = _chunkJNSDHVCKcjs.defineMock; exports.defineMockData = _chunkJNSDHVCKcjs.defineMockData;
|
package/dist/helper.d.cts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { a as MockHttpItem, d as MockWebsocketItem, b as MockOptions } from './types-BYspd62h.cjs';
|
|
2
2
|
export { c as MockRequest } from './types-BYspd62h.cjs';
|
|
3
|
+
import { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http';
|
|
4
|
+
import { Transform } from 'node:stream';
|
|
3
5
|
import 'co-body';
|
|
4
6
|
import 'cookies';
|
|
5
7
|
import 'cors';
|
|
6
8
|
import 'formidable';
|
|
7
9
|
import 'node:buffer';
|
|
8
|
-
import 'node:http';
|
|
9
|
-
import 'node:stream';
|
|
10
10
|
import 'vite';
|
|
11
11
|
import 'ws';
|
|
12
12
|
|
|
@@ -84,4 +84,40 @@ type MockData<T = any> = readonly [
|
|
|
84
84
|
};
|
|
85
85
|
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
interface SSEMessage {
|
|
88
|
+
data?: string | object;
|
|
89
|
+
comment?: string;
|
|
90
|
+
event?: string;
|
|
91
|
+
id?: string;
|
|
92
|
+
retry?: number;
|
|
93
|
+
}
|
|
94
|
+
interface WriteHeaders {
|
|
95
|
+
writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
|
|
96
|
+
flushHeaders?: () => void;
|
|
97
|
+
}
|
|
98
|
+
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
99
|
+
/**
|
|
100
|
+
* Transforms "messages" to W3C event stream content.
|
|
101
|
+
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
102
|
+
* A message is an object with one or more of the following properties:
|
|
103
|
+
* - data (String or object, which gets turned into JSON)
|
|
104
|
+
* - event
|
|
105
|
+
* - id
|
|
106
|
+
* - retry
|
|
107
|
+
* - comment
|
|
108
|
+
*
|
|
109
|
+
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
110
|
+
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
111
|
+
*/
|
|
112
|
+
declare class SSEStream extends Transform {
|
|
113
|
+
constructor(req: IncomingMessage);
|
|
114
|
+
pipe<T extends HeaderStream>(destination: T, options?: {
|
|
115
|
+
end?: boolean;
|
|
116
|
+
}): T;
|
|
117
|
+
_transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
|
|
118
|
+
write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
|
|
119
|
+
write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
|
|
120
|
+
}
|
|
121
|
+
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
122
|
+
|
|
123
|
+
export { type HeaderStream, type MockData, MockHttpItem, MockOptions, MockWebsocketItem, type SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData };
|
package/dist/helper.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { a as MockHttpItem, d as MockWebsocketItem, b as MockOptions } from './types-BYspd62h.js';
|
|
2
2
|
export { c as MockRequest } from './types-BYspd62h.js';
|
|
3
|
+
import { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http';
|
|
4
|
+
import { Transform } from 'node:stream';
|
|
3
5
|
import 'co-body';
|
|
4
6
|
import 'cookies';
|
|
5
7
|
import 'cors';
|
|
6
8
|
import 'formidable';
|
|
7
9
|
import 'node:buffer';
|
|
8
|
-
import 'node:http';
|
|
9
|
-
import 'node:stream';
|
|
10
10
|
import 'vite';
|
|
11
11
|
import 'ws';
|
|
12
12
|
|
|
@@ -84,4 +84,40 @@ type MockData<T = any> = readonly [
|
|
|
84
84
|
};
|
|
85
85
|
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
interface SSEMessage {
|
|
88
|
+
data?: string | object;
|
|
89
|
+
comment?: string;
|
|
90
|
+
event?: string;
|
|
91
|
+
id?: string;
|
|
92
|
+
retry?: number;
|
|
93
|
+
}
|
|
94
|
+
interface WriteHeaders {
|
|
95
|
+
writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
|
|
96
|
+
flushHeaders?: () => void;
|
|
97
|
+
}
|
|
98
|
+
type HeaderStream = NodeJS.WritableStream & WriteHeaders;
|
|
99
|
+
/**
|
|
100
|
+
* Transforms "messages" to W3C event stream content.
|
|
101
|
+
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
102
|
+
* A message is an object with one or more of the following properties:
|
|
103
|
+
* - data (String or object, which gets turned into JSON)
|
|
104
|
+
* - event
|
|
105
|
+
* - id
|
|
106
|
+
* - retry
|
|
107
|
+
* - comment
|
|
108
|
+
*
|
|
109
|
+
* If constructed with a HTTP Request, it will optimise the socket for streaming.
|
|
110
|
+
* If this stream is piped to an HTTP Response, it will set appropriate headers.
|
|
111
|
+
*/
|
|
112
|
+
declare class SSEStream extends Transform {
|
|
113
|
+
constructor(req: IncomingMessage);
|
|
114
|
+
pipe<T extends HeaderStream>(destination: T, options?: {
|
|
115
|
+
end?: boolean;
|
|
116
|
+
}): T;
|
|
117
|
+
_transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
|
|
118
|
+
write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
|
|
119
|
+
write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
|
|
120
|
+
}
|
|
121
|
+
declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
|
|
122
|
+
|
|
123
|
+
export { type HeaderStream, type MockData, MockHttpItem, MockOptions, MockWebsocketItem, type SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData };
|
package/dist/helper.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
var _chunkJNSDHVCKcjs = require('./chunk-JNSDHVCK.cjs');
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
|
|
@@ -267,7 +268,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
267
268
|
},
|
|
268
269
|
dependencies: {
|
|
269
270
|
connect: "^3.7.0",
|
|
270
|
-
["vite-plugin-mock-dev-server"]: `^${"1.7.
|
|
271
|
+
["vite-plugin-mock-dev-server"]: `^${"1.7.3"}`,
|
|
271
272
|
cors: "^2.8.5"
|
|
272
273
|
},
|
|
273
274
|
pnpm: { peerDependencyRules: { ignoreMissing: ["vite"] } }
|
|
@@ -786,4 +787,5 @@ var src_default = mockDevServerPlugin;
|
|
|
786
787
|
|
|
787
788
|
|
|
788
789
|
|
|
789
|
-
|
|
790
|
+
|
|
791
|
+
exports.baseMiddleware = _chunkJP6LPDGGcjs.baseMiddleware; exports.createDefineMock = _chunkJNSDHVCKcjs.createDefineMock; exports.createLogger = _chunkJP6LPDGGcjs.createLogger; exports.createSSEStream = _chunkJNSDHVCKcjs.createSSEStream; exports.default = src_default; exports.defineMock = _chunkJNSDHVCKcjs.defineMock; exports.defineMockData = _chunkJNSDHVCKcjs.defineMockData; exports.logLevels = _chunkJP6LPDGGcjs.logLevels; exports.mockDevServerPlugin = mockDevServerPlugin; exports.mockWebSocket = _chunkJP6LPDGGcjs.mockWebSocket; exports.sortByValidator = _chunkJP6LPDGGcjs.sortByValidator; exports.transformMockData = _chunkJP6LPDGGcjs.transformMockData; exports.transformRawData = _chunkJP6LPDGGcjs.transformRawData;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
2
|
import { M as MockServerPluginOptions } from './types-BYspd62h.cjs';
|
|
3
3
|
export { F as FormidableFile, a as MockHttpItem, b as MockOptions, c as MockRequest, d as MockWebsocketItem } from './types-BYspd62h.cjs';
|
|
4
|
-
export { MockData, createDefineMock, defineMock, defineMockData } from './helper.cjs';
|
|
4
|
+
export { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from './helper.cjs';
|
|
5
5
|
export { BaseMiddlewareOptions, Logger, baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData } from './server.cjs';
|
|
6
6
|
import 'co-body';
|
|
7
7
|
import 'cookies';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
2
|
import { M as MockServerPluginOptions } from './types-BYspd62h.js';
|
|
3
3
|
export { F as FormidableFile, a as MockHttpItem, b as MockOptions, c as MockRequest, d as MockWebsocketItem } from './types-BYspd62h.js';
|
|
4
|
-
export { MockData, createDefineMock, defineMock, defineMockData } from './helper.js';
|
|
4
|
+
export { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from './helper.js';
|
|
5
5
|
export { BaseMiddlewareOptions, Logger, baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData } from './server.js';
|
|
6
6
|
import 'co-body';
|
|
7
7
|
import 'cookies';
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createDefineMock,
|
|
3
|
+
createSSEStream,
|
|
3
4
|
defineMock,
|
|
4
5
|
defineMockData
|
|
5
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-4RQVTS77.js";
|
|
6
7
|
import {
|
|
7
8
|
baseMiddleware,
|
|
8
9
|
createLogger,
|
|
@@ -267,7 +268,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
267
268
|
},
|
|
268
269
|
dependencies: {
|
|
269
270
|
connect: "^3.7.0",
|
|
270
|
-
["vite-plugin-mock-dev-server"]: `^${"1.7.
|
|
271
|
+
["vite-plugin-mock-dev-server"]: `^${"1.7.3"}`,
|
|
271
272
|
cors: "^2.8.5"
|
|
272
273
|
},
|
|
273
274
|
pnpm: { peerDependencyRules: { ignoreMissing: ["vite"] } }
|
|
@@ -777,6 +778,7 @@ export {
|
|
|
777
778
|
baseMiddleware,
|
|
778
779
|
createDefineMock,
|
|
779
780
|
createLogger,
|
|
781
|
+
createSSEStream,
|
|
780
782
|
src_default as default,
|
|
781
783
|
defineMock,
|
|
782
784
|
defineMockData,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-dev-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://vite-plugin-mock-dev-server.netlify.app",
|
|
@@ -87,7 +87,7 @@
|
|
|
87
87
|
},
|
|
88
88
|
"devDependencies": {
|
|
89
89
|
"esbuild": "^0.24.0",
|
|
90
|
-
"vite": "^5.4.
|
|
90
|
+
"vite": "^5.4.8"
|
|
91
91
|
},
|
|
92
92
|
"publishConfig": {
|
|
93
93
|
"access": "public"
|
package/dist/chunk-FND5XIG2.cjs
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// 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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
exports.defineMock = defineMock; exports.createDefineMock = createDefineMock; exports.defineMockData = defineMockData;
|