Compare commits
No commits in common. "develop" and "master" have entirely different histories.
|
@ -1,13 +0,0 @@
|
||||||
name: Build/Publish
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- develop
|
|
||||||
- develop-*
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
main:
|
|
||||||
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
|
|
||||||
secrets: inherit
|
|
|
@ -1,5 +0,0 @@
|
||||||
{
|
|
||||||
"preset": [
|
|
||||||
"@lumeweb/node-library-preset"
|
|
||||||
]
|
|
||||||
}
|
|
84
CHANGELOG.md
84
CHANGELOG.md
|
@ -1,84 +0,0 @@
|
||||||
# [0.1.0-develop.13](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.12...v0.1.0-develop.13) (2023-11-17)
|
|
||||||
|
|
||||||
# [0.1.0-develop.12](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.11...v0.1.0-develop.12) (2023-10-13)
|
|
||||||
|
|
||||||
# [0.1.0-develop.11](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.10...v0.1.0-develop.11) (2023-10-12)
|
|
||||||
|
|
||||||
# [0.1.0-develop.10](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.9...v0.1.0-develop.10) (2023-09-09)
|
|
||||||
|
|
||||||
# [0.1.0-develop.9](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.8...v0.1.0-develop.9) (2023-09-03)
|
|
||||||
|
|
||||||
# [0.1.0-develop.8](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.7...v0.1.0-develop.8) (2023-09-02)
|
|
||||||
|
|
||||||
# [0.1.0-develop.7](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.6...v0.1.0-develop.7) (2023-09-02)
|
|
||||||
|
|
||||||
# [0.1.0-develop.6](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.5...v0.1.0-develop.6) (2023-09-02)
|
|
||||||
|
|
||||||
# [0.1.0-develop.5](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.4...v0.1.0-develop.5) (2023-07-29)
|
|
||||||
|
|
||||||
# [0.1.0-develop.4](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.3...v0.1.0-develop.4) (2023-07-24)
|
|
||||||
|
|
||||||
# [0.1.0-develop.3](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.2...v0.1.0-develop.3) (2023-07-23)
|
|
||||||
|
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
* add setKeepAlive method ([19dceed](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/commit/19dceedab299c1cd35563c5c88a3f608cfc56deb))
|
|
||||||
|
|
||||||
# [0.1.0-develop.2](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.1.0-develop.1...v0.1.0-develop.2) (2023-07-23)
|
|
||||||
|
|
||||||
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.14...v0.1.0-develop.1) (2023-07-22)
|
|
||||||
|
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
* add getListeners method to Socket ([0ec1dd1](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/commit/0ec1dd1aacdce1e85d6250db9b61a3edccfaafa7))
|
|
||||||
|
|
||||||
## [0.0.2-develop.14](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.13...v0.0.2-develop.14) (2023-07-22)
|
|
||||||
|
|
||||||
|
|
||||||
### Bug Fixes
|
|
||||||
|
|
||||||
* missing return ([8380254](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/commit/8380254638dde806ce38d3406ea6b4127cb2444b))
|
|
||||||
|
|
||||||
## [0.0.2-develop.13](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.12...v0.0.2-develop.13) (2023-07-22)
|
|
||||||
|
|
||||||
|
|
||||||
### Bug Fixes
|
|
||||||
|
|
||||||
* on write and end, log errors if we are trying to use a closed socket ([57efedd](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/commit/57efedd0affd5e647db185aff0ba1be2a137aef3))
|
|
||||||
|
|
||||||
## [0.0.2-develop.12](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.11...v0.0.2-develop.12) (2023-07-22)
|
|
||||||
|
|
||||||
|
|
||||||
### Bug Fixes
|
|
||||||
|
|
||||||
* change on to always return an instance of emittery unsubscribe function ([0c18c92](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/commit/0c18c92521de8b8685882b88ad8efa74a76617c6))
|
|
||||||
* return type required ([7eabf6a](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/commit/7eabf6a05da9dcd833b55f386896a70fa6ec736d))
|
|
||||||
|
|
||||||
## [0.0.2-develop.11](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.10...v0.0.2-develop.11) (2023-07-22)
|
|
||||||
|
|
||||||
## [0.0.2-develop.10](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.9...v0.0.2-develop.10) (2023-07-22)
|
|
||||||
|
|
||||||
## [0.0.2-develop.9](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.8...v0.0.2-develop.9) (2023-07-12)
|
|
||||||
|
|
||||||
## [0.0.2-develop.8](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.7...v0.0.2-develop.8) (2023-07-08)
|
|
||||||
|
|
||||||
## [0.0.2-develop.7](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.6...v0.0.2-develop.7) (2023-07-08)
|
|
||||||
|
|
||||||
## [0.0.2-develop.6](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.5...v0.0.2-develop.6) (2023-07-05)
|
|
||||||
|
|
||||||
## [0.0.2-develop.5](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.4...v0.0.2-develop.5) (2023-07-05)
|
|
||||||
|
|
||||||
## [0.0.2-develop.4](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.3...v0.0.2-develop.4) (2023-07-04)
|
|
||||||
|
|
||||||
## [0.0.2-develop.3](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.2...v0.0.2-develop.3) (2023-07-04)
|
|
||||||
|
|
||||||
## [0.0.2-develop.2](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.1...v0.0.2-develop.2) (2023-07-01)
|
|
||||||
|
|
||||||
## [0.0.2-develop.1](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.1...v0.0.2-develop.1) (2023-07-01)
|
|
||||||
|
|
||||||
|
|
||||||
### Reverts
|
|
||||||
|
|
||||||
* Revert "*Change end to close" ([b5a81d5](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/commit/b5a81d51ab490e5e8f405a18a5859f624d7b0b93))
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
/// <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
|
|
@ -0,0 +1 @@
|
||||||
|
{"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;IAIzB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAkBtB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAMd,OAAO;IA2BR,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI1C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAI9B,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAQtE;AAQD,qBAAa,MAAO,SAAQ,MAAM;IAChC,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAqC;IAEzD,OAAO,CAAC,SAAS,CAAe;IAEhC,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,QAAQ,CAAC,CAAa;gBAElB,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW;IAM1C,OAAO,CAAC,gBAAgB,CAAC,CAAa;IAEtC,IAAI,eAAe,IAAI,UAAU,CAEhC;IAED,OAAO,CAAC,UAAU,CAAC,CAAa;IAEhC,IAAI,SAAS,IAAI,UAAU,CAE1B;IAEK,KAAK;IAQX,EAAE,CACA,KAAK,EAAE,KAAK,GAAG,OAAO,EACtB,QAAQ,EAAE,UAAU,EACpB,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,GAC5B,IAAI,GAAG,QAAQ;IAiBlB,GAAG,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,EAAE,QAAQ,EAAE,UAAU,GAAG,IAAI;IASvD,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAUX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAInB;AAED,eAAO,MAAM,MAAM,mDAAmD,CAAC;AAEvE,eAAO,MAAM,YAAY,+BAA4C,CAAC"}
|
|
@ -0,0 +1,179 @@
|
||||||
|
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
41
package.json
41
package.json
|
@ -1,33 +1,24 @@
|
||||||
{
|
{
|
||||||
"name": "@lumeweb/kernel-swarm-client",
|
"name": "@lumeweb/kernel-swarm-client",
|
||||||
"version": "0.1.0-develop.13",
|
"version": "0.1.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "lib/index.js",
|
"main": "dist/index.js",
|
||||||
"repository": {
|
"dependencies": {
|
||||||
"type": "git",
|
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
|
||||||
"url": "gitea@git.lumeweb.com:LumeWeb/kernel-swarm-client.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"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@lumeweb/node-library-preset": "^0.2.7",
|
|
||||||
"@types/b4a": "^1.6.0",
|
"@types/b4a": "^1.6.0",
|
||||||
"presetter": "*"
|
"@types/node": "^18.15.11",
|
||||||
},
|
"eventemitter2": "^6.4.9",
|
||||||
"readme": "ERROR: No README data found!",
|
"prettier": "^2.8.7",
|
||||||
"scripts": {
|
"pretty": "^2.0.0",
|
||||||
"prepare": "presetter bootstrap",
|
"typescript": "^4.9.5"
|
||||||
"build": "run build",
|
|
||||||
"semantic-release": "semantic-release"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@lumeweb/libkernel": "^0.1.0-develop.68",
|
|
||||||
"async-mutex": "^0.4.0",
|
|
||||||
"b4a": "^1.6.4",
|
|
||||||
"backoff.js": "^1.0.4"
|
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"lib/**"
|
|
||||||
],
|
|
||||||
"publishConfig": {
|
|
||||||
"access": "public"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
112
src/index.ts
112
src/index.ts
|
@ -1,15 +1,22 @@
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { Client, factory } from "@lumeweb/libkernel/module";
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
||||||
import { DataFn, ErrTuple, logErr } from "@lumeweb/libkernel";
|
import { DataFn, ErrTuple, hexToBuf } from "@siaweb/libweb";
|
||||||
import { hexToBytes } from "@lumeweb/libweb";
|
|
||||||
import { blake2b } from "@noble/hashes/blake2b";
|
import { blake2b } from "@noble/hashes/blake2b";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Backoff from "backoff.js";
|
import Backoff from "backoff.js";
|
||||||
|
import { Mutex } from "async-mutex";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Protomux from "protomux";
|
import Protomux from "protomux";
|
||||||
import { UnsubscribeFn } from "emittery";
|
|
||||||
|
import type {
|
||||||
|
eventNS,
|
||||||
|
event,
|
||||||
|
ListenerFn,
|
||||||
|
OnOptions,
|
||||||
|
Listener,
|
||||||
|
} from "eventemitter2";
|
||||||
|
|
||||||
export class SwarmClient extends Client {
|
export class SwarmClient extends Client {
|
||||||
private useDefaultSwarm: boolean;
|
private useDefaultSwarm: boolean;
|
||||||
|
@ -20,7 +27,7 @@ export class SwarmClient extends Client {
|
||||||
private _ready?: Promise<void>;
|
private _ready?: Promise<void>;
|
||||||
private _connectionListener?: [
|
private _connectionListener?: [
|
||||||
sendUpdate: DataFn,
|
sendUpdate: DataFn,
|
||||||
response: Promise<ErrTuple>,
|
response: Promise<ErrTuple>
|
||||||
];
|
];
|
||||||
|
|
||||||
private _topics: Set<Uint8Array> = new Set<Uint8Array>();
|
private _topics: Set<Uint8Array> = new Set<Uint8Array>();
|
||||||
|
@ -35,8 +42,8 @@ export class SwarmClient extends Client {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(module: string, useDefaultDht = true, autoReconnect = false) {
|
constructor(useDefaultDht = true, autoReconnect = false) {
|
||||||
super(module);
|
super();
|
||||||
this.useDefaultSwarm = useDefaultDht;
|
this.useDefaultSwarm = useDefaultDht;
|
||||||
this._autoReconnect = autoReconnect;
|
this._autoReconnect = autoReconnect;
|
||||||
this._connectBackoff = new Backoff({
|
this._connectBackoff = new Backoff({
|
||||||
|
@ -45,7 +52,7 @@ export class SwarmClient extends Client {
|
||||||
});
|
});
|
||||||
|
|
||||||
this._connectBackoff.on("retry", (error: any) => {
|
this._connectBackoff.on("retry", (error: any) => {
|
||||||
logErr(error);
|
this.logErr(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +62,8 @@ export class SwarmClient extends Client {
|
||||||
|
|
||||||
public async connect(pubkey: string | Uint8Array): Promise<Socket> {
|
public async connect(pubkey: string | Uint8Array): Promise<Socket> {
|
||||||
if (typeof pubkey === "string") {
|
if (typeof pubkey === "string") {
|
||||||
pubkey = hexToBytes(pubkey);
|
const buf = hexToBuf(pubkey);
|
||||||
|
pubkey = this.handleErrorOrReturn(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
let existing = Array.from(this._sockets.values()).filter((socket) => {
|
let existing = Array.from(this._sockets.values()).filter((socket) => {
|
||||||
|
@ -115,7 +123,7 @@ export class SwarmClient extends Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit("connection", socket);
|
this.emit("connection", socket);
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,13 +166,15 @@ interface SocketRawStream {
|
||||||
|
|
||||||
export class Socket extends Client {
|
export class Socket extends Client {
|
||||||
private id: number;
|
private id: number;
|
||||||
private eventUpdates: { [event: string]: Map<Function, DataFn> } = {};
|
private eventUpdates: { [event: string]: DataFn[] } = {};
|
||||||
|
|
||||||
|
private syncMutex = new Mutex();
|
||||||
|
|
||||||
private swarm: SwarmClient;
|
private swarm: SwarmClient;
|
||||||
private userData?: any = null;
|
private userData?: any = null;
|
||||||
|
|
||||||
constructor(module: string, id: number, swarm: SwarmClient) {
|
constructor(id: number, swarm: SwarmClient) {
|
||||||
super(module);
|
super();
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.swarm = swarm;
|
this.swarm = swarm;
|
||||||
}
|
}
|
||||||
|
@ -186,101 +196,69 @@ export class Socket extends Client {
|
||||||
|
|
||||||
this._remotePublicKey = info.remotePublicKey;
|
this._remotePublicKey = info.remotePublicKey;
|
||||||
this._rawStream = info.rawStream;
|
this._rawStream = info.rawStream;
|
||||||
await this.swarm.emit("setup", this);
|
await this.swarm.emitAsync("setup", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
on(event: any, listener: any): UnsubscribeFn {
|
on(
|
||||||
const parentOn = super.on(event, listener);
|
event: event | eventNS,
|
||||||
if (this.eventUpdates[event]?.has(listener)) {
|
listener: ListenerFn,
|
||||||
return parentOn;
|
options?: boolean | OnOptions
|
||||||
}
|
): this | Listener {
|
||||||
|
|
||||||
const [update, promise] = this.connectModule(
|
const [update, promise] = this.connectModule(
|
||||||
"socketListenEvent",
|
"socketListenEvent",
|
||||||
{ id: this.id, event: event },
|
{ id: this.id, event: event },
|
||||||
(data: any) => {
|
(data: any) => {
|
||||||
this.emit(event, data);
|
this.emit(event, data);
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
this.trackEvent(event as string, listener, update);
|
this.trackEvent(event as string, update);
|
||||||
|
|
||||||
promise.then(() => {
|
promise.then(() => {
|
||||||
this.off(event as string, listener);
|
this.off(event as string, listener);
|
||||||
});
|
});
|
||||||
|
|
||||||
return parentOn;
|
return super.on(event, listener, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
off(event: any, listener: any): this {
|
off(event: event | eventNS, listener: ListenerFn): this {
|
||||||
if (listener) {
|
const updates = [...this.eventUpdates[event as string]];
|
||||||
const updates = this.eventUpdates[event];
|
this.eventUpdates[event as string] = [];
|
||||||
updates?.get(listener)?.();
|
for (const func of updates) {
|
||||||
updates?.delete(listener);
|
func();
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
return super.off(event, listener);
|
||||||
const updates = [...this.eventUpdates[event as string].values()];
|
|
||||||
this.eventUpdates[event as string] = new Map<Function, DataFn>();
|
|
||||||
updates.forEach((update) => update());
|
|
||||||
super.off(event, listener);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
write(message: string | Buffer): void {
|
write(message: string | Buffer): void {
|
||||||
this.callModule("socketExists", { id: this.id }).then(
|
|
||||||
([exists]: ErrTuple) => {
|
|
||||||
if (!exists) {
|
|
||||||
logErr("tried to write to closed socket");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.callModule("socketWrite", { id: this.id, message });
|
|
||||||
},
|
|
||||||
);
|
|
||||||
this.callModule("socketWrite", { id: this.id, message });
|
this.callModule("socketWrite", { id: this.id, message });
|
||||||
}
|
}
|
||||||
|
|
||||||
end(): void {
|
end(): void {
|
||||||
this.callModule("socketExists", { id: this.id }).then(
|
this.callModule("socketExists", { id: this.id }).then(
|
||||||
([exists]: ErrTuple) => {
|
([exists]: ErrTuple) => {
|
||||||
if (!exists) {
|
if (exists) {
|
||||||
logErr("tried to close a closed socket");
|
this.callModule("socketClose", { id: this.id });
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
this.callModule("socketClose", { id: this.id });
|
}
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getListeners() {
|
|
||||||
return this.callModuleReturn("socketListeners", { id: this.id });
|
|
||||||
}
|
|
||||||
|
|
||||||
public setKeepAlive(ms: number) {
|
|
||||||
this.callModuleReturn("socketSetKeepAlive", {
|
|
||||||
id: this.id,
|
|
||||||
alive: ms,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private ensureEvent(event: string): void {
|
private ensureEvent(event: string): void {
|
||||||
if (!(event in this.eventUpdates)) {
|
if (!(event in this.eventUpdates)) {
|
||||||
this.eventUpdates[event] = new Map<Function, DataFn>();
|
this.eventUpdates[event] = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private trackEvent(event: string, listener: Function, update: DataFn): void {
|
private trackEvent(event: string, update: DataFn): void {
|
||||||
this.ensureEvent(event as string);
|
this.ensureEvent(event as string);
|
||||||
this.eventUpdates[event].set(listener, update);
|
this.eventUpdates[event].push(update);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const MODULE = "zrjTDyEX8Mh2PdDdRj5YL2byFGrYe1ksczRwPaTRFaCGSMG";
|
export const MODULE = "_AU-CzTIzrnnJSoVjfWG_d3cAb8_hONuZ1XaqbFlygrFlg";
|
||||||
|
|
||||||
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
const socketFactory = factory<Socket>(Socket, MODULE);
|
const socketFactory = factory<Socket>(Socket, MODULE);
|
||||||
const createSocket = async (...args: any): Promise<Socket> => {
|
const createSocket = async (...args: any): Promise<Socket> => {
|
||||||
const socket = socketFactory(...args);
|
const socket = socketFactory(...args);
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
{
|
||||||
|
"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