diff --git a/dist/socket.d.ts b/dist/socket.d.ts index b51c9c5..8d83d51 100644 --- a/dist/socket.d.ts +++ b/dist/socket.d.ts @@ -1,4 +1,6 @@ +/// import { Duplex, DuplexEvents, Callback } from "streamx"; +import { write } from "fs"; type AddressFamily = "IPv6" | "IPv4"; interface SocketOptions { allowHalfOpen?: boolean; @@ -6,6 +8,7 @@ interface SocketOptions { remotePort?: number; remotePublicKey?: Uint8Array; write?: (this: Duplex>, data: any, cb: Callback) => void; + emulateWebsocket?: boolean; } export default class Socket extends Duplex { private _allowHalfOpen; @@ -16,7 +19,8 @@ export default class Socket extends Duplex { readable: true; writable: true; remotePublicKey: Uint8Array; - constructor({ allowHalfOpen, remoteAddress, remotePort, remotePublicKey, write, }?: SocketOptions); + private _emulateWebsocket; + constructor({ allowHalfOpen, remoteAddress, remotePort, remotePublicKey, write, emulateWebsocket, }?: SocketOptions); private _connecting; get connecting(): boolean; get readyState(): string; @@ -34,6 +38,7 @@ export default class Socket extends Duplex { on: (event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents[TEvent] : (...args: any[]) => void) => this; removeEventListener: (event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents[TEvent] : (...args: any[]) => void) => this; off: (event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents[TEvent] : (...args: any[]) => void) => this; + send: typeof write; static isIP(input: string): number; static isIPv4(input: string): boolean; static isIPv6(input: string): boolean; diff --git a/dist/socket.js b/dist/socket.js index 69da1cc..01d94f7 100644 --- a/dist/socket.js +++ b/dist/socket.js @@ -1,6 +1,7 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const streamx_1 = require("streamx"); +const fs_1 = require("fs"); const IPV4 = "IPv4"; const IPV6 = "IPv6"; class Socket extends streamx_1.Duplex { @@ -10,12 +11,14 @@ class Socket extends streamx_1.Duplex { remoteFamily; bufferSize; remotePublicKey; - constructor({ allowHalfOpen = false, remoteAddress, remotePort, remotePublicKey, write, } = {}) { + _emulateWebsocket; + constructor({ allowHalfOpen = false, remoteAddress, remotePort, remotePublicKey, write, emulateWebsocket = false, } = {}) { super({ write }); this._allowHalfOpen = allowHalfOpen; this.remoteAddress = remoteAddress; this.remotePort = remotePort; this.remotePublicKey = remotePublicKey; + this._emulateWebsocket = emulateWebsocket; if (remoteAddress) { const type = Socket.isIP(remoteAddress); if (!type) { @@ -23,6 +26,10 @@ class Socket extends streamx_1.Duplex { } this.remoteFamily = type === 6 ? IPV6 : IPV4; } + if (this._emulateWebsocket) { + // @ts-ignore + this.addEventListener("data", (data) => this.emit("message", data)); + } } _connecting; get connecting() { @@ -68,6 +75,7 @@ class Socket extends streamx_1.Duplex { on = this.addListener; removeEventListener = this.removeListener; off = this.removeListener; + send = fs_1.write; static isIP(input) { if (Socket.isIPv4(input)) { return 4;