vite-plugin-mock-dev-server 1.7.0 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-TTKDHWOT.js → chunk-FUNGHLD3.js} +131 -131
- package/dist/helper.d.cts +6 -6
- package/dist/helper.d.ts +6 -6
- package/dist/index.cjs +30 -29
- package/dist/index.d.cts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +9 -8
- package/dist/server.cjs +2 -2
- package/dist/server.d.cts +9 -9
- package/dist/server.d.ts +9 -9
- package/dist/server.js +1 -1
- package/dist/{types-D50kW_6z.d.cts → types-BYspd62h.d.cts} +5 -5
- package/dist/{types-D50kW_6z.d.ts → types-BYspd62h.d.ts} +5 -5
- package/package.json +10 -7
- package/dist/{chunk-S76FMTCD.cjs → chunk-JP6LPDGG.cjs} +125 -125
|
@@ -252,10 +252,10 @@ function recoverRequest(config) {
|
|
|
252
252
|
|
|
253
253
|
// src/core/utils.ts
|
|
254
254
|
import fs from "node:fs";
|
|
255
|
+
import os from "node:os";
|
|
255
256
|
import path from "node:path";
|
|
256
257
|
import { parse as queryParse } from "node:querystring";
|
|
257
|
-
import { URL as URL2
|
|
258
|
-
import os from "node:os";
|
|
258
|
+
import { fileURLToPath, URL as URL2 } from "node:url";
|
|
259
259
|
import Debug from "debug";
|
|
260
260
|
import { match } from "path-to-regexp";
|
|
261
261
|
function isStream(stream) {
|
|
@@ -612,10 +612,134 @@ function requestLog(request, filepath) {
|
|
|
612
612
|
return `${ms} ${pathname}${qs}${ps}${bs}${file}`;
|
|
613
613
|
}
|
|
614
614
|
|
|
615
|
+
// src/core/logger.ts
|
|
616
|
+
import { isBoolean } from "@pengzhanbo/utils";
|
|
617
|
+
import colors2 from "picocolors";
|
|
618
|
+
var logLevels = {
|
|
619
|
+
silent: 0,
|
|
620
|
+
error: 1,
|
|
621
|
+
warn: 2,
|
|
622
|
+
info: 3,
|
|
623
|
+
debug: 4
|
|
624
|
+
};
|
|
625
|
+
function createLogger(prefix, defaultLevel = "info") {
|
|
626
|
+
prefix = `[${prefix}]`;
|
|
627
|
+
function output(type, msg, level) {
|
|
628
|
+
level = isBoolean(level) ? level ? defaultLevel : "error" : level;
|
|
629
|
+
const thresh = logLevels[level];
|
|
630
|
+
if (thresh >= logLevels[type]) {
|
|
631
|
+
const method = type === "info" || type === "debug" ? "log" : type;
|
|
632
|
+
const tag = type === "debug" ? colors2.magenta(colors2.bold(prefix)) : type === "info" ? colors2.cyan(colors2.bold(prefix)) : type === "warn" ? colors2.yellow(colors2.bold(prefix)) : colors2.red(colors2.bold(prefix));
|
|
633
|
+
const format = `${colors2.dim(
|
|
634
|
+
(/* @__PURE__ */ new Date()).toLocaleTimeString()
|
|
635
|
+
)} ${tag} ${msg}`;
|
|
636
|
+
console[method](format);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
const logger = {
|
|
640
|
+
debug(msg, level = defaultLevel) {
|
|
641
|
+
output("debug", msg, level);
|
|
642
|
+
},
|
|
643
|
+
info(msg, level = defaultLevel) {
|
|
644
|
+
output("info", msg, level);
|
|
645
|
+
},
|
|
646
|
+
warn(msg, level = defaultLevel) {
|
|
647
|
+
output("warn", msg, level);
|
|
648
|
+
},
|
|
649
|
+
error(msg, level = defaultLevel) {
|
|
650
|
+
output("error", msg, level);
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
return logger;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// src/core/transform.ts
|
|
657
|
+
import {
|
|
658
|
+
isArray as isArray4,
|
|
659
|
+
isEmptyObject as isEmptyObject3,
|
|
660
|
+
isFunction as isFunction2,
|
|
661
|
+
isObject as isObject2,
|
|
662
|
+
sortBy as sortBy2,
|
|
663
|
+
toArray
|
|
664
|
+
} from "@pengzhanbo/utils";
|
|
665
|
+
function transformRawData(raw, __filepath__) {
|
|
666
|
+
let mockConfig;
|
|
667
|
+
if (isArray4(raw)) {
|
|
668
|
+
mockConfig = raw.map((item) => ({ ...item, __filepath__ }));
|
|
669
|
+
} else if ("url" in raw) {
|
|
670
|
+
mockConfig = { ...raw, __filepath__ };
|
|
671
|
+
} else {
|
|
672
|
+
mockConfig = [];
|
|
673
|
+
Object.keys(raw).forEach((key) => {
|
|
674
|
+
const data = raw[key];
|
|
675
|
+
if (isArray4(data)) {
|
|
676
|
+
mockConfig.push(...data.map((item) => ({ ...item, __filepath__ })));
|
|
677
|
+
} else {
|
|
678
|
+
mockConfig.push({ ...data, __filepath__ });
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
return mockConfig;
|
|
683
|
+
}
|
|
684
|
+
function transformMockData(mockList) {
|
|
685
|
+
const list = [];
|
|
686
|
+
for (const [, handle] of mockList.entries()) {
|
|
687
|
+
if (handle)
|
|
688
|
+
list.push(...toArray(handle));
|
|
689
|
+
}
|
|
690
|
+
const mocks = {};
|
|
691
|
+
list.filter((mock) => isObject2(mock) && mock.enabled !== false && mock.url).forEach((mock) => {
|
|
692
|
+
const { pathname, query } = urlParse(mock.url);
|
|
693
|
+
const list2 = mocks[pathname] ??= [];
|
|
694
|
+
const current = { ...mock, url: pathname };
|
|
695
|
+
if (current.ws !== true) {
|
|
696
|
+
const validator = current.validator;
|
|
697
|
+
if (!isEmptyObject3(query)) {
|
|
698
|
+
if (isFunction2(validator)) {
|
|
699
|
+
current.validator = function(request) {
|
|
700
|
+
return isObjectSubset(request.query, query) && validator(request);
|
|
701
|
+
};
|
|
702
|
+
} else if (validator) {
|
|
703
|
+
current.validator = { ...validator };
|
|
704
|
+
current.validator.query = current.validator.query ? { ...query, ...current.validator.query } : query;
|
|
705
|
+
} else {
|
|
706
|
+
current.validator = { query };
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
list2.push(current);
|
|
711
|
+
});
|
|
712
|
+
Object.keys(mocks).forEach((key) => {
|
|
713
|
+
mocks[key] = sortByValidator(mocks[key]);
|
|
714
|
+
});
|
|
715
|
+
return mocks;
|
|
716
|
+
}
|
|
717
|
+
function sortByValidator(mocks) {
|
|
718
|
+
return sortBy2(mocks, (item) => {
|
|
719
|
+
if (item.ws === true)
|
|
720
|
+
return 0;
|
|
721
|
+
const { validator } = item;
|
|
722
|
+
if (!validator || isEmptyObject3(validator))
|
|
723
|
+
return 2;
|
|
724
|
+
if (isFunction2(validator))
|
|
725
|
+
return 0;
|
|
726
|
+
const count = Object.keys(validator).reduce(
|
|
727
|
+
(prev, key) => prev + keysCount(validator[key]),
|
|
728
|
+
0
|
|
729
|
+
);
|
|
730
|
+
return 1 / count;
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
function keysCount(obj) {
|
|
734
|
+
if (!obj)
|
|
735
|
+
return 0;
|
|
736
|
+
return Object.keys(obj).length;
|
|
737
|
+
}
|
|
738
|
+
|
|
615
739
|
// src/core/ws.ts
|
|
616
740
|
import Cookies2 from "cookies";
|
|
617
741
|
import { pathToRegexp as pathToRegexp3 } from "path-to-regexp";
|
|
618
|
-
import
|
|
742
|
+
import colors3 from "picocolors";
|
|
619
743
|
import { WebSocketServer } from "ws";
|
|
620
744
|
function mockWebSocket(compiler, server, {
|
|
621
745
|
wsProxies: proxies,
|
|
@@ -649,7 +773,7 @@ function mockWebSocket(compiler, server, {
|
|
|
649
773
|
wss.on("close", () => wssMap.delete(pathname));
|
|
650
774
|
wss.on("error", (e) => {
|
|
651
775
|
logger.error(
|
|
652
|
-
`${
|
|
776
|
+
`${colors3.red(
|
|
653
777
|
`WebSocket mock error at ${wss.path}`
|
|
654
778
|
)}
|
|
655
779
|
${e}
|
|
@@ -659,7 +783,7 @@ ${e}
|
|
|
659
783
|
});
|
|
660
784
|
} catch (e) {
|
|
661
785
|
logger.error(
|
|
662
|
-
`${
|
|
786
|
+
`${colors3.red(
|
|
663
787
|
`WebSocket mock error at ${wss.path}`
|
|
664
788
|
)}
|
|
665
789
|
${e}
|
|
@@ -741,9 +865,9 @@ ${e}
|
|
|
741
865
|
request.getCookie = cookies.get.bind(cookies);
|
|
742
866
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
743
867
|
logger.info(
|
|
744
|
-
`${
|
|
868
|
+
`${colors3.magenta(colors3.bold("WebSocket"))} ${colors3.green(
|
|
745
869
|
req.url
|
|
746
|
-
)} connected ${
|
|
870
|
+
)} connected ${colors3.dim(`(${filepath})`)}`,
|
|
747
871
|
mock.log
|
|
748
872
|
);
|
|
749
873
|
wssContext.connectionList.push({ req: request, ws });
|
|
@@ -769,130 +893,6 @@ function cleanupRunner(cleanupList) {
|
|
|
769
893
|
cleanup?.();
|
|
770
894
|
}
|
|
771
895
|
|
|
772
|
-
// src/core/transform.ts
|
|
773
|
-
import {
|
|
774
|
-
isArray as isArray4,
|
|
775
|
-
isEmptyObject as isEmptyObject3,
|
|
776
|
-
isFunction as isFunction2,
|
|
777
|
-
isObject as isObject2,
|
|
778
|
-
sortBy as sortBy2,
|
|
779
|
-
toArray
|
|
780
|
-
} from "@pengzhanbo/utils";
|
|
781
|
-
function transformRawData(raw, __filepath__) {
|
|
782
|
-
let mockConfig;
|
|
783
|
-
if (isArray4(raw)) {
|
|
784
|
-
mockConfig = raw.map((item) => ({ ...item, __filepath__ }));
|
|
785
|
-
} else if ("url" in raw) {
|
|
786
|
-
mockConfig = { ...raw, __filepath__ };
|
|
787
|
-
} else {
|
|
788
|
-
mockConfig = [];
|
|
789
|
-
Object.keys(raw).forEach((key) => {
|
|
790
|
-
const data = raw[key];
|
|
791
|
-
if (isArray4(data)) {
|
|
792
|
-
mockConfig.push(...data.map((item) => ({ ...item, __filepath__ })));
|
|
793
|
-
} else {
|
|
794
|
-
mockConfig.push({ ...data, __filepath__ });
|
|
795
|
-
}
|
|
796
|
-
});
|
|
797
|
-
}
|
|
798
|
-
return mockConfig;
|
|
799
|
-
}
|
|
800
|
-
function transformMockData(mockList) {
|
|
801
|
-
const list = [];
|
|
802
|
-
for (const [, handle] of mockList.entries()) {
|
|
803
|
-
if (handle)
|
|
804
|
-
list.push(...toArray(handle));
|
|
805
|
-
}
|
|
806
|
-
const mocks = {};
|
|
807
|
-
list.filter((mock) => isObject2(mock) && mock.enabled !== false && mock.url).forEach((mock) => {
|
|
808
|
-
const { pathname, query } = urlParse(mock.url);
|
|
809
|
-
const list2 = mocks[pathname] ??= [];
|
|
810
|
-
const current = { ...mock, url: pathname };
|
|
811
|
-
if (current.ws !== true) {
|
|
812
|
-
const validator = current.validator;
|
|
813
|
-
if (!isEmptyObject3(query)) {
|
|
814
|
-
if (isFunction2(validator)) {
|
|
815
|
-
current.validator = function(request) {
|
|
816
|
-
return isObjectSubset(request.query, query) && validator(request);
|
|
817
|
-
};
|
|
818
|
-
} else if (validator) {
|
|
819
|
-
current.validator = { ...validator };
|
|
820
|
-
current.validator.query = current.validator.query ? { ...query, ...current.validator.query } : query;
|
|
821
|
-
} else {
|
|
822
|
-
current.validator = { query };
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
list2.push(current);
|
|
827
|
-
});
|
|
828
|
-
Object.keys(mocks).forEach((key) => {
|
|
829
|
-
mocks[key] = sortByValidator(mocks[key]);
|
|
830
|
-
});
|
|
831
|
-
return mocks;
|
|
832
|
-
}
|
|
833
|
-
function sortByValidator(mocks) {
|
|
834
|
-
return sortBy2(mocks, (item) => {
|
|
835
|
-
if (item.ws === true)
|
|
836
|
-
return 0;
|
|
837
|
-
const { validator } = item;
|
|
838
|
-
if (!validator || isEmptyObject3(validator))
|
|
839
|
-
return 2;
|
|
840
|
-
if (isFunction2(validator))
|
|
841
|
-
return 0;
|
|
842
|
-
const count = Object.keys(validator).reduce(
|
|
843
|
-
(prev, key) => prev + keysCount(validator[key]),
|
|
844
|
-
0
|
|
845
|
-
);
|
|
846
|
-
return 1 / count;
|
|
847
|
-
});
|
|
848
|
-
}
|
|
849
|
-
function keysCount(obj) {
|
|
850
|
-
if (!obj)
|
|
851
|
-
return 0;
|
|
852
|
-
return Object.keys(obj).length;
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
// src/core/logger.ts
|
|
856
|
-
import { isBoolean } from "@pengzhanbo/utils";
|
|
857
|
-
import colors3 from "picocolors";
|
|
858
|
-
var logLevels = {
|
|
859
|
-
silent: 0,
|
|
860
|
-
error: 1,
|
|
861
|
-
warn: 2,
|
|
862
|
-
info: 3,
|
|
863
|
-
debug: 4
|
|
864
|
-
};
|
|
865
|
-
function createLogger(prefix, defaultLevel = "info") {
|
|
866
|
-
prefix = `[${prefix}]`;
|
|
867
|
-
function output(type, msg, level) {
|
|
868
|
-
level = isBoolean(level) ? level ? defaultLevel : "error" : level;
|
|
869
|
-
const thresh = logLevels[level];
|
|
870
|
-
if (thresh >= logLevels[type]) {
|
|
871
|
-
const method = type === "info" || type === "debug" ? "log" : type;
|
|
872
|
-
const tag = type === "debug" ? colors3.magenta(colors3.bold(prefix)) : type === "info" ? colors3.cyan(colors3.bold(prefix)) : type === "warn" ? colors3.yellow(colors3.bold(prefix)) : colors3.red(colors3.bold(prefix));
|
|
873
|
-
const format = `${colors3.dim(
|
|
874
|
-
(/* @__PURE__ */ new Date()).toLocaleTimeString()
|
|
875
|
-
)} ${tag} ${msg}`;
|
|
876
|
-
console[method](format);
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
const logger = {
|
|
880
|
-
debug(msg, level = defaultLevel) {
|
|
881
|
-
output("debug", msg, level);
|
|
882
|
-
},
|
|
883
|
-
info(msg, level = defaultLevel) {
|
|
884
|
-
output("info", msg, level);
|
|
885
|
-
},
|
|
886
|
-
warn(msg, level = defaultLevel) {
|
|
887
|
-
output("warn", msg, level);
|
|
888
|
-
},
|
|
889
|
-
error(msg, level = defaultLevel) {
|
|
890
|
-
output("error", msg, level);
|
|
891
|
-
}
|
|
892
|
-
};
|
|
893
|
-
return logger;
|
|
894
|
-
}
|
|
895
|
-
|
|
896
896
|
export {
|
|
897
897
|
debug,
|
|
898
898
|
lookupFile,
|
package/dist/helper.d.cts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
3
|
-
import '
|
|
4
|
-
import 'node:http';
|
|
5
|
-
import 'node:stream';
|
|
1
|
+
import { a as MockHttpItem, d as MockWebsocketItem, b as MockOptions } from './types-BYspd62h.cjs';
|
|
2
|
+
export { c as MockRequest } from './types-BYspd62h.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 'vite';
|
|
10
11
|
import 'ws';
|
|
11
|
-
import 'co-body';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* mock config Type helper
|
package/dist/helper.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
3
|
-
import '
|
|
4
|
-
import 'node:http';
|
|
5
|
-
import 'node:stream';
|
|
1
|
+
import { a as MockHttpItem, d as MockWebsocketItem, b as MockOptions } from './types-BYspd62h.js';
|
|
2
|
+
export { c as MockRequest } from './types-BYspd62h.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 'vite';
|
|
10
11
|
import 'ws';
|
|
11
|
-
import 'co-body';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* mock config Type helper
|
package/dist/index.cjs
CHANGED
|
@@ -18,7 +18,7 @@ var _chunkFND5XIG2cjs = require('./chunk-FND5XIG2.cjs');
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
var
|
|
21
|
+
var _chunkJP6LPDGGcjs = require('./chunk-JP6LPDGG.cjs');
|
|
22
22
|
|
|
23
23
|
// src/plugin.ts
|
|
24
24
|
var _utils = require('@pengzhanbo/utils');
|
|
@@ -29,16 +29,16 @@ var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(
|
|
|
29
29
|
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
30
30
|
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
31
31
|
|
|
32
|
+
var _pluginutils = require('@rollup/pluginutils');
|
|
32
33
|
var _fastglob = require('fast-glob'); var _fastglob2 = _interopRequireDefault(_fastglob);
|
|
33
34
|
var _iscoremodule = require('is-core-module'); var _iscoremodule2 = _interopRequireDefault(_iscoremodule);
|
|
34
|
-
var _pluginutils = require('@rollup/pluginutils');
|
|
35
35
|
var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
|
|
36
36
|
|
|
37
37
|
// src/core/compiler.ts
|
|
38
38
|
|
|
39
39
|
|
|
40
|
-
var _url = require('url');
|
|
41
40
|
|
|
41
|
+
var _url = require('url');
|
|
42
42
|
var _esbuild = require('esbuild');
|
|
43
43
|
var _json5 = require('json5'); var _json52 = _interopRequireDefault(_json5);
|
|
44
44
|
var externalizeDeps = {
|
|
@@ -160,14 +160,15 @@ async function loadFromCode({
|
|
|
160
160
|
}) {
|
|
161
161
|
filepath = _path2.default.resolve(cwd, filepath);
|
|
162
162
|
const ext = isESM ? ".mjs" : ".cjs";
|
|
163
|
-
const
|
|
164
|
-
|
|
163
|
+
const filepathTmp = `${filepath}.timestamp-${Date.now()}${ext}`;
|
|
164
|
+
const file = _url.pathToFileURL.call(void 0, filepathTmp).toString();
|
|
165
|
+
await _fs.promises.writeFile(filepathTmp, code, "utf8");
|
|
165
166
|
try {
|
|
166
167
|
const mod = await import(file);
|
|
167
168
|
return mod.default || mod;
|
|
168
169
|
} finally {
|
|
169
170
|
try {
|
|
170
|
-
_fs2.default.unlinkSync(
|
|
171
|
+
_fs2.default.unlinkSync(filepathTmp);
|
|
171
172
|
} catch (e2) {
|
|
172
173
|
}
|
|
173
174
|
}
|
|
@@ -180,7 +181,7 @@ async function generateMockServer(ctx, options) {
|
|
|
180
181
|
const cwd = options.cwd || _process2.default.cwd();
|
|
181
182
|
let pkg = {};
|
|
182
183
|
try {
|
|
183
|
-
const pkgStr =
|
|
184
|
+
const pkgStr = _chunkJP6LPDGGcjs.lookupFile.call(void 0, options.context, ["package.json"]);
|
|
184
185
|
if (pkgStr)
|
|
185
186
|
pkg = JSON.parse(pkgStr);
|
|
186
187
|
} catch (e3) {
|
|
@@ -266,7 +267,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
266
267
|
},
|
|
267
268
|
dependencies: {
|
|
268
269
|
connect: "^3.7.0",
|
|
269
|
-
["vite-plugin-mock-dev-server"]: `^${"1.
|
|
270
|
+
["vite-plugin-mock-dev-server"]: `^${"1.7.1"}`,
|
|
270
271
|
cors: "^2.8.5"
|
|
271
272
|
},
|
|
272
273
|
pnpm: { peerDependencyRules: { ignoreMissing: ["vite"] } }
|
|
@@ -327,7 +328,7 @@ async function generateMockEntryCode(cwd, include, exclude) {
|
|
|
327
328
|
let importers = "";
|
|
328
329
|
const exporters = [];
|
|
329
330
|
mockFiles.forEach((filepath, index) => {
|
|
330
|
-
const file =
|
|
331
|
+
const file = _chunkJP6LPDGGcjs.normalizePath.call(void 0, _path2.default.join(cwd, filepath));
|
|
331
332
|
importers += `import * as m${index} from '${file}';
|
|
332
333
|
`;
|
|
333
334
|
exporters.push(`[m${index}, '${filepath}']`);
|
|
@@ -352,8 +353,8 @@ var _pathtoregexp = require('path-to-regexp');
|
|
|
352
353
|
var _events = require('events'); var _events2 = _interopRequireDefault(_events);
|
|
353
354
|
|
|
354
355
|
|
|
355
|
-
var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar);
|
|
356
356
|
|
|
357
|
+
var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar);
|
|
357
358
|
|
|
358
359
|
function createMockCompiler(options) {
|
|
359
360
|
return new MockCompiler(options);
|
|
@@ -364,7 +365,7 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
364
365
|
this.options = options;
|
|
365
366
|
this.cwd = options.cwd || _process2.default.cwd();
|
|
366
367
|
try {
|
|
367
|
-
const pkg =
|
|
368
|
+
const pkg = _chunkJP6LPDGGcjs.lookupFile.call(void 0, this.cwd, ["package.json"]);
|
|
368
369
|
this.moduleType = !!pkg && JSON.parse(pkg).type === "module" ? "esm" : "cjs";
|
|
369
370
|
} catch (e4) {
|
|
370
371
|
}
|
|
@@ -418,19 +419,19 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
418
419
|
if (otherGlob.length > 0)
|
|
419
420
|
otherGlob.forEach((glob) => watcher.add(glob));
|
|
420
421
|
watcher.on("add", async (filepath) => {
|
|
421
|
-
filepath =
|
|
422
|
+
filepath = _chunkJP6LPDGGcjs.normalizePath.call(void 0, filepath);
|
|
422
423
|
this.emit("mock:update", filepath);
|
|
423
|
-
|
|
424
|
+
_chunkJP6LPDGGcjs.debug.call(void 0, "watcher:add", filepath);
|
|
424
425
|
});
|
|
425
426
|
watcher.on("change", async (filepath) => {
|
|
426
|
-
filepath =
|
|
427
|
+
filepath = _chunkJP6LPDGGcjs.normalizePath.call(void 0, filepath);
|
|
427
428
|
this.emit("mock:update", filepath);
|
|
428
|
-
|
|
429
|
+
_chunkJP6LPDGGcjs.debug.call(void 0, "watcher:change", filepath);
|
|
429
430
|
});
|
|
430
431
|
watcher.on("unlink", async (filepath) => {
|
|
431
|
-
filepath =
|
|
432
|
+
filepath = _chunkJP6LPDGGcjs.normalizePath.call(void 0, filepath);
|
|
432
433
|
this.emit("mock:unlink", filepath);
|
|
433
|
-
|
|
434
|
+
_chunkJP6LPDGGcjs.debug.call(void 0, "watcher:unlink", filepath);
|
|
434
435
|
});
|
|
435
436
|
}
|
|
436
437
|
/**
|
|
@@ -444,14 +445,14 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
444
445
|
cwd: this.cwd
|
|
445
446
|
});
|
|
446
447
|
this.depsWatcher.on("change", (filepath) => {
|
|
447
|
-
filepath =
|
|
448
|
+
filepath = _chunkJP6LPDGGcjs.normalizePath.call(void 0, filepath);
|
|
448
449
|
const mockFiles = this.moduleDeps.get(filepath);
|
|
449
450
|
_optionalChain([mockFiles, 'optionalAccess', _4 => _4.forEach, 'call', _5 => _5((file) => {
|
|
450
451
|
this.emit("mock:update", file);
|
|
451
452
|
})]);
|
|
452
453
|
});
|
|
453
454
|
this.depsWatcher.on("unlink", (filepath) => {
|
|
454
|
-
filepath =
|
|
455
|
+
filepath = _chunkJP6LPDGGcjs.normalizePath.call(void 0, filepath);
|
|
455
456
|
this.moduleDeps.delete(filepath);
|
|
456
457
|
});
|
|
457
458
|
this.on("update:deps", () => {
|
|
@@ -468,7 +469,7 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
468
469
|
_optionalChain([this, 'access', _9 => _9.depsWatcher, 'optionalAccess', _10 => _10.close, 'call', _11 => _11()]);
|
|
469
470
|
}
|
|
470
471
|
updateMockList() {
|
|
471
|
-
this._mockData =
|
|
472
|
+
this._mockData = _chunkJP6LPDGGcjs.transformMockData.call(void 0, this.moduleCache);
|
|
472
473
|
}
|
|
473
474
|
updateModuleDeps(filepath, deps) {
|
|
474
475
|
Object.keys(deps).forEach((mPath) => {
|
|
@@ -499,7 +500,7 @@ var MockCompiler = (_class = class extends _events2.default {
|
|
|
499
500
|
);
|
|
500
501
|
try {
|
|
501
502
|
const raw = await loadFromCode({ filepath, code, isESM, cwd: this.cwd }) || {};
|
|
502
|
-
this.moduleCache.set(filepath,
|
|
503
|
+
this.moduleCache.set(filepath, _chunkJP6LPDGGcjs.transformRawData.call(void 0, raw, filepath));
|
|
503
504
|
this.updateModuleDeps(filepath, deps);
|
|
504
505
|
} catch (e) {
|
|
505
506
|
console.error(e);
|
|
@@ -516,7 +517,7 @@ function mockServerMiddleware(options, server, ws) {
|
|
|
516
517
|
_optionalChain([ws, 'optionalAccess', _12 => _12.send, 'call', _13 => _13({ type: "full-reload" })]);
|
|
517
518
|
});
|
|
518
519
|
_optionalChain([server, 'optionalAccess', _14 => _14.on, 'call', _15 => _15("close", () => compiler.close())]);
|
|
519
|
-
|
|
520
|
+
_chunkJP6LPDGGcjs.mockWebSocket.call(void 0, compiler, server, options);
|
|
520
521
|
const middlewares = [];
|
|
521
522
|
middlewares.push(
|
|
522
523
|
/**
|
|
@@ -531,14 +532,14 @@ function mockServerMiddleware(options, server, ws) {
|
|
|
531
532
|
* 而用户的配置也仅对 mock 的接口生效。
|
|
532
533
|
*/
|
|
533
534
|
corsMiddleware(compiler, options),
|
|
534
|
-
|
|
535
|
+
_chunkJP6LPDGGcjs.baseMiddleware.call(void 0, compiler, options)
|
|
535
536
|
);
|
|
536
537
|
return middlewares.filter(Boolean);
|
|
537
538
|
}
|
|
538
539
|
function corsMiddleware(compiler, { proxies, cors: corsOptions }) {
|
|
539
540
|
return !corsOptions ? void 0 : function(req, res, next) {
|
|
540
|
-
const { pathname } =
|
|
541
|
-
if (!pathname || proxies.length === 0 || !proxies.some((context) =>
|
|
541
|
+
const { pathname } = _chunkJP6LPDGGcjs.urlParse.call(void 0, req.url);
|
|
542
|
+
if (!pathname || proxies.length === 0 || !proxies.some((context) => _chunkJP6LPDGGcjs.doesProxyContextMatchUrl.call(void 0, context, req.url))) {
|
|
542
543
|
return next();
|
|
543
544
|
}
|
|
544
545
|
const mockData = compiler.mockData;
|
|
@@ -651,8 +652,8 @@ function resolvePluginOptions({
|
|
|
651
652
|
bodyParserOptions = {},
|
|
652
653
|
priority = {}
|
|
653
654
|
}, config) {
|
|
654
|
-
const logger =
|
|
655
|
-
const { httpProxies } =
|
|
655
|
+
const logger = _chunkJP6LPDGGcjs.createLogger.call(void 0, "vite:mock", _utils.isBoolean.call(void 0, log) ? log ? "info" : "error" : log);
|
|
656
|
+
const { httpProxies } = _chunkJP6LPDGGcjs.ensureProxies.call(void 0, config.server.proxy || {});
|
|
656
657
|
const proxies = _utils.uniq.call(void 0, [..._utils.toArray.call(void 0, prefix), ...httpProxies]);
|
|
657
658
|
const wsProxies = _utils.toArray.call(void 0, wsPrefix);
|
|
658
659
|
if (!proxies.length && !wsProxies.length)
|
|
@@ -753,7 +754,7 @@ function serverPlugin(options) {
|
|
|
753
754
|
});
|
|
754
755
|
config.server.proxy = proxy;
|
|
755
756
|
}
|
|
756
|
-
|
|
757
|
+
_chunkJP6LPDGGcjs.recoverRequest.call(void 0, config);
|
|
757
758
|
},
|
|
758
759
|
configResolved(config) {
|
|
759
760
|
resolvedOptions = resolvePluginOptions(options, config);
|
|
@@ -785,4 +786,4 @@ var src_default = mockDevServerPlugin;
|
|
|
785
786
|
|
|
786
787
|
|
|
787
788
|
|
|
788
|
-
exports.baseMiddleware =
|
|
789
|
+
exports.baseMiddleware = _chunkJP6LPDGGcjs.baseMiddleware; exports.createDefineMock = _chunkFND5XIG2cjs.createDefineMock; exports.createLogger = _chunkJP6LPDGGcjs.createLogger; exports.default = src_default; exports.defineMock = _chunkFND5XIG2cjs.defineMock; exports.defineMockData = _chunkFND5XIG2cjs.defineMockData; exports.logLevels = _chunkJP6LPDGGcjs.logLevels; exports.mockDevServerPlugin = mockDevServerPlugin; exports.mockWebSocket = _chunkJP6LPDGGcjs.mockWebSocket; exports.sortByValidator = _chunkJP6LPDGGcjs.sortByValidator; exports.transformMockData = _chunkJP6LPDGGcjs.transformMockData; exports.transformRawData = _chunkJP6LPDGGcjs.transformRawData;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
-
import { M as MockServerPluginOptions } from './types-
|
|
3
|
-
export { F as FormidableFile,
|
|
2
|
+
import { M as MockServerPluginOptions } from './types-BYspd62h.cjs';
|
|
3
|
+
export { F as FormidableFile, a as MockHttpItem, b as MockOptions, c as MockRequest, d as MockWebsocketItem } from './types-BYspd62h.cjs';
|
|
4
4
|
export { MockData, createDefineMock, defineMock, defineMockData } from './helper.cjs';
|
|
5
5
|
export { BaseMiddlewareOptions, Logger, baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData } from './server.cjs';
|
|
6
|
-
import '
|
|
7
|
-
import 'node:http';
|
|
8
|
-
import 'node:stream';
|
|
6
|
+
import 'co-body';
|
|
9
7
|
import 'cookies';
|
|
10
8
|
import 'cors';
|
|
11
9
|
import 'formidable';
|
|
10
|
+
import 'node:buffer';
|
|
11
|
+
import 'node:http';
|
|
12
|
+
import 'node:stream';
|
|
12
13
|
import 'ws';
|
|
13
|
-
import 'co-body';
|
|
14
14
|
import 'node:events';
|
|
15
15
|
import 'chokidar';
|
|
16
16
|
import 'node:http2';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
-
import { M as MockServerPluginOptions } from './types-
|
|
3
|
-
export { F as FormidableFile,
|
|
2
|
+
import { M as MockServerPluginOptions } from './types-BYspd62h.js';
|
|
3
|
+
export { F as FormidableFile, a as MockHttpItem, b as MockOptions, c as MockRequest, d as MockWebsocketItem } from './types-BYspd62h.js';
|
|
4
4
|
export { MockData, createDefineMock, defineMock, defineMockData } from './helper.js';
|
|
5
5
|
export { BaseMiddlewareOptions, Logger, baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData } from './server.js';
|
|
6
|
-
import '
|
|
7
|
-
import 'node:http';
|
|
8
|
-
import 'node:stream';
|
|
6
|
+
import 'co-body';
|
|
9
7
|
import 'cookies';
|
|
10
8
|
import 'cors';
|
|
11
9
|
import 'formidable';
|
|
10
|
+
import 'node:buffer';
|
|
11
|
+
import 'node:http';
|
|
12
|
+
import 'node:stream';
|
|
12
13
|
import 'ws';
|
|
13
|
-
import 'co-body';
|
|
14
14
|
import 'node:events';
|
|
15
15
|
import 'chokidar';
|
|
16
16
|
import 'node:http2';
|
package/dist/index.js
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
transformMockData,
|
|
19
19
|
transformRawData,
|
|
20
20
|
urlParse
|
|
21
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-FUNGHLD3.js";
|
|
22
22
|
|
|
23
23
|
// src/plugin.ts
|
|
24
24
|
import { toArray as toArray4 } from "@pengzhanbo/utils";
|
|
@@ -29,16 +29,16 @@ import fsp2 from "node:fs/promises";
|
|
|
29
29
|
import path2 from "node:path";
|
|
30
30
|
import process2 from "node:process";
|
|
31
31
|
import { toArray } from "@pengzhanbo/utils";
|
|
32
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
32
33
|
import fg from "fast-glob";
|
|
33
34
|
import isCore from "is-core-module";
|
|
34
|
-
import { createFilter } from "@rollup/pluginutils";
|
|
35
35
|
import c from "picocolors";
|
|
36
36
|
|
|
37
37
|
// src/core/compiler.ts
|
|
38
38
|
import fs, { promises as fsp } from "node:fs";
|
|
39
39
|
import path from "node:path";
|
|
40
|
-
import { pathToFileURL } from "node:url";
|
|
41
40
|
import process from "node:process";
|
|
41
|
+
import { pathToFileURL } from "node:url";
|
|
42
42
|
import { build } from "esbuild";
|
|
43
43
|
import JSON5 from "json5";
|
|
44
44
|
var externalizeDeps = {
|
|
@@ -160,14 +160,15 @@ async function loadFromCode({
|
|
|
160
160
|
}) {
|
|
161
161
|
filepath = path.resolve(cwd, filepath);
|
|
162
162
|
const ext = isESM ? ".mjs" : ".cjs";
|
|
163
|
-
const
|
|
164
|
-
|
|
163
|
+
const filepathTmp = `${filepath}.timestamp-${Date.now()}${ext}`;
|
|
164
|
+
const file = pathToFileURL(filepathTmp).toString();
|
|
165
|
+
await fsp.writeFile(filepathTmp, code, "utf8");
|
|
165
166
|
try {
|
|
166
167
|
const mod = await import(file);
|
|
167
168
|
return mod.default || mod;
|
|
168
169
|
} finally {
|
|
169
170
|
try {
|
|
170
|
-
fs.unlinkSync(
|
|
171
|
+
fs.unlinkSync(filepathTmp);
|
|
171
172
|
} catch {
|
|
172
173
|
}
|
|
173
174
|
}
|
|
@@ -266,7 +267,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
266
267
|
},
|
|
267
268
|
dependencies: {
|
|
268
269
|
connect: "^3.7.0",
|
|
269
|
-
["vite-plugin-mock-dev-server"]: `^${"1.
|
|
270
|
+
["vite-plugin-mock-dev-server"]: `^${"1.7.1"}`,
|
|
270
271
|
cors: "^2.8.5"
|
|
271
272
|
},
|
|
272
273
|
pnpm: { peerDependencyRules: { ignoreMissing: ["vite"] } }
|
|
@@ -352,9 +353,9 @@ import { pathToRegexp } from "path-to-regexp";
|
|
|
352
353
|
import EventEmitter from "node:events";
|
|
353
354
|
import process3 from "node:process";
|
|
354
355
|
import { promiseParallel, toArray as toArray2 } from "@pengzhanbo/utils";
|
|
356
|
+
import { createFilter as createFilter2 } from "@rollup/pluginutils";
|
|
355
357
|
import chokidar from "chokidar";
|
|
356
358
|
import fastGlob from "fast-glob";
|
|
357
|
-
import { createFilter as createFilter2 } from "@rollup/pluginutils";
|
|
358
359
|
function createMockCompiler(options) {
|
|
359
360
|
return new MockCompiler(options);
|
|
360
361
|
}
|
package/dist/server.cjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _chunkJP6LPDGGcjs = require('./chunk-JP6LPDGG.cjs');
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
@@ -15,4 +15,4 @@ var _chunkS76FMTCDcjs = require('./chunk-S76FMTCD.cjs');
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
exports.baseMiddleware =
|
|
18
|
+
exports.baseMiddleware = _chunkJP6LPDGGcjs.baseMiddleware; exports.createLogger = _chunkJP6LPDGGcjs.createLogger; exports.logLevels = _chunkJP6LPDGGcjs.logLevels; exports.mockWebSocket = _chunkJP6LPDGGcjs.mockWebSocket; exports.sortByValidator = _chunkJP6LPDGGcjs.sortByValidator; exports.transformMockData = _chunkJP6LPDGGcjs.transformMockData; exports.transformRawData = _chunkJP6LPDGGcjs.transformRawData;
|
package/dist/server.d.cts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { Alias, Connect } from 'vite';
|
|
2
|
-
import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption,
|
|
2
|
+
import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption, b as MockOptions, a as MockHttpItem, d as MockWebsocketItem } from './types-BYspd62h.cjs';
|
|
3
3
|
import EventEmitter from 'node:events';
|
|
4
4
|
import { FSWatcher } from 'chokidar';
|
|
5
5
|
import { CorsOptions } from 'cors';
|
|
6
6
|
import { Server } from 'node:http';
|
|
7
7
|
import { Http2SecureServer } from 'node:http2';
|
|
8
|
-
import '
|
|
9
|
-
import 'node:stream';
|
|
8
|
+
import 'co-body';
|
|
10
9
|
import 'cookies';
|
|
11
10
|
import 'formidable';
|
|
11
|
+
import 'node:buffer';
|
|
12
|
+
import 'node:stream';
|
|
12
13
|
import 'ws';
|
|
13
|
-
import 'co-body';
|
|
14
14
|
|
|
15
15
|
interface Logger {
|
|
16
16
|
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
@@ -69,6 +69,11 @@ interface BaseMiddlewareOptions {
|
|
|
69
69
|
}
|
|
70
70
|
declare function baseMiddleware(compiler: MockCompiler, { formidableOptions, bodyParserOptions, proxies, cookiesOptions, logger, priority, }: BaseMiddlewareOptions): Connect.NextHandleFunction;
|
|
71
71
|
|
|
72
|
+
type MockRawData = MockOptions | MockHttpItem | MockWebsocketItem | Record<string, MockOptions | MockHttpItem | MockWebsocketItem>;
|
|
73
|
+
declare function transformRawData(raw: MockRawData, __filepath__: string): MockOptions | MockHttpItem | MockWebsocketItem;
|
|
74
|
+
declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
75
|
+
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
76
|
+
|
|
72
77
|
/**
|
|
73
78
|
* 不复用 `viteConfig.server.proxy` 中 websocket proxy的原因是,
|
|
74
79
|
* 很难通过一种令人满意的方式,检查 mock 文件中是否有 websocket 相关的 mock 配置,
|
|
@@ -86,9 +91,4 @@ declare function baseMiddleware(compiler: MockCompiler, { formidableOptions, bod
|
|
|
86
91
|
*/
|
|
87
92
|
declare function mockWebSocket(compiler: MockCompiler, server: Server | Http2SecureServer | null, { wsProxies: proxies, cookiesOptions, logger, }: ResolvedMockServerPluginOptions): void;
|
|
88
93
|
|
|
89
|
-
type MockRawData = MockOptions | MockHttpItem | MockWebsocketItem | Record<string, MockOptions | MockHttpItem | MockWebsocketItem>;
|
|
90
|
-
declare function transformRawData(raw: MockRawData, __filepath__: string): MockOptions | MockHttpItem | MockWebsocketItem;
|
|
91
|
-
declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
92
|
-
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
93
|
-
|
|
94
94
|
export { type BaseMiddlewareOptions, type Logger, baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { Alias, Connect } from 'vite';
|
|
2
|
-
import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption,
|
|
2
|
+
import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption, b as MockOptions, a as MockHttpItem, d as MockWebsocketItem } from './types-BYspd62h.js';
|
|
3
3
|
import EventEmitter from 'node:events';
|
|
4
4
|
import { FSWatcher } from 'chokidar';
|
|
5
5
|
import { CorsOptions } from 'cors';
|
|
6
6
|
import { Server } from 'node:http';
|
|
7
7
|
import { Http2SecureServer } from 'node:http2';
|
|
8
|
-
import '
|
|
9
|
-
import 'node:stream';
|
|
8
|
+
import 'co-body';
|
|
10
9
|
import 'cookies';
|
|
11
10
|
import 'formidable';
|
|
11
|
+
import 'node:buffer';
|
|
12
|
+
import 'node:stream';
|
|
12
13
|
import 'ws';
|
|
13
|
-
import 'co-body';
|
|
14
14
|
|
|
15
15
|
interface Logger {
|
|
16
16
|
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
@@ -69,6 +69,11 @@ interface BaseMiddlewareOptions {
|
|
|
69
69
|
}
|
|
70
70
|
declare function baseMiddleware(compiler: MockCompiler, { formidableOptions, bodyParserOptions, proxies, cookiesOptions, logger, priority, }: BaseMiddlewareOptions): Connect.NextHandleFunction;
|
|
71
71
|
|
|
72
|
+
type MockRawData = MockOptions | MockHttpItem | MockWebsocketItem | Record<string, MockOptions | MockHttpItem | MockWebsocketItem>;
|
|
73
|
+
declare function transformRawData(raw: MockRawData, __filepath__: string): MockOptions | MockHttpItem | MockWebsocketItem;
|
|
74
|
+
declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
75
|
+
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
76
|
+
|
|
72
77
|
/**
|
|
73
78
|
* 不复用 `viteConfig.server.proxy` 中 websocket proxy的原因是,
|
|
74
79
|
* 很难通过一种令人满意的方式,检查 mock 文件中是否有 websocket 相关的 mock 配置,
|
|
@@ -86,9 +91,4 @@ declare function baseMiddleware(compiler: MockCompiler, { formidableOptions, bod
|
|
|
86
91
|
*/
|
|
87
92
|
declare function mockWebSocket(compiler: MockCompiler, server: Server | Http2SecureServer | null, { wsProxies: proxies, cookiesOptions, logger, }: ResolvedMockServerPluginOptions): void;
|
|
88
93
|
|
|
89
|
-
type MockRawData = MockOptions | MockHttpItem | MockWebsocketItem | Record<string, MockOptions | MockHttpItem | MockWebsocketItem>;
|
|
90
|
-
declare function transformRawData(raw: MockRawData, __filepath__: string): MockOptions | MockHttpItem | MockWebsocketItem;
|
|
91
|
-
declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
92
|
-
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
93
|
-
|
|
94
94
|
export { type BaseMiddlewareOptions, type Logger, baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData };
|
package/dist/server.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import http from 'node:http';
|
|
3
|
-
import { Readable } from 'node:stream';
|
|
1
|
+
import { Options } from 'co-body';
|
|
4
2
|
import Cookies from 'cookies';
|
|
5
3
|
import { CorsOptions } from 'cors';
|
|
6
4
|
import formidable from 'formidable';
|
|
5
|
+
import { Buffer } from 'node:buffer';
|
|
6
|
+
import http from 'node:http';
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
7
8
|
import { Connect } from 'vite';
|
|
8
9
|
import { WebSocketServer } from 'ws';
|
|
9
|
-
import { Options } from 'co-body';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Configure plugin
|
|
@@ -568,4 +568,4 @@ type FormidableFile = formidable.File | formidable.File[];
|
|
|
568
568
|
type LogType = 'info' | 'warn' | 'error' | 'debug';
|
|
569
569
|
type LogLevel = LogType | 'silent';
|
|
570
570
|
|
|
571
|
-
export type { FormidableFile as F, LogLevel as L, MockServerPluginOptions as M, ServerBuildOption as S,
|
|
571
|
+
export type { FormidableFile as F, LogLevel as L, MockServerPluginOptions as M, ServerBuildOption as S, MockHttpItem as a, MockOptions as b, MockRequest as c, MockWebsocketItem as d };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import http from 'node:http';
|
|
3
|
-
import { Readable } from 'node:stream';
|
|
1
|
+
import { Options } from 'co-body';
|
|
4
2
|
import Cookies from 'cookies';
|
|
5
3
|
import { CorsOptions } from 'cors';
|
|
6
4
|
import formidable from 'formidable';
|
|
5
|
+
import { Buffer } from 'node:buffer';
|
|
6
|
+
import http from 'node:http';
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
7
8
|
import { Connect } from 'vite';
|
|
8
9
|
import { WebSocketServer } from 'ws';
|
|
9
|
-
import { Options } from 'co-body';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Configure plugin
|
|
@@ -568,4 +568,4 @@ type FormidableFile = formidable.File | formidable.File[];
|
|
|
568
568
|
type LogType = 'info' | 'warn' | 'error' | 'debug';
|
|
569
569
|
type LogLevel = LogType | 'silent';
|
|
570
570
|
|
|
571
|
-
export type { FormidableFile as F, LogLevel as L, MockServerPluginOptions as M, ServerBuildOption as S,
|
|
571
|
+
export type { FormidableFile as F, LogLevel as L, MockServerPluginOptions as M, ServerBuildOption as S, MockHttpItem as a, MockOptions as b, MockRequest as c, MockWebsocketItem as d };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-dev-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.7.
|
|
4
|
+
"version": "1.7.2",
|
|
5
5
|
"author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://vite-plugin-mock-dev-server.netlify.app",
|
|
@@ -70,24 +70,27 @@
|
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@pengzhanbo/utils": "^1.1.2",
|
|
72
72
|
"@rollup/pluginutils": "^5.1.0",
|
|
73
|
-
"chokidar": "^
|
|
73
|
+
"chokidar": "^4.0.0",
|
|
74
74
|
"co-body": "^6.2.0",
|
|
75
75
|
"cookies": "^0.9.1",
|
|
76
76
|
"cors": "^2.8.5",
|
|
77
|
-
"debug": "^4.3.
|
|
77
|
+
"debug": "^4.3.7",
|
|
78
78
|
"fast-glob": "^3.3.2",
|
|
79
79
|
"formidable": "2.1.2",
|
|
80
80
|
"http-status": "^1.7.4",
|
|
81
|
-
"is-core-module": "^2.15.
|
|
81
|
+
"is-core-module": "^2.15.1",
|
|
82
82
|
"json5": "^2.2.3",
|
|
83
83
|
"mime-types": "^2.1.35",
|
|
84
84
|
"path-to-regexp": "6.2.2",
|
|
85
|
-
"picocolors": "^1.0
|
|
85
|
+
"picocolors": "^1.1.0",
|
|
86
86
|
"ws": "^8.18.0"
|
|
87
87
|
},
|
|
88
88
|
"devDependencies": {
|
|
89
|
-
"esbuild": "^0.23.
|
|
90
|
-
"vite": "^5.
|
|
89
|
+
"esbuild": "^0.23.1",
|
|
90
|
+
"vite": "^5.4.5"
|
|
91
|
+
},
|
|
92
|
+
"publishConfig": {
|
|
93
|
+
"access": "public"
|
|
91
94
|
},
|
|
92
95
|
"scripts": {
|
|
93
96
|
"build": "tsup"
|
|
@@ -252,10 +252,10 @@ function recoverRequest(config) {
|
|
|
252
252
|
|
|
253
253
|
// src/core/utils.ts
|
|
254
254
|
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
|
|
255
|
+
var _os = require('os'); var _os2 = _interopRequireDefault(_os);
|
|
255
256
|
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
256
257
|
var _querystring = require('querystring');
|
|
257
258
|
var _url = require('url');
|
|
258
|
-
var _os = require('os'); var _os2 = _interopRequireDefault(_os);
|
|
259
259
|
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
260
260
|
|
|
261
261
|
function isStream(stream) {
|
|
@@ -612,6 +612,130 @@ function requestLog(request, filepath) {
|
|
|
612
612
|
return `${ms} ${pathname}${qs}${ps}${bs}${file}`;
|
|
613
613
|
}
|
|
614
614
|
|
|
615
|
+
// src/core/logger.ts
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
var logLevels = {
|
|
619
|
+
silent: 0,
|
|
620
|
+
error: 1,
|
|
621
|
+
warn: 2,
|
|
622
|
+
info: 3,
|
|
623
|
+
debug: 4
|
|
624
|
+
};
|
|
625
|
+
function createLogger(prefix, defaultLevel = "info") {
|
|
626
|
+
prefix = `[${prefix}]`;
|
|
627
|
+
function output(type, msg, level) {
|
|
628
|
+
level = _utils.isBoolean.call(void 0, level) ? level ? defaultLevel : "error" : level;
|
|
629
|
+
const thresh = logLevels[level];
|
|
630
|
+
if (thresh >= logLevels[type]) {
|
|
631
|
+
const method = type === "info" || type === "debug" ? "log" : type;
|
|
632
|
+
const tag = type === "debug" ? _picocolors2.default.magenta(_picocolors2.default.bold(prefix)) : type === "info" ? _picocolors2.default.cyan(_picocolors2.default.bold(prefix)) : type === "warn" ? _picocolors2.default.yellow(_picocolors2.default.bold(prefix)) : _picocolors2.default.red(_picocolors2.default.bold(prefix));
|
|
633
|
+
const format = `${_picocolors2.default.dim(
|
|
634
|
+
(/* @__PURE__ */ new Date()).toLocaleTimeString()
|
|
635
|
+
)} ${tag} ${msg}`;
|
|
636
|
+
console[method](format);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
const logger = {
|
|
640
|
+
debug(msg, level = defaultLevel) {
|
|
641
|
+
output("debug", msg, level);
|
|
642
|
+
},
|
|
643
|
+
info(msg, level = defaultLevel) {
|
|
644
|
+
output("info", msg, level);
|
|
645
|
+
},
|
|
646
|
+
warn(msg, level = defaultLevel) {
|
|
647
|
+
output("warn", msg, level);
|
|
648
|
+
},
|
|
649
|
+
error(msg, level = defaultLevel) {
|
|
650
|
+
output("error", msg, level);
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
return logger;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// src/core/transform.ts
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
function transformRawData(raw, __filepath__) {
|
|
666
|
+
let mockConfig;
|
|
667
|
+
if (_utils.isArray.call(void 0, raw)) {
|
|
668
|
+
mockConfig = raw.map((item) => ({ ...item, __filepath__ }));
|
|
669
|
+
} else if ("url" in raw) {
|
|
670
|
+
mockConfig = { ...raw, __filepath__ };
|
|
671
|
+
} else {
|
|
672
|
+
mockConfig = [];
|
|
673
|
+
Object.keys(raw).forEach((key) => {
|
|
674
|
+
const data = raw[key];
|
|
675
|
+
if (_utils.isArray.call(void 0, data)) {
|
|
676
|
+
mockConfig.push(...data.map((item) => ({ ...item, __filepath__ })));
|
|
677
|
+
} else {
|
|
678
|
+
mockConfig.push({ ...data, __filepath__ });
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
return mockConfig;
|
|
683
|
+
}
|
|
684
|
+
function transformMockData(mockList) {
|
|
685
|
+
const list = [];
|
|
686
|
+
for (const [, handle] of mockList.entries()) {
|
|
687
|
+
if (handle)
|
|
688
|
+
list.push(..._utils.toArray.call(void 0, handle));
|
|
689
|
+
}
|
|
690
|
+
const mocks = {};
|
|
691
|
+
list.filter((mock) => _utils.isObject.call(void 0, mock) && mock.enabled !== false && mock.url).forEach((mock) => {
|
|
692
|
+
const { pathname, query } = urlParse(mock.url);
|
|
693
|
+
const list2 = mocks[pathname] ??= [];
|
|
694
|
+
const current = { ...mock, url: pathname };
|
|
695
|
+
if (current.ws !== true) {
|
|
696
|
+
const validator = current.validator;
|
|
697
|
+
if (!_utils.isEmptyObject.call(void 0, query)) {
|
|
698
|
+
if (_utils.isFunction.call(void 0, validator)) {
|
|
699
|
+
current.validator = function(request) {
|
|
700
|
+
return isObjectSubset(request.query, query) && validator(request);
|
|
701
|
+
};
|
|
702
|
+
} else if (validator) {
|
|
703
|
+
current.validator = { ...validator };
|
|
704
|
+
current.validator.query = current.validator.query ? { ...query, ...current.validator.query } : query;
|
|
705
|
+
} else {
|
|
706
|
+
current.validator = { query };
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
list2.push(current);
|
|
711
|
+
});
|
|
712
|
+
Object.keys(mocks).forEach((key) => {
|
|
713
|
+
mocks[key] = sortByValidator(mocks[key]);
|
|
714
|
+
});
|
|
715
|
+
return mocks;
|
|
716
|
+
}
|
|
717
|
+
function sortByValidator(mocks) {
|
|
718
|
+
return _utils.sortBy.call(void 0, mocks, (item) => {
|
|
719
|
+
if (item.ws === true)
|
|
720
|
+
return 0;
|
|
721
|
+
const { validator } = item;
|
|
722
|
+
if (!validator || _utils.isEmptyObject.call(void 0, validator))
|
|
723
|
+
return 2;
|
|
724
|
+
if (_utils.isFunction.call(void 0, validator))
|
|
725
|
+
return 0;
|
|
726
|
+
const count = Object.keys(validator).reduce(
|
|
727
|
+
(prev, key) => prev + keysCount(validator[key]),
|
|
728
|
+
0
|
|
729
|
+
);
|
|
730
|
+
return 1 / count;
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
function keysCount(obj) {
|
|
734
|
+
if (!obj)
|
|
735
|
+
return 0;
|
|
736
|
+
return Object.keys(obj).length;
|
|
737
|
+
}
|
|
738
|
+
|
|
615
739
|
// src/core/ws.ts
|
|
616
740
|
|
|
617
741
|
|
|
@@ -769,130 +893,6 @@ function cleanupRunner(cleanupList) {
|
|
|
769
893
|
_optionalChain([cleanup, 'optionalCall', _28 => _28()]);
|
|
770
894
|
}
|
|
771
895
|
|
|
772
|
-
// src/core/transform.ts
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
function transformRawData(raw, __filepath__) {
|
|
782
|
-
let mockConfig;
|
|
783
|
-
if (_utils.isArray.call(void 0, raw)) {
|
|
784
|
-
mockConfig = raw.map((item) => ({ ...item, __filepath__ }));
|
|
785
|
-
} else if ("url" in raw) {
|
|
786
|
-
mockConfig = { ...raw, __filepath__ };
|
|
787
|
-
} else {
|
|
788
|
-
mockConfig = [];
|
|
789
|
-
Object.keys(raw).forEach((key) => {
|
|
790
|
-
const data = raw[key];
|
|
791
|
-
if (_utils.isArray.call(void 0, data)) {
|
|
792
|
-
mockConfig.push(...data.map((item) => ({ ...item, __filepath__ })));
|
|
793
|
-
} else {
|
|
794
|
-
mockConfig.push({ ...data, __filepath__ });
|
|
795
|
-
}
|
|
796
|
-
});
|
|
797
|
-
}
|
|
798
|
-
return mockConfig;
|
|
799
|
-
}
|
|
800
|
-
function transformMockData(mockList) {
|
|
801
|
-
const list = [];
|
|
802
|
-
for (const [, handle] of mockList.entries()) {
|
|
803
|
-
if (handle)
|
|
804
|
-
list.push(..._utils.toArray.call(void 0, handle));
|
|
805
|
-
}
|
|
806
|
-
const mocks = {};
|
|
807
|
-
list.filter((mock) => _utils.isObject.call(void 0, mock) && mock.enabled !== false && mock.url).forEach((mock) => {
|
|
808
|
-
const { pathname, query } = urlParse(mock.url);
|
|
809
|
-
const list2 = mocks[pathname] ??= [];
|
|
810
|
-
const current = { ...mock, url: pathname };
|
|
811
|
-
if (current.ws !== true) {
|
|
812
|
-
const validator = current.validator;
|
|
813
|
-
if (!_utils.isEmptyObject.call(void 0, query)) {
|
|
814
|
-
if (_utils.isFunction.call(void 0, validator)) {
|
|
815
|
-
current.validator = function(request) {
|
|
816
|
-
return isObjectSubset(request.query, query) && validator(request);
|
|
817
|
-
};
|
|
818
|
-
} else if (validator) {
|
|
819
|
-
current.validator = { ...validator };
|
|
820
|
-
current.validator.query = current.validator.query ? { ...query, ...current.validator.query } : query;
|
|
821
|
-
} else {
|
|
822
|
-
current.validator = { query };
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
list2.push(current);
|
|
827
|
-
});
|
|
828
|
-
Object.keys(mocks).forEach((key) => {
|
|
829
|
-
mocks[key] = sortByValidator(mocks[key]);
|
|
830
|
-
});
|
|
831
|
-
return mocks;
|
|
832
|
-
}
|
|
833
|
-
function sortByValidator(mocks) {
|
|
834
|
-
return _utils.sortBy.call(void 0, mocks, (item) => {
|
|
835
|
-
if (item.ws === true)
|
|
836
|
-
return 0;
|
|
837
|
-
const { validator } = item;
|
|
838
|
-
if (!validator || _utils.isEmptyObject.call(void 0, validator))
|
|
839
|
-
return 2;
|
|
840
|
-
if (_utils.isFunction.call(void 0, validator))
|
|
841
|
-
return 0;
|
|
842
|
-
const count = Object.keys(validator).reduce(
|
|
843
|
-
(prev, key) => prev + keysCount(validator[key]),
|
|
844
|
-
0
|
|
845
|
-
);
|
|
846
|
-
return 1 / count;
|
|
847
|
-
});
|
|
848
|
-
}
|
|
849
|
-
function keysCount(obj) {
|
|
850
|
-
if (!obj)
|
|
851
|
-
return 0;
|
|
852
|
-
return Object.keys(obj).length;
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
// src/core/logger.ts
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
var logLevels = {
|
|
859
|
-
silent: 0,
|
|
860
|
-
error: 1,
|
|
861
|
-
warn: 2,
|
|
862
|
-
info: 3,
|
|
863
|
-
debug: 4
|
|
864
|
-
};
|
|
865
|
-
function createLogger(prefix, defaultLevel = "info") {
|
|
866
|
-
prefix = `[${prefix}]`;
|
|
867
|
-
function output(type, msg, level) {
|
|
868
|
-
level = _utils.isBoolean.call(void 0, level) ? level ? defaultLevel : "error" : level;
|
|
869
|
-
const thresh = logLevels[level];
|
|
870
|
-
if (thresh >= logLevels[type]) {
|
|
871
|
-
const method = type === "info" || type === "debug" ? "log" : type;
|
|
872
|
-
const tag = type === "debug" ? _picocolors2.default.magenta(_picocolors2.default.bold(prefix)) : type === "info" ? _picocolors2.default.cyan(_picocolors2.default.bold(prefix)) : type === "warn" ? _picocolors2.default.yellow(_picocolors2.default.bold(prefix)) : _picocolors2.default.red(_picocolors2.default.bold(prefix));
|
|
873
|
-
const format = `${_picocolors2.default.dim(
|
|
874
|
-
(/* @__PURE__ */ new Date()).toLocaleTimeString()
|
|
875
|
-
)} ${tag} ${msg}`;
|
|
876
|
-
console[method](format);
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
const logger = {
|
|
880
|
-
debug(msg, level = defaultLevel) {
|
|
881
|
-
output("debug", msg, level);
|
|
882
|
-
},
|
|
883
|
-
info(msg, level = defaultLevel) {
|
|
884
|
-
output("info", msg, level);
|
|
885
|
-
},
|
|
886
|
-
warn(msg, level = defaultLevel) {
|
|
887
|
-
output("warn", msg, level);
|
|
888
|
-
},
|
|
889
|
-
error(msg, level = defaultLevel) {
|
|
890
|
-
output("error", msg, level);
|
|
891
|
-
}
|
|
892
|
-
};
|
|
893
|
-
return logger;
|
|
894
|
-
}
|
|
895
|
-
|
|
896
896
|
|
|
897
897
|
|
|
898
898
|
|