Compare commits

...

4 Commits

Author SHA1 Message Date
semantic-release-bot 1ab97adce3 chore(release): 0.1.0-develop.1 [skip ci]
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/relay-plugin-s5/compare/v0.0.1...v0.1.0-develop.1) (2023-08-31)

### Features

* Initial version ([4444afa](4444afa5fa))
2023-08-31 16:05:43 +00:00
Derrick Hammer 908aacdae6
ci: override moduleResolution 2023-08-31 12:05:01 -04:00
Derrick Hammer 7ff8957f03
ci: setup 2023-08-31 12:02:15 -04:00
Derrick Hammer 4444afa5fa
feat: Initial version 2023-08-31 11:57:11 -04:00
10 changed files with 19868 additions and 0 deletions

13
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,13 @@
name: Build/Publish
on:
push:
branches:
- master
- develop
- develop-*
jobs:
main:
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
secrets: inherit

12
.presetterrc.json Normal file
View File

@ -0,0 +1,12 @@
{
"preset": [
"@lumeweb/presetter-relay-plugin-preset"
],
"config": {
"tsconfig": {
"compilerOptions": {
"moduleResolution": "node"
}
}
}
}

6
CHANGELOG.md Normal file
View File

@ -0,0 +1,6 @@
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/relay-plugin-s5/compare/v0.0.1...v0.1.0-develop.1) (2023-08-31)
### Features
* Initial version ([4444afa](https://git.lumeweb.com/LumeWeb/relay-plugin-s5/commit/4444afa5fa9ce1f873917324fce8be4393d0dadc))

19657
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "@lumeweb/relay-plugin-s5",
"version": "0.1.0-develop.1",
"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!",
"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"
}
}

7
pkg/load-s5.json Normal file
View File

@ -0,0 +1,7 @@
{
"core": {
"plugins": [
"s5"
]
}
}

14
pkg/s5.json Normal file
View File

@ -0,0 +1,14 @@
{
"plugins": {
"s5": {
"p2p": {
"peers": {
"initial": [
"wss://z2DWuWNZcdSyZLpXFK2uCU3haaWMXrDAgxzv17sDEMHstZb@s5.garden/s5/p2p",
"wss://z2DWuPbL5pweybXnEB618pMnV58ECj2VPDNfVGm3tFqBvjF@s5.ninja/s5/p2p"
]
}
}
}
}
}

1
src/constants.ts Normal file
View File

@ -0,0 +1 @@
export const PROTOCOL = "lumeweb.service.s5";

90
src/hyperTransport.ts Normal file
View File

@ -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);
}
}

42
src/index.ts Normal file
View File

@ -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;