rspack-plugin-mock 0.5.0 → 1.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/README.md CHANGED
@@ -35,7 +35,7 @@ Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent w
35
35
  - ⚓️ Support `alias` in the mock file.
36
36
  - 📤 Support `multipart` content-type, mock upload file.
37
37
  - 📥 Support mock download file.
38
- - ⚜️ Support `WebSocket Mock`
38
+ - ⚜️ Support `WebSocket Mock` and `Server-Sent Events Mock`
39
39
  - 🗂 Support building small independent deployable mock services.
40
40
 
41
41
  ## Install
@@ -187,6 +187,23 @@ export default definePostMock({
187
187
  })
188
188
  ```
189
189
 
190
+ ### createSSEStream(req, res)
191
+
192
+ Create a `Server-sent events` write stream to support mocking `EventSource`.
193
+
194
+ ``` ts
195
+ import { createSSEStream, defineMock } from 'rspack-plugin-mock/helper'
196
+
197
+ export default defineMock({
198
+ url: '/api/sse',
199
+ response: (req, res) => {
200
+ const sse = createSSEStream(req, res)
201
+ sse.write({ event: 'message', data: { message: 'hello world' } })
202
+ sse.end()
203
+ }
204
+ })
205
+ ```
206
+
190
207
  ## Plugin Options
191
208
 
192
209
  ### options.prefix
@@ -860,6 +877,40 @@ ws.addEventListener('message', (raw) => {
860
877
  })
861
878
  ```
862
879
 
880
+ **exp:** EventSource Mock
881
+
882
+ ```ts
883
+ // sse.mock.ts
884
+ import { createSSEStream, defineMock } from 'rspack-plugin-mock/helper'
885
+
886
+ export default defineMock({
887
+ url: '/api/sse',
888
+ response(req, res) {
889
+ const sse = createSSEStream(req, res)
890
+ let count = 0
891
+ const timer = setInterval(() => {
892
+ sse.write({
893
+ event: 'count',
894
+ data: { count: ++count },
895
+ })
896
+ if (count >= 10) {
897
+ sse.end()
898
+ clearInterval(timer)
899
+ }
900
+ }, 1000)
901
+ },
902
+ })
903
+ ```
904
+
905
+ ```ts
906
+ // app.js
907
+ const es = new EventSource('/api/sse')
908
+
909
+ es.addEventListener('count', (e) => {
910
+ console.log(e.data)
911
+ })
912
+ ```
913
+
863
914
  ## Mock Services
864
915
 
865
916
  In some scenarios, it may be necessary to use the data provided by mock services for display purposes, but the project may have already been packaged, built and deployed without support from `rspack/rsbuild` and this plugin's mock service. Since this plugin supports importing various node modules in mock files at the design stage, the mock file cannot be inline into client build code.
package/README.zh-CN.md CHANGED
@@ -35,7 +35,7 @@
35
35
  - ⚓️ 支持在 mock文件中使用 `resolve.alias` 路径别名
36
36
  - 📤 支持 multipart 类型,模拟文件上传
37
37
  - 📥 支持模拟文件下载
38
- - ⚜️ 支持模拟 `WebSocket`
38
+ - ⚜️ 支持模拟 `WebSocket` 和 `Server-Sent Events`
39
39
  - 🗂 支持构建可独立部署的小型mock服务
40
40
 
41
41
  ## 安装
@@ -187,6 +187,23 @@ export default definePostMock({
187
187
  })
188
188
  ```
189
189
 
190
+ ### createSSEStream(req, res)
191
+
192
+ 创建一个 `Server-sent events` 写入流,用于支持模拟 `EventSource`。
193
+
194
+ ``` ts
195
+ import { createSSEStream, defineMock } from 'rspack-plugin-mock/helper'
196
+
197
+ export default defineMock({
198
+ url: '/api/sse',
199
+ response: (req, res) => {
200
+ const sse = createSSEStream(req, res)
201
+ sse.write({ event: 'message', data: { message: 'hello world' } })
202
+ sse.end()
203
+ }
204
+ })
205
+ ```
206
+
190
207
  ## 插件配置
191
208
 
192
209
  ### options.prefix
@@ -858,6 +875,40 @@ ws.addEventListener('message', (raw) => {
858
875
  })
859
876
  ```
860
877
 
878
+ **示例:** EventSource Mock
879
+
880
+ ```ts
881
+ // sse.mock.ts
882
+ import { createSSEStream, defineMock } from 'rspack-plugin-mock/helper'
883
+
884
+ export default defineMock({
885
+ url: '/api/sse',
886
+ response(req, res) {
887
+ const sse = createSSEStream(req, res)
888
+ let count = 0
889
+ const timer = setInterval(() => {
890
+ sse.write({
891
+ event: 'count',
892
+ data: { count: ++count },
893
+ })
894
+ if (count >= 10) {
895
+ sse.end()
896
+ clearInterval(timer)
897
+ }
898
+ }, 1000)
899
+ },
900
+ })
901
+ ```
902
+
903
+ ```ts
904
+ // app.js
905
+ const es = new EventSource('/api/sse')
906
+
907
+ es.addEventListener('count', (e) => {
908
+ console.log(e.data)
909
+ })
910
+ ```
911
+
861
912
  ## 独立部署的小型mock服务
862
913
 
863
914
  在一些场景中,可能会需要使用mock服务提供的数据支持,用于展示,但可能项目已完成打包构建部署,已脱离 `rspack/rsbuild` 和本插件提供的 mock服务支持。由于本插件在设计之初,支持在mock文件中引入各种 `node` 模块,所以不能将 mock文件打包内联到客户端构建代码中。
@@ -9,7 +9,7 @@ import {
9
9
  transformRawData,
10
10
  urlParse,
11
11
  vfs
12
- } from "./chunk-KN5FLSDL.js";
12
+ } from "./chunk-DCX6CEAX.js";
13
13
 
14
14
  // src/core/build.ts
15
15
  import fs from "node:fs";
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  isEmptyObject,
4
4
  isFunction,
5
- isObject as isObject2,
5
+ isPlainObject as isPlainObject2,
6
6
  sortBy,
7
7
  toArray
8
8
  } from "@pengzhanbo/utils";
@@ -80,7 +80,7 @@ function waitingFor(onSuccess, maxRetry = 5) {
80
80
  }
81
81
 
82
82
  // src/core/validator.ts
83
- import { isArray, isObject } from "@pengzhanbo/utils";
83
+ import { isArray, isPlainObject } from "@pengzhanbo/utils";
84
84
  function validate(request, validator) {
85
85
  return isObjectSubset(request.headers, validator.headers) && isObjectSubset(request.body, validator.body) && isObjectSubset(request.params, validator.params) && isObjectSubset(request.query, validator.query) && isObjectSubset(request.refererQuery, validator.refererQuery);
86
86
  }
@@ -107,7 +107,7 @@ function isIncluded(source, target) {
107
107
  })
108
108
  );
109
109
  }
110
- if (isObject(source) && isObject(target))
110
+ if (isPlainObject(source) && isPlainObject(target))
111
111
  return isObjectSubset(source, target);
112
112
  return Object.is(source, target);
113
113
  }
@@ -144,7 +144,7 @@ function transformMockData(mockList) {
144
144
  list.push(...toArray(handle));
145
145
  }
146
146
  const mocks = {};
147
- list.filter((mock) => isObject2(mock) && mock.enabled !== false && mock.url).forEach((mock) => {
147
+ list.filter((mock) => isPlainObject2(mock) && mock.enabled !== false && mock.url).forEach((mock) => {
148
148
  const { pathname, query } = urlParse(mock.url);
149
149
  const list2 = mocks[pathname] ??= [];
150
150
  const current = { ...mock, url: pathname };
@@ -107,7 +107,7 @@ function isIncluded(source, target) {
107
107
  })
108
108
  );
109
109
  }
110
- if (_utils.isObject.call(void 0, source) && _utils.isObject.call(void 0, target))
110
+ if (_utils.isPlainObject.call(void 0, source) && _utils.isPlainObject.call(void 0, target))
111
111
  return isObjectSubset(source, target);
112
112
  return Object.is(source, target);
113
113
  }
@@ -144,7 +144,7 @@ function transformMockData(mockList) {
144
144
  list.push(..._utils.toArray.call(void 0, handle));
145
145
  }
146
146
  const mocks = {};
147
- list.filter((mock) => _utils.isObject.call(void 0, mock) && mock.enabled !== false && mock.url).forEach((mock) => {
147
+ list.filter((mock) => _utils.isPlainObject.call(void 0, mock) && mock.enabled !== false && mock.url).forEach((mock) => {
148
148
  const { pathname, query } = urlParse(mock.url);
149
149
  const list2 = mocks[pathname] ??= [];
150
150
  const current = { ...mock, url: pathname };
@@ -9,7 +9,7 @@
9
9
 
10
10
 
11
11
 
12
- var _chunkTKSRTJX5cjs = require('./chunk-TKSRTJX5.cjs');
12
+ var _chunkTO6MO344cjs = require('./chunk-TO6MO344.cjs');
13
13
 
14
14
  // src/core/build.ts
15
15
  var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
@@ -50,7 +50,7 @@ function createCompiler(options, callback) {
50
50
  const info = stats.toJson();
51
51
  logError(info.errors);
52
52
  }
53
- const code = _chunkTKSRTJX5cjs.vfs.readFileSync("/output.js", "utf-8");
53
+ const code = _chunkTO6MO344cjs.vfs.readFileSync("/output.js", "utf-8");
54
54
  const externals = [];
55
55
  if (!isWatch) {
56
56
  const modules = _optionalChain([stats, 'optionalAccess', _3 => _3.toJson, 'call', _4 => _4(), 'access', _5 => _5.modules]) || [];
@@ -67,7 +67,7 @@ function createCompiler(options, callback) {
67
67
  }
68
68
  const compiler = rspackCore.rspack(rspackOptions, isWatch ? handler : void 0);
69
69
  if (compiler)
70
- compiler.outputFileSystem = _chunkTKSRTJX5cjs.vfs;
70
+ compiler.outputFileSystem = _chunkTO6MO344cjs.vfs;
71
71
  if (!isWatch) {
72
72
  _optionalChain([compiler, 'optionalAccess', _8 => _8.run, 'call', _9 => _9(async (...args) => {
73
73
  await handler(...args);
@@ -270,7 +270,7 @@ async function writeMockEntryFile(entryFile, files, cwd) {
270
270
  const importers = [];
271
271
  const exporters = [];
272
272
  for (const [index, filepath] of files.entries()) {
273
- const file = _chunkTKSRTJX5cjs.normalizePath.call(void 0, _path2.default.join(cwd, filepath));
273
+ const file = _chunkTO6MO344cjs.normalizePath.call(void 0, _path2.default.join(cwd, filepath));
274
274
  importers.push(`import * as m${index} from '${file}'`);
275
275
  exporters.push(`[m${index}, '${filepath}']`);
276
276
  }
@@ -288,7 +288,7 @@ export default [
288
288
  function getPluginPackageInfo() {
289
289
  let pkg = {};
290
290
  try {
291
- const filepath = _path2.default.join(_chunkTKSRTJX5cjs.packageDir, "../package.json");
291
+ const filepath = _path2.default.join(_chunkTO6MO344cjs.packageDir, "../package.json");
292
292
  if (_fs2.default.existsSync(filepath)) {
293
293
  pkg = JSON.parse(_fs2.default.readFileSync(filepath, "utf8"));
294
294
  }
@@ -302,7 +302,7 @@ function getPluginPackageInfo() {
302
302
  function getHostDependencies(context) {
303
303
  let pkg = {};
304
304
  try {
305
- const content = _chunkTKSRTJX5cjs.lookupFile.call(void 0, context, ["package.json"]);
305
+ const content = _chunkTO6MO344cjs.lookupFile.call(void 0, context, ["package.json"]);
306
306
  if (content)
307
307
  pkg = JSON.parse(content);
308
308
  } catch (e3) {
@@ -357,7 +357,7 @@ var MockCompiler = (_class = class extends _events2.default {
357
357
  const { include, exclude } = this.options;
358
358
  this.fileFilter = _pluginutils.createFilter.call(void 0, include, exclude, { resolve: false });
359
359
  try {
360
- const pkg = _chunkTKSRTJX5cjs.lookupFile.call(void 0, this.cwd, ["package.json"]);
360
+ const pkg = _chunkTO6MO344cjs.lookupFile.call(void 0, this.cwd, ["package.json"]);
361
361
  this.moduleType = !!pkg && JSON.parse(pkg).type === "module" ? "esm" : "cjs";
362
362
  } catch (e5) {
363
363
  }
@@ -394,7 +394,7 @@ var MockCompiler = (_class = class extends _events2.default {
394
394
  isESM: this.moduleType === "esm",
395
395
  cwd: this.cwd
396
396
  });
397
- this._mockData = _chunkTKSRTJX5cjs.transformMockData.call(void 0, _chunkTKSRTJX5cjs.transformRawData.call(void 0, result));
397
+ this._mockData = _chunkTO6MO344cjs.transformMockData.call(void 0, _chunkTO6MO344cjs.transformRawData.call(void 0, result));
398
398
  this.emit("update", this.watchInfo || {});
399
399
  } catch (e) {
400
400
  this.options.logger.error(e.stack || e.message);
@@ -454,7 +454,7 @@ var _cors = require('cors'); var _cors2 = _interopRequireDefault(_cors);
454
454
  var _pathtoregexp = require('path-to-regexp');
455
455
  function createMockMiddleware(compiler, options) {
456
456
  function mockMiddleware(middlewares, reload) {
457
- middlewares.unshift(_chunkTKSRTJX5cjs.baseMiddleware.call(void 0, compiler, options));
457
+ middlewares.unshift(_chunkTO6MO344cjs.baseMiddleware.call(void 0, compiler, options));
458
458
  const corsMiddleware = createCorsMiddleware(compiler, options);
459
459
  if (corsMiddleware) {
460
460
  middlewares.unshift(corsMiddleware);
@@ -477,9 +477,9 @@ function createCorsMiddleware(compiler, options) {
477
477
  }
478
478
  const proxies = options.proxies;
479
479
  return !enabled ? void 0 : function(req, res, next) {
480
- const { pathname } = _chunkTKSRTJX5cjs.urlParse.call(void 0, req.url);
480
+ const { pathname } = _chunkTO6MO344cjs.urlParse.call(void 0, req.url);
481
481
  if (!pathname || proxies.length === 0 || !proxies.some(
482
- (context) => _chunkTKSRTJX5cjs.doesProxyContextMatchUrl.call(void 0, context, req.url, req)
482
+ (context) => _chunkTO6MO344cjs.doesProxyContextMatchUrl.call(void 0, context, req.url, req)
483
483
  )) {
484
484
  return next();
485
485
  }
@@ -511,7 +511,7 @@ function resolvePluginOptions({
511
511
  bodyParserOptions = {},
512
512
  priority = {}
513
513
  } = {}, { alias, context, plugins, proxies }) {
514
- const logger = _chunkTKSRTJX5cjs.createLogger.call(void 0,
514
+ const logger = _chunkTO6MO344cjs.createLogger.call(void 0,
515
515
  "rspack:mock",
516
516
  _utils.isBoolean.call(void 0, log) ? log ? "info" : "error" : log
517
517
  );
package/dist/helper.cjs CHANGED
@@ -1,4 +1,4 @@
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
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
2
  var _utils = require('@pengzhanbo/utils');
3
3
  function defineMock(config) {
4
4
  return config;
@@ -66,7 +66,64 @@ function defineMockData(key, initialData) {
66
66
  return res;
67
67
  }
68
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
+
69
126
 
70
127
 
71
128
 
72
- exports.createDefineMock = createDefineMock; exports.defineMock = defineMock; exports.defineMockData = defineMockData;
129
+ exports.createDefineMock = createDefineMock; exports.createSSEStream = createSSEStream; exports.defineMock = defineMock; exports.defineMockData = defineMockData;
package/dist/helper.d.cts CHANGED
@@ -1,12 +1,12 @@
1
1
  import { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.cjs';
2
2
  export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, c as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.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 'ws';
11
11
 
12
12
  /**
@@ -83,4 +83,40 @@ type MockData<T = any> = readonly [
83
83
  };
84
84
  declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
85
85
 
86
- export { type MockData, MockHttpItem, MockOptions, MockWebsocketItem, createDefineMock, defineMock, defineMockData };
86
+ interface SSEMessage {
87
+ data?: string | object;
88
+ comment?: string;
89
+ event?: string;
90
+ id?: string;
91
+ retry?: number;
92
+ }
93
+ interface WriteHeaders {
94
+ writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
95
+ flushHeaders?: () => void;
96
+ }
97
+ type HeaderStream = NodeJS.WritableStream & WriteHeaders;
98
+ /**
99
+ * Transforms "messages" to W3C event stream content.
100
+ * See https://html.spec.whatwg.org/multipage/server-sent-events.html
101
+ * A message is an object with one or more of the following properties:
102
+ * - data (String or object, which gets turned into JSON)
103
+ * - event
104
+ * - id
105
+ * - retry
106
+ * - comment
107
+ *
108
+ * If constructed with a HTTP Request, it will optimise the socket for streaming.
109
+ * If this stream is piped to an HTTP Response, it will set appropriate headers.
110
+ */
111
+ declare class SSEStream extends Transform {
112
+ constructor(req: IncomingMessage);
113
+ pipe<T extends HeaderStream>(destination: T, options?: {
114
+ end?: boolean;
115
+ }): T;
116
+ _transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
117
+ write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
118
+ write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
119
+ }
120
+ declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
121
+
122
+ 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 { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.js';
2
2
  export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, c as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.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 'ws';
11
11
 
12
12
  /**
@@ -83,4 +83,40 @@ type MockData<T = any> = readonly [
83
83
  };
84
84
  declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
85
85
 
86
- export { type MockData, MockHttpItem, MockOptions, MockWebsocketItem, createDefineMock, defineMock, defineMockData };
86
+ interface SSEMessage {
87
+ data?: string | object;
88
+ comment?: string;
89
+ event?: string;
90
+ id?: string;
91
+ retry?: number;
92
+ }
93
+ interface WriteHeaders {
94
+ writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
95
+ flushHeaders?: () => void;
96
+ }
97
+ type HeaderStream = NodeJS.WritableStream & WriteHeaders;
98
+ /**
99
+ * Transforms "messages" to W3C event stream content.
100
+ * See https://html.spec.whatwg.org/multipage/server-sent-events.html
101
+ * A message is an object with one or more of the following properties:
102
+ * - data (String or object, which gets turned into JSON)
103
+ * - event
104
+ * - id
105
+ * - retry
106
+ * - comment
107
+ *
108
+ * If constructed with a HTTP Request, it will optimise the socket for streaming.
109
+ * If this stream is piped to an HTTP Response, it will set appropriate headers.
110
+ */
111
+ declare class SSEStream extends Transform {
112
+ constructor(req: IncomingMessage);
113
+ pipe<T extends HeaderStream>(destination: T, options?: {
114
+ end?: boolean;
115
+ }): T;
116
+ _transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
117
+ write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
118
+ write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
119
+ }
120
+ declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
121
+
122
+ export { type HeaderStream, type MockData, MockHttpItem, MockOptions, MockWebsocketItem, type SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData };
package/dist/helper.js CHANGED
@@ -65,8 +65,65 @@ function defineMockData(key, initialData) {
65
65
  responseCache.set(cache, res);
66
66
  return res;
67
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
+ }
68
124
  export {
69
125
  createDefineMock,
126
+ createSSEStream,
70
127
  defineMock,
71
128
  defineMockData
72
129
  };
package/dist/index.cjs CHANGED
@@ -4,12 +4,12 @@
4
4
 
5
5
 
6
6
 
7
- var _chunkYAHWW6TXcjs = require('./chunk-YAHWW6TX.cjs');
7
+ var _chunkXLQXERKWcjs = require('./chunk-XLQXERKW.cjs');
8
8
 
9
9
 
10
10
 
11
11
 
12
- var _chunkTKSRTJX5cjs = require('./chunk-TKSRTJX5.cjs');
12
+ var _chunkTO6MO344cjs = require('./chunk-TO6MO344.cjs');
13
13
 
14
14
  // src/rspack.ts
15
15
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
@@ -25,11 +25,11 @@ var MockServerPlugin = class {
25
25
  const compilerOptions = compiler.options;
26
26
  const options = resolvePluginOptions2(compiler, this.options);
27
27
  if (_process2.default.env.NODE_ENV !== "production") {
28
- const mockCompiler = _chunkYAHWW6TXcjs.createMockCompiler.call(void 0, options);
29
- const mockMiddleware = _chunkYAHWW6TXcjs.createMockMiddleware.call(void 0, mockCompiler, options);
28
+ const mockCompiler = _chunkXLQXERKWcjs.createMockCompiler.call(void 0, options);
29
+ const mockMiddleware = _chunkXLQXERKWcjs.createMockMiddleware.call(void 0, mockCompiler, options);
30
30
  const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
31
- const waitServerForMockWebSocket = _chunkTKSRTJX5cjs.waitingFor.call(void 0, (server) => {
32
- _chunkTKSRTJX5cjs.mockWebSocket.call(void 0, mockCompiler, server, options);
31
+ const waitServerForMockWebSocket = _chunkTO6MO344cjs.waitingFor.call(void 0, (server) => {
32
+ _chunkTO6MO344cjs.mockWebSocket.call(void 0, mockCompiler, server, options);
33
33
  });
34
34
  compilerOptions.devServer = {
35
35
  ...compilerOptions.devServer,
@@ -57,7 +57,7 @@ var MockServerPlugin = class {
57
57
  const onProxyReq = item.onProxyReq;
58
58
  item.onProxyReq = (proxyReq, req, ...args) => {
59
59
  _optionalChain([onProxyReq, 'optionalCall', _9 => _9(proxyReq, req, ...args)]);
60
- _chunkTKSRTJX5cjs.rewriteRequest.call(void 0, proxyReq, req);
60
+ _chunkTO6MO344cjs.rewriteRequest.call(void 0, proxyReq, req);
61
61
  };
62
62
  }
63
63
  return item;
@@ -66,7 +66,7 @@ var MockServerPlugin = class {
66
66
  compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
67
67
  compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
68
68
  } else if (options.build !== false) {
69
- compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkYAHWW6TXcjs.buildMockServer.call(void 0,
69
+ compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkXLQXERKWcjs.buildMockServer.call(void 0,
70
70
  options,
71
71
  compilerOptions.output.path || _path2.default.resolve(_process2.default.cwd(), "dist")
72
72
  ));
@@ -86,7 +86,7 @@ function resolvePluginOptions2(compiler, options) {
86
86
  }
87
87
  return [];
88
88
  });
89
- return _chunkYAHWW6TXcjs.resolvePluginOptions.call(void 0, options, {
89
+ return _chunkXLQXERKWcjs.resolvePluginOptions.call(void 0, options, {
90
90
  alias,
91
91
  context,
92
92
  plugins: _utils.toArray.call(void 0, definePluginInstance),
@@ -100,4 +100,4 @@ function resolvePluginOptions2(compiler, options) {
100
100
 
101
101
 
102
102
 
103
- exports.MockCompiler = _chunkYAHWW6TXcjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunkYAHWW6TXcjs.createMockCompiler; exports.createMockMiddleware = _chunkYAHWW6TXcjs.createMockMiddleware; exports.mockWebSocket = _chunkTKSRTJX5cjs.mockWebSocket; exports.resolvePluginOptions = resolvePluginOptions2;
103
+ exports.MockCompiler = _chunkXLQXERKWcjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunkXLQXERKWcjs.createMockCompiler; exports.createMockMiddleware = _chunkXLQXERKWcjs.createMockMiddleware; exports.mockWebSocket = _chunkTO6MO344cjs.mockWebSocket; exports.resolvePluginOptions = resolvePluginOptions2;
package/dist/index.js CHANGED
@@ -4,12 +4,12 @@ import {
4
4
  createMockCompiler,
5
5
  createMockMiddleware,
6
6
  resolvePluginOptions
7
- } from "./chunk-E5FBQV7W.js";
7
+ } from "./chunk-2ZANSHAK.js";
8
8
  import {
9
9
  mockWebSocket,
10
10
  rewriteRequest,
11
11
  waitingFor
12
- } from "./chunk-KN5FLSDL.js";
12
+ } from "./chunk-DCX6CEAX.js";
13
13
 
14
14
  // src/rspack.ts
15
15
  import path from "node:path";
package/dist/rsbuild.cjs CHANGED
@@ -3,11 +3,11 @@
3
3
 
4
4
 
5
5
 
6
- var _chunkYAHWW6TXcjs = require('./chunk-YAHWW6TX.cjs');
6
+ var _chunkXLQXERKWcjs = require('./chunk-XLQXERKW.cjs');
7
7
 
8
8
 
9
9
 
10
- var _chunkTKSRTJX5cjs = require('./chunk-TKSRTJX5.cjs');
10
+ var _chunkTO6MO344cjs = require('./chunk-TO6MO344.cjs');
11
11
 
12
12
  // src/rsbuild.ts
13
13
  var _http = require('http');
@@ -22,7 +22,7 @@ function pluginMockServer(options = {}) {
22
22
  name: "plugin-mock-server",
23
23
  setup(api) {
24
24
  const rsbuildConfig = api.getRsbuildConfig();
25
- const resolvedOptions = _chunkYAHWW6TXcjs.resolvePluginOptions.call(void 0, options, {
25
+ const resolvedOptions = _chunkXLQXERKWcjs.resolvePluginOptions.call(void 0, options, {
26
26
  proxies: resolveConfigProxies(rsbuildConfig),
27
27
  alias: {},
28
28
  context: api.context.rootPath,
@@ -32,7 +32,7 @@ function pluginMockServer(options = {}) {
32
32
  if (resolvedOptions.build) {
33
33
  api.onAfterBuild(async () => {
34
34
  const config = api.getNormalizedConfig();
35
- await _chunkYAHWW6TXcjs.buildMockServer.call(void 0,
35
+ await _chunkXLQXERKWcjs.buildMockServer.call(void 0,
36
36
  resolvedOptions,
37
37
  _path2.default.resolve(_process2.default.cwd(), config.output.distPath.root || "dist")
38
38
  );
@@ -40,10 +40,10 @@ function pluginMockServer(options = {}) {
40
40
  }
41
41
  return;
42
42
  }
43
- const mockCompiler = _chunkYAHWW6TXcjs.createMockCompiler.call(void 0, resolvedOptions);
43
+ const mockCompiler = _chunkXLQXERKWcjs.createMockCompiler.call(void 0, resolvedOptions);
44
44
  api.modifyRsbuildConfig((config) => {
45
45
  updateServerProxyConfigByHttpMock(config);
46
- const mockMiddleware = _chunkYAHWW6TXcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
46
+ const mockMiddleware = _chunkXLQXERKWcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
47
47
  config.dev ??= {};
48
48
  config.dev.setupMiddlewares ??= [];
49
49
  config.dev.setupMiddlewares.push((middlewares, server2) => {
@@ -64,7 +64,7 @@ function pluginMockServer(options = {}) {
64
64
  mockCompiler.run();
65
65
  if (shouldMockWs) {
66
66
  server = _http.createServer.call(void 0, );
67
- _chunkTKSRTJX5cjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
67
+ _chunkTO6MO344cjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
68
68
  server.listen(port);
69
69
  }
70
70
  }
@@ -105,7 +105,7 @@ function updateServerProxyConfigByHttpMock(config) {
105
105
  onError: onError || onProxyError,
106
106
  onProxyReq: (proxyReq, req, ...args) => {
107
107
  _optionalChain([onProxyReq, 'optionalCall', _16 => _16(proxyReq, req, ...args)]);
108
- _chunkTKSRTJX5cjs.rewriteRequest.call(void 0, proxyReq, req);
108
+ _chunkTO6MO344cjs.rewriteRequest.call(void 0, proxyReq, req);
109
109
  }
110
110
  };
111
111
  }
@@ -115,7 +115,7 @@ function updateServerProxyConfigByHttpMock(config) {
115
115
  const onProxyReq = config.server.proxy.onProxyReq;
116
116
  config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
117
117
  _optionalChain([onProxyReq, 'optionalCall', _17 => _17(proxyReq, req, ...args)]);
118
- _chunkTKSRTJX5cjs.rewriteRequest.call(void 0, proxyReq, req);
118
+ _chunkTO6MO344cjs.rewriteRequest.call(void 0, proxyReq, req);
119
119
  };
120
120
  config.server.proxy.onError ??= onProxyError;
121
121
  } else if (config.server.proxy) {
@@ -130,7 +130,7 @@ function updateServerProxyConfigByHttpMock(config) {
130
130
  ...rest,
131
131
  onProxyReq: (proxyReq, req, ...args) => {
132
132
  _optionalChain([onProxyReq, 'optionalCall', _18 => _18(proxyReq, req, ...args)]);
133
- _chunkTKSRTJX5cjs.rewriteRequest.call(void 0, proxyReq, req);
133
+ _chunkTO6MO344cjs.rewriteRequest.call(void 0, proxyReq, req);
134
134
  },
135
135
  onError: onError || onProxyError
136
136
  };
package/dist/rsbuild.js CHANGED
@@ -3,11 +3,11 @@ import {
3
3
  createMockCompiler,
4
4
  createMockMiddleware,
5
5
  resolvePluginOptions
6
- } from "./chunk-E5FBQV7W.js";
6
+ } from "./chunk-2ZANSHAK.js";
7
7
  import {
8
8
  mockWebSocket,
9
9
  rewriteRequest
10
- } from "./chunk-KN5FLSDL.js";
10
+ } from "./chunk-DCX6CEAX.js";
11
11
 
12
12
  // src/rsbuild.ts
13
13
  import { createServer } from "node:http";
package/dist/server.cjs CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- var _chunkTKSRTJX5cjs = require('./chunk-TKSRTJX5.cjs');
9
+ var _chunkTO6MO344cjs = require('./chunk-TO6MO344.cjs');
10
10
 
11
11
 
12
12
 
@@ -15,4 +15,4 @@ var _chunkTKSRTJX5cjs = require('./chunk-TKSRTJX5.cjs');
15
15
 
16
16
 
17
17
 
18
- exports.baseMiddleware = _chunkTKSRTJX5cjs.baseMiddleware; exports.createLogger = _chunkTKSRTJX5cjs.createLogger; exports.logLevels = _chunkTKSRTJX5cjs.logLevels; exports.mockWebSocket = _chunkTKSRTJX5cjs.mockWebSocket; exports.sortByValidator = _chunkTKSRTJX5cjs.sortByValidator; exports.transformMockData = _chunkTKSRTJX5cjs.transformMockData; exports.transformRawData = _chunkTKSRTJX5cjs.transformRawData;
18
+ exports.baseMiddleware = _chunkTO6MO344cjs.baseMiddleware; exports.createLogger = _chunkTO6MO344cjs.createLogger; exports.logLevels = _chunkTO6MO344cjs.logLevels; exports.mockWebSocket = _chunkTO6MO344cjs.mockWebSocket; exports.sortByValidator = _chunkTO6MO344cjs.sortByValidator; exports.transformMockData = _chunkTO6MO344cjs.transformMockData; exports.transformRawData = _chunkTO6MO344cjs.transformRawData;
package/dist/server.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  sortByValidator,
7
7
  transformMockData,
8
8
  transformRawData
9
- } from "./chunk-KN5FLSDL.js";
9
+ } from "./chunk-DCX6CEAX.js";
10
10
  export {
11
11
  baseMiddleware,
12
12
  createLogger,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rspack-plugin-mock",
3
3
  "type": "module",
4
- "version": "0.5.0",
4
+ "version": "1.0.0",
5
5
  "description": "inject api mock server to development server",
6
6
  "author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
7
7
  "license": "MIT",
@@ -86,19 +86,19 @@
86
86
  }
87
87
  },
88
88
  "dependencies": {
89
- "@pengzhanbo/utils": "^1.1.2",
89
+ "@pengzhanbo/utils": "^1.2.0",
90
90
  "@rollup/pluginutils": "^5.1.3",
91
91
  "chokidar": "3.6.0",
92
92
  "co-body": "^6.2.0",
93
93
  "cookies": "^0.9.1",
94
94
  "cors": "^2.8.5",
95
- "debug": "^4.3.7",
95
+ "debug": "^4.4.0",
96
96
  "fast-glob": "^3.3.2",
97
97
  "formidable": "2.1.2",
98
98
  "http-status": "^2.0.0",
99
99
  "is-core-module": "^2.15.1",
100
100
  "json5": "^2.2.3",
101
- "memfs": "^4.14.0",
101
+ "memfs": "^4.15.0",
102
102
  "mime-types": "^2.1.35",
103
103
  "path-to-regexp": "7.1.0",
104
104
  "picocolors": "^1.1.1",
@@ -106,9 +106,9 @@
106
106
  "ws": "^8.18.0"
107
107
  },
108
108
  "devDependencies": {
109
- "@pengzhanbo/eslint-config": "^1.18.2",
110
- "@rsbuild/core": "^1.1.0",
111
- "@rspack/core": "^1.1.1",
109
+ "@pengzhanbo/eslint-config": "^1.21.0",
110
+ "@rsbuild/core": "^1.1.10",
111
+ "@rspack/core": "^1.1.6",
112
112
  "@types/co-body": "^6.1.3",
113
113
  "@types/cookies": "^0.9.0",
114
114
  "@types/cors": "^2.8.17",
@@ -116,15 +116,15 @@
116
116
  "@types/formidable": "2.0.6",
117
117
  "@types/is-core-module": "^2.2.2",
118
118
  "@types/mime-types": "^2.1.4",
119
- "@types/node": "^22.9.0",
119
+ "@types/node": "^22.10.2",
120
120
  "@types/ws": "^8.5.13",
121
- "bumpp": "^9.8.1",
121
+ "bumpp": "^9.9.1",
122
122
  "conventional-changelog-cli": "^5.0.0",
123
- "eslint": "^9.14.0",
124
- "husky": "^9.1.6",
125
- "lint-staged": "^15.2.10",
123
+ "eslint": "^9.16.0",
124
+ "husky": "^9.1.7",
125
+ "lint-staged": "^15.2.11",
126
126
  "tsup": "^8.3.5",
127
- "typescript": "^5.6.3"
127
+ "typescript": "^5.7.2"
128
128
  },
129
129
  "lint-staged": {
130
130
  "*": "eslint --fix"