2023-01-12 17:50:38 +00:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const streamx_1 = require("streamx");
|
2023-02-26 03:10:17 +00:00
|
|
|
const fs_1 = require("fs");
|
2023-01-12 17:50:38 +00:00
|
|
|
const IPV4 = "IPv4";
|
|
|
|
const IPV6 = "IPv6";
|
|
|
|
class Socket extends streamx_1.Duplex {
|
|
|
|
_allowHalfOpen;
|
|
|
|
remoteAddress;
|
|
|
|
remotePort;
|
|
|
|
remoteFamily;
|
|
|
|
bufferSize;
|
2023-02-25 01:04:45 +00:00
|
|
|
remotePublicKey;
|
2023-02-26 03:10:17 +00:00
|
|
|
_emulateWebsocket;
|
|
|
|
constructor({ allowHalfOpen = false, remoteAddress, remotePort, remotePublicKey, write, emulateWebsocket = false, } = {}) {
|
2023-01-12 17:50:38 +00:00
|
|
|
super({ write });
|
|
|
|
this._allowHalfOpen = allowHalfOpen;
|
|
|
|
this.remoteAddress = remoteAddress;
|
|
|
|
this.remotePort = remotePort;
|
2023-02-25 01:04:45 +00:00
|
|
|
this.remotePublicKey = remotePublicKey;
|
2023-02-26 03:10:17 +00:00
|
|
|
this._emulateWebsocket = emulateWebsocket;
|
2023-01-12 17:50:38 +00:00
|
|
|
if (remoteAddress) {
|
|
|
|
const type = Socket.isIP(remoteAddress);
|
|
|
|
if (!type) {
|
|
|
|
throw Error("invalid remoteAddress");
|
|
|
|
}
|
2023-02-25 04:51:59 +00:00
|
|
|
this.remoteFamily = type === 6 ? IPV6 : IPV4;
|
2023-01-12 17:50:38 +00:00
|
|
|
}
|
2023-02-26 03:10:17 +00:00
|
|
|
if (this._emulateWebsocket) {
|
|
|
|
// @ts-ignore
|
|
|
|
this.addEventListener("data", (data) => this.emit("message", data));
|
|
|
|
}
|
2023-01-12 17:50:38 +00:00
|
|
|
}
|
|
|
|
_connecting;
|
|
|
|
get connecting() {
|
|
|
|
return this._connecting;
|
|
|
|
}
|
|
|
|
get readyState() {
|
|
|
|
if (this._connecting) {
|
|
|
|
return "opening";
|
|
|
|
}
|
|
|
|
else if (this.readable && this.writable) {
|
|
|
|
return "open";
|
|
|
|
}
|
|
|
|
else if (this.readable && !this.writable) {
|
|
|
|
return "readOnly";
|
|
|
|
}
|
|
|
|
else if (!this.readable && this.writable) {
|
|
|
|
return "writeOnly";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "closed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
listen() {
|
|
|
|
throw new Error("Not supported");
|
|
|
|
}
|
|
|
|
setTimeout(msecs, callback) {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
}
|
|
|
|
_onTimeout() {
|
|
|
|
// @ts-ignore
|
|
|
|
this.emit("timeout");
|
|
|
|
}
|
|
|
|
setNoDelay(enable) { }
|
|
|
|
setKeepAlive(setting, msecs) { }
|
|
|
|
address() {
|
|
|
|
return {
|
|
|
|
address: this.remoteAddress,
|
|
|
|
port: this.remotePort,
|
|
|
|
family: this.remoteFamily,
|
|
|
|
};
|
|
|
|
}
|
2023-02-25 06:51:50 +00:00
|
|
|
addEventListener = this.addListener;
|
2023-02-25 17:07:21 +00:00
|
|
|
on = this.addListener;
|
2023-02-25 06:51:50 +00:00
|
|
|
removeEventListener = this.removeListener;
|
2023-02-25 17:07:21 +00:00
|
|
|
off = this.removeListener;
|
2023-02-26 03:10:17 +00:00
|
|
|
send = fs_1.write;
|
2023-01-12 17:50:38 +00:00
|
|
|
static isIP(input) {
|
|
|
|
if (Socket.isIPv4(input)) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
else if (Socket.isIPv6(input)) {
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static isIPv4(input) {
|
|
|
|
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]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