refactor: switch to @lumeweb/libs5-transport-hyper

This commit is contained in:
Derrick Hammer 2023-08-31 18:54:47 -04:00
parent 9fcfc686cf
commit e7935729f3
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 29 additions and 99 deletions

18
npm-shrinkwrap.json generated
View File

@ -9,7 +9,8 @@
"version": "0.1.0-develop.2",
"dependencies": {
"@lumeweb/interface-relay": "^0.0.2-develop.3",
"@lumeweb/libs5": "^0.1.0-develop.9",
"@lumeweb/libs5": "^0.1.0-develop.14",
"@lumeweb/libs5-transport-hyper": "^0.1.0-develop.5",
"@types/streamx": "^2.9.1",
"streamx": "^2.15.1"
},
@ -1817,9 +1818,9 @@
}
},
"node_modules/@lumeweb/libs5": {
"version": "0.1.0-develop.9",
"resolved": "https://registry.npmjs.org/@lumeweb/libs5/-/libs5-0.1.0-develop.9.tgz",
"integrity": "sha512-t4Lo9g6xmzCTCaxtva6R4klk3z4TEh+TDVUCkt7MrP0WLqRamEqckQkPn99xDgio1epgOQGnXz/rHrfGmqSsiQ==",
"version": "0.1.0-develop.14",
"resolved": "https://registry.npmjs.org/@lumeweb/libs5/-/libs5-0.1.0-develop.14.tgz",
"integrity": "sha512-nLRaDl3c355iDIUU40ES9P5FZvgPtpRJNnAymaJeOxQVjI9TlO+4aGiACB8Wv0uywjSfy6CHvYMS32UZNLhIHA==",
"dependencies": {
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",
@ -1830,6 +1831,15 @@
"ws": "^8.13.0"
}
},
"node_modules/@lumeweb/libs5-transport-hyper": {
"version": "0.1.0-develop.5",
"resolved": "https://registry.npmjs.org/@lumeweb/libs5-transport-hyper/-/libs5-transport-hyper-0.1.0-develop.5.tgz",
"integrity": "sha512-xLc7MhL9dIR7WkaLntkpU56XLNeko3FElB29Rm9q/IEG7xBiwD807ikjxPrBb8R/a/H7c7w1aUAlZVu4U26xxQ==",
"dependencies": {
"@lumeweb/libs5": "^0.1.0-develop.13",
"streamx": "^2.15.1"
}
},
"node_modules/@lumeweb/node-library-preset": {
"version": "0.2.7",
"resolved": "https://registry.npmjs.org/@lumeweb/node-library-preset/-/node-library-preset-0.2.7.tgz",

View File

@ -19,7 +19,8 @@
},
"dependencies": {
"@lumeweb/interface-relay": "^0.0.2-develop.3",
"@lumeweb/libs5": "^0.1.0-develop.9",
"@lumeweb/libs5": "^0.1.0-develop.14",
"@lumeweb/libs5-transport-hyper": "^0.1.0-develop.5",
"@types/streamx": "^2.9.1",
"streamx": "^2.15.1"
}

View File

@ -1,90 +0,0 @@
import { Logger, Peer, PeerStatic } from "@lumeweb/libs5";
import { URL } from "url";
import NodeId from "@lumeweb/libs5/lib/nodeId.js";
import { Buffer } from "buffer";
import { PROTOCOL } from "./constants.js";
import { Readable } from "streamx";
export default class HyperTransportPeer implements Peer {
challenge: Uint8Array;
connectionUris: Array<URL>;
isConnected: boolean = false;
private _peer: any;
private _muxer: any;
private _socket = new Readable();
private _pipe?: any;
constructor(peer: any, connectionUris: URL[], muxer: any) {
this.connectionUris = connectionUris.map((uri) => new URL(uri.toString()));
this.challenge = new Uint8Array();
this._peer = peer;
this._muxer = muxer;
}
private _id?: NodeId;
get id(): NodeId {
return this._id as NodeId;
}
set id(value: NodeId) {
this._id = value;
}
public async init() {
const channel = await this._muxer.createChannel({
protocol: PROTOCOL,
});
const self = this;
this._pipe = await channel.addMessage({
async onmessage(m) {
if (m instanceof Uint8Array) {
m = Buffer.from(m);
}
self._socket.push(m);
},
});
await channel.open();
}
public static async connect(uri: URL): Promise<any> {
return Promise.reject("not supported");
}
listenForMessages(
callback: (event: any) => Promise<void>,
{
onDone,
onError,
logger,
}: {
onDone?: any;
onError?: (...args: any[]) => void;
logger: Logger;
},
): void {
this._socket.on("data", async (data: Buffer) => {
await callback(data);
});
if (onDone) {
this._socket.on("end", onDone);
}
if (onError) {
this._socket.on("error", onError);
}
}
renderLocationUri(): string {
return "Hypercore client";
}
sendMessage(message: Uint8Array): void {
this._pipe.write(message);
}
}

View File

@ -1,9 +1,14 @@
import type { PluginAPI } from "@lumeweb/interface-relay";
import { createKeyPair, createNode, S5NodeConfig } from "@lumeweb/libs5";
import {
S5NodeConfig,
createKeyPair,
createNode,
NodeId,
} from "@lumeweb/libs5";
import HyperTransportPeer from "@lumeweb/libs5-transport-hyper";
import { Level } from "level";
import { PROTOCOL } from "./constants.js";
import HyperTransportPeer from "./hyperTransport.js";
import { NodeId } from "@lumeweb/libs5";
import * as fs from "fs/promises";
const plugin = {
@ -35,7 +40,11 @@ const plugin = {
api.swarm.join(api.util.crypto.createHash(PROTOCOL));
api.protocols.register(PROTOCOL, async (peer: any, muxer: any) => {
const s5peer = new HyperTransportPeer(peer, [], muxer);
const s5peer = new HyperTransportPeer({
muxer,
peer,
protocol: PROTOCOL,
});
s5peer.id = new NodeId(peer.remotePublicKey);