Compare commits

...

3 Commits

Author SHA1 Message Date
Derrick Hammer 29c6a909b8
*Update dist 2023-02-25 22:10:17 -05:00
Derrick Hammer 68c12f22be
*Add option to emulate a websocket 2023-02-25 22:10:00 -05:00
Derrick Hammer 11550659d0
*alias write to send to be WS compatible 2023-02-25 22:06:37 -05:00
3 changed files with 27 additions and 2 deletions

7
dist/socket.d.ts vendored
View File

@ -1,4 +1,6 @@
/// <reference types="node" />
import { Duplex, DuplexEvents, Callback } from "streamx"; import { Duplex, DuplexEvents, Callback } from "streamx";
import { write } from "fs";
type AddressFamily = "IPv6" | "IPv4"; type AddressFamily = "IPv6" | "IPv4";
interface SocketOptions { interface SocketOptions {
allowHalfOpen?: boolean; allowHalfOpen?: boolean;
@ -6,6 +8,7 @@ interface SocketOptions {
remotePort?: number; remotePort?: number;
remotePublicKey?: Uint8Array; remotePublicKey?: Uint8Array;
write?: (this: Duplex<any, any, any, any, true, true, DuplexEvents<any, any>>, data: any, cb: Callback) => void; write?: (this: Duplex<any, any, any, any, true, true, DuplexEvents<any, any>>, data: any, cb: Callback) => void;
emulateWebsocket?: boolean;
} }
export default class Socket extends Duplex { export default class Socket extends Duplex {
private _allowHalfOpen; private _allowHalfOpen;
@ -16,7 +19,8 @@ export default class Socket extends Duplex {
readable: true; readable: true;
writable: true; writable: true;
remotePublicKey: Uint8Array; remotePublicKey: Uint8Array;
constructor({ allowHalfOpen, remoteAddress, remotePort, remotePublicKey, write, }?: SocketOptions); private _emulateWebsocket;
constructor({ allowHalfOpen, remoteAddress, remotePort, remotePublicKey, write, emulateWebsocket, }?: SocketOptions);
private _connecting; private _connecting;
get connecting(): boolean; get connecting(): boolean;
get readyState(): string; get readyState(): string;
@ -34,6 +38,7 @@ export default class Socket extends Duplex {
on: <TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping">(event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents<any, any>[TEvent] : (...args: any[]) => void) => this; on: <TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping">(event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents<any, any>[TEvent] : (...args: any[]) => void) => this;
removeEventListener: <TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping">(event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents<any, any>[TEvent] : (...args: any[]) => void) => this; removeEventListener: <TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping">(event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents<any, any>[TEvent] : (...args: any[]) => void) => this;
off: <TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping">(event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents<any, any>[TEvent] : (...args: any[]) => void) => this; off: <TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping">(event: TEvent, listener: TEvent extends "data" | "end" | "finish" | "pipe" | "readable" | "drain" | keyof import("streamx").StreamEvents | "piping" ? DuplexEvents<any, any>[TEvent] : (...args: any[]) => void) => this;
send: typeof write;
static isIP(input: string): number; static isIP(input: string): number;
static isIPv4(input: string): boolean; static isIPv4(input: string): boolean;
static isIPv6(input: string): boolean; static isIPv6(input: string): boolean;

10
dist/socket.js vendored
View File

@ -1,6 +1,7 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const streamx_1 = require("streamx"); const streamx_1 = require("streamx");
const fs_1 = require("fs");
const IPV4 = "IPv4"; const IPV4 = "IPv4";
const IPV6 = "IPv6"; const IPV6 = "IPv6";
class Socket extends streamx_1.Duplex { class Socket extends streamx_1.Duplex {
@ -10,12 +11,14 @@ class Socket extends streamx_1.Duplex {
remoteFamily; remoteFamily;
bufferSize; bufferSize;
remotePublicKey; remotePublicKey;
constructor({ allowHalfOpen = false, remoteAddress, remotePort, remotePublicKey, write, } = {}) { _emulateWebsocket;
constructor({ allowHalfOpen = false, remoteAddress, remotePort, remotePublicKey, write, emulateWebsocket = false, } = {}) {
super({ write }); super({ write });
this._allowHalfOpen = allowHalfOpen; this._allowHalfOpen = allowHalfOpen;
this.remoteAddress = remoteAddress; this.remoteAddress = remoteAddress;
this.remotePort = remotePort; this.remotePort = remotePort;
this.remotePublicKey = remotePublicKey; this.remotePublicKey = remotePublicKey;
this._emulateWebsocket = emulateWebsocket;
if (remoteAddress) { if (remoteAddress) {
const type = Socket.isIP(remoteAddress); const type = Socket.isIP(remoteAddress);
if (!type) { if (!type) {
@ -23,6 +26,10 @@ class Socket extends streamx_1.Duplex {
} }
this.remoteFamily = type === 6 ? IPV6 : IPV4; this.remoteFamily = type === 6 ? IPV6 : IPV4;
} }
if (this._emulateWebsocket) {
// @ts-ignore
this.addEventListener("data", (data) => this.emit("message", data));
}
} }
_connecting; _connecting;
get connecting() { get connecting() {
@ -68,6 +75,7 @@ class Socket extends streamx_1.Duplex {
on = this.addListener; on = this.addListener;
removeEventListener = this.removeListener; removeEventListener = this.removeListener;
off = this.removeListener; off = this.removeListener;
send = fs_1.write;
static isIP(input) { static isIP(input) {
if (Socket.isIPv4(input)) { if (Socket.isIPv4(input)) {
return 4; return 4;

View File

@ -1,4 +1,5 @@
import { Duplex, DuplexEvents, Callback } from "streamx"; import { Duplex, DuplexEvents, Callback } from "streamx";
import { write } from "fs";
const IPV4 = "IPv4"; const IPV4 = "IPv4";
const IPV6 = "IPv6"; const IPV6 = "IPv6";
@ -15,6 +16,7 @@ interface SocketOptions {
data: any, data: any,
cb: Callback cb: Callback
) => void; ) => void;
emulateWebsocket?: boolean;
} }
export default class Socket extends Duplex { export default class Socket extends Duplex {
@ -28,6 +30,7 @@ export default class Socket extends Duplex {
declare readable: true; declare readable: true;
declare writable: true; declare writable: true;
public remotePublicKey: Uint8Array; public remotePublicKey: Uint8Array;
private _emulateWebsocket: boolean;
constructor({ constructor({
allowHalfOpen = false, allowHalfOpen = false,
@ -35,12 +38,14 @@ export default class Socket extends Duplex {
remotePort, remotePort,
remotePublicKey, remotePublicKey,
write, write,
emulateWebsocket = false,
}: SocketOptions = {}) { }: SocketOptions = {}) {
super({ write }); super({ write });
this._allowHalfOpen = allowHalfOpen; this._allowHalfOpen = allowHalfOpen;
this.remoteAddress = remoteAddress; this.remoteAddress = remoteAddress;
this.remotePort = remotePort; this.remotePort = remotePort;
this.remotePublicKey = remotePublicKey; this.remotePublicKey = remotePublicKey;
this._emulateWebsocket = emulateWebsocket;
if (remoteAddress) { if (remoteAddress) {
const type = Socket.isIP(remoteAddress); const type = Socket.isIP(remoteAddress);
@ -50,6 +55,11 @@ export default class Socket extends Duplex {
this.remoteFamily = type === 6 ? IPV6 : IPV4; this.remoteFamily = type === 6 ? IPV6 : IPV4;
} }
if (this._emulateWebsocket) {
// @ts-ignore
this.addEventListener("data", (data: any) => this.emit("message", data));
}
} }
private _connecting: boolean; private _connecting: boolean;
@ -102,6 +112,8 @@ export default class Socket extends Duplex {
removeEventListener = this.removeListener; removeEventListener = this.removeListener;
off = this.removeListener; off = this.removeListener;
send = write;
static isIP(input: string): number { static isIP(input: string): number {
if (Socket.isIPv4(input)) { if (Socket.isIPv4(input)) {
return 4; return 4;