@johnhenry/browsermesh-apps 0.9.0 → 0.11.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 +124 -1
- package/package.json +1 -1
- package/src/chunk-replication.mjs +2 -2
- package/src/index.mjs +1 -0
- package/src/internal/torrent-store.mjs +141 -0
- package/src/internal/wire-envelope.mjs +33 -0
- package/src/mesh-bootstrap.mjs +10 -1
- package/src/mesh-relay-backend.mjs +2 -0
- package/src/mesh-relay-host.mjs +3 -0
- package/src/mesh-service.mjs +28 -6
- package/src/mesh-sync.mjs +2 -0
- package/src/mesh-torrent.mjs +476 -166
- package/src/mesh-websocket.mjs +3 -1
- package/src/peer-ipfs.mjs +51 -112
- package/src/peer-node-transport.mjs +114 -0
- package/src/peer-node.mjs +94 -2
- package/src/peer-torrent.mjs +145 -13
package/src/peer-torrent.mjs
CHANGED
|
@@ -5,11 +5,29 @@
|
|
|
5
5
|
* CDN-loads webtorrent browser bundle for swarm-based file sharing.
|
|
6
6
|
* Falls back to direct chunked transfer when WebTorrent unavailable.
|
|
7
7
|
*
|
|
8
|
+
* DURABILITY (fallback path only): by default the fallback keeps whole blobs
|
|
9
|
+
* in a private in-memory map. Pass `chunkStore` and/or `manifestStore`
|
|
10
|
+
* (the same objects you give `createTorrentService()` in mesh-torrent.mjs)
|
|
11
|
+
* and the fallback instead stores content as SHA-256-addressed pieces in
|
|
12
|
+
* `chunkStore` plus one manifest per torrent in `manifestStore`, and
|
|
13
|
+
* `ensureLoaded()` restores `listTorrents()` from the manifests -- so seeded
|
|
14
|
+
* content survives a reload when those stores are durable (for example
|
|
15
|
+
* `IndexedDBChunkStore` from browsermesh-sync). Injected stores belong to the
|
|
16
|
+
* caller: `destroy()` never clears them. The real-WebTorrent path keeps its
|
|
17
|
+
* own storage and ignores both.
|
|
18
|
+
*
|
|
8
19
|
* Run tests:
|
|
9
20
|
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-peer-torrent.test.mjs
|
|
10
21
|
*/
|
|
11
22
|
|
|
12
23
|
import { ChunkStore } from '@johnhenry/browsermesh-sync'
|
|
24
|
+
import {
|
|
25
|
+
MemoryManifestStore,
|
|
26
|
+
toSeedBytes,
|
|
27
|
+
releaseChunks,
|
|
28
|
+
splitIntoChunks,
|
|
29
|
+
saveChunks,
|
|
30
|
+
} from './internal/torrent-store.mjs'
|
|
13
31
|
|
|
14
32
|
// ---------------------------------------------------------------------------
|
|
15
33
|
// Constants
|
|
@@ -127,6 +145,18 @@ export class TorrentManager {
|
|
|
127
145
|
/** @type {FallbackStore} */
|
|
128
146
|
#fallback = new FallbackStore()
|
|
129
147
|
|
|
148
|
+
/** Injected (or defaulted, when only one was injected) piece store; null = legacy in-memory blob map. */
|
|
149
|
+
#chunkStore = null
|
|
150
|
+
|
|
151
|
+
/** @type {object|null} manifest store paired with #chunkStore */
|
|
152
|
+
#manifestStore = null
|
|
153
|
+
|
|
154
|
+
/** @type {number} piece size used when writing to #chunkStore */
|
|
155
|
+
#chunkSize
|
|
156
|
+
|
|
157
|
+
/** @type {Set<Promise<void>>} in-flight best-effort store cleanups */
|
|
158
|
+
#cleanups = new Set()
|
|
159
|
+
|
|
130
160
|
/** @type {Function} */
|
|
131
161
|
#onLog
|
|
132
162
|
|
|
@@ -143,10 +173,22 @@ export class TorrentManager {
|
|
|
143
173
|
* @param {object} [opts]
|
|
144
174
|
* @param {string} [opts.trackerUrl] - Custom tracker URL
|
|
145
175
|
* @param {Function} [opts.onLog] - Logging callback
|
|
176
|
+
* @param {object} [opts.chunkStore] - Piece store with the `ChunkStore`
|
|
177
|
+
* contract (`save/get/has/remove`, sync or async), e.g. browsermesh-sync's
|
|
178
|
+
* `IndexedDBChunkStore`. Fallback path only; see the module comment.
|
|
179
|
+
* @param {object} [opts.manifestStore] - `{ get, set, delete, entries }`
|
|
180
|
+
* keyed by magnet URI (sync or async). Defaults to in-memory when only
|
|
181
|
+
* `chunkStore` is given.
|
|
182
|
+
* @param {number} [opts.chunkSize=65536] - Piece size when `chunkStore` is used.
|
|
146
183
|
*/
|
|
147
184
|
constructor(opts = {}) {
|
|
148
185
|
this.#trackerUrl = opts.trackerUrl ?? TORRENT_DEFAULTS.trackerUrl
|
|
149
186
|
this.#onLog = opts.onLog || (() => {})
|
|
187
|
+
this.#chunkSize = opts.chunkSize ?? TORRENT_DEFAULTS.chunkSize
|
|
188
|
+
if (opts.chunkStore || opts.manifestStore) {
|
|
189
|
+
this.#chunkStore = opts.chunkStore || new ChunkStore()
|
|
190
|
+
this.#manifestStore = opts.manifestStore || new MemoryManifestStore()
|
|
191
|
+
}
|
|
150
192
|
}
|
|
151
193
|
|
|
152
194
|
// ── CDN Loading ──────────────────────────────────────────────────────
|
|
@@ -190,6 +232,31 @@ export class TorrentManager {
|
|
|
190
232
|
}
|
|
191
233
|
|
|
192
234
|
this.#loaded = true
|
|
235
|
+
|
|
236
|
+
if (!this.#wtAvailable && this.#manifestStore) {
|
|
237
|
+
try {
|
|
238
|
+
await this.#restoreFromStores()
|
|
239
|
+
} catch (err) {
|
|
240
|
+
this.#onLog(1, `Restoring torrents from manifestStore failed: ${err.message}`)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Rebuild #activeTorrents from persisted manifests (fallback path only). */
|
|
246
|
+
async #restoreFromStores() {
|
|
247
|
+
for (const [magnetURI, m] of await this.#manifestStore.entries()) {
|
|
248
|
+
if (!m || this.#activeTorrents.has(magnetURI)) continue
|
|
249
|
+
this.#activeTorrents.set(magnetURI, {
|
|
250
|
+
magnetURI,
|
|
251
|
+
infoHash: m.infoHash,
|
|
252
|
+
name: m.name,
|
|
253
|
+
size: m.size,
|
|
254
|
+
peers: 0,
|
|
255
|
+
progress: 1,
|
|
256
|
+
speed: 0,
|
|
257
|
+
state: 'seeding',
|
|
258
|
+
})
|
|
259
|
+
}
|
|
193
260
|
}
|
|
194
261
|
|
|
195
262
|
/** Whether ensureLoaded() has been called. */
|
|
@@ -207,23 +274,23 @@ export class TorrentManager {
|
|
|
207
274
|
/**
|
|
208
275
|
* Seed a file — make it available for download.
|
|
209
276
|
*
|
|
210
|
-
* @param {Uint8Array|Blob|ArrayBuffer} data - File
|
|
277
|
+
* @param {string|Uint8Array|ArrayBufferView|Blob|ArrayBuffer} data - File
|
|
278
|
+
* content. A string is encoded as UTF-8; anything that is not one of the
|
|
279
|
+
* listed types throws a `TypeError` (it is never seeded as empty content).
|
|
211
280
|
* @param {object} [opts]
|
|
212
281
|
* @param {string} [opts.name] - File name
|
|
213
282
|
* @param {string[]} [opts.announce] - Tracker URLs
|
|
283
|
+
* @param {number} [opts.chunkSize] - Piece size for this torrent when
|
|
284
|
+
* `chunkStore` is injected (default: the constructor's `chunkSize`).
|
|
214
285
|
* @returns {Promise<TorrentInfo>}
|
|
215
286
|
*/
|
|
216
287
|
async seed(data, opts = {}) {
|
|
217
288
|
if (!this.#loaded) await this.ensureLoaded()
|
|
218
289
|
|
|
219
|
-
const rawData = data
|
|
220
|
-
? new Uint8Array(data)
|
|
221
|
-
: data instanceof Uint8Array
|
|
222
|
-
? data
|
|
223
|
-
: data
|
|
290
|
+
const rawData = await toSeedBytes(data)
|
|
224
291
|
|
|
225
292
|
const name = opts.name || `file_${Date.now()}`
|
|
226
|
-
const size = rawData.byteLength
|
|
293
|
+
const size = rawData.byteLength
|
|
227
294
|
|
|
228
295
|
// Real WebTorrent path
|
|
229
296
|
if (this.#wtAvailable && this.#client) {
|
|
@@ -231,7 +298,7 @@ export class TorrentManager {
|
|
|
231
298
|
}
|
|
232
299
|
|
|
233
300
|
// Fallback: generate pseudo-torrent info
|
|
234
|
-
return this.#seedFallback(rawData, name, size)
|
|
301
|
+
return this.#seedFallback(rawData, name, size, opts)
|
|
235
302
|
}
|
|
236
303
|
|
|
237
304
|
/**
|
|
@@ -276,13 +343,19 @@ export class TorrentManager {
|
|
|
276
343
|
* Seed via fallback in-memory store.
|
|
277
344
|
* @returns {Promise<TorrentInfo>}
|
|
278
345
|
*/
|
|
279
|
-
async #seedFallback(data, name, size) {
|
|
280
|
-
const bytes = data
|
|
346
|
+
async #seedFallback(data, name, size, opts = {}) {
|
|
347
|
+
const bytes = data
|
|
281
348
|
const cid = await ChunkStore.computeCid(bytes)
|
|
282
349
|
const infoHash = cid.slice(0, 40) // Use first 40 hex chars as pseudo info hash
|
|
283
350
|
const magnetURI = generateMagnetURI(infoHash)
|
|
284
351
|
|
|
285
|
-
this.#
|
|
352
|
+
if (this.#chunkStore) {
|
|
353
|
+
const chunkSize = opts.chunkSize ?? this.#chunkSize
|
|
354
|
+
const chunkCids = await saveChunks(this.#chunkStore, splitIntoChunks(bytes, chunkSize), (p) => ChunkStore.computeCid(p))
|
|
355
|
+
await this.#manifestStore.set(magnetURI, { infoHash, name, size, chunkSize, chunkCids, cid })
|
|
356
|
+
} else {
|
|
357
|
+
this.#fallback.set(infoHash, { data: bytes, name, size })
|
|
358
|
+
}
|
|
286
359
|
|
|
287
360
|
const info = {
|
|
288
361
|
magnetURI,
|
|
@@ -399,7 +472,9 @@ export class TorrentManager {
|
|
|
399
472
|
|
|
400
473
|
this.#emit('download:start', { magnetURI })
|
|
401
474
|
|
|
402
|
-
const entry = this.#
|
|
475
|
+
const entry = this.#chunkStore
|
|
476
|
+
? await this.#readFromStores(infoHash, magnetURI)
|
|
477
|
+
: this.#fallback.get(infoHash)
|
|
403
478
|
if (!entry) {
|
|
404
479
|
throw new Error(`Content not found for magnet: ${magnetURI}`)
|
|
405
480
|
}
|
|
@@ -424,6 +499,45 @@ export class TorrentManager {
|
|
|
424
499
|
return { data: entry.data, info }
|
|
425
500
|
}
|
|
426
501
|
|
|
502
|
+
/**
|
|
503
|
+
* The stored manifest for a torrent seeded through the injected stores:
|
|
504
|
+
* `{ infoHash, name, size, chunkSize, chunkCids, cid }`, or `null` when no
|
|
505
|
+
* stores were injected, the torrent is unknown, or the real-WebTorrent path
|
|
506
|
+
* is in use (which keeps its own storage).
|
|
507
|
+
*
|
|
508
|
+
* @param {string} magnetURI
|
|
509
|
+
* @returns {Promise<object|null>}
|
|
510
|
+
*/
|
|
511
|
+
async getManifest(magnetURI) {
|
|
512
|
+
if (!this.#manifestStore) return null
|
|
513
|
+
return (await this.#manifestStore.get(magnetURI)) ?? null
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Reassemble a torrent's bytes from the injected stores.
|
|
518
|
+
* @returns {Promise<{ data: Uint8Array, name: string, size: number }|null>} null when no manifest is held
|
|
519
|
+
*/
|
|
520
|
+
async #readFromStores(infoHash, magnetURI) {
|
|
521
|
+
const manifest = await this.#manifestStore.get(generateMagnetURI(infoHash))
|
|
522
|
+
?? await this.#manifestStore.get(magnetURI)
|
|
523
|
+
if (!manifest) return null
|
|
524
|
+
const data = new Uint8Array(manifest.size)
|
|
525
|
+
let offset = 0
|
|
526
|
+
for (const cid of manifest.chunkCids) {
|
|
527
|
+
const piece = await this.#chunkStore.get(cid)
|
|
528
|
+
if (!piece) throw new Error(`Content incomplete for magnet ${magnetURI}: piece ${cid} is missing from the chunk store`)
|
|
529
|
+
if (!(await ChunkStore.computeCid(piece) === cid)) {
|
|
530
|
+
throw new Error(`Content corrupt for magnet ${magnetURI}: piece ${cid} failed integrity verification`)
|
|
531
|
+
}
|
|
532
|
+
data.set(piece, offset)
|
|
533
|
+
offset += piece.length
|
|
534
|
+
}
|
|
535
|
+
if (offset !== manifest.size) {
|
|
536
|
+
throw new Error(`Content incomplete for magnet ${magnetURI}: ${offset} of ${manifest.size} bytes present`)
|
|
537
|
+
}
|
|
538
|
+
return { data, name: manifest.name, size: manifest.size }
|
|
539
|
+
}
|
|
540
|
+
|
|
427
541
|
// ── Mesh sharing ─────────────────────────────────────────────────────
|
|
428
542
|
|
|
429
543
|
/**
|
|
@@ -502,6 +616,19 @@ export class TorrentManager {
|
|
|
502
616
|
this.#fallback.delete(infoHash)
|
|
503
617
|
}
|
|
504
618
|
|
|
619
|
+
// Injected stores: drop the manifest, then any pieces no other torrent
|
|
620
|
+
// still lists. Best-effort and asynchronous (the return value stays sync).
|
|
621
|
+
if (this.#chunkStore) {
|
|
622
|
+
const cleanup = (async () => {
|
|
623
|
+
const manifest = await this.#manifestStore.get(magnetURI)
|
|
624
|
+
if (!manifest) return
|
|
625
|
+
await this.#manifestStore.delete(magnetURI)
|
|
626
|
+
await releaseChunks(this.#chunkStore, this.#manifestStore, manifest, magnetURI)
|
|
627
|
+
})().catch((err) => this.#onLog(1, `Store cleanup failed for ${magnetURI}: ${err.message}`))
|
|
628
|
+
.finally(() => this.#cleanups.delete(cleanup))
|
|
629
|
+
this.#cleanups.add(cleanup)
|
|
630
|
+
}
|
|
631
|
+
|
|
505
632
|
this.#activeTorrents.delete(magnetURI)
|
|
506
633
|
return true
|
|
507
634
|
}
|
|
@@ -578,9 +705,14 @@ export class TorrentManager {
|
|
|
578
705
|
// ── Lifecycle ────────────────────────────────────────────────────────
|
|
579
706
|
|
|
580
707
|
/**
|
|
581
|
-
* Destroy the manager:
|
|
708
|
+
* Destroy the manager: forget all torrents in memory, close the client.
|
|
709
|
+
* Content in injected `chunkStore`/`manifestStore` is left intact.
|
|
582
710
|
*/
|
|
583
711
|
async destroy() {
|
|
712
|
+
// Let any in-flight removeTorrent() store cleanup finish first. Injected
|
|
713
|
+
// stores themselves are never cleared: they belong to the caller.
|
|
714
|
+
await Promise.all([...this.#cleanups])
|
|
715
|
+
|
|
584
716
|
// Clear active torrents
|
|
585
717
|
this.#activeTorrents.clear()
|
|
586
718
|
this.#fallback.clear()
|