rspack-plugin-mock 2.0.0 → 2.1.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.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { _ as WebSocketSetupContext, a as LogType, c as MockMatchPriority, d as MockRequest, f as MockResponse, g as ServerBuildOption, h as ResponseBody, i as LogLevel, l as MockMatchSpecialPriority, m as MockWebsocketItem, n as ExtraRequest, o as Method, p as MockServerPluginOptions, r as FormidableFile, s as MockHttpItem, t as BodyParserOptions, u as MockOptions } from "./types-GT6M6WuI.js";
2
- import { Transform } from "node:stream";
1
+ import { A as ResponseBody, C as HandleFunction, D as MockResponse, E as MockRequest, M as CookiesOption, N as GetCookieOption, O as NextFunction, P as SetCookieOption, S as ExtraRequest, T as Method, _ as RecordedMeta, a as MockWebsocketItem, b as RecordedRes, c as MockHttpItem, d as LogType, f as MockMatchPriority, g as RecordOptions, h as ServerBuildOption, i as MockOptions, j as SimpleHandleFunction, k as NextHandleFunction, l as BodyParserOptions, m as MockServerPluginOptions, n as HttpProxyPlugin, o as WebSocketSetupContext, p as MockMatchSpecialPriority, r as PathFilter, s as MockErrorConfig, t as FormidableFile, u as LogLevel, v as RecordedReq, w as Headers, x as ResolvedRecordOptions, y as RecordedRequest } from "./index-BMMGM-eD.js";
3
2
  import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http";
3
+ import { Transform } from "node:stream";
4
4
 
5
- //#region src/helper/createSSEStream.d.ts
5
+ //#region src/helpers/createSSEStream.d.ts
6
6
  interface SSEMessage {
7
7
  data?: string | object;
8
8
  comment?: string;
@@ -56,9 +56,9 @@ declare class SSEStream extends Transform {
56
56
  */
57
57
  declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
58
58
  //#endregion
59
- //#region src/helper/defineMock.d.ts
59
+ //#region src/helpers/defineMock.d.ts
60
60
  /**
61
- * mock config Type helper
61
+ * Mock config Type helper
62
62
  *
63
63
  * mock配置 类型帮助函数
64
64
  * @param config see config docs:
@@ -117,10 +117,48 @@ declare function defineMock(config: MockOptions): MockOptions;
117
117
  */
118
118
  declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
119
119
  //#endregion
120
- //#region src/helper/defineMockData.d.ts
120
+ //#region src/helpers/defineMockData.d.ts
121
+ /**
122
+ * Options for defineMockData
123
+ *
124
+ * defineMockData 的选项
125
+ */
126
+ interface DefineMockDataOptions {
127
+ /**
128
+ * Whether to persist the data value on HMR (Hot Module Replacement).
129
+ *
130
+ * 热更新时是否保持数据值
131
+ *
132
+ * @default false
133
+ */
134
+ persistOnHMR?: boolean;
135
+ }
136
+ /**
137
+ * Mock data type with getter, setter, and value property
138
+ *
139
+ * 带有 getter、setter 和 value 属性的 Mock 数据类型
140
+ *
141
+ * @template T - Type of mock data / Mock 数据的类型
142
+ */
121
143
  type MockData<T = any> = readonly [() => T, (val: T | ((val: T) => T | void)) => void] & {
144
+ /**
145
+ * Current value
146
+ *
147
+ * 当前值
148
+ */
122
149
  value: T;
123
150
  };
124
- declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
151
+ /**
152
+ * Define mock data with memory-based sharing mechanism
153
+ *
154
+ * 定义带有基于内存的共享机制的 Mock 数据
155
+ *
156
+ * @template T - Type of mock data / Mock 数据的类型
157
+ * @param key - Unique key for mock data / Mock 数据的唯一键
158
+ * @param initialData - Initial data value / 初始数据值
159
+ * @param options - Options / 选项
160
+ * @returns MockData object with getter, setter, and value property / 带有 getter、setter 和 value 属性的 MockData 对象
161
+ */
162
+ declare function defineMockData<T = any>(key: string, initialData: T, options?: DefineMockDataOptions): MockData<T>;
125
163
  //#endregion
126
- 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 };
164
+ export { BodyParserOptions, CookiesOption, DefineMockDataOptions, ExtraRequest, FormidableFile, GetCookieOption, HandleFunction, HeaderStream, Headers, HttpProxyPlugin, LogLevel, LogType, Method, MockData, MockErrorConfig, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, NextFunction, NextHandleFunction, PathFilter, RecordOptions, RecordedMeta, RecordedReq, RecordedRequest, RecordedRes, ResolvedRecordOptions, ResponseBody, SSEMessage, ServerBuildOption, SetCookieOption, SimpleHandleFunction, WebSocketSetupContext, createDefineMock, createSSEStream, defineMock, defineMockData };
package/dist/helper.js CHANGED
@@ -1,6 +1,7 @@
1
+ import { deepClone, isArray, isFunction } from "@pengzhanbo/utils";
2
+ import { createHash } from "node:crypto";
1
3
  import { Transform } from "node:stream";
2
- import { deepClone, deepEqual, isArray, isFunction } from "@pengzhanbo/utils";
3
- //#region src/helper/createSSEStream.ts
4
+ //#region src/helpers/createSSEStream.ts
4
5
  /**
5
6
  * Transforms "messages" to W3C event stream content.
6
7
  * See https://html.spec.whatwg.org/multipage/server-sent-events.html
@@ -81,7 +82,7 @@ function createSSEStream(req, res) {
81
82
  return sse;
82
83
  }
83
84
  //#endregion
84
- //#region src/helper/defineMock.ts
85
+ //#region src/helpers/defineMock.ts
85
86
  function defineMock(config) {
86
87
  return config;
87
88
  }
@@ -110,47 +111,138 @@ function createDefineMock(transformer) {
110
111
  return define;
111
112
  }
112
113
  //#endregion
113
- //#region src/helper/defineMockData.ts
114
+ //#region src/helpers/defineMockData.ts
114
115
  /**
115
- * 由于插件是 分别独立对 `*.mock.*` 等文件作为单独入口进行编译的,
116
+ * Since the plugin compiles `*.mock.*` files as separate independent entries,
117
+ * this leads to inconsistent dependency relationships after mock file compilation,
118
+ * with each mock file having its own scope.
119
+ * Even if multiple `*.mock.*` files import the same `data.ts` file, they are
120
+ * completely different instances of `data`, making operations on `data` not
121
+ * shareable across different `*.mock.*` files.
122
+ *
123
+ * To address this, the plugin provides a memory-based data sharing mechanism.
124
+ *
125
+ * 由于插件是分别独立对 `*.mock.*` 等文件作为单独入口进行编译的,
116
126
  * 这导致了 mock 文件编译后的依赖关系不一致,每个 mock 文件拥有独立的作用域,
117
127
  * 使得即使多个 `*.mock.*` 虽然引入了同一个 `data.ts` 文件,然而确是完全不同两份 `data`,
118
128
  * 使得对 `data` 的操作,在不同的 `*.mock.*` 文件中并不能共享。
119
129
  *
120
130
  * 为此,插件提供了一种基于 memory 的数据共享机制。
121
131
  */
132
+ /**
133
+ * Mock data cache
134
+ *
135
+ * Mock 数据缓存
136
+ */
122
137
  const mockDataCache = /* @__PURE__ */ new Map();
138
+ /**
139
+ * Response cache for MockData objects
140
+ *
141
+ * MockData 对象的响应缓存
142
+ */
123
143
  const responseCache = /* @__PURE__ */ new WeakMap();
144
+ /**
145
+ * Stale interval in milliseconds
146
+ *
147
+ * 缓存过期间隔(毫秒)
148
+ */
124
149
  const staleInterval = 70;
150
+ /**
151
+ * Cache implementation for mock data
152
+ *
153
+ * Mock 数据缓存实现
154
+ *
155
+ * @template T - Type of cached data / 缓存数据的类型
156
+ */
125
157
  var CacheImpl = class {
158
+ /**
159
+ * Current cached value
160
+ *
161
+ * 当前缓存值
162
+ */
126
163
  value;
127
- #initialValue;
164
+ /**
165
+ * Initial value hash, used to detect if initial data has changed
166
+ *
167
+ * 初始化数据的哈希值,用于判断传入的初始化数据是否发生变更
168
+ */
169
+ #hash;
170
+ /**
171
+ * Last update timestamp
172
+ *
173
+ * 最后更新时间戳
174
+ */
128
175
  #lastUpdate;
129
- constructor(value) {
176
+ /**
177
+ * Whether to persist data on HMR
178
+ *
179
+ * 热更新时是否保持数据
180
+ */
181
+ #persistOnHMR;
182
+ /**
183
+ * Constructor
184
+ *
185
+ * 构造函数
186
+ *
187
+ * @param value - Initial value / 初始值
188
+ * @param persistOnHMR - Whether to persist data on HMR / 热更新时是否保持数据
189
+ */
190
+ constructor(value, persistOnHMR) {
130
191
  this.value = value;
131
- this.#initialValue = deepClone(value);
192
+ this.#hash = getHash(value);
132
193
  this.#lastUpdate = Date.now();
194
+ this.#persistOnHMR = persistOnHMR ?? false;
133
195
  }
196
+ /**
197
+ * Hot update cached value
198
+ *
199
+ * 热更新缓存值
200
+ *
201
+ * @param value - New value / 新值
202
+ */
134
203
  hotUpdate(value) {
204
+ if (this.#persistOnHMR) return;
135
205
  if (Date.now() - this.#lastUpdate < staleInterval) return;
136
- if (!deepEqual(value, this.#initialValue)) {
137
- this.value = value;
138
- this.#initialValue = deepClone(value);
206
+ const hash = getHash(value);
207
+ if (this.#hash !== hash) {
208
+ this.value = deepClone(value);
209
+ this.#hash = hash;
139
210
  this.#lastUpdate = Date.now();
140
211
  }
141
212
  }
213
+ /**
214
+ * Set persistOnHMR
215
+ *
216
+ * 设置 persistOnHMR
217
+ *
218
+ * @param persistOnHMR - Whether to persist data on HMR / 热更新时是否保持数据
219
+ */
220
+ setPersistOnHMR(persistOnHMR) {
221
+ if (!this.#persistOnHMR) this.#persistOnHMR = persistOnHMR;
222
+ }
142
223
  };
143
- function defineMockData(key, initialData) {
224
+ /**
225
+ * Define mock data with memory-based sharing mechanism
226
+ *
227
+ * 定义带有基于内存的共享机制的 Mock 数据
228
+ *
229
+ * @template T - Type of mock data / Mock 数据的类型
230
+ * @param key - Unique key for mock data / Mock 数据的唯一键
231
+ * @param initialData - Initial data value / 初始数据值
232
+ * @param options - Options / 选项
233
+ * @returns MockData object with getter, setter, and value property / 带有 getter、setter 和 value 属性的 MockData 对象
234
+ */
235
+ function defineMockData(key, initialData, options) {
144
236
  let cache = mockDataCache.get(key);
145
237
  if (!cache) {
146
- const newCache = new CacheImpl(initialData);
238
+ const newCache = new CacheImpl(initialData, options?.persistOnHMR);
147
239
  const existing = mockDataCache.get(key);
148
240
  if (existing) cache = existing;
149
241
  else {
150
242
  mockDataCache.set(key, newCache);
151
243
  cache = newCache;
152
244
  }
153
- }
245
+ } else cache.setPersistOnHMR(options?.persistOnHMR ?? false);
154
246
  cache.hotUpdate(initialData);
155
247
  if (responseCache.has(cache)) return responseCache.get(cache);
156
248
  const res = [() => cache.value, (val) => {
@@ -168,5 +260,8 @@ function defineMockData(key, initialData) {
168
260
  responseCache.set(cache, res);
169
261
  return res;
170
262
  }
263
+ function getHash(data) {
264
+ return createHash("sha256").update(JSON.stringify(data)).digest("hex");
265
+ }
171
266
  //#endregion
172
267
  export { createDefineMock, createSSEStream, defineMock, defineMockData };