Compare commits
No commits in common. "ebe2a18d91de8c5beba9920719ec8a72043d20be" and "b5a81d51ab490e5e8f405a18a5859f624d7b0b93" have entirely different histories.
ebe2a18d91
...
b5a81d51ab
|
@ -1,20 +1,24 @@
|
||||||
/// <reference types="node" />
|
/// <reference types="node" />
|
||||||
|
/// <reference types="node" />
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
import { ErrTuple } from "libskynet";
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { Client } from "@lumeweb/libkernel-universal";
|
export declare class DHT {
|
||||||
import { ErrTuple } from "@siaweb/libweb";
|
private useDefaultDht;
|
||||||
export declare class SwarmClient extends Client {
|
|
||||||
private useDefaultSwarm;
|
|
||||||
private id;
|
private id;
|
||||||
get swarm(): number | undefined;
|
|
||||||
constructor(useDefaultDht?: boolean);
|
constructor(useDefaultDht?: boolean);
|
||||||
connect(pubkey: string | Uint8Array): Promise<Socket>;
|
connect(pubkey: string): Promise<Socket>;
|
||||||
ready(): Promise<ErrTuple>;
|
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>;
|
||||||
getRelays(): Promise<string[]>;
|
getRelays(): Promise<string[]>;
|
||||||
|
getRelayServers(): Promise<string[]>;
|
||||||
|
private create;
|
||||||
|
close(): Promise<boolean>;
|
||||||
|
private setup;
|
||||||
}
|
}
|
||||||
export declare class Socket extends Client {
|
export declare class Socket extends EventEmitter {
|
||||||
private id;
|
private id;
|
||||||
private eventUpdates;
|
private eventUpdates;
|
||||||
constructor(id: number);
|
constructor(id: number);
|
||||||
|
@ -25,5 +29,4 @@ export declare class Socket extends Client {
|
||||||
private ensureEvent;
|
private ensureEvent;
|
||||||
private trackEvent;
|
private trackEvent;
|
||||||
}
|
}
|
||||||
export declare const createClient: (...args: any) => SwarmClient;
|
|
||||||
//# sourceMappingURL=index.d.ts.map
|
//# sourceMappingURL=index.d.ts.map
|
|
@ -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,OAAO,EAAoB,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE5D,qBAAa,WAAY,SAAQ,MAAM;IACrC,OAAO,CAAC,eAAe,CAAU;IACjC,OAAO,CAAC,EAAE,CAAa;IAEvB,IAAI,KAAK,IAAI,MAAM,GAAG,SAAS,CAE9B;gBAEW,aAAa,UAAO;IAKnB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAc5D,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;IAKnB,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;IAG5B,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAG5C;AAED,qBAAa,MAAO,SAAQ,MAAM;IAChC,OAAO,CAAC,EAAE,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"}
|
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAU,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAqBhC,qBAAa,GAAG;IACd,OAAO,CAAC,aAAa,CAAU;IAC/B,OAAO,CAAC,EAAE,CAAa;gBAEX,aAAa,UAAO;IAInB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAa/C,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;IAMnB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASvC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAY1C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAK5B,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAS9B,eAAe,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YAUnC,MAAM;IAaP,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;YAcxB,KAAK;CAIpB;AAED,qBAAa,MAAO,SAAQ,YAAY;IACtC,OAAO,CAAC,EAAE,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;IAkB/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"}
|
|
@ -1,44 +1,112 @@
|
||||||
import { Client, factory } from "@lumeweb/libkernel-universal";
|
import { EventEmitter } from "events";
|
||||||
import { hexToBuf } from "@siaweb/libweb";
|
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
||||||
export class SwarmClient extends Client {
|
let callModule, connectModule;
|
||||||
useDefaultSwarm;
|
async function loadLibs() {
|
||||||
id = 0;
|
if (callModule && connectModule) {
|
||||||
get swarm() {
|
return;
|
||||||
return this.useDefaultSwarm ? undefined : this.id;
|
|
||||||
}
|
}
|
||||||
constructor(useDefaultDht = true) {
|
if (typeof window !== "undefined" && window?.document) {
|
||||||
super();
|
const pkg = await import("libkernel");
|
||||||
this.useDefaultSwarm = useDefaultDht;
|
callModule = pkg.callModule;
|
||||||
|
connectModule = pkg.connectModule;
|
||||||
}
|
}
|
||||||
async connect(pubkey) {
|
else {
|
||||||
if (typeof pubkey === "string") {
|
const pkg = await import("libkmodule");
|
||||||
const buf = hexToBuf(pubkey);
|
callModule = pkg.callModule;
|
||||||
pubkey = this.handleErrorOrReturn(buf);
|
connectModule = pkg.connectModule;
|
||||||
}
|
|
||||||
const resp = this.callModuleReturn("connect", {
|
|
||||||
pubkey,
|
|
||||||
swarm: this.swarm,
|
|
||||||
});
|
|
||||||
return createSocket(resp.id);
|
|
||||||
}
|
|
||||||
async ready() {
|
|
||||||
const dht = !this.useDefaultSwarm ? this.id : undefined;
|
|
||||||
return this.callModuleReturn("ready", { swarm: this.swarm });
|
|
||||||
}
|
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class Socket extends Client {
|
export class DHT {
|
||||||
|
useDefaultDht;
|
||||||
|
id = 0;
|
||||||
|
constructor(useDefaultDht = true) {
|
||||||
|
this.useDefaultDht = useDefaultDht;
|
||||||
|
}
|
||||||
|
async connect(pubkey) {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
const [resp, err] = await callModule(DHT_MODULE, "connect", {
|
||||||
|
pubkey,
|
||||||
|
dht,
|
||||||
|
});
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
return new Socket(resp.id);
|
||||||
|
}
|
||||||
|
async ready() {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
return callModule(DHT_MODULE, "ready", { dht });
|
||||||
|
}
|
||||||
|
async addRelay(pubkey) {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey, dht });
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async removeRelay(pubkey) {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
const [, err] = await callModule(DHT_MODULE, "removeRelay", {
|
||||||
|
pubkey,
|
||||||
|
dht,
|
||||||
|
});
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async clearRelays() {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
await callModule(DHT_MODULE, "clearRelays", { dht });
|
||||||
|
}
|
||||||
|
async getRelays() {
|
||||||
|
await this.setup();
|
||||||
|
const [list, err] = await callModule(DHT_MODULE, "getRelays");
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
async getRelayServers() {
|
||||||
|
await this.setup();
|
||||||
|
const [list, err] = await callModule(DHT_MODULE, "getRelayServers");
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
async create() {
|
||||||
|
await loadLibs();
|
||||||
|
if (this.useDefaultDht || this.id > 0) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
const [dht, err] = await callModule(DHT_MODULE, "openDht");
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
this.id = dht.dht;
|
||||||
|
}
|
||||||
|
async close() {
|
||||||
|
await this.setup();
|
||||||
|
if (this.useDefaultDht) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const [, err] = await callModule(DHT_MODULE, "closeDht", { dht: this.id });
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
async setup() {
|
||||||
|
await loadLibs();
|
||||||
|
await this.create();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class Socket extends EventEmitter {
|
||||||
id;
|
id;
|
||||||
eventUpdates = {};
|
eventUpdates = {};
|
||||||
constructor(id) {
|
constructor(id) {
|
||||||
|
@ -46,7 +114,7 @@ export class Socket extends Client {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
on(eventName, listener) {
|
on(eventName, listener) {
|
||||||
const [update, promise] = this.connectModule("listenSocketEvent", { id: this.id, event: eventName }, (data) => {
|
const [update, promise] = connectModule(DHT_MODULE, "listenSocketEvent", { id: this.id, event: eventName }, (data) => {
|
||||||
this.emit(eventName, data);
|
this.emit(eventName, data);
|
||||||
});
|
});
|
||||||
this.trackEvent(eventName, update);
|
this.trackEvent(eventName, update);
|
||||||
|
@ -64,12 +132,12 @@ export class Socket extends Client {
|
||||||
return super.off(type, listener);
|
return super.off(type, listener);
|
||||||
}
|
}
|
||||||
write(message) {
|
write(message) {
|
||||||
this.callModule("write", { id: this.id, message });
|
callModule(DHT_MODULE, "write", { id: this.id, message });
|
||||||
}
|
}
|
||||||
end() {
|
end() {
|
||||||
this.callModule("socketExists", { id: this.id }).then(([exists]) => {
|
callModule(DHT_MODULE, "socketExists", { id: this.id }).then(([exists]) => {
|
||||||
if (exists) {
|
if (exists) {
|
||||||
this.callModule("close", { id: this.id });
|
callModule(DHT_MODULE, "close", { id: this.id });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -83,6 +151,3 @@ export class Socket extends Client {
|
||||||
this.eventUpdates[event].push(update);
|
this.eventUpdates[event].push(update);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg";
|
|
||||||
export const createClient = factory(SwarmClient, MODULE);
|
|
||||||
const createSocket = factory(Socket, MODULE);
|
|
||||||
|
|
12
package.json
12
package.json
|
@ -4,12 +4,14 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
|
"buffer": "^6.0.3",
|
||||||
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git"
|
"events": "^3.3.0",
|
||||||
|
"libkernel": "^0.1.43",
|
||||||
|
"libkmodule": "^0.2.44",
|
||||||
|
"libskynet": "^0.0.62"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.0.6",
|
||||||
"prettier": "^2.8.3",
|
"prettier": "^2.7.1"
|
||||||
"typescript": "^4.9.5"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
184
src/index.ts
184
src/index.ts
|
@ -1,56 +1,132 @@
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
import { DataFn, ErrTuple } from "libskynet";
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { Client, factory } from "@lumeweb/libkernel-universal";
|
|
||||||
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
|
|
||||||
|
|
||||||
export class SwarmClient extends Client {
|
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
||||||
private useDefaultSwarm: boolean;
|
|
||||||
private id: number = 0;
|
|
||||||
|
|
||||||
get swarm(): number | undefined {
|
let callModule: any, connectModule: any;
|
||||||
return this.useDefaultSwarm ? undefined : this.id;
|
|
||||||
|
async function loadLibs() {
|
||||||
|
if (callModule && connectModule) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
if (typeof window !== "undefined" && window?.document) {
|
||||||
constructor(useDefaultDht = true) {
|
const pkg = await import("libkernel");
|
||||||
super();
|
callModule = pkg.callModule;
|
||||||
this.useDefaultSwarm = useDefaultDht;
|
connectModule = pkg.connectModule;
|
||||||
}
|
} else {
|
||||||
|
const pkg = await import("libkmodule");
|
||||||
public async connect(pubkey: string | Uint8Array): Promise<Socket> {
|
callModule = pkg.callModule;
|
||||||
if (typeof pubkey === "string") {
|
connectModule = pkg.connectModule;
|
||||||
const buf = hexToBuf(pubkey);
|
|
||||||
pubkey = this.handleErrorOrReturn(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
const resp = this.callModuleReturn("connect", {
|
|
||||||
pubkey,
|
|
||||||
swarm: this.swarm,
|
|
||||||
}) as any;
|
|
||||||
|
|
||||||
return createSocket(resp.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
async ready(): Promise<ErrTuple> {
|
|
||||||
const dht = !this.useDefaultSwarm ? this.id : undefined;
|
|
||||||
return this.callModuleReturn("ready", { swarm: this.swarm });
|
|
||||||
}
|
|
||||||
|
|
||||||
public async addRelay(pubkey: string): Promise<void> {
|
|
||||||
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
|
|
||||||
}
|
|
||||||
|
|
||||||
public async removeRelay(pubkey: string): Promise<void> {
|
|
||||||
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
|
|
||||||
}
|
|
||||||
|
|
||||||
public async clearRelays(): Promise<void> {
|
|
||||||
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
|
|
||||||
}
|
|
||||||
public async getRelays(): Promise<string[]> {
|
|
||||||
return this.callModuleReturn("getRelays", { swarm: this.swarm });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Socket extends Client {
|
export class DHT {
|
||||||
|
private useDefaultDht: boolean;
|
||||||
|
private id: number = 0;
|
||||||
|
|
||||||
|
constructor(useDefaultDht = true) {
|
||||||
|
this.useDefaultDht = useDefaultDht;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async connect(pubkey: string): Promise<Socket> {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
const [resp, err] = await callModule(DHT_MODULE, "connect", {
|
||||||
|
pubkey,
|
||||||
|
dht,
|
||||||
|
});
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
return new Socket(resp.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async ready(): Promise<ErrTuple> {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
return callModule(DHT_MODULE, "ready", { dht });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async addRelay(pubkey: string): Promise<void> {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey, dht });
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async removeRelay(pubkey: string): Promise<void> {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
const [, err] = await callModule(DHT_MODULE, "removeRelay", {
|
||||||
|
pubkey,
|
||||||
|
dht,
|
||||||
|
});
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async clearRelays(): Promise<void> {
|
||||||
|
await this.setup();
|
||||||
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
|
await callModule(DHT_MODULE, "clearRelays", { dht });
|
||||||
|
}
|
||||||
|
public async getRelays(): Promise<string[]> {
|
||||||
|
await this.setup();
|
||||||
|
const [list, err] = await callModule(DHT_MODULE, "getRelays");
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
public async getRelayServers(): Promise<string[]> {
|
||||||
|
await this.setup();
|
||||||
|
const [list, err] = await callModule(DHT_MODULE, "getRelayServers");
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async create() {
|
||||||
|
await loadLibs();
|
||||||
|
if (this.useDefaultDht || this.id > 0) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
const [dht, err] = await callModule(DHT_MODULE, "openDht");
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.id = dht.dht;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async close(): Promise<boolean> {
|
||||||
|
await this.setup();
|
||||||
|
|
||||||
|
if (this.useDefaultDht) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const [, err] = await callModule(DHT_MODULE, "closeDht", { dht: this.id });
|
||||||
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async setup() {
|
||||||
|
await loadLibs();
|
||||||
|
await this.create();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Socket extends EventEmitter {
|
||||||
private id: number;
|
private id: number;
|
||||||
private eventUpdates: { [event: string]: DataFn[] } = {};
|
private eventUpdates: { [event: string]: DataFn[] } = {};
|
||||||
|
|
||||||
|
@ -60,7 +136,8 @@ export class Socket extends Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
on(eventName: string, listener: (...args: any[]) => void): this {
|
on(eventName: string, listener: (...args: any[]) => void): this {
|
||||||
const [update, promise] = this.connectModule(
|
const [update, promise] = connectModule(
|
||||||
|
DHT_MODULE,
|
||||||
"listenSocketEvent",
|
"listenSocketEvent",
|
||||||
{ id: this.id, event: eventName },
|
{ id: this.id, event: eventName },
|
||||||
(data: any) => {
|
(data: any) => {
|
||||||
|
@ -73,7 +150,7 @@ export class Socket extends Client {
|
||||||
this.off(eventName, listener);
|
this.off(eventName, listener);
|
||||||
});
|
});
|
||||||
|
|
||||||
return super.on(eventName, listener) as this;
|
return super.on(eventName, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
off(type: string, listener: any): this {
|
off(type: string, listener: any): this {
|
||||||
|
@ -86,14 +163,14 @@ export class Socket extends Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
write(message: string | Buffer): void {
|
write(message: string | Buffer): void {
|
||||||
this.callModule("write", { id: this.id, message });
|
callModule(DHT_MODULE, "write", { id: this.id, message });
|
||||||
}
|
}
|
||||||
|
|
||||||
end(): void {
|
end(): void {
|
||||||
this.callModule("socketExists", { id: this.id }).then(
|
callModule(DHT_MODULE, "socketExists", { id: this.id }).then(
|
||||||
([exists]: ErrTuple) => {
|
([exists]: ErrTuple) => {
|
||||||
if (exists) {
|
if (exists) {
|
||||||
this.callModule("close", { id: this.id });
|
callModule(DHT_MODULE, "close", { id: this.id });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -110,8 +187,3 @@ export class Socket extends Client {
|
||||||
this.eventUpdates[event].push(update);
|
this.eventUpdates[event].push(update);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg";
|
|
||||||
|
|
||||||
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
|
||||||
const createSocket = factory<Socket>(Socket, MODULE);
|
|
||||||
|
|
Loading…
Reference in New Issue