Compare commits
2 Commits
e69e7a67b6
...
524d647f21
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 524d647f21 | |
Derrick Hammer | 1786e56380 |
|
@ -6,8 +6,8 @@ import type { EventEmitter } from "eventemitter3";
|
|||
export declare class SwarmClient extends Client {
|
||||
private useDefaultSwarm;
|
||||
private id;
|
||||
get swarm(): number | undefined;
|
||||
constructor(useDefaultDht?: boolean);
|
||||
get swarm(): number | undefined;
|
||||
connect(pubkey: string | Uint8Array): Promise<Socket>;
|
||||
init(): Promise<ErrTuple>;
|
||||
ready(): Promise<void>;
|
||||
|
@ -21,6 +21,11 @@ export declare class Socket extends Client {
|
|||
private id;
|
||||
private eventUpdates;
|
||||
constructor(id: number);
|
||||
private _remotePublicKey?;
|
||||
get remotePublicKey(): Uint8Array;
|
||||
private _rawStream?;
|
||||
get rawStream(): Uint8Array;
|
||||
setup(): Promise<void>;
|
||||
on<T extends EventEmitter.EventNames<string | symbol>>(event: T, fn: EventEmitter.EventListener<string | symbol, T>, context?: any): this;
|
||||
off<T extends EventEmitter.EventNames<string | symbol>>(event: T, fn?: EventEmitter.EventListener<string | symbol, T>, context?: any, once?: boolean): this;
|
||||
write(message: string | Buffer): void;
|
||||
|
|
|
@ -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,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,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,M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AAC,MAAM,GAAG,MAAM,CAAC,EACnD,KAAK,EAAE,CAAC,EACR,EAAE,EAAE,YAAY,CAAC,aAAa,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC,EAClD,OAAO,CAAC,EAAE,GAAG,GACZ,IAAI;IAiBP,GAAG,CAAC,CAAC,SAAS,YAAY,CAAC,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC,EACpD,KAAK,EAAE,CAAC,EACR,EAAE,CAAC,EAAE,YAAY,CAAC,aAAa,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC,EACnD,OAAO,CAAC,EAAE,GAAG,EACb,IAAI,CAAC,EAAE,OAAO,GACb,IAAI;IASP,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"}
|
|
@ -3,13 +3,13 @@ import { hexToBuf } from "@siaweb/libweb";
|
|||
export class SwarmClient extends Client {
|
||||
useDefaultSwarm;
|
||||
id = 0;
|
||||
get swarm() {
|
||||
return this.useDefaultSwarm ? undefined : this.id;
|
||||
}
|
||||
constructor(useDefaultDht = true) {
|
||||
super();
|
||||
this.useDefaultSwarm = useDefaultDht;
|
||||
}
|
||||
get swarm() {
|
||||
return this.useDefaultSwarm ? undefined : this.id;
|
||||
}
|
||||
async connect(pubkey) {
|
||||
if (typeof pubkey === "string") {
|
||||
const buf = hexToBuf(pubkey);
|
||||
|
@ -26,8 +26,8 @@ export class SwarmClient extends Client {
|
|||
}
|
||||
async ready() {
|
||||
await this.callModuleReturn("ready", { swarm: this.swarm });
|
||||
this.connectModule("listenConnections", { swarm: this.swarm }, (socketId) => {
|
||||
this.emit("connection", createSocket(socketId));
|
||||
this.connectModule("listenConnections", { swarm: this.swarm }, async (socketId) => {
|
||||
this.emit("connection", await createSocket(socketId));
|
||||
});
|
||||
}
|
||||
async addRelay(pubkey) {
|
||||
|
@ -53,6 +53,19 @@ export class Socket extends Client {
|
|||
super();
|
||||
this.id = id;
|
||||
}
|
||||
_remotePublicKey;
|
||||
get remotePublicKey() {
|
||||
return this._remotePublicKey;
|
||||
}
|
||||
_rawStream;
|
||||
get rawStream() {
|
||||
return this._rawStream;
|
||||
}
|
||||
async setup() {
|
||||
let info = await this.callModuleReturn("socketGetInfo", { id: this.id });
|
||||
this._remotePublicKey = info.remotePublicKey;
|
||||
this._rawStream = info.rawStream;
|
||||
}
|
||||
on(event, fn, context) {
|
||||
const [update, promise] = this.connectModule("socketListenEvent", { id: this.id, event: event }, (data) => {
|
||||
this.emit(event, data);
|
||||
|
@ -93,4 +106,9 @@ export class Socket extends Client {
|
|||
}
|
||||
const MODULE = "_A7ClA0mSa1-Pg5c4V3C0H_fnhAFjgccITYT83Euc7t_9A";
|
||||
export const createClient = factory(SwarmClient, MODULE);
|
||||
const createSocket = factory(Socket, MODULE);
|
||||
const socketFactory = factory(Socket, MODULE);
|
||||
const createSocket = async (...args) => {
|
||||
const socket = socketFactory(...args);
|
||||
await socket.setup();
|
||||
return socket;
|
||||
};
|
||||
|
|
48
src/index.ts
48
src/index.ts
|
@ -8,15 +8,15 @@ export class SwarmClient extends Client {
|
|||
private useDefaultSwarm: boolean;
|
||||
private id: number = 0;
|
||||
|
||||
get swarm(): number | undefined {
|
||||
return this.useDefaultSwarm ? undefined : this.id;
|
||||
}
|
||||
|
||||
constructor(useDefaultDht = true) {
|
||||
super();
|
||||
this.useDefaultSwarm = useDefaultDht;
|
||||
}
|
||||
|
||||
get swarm(): number | undefined {
|
||||
return this.useDefaultSwarm ? undefined : this.id;
|
||||
}
|
||||
|
||||
public async connect(pubkey: string | Uint8Array): Promise<Socket> {
|
||||
if (typeof pubkey === "string") {
|
||||
const buf = hexToBuf(pubkey);
|
||||
|
@ -39,8 +39,8 @@ export class SwarmClient extends Client {
|
|||
this.connectModule(
|
||||
"listenConnections",
|
||||
{ swarm: this.swarm },
|
||||
(socketId: any) => {
|
||||
this.emit("connection", createSocket(socketId));
|
||||
async (socketId: any) => {
|
||||
this.emit("connection", await createSocket(socketId));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -65,6 +65,12 @@ export class SwarmClient extends Client {
|
|||
}
|
||||
}
|
||||
|
||||
interface SocketRawStream {
|
||||
remoteHost: string;
|
||||
remotePort: number;
|
||||
remoteFamily: string;
|
||||
}
|
||||
|
||||
export class Socket extends Client {
|
||||
private id: number;
|
||||
private eventUpdates: { [event: string]: DataFn[] } = {};
|
||||
|
@ -73,6 +79,26 @@ export class Socket extends Client {
|
|||
super();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
private _remotePublicKey?: Uint8Array;
|
||||
|
||||
get remotePublicKey(): Uint8Array {
|
||||
return this._remotePublicKey as Uint8Array;
|
||||
}
|
||||
|
||||
private _rawStream?: Uint8Array;
|
||||
|
||||
get rawStream(): Uint8Array {
|
||||
return this._rawStream as Uint8Array;
|
||||
}
|
||||
|
||||
async setup() {
|
||||
let info = await this.callModuleReturn("socketGetInfo", { id: this.id });
|
||||
|
||||
this._remotePublicKey = info.remotePublicKey;
|
||||
this._rawStream = info.rawStream;
|
||||
}
|
||||
|
||||
on<T extends EventEmitter.EventNames<string | symbol>>(
|
||||
event: T,
|
||||
fn: EventEmitter.EventListener<string | symbol, T>,
|
||||
|
@ -137,4 +163,12 @@ export class Socket extends Client {
|
|||
const MODULE = "_A7ClA0mSa1-Pg5c4V3C0H_fnhAFjgccITYT83Euc7t_9A";
|
||||
|
||||
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
||||
const createSocket = factory<Socket>(Socket, MODULE);
|
||||
|
||||
const socketFactory = factory<Socket>(Socket, MODULE);
|
||||
const createSocket = async (...args: any): Promise<Socket> => {
|
||||
const socket = socketFactory(...args);
|
||||
|
||||
await socket.setup();
|
||||
|
||||
return socket;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue