@automerge/automerge-repo-react-hooks 2.5.0-alpha.2 → 2.5.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/index.js +2067 -1159
- package/dist/index.js.map +1 -0
- package/package.json +3 -3
- package/vite.config.ts +2 -0
package/dist/index.js
CHANGED
|
@@ -1,1242 +1,2150 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import require$$0, { createContext, useContext, useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
function wrapPromise(promise) {
|
|
4
|
+
let status = "pending";
|
|
5
|
+
let result;
|
|
6
|
+
let error;
|
|
7
|
+
const suspender = promise.then(
|
|
8
|
+
(data) => {
|
|
9
|
+
status = "success";
|
|
10
|
+
result = data;
|
|
7
11
|
},
|
|
8
|
-
(
|
|
9
|
-
|
|
12
|
+
(err) => {
|
|
13
|
+
status = "error";
|
|
14
|
+
error = err;
|
|
10
15
|
}
|
|
11
16
|
);
|
|
12
17
|
return {
|
|
13
|
-
promise
|
|
18
|
+
promise,
|
|
14
19
|
read() {
|
|
15
|
-
switch (
|
|
20
|
+
switch (status) {
|
|
16
21
|
case "pending":
|
|
17
|
-
throw
|
|
22
|
+
throw suspender;
|
|
18
23
|
case "error":
|
|
19
|
-
throw
|
|
24
|
+
throw error;
|
|
20
25
|
case "success":
|
|
21
|
-
return
|
|
26
|
+
return result;
|
|
22
27
|
}
|
|
23
28
|
}
|
|
24
29
|
};
|
|
25
30
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
|
|
32
|
+
const RepoContext = createContext(null);
|
|
33
|
+
function useRepo() {
|
|
34
|
+
const repo = useContext(RepoContext);
|
|
35
|
+
if (!repo) throw new Error("Repo was not found on RepoContext.");
|
|
36
|
+
return repo;
|
|
31
37
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
|
|
39
|
+
const REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
40
|
+
|
|
41
|
+
function validate(uuid) {
|
|
42
|
+
return typeof uuid === 'string' && REGEX.test(uuid);
|
|
35
43
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
|
|
45
|
+
function parse(uuid) {
|
|
46
|
+
if (!validate(uuid)) {
|
|
47
|
+
throw TypeError('Invalid UUID');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let v;
|
|
51
|
+
const arr = new Uint8Array(16); // Parse ########-....-....-....-............
|
|
52
|
+
|
|
53
|
+
arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
|
|
54
|
+
arr[1] = v >>> 16 & 0xff;
|
|
55
|
+
arr[2] = v >>> 8 & 0xff;
|
|
56
|
+
arr[3] = v & 0xff; // Parse ........-####-....-....-............
|
|
57
|
+
|
|
58
|
+
arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
|
|
59
|
+
arr[5] = v & 0xff; // Parse ........-....-####-....-............
|
|
60
|
+
|
|
61
|
+
arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
|
|
62
|
+
arr[7] = v & 0xff; // Parse ........-....-....-####-............
|
|
63
|
+
|
|
64
|
+
arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
|
|
65
|
+
arr[9] = v & 0xff; // Parse ........-....-....-....-############
|
|
66
|
+
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
|
|
67
|
+
|
|
68
|
+
arr[10] = (v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000 & 0xff;
|
|
69
|
+
arr[11] = v / 0x100000000 & 0xff;
|
|
70
|
+
arr[12] = v >>> 24 & 0xff;
|
|
71
|
+
arr[13] = v >>> 16 & 0xff;
|
|
72
|
+
arr[14] = v >>> 8 & 0xff;
|
|
73
|
+
arr[15] = v & 0xff;
|
|
74
|
+
return arr;
|
|
42
75
|
}
|
|
43
|
-
|
|
44
|
-
|
|
76
|
+
|
|
77
|
+
function getDefaultExportFromCjs (x) {
|
|
78
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
45
79
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
80
|
+
|
|
81
|
+
var sha256 = {};
|
|
82
|
+
|
|
83
|
+
var sha2 = {};
|
|
84
|
+
|
|
85
|
+
var _md = {};
|
|
86
|
+
|
|
87
|
+
var utils = {};
|
|
88
|
+
|
|
89
|
+
var crypto = {};
|
|
90
|
+
|
|
91
|
+
var hasRequiredCrypto;
|
|
92
|
+
|
|
93
|
+
function requireCrypto () {
|
|
94
|
+
if (hasRequiredCrypto) return crypto;
|
|
95
|
+
hasRequiredCrypto = 1;
|
|
96
|
+
Object.defineProperty(crypto, "__esModule", { value: true });
|
|
97
|
+
crypto.crypto = void 0;
|
|
98
|
+
crypto.crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
|
|
99
|
+
|
|
100
|
+
return crypto;
|
|
49
101
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
102
|
+
|
|
103
|
+
var hasRequiredUtils;
|
|
104
|
+
|
|
105
|
+
function requireUtils () {
|
|
106
|
+
if (hasRequiredUtils) return utils;
|
|
107
|
+
hasRequiredUtils = 1;
|
|
108
|
+
(function (exports) {
|
|
109
|
+
/**
|
|
110
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
111
|
+
* @module
|
|
112
|
+
*/
|
|
113
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
114
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
115
|
+
exports.wrapXOFConstructorWithOpts = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.Hash = exports.nextTick = exports.swap32IfBE = exports.byteSwapIfBE = exports.swap8IfBE = exports.isLE = void 0;
|
|
116
|
+
exports.isBytes = isBytes;
|
|
117
|
+
exports.anumber = anumber;
|
|
118
|
+
exports.abytes = abytes;
|
|
119
|
+
exports.ahash = ahash;
|
|
120
|
+
exports.aexists = aexists;
|
|
121
|
+
exports.aoutput = aoutput;
|
|
122
|
+
exports.u8 = u8;
|
|
123
|
+
exports.u32 = u32;
|
|
124
|
+
exports.clean = clean;
|
|
125
|
+
exports.createView = createView;
|
|
126
|
+
exports.rotr = rotr;
|
|
127
|
+
exports.rotl = rotl;
|
|
128
|
+
exports.byteSwap = byteSwap;
|
|
129
|
+
exports.byteSwap32 = byteSwap32;
|
|
130
|
+
exports.bytesToHex = bytesToHex;
|
|
131
|
+
exports.hexToBytes = hexToBytes;
|
|
132
|
+
exports.asyncLoop = asyncLoop;
|
|
133
|
+
exports.utf8ToBytes = utf8ToBytes;
|
|
134
|
+
exports.bytesToUtf8 = bytesToUtf8;
|
|
135
|
+
exports.toBytes = toBytes;
|
|
136
|
+
exports.kdfInputToBytes = kdfInputToBytes;
|
|
137
|
+
exports.concatBytes = concatBytes;
|
|
138
|
+
exports.checkOpts = checkOpts;
|
|
139
|
+
exports.createHasher = createHasher;
|
|
140
|
+
exports.createOptHasher = createOptHasher;
|
|
141
|
+
exports.createXOFer = createXOFer;
|
|
142
|
+
exports.randomBytes = randomBytes;
|
|
143
|
+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
144
|
+
// node.js versions earlier than v19 don't declare it in global scope.
|
|
145
|
+
// For node.js, package.json#exports field mapping rewrites import
|
|
146
|
+
// from `crypto` to `cryptoNode`, which imports native module.
|
|
147
|
+
// Makes the utils un-importable in browsers without a bundler.
|
|
148
|
+
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
|
149
|
+
const crypto_1 = /*@__PURE__*/ requireCrypto();
|
|
150
|
+
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
151
|
+
function isBytes(a) {
|
|
152
|
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
153
|
+
}
|
|
154
|
+
/** Asserts something is positive integer. */
|
|
155
|
+
function anumber(n) {
|
|
156
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
157
|
+
throw new Error('positive integer expected, got ' + n);
|
|
158
|
+
}
|
|
159
|
+
/** Asserts something is Uint8Array. */
|
|
160
|
+
function abytes(b, ...lengths) {
|
|
161
|
+
if (!isBytes(b))
|
|
162
|
+
throw new Error('Uint8Array expected');
|
|
163
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
164
|
+
throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
|
|
165
|
+
}
|
|
166
|
+
/** Asserts something is hash */
|
|
167
|
+
function ahash(h) {
|
|
168
|
+
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
169
|
+
throw new Error('Hash should be wrapped by utils.createHasher');
|
|
170
|
+
anumber(h.outputLen);
|
|
171
|
+
anumber(h.blockLen);
|
|
172
|
+
}
|
|
173
|
+
/** Asserts a hash instance has not been destroyed / finished */
|
|
174
|
+
function aexists(instance, checkFinished = true) {
|
|
175
|
+
if (instance.destroyed)
|
|
176
|
+
throw new Error('Hash instance has been destroyed');
|
|
177
|
+
if (checkFinished && instance.finished)
|
|
178
|
+
throw new Error('Hash#digest() has already been called');
|
|
179
|
+
}
|
|
180
|
+
/** Asserts output is properly-sized byte array */
|
|
181
|
+
function aoutput(out, instance) {
|
|
182
|
+
abytes(out);
|
|
183
|
+
const min = instance.outputLen;
|
|
184
|
+
if (out.length < min) {
|
|
185
|
+
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** Cast u8 / u16 / u32 to u8. */
|
|
189
|
+
function u8(arr) {
|
|
190
|
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
191
|
+
}
|
|
192
|
+
/** Cast u8 / u16 / u32 to u32. */
|
|
193
|
+
function u32(arr) {
|
|
194
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
195
|
+
}
|
|
196
|
+
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
|
197
|
+
function clean(...arrays) {
|
|
198
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
199
|
+
arrays[i].fill(0);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/** Create DataView of an array for easy byte-level manipulation. */
|
|
203
|
+
function createView(arr) {
|
|
204
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
205
|
+
}
|
|
206
|
+
/** The rotate right (circular right shift) operation for uint32 */
|
|
207
|
+
function rotr(word, shift) {
|
|
208
|
+
return (word << (32 - shift)) | (word >>> shift);
|
|
209
|
+
}
|
|
210
|
+
/** The rotate left (circular left shift) operation for uint32 */
|
|
211
|
+
function rotl(word, shift) {
|
|
212
|
+
return (word << shift) | ((word >>> (32 - shift)) >>> 0);
|
|
213
|
+
}
|
|
214
|
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
|
215
|
+
exports.isLE = (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
216
|
+
/** The byte swap operation for uint32 */
|
|
217
|
+
function byteSwap(word) {
|
|
218
|
+
return (((word << 24) & 0xff000000) |
|
|
219
|
+
((word << 8) & 0xff0000) |
|
|
220
|
+
((word >>> 8) & 0xff00) |
|
|
221
|
+
((word >>> 24) & 0xff));
|
|
222
|
+
}
|
|
223
|
+
/** Conditionally byte swap if on a big-endian platform */
|
|
224
|
+
exports.swap8IfBE = exports.isLE
|
|
225
|
+
? (n) => n
|
|
226
|
+
: (n) => byteSwap(n);
|
|
227
|
+
/** @deprecated */
|
|
228
|
+
exports.byteSwapIfBE = exports.swap8IfBE;
|
|
229
|
+
/** In place byte swap for Uint32Array */
|
|
230
|
+
function byteSwap32(arr) {
|
|
231
|
+
for (let i = 0; i < arr.length; i++) {
|
|
232
|
+
arr[i] = byteSwap(arr[i]);
|
|
233
|
+
}
|
|
234
|
+
return arr;
|
|
235
|
+
}
|
|
236
|
+
exports.swap32IfBE = exports.isLE
|
|
237
|
+
? (u) => u
|
|
238
|
+
: byteSwap32;
|
|
239
|
+
// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex
|
|
240
|
+
const hasHexBuiltin = /* @__PURE__ */ (() =>
|
|
241
|
+
// @ts-ignore
|
|
242
|
+
typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
243
|
+
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
244
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
245
|
+
/**
|
|
246
|
+
* Convert byte array to hex string. Uses built-in function, when available.
|
|
247
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
248
|
+
*/
|
|
249
|
+
function bytesToHex(bytes) {
|
|
250
|
+
abytes(bytes);
|
|
251
|
+
// @ts-ignore
|
|
252
|
+
if (hasHexBuiltin)
|
|
253
|
+
return bytes.toHex();
|
|
254
|
+
// pre-caching improves the speed 6x
|
|
255
|
+
let hex = '';
|
|
256
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
257
|
+
hex += hexes[bytes[i]];
|
|
258
|
+
}
|
|
259
|
+
return hex;
|
|
260
|
+
}
|
|
261
|
+
// We use optimized technique to convert hex string to byte array
|
|
262
|
+
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
263
|
+
function asciiToBase16(ch) {
|
|
264
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
265
|
+
return ch - asciis._0; // '2' => 50-48
|
|
266
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
267
|
+
return ch - (asciis.A - 10); // 'B' => 66-(65-10)
|
|
268
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
269
|
+
return ch - (asciis.a - 10); // 'b' => 98-(97-10)
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Convert hex string to byte array. Uses built-in function, when available.
|
|
274
|
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
275
|
+
*/
|
|
276
|
+
function hexToBytes(hex) {
|
|
277
|
+
if (typeof hex !== 'string')
|
|
278
|
+
throw new Error('hex string expected, got ' + typeof hex);
|
|
279
|
+
// @ts-ignore
|
|
280
|
+
if (hasHexBuiltin)
|
|
281
|
+
return Uint8Array.fromHex(hex);
|
|
282
|
+
const hl = hex.length;
|
|
283
|
+
const al = hl / 2;
|
|
284
|
+
if (hl % 2)
|
|
285
|
+
throw new Error('hex string expected, got unpadded hex of length ' + hl);
|
|
286
|
+
const array = new Uint8Array(al);
|
|
287
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
288
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
289
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
290
|
+
if (n1 === undefined || n2 === undefined) {
|
|
291
|
+
const char = hex[hi] + hex[hi + 1];
|
|
292
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
293
|
+
}
|
|
294
|
+
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
|
|
295
|
+
}
|
|
296
|
+
return array;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* There is no setImmediate in browser and setTimeout is slow.
|
|
300
|
+
* Call of async fn will return Promise, which will be fullfiled only on
|
|
301
|
+
* next scheduler queue processing step and this is exactly what we need.
|
|
302
|
+
*/
|
|
303
|
+
const nextTick = async () => { };
|
|
304
|
+
exports.nextTick = nextTick;
|
|
305
|
+
/** Returns control to thread each 'tick' ms to avoid blocking. */
|
|
306
|
+
async function asyncLoop(iters, tick, cb) {
|
|
307
|
+
let ts = Date.now();
|
|
308
|
+
for (let i = 0; i < iters; i++) {
|
|
309
|
+
cb(i);
|
|
310
|
+
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
|
311
|
+
const diff = Date.now() - ts;
|
|
312
|
+
if (diff >= 0 && diff < tick)
|
|
313
|
+
continue;
|
|
314
|
+
await (0, exports.nextTick)();
|
|
315
|
+
ts += diff;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Converts string to bytes using UTF8 encoding.
|
|
320
|
+
* @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
|
|
321
|
+
*/
|
|
322
|
+
function utf8ToBytes(str) {
|
|
323
|
+
if (typeof str !== 'string')
|
|
324
|
+
throw new Error('string expected');
|
|
325
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Converts bytes to string using UTF8 encoding.
|
|
329
|
+
* @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'
|
|
330
|
+
*/
|
|
331
|
+
function bytesToUtf8(bytes) {
|
|
332
|
+
return new TextDecoder().decode(bytes);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
336
|
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
337
|
+
* Keep in mind for future mutable operations.
|
|
338
|
+
*/
|
|
339
|
+
function toBytes(data) {
|
|
340
|
+
if (typeof data === 'string')
|
|
341
|
+
data = utf8ToBytes(data);
|
|
342
|
+
abytes(data);
|
|
343
|
+
return data;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Helper for KDFs: consumes uint8array or string.
|
|
347
|
+
* When string is passed, does utf8 decoding, using TextDecoder.
|
|
348
|
+
*/
|
|
349
|
+
function kdfInputToBytes(data) {
|
|
350
|
+
if (typeof data === 'string')
|
|
351
|
+
data = utf8ToBytes(data);
|
|
352
|
+
abytes(data);
|
|
353
|
+
return data;
|
|
354
|
+
}
|
|
355
|
+
/** Copies several Uint8Arrays into one. */
|
|
356
|
+
function concatBytes(...arrays) {
|
|
357
|
+
let sum = 0;
|
|
358
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
359
|
+
const a = arrays[i];
|
|
360
|
+
abytes(a);
|
|
361
|
+
sum += a.length;
|
|
362
|
+
}
|
|
363
|
+
const res = new Uint8Array(sum);
|
|
364
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
365
|
+
const a = arrays[i];
|
|
366
|
+
res.set(a, pad);
|
|
367
|
+
pad += a.length;
|
|
368
|
+
}
|
|
369
|
+
return res;
|
|
370
|
+
}
|
|
371
|
+
function checkOpts(defaults, opts) {
|
|
372
|
+
if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
|
|
373
|
+
throw new Error('options should be object or undefined');
|
|
374
|
+
const merged = Object.assign(defaults, opts);
|
|
375
|
+
return merged;
|
|
376
|
+
}
|
|
377
|
+
/** For runtime check if class implements interface */
|
|
378
|
+
class Hash {
|
|
379
|
+
}
|
|
380
|
+
exports.Hash = Hash;
|
|
381
|
+
/** Wraps hash function, creating an interface on top of it */
|
|
382
|
+
function createHasher(hashCons) {
|
|
383
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
384
|
+
const tmp = hashCons();
|
|
385
|
+
hashC.outputLen = tmp.outputLen;
|
|
386
|
+
hashC.blockLen = tmp.blockLen;
|
|
387
|
+
hashC.create = () => hashCons();
|
|
388
|
+
return hashC;
|
|
389
|
+
}
|
|
390
|
+
function createOptHasher(hashCons) {
|
|
391
|
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
392
|
+
const tmp = hashCons({});
|
|
393
|
+
hashC.outputLen = tmp.outputLen;
|
|
394
|
+
hashC.blockLen = tmp.blockLen;
|
|
395
|
+
hashC.create = (opts) => hashCons(opts);
|
|
396
|
+
return hashC;
|
|
397
|
+
}
|
|
398
|
+
function createXOFer(hashCons) {
|
|
399
|
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
400
|
+
const tmp = hashCons({});
|
|
401
|
+
hashC.outputLen = tmp.outputLen;
|
|
402
|
+
hashC.blockLen = tmp.blockLen;
|
|
403
|
+
hashC.create = (opts) => hashCons(opts);
|
|
404
|
+
return hashC;
|
|
405
|
+
}
|
|
406
|
+
exports.wrapConstructor = createHasher;
|
|
407
|
+
exports.wrapConstructorWithOpts = createOptHasher;
|
|
408
|
+
exports.wrapXOFConstructorWithOpts = createXOFer;
|
|
409
|
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
|
410
|
+
function randomBytes(bytesLength = 32) {
|
|
411
|
+
if (crypto_1.crypto && typeof crypto_1.crypto.getRandomValues === 'function') {
|
|
412
|
+
return crypto_1.crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
413
|
+
}
|
|
414
|
+
// Legacy Node.js compatibility
|
|
415
|
+
if (crypto_1.crypto && typeof crypto_1.crypto.randomBytes === 'function') {
|
|
416
|
+
return Uint8Array.from(crypto_1.crypto.randomBytes(bytesLength));
|
|
417
|
+
}
|
|
418
|
+
throw new Error('crypto.getRandomValues must be defined');
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
} (utils));
|
|
422
|
+
return utils;
|
|
220
423
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
424
|
+
|
|
425
|
+
var hasRequired_md;
|
|
426
|
+
|
|
427
|
+
function require_md () {
|
|
428
|
+
if (hasRequired_md) return _md;
|
|
429
|
+
hasRequired_md = 1;
|
|
430
|
+
Object.defineProperty(_md, "__esModule", { value: true });
|
|
431
|
+
_md.SHA512_IV = _md.SHA384_IV = _md.SHA224_IV = _md.SHA256_IV = _md.HashMD = void 0;
|
|
432
|
+
_md.setBigUint64 = setBigUint64;
|
|
433
|
+
_md.Chi = Chi;
|
|
434
|
+
_md.Maj = Maj;
|
|
435
|
+
/**
|
|
436
|
+
* Internal Merkle-Damgard hash utils.
|
|
437
|
+
* @module
|
|
438
|
+
*/
|
|
439
|
+
const utils_ts_1 = /*@__PURE__*/ requireUtils();
|
|
440
|
+
/** Polyfill for Safari 14. https://caniuse.com/mdn-javascript_builtins_dataview_setbiguint64 */
|
|
441
|
+
function setBigUint64(view, byteOffset, value, isLE) {
|
|
442
|
+
if (typeof view.setBigUint64 === 'function')
|
|
443
|
+
return view.setBigUint64(byteOffset, value, isLE);
|
|
444
|
+
const _32n = BigInt(32);
|
|
445
|
+
const _u32_max = BigInt(0xffffffff);
|
|
446
|
+
const wh = Number((value >> _32n) & _u32_max);
|
|
447
|
+
const wl = Number(value & _u32_max);
|
|
448
|
+
const h = isLE ? 4 : 0;
|
|
449
|
+
const l = isLE ? 0 : 4;
|
|
450
|
+
view.setUint32(byteOffset + h, wh, isLE);
|
|
451
|
+
view.setUint32(byteOffset + l, wl, isLE);
|
|
452
|
+
}
|
|
453
|
+
/** Choice: a ? b : c */
|
|
454
|
+
function Chi(a, b, c) {
|
|
455
|
+
return (a & b) ^ (~a & c);
|
|
456
|
+
}
|
|
457
|
+
/** Majority function, true if any two inputs is true. */
|
|
458
|
+
function Maj(a, b, c) {
|
|
459
|
+
return (a & b) ^ (a & c) ^ (b & c);
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Merkle-Damgard hash construction base class.
|
|
463
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
464
|
+
*/
|
|
465
|
+
class HashMD extends utils_ts_1.Hash {
|
|
466
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
467
|
+
super();
|
|
468
|
+
this.finished = false;
|
|
469
|
+
this.length = 0;
|
|
470
|
+
this.pos = 0;
|
|
471
|
+
this.destroyed = false;
|
|
472
|
+
this.blockLen = blockLen;
|
|
473
|
+
this.outputLen = outputLen;
|
|
474
|
+
this.padOffset = padOffset;
|
|
475
|
+
this.isLE = isLE;
|
|
476
|
+
this.buffer = new Uint8Array(blockLen);
|
|
477
|
+
this.view = (0, utils_ts_1.createView)(this.buffer);
|
|
478
|
+
}
|
|
479
|
+
update(data) {
|
|
480
|
+
(0, utils_ts_1.aexists)(this);
|
|
481
|
+
data = (0, utils_ts_1.toBytes)(data);
|
|
482
|
+
(0, utils_ts_1.abytes)(data);
|
|
483
|
+
const { view, buffer, blockLen } = this;
|
|
484
|
+
const len = data.length;
|
|
485
|
+
for (let pos = 0; pos < len;) {
|
|
486
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
487
|
+
// Fast path: we have at least one block in input, cast it to view and process
|
|
488
|
+
if (take === blockLen) {
|
|
489
|
+
const dataView = (0, utils_ts_1.createView)(data);
|
|
490
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
491
|
+
this.process(dataView, pos);
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
495
|
+
this.pos += take;
|
|
496
|
+
pos += take;
|
|
497
|
+
if (this.pos === blockLen) {
|
|
498
|
+
this.process(view, 0);
|
|
499
|
+
this.pos = 0;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
this.length += data.length;
|
|
503
|
+
this.roundClean();
|
|
504
|
+
return this;
|
|
505
|
+
}
|
|
506
|
+
digestInto(out) {
|
|
507
|
+
(0, utils_ts_1.aexists)(this);
|
|
508
|
+
(0, utils_ts_1.aoutput)(out, this);
|
|
509
|
+
this.finished = true;
|
|
510
|
+
// Padding
|
|
511
|
+
// We can avoid allocation of buffer for padding completely if it
|
|
512
|
+
// was previously not allocated here. But it won't change performance.
|
|
513
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
514
|
+
let { pos } = this;
|
|
515
|
+
// append the bit '1' to the message
|
|
516
|
+
buffer[pos++] = 0b10000000;
|
|
517
|
+
(0, utils_ts_1.clean)(this.buffer.subarray(pos));
|
|
518
|
+
// we have less than padOffset left in buffer, so we cannot put length in
|
|
519
|
+
// current block, need process it and pad again
|
|
520
|
+
if (this.padOffset > blockLen - pos) {
|
|
521
|
+
this.process(view, 0);
|
|
522
|
+
pos = 0;
|
|
523
|
+
}
|
|
524
|
+
// Pad until full block byte with zeros
|
|
525
|
+
for (let i = pos; i < blockLen; i++)
|
|
526
|
+
buffer[i] = 0;
|
|
527
|
+
// Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
|
|
528
|
+
// You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
|
|
529
|
+
// So we just write lowest 64 bits of that value.
|
|
530
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
531
|
+
this.process(view, 0);
|
|
532
|
+
const oview = (0, utils_ts_1.createView)(out);
|
|
533
|
+
const len = this.outputLen;
|
|
534
|
+
// NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
|
|
535
|
+
if (len % 4)
|
|
536
|
+
throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
537
|
+
const outLen = len / 4;
|
|
538
|
+
const state = this.get();
|
|
539
|
+
if (outLen > state.length)
|
|
540
|
+
throw new Error('_sha2: outputLen bigger than state');
|
|
541
|
+
for (let i = 0; i < outLen; i++)
|
|
542
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
543
|
+
}
|
|
544
|
+
digest() {
|
|
545
|
+
const { buffer, outputLen } = this;
|
|
546
|
+
this.digestInto(buffer);
|
|
547
|
+
const res = buffer.slice(0, outputLen);
|
|
548
|
+
this.destroy();
|
|
549
|
+
return res;
|
|
550
|
+
}
|
|
551
|
+
_cloneInto(to) {
|
|
552
|
+
to || (to = new this.constructor());
|
|
553
|
+
to.set(...this.get());
|
|
554
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
555
|
+
to.destroyed = destroyed;
|
|
556
|
+
to.finished = finished;
|
|
557
|
+
to.length = length;
|
|
558
|
+
to.pos = pos;
|
|
559
|
+
if (length % blockLen)
|
|
560
|
+
to.buffer.set(buffer);
|
|
561
|
+
return to;
|
|
562
|
+
}
|
|
563
|
+
clone() {
|
|
564
|
+
return this._cloneInto();
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
_md.HashMD = HashMD;
|
|
568
|
+
/**
|
|
569
|
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
570
|
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
571
|
+
*/
|
|
572
|
+
/** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
|
|
573
|
+
_md.SHA256_IV = Uint32Array.from([
|
|
574
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
575
|
+
]);
|
|
576
|
+
/** Initial SHA224 state. Bits 32..64 of frac part of sqrt of primes 23..53 */
|
|
577
|
+
_md.SHA224_IV = Uint32Array.from([
|
|
578
|
+
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,
|
|
579
|
+
]);
|
|
580
|
+
/** Initial SHA384 state. Bits 0..64 of frac part of sqrt of primes 23..53 */
|
|
581
|
+
_md.SHA384_IV = Uint32Array.from([
|
|
582
|
+
0xcbbb9d5d, 0xc1059ed8, 0x629a292a, 0x367cd507, 0x9159015a, 0x3070dd17, 0x152fecd8, 0xf70e5939,
|
|
583
|
+
0x67332667, 0xffc00b31, 0x8eb44a87, 0x68581511, 0xdb0c2e0d, 0x64f98fa7, 0x47b5481d, 0xbefa4fa4,
|
|
584
|
+
]);
|
|
585
|
+
/** Initial SHA512 state. Bits 0..64 of frac part of sqrt of primes 2..19 */
|
|
586
|
+
_md.SHA512_IV = Uint32Array.from([
|
|
587
|
+
0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
|
|
588
|
+
0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
|
|
589
|
+
]);
|
|
590
|
+
|
|
591
|
+
return _md;
|
|
342
592
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
593
|
+
|
|
594
|
+
var _u64 = {};
|
|
595
|
+
|
|
596
|
+
var hasRequired_u64;
|
|
597
|
+
|
|
598
|
+
function require_u64 () {
|
|
599
|
+
if (hasRequired_u64) return _u64;
|
|
600
|
+
hasRequired_u64 = 1;
|
|
601
|
+
Object.defineProperty(_u64, "__esModule", { value: true });
|
|
602
|
+
_u64.toBig = _u64.shrSL = _u64.shrSH = _u64.rotrSL = _u64.rotrSH = _u64.rotrBL = _u64.rotrBH = _u64.rotr32L = _u64.rotr32H = _u64.rotlSL = _u64.rotlSH = _u64.rotlBL = _u64.rotlBH = _u64.add5L = _u64.add5H = _u64.add4L = _u64.add4H = _u64.add3L = _u64.add3H = void 0;
|
|
603
|
+
_u64.add = add;
|
|
604
|
+
_u64.fromBig = fromBig;
|
|
605
|
+
_u64.split = split;
|
|
606
|
+
/**
|
|
607
|
+
* Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
|
|
608
|
+
* @todo re-check https://issues.chromium.org/issues/42212588
|
|
609
|
+
* @module
|
|
610
|
+
*/
|
|
611
|
+
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
612
|
+
const _32n = /* @__PURE__ */ BigInt(32);
|
|
613
|
+
function fromBig(n, le = false) {
|
|
614
|
+
if (le)
|
|
615
|
+
return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
|
|
616
|
+
return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
617
|
+
}
|
|
618
|
+
function split(lst, le = false) {
|
|
619
|
+
const len = lst.length;
|
|
620
|
+
let Ah = new Uint32Array(len);
|
|
621
|
+
let Al = new Uint32Array(len);
|
|
622
|
+
for (let i = 0; i < len; i++) {
|
|
623
|
+
const { h, l } = fromBig(lst[i], le);
|
|
624
|
+
[Ah[i], Al[i]] = [h, l];
|
|
625
|
+
}
|
|
626
|
+
return [Ah, Al];
|
|
627
|
+
}
|
|
628
|
+
const toBig = (h, l) => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
|
|
629
|
+
_u64.toBig = toBig;
|
|
630
|
+
// for Shift in [0, 32)
|
|
631
|
+
const shrSH = (h, _l, s) => h >>> s;
|
|
632
|
+
_u64.shrSH = shrSH;
|
|
633
|
+
const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
634
|
+
_u64.shrSL = shrSL;
|
|
635
|
+
// Right rotate for Shift in [1, 32)
|
|
636
|
+
const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
|
|
637
|
+
_u64.rotrSH = rotrSH;
|
|
638
|
+
const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
639
|
+
_u64.rotrSL = rotrSL;
|
|
640
|
+
// Right rotate for Shift in (32, 64), NOTE: 32 is special case.
|
|
641
|
+
const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
|
|
642
|
+
_u64.rotrBH = rotrBH;
|
|
643
|
+
const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
|
|
644
|
+
_u64.rotrBL = rotrBL;
|
|
645
|
+
// Right rotate for shift===32 (just swaps l&h)
|
|
646
|
+
const rotr32H = (_h, l) => l;
|
|
647
|
+
_u64.rotr32H = rotr32H;
|
|
648
|
+
const rotr32L = (h, _l) => h;
|
|
649
|
+
_u64.rotr32L = rotr32L;
|
|
650
|
+
// Left rotate for Shift in [1, 32)
|
|
651
|
+
const rotlSH = (h, l, s) => (h << s) | (l >>> (32 - s));
|
|
652
|
+
_u64.rotlSH = rotlSH;
|
|
653
|
+
const rotlSL = (h, l, s) => (l << s) | (h >>> (32 - s));
|
|
654
|
+
_u64.rotlSL = rotlSL;
|
|
655
|
+
// Left rotate for Shift in (32, 64), NOTE: 32 is special case.
|
|
656
|
+
const rotlBH = (h, l, s) => (l << (s - 32)) | (h >>> (64 - s));
|
|
657
|
+
_u64.rotlBH = rotlBH;
|
|
658
|
+
const rotlBL = (h, l, s) => (h << (s - 32)) | (l >>> (64 - s));
|
|
659
|
+
_u64.rotlBL = rotlBL;
|
|
660
|
+
// JS uses 32-bit signed integers for bitwise operations which means we cannot
|
|
661
|
+
// simple take carry out of low bit sum by shift, we need to use division.
|
|
662
|
+
function add(Ah, Al, Bh, Bl) {
|
|
663
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
664
|
+
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
|
665
|
+
}
|
|
666
|
+
// Addition with more than 2 elements
|
|
667
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
668
|
+
_u64.add3L = add3L;
|
|
669
|
+
const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
|
|
670
|
+
_u64.add3H = add3H;
|
|
671
|
+
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
672
|
+
_u64.add4L = add4L;
|
|
673
|
+
const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
|
|
674
|
+
_u64.add4H = add4H;
|
|
675
|
+
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
676
|
+
_u64.add5L = add5L;
|
|
677
|
+
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
|
678
|
+
_u64.add5H = add5H;
|
|
679
|
+
// prettier-ignore
|
|
680
|
+
const u64 = {
|
|
681
|
+
fromBig, split, toBig,
|
|
682
|
+
shrSH, shrSL,
|
|
683
|
+
rotrSH, rotrSL, rotrBH, rotrBL,
|
|
684
|
+
rotr32H, rotr32L,
|
|
685
|
+
rotlSH, rotlSL, rotlBH, rotlBL,
|
|
686
|
+
add, add3L, add3H, add4L, add4H, add5H, add5L,
|
|
687
|
+
};
|
|
688
|
+
_u64.default = u64;
|
|
689
|
+
|
|
690
|
+
return _u64;
|
|
427
691
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
692
|
+
|
|
693
|
+
var hasRequiredSha2;
|
|
694
|
+
|
|
695
|
+
function requireSha2 () {
|
|
696
|
+
if (hasRequiredSha2) return sha2;
|
|
697
|
+
hasRequiredSha2 = 1;
|
|
698
|
+
Object.defineProperty(sha2, "__esModule", { value: true });
|
|
699
|
+
sha2.sha512_224 = sha2.sha512_256 = sha2.sha384 = sha2.sha512 = sha2.sha224 = sha2.sha256 = sha2.SHA512_256 = sha2.SHA512_224 = sha2.SHA384 = sha2.SHA512 = sha2.SHA224 = sha2.SHA256 = void 0;
|
|
700
|
+
/**
|
|
701
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
702
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
703
|
+
* Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
|
|
704
|
+
* [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
|
705
|
+
* @module
|
|
706
|
+
*/
|
|
707
|
+
const _md_ts_1 = /*@__PURE__*/ require_md();
|
|
708
|
+
const u64 = /*@__PURE__*/ require_u64();
|
|
709
|
+
const utils_ts_1 = /*@__PURE__*/ requireUtils();
|
|
710
|
+
/**
|
|
711
|
+
* Round constants:
|
|
712
|
+
* First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
|
|
713
|
+
*/
|
|
714
|
+
// prettier-ignore
|
|
715
|
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
716
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
717
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
718
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
719
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
720
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
721
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
722
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
723
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
724
|
+
]);
|
|
725
|
+
/** Reusable temporary buffer. "W" comes straight from spec. */
|
|
726
|
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
727
|
+
class SHA256 extends _md_ts_1.HashMD {
|
|
728
|
+
constructor(outputLen = 32) {
|
|
729
|
+
super(64, outputLen, 8, false);
|
|
730
|
+
// We cannot use array here since array allows indexing by variable
|
|
731
|
+
// which means optimizer/compiler cannot use registers.
|
|
732
|
+
this.A = _md_ts_1.SHA256_IV[0] | 0;
|
|
733
|
+
this.B = _md_ts_1.SHA256_IV[1] | 0;
|
|
734
|
+
this.C = _md_ts_1.SHA256_IV[2] | 0;
|
|
735
|
+
this.D = _md_ts_1.SHA256_IV[3] | 0;
|
|
736
|
+
this.E = _md_ts_1.SHA256_IV[4] | 0;
|
|
737
|
+
this.F = _md_ts_1.SHA256_IV[5] | 0;
|
|
738
|
+
this.G = _md_ts_1.SHA256_IV[6] | 0;
|
|
739
|
+
this.H = _md_ts_1.SHA256_IV[7] | 0;
|
|
740
|
+
}
|
|
741
|
+
get() {
|
|
742
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
743
|
+
return [A, B, C, D, E, F, G, H];
|
|
744
|
+
}
|
|
745
|
+
// prettier-ignore
|
|
746
|
+
set(A, B, C, D, E, F, G, H) {
|
|
747
|
+
this.A = A | 0;
|
|
748
|
+
this.B = B | 0;
|
|
749
|
+
this.C = C | 0;
|
|
750
|
+
this.D = D | 0;
|
|
751
|
+
this.E = E | 0;
|
|
752
|
+
this.F = F | 0;
|
|
753
|
+
this.G = G | 0;
|
|
754
|
+
this.H = H | 0;
|
|
755
|
+
}
|
|
756
|
+
process(view, offset) {
|
|
757
|
+
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
758
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
759
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
760
|
+
for (let i = 16; i < 64; i++) {
|
|
761
|
+
const W15 = SHA256_W[i - 15];
|
|
762
|
+
const W2 = SHA256_W[i - 2];
|
|
763
|
+
const s0 = (0, utils_ts_1.rotr)(W15, 7) ^ (0, utils_ts_1.rotr)(W15, 18) ^ (W15 >>> 3);
|
|
764
|
+
const s1 = (0, utils_ts_1.rotr)(W2, 17) ^ (0, utils_ts_1.rotr)(W2, 19) ^ (W2 >>> 10);
|
|
765
|
+
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
766
|
+
}
|
|
767
|
+
// Compression function main loop, 64 rounds
|
|
768
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
769
|
+
for (let i = 0; i < 64; i++) {
|
|
770
|
+
const sigma1 = (0, utils_ts_1.rotr)(E, 6) ^ (0, utils_ts_1.rotr)(E, 11) ^ (0, utils_ts_1.rotr)(E, 25);
|
|
771
|
+
const T1 = (H + sigma1 + (0, _md_ts_1.Chi)(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
772
|
+
const sigma0 = (0, utils_ts_1.rotr)(A, 2) ^ (0, utils_ts_1.rotr)(A, 13) ^ (0, utils_ts_1.rotr)(A, 22);
|
|
773
|
+
const T2 = (sigma0 + (0, _md_ts_1.Maj)(A, B, C)) | 0;
|
|
774
|
+
H = G;
|
|
775
|
+
G = F;
|
|
776
|
+
F = E;
|
|
777
|
+
E = (D + T1) | 0;
|
|
778
|
+
D = C;
|
|
779
|
+
C = B;
|
|
780
|
+
B = A;
|
|
781
|
+
A = (T1 + T2) | 0;
|
|
782
|
+
}
|
|
783
|
+
// Add the compressed chunk to the current hash value
|
|
784
|
+
A = (A + this.A) | 0;
|
|
785
|
+
B = (B + this.B) | 0;
|
|
786
|
+
C = (C + this.C) | 0;
|
|
787
|
+
D = (D + this.D) | 0;
|
|
788
|
+
E = (E + this.E) | 0;
|
|
789
|
+
F = (F + this.F) | 0;
|
|
790
|
+
G = (G + this.G) | 0;
|
|
791
|
+
H = (H + this.H) | 0;
|
|
792
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
793
|
+
}
|
|
794
|
+
roundClean() {
|
|
795
|
+
(0, utils_ts_1.clean)(SHA256_W);
|
|
796
|
+
}
|
|
797
|
+
destroy() {
|
|
798
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
799
|
+
(0, utils_ts_1.clean)(this.buffer);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
sha2.SHA256 = SHA256;
|
|
803
|
+
class SHA224 extends SHA256 {
|
|
804
|
+
constructor() {
|
|
805
|
+
super(28);
|
|
806
|
+
this.A = _md_ts_1.SHA224_IV[0] | 0;
|
|
807
|
+
this.B = _md_ts_1.SHA224_IV[1] | 0;
|
|
808
|
+
this.C = _md_ts_1.SHA224_IV[2] | 0;
|
|
809
|
+
this.D = _md_ts_1.SHA224_IV[3] | 0;
|
|
810
|
+
this.E = _md_ts_1.SHA224_IV[4] | 0;
|
|
811
|
+
this.F = _md_ts_1.SHA224_IV[5] | 0;
|
|
812
|
+
this.G = _md_ts_1.SHA224_IV[6] | 0;
|
|
813
|
+
this.H = _md_ts_1.SHA224_IV[7] | 0;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
sha2.SHA224 = SHA224;
|
|
817
|
+
// SHA2-512 is slower than sha256 in js because u64 operations are slow.
|
|
818
|
+
// Round contants
|
|
819
|
+
// First 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409
|
|
820
|
+
// prettier-ignore
|
|
821
|
+
const K512 = /* @__PURE__ */ (() => u64.split([
|
|
822
|
+
'0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
|
|
823
|
+
'0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
|
|
824
|
+
'0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
|
|
825
|
+
'0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
|
|
826
|
+
'0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
|
|
827
|
+
'0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
|
|
828
|
+
'0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
|
|
829
|
+
'0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
|
|
830
|
+
'0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
|
|
831
|
+
'0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
|
|
832
|
+
'0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
|
|
833
|
+
'0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
|
|
834
|
+
'0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
|
|
835
|
+
'0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
|
|
836
|
+
'0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
|
|
837
|
+
'0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
|
|
838
|
+
'0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
|
|
839
|
+
'0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
|
|
840
|
+
'0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
|
|
841
|
+
'0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
|
|
842
|
+
].map(n => BigInt(n))))();
|
|
843
|
+
const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
844
|
+
const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
845
|
+
// Reusable temporary buffers
|
|
846
|
+
const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
847
|
+
const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
848
|
+
class SHA512 extends _md_ts_1.HashMD {
|
|
849
|
+
constructor(outputLen = 64) {
|
|
850
|
+
super(128, outputLen, 16, false);
|
|
851
|
+
// We cannot use array here since array allows indexing by variable
|
|
852
|
+
// which means optimizer/compiler cannot use registers.
|
|
853
|
+
// h -- high 32 bits, l -- low 32 bits
|
|
854
|
+
this.Ah = _md_ts_1.SHA512_IV[0] | 0;
|
|
855
|
+
this.Al = _md_ts_1.SHA512_IV[1] | 0;
|
|
856
|
+
this.Bh = _md_ts_1.SHA512_IV[2] | 0;
|
|
857
|
+
this.Bl = _md_ts_1.SHA512_IV[3] | 0;
|
|
858
|
+
this.Ch = _md_ts_1.SHA512_IV[4] | 0;
|
|
859
|
+
this.Cl = _md_ts_1.SHA512_IV[5] | 0;
|
|
860
|
+
this.Dh = _md_ts_1.SHA512_IV[6] | 0;
|
|
861
|
+
this.Dl = _md_ts_1.SHA512_IV[7] | 0;
|
|
862
|
+
this.Eh = _md_ts_1.SHA512_IV[8] | 0;
|
|
863
|
+
this.El = _md_ts_1.SHA512_IV[9] | 0;
|
|
864
|
+
this.Fh = _md_ts_1.SHA512_IV[10] | 0;
|
|
865
|
+
this.Fl = _md_ts_1.SHA512_IV[11] | 0;
|
|
866
|
+
this.Gh = _md_ts_1.SHA512_IV[12] | 0;
|
|
867
|
+
this.Gl = _md_ts_1.SHA512_IV[13] | 0;
|
|
868
|
+
this.Hh = _md_ts_1.SHA512_IV[14] | 0;
|
|
869
|
+
this.Hl = _md_ts_1.SHA512_IV[15] | 0;
|
|
870
|
+
}
|
|
871
|
+
// prettier-ignore
|
|
872
|
+
get() {
|
|
873
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
874
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
875
|
+
}
|
|
876
|
+
// prettier-ignore
|
|
877
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
878
|
+
this.Ah = Ah | 0;
|
|
879
|
+
this.Al = Al | 0;
|
|
880
|
+
this.Bh = Bh | 0;
|
|
881
|
+
this.Bl = Bl | 0;
|
|
882
|
+
this.Ch = Ch | 0;
|
|
883
|
+
this.Cl = Cl | 0;
|
|
884
|
+
this.Dh = Dh | 0;
|
|
885
|
+
this.Dl = Dl | 0;
|
|
886
|
+
this.Eh = Eh | 0;
|
|
887
|
+
this.El = El | 0;
|
|
888
|
+
this.Fh = Fh | 0;
|
|
889
|
+
this.Fl = Fl | 0;
|
|
890
|
+
this.Gh = Gh | 0;
|
|
891
|
+
this.Gl = Gl | 0;
|
|
892
|
+
this.Hh = Hh | 0;
|
|
893
|
+
this.Hl = Hl | 0;
|
|
894
|
+
}
|
|
895
|
+
process(view, offset) {
|
|
896
|
+
// Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
|
|
897
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
898
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
899
|
+
SHA512_W_L[i] = view.getUint32((offset += 4));
|
|
900
|
+
}
|
|
901
|
+
for (let i = 16; i < 80; i++) {
|
|
902
|
+
// s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
|
|
903
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
904
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
905
|
+
const s0h = u64.rotrSH(W15h, W15l, 1) ^ u64.rotrSH(W15h, W15l, 8) ^ u64.shrSH(W15h, W15l, 7);
|
|
906
|
+
const s0l = u64.rotrSL(W15h, W15l, 1) ^ u64.rotrSL(W15h, W15l, 8) ^ u64.shrSL(W15h, W15l, 7);
|
|
907
|
+
// s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
|
|
908
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
909
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
910
|
+
const s1h = u64.rotrSH(W2h, W2l, 19) ^ u64.rotrBH(W2h, W2l, 61) ^ u64.shrSH(W2h, W2l, 6);
|
|
911
|
+
const s1l = u64.rotrSL(W2h, W2l, 19) ^ u64.rotrBL(W2h, W2l, 61) ^ u64.shrSL(W2h, W2l, 6);
|
|
912
|
+
// SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
|
|
913
|
+
const SUMl = u64.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
914
|
+
const SUMh = u64.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
915
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
916
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
917
|
+
}
|
|
918
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
919
|
+
// Compression function main loop, 80 rounds
|
|
920
|
+
for (let i = 0; i < 80; i++) {
|
|
921
|
+
// S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
|
|
922
|
+
const sigma1h = u64.rotrSH(Eh, El, 14) ^ u64.rotrSH(Eh, El, 18) ^ u64.rotrBH(Eh, El, 41);
|
|
923
|
+
const sigma1l = u64.rotrSL(Eh, El, 14) ^ u64.rotrSL(Eh, El, 18) ^ u64.rotrBL(Eh, El, 41);
|
|
924
|
+
//const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
925
|
+
const CHIh = (Eh & Fh) ^ (~Eh & Gh);
|
|
926
|
+
const CHIl = (El & Fl) ^ (~El & Gl);
|
|
927
|
+
// T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
|
|
928
|
+
// prettier-ignore
|
|
929
|
+
const T1ll = u64.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
930
|
+
const T1h = u64.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
931
|
+
const T1l = T1ll | 0;
|
|
932
|
+
// S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
|
|
933
|
+
const sigma0h = u64.rotrSH(Ah, Al, 28) ^ u64.rotrBH(Ah, Al, 34) ^ u64.rotrBH(Ah, Al, 39);
|
|
934
|
+
const sigma0l = u64.rotrSL(Ah, Al, 28) ^ u64.rotrBL(Ah, Al, 34) ^ u64.rotrBL(Ah, Al, 39);
|
|
935
|
+
const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
|
|
936
|
+
const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
|
|
937
|
+
Hh = Gh | 0;
|
|
938
|
+
Hl = Gl | 0;
|
|
939
|
+
Gh = Fh | 0;
|
|
940
|
+
Gl = Fl | 0;
|
|
941
|
+
Fh = Eh | 0;
|
|
942
|
+
Fl = El | 0;
|
|
943
|
+
({ h: Eh, l: El } = u64.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
944
|
+
Dh = Ch | 0;
|
|
945
|
+
Dl = Cl | 0;
|
|
946
|
+
Ch = Bh | 0;
|
|
947
|
+
Cl = Bl | 0;
|
|
948
|
+
Bh = Ah | 0;
|
|
949
|
+
Bl = Al | 0;
|
|
950
|
+
const All = u64.add3L(T1l, sigma0l, MAJl);
|
|
951
|
+
Ah = u64.add3H(All, T1h, sigma0h, MAJh);
|
|
952
|
+
Al = All | 0;
|
|
953
|
+
}
|
|
954
|
+
// Add the compressed chunk to the current hash value
|
|
955
|
+
({ h: Ah, l: Al } = u64.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
956
|
+
({ h: Bh, l: Bl } = u64.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
957
|
+
({ h: Ch, l: Cl } = u64.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
958
|
+
({ h: Dh, l: Dl } = u64.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
959
|
+
({ h: Eh, l: El } = u64.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
960
|
+
({ h: Fh, l: Fl } = u64.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
961
|
+
({ h: Gh, l: Gl } = u64.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
962
|
+
({ h: Hh, l: Hl } = u64.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
963
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
964
|
+
}
|
|
965
|
+
roundClean() {
|
|
966
|
+
(0, utils_ts_1.clean)(SHA512_W_H, SHA512_W_L);
|
|
967
|
+
}
|
|
968
|
+
destroy() {
|
|
969
|
+
(0, utils_ts_1.clean)(this.buffer);
|
|
970
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
sha2.SHA512 = SHA512;
|
|
974
|
+
class SHA384 extends SHA512 {
|
|
975
|
+
constructor() {
|
|
976
|
+
super(48);
|
|
977
|
+
this.Ah = _md_ts_1.SHA384_IV[0] | 0;
|
|
978
|
+
this.Al = _md_ts_1.SHA384_IV[1] | 0;
|
|
979
|
+
this.Bh = _md_ts_1.SHA384_IV[2] | 0;
|
|
980
|
+
this.Bl = _md_ts_1.SHA384_IV[3] | 0;
|
|
981
|
+
this.Ch = _md_ts_1.SHA384_IV[4] | 0;
|
|
982
|
+
this.Cl = _md_ts_1.SHA384_IV[5] | 0;
|
|
983
|
+
this.Dh = _md_ts_1.SHA384_IV[6] | 0;
|
|
984
|
+
this.Dl = _md_ts_1.SHA384_IV[7] | 0;
|
|
985
|
+
this.Eh = _md_ts_1.SHA384_IV[8] | 0;
|
|
986
|
+
this.El = _md_ts_1.SHA384_IV[9] | 0;
|
|
987
|
+
this.Fh = _md_ts_1.SHA384_IV[10] | 0;
|
|
988
|
+
this.Fl = _md_ts_1.SHA384_IV[11] | 0;
|
|
989
|
+
this.Gh = _md_ts_1.SHA384_IV[12] | 0;
|
|
990
|
+
this.Gl = _md_ts_1.SHA384_IV[13] | 0;
|
|
991
|
+
this.Hh = _md_ts_1.SHA384_IV[14] | 0;
|
|
992
|
+
this.Hl = _md_ts_1.SHA384_IV[15] | 0;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
sha2.SHA384 = SHA384;
|
|
996
|
+
/**
|
|
997
|
+
* Truncated SHA512/256 and SHA512/224.
|
|
998
|
+
* SHA512_IV is XORed with 0xa5a5a5a5a5a5a5a5, then used as "intermediary" IV of SHA512/t.
|
|
999
|
+
* Then t hashes string to produce result IV.
|
|
1000
|
+
* See `test/misc/sha2-gen-iv.js`.
|
|
1001
|
+
*/
|
|
1002
|
+
/** SHA512/224 IV */
|
|
1003
|
+
const T224_IV = /* @__PURE__ */ Uint32Array.from([
|
|
1004
|
+
0x8c3d37c8, 0x19544da2, 0x73e19966, 0x89dcd4d6, 0x1dfab7ae, 0x32ff9c82, 0x679dd514, 0x582f9fcf,
|
|
1005
|
+
0x0f6d2b69, 0x7bd44da8, 0x77e36f73, 0x04c48942, 0x3f9d85a8, 0x6a1d36c8, 0x1112e6ad, 0x91d692a1,
|
|
1006
|
+
]);
|
|
1007
|
+
/** SHA512/256 IV */
|
|
1008
|
+
const T256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
1009
|
+
0x22312194, 0xfc2bf72c, 0x9f555fa3, 0xc84c64c2, 0x2393b86b, 0x6f53b151, 0x96387719, 0x5940eabd,
|
|
1010
|
+
0x96283ee2, 0xa88effe3, 0xbe5e1e25, 0x53863992, 0x2b0199fc, 0x2c85b8aa, 0x0eb72ddc, 0x81c52ca2,
|
|
1011
|
+
]);
|
|
1012
|
+
class SHA512_224 extends SHA512 {
|
|
1013
|
+
constructor() {
|
|
1014
|
+
super(28);
|
|
1015
|
+
this.Ah = T224_IV[0] | 0;
|
|
1016
|
+
this.Al = T224_IV[1] | 0;
|
|
1017
|
+
this.Bh = T224_IV[2] | 0;
|
|
1018
|
+
this.Bl = T224_IV[3] | 0;
|
|
1019
|
+
this.Ch = T224_IV[4] | 0;
|
|
1020
|
+
this.Cl = T224_IV[5] | 0;
|
|
1021
|
+
this.Dh = T224_IV[6] | 0;
|
|
1022
|
+
this.Dl = T224_IV[7] | 0;
|
|
1023
|
+
this.Eh = T224_IV[8] | 0;
|
|
1024
|
+
this.El = T224_IV[9] | 0;
|
|
1025
|
+
this.Fh = T224_IV[10] | 0;
|
|
1026
|
+
this.Fl = T224_IV[11] | 0;
|
|
1027
|
+
this.Gh = T224_IV[12] | 0;
|
|
1028
|
+
this.Gl = T224_IV[13] | 0;
|
|
1029
|
+
this.Hh = T224_IV[14] | 0;
|
|
1030
|
+
this.Hl = T224_IV[15] | 0;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
sha2.SHA512_224 = SHA512_224;
|
|
1034
|
+
class SHA512_256 extends SHA512 {
|
|
1035
|
+
constructor() {
|
|
1036
|
+
super(32);
|
|
1037
|
+
this.Ah = T256_IV[0] | 0;
|
|
1038
|
+
this.Al = T256_IV[1] | 0;
|
|
1039
|
+
this.Bh = T256_IV[2] | 0;
|
|
1040
|
+
this.Bl = T256_IV[3] | 0;
|
|
1041
|
+
this.Ch = T256_IV[4] | 0;
|
|
1042
|
+
this.Cl = T256_IV[5] | 0;
|
|
1043
|
+
this.Dh = T256_IV[6] | 0;
|
|
1044
|
+
this.Dl = T256_IV[7] | 0;
|
|
1045
|
+
this.Eh = T256_IV[8] | 0;
|
|
1046
|
+
this.El = T256_IV[9] | 0;
|
|
1047
|
+
this.Fh = T256_IV[10] | 0;
|
|
1048
|
+
this.Fl = T256_IV[11] | 0;
|
|
1049
|
+
this.Gh = T256_IV[12] | 0;
|
|
1050
|
+
this.Gl = T256_IV[13] | 0;
|
|
1051
|
+
this.Hh = T256_IV[14] | 0;
|
|
1052
|
+
this.Hl = T256_IV[15] | 0;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
sha2.SHA512_256 = SHA512_256;
|
|
1056
|
+
/**
|
|
1057
|
+
* SHA2-256 hash function from RFC 4634.
|
|
1058
|
+
*
|
|
1059
|
+
* It is the fastest JS hash, even faster than Blake3.
|
|
1060
|
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
|
1061
|
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
1062
|
+
*/
|
|
1063
|
+
sha2.sha256 = (0, utils_ts_1.createHasher)(() => new SHA256());
|
|
1064
|
+
/** SHA2-224 hash function from RFC 4634 */
|
|
1065
|
+
sha2.sha224 = (0, utils_ts_1.createHasher)(() => new SHA224());
|
|
1066
|
+
/** SHA2-512 hash function from RFC 4634. */
|
|
1067
|
+
sha2.sha512 = (0, utils_ts_1.createHasher)(() => new SHA512());
|
|
1068
|
+
/** SHA2-384 hash function from RFC 4634. */
|
|
1069
|
+
sha2.sha384 = (0, utils_ts_1.createHasher)(() => new SHA384());
|
|
1070
|
+
/**
|
|
1071
|
+
* SHA2-512/256 "truncated" hash function, with improved resistance to length extension attacks.
|
|
1072
|
+
* See the paper on [truncated SHA512](https://eprint.iacr.org/2010/548.pdf).
|
|
1073
|
+
*/
|
|
1074
|
+
sha2.sha512_256 = (0, utils_ts_1.createHasher)(() => new SHA512_256());
|
|
1075
|
+
/**
|
|
1076
|
+
* SHA2-512/224 "truncated" hash function, with improved resistance to length extension attacks.
|
|
1077
|
+
* See the paper on [truncated SHA512](https://eprint.iacr.org/2010/548.pdf).
|
|
1078
|
+
*/
|
|
1079
|
+
sha2.sha512_224 = (0, utils_ts_1.createHasher)(() => new SHA512_224());
|
|
1080
|
+
|
|
1081
|
+
return sha2;
|
|
710
1082
|
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
1083
|
+
|
|
1084
|
+
var hasRequiredSha256;
|
|
1085
|
+
|
|
1086
|
+
function requireSha256 () {
|
|
1087
|
+
if (hasRequiredSha256) return sha256;
|
|
1088
|
+
hasRequiredSha256 = 1;
|
|
1089
|
+
Object.defineProperty(sha256, "__esModule", { value: true });
|
|
1090
|
+
sha256.sha224 = sha256.SHA224 = sha256.sha256 = sha256.SHA256 = void 0;
|
|
1091
|
+
/**
|
|
1092
|
+
* SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
|
|
1093
|
+
*
|
|
1094
|
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
|
1095
|
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
1096
|
+
*
|
|
1097
|
+
* Check out [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
|
1098
|
+
* @module
|
|
1099
|
+
* @deprecated
|
|
1100
|
+
*/
|
|
1101
|
+
const sha2_ts_1 = /*@__PURE__*/ requireSha2();
|
|
1102
|
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
|
1103
|
+
sha256.SHA256 = sha2_ts_1.SHA256;
|
|
1104
|
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
|
1105
|
+
sha256.sha256 = sha2_ts_1.sha256;
|
|
1106
|
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
|
1107
|
+
sha256.SHA224 = sha2_ts_1.SHA224;
|
|
1108
|
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
|
1109
|
+
sha256.sha224 = sha2_ts_1.sha224;
|
|
1110
|
+
|
|
1111
|
+
return sha256;
|
|
717
1112
|
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1113
|
+
|
|
1114
|
+
var src;
|
|
1115
|
+
var hasRequiredSrc;
|
|
1116
|
+
|
|
1117
|
+
function requireSrc () {
|
|
1118
|
+
if (hasRequiredSrc) return src;
|
|
1119
|
+
hasRequiredSrc = 1;
|
|
1120
|
+
// base-x encoding / decoding
|
|
1121
|
+
// Copyright (c) 2018 base-x contributors
|
|
1122
|
+
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
|
1123
|
+
// Distributed under the MIT software license, see the accompanying
|
|
1124
|
+
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
1125
|
+
function base (ALPHABET) {
|
|
1126
|
+
if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
|
|
1127
|
+
var BASE_MAP = new Uint8Array(256);
|
|
1128
|
+
for (var j = 0; j < BASE_MAP.length; j++) {
|
|
1129
|
+
BASE_MAP[j] = 255;
|
|
1130
|
+
}
|
|
1131
|
+
for (var i = 0; i < ALPHABET.length; i++) {
|
|
1132
|
+
var x = ALPHABET.charAt(i);
|
|
1133
|
+
var xc = x.charCodeAt(0);
|
|
1134
|
+
if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
|
|
1135
|
+
BASE_MAP[xc] = i;
|
|
1136
|
+
}
|
|
1137
|
+
var BASE = ALPHABET.length;
|
|
1138
|
+
var LEADER = ALPHABET.charAt(0);
|
|
1139
|
+
var FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
|
|
1140
|
+
var iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
|
|
1141
|
+
function encode (source) {
|
|
1142
|
+
if (source instanceof Uint8Array) ; else if (ArrayBuffer.isView(source)) {
|
|
1143
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
1144
|
+
} else if (Array.isArray(source)) {
|
|
1145
|
+
source = Uint8Array.from(source);
|
|
1146
|
+
}
|
|
1147
|
+
if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }
|
|
1148
|
+
if (source.length === 0) { return '' }
|
|
1149
|
+
// Skip & count leading zeroes.
|
|
1150
|
+
var zeroes = 0;
|
|
1151
|
+
var length = 0;
|
|
1152
|
+
var pbegin = 0;
|
|
1153
|
+
var pend = source.length;
|
|
1154
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
|
1155
|
+
pbegin++;
|
|
1156
|
+
zeroes++;
|
|
1157
|
+
}
|
|
1158
|
+
// Allocate enough space in big-endian base58 representation.
|
|
1159
|
+
var size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
|
|
1160
|
+
var b58 = new Uint8Array(size);
|
|
1161
|
+
// Process the bytes.
|
|
1162
|
+
while (pbegin !== pend) {
|
|
1163
|
+
var carry = source[pbegin];
|
|
1164
|
+
// Apply "b58 = b58 * 256 + ch".
|
|
1165
|
+
var i = 0;
|
|
1166
|
+
for (var it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
|
|
1167
|
+
carry += (256 * b58[it1]) >>> 0;
|
|
1168
|
+
b58[it1] = (carry % BASE) >>> 0;
|
|
1169
|
+
carry = (carry / BASE) >>> 0;
|
|
1170
|
+
}
|
|
1171
|
+
if (carry !== 0) { throw new Error('Non-zero carry') }
|
|
1172
|
+
length = i;
|
|
1173
|
+
pbegin++;
|
|
1174
|
+
}
|
|
1175
|
+
// Skip leading zeroes in base58 result.
|
|
1176
|
+
var it2 = size - length;
|
|
1177
|
+
while (it2 !== size && b58[it2] === 0) {
|
|
1178
|
+
it2++;
|
|
1179
|
+
}
|
|
1180
|
+
// Translate the result into a string.
|
|
1181
|
+
var str = LEADER.repeat(zeroes);
|
|
1182
|
+
for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]); }
|
|
1183
|
+
return str
|
|
1184
|
+
}
|
|
1185
|
+
function decodeUnsafe (source) {
|
|
1186
|
+
if (typeof source !== 'string') { throw new TypeError('Expected String') }
|
|
1187
|
+
if (source.length === 0) { return new Uint8Array() }
|
|
1188
|
+
var psz = 0;
|
|
1189
|
+
// Skip and count leading '1's.
|
|
1190
|
+
var zeroes = 0;
|
|
1191
|
+
var length = 0;
|
|
1192
|
+
while (source[psz] === LEADER) {
|
|
1193
|
+
zeroes++;
|
|
1194
|
+
psz++;
|
|
1195
|
+
}
|
|
1196
|
+
// Allocate enough space in big-endian base256 representation.
|
|
1197
|
+
var size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
|
|
1198
|
+
var b256 = new Uint8Array(size);
|
|
1199
|
+
// Process the characters.
|
|
1200
|
+
while (source[psz]) {
|
|
1201
|
+
// Find code of next character
|
|
1202
|
+
var charCode = source.charCodeAt(psz);
|
|
1203
|
+
// Base map can not be indexed using char code
|
|
1204
|
+
if (charCode > 255) { return }
|
|
1205
|
+
// Decode character
|
|
1206
|
+
var carry = BASE_MAP[charCode];
|
|
1207
|
+
// Invalid character
|
|
1208
|
+
if (carry === 255) { return }
|
|
1209
|
+
var i = 0;
|
|
1210
|
+
for (var it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
|
|
1211
|
+
carry += (BASE * b256[it3]) >>> 0;
|
|
1212
|
+
b256[it3] = (carry % 256) >>> 0;
|
|
1213
|
+
carry = (carry / 256) >>> 0;
|
|
1214
|
+
}
|
|
1215
|
+
if (carry !== 0) { throw new Error('Non-zero carry') }
|
|
1216
|
+
length = i;
|
|
1217
|
+
psz++;
|
|
1218
|
+
}
|
|
1219
|
+
// Skip leading zeroes in b256.
|
|
1220
|
+
var it4 = size - length;
|
|
1221
|
+
while (it4 !== size && b256[it4] === 0) {
|
|
1222
|
+
it4++;
|
|
1223
|
+
}
|
|
1224
|
+
var vch = new Uint8Array(zeroes + (size - it4));
|
|
1225
|
+
var j = zeroes;
|
|
1226
|
+
while (it4 !== size) {
|
|
1227
|
+
vch[j++] = b256[it4++];
|
|
1228
|
+
}
|
|
1229
|
+
return vch
|
|
1230
|
+
}
|
|
1231
|
+
function decode (string) {
|
|
1232
|
+
var buffer = decodeUnsafe(string);
|
|
1233
|
+
if (buffer) { return buffer }
|
|
1234
|
+
throw new Error('Non-base' + BASE + ' character')
|
|
1235
|
+
}
|
|
1236
|
+
return {
|
|
1237
|
+
encode: encode,
|
|
1238
|
+
decodeUnsafe: decodeUnsafe,
|
|
1239
|
+
decode: decode
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
src = base;
|
|
1243
|
+
return src;
|
|
793
1244
|
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
1245
|
+
|
|
1246
|
+
var bs58;
|
|
1247
|
+
var hasRequiredBs58;
|
|
1248
|
+
|
|
1249
|
+
function requireBs58 () {
|
|
1250
|
+
if (hasRequiredBs58) return bs58;
|
|
1251
|
+
hasRequiredBs58 = 1;
|
|
1252
|
+
const basex = requireSrc();
|
|
1253
|
+
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
1254
|
+
|
|
1255
|
+
bs58 = basex(ALPHABET);
|
|
1256
|
+
return bs58;
|
|
797
1257
|
}
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
1258
|
+
|
|
1259
|
+
var base;
|
|
1260
|
+
var hasRequiredBase;
|
|
1261
|
+
|
|
1262
|
+
function requireBase () {
|
|
1263
|
+
if (hasRequiredBase) return base;
|
|
1264
|
+
hasRequiredBase = 1;
|
|
1265
|
+
|
|
1266
|
+
var base58 = requireBs58();
|
|
1267
|
+
|
|
1268
|
+
base = function (checksumFn) {
|
|
1269
|
+
// Encode a buffer as a base58-check encoded string
|
|
1270
|
+
function encode (payload) {
|
|
1271
|
+
var payloadU8 = Uint8Array.from(payload);
|
|
1272
|
+
var checksum = checksumFn(payloadU8);
|
|
1273
|
+
var length = payloadU8.length + 4;
|
|
1274
|
+
var both = new Uint8Array(length);
|
|
1275
|
+
both.set(payloadU8, 0);
|
|
1276
|
+
both.set(checksum.subarray(0, 4), payloadU8.length);
|
|
1277
|
+
return base58.encode(both, length)
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
function decodeRaw (buffer) {
|
|
1281
|
+
var payload = buffer.slice(0, -4);
|
|
1282
|
+
var checksum = buffer.slice(-4);
|
|
1283
|
+
var newChecksum = checksumFn(payload);
|
|
1284
|
+
|
|
1285
|
+
if (checksum[0] ^ newChecksum[0] |
|
|
1286
|
+
checksum[1] ^ newChecksum[1] |
|
|
1287
|
+
checksum[2] ^ newChecksum[2] |
|
|
1288
|
+
checksum[3] ^ newChecksum[3]) return
|
|
1289
|
+
|
|
1290
|
+
return payload
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
// Decode a base58-check encoded string to a buffer, no result if checksum is wrong
|
|
1294
|
+
function decodeUnsafe (string) {
|
|
1295
|
+
var buffer = base58.decodeUnsafe(string);
|
|
1296
|
+
if (!buffer) return
|
|
1297
|
+
|
|
1298
|
+
return decodeRaw(buffer)
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
function decode (string) {
|
|
1302
|
+
var buffer = base58.decode(string);
|
|
1303
|
+
var payload = decodeRaw(buffer);
|
|
1304
|
+
if (!payload) throw new Error('Invalid checksum')
|
|
1305
|
+
return payload
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
return {
|
|
1309
|
+
encode: encode,
|
|
1310
|
+
decode: decode,
|
|
1311
|
+
decodeUnsafe: decodeUnsafe
|
|
1312
|
+
}
|
|
1313
|
+
};
|
|
1314
|
+
return base;
|
|
829
1315
|
}
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
1316
|
+
|
|
1317
|
+
var bs58check$1;
|
|
1318
|
+
var hasRequiredBs58check;
|
|
1319
|
+
|
|
1320
|
+
function requireBs58check () {
|
|
1321
|
+
if (hasRequiredBs58check) return bs58check$1;
|
|
1322
|
+
hasRequiredBs58check = 1;
|
|
1323
|
+
|
|
1324
|
+
var { sha256 } = /*@__PURE__*/ requireSha256();
|
|
1325
|
+
var bs58checkBase = requireBase();
|
|
1326
|
+
|
|
1327
|
+
// SHA256(SHA256(buffer))
|
|
1328
|
+
function sha256x2 (buffer) {
|
|
1329
|
+
return sha256(sha256(buffer))
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
bs58check$1 = bs58checkBase(sha256x2);
|
|
1333
|
+
return bs58check$1;
|
|
839
1334
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
1335
|
+
|
|
1336
|
+
var bs58checkExports = requireBs58check();
|
|
1337
|
+
const bs58check = /*@__PURE__*/getDefaultExportFromCjs(bs58checkExports);
|
|
1338
|
+
|
|
1339
|
+
const uint8ArrayToHexString = (data) => {
|
|
1340
|
+
return Array.from(data, byte => byte.toString(16).padStart(2, "0")).join("");
|
|
1341
|
+
};
|
|
1342
|
+
|
|
1343
|
+
const urlPrefix = "automerge:";
|
|
1344
|
+
/** Given an Automerge URL, returns the DocumentId in both base58check-encoded form and binary form */
|
|
1345
|
+
const parseAutomergeUrl = (url) => {
|
|
1346
|
+
const [baseUrl, headsSection, ...rest] = url.split("#");
|
|
1347
|
+
if (rest.length > 0) {
|
|
1348
|
+
throw new Error("Invalid URL: contains multiple heads sections");
|
|
1349
|
+
}
|
|
1350
|
+
const regex = new RegExp(`^${urlPrefix}(\\w+)$`);
|
|
1351
|
+
const [, docMatch] = baseUrl.match(regex) || [];
|
|
1352
|
+
const documentId = docMatch;
|
|
1353
|
+
const binaryDocumentId = documentIdToBinary(documentId);
|
|
1354
|
+
if (!binaryDocumentId)
|
|
1355
|
+
throw new Error("Invalid document URL: " + url);
|
|
1356
|
+
if (headsSection === undefined)
|
|
1357
|
+
return { binaryDocumentId, documentId };
|
|
1358
|
+
const heads = (headsSection === "" ? [] : headsSection.split("|"));
|
|
1359
|
+
const hexHeads = heads.map(head => {
|
|
1360
|
+
try {
|
|
1361
|
+
return uint8ArrayToHexString(bs58check.decode(head));
|
|
1362
|
+
}
|
|
1363
|
+
catch (e) {
|
|
1364
|
+
throw new Error(`Invalid head in URL: ${head}`);
|
|
1365
|
+
}
|
|
1366
|
+
});
|
|
1367
|
+
return { binaryDocumentId, hexHeads, documentId, heads };
|
|
1368
|
+
};
|
|
1369
|
+
/**
|
|
1370
|
+
* Given a documentId in either binary or base58check-encoded form, returns an Automerge URL.
|
|
1371
|
+
* Throws on invalid input.
|
|
1372
|
+
*/
|
|
1373
|
+
const stringifyAutomergeUrl = (arg) => {
|
|
1374
|
+
if (arg instanceof Uint8Array || typeof arg === "string") {
|
|
1375
|
+
return (urlPrefix +
|
|
1376
|
+
(arg instanceof Uint8Array
|
|
1377
|
+
? binaryToDocumentId(arg)
|
|
1378
|
+
: arg));
|
|
1379
|
+
}
|
|
1380
|
+
const { documentId, heads = undefined } = arg;
|
|
1381
|
+
if (documentId === undefined)
|
|
1382
|
+
throw new Error("Invalid documentId: " + documentId);
|
|
1383
|
+
const encodedDocumentId = documentId instanceof Uint8Array
|
|
1384
|
+
? binaryToDocumentId(documentId)
|
|
1385
|
+
: documentId;
|
|
1386
|
+
let url = `${urlPrefix}${encodedDocumentId}`;
|
|
1387
|
+
if (heads !== undefined) {
|
|
1388
|
+
heads.forEach(head => {
|
|
1389
|
+
try {
|
|
1390
|
+
bs58check.decode(head);
|
|
1391
|
+
}
|
|
1392
|
+
catch (e) {
|
|
1393
|
+
throw new Error(`Invalid head: ${head}`);
|
|
1394
|
+
}
|
|
1395
|
+
});
|
|
1396
|
+
url += "#" + heads.join("|");
|
|
1397
|
+
}
|
|
1398
|
+
return url;
|
|
1399
|
+
};
|
|
1400
|
+
const anyDocumentIdToAutomergeUrl = (id) => isValidAutomergeUrl(id)
|
|
1401
|
+
? id
|
|
1402
|
+
: isValidDocumentId(id)
|
|
1403
|
+
? stringifyAutomergeUrl({ documentId: id })
|
|
1404
|
+
: isValidUuid(id)
|
|
1405
|
+
? parseLegacyUUID(id)
|
|
1406
|
+
: undefined;
|
|
1407
|
+
/**
|
|
1408
|
+
* Given a string, returns true if it is a valid Automerge URL. This function also acts as a type
|
|
1409
|
+
* discriminator in Typescript.
|
|
1410
|
+
*/
|
|
1411
|
+
const isValidAutomergeUrl = (str) => {
|
|
1412
|
+
if (typeof str !== "string" || !str || !str.startsWith(urlPrefix))
|
|
1413
|
+
return false;
|
|
867
1414
|
try {
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
1415
|
+
const { documentId, heads } = parseAutomergeUrl(str);
|
|
1416
|
+
if (!isValidDocumentId(documentId))
|
|
1417
|
+
return false;
|
|
1418
|
+
if (heads &&
|
|
1419
|
+
!heads.every(head => {
|
|
1420
|
+
try {
|
|
1421
|
+
bs58check.decode(head);
|
|
1422
|
+
return true;
|
|
1423
|
+
}
|
|
1424
|
+
catch {
|
|
1425
|
+
return false;
|
|
1426
|
+
}
|
|
1427
|
+
}))
|
|
1428
|
+
return false;
|
|
1429
|
+
return true;
|
|
1430
|
+
}
|
|
1431
|
+
catch {
|
|
1432
|
+
return false;
|
|
871
1433
|
}
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
const
|
|
878
|
-
return
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
let a = (
|
|
1434
|
+
};
|
|
1435
|
+
const isValidDocumentId = (str) => {
|
|
1436
|
+
if (typeof str !== "string")
|
|
1437
|
+
return false;
|
|
1438
|
+
// try to decode from base58
|
|
1439
|
+
const binaryDocumentID = documentIdToBinary(str);
|
|
1440
|
+
return binaryDocumentID !== undefined;
|
|
1441
|
+
};
|
|
1442
|
+
const isValidUuid = (str) => typeof str === "string" && validate(str);
|
|
1443
|
+
const documentIdToBinary = (docId) => bs58check.decodeUnsafe(docId);
|
|
1444
|
+
const binaryToDocumentId = (docId) => bs58check.encode(docId);
|
|
1445
|
+
const parseLegacyUUID = (str) => {
|
|
1446
|
+
if (!validate(str))
|
|
1447
|
+
return undefined;
|
|
1448
|
+
const documentId = parse(str);
|
|
1449
|
+
return stringifyAutomergeUrl({ documentId });
|
|
1450
|
+
};
|
|
1451
|
+
|
|
1452
|
+
const wrapperCache = /* @__PURE__ */ new Map();
|
|
1453
|
+
function useDocHandle(id, { suspense } = { suspense: false }) {
|
|
1454
|
+
const repo = useRepo();
|
|
1455
|
+
const controllerRef = useRef();
|
|
1456
|
+
const [handle, setHandle] = useState();
|
|
1457
|
+
let currentHandle = (
|
|
897
1458
|
// make sure the handle matches the id
|
|
898
|
-
|
|
1459
|
+
id && handle && handle.url === anyDocumentIdToAutomergeUrl(id) ? handle : void 0
|
|
899
1460
|
);
|
|
900
|
-
if (
|
|
901
|
-
const
|
|
902
|
-
|
|
1461
|
+
if (id && !currentHandle) {
|
|
1462
|
+
const progress = repo.findWithProgress(id);
|
|
1463
|
+
if (progress.state === "ready") {
|
|
1464
|
+
currentHandle = progress.handle;
|
|
1465
|
+
}
|
|
903
1466
|
}
|
|
904
|
-
let
|
|
905
|
-
if (!
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
1467
|
+
let wrapper = id ? wrapperCache.get(id) : void 0;
|
|
1468
|
+
if (!wrapper && id) {
|
|
1469
|
+
controllerRef.current?.abort();
|
|
1470
|
+
controllerRef.current = new AbortController();
|
|
1471
|
+
const promise = repo.find(id, { signal: controllerRef.current.signal });
|
|
1472
|
+
wrapper = wrapPromise(promise);
|
|
1473
|
+
wrapperCache.set(id, wrapper);
|
|
909
1474
|
}
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
1475
|
+
useEffect(() => {
|
|
1476
|
+
if (currentHandle || suspense || !wrapper) {
|
|
1477
|
+
return;
|
|
1478
|
+
}
|
|
1479
|
+
wrapper.promise.then((handle2) => {
|
|
1480
|
+
setHandle(handle2);
|
|
913
1481
|
}).catch(() => {
|
|
914
|
-
|
|
1482
|
+
setHandle(void 0);
|
|
915
1483
|
});
|
|
916
|
-
}, [
|
|
1484
|
+
}, [currentHandle, suspense, wrapper]);
|
|
1485
|
+
if (currentHandle || !suspense || !wrapper) {
|
|
1486
|
+
return currentHandle;
|
|
1487
|
+
}
|
|
1488
|
+
return wrapper.read();
|
|
917
1489
|
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
1490
|
+
|
|
1491
|
+
function useDocument(id, params = { suspense: false }) {
|
|
1492
|
+
const handle = useDocHandle(id, params);
|
|
1493
|
+
const [doc, setDoc] = useState(() => handle?.doc());
|
|
1494
|
+
const [deleteError, setDeleteError] = useState();
|
|
1495
|
+
useEffect(() => {
|
|
1496
|
+
setDoc(handle?.doc());
|
|
1497
|
+
}, [handle]);
|
|
1498
|
+
useEffect(() => {
|
|
1499
|
+
if (!handle) {
|
|
924
1500
|
return;
|
|
925
|
-
|
|
926
|
-
|
|
1501
|
+
}
|
|
1502
|
+
const onChange = () => setDoc(handle.doc());
|
|
1503
|
+
const onDelete = () => {
|
|
1504
|
+
setDeleteError(new Error(`Document ${id} was deleted`));
|
|
927
1505
|
};
|
|
928
|
-
|
|
929
|
-
|
|
1506
|
+
handle.on("change", onChange);
|
|
1507
|
+
handle.on("delete", onDelete);
|
|
1508
|
+
return () => {
|
|
1509
|
+
handle.removeListener("change", onChange);
|
|
1510
|
+
handle.removeListener("delete", onDelete);
|
|
930
1511
|
};
|
|
931
|
-
}, [
|
|
932
|
-
const
|
|
933
|
-
(
|
|
934
|
-
|
|
1512
|
+
}, [handle, id]);
|
|
1513
|
+
const changeDoc = useCallback(
|
|
1514
|
+
(changeFn, options) => {
|
|
1515
|
+
handle.change(changeFn, options);
|
|
935
1516
|
},
|
|
936
|
-
[
|
|
1517
|
+
[handle]
|
|
937
1518
|
);
|
|
938
|
-
if (
|
|
939
|
-
throw
|
|
940
|
-
|
|
941
|
-
|
|
1519
|
+
if (deleteError) {
|
|
1520
|
+
throw deleteError;
|
|
1521
|
+
}
|
|
1522
|
+
if (!doc) {
|
|
1523
|
+
return [void 0, () => {
|
|
1524
|
+
}];
|
|
1525
|
+
}
|
|
1526
|
+
return [doc, changeDoc];
|
|
942
1527
|
}
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
1528
|
+
|
|
1529
|
+
function useSet(items) {
|
|
1530
|
+
const [set, setSet] = useState(() => {
|
|
1531
|
+
return new Set(items);
|
|
1532
|
+
});
|
|
1533
|
+
useEffect(() => {
|
|
1534
|
+
const newSet = new Set(items);
|
|
1535
|
+
if (identical(set, newSet)) {
|
|
1536
|
+
return;
|
|
1537
|
+
}
|
|
1538
|
+
setSet(newSet);
|
|
1539
|
+
}, [set, items]);
|
|
1540
|
+
return set;
|
|
949
1541
|
}
|
|
950
|
-
function
|
|
951
|
-
return
|
|
1542
|
+
function identical(s1, s2) {
|
|
1543
|
+
return s1.size === s2.size && Array.from(s1).every((v) => s2.has(v));
|
|
952
1544
|
}
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
1545
|
+
|
|
1546
|
+
function useDocHandles(ids, { suspense = false } = {}) {
|
|
1547
|
+
const idSet = useSet(ids);
|
|
1548
|
+
const repo = useRepo();
|
|
1549
|
+
const [handleMap, setHandleMap] = useState(() => {
|
|
1550
|
+
const map = /* @__PURE__ */ new Map();
|
|
1551
|
+
for (const id of idSet.values()) {
|
|
1552
|
+
let progress;
|
|
958
1553
|
try {
|
|
959
|
-
|
|
960
|
-
} catch {
|
|
1554
|
+
progress = repo.findWithProgress(id);
|
|
1555
|
+
} catch (e) {
|
|
961
1556
|
continue;
|
|
962
1557
|
}
|
|
963
|
-
|
|
1558
|
+
if (progress.state === "ready") {
|
|
1559
|
+
map.set(id, progress.handle);
|
|
1560
|
+
}
|
|
964
1561
|
}
|
|
965
|
-
return
|
|
966
|
-
})
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1562
|
+
return map;
|
|
1563
|
+
});
|
|
1564
|
+
const pendingPromises = [];
|
|
1565
|
+
const nextHandleMap = /* @__PURE__ */ new Map();
|
|
1566
|
+
for (const id of idSet.values()) {
|
|
1567
|
+
let handle = handleMap.get(id);
|
|
1568
|
+
let wrapper = wrapperCache.get(id);
|
|
1569
|
+
if (!wrapper) {
|
|
970
1570
|
try {
|
|
971
|
-
const
|
|
972
|
-
|
|
973
|
-
|
|
1571
|
+
const promise = repo.find(id);
|
|
1572
|
+
wrapper = wrapPromise(promise);
|
|
1573
|
+
wrapperCache.set(id, wrapper);
|
|
1574
|
+
} catch (e) {
|
|
974
1575
|
continue;
|
|
975
1576
|
}
|
|
1577
|
+
}
|
|
976
1578
|
try {
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
1579
|
+
handle ??= wrapper.read();
|
|
1580
|
+
nextHandleMap.set(id, handle);
|
|
1581
|
+
} catch (e) {
|
|
1582
|
+
if (e instanceof Promise) {
|
|
1583
|
+
pendingPromises.push(wrapper);
|
|
1584
|
+
} else {
|
|
1585
|
+
nextHandleMap.set(id, void 0);
|
|
1586
|
+
}
|
|
980
1587
|
}
|
|
981
1588
|
}
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
(
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
1589
|
+
useEffect(() => {
|
|
1590
|
+
if (pendingPromises.length > 0) {
|
|
1591
|
+
void Promise.allSettled(pendingPromises.map((p) => p.promise)).then(
|
|
1592
|
+
(handles) => {
|
|
1593
|
+
handles.forEach((r) => {
|
|
1594
|
+
if (r.status === "fulfilled") {
|
|
1595
|
+
const h = r.value;
|
|
1596
|
+
nextHandleMap.set(h.url, h);
|
|
1597
|
+
}
|
|
1598
|
+
});
|
|
1599
|
+
setHandleMap(nextHandleMap);
|
|
1600
|
+
}
|
|
1601
|
+
);
|
|
1602
|
+
} else {
|
|
1603
|
+
setHandleMap(nextHandleMap);
|
|
1604
|
+
}
|
|
1605
|
+
}, [suspense, idSet]);
|
|
1606
|
+
if (suspense && pendingPromises.length > 0) {
|
|
1607
|
+
throw Promise.all(pendingPromises.map((p) => p.promise));
|
|
1608
|
+
}
|
|
1609
|
+
return handleMap;
|
|
996
1610
|
}
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1611
|
+
|
|
1612
|
+
function useDocuments(ids, { suspense = true } = {}) {
|
|
1613
|
+
const handleMap = useDocHandles(ids, { suspense });
|
|
1614
|
+
const [docMap, setDocMap] = useState(() => {
|
|
1615
|
+
const docs = /* @__PURE__ */ new Map();
|
|
1616
|
+
handleMap.forEach((handle) => {
|
|
1617
|
+
const url = handle?.url;
|
|
1618
|
+
if (url) {
|
|
1619
|
+
docs.set(url, handle?.doc());
|
|
1620
|
+
}
|
|
1621
|
+
});
|
|
1622
|
+
return docs;
|
|
1004
1623
|
});
|
|
1005
|
-
|
|
1006
|
-
const
|
|
1007
|
-
|
|
1008
|
-
if (
|
|
1009
|
-
const
|
|
1010
|
-
|
|
1011
|
-
const
|
|
1012
|
-
|
|
1624
|
+
useEffect(() => {
|
|
1625
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
1626
|
+
handleMap.forEach((handle, id) => {
|
|
1627
|
+
if (handle) {
|
|
1628
|
+
const onChange = () => {
|
|
1629
|
+
setDocMap((prev) => {
|
|
1630
|
+
const next = new Map(prev);
|
|
1631
|
+
next.set(id, handle.doc());
|
|
1632
|
+
return next;
|
|
1013
1633
|
});
|
|
1014
1634
|
};
|
|
1015
|
-
|
|
1016
|
-
const
|
|
1017
|
-
|
|
1018
|
-
|
|
1635
|
+
setDocMap((prev) => {
|
|
1636
|
+
const next = new Map(prev);
|
|
1637
|
+
next.set(id, handle.doc());
|
|
1638
|
+
return next;
|
|
1639
|
+
});
|
|
1640
|
+
handle.on("change", onChange);
|
|
1641
|
+
listeners.set(id, onChange);
|
|
1642
|
+
}
|
|
1643
|
+
});
|
|
1644
|
+
setDocMap((prev) => {
|
|
1645
|
+
const next = new Map(prev);
|
|
1646
|
+
for (const [id] of next) {
|
|
1647
|
+
if (!handleMap.has(id)) {
|
|
1648
|
+
next.delete(id);
|
|
1649
|
+
}
|
|
1019
1650
|
}
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
c && o && c.removeListener("change", o);
|
|
1651
|
+
return next;
|
|
1652
|
+
});
|
|
1653
|
+
return () => {
|
|
1654
|
+
handleMap.forEach((handle, id) => {
|
|
1655
|
+
const listener = listeners.get(id);
|
|
1656
|
+
if (handle && listener) {
|
|
1657
|
+
handle.removeListener("change", listener);
|
|
1658
|
+
}
|
|
1029
1659
|
});
|
|
1030
1660
|
};
|
|
1031
|
-
}, [
|
|
1032
|
-
const
|
|
1033
|
-
(
|
|
1034
|
-
const
|
|
1035
|
-
|
|
1661
|
+
}, [handleMap]);
|
|
1662
|
+
const changeDoc = useCallback(
|
|
1663
|
+
(id, changeFn, options) => {
|
|
1664
|
+
const handle = handleMap.get(id);
|
|
1665
|
+
if (handle) {
|
|
1666
|
+
handle.change(changeFn, options);
|
|
1667
|
+
}
|
|
1036
1668
|
},
|
|
1037
|
-
[
|
|
1669
|
+
[handleMap]
|
|
1038
1670
|
);
|
|
1039
|
-
return [
|
|
1671
|
+
return [docMap, changeDoc];
|
|
1040
1672
|
}
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1673
|
+
|
|
1674
|
+
var dist;
|
|
1675
|
+
var hasRequiredDist;
|
|
1676
|
+
|
|
1677
|
+
function requireDist () {
|
|
1678
|
+
if (hasRequiredDist) return dist;
|
|
1679
|
+
hasRequiredDist = 1;
|
|
1680
|
+
var react_1 = require$$0;
|
|
1681
|
+
var isFunction = function (setStateAction) {
|
|
1682
|
+
return typeof setStateAction === "function";
|
|
1683
|
+
};
|
|
1684
|
+
var useStateRef = function (initialState) {
|
|
1685
|
+
var _a = react_1.useState(initialState), state = _a[0], setState = _a[1];
|
|
1686
|
+
var ref = react_1.useRef(state);
|
|
1687
|
+
var dispatch = react_1.useCallback(function (setStateAction) {
|
|
1688
|
+
ref.current = isFunction(setStateAction) ? setStateAction(ref.current) : setStateAction;
|
|
1689
|
+
setState(ref.current);
|
|
1690
|
+
}, []);
|
|
1691
|
+
return [state, dispatch, ref];
|
|
1692
|
+
};
|
|
1693
|
+
dist = useStateRef;
|
|
1694
|
+
return dist;
|
|
1054
1695
|
}
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1696
|
+
|
|
1697
|
+
var distExports = requireDist();
|
|
1698
|
+
const useStateRef = /*@__PURE__*/getDefaultExportFromCjs(distExports);
|
|
1699
|
+
|
|
1700
|
+
var eventemitter3 = {exports: {}};
|
|
1701
|
+
|
|
1702
|
+
var hasRequiredEventemitter3;
|
|
1703
|
+
|
|
1704
|
+
function requireEventemitter3 () {
|
|
1705
|
+
if (hasRequiredEventemitter3) return eventemitter3.exports;
|
|
1706
|
+
hasRequiredEventemitter3 = 1;
|
|
1707
|
+
(function (module) {
|
|
1708
|
+
|
|
1709
|
+
var has = Object.prototype.hasOwnProperty
|
|
1710
|
+
, prefix = '~';
|
|
1711
|
+
|
|
1712
|
+
/**
|
|
1713
|
+
* Constructor to create a storage for our `EE` objects.
|
|
1714
|
+
* An `Events` instance is a plain object whose properties are event names.
|
|
1715
|
+
*
|
|
1716
|
+
* @constructor
|
|
1717
|
+
* @private
|
|
1718
|
+
*/
|
|
1719
|
+
function Events() {}
|
|
1720
|
+
|
|
1721
|
+
//
|
|
1722
|
+
// We try to not inherit from `Object.prototype`. In some engines creating an
|
|
1723
|
+
// instance in this way is faster than calling `Object.create(null)` directly.
|
|
1724
|
+
// If `Object.create(null)` is not supported we prefix the event names with a
|
|
1725
|
+
// character to make sure that the built-in object properties are not
|
|
1726
|
+
// overridden or used as an attack vector.
|
|
1727
|
+
//
|
|
1728
|
+
if (Object.create) {
|
|
1729
|
+
Events.prototype = Object.create(null);
|
|
1730
|
+
|
|
1731
|
+
//
|
|
1732
|
+
// This hack is needed because the `__proto__` property is still inherited in
|
|
1733
|
+
// some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
|
|
1734
|
+
//
|
|
1735
|
+
if (!new Events().__proto__) prefix = false;
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
/**
|
|
1739
|
+
* Representation of a single event listener.
|
|
1740
|
+
*
|
|
1741
|
+
* @param {Function} fn The listener function.
|
|
1742
|
+
* @param {*} context The context to invoke the listener with.
|
|
1743
|
+
* @param {Boolean} [once=false] Specify if the listener is a one-time listener.
|
|
1744
|
+
* @constructor
|
|
1745
|
+
* @private
|
|
1746
|
+
*/
|
|
1747
|
+
function EE(fn, context, once) {
|
|
1748
|
+
this.fn = fn;
|
|
1749
|
+
this.context = context;
|
|
1750
|
+
this.once = once || false;
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
/**
|
|
1754
|
+
* Add a listener for a given event.
|
|
1755
|
+
*
|
|
1756
|
+
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
|
|
1757
|
+
* @param {(String|Symbol)} event The event name.
|
|
1758
|
+
* @param {Function} fn The listener function.
|
|
1759
|
+
* @param {*} context The context to invoke the listener with.
|
|
1760
|
+
* @param {Boolean} once Specify if the listener is a one-time listener.
|
|
1761
|
+
* @returns {EventEmitter}
|
|
1762
|
+
* @private
|
|
1763
|
+
*/
|
|
1764
|
+
function addListener(emitter, event, fn, context, once) {
|
|
1765
|
+
if (typeof fn !== 'function') {
|
|
1766
|
+
throw new TypeError('The listener must be a function');
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
var listener = new EE(fn, context || emitter, once)
|
|
1770
|
+
, evt = prefix ? prefix + event : event;
|
|
1771
|
+
|
|
1772
|
+
if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
|
|
1773
|
+
else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
|
|
1774
|
+
else emitter._events[evt] = [emitter._events[evt], listener];
|
|
1775
|
+
|
|
1776
|
+
return emitter;
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
/**
|
|
1780
|
+
* Clear event by name.
|
|
1781
|
+
*
|
|
1782
|
+
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
|
|
1783
|
+
* @param {(String|Symbol)} evt The Event name.
|
|
1784
|
+
* @private
|
|
1785
|
+
*/
|
|
1786
|
+
function clearEvent(emitter, evt) {
|
|
1787
|
+
if (--emitter._eventsCount === 0) emitter._events = new Events();
|
|
1788
|
+
else delete emitter._events[evt];
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
/**
|
|
1792
|
+
* Minimal `EventEmitter` interface that is molded against the Node.js
|
|
1793
|
+
* `EventEmitter` interface.
|
|
1794
|
+
*
|
|
1795
|
+
* @constructor
|
|
1796
|
+
* @public
|
|
1797
|
+
*/
|
|
1798
|
+
function EventEmitter() {
|
|
1799
|
+
this._events = new Events();
|
|
1800
|
+
this._eventsCount = 0;
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
/**
|
|
1804
|
+
* Return an array listing the events for which the emitter has registered
|
|
1805
|
+
* listeners.
|
|
1806
|
+
*
|
|
1807
|
+
* @returns {Array}
|
|
1808
|
+
* @public
|
|
1809
|
+
*/
|
|
1810
|
+
EventEmitter.prototype.eventNames = function eventNames() {
|
|
1811
|
+
var names = []
|
|
1812
|
+
, events
|
|
1813
|
+
, name;
|
|
1814
|
+
|
|
1815
|
+
if (this._eventsCount === 0) return names;
|
|
1816
|
+
|
|
1817
|
+
for (name in (events = this._events)) {
|
|
1818
|
+
if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
if (Object.getOwnPropertySymbols) {
|
|
1822
|
+
return names.concat(Object.getOwnPropertySymbols(events));
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
return names;
|
|
1826
|
+
};
|
|
1827
|
+
|
|
1828
|
+
/**
|
|
1829
|
+
* Return the listeners registered for a given event.
|
|
1830
|
+
*
|
|
1831
|
+
* @param {(String|Symbol)} event The event name.
|
|
1832
|
+
* @returns {Array} The registered listeners.
|
|
1833
|
+
* @public
|
|
1834
|
+
*/
|
|
1835
|
+
EventEmitter.prototype.listeners = function listeners(event) {
|
|
1836
|
+
var evt = prefix ? prefix + event : event
|
|
1837
|
+
, handlers = this._events[evt];
|
|
1838
|
+
|
|
1839
|
+
if (!handlers) return [];
|
|
1840
|
+
if (handlers.fn) return [handlers.fn];
|
|
1841
|
+
|
|
1842
|
+
for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
|
|
1843
|
+
ee[i] = handlers[i].fn;
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
return ee;
|
|
1847
|
+
};
|
|
1848
|
+
|
|
1849
|
+
/**
|
|
1850
|
+
* Return the number of listeners listening to a given event.
|
|
1851
|
+
*
|
|
1852
|
+
* @param {(String|Symbol)} event The event name.
|
|
1853
|
+
* @returns {Number} The number of listeners.
|
|
1854
|
+
* @public
|
|
1855
|
+
*/
|
|
1856
|
+
EventEmitter.prototype.listenerCount = function listenerCount(event) {
|
|
1857
|
+
var evt = prefix ? prefix + event : event
|
|
1858
|
+
, listeners = this._events[evt];
|
|
1859
|
+
|
|
1860
|
+
if (!listeners) return 0;
|
|
1861
|
+
if (listeners.fn) return 1;
|
|
1862
|
+
return listeners.length;
|
|
1863
|
+
};
|
|
1864
|
+
|
|
1865
|
+
/**
|
|
1866
|
+
* Calls each of the listeners registered for a given event.
|
|
1867
|
+
*
|
|
1868
|
+
* @param {(String|Symbol)} event The event name.
|
|
1869
|
+
* @returns {Boolean} `true` if the event had listeners, else `false`.
|
|
1870
|
+
* @public
|
|
1871
|
+
*/
|
|
1872
|
+
EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
|
|
1873
|
+
var evt = prefix ? prefix + event : event;
|
|
1874
|
+
|
|
1875
|
+
if (!this._events[evt]) return false;
|
|
1876
|
+
|
|
1877
|
+
var listeners = this._events[evt]
|
|
1878
|
+
, len = arguments.length
|
|
1879
|
+
, args
|
|
1880
|
+
, i;
|
|
1881
|
+
|
|
1882
|
+
if (listeners.fn) {
|
|
1883
|
+
if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
|
|
1884
|
+
|
|
1885
|
+
switch (len) {
|
|
1886
|
+
case 1: return listeners.fn.call(listeners.context), true;
|
|
1887
|
+
case 2: return listeners.fn.call(listeners.context, a1), true;
|
|
1888
|
+
case 3: return listeners.fn.call(listeners.context, a1, a2), true;
|
|
1889
|
+
case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
|
|
1890
|
+
case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
|
|
1891
|
+
case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
for (i = 1, args = new Array(len -1); i < len; i++) {
|
|
1895
|
+
args[i - 1] = arguments[i];
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
listeners.fn.apply(listeners.context, args);
|
|
1899
|
+
} else {
|
|
1900
|
+
var length = listeners.length
|
|
1901
|
+
, j;
|
|
1902
|
+
|
|
1903
|
+
for (i = 0; i < length; i++) {
|
|
1904
|
+
if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
|
|
1905
|
+
|
|
1906
|
+
switch (len) {
|
|
1907
|
+
case 1: listeners[i].fn.call(listeners[i].context); break;
|
|
1908
|
+
case 2: listeners[i].fn.call(listeners[i].context, a1); break;
|
|
1909
|
+
case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
|
|
1910
|
+
case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
|
|
1911
|
+
default:
|
|
1912
|
+
if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
|
|
1913
|
+
args[j - 1] = arguments[j];
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
listeners[i].fn.apply(listeners[i].context, args);
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
return true;
|
|
1922
|
+
};
|
|
1923
|
+
|
|
1924
|
+
/**
|
|
1925
|
+
* Add a listener for a given event.
|
|
1926
|
+
*
|
|
1927
|
+
* @param {(String|Symbol)} event The event name.
|
|
1928
|
+
* @param {Function} fn The listener function.
|
|
1929
|
+
* @param {*} [context=this] The context to invoke the listener with.
|
|
1930
|
+
* @returns {EventEmitter} `this`.
|
|
1931
|
+
* @public
|
|
1932
|
+
*/
|
|
1933
|
+
EventEmitter.prototype.on = function on(event, fn, context) {
|
|
1934
|
+
return addListener(this, event, fn, context, false);
|
|
1935
|
+
};
|
|
1936
|
+
|
|
1937
|
+
/**
|
|
1938
|
+
* Add a one-time listener for a given event.
|
|
1939
|
+
*
|
|
1940
|
+
* @param {(String|Symbol)} event The event name.
|
|
1941
|
+
* @param {Function} fn The listener function.
|
|
1942
|
+
* @param {*} [context=this] The context to invoke the listener with.
|
|
1943
|
+
* @returns {EventEmitter} `this`.
|
|
1944
|
+
* @public
|
|
1945
|
+
*/
|
|
1946
|
+
EventEmitter.prototype.once = function once(event, fn, context) {
|
|
1947
|
+
return addListener(this, event, fn, context, true);
|
|
1948
|
+
};
|
|
1949
|
+
|
|
1950
|
+
/**
|
|
1951
|
+
* Remove the listeners of a given event.
|
|
1952
|
+
*
|
|
1953
|
+
* @param {(String|Symbol)} event The event name.
|
|
1954
|
+
* @param {Function} fn Only remove the listeners that match this function.
|
|
1955
|
+
* @param {*} context Only remove the listeners that have this context.
|
|
1956
|
+
* @param {Boolean} once Only remove one-time listeners.
|
|
1957
|
+
* @returns {EventEmitter} `this`.
|
|
1958
|
+
* @public
|
|
1959
|
+
*/
|
|
1960
|
+
EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
|
|
1961
|
+
var evt = prefix ? prefix + event : event;
|
|
1962
|
+
|
|
1963
|
+
if (!this._events[evt]) return this;
|
|
1964
|
+
if (!fn) {
|
|
1965
|
+
clearEvent(this, evt);
|
|
1966
|
+
return this;
|
|
1967
|
+
}
|
|
1968
|
+
|
|
1969
|
+
var listeners = this._events[evt];
|
|
1970
|
+
|
|
1971
|
+
if (listeners.fn) {
|
|
1972
|
+
if (
|
|
1973
|
+
listeners.fn === fn &&
|
|
1974
|
+
(!once || listeners.once) &&
|
|
1975
|
+
(!context || listeners.context === context)
|
|
1976
|
+
) {
|
|
1977
|
+
clearEvent(this, evt);
|
|
1978
|
+
}
|
|
1979
|
+
} else {
|
|
1980
|
+
for (var i = 0, events = [], length = listeners.length; i < length; i++) {
|
|
1981
|
+
if (
|
|
1982
|
+
listeners[i].fn !== fn ||
|
|
1983
|
+
(once && !listeners[i].once) ||
|
|
1984
|
+
(context && listeners[i].context !== context)
|
|
1985
|
+
) {
|
|
1986
|
+
events.push(listeners[i]);
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
//
|
|
1991
|
+
// Reset the array, or remove it completely if we have no more listeners.
|
|
1992
|
+
//
|
|
1993
|
+
if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
|
|
1994
|
+
else clearEvent(this, evt);
|
|
1995
|
+
}
|
|
1996
|
+
|
|
1997
|
+
return this;
|
|
1998
|
+
};
|
|
1999
|
+
|
|
2000
|
+
/**
|
|
2001
|
+
* Remove all listeners, or those of the specified event.
|
|
2002
|
+
*
|
|
2003
|
+
* @param {(String|Symbol)} [event] The event name.
|
|
2004
|
+
* @returns {EventEmitter} `this`.
|
|
2005
|
+
* @public
|
|
2006
|
+
*/
|
|
2007
|
+
EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
|
|
2008
|
+
var evt;
|
|
2009
|
+
|
|
2010
|
+
if (event) {
|
|
2011
|
+
evt = prefix ? prefix + event : event;
|
|
2012
|
+
if (this._events[evt]) clearEvent(this, evt);
|
|
2013
|
+
} else {
|
|
2014
|
+
this._events = new Events();
|
|
2015
|
+
this._eventsCount = 0;
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
return this;
|
|
2019
|
+
};
|
|
2020
|
+
|
|
2021
|
+
//
|
|
2022
|
+
// Alias methods names because people roll like that.
|
|
2023
|
+
//
|
|
2024
|
+
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
|
|
2025
|
+
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
|
|
2026
|
+
|
|
2027
|
+
//
|
|
2028
|
+
// Expose the prefix.
|
|
2029
|
+
//
|
|
2030
|
+
EventEmitter.prefixed = prefix;
|
|
2031
|
+
|
|
2032
|
+
//
|
|
2033
|
+
// Allow `EventEmitter` to be imported as module namespace.
|
|
2034
|
+
//
|
|
2035
|
+
EventEmitter.EventEmitter = EventEmitter;
|
|
2036
|
+
|
|
2037
|
+
//
|
|
2038
|
+
// Expose the module.
|
|
2039
|
+
//
|
|
2040
|
+
{
|
|
2041
|
+
module.exports = EventEmitter;
|
|
2042
|
+
}
|
|
2043
|
+
} (eventemitter3));
|
|
2044
|
+
return eventemitter3.exports;
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
var eventemitter3Exports = requireEventemitter3();
|
|
2048
|
+
const EventEmitter = /*@__PURE__*/getDefaultExportFromCjs(eventemitter3Exports);
|
|
2049
|
+
|
|
2050
|
+
const peerEvents = new EventEmitter();
|
|
2051
|
+
const useRemoteAwareness = ({
|
|
2052
|
+
handle,
|
|
2053
|
+
localUserId,
|
|
2054
|
+
offlineTimeout = 3e4,
|
|
2055
|
+
getTime = () => (/* @__PURE__ */ new Date()).getTime()
|
|
2056
|
+
}) => {
|
|
2057
|
+
const [peerStates, setPeerStates, peerStatesRef] = useStateRef({});
|
|
2058
|
+
const [heartbeats, setHeartbeats, heartbeatsRef] = useStateRef({});
|
|
2059
|
+
useEffect(() => {
|
|
2060
|
+
if (!handle) {
|
|
2061
|
+
return;
|
|
1078
2062
|
}
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
if (
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
return d.fn.call(d.context), !0;
|
|
1103
|
-
case 2:
|
|
1104
|
-
return d.fn.call(d.context, i), !0;
|
|
1105
|
-
case 3:
|
|
1106
|
-
return d.fn.call(d.context, i, h), !0;
|
|
1107
|
-
case 4:
|
|
1108
|
-
return d.fn.call(d.context, i, h, m), !0;
|
|
1109
|
-
case 5:
|
|
1110
|
-
return d.fn.call(d.context, i, h, m, B), !0;
|
|
1111
|
-
case 6:
|
|
1112
|
-
return d.fn.call(d.context, i, h, m, B, f), !0;
|
|
2063
|
+
const handleIncomingUpdate = (event) => {
|
|
2064
|
+
const [userId, state] = event.message;
|
|
2065
|
+
if (userId === localUserId) return;
|
|
2066
|
+
if (!heartbeatsRef.current[userId]) peerEvents.emit("new_peer", event);
|
|
2067
|
+
setPeerStates({
|
|
2068
|
+
...peerStatesRef.current,
|
|
2069
|
+
[userId]: state
|
|
2070
|
+
});
|
|
2071
|
+
setHeartbeats({
|
|
2072
|
+
...heartbeatsRef.current,
|
|
2073
|
+
[userId]: getTime()
|
|
2074
|
+
});
|
|
2075
|
+
};
|
|
2076
|
+
const pruneOfflinePeers = () => {
|
|
2077
|
+
const peerStates2 = { ...peerStatesRef.current };
|
|
2078
|
+
const heartbeats2 = { ...heartbeatsRef.current };
|
|
2079
|
+
const time = getTime();
|
|
2080
|
+
let hasChanges = false;
|
|
2081
|
+
for (const key in heartbeats2) {
|
|
2082
|
+
if (time - heartbeats2[key] > offlineTimeout) {
|
|
2083
|
+
delete peerStates2[key];
|
|
2084
|
+
delete heartbeats2[key];
|
|
2085
|
+
hasChanges = true;
|
|
1113
2086
|
}
|
|
1114
|
-
for (x = 1, M = new Array(V - 1); x < V; x++)
|
|
1115
|
-
M[x - 1] = arguments[x];
|
|
1116
|
-
d.fn.apply(d.context, M);
|
|
1117
|
-
} else {
|
|
1118
|
-
var R = d.length, A;
|
|
1119
|
-
for (x = 0; x < R; x++)
|
|
1120
|
-
switch (d[x].once && this.removeListener(o, d[x].fn, void 0, !0), V) {
|
|
1121
|
-
case 1:
|
|
1122
|
-
d[x].fn.call(d[x].context);
|
|
1123
|
-
break;
|
|
1124
|
-
case 2:
|
|
1125
|
-
d[x].fn.call(d[x].context, i);
|
|
1126
|
-
break;
|
|
1127
|
-
case 3:
|
|
1128
|
-
d[x].fn.call(d[x].context, i, h);
|
|
1129
|
-
break;
|
|
1130
|
-
case 4:
|
|
1131
|
-
d[x].fn.call(d[x].context, i, h, m);
|
|
1132
|
-
break;
|
|
1133
|
-
default:
|
|
1134
|
-
if (!M) for (A = 1, M = new Array(V - 1); A < V; A++)
|
|
1135
|
-
M[A - 1] = arguments[A];
|
|
1136
|
-
d[x].fn.apply(d[x].context, M);
|
|
1137
|
-
}
|
|
1138
2087
|
}
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
}, c.prototype.once = function(o, i, h) {
|
|
1143
|
-
return w(this, o, i, h, !0);
|
|
1144
|
-
}, c.prototype.removeListener = function(o, i, h, m) {
|
|
1145
|
-
var B = n ? n + o : o;
|
|
1146
|
-
if (!this._events[B]) return this;
|
|
1147
|
-
if (!i)
|
|
1148
|
-
return a(this, B), this;
|
|
1149
|
-
var f = this._events[B];
|
|
1150
|
-
if (f.fn)
|
|
1151
|
-
f.fn === i && (!m || f.once) && (!h || f.context === h) && a(this, B);
|
|
1152
|
-
else {
|
|
1153
|
-
for (var g = 0, d = [], V = f.length; g < V; g++)
|
|
1154
|
-
(f[g].fn !== i || m && !f[g].once || h && f[g].context !== h) && d.push(f[g]);
|
|
1155
|
-
d.length ? this._events[B] = d.length === 1 ? d[0] : d : a(this, B);
|
|
2088
|
+
if (hasChanges) {
|
|
2089
|
+
setPeerStates(peerStates2);
|
|
2090
|
+
setHeartbeats(heartbeats2);
|
|
1156
2091
|
}
|
|
1157
|
-
return this;
|
|
1158
|
-
}, c.prototype.removeAllListeners = function(o) {
|
|
1159
|
-
var i;
|
|
1160
|
-
return o ? (i = n ? n + o : o, this._events[i] && a(this, i)) : (this._events = new H(), this._eventsCount = 0), this;
|
|
1161
|
-
}, c.prototype.off = c.prototype.removeListener, c.prototype.addListener = c.prototype.on, c.prefixed = n, c.EventEmitter = c, e.exports = c;
|
|
1162
|
-
}(he)), he.exports;
|
|
1163
|
-
}
|
|
1164
|
-
var dt = lt();
|
|
1165
|
-
const bt = /* @__PURE__ */ be(dt), le = new bt(), yt = ({
|
|
1166
|
-
handle: e,
|
|
1167
|
-
localUserId: t,
|
|
1168
|
-
offlineTimeout: n = 3e4,
|
|
1169
|
-
getTime: H = () => (/* @__PURE__ */ new Date()).getTime()
|
|
1170
|
-
}) => {
|
|
1171
|
-
const [y, w, a] = ue({}), [c, s, o] = ue({});
|
|
1172
|
-
return z(() => {
|
|
1173
|
-
if (!e)
|
|
1174
|
-
return;
|
|
1175
|
-
const i = (B) => {
|
|
1176
|
-
const [f, g] = B.message;
|
|
1177
|
-
f !== t && (o.current[f] || le.emit("new_peer", B), w({
|
|
1178
|
-
...a.current,
|
|
1179
|
-
[f]: g
|
|
1180
|
-
}), s({
|
|
1181
|
-
...o.current,
|
|
1182
|
-
[f]: H()
|
|
1183
|
-
}));
|
|
1184
|
-
}, h = () => {
|
|
1185
|
-
const B = { ...a.current }, f = { ...o.current }, g = H();
|
|
1186
|
-
let d = !1;
|
|
1187
|
-
for (const V in f)
|
|
1188
|
-
g - f[V] > n && (delete B[V], delete f[V], d = !0);
|
|
1189
|
-
d && (w(B), s(f));
|
|
1190
2092
|
};
|
|
1191
|
-
|
|
1192
|
-
const
|
|
1193
|
-
|
|
1194
|
-
|
|
2093
|
+
handle.on("ephemeral-message", handleIncomingUpdate);
|
|
2094
|
+
const pruneOfflinePeersIntervalId = setInterval(
|
|
2095
|
+
pruneOfflinePeers,
|
|
2096
|
+
offlineTimeout
|
|
1195
2097
|
);
|
|
1196
2098
|
return () => {
|
|
1197
|
-
|
|
2099
|
+
handle.removeListener("ephemeral-message", handleIncomingUpdate);
|
|
2100
|
+
clearInterval(pruneOfflinePeersIntervalId);
|
|
1198
2101
|
};
|
|
1199
|
-
}, [
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
2102
|
+
}, [handle, localUserId, offlineTimeout, getTime]);
|
|
2103
|
+
return [peerStates, heartbeats];
|
|
2104
|
+
};
|
|
2105
|
+
|
|
2106
|
+
const useLocalAwareness = ({
|
|
2107
|
+
handle,
|
|
2108
|
+
userId,
|
|
2109
|
+
initialState,
|
|
2110
|
+
heartbeatTime = 15e3
|
|
1205
2111
|
}) => {
|
|
1206
|
-
const [
|
|
1207
|
-
|
|
1208
|
-
|
|
2112
|
+
const [localState, setLocalState, localStateRef] = useStateRef(initialState);
|
|
2113
|
+
const setState = (stateOrUpdater) => {
|
|
2114
|
+
const state = typeof stateOrUpdater === "function" ? stateOrUpdater(localStateRef.current) : stateOrUpdater;
|
|
2115
|
+
setLocalState(state);
|
|
2116
|
+
if (handle) {
|
|
2117
|
+
handle.broadcast([userId, state]);
|
|
2118
|
+
}
|
|
1209
2119
|
};
|
|
1210
|
-
|
|
1211
|
-
if (!
|
|
2120
|
+
useEffect(() => {
|
|
2121
|
+
if (!userId || !handle) {
|
|
1212
2122
|
return;
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
2123
|
+
}
|
|
2124
|
+
const heartbeat = () => void handle.broadcast([userId, localStateRef.current]);
|
|
2125
|
+
heartbeat();
|
|
2126
|
+
const heartbeatIntervalId = setInterval(heartbeat, heartbeatTime);
|
|
2127
|
+
return () => void clearInterval(heartbeatIntervalId);
|
|
2128
|
+
}, [handle, userId, heartbeatTime]);
|
|
2129
|
+
useEffect(() => {
|
|
2130
|
+
if (!handle || !userId) {
|
|
1219
2131
|
return;
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
2132
|
+
}
|
|
2133
|
+
let broadcastTimeoutId;
|
|
2134
|
+
const newPeerEvents = peerEvents.on("new_peer", () => {
|
|
2135
|
+
broadcastTimeoutId = setTimeout(
|
|
2136
|
+
() => handle.broadcast([userId, localStateRef.current]),
|
|
1224
2137
|
500
|
|
1225
2138
|
// Wait for the peer to be ready
|
|
1226
2139
|
);
|
|
1227
2140
|
});
|
|
1228
2141
|
return () => {
|
|
1229
|
-
|
|
2142
|
+
newPeerEvents.off("new_peer");
|
|
2143
|
+
broadcastTimeoutId && clearTimeout(broadcastTimeoutId);
|
|
1230
2144
|
};
|
|
1231
|
-
}, [
|
|
1232
|
-
|
|
1233
|
-
export {
|
|
1234
|
-
Ge as RepoContext,
|
|
1235
|
-
ct as useDocHandle,
|
|
1236
|
-
ft as useDocHandles,
|
|
1237
|
-
pt as useDocument,
|
|
1238
|
-
Ht as useDocuments,
|
|
1239
|
-
At as useLocalAwareness,
|
|
1240
|
-
yt as useRemoteAwareness,
|
|
1241
|
-
Ve as useRepo
|
|
2145
|
+
}, [handle, userId, peerEvents]);
|
|
2146
|
+
return [localState, setState];
|
|
1242
2147
|
};
|
|
2148
|
+
|
|
2149
|
+
export { RepoContext, useDocHandle, useDocHandles, useDocument, useDocuments, useLocalAwareness, useRemoteAwareness, useRepo };
|
|
2150
|
+
//# sourceMappingURL=index.js.map
|