vite-plugin-mock-dev-server 2.0.5 → 2.0.7

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,1005 +0,0 @@
1
- import { hasOwn, isArray, isBoolean, isEmptyObject, isFunction, isPlainObject, random, sleep, sortBy, timestamp, toArray, uniq } from "@pengzhanbo/utils";
2
- import path from "node:path";
3
- import ansis from "ansis";
4
- import picomatch from "picomatch";
5
- import { match, parse, pathToRegexp } from "path-to-regexp";
6
- import os from "node:os";
7
- import Debug from "debug";
8
- import { parse as parse$1 } from "node:querystring";
9
- import bodyParser from "co-body";
10
- import formidable from "formidable";
11
- import http from "node:http";
12
- import crypto from "node:crypto";
13
- import { Buffer } from "node:buffer";
14
- import HTTP_STATUS from "http-status";
15
- import * as mime from "mime-types";
16
- import { WebSocketServer } from "ws";
17
-
18
- //#region src/utils/createMatcher.ts
19
- function createMatcher(include, exclude) {
20
- const pattern = [];
21
- const ignore = ["**/node_modules/**", ...toArray(exclude)];
22
- toArray(include).forEach((item) => {
23
- if (item[0] === "!") ignore.push(item.slice(1));
24
- else pattern.push(item);
25
- });
26
- return {
27
- pattern,
28
- ignore,
29
- isMatch: picomatch(pattern, { ignore })
30
- };
31
- }
32
-
33
- //#endregion
34
- //#region src/utils/doesProxyContextMatchUrl.ts
35
- function doesProxyContextMatchUrl(context, url) {
36
- return context[0] === "^" && new RegExp(context).test(url) || url.startsWith(context);
37
- }
38
-
39
- //#endregion
40
- //#region src/utils/is.ts
41
- function isStream(stream) {
42
- return stream !== null && typeof stream === "object" && typeof stream.pipe === "function";
43
- }
44
- function isReadableStream(stream) {
45
- return isStream(stream) && stream.readable !== false && typeof stream._read === "function" && typeof stream._readableState === "object";
46
- }
47
- const PACKAGE_CACHE = {};
48
- async function isPackageExists(mod) {
49
- if (hasOwn(PACKAGE_CACHE, mod)) return PACKAGE_CACHE[mod];
50
- try {
51
- if (import.meta.resolve) PACKAGE_CACHE[mod] = !!import.meta.resolve(mod);
52
- else {
53
- await import(mod);
54
- PACKAGE_CACHE[mod] = true;
55
- }
56
- return PACKAGE_CACHE[mod];
57
- } catch {}
58
- PACKAGE_CACHE[mod] = false;
59
- return false;
60
- }
61
-
62
- //#endregion
63
- //#region src/utils/isObjectSubset.ts
64
- /**
65
- * Checks if target object is a subset of source object.
66
- * That is, all properties and their corresponding values in target exist in source.
67
- *
68
- * 深度比较两个对象之间,target 是否属于 source 的子集,
69
- * 即 target 的所有属性和对应的值,都在 source 中,
70
- */
71
- function isObjectSubset(source, target) {
72
- if (!target) return true;
73
- for (const key in target) if (!isIncluded(source[key], target[key])) return false;
74
- return true;
75
- }
76
- function isIncluded(source, target) {
77
- if (isArray(source) && isArray(target)) {
78
- const seen = /* @__PURE__ */ new Set();
79
- return target.every((ti) => source.some((si, i) => {
80
- if (seen.has(i)) return false;
81
- const included = isIncluded(si, ti);
82
- if (included) seen.add(i);
83
- return included;
84
- }));
85
- }
86
- if (isPlainObject(source) && isPlainObject(target)) return isObjectSubset(source, target);
87
- return Object.is(source, target);
88
- }
89
-
90
- //#endregion
91
- //#region src/utils/isPathMatch.ts
92
- const cache$1 = /* @__PURE__ */ new Map();
93
- /**
94
- * 判断 path 是否匹配 pattern
95
- */
96
- function isPathMatch(pattern, path$1) {
97
- let regexp = cache$1.get(pattern);
98
- if (!regexp) {
99
- regexp = pathToRegexp(pattern).regexp;
100
- cache$1.set(pattern, regexp);
101
- }
102
- return regexp.test(path$1);
103
- }
104
-
105
- //#endregion
106
- //#region src/utils/logger.ts
107
- const logLevels = {
108
- silent: 0,
109
- error: 1,
110
- warn: 2,
111
- info: 3,
112
- debug: 4
113
- };
114
- function createLogger(prefix, defaultLevel = "info") {
115
- prefix = `[${prefix}]`;
116
- function output(type, msg, level) {
117
- level = isBoolean(level) ? level ? defaultLevel : "error" : level;
118
- if (logLevels[level] >= logLevels[type]) {
119
- const method = type === "info" || type === "debug" ? "log" : type;
120
- const tag = type === "debug" ? ansis.magenta.bold(prefix) : type === "info" ? ansis.cyan.bold(prefix) : type === "warn" ? ansis.yellow.bold(prefix) : ansis.red.bold(prefix);
121
- const format = `${ansis.dim((/* @__PURE__ */ new Date()).toLocaleTimeString())} ${tag} ${msg}`;
122
- console[method](format);
123
- }
124
- }
125
- return {
126
- debug(msg, level = defaultLevel) {
127
- output("debug", msg, level);
128
- },
129
- info(msg, level = defaultLevel) {
130
- output("info", msg, level);
131
- },
132
- warn(msg, level = defaultLevel) {
133
- output("warn", msg, level);
134
- },
135
- error(msg, level = defaultLevel) {
136
- output("error", msg, level);
137
- }
138
- };
139
- }
140
-
141
- //#endregion
142
- //#region src/utils/shared.ts
143
- const debug = Debug("vite:mock-dev-server");
144
- const windowsSlashRE = /\\/g;
145
- const isWindows = os.platform() === "win32";
146
- function slash(p) {
147
- return p.replace(windowsSlashRE, "/");
148
- }
149
- function normalizePath(id) {
150
- return path.posix.normalize(isWindows ? slash(id) : id);
151
- }
152
-
153
- //#endregion
154
- //#region src/utils/urlParse.ts
155
- /**
156
- * nodejs 从 19.0.0 开始 弃用 url.parse,因此使用 url.parse 来解析 可能会报错,
157
- * 使用 URL 来解析
158
- */
159
- function urlParse(input) {
160
- const url = new URL(input, "http://example.com");
161
- return {
162
- pathname: decodeURIComponent(url.pathname),
163
- query: parse$1(url.search.replace(/^\?/, ""))
164
- };
165
- }
166
-
167
- //#endregion
168
- //#region src/compiler/processData.ts
169
- function processRawData(raw, __filepath__) {
170
- let res;
171
- if (isArray(raw)) res = raw.map((item) => ({
172
- ...item,
173
- __filepath__
174
- }));
175
- else if ("url" in raw) res = {
176
- ...raw,
177
- __filepath__
178
- };
179
- else {
180
- res = [];
181
- Object.keys(raw).forEach((key) => {
182
- const data = raw[key];
183
- if (isArray(data)) res.push(...data.map((item) => ({
184
- ...item,
185
- __filepath__
186
- })));
187
- else res.push({
188
- ...data,
189
- __filepath__
190
- });
191
- });
192
- }
193
- return res;
194
- }
195
- function processMockData(mockList) {
196
- const list = [];
197
- for (const [, handle] of mockList.entries()) if (handle) list.push(...toArray(handle));
198
- const mocks = {};
199
- list.filter((mock) => isPlainObject(mock) && mock.enabled !== false && mock.url).forEach((mock) => {
200
- const { pathname, query } = urlParse(mock.url);
201
- const list$1 = mocks[pathname] ??= [];
202
- const current = {
203
- ...mock,
204
- url: pathname
205
- };
206
- if (current.ws !== true) {
207
- const validator = current.validator;
208
- if (!isEmptyObject(query)) if (isFunction(validator)) current.validator = function(request) {
209
- return isObjectSubset(request.query, query) && validator(request);
210
- };
211
- else if (validator) {
212
- current.validator = { ...validator };
213
- current.validator.query = current.validator.query ? {
214
- ...query,
215
- ...current.validator.query
216
- } : query;
217
- } else current.validator = { query };
218
- }
219
- list$1.push(current);
220
- });
221
- Object.keys(mocks).forEach((key) => {
222
- mocks[key] = sortByValidator(mocks[key]);
223
- });
224
- return mocks;
225
- }
226
- function sortByValidator(mocks) {
227
- return sortBy(mocks, (item) => {
228
- if (item.ws === true) return 0;
229
- const { validator } = item;
230
- if (!validator || isEmptyObject(validator)) return 2;
231
- if (isFunction(validator)) return 0;
232
- return 1 / Object.keys(validator).reduce((prev, key) => prev + keysCount(validator[key]), 0);
233
- });
234
- }
235
- function keysCount(obj) {
236
- if (!obj) return 0;
237
- return Object.keys(obj).length;
238
- }
239
-
240
- //#endregion
241
- //#region src/core/request.ts
242
- /**
243
- * 解析请求体 request.body
244
- */
245
- async function parseRequestBody(req, formidableOptions, bodyParserOptions = {}) {
246
- const method = req.method.toUpperCase();
247
- if (["HEAD", "OPTIONS"].includes(method)) return void 0;
248
- const type = req.headers["content-type"]?.toLocaleLowerCase() || "";
249
- const { limit, formLimit, jsonLimit, textLimit, ...rest } = bodyParserOptions;
250
- try {
251
- if (type.startsWith("application/json")) return await bodyParser.json(req, {
252
- limit: jsonLimit || limit,
253
- ...rest
254
- });
255
- if (type.startsWith("application/x-www-form-urlencoded")) return await bodyParser.form(req, {
256
- limit: formLimit || limit,
257
- ...rest
258
- });
259
- if (type.startsWith("text/plain")) return await bodyParser.text(req, {
260
- limit: textLimit || limit,
261
- ...rest
262
- });
263
- if (type.startsWith("multipart/form-data")) return await parseRequestBodyWithMultipart(req, formidableOptions);
264
- } catch (e) {
265
- console.error(e);
266
- }
267
- }
268
- const DEFAULT_FORMIDABLE_OPTIONS = {
269
- keepExtensions: true,
270
- filename(name, ext, part) {
271
- return part?.originalFilename || `${name}.${Date.now()}${ext ? `.${ext}` : ""}`;
272
- }
273
- };
274
- /**
275
- * 解析 request form multipart body
276
- */
277
- async function parseRequestBodyWithMultipart(req, options) {
278
- const form = formidable({
279
- ...DEFAULT_FORMIDABLE_OPTIONS,
280
- ...options
281
- });
282
- return new Promise((resolve, reject) => {
283
- form.parse(req, (error, fields, files) => {
284
- if (error) {
285
- reject(error);
286
- return;
287
- }
288
- resolve({
289
- ...fields,
290
- ...files
291
- });
292
- });
293
- });
294
- }
295
- const matcherCache = /* @__PURE__ */ new Map();
296
- /**
297
- * 解析请求 url 中的动态参数 params
298
- */
299
- function parseRequestParams(pattern, url) {
300
- let matcher = matcherCache.get(pattern);
301
- if (!matcher) {
302
- matcher = match(pattern, { decode: decodeURIComponent });
303
- matcherCache.set(pattern, matcher);
304
- }
305
- const matched = matcher(url);
306
- return matched ? matched.params : {};
307
- }
308
- /**
309
- * 验证请求是否符合 validator
310
- */
311
- function requestValidate(request, validator) {
312
- 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);
313
- }
314
- function formatLog(prefix, data) {
315
- return !data || isEmptyObject(data) ? "" : ` ${ansis.gray(`${prefix}:`)}${JSON.stringify(data)}`;
316
- }
317
- function requestLog(request, filepath) {
318
- const { url, method, query, params, body } = request;
319
- let { pathname } = new URL(url, "http://example.com");
320
- pathname = ansis.green(decodeURIComponent(pathname));
321
- const ms = ansis.magenta.bold(method);
322
- const qs = formatLog("query", query);
323
- const ps = formatLog("params", params);
324
- const bs = formatLog("body", body);
325
- const file = ` ${ansis.dim.underline(`(${filepath})`)}`;
326
- return `${ms} ${pathname}${qs}${ps}${bs}${file}`;
327
- }
328
-
329
- //#endregion
330
- //#region src/core/findMockData.ts
331
- /**
332
- * 查找匹配的 mock data
333
- */
334
- function fineMockData(mockList, logger, { pathname, method, request }) {
335
- return mockList.find((mock) => {
336
- if (!pathname || !mock || !mock.url || mock.ws) return false;
337
- if (!(mock.method ? isArray(mock.method) ? mock.method : [mock.method] : ["GET", "POST"]).includes(method)) return false;
338
- const hasMock = isPathMatch(mock.url, pathname);
339
- if (hasMock && mock.validator) {
340
- const params = parseRequestParams(mock.url, pathname);
341
- if (isFunction(mock.validator)) return mock.validator({
342
- params,
343
- ...request
344
- });
345
- else try {
346
- return requestValidate({
347
- params,
348
- ...request
349
- }, mock.validator);
350
- } catch (e) {
351
- const file = mock.__filepath__;
352
- logger.error(`${ansis.red(`mock error at ${pathname}`)}\n${e}\n at validator (${ansis.underline(file)})`, mock.log);
353
- return false;
354
- }
355
- }
356
- return hasMock;
357
- });
358
- }
359
-
360
- //#endregion
361
- //#region src/cookies/constants.ts
362
- /**
363
- * RegExp to match field-content in RFC 7230 sec 3.2
364
- *
365
- * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
366
- * field-vchar = VCHAR / obs-text
367
- * obs-text = %x80-FF
368
- */
369
- const fieldContentRegExp = /^[\t\u0020-\u007E\u0080-\u00FF]+$/;
370
- /**
371
- * RegExp to match Priority cookie attribute value.
372
- */
373
- const PRIORITY_REGEXP = /^(?:low|medium|high)$/i;
374
- /**
375
- * Cache for generated name regular expressions.
376
- */
377
- const REGEXP_CACHE = Object.create(null);
378
- /**
379
- * RegExp to match all characters to escape in a RegExp.
380
- */
381
- const REGEXP_ESCAPE_CHARS_REGEXP = /[\^$\\.*+?()[\]{}|]/g;
382
- /**
383
- * RegExp to match basic restricted name characters for loose validation.
384
- */
385
- const RESTRICTED_NAME_CHARS_REGEXP = /[;=]/;
386
- /**
387
- * RegExp to match basic restricted value characters for loose validation.
388
- */
389
- const RESTRICTED_VALUE_CHARS_REGEXP = /;/;
390
- /**
391
- * RegExp to match Same-Site cookie attribute value.
392
- */
393
- const SAME_SITE_REGEXP = /^(?:lax|none|strict)$/i;
394
-
395
- //#endregion
396
- //#region src/cookies/Cookie.ts
397
- var Cookie = class {
398
- name;
399
- value;
400
- maxAge;
401
- expires;
402
- path = "/";
403
- domain;
404
- secure = false;
405
- httpOnly = true;
406
- sameSite = false;
407
- overwrite = false;
408
- priority;
409
- partitioned;
410
- constructor(name, value, options = {}) {
411
- if (!fieldContentRegExp.test(name) || RESTRICTED_NAME_CHARS_REGEXP.test(name)) throw new TypeError("argument name is invalid");
412
- if (value && (!fieldContentRegExp.test(value) || RESTRICTED_VALUE_CHARS_REGEXP.test(value))) throw new TypeError("argument value is invalid");
413
- this.name = name;
414
- this.value = value;
415
- Object.assign(this, options);
416
- if (!this.value) {
417
- this.expires = /* @__PURE__ */ new Date(0);
418
- this.maxAge = void 0;
419
- }
420
- if (this.path && !fieldContentRegExp.test(this.path)) throw new TypeError("[Cookie] option path is invalid");
421
- if (this.domain && !fieldContentRegExp.test(this.domain)) throw new TypeError("[Cookie] option domain is invalid");
422
- if (typeof this.maxAge === "number" ? Number.isNaN(this.maxAge) || !Number.isFinite(this.maxAge) : this.maxAge) throw new TypeError("[Cookie] option maxAge is invalid");
423
- if (this.priority && !PRIORITY_REGEXP.test(this.priority)) throw new TypeError("[Cookie] option priority is invalid");
424
- if (this.sameSite && this.sameSite !== true && !SAME_SITE_REGEXP.test(this.sameSite)) throw new TypeError("[Cookie] option sameSite is invalid");
425
- }
426
- toString() {
427
- return `${this.name}=${this.value}`;
428
- }
429
- toHeader() {
430
- let header = this.toString();
431
- if (this.maxAge) this.expires = new Date(Date.now() + this.maxAge);
432
- if (this.path) header += `; path=${this.path}`;
433
- if (this.expires) header += `; expires=${this.expires.toUTCString()}`;
434
- if (this.domain) header += `; domain=${this.domain}`;
435
- if (this.priority) header += `; priority=${this.priority.toLowerCase()}`;
436
- if (this.sameSite) header += `; samesite=${this.sameSite === true ? "strict" : this.sameSite.toLowerCase()}`;
437
- if (this.secure) header += "; secure";
438
- if (this.httpOnly) header += "; httponly";
439
- if (this.partitioned) header += "; partitioned";
440
- return header;
441
- }
442
- };
443
-
444
- //#endregion
445
- //#region src/cookies/timeSafeCompare.ts
446
- function bufferEqual(a, b) {
447
- if (a.length !== b.length) return false;
448
- if (crypto.timingSafeEqual) return crypto.timingSafeEqual(a, b);
449
- for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
450
- return true;
451
- }
452
- function createHmac(key, data) {
453
- return crypto.createHmac("sha256", key).update(data).digest();
454
- }
455
- function timeSafeCompare(a, b) {
456
- const sa = String(a);
457
- const sb = String(b);
458
- const key = crypto.randomBytes(32);
459
- return bufferEqual(createHmac(key, sa), createHmac(key, sb)) && a === b;
460
- }
461
-
462
- //#endregion
463
- //#region src/cookies/Keygrip.ts
464
- const SLASH_PATTERN = /[/+=]/g;
465
- const REPLACE_MAP = {
466
- "/": "_",
467
- "+": "-",
468
- "=": ""
469
- };
470
- var Keygrip = class {
471
- algorithm;
472
- encoding;
473
- keys = [];
474
- constructor(keys, algorithm, encoding) {
475
- this.keys = keys;
476
- this.algorithm = algorithm || "sha256";
477
- this.encoding = encoding || "base64";
478
- }
479
- sign(data, key = this.keys[0]) {
480
- return crypto.createHmac(this.algorithm, key).update(data).digest(this.encoding).replace(SLASH_PATTERN, (m) => REPLACE_MAP[m]);
481
- }
482
- index(data, digest) {
483
- for (let i = 0, l = this.keys.length; i < l; i++) if (timeSafeCompare(digest, this.sign(data, this.keys[i]))) return i;
484
- return -1;
485
- }
486
- verify(data, digest) {
487
- return this.index(data, digest) > -1;
488
- }
489
- };
490
-
491
- //#endregion
492
- //#region src/cookies/Cookies.ts
493
- var Cookies = class {
494
- request;
495
- response;
496
- secure;
497
- keys;
498
- constructor(req, res, options = {}) {
499
- this.request = req;
500
- this.response = res;
501
- this.secure = options.secure;
502
- if (options.keys instanceof Keygrip) this.keys = options.keys;
503
- else if (isArray(options.keys)) this.keys = new Keygrip(options.keys);
504
- }
505
- set(name, value, options) {
506
- const req = this.request;
507
- const res = this.response;
508
- const headers = toArray(res.getHeader("Set-Cookie"));
509
- const cookie = new Cookie(name, value, options);
510
- const signed = options?.signed ?? !!this.keys;
511
- const secure = this.secure === void 0 ? req.protocol === "https" || isRequestEncrypted(req) : Boolean(this.secure);
512
- if (!secure && options?.secure) throw new Error("Cannot send secure cookie over unencrypted connection");
513
- cookie.secure = options?.secure ?? secure;
514
- pushCookie(headers, cookie);
515
- if (signed && options) {
516
- if (!this.keys) throw new Error(".keys required for signed cookies");
517
- cookie.value = this.keys.sign(cookie.toString());
518
- cookie.name += ".sig";
519
- pushCookie(headers, cookie);
520
- }
521
- (res.set ? http.OutgoingMessage.prototype.setHeader : res.setHeader).call(res, "Set-Cookie", headers);
522
- return this;
523
- }
524
- get(name, options) {
525
- const signName = `${name}.sig`;
526
- const signed = options?.signed ?? !!this.keys;
527
- const header = this.request.headers.cookie;
528
- if (!header) return;
529
- const match$1 = header.match(getPattern(name));
530
- if (!match$1) return;
531
- let value = match$1[1];
532
- if (value[0] === "\"") value = value.slice(1, -1);
533
- if (!options || !signed) return value;
534
- const remote = this.get(signName);
535
- if (!remote) return;
536
- const data = `${name}=${value}`;
537
- if (!this.keys) throw new Error(".keys required for signed cookies");
538
- const index = this.keys.index(data, remote);
539
- if (index < 0) this.set(signName, null, {
540
- path: "/",
541
- signed: false
542
- });
543
- else {
544
- index && this.set(signName, this.keys.sign(data), { signed: false });
545
- return value;
546
- }
547
- }
548
- };
549
- /**
550
- * Get the pattern to search for a cookie in a string.
551
- */
552
- function getPattern(name) {
553
- if (!REGEXP_CACHE[name]) REGEXP_CACHE[name] = /* @__PURE__ */ new RegExp(`(?:^|;) *${name.replace(REGEXP_ESCAPE_CHARS_REGEXP, "\\$&")}=([^;]*)`);
554
- return REGEXP_CACHE[name];
555
- }
556
- /**
557
- * Get the encrypted status for a request.
558
- */
559
- function isRequestEncrypted(req) {
560
- return Boolean(req.socket ? req.socket.encrypted : req.connection.encrypted);
561
- }
562
- function pushCookie(headers, cookie) {
563
- if (cookie.overwrite) {
564
- for (let i = headers.length - 1; i >= 0; i--) if (headers[i].indexOf(`${cookie.name}=`) === 0) headers.splice(i, 1);
565
- }
566
- headers.push(cookie.toHeader());
567
- }
568
-
569
- //#endregion
570
- //#region src/core/matchingWeight.ts
571
- const tokensCache = {};
572
- function getTokens(rule) {
573
- if (tokensCache[rule]) return tokensCache[rule];
574
- const res = [];
575
- const flatten = (tokens, group = false) => {
576
- for (const token of tokens) if (token.type === "text") {
577
- const sub = token.value.split("/").filter(Boolean);
578
- sub.length && res.push(...sub.map((v) => ({
579
- type: "text",
580
- value: v
581
- })));
582
- } else if (token.type === "group") flatten(token.tokens, true);
583
- else {
584
- if (group) token.optional = true;
585
- res.push(token);
586
- }
587
- };
588
- flatten(parse(rule).tokens);
589
- tokensCache[rule] = res;
590
- return res;
591
- }
592
- function getHighest(rules) {
593
- let weights = rules.map((rule) => getTokens(rule).length);
594
- weights = weights.length === 0 ? [1] : weights;
595
- return Math.max(...weights) + 2;
596
- }
597
- function sortFn(rule) {
598
- const tokens = getTokens(rule);
599
- let w = 0;
600
- for (let i = 0; i < tokens.length; i++) {
601
- if (tokens[i].type !== "text") w += 10 ** (i + 1);
602
- w += 10 ** (i + 1);
603
- }
604
- return w;
605
- }
606
- function preSort(rules) {
607
- let matched = [];
608
- const preMatch = [];
609
- for (const rule of rules) {
610
- const len = getTokens(rule).filter((token) => token.type !== "text").length;
611
- if (!preMatch[len]) preMatch[len] = [];
612
- preMatch[len].push(rule);
613
- }
614
- for (const match$1 of preMatch.filter((v) => v && v.length > 0)) matched = [...matched, ...sortBy(match$1, sortFn).reverse()];
615
- return matched;
616
- }
617
- function defaultPriority(rules) {
618
- const highest = getHighest(rules);
619
- return sortBy(rules, (rule) => {
620
- const tokens = getTokens(rule);
621
- const dym = tokens.filter((token) => token.type !== "text");
622
- if (dym.length === 0) return 0;
623
- let weight = dym.length;
624
- let exp = 0;
625
- for (let i = 0; i < tokens.length; i++) {
626
- const token = tokens[i];
627
- const isDynamic = token.type !== "text";
628
- const isWildcard = token.type === "wildcard";
629
- const isOptional = !!token.optional;
630
- exp += isDynamic ? 1 : 0;
631
- if (i === tokens.length - 1 && isWildcard) weight += (isOptional ? 5 : 4) * 10 ** (tokens.length === 1 ? highest + 1 : highest);
632
- else {
633
- if (isWildcard) weight += 3 * 10 ** (highest - 1);
634
- else weight += 2 * 10 ** exp;
635
- if (isOptional) weight += 10 ** exp;
636
- }
637
- }
638
- return weight;
639
- });
640
- }
641
- function matchingWeight(rules, url, priority) {
642
- let matched = defaultPriority(preSort(rules.filter((rule) => isPathMatch(rule, url))));
643
- const { global = [], special = {} } = priority;
644
- if (global.length === 0 && isEmptyObject(special) || matched.length === 0) return matched;
645
- const [statics, dynamics] = twoPartMatch(matched);
646
- const globalMatch = global.filter((rule) => dynamics.includes(rule));
647
- if (globalMatch.length > 0) matched = uniq([
648
- ...statics,
649
- ...globalMatch,
650
- ...dynamics
651
- ]);
652
- if (isEmptyObject(special)) return matched;
653
- const specialRule = Object.keys(special).filter((rule) => matched.includes(rule))[0];
654
- if (!specialRule) return matched;
655
- const options = special[specialRule];
656
- const { rules: lowerRules, when } = isArray(options) ? {
657
- rules: options,
658
- when: []
659
- } : options;
660
- if (lowerRules.includes(matched[0])) {
661
- if (when.length === 0 || when.some((path$1) => pathToRegexp(path$1).regexp.test(url))) matched = uniq([specialRule, ...matched]);
662
- }
663
- return matched;
664
- }
665
- /**
666
- * 将规则分为静态和动态两部分
667
- */
668
- function twoPartMatch(rules) {
669
- const statics = [];
670
- const dynamics = [];
671
- for (const rule of rules) if (getTokens(rule).filter((token) => token.type !== "text").length > 0) dynamics.push(rule);
672
- else statics.push(rule);
673
- return [statics, dynamics];
674
- }
675
-
676
- //#endregion
677
- //#region src/core/requestRecovery.ts
678
- const cache = /* @__PURE__ */ new WeakMap();
679
- /**
680
- * 备份请求数据
681
- */
682
- function collectRequest(req) {
683
- const chunks = [];
684
- req.addListener("data", (chunk) => {
685
- chunks.push(Buffer.from(chunk));
686
- });
687
- req.addListener("end", () => {
688
- if (chunks.length) cache.set(req, Buffer.concat(chunks));
689
- });
690
- }
691
- /**
692
- * vite 在 proxy 配置中,允许通过 configure 访问 http-proxy 实例,
693
- * 通过 http-proxy 的 proxyReq 事件,重新写入请求流
694
- */
695
- function recoverRequest(config) {
696
- if (!config.server) return;
697
- const proxies = config.server.proxy || {};
698
- Object.keys(proxies).forEach((key) => {
699
- const target = proxies[key];
700
- const options = typeof target === "string" ? { target } : target;
701
- if (options.ws) return;
702
- const { configure, ...rest } = options;
703
- proxies[key] = {
704
- ...rest,
705
- configure(proxy, options$1) {
706
- configure?.(proxy, options$1);
707
- proxy.on("proxyReq", (proxyReq, req) => {
708
- const buffer = cache.get(req);
709
- if (buffer) {
710
- cache.delete(req);
711
- /**
712
- * 使用 http-proxy 的 agent 配置会提前写入代理请求流
713
- * https://github.com/http-party/node-http-proxy/issues/1287
714
- */
715
- if (!proxyReq.headersSent) proxyReq.setHeader("Content-Length", buffer.byteLength);
716
- if (!proxyReq.writableEnded) proxyReq.write(buffer);
717
- }
718
- });
719
- }
720
- };
721
- });
722
- }
723
-
724
- //#endregion
725
- //#region src/core/response.ts
726
- /**
727
- * 根据状态码获取状态文本
728
- */
729
- function getHTTPStatusText(status) {
730
- return HTTP_STATUS[status] || "Unknown";
731
- }
732
- /**
733
- * 设置响应状态
734
- */
735
- function provideResponseStatus(response, status = 200, statusText) {
736
- response.statusCode = status;
737
- response.statusMessage = statusText || getHTTPStatusText(status);
738
- }
739
- /**
740
- * 设置响应头
741
- */
742
- async function provideResponseHeaders(req, res, mock, logger) {
743
- const { headers, type = "json" } = mock;
744
- const filepath = mock.__filepath__;
745
- const contentType = mime.contentType(type) || mime.contentType(mime.lookup(type) || "");
746
- if (contentType) res.setHeader("Content-Type", contentType);
747
- res.setHeader("Cache-Control", "no-cache,max-age=0");
748
- res.setHeader("X-Mock-Power-By", "vite-plugin-mock-dev-server");
749
- if (filepath) res.setHeader("X-File-Path", filepath);
750
- if (!headers) return;
751
- try {
752
- const raw = isFunction(headers) ? await headers(req) : headers;
753
- Object.keys(raw).forEach((key) => {
754
- res.setHeader(key, raw[key]);
755
- });
756
- } catch (e) {
757
- logger.error(`${ansis.red(`mock error at ${req.url.split("?")[0]}`)}\n${e}\n at headers (${ansis.underline(filepath)})`, mock.log);
758
- }
759
- }
760
- /**
761
- * 设置响应cookie
762
- */
763
- async function provideResponseCookies(req, res, mock, logger) {
764
- const { cookies } = mock;
765
- const filepath = mock.__filepath__;
766
- if (!cookies) return;
767
- try {
768
- const raw = isFunction(cookies) ? await cookies(req) : cookies;
769
- Object.keys(raw).forEach((key) => {
770
- const cookie = raw[key];
771
- if (isArray(cookie)) {
772
- const [value, options] = cookie;
773
- res.setCookie(key, value, options);
774
- } else res.setCookie(key, cookie);
775
- });
776
- } catch (e) {
777
- logger.error(`${ansis.red(`mock error at ${req.url.split("?")[0]}`)}\n${e}\n at cookies (${ansis.underline(filepath)})`, mock.log);
778
- }
779
- }
780
- /**
781
- * 设置响应数据
782
- */
783
- function sendResponseData(res, raw, type) {
784
- if (isReadableStream(raw)) raw.pipe(res);
785
- else if (Buffer.isBuffer(raw)) res.end(type === "text" || type === "json" ? raw.toString("utf-8") : raw);
786
- else {
787
- const content = typeof raw === "string" ? raw : JSON.stringify(raw);
788
- res.end(type === "buffer" ? Buffer.from(content) : content);
789
- }
790
- }
791
- /**
792
- * 实际响应延迟
793
- */
794
- async function responseRealDelay(startTime, delay) {
795
- if (!delay || typeof delay === "number" && delay <= 0 || isArray(delay) && delay.length !== 2) return;
796
- let realDelay = 0;
797
- if (isArray(delay)) {
798
- const [min, max] = delay;
799
- realDelay = random(min, max);
800
- } else realDelay = delay - (timestamp() - startTime);
801
- if (realDelay > 0) await sleep(realDelay);
802
- }
803
-
804
- //#endregion
805
- //#region src/core/mockMiddleware.ts
806
- function createMockMiddleware(compiler, { formidableOptions = {}, bodyParserOptions = {}, proxies, cookiesOptions, logger, priority = {} }) {
807
- return async function mockMiddleware(req, res, next) {
808
- const startTime = timestamp();
809
- const { query, pathname } = urlParse(req.url);
810
- if (!pathname || proxies.length === 0 || !proxies.some((context) => doesProxyContextMatchUrl(context, req.url))) return next();
811
- const mockData = compiler.mockData;
812
- const mockUrls = matchingWeight(Object.keys(mockData), pathname, priority);
813
- if (mockUrls.length === 0) return next();
814
- collectRequest(req);
815
- const { query: refererQuery } = urlParse(req.headers.referer || "");
816
- const reqBody = await parseRequestBody(req, formidableOptions, bodyParserOptions);
817
- const cookies = new Cookies(req, res, cookiesOptions);
818
- const getCookie = cookies.get.bind(cookies);
819
- const method = req.method.toUpperCase();
820
- let mock;
821
- let _mockUrl;
822
- for (const mockUrl of mockUrls) {
823
- mock = fineMockData(mockData[mockUrl], logger, {
824
- pathname,
825
- method,
826
- request: {
827
- query,
828
- refererQuery,
829
- body: reqBody,
830
- headers: req.headers,
831
- getCookie
832
- }
833
- });
834
- if (mock) {
835
- _mockUrl = mockUrl;
836
- break;
837
- }
838
- }
839
- if (!mock) {
840
- const matched = mockUrls.map((m) => m === _mockUrl ? ansis.underline.bold(m) : ansis.dim(m)).join(", ");
841
- logger.warn(`${ansis.green(pathname)} matches ${matched} , but mock data is not found.`);
842
- return next();
843
- }
844
- const request = req;
845
- const response = res;
846
- request.body = reqBody;
847
- request.query = query;
848
- request.refererQuery = refererQuery;
849
- request.params = parseRequestParams(mock.url, pathname);
850
- request.getCookie = getCookie;
851
- response.setCookie = cookies.set.bind(cookies);
852
- const { body, delay, type = "json", response: responseFn, status = 200, statusText, log: logLevel, __filepath__: filepath } = mock;
853
- provideResponseStatus(response, status, statusText);
854
- await provideResponseHeaders(request, response, mock, logger);
855
- await provideResponseCookies(request, response, mock, logger);
856
- logger.info(requestLog(request, filepath), logLevel);
857
- logger.debug(`${ansis.magenta("DEBUG")} ${ansis.underline(pathname)} matches: [ ${mockUrls.map((m) => m === _mockUrl ? ansis.underline.bold(m) : ansis.dim(m)).join(", ")} ]\n`);
858
- if (body) {
859
- try {
860
- const content = isFunction(body) ? await body(request) : body;
861
- await responseRealDelay(startTime, delay);
862
- sendResponseData(response, content, type);
863
- } catch (e) {
864
- logger.error(`${ansis.red(`mock error at ${pathname}`)}\n${e}\n at body (${ansis.underline(filepath)})`, logLevel);
865
- provideResponseStatus(response, 500);
866
- res.end("");
867
- }
868
- return;
869
- }
870
- if (responseFn) {
871
- try {
872
- await responseRealDelay(startTime, delay);
873
- await responseFn(request, response, next);
874
- } catch (e) {
875
- logger.error(`${ansis.red(`mock error at ${pathname}`)}\n${e}\n at response (${ansis.underline(filepath)})`, logLevel);
876
- provideResponseStatus(response, 500);
877
- res.end("");
878
- }
879
- return;
880
- }
881
- res.end("");
882
- };
883
- }
884
-
885
- //#endregion
886
- //#region src/core/ws.ts
887
- /**
888
- * mock websocket
889
- */
890
- function mockWebSocket(compiler, server, { wsProxies: proxies, cookiesOptions, logger }) {
891
- const hmrMap = /* @__PURE__ */ new Map();
892
- const poolMap = /* @__PURE__ */ new Map();
893
- const wssContextMap = /* @__PURE__ */ new WeakMap();
894
- const getWssMap = (mockUrl) => {
895
- let wssMap = poolMap.get(mockUrl);
896
- if (!wssMap) poolMap.set(mockUrl, wssMap = /* @__PURE__ */ new Map());
897
- return wssMap;
898
- };
899
- const getWss = (wssMap, pathname) => {
900
- let wss = wssMap.get(pathname);
901
- if (!wss) wssMap.set(pathname, wss = new WebSocketServer({ noServer: true }));
902
- return wss;
903
- };
904
- const addHmr = (filepath, mockUrl) => {
905
- let urlList = hmrMap.get(filepath);
906
- if (!urlList) hmrMap.set(filepath, urlList = /* @__PURE__ */ new Set());
907
- urlList.add(mockUrl);
908
- };
909
- const setupWss = (wssMap, wss, mock, context, pathname, filepath) => {
910
- try {
911
- mock.setup?.(wss, context);
912
- wss.on("close", () => wssMap.delete(pathname));
913
- wss.on("error", (e) => {
914
- logger.error(`${ansis.red(`WebSocket mock error at ${wss.path}`)}\n${e}\n at setup (${filepath})`, mock.log);
915
- });
916
- } catch (e) {
917
- logger.error(`${ansis.red(`WebSocket mock error at ${wss.path}`)}\n${e}\n at setup (${filepath})`, mock.log);
918
- }
919
- };
920
- const emitConnection = (wss, ws, req, connectionList) => {
921
- wss.emit("connection", ws, req);
922
- ws.on("close", () => {
923
- const i = connectionList.findIndex((item) => item.ws === ws);
924
- if (i !== -1) connectionList.splice(i, 1);
925
- });
926
- };
927
- const restartWss = (wssMap, wss, mock, pathname, filepath) => {
928
- const { cleanupList, connectionList, context } = wssContextMap.get(wss);
929
- cleanupRunner(cleanupList);
930
- connectionList.forEach(({ ws }) => ws.removeAllListeners());
931
- wss.removeAllListeners();
932
- setupWss(wssMap, wss, mock, context, pathname, filepath);
933
- connectionList.forEach(({ ws, req }) => emitConnection(wss, ws, req, connectionList));
934
- };
935
- compiler.on?.("mock:update-end", (filepath) => {
936
- if (!hmrMap.has(filepath)) return;
937
- const mockUrlList = hmrMap.get(filepath);
938
- if (!mockUrlList) return;
939
- for (const mockUrl of mockUrlList.values()) for (const mock of compiler.mockData[mockUrl]) {
940
- if (!mock.ws || mock.__filepath__ !== filepath) return;
941
- const wssMap = getWssMap(mockUrl);
942
- for (const [pathname, wss] of wssMap.entries()) restartWss(wssMap, wss, mock, pathname, filepath);
943
- }
944
- });
945
- server?.on("upgrade", (req, socket, head) => {
946
- const { pathname, query } = urlParse(req.url);
947
- if (!pathname || proxies.length === 0 || !proxies.some((context) => doesProxyContextMatchUrl(context, req.url))) return;
948
- const mockData = compiler.mockData;
949
- const mockUrl = Object.keys(mockData).find((key) => isPathMatch(key, pathname));
950
- if (!mockUrl) return;
951
- const mock = mockData[mockUrl].find((mock$1) => {
952
- return mock$1.url && mock$1.ws && isPathMatch(mock$1.url, pathname);
953
- });
954
- if (!mock) return;
955
- const filepath = mock.__filepath__;
956
- addHmr(filepath, mockUrl);
957
- const wssMap = getWssMap(mockUrl);
958
- const wss = getWss(wssMap, pathname);
959
- let wssContext = wssContextMap.get(wss);
960
- if (!wssContext) {
961
- const cleanupList = [];
962
- const context = { onCleanup: (cleanup) => cleanupList.push(cleanup) };
963
- wssContext = {
964
- cleanupList,
965
- context,
966
- connectionList: []
967
- };
968
- wssContextMap.set(wss, wssContext);
969
- setupWss(wssMap, wss, mock, context, pathname, filepath);
970
- }
971
- const request = req;
972
- const cookies = new Cookies(req, req, cookiesOptions);
973
- const { query: refererQuery } = urlParse(req.headers.referer || "");
974
- request.query = query;
975
- request.refererQuery = refererQuery;
976
- request.params = parseRequestParams(mockUrl, pathname);
977
- request.getCookie = cookies.get.bind(cookies);
978
- wss.handleUpgrade(request, socket, head, (ws) => {
979
- logger.info(`${ansis.magenta.bold("WebSocket")} ${ansis.green(req.url)} connected ${ansis.dim(`(${filepath})`)}`, mock.log);
980
- wssContext.connectionList.push({
981
- req: request,
982
- ws
983
- });
984
- emitConnection(wss, ws, request, wssContext.connectionList);
985
- });
986
- });
987
- server?.on("close", () => {
988
- for (const wssMap of poolMap.values()) {
989
- for (const wss of wssMap.values()) {
990
- cleanupRunner(wssContextMap.get(wss).cleanupList);
991
- wss.close();
992
- }
993
- wssMap.clear();
994
- }
995
- poolMap.clear();
996
- hmrMap.clear();
997
- });
998
- }
999
- function cleanupRunner(cleanupList) {
1000
- let cleanup;
1001
- while (cleanup = cleanupList.shift()) cleanup?.();
1002
- }
1003
-
1004
- //#endregion
1005
- export { processRawData as a, debug as c, logLevels as d, isPathMatch as f, createMatcher as h, processMockData as i, normalizePath as l, doesProxyContextMatchUrl as m, createMockMiddleware as n, sortByValidator as o, isPackageExists as p, recoverRequest as r, urlParse as s, mockWebSocket as t, createLogger as u };