From 336c1996e4870e00b51eeb70e917bed6c183ea27 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 16 Apr 2023 20:18:20 -0400 Subject: [PATCH] *Refactor to use new MultiSocketProxy from libhyperproxy --- package.json | 18 ++++-- patches/b4a@1.6.3.patch | 36 ++++++++++++ src/index.ts | 124 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 160 insertions(+), 18 deletions(-) create mode 100644 patches/b4a@1.6.3.patch diff --git a/package.json b/package.json index bf30195..e8e3343 100644 --- a/package.json +++ b/package.json @@ -14,13 +14,16 @@ }, "type": "module", "dependencies": { - "@lumeweb/hypercore-proxy-handshake": "git+https://git.lumeweb.com/LumeWeb/hypercore-proxy-handshake.git", "@lumeweb/kernel-swarm-client": "git+https://git.lumeweb.com/LumeWeb/kernel-swarm-client.git", - "libkmodule": "^0.2.53" + "@lumeweb/libhyperproxy": "git+https://git.lumeweb.com/LumeWeb/libhyperproxy.git", + "hsd": "github:handshake-org/hsd", + "libkmodule": "^0.2.53", + "p-defer": "^4.0.0" }, "devDependencies": { + "@i2labs/dns": "^1.0.1", "@rollup/plugin-alias": "^4.0.4", - "@rollup/plugin-commonjs": "^24.0.1", + "@rollup/plugin-commonjs": "^24.1.0", "@rollup/plugin-inject": "^5.0.3", "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.0.2", @@ -32,14 +35,14 @@ "@types/read": "^0.0.29", "@types/streamx": "^2.9.1", "cli-progress": "^3.12.0", - "esbuild": "^0.17.15", + "esbuild": "^0.17.17", "eventemitter3": "^5.0.0", "node-stdlib-browser": "^1.2.0", "os-browserify": "^0.3.0", "path-browserify": "^1.0.1", "prettier": "^2.8.7", - "read": "^2.0.0", - "rollup": "^3.20.2", + "read": "^2.1.0", + "rollup": "^3.20.3", "rollup-plugin-polyfill": "^4.0.0", "rollup-plugin-polyfill-inject": "^1.0.4", "rollup-plugin-polyfill-node": "^0.12.0", @@ -54,6 +57,9 @@ "pnpm": { "overrides": { "protomux": "git+https://git.lumeweb.com/LumeWeb/kernel-protomux-client.git" + }, + "patchedDependencies": { + "b4a@1.6.3": "patches/b4a@1.6.3.patch" } } } diff --git a/patches/b4a@1.6.3.patch b/patches/b4a@1.6.3.patch new file mode 100644 index 0000000..13e4b09 --- /dev/null +++ b/patches/b4a@1.6.3.patch @@ -0,0 +1,36 @@ +diff --git a/browser.js b/browser.js +index e07f78d17b7b4a2963e4f0062047cc96e6025f9e..a9f266a2644f3f8ebd0f6684b3e11a7519059851 100644 +--- a/browser.js ++++ b/browser.js +@@ -3,6 +3,7 @@ const base64 = require('./lib/base64') + const hex = require('./lib/hex') + const utf8 = require('./lib/utf8') + const utf16le = require('./lib/utf16le') ++const Buffer = require('buffer').Buffer + + const LE = new Uint8Array(Uint16Array.of(0xff).buffer)[0] === 0xff + +diff --git a/index.js b/index.js +index a751d6b4d27c7dd903efbfdc87a98f61044a62b8..8bd32640afdcfbc475168d458c51f8b9162d1656 100644 +--- a/index.js ++++ b/index.js +@@ -1,3 +1,5 @@ ++const Buffer = require('buffer').Buffer ++ + function isBuffer (value) { + return Buffer.isBuffer(value) || value instanceof Uint8Array + } +diff --git a/package.json b/package.json +index eb0f792709b27e66f26658054781b4099507b5ed..69c4ff39c48b0bdd5f0bc7ebbade517e7c5d76e4 100644 +--- a/package.json ++++ b/package.json +@@ -8,9 +8,6 @@ + "index.js", + "lib" + ], +- "browser": { +- "./index.js": "./browser.js" +- }, + "scripts": { + "test": "standard && brittle test/*.mjs" + }, \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index df10d55..9456491 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,16 @@ import type { ActiveQuery } from "libkmodule"; import { addHandler, handleMessage } from "libkmodule"; import { createClient } from "@lumeweb/kernel-swarm-client"; -import HandshakeProxy from "@lumeweb/hypercore-proxy-handshake"; +import { + createServer, + DummySocket, + MultiSocketProxy, +} from "@lumeweb/libhyperproxy"; +// @ts-ignore +import { SPVNode } from "hsd/lib/node"; +import defer from "p-defer"; +import dns from "@i2labs/dns"; +import assert from "assert"; const PROTOCOL = "lumeweb.proxy.handshake"; @@ -17,11 +26,12 @@ addHandler("ready", handleReady); addHandler("query", handleQuery); let swarm; -let proxy: HandshakeProxy; +let proxy: MultiSocketProxy; +let node: SPVNode; function resolveWithPeers(resolve: Function) { - if (!proxy.node.pool.peers.head()) { - proxy.node.pool.on("peer", () => { + if (!node.pool.peers.head()) { + node.pool.on("peer", () => { resolveWithPeers(resolve); }); return; @@ -29,15 +39,15 @@ function resolveWithPeers(resolve: Function) { let syncable = false; - for (let peer = proxy.node.pool.peers.head(); peer; peer = peer.next) { - if (proxy.node.pool.isSyncable(peer)) { + for (let peer = node.pool.peers.head(); peer; peer = peer.next) { + if (node.pool.isSyncable(peer)) { syncable = true; break; } } if (!syncable) { - for (let peer = proxy.node.pool.peers.head(); peer; peer = peer.next) { + for (let peer = node.pool.peers.head(); peer; peer = peer.next) { const listener = () => { peer.off("open", listener); resolve(); @@ -52,11 +62,101 @@ function resolveWithPeers(resolve: Function) { async function handlePresentSeed(aq: ActiveQuery) { swarm = createClient(); - proxy = new HandshakeProxy({ swarm, listen: true }); + + const peerConnected = defer(); + node = new SPVNode({ + config: false, + argv: false, + env: false, + noDns: true, + memory: false, + logFile: false, + logConsole: true, + logLevel: "info", + workers: true, + network: "main", + createServer, + createSocket: (port: number, host: string) => { + const socket = proxy.createSocket({ + host, + port, + }) as unknown as DummySocket; + socket.connect(); + + return socket; + }, + }); + + node.pool.hosts.resolve = async (host: any, family?: any) => { + if (family == null) family = null; + + assert(family === null || family === 4 || family === 6); + + const stub = new dns.promises.Resolver(); + + stub.setServers([ + // Cloudflare + "1.1.1.1", + // Google + "8.8.8.8", + "8.8.4.4", + // OpenDNS + "208.67.222.222", + "208.67.220.220", + "208.67.222.220", + "208.67.220.222", + ]); + + const out = []; + const types = []; + + if (family == null || family === 4) types.push("A"); + + if (family == null || family === 6) types.push("AAAA"); + + for (const type of types) { + let addrs; + + try { + addrs = await stub.resolve(host, type as any); + } catch (e) { + continue; + } + + // @ts-ignore + out.push(...addrs); + } + + if (out.length === 0) throw new Error("No DNS results."); + + return out; + }; + + if (node?.http?.http?.listen) { + node.http.http.listen = (port: number, host: string, cb: Function) => cb(); + } + + proxy = new MultiSocketProxy({ + protocol: PROTOCOL, + swarm, + server: false, + autostart: true, + listen: true, + }); + + proxy.on("peerChannelOpen", () => { + peerConnected.resolve(); + }); swarm.join(PROTOCOL); await swarm.start(); + await peerConnected.promise; + + await node.open(); + await node.connect(); + await node.startSync(); + moduleLoadedResolve(); } @@ -64,11 +164,11 @@ async function handleReady(aq: ActiveQuery) { await moduleLoaded; await new Promise((resolve): void => { - if (proxy.node.chain.synced) { + if (node.chain.synced) { return resolveWithPeers(resolve); } - proxy.node.pool.once("full", () => { + node.pool.once("full", () => { resolveWithPeers(resolve); }); }); @@ -77,13 +177,13 @@ async function handleReady(aq: ActiveQuery) { } async function handleQuery(aq: ActiveQuery) { - if (!proxy.node.chain.synced || !proxy.node.pool.peers.head()) { + if (!node.chain.synced || !node.pool.peers.head()) { aq.reject("not ready"); return; } try { - aq.respond(await proxy.node.rpc.call(aq.callerInput)); + aq.respond(await node.rpc.call(aq.callerInput)); } catch (e) { aq.reject((e as Error).message); }