@kelceyp/caw-server 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +3486 -11
- package/dist/main.js.map +26 -7
- package/package.json +4 -2
package/dist/main.js
CHANGED
|
@@ -41751,9 +41751,2849 @@ var require_dist = __commonJS((exports, module) => {
|
|
|
41751
41751
|
exports.default = formatsPlugin;
|
|
41752
41752
|
});
|
|
41753
41753
|
|
|
41754
|
+
// node_modules/ws/lib/constants.js
|
|
41755
|
+
var require_constants = __commonJS((exports, module) => {
|
|
41756
|
+
var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
41757
|
+
var hasBlob = typeof Blob !== "undefined";
|
|
41758
|
+
if (hasBlob)
|
|
41759
|
+
BINARY_TYPES.push("blob");
|
|
41760
|
+
module.exports = {
|
|
41761
|
+
BINARY_TYPES,
|
|
41762
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
41763
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
41764
|
+
hasBlob,
|
|
41765
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
41766
|
+
kListener: Symbol("kListener"),
|
|
41767
|
+
kStatusCode: Symbol("status-code"),
|
|
41768
|
+
kWebSocket: Symbol("websocket"),
|
|
41769
|
+
NOOP: () => {}
|
|
41770
|
+
};
|
|
41771
|
+
});
|
|
41772
|
+
|
|
41773
|
+
// node_modules/ws/lib/buffer-util.js
|
|
41774
|
+
var require_buffer_util = __commonJS((exports, module) => {
|
|
41775
|
+
var { EMPTY_BUFFER } = require_constants();
|
|
41776
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
41777
|
+
function concat(list, totalLength) {
|
|
41778
|
+
if (list.length === 0)
|
|
41779
|
+
return EMPTY_BUFFER;
|
|
41780
|
+
if (list.length === 1)
|
|
41781
|
+
return list[0];
|
|
41782
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
41783
|
+
let offset = 0;
|
|
41784
|
+
for (let i = 0;i < list.length; i++) {
|
|
41785
|
+
const buf = list[i];
|
|
41786
|
+
target.set(buf, offset);
|
|
41787
|
+
offset += buf.length;
|
|
41788
|
+
}
|
|
41789
|
+
if (offset < totalLength) {
|
|
41790
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
41791
|
+
}
|
|
41792
|
+
return target;
|
|
41793
|
+
}
|
|
41794
|
+
function _mask(source, mask, output, offset, length) {
|
|
41795
|
+
for (let i = 0;i < length; i++) {
|
|
41796
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
41797
|
+
}
|
|
41798
|
+
}
|
|
41799
|
+
function _unmask(buffer, mask) {
|
|
41800
|
+
for (let i = 0;i < buffer.length; i++) {
|
|
41801
|
+
buffer[i] ^= mask[i & 3];
|
|
41802
|
+
}
|
|
41803
|
+
}
|
|
41804
|
+
function toArrayBuffer(buf) {
|
|
41805
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
41806
|
+
return buf.buffer;
|
|
41807
|
+
}
|
|
41808
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
41809
|
+
}
|
|
41810
|
+
function toBuffer(data) {
|
|
41811
|
+
toBuffer.readOnly = true;
|
|
41812
|
+
if (Buffer.isBuffer(data))
|
|
41813
|
+
return data;
|
|
41814
|
+
let buf;
|
|
41815
|
+
if (data instanceof ArrayBuffer) {
|
|
41816
|
+
buf = new FastBuffer(data);
|
|
41817
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
41818
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
41819
|
+
} else {
|
|
41820
|
+
buf = Buffer.from(data);
|
|
41821
|
+
toBuffer.readOnly = false;
|
|
41822
|
+
}
|
|
41823
|
+
return buf;
|
|
41824
|
+
}
|
|
41825
|
+
module.exports = {
|
|
41826
|
+
concat,
|
|
41827
|
+
mask: _mask,
|
|
41828
|
+
toArrayBuffer,
|
|
41829
|
+
toBuffer,
|
|
41830
|
+
unmask: _unmask
|
|
41831
|
+
};
|
|
41832
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
41833
|
+
try {
|
|
41834
|
+
const bufferUtil = (()=>{throw new Error("Cannot require module "+"bufferutil");})();
|
|
41835
|
+
module.exports.mask = function(source, mask, output, offset, length) {
|
|
41836
|
+
if (length < 48)
|
|
41837
|
+
_mask(source, mask, output, offset, length);
|
|
41838
|
+
else
|
|
41839
|
+
bufferUtil.mask(source, mask, output, offset, length);
|
|
41840
|
+
};
|
|
41841
|
+
module.exports.unmask = function(buffer, mask) {
|
|
41842
|
+
if (buffer.length < 32)
|
|
41843
|
+
_unmask(buffer, mask);
|
|
41844
|
+
else
|
|
41845
|
+
bufferUtil.unmask(buffer, mask);
|
|
41846
|
+
};
|
|
41847
|
+
} catch (e) {}
|
|
41848
|
+
}
|
|
41849
|
+
});
|
|
41850
|
+
|
|
41851
|
+
// node_modules/ws/lib/limiter.js
|
|
41852
|
+
var require_limiter = __commonJS((exports, module) => {
|
|
41853
|
+
var kDone = Symbol("kDone");
|
|
41854
|
+
var kRun = Symbol("kRun");
|
|
41855
|
+
|
|
41856
|
+
class Limiter {
|
|
41857
|
+
constructor(concurrency) {
|
|
41858
|
+
this[kDone] = () => {
|
|
41859
|
+
this.pending--;
|
|
41860
|
+
this[kRun]();
|
|
41861
|
+
};
|
|
41862
|
+
this.concurrency = concurrency || Infinity;
|
|
41863
|
+
this.jobs = [];
|
|
41864
|
+
this.pending = 0;
|
|
41865
|
+
}
|
|
41866
|
+
add(job) {
|
|
41867
|
+
this.jobs.push(job);
|
|
41868
|
+
this[kRun]();
|
|
41869
|
+
}
|
|
41870
|
+
[kRun]() {
|
|
41871
|
+
if (this.pending === this.concurrency)
|
|
41872
|
+
return;
|
|
41873
|
+
if (this.jobs.length) {
|
|
41874
|
+
const job = this.jobs.shift();
|
|
41875
|
+
this.pending++;
|
|
41876
|
+
job(this[kDone]);
|
|
41877
|
+
}
|
|
41878
|
+
}
|
|
41879
|
+
}
|
|
41880
|
+
module.exports = Limiter;
|
|
41881
|
+
});
|
|
41882
|
+
|
|
41883
|
+
// node_modules/ws/lib/permessage-deflate.js
|
|
41884
|
+
var require_permessage_deflate = __commonJS((exports, module) => {
|
|
41885
|
+
var zlib = __require("zlib");
|
|
41886
|
+
var bufferUtil = require_buffer_util();
|
|
41887
|
+
var Limiter = require_limiter();
|
|
41888
|
+
var { kStatusCode } = require_constants();
|
|
41889
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
41890
|
+
var TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
41891
|
+
var kPerMessageDeflate = Symbol("permessage-deflate");
|
|
41892
|
+
var kTotalLength = Symbol("total-length");
|
|
41893
|
+
var kCallback = Symbol("callback");
|
|
41894
|
+
var kBuffers = Symbol("buffers");
|
|
41895
|
+
var kError = Symbol("error");
|
|
41896
|
+
var zlibLimiter;
|
|
41897
|
+
|
|
41898
|
+
class PerMessageDeflate {
|
|
41899
|
+
constructor(options, isServer, maxPayload) {
|
|
41900
|
+
this._maxPayload = maxPayload | 0;
|
|
41901
|
+
this._options = options || {};
|
|
41902
|
+
this._threshold = this._options.threshold !== undefined ? this._options.threshold : 1024;
|
|
41903
|
+
this._isServer = !!isServer;
|
|
41904
|
+
this._deflate = null;
|
|
41905
|
+
this._inflate = null;
|
|
41906
|
+
this.params = null;
|
|
41907
|
+
if (!zlibLimiter) {
|
|
41908
|
+
const concurrency = this._options.concurrencyLimit !== undefined ? this._options.concurrencyLimit : 10;
|
|
41909
|
+
zlibLimiter = new Limiter(concurrency);
|
|
41910
|
+
}
|
|
41911
|
+
}
|
|
41912
|
+
static get extensionName() {
|
|
41913
|
+
return "permessage-deflate";
|
|
41914
|
+
}
|
|
41915
|
+
offer() {
|
|
41916
|
+
const params = {};
|
|
41917
|
+
if (this._options.serverNoContextTakeover) {
|
|
41918
|
+
params.server_no_context_takeover = true;
|
|
41919
|
+
}
|
|
41920
|
+
if (this._options.clientNoContextTakeover) {
|
|
41921
|
+
params.client_no_context_takeover = true;
|
|
41922
|
+
}
|
|
41923
|
+
if (this._options.serverMaxWindowBits) {
|
|
41924
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
41925
|
+
}
|
|
41926
|
+
if (this._options.clientMaxWindowBits) {
|
|
41927
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
41928
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
41929
|
+
params.client_max_window_bits = true;
|
|
41930
|
+
}
|
|
41931
|
+
return params;
|
|
41932
|
+
}
|
|
41933
|
+
accept(configurations) {
|
|
41934
|
+
configurations = this.normalizeParams(configurations);
|
|
41935
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
41936
|
+
return this.params;
|
|
41937
|
+
}
|
|
41938
|
+
cleanup() {
|
|
41939
|
+
if (this._inflate) {
|
|
41940
|
+
this._inflate.close();
|
|
41941
|
+
this._inflate = null;
|
|
41942
|
+
}
|
|
41943
|
+
if (this._deflate) {
|
|
41944
|
+
const callback = this._deflate[kCallback];
|
|
41945
|
+
this._deflate.close();
|
|
41946
|
+
this._deflate = null;
|
|
41947
|
+
if (callback) {
|
|
41948
|
+
callback(new Error("The deflate stream was closed while data was being processed"));
|
|
41949
|
+
}
|
|
41950
|
+
}
|
|
41951
|
+
}
|
|
41952
|
+
acceptAsServer(offers) {
|
|
41953
|
+
const opts = this._options;
|
|
41954
|
+
const accepted = offers.find((params) => {
|
|
41955
|
+
if (opts.serverNoContextTakeover === false && params.server_no_context_takeover || params.server_max_window_bits && (opts.serverMaxWindowBits === false || typeof opts.serverMaxWindowBits === "number" && opts.serverMaxWindowBits > params.server_max_window_bits) || typeof opts.clientMaxWindowBits === "number" && !params.client_max_window_bits) {
|
|
41956
|
+
return false;
|
|
41957
|
+
}
|
|
41958
|
+
return true;
|
|
41959
|
+
});
|
|
41960
|
+
if (!accepted) {
|
|
41961
|
+
throw new Error("None of the extension offers can be accepted");
|
|
41962
|
+
}
|
|
41963
|
+
if (opts.serverNoContextTakeover) {
|
|
41964
|
+
accepted.server_no_context_takeover = true;
|
|
41965
|
+
}
|
|
41966
|
+
if (opts.clientNoContextTakeover) {
|
|
41967
|
+
accepted.client_no_context_takeover = true;
|
|
41968
|
+
}
|
|
41969
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
41970
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
41971
|
+
}
|
|
41972
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
41973
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
41974
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
41975
|
+
delete accepted.client_max_window_bits;
|
|
41976
|
+
}
|
|
41977
|
+
return accepted;
|
|
41978
|
+
}
|
|
41979
|
+
acceptAsClient(response) {
|
|
41980
|
+
const params = response[0];
|
|
41981
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
41982
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
41983
|
+
}
|
|
41984
|
+
if (!params.client_max_window_bits) {
|
|
41985
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
41986
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
41987
|
+
}
|
|
41988
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
41989
|
+
throw new Error('Unexpected or invalid parameter "client_max_window_bits"');
|
|
41990
|
+
}
|
|
41991
|
+
return params;
|
|
41992
|
+
}
|
|
41993
|
+
normalizeParams(configurations) {
|
|
41994
|
+
configurations.forEach((params) => {
|
|
41995
|
+
Object.keys(params).forEach((key) => {
|
|
41996
|
+
let value = params[key];
|
|
41997
|
+
if (value.length > 1) {
|
|
41998
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
41999
|
+
}
|
|
42000
|
+
value = value[0];
|
|
42001
|
+
if (key === "client_max_window_bits") {
|
|
42002
|
+
if (value !== true) {
|
|
42003
|
+
const num = +value;
|
|
42004
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
42005
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
42006
|
+
}
|
|
42007
|
+
value = num;
|
|
42008
|
+
} else if (!this._isServer) {
|
|
42009
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
42010
|
+
}
|
|
42011
|
+
} else if (key === "server_max_window_bits") {
|
|
42012
|
+
const num = +value;
|
|
42013
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
42014
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
42015
|
+
}
|
|
42016
|
+
value = num;
|
|
42017
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
42018
|
+
if (value !== true) {
|
|
42019
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
42020
|
+
}
|
|
42021
|
+
} else {
|
|
42022
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
42023
|
+
}
|
|
42024
|
+
params[key] = value;
|
|
42025
|
+
});
|
|
42026
|
+
});
|
|
42027
|
+
return configurations;
|
|
42028
|
+
}
|
|
42029
|
+
decompress(data, fin, callback) {
|
|
42030
|
+
zlibLimiter.add((done) => {
|
|
42031
|
+
this._decompress(data, fin, (err, result) => {
|
|
42032
|
+
done();
|
|
42033
|
+
callback(err, result);
|
|
42034
|
+
});
|
|
42035
|
+
});
|
|
42036
|
+
}
|
|
42037
|
+
compress(data, fin, callback) {
|
|
42038
|
+
zlibLimiter.add((done) => {
|
|
42039
|
+
this._compress(data, fin, (err, result) => {
|
|
42040
|
+
done();
|
|
42041
|
+
callback(err, result);
|
|
42042
|
+
});
|
|
42043
|
+
});
|
|
42044
|
+
}
|
|
42045
|
+
_decompress(data, fin, callback) {
|
|
42046
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
42047
|
+
if (!this._inflate) {
|
|
42048
|
+
const key = `${endpoint}_max_window_bits`;
|
|
42049
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
42050
|
+
this._inflate = zlib.createInflateRaw({
|
|
42051
|
+
...this._options.zlibInflateOptions,
|
|
42052
|
+
windowBits
|
|
42053
|
+
});
|
|
42054
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
42055
|
+
this._inflate[kTotalLength] = 0;
|
|
42056
|
+
this._inflate[kBuffers] = [];
|
|
42057
|
+
this._inflate.on("error", inflateOnError);
|
|
42058
|
+
this._inflate.on("data", inflateOnData);
|
|
42059
|
+
}
|
|
42060
|
+
this._inflate[kCallback] = callback;
|
|
42061
|
+
this._inflate.write(data);
|
|
42062
|
+
if (fin)
|
|
42063
|
+
this._inflate.write(TRAILER);
|
|
42064
|
+
this._inflate.flush(() => {
|
|
42065
|
+
const err = this._inflate[kError];
|
|
42066
|
+
if (err) {
|
|
42067
|
+
this._inflate.close();
|
|
42068
|
+
this._inflate = null;
|
|
42069
|
+
callback(err);
|
|
42070
|
+
return;
|
|
42071
|
+
}
|
|
42072
|
+
const data2 = bufferUtil.concat(this._inflate[kBuffers], this._inflate[kTotalLength]);
|
|
42073
|
+
if (this._inflate._readableState.endEmitted) {
|
|
42074
|
+
this._inflate.close();
|
|
42075
|
+
this._inflate = null;
|
|
42076
|
+
} else {
|
|
42077
|
+
this._inflate[kTotalLength] = 0;
|
|
42078
|
+
this._inflate[kBuffers] = [];
|
|
42079
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
42080
|
+
this._inflate.reset();
|
|
42081
|
+
}
|
|
42082
|
+
}
|
|
42083
|
+
callback(null, data2);
|
|
42084
|
+
});
|
|
42085
|
+
}
|
|
42086
|
+
_compress(data, fin, callback) {
|
|
42087
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
42088
|
+
if (!this._deflate) {
|
|
42089
|
+
const key = `${endpoint}_max_window_bits`;
|
|
42090
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
42091
|
+
this._deflate = zlib.createDeflateRaw({
|
|
42092
|
+
...this._options.zlibDeflateOptions,
|
|
42093
|
+
windowBits
|
|
42094
|
+
});
|
|
42095
|
+
this._deflate[kTotalLength] = 0;
|
|
42096
|
+
this._deflate[kBuffers] = [];
|
|
42097
|
+
this._deflate.on("data", deflateOnData);
|
|
42098
|
+
}
|
|
42099
|
+
this._deflate[kCallback] = callback;
|
|
42100
|
+
this._deflate.write(data);
|
|
42101
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
42102
|
+
if (!this._deflate) {
|
|
42103
|
+
return;
|
|
42104
|
+
}
|
|
42105
|
+
let data2 = bufferUtil.concat(this._deflate[kBuffers], this._deflate[kTotalLength]);
|
|
42106
|
+
if (fin) {
|
|
42107
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
42108
|
+
}
|
|
42109
|
+
this._deflate[kCallback] = null;
|
|
42110
|
+
this._deflate[kTotalLength] = 0;
|
|
42111
|
+
this._deflate[kBuffers] = [];
|
|
42112
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
42113
|
+
this._deflate.reset();
|
|
42114
|
+
}
|
|
42115
|
+
callback(null, data2);
|
|
42116
|
+
});
|
|
42117
|
+
}
|
|
42118
|
+
}
|
|
42119
|
+
module.exports = PerMessageDeflate;
|
|
42120
|
+
function deflateOnData(chunk) {
|
|
42121
|
+
this[kBuffers].push(chunk);
|
|
42122
|
+
this[kTotalLength] += chunk.length;
|
|
42123
|
+
}
|
|
42124
|
+
function inflateOnData(chunk) {
|
|
42125
|
+
this[kTotalLength] += chunk.length;
|
|
42126
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
42127
|
+
this[kBuffers].push(chunk);
|
|
42128
|
+
return;
|
|
42129
|
+
}
|
|
42130
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
42131
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
42132
|
+
this[kError][kStatusCode] = 1009;
|
|
42133
|
+
this.removeListener("data", inflateOnData);
|
|
42134
|
+
this.reset();
|
|
42135
|
+
}
|
|
42136
|
+
function inflateOnError(err) {
|
|
42137
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
42138
|
+
if (this[kError]) {
|
|
42139
|
+
this[kCallback](this[kError]);
|
|
42140
|
+
return;
|
|
42141
|
+
}
|
|
42142
|
+
err[kStatusCode] = 1007;
|
|
42143
|
+
this[kCallback](err);
|
|
42144
|
+
}
|
|
42145
|
+
});
|
|
42146
|
+
|
|
42147
|
+
// node_modules/ws/lib/validation.js
|
|
42148
|
+
var require_validation2 = __commonJS((exports, module) => {
|
|
42149
|
+
var { isUtf8 } = __require("buffer");
|
|
42150
|
+
var { hasBlob } = require_constants();
|
|
42151
|
+
var tokenChars = [
|
|
42152
|
+
0,
|
|
42153
|
+
0,
|
|
42154
|
+
0,
|
|
42155
|
+
0,
|
|
42156
|
+
0,
|
|
42157
|
+
0,
|
|
42158
|
+
0,
|
|
42159
|
+
0,
|
|
42160
|
+
0,
|
|
42161
|
+
0,
|
|
42162
|
+
0,
|
|
42163
|
+
0,
|
|
42164
|
+
0,
|
|
42165
|
+
0,
|
|
42166
|
+
0,
|
|
42167
|
+
0,
|
|
42168
|
+
0,
|
|
42169
|
+
0,
|
|
42170
|
+
0,
|
|
42171
|
+
0,
|
|
42172
|
+
0,
|
|
42173
|
+
0,
|
|
42174
|
+
0,
|
|
42175
|
+
0,
|
|
42176
|
+
0,
|
|
42177
|
+
0,
|
|
42178
|
+
0,
|
|
42179
|
+
0,
|
|
42180
|
+
0,
|
|
42181
|
+
0,
|
|
42182
|
+
0,
|
|
42183
|
+
0,
|
|
42184
|
+
0,
|
|
42185
|
+
1,
|
|
42186
|
+
0,
|
|
42187
|
+
1,
|
|
42188
|
+
1,
|
|
42189
|
+
1,
|
|
42190
|
+
1,
|
|
42191
|
+
1,
|
|
42192
|
+
0,
|
|
42193
|
+
0,
|
|
42194
|
+
1,
|
|
42195
|
+
1,
|
|
42196
|
+
0,
|
|
42197
|
+
1,
|
|
42198
|
+
1,
|
|
42199
|
+
0,
|
|
42200
|
+
1,
|
|
42201
|
+
1,
|
|
42202
|
+
1,
|
|
42203
|
+
1,
|
|
42204
|
+
1,
|
|
42205
|
+
1,
|
|
42206
|
+
1,
|
|
42207
|
+
1,
|
|
42208
|
+
1,
|
|
42209
|
+
1,
|
|
42210
|
+
0,
|
|
42211
|
+
0,
|
|
42212
|
+
0,
|
|
42213
|
+
0,
|
|
42214
|
+
0,
|
|
42215
|
+
0,
|
|
42216
|
+
0,
|
|
42217
|
+
1,
|
|
42218
|
+
1,
|
|
42219
|
+
1,
|
|
42220
|
+
1,
|
|
42221
|
+
1,
|
|
42222
|
+
1,
|
|
42223
|
+
1,
|
|
42224
|
+
1,
|
|
42225
|
+
1,
|
|
42226
|
+
1,
|
|
42227
|
+
1,
|
|
42228
|
+
1,
|
|
42229
|
+
1,
|
|
42230
|
+
1,
|
|
42231
|
+
1,
|
|
42232
|
+
1,
|
|
42233
|
+
1,
|
|
42234
|
+
1,
|
|
42235
|
+
1,
|
|
42236
|
+
1,
|
|
42237
|
+
1,
|
|
42238
|
+
1,
|
|
42239
|
+
1,
|
|
42240
|
+
1,
|
|
42241
|
+
1,
|
|
42242
|
+
1,
|
|
42243
|
+
0,
|
|
42244
|
+
0,
|
|
42245
|
+
0,
|
|
42246
|
+
1,
|
|
42247
|
+
1,
|
|
42248
|
+
1,
|
|
42249
|
+
1,
|
|
42250
|
+
1,
|
|
42251
|
+
1,
|
|
42252
|
+
1,
|
|
42253
|
+
1,
|
|
42254
|
+
1,
|
|
42255
|
+
1,
|
|
42256
|
+
1,
|
|
42257
|
+
1,
|
|
42258
|
+
1,
|
|
42259
|
+
1,
|
|
42260
|
+
1,
|
|
42261
|
+
1,
|
|
42262
|
+
1,
|
|
42263
|
+
1,
|
|
42264
|
+
1,
|
|
42265
|
+
1,
|
|
42266
|
+
1,
|
|
42267
|
+
1,
|
|
42268
|
+
1,
|
|
42269
|
+
1,
|
|
42270
|
+
1,
|
|
42271
|
+
1,
|
|
42272
|
+
1,
|
|
42273
|
+
1,
|
|
42274
|
+
1,
|
|
42275
|
+
0,
|
|
42276
|
+
1,
|
|
42277
|
+
0,
|
|
42278
|
+
1,
|
|
42279
|
+
0
|
|
42280
|
+
];
|
|
42281
|
+
function isValidStatusCode(code) {
|
|
42282
|
+
return code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3000 && code <= 4999;
|
|
42283
|
+
}
|
|
42284
|
+
function _isValidUTF8(buf) {
|
|
42285
|
+
const len = buf.length;
|
|
42286
|
+
let i = 0;
|
|
42287
|
+
while (i < len) {
|
|
42288
|
+
if ((buf[i] & 128) === 0) {
|
|
42289
|
+
i++;
|
|
42290
|
+
} else if ((buf[i] & 224) === 192) {
|
|
42291
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) {
|
|
42292
|
+
return false;
|
|
42293
|
+
}
|
|
42294
|
+
i += 2;
|
|
42295
|
+
} else if ((buf[i] & 240) === 224) {
|
|
42296
|
+
if (i + 2 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || buf[i] === 224 && (buf[i + 1] & 224) === 128 || buf[i] === 237 && (buf[i + 1] & 224) === 160) {
|
|
42297
|
+
return false;
|
|
42298
|
+
}
|
|
42299
|
+
i += 3;
|
|
42300
|
+
} else if ((buf[i] & 248) === 240) {
|
|
42301
|
+
if (i + 3 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || (buf[i + 3] & 192) !== 128 || buf[i] === 240 && (buf[i + 1] & 240) === 128 || buf[i] === 244 && buf[i + 1] > 143 || buf[i] > 244) {
|
|
42302
|
+
return false;
|
|
42303
|
+
}
|
|
42304
|
+
i += 4;
|
|
42305
|
+
} else {
|
|
42306
|
+
return false;
|
|
42307
|
+
}
|
|
42308
|
+
}
|
|
42309
|
+
return true;
|
|
42310
|
+
}
|
|
42311
|
+
function isBlob(value) {
|
|
42312
|
+
return hasBlob && typeof value === "object" && typeof value.arrayBuffer === "function" && typeof value.type === "string" && typeof value.stream === "function" && (value[Symbol.toStringTag] === "Blob" || value[Symbol.toStringTag] === "File");
|
|
42313
|
+
}
|
|
42314
|
+
module.exports = {
|
|
42315
|
+
isBlob,
|
|
42316
|
+
isValidStatusCode,
|
|
42317
|
+
isValidUTF8: _isValidUTF8,
|
|
42318
|
+
tokenChars
|
|
42319
|
+
};
|
|
42320
|
+
if (isUtf8) {
|
|
42321
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
42322
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
42323
|
+
};
|
|
42324
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
42325
|
+
try {
|
|
42326
|
+
const isValidUTF8 = (()=>{throw new Error("Cannot require module "+"utf-8-validate");})();
|
|
42327
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
42328
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
42329
|
+
};
|
|
42330
|
+
} catch (e) {}
|
|
42331
|
+
}
|
|
42332
|
+
});
|
|
42333
|
+
|
|
42334
|
+
// node_modules/ws/lib/receiver.js
|
|
42335
|
+
var require_receiver = __commonJS((exports, module) => {
|
|
42336
|
+
var { Writable } = __require("stream");
|
|
42337
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
42338
|
+
var {
|
|
42339
|
+
BINARY_TYPES,
|
|
42340
|
+
EMPTY_BUFFER,
|
|
42341
|
+
kStatusCode,
|
|
42342
|
+
kWebSocket
|
|
42343
|
+
} = require_constants();
|
|
42344
|
+
var { concat, toArrayBuffer, unmask } = require_buffer_util();
|
|
42345
|
+
var { isValidStatusCode, isValidUTF8 } = require_validation2();
|
|
42346
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
42347
|
+
var GET_INFO = 0;
|
|
42348
|
+
var GET_PAYLOAD_LENGTH_16 = 1;
|
|
42349
|
+
var GET_PAYLOAD_LENGTH_64 = 2;
|
|
42350
|
+
var GET_MASK = 3;
|
|
42351
|
+
var GET_DATA = 4;
|
|
42352
|
+
var INFLATING = 5;
|
|
42353
|
+
var DEFER_EVENT = 6;
|
|
42354
|
+
|
|
42355
|
+
class Receiver extends Writable {
|
|
42356
|
+
constructor(options = {}) {
|
|
42357
|
+
super();
|
|
42358
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== undefined ? options.allowSynchronousEvents : true;
|
|
42359
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
42360
|
+
this._extensions = options.extensions || {};
|
|
42361
|
+
this._isServer = !!options.isServer;
|
|
42362
|
+
this._maxPayload = options.maxPayload | 0;
|
|
42363
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
42364
|
+
this[kWebSocket] = undefined;
|
|
42365
|
+
this._bufferedBytes = 0;
|
|
42366
|
+
this._buffers = [];
|
|
42367
|
+
this._compressed = false;
|
|
42368
|
+
this._payloadLength = 0;
|
|
42369
|
+
this._mask = undefined;
|
|
42370
|
+
this._fragmented = 0;
|
|
42371
|
+
this._masked = false;
|
|
42372
|
+
this._fin = false;
|
|
42373
|
+
this._opcode = 0;
|
|
42374
|
+
this._totalPayloadLength = 0;
|
|
42375
|
+
this._messageLength = 0;
|
|
42376
|
+
this._fragments = [];
|
|
42377
|
+
this._errored = false;
|
|
42378
|
+
this._loop = false;
|
|
42379
|
+
this._state = GET_INFO;
|
|
42380
|
+
}
|
|
42381
|
+
_write(chunk, encoding, cb) {
|
|
42382
|
+
if (this._opcode === 8 && this._state == GET_INFO)
|
|
42383
|
+
return cb();
|
|
42384
|
+
this._bufferedBytes += chunk.length;
|
|
42385
|
+
this._buffers.push(chunk);
|
|
42386
|
+
this.startLoop(cb);
|
|
42387
|
+
}
|
|
42388
|
+
consume(n) {
|
|
42389
|
+
this._bufferedBytes -= n;
|
|
42390
|
+
if (n === this._buffers[0].length)
|
|
42391
|
+
return this._buffers.shift();
|
|
42392
|
+
if (n < this._buffers[0].length) {
|
|
42393
|
+
const buf = this._buffers[0];
|
|
42394
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
42395
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
42396
|
+
}
|
|
42397
|
+
const dst = Buffer.allocUnsafe(n);
|
|
42398
|
+
do {
|
|
42399
|
+
const buf = this._buffers[0];
|
|
42400
|
+
const offset = dst.length - n;
|
|
42401
|
+
if (n >= buf.length) {
|
|
42402
|
+
dst.set(this._buffers.shift(), offset);
|
|
42403
|
+
} else {
|
|
42404
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
42405
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
42406
|
+
}
|
|
42407
|
+
n -= buf.length;
|
|
42408
|
+
} while (n > 0);
|
|
42409
|
+
return dst;
|
|
42410
|
+
}
|
|
42411
|
+
startLoop(cb) {
|
|
42412
|
+
this._loop = true;
|
|
42413
|
+
do {
|
|
42414
|
+
switch (this._state) {
|
|
42415
|
+
case GET_INFO:
|
|
42416
|
+
this.getInfo(cb);
|
|
42417
|
+
break;
|
|
42418
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
42419
|
+
this.getPayloadLength16(cb);
|
|
42420
|
+
break;
|
|
42421
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
42422
|
+
this.getPayloadLength64(cb);
|
|
42423
|
+
break;
|
|
42424
|
+
case GET_MASK:
|
|
42425
|
+
this.getMask();
|
|
42426
|
+
break;
|
|
42427
|
+
case GET_DATA:
|
|
42428
|
+
this.getData(cb);
|
|
42429
|
+
break;
|
|
42430
|
+
case INFLATING:
|
|
42431
|
+
case DEFER_EVENT:
|
|
42432
|
+
this._loop = false;
|
|
42433
|
+
return;
|
|
42434
|
+
}
|
|
42435
|
+
} while (this._loop);
|
|
42436
|
+
if (!this._errored)
|
|
42437
|
+
cb();
|
|
42438
|
+
}
|
|
42439
|
+
getInfo(cb) {
|
|
42440
|
+
if (this._bufferedBytes < 2) {
|
|
42441
|
+
this._loop = false;
|
|
42442
|
+
return;
|
|
42443
|
+
}
|
|
42444
|
+
const buf = this.consume(2);
|
|
42445
|
+
if ((buf[0] & 48) !== 0) {
|
|
42446
|
+
const error = this.createError(RangeError, "RSV2 and RSV3 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_2_3");
|
|
42447
|
+
cb(error);
|
|
42448
|
+
return;
|
|
42449
|
+
}
|
|
42450
|
+
const compressed = (buf[0] & 64) === 64;
|
|
42451
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
42452
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
42453
|
+
cb(error);
|
|
42454
|
+
return;
|
|
42455
|
+
}
|
|
42456
|
+
this._fin = (buf[0] & 128) === 128;
|
|
42457
|
+
this._opcode = buf[0] & 15;
|
|
42458
|
+
this._payloadLength = buf[1] & 127;
|
|
42459
|
+
if (this._opcode === 0) {
|
|
42460
|
+
if (compressed) {
|
|
42461
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
42462
|
+
cb(error);
|
|
42463
|
+
return;
|
|
42464
|
+
}
|
|
42465
|
+
if (!this._fragmented) {
|
|
42466
|
+
const error = this.createError(RangeError, "invalid opcode 0", true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
42467
|
+
cb(error);
|
|
42468
|
+
return;
|
|
42469
|
+
}
|
|
42470
|
+
this._opcode = this._fragmented;
|
|
42471
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
42472
|
+
if (this._fragmented) {
|
|
42473
|
+
const error = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
42474
|
+
cb(error);
|
|
42475
|
+
return;
|
|
42476
|
+
}
|
|
42477
|
+
this._compressed = compressed;
|
|
42478
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
42479
|
+
if (!this._fin) {
|
|
42480
|
+
const error = this.createError(RangeError, "FIN must be set", true, 1002, "WS_ERR_EXPECTED_FIN");
|
|
42481
|
+
cb(error);
|
|
42482
|
+
return;
|
|
42483
|
+
}
|
|
42484
|
+
if (compressed) {
|
|
42485
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
42486
|
+
cb(error);
|
|
42487
|
+
return;
|
|
42488
|
+
}
|
|
42489
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
42490
|
+
const error = this.createError(RangeError, `invalid payload length ${this._payloadLength}`, true, 1002, "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");
|
|
42491
|
+
cb(error);
|
|
42492
|
+
return;
|
|
42493
|
+
}
|
|
42494
|
+
} else {
|
|
42495
|
+
const error = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
42496
|
+
cb(error);
|
|
42497
|
+
return;
|
|
42498
|
+
}
|
|
42499
|
+
if (!this._fin && !this._fragmented)
|
|
42500
|
+
this._fragmented = this._opcode;
|
|
42501
|
+
this._masked = (buf[1] & 128) === 128;
|
|
42502
|
+
if (this._isServer) {
|
|
42503
|
+
if (!this._masked) {
|
|
42504
|
+
const error = this.createError(RangeError, "MASK must be set", true, 1002, "WS_ERR_EXPECTED_MASK");
|
|
42505
|
+
cb(error);
|
|
42506
|
+
return;
|
|
42507
|
+
}
|
|
42508
|
+
} else if (this._masked) {
|
|
42509
|
+
const error = this.createError(RangeError, "MASK must be clear", true, 1002, "WS_ERR_UNEXPECTED_MASK");
|
|
42510
|
+
cb(error);
|
|
42511
|
+
return;
|
|
42512
|
+
}
|
|
42513
|
+
if (this._payloadLength === 126)
|
|
42514
|
+
this._state = GET_PAYLOAD_LENGTH_16;
|
|
42515
|
+
else if (this._payloadLength === 127)
|
|
42516
|
+
this._state = GET_PAYLOAD_LENGTH_64;
|
|
42517
|
+
else
|
|
42518
|
+
this.haveLength(cb);
|
|
42519
|
+
}
|
|
42520
|
+
getPayloadLength16(cb) {
|
|
42521
|
+
if (this._bufferedBytes < 2) {
|
|
42522
|
+
this._loop = false;
|
|
42523
|
+
return;
|
|
42524
|
+
}
|
|
42525
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
42526
|
+
this.haveLength(cb);
|
|
42527
|
+
}
|
|
42528
|
+
getPayloadLength64(cb) {
|
|
42529
|
+
if (this._bufferedBytes < 8) {
|
|
42530
|
+
this._loop = false;
|
|
42531
|
+
return;
|
|
42532
|
+
}
|
|
42533
|
+
const buf = this.consume(8);
|
|
42534
|
+
const num = buf.readUInt32BE(0);
|
|
42535
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
42536
|
+
const error = this.createError(RangeError, "Unsupported WebSocket frame: payload length > 2^53 - 1", false, 1009, "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");
|
|
42537
|
+
cb(error);
|
|
42538
|
+
return;
|
|
42539
|
+
}
|
|
42540
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
42541
|
+
this.haveLength(cb);
|
|
42542
|
+
}
|
|
42543
|
+
haveLength(cb) {
|
|
42544
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
42545
|
+
this._totalPayloadLength += this._payloadLength;
|
|
42546
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
42547
|
+
const error = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
42548
|
+
cb(error);
|
|
42549
|
+
return;
|
|
42550
|
+
}
|
|
42551
|
+
}
|
|
42552
|
+
if (this._masked)
|
|
42553
|
+
this._state = GET_MASK;
|
|
42554
|
+
else
|
|
42555
|
+
this._state = GET_DATA;
|
|
42556
|
+
}
|
|
42557
|
+
getMask() {
|
|
42558
|
+
if (this._bufferedBytes < 4) {
|
|
42559
|
+
this._loop = false;
|
|
42560
|
+
return;
|
|
42561
|
+
}
|
|
42562
|
+
this._mask = this.consume(4);
|
|
42563
|
+
this._state = GET_DATA;
|
|
42564
|
+
}
|
|
42565
|
+
getData(cb) {
|
|
42566
|
+
let data = EMPTY_BUFFER;
|
|
42567
|
+
if (this._payloadLength) {
|
|
42568
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
42569
|
+
this._loop = false;
|
|
42570
|
+
return;
|
|
42571
|
+
}
|
|
42572
|
+
data = this.consume(this._payloadLength);
|
|
42573
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
42574
|
+
unmask(data, this._mask);
|
|
42575
|
+
}
|
|
42576
|
+
}
|
|
42577
|
+
if (this._opcode > 7) {
|
|
42578
|
+
this.controlMessage(data, cb);
|
|
42579
|
+
return;
|
|
42580
|
+
}
|
|
42581
|
+
if (this._compressed) {
|
|
42582
|
+
this._state = INFLATING;
|
|
42583
|
+
this.decompress(data, cb);
|
|
42584
|
+
return;
|
|
42585
|
+
}
|
|
42586
|
+
if (data.length) {
|
|
42587
|
+
this._messageLength = this._totalPayloadLength;
|
|
42588
|
+
this._fragments.push(data);
|
|
42589
|
+
}
|
|
42590
|
+
this.dataMessage(cb);
|
|
42591
|
+
}
|
|
42592
|
+
decompress(data, cb) {
|
|
42593
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
42594
|
+
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
|
42595
|
+
if (err)
|
|
42596
|
+
return cb(err);
|
|
42597
|
+
if (buf.length) {
|
|
42598
|
+
this._messageLength += buf.length;
|
|
42599
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
42600
|
+
const error = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
42601
|
+
cb(error);
|
|
42602
|
+
return;
|
|
42603
|
+
}
|
|
42604
|
+
this._fragments.push(buf);
|
|
42605
|
+
}
|
|
42606
|
+
this.dataMessage(cb);
|
|
42607
|
+
if (this._state === GET_INFO)
|
|
42608
|
+
this.startLoop(cb);
|
|
42609
|
+
});
|
|
42610
|
+
}
|
|
42611
|
+
dataMessage(cb) {
|
|
42612
|
+
if (!this._fin) {
|
|
42613
|
+
this._state = GET_INFO;
|
|
42614
|
+
return;
|
|
42615
|
+
}
|
|
42616
|
+
const messageLength = this._messageLength;
|
|
42617
|
+
const fragments = this._fragments;
|
|
42618
|
+
this._totalPayloadLength = 0;
|
|
42619
|
+
this._messageLength = 0;
|
|
42620
|
+
this._fragmented = 0;
|
|
42621
|
+
this._fragments = [];
|
|
42622
|
+
if (this._opcode === 2) {
|
|
42623
|
+
let data;
|
|
42624
|
+
if (this._binaryType === "nodebuffer") {
|
|
42625
|
+
data = concat(fragments, messageLength);
|
|
42626
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
42627
|
+
data = toArrayBuffer(concat(fragments, messageLength));
|
|
42628
|
+
} else if (this._binaryType === "blob") {
|
|
42629
|
+
data = new Blob(fragments);
|
|
42630
|
+
} else {
|
|
42631
|
+
data = fragments;
|
|
42632
|
+
}
|
|
42633
|
+
if (this._allowSynchronousEvents) {
|
|
42634
|
+
this.emit("message", data, true);
|
|
42635
|
+
this._state = GET_INFO;
|
|
42636
|
+
} else {
|
|
42637
|
+
this._state = DEFER_EVENT;
|
|
42638
|
+
setImmediate(() => {
|
|
42639
|
+
this.emit("message", data, true);
|
|
42640
|
+
this._state = GET_INFO;
|
|
42641
|
+
this.startLoop(cb);
|
|
42642
|
+
});
|
|
42643
|
+
}
|
|
42644
|
+
} else {
|
|
42645
|
+
const buf = concat(fragments, messageLength);
|
|
42646
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
42647
|
+
const error = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
42648
|
+
cb(error);
|
|
42649
|
+
return;
|
|
42650
|
+
}
|
|
42651
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
42652
|
+
this.emit("message", buf, false);
|
|
42653
|
+
this._state = GET_INFO;
|
|
42654
|
+
} else {
|
|
42655
|
+
this._state = DEFER_EVENT;
|
|
42656
|
+
setImmediate(() => {
|
|
42657
|
+
this.emit("message", buf, false);
|
|
42658
|
+
this._state = GET_INFO;
|
|
42659
|
+
this.startLoop(cb);
|
|
42660
|
+
});
|
|
42661
|
+
}
|
|
42662
|
+
}
|
|
42663
|
+
}
|
|
42664
|
+
controlMessage(data, cb) {
|
|
42665
|
+
if (this._opcode === 8) {
|
|
42666
|
+
if (data.length === 0) {
|
|
42667
|
+
this._loop = false;
|
|
42668
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
42669
|
+
this.end();
|
|
42670
|
+
} else {
|
|
42671
|
+
const code = data.readUInt16BE(0);
|
|
42672
|
+
if (!isValidStatusCode(code)) {
|
|
42673
|
+
const error = this.createError(RangeError, `invalid status code ${code}`, true, 1002, "WS_ERR_INVALID_CLOSE_CODE");
|
|
42674
|
+
cb(error);
|
|
42675
|
+
return;
|
|
42676
|
+
}
|
|
42677
|
+
const buf = new FastBuffer(data.buffer, data.byteOffset + 2, data.length - 2);
|
|
42678
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
42679
|
+
const error = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
42680
|
+
cb(error);
|
|
42681
|
+
return;
|
|
42682
|
+
}
|
|
42683
|
+
this._loop = false;
|
|
42684
|
+
this.emit("conclude", code, buf);
|
|
42685
|
+
this.end();
|
|
42686
|
+
}
|
|
42687
|
+
this._state = GET_INFO;
|
|
42688
|
+
return;
|
|
42689
|
+
}
|
|
42690
|
+
if (this._allowSynchronousEvents) {
|
|
42691
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
42692
|
+
this._state = GET_INFO;
|
|
42693
|
+
} else {
|
|
42694
|
+
this._state = DEFER_EVENT;
|
|
42695
|
+
setImmediate(() => {
|
|
42696
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
42697
|
+
this._state = GET_INFO;
|
|
42698
|
+
this.startLoop(cb);
|
|
42699
|
+
});
|
|
42700
|
+
}
|
|
42701
|
+
}
|
|
42702
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
42703
|
+
this._loop = false;
|
|
42704
|
+
this._errored = true;
|
|
42705
|
+
const err = new ErrorCtor(prefix ? `Invalid WebSocket frame: ${message}` : message);
|
|
42706
|
+
Error.captureStackTrace(err, this.createError);
|
|
42707
|
+
err.code = errorCode;
|
|
42708
|
+
err[kStatusCode] = statusCode;
|
|
42709
|
+
return err;
|
|
42710
|
+
}
|
|
42711
|
+
}
|
|
42712
|
+
module.exports = Receiver;
|
|
42713
|
+
});
|
|
42714
|
+
|
|
42715
|
+
// node_modules/ws/lib/sender.js
|
|
42716
|
+
var require_sender = __commonJS((exports, module) => {
|
|
42717
|
+
var { Duplex } = __require("stream");
|
|
42718
|
+
var { randomFillSync } = __require("crypto");
|
|
42719
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
42720
|
+
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
|
|
42721
|
+
var { isBlob, isValidStatusCode } = require_validation2();
|
|
42722
|
+
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
42723
|
+
var kByteLength = Symbol("kByteLength");
|
|
42724
|
+
var maskBuffer = Buffer.alloc(4);
|
|
42725
|
+
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
42726
|
+
var randomPool;
|
|
42727
|
+
var randomPoolPointer = RANDOM_POOL_SIZE;
|
|
42728
|
+
var DEFAULT = 0;
|
|
42729
|
+
var DEFLATING = 1;
|
|
42730
|
+
var GET_BLOB_DATA = 2;
|
|
42731
|
+
|
|
42732
|
+
class Sender {
|
|
42733
|
+
constructor(socket, extensions, generateMask) {
|
|
42734
|
+
this._extensions = extensions || {};
|
|
42735
|
+
if (generateMask) {
|
|
42736
|
+
this._generateMask = generateMask;
|
|
42737
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
42738
|
+
}
|
|
42739
|
+
this._socket = socket;
|
|
42740
|
+
this._firstFragment = true;
|
|
42741
|
+
this._compress = false;
|
|
42742
|
+
this._bufferedBytes = 0;
|
|
42743
|
+
this._queue = [];
|
|
42744
|
+
this._state = DEFAULT;
|
|
42745
|
+
this.onerror = NOOP;
|
|
42746
|
+
this[kWebSocket] = undefined;
|
|
42747
|
+
}
|
|
42748
|
+
static frame(data, options) {
|
|
42749
|
+
let mask;
|
|
42750
|
+
let merge = false;
|
|
42751
|
+
let offset = 2;
|
|
42752
|
+
let skipMasking = false;
|
|
42753
|
+
if (options.mask) {
|
|
42754
|
+
mask = options.maskBuffer || maskBuffer;
|
|
42755
|
+
if (options.generateMask) {
|
|
42756
|
+
options.generateMask(mask);
|
|
42757
|
+
} else {
|
|
42758
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
42759
|
+
if (randomPool === undefined) {
|
|
42760
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
42761
|
+
}
|
|
42762
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
42763
|
+
randomPoolPointer = 0;
|
|
42764
|
+
}
|
|
42765
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
42766
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
42767
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
42768
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
42769
|
+
}
|
|
42770
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
42771
|
+
offset = 6;
|
|
42772
|
+
}
|
|
42773
|
+
let dataLength;
|
|
42774
|
+
if (typeof data === "string") {
|
|
42775
|
+
if ((!options.mask || skipMasking) && options[kByteLength] !== undefined) {
|
|
42776
|
+
dataLength = options[kByteLength];
|
|
42777
|
+
} else {
|
|
42778
|
+
data = Buffer.from(data);
|
|
42779
|
+
dataLength = data.length;
|
|
42780
|
+
}
|
|
42781
|
+
} else {
|
|
42782
|
+
dataLength = data.length;
|
|
42783
|
+
merge = options.mask && options.readOnly && !skipMasking;
|
|
42784
|
+
}
|
|
42785
|
+
let payloadLength = dataLength;
|
|
42786
|
+
if (dataLength >= 65536) {
|
|
42787
|
+
offset += 8;
|
|
42788
|
+
payloadLength = 127;
|
|
42789
|
+
} else if (dataLength > 125) {
|
|
42790
|
+
offset += 2;
|
|
42791
|
+
payloadLength = 126;
|
|
42792
|
+
}
|
|
42793
|
+
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
|
42794
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
42795
|
+
if (options.rsv1)
|
|
42796
|
+
target[0] |= 64;
|
|
42797
|
+
target[1] = payloadLength;
|
|
42798
|
+
if (payloadLength === 126) {
|
|
42799
|
+
target.writeUInt16BE(dataLength, 2);
|
|
42800
|
+
} else if (payloadLength === 127) {
|
|
42801
|
+
target[2] = target[3] = 0;
|
|
42802
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
42803
|
+
}
|
|
42804
|
+
if (!options.mask)
|
|
42805
|
+
return [target, data];
|
|
42806
|
+
target[1] |= 128;
|
|
42807
|
+
target[offset - 4] = mask[0];
|
|
42808
|
+
target[offset - 3] = mask[1];
|
|
42809
|
+
target[offset - 2] = mask[2];
|
|
42810
|
+
target[offset - 1] = mask[3];
|
|
42811
|
+
if (skipMasking)
|
|
42812
|
+
return [target, data];
|
|
42813
|
+
if (merge) {
|
|
42814
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
42815
|
+
return [target];
|
|
42816
|
+
}
|
|
42817
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
42818
|
+
return [target, data];
|
|
42819
|
+
}
|
|
42820
|
+
close(code, data, mask, cb) {
|
|
42821
|
+
let buf;
|
|
42822
|
+
if (code === undefined) {
|
|
42823
|
+
buf = EMPTY_BUFFER;
|
|
42824
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
42825
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
42826
|
+
} else if (data === undefined || !data.length) {
|
|
42827
|
+
buf = Buffer.allocUnsafe(2);
|
|
42828
|
+
buf.writeUInt16BE(code, 0);
|
|
42829
|
+
} else {
|
|
42830
|
+
const length = Buffer.byteLength(data);
|
|
42831
|
+
if (length > 123) {
|
|
42832
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
42833
|
+
}
|
|
42834
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
42835
|
+
buf.writeUInt16BE(code, 0);
|
|
42836
|
+
if (typeof data === "string") {
|
|
42837
|
+
buf.write(data, 2);
|
|
42838
|
+
} else {
|
|
42839
|
+
buf.set(data, 2);
|
|
42840
|
+
}
|
|
42841
|
+
}
|
|
42842
|
+
const options = {
|
|
42843
|
+
[kByteLength]: buf.length,
|
|
42844
|
+
fin: true,
|
|
42845
|
+
generateMask: this._generateMask,
|
|
42846
|
+
mask,
|
|
42847
|
+
maskBuffer: this._maskBuffer,
|
|
42848
|
+
opcode: 8,
|
|
42849
|
+
readOnly: false,
|
|
42850
|
+
rsv1: false
|
|
42851
|
+
};
|
|
42852
|
+
if (this._state !== DEFAULT) {
|
|
42853
|
+
this.enqueue([this.dispatch, buf, false, options, cb]);
|
|
42854
|
+
} else {
|
|
42855
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
42856
|
+
}
|
|
42857
|
+
}
|
|
42858
|
+
ping(data, mask, cb) {
|
|
42859
|
+
let byteLength;
|
|
42860
|
+
let readOnly;
|
|
42861
|
+
if (typeof data === "string") {
|
|
42862
|
+
byteLength = Buffer.byteLength(data);
|
|
42863
|
+
readOnly = false;
|
|
42864
|
+
} else if (isBlob(data)) {
|
|
42865
|
+
byteLength = data.size;
|
|
42866
|
+
readOnly = false;
|
|
42867
|
+
} else {
|
|
42868
|
+
data = toBuffer(data);
|
|
42869
|
+
byteLength = data.length;
|
|
42870
|
+
readOnly = toBuffer.readOnly;
|
|
42871
|
+
}
|
|
42872
|
+
if (byteLength > 125) {
|
|
42873
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
42874
|
+
}
|
|
42875
|
+
const options = {
|
|
42876
|
+
[kByteLength]: byteLength,
|
|
42877
|
+
fin: true,
|
|
42878
|
+
generateMask: this._generateMask,
|
|
42879
|
+
mask,
|
|
42880
|
+
maskBuffer: this._maskBuffer,
|
|
42881
|
+
opcode: 9,
|
|
42882
|
+
readOnly,
|
|
42883
|
+
rsv1: false
|
|
42884
|
+
};
|
|
42885
|
+
if (isBlob(data)) {
|
|
42886
|
+
if (this._state !== DEFAULT) {
|
|
42887
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
42888
|
+
} else {
|
|
42889
|
+
this.getBlobData(data, false, options, cb);
|
|
42890
|
+
}
|
|
42891
|
+
} else if (this._state !== DEFAULT) {
|
|
42892
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
42893
|
+
} else {
|
|
42894
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
42895
|
+
}
|
|
42896
|
+
}
|
|
42897
|
+
pong(data, mask, cb) {
|
|
42898
|
+
let byteLength;
|
|
42899
|
+
let readOnly;
|
|
42900
|
+
if (typeof data === "string") {
|
|
42901
|
+
byteLength = Buffer.byteLength(data);
|
|
42902
|
+
readOnly = false;
|
|
42903
|
+
} else if (isBlob(data)) {
|
|
42904
|
+
byteLength = data.size;
|
|
42905
|
+
readOnly = false;
|
|
42906
|
+
} else {
|
|
42907
|
+
data = toBuffer(data);
|
|
42908
|
+
byteLength = data.length;
|
|
42909
|
+
readOnly = toBuffer.readOnly;
|
|
42910
|
+
}
|
|
42911
|
+
if (byteLength > 125) {
|
|
42912
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
42913
|
+
}
|
|
42914
|
+
const options = {
|
|
42915
|
+
[kByteLength]: byteLength,
|
|
42916
|
+
fin: true,
|
|
42917
|
+
generateMask: this._generateMask,
|
|
42918
|
+
mask,
|
|
42919
|
+
maskBuffer: this._maskBuffer,
|
|
42920
|
+
opcode: 10,
|
|
42921
|
+
readOnly,
|
|
42922
|
+
rsv1: false
|
|
42923
|
+
};
|
|
42924
|
+
if (isBlob(data)) {
|
|
42925
|
+
if (this._state !== DEFAULT) {
|
|
42926
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
42927
|
+
} else {
|
|
42928
|
+
this.getBlobData(data, false, options, cb);
|
|
42929
|
+
}
|
|
42930
|
+
} else if (this._state !== DEFAULT) {
|
|
42931
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
42932
|
+
} else {
|
|
42933
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
42934
|
+
}
|
|
42935
|
+
}
|
|
42936
|
+
send(data, options, cb) {
|
|
42937
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
42938
|
+
let opcode = options.binary ? 2 : 1;
|
|
42939
|
+
let rsv1 = options.compress;
|
|
42940
|
+
let byteLength;
|
|
42941
|
+
let readOnly;
|
|
42942
|
+
if (typeof data === "string") {
|
|
42943
|
+
byteLength = Buffer.byteLength(data);
|
|
42944
|
+
readOnly = false;
|
|
42945
|
+
} else if (isBlob(data)) {
|
|
42946
|
+
byteLength = data.size;
|
|
42947
|
+
readOnly = false;
|
|
42948
|
+
} else {
|
|
42949
|
+
data = toBuffer(data);
|
|
42950
|
+
byteLength = data.length;
|
|
42951
|
+
readOnly = toBuffer.readOnly;
|
|
42952
|
+
}
|
|
42953
|
+
if (this._firstFragment) {
|
|
42954
|
+
this._firstFragment = false;
|
|
42955
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
42956
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
42957
|
+
}
|
|
42958
|
+
this._compress = rsv1;
|
|
42959
|
+
} else {
|
|
42960
|
+
rsv1 = false;
|
|
42961
|
+
opcode = 0;
|
|
42962
|
+
}
|
|
42963
|
+
if (options.fin)
|
|
42964
|
+
this._firstFragment = true;
|
|
42965
|
+
const opts = {
|
|
42966
|
+
[kByteLength]: byteLength,
|
|
42967
|
+
fin: options.fin,
|
|
42968
|
+
generateMask: this._generateMask,
|
|
42969
|
+
mask: options.mask,
|
|
42970
|
+
maskBuffer: this._maskBuffer,
|
|
42971
|
+
opcode,
|
|
42972
|
+
readOnly,
|
|
42973
|
+
rsv1
|
|
42974
|
+
};
|
|
42975
|
+
if (isBlob(data)) {
|
|
42976
|
+
if (this._state !== DEFAULT) {
|
|
42977
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
42978
|
+
} else {
|
|
42979
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
42980
|
+
}
|
|
42981
|
+
} else if (this._state !== DEFAULT) {
|
|
42982
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
42983
|
+
} else {
|
|
42984
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
42985
|
+
}
|
|
42986
|
+
}
|
|
42987
|
+
getBlobData(blob, compress, options, cb) {
|
|
42988
|
+
this._bufferedBytes += options[kByteLength];
|
|
42989
|
+
this._state = GET_BLOB_DATA;
|
|
42990
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
42991
|
+
if (this._socket.destroyed) {
|
|
42992
|
+
const err = new Error("The socket was closed while the blob was being read");
|
|
42993
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
42994
|
+
return;
|
|
42995
|
+
}
|
|
42996
|
+
this._bufferedBytes -= options[kByteLength];
|
|
42997
|
+
const data = toBuffer(arrayBuffer);
|
|
42998
|
+
if (!compress) {
|
|
42999
|
+
this._state = DEFAULT;
|
|
43000
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
43001
|
+
this.dequeue();
|
|
43002
|
+
} else {
|
|
43003
|
+
this.dispatch(data, compress, options, cb);
|
|
43004
|
+
}
|
|
43005
|
+
}).catch((err) => {
|
|
43006
|
+
process.nextTick(onError, this, err, cb);
|
|
43007
|
+
});
|
|
43008
|
+
}
|
|
43009
|
+
dispatch(data, compress, options, cb) {
|
|
43010
|
+
if (!compress) {
|
|
43011
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
43012
|
+
return;
|
|
43013
|
+
}
|
|
43014
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
43015
|
+
this._bufferedBytes += options[kByteLength];
|
|
43016
|
+
this._state = DEFLATING;
|
|
43017
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
|
43018
|
+
if (this._socket.destroyed) {
|
|
43019
|
+
const err = new Error("The socket was closed while data was being compressed");
|
|
43020
|
+
callCallbacks(this, err, cb);
|
|
43021
|
+
return;
|
|
43022
|
+
}
|
|
43023
|
+
this._bufferedBytes -= options[kByteLength];
|
|
43024
|
+
this._state = DEFAULT;
|
|
43025
|
+
options.readOnly = false;
|
|
43026
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
43027
|
+
this.dequeue();
|
|
43028
|
+
});
|
|
43029
|
+
}
|
|
43030
|
+
dequeue() {
|
|
43031
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
43032
|
+
const params = this._queue.shift();
|
|
43033
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
43034
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
43035
|
+
}
|
|
43036
|
+
}
|
|
43037
|
+
enqueue(params) {
|
|
43038
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
43039
|
+
this._queue.push(params);
|
|
43040
|
+
}
|
|
43041
|
+
sendFrame(list, cb) {
|
|
43042
|
+
if (list.length === 2) {
|
|
43043
|
+
this._socket.cork();
|
|
43044
|
+
this._socket.write(list[0]);
|
|
43045
|
+
this._socket.write(list[1], cb);
|
|
43046
|
+
this._socket.uncork();
|
|
43047
|
+
} else {
|
|
43048
|
+
this._socket.write(list[0], cb);
|
|
43049
|
+
}
|
|
43050
|
+
}
|
|
43051
|
+
}
|
|
43052
|
+
module.exports = Sender;
|
|
43053
|
+
function callCallbacks(sender, err, cb) {
|
|
43054
|
+
if (typeof cb === "function")
|
|
43055
|
+
cb(err);
|
|
43056
|
+
for (let i = 0;i < sender._queue.length; i++) {
|
|
43057
|
+
const params = sender._queue[i];
|
|
43058
|
+
const callback = params[params.length - 1];
|
|
43059
|
+
if (typeof callback === "function")
|
|
43060
|
+
callback(err);
|
|
43061
|
+
}
|
|
43062
|
+
}
|
|
43063
|
+
function onError(sender, err, cb) {
|
|
43064
|
+
callCallbacks(sender, err, cb);
|
|
43065
|
+
sender.onerror(err);
|
|
43066
|
+
}
|
|
43067
|
+
});
|
|
43068
|
+
|
|
43069
|
+
// node_modules/ws/lib/event-target.js
|
|
43070
|
+
var require_event_target = __commonJS((exports, module) => {
|
|
43071
|
+
var { kForOnEventAttribute, kListener } = require_constants();
|
|
43072
|
+
var kCode = Symbol("kCode");
|
|
43073
|
+
var kData = Symbol("kData");
|
|
43074
|
+
var kError = Symbol("kError");
|
|
43075
|
+
var kMessage = Symbol("kMessage");
|
|
43076
|
+
var kReason = Symbol("kReason");
|
|
43077
|
+
var kTarget = Symbol("kTarget");
|
|
43078
|
+
var kType = Symbol("kType");
|
|
43079
|
+
var kWasClean = Symbol("kWasClean");
|
|
43080
|
+
|
|
43081
|
+
class Event {
|
|
43082
|
+
constructor(type) {
|
|
43083
|
+
this[kTarget] = null;
|
|
43084
|
+
this[kType] = type;
|
|
43085
|
+
}
|
|
43086
|
+
get target() {
|
|
43087
|
+
return this[kTarget];
|
|
43088
|
+
}
|
|
43089
|
+
get type() {
|
|
43090
|
+
return this[kType];
|
|
43091
|
+
}
|
|
43092
|
+
}
|
|
43093
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
43094
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
43095
|
+
|
|
43096
|
+
class CloseEvent extends Event {
|
|
43097
|
+
constructor(type, options = {}) {
|
|
43098
|
+
super(type);
|
|
43099
|
+
this[kCode] = options.code === undefined ? 0 : options.code;
|
|
43100
|
+
this[kReason] = options.reason === undefined ? "" : options.reason;
|
|
43101
|
+
this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
|
|
43102
|
+
}
|
|
43103
|
+
get code() {
|
|
43104
|
+
return this[kCode];
|
|
43105
|
+
}
|
|
43106
|
+
get reason() {
|
|
43107
|
+
return this[kReason];
|
|
43108
|
+
}
|
|
43109
|
+
get wasClean() {
|
|
43110
|
+
return this[kWasClean];
|
|
43111
|
+
}
|
|
43112
|
+
}
|
|
43113
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
43114
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
43115
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
43116
|
+
|
|
43117
|
+
class ErrorEvent extends Event {
|
|
43118
|
+
constructor(type, options = {}) {
|
|
43119
|
+
super(type);
|
|
43120
|
+
this[kError] = options.error === undefined ? null : options.error;
|
|
43121
|
+
this[kMessage] = options.message === undefined ? "" : options.message;
|
|
43122
|
+
}
|
|
43123
|
+
get error() {
|
|
43124
|
+
return this[kError];
|
|
43125
|
+
}
|
|
43126
|
+
get message() {
|
|
43127
|
+
return this[kMessage];
|
|
43128
|
+
}
|
|
43129
|
+
}
|
|
43130
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
43131
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
43132
|
+
|
|
43133
|
+
class MessageEvent extends Event {
|
|
43134
|
+
constructor(type, options = {}) {
|
|
43135
|
+
super(type);
|
|
43136
|
+
this[kData] = options.data === undefined ? null : options.data;
|
|
43137
|
+
}
|
|
43138
|
+
get data() {
|
|
43139
|
+
return this[kData];
|
|
43140
|
+
}
|
|
43141
|
+
}
|
|
43142
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
43143
|
+
var EventTarget = {
|
|
43144
|
+
addEventListener(type, handler, options = {}) {
|
|
43145
|
+
for (const listener of this.listeners(type)) {
|
|
43146
|
+
if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
43147
|
+
return;
|
|
43148
|
+
}
|
|
43149
|
+
}
|
|
43150
|
+
let wrapper;
|
|
43151
|
+
if (type === "message") {
|
|
43152
|
+
wrapper = function onMessage(data, isBinary) {
|
|
43153
|
+
const event = new MessageEvent("message", {
|
|
43154
|
+
data: isBinary ? data : data.toString()
|
|
43155
|
+
});
|
|
43156
|
+
event[kTarget] = this;
|
|
43157
|
+
callListener(handler, this, event);
|
|
43158
|
+
};
|
|
43159
|
+
} else if (type === "close") {
|
|
43160
|
+
wrapper = function onClose(code, message) {
|
|
43161
|
+
const event = new CloseEvent("close", {
|
|
43162
|
+
code,
|
|
43163
|
+
reason: message.toString(),
|
|
43164
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
43165
|
+
});
|
|
43166
|
+
event[kTarget] = this;
|
|
43167
|
+
callListener(handler, this, event);
|
|
43168
|
+
};
|
|
43169
|
+
} else if (type === "error") {
|
|
43170
|
+
wrapper = function onError(error) {
|
|
43171
|
+
const event = new ErrorEvent("error", {
|
|
43172
|
+
error,
|
|
43173
|
+
message: error.message
|
|
43174
|
+
});
|
|
43175
|
+
event[kTarget] = this;
|
|
43176
|
+
callListener(handler, this, event);
|
|
43177
|
+
};
|
|
43178
|
+
} else if (type === "open") {
|
|
43179
|
+
wrapper = function onOpen() {
|
|
43180
|
+
const event = new Event("open");
|
|
43181
|
+
event[kTarget] = this;
|
|
43182
|
+
callListener(handler, this, event);
|
|
43183
|
+
};
|
|
43184
|
+
} else {
|
|
43185
|
+
return;
|
|
43186
|
+
}
|
|
43187
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
43188
|
+
wrapper[kListener] = handler;
|
|
43189
|
+
if (options.once) {
|
|
43190
|
+
this.once(type, wrapper);
|
|
43191
|
+
} else {
|
|
43192
|
+
this.on(type, wrapper);
|
|
43193
|
+
}
|
|
43194
|
+
},
|
|
43195
|
+
removeEventListener(type, handler) {
|
|
43196
|
+
for (const listener of this.listeners(type)) {
|
|
43197
|
+
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
43198
|
+
this.removeListener(type, listener);
|
|
43199
|
+
break;
|
|
43200
|
+
}
|
|
43201
|
+
}
|
|
43202
|
+
}
|
|
43203
|
+
};
|
|
43204
|
+
module.exports = {
|
|
43205
|
+
CloseEvent,
|
|
43206
|
+
ErrorEvent,
|
|
43207
|
+
Event,
|
|
43208
|
+
EventTarget,
|
|
43209
|
+
MessageEvent
|
|
43210
|
+
};
|
|
43211
|
+
function callListener(listener, thisArg, event) {
|
|
43212
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
43213
|
+
listener.handleEvent.call(listener, event);
|
|
43214
|
+
} else {
|
|
43215
|
+
listener.call(thisArg, event);
|
|
43216
|
+
}
|
|
43217
|
+
}
|
|
43218
|
+
});
|
|
43219
|
+
|
|
43220
|
+
// node_modules/ws/lib/extension.js
|
|
43221
|
+
var require_extension = __commonJS((exports, module) => {
|
|
43222
|
+
var { tokenChars } = require_validation2();
|
|
43223
|
+
function push(dest, name, elem) {
|
|
43224
|
+
if (dest[name] === undefined)
|
|
43225
|
+
dest[name] = [elem];
|
|
43226
|
+
else
|
|
43227
|
+
dest[name].push(elem);
|
|
43228
|
+
}
|
|
43229
|
+
function parse(header) {
|
|
43230
|
+
const offers = Object.create(null);
|
|
43231
|
+
let params = Object.create(null);
|
|
43232
|
+
let mustUnescape = false;
|
|
43233
|
+
let isEscaping = false;
|
|
43234
|
+
let inQuotes = false;
|
|
43235
|
+
let extensionName;
|
|
43236
|
+
let paramName;
|
|
43237
|
+
let start = -1;
|
|
43238
|
+
let code = -1;
|
|
43239
|
+
let end = -1;
|
|
43240
|
+
let i = 0;
|
|
43241
|
+
for (;i < header.length; i++) {
|
|
43242
|
+
code = header.charCodeAt(i);
|
|
43243
|
+
if (extensionName === undefined) {
|
|
43244
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
43245
|
+
if (start === -1)
|
|
43246
|
+
start = i;
|
|
43247
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
43248
|
+
if (end === -1 && start !== -1)
|
|
43249
|
+
end = i;
|
|
43250
|
+
} else if (code === 59 || code === 44) {
|
|
43251
|
+
if (start === -1) {
|
|
43252
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
43253
|
+
}
|
|
43254
|
+
if (end === -1)
|
|
43255
|
+
end = i;
|
|
43256
|
+
const name = header.slice(start, end);
|
|
43257
|
+
if (code === 44) {
|
|
43258
|
+
push(offers, name, params);
|
|
43259
|
+
params = Object.create(null);
|
|
43260
|
+
} else {
|
|
43261
|
+
extensionName = name;
|
|
43262
|
+
}
|
|
43263
|
+
start = end = -1;
|
|
43264
|
+
} else {
|
|
43265
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
43266
|
+
}
|
|
43267
|
+
} else if (paramName === undefined) {
|
|
43268
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
43269
|
+
if (start === -1)
|
|
43270
|
+
start = i;
|
|
43271
|
+
} else if (code === 32 || code === 9) {
|
|
43272
|
+
if (end === -1 && start !== -1)
|
|
43273
|
+
end = i;
|
|
43274
|
+
} else if (code === 59 || code === 44) {
|
|
43275
|
+
if (start === -1) {
|
|
43276
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
43277
|
+
}
|
|
43278
|
+
if (end === -1)
|
|
43279
|
+
end = i;
|
|
43280
|
+
push(params, header.slice(start, end), true);
|
|
43281
|
+
if (code === 44) {
|
|
43282
|
+
push(offers, extensionName, params);
|
|
43283
|
+
params = Object.create(null);
|
|
43284
|
+
extensionName = undefined;
|
|
43285
|
+
}
|
|
43286
|
+
start = end = -1;
|
|
43287
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
43288
|
+
paramName = header.slice(start, i);
|
|
43289
|
+
start = end = -1;
|
|
43290
|
+
} else {
|
|
43291
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
43292
|
+
}
|
|
43293
|
+
} else {
|
|
43294
|
+
if (isEscaping) {
|
|
43295
|
+
if (tokenChars[code] !== 1) {
|
|
43296
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
43297
|
+
}
|
|
43298
|
+
if (start === -1)
|
|
43299
|
+
start = i;
|
|
43300
|
+
else if (!mustUnescape)
|
|
43301
|
+
mustUnescape = true;
|
|
43302
|
+
isEscaping = false;
|
|
43303
|
+
} else if (inQuotes) {
|
|
43304
|
+
if (tokenChars[code] === 1) {
|
|
43305
|
+
if (start === -1)
|
|
43306
|
+
start = i;
|
|
43307
|
+
} else if (code === 34 && start !== -1) {
|
|
43308
|
+
inQuotes = false;
|
|
43309
|
+
end = i;
|
|
43310
|
+
} else if (code === 92) {
|
|
43311
|
+
isEscaping = true;
|
|
43312
|
+
} else {
|
|
43313
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
43314
|
+
}
|
|
43315
|
+
} else if (code === 34 && header.charCodeAt(i - 1) === 61) {
|
|
43316
|
+
inQuotes = true;
|
|
43317
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
43318
|
+
if (start === -1)
|
|
43319
|
+
start = i;
|
|
43320
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
43321
|
+
if (end === -1)
|
|
43322
|
+
end = i;
|
|
43323
|
+
} else if (code === 59 || code === 44) {
|
|
43324
|
+
if (start === -1) {
|
|
43325
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
43326
|
+
}
|
|
43327
|
+
if (end === -1)
|
|
43328
|
+
end = i;
|
|
43329
|
+
let value = header.slice(start, end);
|
|
43330
|
+
if (mustUnescape) {
|
|
43331
|
+
value = value.replace(/\\/g, "");
|
|
43332
|
+
mustUnescape = false;
|
|
43333
|
+
}
|
|
43334
|
+
push(params, paramName, value);
|
|
43335
|
+
if (code === 44) {
|
|
43336
|
+
push(offers, extensionName, params);
|
|
43337
|
+
params = Object.create(null);
|
|
43338
|
+
extensionName = undefined;
|
|
43339
|
+
}
|
|
43340
|
+
paramName = undefined;
|
|
43341
|
+
start = end = -1;
|
|
43342
|
+
} else {
|
|
43343
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
43344
|
+
}
|
|
43345
|
+
}
|
|
43346
|
+
}
|
|
43347
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) {
|
|
43348
|
+
throw new SyntaxError("Unexpected end of input");
|
|
43349
|
+
}
|
|
43350
|
+
if (end === -1)
|
|
43351
|
+
end = i;
|
|
43352
|
+
const token = header.slice(start, end);
|
|
43353
|
+
if (extensionName === undefined) {
|
|
43354
|
+
push(offers, token, params);
|
|
43355
|
+
} else {
|
|
43356
|
+
if (paramName === undefined) {
|
|
43357
|
+
push(params, token, true);
|
|
43358
|
+
} else if (mustUnescape) {
|
|
43359
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
43360
|
+
} else {
|
|
43361
|
+
push(params, paramName, token);
|
|
43362
|
+
}
|
|
43363
|
+
push(offers, extensionName, params);
|
|
43364
|
+
}
|
|
43365
|
+
return offers;
|
|
43366
|
+
}
|
|
43367
|
+
function format(extensions) {
|
|
43368
|
+
return Object.keys(extensions).map((extension) => {
|
|
43369
|
+
let configurations = extensions[extension];
|
|
43370
|
+
if (!Array.isArray(configurations))
|
|
43371
|
+
configurations = [configurations];
|
|
43372
|
+
return configurations.map((params) => {
|
|
43373
|
+
return [extension].concat(Object.keys(params).map((k) => {
|
|
43374
|
+
let values = params[k];
|
|
43375
|
+
if (!Array.isArray(values))
|
|
43376
|
+
values = [values];
|
|
43377
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
43378
|
+
})).join("; ");
|
|
43379
|
+
}).join(", ");
|
|
43380
|
+
}).join(", ");
|
|
43381
|
+
}
|
|
43382
|
+
module.exports = { format, parse };
|
|
43383
|
+
});
|
|
43384
|
+
|
|
43385
|
+
// node_modules/ws/lib/websocket.js
|
|
43386
|
+
var require_websocket = __commonJS((exports, module) => {
|
|
43387
|
+
var EventEmitter = __require("events");
|
|
43388
|
+
var https = __require("https");
|
|
43389
|
+
var http = __require("http");
|
|
43390
|
+
var net = __require("net");
|
|
43391
|
+
var tls = __require("tls");
|
|
43392
|
+
var { randomBytes, createHash } = __require("crypto");
|
|
43393
|
+
var { Duplex, Readable } = __require("stream");
|
|
43394
|
+
var { URL: URL2 } = __require("url");
|
|
43395
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
43396
|
+
var Receiver = require_receiver();
|
|
43397
|
+
var Sender = require_sender();
|
|
43398
|
+
var { isBlob } = require_validation2();
|
|
43399
|
+
var {
|
|
43400
|
+
BINARY_TYPES,
|
|
43401
|
+
EMPTY_BUFFER,
|
|
43402
|
+
GUID,
|
|
43403
|
+
kForOnEventAttribute,
|
|
43404
|
+
kListener,
|
|
43405
|
+
kStatusCode,
|
|
43406
|
+
kWebSocket,
|
|
43407
|
+
NOOP
|
|
43408
|
+
} = require_constants();
|
|
43409
|
+
var {
|
|
43410
|
+
EventTarget: { addEventListener, removeEventListener }
|
|
43411
|
+
} = require_event_target();
|
|
43412
|
+
var { format, parse } = require_extension();
|
|
43413
|
+
var { toBuffer } = require_buffer_util();
|
|
43414
|
+
var closeTimeout = 30 * 1000;
|
|
43415
|
+
var kAborted = Symbol("kAborted");
|
|
43416
|
+
var protocolVersions = [8, 13];
|
|
43417
|
+
var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
43418
|
+
var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
43419
|
+
|
|
43420
|
+
class WebSocket extends EventEmitter {
|
|
43421
|
+
constructor(address, protocols, options) {
|
|
43422
|
+
super();
|
|
43423
|
+
this._binaryType = BINARY_TYPES[0];
|
|
43424
|
+
this._closeCode = 1006;
|
|
43425
|
+
this._closeFrameReceived = false;
|
|
43426
|
+
this._closeFrameSent = false;
|
|
43427
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
43428
|
+
this._closeTimer = null;
|
|
43429
|
+
this._errorEmitted = false;
|
|
43430
|
+
this._extensions = {};
|
|
43431
|
+
this._paused = false;
|
|
43432
|
+
this._protocol = "";
|
|
43433
|
+
this._readyState = WebSocket.CONNECTING;
|
|
43434
|
+
this._receiver = null;
|
|
43435
|
+
this._sender = null;
|
|
43436
|
+
this._socket = null;
|
|
43437
|
+
if (address !== null) {
|
|
43438
|
+
this._bufferedAmount = 0;
|
|
43439
|
+
this._isServer = false;
|
|
43440
|
+
this._redirects = 0;
|
|
43441
|
+
if (protocols === undefined) {
|
|
43442
|
+
protocols = [];
|
|
43443
|
+
} else if (!Array.isArray(protocols)) {
|
|
43444
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
43445
|
+
options = protocols;
|
|
43446
|
+
protocols = [];
|
|
43447
|
+
} else {
|
|
43448
|
+
protocols = [protocols];
|
|
43449
|
+
}
|
|
43450
|
+
}
|
|
43451
|
+
initAsClient(this, address, protocols, options);
|
|
43452
|
+
} else {
|
|
43453
|
+
this._autoPong = options.autoPong;
|
|
43454
|
+
this._isServer = true;
|
|
43455
|
+
}
|
|
43456
|
+
}
|
|
43457
|
+
get binaryType() {
|
|
43458
|
+
return this._binaryType;
|
|
43459
|
+
}
|
|
43460
|
+
set binaryType(type) {
|
|
43461
|
+
if (!BINARY_TYPES.includes(type))
|
|
43462
|
+
return;
|
|
43463
|
+
this._binaryType = type;
|
|
43464
|
+
if (this._receiver)
|
|
43465
|
+
this._receiver._binaryType = type;
|
|
43466
|
+
}
|
|
43467
|
+
get bufferedAmount() {
|
|
43468
|
+
if (!this._socket)
|
|
43469
|
+
return this._bufferedAmount;
|
|
43470
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
43471
|
+
}
|
|
43472
|
+
get extensions() {
|
|
43473
|
+
return Object.keys(this._extensions).join();
|
|
43474
|
+
}
|
|
43475
|
+
get isPaused() {
|
|
43476
|
+
return this._paused;
|
|
43477
|
+
}
|
|
43478
|
+
get onclose() {
|
|
43479
|
+
return null;
|
|
43480
|
+
}
|
|
43481
|
+
get onerror() {
|
|
43482
|
+
return null;
|
|
43483
|
+
}
|
|
43484
|
+
get onopen() {
|
|
43485
|
+
return null;
|
|
43486
|
+
}
|
|
43487
|
+
get onmessage() {
|
|
43488
|
+
return null;
|
|
43489
|
+
}
|
|
43490
|
+
get protocol() {
|
|
43491
|
+
return this._protocol;
|
|
43492
|
+
}
|
|
43493
|
+
get readyState() {
|
|
43494
|
+
return this._readyState;
|
|
43495
|
+
}
|
|
43496
|
+
get url() {
|
|
43497
|
+
return this._url;
|
|
43498
|
+
}
|
|
43499
|
+
setSocket(socket, head, options) {
|
|
43500
|
+
const receiver = new Receiver({
|
|
43501
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
43502
|
+
binaryType: this.binaryType,
|
|
43503
|
+
extensions: this._extensions,
|
|
43504
|
+
isServer: this._isServer,
|
|
43505
|
+
maxPayload: options.maxPayload,
|
|
43506
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
43507
|
+
});
|
|
43508
|
+
const sender = new Sender(socket, this._extensions, options.generateMask);
|
|
43509
|
+
this._receiver = receiver;
|
|
43510
|
+
this._sender = sender;
|
|
43511
|
+
this._socket = socket;
|
|
43512
|
+
receiver[kWebSocket] = this;
|
|
43513
|
+
sender[kWebSocket] = this;
|
|
43514
|
+
socket[kWebSocket] = this;
|
|
43515
|
+
receiver.on("conclude", receiverOnConclude);
|
|
43516
|
+
receiver.on("drain", receiverOnDrain);
|
|
43517
|
+
receiver.on("error", receiverOnError);
|
|
43518
|
+
receiver.on("message", receiverOnMessage);
|
|
43519
|
+
receiver.on("ping", receiverOnPing);
|
|
43520
|
+
receiver.on("pong", receiverOnPong);
|
|
43521
|
+
sender.onerror = senderOnError;
|
|
43522
|
+
if (socket.setTimeout)
|
|
43523
|
+
socket.setTimeout(0);
|
|
43524
|
+
if (socket.setNoDelay)
|
|
43525
|
+
socket.setNoDelay();
|
|
43526
|
+
if (head.length > 0)
|
|
43527
|
+
socket.unshift(head);
|
|
43528
|
+
socket.on("close", socketOnClose);
|
|
43529
|
+
socket.on("data", socketOnData);
|
|
43530
|
+
socket.on("end", socketOnEnd);
|
|
43531
|
+
socket.on("error", socketOnError);
|
|
43532
|
+
this._readyState = WebSocket.OPEN;
|
|
43533
|
+
this.emit("open");
|
|
43534
|
+
}
|
|
43535
|
+
emitClose() {
|
|
43536
|
+
if (!this._socket) {
|
|
43537
|
+
this._readyState = WebSocket.CLOSED;
|
|
43538
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
43539
|
+
return;
|
|
43540
|
+
}
|
|
43541
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
43542
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
43543
|
+
}
|
|
43544
|
+
this._receiver.removeAllListeners();
|
|
43545
|
+
this._readyState = WebSocket.CLOSED;
|
|
43546
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
43547
|
+
}
|
|
43548
|
+
close(code, data) {
|
|
43549
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
43550
|
+
return;
|
|
43551
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
43552
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
43553
|
+
abortHandshake(this, this._req, msg);
|
|
43554
|
+
return;
|
|
43555
|
+
}
|
|
43556
|
+
if (this.readyState === WebSocket.CLOSING) {
|
|
43557
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
43558
|
+
this._socket.end();
|
|
43559
|
+
}
|
|
43560
|
+
return;
|
|
43561
|
+
}
|
|
43562
|
+
this._readyState = WebSocket.CLOSING;
|
|
43563
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
43564
|
+
if (err)
|
|
43565
|
+
return;
|
|
43566
|
+
this._closeFrameSent = true;
|
|
43567
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
43568
|
+
this._socket.end();
|
|
43569
|
+
}
|
|
43570
|
+
});
|
|
43571
|
+
setCloseTimer(this);
|
|
43572
|
+
}
|
|
43573
|
+
pause() {
|
|
43574
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
43575
|
+
return;
|
|
43576
|
+
}
|
|
43577
|
+
this._paused = true;
|
|
43578
|
+
this._socket.pause();
|
|
43579
|
+
}
|
|
43580
|
+
ping(data, mask, cb) {
|
|
43581
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
43582
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
43583
|
+
}
|
|
43584
|
+
if (typeof data === "function") {
|
|
43585
|
+
cb = data;
|
|
43586
|
+
data = mask = undefined;
|
|
43587
|
+
} else if (typeof mask === "function") {
|
|
43588
|
+
cb = mask;
|
|
43589
|
+
mask = undefined;
|
|
43590
|
+
}
|
|
43591
|
+
if (typeof data === "number")
|
|
43592
|
+
data = data.toString();
|
|
43593
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
43594
|
+
sendAfterClose(this, data, cb);
|
|
43595
|
+
return;
|
|
43596
|
+
}
|
|
43597
|
+
if (mask === undefined)
|
|
43598
|
+
mask = !this._isServer;
|
|
43599
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
43600
|
+
}
|
|
43601
|
+
pong(data, mask, cb) {
|
|
43602
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
43603
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
43604
|
+
}
|
|
43605
|
+
if (typeof data === "function") {
|
|
43606
|
+
cb = data;
|
|
43607
|
+
data = mask = undefined;
|
|
43608
|
+
} else if (typeof mask === "function") {
|
|
43609
|
+
cb = mask;
|
|
43610
|
+
mask = undefined;
|
|
43611
|
+
}
|
|
43612
|
+
if (typeof data === "number")
|
|
43613
|
+
data = data.toString();
|
|
43614
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
43615
|
+
sendAfterClose(this, data, cb);
|
|
43616
|
+
return;
|
|
43617
|
+
}
|
|
43618
|
+
if (mask === undefined)
|
|
43619
|
+
mask = !this._isServer;
|
|
43620
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
43621
|
+
}
|
|
43622
|
+
resume() {
|
|
43623
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
43624
|
+
return;
|
|
43625
|
+
}
|
|
43626
|
+
this._paused = false;
|
|
43627
|
+
if (!this._receiver._writableState.needDrain)
|
|
43628
|
+
this._socket.resume();
|
|
43629
|
+
}
|
|
43630
|
+
send(data, options, cb) {
|
|
43631
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
43632
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
43633
|
+
}
|
|
43634
|
+
if (typeof options === "function") {
|
|
43635
|
+
cb = options;
|
|
43636
|
+
options = {};
|
|
43637
|
+
}
|
|
43638
|
+
if (typeof data === "number")
|
|
43639
|
+
data = data.toString();
|
|
43640
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
43641
|
+
sendAfterClose(this, data, cb);
|
|
43642
|
+
return;
|
|
43643
|
+
}
|
|
43644
|
+
const opts = {
|
|
43645
|
+
binary: typeof data !== "string",
|
|
43646
|
+
mask: !this._isServer,
|
|
43647
|
+
compress: true,
|
|
43648
|
+
fin: true,
|
|
43649
|
+
...options
|
|
43650
|
+
};
|
|
43651
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
43652
|
+
opts.compress = false;
|
|
43653
|
+
}
|
|
43654
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
43655
|
+
}
|
|
43656
|
+
terminate() {
|
|
43657
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
43658
|
+
return;
|
|
43659
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
43660
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
43661
|
+
abortHandshake(this, this._req, msg);
|
|
43662
|
+
return;
|
|
43663
|
+
}
|
|
43664
|
+
if (this._socket) {
|
|
43665
|
+
this._readyState = WebSocket.CLOSING;
|
|
43666
|
+
this._socket.destroy();
|
|
43667
|
+
}
|
|
43668
|
+
}
|
|
43669
|
+
}
|
|
43670
|
+
Object.defineProperty(WebSocket, "CONNECTING", {
|
|
43671
|
+
enumerable: true,
|
|
43672
|
+
value: readyStates.indexOf("CONNECTING")
|
|
43673
|
+
});
|
|
43674
|
+
Object.defineProperty(WebSocket.prototype, "CONNECTING", {
|
|
43675
|
+
enumerable: true,
|
|
43676
|
+
value: readyStates.indexOf("CONNECTING")
|
|
43677
|
+
});
|
|
43678
|
+
Object.defineProperty(WebSocket, "OPEN", {
|
|
43679
|
+
enumerable: true,
|
|
43680
|
+
value: readyStates.indexOf("OPEN")
|
|
43681
|
+
});
|
|
43682
|
+
Object.defineProperty(WebSocket.prototype, "OPEN", {
|
|
43683
|
+
enumerable: true,
|
|
43684
|
+
value: readyStates.indexOf("OPEN")
|
|
43685
|
+
});
|
|
43686
|
+
Object.defineProperty(WebSocket, "CLOSING", {
|
|
43687
|
+
enumerable: true,
|
|
43688
|
+
value: readyStates.indexOf("CLOSING")
|
|
43689
|
+
});
|
|
43690
|
+
Object.defineProperty(WebSocket.prototype, "CLOSING", {
|
|
43691
|
+
enumerable: true,
|
|
43692
|
+
value: readyStates.indexOf("CLOSING")
|
|
43693
|
+
});
|
|
43694
|
+
Object.defineProperty(WebSocket, "CLOSED", {
|
|
43695
|
+
enumerable: true,
|
|
43696
|
+
value: readyStates.indexOf("CLOSED")
|
|
43697
|
+
});
|
|
43698
|
+
Object.defineProperty(WebSocket.prototype, "CLOSED", {
|
|
43699
|
+
enumerable: true,
|
|
43700
|
+
value: readyStates.indexOf("CLOSED")
|
|
43701
|
+
});
|
|
43702
|
+
[
|
|
43703
|
+
"binaryType",
|
|
43704
|
+
"bufferedAmount",
|
|
43705
|
+
"extensions",
|
|
43706
|
+
"isPaused",
|
|
43707
|
+
"protocol",
|
|
43708
|
+
"readyState",
|
|
43709
|
+
"url"
|
|
43710
|
+
].forEach((property) => {
|
|
43711
|
+
Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
|
|
43712
|
+
});
|
|
43713
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
43714
|
+
Object.defineProperty(WebSocket.prototype, `on${method}`, {
|
|
43715
|
+
enumerable: true,
|
|
43716
|
+
get() {
|
|
43717
|
+
for (const listener of this.listeners(method)) {
|
|
43718
|
+
if (listener[kForOnEventAttribute])
|
|
43719
|
+
return listener[kListener];
|
|
43720
|
+
}
|
|
43721
|
+
return null;
|
|
43722
|
+
},
|
|
43723
|
+
set(handler) {
|
|
43724
|
+
for (const listener of this.listeners(method)) {
|
|
43725
|
+
if (listener[kForOnEventAttribute]) {
|
|
43726
|
+
this.removeListener(method, listener);
|
|
43727
|
+
break;
|
|
43728
|
+
}
|
|
43729
|
+
}
|
|
43730
|
+
if (typeof handler !== "function")
|
|
43731
|
+
return;
|
|
43732
|
+
this.addEventListener(method, handler, {
|
|
43733
|
+
[kForOnEventAttribute]: true
|
|
43734
|
+
});
|
|
43735
|
+
}
|
|
43736
|
+
});
|
|
43737
|
+
});
|
|
43738
|
+
WebSocket.prototype.addEventListener = addEventListener;
|
|
43739
|
+
WebSocket.prototype.removeEventListener = removeEventListener;
|
|
43740
|
+
module.exports = WebSocket;
|
|
43741
|
+
function initAsClient(websocket, address, protocols, options) {
|
|
43742
|
+
const opts = {
|
|
43743
|
+
allowSynchronousEvents: true,
|
|
43744
|
+
autoPong: true,
|
|
43745
|
+
protocolVersion: protocolVersions[1],
|
|
43746
|
+
maxPayload: 100 * 1024 * 1024,
|
|
43747
|
+
skipUTF8Validation: false,
|
|
43748
|
+
perMessageDeflate: true,
|
|
43749
|
+
followRedirects: false,
|
|
43750
|
+
maxRedirects: 10,
|
|
43751
|
+
...options,
|
|
43752
|
+
socketPath: undefined,
|
|
43753
|
+
hostname: undefined,
|
|
43754
|
+
protocol: undefined,
|
|
43755
|
+
timeout: undefined,
|
|
43756
|
+
method: "GET",
|
|
43757
|
+
host: undefined,
|
|
43758
|
+
path: undefined,
|
|
43759
|
+
port: undefined
|
|
43760
|
+
};
|
|
43761
|
+
websocket._autoPong = opts.autoPong;
|
|
43762
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
43763
|
+
throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} ` + `(supported versions: ${protocolVersions.join(", ")})`);
|
|
43764
|
+
}
|
|
43765
|
+
let parsedUrl;
|
|
43766
|
+
if (address instanceof URL2) {
|
|
43767
|
+
parsedUrl = address;
|
|
43768
|
+
} else {
|
|
43769
|
+
try {
|
|
43770
|
+
parsedUrl = new URL2(address);
|
|
43771
|
+
} catch (e) {
|
|
43772
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
43773
|
+
}
|
|
43774
|
+
}
|
|
43775
|
+
if (parsedUrl.protocol === "http:") {
|
|
43776
|
+
parsedUrl.protocol = "ws:";
|
|
43777
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
43778
|
+
parsedUrl.protocol = "wss:";
|
|
43779
|
+
}
|
|
43780
|
+
websocket._url = parsedUrl.href;
|
|
43781
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
43782
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
43783
|
+
let invalidUrlMessage;
|
|
43784
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
43785
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", ` + '"http:", "https:", or "ws+unix:"';
|
|
43786
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
43787
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
43788
|
+
} else if (parsedUrl.hash) {
|
|
43789
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
43790
|
+
}
|
|
43791
|
+
if (invalidUrlMessage) {
|
|
43792
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
43793
|
+
if (websocket._redirects === 0) {
|
|
43794
|
+
throw err;
|
|
43795
|
+
} else {
|
|
43796
|
+
emitErrorAndClose(websocket, err);
|
|
43797
|
+
return;
|
|
43798
|
+
}
|
|
43799
|
+
}
|
|
43800
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
43801
|
+
const key = randomBytes(16).toString("base64");
|
|
43802
|
+
const request = isSecure ? https.request : http.request;
|
|
43803
|
+
const protocolSet = new Set;
|
|
43804
|
+
let perMessageDeflate;
|
|
43805
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
43806
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
43807
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
43808
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
43809
|
+
opts.headers = {
|
|
43810
|
+
...opts.headers,
|
|
43811
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
43812
|
+
"Sec-WebSocket-Key": key,
|
|
43813
|
+
Connection: "Upgrade",
|
|
43814
|
+
Upgrade: "websocket"
|
|
43815
|
+
};
|
|
43816
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
43817
|
+
opts.timeout = opts.handshakeTimeout;
|
|
43818
|
+
if (opts.perMessageDeflate) {
|
|
43819
|
+
perMessageDeflate = new PerMessageDeflate(opts.perMessageDeflate !== true ? opts.perMessageDeflate : {}, false, opts.maxPayload);
|
|
43820
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({
|
|
43821
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
43822
|
+
});
|
|
43823
|
+
}
|
|
43824
|
+
if (protocols.length) {
|
|
43825
|
+
for (const protocol of protocols) {
|
|
43826
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
43827
|
+
throw new SyntaxError("An invalid or duplicated subprotocol was specified");
|
|
43828
|
+
}
|
|
43829
|
+
protocolSet.add(protocol);
|
|
43830
|
+
}
|
|
43831
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
43832
|
+
}
|
|
43833
|
+
if (opts.origin) {
|
|
43834
|
+
if (opts.protocolVersion < 13) {
|
|
43835
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
43836
|
+
} else {
|
|
43837
|
+
opts.headers.Origin = opts.origin;
|
|
43838
|
+
}
|
|
43839
|
+
}
|
|
43840
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
43841
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
43842
|
+
}
|
|
43843
|
+
if (isIpcUrl) {
|
|
43844
|
+
const parts = opts.path.split(":");
|
|
43845
|
+
opts.socketPath = parts[0];
|
|
43846
|
+
opts.path = parts[1];
|
|
43847
|
+
}
|
|
43848
|
+
let req;
|
|
43849
|
+
if (opts.followRedirects) {
|
|
43850
|
+
if (websocket._redirects === 0) {
|
|
43851
|
+
websocket._originalIpc = isIpcUrl;
|
|
43852
|
+
websocket._originalSecure = isSecure;
|
|
43853
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
43854
|
+
const headers = options && options.headers;
|
|
43855
|
+
options = { ...options, headers: {} };
|
|
43856
|
+
if (headers) {
|
|
43857
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
43858
|
+
options.headers[key2.toLowerCase()] = value;
|
|
43859
|
+
}
|
|
43860
|
+
}
|
|
43861
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
43862
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
43863
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
43864
|
+
delete opts.headers.authorization;
|
|
43865
|
+
delete opts.headers.cookie;
|
|
43866
|
+
if (!isSameHost)
|
|
43867
|
+
delete opts.headers.host;
|
|
43868
|
+
opts.auth = undefined;
|
|
43869
|
+
}
|
|
43870
|
+
}
|
|
43871
|
+
if (opts.auth && !options.headers.authorization) {
|
|
43872
|
+
options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
43873
|
+
}
|
|
43874
|
+
req = websocket._req = request(opts);
|
|
43875
|
+
if (websocket._redirects) {
|
|
43876
|
+
websocket.emit("redirect", websocket.url, req);
|
|
43877
|
+
}
|
|
43878
|
+
} else {
|
|
43879
|
+
req = websocket._req = request(opts);
|
|
43880
|
+
}
|
|
43881
|
+
if (opts.timeout) {
|
|
43882
|
+
req.on("timeout", () => {
|
|
43883
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
43884
|
+
});
|
|
43885
|
+
}
|
|
43886
|
+
req.on("error", (err) => {
|
|
43887
|
+
if (req === null || req[kAborted])
|
|
43888
|
+
return;
|
|
43889
|
+
req = websocket._req = null;
|
|
43890
|
+
emitErrorAndClose(websocket, err);
|
|
43891
|
+
});
|
|
43892
|
+
req.on("response", (res) => {
|
|
43893
|
+
const location = res.headers.location;
|
|
43894
|
+
const statusCode = res.statusCode;
|
|
43895
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
43896
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
43897
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
43898
|
+
return;
|
|
43899
|
+
}
|
|
43900
|
+
req.abort();
|
|
43901
|
+
let addr;
|
|
43902
|
+
try {
|
|
43903
|
+
addr = new URL2(location, address);
|
|
43904
|
+
} catch (e) {
|
|
43905
|
+
const err = new SyntaxError(`Invalid URL: ${location}`);
|
|
43906
|
+
emitErrorAndClose(websocket, err);
|
|
43907
|
+
return;
|
|
43908
|
+
}
|
|
43909
|
+
initAsClient(websocket, addr, protocols, options);
|
|
43910
|
+
} else if (!websocket.emit("unexpected-response", req, res)) {
|
|
43911
|
+
abortHandshake(websocket, req, `Unexpected server response: ${res.statusCode}`);
|
|
43912
|
+
}
|
|
43913
|
+
});
|
|
43914
|
+
req.on("upgrade", (res, socket, head) => {
|
|
43915
|
+
websocket.emit("upgrade", res);
|
|
43916
|
+
if (websocket.readyState !== WebSocket.CONNECTING)
|
|
43917
|
+
return;
|
|
43918
|
+
req = websocket._req = null;
|
|
43919
|
+
const upgrade = res.headers.upgrade;
|
|
43920
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
43921
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
43922
|
+
return;
|
|
43923
|
+
}
|
|
43924
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
43925
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
43926
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
43927
|
+
return;
|
|
43928
|
+
}
|
|
43929
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
43930
|
+
let protError;
|
|
43931
|
+
if (serverProt !== undefined) {
|
|
43932
|
+
if (!protocolSet.size) {
|
|
43933
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
43934
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
43935
|
+
protError = "Server sent an invalid subprotocol";
|
|
43936
|
+
}
|
|
43937
|
+
} else if (protocolSet.size) {
|
|
43938
|
+
protError = "Server sent no subprotocol";
|
|
43939
|
+
}
|
|
43940
|
+
if (protError) {
|
|
43941
|
+
abortHandshake(websocket, socket, protError);
|
|
43942
|
+
return;
|
|
43943
|
+
}
|
|
43944
|
+
if (serverProt)
|
|
43945
|
+
websocket._protocol = serverProt;
|
|
43946
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
43947
|
+
if (secWebSocketExtensions !== undefined) {
|
|
43948
|
+
if (!perMessageDeflate) {
|
|
43949
|
+
const message = "Server sent a Sec-WebSocket-Extensions header but no extension " + "was requested";
|
|
43950
|
+
abortHandshake(websocket, socket, message);
|
|
43951
|
+
return;
|
|
43952
|
+
}
|
|
43953
|
+
let extensions;
|
|
43954
|
+
try {
|
|
43955
|
+
extensions = parse(secWebSocketExtensions);
|
|
43956
|
+
} catch (err) {
|
|
43957
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
43958
|
+
abortHandshake(websocket, socket, message);
|
|
43959
|
+
return;
|
|
43960
|
+
}
|
|
43961
|
+
const extensionNames = Object.keys(extensions);
|
|
43962
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
43963
|
+
const message = "Server indicated an extension that was not requested";
|
|
43964
|
+
abortHandshake(websocket, socket, message);
|
|
43965
|
+
return;
|
|
43966
|
+
}
|
|
43967
|
+
try {
|
|
43968
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
43969
|
+
} catch (err) {
|
|
43970
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
43971
|
+
abortHandshake(websocket, socket, message);
|
|
43972
|
+
return;
|
|
43973
|
+
}
|
|
43974
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
43975
|
+
}
|
|
43976
|
+
websocket.setSocket(socket, head, {
|
|
43977
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
43978
|
+
generateMask: opts.generateMask,
|
|
43979
|
+
maxPayload: opts.maxPayload,
|
|
43980
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
43981
|
+
});
|
|
43982
|
+
});
|
|
43983
|
+
if (opts.finishRequest) {
|
|
43984
|
+
opts.finishRequest(req, websocket);
|
|
43985
|
+
} else {
|
|
43986
|
+
req.end();
|
|
43987
|
+
}
|
|
43988
|
+
}
|
|
43989
|
+
function emitErrorAndClose(websocket, err) {
|
|
43990
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
43991
|
+
websocket._errorEmitted = true;
|
|
43992
|
+
websocket.emit("error", err);
|
|
43993
|
+
websocket.emitClose();
|
|
43994
|
+
}
|
|
43995
|
+
function netConnect(options) {
|
|
43996
|
+
options.path = options.socketPath;
|
|
43997
|
+
return net.connect(options);
|
|
43998
|
+
}
|
|
43999
|
+
function tlsConnect(options) {
|
|
44000
|
+
options.path = undefined;
|
|
44001
|
+
if (!options.servername && options.servername !== "") {
|
|
44002
|
+
options.servername = net.isIP(options.host) ? "" : options.host;
|
|
44003
|
+
}
|
|
44004
|
+
return tls.connect(options);
|
|
44005
|
+
}
|
|
44006
|
+
function abortHandshake(websocket, stream, message) {
|
|
44007
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
44008
|
+
const err = new Error(message);
|
|
44009
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
44010
|
+
if (stream.setHeader) {
|
|
44011
|
+
stream[kAborted] = true;
|
|
44012
|
+
stream.abort();
|
|
44013
|
+
if (stream.socket && !stream.socket.destroyed) {
|
|
44014
|
+
stream.socket.destroy();
|
|
44015
|
+
}
|
|
44016
|
+
process.nextTick(emitErrorAndClose, websocket, err);
|
|
44017
|
+
} else {
|
|
44018
|
+
stream.destroy(err);
|
|
44019
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
44020
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
44021
|
+
}
|
|
44022
|
+
}
|
|
44023
|
+
function sendAfterClose(websocket, data, cb) {
|
|
44024
|
+
if (data) {
|
|
44025
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
44026
|
+
if (websocket._socket)
|
|
44027
|
+
websocket._sender._bufferedBytes += length;
|
|
44028
|
+
else
|
|
44029
|
+
websocket._bufferedAmount += length;
|
|
44030
|
+
}
|
|
44031
|
+
if (cb) {
|
|
44032
|
+
const err = new Error(`WebSocket is not open: readyState ${websocket.readyState} ` + `(${readyStates[websocket.readyState]})`);
|
|
44033
|
+
process.nextTick(cb, err);
|
|
44034
|
+
}
|
|
44035
|
+
}
|
|
44036
|
+
function receiverOnConclude(code, reason) {
|
|
44037
|
+
const websocket = this[kWebSocket];
|
|
44038
|
+
websocket._closeFrameReceived = true;
|
|
44039
|
+
websocket._closeMessage = reason;
|
|
44040
|
+
websocket._closeCode = code;
|
|
44041
|
+
if (websocket._socket[kWebSocket] === undefined)
|
|
44042
|
+
return;
|
|
44043
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
44044
|
+
process.nextTick(resume, websocket._socket);
|
|
44045
|
+
if (code === 1005)
|
|
44046
|
+
websocket.close();
|
|
44047
|
+
else
|
|
44048
|
+
websocket.close(code, reason);
|
|
44049
|
+
}
|
|
44050
|
+
function receiverOnDrain() {
|
|
44051
|
+
const websocket = this[kWebSocket];
|
|
44052
|
+
if (!websocket.isPaused)
|
|
44053
|
+
websocket._socket.resume();
|
|
44054
|
+
}
|
|
44055
|
+
function receiverOnError(err) {
|
|
44056
|
+
const websocket = this[kWebSocket];
|
|
44057
|
+
if (websocket._socket[kWebSocket] !== undefined) {
|
|
44058
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
44059
|
+
process.nextTick(resume, websocket._socket);
|
|
44060
|
+
websocket.close(err[kStatusCode]);
|
|
44061
|
+
}
|
|
44062
|
+
if (!websocket._errorEmitted) {
|
|
44063
|
+
websocket._errorEmitted = true;
|
|
44064
|
+
websocket.emit("error", err);
|
|
44065
|
+
}
|
|
44066
|
+
}
|
|
44067
|
+
function receiverOnFinish() {
|
|
44068
|
+
this[kWebSocket].emitClose();
|
|
44069
|
+
}
|
|
44070
|
+
function receiverOnMessage(data, isBinary) {
|
|
44071
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
44072
|
+
}
|
|
44073
|
+
function receiverOnPing(data) {
|
|
44074
|
+
const websocket = this[kWebSocket];
|
|
44075
|
+
if (websocket._autoPong)
|
|
44076
|
+
websocket.pong(data, !this._isServer, NOOP);
|
|
44077
|
+
websocket.emit("ping", data);
|
|
44078
|
+
}
|
|
44079
|
+
function receiverOnPong(data) {
|
|
44080
|
+
this[kWebSocket].emit("pong", data);
|
|
44081
|
+
}
|
|
44082
|
+
function resume(stream) {
|
|
44083
|
+
stream.resume();
|
|
44084
|
+
}
|
|
44085
|
+
function senderOnError(err) {
|
|
44086
|
+
const websocket = this[kWebSocket];
|
|
44087
|
+
if (websocket.readyState === WebSocket.CLOSED)
|
|
44088
|
+
return;
|
|
44089
|
+
if (websocket.readyState === WebSocket.OPEN) {
|
|
44090
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
44091
|
+
setCloseTimer(websocket);
|
|
44092
|
+
}
|
|
44093
|
+
this._socket.end();
|
|
44094
|
+
if (!websocket._errorEmitted) {
|
|
44095
|
+
websocket._errorEmitted = true;
|
|
44096
|
+
websocket.emit("error", err);
|
|
44097
|
+
}
|
|
44098
|
+
}
|
|
44099
|
+
function setCloseTimer(websocket) {
|
|
44100
|
+
websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), closeTimeout);
|
|
44101
|
+
}
|
|
44102
|
+
function socketOnClose() {
|
|
44103
|
+
const websocket = this[kWebSocket];
|
|
44104
|
+
this.removeListener("close", socketOnClose);
|
|
44105
|
+
this.removeListener("data", socketOnData);
|
|
44106
|
+
this.removeListener("end", socketOnEnd);
|
|
44107
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
44108
|
+
let chunk;
|
|
44109
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && (chunk = websocket._socket.read()) !== null) {
|
|
44110
|
+
websocket._receiver.write(chunk);
|
|
44111
|
+
}
|
|
44112
|
+
websocket._receiver.end();
|
|
44113
|
+
this[kWebSocket] = undefined;
|
|
44114
|
+
clearTimeout(websocket._closeTimer);
|
|
44115
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
|
|
44116
|
+
websocket.emitClose();
|
|
44117
|
+
} else {
|
|
44118
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
44119
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
44120
|
+
}
|
|
44121
|
+
}
|
|
44122
|
+
function socketOnData(chunk) {
|
|
44123
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
44124
|
+
this.pause();
|
|
44125
|
+
}
|
|
44126
|
+
}
|
|
44127
|
+
function socketOnEnd() {
|
|
44128
|
+
const websocket = this[kWebSocket];
|
|
44129
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
44130
|
+
websocket._receiver.end();
|
|
44131
|
+
this.end();
|
|
44132
|
+
}
|
|
44133
|
+
function socketOnError() {
|
|
44134
|
+
const websocket = this[kWebSocket];
|
|
44135
|
+
this.removeListener("error", socketOnError);
|
|
44136
|
+
this.on("error", NOOP);
|
|
44137
|
+
if (websocket) {
|
|
44138
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
44139
|
+
this.destroy();
|
|
44140
|
+
}
|
|
44141
|
+
}
|
|
44142
|
+
});
|
|
44143
|
+
|
|
44144
|
+
// node_modules/ws/lib/stream.js
|
|
44145
|
+
var require_stream = __commonJS((exports, module) => {
|
|
44146
|
+
var WebSocket = require_websocket();
|
|
44147
|
+
var { Duplex } = __require("stream");
|
|
44148
|
+
function emitClose(stream) {
|
|
44149
|
+
stream.emit("close");
|
|
44150
|
+
}
|
|
44151
|
+
function duplexOnEnd() {
|
|
44152
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
44153
|
+
this.destroy();
|
|
44154
|
+
}
|
|
44155
|
+
}
|
|
44156
|
+
function duplexOnError(err) {
|
|
44157
|
+
this.removeListener("error", duplexOnError);
|
|
44158
|
+
this.destroy();
|
|
44159
|
+
if (this.listenerCount("error") === 0) {
|
|
44160
|
+
this.emit("error", err);
|
|
44161
|
+
}
|
|
44162
|
+
}
|
|
44163
|
+
function createWebSocketStream(ws, options) {
|
|
44164
|
+
let terminateOnDestroy = true;
|
|
44165
|
+
const duplex = new Duplex({
|
|
44166
|
+
...options,
|
|
44167
|
+
autoDestroy: false,
|
|
44168
|
+
emitClose: false,
|
|
44169
|
+
objectMode: false,
|
|
44170
|
+
writableObjectMode: false
|
|
44171
|
+
});
|
|
44172
|
+
ws.on("message", function message(msg, isBinary) {
|
|
44173
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
44174
|
+
if (!duplex.push(data))
|
|
44175
|
+
ws.pause();
|
|
44176
|
+
});
|
|
44177
|
+
ws.once("error", function error(err) {
|
|
44178
|
+
if (duplex.destroyed)
|
|
44179
|
+
return;
|
|
44180
|
+
terminateOnDestroy = false;
|
|
44181
|
+
duplex.destroy(err);
|
|
44182
|
+
});
|
|
44183
|
+
ws.once("close", function close() {
|
|
44184
|
+
if (duplex.destroyed)
|
|
44185
|
+
return;
|
|
44186
|
+
duplex.push(null);
|
|
44187
|
+
});
|
|
44188
|
+
duplex._destroy = function(err, callback) {
|
|
44189
|
+
if (ws.readyState === ws.CLOSED) {
|
|
44190
|
+
callback(err);
|
|
44191
|
+
process.nextTick(emitClose, duplex);
|
|
44192
|
+
return;
|
|
44193
|
+
}
|
|
44194
|
+
let called = false;
|
|
44195
|
+
ws.once("error", function error(err2) {
|
|
44196
|
+
called = true;
|
|
44197
|
+
callback(err2);
|
|
44198
|
+
});
|
|
44199
|
+
ws.once("close", function close() {
|
|
44200
|
+
if (!called)
|
|
44201
|
+
callback(err);
|
|
44202
|
+
process.nextTick(emitClose, duplex);
|
|
44203
|
+
});
|
|
44204
|
+
if (terminateOnDestroy)
|
|
44205
|
+
ws.terminate();
|
|
44206
|
+
};
|
|
44207
|
+
duplex._final = function(callback) {
|
|
44208
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
44209
|
+
ws.once("open", function open() {
|
|
44210
|
+
duplex._final(callback);
|
|
44211
|
+
});
|
|
44212
|
+
return;
|
|
44213
|
+
}
|
|
44214
|
+
if (ws._socket === null)
|
|
44215
|
+
return;
|
|
44216
|
+
if (ws._socket._writableState.finished) {
|
|
44217
|
+
callback();
|
|
44218
|
+
if (duplex._readableState.endEmitted)
|
|
44219
|
+
duplex.destroy();
|
|
44220
|
+
} else {
|
|
44221
|
+
ws._socket.once("finish", function finish() {
|
|
44222
|
+
callback();
|
|
44223
|
+
});
|
|
44224
|
+
ws.close();
|
|
44225
|
+
}
|
|
44226
|
+
};
|
|
44227
|
+
duplex._read = function() {
|
|
44228
|
+
if (ws.isPaused)
|
|
44229
|
+
ws.resume();
|
|
44230
|
+
};
|
|
44231
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
44232
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
44233
|
+
ws.once("open", function open() {
|
|
44234
|
+
duplex._write(chunk, encoding, callback);
|
|
44235
|
+
});
|
|
44236
|
+
return;
|
|
44237
|
+
}
|
|
44238
|
+
ws.send(chunk, callback);
|
|
44239
|
+
};
|
|
44240
|
+
duplex.on("end", duplexOnEnd);
|
|
44241
|
+
duplex.on("error", duplexOnError);
|
|
44242
|
+
return duplex;
|
|
44243
|
+
}
|
|
44244
|
+
module.exports = createWebSocketStream;
|
|
44245
|
+
});
|
|
44246
|
+
|
|
44247
|
+
// node_modules/ws/lib/subprotocol.js
|
|
44248
|
+
var require_subprotocol = __commonJS((exports, module) => {
|
|
44249
|
+
var { tokenChars } = require_validation2();
|
|
44250
|
+
function parse(header) {
|
|
44251
|
+
const protocols = new Set;
|
|
44252
|
+
let start = -1;
|
|
44253
|
+
let end = -1;
|
|
44254
|
+
let i = 0;
|
|
44255
|
+
for (i;i < header.length; i++) {
|
|
44256
|
+
const code = header.charCodeAt(i);
|
|
44257
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
44258
|
+
if (start === -1)
|
|
44259
|
+
start = i;
|
|
44260
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
44261
|
+
if (end === -1 && start !== -1)
|
|
44262
|
+
end = i;
|
|
44263
|
+
} else if (code === 44) {
|
|
44264
|
+
if (start === -1) {
|
|
44265
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
44266
|
+
}
|
|
44267
|
+
if (end === -1)
|
|
44268
|
+
end = i;
|
|
44269
|
+
const protocol2 = header.slice(start, end);
|
|
44270
|
+
if (protocols.has(protocol2)) {
|
|
44271
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
44272
|
+
}
|
|
44273
|
+
protocols.add(protocol2);
|
|
44274
|
+
start = end = -1;
|
|
44275
|
+
} else {
|
|
44276
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
44277
|
+
}
|
|
44278
|
+
}
|
|
44279
|
+
if (start === -1 || end !== -1) {
|
|
44280
|
+
throw new SyntaxError("Unexpected end of input");
|
|
44281
|
+
}
|
|
44282
|
+
const protocol = header.slice(start, i);
|
|
44283
|
+
if (protocols.has(protocol)) {
|
|
44284
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
44285
|
+
}
|
|
44286
|
+
protocols.add(protocol);
|
|
44287
|
+
return protocols;
|
|
44288
|
+
}
|
|
44289
|
+
module.exports = { parse };
|
|
44290
|
+
});
|
|
44291
|
+
|
|
44292
|
+
// node_modules/ws/lib/websocket-server.js
|
|
44293
|
+
var require_websocket_server = __commonJS((exports, module) => {
|
|
44294
|
+
var EventEmitter = __require("events");
|
|
44295
|
+
var http = __require("http");
|
|
44296
|
+
var { Duplex } = __require("stream");
|
|
44297
|
+
var { createHash } = __require("crypto");
|
|
44298
|
+
var extension = require_extension();
|
|
44299
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
44300
|
+
var subprotocol = require_subprotocol();
|
|
44301
|
+
var WebSocket = require_websocket();
|
|
44302
|
+
var { GUID, kWebSocket } = require_constants();
|
|
44303
|
+
var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
44304
|
+
var RUNNING = 0;
|
|
44305
|
+
var CLOSING = 1;
|
|
44306
|
+
var CLOSED = 2;
|
|
44307
|
+
|
|
44308
|
+
class WebSocketServer extends EventEmitter {
|
|
44309
|
+
constructor(options, callback) {
|
|
44310
|
+
super();
|
|
44311
|
+
options = {
|
|
44312
|
+
allowSynchronousEvents: true,
|
|
44313
|
+
autoPong: true,
|
|
44314
|
+
maxPayload: 100 * 1024 * 1024,
|
|
44315
|
+
skipUTF8Validation: false,
|
|
44316
|
+
perMessageDeflate: false,
|
|
44317
|
+
handleProtocols: null,
|
|
44318
|
+
clientTracking: true,
|
|
44319
|
+
verifyClient: null,
|
|
44320
|
+
noServer: false,
|
|
44321
|
+
backlog: null,
|
|
44322
|
+
server: null,
|
|
44323
|
+
host: null,
|
|
44324
|
+
path: null,
|
|
44325
|
+
port: null,
|
|
44326
|
+
WebSocket,
|
|
44327
|
+
...options
|
|
44328
|
+
};
|
|
44329
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
|
|
44330
|
+
throw new TypeError('One and only one of the "port", "server", or "noServer" options ' + "must be specified");
|
|
44331
|
+
}
|
|
44332
|
+
if (options.port != null) {
|
|
44333
|
+
this._server = http.createServer((req, res) => {
|
|
44334
|
+
const body = http.STATUS_CODES[426];
|
|
44335
|
+
res.writeHead(426, {
|
|
44336
|
+
"Content-Length": body.length,
|
|
44337
|
+
"Content-Type": "text/plain"
|
|
44338
|
+
});
|
|
44339
|
+
res.end(body);
|
|
44340
|
+
});
|
|
44341
|
+
this._server.listen(options.port, options.host, options.backlog, callback);
|
|
44342
|
+
} else if (options.server) {
|
|
44343
|
+
this._server = options.server;
|
|
44344
|
+
}
|
|
44345
|
+
if (this._server) {
|
|
44346
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
44347
|
+
this._removeListeners = addListeners(this._server, {
|
|
44348
|
+
listening: this.emit.bind(this, "listening"),
|
|
44349
|
+
error: this.emit.bind(this, "error"),
|
|
44350
|
+
upgrade: (req, socket, head) => {
|
|
44351
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
44352
|
+
}
|
|
44353
|
+
});
|
|
44354
|
+
}
|
|
44355
|
+
if (options.perMessageDeflate === true)
|
|
44356
|
+
options.perMessageDeflate = {};
|
|
44357
|
+
if (options.clientTracking) {
|
|
44358
|
+
this.clients = new Set;
|
|
44359
|
+
this._shouldEmitClose = false;
|
|
44360
|
+
}
|
|
44361
|
+
this.options = options;
|
|
44362
|
+
this._state = RUNNING;
|
|
44363
|
+
}
|
|
44364
|
+
address() {
|
|
44365
|
+
if (this.options.noServer) {
|
|
44366
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
44367
|
+
}
|
|
44368
|
+
if (!this._server)
|
|
44369
|
+
return null;
|
|
44370
|
+
return this._server.address();
|
|
44371
|
+
}
|
|
44372
|
+
close(cb) {
|
|
44373
|
+
if (this._state === CLOSED) {
|
|
44374
|
+
if (cb) {
|
|
44375
|
+
this.once("close", () => {
|
|
44376
|
+
cb(new Error("The server is not running"));
|
|
44377
|
+
});
|
|
44378
|
+
}
|
|
44379
|
+
process.nextTick(emitClose, this);
|
|
44380
|
+
return;
|
|
44381
|
+
}
|
|
44382
|
+
if (cb)
|
|
44383
|
+
this.once("close", cb);
|
|
44384
|
+
if (this._state === CLOSING)
|
|
44385
|
+
return;
|
|
44386
|
+
this._state = CLOSING;
|
|
44387
|
+
if (this.options.noServer || this.options.server) {
|
|
44388
|
+
if (this._server) {
|
|
44389
|
+
this._removeListeners();
|
|
44390
|
+
this._removeListeners = this._server = null;
|
|
44391
|
+
}
|
|
44392
|
+
if (this.clients) {
|
|
44393
|
+
if (!this.clients.size) {
|
|
44394
|
+
process.nextTick(emitClose, this);
|
|
44395
|
+
} else {
|
|
44396
|
+
this._shouldEmitClose = true;
|
|
44397
|
+
}
|
|
44398
|
+
} else {
|
|
44399
|
+
process.nextTick(emitClose, this);
|
|
44400
|
+
}
|
|
44401
|
+
} else {
|
|
44402
|
+
const server = this._server;
|
|
44403
|
+
this._removeListeners();
|
|
44404
|
+
this._removeListeners = this._server = null;
|
|
44405
|
+
server.close(() => {
|
|
44406
|
+
emitClose(this);
|
|
44407
|
+
});
|
|
44408
|
+
}
|
|
44409
|
+
}
|
|
44410
|
+
shouldHandle(req) {
|
|
44411
|
+
if (this.options.path) {
|
|
44412
|
+
const index = req.url.indexOf("?");
|
|
44413
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
|
44414
|
+
if (pathname !== this.options.path)
|
|
44415
|
+
return false;
|
|
44416
|
+
}
|
|
44417
|
+
return true;
|
|
44418
|
+
}
|
|
44419
|
+
handleUpgrade(req, socket, head, cb) {
|
|
44420
|
+
socket.on("error", socketOnError);
|
|
44421
|
+
const key = req.headers["sec-websocket-key"];
|
|
44422
|
+
const upgrade = req.headers.upgrade;
|
|
44423
|
+
const version = +req.headers["sec-websocket-version"];
|
|
44424
|
+
if (req.method !== "GET") {
|
|
44425
|
+
const message = "Invalid HTTP method";
|
|
44426
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
|
|
44427
|
+
return;
|
|
44428
|
+
}
|
|
44429
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
44430
|
+
const message = "Invalid Upgrade header";
|
|
44431
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
44432
|
+
return;
|
|
44433
|
+
}
|
|
44434
|
+
if (key === undefined || !keyRegex.test(key)) {
|
|
44435
|
+
const message = "Missing or invalid Sec-WebSocket-Key header";
|
|
44436
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
44437
|
+
return;
|
|
44438
|
+
}
|
|
44439
|
+
if (version !== 13 && version !== 8) {
|
|
44440
|
+
const message = "Missing or invalid Sec-WebSocket-Version header";
|
|
44441
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
|
|
44442
|
+
"Sec-WebSocket-Version": "13, 8"
|
|
44443
|
+
});
|
|
44444
|
+
return;
|
|
44445
|
+
}
|
|
44446
|
+
if (!this.shouldHandle(req)) {
|
|
44447
|
+
abortHandshake(socket, 400);
|
|
44448
|
+
return;
|
|
44449
|
+
}
|
|
44450
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
44451
|
+
let protocols = new Set;
|
|
44452
|
+
if (secWebSocketProtocol !== undefined) {
|
|
44453
|
+
try {
|
|
44454
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
44455
|
+
} catch (err) {
|
|
44456
|
+
const message = "Invalid Sec-WebSocket-Protocol header";
|
|
44457
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
44458
|
+
return;
|
|
44459
|
+
}
|
|
44460
|
+
}
|
|
44461
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
44462
|
+
const extensions = {};
|
|
44463
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== undefined) {
|
|
44464
|
+
const perMessageDeflate = new PerMessageDeflate(this.options.perMessageDeflate, true, this.options.maxPayload);
|
|
44465
|
+
try {
|
|
44466
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
44467
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
44468
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
44469
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
44470
|
+
}
|
|
44471
|
+
} catch (err) {
|
|
44472
|
+
const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
44473
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
44474
|
+
return;
|
|
44475
|
+
}
|
|
44476
|
+
}
|
|
44477
|
+
if (this.options.verifyClient) {
|
|
44478
|
+
const info = {
|
|
44479
|
+
origin: req.headers[`${version === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
44480
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
44481
|
+
req
|
|
44482
|
+
};
|
|
44483
|
+
if (this.options.verifyClient.length === 2) {
|
|
44484
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
44485
|
+
if (!verified) {
|
|
44486
|
+
return abortHandshake(socket, code || 401, message, headers);
|
|
44487
|
+
}
|
|
44488
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
44489
|
+
});
|
|
44490
|
+
return;
|
|
44491
|
+
}
|
|
44492
|
+
if (!this.options.verifyClient(info))
|
|
44493
|
+
return abortHandshake(socket, 401);
|
|
44494
|
+
}
|
|
44495
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
44496
|
+
}
|
|
44497
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
44498
|
+
if (!socket.readable || !socket.writable)
|
|
44499
|
+
return socket.destroy();
|
|
44500
|
+
if (socket[kWebSocket]) {
|
|
44501
|
+
throw new Error("server.handleUpgrade() was called more than once with the same " + "socket, possibly due to a misconfiguration");
|
|
44502
|
+
}
|
|
44503
|
+
if (this._state > RUNNING)
|
|
44504
|
+
return abortHandshake(socket, 503);
|
|
44505
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
44506
|
+
const headers = [
|
|
44507
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
44508
|
+
"Upgrade: websocket",
|
|
44509
|
+
"Connection: Upgrade",
|
|
44510
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
44511
|
+
];
|
|
44512
|
+
const ws = new this.options.WebSocket(null, undefined, this.options);
|
|
44513
|
+
if (protocols.size) {
|
|
44514
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
44515
|
+
if (protocol) {
|
|
44516
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
44517
|
+
ws._protocol = protocol;
|
|
44518
|
+
}
|
|
44519
|
+
}
|
|
44520
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
44521
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
44522
|
+
const value = extension.format({
|
|
44523
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
44524
|
+
});
|
|
44525
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
44526
|
+
ws._extensions = extensions;
|
|
44527
|
+
}
|
|
44528
|
+
this.emit("headers", headers, req);
|
|
44529
|
+
socket.write(headers.concat(`\r
|
|
44530
|
+
`).join(`\r
|
|
44531
|
+
`));
|
|
44532
|
+
socket.removeListener("error", socketOnError);
|
|
44533
|
+
ws.setSocket(socket, head, {
|
|
44534
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
44535
|
+
maxPayload: this.options.maxPayload,
|
|
44536
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
44537
|
+
});
|
|
44538
|
+
if (this.clients) {
|
|
44539
|
+
this.clients.add(ws);
|
|
44540
|
+
ws.on("close", () => {
|
|
44541
|
+
this.clients.delete(ws);
|
|
44542
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
44543
|
+
process.nextTick(emitClose, this);
|
|
44544
|
+
}
|
|
44545
|
+
});
|
|
44546
|
+
}
|
|
44547
|
+
cb(ws, req);
|
|
44548
|
+
}
|
|
44549
|
+
}
|
|
44550
|
+
module.exports = WebSocketServer;
|
|
44551
|
+
function addListeners(server, map2) {
|
|
44552
|
+
for (const event of Object.keys(map2))
|
|
44553
|
+
server.on(event, map2[event]);
|
|
44554
|
+
return function removeListeners() {
|
|
44555
|
+
for (const event of Object.keys(map2)) {
|
|
44556
|
+
server.removeListener(event, map2[event]);
|
|
44557
|
+
}
|
|
44558
|
+
};
|
|
44559
|
+
}
|
|
44560
|
+
function emitClose(server) {
|
|
44561
|
+
server._state = CLOSED;
|
|
44562
|
+
server.emit("close");
|
|
44563
|
+
}
|
|
44564
|
+
function socketOnError() {
|
|
44565
|
+
this.destroy();
|
|
44566
|
+
}
|
|
44567
|
+
function abortHandshake(socket, code, message, headers) {
|
|
44568
|
+
message = message || http.STATUS_CODES[code];
|
|
44569
|
+
headers = {
|
|
44570
|
+
Connection: "close",
|
|
44571
|
+
"Content-Type": "text/html",
|
|
44572
|
+
"Content-Length": Buffer.byteLength(message),
|
|
44573
|
+
...headers
|
|
44574
|
+
};
|
|
44575
|
+
socket.once("finish", socket.destroy);
|
|
44576
|
+
socket.end(`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
44577
|
+
` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join(`\r
|
|
44578
|
+
`) + `\r
|
|
44579
|
+
\r
|
|
44580
|
+
` + message);
|
|
44581
|
+
}
|
|
44582
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
44583
|
+
if (server.listenerCount("wsClientError")) {
|
|
44584
|
+
const err = new Error(message);
|
|
44585
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
44586
|
+
server.emit("wsClientError", err, socket, req);
|
|
44587
|
+
} else {
|
|
44588
|
+
abortHandshake(socket, code, message, headers);
|
|
44589
|
+
}
|
|
44590
|
+
}
|
|
44591
|
+
});
|
|
44592
|
+
|
|
41754
44593
|
// src/Server.js
|
|
41755
44594
|
var import_express2 = __toESM(require_express2(), 1);
|
|
41756
44595
|
var import_cors = __toESM(require_lib3(), 1);
|
|
44596
|
+
import { createServer } from "http";
|
|
41757
44597
|
|
|
41758
44598
|
// src/common/Auth.js
|
|
41759
44599
|
var HARDCODED_TOKEN = "test-token-123";
|
|
@@ -53327,7 +56167,7 @@ var EMPTY_COMPLETION_RESULT = {
|
|
|
53327
56167
|
}
|
|
53328
56168
|
};
|
|
53329
56169
|
// src/mcp/McpServer.js
|
|
53330
|
-
var createMcpServer = () => {
|
|
56170
|
+
var createMcpServer = ({ sessionManager } = {}) => {
|
|
53331
56171
|
const server = new McpServer({
|
|
53332
56172
|
name: "caw-server",
|
|
53333
56173
|
version: "0.0.1"
|
|
@@ -53349,14 +56189,118 @@ var createMcpServer = () => {
|
|
|
53349
56189
|
]
|
|
53350
56190
|
};
|
|
53351
56191
|
});
|
|
56192
|
+
if (sessionManager) {
|
|
56193
|
+
server.registerTool("get_state", {
|
|
56194
|
+
title: "Get ChatGPT State",
|
|
56195
|
+
description: "Get the current state of ChatGPT (IDLE, COMPOSING, RESPONDING, or UNKNOWN)",
|
|
56196
|
+
inputSchema: exports_external2.object({
|
|
56197
|
+
pageId: exports_external2.string().describe("Page ID of the ChatGPT tab")
|
|
56198
|
+
})
|
|
56199
|
+
}, async ({ pageId }) => {
|
|
56200
|
+
try {
|
|
56201
|
+
const result = await sessionManager.sendCommand(pageId, "getState", {});
|
|
56202
|
+
return {
|
|
56203
|
+
content: [
|
|
56204
|
+
{
|
|
56205
|
+
type: "text",
|
|
56206
|
+
text: `ChatGPT state: ${result.state}`
|
|
56207
|
+
}
|
|
56208
|
+
]
|
|
56209
|
+
};
|
|
56210
|
+
} catch (error) {
|
|
56211
|
+
return {
|
|
56212
|
+
content: [
|
|
56213
|
+
{
|
|
56214
|
+
type: "text",
|
|
56215
|
+
text: `Error: ${error.message}`
|
|
56216
|
+
}
|
|
56217
|
+
],
|
|
56218
|
+
isError: true
|
|
56219
|
+
};
|
|
56220
|
+
}
|
|
56221
|
+
});
|
|
56222
|
+
server.registerTool("send_message", {
|
|
56223
|
+
title: "Send Message to ChatGPT",
|
|
56224
|
+
description: "Send a message to ChatGPT and wait for the response. Requires ChatGPT to be in IDLE state.",
|
|
56225
|
+
inputSchema: exports_external2.object({
|
|
56226
|
+
pageId: exports_external2.string().describe("Page ID of the ChatGPT tab"),
|
|
56227
|
+
text: exports_external2.string().describe("Message to send to ChatGPT"),
|
|
56228
|
+
timeout: exports_external2.number().optional().describe("Timeout in milliseconds (default: 120000)")
|
|
56229
|
+
})
|
|
56230
|
+
}, async ({ pageId, text, timeout }) => {
|
|
56231
|
+
try {
|
|
56232
|
+
const result = await sessionManager.sendCommand(pageId, "sendMessage", { text, timeout });
|
|
56233
|
+
return {
|
|
56234
|
+
content: [
|
|
56235
|
+
{
|
|
56236
|
+
type: "text",
|
|
56237
|
+
text: `ChatGPT response (turn ${result.turn}, ${result.duration}ms):
|
|
56238
|
+
|
|
56239
|
+
${result.text}`
|
|
56240
|
+
}
|
|
56241
|
+
]
|
|
56242
|
+
};
|
|
56243
|
+
} catch (error) {
|
|
56244
|
+
return {
|
|
56245
|
+
content: [
|
|
56246
|
+
{
|
|
56247
|
+
type: "text",
|
|
56248
|
+
text: `Error: ${error.message}`
|
|
56249
|
+
}
|
|
56250
|
+
],
|
|
56251
|
+
isError: true
|
|
56252
|
+
};
|
|
56253
|
+
}
|
|
56254
|
+
});
|
|
56255
|
+
server.registerTool("get_last_message", {
|
|
56256
|
+
title: "Get Last ChatGPT Message",
|
|
56257
|
+
description: "Get the most recent message from ChatGPT",
|
|
56258
|
+
inputSchema: exports_external2.object({
|
|
56259
|
+
pageId: exports_external2.string().describe("Page ID of the ChatGPT tab")
|
|
56260
|
+
})
|
|
56261
|
+
}, async ({ pageId }) => {
|
|
56262
|
+
try {
|
|
56263
|
+
const result = await sessionManager.sendCommand(pageId, "getLastMessage", {});
|
|
56264
|
+
if (result.text) {
|
|
56265
|
+
return {
|
|
56266
|
+
content: [
|
|
56267
|
+
{
|
|
56268
|
+
type: "text",
|
|
56269
|
+
text: result.text
|
|
56270
|
+
}
|
|
56271
|
+
]
|
|
56272
|
+
};
|
|
56273
|
+
} else {
|
|
56274
|
+
return {
|
|
56275
|
+
content: [
|
|
56276
|
+
{
|
|
56277
|
+
type: "text",
|
|
56278
|
+
text: "No message found"
|
|
56279
|
+
}
|
|
56280
|
+
]
|
|
56281
|
+
};
|
|
56282
|
+
}
|
|
56283
|
+
} catch (error) {
|
|
56284
|
+
return {
|
|
56285
|
+
content: [
|
|
56286
|
+
{
|
|
56287
|
+
type: "text",
|
|
56288
|
+
text: `Error: ${error.message}`
|
|
56289
|
+
}
|
|
56290
|
+
],
|
|
56291
|
+
isError: true
|
|
56292
|
+
};
|
|
56293
|
+
}
|
|
56294
|
+
});
|
|
56295
|
+
}
|
|
53352
56296
|
return server;
|
|
53353
56297
|
};
|
|
53354
56298
|
var McpServer_default = createMcpServer;
|
|
53355
56299
|
|
|
53356
56300
|
// src/mcp/McpRoutes.js
|
|
53357
|
-
var create3 = (app) => {
|
|
56301
|
+
var create3 = (app, { sessionManager } = {}) => {
|
|
53358
56302
|
const auth = Auth_default.create();
|
|
53359
|
-
const mcpServer = McpServer_default();
|
|
56303
|
+
const mcpServer = McpServer_default({ sessionManager });
|
|
53360
56304
|
app.post("/api/mcp", auth.middleware, async (req, res) => {
|
|
53361
56305
|
try {
|
|
53362
56306
|
const transport = new StreamableHTTPServerTransport({
|
|
@@ -53400,10 +56344,253 @@ var create4 = (app, options) => {
|
|
|
53400
56344
|
};
|
|
53401
56345
|
var StaticRoutes_default = { create: create4 };
|
|
53402
56346
|
|
|
56347
|
+
// src/test/TestRoutes.js
|
|
56348
|
+
var create5 = (app, { sessionManager }) => {
|
|
56349
|
+
if (!sessionManager) {
|
|
56350
|
+
console.warn("[TestRoutes] SessionManager not provided, skipping test routes");
|
|
56351
|
+
return;
|
|
56352
|
+
}
|
|
56353
|
+
app.get("/test/sessions", (req, res) => {
|
|
56354
|
+
const sessions = [];
|
|
56355
|
+
const allSessions = sessionManager.getSession();
|
|
56356
|
+
res.json({
|
|
56357
|
+
message: "Session listing not implemented - check server logs for pageIds"
|
|
56358
|
+
});
|
|
56359
|
+
});
|
|
56360
|
+
app.post("/test/command", async (req, res) => {
|
|
56361
|
+
const { pageId, operation, args = {} } = req.body;
|
|
56362
|
+
if (!pageId || !operation) {
|
|
56363
|
+
return res.status(400).json({
|
|
56364
|
+
error: "Missing required fields: pageId, operation"
|
|
56365
|
+
});
|
|
56366
|
+
}
|
|
56367
|
+
try {
|
|
56368
|
+
const result = await sessionManager.sendCommand(pageId, operation, args);
|
|
56369
|
+
res.json({
|
|
56370
|
+
success: true,
|
|
56371
|
+
data: result
|
|
56372
|
+
});
|
|
56373
|
+
} catch (error) {
|
|
56374
|
+
res.status(500).json({
|
|
56375
|
+
success: false,
|
|
56376
|
+
error: error.message
|
|
56377
|
+
});
|
|
56378
|
+
}
|
|
56379
|
+
});
|
|
56380
|
+
console.log("[TestRoutes] Test endpoints registered: GET /test/sessions, POST /test/command");
|
|
56381
|
+
};
|
|
56382
|
+
var TestRoutes_default = { create: create5 };
|
|
56383
|
+
|
|
56384
|
+
// node_modules/ws/wrapper.mjs
|
|
56385
|
+
var import_stream = __toESM(require_stream(), 1);
|
|
56386
|
+
var import_receiver = __toESM(require_receiver(), 1);
|
|
56387
|
+
var import_sender = __toESM(require_sender(), 1);
|
|
56388
|
+
var import_websocket = __toESM(require_websocket(), 1);
|
|
56389
|
+
var import_websocket_server = __toESM(require_websocket_server(), 1);
|
|
56390
|
+
|
|
56391
|
+
// src/wss/MessageTypes.js
|
|
56392
|
+
var MessageKinds = Object.freeze({
|
|
56393
|
+
HELLO: "hello",
|
|
56394
|
+
PONG: "pong",
|
|
56395
|
+
RESPONSE: "res",
|
|
56396
|
+
PING: "ping",
|
|
56397
|
+
COMMAND: "cmd"
|
|
56398
|
+
});
|
|
56399
|
+
|
|
56400
|
+
// src/wss/MessageValidator.js
|
|
56401
|
+
var SessionState = Object.freeze({
|
|
56402
|
+
INJECTING: "INJECTING",
|
|
56403
|
+
CONNECTED: "CONNECTED",
|
|
56404
|
+
REMOVED: "REMOVED"
|
|
56405
|
+
});
|
|
56406
|
+
var parseJSON = (data) => {
|
|
56407
|
+
try {
|
|
56408
|
+
const message = JSON.parse(data);
|
|
56409
|
+
return { valid: true, message };
|
|
56410
|
+
} catch (error) {
|
|
56411
|
+
return { valid: false, error: "Invalid JSON" };
|
|
56412
|
+
}
|
|
56413
|
+
};
|
|
56414
|
+
var validateKind = (message) => {
|
|
56415
|
+
if (!message.kind || typeof message.kind !== "string") {
|
|
56416
|
+
return { valid: false, error: "Missing or invalid kind field" };
|
|
56417
|
+
}
|
|
56418
|
+
const validKinds = Object.values(MessageKinds);
|
|
56419
|
+
if (!validKinds.includes(message.kind)) {
|
|
56420
|
+
return { valid: false, error: `Unknown message kind: ${message.kind}` };
|
|
56421
|
+
}
|
|
56422
|
+
return { valid: true, message };
|
|
56423
|
+
};
|
|
56424
|
+
var validateHello = (message) => {
|
|
56425
|
+
if (!message.pageId || typeof message.pageId !== "string") {
|
|
56426
|
+
return { valid: false, error: "Hello message missing pageId" };
|
|
56427
|
+
}
|
|
56428
|
+
if (!message.version || typeof message.version !== "string") {
|
|
56429
|
+
return { valid: false, error: "Hello message missing version" };
|
|
56430
|
+
}
|
|
56431
|
+
if (!Array.isArray(message.capabilities)) {
|
|
56432
|
+
return { valid: false, error: "Hello message missing capabilities array" };
|
|
56433
|
+
}
|
|
56434
|
+
return { valid: true, message };
|
|
56435
|
+
};
|
|
56436
|
+
var validatePong = (message) => {
|
|
56437
|
+
if (typeof message.timestamp !== "number") {
|
|
56438
|
+
return { valid: false, error: "Pong message missing timestamp" };
|
|
56439
|
+
}
|
|
56440
|
+
return { valid: true, message };
|
|
56441
|
+
};
|
|
56442
|
+
var validateResponse = (message) => {
|
|
56443
|
+
if (!message.requestId || typeof message.requestId !== "string") {
|
|
56444
|
+
return { valid: false, error: "Response message missing requestId" };
|
|
56445
|
+
}
|
|
56446
|
+
if (typeof message.success !== "boolean") {
|
|
56447
|
+
return { valid: false, error: "Response message missing success field" };
|
|
56448
|
+
}
|
|
56449
|
+
if (message.success === true) {
|
|
56450
|
+
if (!("data" in message)) {
|
|
56451
|
+
return { valid: false, error: "Success response missing data field" };
|
|
56452
|
+
}
|
|
56453
|
+
} else {
|
|
56454
|
+
if (!message.error || typeof message.error !== "string") {
|
|
56455
|
+
return { valid: false, error: "Error response missing error message" };
|
|
56456
|
+
}
|
|
56457
|
+
}
|
|
56458
|
+
return { valid: true, message };
|
|
56459
|
+
};
|
|
56460
|
+
var validateStructure = (message) => {
|
|
56461
|
+
switch (message.kind) {
|
|
56462
|
+
case MessageKinds.HELLO:
|
|
56463
|
+
return validateHello(message);
|
|
56464
|
+
case MessageKinds.PONG:
|
|
56465
|
+
return validatePong(message);
|
|
56466
|
+
case MessageKinds.RESPONSE:
|
|
56467
|
+
return validateResponse(message);
|
|
56468
|
+
default:
|
|
56469
|
+
return { valid: false, error: `No validator for message kind: ${message.kind}` };
|
|
56470
|
+
}
|
|
56471
|
+
};
|
|
56472
|
+
var validateState = (message, sessionState) => {
|
|
56473
|
+
if (sessionState === SessionState.REMOVED) {
|
|
56474
|
+
return { valid: false, error: "Session has been removed" };
|
|
56475
|
+
}
|
|
56476
|
+
if (sessionState === SessionState.INJECTING) {
|
|
56477
|
+
if (message.kind !== MessageKinds.HELLO) {
|
|
56478
|
+
return { valid: false, error: `Only hello messages allowed in INJECTING state, got: ${message.kind}` };
|
|
56479
|
+
}
|
|
56480
|
+
}
|
|
56481
|
+
if (sessionState === SessionState.CONNECTED) {
|
|
56482
|
+
if (message.kind === MessageKinds.HELLO) {
|
|
56483
|
+
return { valid: false, error: "Hello message not allowed in CONNECTED state" };
|
|
56484
|
+
}
|
|
56485
|
+
}
|
|
56486
|
+
return { valid: true, message };
|
|
56487
|
+
};
|
|
56488
|
+
var validate = (data, sessionState = null) => {
|
|
56489
|
+
let result = parseJSON(data);
|
|
56490
|
+
if (!result.valid) {
|
|
56491
|
+
return result;
|
|
56492
|
+
}
|
|
56493
|
+
const message = result.message;
|
|
56494
|
+
result = validateKind(message);
|
|
56495
|
+
if (!result.valid) {
|
|
56496
|
+
return result;
|
|
56497
|
+
}
|
|
56498
|
+
result = validateStructure(message);
|
|
56499
|
+
if (!result.valid) {
|
|
56500
|
+
return result;
|
|
56501
|
+
}
|
|
56502
|
+
if (sessionState !== null) {
|
|
56503
|
+
result = validateState(message, sessionState);
|
|
56504
|
+
if (!result.valid) {
|
|
56505
|
+
return result;
|
|
56506
|
+
}
|
|
56507
|
+
}
|
|
56508
|
+
return { valid: true, message };
|
|
56509
|
+
};
|
|
56510
|
+
var MessageValidator_default = {
|
|
56511
|
+
validate,
|
|
56512
|
+
SessionState
|
|
56513
|
+
};
|
|
56514
|
+
|
|
56515
|
+
// src/wss/WebSocketServer.js
|
|
56516
|
+
var create6 = ({ server, sessionManager }) => {
|
|
56517
|
+
if (!server) {
|
|
56518
|
+
throw new Error("WebSocketServer requires HTTP server instance");
|
|
56519
|
+
}
|
|
56520
|
+
if (!sessionManager) {
|
|
56521
|
+
throw new Error("WebSocketServer requires SessionManager instance");
|
|
56522
|
+
}
|
|
56523
|
+
let wss = null;
|
|
56524
|
+
const start = () => {
|
|
56525
|
+
wss = new import_websocket_server.default({ server, path: "/ws" });
|
|
56526
|
+
wss.on("connection", (ws) => {
|
|
56527
|
+
console.log("WebSocket connection established");
|
|
56528
|
+
ws.on("message", (data) => {
|
|
56529
|
+
try {
|
|
56530
|
+
const validation = MessageValidator_default.validate(data.toString());
|
|
56531
|
+
if (!validation.valid) {
|
|
56532
|
+
console.warn("[WebSocketServer] Invalid message:", validation.error);
|
|
56533
|
+
return;
|
|
56534
|
+
}
|
|
56535
|
+
const message = validation.message;
|
|
56536
|
+
if (message.kind === "hello") {
|
|
56537
|
+
sessionManager.handleHello(ws, message);
|
|
56538
|
+
} else if (message.kind === "pong") {
|
|
56539
|
+
sessionManager.handlePongWithConnection(ws, message);
|
|
56540
|
+
} else if (message.kind === "res") {
|
|
56541
|
+
sessionManager.handleResponse(ws, message);
|
|
56542
|
+
} else {
|
|
56543
|
+
console.warn("[WebSocketServer] Unknown message kind:", message.kind);
|
|
56544
|
+
}
|
|
56545
|
+
} catch (error) {
|
|
56546
|
+
console.error("[WebSocketServer] Error handling message:", error);
|
|
56547
|
+
}
|
|
56548
|
+
});
|
|
56549
|
+
ws.on("error", (error) => {
|
|
56550
|
+
console.error("[WebSocketServer] WebSocket error:", error);
|
|
56551
|
+
});
|
|
56552
|
+
ws.on("close", () => {
|
|
56553
|
+
console.log("[WebSocketServer] WebSocket connection closed");
|
|
56554
|
+
sessionManager.handleDisconnect(ws);
|
|
56555
|
+
});
|
|
56556
|
+
});
|
|
56557
|
+
wss.on("error", (error) => {
|
|
56558
|
+
console.error("[WebSocketServer] Server error:", error);
|
|
56559
|
+
});
|
|
56560
|
+
console.log("[WebSocketServer] WebSocket server attached to HTTP server at path /ws");
|
|
56561
|
+
};
|
|
56562
|
+
const stop = () => {
|
|
56563
|
+
return new Promise((resolve, reject) => {
|
|
56564
|
+
if (!wss) {
|
|
56565
|
+
resolve();
|
|
56566
|
+
return;
|
|
56567
|
+
}
|
|
56568
|
+
wss.clients.forEach((ws) => {
|
|
56569
|
+
ws.close();
|
|
56570
|
+
});
|
|
56571
|
+
wss.close((err) => {
|
|
56572
|
+
if (err) {
|
|
56573
|
+
console.error("Error closing WebSocket server:", err);
|
|
56574
|
+
reject(err);
|
|
56575
|
+
} else {
|
|
56576
|
+
console.log("WebSocket server stopped");
|
|
56577
|
+
resolve();
|
|
56578
|
+
}
|
|
56579
|
+
});
|
|
56580
|
+
});
|
|
56581
|
+
};
|
|
56582
|
+
return Object.freeze({
|
|
56583
|
+
start,
|
|
56584
|
+
stop
|
|
56585
|
+
});
|
|
56586
|
+
};
|
|
56587
|
+
var WebSocketServer_default = { create: create6 };
|
|
56588
|
+
|
|
53403
56589
|
// src/Server.js
|
|
53404
|
-
var
|
|
53405
|
-
const { port = 3000, staticPath } = options;
|
|
56590
|
+
var create7 = (options = {}) => {
|
|
56591
|
+
const { port = 3000, staticPath, sessionManager } = options;
|
|
53406
56592
|
const app = import_express2.default();
|
|
56593
|
+
const httpServer = createServer(app);
|
|
53407
56594
|
app.use(import_cors.default());
|
|
53408
56595
|
app.use(import_express2.default.json());
|
|
53409
56596
|
app.get("/health", (req, res) => {
|
|
@@ -53413,19 +56600,291 @@ var create5 = (options = {}) => {
|
|
|
53413
56600
|
});
|
|
53414
56601
|
});
|
|
53415
56602
|
ApiRoutes_default.create(app);
|
|
53416
|
-
McpRoutes_default.create(app);
|
|
56603
|
+
McpRoutes_default.create(app, { sessionManager });
|
|
53417
56604
|
StaticRoutes_default.create(app, { staticPath });
|
|
56605
|
+
if (sessionManager) {
|
|
56606
|
+
TestRoutes_default.create(app, { sessionManager });
|
|
56607
|
+
}
|
|
56608
|
+
let wss = null;
|
|
56609
|
+
if (sessionManager) {
|
|
56610
|
+
wss = WebSocketServer_default.create({ server: httpServer, sessionManager });
|
|
56611
|
+
}
|
|
53418
56612
|
const start = () => {
|
|
53419
|
-
|
|
56613
|
+
if (wss) {
|
|
56614
|
+
wss.start();
|
|
56615
|
+
}
|
|
56616
|
+
httpServer.listen(port, () => {
|
|
53420
56617
|
console.log(`CAW server listening on http://localhost:${port}`);
|
|
53421
56618
|
});
|
|
53422
56619
|
};
|
|
56620
|
+
const stop = async () => {
|
|
56621
|
+
if (wss) {
|
|
56622
|
+
await wss.stop();
|
|
56623
|
+
}
|
|
56624
|
+
return new Promise((resolve, reject) => {
|
|
56625
|
+
httpServer.close((err) => {
|
|
56626
|
+
if (err) {
|
|
56627
|
+
reject(err);
|
|
56628
|
+
} else {
|
|
56629
|
+
resolve();
|
|
56630
|
+
}
|
|
56631
|
+
});
|
|
56632
|
+
});
|
|
56633
|
+
};
|
|
53423
56634
|
return Object.freeze({
|
|
53424
56635
|
start,
|
|
53425
|
-
|
|
56636
|
+
stop,
|
|
56637
|
+
app,
|
|
56638
|
+
httpServer,
|
|
56639
|
+
sessionManager
|
|
56640
|
+
});
|
|
56641
|
+
};
|
|
56642
|
+
var Server_default = { create: create7 };
|
|
56643
|
+
|
|
56644
|
+
// src/wss/SessionManager.js
|
|
56645
|
+
var { SessionState: SessionState2 } = MessageValidator_default;
|
|
56646
|
+
var DEFAULT_HEARTBEAT_INTERVAL_MS = 30000;
|
|
56647
|
+
var DEFAULT_PONG_TIMEOUT_MS = 5000;
|
|
56648
|
+
var DEFAULT_MAX_MISSED_PONGS = 3;
|
|
56649
|
+
var DEFAULT_COMMAND_TIMEOUT_MS = 30000;
|
|
56650
|
+
var create8 = (options = {}) => {
|
|
56651
|
+
const {
|
|
56652
|
+
heartbeatIntervalMs = DEFAULT_HEARTBEAT_INTERVAL_MS,
|
|
56653
|
+
pongTimeoutMs = DEFAULT_PONG_TIMEOUT_MS,
|
|
56654
|
+
maxMissedPongs = DEFAULT_MAX_MISSED_PONGS,
|
|
56655
|
+
commandTimeoutMs = DEFAULT_COMMAND_TIMEOUT_MS
|
|
56656
|
+
} = options;
|
|
56657
|
+
const sessions = new Map;
|
|
56658
|
+
const connectionToPageId = new Map;
|
|
56659
|
+
const endSession = (pageId, reason) => {
|
|
56660
|
+
const session = sessions.get(pageId);
|
|
56661
|
+
if (!session) {
|
|
56662
|
+
return;
|
|
56663
|
+
}
|
|
56664
|
+
const expectedReasons = ["MANUAL"];
|
|
56665
|
+
const isExpected = expectedReasons.includes(reason);
|
|
56666
|
+
if (isExpected) {
|
|
56667
|
+
console.info(`[SessionManager] Session ${pageId} ended: ${reason}`);
|
|
56668
|
+
} else {
|
|
56669
|
+
console.error(`[SessionManager] Session ${pageId} ended: ${reason}`);
|
|
56670
|
+
}
|
|
56671
|
+
if (session.heartbeatInterval) {
|
|
56672
|
+
clearInterval(session.heartbeatInterval);
|
|
56673
|
+
session.heartbeatInterval = null;
|
|
56674
|
+
}
|
|
56675
|
+
if (session.pongTimeout) {
|
|
56676
|
+
clearTimeout(session.pongTimeout);
|
|
56677
|
+
session.pongTimeout = null;
|
|
56678
|
+
}
|
|
56679
|
+
for (const [requestId, pending] of session.pendingCommands) {
|
|
56680
|
+
clearTimeout(pending.timeout);
|
|
56681
|
+
pending.reject(new Error(`Session ended: ${reason}`));
|
|
56682
|
+
}
|
|
56683
|
+
session.pendingCommands.clear();
|
|
56684
|
+
if (session.wsConnection) {
|
|
56685
|
+
connectionToPageId.delete(session.wsConnection);
|
|
56686
|
+
}
|
|
56687
|
+
session.state = SessionState2.REMOVED;
|
|
56688
|
+
sessions.delete(pageId);
|
|
56689
|
+
};
|
|
56690
|
+
const startHeartbeat = (pageId) => {
|
|
56691
|
+
const session = sessions.get(pageId);
|
|
56692
|
+
if (!session || !session.wsConnection) {
|
|
56693
|
+
return;
|
|
56694
|
+
}
|
|
56695
|
+
if (session.heartbeatInterval) {
|
|
56696
|
+
return;
|
|
56697
|
+
}
|
|
56698
|
+
session.heartbeatInterval = setInterval(() => {
|
|
56699
|
+
const currentSession = sessions.get(pageId);
|
|
56700
|
+
if (!currentSession || !currentSession.wsConnection) {
|
|
56701
|
+
return;
|
|
56702
|
+
}
|
|
56703
|
+
const pingMessage = {
|
|
56704
|
+
kind: MessageKinds.PING,
|
|
56705
|
+
timestamp: Date.now()
|
|
56706
|
+
};
|
|
56707
|
+
try {
|
|
56708
|
+
currentSession.wsConnection.send(JSON.stringify(pingMessage));
|
|
56709
|
+
} catch (error) {
|
|
56710
|
+
console.error(`[SessionManager] Error sending ping to ${pageId}:`, error);
|
|
56711
|
+
endSession(pageId, "PING_SEND_ERROR");
|
|
56712
|
+
return;
|
|
56713
|
+
}
|
|
56714
|
+
if (currentSession.pongTimeout) {
|
|
56715
|
+
clearTimeout(currentSession.pongTimeout);
|
|
56716
|
+
}
|
|
56717
|
+
currentSession.pongTimeout = setTimeout(() => {
|
|
56718
|
+
const sessionAfterTimeout = sessions.get(pageId);
|
|
56719
|
+
if (!sessionAfterTimeout) {
|
|
56720
|
+
return;
|
|
56721
|
+
}
|
|
56722
|
+
sessionAfterTimeout.missedPongs += 1;
|
|
56723
|
+
if (sessionAfterTimeout.missedPongs >= maxMissedPongs) {
|
|
56724
|
+
endSession(pageId, "HEARTBEAT");
|
|
56725
|
+
}
|
|
56726
|
+
}, pongTimeoutMs);
|
|
56727
|
+
}, heartbeatIntervalMs);
|
|
56728
|
+
};
|
|
56729
|
+
const getSession = (pageId) => {
|
|
56730
|
+
return sessions.get(pageId);
|
|
56731
|
+
};
|
|
56732
|
+
const getPageIdForConnection = (ws) => {
|
|
56733
|
+
return connectionToPageId.get(ws);
|
|
56734
|
+
};
|
|
56735
|
+
const handleHello = (ws, message) => {
|
|
56736
|
+
const { pageId, version, capabilities } = message;
|
|
56737
|
+
const existingSession = sessions.get(pageId);
|
|
56738
|
+
if (existingSession) {
|
|
56739
|
+
console.warn(`[SessionManager] Hello received for existing session: ${pageId}`);
|
|
56740
|
+
if (existingSession.wsConnection !== ws) {
|
|
56741
|
+
if (existingSession.wsConnection) {
|
|
56742
|
+
connectionToPageId.delete(existingSession.wsConnection);
|
|
56743
|
+
}
|
|
56744
|
+
existingSession.wsConnection = ws;
|
|
56745
|
+
connectionToPageId.set(ws, pageId);
|
|
56746
|
+
}
|
|
56747
|
+
return;
|
|
56748
|
+
}
|
|
56749
|
+
console.info(`[SessionManager] Creating session for pageId: ${pageId}`);
|
|
56750
|
+
const now = Date.now();
|
|
56751
|
+
const session = {
|
|
56752
|
+
pageId,
|
|
56753
|
+
state: SessionState2.CONNECTED,
|
|
56754
|
+
wsConnection: ws,
|
|
56755
|
+
createdAt: now,
|
|
56756
|
+
lastHeartbeat: now,
|
|
56757
|
+
version,
|
|
56758
|
+
capabilities,
|
|
56759
|
+
heartbeatInterval: null,
|
|
56760
|
+
pongTimeout: null,
|
|
56761
|
+
missedPongs: 0,
|
|
56762
|
+
pendingCommands: new Map,
|
|
56763
|
+
seq: 0
|
|
56764
|
+
};
|
|
56765
|
+
sessions.set(pageId, session);
|
|
56766
|
+
connectionToPageId.set(ws, pageId);
|
|
56767
|
+
ws.on("close", () => {
|
|
56768
|
+
const currentPageId = connectionToPageId.get(ws);
|
|
56769
|
+
if (currentPageId === pageId) {
|
|
56770
|
+
endSession(pageId, "WS_CLOSE");
|
|
56771
|
+
}
|
|
56772
|
+
});
|
|
56773
|
+
startHeartbeat(pageId);
|
|
56774
|
+
};
|
|
56775
|
+
const handlePongWithConnection = (ws, message) => {
|
|
56776
|
+
const pageId = connectionToPageId.get(ws);
|
|
56777
|
+
if (!pageId) {
|
|
56778
|
+
console.warn("[SessionManager] Pong received for unknown connection");
|
|
56779
|
+
return;
|
|
56780
|
+
}
|
|
56781
|
+
const session = sessions.get(pageId);
|
|
56782
|
+
if (!session) {
|
|
56783
|
+
return;
|
|
56784
|
+
}
|
|
56785
|
+
if (session.state !== SessionState2.CONNECTED) {
|
|
56786
|
+
console.warn(`[SessionManager] Pong received in ${session.state} state for pageId ${pageId}`);
|
|
56787
|
+
return;
|
|
56788
|
+
}
|
|
56789
|
+
if (session.pongTimeout) {
|
|
56790
|
+
clearTimeout(session.pongTimeout);
|
|
56791
|
+
session.pongTimeout = null;
|
|
56792
|
+
}
|
|
56793
|
+
session.missedPongs = 0;
|
|
56794
|
+
session.lastHeartbeat = Date.now();
|
|
56795
|
+
};
|
|
56796
|
+
const handleResponse = (ws, message) => {
|
|
56797
|
+
const pageId = connectionToPageId.get(ws);
|
|
56798
|
+
if (!pageId) {
|
|
56799
|
+
console.warn("[SessionManager] Response received for unknown connection");
|
|
56800
|
+
return;
|
|
56801
|
+
}
|
|
56802
|
+
const session = sessions.get(pageId);
|
|
56803
|
+
if (!session) {
|
|
56804
|
+
return;
|
|
56805
|
+
}
|
|
56806
|
+
if (session.state !== SessionState2.CONNECTED) {
|
|
56807
|
+
console.warn(`[SessionManager] Response received in ${session.state} state for pageId ${pageId}`);
|
|
56808
|
+
return;
|
|
56809
|
+
}
|
|
56810
|
+
const { requestId, success, data, error } = message;
|
|
56811
|
+
const pending = session.pendingCommands.get(requestId);
|
|
56812
|
+
if (!pending) {
|
|
56813
|
+
console.warn(`[SessionManager] Unknown request ID: ${requestId} for pageId ${pageId}`);
|
|
56814
|
+
return;
|
|
56815
|
+
}
|
|
56816
|
+
clearTimeout(pending.timeout);
|
|
56817
|
+
session.pendingCommands.delete(requestId);
|
|
56818
|
+
if (success) {
|
|
56819
|
+
pending.resolve(data);
|
|
56820
|
+
} else {
|
|
56821
|
+
pending.reject(new Error(error || "Unknown error"));
|
|
56822
|
+
}
|
|
56823
|
+
};
|
|
56824
|
+
const handleDisconnect = (ws) => {
|
|
56825
|
+
const pageId = connectionToPageId.get(ws);
|
|
56826
|
+
if (pageId) {}
|
|
56827
|
+
};
|
|
56828
|
+
const sendCommand = (pageId, operation, args, timeout = commandTimeoutMs) => {
|
|
56829
|
+
const session = sessions.get(pageId);
|
|
56830
|
+
if (!session) {
|
|
56831
|
+
return Promise.reject(new Error("Session not found"));
|
|
56832
|
+
}
|
|
56833
|
+
if (session.state !== SessionState2.CONNECTED) {
|
|
56834
|
+
return Promise.reject(new Error("Session not connected"));
|
|
56835
|
+
}
|
|
56836
|
+
if (!session.wsConnection) {
|
|
56837
|
+
return Promise.reject(new Error("No WebSocket connection"));
|
|
56838
|
+
}
|
|
56839
|
+
return new Promise((resolve, reject) => {
|
|
56840
|
+
session.seq += 1;
|
|
56841
|
+
const requestId = `req-${Date.now()}-${session.seq}`;
|
|
56842
|
+
const timeoutHandle = setTimeout(() => {
|
|
56843
|
+
session.pendingCommands.delete(requestId);
|
|
56844
|
+
reject(new Error("Command timeout"));
|
|
56845
|
+
}, timeout);
|
|
56846
|
+
session.pendingCommands.set(requestId, {
|
|
56847
|
+
requestId,
|
|
56848
|
+
operation,
|
|
56849
|
+
resolve,
|
|
56850
|
+
reject,
|
|
56851
|
+
timeout: timeoutHandle
|
|
56852
|
+
});
|
|
56853
|
+
const message = {
|
|
56854
|
+
kind: MessageKinds.COMMAND,
|
|
56855
|
+
requestId,
|
|
56856
|
+
operation,
|
|
56857
|
+
args
|
|
56858
|
+
};
|
|
56859
|
+
try {
|
|
56860
|
+
session.wsConnection.send(JSON.stringify(message));
|
|
56861
|
+
} catch (error) {
|
|
56862
|
+
session.pendingCommands.delete(requestId);
|
|
56863
|
+
clearTimeout(timeoutHandle);
|
|
56864
|
+
reject(error);
|
|
56865
|
+
}
|
|
56866
|
+
});
|
|
56867
|
+
};
|
|
56868
|
+
const shutdown = () => {
|
|
56869
|
+
for (const [pageId] of sessions) {
|
|
56870
|
+
endSession(pageId, "SHUTDOWN");
|
|
56871
|
+
}
|
|
56872
|
+
sessions.clear();
|
|
56873
|
+
connectionToPageId.clear();
|
|
56874
|
+
};
|
|
56875
|
+
return Object.freeze({
|
|
56876
|
+
getSession,
|
|
56877
|
+
getPageIdForConnection,
|
|
56878
|
+
handleHello,
|
|
56879
|
+
handlePongWithConnection,
|
|
56880
|
+
handleResponse,
|
|
56881
|
+
handleDisconnect,
|
|
56882
|
+
sendCommand,
|
|
56883
|
+
endSession,
|
|
56884
|
+
shutdown
|
|
53426
56885
|
});
|
|
53427
56886
|
};
|
|
53428
|
-
var
|
|
56887
|
+
var SessionManager_default = { create: create8 };
|
|
53429
56888
|
|
|
53430
56889
|
// src/main.js
|
|
53431
56890
|
import path from "path";
|
|
@@ -53434,7 +56893,23 @@ var __filename2 = fileURLToPath(import.meta.url);
|
|
|
53434
56893
|
var __dirname2 = path.dirname(__filename2);
|
|
53435
56894
|
var port = parseInt(process.env.PORT || "3000", 10);
|
|
53436
56895
|
var staticPath = process.env.STATIC_PATH || path.join(__dirname2, "public_html");
|
|
53437
|
-
var
|
|
56896
|
+
var sessionManager = SessionManager_default.create();
|
|
56897
|
+
var server = Server_default.create({ port, staticPath, sessionManager });
|
|
53438
56898
|
server.start();
|
|
56899
|
+
var shutdown = async (signal) => {
|
|
56900
|
+
console.log(`
|
|
56901
|
+
${signal} received, shutting down gracefully...`);
|
|
56902
|
+
try {
|
|
56903
|
+
sessionManager.shutdown();
|
|
56904
|
+
await server.stop();
|
|
56905
|
+
console.log("Server stopped successfully");
|
|
56906
|
+
process.exit(0);
|
|
56907
|
+
} catch (error) {
|
|
56908
|
+
console.error("Error during shutdown:", error);
|
|
56909
|
+
process.exit(1);
|
|
56910
|
+
}
|
|
56911
|
+
};
|
|
56912
|
+
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
56913
|
+
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
53439
56914
|
|
|
53440
|
-
//# debugId=
|
|
56915
|
+
//# debugId=F05DD80CB01A9C4E64756E2164756E21
|