|
| 1 | +/** |
| 2 | + * AR.IO Gateway |
| 3 | + * Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +import { Logger } from 'winston'; |
| 20 | +import WebTorrent from 'webtorrent'; |
| 21 | +import { ContiguousDataSource } from '../types.js'; |
| 22 | + |
| 23 | +export class SeedingWorker { |
| 24 | + private log: Logger; |
| 25 | + private contiguousDataSource: ContiguousDataSource; |
| 26 | + |
| 27 | + public webTorrentClient: WebTorrent.Instance; |
| 28 | + |
| 29 | + constructor({ |
| 30 | + log, |
| 31 | + contiguousDataSource, |
| 32 | + }: { |
| 33 | + log: Logger; |
| 34 | + contiguousDataSource: ContiguousDataSource; |
| 35 | + }) { |
| 36 | + this.webTorrentClient = new WebTorrent(); |
| 37 | + this.contiguousDataSource = contiguousDataSource; |
| 38 | + this.log = log.child({ class: 'SeedingWorker' }); |
| 39 | + } |
| 40 | + |
| 41 | + async seed(txId: string) { |
| 42 | + this.log.debug(`Seeding ${txId}`); |
| 43 | + const data = await this.contiguousDataSource.getData({ id: txId }); |
| 44 | + await new Promise<void>((resolve) => |
| 45 | + this.webTorrentClient.seed( |
| 46 | + data.stream, |
| 47 | + { |
| 48 | + announce: [ |
| 49 | + 'wss://tracker.btorrent.xyz', |
| 50 | + 'wss://tracker.openwebtorrent.com', |
| 51 | + 'wss://tracker.webtorrent.io', |
| 52 | + ], |
| 53 | + }, |
| 54 | + (torrent: WebTorrent.Torrent) => { |
| 55 | + this.log.debug(`Seeding ${txId} started: ${torrent.magnetURI}`); |
| 56 | + resolve(); |
| 57 | + }, |
| 58 | + ), |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
0 commit comments