diff --git a/capacitor/package.json b/capacitor/package.json index 23c1a98..67a7499 100644 --- a/capacitor/package.json +++ b/capacitor/package.json @@ -44,6 +44,7 @@ "@capacitor/status-bar": "^5.0.6", "@superfrogbe/cordova-plugin-chrome-apps-sockets-udp": "github:superfrogbe/cordova-plugin-chrome-apps-sockets-udp", "capacitor-dns": "github:funniray/capacitor-dns", + "capacitor-os-interfaces-hack": "github:funniray/capacitor-os-interfaces-hack", "capacitor-plugin-safe-area": "^2.0.5", "common": "workspace:*", "cordova-plugin-chrome-apps-common": "^1.0.7", diff --git a/capacitor/src/chrome-dgram.js b/capacitor/src/chrome-dgram.js index 8ad4a5c..77dd824 100644 --- a/capacitor/src/chrome-dgram.js +++ b/capacitor/src/chrome-dgram.js @@ -93,7 +93,7 @@ class Socket extends EventEmitter { constructor (options, listener) { super() if (typeof options === 'string') options = { type: options } - if (options.type !== 'udp4') throw new Error('Bad socket type specified. Valid types are: udp4') + if (options.type !== 'udp4' && options.type !== 'udp6') throw new Error('Bad socket type specified. Valid types are: udp4') if (typeof listener === 'function') this.on('message', listener) diff --git a/capacitor/src/ipc.js b/capacitor/src/ipc.js index 493eb33..7aa942c 100644 --- a/capacitor/src/ipc.js +++ b/capacitor/src/ipc.js @@ -32,6 +32,8 @@ async function portRequest (data) { } } await globalThis.controller + await globalThis.prefetchNetworkInterfaces + await new Promise(resolve => setTimeout(() => resolve(), 50)) ipcRendererUI.emit('port', { ports: [port2] }) ipcRendererWebTorrent.emit('port', { ports: [port1] }) } diff --git a/capacitor/src/webtorrent.js b/capacitor/src/webtorrent.js index dbf1a39..d29ec60 100644 --- a/capacitor/src/webtorrent.js +++ b/capacitor/src/webtorrent.js @@ -1,5 +1,6 @@ import TorrentClient from 'common/modules/webtorrent.js' import { ipcRendererWebTorrent } from './ipc.js' +import { prefetchNetworkInterfaces } from 'os' globalThis.chrome.runtime = { lastError: false, id: 'something' } @@ -18,6 +19,7 @@ const controller = (async () => { if (!checkState(worker)) worker.addEventListener('statechange', ({ target }) => checkState(target)) }) })() +globalThis.prefetchNetworkInterfaces = await prefetchNetworkInterfaces() globalThis.controller = controller async function storageQuota () { @@ -25,4 +27,6 @@ async function storageQuota () { return quota - usage } +await prefetchNetworkInterfaces + globalThis.client = new TorrentClient(ipcRendererWebTorrent, storageQuota, 'browser', controller, { torrentPort: Math.floor(Math.random() * 65535 + 1) }) diff --git a/capacitor/webpack.config.cjs b/capacitor/webpack.config.cjs index dbb7084..6cc5dcb 100644 --- a/capacitor/webpack.config.cjs +++ b/capacitor/webpack.config.cjs @@ -31,10 +31,8 @@ const capacitorConfig = { } const alias = { fs: false, - os: false, ws: false, - dns: 'capacitor-dns', - '@silentbot1/nat-api': false, + 'default-gateway': false, 'load-ip-set': false, 'node-fetch': false, 'webtorrent/lib/utp.cjs': false, @@ -42,8 +40,12 @@ const alias = { '@/modules/support.js': join(__dirname, 'src', 'support.js'), net: join(__dirname, 'src', 'chrome-net.js'), dgram: join(__dirname, 'src', 'chrome-dgram.js'), + os: 'capacitor-os-interfaces-hack', + dns: 'capacitor-dns', http: 'stream-http', https: 'stream-http', + 'utp-native': false, + socks: false, assert: 'assert', ut_pex: 'ut_pex', path: 'path-esm', @@ -52,10 +54,14 @@ const alias = { timers: 'timers-browserify', crypto: 'crypto-browserify', buffer: 'buffer', + '@silentbot1/nat-api': '@silentbot1/nat-api', 'bittorrent-tracker': 'bittorrent-tracker', querystring: 'querystring', zlib: 'webtorrent/polyfills/inflate-sync-web.js', + 'bittorrent-tracker/server.js': false, + 'cross-fetch-ponyfill': resolve('../node_modules/cross-fetch-ponyfill/browser.js'), + 'abort-controller': false, 'bittorrent-tracker/lib/client/http-tracker.js': resolve('../node_modules/bittorrent-tracker/lib/client/http-tracker.js') } -module.exports = merge(commonConfig(__dirname, alias, 'chromeapp', 'index'), capacitorConfig) +module.exports = merge(commonConfig(__dirname, alias, 'node', 'index'), capacitorConfig) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fab6399..de50c66 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -83,6 +83,9 @@ importers: capacitor-dns: specifier: github:funniray/capacitor-dns version: github.com/funniray/capacitor-dns/6362cd1ad0a6b9b5bdbdc9fee3fba4c8dae91e16(@capacitor/core@5.5.1) + capacitor-os-interfaces-hack: + specifier: github:funniray/capacitor-os-interfaces-hack + version: github.com/funniray/capacitor-os-interfaces-hack/95a51afe9dee88b3ae8df4b1b96fd106c1655954(@capacitor/core@5.5.1) capacitor-plugin-safe-area: specifier: ^2.0.5 version: 2.0.5(@capacitor/core@5.5.1) @@ -8310,6 +8313,17 @@ packages: '@capacitor/core': 5.5.1 dev: false + github.com/funniray/capacitor-os-interfaces-hack/95a51afe9dee88b3ae8df4b1b96fd106c1655954(@capacitor/core@5.5.1): + resolution: {tarball: https://codeload.github.com/funniray/capacitor-os-interfaces-hack/tar.gz/95a51afe9dee88b3ae8df4b1b96fd106c1655954} + id: github.com/funniray/capacitor-os-interfaces-hack/95a51afe9dee88b3ae8df4b1b96fd106c1655954 + name: capacitor-os-interfaces-hack + version: 0.0.1 + peerDependencies: + '@capacitor/core': ^5.0.0 + dependencies: + '@capacitor/core': 5.5.1 + dev: false + github.com/superfrogbe/cordova-plugin-chrome-apps-sockets-udp/4b740017299c81cfc7d5b49c7d6122a6650b57d4: resolution: {tarball: https://codeload.github.com/superfrogbe/cordova-plugin-chrome-apps-sockets-udp/tar.gz/4b740017299c81cfc7d5b49c7d6122a6650b57d4} name: '@superfrogbe/cordova-plugin-chrome-apps-sockets-udp'