Compare commits
83 Commits
Author | SHA1 | Date |
---|---|---|
semantic-release-bot | a021ad79fe | |
Derrick Hammer | 348614dbd0 | |
Derrick Hammer | abca3a9b6c | |
semantic-release-bot | 8b85e59be8 | |
Derrick Hammer | 5f81ea2e8f | |
Derrick Hammer | 054ae79386 | |
semantic-release-bot | 0810b05074 | |
Derrick Hammer | 3a863c00de | |
Derrick Hammer | 6159a94a19 | |
semantic-release-bot | 2f66337592 | |
Derrick Hammer | 273f351163 | |
Derrick Hammer | 7856713abf | |
semantic-release-bot | e94b2a7f2e | |
Derrick Hammer | 81851d9595 | |
Derrick Hammer | 34b807743f | |
semantic-release-bot | 89fa1c31c2 | |
Derrick Hammer | f1841a8b0f | |
Derrick Hammer | 7ec1ec177e | |
semantic-release-bot | 28670320ab | |
Derrick Hammer | 00f2b05ee4 | |
Derrick Hammer | 3bffac2223 | |
Derrick Hammer | 69a16dcc17 | |
semantic-release-bot | 3959d7eddc | |
Derrick Hammer | a3c539df20 | |
Derrick Hammer | e079063f94 | |
semantic-release-bot | 2e7c6f3d75 | |
Derrick Hammer | 84cbd1d164 | |
semantic-release-bot | 084138cff3 | |
Derrick Hammer | 5f6193dcf7 | |
Derrick Hammer | fada2f8355 | |
semantic-release-bot | cc60ef4727 | |
Derrick Hammer | e6dd89b347 | |
Derrick Hammer | 19dceedab2 | |
Derrick Hammer | 88586ca65a | |
semantic-release-bot | b90f616cad | |
Derrick Hammer | 712bf44d83 | |
Derrick Hammer | e0735052a0 | |
semantic-release-bot | 3669528c56 | |
Derrick Hammer | 61cee154e5 | |
Derrick Hammer | 0ec1dd1aac | |
Derrick Hammer | 8c6b038f89 | |
semantic-release-bot | 616dc63bcf | |
Derrick Hammer | 84ab91442a | |
Derrick Hammer | 8380254638 | |
semantic-release-bot | 1c0d11ccb0 | |
Derrick Hammer | 0747c0b31f | |
Derrick Hammer | 57efedd0af | |
semantic-release-bot | 807d9376a0 | |
Derrick Hammer | 7eabf6a05d | |
Derrick Hammer | 50335dd062 | |
Derrick Hammer | 0c18c92521 | |
semantic-release-bot | 82d83c20fd | |
Derrick Hammer | 5691c7a488 | |
Derrick Hammer | 720b7a60d9 | |
semantic-release-bot | a490281514 | |
Derrick Hammer | cbdf0a3ffa | |
Derrick Hammer | 11ee1d687a | |
semantic-release-bot | d870170aa1 | |
Derrick Hammer | bbdb6d5848 | |
semantic-release-bot | 4ff2d1d09e | |
Derrick Hammer | 3ec590aaa2 | |
Derrick Hammer | 296a23a3ce | |
semantic-release-bot | a119226840 | |
Derrick Hammer | 86bbe98ef8 | |
Derrick Hammer | fa071d53a7 | |
semantic-release-bot | 34b5ccf77a | |
Derrick Hammer | 07d1fc514b | |
Derrick Hammer | a03e775060 | |
semantic-release-bot | 252e0007d6 | |
Derrick Hammer | b337aee1ab | |
Derrick Hammer | 2a20232269 | |
semantic-release-bot | 8dfd7e5bef | |
Derrick Hammer | bd50637c14 | |
Derrick Hammer | e47fe353aa | |
semantic-release-bot | 4cdaa8caab | |
Derrick Hammer | 47ad52a358 | |
Derrick Hammer | 612aa1a0a0 | |
semantic-release-bot | c8746c001d | |
Derrick Hammer | 608cd614a4 | |
Derrick Hammer | 8141edeb79 | |
semantic-release-bot | 11034dc7a8 | |
Derrick Hammer | 009dc4e003 | |
Derrick Hammer | 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"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
# [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))
|
|
@ -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;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"}
|
|
@ -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
41
package.json
41
package.json
|
@ -1,24 +1,33 @@
|
|||
{
|
||||
"name": "@lumeweb/kernel-swarm-client",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.0-develop.13",
|
||||
"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!",
|
||||
"scripts": {
|
||||
"prepare": "presetter bootstrap",
|
||||
"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,22 +1,15 @@
|
|||
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, logErr } from "@lumeweb/libkernel";
|
||||
import { hexToBytes } from "@lumeweb/libweb";
|
||||
import { blake2b } from "@noble/hashes/blake2b";
|
||||
import b4a from "b4a";
|
||||
|
||||
// @ts-ignore
|
||||
import Backoff from "backoff.js";
|
||||
import { Mutex } from "async-mutex";
|
||||
// @ts-ignore
|
||||
import Protomux from "protomux";
|
||||
|
||||
import type {
|
||||
eventNS,
|
||||
event,
|
||||
ListenerFn,
|
||||
OnOptions,
|
||||
Listener,
|
||||
} from "eventemitter2";
|
||||
import { UnsubscribeFn } from "emittery";
|
||||
|
||||
export class SwarmClient extends Client {
|
||||
private useDefaultSwarm: boolean;
|
||||
|
@ -27,7 +20,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 +35,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 +45,7 @@ export class SwarmClient extends Client {
|
|||
});
|
||||
|
||||
this._connectBackoff.on("retry", (error: any) => {
|
||||
this.logErr(error);
|
||||
logErr(error);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -62,8 +55,7 @@ export class SwarmClient extends Client {
|
|||
|
||||
public async connect(pubkey: string | Uint8Array): Promise<Socket> {
|
||||
if (typeof pubkey === "string") {
|
||||
const buf = hexToBuf(pubkey);
|
||||
pubkey = this.handleErrorOrReturn(buf);
|
||||
pubkey = hexToBytes(pubkey);
|
||||
}
|
||||
|
||||
let existing = Array.from(this._sockets.values()).filter((socket) => {
|
||||
|
@ -123,7 +115,7 @@ export class SwarmClient extends Client {
|
|||
}
|
||||
|
||||
this.emit("connection", socket);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -166,15 +158,13 @@ interface SocketRawStream {
|
|||
|
||||
export class Socket extends Client {
|
||||
private id: number;
|
||||
private eventUpdates: { [event: string]: DataFn[] } = {};
|
||||
|
||||
private syncMutex = new Mutex();
|
||||
private eventUpdates: { [event: string]: Map<Function, DataFn> } = {};
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -196,69 +186,101 @@ export class Socket extends Client {
|
|||
|
||||
this._remotePublicKey = info.remotePublicKey;
|
||||
this._rawStream = info.rawStream;
|
||||
await this.swarm.emitAsync("setup", this);
|
||||
await this.swarm.emit("setup", this);
|
||||
}
|
||||
|
||||
on(event: any, listener: any): UnsubscribeFn {
|
||||
const parentOn = super.on(event, listener);
|
||||
if (this.eventUpdates[event]?.has(listener)) {
|
||||
return parentOn;
|
||||
}
|
||||
|
||||
on(
|
||||
event: event | eventNS,
|
||||
listener: ListenerFn,
|
||||
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);
|
||||
this.trackEvent(event as string, listener, update);
|
||||
|
||||
promise.then(() => {
|
||||
this.off(event as string, listener);
|
||||
});
|
||||
|
||||
return super.on(event, listener, options);
|
||||
return parentOn;
|
||||
}
|
||||
|
||||
off(event: event | eventNS, listener: ListenerFn): this {
|
||||
const updates = [...this.eventUpdates[event as string]];
|
||||
this.eventUpdates[event as string] = [];
|
||||
for (const func of updates) {
|
||||
func();
|
||||
off(event: any, listener: any): this {
|
||||
if (listener) {
|
||||
const updates = this.eventUpdates[event];
|
||||
updates?.get(listener)?.();
|
||||
updates?.delete(listener);
|
||||
|
||||
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 {
|
||||
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 });
|
||||
}
|
||||
|
||||
end(): void {
|
||||
this.callModule("socketExists", { id: this.id }).then(
|
||||
([exists]: ErrTuple) => {
|
||||
if (exists) {
|
||||
if (!exists) {
|
||||
logErr("tried to close a closed socket");
|
||||
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 {
|
||||
if (!(event in this.eventUpdates)) {
|
||||
this.eventUpdates[event] = [];
|
||||
this.eventUpdates[event] = new Map<Function, DataFn>();
|
||||
}
|
||||
}
|
||||
|
||||
private trackEvent(event: string, update: DataFn): void {
|
||||
private trackEvent(event: string, listener: Function, update: DataFn): void {
|
||||
this.ensureEvent(event as string);
|
||||
this.eventUpdates[event].push(update);
|
||||
this.eventUpdates[event].set(listener, update);
|
||||
}
|
||||
}
|
||||
|
||||
export const MODULE = "_AU-CzTIzrnnJSoVjfWG_d3cAb8_hONuZ1XaqbFlygrFlg";
|
||||
export const MODULE = "zrjTDyEX8Mh2PdDdRj5YL2byFGrYe1ksczRwPaTRFaCGSMG";
|
||||
|
||||
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
||||
|
||||
// @ts-ignore
|
||||
const socketFactory = factory<Socket>(Socket, MODULE);
|
||||
const createSocket = async (...args: any): Promise<Socket> => {
|
||||
const socket = socketFactory(...args);
|
||||
|
|
|
@ -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