refactor: use new sdks and portal system
This commit is contained in:
parent
2ebe64b33a
commit
d22b636889
|
@ -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
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"preset": [
|
||||
"@lumeweb/node-library-preset"
|
||||
]
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
/// <reference types="node" />
|
||||
import { Buffer } from "buffer";
|
||||
import { Client } from "@lumeweb/libkernel-universal";
|
||||
import { ErrTuple } from "@siaweb/libweb";
|
||||
import type { eventNS, event, ListenerFn, OnOptions, Listener } from "eventemitter2";
|
||||
export declare class SwarmClient extends Client {
|
||||
private useDefaultSwarm;
|
||||
private id;
|
||||
private _autoReconnect;
|
||||
private _connectBackoff;
|
||||
private _ready?;
|
||||
private _connectionListener?;
|
||||
private _topics;
|
||||
private _sockets;
|
||||
get dht(): {
|
||||
ready(): Promise<void>;
|
||||
};
|
||||
constructor(useDefaultDht?: boolean, autoReconnect?: boolean);
|
||||
get swarm(): number | undefined;
|
||||
connect(pubkey: string | Uint8Array): Promise<Socket>;
|
||||
init(): Promise<ErrTuple>;
|
||||
ready(): Promise<void>;
|
||||
start(): Promise<void>;
|
||||
private _listen;
|
||||
addRelay(pubkey: string): Promise<void>;
|
||||
removeRelay(pubkey: string): Promise<void>;
|
||||
clearRelays(): Promise<void>;
|
||||
getRelays(): Promise<string[]>;
|
||||
join(topic: Buffer | Uint8Array | string): Promise<void>;
|
||||
}
|
||||
export declare class Socket extends Client {
|
||||
private id;
|
||||
private eventUpdates;
|
||||
private syncMutex;
|
||||
private swarm;
|
||||
private userData?;
|
||||
constructor(id: number, swarm: SwarmClient);
|
||||
private _remotePublicKey?;
|
||||
get remotePublicKey(): Uint8Array;
|
||||
private _rawStream?;
|
||||
get rawStream(): Uint8Array;
|
||||
setup(): Promise<void>;
|
||||
on(event: event | eventNS, listener: ListenerFn, options?: boolean | OnOptions): this | Listener;
|
||||
off(event: event | eventNS, listener: ListenerFn): this;
|
||||
write(message: string | Buffer): void;
|
||||
end(): void;
|
||||
private ensureEvent;
|
||||
private trackEvent;
|
||||
}
|
||||
export declare const MODULE = "_AU-CzTIzrnnJSoVjfWG_d3cAb8_hONuZ1XaqbFlygrFlg";
|
||||
export declare const createClient: (...args: any) => SwarmClient;
|
||||
//# sourceMappingURL=index.d.ts.map
|
|
@ -1 +0,0 @@
|
|||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,MAAM,EAAW,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAU,QAAQ,EAAY,MAAM,gBAAgB,CAAC;AAU5D,OAAO,KAAK,EACV,OAAO,EACP,KAAK,EACL,UAAU,EACV,SAAS,EACT,QAAQ,EACT,MAAM,eAAe,CAAC;AAEvB,qBAAa,WAAY,SAAQ,MAAM;IACrC,OAAO,CAAC,eAAe,CAAU;IACjC,OAAO,CAAC,EAAE,CAAa;IACvB,OAAO,CAAC,cAAc,CAAU;IAChC,OAAO,CAAC,eAAe,CAAM;IAE7B,OAAO,CAAC,MAAM,CAAC,CAAgB;IAC/B,OAAO,CAAC,mBAAmB,CAAC,CAG1B;IAEF,OAAO,CAAC,OAAO,CAA0C;IACzD,OAAO,CAAC,QAAQ,CAAkD;IAElE,IAAI,GAAG;;MAON;gBAEW,aAAa,UAAO,EAAE,aAAa,UAAQ;IAcvD,IAAI,KAAK,IAAI,MAAM,GAAG,SAAS,CAE9B;IAEY,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAiB5D,IAAI,IAAI,OAAO,CAAC,QAAQ,CAAC;IA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,CAAC;AAEvE,eAAO,MAAM,YAAY,+BAA4C,CAAC"}
|
|
@ -1,179 +0,0 @@
|
|||
import { Client, factory } from "@lumeweb/libkernel-universal";
|
||||
import { hexToBuf } from "@siaweb/libweb";
|
||||
import { blake2b } from "@noble/hashes/blake2b";
|
||||
import b4a from "b4a";
|
||||
// @ts-ignore
|
||||
import Backoff from "backoff.js";
|
||||
import { Mutex } from "async-mutex";
|
||||
export class SwarmClient extends Client {
|
||||
useDefaultSwarm;
|
||||
id = 0;
|
||||
_autoReconnect;
|
||||
_connectBackoff;
|
||||
_ready;
|
||||
_connectionListener;
|
||||
_topics = new Set();
|
||||
_sockets = new Map();
|
||||
get dht() {
|
||||
const self = this;
|
||||
return {
|
||||
async ready() {
|
||||
return self.ready();
|
||||
},
|
||||
};
|
||||
}
|
||||
constructor(useDefaultDht = true, autoReconnect = false) {
|
||||
super();
|
||||
this.useDefaultSwarm = useDefaultDht;
|
||||
this._autoReconnect = autoReconnect;
|
||||
this._connectBackoff = new Backoff({
|
||||
strategy: "fibo",
|
||||
maxAttempts: Number.MAX_SAFE_INTEGER,
|
||||
});
|
||||
this._connectBackoff.on("retry", (error) => {
|
||||
this.logErr(error);
|
||||
});
|
||||
}
|
||||
get swarm() {
|
||||
return this.useDefaultSwarm ? undefined : this.id;
|
||||
}
|
||||
async connect(pubkey) {
|
||||
if (typeof pubkey === "string") {
|
||||
const buf = hexToBuf(pubkey);
|
||||
pubkey = this.handleErrorOrReturn(buf);
|
||||
}
|
||||
let existing = Array.from(this._sockets.values()).filter((socket) => {
|
||||
return b4a.equals(socket.remotePublicKey, pubkey);
|
||||
});
|
||||
if (existing.length) {
|
||||
return existing[0];
|
||||
}
|
||||
throw new Error("not implemented");
|
||||
}
|
||||
async init() {
|
||||
return await this.callModuleReturn("init", { swarm: this.swarm });
|
||||
}
|
||||
async ready() {
|
||||
if (this._ready) {
|
||||
return this._ready;
|
||||
}
|
||||
this._listen();
|
||||
this._ready = this.callModuleReturn("ready", { swarm: this.swarm });
|
||||
await this._ready;
|
||||
this._ready = undefined;
|
||||
for (const topic of this._topics) {
|
||||
await this.join(topic);
|
||||
}
|
||||
}
|
||||
async start() {
|
||||
await this._connectBackoff.run(() => this.init());
|
||||
await this.ready();
|
||||
}
|
||||
async _listen() {
|
||||
if (!this._connectionListener) {
|
||||
this._connectionListener = this.connectModule("listenConnections", { swarm: this.swarm }, async (socketId) => {
|
||||
const socket = this._sockets.get(socketId) ?? (await createSocket(socketId, this));
|
||||
socket.on("close", () => {
|
||||
this._sockets.delete(socketId);
|
||||
});
|
||||
if (!this._sockets.has(socketId)) {
|
||||
this._sockets.set(socketId, socket);
|
||||
}
|
||||
this.emit("connection", socket);
|
||||
});
|
||||
}
|
||||
await this._connectionListener[1];
|
||||
this._connectionListener = undefined;
|
||||
this.start();
|
||||
}
|
||||
async addRelay(pubkey) {
|
||||
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
|
||||
}
|
||||
async removeRelay(pubkey) {
|
||||
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
|
||||
}
|
||||
async clearRelays() {
|
||||
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
|
||||
}
|
||||
async getRelays() {
|
||||
return this.callModuleReturn("getRelays", { swarm: this.swarm });
|
||||
}
|
||||
async join(topic) {
|
||||
if (typeof topic === "string") {
|
||||
topic = blake2b(topic, { dkLen: 32 });
|
||||
}
|
||||
this._topics.add(topic);
|
||||
this.callModule("join", { id: this.id, topic });
|
||||
}
|
||||
}
|
||||
export class Socket extends Client {
|
||||
id;
|
||||
eventUpdates = {};
|
||||
syncMutex = new Mutex();
|
||||
swarm;
|
||||
userData = null;
|
||||
constructor(id, swarm) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.swarm = swarm;
|
||||
}
|
||||
_remotePublicKey;
|
||||
get remotePublicKey() {
|
||||
return this._remotePublicKey;
|
||||
}
|
||||
_rawStream;
|
||||
get rawStream() {
|
||||
return this._rawStream;
|
||||
}
|
||||
async setup() {
|
||||
let info = await this.callModuleReturn("socketGetInfo", { id: this.id });
|
||||
this._remotePublicKey = info.remotePublicKey;
|
||||
this._rawStream = info.rawStream;
|
||||
await this.swarm.emitAsync("setup", this);
|
||||
}
|
||||
on(event, listener, options) {
|
||||
const [update, promise] = this.connectModule("socketListenEvent", { id: this.id, event: event }, (data) => {
|
||||
this.emit(event, data);
|
||||
});
|
||||
this.trackEvent(event, update);
|
||||
promise.then(() => {
|
||||
this.off(event, listener);
|
||||
});
|
||||
return super.on(event, listener, options);
|
||||
}
|
||||
off(event, listener) {
|
||||
const updates = [...this.eventUpdates[event]];
|
||||
this.eventUpdates[event] = [];
|
||||
for (const func of updates) {
|
||||
func();
|
||||
}
|
||||
return super.off(event, listener);
|
||||
}
|
||||
write(message) {
|
||||
this.callModule("socketWrite", { id: this.id, message });
|
||||
}
|
||||
end() {
|
||||
this.callModule("socketExists", { id: this.id }).then(([exists]) => {
|
||||
if (exists) {
|
||||
this.callModule("socketClose", { id: this.id });
|
||||
}
|
||||
});
|
||||
}
|
||||
ensureEvent(event) {
|
||||
if (!(event in this.eventUpdates)) {
|
||||
this.eventUpdates[event] = [];
|
||||
}
|
||||
}
|
||||
trackEvent(event, update) {
|
||||
this.ensureEvent(event);
|
||||
this.eventUpdates[event].push(update);
|
||||
}
|
||||
}
|
||||
export const MODULE = "_AU-CzTIzrnnJSoVjfWG_d3cAb8_hONuZ1XaqbFlygrFlg";
|
||||
export const createClient = factory(SwarmClient, MODULE);
|
||||
const socketFactory = factory(Socket, MODULE);
|
||||
const createSocket = async (...args) => {
|
||||
const socket = socketFactory(...args);
|
||||
await socket.setup();
|
||||
return socket;
|
||||
};
|
File diff suppressed because it is too large
Load Diff
39
package.json
39
package.json
|
@ -2,23 +2,32 @@
|
|||
"name": "@lumeweb/kernel-swarm-client",
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"dependencies": {
|
||||
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
|
||||
"@noble/hashes": "^1.3.0",
|
||||
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git",
|
||||
"async-mutex": "^0.4.0",
|
||||
"b4a": "^1.6.3",
|
||||
"backoff.js": "^1.0.4",
|
||||
"p-defer": "^4.0.0",
|
||||
"protomux": "git+https://git.lumeweb.com/LumeWeb/protomux.git"
|
||||
"main": "lib/index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "gitea@git.lumeweb.com:LumeWeb/kernel-swarm-client.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@lumeweb/node-library-preset": "^0.2.7",
|
||||
"@types/b4a": "^1.6.0",
|
||||
"@types/node": "^18.15.11",
|
||||
"eventemitter2": "^6.4.9",
|
||||
"prettier": "^2.8.7",
|
||||
"pretty": "^2.0.0",
|
||||
"typescript": "^4.9.5"
|
||||
"presetter": "*"
|
||||
},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"_id": "@lumeweb/kernel-swarm-client@0.1.0",
|
||||
"scripts": {
|
||||
"prepare": "presetter bootstrap",
|
||||
"build": "run build",
|
||||
"semantic-release": "semantic-release"
|
||||
},
|
||||
"dependencies": {
|
||||
"@lumeweb/libkernel": "^0.1.0-develop.8",
|
||||
"async-mutex": "^0.4.0",
|
||||
"b4a": "^1.6.4"
|
||||
},
|
||||
"files": [
|
||||
"lib/**"
|
||||
],
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
|
|
27
src/index.ts
27
src/index.ts
|
@ -1,6 +1,6 @@
|
|||
import { Buffer } from "buffer";
|
||||
import { Client, factory } from "@lumeweb/libkernel-universal";
|
||||
import { DataFn, ErrTuple, hexToBuf } from "@siaweb/libweb";
|
||||
import { Client, factory } from "@lumeweb/libkernel/module";
|
||||
import { DataFn, ErrTuple, hexToBuf, logErr } from "@lumeweb/libkernel";
|
||||
import { blake2b } from "@noble/hashes/blake2b";
|
||||
import b4a from "b4a";
|
||||
|
||||
|
@ -27,7 +27,7 @@ export class SwarmClient extends Client {
|
|||
private _ready?: Promise<void>;
|
||||
private _connectionListener?: [
|
||||
sendUpdate: DataFn,
|
||||
response: Promise<ErrTuple>
|
||||
response: Promise<ErrTuple>,
|
||||
];
|
||||
|
||||
private _topics: Set<Uint8Array> = new Set<Uint8Array>();
|
||||
|
@ -42,8 +42,8 @@ export class SwarmClient extends Client {
|
|||
};
|
||||
}
|
||||
|
||||
constructor(useDefaultDht = true, autoReconnect = false) {
|
||||
super();
|
||||
constructor(module: string, useDefaultDht = true, autoReconnect = false) {
|
||||
super(module);
|
||||
this.useDefaultSwarm = useDefaultDht;
|
||||
this._autoReconnect = autoReconnect;
|
||||
this._connectBackoff = new Backoff({
|
||||
|
@ -52,7 +52,7 @@ export class SwarmClient extends Client {
|
|||
});
|
||||
|
||||
this._connectBackoff.on("retry", (error: any) => {
|
||||
this.logErr(error);
|
||||
logErr(error);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -123,7 +123,7 @@ export class SwarmClient extends Client {
|
|||
}
|
||||
|
||||
this.emit("connection", socket);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -173,8 +173,8 @@ export class Socket extends Client {
|
|||
private swarm: SwarmClient;
|
||||
private userData?: any = null;
|
||||
|
||||
constructor(id: number, swarm: SwarmClient) {
|
||||
super();
|
||||
constructor(module: string, id: number, swarm: SwarmClient) {
|
||||
super(module);
|
||||
this.id = id;
|
||||
this.swarm = swarm;
|
||||
}
|
||||
|
@ -202,14 +202,14 @@ export class Socket extends Client {
|
|||
on(
|
||||
event: event | eventNS,
|
||||
listener: ListenerFn,
|
||||
options?: boolean | OnOptions
|
||||
options?: boolean | OnOptions,
|
||||
): this | Listener {
|
||||
const [update, promise] = this.connectModule(
|
||||
"socketListenEvent",
|
||||
{ id: this.id, event: event },
|
||||
(data: any) => {
|
||||
this.emit(event, data);
|
||||
}
|
||||
},
|
||||
);
|
||||
this.trackEvent(event as string, update);
|
||||
|
||||
|
@ -239,7 +239,7 @@ export class Socket extends Client {
|
|||
if (exists) {
|
||||
this.callModule("socketClose", { id: this.id });
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -255,7 +255,8 @@ export class Socket extends Client {
|
|||
}
|
||||
}
|
||||
|
||||
export const MODULE = "_AU-CzTIzrnnJSoVjfWG_d3cAb8_hONuZ1XaqbFlygrFlg";
|
||||
export const MODULE =
|
||||
"zduSB7gEXpcHo4F1esrpHRaB9nYh6KtTn3bktnpJRxWnpvqPhaLNmPwAp7";
|
||||
|
||||
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
||||
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"strict": true,
|
||||
"module": "esnext",
|
||||
"target": "esnext",
|
||||
"esModuleInterop": true,
|
||||
"sourceMap": false,
|
||||
"rootDir": "src",
|
||||
"outDir": "dist",
|
||||
"typeRoots": [
|
||||
"node_modules/@types",
|
||||
],
|
||||
"moduleResolution": "node",
|
||||
"declarationMap": true,
|
||||
"declarationDir": "dist",
|
||||
"emitDeclarationOnly": false,
|
||||
"allowJs": true
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue