*Initial version
This commit is contained in:
commit
9b4ac8ff93
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2022 Hammer Technologies LLC
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"name": "@lumeweb/kernel-dht-client",
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"dependencies": {
|
||||
"buffer": "^6.0.3",
|
||||
"libkernel": "^0.1.43",
|
||||
"libkmodule": "^0.2.44",
|
||||
"libskynet": "^0.0.62"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.0.6",
|
||||
"prettier": "^2.7.1"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
import {
|
||||
callModule as callModuleKernel,
|
||||
connectModule as connectModuleKernel,
|
||||
} from "libkernel";
|
||||
import {
|
||||
callModule as callModuleModule,
|
||||
connectModule as connectModuleModule,
|
||||
} from "libkmodule";
|
||||
import { EventEmitter } from "events";
|
||||
import { DataFn, ErrTuple } from "libskynet";
|
||||
import { Buffer } from "buffer";
|
||||
|
||||
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
||||
|
||||
let callModule: typeof callModuleModule,
|
||||
connectModule: typeof connectModuleModule;
|
||||
|
||||
if (window.document) {
|
||||
callModule = callModuleKernel;
|
||||
connectModule = connectModuleKernel;
|
||||
} else {
|
||||
callModule = callModuleModule;
|
||||
connectModule = connectModuleModule;
|
||||
}
|
||||
|
||||
export class DHT {
|
||||
public async connect(pubkey: string): Promise<Socket> {
|
||||
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
return new Socket(resp.id);
|
||||
}
|
||||
|
||||
async ready(): Promise<ErrTuple> {
|
||||
return callModule(DHT_MODULE, "ready");
|
||||
}
|
||||
}
|
||||
|
||||
export class Socket extends EventEmitter {
|
||||
private id: number;
|
||||
private eventUpdates: { [event: string]: DataFn[] } = {};
|
||||
|
||||
constructor(id: number) {
|
||||
super();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
on(eventName: string, listener: (...args: any[]) => void): this {
|
||||
const [update, promise] = connectModule(
|
||||
DHT_MODULE,
|
||||
"listenSocketEvent",
|
||||
{ id: this.id, event: eventName },
|
||||
(data: any) => {
|
||||
this.emit(eventName, data);
|
||||
}
|
||||
);
|
||||
this.trackEvent(eventName, update);
|
||||
|
||||
promise.then(() => {
|
||||
this.off(eventName, listener);
|
||||
});
|
||||
|
||||
return super.on(eventName, listener);
|
||||
}
|
||||
|
||||
off(type: string, listener: any): this {
|
||||
const updates = [...this.eventUpdates[type]];
|
||||
this.eventUpdates[type] = [];
|
||||
for (const func of updates) {
|
||||
func({ action: "off" });
|
||||
}
|
||||
return super.off(type, listener);
|
||||
}
|
||||
|
||||
write(message: string | Buffer): void {
|
||||
callModule(DHT_MODULE, "write", { id: this.id, message });
|
||||
}
|
||||
|
||||
end(): void {
|
||||
callModule(DHT_MODULE, "close", { id: this.id });
|
||||
}
|
||||
|
||||
private ensureEvent(event: string): void {
|
||||
if (!(event in this.eventUpdates)) {
|
||||
this.eventUpdates[event] = [];
|
||||
}
|
||||
}
|
||||
|
||||
private trackEvent(event: string, update: DataFn): void {
|
||||
this.ensureEvent(event as string);
|
||||
this.eventUpdates[event].push(update);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"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