@based/client 8.0.1 → 8.0.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/index.cjs +9 -9
- package/dist/src/genObserveId.js +42 -1
- package/dist/src/genObserveId.js.map +1 -1
- package/package.json +1 -1
package/dist/src/genObserveId.js
CHANGED
|
@@ -1,8 +1,49 @@
|
|
|
1
1
|
import { hashObjectIgnoreKeyOrder, hash } from '@saulx/hash';
|
|
2
|
+
import { ENCODER } from '@saulx/utils';
|
|
3
|
+
const calculateHash32 = (name, uint8Array, seed = 0) => {
|
|
4
|
+
let hash = seed;
|
|
5
|
+
const prime1 = 31;
|
|
6
|
+
const prime2 = 17;
|
|
7
|
+
const prime3 = 41;
|
|
8
|
+
for (let i = 0; i < name.length; i++) {
|
|
9
|
+
const charCode = name.charCodeAt(i);
|
|
10
|
+
hash = (hash * prime3) ^ charCode;
|
|
11
|
+
hash = (hash * prime1) & 0xffffffff;
|
|
12
|
+
}
|
|
13
|
+
for (let i = 0; i < uint8Array.length; i++) {
|
|
14
|
+
hash = (hash * prime1) ^ uint8Array[i];
|
|
15
|
+
hash = (hash * prime2) & 0xffffffff;
|
|
16
|
+
}
|
|
17
|
+
hash ^= hash >>> 16;
|
|
18
|
+
hash = Math.imul(hash, 0x85ebca6b);
|
|
19
|
+
hash ^= hash >>> 13;
|
|
20
|
+
hash = Math.imul(hash, 0xc2b2ae35);
|
|
21
|
+
hash ^= hash >>> 16;
|
|
22
|
+
return hash >>> 0;
|
|
23
|
+
};
|
|
24
|
+
const simpleHashU8 = (name, uint8Array) => {
|
|
25
|
+
const seed1 = 0xabcdef01;
|
|
26
|
+
const seed2 = 0x10fedcba;
|
|
27
|
+
const hashPart1 = calculateHash32(name, uint8Array, seed1);
|
|
28
|
+
const hashPart2 = calculateHash32(name, uint8Array, seed2);
|
|
29
|
+
const highBitsContribution = Number(hashPart1) * 2097152;
|
|
30
|
+
const lowBitsContribution = hashPart2 >>> 11;
|
|
31
|
+
const result = highBitsContribution + lowBitsContribution;
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
2
34
|
export const genObserveId = (name, payload) => {
|
|
3
35
|
if (payload === undefined) {
|
|
36
|
+
// also for null :/ ?
|
|
4
37
|
return hash(name);
|
|
5
38
|
}
|
|
6
|
-
|
|
39
|
+
if (payload instanceof Uint8Array) {
|
|
40
|
+
return simpleHashU8(name, payload);
|
|
41
|
+
}
|
|
42
|
+
else if (typeof payload === 'string') {
|
|
43
|
+
return simpleHashU8(name, ENCODER.encode(payload));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return hashObjectIgnoreKeyOrder([name, payload]);
|
|
47
|
+
}
|
|
7
48
|
};
|
|
8
49
|
//# sourceMappingURL=genObserveId.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"genObserveId.js","sourceRoot":"","sources":["../../src/genObserveId.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"genObserveId.js","sourceRoot":"","sources":["../../src/genObserveId.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,MAAM,eAAe,GAAG,CAAC,IAAY,EAAE,UAAsB,EAAE,IAAI,GAAG,CAAC,EAAE,EAAE;IACzE,IAAI,IAAI,GAAG,IAAI,CAAA;IACf,MAAM,MAAM,GAAG,EAAE,CAAA;IACjB,MAAM,MAAM,GAAG,EAAE,CAAA;IACjB,MAAM,MAAM,GAAG,EAAE,CAAA;IACjB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAA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