hypercore-proxy-handshake/dist/index.js

76 lines
2.6 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-ignore
const node_1 = require("hsd/lib/node");
// @ts-ignore
const net_1 = require("hsd/lib/net");
// @ts-ignore
const sodium_universal_1 = __importDefault(require("sodium-universal"));
const b4a_1 = __importDefault(require("b4a"));
const PROTOCOL = "lumeweb.proxy.handshake";
const libhyperproxy_1 = require("@lumeweb/libhyperproxy");
class HandshakeProxy extends libhyperproxy_1.Proxy {
constructor({ swarm, listen = false, join = false, }) {
super({
swarm,
listen,
autostart: true,
protocol: PROTOCOL,
async onopen(socket) {
const peer = net_1.Peer.fromInbound(self._node.pool.options, socket);
peer.connected = false;
peer.outbound = true;
if (self._node.pool.peers.map.has(peer.hostname()) ||
self._node.pool.peers.ids.has(peer.id)) {
return { connect: false };
}
self._node.pool.bindPeer(peer);
peer.tryOpen();
// @ts-ignore
socket.emit("connect");
self._node.pool.peers.add(peer);
self._node.pool.connectedGroups.add(peer.address.getGroup());
self._node.pool.emit("peer", peer);
return { connect: false };
},
});
const self = this;
this._join = join;
}
get node() {
return this._node;
}
async _init() {
this._node = new node_1.SPVNode({
config: false,
argv: false,
env: false,
noDns: true,
memory: false,
logFile: false,
logConsole: true,
logLevel: "info",
workers: true,
network: "main",
createServer: libhyperproxy_1.createServer,
createSocket: libhyperproxy_1.createSocket,
});
if (this?._node?.http?.http?.listen) {
this._node.http.http.listen = (port, host, cb) => cb();
}
await this._node.open();
this._node.pool.connected = true;
this._node.startSync();
if (this._join) {
const topic = b4a_1.default.from(PROTOCOL);
const topicHash = b4a_1.default.allocUnsafe(32);
sodium_universal_1.default.crypto_generichash(topicHash, topic);
this.swarm.join(topicHash);
}
}
}
exports.default = HandshakeProxy;