zss-engine 2.0.0 → 2.1.1
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/cjs/utils/hash.js +92 -53
- package/dist/esm/utils/hash.js +92 -53
- package/package.json +1 -1
package/dist/cjs/utils/hash.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.genBase36Hash = genBase36Hash;
|
|
4
|
+
const { asUintN } = BigInt;
|
|
4
5
|
function deepNormalize(obj) {
|
|
5
6
|
if (obj === null)
|
|
6
7
|
return 'null';
|
|
@@ -16,65 +17,103 @@ function deepNormalize(obj) {
|
|
|
16
17
|
const pairs = keys.map(key => `"${key}":${deepNormalize(recordObj[key])}`);
|
|
17
18
|
return '{' + pairs.join(',') + '}';
|
|
18
19
|
}
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
20
|
+
function rotl64(x, r) {
|
|
21
|
+
const shift = BigInt(r);
|
|
22
|
+
return asUintN(64, (x << shift) | (x >> (64n - shift)));
|
|
23
|
+
}
|
|
24
|
+
function fmix64(k) {
|
|
25
|
+
k ^= k >> 33n;
|
|
26
|
+
k = asUintN(64, k * 0xff51afd7ed558ccdn);
|
|
27
|
+
k ^= k >> 33n;
|
|
28
|
+
k = asUintN(64, k * 0xc4ceb9fe1a85ec53n);
|
|
29
|
+
k ^= k >> 33n;
|
|
30
|
+
return k;
|
|
31
|
+
}
|
|
32
|
+
function murmurhash3_x64_128(str, seed) {
|
|
33
|
+
let h1 = asUintN(64, BigInt(seed));
|
|
34
|
+
let h2 = asUintN(64, BigInt(seed));
|
|
35
|
+
const c1 = 0x87c37b91114253d5n;
|
|
36
|
+
const c2 = 0x4cf5ad432745937fn;
|
|
37
|
+
const encoder = new TextEncoder();
|
|
38
|
+
const key = encoder.encode(str);
|
|
39
|
+
const len = key.length;
|
|
40
|
+
const nblocks = Math.floor(len / 16);
|
|
41
|
+
const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
|
|
42
|
+
for (let i = 0; i < nblocks; i++) {
|
|
43
|
+
const i16 = i * 16;
|
|
44
|
+
let k1 = view.getBigUint64(i16, true);
|
|
45
|
+
let k2 = view.getBigUint64(i16 + 8, true);
|
|
46
|
+
k1 = asUintN(64, k1 * c1);
|
|
47
|
+
k1 = rotl64(k1, 31);
|
|
48
|
+
k1 = asUintN(64, k1 * c2);
|
|
49
|
+
h1 ^= k1;
|
|
50
|
+
h1 = rotl64(h1, 27);
|
|
51
|
+
h1 = asUintN(64, h1 + h2);
|
|
52
|
+
h1 = asUintN(64, h1 * 5n + 0x52dce729n);
|
|
53
|
+
k2 = asUintN(64, k2 * c2);
|
|
54
|
+
k2 = rotl64(k2, 33);
|
|
55
|
+
k2 = asUintN(64, k2 * c1);
|
|
56
|
+
h2 ^= k2;
|
|
57
|
+
h2 = rotl64(h2, 31);
|
|
58
|
+
h2 = asUintN(64, h2 + h1);
|
|
59
|
+
h2 = asUintN(64, h2 * 5n + 0x38495ab5n);
|
|
38
60
|
}
|
|
39
|
-
|
|
40
|
-
|
|
61
|
+
const tailIndex = nblocks * 16;
|
|
62
|
+
let k1 = 0n;
|
|
63
|
+
let k2 = 0n;
|
|
64
|
+
switch (len & 15) {
|
|
65
|
+
case 15:
|
|
66
|
+
k2 ^= BigInt(key[tailIndex + 14]) << 48n;
|
|
67
|
+
case 14:
|
|
68
|
+
k2 ^= BigInt(key[tailIndex + 13]) << 40n;
|
|
69
|
+
case 13:
|
|
70
|
+
k2 ^= BigInt(key[tailIndex + 12]) << 32n;
|
|
71
|
+
case 12:
|
|
72
|
+
k2 ^= BigInt(key[tailIndex + 11]) << 24n;
|
|
73
|
+
case 11:
|
|
74
|
+
k2 ^= BigInt(key[tailIndex + 10]) << 16n;
|
|
75
|
+
case 10:
|
|
76
|
+
k2 ^= BigInt(key[tailIndex + 9]) << 8n;
|
|
77
|
+
case 9:
|
|
78
|
+
k2 ^= BigInt(key[tailIndex + 8]);
|
|
79
|
+
k2 = asUintN(64, k2 * c2);
|
|
80
|
+
k2 = rotl64(k2, 33);
|
|
81
|
+
k2 = asUintN(64, k2 * c1);
|
|
82
|
+
h2 ^= k2;
|
|
83
|
+
case 8:
|
|
84
|
+
k1 ^= BigInt(key[tailIndex + 7]) << 56n;
|
|
85
|
+
case 7:
|
|
86
|
+
k1 ^= BigInt(key[tailIndex + 6]) << 48n;
|
|
87
|
+
case 6:
|
|
88
|
+
k1 ^= BigInt(key[tailIndex + 5]) << 40n;
|
|
89
|
+
case 5:
|
|
90
|
+
k1 ^= BigInt(key[tailIndex + 4]) << 32n;
|
|
91
|
+
case 4:
|
|
92
|
+
k1 ^= BigInt(key[tailIndex + 3]) << 24n;
|
|
41
93
|
case 3:
|
|
42
|
-
|
|
94
|
+
k1 ^= BigInt(key[tailIndex + 2]) << 16n;
|
|
43
95
|
case 2:
|
|
44
|
-
|
|
96
|
+
k1 ^= BigInt(key[tailIndex + 1]) << 8n;
|
|
45
97
|
case 1:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
98
|
+
k1 ^= BigInt(key[tailIndex + 0]);
|
|
99
|
+
k1 = asUintN(64, k1 * c1);
|
|
100
|
+
k1 = rotl64(k1, 31);
|
|
101
|
+
k1 = asUintN(64, k1 * c2);
|
|
102
|
+
h1 ^= k1;
|
|
51
103
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
104
|
+
h1 ^= BigInt(len);
|
|
105
|
+
h2 ^= BigInt(len);
|
|
106
|
+
h1 = asUintN(64, h1 + h2);
|
|
107
|
+
h2 = asUintN(64, h2 + h1);
|
|
108
|
+
h1 = fmix64(h1);
|
|
109
|
+
h2 = fmix64(h2);
|
|
110
|
+
h1 = asUintN(64, h1 + h2);
|
|
111
|
+
h2 = asUintN(64, h2 + h1);
|
|
112
|
+
return asUintN(64, h1 ^ h2);
|
|
59
113
|
}
|
|
60
114
|
function genBase36Hash(obj, seed, length) {
|
|
61
115
|
const normalized = deepNormalize(obj);
|
|
62
|
-
const hashValue =
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
let result = firstChar + hashStr;
|
|
66
|
-
if (result.length > length) {
|
|
67
|
-
result = result.slice(0, length);
|
|
68
|
-
}
|
|
69
|
-
else if (result.length < length) {
|
|
70
|
-
const paddingNeeded = length - result.length;
|
|
71
|
-
const paddingChars = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|
72
|
-
let paddingHash = hashValue;
|
|
73
|
-
for (let i = 0; i < paddingNeeded; i++) {
|
|
74
|
-
paddingHash = paddingHash * 1103515245 + 12345;
|
|
75
|
-
const paddingChar = paddingChars[Math.abs(paddingHash) % 36];
|
|
76
|
-
result += paddingChar;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return result;
|
|
116
|
+
const hashValue = murmurhash3_x64_128(normalized, seed);
|
|
117
|
+
const hash = hashValue.toString(36);
|
|
118
|
+
return 'x' + hash.slice(-(length - 1));
|
|
80
119
|
}
|
package/dist/esm/utils/hash.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const { asUintN } = BigInt;
|
|
1
2
|
function deepNormalize(obj) {
|
|
2
3
|
if (obj === null)
|
|
3
4
|
return 'null';
|
|
@@ -13,65 +14,103 @@ function deepNormalize(obj) {
|
|
|
13
14
|
const pairs = keys.map(key => `"${key}":${deepNormalize(recordObj[key])}`);
|
|
14
15
|
return '{' + pairs.join(',') + '}';
|
|
15
16
|
}
|
|
16
|
-
function
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
function rotl64(x, r) {
|
|
18
|
+
const shift = BigInt(r);
|
|
19
|
+
return asUintN(64, (x << shift) | (x >> (64n - shift)));
|
|
20
|
+
}
|
|
21
|
+
function fmix64(k) {
|
|
22
|
+
k ^= k >> 33n;
|
|
23
|
+
k = asUintN(64, k * 0xff51afd7ed558ccdn);
|
|
24
|
+
k ^= k >> 33n;
|
|
25
|
+
k = asUintN(64, k * 0xc4ceb9fe1a85ec53n);
|
|
26
|
+
k ^= k >> 33n;
|
|
27
|
+
return k;
|
|
28
|
+
}
|
|
29
|
+
function murmurhash3_x64_128(str, seed) {
|
|
30
|
+
let h1 = asUintN(64, BigInt(seed));
|
|
31
|
+
let h2 = asUintN(64, BigInt(seed));
|
|
32
|
+
const c1 = 0x87c37b91114253d5n;
|
|
33
|
+
const c2 = 0x4cf5ad432745937fn;
|
|
34
|
+
const encoder = new TextEncoder();
|
|
35
|
+
const key = encoder.encode(str);
|
|
36
|
+
const len = key.length;
|
|
37
|
+
const nblocks = Math.floor(len / 16);
|
|
38
|
+
const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
|
|
39
|
+
for (let i = 0; i < nblocks; i++) {
|
|
40
|
+
const i16 = i * 16;
|
|
41
|
+
let k1 = view.getBigUint64(i16, true);
|
|
42
|
+
let k2 = view.getBigUint64(i16 + 8, true);
|
|
43
|
+
k1 = asUintN(64, k1 * c1);
|
|
44
|
+
k1 = rotl64(k1, 31);
|
|
45
|
+
k1 = asUintN(64, k1 * c2);
|
|
46
|
+
h1 ^= k1;
|
|
47
|
+
h1 = rotl64(h1, 27);
|
|
48
|
+
h1 = asUintN(64, h1 + h2);
|
|
49
|
+
h1 = asUintN(64, h1 * 5n + 0x52dce729n);
|
|
50
|
+
k2 = asUintN(64, k2 * c2);
|
|
51
|
+
k2 = rotl64(k2, 33);
|
|
52
|
+
k2 = asUintN(64, k2 * c1);
|
|
53
|
+
h2 ^= k2;
|
|
54
|
+
h2 = rotl64(h2, 31);
|
|
55
|
+
h2 = asUintN(64, h2 + h1);
|
|
56
|
+
h2 = asUintN(64, h2 * 5n + 0x38495ab5n);
|
|
35
57
|
}
|
|
36
|
-
|
|
37
|
-
|
|
58
|
+
const tailIndex = nblocks * 16;
|
|
59
|
+
let k1 = 0n;
|
|
60
|
+
let k2 = 0n;
|
|
61
|
+
switch (len & 15) {
|
|
62
|
+
case 15:
|
|
63
|
+
k2 ^= BigInt(key[tailIndex + 14]) << 48n;
|
|
64
|
+
case 14:
|
|
65
|
+
k2 ^= BigInt(key[tailIndex + 13]) << 40n;
|
|
66
|
+
case 13:
|
|
67
|
+
k2 ^= BigInt(key[tailIndex + 12]) << 32n;
|
|
68
|
+
case 12:
|
|
69
|
+
k2 ^= BigInt(key[tailIndex + 11]) << 24n;
|
|
70
|
+
case 11:
|
|
71
|
+
k2 ^= BigInt(key[tailIndex + 10]) << 16n;
|
|
72
|
+
case 10:
|
|
73
|
+
k2 ^= BigInt(key[tailIndex + 9]) << 8n;
|
|
74
|
+
case 9:
|
|
75
|
+
k2 ^= BigInt(key[tailIndex + 8]);
|
|
76
|
+
k2 = asUintN(64, k2 * c2);
|
|
77
|
+
k2 = rotl64(k2, 33);
|
|
78
|
+
k2 = asUintN(64, k2 * c1);
|
|
79
|
+
h2 ^= k2;
|
|
80
|
+
case 8:
|
|
81
|
+
k1 ^= BigInt(key[tailIndex + 7]) << 56n;
|
|
82
|
+
case 7:
|
|
83
|
+
k1 ^= BigInt(key[tailIndex + 6]) << 48n;
|
|
84
|
+
case 6:
|
|
85
|
+
k1 ^= BigInt(key[tailIndex + 5]) << 40n;
|
|
86
|
+
case 5:
|
|
87
|
+
k1 ^= BigInt(key[tailIndex + 4]) << 32n;
|
|
88
|
+
case 4:
|
|
89
|
+
k1 ^= BigInt(key[tailIndex + 3]) << 24n;
|
|
38
90
|
case 3:
|
|
39
|
-
|
|
91
|
+
k1 ^= BigInt(key[tailIndex + 2]) << 16n;
|
|
40
92
|
case 2:
|
|
41
|
-
|
|
93
|
+
k1 ^= BigInt(key[tailIndex + 1]) << 8n;
|
|
42
94
|
case 1:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
95
|
+
k1 ^= BigInt(key[tailIndex + 0]);
|
|
96
|
+
k1 = asUintN(64, k1 * c1);
|
|
97
|
+
k1 = rotl64(k1, 31);
|
|
98
|
+
k1 = asUintN(64, k1 * c2);
|
|
99
|
+
h1 ^= k1;
|
|
48
100
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
101
|
+
h1 ^= BigInt(len);
|
|
102
|
+
h2 ^= BigInt(len);
|
|
103
|
+
h1 = asUintN(64, h1 + h2);
|
|
104
|
+
h2 = asUintN(64, h2 + h1);
|
|
105
|
+
h1 = fmix64(h1);
|
|
106
|
+
h2 = fmix64(h2);
|
|
107
|
+
h1 = asUintN(64, h1 + h2);
|
|
108
|
+
h2 = asUintN(64, h2 + h1);
|
|
109
|
+
return asUintN(64, h1 ^ h2);
|
|
56
110
|
}
|
|
57
111
|
export function genBase36Hash(obj, seed, length) {
|
|
58
112
|
const normalized = deepNormalize(obj);
|
|
59
|
-
const hashValue =
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
let result = firstChar + hashStr;
|
|
63
|
-
if (result.length > length) {
|
|
64
|
-
result = result.slice(0, length);
|
|
65
|
-
}
|
|
66
|
-
else if (result.length < length) {
|
|
67
|
-
const paddingNeeded = length - result.length;
|
|
68
|
-
const paddingChars = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|
69
|
-
let paddingHash = hashValue;
|
|
70
|
-
for (let i = 0; i < paddingNeeded; i++) {
|
|
71
|
-
paddingHash = paddingHash * 1103515245 + 12345;
|
|
72
|
-
const paddingChar = paddingChars[Math.abs(paddingHash) % 36];
|
|
73
|
-
result += paddingChar;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return result;
|
|
113
|
+
const hashValue = murmurhash3_x64_128(normalized, seed);
|
|
114
|
+
const hash = hashValue.toString(36);
|
|
115
|
+
return 'x' + hash.slice(-(length - 1));
|
|
77
116
|
}
|