From e7935729f3b40584cd3cd9de0e571f89ec336194 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 31 Aug 2023 18:54:47 -0400 Subject: [PATCH] refactor: switch to @lumeweb/libs5-transport-hyper --- npm-shrinkwrap.json | 18 +++++++-- package.json | 3 +- src/hyperTransport.ts | 90 ------------------------------------------- src/index.ts | 17 ++++++-- 4 files changed, 29 insertions(+), 99 deletions(-) delete mode 100644 src/hyperTransport.ts diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 1078977..7233989 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -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", diff --git a/package.json b/package.json index 183e488..f025257 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/hyperTransport.ts b/src/hyperTransport.ts deleted file mode 100644 index 9c75c62..0000000 --- a/src/hyperTransport.ts +++ /dev/null @@ -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; - 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 { - return Promise.reject("not supported"); - } - - listenForMessages( - callback: (event: any) => Promise, - { - 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); - } -} diff --git a/src/index.ts b/src/index.ts index 902b2bb..ebc5e66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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);