vite-plugin-mock-dev-server 1.9.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{helper-DhHU-YoO.js → helper-BbR8Si2U.js} +85 -61
- package/dist/helper.d.ts +3 -3
- package/dist/helper.js +1 -2
- package/dist/{helper-r_bW1AY8.d.ts → index-CJc2Oax2.d.ts} +57 -41
- package/dist/index.d.ts +4 -10
- package/dist/index.js +442 -324
- package/dist/{server-C-u7jwot.js → server-D-sv_WW9.js} +460 -391
- package/dist/{server-DgmHgcvl.d.ts → server-tcT2vAaP.d.ts} +33 -38
- package/dist/server.d.ts +3 -3
- package/dist/server.js +2 -3
- package/dist/{types-BbbTJG0b.d.ts → types-BtCJqeLH.d.ts} +12 -4
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +15 -21
- package/dist/dist-CAA1v47s.js +0 -204
- package/dist/dist-DrfpZ4UT.cjs +0 -323
- package/dist/helper-CCVedLL0.cjs +0 -159
- package/dist/helper-iVHsUTZ6.d.cts +0 -110
- package/dist/helper.cjs +0 -7
- package/dist/helper.d.cts +0 -3
- package/dist/index.cjs +0 -730
- package/dist/index.d.cts +0 -15
- package/dist/server-B5Ua2cmP.d.cts +0 -93
- package/dist/server-BwOfV_62.cjs +0 -810
- package/dist/server.cjs +0 -10
- package/dist/server.d.cts +0 -3
- package/dist/types-DpbHkRjL.d.cts +0 -573
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import { LogLevel, MockHttpItem, MockOptions, MockServerPluginOptions, MockWebsocketItem, ServerBuildOption } from "./types-
|
|
2
|
-
import { CorsOptions } from "cors";
|
|
1
|
+
import { LogLevel, MockHttpItem, MockOptions, MockServerPluginOptions, MockWebsocketItem, ServerBuildOption } from "./types-BtCJqeLH.js";
|
|
3
2
|
import EventEmitter from "node:events";
|
|
4
|
-
import {
|
|
3
|
+
import { Matcher } from "picomatch";
|
|
4
|
+
import { CorsOptions } from "cors";
|
|
5
5
|
import { Server } from "node:http";
|
|
6
6
|
import { Alias, Connect } from "vite";
|
|
7
7
|
import { Http2SecureServer } from "node:http2";
|
|
8
8
|
|
|
9
|
-
//#region src/
|
|
9
|
+
//#region src/compiler/types.d.ts
|
|
10
|
+
|
|
11
|
+
type MockRawData = MockOptions | MockHttpItem | MockWebsocketItem | Record<string, MockOptions | MockHttpItem | MockWebsocketItem>;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/compiler/processData.d.ts
|
|
14
|
+
declare function processRawData(raw: MockRawData, __filepath__: string): MockOptions | MockHttpItem | MockWebsocketItem;
|
|
15
|
+
declare function processMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
16
|
+
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/utils/logger.d.ts
|
|
10
19
|
interface Logger {
|
|
11
20
|
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
12
21
|
info: (msg: string, level?: boolean | LogLevel) => void;
|
|
@@ -16,7 +25,7 @@ interface Logger {
|
|
|
16
25
|
declare const logLevels: Record<LogLevel, number>;
|
|
17
26
|
declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
|
|
18
27
|
//#endregion
|
|
19
|
-
//#region src/
|
|
28
|
+
//#region src/options.d.ts
|
|
20
29
|
type ResolvedMockServerPluginOptions = Required<Omit<MockServerPluginOptions, "build" | "cors" | "wsPrefix" | "prefix">> & {
|
|
21
30
|
context: string;
|
|
22
31
|
logger: Logger;
|
|
@@ -28,66 +37,52 @@ type ResolvedMockServerPluginOptions = Required<Omit<MockServerPluginOptions, "b
|
|
|
28
37
|
cors: false | CorsOptions;
|
|
29
38
|
};
|
|
30
39
|
//#endregion
|
|
31
|
-
//#region src/
|
|
40
|
+
//#region src/compiler/compiler.d.ts
|
|
32
41
|
/**
|
|
33
|
-
*
|
|
42
|
+
* Mock 文件加载编译,并转换为 Mock 数据
|
|
34
43
|
*/
|
|
35
|
-
declare class
|
|
44
|
+
declare class Compiler extends EventEmitter {
|
|
36
45
|
options: ResolvedMockServerPluginOptions;
|
|
37
|
-
moduleCache
|
|
38
|
-
moduleDeps
|
|
46
|
+
private moduleCache;
|
|
47
|
+
private moduleDeps;
|
|
39
48
|
cwd: string;
|
|
40
|
-
mockWatcher
|
|
41
|
-
depsWatcher
|
|
42
|
-
|
|
49
|
+
private mockWatcher;
|
|
50
|
+
private depsWatcher;
|
|
51
|
+
private isESM;
|
|
43
52
|
private _mockData;
|
|
44
53
|
constructor(options: ResolvedMockServerPluginOptions);
|
|
45
54
|
get mockData(): Record<string, MockOptions>;
|
|
46
|
-
run(): void;
|
|
47
|
-
private watchMockEntry;
|
|
48
|
-
/**
|
|
49
|
-
* 监听 mock文件依赖的本地文件变动,
|
|
50
|
-
* mock依赖文件更新,mock文件也一并更新
|
|
51
|
-
*/
|
|
52
|
-
private watchDeps;
|
|
55
|
+
run(watch?: boolean): void;
|
|
53
56
|
close(): void;
|
|
54
|
-
private
|
|
57
|
+
private load;
|
|
58
|
+
private updateMockData;
|
|
55
59
|
private updateModuleDeps;
|
|
56
|
-
|
|
60
|
+
watchMockEntry(isMatch: Matcher): void;
|
|
61
|
+
watchDeps(): void;
|
|
57
62
|
}
|
|
58
63
|
//#endregion
|
|
59
|
-
//#region src/core/
|
|
60
|
-
interface
|
|
61
|
-
formidableOptions: MockServerPluginOptions["formidableOptions"];
|
|
62
|
-
cookiesOptions: MockServerPluginOptions["cookiesOptions"];
|
|
63
|
-
bodyParserOptions: MockServerPluginOptions["bodyParserOptions"];
|
|
64
|
+
//#region src/core/mockMiddleware.d.ts
|
|
65
|
+
interface CreateMockMiddlewareOptions extends Pick<MockServerPluginOptions, "formidableOptions" | "cookiesOptions" | "bodyParserOptions" | "priority"> {
|
|
64
66
|
proxies: string[];
|
|
65
67
|
logger: Logger;
|
|
66
|
-
priority: MockServerPluginOptions["priority"];
|
|
67
68
|
}
|
|
68
|
-
declare function
|
|
69
|
+
declare function createMockMiddleware(compiler: Compiler, {
|
|
69
70
|
formidableOptions,
|
|
70
71
|
bodyParserOptions,
|
|
71
72
|
proxies,
|
|
72
73
|
cookiesOptions,
|
|
73
74
|
logger,
|
|
74
75
|
priority
|
|
75
|
-
}:
|
|
76
|
-
//#endregion
|
|
77
|
-
//#region src/core/transform.d.ts
|
|
78
|
-
type MockRawData = MockOptions | MockHttpItem | MockWebsocketItem | Record<string, MockOptions | MockHttpItem | MockWebsocketItem>;
|
|
79
|
-
declare function transformRawData(raw: MockRawData, __filepath__: string): MockOptions | MockHttpItem | MockWebsocketItem;
|
|
80
|
-
declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
81
|
-
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
76
|
+
}: CreateMockMiddlewareOptions): Connect.NextHandleFunction;
|
|
82
77
|
//#endregion
|
|
83
78
|
//#region src/core/ws.d.ts
|
|
84
79
|
/**
|
|
85
80
|
* mock websocket
|
|
86
81
|
*/
|
|
87
|
-
declare function mockWebSocket(compiler:
|
|
82
|
+
declare function mockWebSocket(compiler: Compiler, server: Server | Http2SecureServer | null, {
|
|
88
83
|
wsProxies: proxies,
|
|
89
84
|
cookiesOptions,
|
|
90
85
|
logger
|
|
91
86
|
}: ResolvedMockServerPluginOptions): void;
|
|
92
87
|
//#endregion
|
|
93
|
-
export {
|
|
88
|
+
export { CreateMockMiddlewareOptions, Logger, createLogger, createMockMiddleware, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./types-
|
|
2
|
-
import {
|
|
3
|
-
export {
|
|
1
|
+
import "./types-BtCJqeLH.js";
|
|
2
|
+
import { CreateMockMiddlewareOptions, Logger, createLogger, createMockMiddleware, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator } from "./server-tcT2vAaP.js";
|
|
3
|
+
export { CreateMockMiddlewareOptions, Logger, createLogger, createMockMiddleware, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
package/dist/server.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import { baseMiddleware, createLogger, logLevels, mockWebSocket, sortByValidator, transformMockData, transformRawData } from "./server-C-u7jwot.js";
|
|
1
|
+
import { createLogger, createMockMiddleware, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator } from "./server-D-sv_WW9.js";
|
|
3
2
|
|
|
4
|
-
export {
|
|
3
|
+
export { createLogger, createMockMiddleware, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Readable } from "node:stream";
|
|
2
1
|
import { CorsOptions } from "cors";
|
|
3
|
-
import { Buffer } from "node:buffer";
|
|
4
|
-
import Cookies from "cookies";
|
|
5
2
|
import { Options } from "co-body";
|
|
6
3
|
import formidable from "formidable";
|
|
4
|
+
import Cookies from "cookies";
|
|
5
|
+
import { Buffer } from "node:buffer";
|
|
7
6
|
import { WebSocketServer } from "ws";
|
|
7
|
+
import { Readable } from "node:stream";
|
|
8
8
|
import http from "node:http";
|
|
9
9
|
import { Connect } from "vite";
|
|
10
10
|
|
|
@@ -49,6 +49,14 @@ interface MockServerPluginOptions {
|
|
|
49
49
|
*/
|
|
50
50
|
cwd?: string;
|
|
51
51
|
/**
|
|
52
|
+
* The directory to store mock files
|
|
53
|
+
*
|
|
54
|
+
* 存储 mock 文件的目录
|
|
55
|
+
*
|
|
56
|
+
* @default 'mock'
|
|
57
|
+
*/
|
|
58
|
+
dir?: string;
|
|
59
|
+
/**
|
|
52
60
|
* glob string matching mock includes files
|
|
53
61
|
*
|
|
54
62
|
* glob 字符串匹配 mock 包含的文件
|
|
@@ -570,4 +578,4 @@ type FormidableFile = formidable.File | formidable.File[];
|
|
|
570
578
|
type LogType = "info" | "warn" | "error" | "debug";
|
|
571
579
|
type LogLevel = LogType | "silent";
|
|
572
580
|
//#endregion
|
|
573
|
-
export { FormidableFile, LogLevel, MockHttpItem, MockOptions, MockRequest, MockServerPluginOptions, MockWebsocketItem, ServerBuildOption };
|
|
581
|
+
export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext } from "./types-BtCJqeLH.js";
|
|
2
|
+
export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-dev-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
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",
|
|
@@ -22,30 +22,18 @@
|
|
|
22
22
|
"import": {
|
|
23
23
|
"types": "./dist/index.d.ts",
|
|
24
24
|
"default": "./dist/index.js"
|
|
25
|
-
},
|
|
26
|
-
"require": {
|
|
27
|
-
"types": "./dist/index.d.cts",
|
|
28
|
-
"default": "./dist/index.cjs"
|
|
29
25
|
}
|
|
30
26
|
},
|
|
31
27
|
"./helper": {
|
|
32
28
|
"import": {
|
|
33
29
|
"types": "./dist/helper.d.ts",
|
|
34
30
|
"default": "./dist/helper.js"
|
|
35
|
-
},
|
|
36
|
-
"require": {
|
|
37
|
-
"types": "./dist/helper.d.cts",
|
|
38
|
-
"default": "./dist/helper.cjs"
|
|
39
31
|
}
|
|
40
32
|
},
|
|
41
33
|
"./server": {
|
|
42
34
|
"import": {
|
|
43
35
|
"types": "./dist/server.d.ts",
|
|
44
36
|
"default": "./dist/server.js"
|
|
45
|
-
},
|
|
46
|
-
"require": {
|
|
47
|
-
"types": "./dist/server.d.cts",
|
|
48
|
-
"default": "./dist/server.cjs"
|
|
49
37
|
}
|
|
50
38
|
},
|
|
51
39
|
"./package.json": "./package.json"
|
|
@@ -60,34 +48,40 @@
|
|
|
60
48
|
},
|
|
61
49
|
"peerDependencies": {
|
|
62
50
|
"esbuild": ">=0.21.0",
|
|
51
|
+
"rolldown": ">=1.0.0-beta.34",
|
|
63
52
|
"vite": ">=4.0.0"
|
|
64
53
|
},
|
|
65
54
|
"peerDependenciesMeta": {
|
|
66
55
|
"esbuild": {
|
|
67
56
|
"optional": true
|
|
57
|
+
},
|
|
58
|
+
"rolldown": {
|
|
59
|
+
"optional": true
|
|
68
60
|
}
|
|
69
61
|
},
|
|
70
62
|
"dependencies": {
|
|
71
|
-
"@
|
|
72
|
-
"
|
|
63
|
+
"@pengzhanbo/utils": "^2.1.0",
|
|
64
|
+
"ansis": "^4.1.0",
|
|
65
|
+
"chokidar": "^4.0.3",
|
|
73
66
|
"co-body": "^6.2.0",
|
|
74
67
|
"cookies": "^0.9.1",
|
|
75
68
|
"cors": "^2.8.5",
|
|
76
69
|
"debug": "^4.4.1",
|
|
77
|
-
"fast-glob": "^3.3.3",
|
|
78
70
|
"formidable": "3.5.4",
|
|
79
71
|
"http-status": "^2.1.0",
|
|
80
72
|
"is-core-module": "^2.16.1",
|
|
81
73
|
"json5": "^2.2.3",
|
|
74
|
+
"local-pkg": "^1.1.2",
|
|
82
75
|
"mime-types": "^3.0.1",
|
|
83
|
-
"path-to-regexp": "
|
|
84
|
-
"
|
|
76
|
+
"path-to-regexp": "^8.2.0",
|
|
77
|
+
"picomatch": "^4.0.3",
|
|
78
|
+
"tinyglobby": "^0.2.14",
|
|
85
79
|
"ws": "^8.18.3"
|
|
86
80
|
},
|
|
87
81
|
"devDependencies": {
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"vite": "
|
|
82
|
+
"esbuild": "^0.25.9",
|
|
83
|
+
"rolldown": "^1.0.0-beta.34",
|
|
84
|
+
"vite": "npm:rolldown-vite@latest"
|
|
91
85
|
},
|
|
92
86
|
"publishConfig": {
|
|
93
87
|
"access": "public"
|
package/dist/dist-CAA1v47s.js
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
//#region ../node_modules/.pnpm/@pengzhanbo+utils@2.1.0/node_modules/@pengzhanbo/utils/dist/index.mjs
|
|
2
|
-
function toString(s) {
|
|
3
|
-
return Object.prototype.toString.call(s);
|
|
4
|
-
}
|
|
5
|
-
function getTypeName(s) {
|
|
6
|
-
return s === null ? "null" : typeof s === "object" || typeof s === "function" ? toString(s).slice(8, -1).toLowerCase() : typeof s;
|
|
7
|
-
}
|
|
8
|
-
function isPrimitive(v) {
|
|
9
|
-
return v === null || typeof v !== "object" && typeof v !== "function";
|
|
10
|
-
}
|
|
11
|
-
function isBoolean(v) {
|
|
12
|
-
return typeof v === "boolean";
|
|
13
|
-
}
|
|
14
|
-
function isFunction(v) {
|
|
15
|
-
return typeof v === "function";
|
|
16
|
-
}
|
|
17
|
-
function isString(v) {
|
|
18
|
-
return typeof v === "string";
|
|
19
|
-
}
|
|
20
|
-
function isPlainObject(v) {
|
|
21
|
-
return getTypeName(v) === "object";
|
|
22
|
-
}
|
|
23
|
-
function isArray(v) {
|
|
24
|
-
return Array.isArray(v);
|
|
25
|
-
}
|
|
26
|
-
function isEmptyObject(v) {
|
|
27
|
-
if (!isPlainObject(v)) return false;
|
|
28
|
-
for (const _ in v) return false;
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
function isTypedArray(v) {
|
|
32
|
-
return ArrayBuffer.isView(v) && !(v instanceof DataView);
|
|
33
|
-
}
|
|
34
|
-
function toArray(v) {
|
|
35
|
-
if (v === null || v === void 0) return [];
|
|
36
|
-
if (isArray(v)) return v;
|
|
37
|
-
return [v];
|
|
38
|
-
}
|
|
39
|
-
function uniq(v) {
|
|
40
|
-
return Array.from(new Set(v));
|
|
41
|
-
}
|
|
42
|
-
function sortBy(array, cb) {
|
|
43
|
-
if (array.length === 0) return [];
|
|
44
|
-
return array.sort((a, b) => {
|
|
45
|
-
const s1 = cb(a);
|
|
46
|
-
const s2 = cb(b);
|
|
47
|
-
return s1 > s2 ? 1 : s2 > s1 ? -1 : 0;
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
function hasOwn(obj, key) {
|
|
51
|
-
return obj === null ? false : Object.prototype.hasOwnProperty.call(obj, key);
|
|
52
|
-
}
|
|
53
|
-
function deepCloneImpl(valueToClone, objectToClone, stack = /* @__PURE__ */ new Map()) {
|
|
54
|
-
if (isPrimitive(valueToClone)) return valueToClone;
|
|
55
|
-
/* istanbul ignore if -- @preserve */
|
|
56
|
-
if (stack.has(valueToClone)) return stack.get(valueToClone);
|
|
57
|
-
if (Array.isArray(valueToClone)) {
|
|
58
|
-
const result = Array.from({ length: valueToClone.length });
|
|
59
|
-
stack.set(valueToClone, result);
|
|
60
|
-
for (let i = 0; i < valueToClone.length; i++) result[i] = deepCloneImpl(valueToClone[i], objectToClone, stack);
|
|
61
|
-
if (Object.hasOwn(valueToClone, "index")) result.index = valueToClone.index;
|
|
62
|
-
if (Object.hasOwn(valueToClone, "input")) result.input = valueToClone.input;
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
if (valueToClone instanceof Date) return new Date(valueToClone.getTime());
|
|
66
|
-
if (valueToClone instanceof RegExp) {
|
|
67
|
-
const result = new RegExp(valueToClone.source, valueToClone.flags);
|
|
68
|
-
result.lastIndex = valueToClone.lastIndex;
|
|
69
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
if (valueToClone instanceof Map) {
|
|
72
|
-
const result = /* @__PURE__ */ new Map();
|
|
73
|
-
stack.set(valueToClone, result);
|
|
74
|
-
for (const [key, value] of valueToClone) result.set(key, deepCloneImpl(value, objectToClone, stack));
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
if (valueToClone instanceof Set) {
|
|
78
|
-
const result = /* @__PURE__ */ new Set();
|
|
79
|
-
stack.set(valueToClone, result);
|
|
80
|
-
for (const value of valueToClone) result.add(deepCloneImpl(value, objectToClone, stack));
|
|
81
|
-
return result;
|
|
82
|
-
}
|
|
83
|
-
if (typeof Buffer !== "undefined" && Buffer.isBuffer(valueToClone)) return valueToClone.subarray();
|
|
84
|
-
if (isTypedArray(valueToClone)) {
|
|
85
|
-
const result = new (Object.getPrototypeOf(valueToClone)).constructor(valueToClone.length);
|
|
86
|
-
stack.set(valueToClone, result);
|
|
87
|
-
for (let i = 0; i < valueToClone.length; i++) result[i] = deepCloneImpl(valueToClone[i], objectToClone, stack);
|
|
88
|
-
return result;
|
|
89
|
-
}
|
|
90
|
-
if (valueToClone instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && valueToClone instanceof SharedArrayBuffer) return valueToClone.slice(0);
|
|
91
|
-
if (valueToClone instanceof DataView) {
|
|
92
|
-
const result = new DataView(valueToClone.buffer.slice(0), valueToClone.byteOffset, valueToClone.byteLength);
|
|
93
|
-
stack.set(valueToClone, result);
|
|
94
|
-
copyProperties(result, valueToClone, objectToClone, stack);
|
|
95
|
-
return result;
|
|
96
|
-
}
|
|
97
|
-
if (typeof File !== "undefined" && valueToClone instanceof File) {
|
|
98
|
-
const result = new File([valueToClone], valueToClone.name, { type: valueToClone.type });
|
|
99
|
-
stack.set(valueToClone, result);
|
|
100
|
-
copyProperties(result, valueToClone, objectToClone, stack);
|
|
101
|
-
return result;
|
|
102
|
-
}
|
|
103
|
-
if (valueToClone instanceof Blob) {
|
|
104
|
-
const result = new Blob([valueToClone], { type: valueToClone.type });
|
|
105
|
-
stack.set(valueToClone, result);
|
|
106
|
-
copyProperties(result, valueToClone, objectToClone, stack);
|
|
107
|
-
return result;
|
|
108
|
-
}
|
|
109
|
-
if (valueToClone instanceof Error) {
|
|
110
|
-
const result = new valueToClone.constructor(valueToClone.message, { cause: valueToClone.cause });
|
|
111
|
-
stack.set(valueToClone, result);
|
|
112
|
-
if (hasOwn(valueToClone, "name")) result.name = valueToClone.name;
|
|
113
|
-
result.stack = valueToClone.stack;
|
|
114
|
-
copyProperties(result, valueToClone, objectToClone, stack);
|
|
115
|
-
return result;
|
|
116
|
-
}
|
|
117
|
-
/* istanbul ignore if -- @preserve */
|
|
118
|
-
if (typeof valueToClone === "object" && valueToClone !== null) {
|
|
119
|
-
const result = Object.create(Object.getPrototypeOf(valueToClone));
|
|
120
|
-
stack.set(valueToClone, result);
|
|
121
|
-
copyProperties(result, valueToClone, objectToClone, stack);
|
|
122
|
-
return result;
|
|
123
|
-
}
|
|
124
|
-
/* istanbul ignore next -- @preserve */
|
|
125
|
-
return valueToClone;
|
|
126
|
-
}
|
|
127
|
-
function copyProperties(target, source, objectToClone = target, stack) {
|
|
128
|
-
const keys = [...Object.keys(source), ...getSymbols(source)];
|
|
129
|
-
for (let i = 0; i < keys.length; i++) {
|
|
130
|
-
const key = keys[i];
|
|
131
|
-
const descriptor = Object.getOwnPropertyDescriptor(target, key);
|
|
132
|
-
if (descriptor == null || descriptor.writable) target[key] = deepCloneImpl(source[key], objectToClone, stack);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
function getSymbols(object) {
|
|
136
|
-
return Object.getOwnPropertySymbols(object).filter((symbol) => Object.prototype.propertyIsEnumerable.call(object, symbol));
|
|
137
|
-
}
|
|
138
|
-
function deepClone(source) {
|
|
139
|
-
return deepCloneImpl(source, source, /* @__PURE__ */ new Map());
|
|
140
|
-
}
|
|
141
|
-
function deepEqual(v1, v2) {
|
|
142
|
-
const type1 = getTypeName(v1);
|
|
143
|
-
const type2 = getTypeName(v2);
|
|
144
|
-
if (type1 !== type2) return false;
|
|
145
|
-
if (type1 === "array") {
|
|
146
|
-
if (v1.length !== v2.length) return false;
|
|
147
|
-
return v1.every((item, index) => deepEqual(item, v2[index]));
|
|
148
|
-
}
|
|
149
|
-
if (type1 === "object") {
|
|
150
|
-
const keys1 = Object.keys(v1);
|
|
151
|
-
if (keys1.length !== Object.keys(v2).length) return false;
|
|
152
|
-
return keys1.every((key) => deepEqual(v1[key], v2[key]));
|
|
153
|
-
}
|
|
154
|
-
return Object.is(v1, v2);
|
|
155
|
-
}
|
|
156
|
-
function random(...args) {
|
|
157
|
-
let min, max, float;
|
|
158
|
-
if (args.length === 1) {
|
|
159
|
-
min = 0;
|
|
160
|
-
max = args[0];
|
|
161
|
-
float = false;
|
|
162
|
-
} else if (typeof args[1] === "number") {
|
|
163
|
-
min = args[0];
|
|
164
|
-
max = args[1];
|
|
165
|
-
float = !!args[2];
|
|
166
|
-
} else {
|
|
167
|
-
min = 0;
|
|
168
|
-
max = args[0];
|
|
169
|
-
float = !!args[1];
|
|
170
|
-
}
|
|
171
|
-
const num = Math.random() * (max - min) + min;
|
|
172
|
-
return float ? num : Math.floor(num);
|
|
173
|
-
}
|
|
174
|
-
async function sleep(ms, callback) {
|
|
175
|
-
return new Promise((resolve) => setTimeout(async () => {
|
|
176
|
-
await callback?.();
|
|
177
|
-
resolve();
|
|
178
|
-
}, ms));
|
|
179
|
-
}
|
|
180
|
-
function promiseParallel(promises, concurrency = Number.POSITIVE_INFINITY) {
|
|
181
|
-
promises = Array.from(promises);
|
|
182
|
-
let current = 0;
|
|
183
|
-
const result = [];
|
|
184
|
-
let resolvedCount = 0;
|
|
185
|
-
const len = promises.length;
|
|
186
|
-
return new Promise((resolve, reject) => {
|
|
187
|
-
function next() {
|
|
188
|
-
const index = current++;
|
|
189
|
-
const promise = promises[index];
|
|
190
|
-
Promise.resolve(isFunction(promise) ? promise() : promise).then((res) => {
|
|
191
|
-
result[index] = res;
|
|
192
|
-
if (++resolvedCount === len) resolve(result);
|
|
193
|
-
if (current < len) next();
|
|
194
|
-
}).catch((reason) => reject(reason));
|
|
195
|
-
}
|
|
196
|
-
for (let i = 0; i < concurrency && i < len; i++) next();
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
function timestamp() {
|
|
200
|
-
return +Date.now();
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
//#endregion
|
|
204
|
-
export { deepClone, deepEqual, isArray, isBoolean, isEmptyObject, isFunction, isPlainObject, isString, promiseParallel, random, sleep, sortBy, timestamp, toArray, uniq };
|