libhyperproxy/dist/proxies/multiSocket/dummySocket.js

68 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2023-04-15 22:40:09 +00:00
"use strict";
2023-04-16 02:17:57 +00:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2023-04-15 22:40:09 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
const timers_1 = require("timers");
const util_js_1 = require("../../util.js");
2023-04-16 02:17:57 +00:00
const socket_js_1 = __importDefault(require("../../socket.js"));
class DummySocket extends socket_js_1.default {
2023-04-15 22:40:09 +00:00
_options;
_id;
_proxy;
_connectTimeout;
2023-04-16 02:17:57 +00:00
constructor(id, manager, peer, connectOptions, socketOptions) {
super(socketOptions);
2023-04-15 22:40:09 +00:00
this._id = id;
this._proxy = manager;
this._peer = peer;
2023-04-16 02:17:57 +00:00
this._options = connectOptions;
2023-04-15 22:40:09 +00:00
// @ts-ignore
this.on("timeout", () => {
if (this._connectTimeout) {
(0, timers_1.clearTimeout)(this._connectTimeout);
}
});
}
_remoteId = 0;
set remoteId(value) {
this._remoteId = value;
this._proxy.socketMap.set(this._id, value);
}
_peer;
get peer() {
return this._peer;
}
async _write(data, cb) {
(await (0, util_js_1.maybeGetAsyncProperty)(this._peer.messages.writeSocket))?.send({
id: this._id,
remoteId: this._remoteId,
data,
});
cb();
}
async _destroy(cb) {
(await (0, util_js_1.maybeGetAsyncProperty)(this._peer.messages.closeSocket))?.send({
id: this._id,
remoteId: this._remoteId,
});
this._proxy.socketMap.delete(this._id);
this._proxy.sockets.delete(this._id);
}
async connect() {
(await (0, util_js_1.maybeGetAsyncProperty)(this._peer.messages.openSocket))?.send({
...this._options,
id: this._id,
});
}
setTimeout(ms, cb) {
if (this._connectTimeout) {
(0, timers_1.clearTimeout)(this._connectTimeout);
}
this._connectTimeout = setTimeout(() => {
cb && cb();
}, ms);
}
}
exports.default = DummySocket;