Compare commits

..

No commits in common. "ebe2a18d91de8c5beba9920719ec8a72043d20be" and "b5a81d51ab490e5e8f405a18a5859f624d7b0b93" have entirely different histories.

5 changed files with 256 additions and 114 deletions

19
dist/index.d.ts vendored
View File

@ -1,20 +1,24 @@
/// <reference types="node" />
/// <reference types="node" />
import { EventEmitter } from "events";
import { ErrTuple } from "libskynet";
import { Buffer } from "buffer";
import { Client } from "@lumeweb/libkernel-universal";
import { ErrTuple } from "@siaweb/libweb";
export declare class SwarmClient extends Client {
private useDefaultSwarm;
export declare class DHT {
private useDefaultDht;
private id;
get swarm(): number | undefined;
constructor(useDefaultDht?: boolean);
connect(pubkey: string | Uint8Array): Promise<Socket>;
connect(pubkey: string): Promise<Socket>;
ready(): Promise<ErrTuple>;
addRelay(pubkey: string): Promise<void>;
removeRelay(pubkey: string): Promise<void>;
clearRelays(): Promise<void>;
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 eventUpdates;
constructor(id: number);
@ -25,5 +29,4 @@ export declare class Socket extends Client {
private ensureEvent;
private trackEvent;
}
export declare const createClient: (...args: any) => SwarmClient;
//# sourceMappingURL=index.d.ts.map

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,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,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AAC;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"}

125
dist/index.js vendored
View File

@ -1,44 +1,112 @@
import { Client, factory } from "@lumeweb/libkernel-universal";
import { hexToBuf } from "@siaweb/libweb";
export class SwarmClient extends Client {
useDefaultSwarm;
id = 0;
get swarm() {
return this.useDefaultSwarm ? undefined : this.id;
import { EventEmitter } from "events";
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
let callModule, connectModule;
async function loadLibs() {
if (callModule && connectModule) {
return;
}
if (typeof window !== "undefined" && window?.document) {
const pkg = await import("libkernel");
callModule = pkg.callModule;
connectModule = pkg.connectModule;
}
else {
const pkg = await import("libkmodule");
callModule = pkg.callModule;
connectModule = pkg.connectModule;
}
}
export class DHT {
useDefaultDht;
id = 0;
constructor(useDefaultDht = true) {
super();
this.useDefaultSwarm = useDefaultDht;
this.useDefaultDht = useDefaultDht;
}
async connect(pubkey) {
if (typeof pubkey === "string") {
const buf = hexToBuf(pubkey);
pubkey = this.handleErrorOrReturn(buf);
}
const resp = this.callModuleReturn("connect", {
await this.setup();
const dht = !this.useDefaultDht ? this.id : undefined;
const [resp, err] = await callModule(DHT_MODULE, "connect", {
pubkey,
swarm: this.swarm,
dht,
});
return createSocket(resp.id);
if (err) {
throw new Error(err);
}
return new Socket(resp.id);
}
async ready() {
const dht = !this.useDefaultSwarm ? this.id : undefined;
return this.callModuleReturn("ready", { swarm: this.swarm });
await this.setup();
const dht = !this.useDefaultDht ? this.id : undefined;
return callModule(DHT_MODULE, "ready", { dht });
}
async addRelay(pubkey) {
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
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) {
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
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() {
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
await this.setup();
const dht = !this.useDefaultDht ? this.id : undefined;
await callModule(DHT_MODULE, "clearRelays", { dht });
}
async getRelays() {
return this.callModuleReturn("getRelays", { swarm: this.swarm });
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 Client {
export class Socket extends EventEmitter {
id;
eventUpdates = {};
constructor(id) {
@ -46,7 +114,7 @@ export class Socket extends Client {
this.id = id;
}
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.trackEvent(eventName, update);
@ -64,12 +132,12 @@ export class Socket extends Client {
return super.off(type, listener);
}
write(message) {
this.callModule("write", { id: this.id, message });
callModule(DHT_MODULE, "write", { id: this.id, message });
}
end() {
this.callModule("socketExists", { id: this.id }).then(([exists]) => {
callModule(DHT_MODULE, "socketExists", { id: this.id }).then(([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);
}
}
const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg";
export const createClient = factory(SwarmClient, MODULE);
const createSocket = factory(Socket, MODULE);

View File

@ -4,12 +4,14 @@
"type": "module",
"main": "dist/index.js",
"dependencies": {
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git"
"buffer": "^6.0.3",
"events": "^3.3.0",
"libkernel": "^0.1.43",
"libkmodule": "^0.2.44",
"libskynet": "^0.0.62"
},
"devDependencies": {
"@types/node": "^18.11.18",
"prettier": "^2.8.3",
"typescript": "^4.9.5"
"@types/node": "^18.0.6",
"prettier": "^2.7.1"
}
}

View File

@ -1,56 +1,132 @@
import { EventEmitter } from "events";
import { DataFn, ErrTuple } from "libskynet";
import { Buffer } from "buffer";
import { Client, factory } from "@lumeweb/libkernel-universal";
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
export class SwarmClient extends Client {
private useDefaultSwarm: boolean;
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
let callModule: any, connectModule: any;
async function loadLibs() {
if (callModule && connectModule) {
return;
}
if (typeof window !== "undefined" && window?.document) {
const pkg = await import("libkernel");
callModule = pkg.callModule;
connectModule = pkg.connectModule;
} else {
const pkg = await import("libkmodule");
callModule = pkg.callModule;
connectModule = pkg.connectModule;
}
}
export class DHT {
private useDefaultDht: boolean;
private id: number = 0;
get swarm(): number | undefined {
return this.useDefaultSwarm ? undefined : this.id;
}
constructor(useDefaultDht = true) {
super();
this.useDefaultSwarm = useDefaultDht;
this.useDefaultDht = useDefaultDht;
}
public async connect(pubkey: string | Uint8Array): Promise<Socket> {
if (typeof pubkey === "string") {
const buf = hexToBuf(pubkey);
pubkey = this.handleErrorOrReturn(buf);
}
const resp = this.callModuleReturn("connect", {
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,
swarm: this.swarm,
}) as any;
return createSocket(resp.id);
dht,
});
if (err) {
throw new Error(err);
}
return new Socket(resp.id);
}
async ready(): Promise<ErrTuple> {
const dht = !this.useDefaultSwarm ? this.id : undefined;
return this.callModuleReturn("ready", { swarm: this.swarm });
await this.setup();
const dht = !this.useDefaultDht ? this.id : undefined;
return callModule(DHT_MODULE, "ready", { dht });
}
public async addRelay(pubkey: string): Promise<void> {
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
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> {
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
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> {
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
await this.setup();
const dht = !this.useDefaultDht ? this.id : undefined;
await callModule(DHT_MODULE, "clearRelays", { dht });
}
public async getRelays(): Promise<string[]> {
return this.callModuleReturn("getRelays", { swarm: this.swarm });
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 Client {
export class Socket extends EventEmitter {
private id: number;
private eventUpdates: { [event: string]: DataFn[] } = {};
@ -60,7 +136,8 @@ export class Socket extends Client {
}
on(eventName: string, listener: (...args: any[]) => void): this {
const [update, promise] = this.connectModule(
const [update, promise] = connectModule(
DHT_MODULE,
"listenSocketEvent",
{ id: this.id, event: eventName },
(data: any) => {
@ -73,7 +150,7 @@ export class Socket extends Client {
this.off(eventName, listener);
});
return super.on(eventName, listener) as this;
return super.on(eventName, listener);
}
off(type: string, listener: any): this {
@ -86,14 +163,14 @@ export class Socket extends Client {
}
write(message: string | Buffer): void {
this.callModule("write", { id: this.id, message });
callModule(DHT_MODULE, "write", { id: this.id, message });
}
end(): void {
this.callModule("socketExists", { id: this.id }).then(
callModule(DHT_MODULE, "socketExists", { id: this.id }).then(
([exists]: ErrTuple) => {
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);
}
}
const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg";
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
const createSocket = factory<Socket>(Socket, MODULE);