@weapp-core/shared 3.0.3 → 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/fs/index.d.ts +93 -0
- package/dist/fs/index.js +198 -0
- package/dist/index.d.ts +306 -92
- package/dist/index.js +488 -215
- package/dist/node.d.ts +9 -0
- package/dist/node.js +61 -0
- package/package.json +17 -1
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CopyOptions, FsDirent, FsStats, JsonReadOptions, JsonWriteOptions, MoveOptions, fs } from "./fs/index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/node.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description 生成稳定对象哈希,避免依赖已被禁用的 object-hash 包。
|
|
6
|
+
*/
|
|
7
|
+
declare function objectHash(value: unknown): string;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { type CopyOptions, type FsDirent, type FsStats, type JsonReadOptions, type JsonWriteOptions, type MoveOptions, fs, objectHash };
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { fs } from "./fs/index.js";
|
|
2
|
+
//#region src/node.ts
|
|
3
|
+
const UTF8_ENCODER = new TextEncoder();
|
|
4
|
+
const HASH_SEEDS = [
|
|
5
|
+
0,
|
|
6
|
+
1,
|
|
7
|
+
2,
|
|
8
|
+
3
|
|
9
|
+
];
|
|
10
|
+
function bytesToHex(bytes) {
|
|
11
|
+
let result = "";
|
|
12
|
+
for (const byte of bytes) result += byte.toString(16).padStart(2, "0");
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
function stableSerialize(value, seen = /* @__PURE__ */ new Map()) {
|
|
16
|
+
if (value === null) return "null";
|
|
17
|
+
switch (typeof value) {
|
|
18
|
+
case "undefined": return "undefined";
|
|
19
|
+
case "string": return JSON.stringify(value);
|
|
20
|
+
case "number":
|
|
21
|
+
if (Number.isNaN(value)) return "number:NaN";
|
|
22
|
+
if (!Number.isFinite(value)) return `number:${value > 0 ? "Infinity" : "-Infinity"}`;
|
|
23
|
+
if (Object.is(value, -0)) return "number:-0";
|
|
24
|
+
return `number:${value}`;
|
|
25
|
+
case "bigint": return `bigint:${value.toString()}`;
|
|
26
|
+
case "boolean": return `boolean:${value}`;
|
|
27
|
+
case "symbol": return `symbol:${String(value)}`;
|
|
28
|
+
case "function": return `function:${value.toString()}`;
|
|
29
|
+
}
|
|
30
|
+
if (value instanceof Date) return `date:${value.toISOString()}`;
|
|
31
|
+
if (value instanceof RegExp) return `regexp:${value.toString()}`;
|
|
32
|
+
if (ArrayBuffer.isView(value)) return `${value.constructor.name}:${bytesToHex(new Uint8Array(value.buffer, value.byteOffset, value.byteLength))}`;
|
|
33
|
+
if (value instanceof ArrayBuffer) return `ArrayBuffer:${bytesToHex(new Uint8Array(value))}`;
|
|
34
|
+
if (seen.has(value)) return `circular:${seen.get(value)}`;
|
|
35
|
+
seen.set(value, seen.size);
|
|
36
|
+
if (Array.isArray(value)) return `[${value.map((item) => stableSerialize(item, seen)).join(",")}]`;
|
|
37
|
+
if (value instanceof Set) return `Set(${Array.from(value, (item) => stableSerialize(item, seen)).join(",")})`;
|
|
38
|
+
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(",")})`;
|
|
39
|
+
return `{${Reflect.ownKeys(value).sort((left, right) => String(left).localeCompare(String(right))).map((key) => {
|
|
40
|
+
return `${stableSerialize(key, seen)}:${stableSerialize(value[key], seen)}`;
|
|
41
|
+
}).join(",")}}`;
|
|
42
|
+
}
|
|
43
|
+
function xmur3(bytes, seed) {
|
|
44
|
+
let hash = (1779033703 ^ bytes.length ^ seed) >>> 0;
|
|
45
|
+
for (const byte of bytes) {
|
|
46
|
+
hash = Math.imul(hash ^ byte, 3432918353);
|
|
47
|
+
hash = hash << 13 | hash >>> 19;
|
|
48
|
+
}
|
|
49
|
+
hash = Math.imul(hash ^ hash >>> 16, 2246822507);
|
|
50
|
+
hash = Math.imul(hash ^ hash >>> 13, 3266489909);
|
|
51
|
+
return (hash ^ hash >>> 16) >>> 0;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @description 生成稳定对象哈希,避免依赖已被禁用的 object-hash 包。
|
|
55
|
+
*/
|
|
56
|
+
function objectHash(value) {
|
|
57
|
+
const bytes = UTF8_ENCODER.encode(stableSerialize(value));
|
|
58
|
+
return HASH_SEEDS.map((seed) => xmur3(bytes, seed).toString(16).padStart(8, "0")).join("");
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { fs, objectHash };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-core/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.0.
|
|
4
|
+
"version": "3.0.4",
|
|
5
5
|
"description": "@weapp-core/shared",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -20,6 +20,14 @@
|
|
|
20
20
|
".": {
|
|
21
21
|
"types": "./src/index.ts",
|
|
22
22
|
"default": "./src/index.ts"
|
|
23
|
+
},
|
|
24
|
+
"./node": {
|
|
25
|
+
"types": "./src/node.ts",
|
|
26
|
+
"default": "./src/node.ts"
|
|
27
|
+
},
|
|
28
|
+
"./fs": {
|
|
29
|
+
"types": "./src/fs/index.ts",
|
|
30
|
+
"default": "./src/fs/index.ts"
|
|
23
31
|
}
|
|
24
32
|
},
|
|
25
33
|
"main": "./src/index.ts",
|
|
@@ -30,6 +38,14 @@
|
|
|
30
38
|
".": {
|
|
31
39
|
"types": "./dist/index.d.ts",
|
|
32
40
|
"import": "./dist/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./node": {
|
|
43
|
+
"types": "./dist/node.d.ts",
|
|
44
|
+
"import": "./dist/node.js"
|
|
45
|
+
},
|
|
46
|
+
"./fs": {
|
|
47
|
+
"types": "./dist/fs/index.d.ts",
|
|
48
|
+
"import": "./dist/fs/index.js"
|
|
33
49
|
}
|
|
34
50
|
},
|
|
35
51
|
"main": "./dist/index.js",
|