@dimchansky/meshpost 0.5.0 → 0.7.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 -544
- package/dist/bip340.d.ts +1 -0
- package/dist/bip340.js +346 -0
- package/dist/blob.d.ts +1 -1
- package/dist/blob.js +44 -45
- package/dist/channel.d.ts +4 -0
- package/dist/channel.js +19 -44
- package/dist/combine.d.ts +9 -2
- package/dist/combine.js +40 -57
- package/dist/crypto.js +10 -9
- package/dist/identity.d.ts +2 -0
- package/dist/identity.js +39 -71
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/listeners.js +18 -3
- package/dist/mqtt.js +211 -476
- package/dist/nostr.js +194 -836
- package/dist/peer-link.d.ts +1 -0
- package/dist/peer-link.js +983 -0
- package/dist/presence.d.ts +1 -0
- package/dist/presence.js +74 -0
- package/dist/public.d.ts +1 -1
- package/dist/public.js +25 -77
- package/dist/relay-core.d.ts +1 -0
- package/dist/relay-core.js +399 -0
- package/dist/room-reclaim.d.ts +1 -0
- package/dist/room-reclaim.js +115 -0
- package/dist/room-stats.d.ts +1 -0
- package/dist/room-stats.js +75 -0
- package/dist/room.js +245 -1218
- package/dist/rpc.js +25 -28
- package/dist/signaling.d.ts +8 -0
- package/dist/signaling.js +55 -0
- package/dist/state.js +23 -33
- package/dist/tabs.js +6 -50
- package/dist/voice-mic.d.ts +1 -0
- package/dist/voice-mic.js +496 -0
- package/dist/voice-remote.d.ts +1 -0
- package/dist/voice-remote.js +568 -0
- package/dist/voice.d.ts +19 -1
- package/dist/voice.js +95 -1036
- package/dist/ws.js +56 -197
- package/docs/adr/0001-room-internals-and-channel-budget.md +31 -0
- package/docs/api.md +219 -0
- package/docs/behavior.md +14 -8
- 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 +6 -3
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { toBase64Url } from './crypto.js';
|
|
2
|
+
import { decodeCanonicalBase64Url, sha256Base64Url } from './identity.js';
|
|
3
|
+
import { isUuid } from './signaling.js';
|
|
4
|
+
const reclaimTtlMs = 60_000;
|
|
5
|
+
const maxSupersededPeers = 128;
|
|
6
|
+
/** @internal */
|
|
7
|
+
export function computeReclaimCommit(id, secret) {
|
|
8
|
+
return sha256Base64Url(`meshpost/1/reclaim:${id}:${secret}`);
|
|
9
|
+
}
|
|
10
|
+
/** @internal */
|
|
11
|
+
export function createSessionReclaim(params) {
|
|
12
|
+
const { appId, roomName, self, isStopped, sendUnloadLeaveNotices } = params;
|
|
13
|
+
const supersededPeers = new Set();
|
|
14
|
+
const storageKey = `meshpost/1/reclaim:${appId}:${roomName}`;
|
|
15
|
+
let pendingReclaim = null;
|
|
16
|
+
try {
|
|
17
|
+
const raw = globalThis.sessionStorage?.getItem(storageKey);
|
|
18
|
+
if (raw) {
|
|
19
|
+
globalThis.sessionStorage?.removeItem(storageKey);
|
|
20
|
+
const parsed = JSON.parse(raw);
|
|
21
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
22
|
+
const rec = parsed;
|
|
23
|
+
if (typeof rec.id === 'string' &&
|
|
24
|
+
isUuid(rec.id) &&
|
|
25
|
+
rec.id !== self &&
|
|
26
|
+
decodeCanonicalBase64Url(rec.secret, 22, 16) !== null &&
|
|
27
|
+
typeof rec.ts === 'number' &&
|
|
28
|
+
Number.isFinite(rec.ts) &&
|
|
29
|
+
Math.abs(Date.now() - rec.ts) <= reclaimTtlMs) {
|
|
30
|
+
pendingReclaim = { id: rec.id, secret: rec.secret, ts: rec.ts };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Ignore storage restrictions on opaque origins or non-browser runtimes.
|
|
37
|
+
}
|
|
38
|
+
const localSecret = globalThis.crypto?.subtle
|
|
39
|
+
? toBase64Url(crypto.getRandomValues(new Uint8Array(16)))
|
|
40
|
+
: null;
|
|
41
|
+
const localCommitPromise = localSecret
|
|
42
|
+
? computeReclaimCommit(self, localSecret)
|
|
43
|
+
: Promise.resolve(null);
|
|
44
|
+
const hasWindowEvents = typeof window !== 'undefined' &&
|
|
45
|
+
typeof window.addEventListener === 'function' &&
|
|
46
|
+
typeof window.removeEventListener === 'function';
|
|
47
|
+
const onPageHide = (event) => {
|
|
48
|
+
if (isStopped())
|
|
49
|
+
return;
|
|
50
|
+
if (!event.persisted)
|
|
51
|
+
sendUnloadLeaveNotices();
|
|
52
|
+
if (localSecret) {
|
|
53
|
+
try {
|
|
54
|
+
globalThis.sessionStorage?.setItem(storageKey, JSON.stringify({ id: self, secret: localSecret, ts: Date.now() }));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Ignore storage quota or security errors.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const onPageShow = () => {
|
|
62
|
+
if (isStopped())
|
|
63
|
+
return;
|
|
64
|
+
try {
|
|
65
|
+
globalThis.sessionStorage?.removeItem(storageKey);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Ignore storage errors.
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
let attached = false;
|
|
72
|
+
return {
|
|
73
|
+
localCommitPromise,
|
|
74
|
+
getPendingReclaim() {
|
|
75
|
+
if (!pendingReclaim)
|
|
76
|
+
return null;
|
|
77
|
+
if (Math.abs(Date.now() - pendingReclaim.ts) <= reclaimTtlMs) {
|
|
78
|
+
return { id: pendingReclaim.id, secret: pendingReclaim.secret };
|
|
79
|
+
}
|
|
80
|
+
pendingReclaim = null;
|
|
81
|
+
return null;
|
|
82
|
+
},
|
|
83
|
+
isSuperseded: (id) => supersededPeers.has(id),
|
|
84
|
+
markSuperseded(id) {
|
|
85
|
+
supersededPeers.delete(id);
|
|
86
|
+
supersededPeers.add(id);
|
|
87
|
+
if (supersededPeers.size > maxSupersededPeers) {
|
|
88
|
+
const oldest = supersededPeers.values().next().value;
|
|
89
|
+
if (oldest !== undefined)
|
|
90
|
+
supersededPeers.delete(oldest);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
attachWindowListeners() {
|
|
94
|
+
if (hasWindowEvents && !attached) {
|
|
95
|
+
attached = true;
|
|
96
|
+
window.addEventListener('pagehide', onPageHide);
|
|
97
|
+
window.addEventListener('pageshow', onPageShow);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
cleanup() {
|
|
101
|
+
if (hasWindowEvents && attached) {
|
|
102
|
+
attached = false;
|
|
103
|
+
window.removeEventListener('pagehide', onPageHide);
|
|
104
|
+
window.removeEventListener('pageshow', onPageShow);
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
globalThis.sessionStorage?.removeItem(storageKey);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// Ignore storage errors.
|
|
111
|
+
}
|
|
112
|
+
supersededPeers.clear();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
function asStat(value) {
|
|
2
|
+
return value && typeof value === 'object' ? value : undefined;
|
|
3
|
+
}
|
|
4
|
+
function nonNegativeNumber(value) {
|
|
5
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null;
|
|
6
|
+
}
|
|
7
|
+
/** @internal */
|
|
8
|
+
export function parsePeerStats(report, bufferedUnreliable, bufferedReliable) {
|
|
9
|
+
const byId = new Map();
|
|
10
|
+
let bytesSent = 0;
|
|
11
|
+
let bytesReceived = 0;
|
|
12
|
+
let selectedId;
|
|
13
|
+
let selectedPair;
|
|
14
|
+
let nominatedPair;
|
|
15
|
+
let succeededPair;
|
|
16
|
+
report.forEach((raw, id) => {
|
|
17
|
+
const stat = asStat(raw);
|
|
18
|
+
if (!stat)
|
|
19
|
+
return;
|
|
20
|
+
byId.set(id, stat);
|
|
21
|
+
if (stat.type === 'data-channel') {
|
|
22
|
+
bytesSent += nonNegativeNumber(stat.bytesSent) ?? 0;
|
|
23
|
+
bytesReceived += nonNegativeNumber(stat.bytesReceived) ?? 0;
|
|
24
|
+
}
|
|
25
|
+
else if (stat.type === 'transport' && typeof stat.selectedCandidatePairId === 'string') {
|
|
26
|
+
selectedId = stat.selectedCandidatePairId;
|
|
27
|
+
}
|
|
28
|
+
else if (stat.type === 'candidate-pair') {
|
|
29
|
+
if (stat.selected === true && !selectedPair)
|
|
30
|
+
selectedPair = stat;
|
|
31
|
+
if (stat.state === 'succeeded') {
|
|
32
|
+
if (stat.nominated === true && !nominatedPair)
|
|
33
|
+
nominatedPair = stat;
|
|
34
|
+
if (!succeededPair)
|
|
35
|
+
succeededPair = stat;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
let activePair;
|
|
40
|
+
if (selectedId) {
|
|
41
|
+
const pair = byId.get(selectedId);
|
|
42
|
+
if (pair?.type === 'candidate-pair')
|
|
43
|
+
activePair = pair;
|
|
44
|
+
}
|
|
45
|
+
activePair ??= selectedPair ?? nominatedPair ?? succeededPair;
|
|
46
|
+
const rttSec = activePair ? nonNegativeNumber(activePair.currentRoundTripTime) : null;
|
|
47
|
+
const rttMs = rttSec !== null ? Math.round(rttSec * 1000) : null;
|
|
48
|
+
let route = 'unknown';
|
|
49
|
+
if (activePair) {
|
|
50
|
+
const local = typeof activePair.localCandidateId === 'string'
|
|
51
|
+
? byId.get(activePair.localCandidateId)
|
|
52
|
+
: undefined;
|
|
53
|
+
const remote = typeof activePair.remoteCandidateId === 'string'
|
|
54
|
+
? byId.get(activePair.remoteCandidateId)
|
|
55
|
+
: undefined;
|
|
56
|
+
const localType = local?.type === 'local-candidate' && typeof local.candidateType === 'string'
|
|
57
|
+
? local.candidateType
|
|
58
|
+
: undefined;
|
|
59
|
+
const remoteType = remote?.type === 'remote-candidate' && typeof remote.candidateType === 'string'
|
|
60
|
+
? remote.candidateType
|
|
61
|
+
: undefined;
|
|
62
|
+
if (localType === 'relay' || remoteType === 'relay')
|
|
63
|
+
route = 'relay';
|
|
64
|
+
else if (localType && remoteType)
|
|
65
|
+
route = 'direct';
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
rttMs,
|
|
69
|
+
route,
|
|
70
|
+
bytesSent,
|
|
71
|
+
bytesReceived,
|
|
72
|
+
bufferedUnreliable,
|
|
73
|
+
bufferedReliable
|
|
74
|
+
};
|
|
75
|
+
}
|