Skip to content

Commit

Permalink
Merge pull request #20 from fabianbormann/feature/universal_module
Browse files Browse the repository at this point in the history
feat: use dynamic imports to prevent meerkat from throwing errors in …
  • Loading branch information
fabianbormann authored Nov 27, 2024
2 parents 6fd8895 + 63a2c04 commit 2242d33
Show file tree
Hide file tree
Showing 6 changed files with 1,668 additions and 7,967 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 56 additions & 23 deletions meerkat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import nacl from 'tweetnacl';
import type { SignKeyPair, BoxKeyPair } from 'tweetnacl';
import bs58check from 'bs58check';
import { Buffer } from 'buffer';
import WebTorrent from 'webtorrent';
import type { Wire, ExtensionConstructor } from 'bittorrent-protocol';
import bs58 from 'bs58';
import ripemd160 from 'ripemd160';
Expand All @@ -15,6 +14,7 @@ import {
const PEER_TIMEOUT = 5 * 60 * 1000;
const EXT = 'bo_channel';
import Logger from './logger';
let WebTorrent: any = null;

export default class Meerkat extends EventEmitter {
announce: Array<String>;
Expand All @@ -39,9 +39,14 @@ export default class Meerkat extends EventEmitter {

constructor(parameters: MeerkatParameters = {}) {
super();
let isBrowserEnvironmet = true;
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
console.warn('Meerkat is designed to run in a browser environment.');
isBrowserEnvironmet = false;
}

const { identifier, announce, seed, loggingEnabled } = parameters;
this.logger = new Logger({ scope: 'Meerkat', enabled: loggingEnabled });

this.announce = announce || [
'wss://tracker.openwebtorrent.com',
'wss://dev.btt.cf-identity-wallet.metadata.dev.cf-deployments.org',
Expand All @@ -66,29 +71,57 @@ export default class Meerkat extends EventEmitter {
this.logger.debug(`Meerkat address: ${this.identifier}`);
this.lastwirecount = null;

this.webTorrent = new WebTorrent({});

this.torrent = this.webTorrent.seed(
Buffer.from(this.identifier),
{
name: this.identifier,
announce: this.announce,
},
() => {
this.emit('torrent', this.identifier, this.torrent);
if (this.torrent.discovery.tracker) {
this.torrent.discovery.tracker.on('update', (update: any) => {
this.emit('tracker', this.identifier, update);
if (isBrowserEnvironmet) {
this.configureTorrent();
}
}

private async dynamicImportWebTorrent() {
if (WebTorrent === null) {
try {
const module = await import(/* webpackMode: "eager" */ 'webtorrent');
WebTorrent = module.default;
} catch (error) {
this.logger.error('Error loading WebTorrent:', error);
this.logger.warn(
'Meerkat will not be able to connect to peers. Please make sure using meerkat in a browser environment.'
);
}
}
}

private async configureTorrent() {
await this.dynamicImportWebTorrent();

if (WebTorrent === null) {
this.logger.warn(
'WebTorrent is not available in the current environment.'
);
} else {
this.webTorrent = new WebTorrent({});

this.torrent = this.webTorrent.seed(
Buffer.from(this.identifier),
{
name: this.identifier,
announce: this.announce,
},
() => {
this.emit('torrent', this.identifier, this.torrent);
if (this.torrent.discovery.tracker) {
this.torrent.discovery.tracker.on('update', (update: any) => {
this.emit('tracker', this.identifier, update);
});
}
this.torrent.discovery.on('trackerAnnounce', () => {
this.emit('announce', this.identifier);
this.connections();
});
}
this.torrent.discovery.on('trackerAnnounce', () => {
this.emit('announce', this.identifier);
this.connections();
});
}
);
this.torrentCreated = true;
this.torrent.on('wire', (wire: Wire) => this.attach(wire));
);
this.torrentCreated = true;
this.torrent.on('wire', (wire: Wire) => this.attach(wire));
}
}

disableLogging() {
Expand Down
Loading

0 comments on commit 2242d33

Please sign in to comment.