@weapp-core/shared 3.0.2 → 3.0.3

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/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- import objectHash from "object-hash";
1
+ import { Dirent, Stats, closeSync, constants, existsSync, mkdirSync, openSync, promises, readFileSync, unlinkSync, utimesSync, watch, writeFileSync } from "node:fs";
2
+ import { FileHandle, OpenMode, PathLike } from "node:fs/promises";
2
3
  import set from "set-value";
3
4
 
4
- //#region ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.d.ts
5
+ //#region ../../node_modules/.pnpm/defu@6.1.6/node_modules/defu/dist/defu.d.mts
5
6
  type Input = Record<string | number | symbol, any>;
6
7
  type IgnoredInput = boolean | number | null | any[] | Record<never, any> | undefined;
7
8
  type Merger = <T extends Input, K extends keyof T>(object: T, key: keyof T, value: T[K], namespace: string) => any;
@@ -19,6 +20,95 @@ type MergeArrays<Destination, Source> = Destination extends Array<infer Destinat
19
20
  type Merge<Destination extends Input, Defaults extends Input> = Destination extends nullish ? Defaults extends nullish ? nullish : Defaults : Defaults extends nullish ? Destination : Destination extends Array<any> ? Defaults extends Array<any> ? MergeArrays<Destination, Defaults> : Destination | Defaults : Destination extends Function ? Destination | Defaults : Destination extends RegExp ? Destination | Defaults : Destination extends Promise<any> ? Destination | Defaults : Defaults extends Function ? Destination | Defaults : Defaults extends RegExp ? Destination | Defaults : Defaults extends Promise<any> ? Destination | Defaults : Destination extends Input ? Defaults extends Input ? MergeObjects<Destination, Defaults> : Destination | Defaults : Destination | Defaults;
20
21
  declare const defu: DefuInstance;
21
22
  //#endregion
23
+ //#region src/fs/index.d.ts
24
+ interface JsonReadOptions {
25
+ encoding?: BufferEncoding;
26
+ throws?: boolean;
27
+ }
28
+ interface JsonWriteOptions {
29
+ encoding?: BufferEncoding;
30
+ replacer?: ((this: any, key: string, value: any) => any) | Array<number | string> | null;
31
+ spaces?: number | string;
32
+ }
33
+ interface CopyOptions {
34
+ dereference?: boolean;
35
+ errorOnExist?: boolean;
36
+ filter?: (src: string, dest: string) => boolean | Promise<boolean>;
37
+ overwrite?: boolean;
38
+ preserveTimestamps?: boolean;
39
+ verbatimSymlinks?: boolean;
40
+ }
41
+ interface MoveOptions {
42
+ overwrite?: boolean;
43
+ }
44
+ declare function pathExists(target: string): Promise<boolean>;
45
+ declare function pathExistsSync(target: string): boolean;
46
+ declare function exists(target: string): Promise<boolean>;
47
+ declare function ensureDir(dir: string): Promise<void>;
48
+ declare function ensureFile(file: string): Promise<void>;
49
+ declare function remove(target: string): Promise<void>;
50
+ declare function ensureSymlink(target: string, file: string): Promise<void>;
51
+ declare function emptyDir(dir: string): Promise<void>;
52
+ declare function outputFile(file: string, data: string | NodeJS.ArrayBufferView, options?: BufferEncoding): Promise<void>;
53
+ declare function appendFile(file: string, data: string | NodeJS.ArrayBufferView, options?: BufferEncoding): Promise<void>;
54
+ declare function writeJson(file: string, data: unknown, options?: JsonWriteOptions): Promise<void>;
55
+ declare function writeJSON(file: string, data: unknown, options?: JsonWriteOptions): Promise<void>;
56
+ declare function outputJson(file: string, data: unknown, options?: JsonWriteOptions): Promise<void>;
57
+ declare function outputJSON(file: string, data: unknown, options?: JsonWriteOptions): Promise<void>;
58
+ declare function readJson<T = unknown>(file: string, options?: JsonReadOptions): Promise<T | null>;
59
+ declare function readJSON<T = unknown>(file: string, options?: JsonReadOptions): Promise<T | null>;
60
+ declare function readJsonSync<T = unknown>(file: string, options?: JsonReadOptions): T | null;
61
+ declare function copy(src: string, dest: string, options?: CopyOptions): Promise<void>;
62
+ declare function move(src: string, dest: string, options?: MoveOptions): Promise<void>;
63
+ declare function close$1(handle: number | FileHandle): Promise<void>;
64
+ declare const fs: {
65
+ Dirent: typeof Dirent;
66
+ Stats: typeof Stats;
67
+ access: typeof promises.access;
68
+ appendFile: typeof appendFile;
69
+ close: typeof close$1;
70
+ closeSync: typeof closeSync;
71
+ constants: typeof constants;
72
+ copy: typeof copy;
73
+ emptyDir: typeof emptyDir;
74
+ ensureDir: typeof ensureDir;
75
+ ensureFile: typeof ensureFile;
76
+ ensureSymlink: typeof ensureSymlink;
77
+ exists: typeof exists;
78
+ existsSync: typeof existsSync;
79
+ mkdir: typeof promises.mkdir;
80
+ mkdirSync: typeof mkdirSync;
81
+ mkdtemp: typeof promises.mkdtemp;
82
+ move: typeof move;
83
+ open: (file: PathLike, flags: OpenMode) => Promise<FileHandle>;
84
+ openSync: typeof openSync;
85
+ outputFile: typeof outputFile;
86
+ outputJSON: typeof outputJSON;
87
+ outputJson: typeof outputJson;
88
+ pathExists: typeof pathExists;
89
+ pathExistsSync: typeof pathExistsSync;
90
+ readFile: typeof promises.readFile;
91
+ readFileSync: typeof readFileSync;
92
+ readJSON: typeof readJSON;
93
+ readJson: typeof readJson;
94
+ readJsonSync: typeof readJsonSync;
95
+ readdir: typeof promises.readdir;
96
+ rm: typeof promises.rm;
97
+ remove: typeof remove;
98
+ stat: typeof promises.stat;
99
+ symlink: typeof promises.symlink;
100
+ unlinkSync: typeof unlinkSync;
101
+ utimes: typeof promises.utimes;
102
+ utimesSync: typeof utimesSync;
103
+ watch: typeof watch;
104
+ writeFile: typeof promises.writeFile;
105
+ writeFileSync: typeof writeFileSync;
106
+ writeJSON: typeof writeJSON;
107
+ writeJson: typeof writeJson;
108
+ };
109
+ type FsDirent = Dirent;
110
+ type FsStats = Stats;
111
+ //#endregion
22
112
  //#region ../../node_modules/.pnpm/get-value@4.0.1/node_modules/get-value/index.d.ts
23
113
  declare function get<T>(obj: T): T;
24
114
  declare function get(obj: object, key: string | string[], options?: get.Options): any;
@@ -89,15 +179,7 @@ declare function arrify<T>(val: T | readonly T[]): T[];
89
179
  /**
90
180
  * @description defu 合并策略:当目标/来源为数组时直接覆盖
91
181
  */
92
- declare const defuOverrideArray: <Source extends {
93
- [x: string]: any;
94
- [x: number]: any;
95
- [x: symbol]: any;
96
- }, Defaults extends Array<{
97
- [x: string]: any;
98
- [x: number]: any;
99
- [x: symbol]: any;
100
- } | (number | boolean | any[] | Record<never, any> | null | undefined)>>(source: Source, ...defaults: Defaults) => Defu<Source, Defaults>;
182
+ declare const defuOverrideArray: DefuFn;
101
183
  /**
102
184
  * @description 判断是否为非 null 的对象
103
185
  */
@@ -106,5 +188,9 @@ declare function isObject(x: unknown): x is Record<string | symbol | number, unk
106
188
  * @description 判断对象是否为空对象
107
189
  */
108
190
  declare function isEmptyObject(obj: unknown): boolean;
191
+ /**
192
+ * @description 生成稳定对象哈希,避免依赖已被禁用的 object-hash 包
193
+ */
194
+ declare function objectHash(value: unknown): string;
109
195
  //#endregion
110
- export { addExtension, arrify, defu, defuOverrideArray, escapeStringRegexp, get, isEmptyObject, isObject, objectHash, removeExtension, removeExtensionDeep, set };
196
+ export { CopyOptions, FsDirent, FsStats, JsonReadOptions, JsonWriteOptions, MoveOptions, addExtension, arrify, defu, defuOverrideArray, escapeStringRegexp, fs, get, isEmptyObject, isObject, objectHash, removeExtension, removeExtensionDeep, set };
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
- import { extname } from "node:path";
2
- import objectHash from "object-hash";
1
+ import { Buffer as Buffer$1 } from "node:buffer";
2
+ import { createHash } from "node:crypto";
3
+ import path, { extname } from "node:path";
4
+ import { Dirent, Stats, close, closeSync, constants, existsSync, mkdirSync, openSync, promises, readFileSync, unlinkSync, utimesSync, watch, writeFileSync } from "node:fs";
3
5
  //#region \0rolldown/runtime.js
4
6
  var __create = Object.create;
5
7
  var __defProp$1 = Object.defineProperty;
@@ -23,7 +25,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
25
  enumerable: true
24
26
  }) : target, mod));
25
27
  //#endregion
26
- //#region ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
28
+ //#region ../../node_modules/.pnpm/defu@6.1.6/node_modules/defu/dist/defu.mjs
27
29
  function isPlainObject(value) {
28
30
  if (value === null || typeof value !== "object") return false;
29
31
  const prototype = Object.getPrototypeOf(value);
@@ -34,8 +36,8 @@ function isPlainObject(value) {
34
36
  }
35
37
  function _defu(baseObject, defaults, namespace = ".", merger) {
36
38
  if (!isPlainObject(defaults)) return _defu(baseObject, {}, namespace, merger);
37
- const object = Object.assign({}, defaults);
38
- for (const key in baseObject) {
39
+ const object = { ...defaults };
40
+ for (const key of Object.keys(baseObject)) {
39
41
  if (key === "__proto__" || key === "constructor") continue;
40
42
  const value = baseObject[key];
41
43
  if (value === null || value === void 0) continue;
@@ -63,6 +65,198 @@ createDefu((object, key, currentValue) => {
63
65
  }
64
66
  });
65
67
  //#endregion
68
+ //#region src/fs/index.ts
69
+ function normalizeJsonEncoding(encoding) {
70
+ return encoding ?? "utf8";
71
+ }
72
+ function normalizeJsonText(source) {
73
+ return source.charCodeAt(0) === 65279 ? source.slice(1) : source;
74
+ }
75
+ function serializeJson(data, options) {
76
+ return JSON.stringify(data, options?.replacer ?? null, options?.spaces);
77
+ }
78
+ async function pathExists(target) {
79
+ try {
80
+ await promises.access(target);
81
+ return true;
82
+ } catch {
83
+ return false;
84
+ }
85
+ }
86
+ function pathExistsSync(target) {
87
+ try {
88
+ return existsSync(target);
89
+ } catch {
90
+ return false;
91
+ }
92
+ }
93
+ async function exists(target) {
94
+ return pathExists(target);
95
+ }
96
+ async function ensureDir(dir) {
97
+ await promises.mkdir(dir, { recursive: true });
98
+ }
99
+ async function ensureFile(file) {
100
+ await ensureDir(path.dirname(file));
101
+ await (await promises.open(file, "a")).close();
102
+ }
103
+ async function remove(target) {
104
+ await promises.rm(target, {
105
+ force: true,
106
+ recursive: true
107
+ });
108
+ }
109
+ async function ensureSymlink(target, file) {
110
+ await ensureDir(path.dirname(file));
111
+ try {
112
+ if (await promises.readlink(file) === target) return;
113
+ await remove(file);
114
+ } catch (error) {
115
+ if (error?.code !== "ENOENT") throw error;
116
+ }
117
+ await promises.symlink(target, file);
118
+ }
119
+ async function emptyDir(dir) {
120
+ await ensureDir(dir);
121
+ const entries = await promises.readdir(dir, { withFileTypes: true });
122
+ await Promise.all(entries.map((entry) => {
123
+ return remove(path.join(dir, entry.name));
124
+ }));
125
+ }
126
+ async function outputFile(file, data, options) {
127
+ await ensureDir(path.dirname(file));
128
+ await promises.writeFile(file, data, options);
129
+ }
130
+ async function appendFile(file, data, options) {
131
+ await ensureDir(path.dirname(file));
132
+ await promises.appendFile(file, data, options);
133
+ }
134
+ async function writeJson(file, data, options) {
135
+ const source = serializeJson(data, options);
136
+ await promises.writeFile(file, source, normalizeJsonEncoding(options?.encoding));
137
+ }
138
+ async function writeJSON(file, data, options) {
139
+ await writeJson(file, data, options);
140
+ }
141
+ async function outputJson(file, data, options) {
142
+ await ensureDir(path.dirname(file));
143
+ await writeJson(file, data, options);
144
+ }
145
+ async function outputJSON(file, data, options) {
146
+ await outputJson(file, data, options);
147
+ }
148
+ async function readJson(file, options) {
149
+ try {
150
+ const source = await promises.readFile(file, normalizeJsonEncoding(options?.encoding));
151
+ return JSON.parse(normalizeJsonText(source));
152
+ } catch (error) {
153
+ if (options?.throws === false && error instanceof SyntaxError) return null;
154
+ throw error;
155
+ }
156
+ }
157
+ async function readJSON(file, options) {
158
+ return readJson(file, options);
159
+ }
160
+ function readJsonSync(file, options) {
161
+ try {
162
+ const source = readFileSync(file, normalizeJsonEncoding(options?.encoding));
163
+ return JSON.parse(normalizeJsonText(source));
164
+ } catch (error) {
165
+ if (options?.throws === false && error instanceof SyntaxError) return null;
166
+ throw error;
167
+ }
168
+ }
169
+ async function copy(src, dest, options) {
170
+ await promises.cp(src, dest, {
171
+ ...typeof options?.dereference === "boolean" ? { dereference: options.dereference } : {},
172
+ ...typeof options?.errorOnExist === "boolean" ? { errorOnExist: options.errorOnExist } : {},
173
+ ...typeof options?.filter === "function" ? { filter: options.filter } : {},
174
+ ...typeof options?.preserveTimestamps === "boolean" ? { preserveTimestamps: options.preserveTimestamps } : {},
175
+ ...typeof options?.verbatimSymlinks === "boolean" ? { verbatimSymlinks: options.verbatimSymlinks } : {},
176
+ force: options?.overwrite ?? true,
177
+ recursive: true
178
+ });
179
+ }
180
+ async function move(src, dest, options) {
181
+ if (src === dest) return;
182
+ if (await pathExists(dest)) {
183
+ if (!options?.overwrite) {
184
+ const error = /* @__PURE__ */ new Error(`dest already exists: ${dest}`);
185
+ error.code = "EEXIST";
186
+ throw error;
187
+ }
188
+ await remove(dest);
189
+ }
190
+ await ensureDir(path.dirname(dest));
191
+ try {
192
+ await promises.rename(src, dest);
193
+ } catch (error) {
194
+ if (error?.code !== "EXDEV") throw error;
195
+ await copy(src, dest, { overwrite: options?.overwrite ?? true });
196
+ await remove(src);
197
+ }
198
+ }
199
+ async function close$1(handle) {
200
+ if (typeof handle === "number") {
201
+ await new Promise((resolve, reject) => {
202
+ close(handle, (error) => {
203
+ if (error) {
204
+ reject(error);
205
+ return;
206
+ }
207
+ resolve();
208
+ });
209
+ });
210
+ return;
211
+ }
212
+ await handle.close();
213
+ }
214
+ const fs = {
215
+ Dirent,
216
+ Stats,
217
+ access: promises.access,
218
+ appendFile,
219
+ close: close$1,
220
+ closeSync,
221
+ constants,
222
+ copy,
223
+ emptyDir,
224
+ ensureDir,
225
+ ensureFile,
226
+ ensureSymlink,
227
+ exists,
228
+ existsSync,
229
+ mkdir: promises.mkdir,
230
+ mkdirSync,
231
+ mkdtemp: promises.mkdtemp,
232
+ move,
233
+ open: (file, flags) => promises.open(file, flags),
234
+ openSync,
235
+ outputFile,
236
+ outputJSON,
237
+ outputJson,
238
+ pathExists,
239
+ pathExistsSync,
240
+ readFile: promises.readFile,
241
+ readFileSync,
242
+ readJSON,
243
+ readJson,
244
+ readJsonSync,
245
+ readdir: promises.readdir,
246
+ rm: promises.rm,
247
+ remove,
248
+ stat: promises.stat,
249
+ symlink: promises.symlink,
250
+ unlinkSync,
251
+ utimes: promises.utimes,
252
+ utimesSync,
253
+ watch,
254
+ writeFile: promises.writeFile,
255
+ writeFileSync,
256
+ writeJSON,
257
+ writeJson
258
+ };
259
+ //#endregion
66
260
  //#region ../../node_modules/.pnpm/get-value@4.0.1/node_modules/get-value/dist/index.mjs
67
261
  var __defProp = Object.defineProperty;
68
262
  var __name = (target, value) => __defProp(target, "name", {
@@ -291,6 +485,34 @@ const SPECIAL_CHARS_RE = /[|\\{}()[\]^$+*?.]/g;
291
485
  const HYPHEN_RE = /-/g;
292
486
  const LAST_EXT_RE = /\.[^/.]+$/;
293
487
  const ALL_EXT_RE = /(\.[^/.]+)+$/;
488
+ function stableSerialize(value, seen = /* @__PURE__ */ new Map()) {
489
+ if (value === null) return "null";
490
+ switch (typeof value) {
491
+ case "undefined": return "undefined";
492
+ case "string": return JSON.stringify(value);
493
+ case "number":
494
+ if (Number.isNaN(value)) return "number:NaN";
495
+ if (!Number.isFinite(value)) return `number:${value > 0 ? "Infinity" : "-Infinity"}`;
496
+ if (Object.is(value, -0)) return "number:-0";
497
+ return `number:${value}`;
498
+ case "bigint": return `bigint:${value.toString()}`;
499
+ case "boolean": return `boolean:${value}`;
500
+ case "symbol": return `symbol:${String(value)}`;
501
+ case "function": return `function:${value.toString()}`;
502
+ }
503
+ if (value instanceof Date) return `date:${value.toISOString()}`;
504
+ if (value instanceof RegExp) return `regexp:${value.toString()}`;
505
+ if (ArrayBuffer.isView(value)) return `${value.constructor.name}:${Buffer$1.from(value.buffer, value.byteOffset, value.byteLength).toString("hex")}`;
506
+ if (value instanceof ArrayBuffer) return `ArrayBuffer:${Buffer$1.from(value).toString("hex")}`;
507
+ if (seen.has(value)) return `circular:${seen.get(value)}`;
508
+ seen.set(value, seen.size);
509
+ if (Array.isArray(value)) return `[${value.map((item) => stableSerialize(item, seen)).join(",")}]`;
510
+ if (value instanceof Set) return `Set(${Array.from(value, (item) => stableSerialize(item, seen)).join(",")})`;
511
+ if (value instanceof Map) return `Map(${Array.from(value.entries(), ([key, item]) => [stableSerialize(key, seen), stableSerialize(item, seen)]).sort(([left], [right]) => left.localeCompare(right)).map(([key, item]) => `${key}=>${item}`).join(",")})`;
512
+ return `{${Reflect.ownKeys(value).sort((left, right) => String(left).localeCompare(String(right))).map((key) => {
513
+ return `${stableSerialize(key, seen)}:${stableSerialize(value[key], seen)}`;
514
+ }).join(",")}}`;
515
+ }
294
516
  /**
295
517
  * @description 转义字符串中的正则特殊字符
296
518
  */
@@ -350,6 +572,12 @@ function isEmptyObject(obj) {
350
572
  }
351
573
  return false;
352
574
  }
575
+ /**
576
+ * @description 生成稳定对象哈希,避免依赖已被禁用的 object-hash 包
577
+ */
578
+ function objectHash(value) {
579
+ return createHash("sha256").update(stableSerialize(value)).digest("hex");
580
+ }
353
581
  //#endregion
354
582
  var set = import_set_value.default;
355
- export { addExtension, arrify, defu, defuOverrideArray, escapeStringRegexp, index_default as get, isEmptyObject, isObject, objectHash, removeExtension, removeExtensionDeep, set };
583
+ export { addExtension, arrify, defu, defuOverrideArray, escapeStringRegexp, fs, index_default as get, isEmptyObject, isObject, objectHash, removeExtension, removeExtensionDeep, set };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-core/shared",
3
3
  "type": "module",
4
- "version": "3.0.2",
4
+ "version": "3.0.3",
5
5
  "description": "@weapp-core/shared",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -41,9 +41,7 @@
41
41
  "engines": {
42
42
  "node": "^20.19.0 || >=22.12.0"
43
43
  },
44
- "dependencies": {
45
- "object-hash": "^3.0.0"
46
- },
44
+ "dependencies": {},
47
45
  "devDependencies": {},
48
46
  "scripts": {
49
47
  "dev": "tsdown -w --sourcemap",