Compare commits

..

No commits in common. "d4df7d45c384ec7de39102261235af6317b8998e" and "176a95f0269974248f63670595076798dfe617a5" have entirely different histories.

5 changed files with 32 additions and 55 deletions

7
dist/index.d.ts vendored
View File

@ -2,7 +2,6 @@
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";
export declare class SwarmClient extends Client { export declare class SwarmClient extends Client {
private useDefaultSwarm; private useDefaultSwarm;
private id; private id;
@ -10,7 +9,7 @@ export declare class SwarmClient extends Client {
constructor(useDefaultDht?: boolean); constructor(useDefaultDht?: boolean);
connect(pubkey: string | Uint8Array): Promise<Socket>; connect(pubkey: string | Uint8Array): Promise<Socket>;
init(): Promise<ErrTuple>; init(): Promise<ErrTuple>;
ready(): Promise<void>; ready(): Promise<ErrTuple>;
addRelay(pubkey: string): Promise<void>; addRelay(pubkey: string): Promise<void>;
removeRelay(pubkey: string): Promise<void>; removeRelay(pubkey: string): Promise<void>;
clearRelays(): Promise<void>; clearRelays(): Promise<void>;
@ -21,8 +20,8 @@ export declare class Socket extends Client {
private id; private id;
private eventUpdates; private eventUpdates;
constructor(id: number); constructor(id: number);
on<T extends EventEmitter.EventNames<string | symbol>>(event: T, fn: EventEmitter.EventListener<string | symbol, T>, context?: any): this; on(eventName: string, listener: (...args: any[]) => void): this;
off<T extends EventEmitter.EventNames<string | symbol>>(event: T, fn?: EventEmitter.EventListener<string | symbol, T>, context?: any, once?: boolean): this; off(type: string, listener: any): this;
write(message: string | Buffer): void; write(message: string | Buffer): void;
end(): void; end(): void;
private ensureEvent; private ensureEvent;

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;AA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AE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAqC;gBAE7C,EAAE,EAAE,MAAM;IAKtB,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAiB/D,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,IAAI;IAStC,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAUX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAInB;AAID,eAAO,MAAM,YAAY,+BAA4C,CAAC"}

27
dist/index.js vendored
View File

@ -25,10 +25,7 @@ export class SwarmClient extends Client {
return this.callModuleReturn("init", { swarm: this.swarm }); return this.callModuleReturn("init", { swarm: this.swarm });
} }
async ready() { async ready() {
await this.callModuleReturn("ready", { swarm: this.swarm }); return this.callModuleReturn("ready", { swarm: this.swarm });
this.connectModule("listenConnections", { swarm: this.swarm }, (socketId) => {
this.emit("connection", createSocket(socketId));
});
} }
async addRelay(pubkey) { async addRelay(pubkey) {
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm }); return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
@ -53,23 +50,23 @@ export class Socket extends Client {
super(); super();
this.id = id; this.id = id;
} }
on(event, fn, context) { on(eventName, listener) {
const [update, promise] = this.connectModule("listenSocketEvent", { id: this.id, event: event }, (data) => { const [update, promise] = this.connectModule("listenSocketEvent", { id: this.id, event: eventName }, (data) => {
this.emit(event, data); this.emit(eventName, data);
}); });
this.trackEvent(event, update); this.trackEvent(eventName, update);
promise.then(() => { promise.then(() => {
this.off(event, fn); this.off(eventName, listener);
}); });
return super.on(event, fn, context); return super.on(eventName, listener);
} }
off(event, fn, context, once) { off(type, listener) {
const updates = [...this.eventUpdates[event]]; const updates = [...this.eventUpdates[type]];
this.eventUpdates[event] = []; this.eventUpdates[type] = [];
for (const func of updates) { for (const func of updates) {
func(); func({ action: "off" });
} }
return super.off(event, fn, context, once); return super.off(type, listener);
} }
write(message) { write(message) {
this.callModule("write", { id: this.id, message }); this.callModule("write", { id: this.id, message });

View File

@ -5,11 +5,10 @@
"main": "dist/index.js", "main": "dist/index.js",
"dependencies": { "dependencies": {
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git", "@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git", "@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git"
"eventemitter3": "^5.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.11.19", "@types/node": "^18.11.18",
"prettier": "^2.8.3", "prettier": "^2.8.3",
"pretty": "^2.0.0", "pretty": "^2.0.0",
"typescript": "^4.9.5" "typescript": "^4.9.5"

View File

@ -2,8 +2,6 @@ import { Buffer } from "buffer";
import { Client, factory } from "@lumeweb/libkernel-universal"; import { Client, factory } from "@lumeweb/libkernel-universal";
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb"; import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
import type { EventEmitter } from "eventemitter3";
export class SwarmClient extends Client { export class SwarmClient extends Client {
private useDefaultSwarm: boolean; private useDefaultSwarm: boolean;
private id: number = 0; private id: number = 0;
@ -33,16 +31,8 @@ export class SwarmClient extends Client {
async init(): Promise<ErrTuple> { async init(): Promise<ErrTuple> {
return this.callModuleReturn("init", { swarm: this.swarm }); return this.callModuleReturn("init", { swarm: this.swarm });
} }
async ready(): Promise<void> { async ready(): Promise<ErrTuple> {
await this.callModuleReturn("ready", { swarm: this.swarm }); return this.callModuleReturn("ready", { swarm: this.swarm });
this.connectModule(
"listenConnections",
{ swarm: this.swarm },
(socketId: any) => {
this.emit("connection", createSocket(socketId));
}
);
} }
public async addRelay(pubkey: string): Promise<void> { public async addRelay(pubkey: string): Promise<void> {
@ -73,39 +63,31 @@ export class Socket extends Client {
super(); super();
this.id = id; this.id = id;
} }
on<T extends EventEmitter.EventNames<string | symbol>>(
event: T, on(eventName: string, listener: (...args: any[]) => void): this {
fn: EventEmitter.EventListener<string | symbol, T>,
context?: any
): this {
const [update, promise] = this.connectModule( const [update, promise] = this.connectModule(
"listenSocketEvent", "listenSocketEvent",
{ id: this.id, event: event }, { id: this.id, event: eventName },
(data: any) => { (data: any) => {
this.emit(event, data); this.emit(eventName, data);
} }
); );
this.trackEvent(event as string, update); this.trackEvent(eventName, update);
promise.then(() => { promise.then(() => {
this.off(event as string, fn); this.off(eventName, listener);
}); });
return super.on(event, fn, context) as this; return super.on(eventName, listener) as this;
} }
off<T extends EventEmitter.EventNames<string | symbol>>( off(type: string, listener: any): this {
event: T, const updates = [...this.eventUpdates[type]];
fn?: EventEmitter.EventListener<string | symbol, T>, this.eventUpdates[type] = [];
context?: any,
once?: boolean
): this {
const updates = [...this.eventUpdates[event as string]];
this.eventUpdates[event as string] = [];
for (const func of updates) { for (const func of updates) {
func(); func({ action: "off" });
} }
return super.off(event, fn, context, once); return super.off(type, listener);
} }
write(message: string | Buffer): void { write(message: string | Buffer): void {