Compare commits

..

3 Commits

5 changed files with 30 additions and 39 deletions

7
dist/index.d.ts vendored
View File

@ -2,7 +2,7 @@
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import { Client } from "@lumeweb/libkernel-universal"; import { Client } from "@lumeweb/libkernel-universal";
import { ErrTuple } from "@siaweb/libweb"; import { ErrTuple } from "@siaweb/libweb";
import type { EventEmitter } from "eventemitter3"; import type { eventNS, event, ListenerFn, OnOptions, Listener } from "eventemitter2";
export declare class SwarmClient extends Client { export declare class SwarmClient extends Client {
private useDefaultSwarm; private useDefaultSwarm;
private id; private id;
@ -40,13 +40,12 @@ export declare class Socket extends Client {
private _rawStream?; private _rawStream?;
get rawStream(): Uint8Array; get rawStream(): Uint8Array;
setup(): Promise<void>; setup(): Promise<void>;
on<T extends EventEmitter.EventNames<string | symbol>>(event: T, fn: EventEmitter.EventListener<string | symbol, T>, context?: any): this; on(event: event | eventNS, listener: ListenerFn, options?: boolean | OnOptions): this | Listener;
off<T extends EventEmitter.EventNames<string | symbol>>(event: T, fn?: EventEmitter.EventListener<string | symbol, T>, context?: any, once?: boolean): this; off(event: event | eventNS, listener: ListenerFn): this;
write(message: string | Buffer): void; write(message: string | Buffer): void;
end(): void; end(): void;
private ensureEvent; private ensureEvent;
private trackEvent; private trackEvent;
syncProtomux(action: string, id: number): Promise<any>;
} }
export declare const MODULE = "_AVKgzVYC8Sb_qiTA6kw5BDzQ4Ch-8D4sldQJl8dXF9oTw"; export declare const MODULE = "_AVKgzVYC8Sb_qiTA6kw5BDzQ4Ch-8D4sldQJl8dXF9oTw";
export declare const createClient: (...args: any) => SwarmClient; export declare const createClient: (...args: any) => SwarmClient;

2
dist/index.d.ts.map vendored
View File

@ -1 +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,O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B,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"}

21
dist/index.js vendored
View File

@ -5,8 +5,6 @@ import b4a from "b4a";
// @ts-ignore // @ts-ignore
import Backoff from "backoff.js"; import Backoff from "backoff.js";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
// @ts-ignore
import Protomux from "protomux";
export class SwarmClient extends Client { export class SwarmClient extends Client {
useDefaultSwarm; useDefaultSwarm;
id = 0; id = 0;
@ -131,25 +129,25 @@ export class Socket extends Client {
let info = await this.callModuleReturn("socketGetInfo", { id: this.id }); let info = await this.callModuleReturn("socketGetInfo", { id: this.id });
this._remotePublicKey = info.remotePublicKey; this._remotePublicKey = info.remotePublicKey;
this._rawStream = info.rawStream; this._rawStream = info.rawStream;
Protomux.from(this, { slave: true }); await this.swarm.emitAsync("setup", this);
} }
on(event, fn, context) { on(event, listener, options) {
const [update, promise] = this.connectModule("socketListenEvent", { id: this.id, event: event }, (data) => { const [update, promise] = this.connectModule("socketListenEvent", { id: this.id, event: event }, (data) => {
this.emit(event, data); this.emit(event, data);
}); });
this.trackEvent(event, update); this.trackEvent(event, update);
promise.then(() => { promise.then(() => {
this.off(event, fn); this.off(event, listener);
}); });
return super.on(event, fn, context); return super.on(event, listener, options);
} }
off(event, fn, context, once) { off(event, listener) {
const updates = [...this.eventUpdates[event]]; const updates = [...this.eventUpdates[event]];
this.eventUpdates[event] = []; this.eventUpdates[event] = [];
for (const func of updates) { for (const func of updates) {
func(); func();
} }
return super.off(event, fn, context, once); return super.off(event, listener);
} }
write(message) { write(message) {
this.callModule("socketWrite", { id: this.id, message }); this.callModule("socketWrite", { id: this.id, message });
@ -170,13 +168,6 @@ export class Socket extends Client {
this.ensureEvent(event); this.ensureEvent(event);
this.eventUpdates[event].push(update); this.eventUpdates[event].push(update);
} }
async syncProtomux(action, id) {
return this.callModuleReturn("syncProtomux", {
id: this.id,
action,
data: id,
});
}
} }
export const MODULE = "_AVKgzVYC8Sb_qiTA6kw5BDzQ4Ch-8D4sldQJl8dXF9oTw"; export const MODULE = "_AVKgzVYC8Sb_qiTA6kw5BDzQ4Ch-8D4sldQJl8dXF9oTw";
export const createClient = factory(SwarmClient, MODULE); export const createClient = factory(SwarmClient, MODULE);

View File

@ -10,13 +10,13 @@
"async-mutex": "^0.4.0", "async-mutex": "^0.4.0",
"b4a": "^1.6.3", "b4a": "^1.6.3",
"backoff.js": "^1.0.4", "backoff.js": "^1.0.4",
"eventemitter3": "^5.0.0",
"p-defer": "^4.0.0", "p-defer": "^4.0.0",
"protomux": "git+https://git.lumeweb.com/LumeWeb/protomux.git" "protomux": "git+https://git.lumeweb.com/LumeWeb/protomux.git"
}, },
"devDependencies": { "devDependencies": {
"@types/b4a": "^1.6.0", "@types/b4a": "^1.6.0",
"@types/node": "^18.15.11", "@types/node": "^18.15.11",
"eventemitter2": "^6.4.9",
"prettier": "^2.8.7", "prettier": "^2.8.7",
"pretty": "^2.0.0", "pretty": "^2.0.0",
"typescript": "^4.9.5" "typescript": "^4.9.5"

View File

@ -4,14 +4,19 @@ import { DataFn, ErrTuple, hexToBuf } from "@siaweb/libweb";
import { blake2b } from "@noble/hashes/blake2b"; import { blake2b } from "@noble/hashes/blake2b";
import b4a from "b4a"; import b4a from "b4a";
import type { EventEmitter } from "eventemitter3";
// @ts-ignore // @ts-ignore
import Backoff from "backoff.js"; import Backoff from "backoff.js";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
// @ts-ignore // @ts-ignore
import Protomux from "protomux"; import Protomux from "protomux";
import defer from "p-defer";
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;
@ -191,13 +196,14 @@ export class Socket extends Client {
this._remotePublicKey = info.remotePublicKey; this._remotePublicKey = info.remotePublicKey;
this._rawStream = info.rawStream; this._rawStream = info.rawStream;
await this.swarm.emitAsync("setup"); await this.swarm.emitAsync("setup", this);
} }
on<T extends EventEmitter.EventNames<string | symbol>>(
event: T, on(
fn: EventEmitter.EventListener<string | symbol, T>, event: event | eventNS,
context?: any listener: ListenerFn,
): this { 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 },
@ -208,24 +214,19 @@ export class Socket extends Client {
this.trackEvent(event as string, update); this.trackEvent(event as string, update);
promise.then(() => { promise.then(() => {
this.off(event as string, fn); this.off(event as string, listener);
}); });
return super.on(event, fn, context) as this; return super.on(event, listener, options);
} }
off<T extends EventEmitter.EventNames<string | symbol>>( off(event: event | eventNS, listener: ListenerFn): this {
event: T,
fn?: EventEmitter.EventListener<string | symbol, T>,
context?: any,
once?: boolean
): this {
const updates = [...this.eventUpdates[event as string]]; const updates = [...this.eventUpdates[event as string]];
this.eventUpdates[event as string] = []; this.eventUpdates[event as string] = [];
for (const func of updates) { for (const func of updates) {
func(); func();
} }
return super.off(event, fn, context, once); return super.off(event, listener);
} }
write(message: string | Buffer): void { write(message: string | Buffer): void {