rspack-plugin-mock 0.3.4 → 0.4.1
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 +2 -2
- package/README.zh-CN.md +2 -2
- package/dist/{chunk-O6PRDW23.js → chunk-4XOUX6CL.js} +127 -125
- package/dist/{chunk-EY46RSAC.js → chunk-JMXFIX5Q.js} +274 -272
- package/dist/{chunk-ZEC4FWWY.cjs → chunk-TKSRTJX5.cjs} +254 -252
- package/dist/{chunk-HLMEDDGX.cjs → chunk-YAHWW6TX.cjs} +127 -125
- package/dist/helper.cjs +55 -2
- package/dist/helper.d.cts +21 -7
- package/dist/helper.d.ts +21 -7
- package/dist/helper.js +54 -1
- package/dist/index.cjs +25 -25
- package/dist/index.d.cts +9 -9
- package/dist/index.d.ts +9 -9
- package/dist/index.js +14 -14
- package/dist/{mockWebsocket-CPuTAvL0.d.ts → mockWebsocket-DBgZBsdo.d.ts} +15 -15
- package/dist/{mockWebsocket-Dc9CZBfv.d.cts → mockWebsocket-Ki_cShTv.d.cts} +15 -15
- package/dist/rsbuild.cjs +17 -17
- package/dist/rsbuild.d.cts +5 -5
- package/dist/rsbuild.d.ts +5 -5
- package/dist/rsbuild.js +8 -8
- package/dist/server.cjs +2 -2
- package/dist/server.d.cts +8 -8
- package/dist/server.d.ts +8 -8
- package/dist/server.js +1 -1
- package/dist/{types-BgpcN3jm.d.cts → types-Aw0AciTG.d.cts} +5 -5
- package/dist/{types-BgpcN3jm.d.ts → types-Aw0AciTG.d.ts} +5 -5
- package/package.json +20 -20
|
@@ -9,121 +9,23 @@
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
// src/core/mockMiddleware.ts
|
|
15
|
-
var _cors = require('cors'); var _cors2 = _interopRequireDefault(_cors);
|
|
16
|
-
var _pathtoregexp = require('path-to-regexp');
|
|
17
|
-
function createMockMiddleware(compiler, options) {
|
|
18
|
-
function mockMiddleware(middlewares, reload) {
|
|
19
|
-
middlewares.unshift(_chunkZEC4FWWYcjs.baseMiddleware.call(void 0, compiler, options));
|
|
20
|
-
const corsMiddleware = createCorsMiddleware(compiler, options);
|
|
21
|
-
if (corsMiddleware) {
|
|
22
|
-
middlewares.unshift(corsMiddleware);
|
|
23
|
-
}
|
|
24
|
-
if (options.reload) {
|
|
25
|
-
compiler.on("update", () => _optionalChain([reload, 'optionalCall', _ => _()]));
|
|
26
|
-
}
|
|
27
|
-
return middlewares;
|
|
28
|
-
}
|
|
29
|
-
return mockMiddleware;
|
|
30
|
-
}
|
|
31
|
-
function createCorsMiddleware(compiler, options) {
|
|
32
|
-
let corsOptions = {};
|
|
33
|
-
const enabled = options.cors !== false;
|
|
34
|
-
if (enabled) {
|
|
35
|
-
corsOptions = {
|
|
36
|
-
...corsOptions,
|
|
37
|
-
...typeof options.cors === "boolean" ? {} : options.cors
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
const proxies = options.proxies;
|
|
41
|
-
return !enabled ? void 0 : function(req, res, next) {
|
|
42
|
-
const { pathname } = _chunkZEC4FWWYcjs.urlParse.call(void 0, req.url);
|
|
43
|
-
if (!pathname || proxies.length === 0 || !proxies.some(
|
|
44
|
-
(context) => _chunkZEC4FWWYcjs.doesProxyContextMatchUrl.call(void 0, context, req.url, req)
|
|
45
|
-
)) {
|
|
46
|
-
return next();
|
|
47
|
-
}
|
|
48
|
-
const mockData = compiler.mockData;
|
|
49
|
-
const mockUrl = Object.keys(mockData).find(
|
|
50
|
-
(key) => _pathtoregexp.pathToRegexp.call(void 0, key).test(pathname)
|
|
51
|
-
);
|
|
52
|
-
if (!mockUrl)
|
|
53
|
-
return next();
|
|
54
|
-
_cors2.default.call(void 0, corsOptions)(req, res, next);
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/core/resolvePluginOptions.ts
|
|
59
|
-
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
60
|
-
var _utils = require('@pengzhanbo/utils');
|
|
61
|
-
function resolvePluginOptions({
|
|
62
|
-
prefix = [],
|
|
63
|
-
wsPrefix = [],
|
|
64
|
-
cwd,
|
|
65
|
-
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
66
|
-
exclude = ["**/node_modules/**", "**/.vscode/**", "**/.git/**"],
|
|
67
|
-
reload = false,
|
|
68
|
-
log = "info",
|
|
69
|
-
cors: cors2 = true,
|
|
70
|
-
formidableOptions = {},
|
|
71
|
-
build = false,
|
|
72
|
-
cookiesOptions = {},
|
|
73
|
-
bodyParserOptions = {},
|
|
74
|
-
priority = {}
|
|
75
|
-
} = {}, { alias, context, plugins, proxies }) {
|
|
76
|
-
const logger = _chunkZEC4FWWYcjs.createLogger.call(void 0,
|
|
77
|
-
"rspack:mock",
|
|
78
|
-
_utils.isBoolean.call(void 0, log) ? log ? "info" : "error" : log
|
|
79
|
-
);
|
|
80
|
-
return {
|
|
81
|
-
prefix,
|
|
82
|
-
wsPrefix,
|
|
83
|
-
cwd: cwd || context || _process2.default.cwd(),
|
|
84
|
-
include,
|
|
85
|
-
exclude,
|
|
86
|
-
reload,
|
|
87
|
-
cors: cors2,
|
|
88
|
-
cookiesOptions,
|
|
89
|
-
log,
|
|
90
|
-
formidableOptions: {
|
|
91
|
-
multiples: true,
|
|
92
|
-
...formidableOptions
|
|
93
|
-
},
|
|
94
|
-
bodyParserOptions,
|
|
95
|
-
priority,
|
|
96
|
-
build: build ? Object.assign(
|
|
97
|
-
{
|
|
98
|
-
serverPort: 8080,
|
|
99
|
-
dist: "mockServer",
|
|
100
|
-
log: "error"
|
|
101
|
-
},
|
|
102
|
-
typeof build === "object" ? build : {}
|
|
103
|
-
) : false,
|
|
104
|
-
alias,
|
|
105
|
-
plugins,
|
|
106
|
-
proxies,
|
|
107
|
-
wsProxies: _utils.toArray.call(void 0, wsPrefix),
|
|
108
|
-
logger
|
|
109
|
-
};
|
|
110
|
-
}
|
|
12
|
+
var _chunkTKSRTJX5cjs = require('./chunk-TKSRTJX5.cjs');
|
|
111
13
|
|
|
112
14
|
// src/core/build.ts
|
|
113
15
|
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
|
|
114
16
|
var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(_promises);
|
|
115
17
|
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
116
|
-
|
|
117
|
-
var
|
|
18
|
+
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
19
|
+
var _utils = require('@pengzhanbo/utils');
|
|
118
20
|
var _pluginutils = require('@rollup/pluginutils');
|
|
21
|
+
var _fastglob = require('fast-glob'); var _fastglob2 = _interopRequireDefault(_fastglob);
|
|
119
22
|
var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
|
|
120
23
|
|
|
121
|
-
|
|
122
24
|
// src/core/createRspackCompiler.ts
|
|
123
25
|
var _module = require('module');
|
|
124
26
|
var _core = require('@rspack/core'); var rspackCore = _interopRequireWildcard(_core);
|
|
125
|
-
|
|
126
27
|
var _iscoremodule = require('is-core-module'); var _iscoremodule2 = _interopRequireDefault(_iscoremodule);
|
|
28
|
+
|
|
127
29
|
var require2 = _module.createRequire.call(void 0, import.meta.url);
|
|
128
30
|
function createCompiler(options, callback) {
|
|
129
31
|
const rspackOptions = resolveRspackOptions(options);
|
|
@@ -144,17 +46,17 @@ function createCompiler(options, callback) {
|
|
|
144
46
|
}
|
|
145
47
|
return;
|
|
146
48
|
}
|
|
147
|
-
if (_optionalChain([stats, 'optionalAccess',
|
|
49
|
+
if (_optionalChain([stats, 'optionalAccess', _ => _.hasErrors, 'call', _2 => _2()])) {
|
|
148
50
|
const info = stats.toJson();
|
|
149
51
|
logError(info.errors);
|
|
150
52
|
}
|
|
151
|
-
const code =
|
|
53
|
+
const code = _chunkTKSRTJX5cjs.vfs.readFileSync("/output.js", "utf-8");
|
|
152
54
|
const externals = [];
|
|
153
55
|
if (!isWatch) {
|
|
154
|
-
const modules = _optionalChain([stats, 'optionalAccess',
|
|
56
|
+
const modules = _optionalChain([stats, 'optionalAccess', _3 => _3.toJson, 'call', _4 => _4(), 'access', _5 => _5.modules]) || [];
|
|
155
57
|
const aliasList = Object.keys(options.alias || {}).map((key) => key.replace(/\$$/g, ""));
|
|
156
58
|
for (const { name: name2 } of modules) {
|
|
157
|
-
if (_optionalChain([name2, 'optionalAccess',
|
|
59
|
+
if (_optionalChain([name2, 'optionalAccess', _6 => _6.startsWith, 'call', _7 => _7("external")])) {
|
|
158
60
|
const packageName = normalizePackageName(name2);
|
|
159
61
|
if (!_iscoremodule2.default.call(void 0, packageName) && !aliasList.includes(packageName))
|
|
160
62
|
externals.push(normalizePackageName(name2));
|
|
@@ -165,9 +67,9 @@ function createCompiler(options, callback) {
|
|
|
165
67
|
}
|
|
166
68
|
const compiler = rspackCore.rspack(rspackOptions, isWatch ? handler : void 0);
|
|
167
69
|
if (compiler)
|
|
168
|
-
compiler.outputFileSystem =
|
|
70
|
+
compiler.outputFileSystem = _chunkTKSRTJX5cjs.vfs;
|
|
169
71
|
if (!isWatch) {
|
|
170
|
-
_optionalChain([compiler, 'optionalAccess',
|
|
72
|
+
_optionalChain([compiler, 'optionalAccess', _8 => _8.run, 'call', _9 => _9(async (...args) => {
|
|
171
73
|
await handler(...args);
|
|
172
74
|
compiler.close(() => {
|
|
173
75
|
});
|
|
@@ -199,6 +101,9 @@ function resolveRspackOptions({
|
|
|
199
101
|
watch = false
|
|
200
102
|
}) {
|
|
201
103
|
const targets = ["node >= 18.0.0"];
|
|
104
|
+
if (alias && "@swc/helpers" in alias) {
|
|
105
|
+
delete alias["@swc/helpers"];
|
|
106
|
+
}
|
|
202
107
|
return {
|
|
203
108
|
mode: "production",
|
|
204
109
|
context: cwd,
|
|
@@ -206,7 +111,6 @@ function resolveRspackOptions({
|
|
|
206
111
|
watch,
|
|
207
112
|
target: "node18.0",
|
|
208
113
|
externalsType: isEsm ? "module" : "commonjs2",
|
|
209
|
-
externals: /^[^./].*/,
|
|
210
114
|
resolve: {
|
|
211
115
|
alias,
|
|
212
116
|
extensions: [".js", ".ts", ".cjs", ".mjs", ".json5", ".json"]
|
|
@@ -366,7 +270,7 @@ async function writeMockEntryFile(entryFile, files, cwd) {
|
|
|
366
270
|
const importers = [];
|
|
367
271
|
const exporters = [];
|
|
368
272
|
for (const [index, filepath] of files.entries()) {
|
|
369
|
-
const file =
|
|
273
|
+
const file = _chunkTKSRTJX5cjs.normalizePath.call(void 0, _path2.default.join(cwd, filepath));
|
|
370
274
|
importers.push(`import * as m${index} from '${file}'`);
|
|
371
275
|
exporters.push(`[m${index}, '${filepath}']`);
|
|
372
276
|
}
|
|
@@ -384,7 +288,7 @@ export default [
|
|
|
384
288
|
function getPluginPackageInfo() {
|
|
385
289
|
let pkg = {};
|
|
386
290
|
try {
|
|
387
|
-
const filepath = _path2.default.join(
|
|
291
|
+
const filepath = _path2.default.join(_chunkTKSRTJX5cjs.packageDir, "../package.json");
|
|
388
292
|
if (_fs2.default.existsSync(filepath)) {
|
|
389
293
|
pkg = JSON.parse(_fs2.default.readFileSync(filepath, "utf8"));
|
|
390
294
|
}
|
|
@@ -398,7 +302,7 @@ function getPluginPackageInfo() {
|
|
|
398
302
|
function getHostDependencies(context) {
|
|
399
303
|
let pkg = {};
|
|
400
304
|
try {
|
|
401
|
-
const content =
|
|
305
|
+
const content = _chunkTKSRTJX5cjs.lookupFile.call(void 0, context, ["package.json"]);
|
|
402
306
|
if (content)
|
|
403
307
|
pkg = JSON.parse(content);
|
|
404
308
|
} catch (e3) {
|
|
@@ -411,8 +315,8 @@ var _events = require('events'); var _events2 = _interopRequireDefault(_events);
|
|
|
411
315
|
|
|
412
316
|
|
|
413
317
|
|
|
414
|
-
var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar);
|
|
415
318
|
|
|
319
|
+
var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar);
|
|
416
320
|
|
|
417
321
|
|
|
418
322
|
// src/core/loadFromCode.ts
|
|
@@ -426,17 +330,16 @@ async function loadFromCode({
|
|
|
426
330
|
cwd
|
|
427
331
|
}) {
|
|
428
332
|
filepath = _path2.default.resolve(cwd, filepath);
|
|
429
|
-
const fileBase = `${filepath}.timestamp-${Date.now()}`;
|
|
430
333
|
const ext = isESM ? ".mjs" : ".cjs";
|
|
431
|
-
const
|
|
432
|
-
const
|
|
433
|
-
await _fs.promises.writeFile(
|
|
334
|
+
const filepathTmp = `${filepath}.timestamp-${Date.now()}${ext}`;
|
|
335
|
+
const file = _url.pathToFileURL.call(void 0, filepathTmp).toString();
|
|
336
|
+
await _fs.promises.writeFile(filepathTmp, code, "utf8");
|
|
434
337
|
try {
|
|
435
|
-
const
|
|
436
|
-
return
|
|
338
|
+
const mod = await Promise.resolve().then(() => _interopRequireWildcard(require(file)));
|
|
339
|
+
return mod.default || mod;
|
|
437
340
|
} finally {
|
|
438
341
|
try {
|
|
439
|
-
_fs2.default.unlinkSync(
|
|
342
|
+
_fs2.default.unlinkSync(filepathTmp);
|
|
440
343
|
} catch (e4) {
|
|
441
344
|
}
|
|
442
345
|
}
|
|
@@ -454,7 +357,7 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
454
357
|
const { include, exclude } = this.options;
|
|
455
358
|
this.fileFilter = _pluginutils.createFilter.call(void 0, include, exclude, { resolve: false });
|
|
456
359
|
try {
|
|
457
|
-
const pkg =
|
|
360
|
+
const pkg = _chunkTKSRTJX5cjs.lookupFile.call(void 0, this.cwd, ["package.json"]);
|
|
458
361
|
this.moduleType = !!pkg && JSON.parse(pkg).type === "module" ? "esm" : "cjs";
|
|
459
362
|
} catch (e5) {
|
|
460
363
|
}
|
|
@@ -491,7 +394,7 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
491
394
|
isESM: this.moduleType === "esm",
|
|
492
395
|
cwd: this.cwd
|
|
493
396
|
});
|
|
494
|
-
this._mockData =
|
|
397
|
+
this._mockData = _chunkTKSRTJX5cjs.transformMockData.call(void 0, _chunkTKSRTJX5cjs.transformRawData.call(void 0, result));
|
|
495
398
|
this.emit("update", this.watchInfo || {});
|
|
496
399
|
} catch (e) {
|
|
497
400
|
this.options.logger.error(e.stack || e.message);
|
|
@@ -500,7 +403,7 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
500
403
|
}
|
|
501
404
|
close() {
|
|
502
405
|
this.mockWatcher.close();
|
|
503
|
-
_optionalChain([this, 'access',
|
|
406
|
+
_optionalChain([this, 'access', _10 => _10.compiler, 'optionalAccess', _11 => _11.close, 'call', _12 => _12(() => {
|
|
504
407
|
})]);
|
|
505
408
|
this.emit("close");
|
|
506
409
|
}
|
|
@@ -546,10 +449,109 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
546
449
|
}
|
|
547
450
|
}, _class);
|
|
548
451
|
|
|
452
|
+
// src/core/mockMiddleware.ts
|
|
453
|
+
var _cors = require('cors'); var _cors2 = _interopRequireDefault(_cors);
|
|
454
|
+
var _pathtoregexp = require('path-to-regexp');
|
|
455
|
+
function createMockMiddleware(compiler, options) {
|
|
456
|
+
function mockMiddleware(middlewares, reload) {
|
|
457
|
+
middlewares.unshift(_chunkTKSRTJX5cjs.baseMiddleware.call(void 0, compiler, options));
|
|
458
|
+
const corsMiddleware = createCorsMiddleware(compiler, options);
|
|
459
|
+
if (corsMiddleware) {
|
|
460
|
+
middlewares.unshift(corsMiddleware);
|
|
461
|
+
}
|
|
462
|
+
if (options.reload) {
|
|
463
|
+
compiler.on("update", () => _optionalChain([reload, 'optionalCall', _13 => _13()]));
|
|
464
|
+
}
|
|
465
|
+
return middlewares;
|
|
466
|
+
}
|
|
467
|
+
return mockMiddleware;
|
|
468
|
+
}
|
|
469
|
+
function createCorsMiddleware(compiler, options) {
|
|
470
|
+
let corsOptions = {};
|
|
471
|
+
const enabled = options.cors !== false;
|
|
472
|
+
if (enabled) {
|
|
473
|
+
corsOptions = {
|
|
474
|
+
...corsOptions,
|
|
475
|
+
...typeof options.cors === "boolean" ? {} : options.cors
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
const proxies = options.proxies;
|
|
479
|
+
return !enabled ? void 0 : function(req, res, next) {
|
|
480
|
+
const { pathname } = _chunkTKSRTJX5cjs.urlParse.call(void 0, req.url);
|
|
481
|
+
if (!pathname || proxies.length === 0 || !proxies.some(
|
|
482
|
+
(context) => _chunkTKSRTJX5cjs.doesProxyContextMatchUrl.call(void 0, context, req.url, req)
|
|
483
|
+
)) {
|
|
484
|
+
return next();
|
|
485
|
+
}
|
|
486
|
+
const mockData = compiler.mockData;
|
|
487
|
+
const mockUrl = Object.keys(mockData).find(
|
|
488
|
+
(key) => _pathtoregexp.pathToRegexp.call(void 0, key).test(pathname)
|
|
489
|
+
);
|
|
490
|
+
if (!mockUrl)
|
|
491
|
+
return next();
|
|
492
|
+
_cors2.default.call(void 0, corsOptions)(req, res, next);
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// src/core/resolvePluginOptions.ts
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
function resolvePluginOptions({
|
|
500
|
+
prefix = [],
|
|
501
|
+
wsPrefix = [],
|
|
502
|
+
cwd,
|
|
503
|
+
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
504
|
+
exclude = ["**/node_modules/**", "**/.vscode/**", "**/.git/**"],
|
|
505
|
+
reload = false,
|
|
506
|
+
log = "info",
|
|
507
|
+
cors: cors2 = true,
|
|
508
|
+
formidableOptions = {},
|
|
509
|
+
build = false,
|
|
510
|
+
cookiesOptions = {},
|
|
511
|
+
bodyParserOptions = {},
|
|
512
|
+
priority = {}
|
|
513
|
+
} = {}, { alias, context, plugins, proxies }) {
|
|
514
|
+
const logger = _chunkTKSRTJX5cjs.createLogger.call(void 0,
|
|
515
|
+
"rspack:mock",
|
|
516
|
+
_utils.isBoolean.call(void 0, log) ? log ? "info" : "error" : log
|
|
517
|
+
);
|
|
518
|
+
prefix = _utils.toArray.call(void 0, prefix);
|
|
519
|
+
return {
|
|
520
|
+
prefix,
|
|
521
|
+
wsPrefix,
|
|
522
|
+
cwd: cwd || context || _process2.default.cwd(),
|
|
523
|
+
include,
|
|
524
|
+
exclude,
|
|
525
|
+
reload,
|
|
526
|
+
cors: cors2,
|
|
527
|
+
cookiesOptions,
|
|
528
|
+
log,
|
|
529
|
+
formidableOptions: {
|
|
530
|
+
multiples: true,
|
|
531
|
+
...formidableOptions
|
|
532
|
+
},
|
|
533
|
+
bodyParserOptions,
|
|
534
|
+
priority,
|
|
535
|
+
build: build ? Object.assign(
|
|
536
|
+
{
|
|
537
|
+
serverPort: 8080,
|
|
538
|
+
dist: "mockServer",
|
|
539
|
+
log: "error"
|
|
540
|
+
},
|
|
541
|
+
typeof build === "object" ? build : {}
|
|
542
|
+
) : false,
|
|
543
|
+
alias,
|
|
544
|
+
plugins,
|
|
545
|
+
proxies: [...proxies, ...prefix],
|
|
546
|
+
wsProxies: _utils.toArray.call(void 0, wsPrefix),
|
|
547
|
+
logger
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
549
551
|
|
|
550
552
|
|
|
551
553
|
|
|
552
554
|
|
|
553
555
|
|
|
554
556
|
|
|
555
|
-
exports.
|
|
557
|
+
exports.buildMockServer = buildMockServer; exports.createMockCompiler = createMockCompiler; exports.MockCompiler = MockCompiler; exports.createMockMiddleware = createMockMiddleware; exports.resolvePluginOptions = resolvePluginOptions;
|
package/dist/helper.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true})
|
|
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
2
|
var _utils = require('@pengzhanbo/utils');
|
|
3
3
|
function defineMock(config) {
|
|
4
4
|
return config;
|
|
@@ -14,6 +14,59 @@ function createDefineMock(transformer) {
|
|
|
14
14
|
return define;
|
|
15
15
|
}
|
|
16
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
|
+
|
|
17
70
|
|
|
18
71
|
|
|
19
|
-
exports.createDefineMock = createDefineMock; exports.defineMock = defineMock;
|
|
72
|
+
exports.createDefineMock = createDefineMock; exports.defineMock = defineMock; exports.defineMockData = defineMockData;
|
package/dist/helper.d.cts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
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,
|
|
3
|
-
import '
|
|
4
|
-
import 'node:http';
|
|
5
|
-
import 'node:stream';
|
|
1
|
+
import { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.cjs';
|
|
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 'co-body';
|
|
6
4
|
import 'cookies';
|
|
7
5
|
import 'cors';
|
|
8
6
|
import 'formidable';
|
|
7
|
+
import 'node:buffer';
|
|
8
|
+
import 'node:http';
|
|
9
|
+
import 'node:stream';
|
|
9
10
|
import 'ws';
|
|
10
|
-
import 'co-body';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* mock config Type helper
|
|
@@ -69,4 +69,18 @@ declare function defineMock(config: MockOptions): MockOptions;
|
|
|
69
69
|
*/
|
|
70
70
|
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
type MockData<T = any> = readonly [
|
|
73
|
+
/**
|
|
74
|
+
* getter
|
|
75
|
+
*/
|
|
76
|
+
() => T,
|
|
77
|
+
/**
|
|
78
|
+
* setter
|
|
79
|
+
*/
|
|
80
|
+
(val: T | ((val: T) => T | void)) => void
|
|
81
|
+
] & {
|
|
82
|
+
value: T;
|
|
83
|
+
};
|
|
84
|
+
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
|
|
85
|
+
|
|
86
|
+
export { type MockData, MockHttpItem, MockOptions, MockWebsocketItem, createDefineMock, defineMock, defineMockData };
|
package/dist/helper.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
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,
|
|
3
|
-
import '
|
|
4
|
-
import 'node:http';
|
|
5
|
-
import 'node:stream';
|
|
1
|
+
import { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.js';
|
|
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 'co-body';
|
|
6
4
|
import 'cookies';
|
|
7
5
|
import 'cors';
|
|
8
6
|
import 'formidable';
|
|
7
|
+
import 'node:buffer';
|
|
8
|
+
import 'node:http';
|
|
9
|
+
import 'node:stream';
|
|
9
10
|
import 'ws';
|
|
10
|
-
import 'co-body';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* mock config Type helper
|
|
@@ -69,4 +69,18 @@ declare function defineMock(config: MockOptions): MockOptions;
|
|
|
69
69
|
*/
|
|
70
70
|
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
type MockData<T = any> = readonly [
|
|
73
|
+
/**
|
|
74
|
+
* getter
|
|
75
|
+
*/
|
|
76
|
+
() => T,
|
|
77
|
+
/**
|
|
78
|
+
* setter
|
|
79
|
+
*/
|
|
80
|
+
(val: T | ((val: T) => T | void)) => void
|
|
81
|
+
] & {
|
|
82
|
+
value: T;
|
|
83
|
+
};
|
|
84
|
+
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
|
|
85
|
+
|
|
86
|
+
export { type MockData, MockHttpItem, MockOptions, MockWebsocketItem, createDefineMock, defineMock, defineMockData };
|
package/dist/helper.js
CHANGED
|
@@ -13,7 +13,60 @@ function createDefineMock(transformer) {
|
|
|
13
13
|
};
|
|
14
14
|
return define;
|
|
15
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
|
+
}
|
|
16
68
|
export {
|
|
17
69
|
createDefineMock,
|
|
18
|
-
defineMock
|
|
70
|
+
defineMock,
|
|
71
|
+
defineMockData
|
|
19
72
|
};
|