@dimchansky/meshpost 0.4.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -550
- package/dist/bip340.d.ts +1 -0
- package/dist/bip340.js +346 -0
- package/dist/blob.d.ts +1 -1
- package/dist/blob.js +15 -16
- package/dist/channel.js +15 -44
- package/dist/combine.js +25 -39
- package/dist/crypto.js +10 -9
- package/dist/identity.d.ts +45 -0
- package/dist/identity.js +188 -28
- package/dist/index.d.ts +6 -6
- package/dist/index.js +3 -3
- package/dist/listeners.js +18 -3
- package/dist/mqtt.js +193 -408
- package/dist/nostr.js +163 -745
- package/dist/public.d.ts +19 -1
- package/dist/public.js +26 -9
- package/dist/relay-core.d.ts +1 -0
- package/dist/relay-core.js +323 -0
- package/dist/room-hello.d.ts +1 -0
- package/dist/room-hello.js +129 -0
- package/dist/room-ice.d.ts +1 -0
- package/dist/room-ice.js +115 -0
- package/dist/room-media.d.ts +1 -0
- package/dist/room-media.js +477 -0
- package/dist/room-reclaim.d.ts +1 -0
- package/dist/room-reclaim.js +111 -0
- package/dist/room-stats.d.ts +1 -0
- package/dist/room-stats.js +75 -0
- package/dist/room.js +109 -919
- package/dist/signaling.d.ts +8 -0
- package/dist/signaling.js +64 -0
- package/dist/state.js +1 -2
- package/dist/voice-helpers.d.ts +1 -0
- package/dist/voice-helpers.js +100 -0
- package/dist/voice-meter.d.ts +1 -0
- package/dist/voice-meter.js +79 -0
- package/dist/voice.d.ts +41 -0
- package/dist/voice.js +205 -273
- package/dist/ws.js +60 -146
- package/docs/api.md +217 -0
- package/docs/behavior.md +16 -9
- package/docs/guide.md +404 -0
- package/docs/signaling-and-production.md +312 -0
- package/docs/voice-and-media.md +193 -0
- package/package.json +3 -2
package/dist/bip340.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/bip340.js
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
// =============================================================================
|
|
2
|
+
// Pure-TS Zero-Dependency SHA-256 & BIP-340 Schnorr (secp256k1)
|
|
3
|
+
// Lazily initialized so bundlers drop 100% of this module when unused.
|
|
4
|
+
// =============================================================================
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
const secpP = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn;
|
|
7
|
+
const secpN = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n;
|
|
8
|
+
const secpGx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n;
|
|
9
|
+
const secpGy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n;
|
|
10
|
+
const sqrtExp = (secpP + 1n) >> 2n;
|
|
11
|
+
/** @internal */
|
|
12
|
+
export const hex64Pattern = /^[0-9a-f]{64}$/;
|
|
13
|
+
/** @internal */
|
|
14
|
+
export const hex128Pattern = /^[0-9a-f]{128}$/;
|
|
15
|
+
let cachedSha256K;
|
|
16
|
+
let cachedTagHashes;
|
|
17
|
+
function getSha256K() {
|
|
18
|
+
cachedSha256K ??= new Uint32Array([
|
|
19
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
20
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
21
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
22
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
23
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
24
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
25
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
26
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
27
|
+
]);
|
|
28
|
+
return cachedSha256K;
|
|
29
|
+
}
|
|
30
|
+
/** @internal */
|
|
31
|
+
export function sha256(data) {
|
|
32
|
+
const K = getSha256K();
|
|
33
|
+
let h0 = 0x6a09e667;
|
|
34
|
+
let h1 = 0xbb67ae85;
|
|
35
|
+
let h2 = 0x3c6ef372;
|
|
36
|
+
let h3 = 0xa54ff53a;
|
|
37
|
+
let h4 = 0x510e527f;
|
|
38
|
+
let h5 = 0x9b05688c;
|
|
39
|
+
let h6 = 0x1f83d9ab;
|
|
40
|
+
let h7 = 0x5be0cd19;
|
|
41
|
+
const len = data.byteLength;
|
|
42
|
+
const paddedLen = (len + 9 + 63) & ~63;
|
|
43
|
+
const padded = new Uint8Array(paddedLen);
|
|
44
|
+
padded.set(data);
|
|
45
|
+
padded[len] = 0x80;
|
|
46
|
+
const dv = new DataView(padded.buffer);
|
|
47
|
+
dv.setUint32(paddedLen - 8, Math.floor(len / 0x20000000) >>> 0, false);
|
|
48
|
+
dv.setUint32(paddedLen - 4, (len << 3) >>> 0, false);
|
|
49
|
+
const w = new Uint32Array(64);
|
|
50
|
+
for (let off = 0; off < paddedLen; off += 64) {
|
|
51
|
+
for (let i = 0; i < 16; i++)
|
|
52
|
+
w[i] = dv.getUint32(off + i * 4, false);
|
|
53
|
+
for (let i = 16; i < 64; i++) {
|
|
54
|
+
const w15 = w[i - 15];
|
|
55
|
+
const w2 = w[i - 2];
|
|
56
|
+
const s0 = ((w15 >>> 7) | (w15 << 25)) ^ ((w15 >>> 18) | (w15 << 14)) ^ (w15 >>> 3);
|
|
57
|
+
const s1 = ((w2 >>> 17) | (w2 << 15)) ^ ((w2 >>> 19) | (w2 << 13)) ^ (w2 >>> 10);
|
|
58
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
59
|
+
}
|
|
60
|
+
let a = h0;
|
|
61
|
+
let b = h1;
|
|
62
|
+
let c = h2;
|
|
63
|
+
let d = h3;
|
|
64
|
+
let e = h4;
|
|
65
|
+
let f = h5;
|
|
66
|
+
let g = h6;
|
|
67
|
+
let h = h7;
|
|
68
|
+
for (let i = 0; i < 64; i++) {
|
|
69
|
+
const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
|
70
|
+
const ch = (e & f) ^ (~e & g);
|
|
71
|
+
const temp1 = (h + S1 + ch + K[i] + w[i]) >>> 0;
|
|
72
|
+
const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
|
73
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
74
|
+
const temp2 = (S0 + maj) >>> 0;
|
|
75
|
+
h = g;
|
|
76
|
+
g = f;
|
|
77
|
+
f = e;
|
|
78
|
+
e = (d + temp1) >>> 0;
|
|
79
|
+
d = c;
|
|
80
|
+
c = b;
|
|
81
|
+
b = a;
|
|
82
|
+
a = (temp1 + temp2) >>> 0;
|
|
83
|
+
}
|
|
84
|
+
h0 = (h0 + a) >>> 0;
|
|
85
|
+
h1 = (h1 + b) >>> 0;
|
|
86
|
+
h2 = (h2 + c) >>> 0;
|
|
87
|
+
h3 = (h3 + d) >>> 0;
|
|
88
|
+
h4 = (h4 + e) >>> 0;
|
|
89
|
+
h5 = (h5 + f) >>> 0;
|
|
90
|
+
h6 = (h6 + g) >>> 0;
|
|
91
|
+
h7 = (h7 + h) >>> 0;
|
|
92
|
+
}
|
|
93
|
+
const out = new Uint8Array(32);
|
|
94
|
+
const outDv = new DataView(out.buffer);
|
|
95
|
+
outDv.setUint32(0, h0, false);
|
|
96
|
+
outDv.setUint32(4, h1, false);
|
|
97
|
+
outDv.setUint32(8, h2, false);
|
|
98
|
+
outDv.setUint32(12, h3, false);
|
|
99
|
+
outDv.setUint32(16, h4, false);
|
|
100
|
+
outDv.setUint32(20, h5, false);
|
|
101
|
+
outDv.setUint32(24, h6, false);
|
|
102
|
+
outDv.setUint32(28, h7, false);
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
function getTagHashes() {
|
|
106
|
+
if (!cachedTagHashes) {
|
|
107
|
+
cachedTagHashes = {
|
|
108
|
+
aux: sha256(encoder.encode('BIP0340/aux')),
|
|
109
|
+
nonce: sha256(encoder.encode('BIP0340/nonce')),
|
|
110
|
+
challenge: sha256(encoder.encode('BIP0340/challenge'))
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return cachedTagHashes;
|
|
114
|
+
}
|
|
115
|
+
function taggedHash(tagHash, ...parts) {
|
|
116
|
+
let total = 64;
|
|
117
|
+
for (const p of parts)
|
|
118
|
+
total += p.byteLength;
|
|
119
|
+
const buf = new Uint8Array(total);
|
|
120
|
+
buf.set(tagHash, 0);
|
|
121
|
+
buf.set(tagHash, 32);
|
|
122
|
+
let off = 64;
|
|
123
|
+
for (const p of parts) {
|
|
124
|
+
buf.set(p, off);
|
|
125
|
+
off += p.byteLength;
|
|
126
|
+
}
|
|
127
|
+
return sha256(buf);
|
|
128
|
+
}
|
|
129
|
+
/** @internal */
|
|
130
|
+
export function bytesToHex(bytes) {
|
|
131
|
+
let out = '';
|
|
132
|
+
for (let i = 0; i < bytes.length; i++)
|
|
133
|
+
out += bytes[i].toString(16).padStart(2, '0');
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
/** @internal */
|
|
137
|
+
export function hexToBytes(hex) {
|
|
138
|
+
const out = new Uint8Array(hex.length >>> 1);
|
|
139
|
+
for (let i = 0; i < out.length; i++) {
|
|
140
|
+
out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
function bytesToBigInt(bytes) {
|
|
145
|
+
let res = 0n;
|
|
146
|
+
for (let i = 0; i < bytes.length; i++)
|
|
147
|
+
res = (res << 8n) | BigInt(bytes[i]);
|
|
148
|
+
return res;
|
|
149
|
+
}
|
|
150
|
+
function bigIntTo32Bytes(num) {
|
|
151
|
+
const out = new Uint8Array(32);
|
|
152
|
+
let n = num;
|
|
153
|
+
for (let i = 31; i >= 0; i--) {
|
|
154
|
+
out[i] = Number(n & 0xffn);
|
|
155
|
+
n >>= 8n;
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
function modP(a) {
|
|
160
|
+
const r = a % secpP;
|
|
161
|
+
return r >= 0n ? r : r + secpP;
|
|
162
|
+
}
|
|
163
|
+
function modN(a) {
|
|
164
|
+
const r = a % secpN;
|
|
165
|
+
return r >= 0n ? r : r + secpN;
|
|
166
|
+
}
|
|
167
|
+
function modPow(base, exp, m) {
|
|
168
|
+
let res = 1n;
|
|
169
|
+
let b = ((base % m) + m) % m;
|
|
170
|
+
let e = exp;
|
|
171
|
+
while (e > 0n) {
|
|
172
|
+
if ((e & 1n) === 1n)
|
|
173
|
+
res = (res * b) % m;
|
|
174
|
+
b = (b * b) % m;
|
|
175
|
+
e >>= 1n;
|
|
176
|
+
}
|
|
177
|
+
return res;
|
|
178
|
+
}
|
|
179
|
+
function modInvP(a) {
|
|
180
|
+
let t = 0n;
|
|
181
|
+
let newT = 1n;
|
|
182
|
+
let r = secpP;
|
|
183
|
+
let newR = modP(a);
|
|
184
|
+
while (newR !== 0n) {
|
|
185
|
+
const q = r / newR;
|
|
186
|
+
const tmpT = t - q * newT;
|
|
187
|
+
t = newT;
|
|
188
|
+
newT = tmpT;
|
|
189
|
+
const tmpR = r - q * newR;
|
|
190
|
+
r = newR;
|
|
191
|
+
newR = tmpR;
|
|
192
|
+
}
|
|
193
|
+
return t < 0n ? t + secpP : t;
|
|
194
|
+
}
|
|
195
|
+
const jacInfinity = { x: 0n, y: 1n, z: 0n };
|
|
196
|
+
const jacG = { x: secpGx, y: secpGy, z: 1n };
|
|
197
|
+
function pointDouble(p) {
|
|
198
|
+
if (p.z === 0n || p.y === 0n)
|
|
199
|
+
return jacInfinity;
|
|
200
|
+
const y2 = modP(p.y * p.y);
|
|
201
|
+
const s = modP(4n * p.x * y2);
|
|
202
|
+
const m = modP(3n * modP(p.x * p.x));
|
|
203
|
+
const x3 = modP(m * m - 2n * s);
|
|
204
|
+
const y3 = modP(m * (s - x3) - 8n * modP(y2 * y2));
|
|
205
|
+
const z3 = modP(2n * modP(p.y * p.z));
|
|
206
|
+
return { x: x3, y: y3, z: z3 };
|
|
207
|
+
}
|
|
208
|
+
function pointAdd(p, q) {
|
|
209
|
+
if (p.z === 0n)
|
|
210
|
+
return q;
|
|
211
|
+
if (q.z === 0n)
|
|
212
|
+
return p;
|
|
213
|
+
const z1z1 = modP(p.z * p.z);
|
|
214
|
+
const z2z2 = modP(q.z * q.z);
|
|
215
|
+
const u1 = modP(p.x * z2z2);
|
|
216
|
+
const u2 = modP(q.x * z1z1);
|
|
217
|
+
const s1 = modP(p.y * modP(q.z * z2z2));
|
|
218
|
+
const s2 = modP(q.y * modP(p.z * z1z1));
|
|
219
|
+
if (u1 === u2) {
|
|
220
|
+
return s1 !== s2 ? jacInfinity : pointDouble(p);
|
|
221
|
+
}
|
|
222
|
+
const h = modP(u2 - u1);
|
|
223
|
+
const r = modP(s2 - s1);
|
|
224
|
+
const h2 = modP(h * h);
|
|
225
|
+
const h3 = modP(h * h2);
|
|
226
|
+
const u1h2 = modP(u1 * h2);
|
|
227
|
+
const x3 = modP(r * r - h3 - 2n * u1h2);
|
|
228
|
+
const y3 = modP(r * (u1h2 - x3) - modP(s1 * h3));
|
|
229
|
+
const z3 = modP(h * modP(p.z * q.z));
|
|
230
|
+
return { x: x3, y: y3, z: z3 };
|
|
231
|
+
}
|
|
232
|
+
function pointMul(base, scalar) {
|
|
233
|
+
let res = jacInfinity;
|
|
234
|
+
let addend = base;
|
|
235
|
+
let k = scalar;
|
|
236
|
+
while (k > 0n) {
|
|
237
|
+
if ((k & 1n) === 1n)
|
|
238
|
+
res = pointAdd(res, addend);
|
|
239
|
+
addend = pointDouble(addend);
|
|
240
|
+
k >>= 1n;
|
|
241
|
+
}
|
|
242
|
+
return res;
|
|
243
|
+
}
|
|
244
|
+
function toAffine(p) {
|
|
245
|
+
if (p.z === 0n)
|
|
246
|
+
return null;
|
|
247
|
+
const invZ = modInvP(p.z);
|
|
248
|
+
const invZ2 = modP(invZ * invZ);
|
|
249
|
+
const invZ3 = modP(invZ2 * invZ);
|
|
250
|
+
return { x: modP(p.x * invZ2), y: modP(p.y * invZ3) };
|
|
251
|
+
}
|
|
252
|
+
/** @internal */
|
|
253
|
+
export function schnorrPublicKey(secretKey32) {
|
|
254
|
+
if (secretKey32.byteLength !== 32)
|
|
255
|
+
throw new RangeError('Expected 32-byte secret key');
|
|
256
|
+
const d0 = bytesToBigInt(secretKey32);
|
|
257
|
+
if (d0 <= 0n || d0 >= secpN)
|
|
258
|
+
throw new RangeError('Secret key out of range');
|
|
259
|
+
const p = toAffine(pointMul(jacG, d0));
|
|
260
|
+
if (!p)
|
|
261
|
+
throw new Error('Invalid point');
|
|
262
|
+
return bigIntTo32Bytes(p.x);
|
|
263
|
+
}
|
|
264
|
+
/** @internal */
|
|
265
|
+
export function schnorrSign(msg32, secretKey32, auxRand32 = crypto.getRandomValues(new Uint8Array(32))) {
|
|
266
|
+
if (msg32.byteLength !== 32 || secretKey32.byteLength !== 32 || auxRand32.byteLength !== 32) {
|
|
267
|
+
throw new RangeError('Expected 32-byte inputs for BIP-340 schnorrSign');
|
|
268
|
+
}
|
|
269
|
+
const d0 = bytesToBigInt(secretKey32);
|
|
270
|
+
if (d0 <= 0n || d0 >= secpN)
|
|
271
|
+
throw new RangeError('Secret key out of range');
|
|
272
|
+
const p = toAffine(pointMul(jacG, d0));
|
|
273
|
+
if (!p)
|
|
274
|
+
throw new Error('Invalid public point');
|
|
275
|
+
const d = (p.y & 1n) === 0n ? d0 : secpN - d0;
|
|
276
|
+
const pkBytes = bigIntTo32Bytes(p.x);
|
|
277
|
+
const dBytes = bigIntTo32Bytes(d);
|
|
278
|
+
const tags = getTagHashes();
|
|
279
|
+
let aux = auxRand32;
|
|
280
|
+
for (let attempt = 0; attempt < 8; attempt++) {
|
|
281
|
+
const tHash = taggedHash(tags.aux, aux);
|
|
282
|
+
const t = new Uint8Array(32);
|
|
283
|
+
for (let i = 0; i < 32; i++)
|
|
284
|
+
t[i] = dBytes[i] ^ tHash[i];
|
|
285
|
+
const rand = taggedHash(tags.nonce, t, pkBytes, msg32);
|
|
286
|
+
const k0 = bytesToBigInt(rand) % secpN;
|
|
287
|
+
if (k0 === 0n) {
|
|
288
|
+
aux = crypto.getRandomValues(new Uint8Array(32));
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const rPoint = toAffine(pointMul(jacG, k0));
|
|
292
|
+
if (!rPoint) {
|
|
293
|
+
aux = crypto.getRandomValues(new Uint8Array(32));
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
const k = (rPoint.y & 1n) === 0n ? k0 : secpN - k0;
|
|
297
|
+
const rBytes = bigIntTo32Bytes(rPoint.x);
|
|
298
|
+
const e = bytesToBigInt(taggedHash(tags.challenge, rBytes, pkBytes, msg32)) % secpN;
|
|
299
|
+
const s = modN(k + modN(e * d));
|
|
300
|
+
const sig = new Uint8Array(64);
|
|
301
|
+
sig.set(rBytes, 0);
|
|
302
|
+
sig.set(bigIntTo32Bytes(s), 32);
|
|
303
|
+
return sig;
|
|
304
|
+
}
|
|
305
|
+
throw new Error('Failed to derive non-zero BIP-340 nonce');
|
|
306
|
+
}
|
|
307
|
+
/** @internal */
|
|
308
|
+
export function verifySchnorr(sig64, msg32, pubkey32) {
|
|
309
|
+
if (sig64.byteLength !== 64 || msg32.byteLength !== 32 || pubkey32.byteLength !== 32) {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
const px = bytesToBigInt(pubkey32);
|
|
313
|
+
if (px >= secpP)
|
|
314
|
+
return false;
|
|
315
|
+
const r = bytesToBigInt(sig64.subarray(0, 32));
|
|
316
|
+
if (r >= secpP)
|
|
317
|
+
return false;
|
|
318
|
+
const s = bytesToBigInt(sig64.subarray(32, 64));
|
|
319
|
+
if (s >= secpN)
|
|
320
|
+
return false;
|
|
321
|
+
const c = modP(modP(px * modP(px * px)) + 7n);
|
|
322
|
+
let py = modPow(c, sqrtExp, secpP);
|
|
323
|
+
if (modP(py * py) !== c)
|
|
324
|
+
return false;
|
|
325
|
+
if ((py & 1n) === 1n)
|
|
326
|
+
py = secpP - py;
|
|
327
|
+
const tags = getTagHashes();
|
|
328
|
+
const e = bytesToBigInt(taggedHash(tags.challenge, sig64.subarray(0, 32), pubkey32, msg32)) % secpN;
|
|
329
|
+
const sG = pointMul(jacG, s);
|
|
330
|
+
const negE = e === 0n ? 0n : secpN - e;
|
|
331
|
+
const negEP = pointMul({ x: px, y: py, z: 1n }, negE);
|
|
332
|
+
const rPoint = toAffine(pointAdd(sG, negEP));
|
|
333
|
+
if (!rPoint || (rPoint.y & 1n) === 1n)
|
|
334
|
+
return false;
|
|
335
|
+
return rPoint.x === r;
|
|
336
|
+
}
|
|
337
|
+
/** @internal */
|
|
338
|
+
export function generateSessionKeypair() {
|
|
339
|
+
for (;;) {
|
|
340
|
+
const sk = crypto.getRandomValues(new Uint8Array(32));
|
|
341
|
+
const d0 = bytesToBigInt(sk);
|
|
342
|
+
if (d0 > 0n && d0 < secpN) {
|
|
343
|
+
return { secretKey: sk, pubkeyHex: bytesToHex(schnorrPublicKey(sk)) };
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
package/dist/blob.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ListenOptions, type Room } from './room.js';
|
|
2
2
|
export interface BlobChannelOptions {
|
|
3
3
|
/** Maximum total bytes accepted for a single incoming blob. @default 4194304 (4 MiB) */
|
|
4
4
|
maxReceiveBytes?: number;
|
package/dist/blob.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createChannel } from './channel.js';
|
|
2
|
-
import { bindAbort, createListeners } from './listeners.js';
|
|
2
|
+
import { bindAbort, createListeners, reportAsyncError } from './listeners.js';
|
|
3
|
+
import { isRoomClosed, onRoomClose } from './room.js';
|
|
3
4
|
const opStart = 0x01;
|
|
4
5
|
const opData = 0x02;
|
|
5
6
|
const opAbort = 0x03;
|
|
@@ -13,7 +14,6 @@ const defaultChunkSize = 15 * 1024;
|
|
|
13
14
|
const defaultReceiveTimeoutMs = 30_000;
|
|
14
15
|
const defaultMaxConcurrentPerPeer = 4;
|
|
15
16
|
const encoder = new TextEncoder();
|
|
16
|
-
const listeners = createListeners;
|
|
17
17
|
/**
|
|
18
18
|
* Create a chunked reliable binary channel for payloads larger than Meshpost's 16 KiB packet limit.
|
|
19
19
|
* Built strictly on top of `createChannel(room, name, { delivery: 'reliable' })` and `room.onDrain`.
|
|
@@ -38,14 +38,14 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
38
38
|
maxConcurrentPerPeer <= 0 ||
|
|
39
39
|
maxConcurrentPerPeer > 64)
|
|
40
40
|
throw new RangeError('Invalid maxConcurrentPerPeer');
|
|
41
|
-
const blobs =
|
|
42
|
-
const progresses =
|
|
41
|
+
const blobs = createListeners();
|
|
42
|
+
const progresses = createListeners();
|
|
43
43
|
const incoming = new Map();
|
|
44
44
|
const closeWaiters = new Set();
|
|
45
45
|
const internalAbort = new AbortController();
|
|
46
46
|
let nextTransferId = (crypto.getRandomValues(new Uint32Array(1))[0] & 0x7fff_ffff) + 1;
|
|
47
|
-
|
|
48
|
-
let
|
|
47
|
+
let closed = isRoomClosed(room);
|
|
48
|
+
let offRoomClose;
|
|
49
49
|
const dropTransfer = (peerId, transferId) => {
|
|
50
50
|
const byPeer = incoming.get(peerId);
|
|
51
51
|
const entry = byPeer?.get(transferId);
|
|
@@ -68,6 +68,8 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
68
68
|
if (closed)
|
|
69
69
|
return;
|
|
70
70
|
closed = true;
|
|
71
|
+
offRoomClose?.();
|
|
72
|
+
offRoomClose = undefined;
|
|
71
73
|
internalAbort.abort();
|
|
72
74
|
for (const byPeer of incoming.values()) {
|
|
73
75
|
for (const entry of byPeer.values())
|
|
@@ -81,7 +83,7 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
81
83
|
progresses.clear();
|
|
82
84
|
};
|
|
83
85
|
const sendAbort = (peerId, transferId) => {
|
|
84
|
-
if (isRoomClosed())
|
|
86
|
+
if (isRoomClosed(room))
|
|
85
87
|
return;
|
|
86
88
|
const frame = new Uint8Array(abortFrameBytes);
|
|
87
89
|
const view = new DataView(frame.buffer);
|
|
@@ -99,7 +101,7 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
99
101
|
resolve('abort');
|
|
100
102
|
return;
|
|
101
103
|
}
|
|
102
|
-
if (isRoomClosed()) {
|
|
104
|
+
if (isRoomClosed(room)) {
|
|
103
105
|
resolve('unavailable');
|
|
104
106
|
return;
|
|
105
107
|
}
|
|
@@ -132,7 +134,7 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
132
134
|
const { onProgress, signal } = options;
|
|
133
135
|
if (closed || signal?.aborted)
|
|
134
136
|
return 'aborted';
|
|
135
|
-
if (isRoomClosed())
|
|
137
|
+
if (isRoomClosed(room))
|
|
136
138
|
return 'unavailable';
|
|
137
139
|
if (bytes.byteLength > maxUint32)
|
|
138
140
|
return 'too-large';
|
|
@@ -146,7 +148,7 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
146
148
|
sendAbort(peerId, transferId);
|
|
147
149
|
return 'aborted';
|
|
148
150
|
}
|
|
149
|
-
if (isRoomClosed())
|
|
151
|
+
if (isRoomClosed(room))
|
|
150
152
|
return 'unavailable';
|
|
151
153
|
const isStart = !started;
|
|
152
154
|
const sliceEnd = Math.min(totalBytes, offset + chunkSize);
|
|
@@ -174,7 +176,7 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
174
176
|
onProgress?.(peerId, offset, totalBytes);
|
|
175
177
|
}
|
|
176
178
|
catch (error) {
|
|
177
|
-
|
|
179
|
+
reportAsyncError(error);
|
|
178
180
|
}
|
|
179
181
|
if (offset >= totalBytes)
|
|
180
182
|
return 'sent';
|
|
@@ -201,11 +203,8 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
201
203
|
};
|
|
202
204
|
if (!closed) {
|
|
203
205
|
const internalListen = { signal: internalAbort.signal };
|
|
206
|
+
offRoomClose = onRoomClose(room, shutdown);
|
|
204
207
|
room.onPeerLeave((peerId) => dropPeerTransfers(peerId), internalListen);
|
|
205
|
-
room.onSignalingChange((state) => {
|
|
206
|
-
if (state === 'closed')
|
|
207
|
-
shutdown();
|
|
208
|
-
}, internalListen);
|
|
209
208
|
channel.onPacket((from, bytes) => {
|
|
210
209
|
if (closed || bytes.byteLength < abortFrameBytes)
|
|
211
210
|
return;
|
|
@@ -305,7 +304,7 @@ export function createBlobChannel(room, name, options = {}) {
|
|
|
305
304
|
return {
|
|
306
305
|
name,
|
|
307
306
|
async send(bytes, sendOptions = {}) {
|
|
308
|
-
if (closed || isRoomClosed())
|
|
307
|
+
if (closed || isRoomClosed(room))
|
|
309
308
|
throw new Error('BlobChannel is closed');
|
|
310
309
|
if (!(bytes instanceof Uint8Array))
|
|
311
310
|
throw new TypeError('Expected a Uint8Array');
|
package/dist/channel.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createListeners } from './listeners.js';
|
|
2
2
|
import { isRoomClosed, onRoomClose } from './room.js';
|
|
3
3
|
const encoder = new TextEncoder();
|
|
4
4
|
const strictDecoder = new TextDecoder('utf-8', { fatal: true });
|
|
@@ -50,26 +50,12 @@ function getRouter(room) {
|
|
|
50
50
|
const subs = table.byName.get(channelName);
|
|
51
51
|
if (!subs || subs.size === 0)
|
|
52
52
|
return;
|
|
53
|
-
|
|
54
|
-
for (const entry of [...subs]) {
|
|
55
|
-
if (!subs.has(entry))
|
|
56
|
-
continue;
|
|
57
|
-
if (entry.once)
|
|
58
|
-
entry.off();
|
|
59
|
-
try {
|
|
60
|
-
entry.fn(from, payload);
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
globalThis.reportError?.(error);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
53
|
+
subs.fire(from, bytes.subarray(1 + len));
|
|
66
54
|
};
|
|
67
55
|
const clearAll = () => {
|
|
68
56
|
for (const table of [unreliable, reliable]) {
|
|
69
|
-
for (const subs of [...table.byName.values()])
|
|
70
|
-
|
|
71
|
-
entry.off();
|
|
72
|
-
}
|
|
57
|
+
for (const subs of [...table.byName.values()])
|
|
58
|
+
subs.clear();
|
|
73
59
|
table.byName.clear();
|
|
74
60
|
table.lengthCounts.fill(0);
|
|
75
61
|
}
|
|
@@ -97,36 +83,21 @@ function getRouter(room) {
|
|
|
97
83
|
const table = delivery === 'reliable' ? reliable : unreliable;
|
|
98
84
|
let subs = table.byName.get(name);
|
|
99
85
|
if (!subs) {
|
|
100
|
-
|
|
86
|
+
const group = createListeners(() => {
|
|
87
|
+
activeSubs = Math.max(0, activeSubs - 1);
|
|
88
|
+
if (group.size === 0 && table.byName.get(name) === group) {
|
|
89
|
+
table.byName.delete(name);
|
|
90
|
+
const prevLenCount = table.lengthCounts[nameLen] ?? 0;
|
|
91
|
+
table.lengthCounts[nameLen] = prevLenCount > 0 ? prevLenCount - 1 : 0;
|
|
92
|
+
}
|
|
93
|
+
detachIfIdle();
|
|
94
|
+
});
|
|
95
|
+
subs = group;
|
|
101
96
|
table.byName.set(name, subs);
|
|
102
97
|
table.lengthCounts[nameLen] = (table.lengthCounts[nameLen] ?? 0) + 1;
|
|
103
98
|
}
|
|
104
|
-
let removed = false;
|
|
105
|
-
let unbindAbort;
|
|
106
|
-
const entry = {
|
|
107
|
-
fn,
|
|
108
|
-
once: Boolean(options?.once),
|
|
109
|
-
off() {
|
|
110
|
-
if (removed)
|
|
111
|
-
return;
|
|
112
|
-
removed = true;
|
|
113
|
-
unbindAbort?.();
|
|
114
|
-
const current = table.byName.get(name);
|
|
115
|
-
if (current?.delete(entry)) {
|
|
116
|
-
activeSubs = Math.max(0, activeSubs - 1);
|
|
117
|
-
if (current.size === 0) {
|
|
118
|
-
table.byName.delete(name);
|
|
119
|
-
const prevLenCount = table.lengthCounts[nameLen] ?? 0;
|
|
120
|
-
table.lengthCounts[nameLen] = prevLenCount > 0 ? prevLenCount - 1 : 0;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
detachIfIdle();
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
subs.add(entry);
|
|
127
99
|
activeSubs++;
|
|
128
|
-
|
|
129
|
-
return entry.off;
|
|
100
|
+
return subs.add(fn, options);
|
|
130
101
|
}
|
|
131
102
|
};
|
|
132
103
|
routers.set(room, router);
|
package/dist/combine.js
CHANGED
|
@@ -150,32 +150,13 @@ function createCompositeSession(discovery, list, primaryIndex) {
|
|
|
150
150
|
if (closed || next === child.state)
|
|
151
151
|
return;
|
|
152
152
|
child.state = next;
|
|
153
|
-
if (next === 'closed') {
|
|
154
|
-
|
|
155
|
-
child
|
|
156
|
-
|
|
153
|
+
if (next === 'closed' || next === 'offline') {
|
|
154
|
+
for (const id of [...child.peers]) {
|
|
155
|
+
if (next === 'closed' || !childCanReach(child, id))
|
|
156
|
+
child.peers.delete(id);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
|
-
|
|
160
|
-
if (typeof child.session?.canReach === 'function') {
|
|
161
|
-
let removed = false;
|
|
162
|
-
for (const id of [...child.peers]) {
|
|
163
|
-
if (!child.session.canReach(id)) {
|
|
164
|
-
child.peers.delete(id);
|
|
165
|
-
removed = true;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
if (removed)
|
|
169
|
-
syncRoster();
|
|
170
|
-
}
|
|
171
|
-
else if (child.peers.size > 0) {
|
|
172
|
-
child.peers.clear();
|
|
173
|
-
syncRoster();
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
else if (next === 'online' && child.peers.size > 0) {
|
|
177
|
-
syncRoster();
|
|
178
|
-
}
|
|
159
|
+
syncRoster();
|
|
179
160
|
if (primaryIndex === null || index === primaryIndex) {
|
|
180
161
|
syncState(reason);
|
|
181
162
|
}
|
|
@@ -323,20 +304,8 @@ function canUseLocalTabs() {
|
|
|
323
304
|
}
|
|
324
305
|
return true;
|
|
325
306
|
}
|
|
326
|
-
/**
|
|
327
|
-
|
|
328
|
-
* same-browser `BroadcastChannel` discovery (`tabSignaling()`).
|
|
329
|
-
*
|
|
330
|
-
* - Exposes `primary`'s `SignalingState` and errors for `room.signalingState` / `room.onSignalingChange`
|
|
331
|
-
* (avoiding a permanently `'online'` indicator when network connectivity is lost).
|
|
332
|
-
* - Still connects same-browser tabs locally even while `primary` is `'connecting'` or `'offline'`.
|
|
333
|
-
* - Safely falls back to `primary` alone when `BroadcastChannel` is unavailable, on `file:` / opaque
|
|
334
|
-
* origins, or if `new BroadcastChannel(...)` throws `SecurityError`.
|
|
335
|
-
*/
|
|
336
|
-
export function withLocalTabs(primary) {
|
|
337
|
-
if (typeof primary !== 'function') {
|
|
338
|
-
throw new TypeError('Expected a signaling adapter function');
|
|
339
|
-
}
|
|
307
|
+
/** @internal */
|
|
308
|
+
export function withLocalTabsInternal(primary, wrapTabs) {
|
|
340
309
|
return (discovery) => {
|
|
341
310
|
if (!canUseLocalTabs())
|
|
342
311
|
return primary(discovery);
|
|
@@ -349,7 +318,24 @@ export function withLocalTabs(primary) {
|
|
|
349
318
|
catch {
|
|
350
319
|
return primary(discovery);
|
|
351
320
|
}
|
|
352
|
-
const
|
|
321
|
+
const baseTabs = tabSignaling();
|
|
322
|
+
const tabs = wrapTabs ? wrapTabs(baseTabs) : baseTabs;
|
|
353
323
|
return createCompositeSession(discovery, [tabs, primary], 1);
|
|
354
324
|
};
|
|
355
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* Decorate a network signaling adapter (`mqttSignaling()`, `wsSignaling()`, etc.) with instant
|
|
328
|
+
* same-browser `BroadcastChannel` discovery (`tabSignaling()`).
|
|
329
|
+
*
|
|
330
|
+
* - Exposes `primary`'s `SignalingState` and errors for `room.signalingState` / `room.onSignalingChange`
|
|
331
|
+
* (avoiding a permanently `'online'` indicator when network connectivity is lost).
|
|
332
|
+
* - Still connects same-browser tabs locally even while `primary` is `'connecting'` or `'offline'`.
|
|
333
|
+
* - Safely falls back to `primary` alone when `BroadcastChannel` is unavailable, on `file:` / opaque
|
|
334
|
+
* origins, or if `new BroadcastChannel(...)` throws `SecurityError`.
|
|
335
|
+
*/
|
|
336
|
+
export function withLocalTabs(primary) {
|
|
337
|
+
if (typeof primary !== 'function') {
|
|
338
|
+
throw new TypeError('Expected a signaling adapter function');
|
|
339
|
+
}
|
|
340
|
+
return withLocalTabsInternal(primary);
|
|
341
|
+
}
|
package/dist/crypto.js
CHANGED
|
@@ -92,20 +92,14 @@ export function toBase64Url(bytes) {
|
|
|
92
92
|
}
|
|
93
93
|
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
94
94
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
value.length > maxChars ||
|
|
99
|
-
value.length % 4 === 1 ||
|
|
100
|
-
!base64UrlPattern.test(value)) {
|
|
95
|
+
/** @internal */
|
|
96
|
+
export function decodeBase64UrlBytes(value) {
|
|
97
|
+
if (value.length % 4 === 1 || !base64UrlPattern.test(value))
|
|
101
98
|
return null;
|
|
102
|
-
}
|
|
103
99
|
try {
|
|
104
100
|
const base64 = value.replace(/-/g, '+').replace(/_/g, '/');
|
|
105
101
|
const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4);
|
|
106
102
|
const binary = atob(padded);
|
|
107
|
-
if (binary.length < minBoxBytes)
|
|
108
|
-
return null;
|
|
109
103
|
const out = new Uint8Array(binary.length);
|
|
110
104
|
for (let i = 0; i < binary.length; i++)
|
|
111
105
|
out[i] = binary.charCodeAt(i);
|
|
@@ -115,6 +109,13 @@ export function fromBase64Url(value, maxChars = maxBoxChars) {
|
|
|
115
109
|
return null;
|
|
116
110
|
}
|
|
117
111
|
}
|
|
112
|
+
export function fromBase64Url(value, maxChars = maxBoxChars) {
|
|
113
|
+
if (typeof value !== 'string' || value.length < minBoxChars || value.length > maxChars) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const out = decodeBase64UrlBytes(value);
|
|
117
|
+
return out && out.byteLength >= minBoxBytes ? out : null;
|
|
118
|
+
}
|
|
118
119
|
export function createReplayGuard() {
|
|
119
120
|
const seenMids = new Map();
|
|
120
121
|
const recordMid = (scopedMid, now = Date.now()) => {
|