feat: Initial version
This commit is contained in:
parent
c443eaa7e1
commit
4444afa5fa
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"name": "@lumeweb/relay-plugin-s5",
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "gitea@git.lumeweb.com:LumeWeb/relay-plugin-s5.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@lumeweb/presetter-relay-plugin-preset": "^0.1.0-develop.1",
|
||||
"ed25519-keygen": "^0.4.8",
|
||||
"presetter": "^4.1.2"
|
||||
},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"_id": "@lumeweb/relay-plugin-s5@0.1.0",
|
||||
"scripts": {
|
||||
"prepare": "presetter bootstrap",
|
||||
"build": "run build",
|
||||
"semantic-release": "semantic-release"
|
||||
},
|
||||
"dependencies": {
|
||||
"@lumeweb/interface-relay": "^0.0.2-develop.3",
|
||||
"@lumeweb/libs5": "^0.1.0-develop.9",
|
||||
"@types/streamx": "^2.9.1",
|
||||
"streamx": "^2.15.1"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"core": {
|
||||
"plugins": [
|
||||
"s5"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"plugins": {
|
||||
"s5": {
|
||||
"p2p": {
|
||||
"peers": {
|
||||
"initial": [
|
||||
"wss://z2DWuWNZcdSyZLpXFK2uCU3haaWMXrDAgxzv17sDEMHstZb@s5.garden/s5/p2p",
|
||||
"wss://z2DWuPbL5pweybXnEB618pMnV58ECj2VPDNfVGm3tFqBvjF@s5.ninja/s5/p2p"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
export const PROTOCOL = "lumeweb.service.s5";
|
|
@ -0,0 +1,90 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
import type { PluginAPI } from "@lumeweb/interface-relay";
|
||||
import { createKeyPair, createNode, S5NodeConfig } from "@lumeweb/libs5";
|
||||
import { Level } from "level";
|
||||
import { PROTOCOL } from "./constants.js";
|
||||
import HyperTransportPeer from "./hyperTransport.js";
|
||||
import { NodeId } from "@lumeweb/libs5";
|
||||
import { string } from "micro-packed";
|
||||
|
||||
const plugin = {
|
||||
name: "s5",
|
||||
async plugin(api: PluginAPI) {
|
||||
const db = new Level<string, Uint8Array>(
|
||||
api.pluginConfig.str("db") as string,
|
||||
);
|
||||
await db.open();
|
||||
let config = {
|
||||
keyPair: createKeyPair(api.identity.publicKeyRaw),
|
||||
db,
|
||||
p2p: {
|
||||
peers: {
|
||||
initial: api.pluginConfig.array("p2p.peers.initial") ?? [],
|
||||
},
|
||||
},
|
||||
} as S5NodeConfig;
|
||||
|
||||
const node = createNode(config);
|
||||
|
||||
await node.start();
|
||||
|
||||
api.swarm.join(api.util.crypto.createHash(PROTOCOL));
|
||||
api.protocols.register(PROTOCOL, async (peer: any, muxer: any) => {
|
||||
const s5peer = new HyperTransportPeer(peer, [], muxer);
|
||||
|
||||
s5peer.id = new NodeId(peer.remotePublicKey);
|
||||
|
||||
await s5peer.init();
|
||||
node.services.p2p.onNewPeer(s5peer, true);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export default plugin;
|
Loading…
Reference in New Issue