vite-plugin-mock-dev-server 1.8.4 → 1.8.6

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.
@@ -1,130 +0,0 @@
1
- // src/core/defineMock.ts
2
- import { isArray } from "@pengzhanbo/utils";
3
- function defineMock(config) {
4
- return config;
5
- }
6
- function createDefineMock(transformer) {
7
- const define = (config) => {
8
- if (isArray(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
- import { deepClone, deepEqual, isFunction } from "@pengzhanbo/utils";
19
- var mockDataCache = /* @__PURE__ */ new Map();
20
- var responseCache = /* @__PURE__ */ new WeakMap();
21
- var staleInterval = 70;
22
- var CacheImpl = class {
23
- value;
24
- // 初始化数据的备份,用于 判断 传入的初始化数据是否发生变更
25
- #initialValue;
26
- #lastUpdate;
27
- constructor(value) {
28
- this.value = value;
29
- this.#initialValue = deepClone(value);
30
- this.#lastUpdate = Date.now();
31
- }
32
- hotUpdate(value) {
33
- if (Date.now() - this.#lastUpdate < staleInterval)
34
- return;
35
- if (!deepEqual(value, this.#initialValue)) {
36
- this.value = value;
37
- this.#initialValue = deepClone(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 (isFunction(val))
53
- val = 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
- 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
-
125
- export {
126
- defineMock,
127
- createDefineMock,
128
- defineMockData,
129
- createSSEStream
130
- };