*Refactor to use new MultiSocketProxy from libhyperproxy

This commit is contained in:
Derrick Hammer 2023-04-16 20:18:20 -04:00
parent 48810569d8
commit 336c1996e4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 160 additions and 18 deletions

View File

@ -14,13 +14,16 @@
}, },
"type": "module", "type": "module",
"dependencies": { "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", "@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": { "devDependencies": {
"@i2labs/dns": "^1.0.1",
"@rollup/plugin-alias": "^4.0.4", "@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-inject": "^5.0.3",
"@rollup/plugin-json": "^6.0.0", "@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2", "@rollup/plugin-node-resolve": "^15.0.2",
@ -32,14 +35,14 @@
"@types/read": "^0.0.29", "@types/read": "^0.0.29",
"@types/streamx": "^2.9.1", "@types/streamx": "^2.9.1",
"cli-progress": "^3.12.0", "cli-progress": "^3.12.0",
"esbuild": "^0.17.15", "esbuild": "^0.17.17",
"eventemitter3": "^5.0.0", "eventemitter3": "^5.0.0",
"node-stdlib-browser": "^1.2.0", "node-stdlib-browser": "^1.2.0",
"os-browserify": "^0.3.0", "os-browserify": "^0.3.0",
"path-browserify": "^1.0.1", "path-browserify": "^1.0.1",
"prettier": "^2.8.7", "prettier": "^2.8.7",
"read": "^2.0.0", "read": "^2.1.0",
"rollup": "^3.20.2", "rollup": "^3.20.3",
"rollup-plugin-polyfill": "^4.0.0", "rollup-plugin-polyfill": "^4.0.0",
"rollup-plugin-polyfill-inject": "^1.0.4", "rollup-plugin-polyfill-inject": "^1.0.4",
"rollup-plugin-polyfill-node": "^0.12.0", "rollup-plugin-polyfill-node": "^0.12.0",
@ -54,6 +57,9 @@
"pnpm": { "pnpm": {
"overrides": { "overrides": {
"protomux": "git+https://git.lumeweb.com/LumeWeb/kernel-protomux-client.git" "protomux": "git+https://git.lumeweb.com/LumeWeb/kernel-protomux-client.git"
},
"patchedDependencies": {
"b4a@1.6.3": "patches/b4a@1.6.3.patch"
} }
} }
} }

36
patches/b4a@1.6.3.patch Normal file
View File

@ -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"
},

View File

@ -1,7 +1,16 @@
import type { ActiveQuery } from "libkmodule"; import type { ActiveQuery } from "libkmodule";
import { addHandler, handleMessage } from "libkmodule"; import { addHandler, handleMessage } from "libkmodule";
import { createClient } from "@lumeweb/kernel-swarm-client"; 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"; const PROTOCOL = "lumeweb.proxy.handshake";
@ -17,11 +26,12 @@ addHandler("ready", handleReady);
addHandler("query", handleQuery); addHandler("query", handleQuery);
let swarm; let swarm;
let proxy: HandshakeProxy; let proxy: MultiSocketProxy;
let node: SPVNode;
function resolveWithPeers(resolve: Function) { function resolveWithPeers(resolve: Function) {
if (!proxy.node.pool.peers.head()) { if (!node.pool.peers.head()) {
proxy.node.pool.on("peer", () => { node.pool.on("peer", () => {
resolveWithPeers(resolve); resolveWithPeers(resolve);
}); });
return; return;
@ -29,15 +39,15 @@ function resolveWithPeers(resolve: Function) {
let syncable = false; let syncable = false;
for (let peer = proxy.node.pool.peers.head(); peer; peer = peer.next) { for (let peer = node.pool.peers.head(); peer; peer = peer.next) {
if (proxy.node.pool.isSyncable(peer)) { if (node.pool.isSyncable(peer)) {
syncable = true; syncable = true;
break; break;
} }
} }
if (!syncable) { 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 = () => { const listener = () => {
peer.off("open", listener); peer.off("open", listener);
resolve(); resolve();
@ -52,11 +62,101 @@ function resolveWithPeers(resolve: Function) {
async function handlePresentSeed(aq: ActiveQuery) { async function handlePresentSeed(aq: ActiveQuery) {
swarm = createClient(); 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); swarm.join(PROTOCOL);
await swarm.start(); await swarm.start();
await peerConnected.promise;
await node.open();
await node.connect();
await node.startSync();
moduleLoadedResolve(); moduleLoadedResolve();
} }
@ -64,11 +164,11 @@ async function handleReady(aq: ActiveQuery) {
await moduleLoaded; await moduleLoaded;
await new Promise((resolve): void => { await new Promise((resolve): void => {
if (proxy.node.chain.synced) { if (node.chain.synced) {
return resolveWithPeers(resolve); return resolveWithPeers(resolve);
} }
proxy.node.pool.once("full", () => { node.pool.once("full", () => {
resolveWithPeers(resolve); resolveWithPeers(resolve);
}); });
}); });
@ -77,13 +177,13 @@ async function handleReady(aq: ActiveQuery) {
} }
async function handleQuery(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"); aq.reject("not ready");
return; return;
} }
try { try {
aq.respond(await proxy.node.rpc.call(aq.callerInput)); aq.respond(await node.rpc.call(aq.callerInput));
} catch (e) { } catch (e) {
aq.reject((e as Error).message); aq.reject((e as Error).message);
} }