Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Fix Windows torrent startup after cross-platform packaging (#74)

Anmol Kapil8cc15e4ca1b5
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 8195ce7..1fdf4d1 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -67,3 +67,27 @@ jobs:
name: playwright-report
path: playwright-report
retention-days: 14
+
+ windows-torrent:
+ name: Windows torrent engine
+ runs-on: windows-latest
+ steps:
+ - uses: actions/checkout@v4
+
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 22
+ cache: npm
+
+ # Deliberately don't install native binaries: importing the TCP torrent engine must work
+ # without node-datachannel, just as it must in the Windows installer.
+ - name: Install dependencies without native binaries
+ run: npm ci --ignore-scripts
+
+ - name: Apply dependency patches
+ run: npx patch-package
+
+ - name: Check torrent engine without WebRTC
+ run: npx playwright test e2e/torrentNative.spec.ts
+ env:
+ PLEXO_E2E_SKIP_BUILD: '1'
diff --git a/README.md b/README.md
index e27e7d2..0b0ca9f 100644
--- a/README.md
+++ b/README.md
@@ -131,7 +131,7 @@ Each block is written straight to its place in `<filename>.plexo`, a staging fil
### Torrents pin each peer to a network
-Plexo runs torrents with [WebTorrent](https://github.com/webtorrent/webtorrent) and opens every peer connection from one network's address. To keep every peer on a network Plexo chose, it turns off the parts that would connect on their own: uTP, web seeds, UPnP and NAT-PMP port mapping, and local peer discovery. Peers are found through trackers and the DHT. Torrents that are v2-only aren't supported yet.
+Plexo runs torrents with [WebTorrent](https://github.com/webtorrent/webtorrent) and opens every peer connection from one network's address. To keep every peer on a network Plexo chose, it turns off the parts that would connect on their own: uTP, WebRTC, web seeds, UPnP and NAT-PMP port mapping, and local peer discovery. Peers are found through HTTP/UDP trackers and the DHT. Torrents that are v2-only aren't supported yet.
### Resuming checks the file hasn't changed
diff --git a/e2e/torrentNative.spec.ts b/e2e/torrentNative.spec.ts
new file mode 100644
index 0000000..95d3db2
--- /dev/null
+++ b/e2e/torrentNative.spec.ts
@@ -0,0 +1,25 @@
+import { execFile } from 'node:child_process'
+import { promisify } from 'node:util'
+import { expect, test } from '@playwright/test'
+
+test('torrent engine loads without native WebRTC binaries @smoke', async () => {
+ // A fresh process avoids a cached import hiding the failure. Packaged Windows builds don't
+ // contain the unused WebRTC binary; refuse native loads here to reproduce that environment.
+ const { stdout } = await promisify(execFile)(process.execPath, [
+ '--input-type=module',
+ '-e',
+ `
+ import assert from 'node:assert/strict'
+ process.dlopen = () => { throw new Error('Unexpected native addon load') }
+ const { default: WebTorrent } = await import('webtorrent')
+ assert.equal(WebTorrent.WEBRTC_SUPPORT, false)
+ const client = new WebTorrent({
+ utp: false, dht: false, tracker: { wrtc: false },
+ lsd: false, natUpnp: false, natPmp: false
+ })
+ await new Promise((resolve, reject) => client.destroy(error => error ? reject(error) : resolve()))
+ console.log('ok')
+ `
+ ])
+ expect(stdout.trim()).toBe('ok')
+})
diff --git a/electron-builder.yml b/electron-builder.yml
index 152426d..65d9537 100644
--- a/electron-builder.yml
+++ b/electron-builder.yml
@@ -1,5 +1,6 @@
appId: com.plexo.app
productName: Plexo
+afterPack: scripts/check-torrent-package.mjs
directories:
buildResources: build
files:
@@ -11,6 +12,10 @@ files:
# koffi is only loaded on Linux (network/deviceBinding.ts) and ships prebuilds for every OS.
- '!node_modules/koffi/{src,vendor,doc}/**'
- '!node_modules/koffi/build/koffi/!(linux_x64|linux_arm64)/**'
+ # Plexo uses TCP peers only. simple-peer is patched to use browser globals instead of loading
+ # the native WebRTC polyfill; don't ship unused host-platform binaries in cross-platform builds.
+ - '!node_modules/node-datachannel/**'
+ - '!node_modules/webrtc-polyfill/**'
- '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}'
asarUnpack:
- resources/**
diff --git a/package-lock.json b/package-lock.json
index 532d422..1ceccef 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "plexo",
- "version": "1.0.0-rc.12",
+ "version": "1.0.0-rc.13",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "plexo",
- "version": "1.0.0-rc.12",
+ "version": "1.0.0-rc.13",
"hasInstallScript": true,
"dependencies": {
"@base-ui/react": "^1.8.0",
diff --git a/package.json b/package.json
index b72638c..931399a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "plexo",
- "version": "1.0.0-rc.12",
+ "version": "1.0.0-rc.13",
"description": "Speed up downloads by combining multiple network connections in parallel",
"main": "./out/main/index.js",
"author": "Anmol Kapil",
diff --git a/patches/@thaunknown+simple-peer+10.1.2.patch b/patches/@thaunknown+simple-peer+10.1.2.patch
new file mode 100644
index 0000000..062f0ad
--- /dev/null
+++ b/patches/@thaunknown+simple-peer+10.1.2.patch
@@ -0,0 +1,13 @@
+diff --git a/node_modules/@thaunknown/simple-peer/lite.js b/node_modules/@thaunknown/simple-peer/lite.js
+--- a/node_modules/@thaunknown/simple-peer/lite.js
++++ b/node_modules/@thaunknown/simple-peer/lite.js
+@@ -1,6 +1,8 @@
+ /*! simple-peer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
+ import debug from 'debug'
+-import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } from 'webrtc-polyfill'
++// Plexo binds TCP peers to selected networks; WebRTC peers bypass that hook.
++// Avoid loading the unused native polyfill when importing the torrent engine.
++const { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } = globalThis
+ import { Duplex } from 'streamx'
+ import errCode from 'err-code'
+ import { randomBytes, arr2hex, text2arr } from 'uint8-util'
diff --git a/scripts/check-torrent-package.mjs b/scripts/check-torrent-package.mjs
new file mode 100644
index 0000000..c11f92e
--- /dev/null
+++ b/scripts/check-torrent-package.mjs
@@ -0,0 +1,14 @@
+import { listPackage } from '@electron/asar'
+import { join } from 'node:path'
+
+// Check the actual archive, including entries marked as unpacked. A cross-platform build must
+// never ship the host's unused WebRTC native binary (rc.12 shipped a macOS binary on Windows).
+export default async function checkTorrentPackage(context) {
+ const resources = context.packager.getResourcesDir(context.appOutDir)
+ const unused = listPackage(join(resources, 'app.asar')).filter((entry) =>
+ /\/node_modules\/(node-datachannel|webrtc-polyfill)(\/|$)/.test(entry)
+ )
+ if (unused.length) {
+ throw new Error(`Unused WebRTC dependencies in packaged app: ${unused.join(', ')}`)
+ }
+}
diff --git a/src/main/download/torrent/engine.ts b/src/main/download/torrent/engine.ts
index 4ed2e87..584d1fe 100644
--- a/src/main/download/torrent/engine.ts
+++ b/src/main/download/torrent/engine.ts
@@ -59,6 +59,8 @@ export async function storeNamed(name: string, onWritten: (path: string) => void
* A webtorrent client set up for Plexo:
* - uTP is off: its UDP sockets can't be pinned to a network the way `connect` pins TCP ones.
* - Web seeds are off: webtorrent fetches them over HTTP itself, again past `connect`.
+ * - WebRTC is off: those peers bypass the TCP hook too. The simple-peer patch also removes
+ * its eager native polyfill import, so loading the engine doesn't need node-datachannel.
* - No port mapping (UPnP, NAT-PMP) and no local peer discovery: nothing beyond the peers.
*/
export async function createClient(
@@ -68,6 +70,7 @@ export async function createClient(
return new Client({
utp: false,
webSeeds: false,
+ tracker: { wrtc: false },
natUpnp: false,
natPmp: false,
lsd: false,