zefiro 0.8.0 → 0.9.0
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/README.md +8 -76
- package/dist/App-bh752tgg.js +24972 -0
- package/dist/cli-6qr9gvkp.js +508 -0
- package/dist/cli-b26q1e27.js +264 -0
- package/dist/cli-cymqw41r.js +119 -0
- package/dist/cli-g7n2me6z.js +133 -0
- package/dist/cli.js +51 -45
- package/dist/explorer-g6bmbak1.js +10 -0
- package/dist/index.js +1 -1
- package/dist/mcp.js +3836 -26
- package/dist/report-sdtah1f4.js +11 -0
- package/package.json +4 -2
- package/skills/zefiro-copilot/SKILL.md +103 -0
package/dist/mcp.js
CHANGED
|
@@ -32,6 +32,22 @@ import {
|
|
|
32
32
|
union,
|
|
33
33
|
unknown
|
|
34
34
|
} from "./cli-z2krvkcq.js";
|
|
35
|
+
import {
|
|
36
|
+
AgentBrowser,
|
|
37
|
+
authenticate
|
|
38
|
+
} from "./cli-b26q1e27.js";
|
|
39
|
+
import {
|
|
40
|
+
ensureDir,
|
|
41
|
+
writeFile
|
|
42
|
+
} from "./cli-zvk8gwe4.js";
|
|
43
|
+
import {
|
|
44
|
+
buildTopology,
|
|
45
|
+
generateIndexHtml,
|
|
46
|
+
generateReadme,
|
|
47
|
+
generateSectionMarkdown,
|
|
48
|
+
normalizeUrl,
|
|
49
|
+
urlToSlug
|
|
50
|
+
} from "./cli-6qr9gvkp.js";
|
|
35
51
|
import {
|
|
36
52
|
__commonJS,
|
|
37
53
|
__require,
|
|
@@ -6505,9 +6521,2861 @@ var require_dist = __commonJS((exports, module) => {
|
|
|
6505
6521
|
for (const f of list)
|
|
6506
6522
|
ajv.addFormat(f, fs[f]);
|
|
6507
6523
|
}
|
|
6508
|
-
module.exports = exports = formatsPlugin;
|
|
6509
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6510
|
-
exports.default = formatsPlugin;
|
|
6524
|
+
module.exports = exports = formatsPlugin;
|
|
6525
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6526
|
+
exports.default = formatsPlugin;
|
|
6527
|
+
});
|
|
6528
|
+
|
|
6529
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/constants.js
|
|
6530
|
+
var require_constants = __commonJS((exports, module) => {
|
|
6531
|
+
var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
6532
|
+
var hasBlob = typeof Blob !== "undefined";
|
|
6533
|
+
if (hasBlob)
|
|
6534
|
+
BINARY_TYPES.push("blob");
|
|
6535
|
+
module.exports = {
|
|
6536
|
+
BINARY_TYPES,
|
|
6537
|
+
CLOSE_TIMEOUT: 30000,
|
|
6538
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
6539
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
6540
|
+
hasBlob,
|
|
6541
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
6542
|
+
kListener: Symbol("kListener"),
|
|
6543
|
+
kStatusCode: Symbol("status-code"),
|
|
6544
|
+
kWebSocket: Symbol("websocket"),
|
|
6545
|
+
NOOP: () => {}
|
|
6546
|
+
};
|
|
6547
|
+
});
|
|
6548
|
+
|
|
6549
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/buffer-util.js
|
|
6550
|
+
var require_buffer_util = __commonJS((exports, module) => {
|
|
6551
|
+
var { EMPTY_BUFFER } = require_constants();
|
|
6552
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
6553
|
+
function concat(list, totalLength) {
|
|
6554
|
+
if (list.length === 0)
|
|
6555
|
+
return EMPTY_BUFFER;
|
|
6556
|
+
if (list.length === 1)
|
|
6557
|
+
return list[0];
|
|
6558
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
6559
|
+
let offset = 0;
|
|
6560
|
+
for (let i = 0;i < list.length; i++) {
|
|
6561
|
+
const buf = list[i];
|
|
6562
|
+
target.set(buf, offset);
|
|
6563
|
+
offset += buf.length;
|
|
6564
|
+
}
|
|
6565
|
+
if (offset < totalLength) {
|
|
6566
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
6567
|
+
}
|
|
6568
|
+
return target;
|
|
6569
|
+
}
|
|
6570
|
+
function _mask(source, mask, output, offset, length) {
|
|
6571
|
+
for (let i = 0;i < length; i++) {
|
|
6572
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
6573
|
+
}
|
|
6574
|
+
}
|
|
6575
|
+
function _unmask(buffer, mask) {
|
|
6576
|
+
for (let i = 0;i < buffer.length; i++) {
|
|
6577
|
+
buffer[i] ^= mask[i & 3];
|
|
6578
|
+
}
|
|
6579
|
+
}
|
|
6580
|
+
function toArrayBuffer(buf) {
|
|
6581
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
6582
|
+
return buf.buffer;
|
|
6583
|
+
}
|
|
6584
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
6585
|
+
}
|
|
6586
|
+
function toBuffer(data) {
|
|
6587
|
+
toBuffer.readOnly = true;
|
|
6588
|
+
if (Buffer.isBuffer(data))
|
|
6589
|
+
return data;
|
|
6590
|
+
let buf;
|
|
6591
|
+
if (data instanceof ArrayBuffer) {
|
|
6592
|
+
buf = new FastBuffer(data);
|
|
6593
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
6594
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
6595
|
+
} else {
|
|
6596
|
+
buf = Buffer.from(data);
|
|
6597
|
+
toBuffer.readOnly = false;
|
|
6598
|
+
}
|
|
6599
|
+
return buf;
|
|
6600
|
+
}
|
|
6601
|
+
module.exports = {
|
|
6602
|
+
concat,
|
|
6603
|
+
mask: _mask,
|
|
6604
|
+
toArrayBuffer,
|
|
6605
|
+
toBuffer,
|
|
6606
|
+
unmask: _unmask
|
|
6607
|
+
};
|
|
6608
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
6609
|
+
try {
|
|
6610
|
+
const bufferUtil = (()=>{throw new Error("Cannot require module "+"bufferutil");})();
|
|
6611
|
+
module.exports.mask = function(source, mask, output, offset, length) {
|
|
6612
|
+
if (length < 48)
|
|
6613
|
+
_mask(source, mask, output, offset, length);
|
|
6614
|
+
else
|
|
6615
|
+
bufferUtil.mask(source, mask, output, offset, length);
|
|
6616
|
+
};
|
|
6617
|
+
module.exports.unmask = function(buffer, mask) {
|
|
6618
|
+
if (buffer.length < 32)
|
|
6619
|
+
_unmask(buffer, mask);
|
|
6620
|
+
else
|
|
6621
|
+
bufferUtil.unmask(buffer, mask);
|
|
6622
|
+
};
|
|
6623
|
+
} catch (e) {}
|
|
6624
|
+
}
|
|
6625
|
+
});
|
|
6626
|
+
|
|
6627
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/limiter.js
|
|
6628
|
+
var require_limiter = __commonJS((exports, module) => {
|
|
6629
|
+
var kDone = Symbol("kDone");
|
|
6630
|
+
var kRun = Symbol("kRun");
|
|
6631
|
+
|
|
6632
|
+
class Limiter {
|
|
6633
|
+
constructor(concurrency) {
|
|
6634
|
+
this[kDone] = () => {
|
|
6635
|
+
this.pending--;
|
|
6636
|
+
this[kRun]();
|
|
6637
|
+
};
|
|
6638
|
+
this.concurrency = concurrency || Infinity;
|
|
6639
|
+
this.jobs = [];
|
|
6640
|
+
this.pending = 0;
|
|
6641
|
+
}
|
|
6642
|
+
add(job) {
|
|
6643
|
+
this.jobs.push(job);
|
|
6644
|
+
this[kRun]();
|
|
6645
|
+
}
|
|
6646
|
+
[kRun]() {
|
|
6647
|
+
if (this.pending === this.concurrency)
|
|
6648
|
+
return;
|
|
6649
|
+
if (this.jobs.length) {
|
|
6650
|
+
const job = this.jobs.shift();
|
|
6651
|
+
this.pending++;
|
|
6652
|
+
job(this[kDone]);
|
|
6653
|
+
}
|
|
6654
|
+
}
|
|
6655
|
+
}
|
|
6656
|
+
module.exports = Limiter;
|
|
6657
|
+
});
|
|
6658
|
+
|
|
6659
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/permessage-deflate.js
|
|
6660
|
+
var require_permessage_deflate = __commonJS((exports, module) => {
|
|
6661
|
+
var zlib = __require("zlib");
|
|
6662
|
+
var bufferUtil = require_buffer_util();
|
|
6663
|
+
var Limiter = require_limiter();
|
|
6664
|
+
var { kStatusCode } = require_constants();
|
|
6665
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
6666
|
+
var TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
6667
|
+
var kPerMessageDeflate = Symbol("permessage-deflate");
|
|
6668
|
+
var kTotalLength = Symbol("total-length");
|
|
6669
|
+
var kCallback = Symbol("callback");
|
|
6670
|
+
var kBuffers = Symbol("buffers");
|
|
6671
|
+
var kError = Symbol("error");
|
|
6672
|
+
var zlibLimiter;
|
|
6673
|
+
|
|
6674
|
+
class PerMessageDeflate {
|
|
6675
|
+
constructor(options) {
|
|
6676
|
+
this._options = options || {};
|
|
6677
|
+
this._threshold = this._options.threshold !== undefined ? this._options.threshold : 1024;
|
|
6678
|
+
this._maxPayload = this._options.maxPayload | 0;
|
|
6679
|
+
this._isServer = !!this._options.isServer;
|
|
6680
|
+
this._deflate = null;
|
|
6681
|
+
this._inflate = null;
|
|
6682
|
+
this.params = null;
|
|
6683
|
+
if (!zlibLimiter) {
|
|
6684
|
+
const concurrency = this._options.concurrencyLimit !== undefined ? this._options.concurrencyLimit : 10;
|
|
6685
|
+
zlibLimiter = new Limiter(concurrency);
|
|
6686
|
+
}
|
|
6687
|
+
}
|
|
6688
|
+
static get extensionName() {
|
|
6689
|
+
return "permessage-deflate";
|
|
6690
|
+
}
|
|
6691
|
+
offer() {
|
|
6692
|
+
const params = {};
|
|
6693
|
+
if (this._options.serverNoContextTakeover) {
|
|
6694
|
+
params.server_no_context_takeover = true;
|
|
6695
|
+
}
|
|
6696
|
+
if (this._options.clientNoContextTakeover) {
|
|
6697
|
+
params.client_no_context_takeover = true;
|
|
6698
|
+
}
|
|
6699
|
+
if (this._options.serverMaxWindowBits) {
|
|
6700
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
6701
|
+
}
|
|
6702
|
+
if (this._options.clientMaxWindowBits) {
|
|
6703
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
6704
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
6705
|
+
params.client_max_window_bits = true;
|
|
6706
|
+
}
|
|
6707
|
+
return params;
|
|
6708
|
+
}
|
|
6709
|
+
accept(configurations) {
|
|
6710
|
+
configurations = this.normalizeParams(configurations);
|
|
6711
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
6712
|
+
return this.params;
|
|
6713
|
+
}
|
|
6714
|
+
cleanup() {
|
|
6715
|
+
if (this._inflate) {
|
|
6716
|
+
this._inflate.close();
|
|
6717
|
+
this._inflate = null;
|
|
6718
|
+
}
|
|
6719
|
+
if (this._deflate) {
|
|
6720
|
+
const callback = this._deflate[kCallback];
|
|
6721
|
+
this._deflate.close();
|
|
6722
|
+
this._deflate = null;
|
|
6723
|
+
if (callback) {
|
|
6724
|
+
callback(new Error("The deflate stream was closed while data was being processed"));
|
|
6725
|
+
}
|
|
6726
|
+
}
|
|
6727
|
+
}
|
|
6728
|
+
acceptAsServer(offers) {
|
|
6729
|
+
const opts = this._options;
|
|
6730
|
+
const accepted = offers.find((params) => {
|
|
6731
|
+
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) {
|
|
6732
|
+
return false;
|
|
6733
|
+
}
|
|
6734
|
+
return true;
|
|
6735
|
+
});
|
|
6736
|
+
if (!accepted) {
|
|
6737
|
+
throw new Error("None of the extension offers can be accepted");
|
|
6738
|
+
}
|
|
6739
|
+
if (opts.serverNoContextTakeover) {
|
|
6740
|
+
accepted.server_no_context_takeover = true;
|
|
6741
|
+
}
|
|
6742
|
+
if (opts.clientNoContextTakeover) {
|
|
6743
|
+
accepted.client_no_context_takeover = true;
|
|
6744
|
+
}
|
|
6745
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
6746
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
6747
|
+
}
|
|
6748
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
6749
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
6750
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
6751
|
+
delete accepted.client_max_window_bits;
|
|
6752
|
+
}
|
|
6753
|
+
return accepted;
|
|
6754
|
+
}
|
|
6755
|
+
acceptAsClient(response) {
|
|
6756
|
+
const params = response[0];
|
|
6757
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
6758
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
6759
|
+
}
|
|
6760
|
+
if (!params.client_max_window_bits) {
|
|
6761
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
6762
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
6763
|
+
}
|
|
6764
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
6765
|
+
throw new Error('Unexpected or invalid parameter "client_max_window_bits"');
|
|
6766
|
+
}
|
|
6767
|
+
return params;
|
|
6768
|
+
}
|
|
6769
|
+
normalizeParams(configurations) {
|
|
6770
|
+
configurations.forEach((params) => {
|
|
6771
|
+
Object.keys(params).forEach((key) => {
|
|
6772
|
+
let value = params[key];
|
|
6773
|
+
if (value.length > 1) {
|
|
6774
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
6775
|
+
}
|
|
6776
|
+
value = value[0];
|
|
6777
|
+
if (key === "client_max_window_bits") {
|
|
6778
|
+
if (value !== true) {
|
|
6779
|
+
const num = +value;
|
|
6780
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
6781
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
6782
|
+
}
|
|
6783
|
+
value = num;
|
|
6784
|
+
} else if (!this._isServer) {
|
|
6785
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
6786
|
+
}
|
|
6787
|
+
} else if (key === "server_max_window_bits") {
|
|
6788
|
+
const num = +value;
|
|
6789
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
6790
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
6791
|
+
}
|
|
6792
|
+
value = num;
|
|
6793
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
6794
|
+
if (value !== true) {
|
|
6795
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
6796
|
+
}
|
|
6797
|
+
} else {
|
|
6798
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
6799
|
+
}
|
|
6800
|
+
params[key] = value;
|
|
6801
|
+
});
|
|
6802
|
+
});
|
|
6803
|
+
return configurations;
|
|
6804
|
+
}
|
|
6805
|
+
decompress(data, fin, callback) {
|
|
6806
|
+
zlibLimiter.add((done) => {
|
|
6807
|
+
this._decompress(data, fin, (err, result) => {
|
|
6808
|
+
done();
|
|
6809
|
+
callback(err, result);
|
|
6810
|
+
});
|
|
6811
|
+
});
|
|
6812
|
+
}
|
|
6813
|
+
compress(data, fin, callback) {
|
|
6814
|
+
zlibLimiter.add((done) => {
|
|
6815
|
+
this._compress(data, fin, (err, result) => {
|
|
6816
|
+
done();
|
|
6817
|
+
callback(err, result);
|
|
6818
|
+
});
|
|
6819
|
+
});
|
|
6820
|
+
}
|
|
6821
|
+
_decompress(data, fin, callback) {
|
|
6822
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
6823
|
+
if (!this._inflate) {
|
|
6824
|
+
const key = `${endpoint}_max_window_bits`;
|
|
6825
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
6826
|
+
this._inflate = zlib.createInflateRaw({
|
|
6827
|
+
...this._options.zlibInflateOptions,
|
|
6828
|
+
windowBits
|
|
6829
|
+
});
|
|
6830
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
6831
|
+
this._inflate[kTotalLength] = 0;
|
|
6832
|
+
this._inflate[kBuffers] = [];
|
|
6833
|
+
this._inflate.on("error", inflateOnError);
|
|
6834
|
+
this._inflate.on("data", inflateOnData);
|
|
6835
|
+
}
|
|
6836
|
+
this._inflate[kCallback] = callback;
|
|
6837
|
+
this._inflate.write(data);
|
|
6838
|
+
if (fin)
|
|
6839
|
+
this._inflate.write(TRAILER);
|
|
6840
|
+
this._inflate.flush(() => {
|
|
6841
|
+
const err = this._inflate[kError];
|
|
6842
|
+
if (err) {
|
|
6843
|
+
this._inflate.close();
|
|
6844
|
+
this._inflate = null;
|
|
6845
|
+
callback(err);
|
|
6846
|
+
return;
|
|
6847
|
+
}
|
|
6848
|
+
const data2 = bufferUtil.concat(this._inflate[kBuffers], this._inflate[kTotalLength]);
|
|
6849
|
+
if (this._inflate._readableState.endEmitted) {
|
|
6850
|
+
this._inflate.close();
|
|
6851
|
+
this._inflate = null;
|
|
6852
|
+
} else {
|
|
6853
|
+
this._inflate[kTotalLength] = 0;
|
|
6854
|
+
this._inflate[kBuffers] = [];
|
|
6855
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
6856
|
+
this._inflate.reset();
|
|
6857
|
+
}
|
|
6858
|
+
}
|
|
6859
|
+
callback(null, data2);
|
|
6860
|
+
});
|
|
6861
|
+
}
|
|
6862
|
+
_compress(data, fin, callback) {
|
|
6863
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
6864
|
+
if (!this._deflate) {
|
|
6865
|
+
const key = `${endpoint}_max_window_bits`;
|
|
6866
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
6867
|
+
this._deflate = zlib.createDeflateRaw({
|
|
6868
|
+
...this._options.zlibDeflateOptions,
|
|
6869
|
+
windowBits
|
|
6870
|
+
});
|
|
6871
|
+
this._deflate[kTotalLength] = 0;
|
|
6872
|
+
this._deflate[kBuffers] = [];
|
|
6873
|
+
this._deflate.on("data", deflateOnData);
|
|
6874
|
+
}
|
|
6875
|
+
this._deflate[kCallback] = callback;
|
|
6876
|
+
this._deflate.write(data);
|
|
6877
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
6878
|
+
if (!this._deflate) {
|
|
6879
|
+
return;
|
|
6880
|
+
}
|
|
6881
|
+
let data2 = bufferUtil.concat(this._deflate[kBuffers], this._deflate[kTotalLength]);
|
|
6882
|
+
if (fin) {
|
|
6883
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
6884
|
+
}
|
|
6885
|
+
this._deflate[kCallback] = null;
|
|
6886
|
+
this._deflate[kTotalLength] = 0;
|
|
6887
|
+
this._deflate[kBuffers] = [];
|
|
6888
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
6889
|
+
this._deflate.reset();
|
|
6890
|
+
}
|
|
6891
|
+
callback(null, data2);
|
|
6892
|
+
});
|
|
6893
|
+
}
|
|
6894
|
+
}
|
|
6895
|
+
module.exports = PerMessageDeflate;
|
|
6896
|
+
function deflateOnData(chunk) {
|
|
6897
|
+
this[kBuffers].push(chunk);
|
|
6898
|
+
this[kTotalLength] += chunk.length;
|
|
6899
|
+
}
|
|
6900
|
+
function inflateOnData(chunk) {
|
|
6901
|
+
this[kTotalLength] += chunk.length;
|
|
6902
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
6903
|
+
this[kBuffers].push(chunk);
|
|
6904
|
+
return;
|
|
6905
|
+
}
|
|
6906
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
6907
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
6908
|
+
this[kError][kStatusCode] = 1009;
|
|
6909
|
+
this.removeListener("data", inflateOnData);
|
|
6910
|
+
this.reset();
|
|
6911
|
+
}
|
|
6912
|
+
function inflateOnError(err) {
|
|
6913
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
6914
|
+
if (this[kError]) {
|
|
6915
|
+
this[kCallback](this[kError]);
|
|
6916
|
+
return;
|
|
6917
|
+
}
|
|
6918
|
+
err[kStatusCode] = 1007;
|
|
6919
|
+
this[kCallback](err);
|
|
6920
|
+
}
|
|
6921
|
+
});
|
|
6922
|
+
|
|
6923
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/validation.js
|
|
6924
|
+
var require_validation2 = __commonJS((exports, module) => {
|
|
6925
|
+
var { isUtf8 } = __require("buffer");
|
|
6926
|
+
var { hasBlob } = require_constants();
|
|
6927
|
+
var tokenChars = [
|
|
6928
|
+
0,
|
|
6929
|
+
0,
|
|
6930
|
+
0,
|
|
6931
|
+
0,
|
|
6932
|
+
0,
|
|
6933
|
+
0,
|
|
6934
|
+
0,
|
|
6935
|
+
0,
|
|
6936
|
+
0,
|
|
6937
|
+
0,
|
|
6938
|
+
0,
|
|
6939
|
+
0,
|
|
6940
|
+
0,
|
|
6941
|
+
0,
|
|
6942
|
+
0,
|
|
6943
|
+
0,
|
|
6944
|
+
0,
|
|
6945
|
+
0,
|
|
6946
|
+
0,
|
|
6947
|
+
0,
|
|
6948
|
+
0,
|
|
6949
|
+
0,
|
|
6950
|
+
0,
|
|
6951
|
+
0,
|
|
6952
|
+
0,
|
|
6953
|
+
0,
|
|
6954
|
+
0,
|
|
6955
|
+
0,
|
|
6956
|
+
0,
|
|
6957
|
+
0,
|
|
6958
|
+
0,
|
|
6959
|
+
0,
|
|
6960
|
+
0,
|
|
6961
|
+
1,
|
|
6962
|
+
0,
|
|
6963
|
+
1,
|
|
6964
|
+
1,
|
|
6965
|
+
1,
|
|
6966
|
+
1,
|
|
6967
|
+
1,
|
|
6968
|
+
0,
|
|
6969
|
+
0,
|
|
6970
|
+
1,
|
|
6971
|
+
1,
|
|
6972
|
+
0,
|
|
6973
|
+
1,
|
|
6974
|
+
1,
|
|
6975
|
+
0,
|
|
6976
|
+
1,
|
|
6977
|
+
1,
|
|
6978
|
+
1,
|
|
6979
|
+
1,
|
|
6980
|
+
1,
|
|
6981
|
+
1,
|
|
6982
|
+
1,
|
|
6983
|
+
1,
|
|
6984
|
+
1,
|
|
6985
|
+
1,
|
|
6986
|
+
0,
|
|
6987
|
+
0,
|
|
6988
|
+
0,
|
|
6989
|
+
0,
|
|
6990
|
+
0,
|
|
6991
|
+
0,
|
|
6992
|
+
0,
|
|
6993
|
+
1,
|
|
6994
|
+
1,
|
|
6995
|
+
1,
|
|
6996
|
+
1,
|
|
6997
|
+
1,
|
|
6998
|
+
1,
|
|
6999
|
+
1,
|
|
7000
|
+
1,
|
|
7001
|
+
1,
|
|
7002
|
+
1,
|
|
7003
|
+
1,
|
|
7004
|
+
1,
|
|
7005
|
+
1,
|
|
7006
|
+
1,
|
|
7007
|
+
1,
|
|
7008
|
+
1,
|
|
7009
|
+
1,
|
|
7010
|
+
1,
|
|
7011
|
+
1,
|
|
7012
|
+
1,
|
|
7013
|
+
1,
|
|
7014
|
+
1,
|
|
7015
|
+
1,
|
|
7016
|
+
1,
|
|
7017
|
+
1,
|
|
7018
|
+
1,
|
|
7019
|
+
0,
|
|
7020
|
+
0,
|
|
7021
|
+
0,
|
|
7022
|
+
1,
|
|
7023
|
+
1,
|
|
7024
|
+
1,
|
|
7025
|
+
1,
|
|
7026
|
+
1,
|
|
7027
|
+
1,
|
|
7028
|
+
1,
|
|
7029
|
+
1,
|
|
7030
|
+
1,
|
|
7031
|
+
1,
|
|
7032
|
+
1,
|
|
7033
|
+
1,
|
|
7034
|
+
1,
|
|
7035
|
+
1,
|
|
7036
|
+
1,
|
|
7037
|
+
1,
|
|
7038
|
+
1,
|
|
7039
|
+
1,
|
|
7040
|
+
1,
|
|
7041
|
+
1,
|
|
7042
|
+
1,
|
|
7043
|
+
1,
|
|
7044
|
+
1,
|
|
7045
|
+
1,
|
|
7046
|
+
1,
|
|
7047
|
+
1,
|
|
7048
|
+
1,
|
|
7049
|
+
1,
|
|
7050
|
+
1,
|
|
7051
|
+
0,
|
|
7052
|
+
1,
|
|
7053
|
+
0,
|
|
7054
|
+
1,
|
|
7055
|
+
0
|
|
7056
|
+
];
|
|
7057
|
+
function isValidStatusCode(code) {
|
|
7058
|
+
return code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3000 && code <= 4999;
|
|
7059
|
+
}
|
|
7060
|
+
function _isValidUTF8(buf) {
|
|
7061
|
+
const len = buf.length;
|
|
7062
|
+
let i = 0;
|
|
7063
|
+
while (i < len) {
|
|
7064
|
+
if ((buf[i] & 128) === 0) {
|
|
7065
|
+
i++;
|
|
7066
|
+
} else if ((buf[i] & 224) === 192) {
|
|
7067
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) {
|
|
7068
|
+
return false;
|
|
7069
|
+
}
|
|
7070
|
+
i += 2;
|
|
7071
|
+
} else if ((buf[i] & 240) === 224) {
|
|
7072
|
+
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) {
|
|
7073
|
+
return false;
|
|
7074
|
+
}
|
|
7075
|
+
i += 3;
|
|
7076
|
+
} else if ((buf[i] & 248) === 240) {
|
|
7077
|
+
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) {
|
|
7078
|
+
return false;
|
|
7079
|
+
}
|
|
7080
|
+
i += 4;
|
|
7081
|
+
} else {
|
|
7082
|
+
return false;
|
|
7083
|
+
}
|
|
7084
|
+
}
|
|
7085
|
+
return true;
|
|
7086
|
+
}
|
|
7087
|
+
function isBlob(value) {
|
|
7088
|
+
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");
|
|
7089
|
+
}
|
|
7090
|
+
module.exports = {
|
|
7091
|
+
isBlob,
|
|
7092
|
+
isValidStatusCode,
|
|
7093
|
+
isValidUTF8: _isValidUTF8,
|
|
7094
|
+
tokenChars
|
|
7095
|
+
};
|
|
7096
|
+
if (isUtf8) {
|
|
7097
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
7098
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
7099
|
+
};
|
|
7100
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
7101
|
+
try {
|
|
7102
|
+
const isValidUTF8 = (()=>{throw new Error("Cannot require module "+"utf-8-validate");})();
|
|
7103
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
7104
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
7105
|
+
};
|
|
7106
|
+
} catch (e) {}
|
|
7107
|
+
}
|
|
7108
|
+
});
|
|
7109
|
+
|
|
7110
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/receiver.js
|
|
7111
|
+
var require_receiver = __commonJS((exports, module) => {
|
|
7112
|
+
var { Writable } = __require("stream");
|
|
7113
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
7114
|
+
var {
|
|
7115
|
+
BINARY_TYPES,
|
|
7116
|
+
EMPTY_BUFFER,
|
|
7117
|
+
kStatusCode,
|
|
7118
|
+
kWebSocket
|
|
7119
|
+
} = require_constants();
|
|
7120
|
+
var { concat, toArrayBuffer, unmask } = require_buffer_util();
|
|
7121
|
+
var { isValidStatusCode, isValidUTF8 } = require_validation2();
|
|
7122
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
7123
|
+
var GET_INFO = 0;
|
|
7124
|
+
var GET_PAYLOAD_LENGTH_16 = 1;
|
|
7125
|
+
var GET_PAYLOAD_LENGTH_64 = 2;
|
|
7126
|
+
var GET_MASK = 3;
|
|
7127
|
+
var GET_DATA = 4;
|
|
7128
|
+
var INFLATING = 5;
|
|
7129
|
+
var DEFER_EVENT = 6;
|
|
7130
|
+
|
|
7131
|
+
class Receiver extends Writable {
|
|
7132
|
+
constructor(options = {}) {
|
|
7133
|
+
super();
|
|
7134
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== undefined ? options.allowSynchronousEvents : true;
|
|
7135
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
7136
|
+
this._extensions = options.extensions || {};
|
|
7137
|
+
this._isServer = !!options.isServer;
|
|
7138
|
+
this._maxPayload = options.maxPayload | 0;
|
|
7139
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
7140
|
+
this[kWebSocket] = undefined;
|
|
7141
|
+
this._bufferedBytes = 0;
|
|
7142
|
+
this._buffers = [];
|
|
7143
|
+
this._compressed = false;
|
|
7144
|
+
this._payloadLength = 0;
|
|
7145
|
+
this._mask = undefined;
|
|
7146
|
+
this._fragmented = 0;
|
|
7147
|
+
this._masked = false;
|
|
7148
|
+
this._fin = false;
|
|
7149
|
+
this._opcode = 0;
|
|
7150
|
+
this._totalPayloadLength = 0;
|
|
7151
|
+
this._messageLength = 0;
|
|
7152
|
+
this._fragments = [];
|
|
7153
|
+
this._errored = false;
|
|
7154
|
+
this._loop = false;
|
|
7155
|
+
this._state = GET_INFO;
|
|
7156
|
+
}
|
|
7157
|
+
_write(chunk, encoding, cb) {
|
|
7158
|
+
if (this._opcode === 8 && this._state == GET_INFO)
|
|
7159
|
+
return cb();
|
|
7160
|
+
this._bufferedBytes += chunk.length;
|
|
7161
|
+
this._buffers.push(chunk);
|
|
7162
|
+
this.startLoop(cb);
|
|
7163
|
+
}
|
|
7164
|
+
consume(n) {
|
|
7165
|
+
this._bufferedBytes -= n;
|
|
7166
|
+
if (n === this._buffers[0].length)
|
|
7167
|
+
return this._buffers.shift();
|
|
7168
|
+
if (n < this._buffers[0].length) {
|
|
7169
|
+
const buf = this._buffers[0];
|
|
7170
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
7171
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
7172
|
+
}
|
|
7173
|
+
const dst = Buffer.allocUnsafe(n);
|
|
7174
|
+
do {
|
|
7175
|
+
const buf = this._buffers[0];
|
|
7176
|
+
const offset = dst.length - n;
|
|
7177
|
+
if (n >= buf.length) {
|
|
7178
|
+
dst.set(this._buffers.shift(), offset);
|
|
7179
|
+
} else {
|
|
7180
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
7181
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
7182
|
+
}
|
|
7183
|
+
n -= buf.length;
|
|
7184
|
+
} while (n > 0);
|
|
7185
|
+
return dst;
|
|
7186
|
+
}
|
|
7187
|
+
startLoop(cb) {
|
|
7188
|
+
this._loop = true;
|
|
7189
|
+
do {
|
|
7190
|
+
switch (this._state) {
|
|
7191
|
+
case GET_INFO:
|
|
7192
|
+
this.getInfo(cb);
|
|
7193
|
+
break;
|
|
7194
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
7195
|
+
this.getPayloadLength16(cb);
|
|
7196
|
+
break;
|
|
7197
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
7198
|
+
this.getPayloadLength64(cb);
|
|
7199
|
+
break;
|
|
7200
|
+
case GET_MASK:
|
|
7201
|
+
this.getMask();
|
|
7202
|
+
break;
|
|
7203
|
+
case GET_DATA:
|
|
7204
|
+
this.getData(cb);
|
|
7205
|
+
break;
|
|
7206
|
+
case INFLATING:
|
|
7207
|
+
case DEFER_EVENT:
|
|
7208
|
+
this._loop = false;
|
|
7209
|
+
return;
|
|
7210
|
+
}
|
|
7211
|
+
} while (this._loop);
|
|
7212
|
+
if (!this._errored)
|
|
7213
|
+
cb();
|
|
7214
|
+
}
|
|
7215
|
+
getInfo(cb) {
|
|
7216
|
+
if (this._bufferedBytes < 2) {
|
|
7217
|
+
this._loop = false;
|
|
7218
|
+
return;
|
|
7219
|
+
}
|
|
7220
|
+
const buf = this.consume(2);
|
|
7221
|
+
if ((buf[0] & 48) !== 0) {
|
|
7222
|
+
const error = this.createError(RangeError, "RSV2 and RSV3 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_2_3");
|
|
7223
|
+
cb(error);
|
|
7224
|
+
return;
|
|
7225
|
+
}
|
|
7226
|
+
const compressed = (buf[0] & 64) === 64;
|
|
7227
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
7228
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
7229
|
+
cb(error);
|
|
7230
|
+
return;
|
|
7231
|
+
}
|
|
7232
|
+
this._fin = (buf[0] & 128) === 128;
|
|
7233
|
+
this._opcode = buf[0] & 15;
|
|
7234
|
+
this._payloadLength = buf[1] & 127;
|
|
7235
|
+
if (this._opcode === 0) {
|
|
7236
|
+
if (compressed) {
|
|
7237
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
7238
|
+
cb(error);
|
|
7239
|
+
return;
|
|
7240
|
+
}
|
|
7241
|
+
if (!this._fragmented) {
|
|
7242
|
+
const error = this.createError(RangeError, "invalid opcode 0", true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
7243
|
+
cb(error);
|
|
7244
|
+
return;
|
|
7245
|
+
}
|
|
7246
|
+
this._opcode = this._fragmented;
|
|
7247
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
7248
|
+
if (this._fragmented) {
|
|
7249
|
+
const error = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
7250
|
+
cb(error);
|
|
7251
|
+
return;
|
|
7252
|
+
}
|
|
7253
|
+
this._compressed = compressed;
|
|
7254
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
7255
|
+
if (!this._fin) {
|
|
7256
|
+
const error = this.createError(RangeError, "FIN must be set", true, 1002, "WS_ERR_EXPECTED_FIN");
|
|
7257
|
+
cb(error);
|
|
7258
|
+
return;
|
|
7259
|
+
}
|
|
7260
|
+
if (compressed) {
|
|
7261
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
7262
|
+
cb(error);
|
|
7263
|
+
return;
|
|
7264
|
+
}
|
|
7265
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
7266
|
+
const error = this.createError(RangeError, `invalid payload length ${this._payloadLength}`, true, 1002, "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");
|
|
7267
|
+
cb(error);
|
|
7268
|
+
return;
|
|
7269
|
+
}
|
|
7270
|
+
} else {
|
|
7271
|
+
const error = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
7272
|
+
cb(error);
|
|
7273
|
+
return;
|
|
7274
|
+
}
|
|
7275
|
+
if (!this._fin && !this._fragmented)
|
|
7276
|
+
this._fragmented = this._opcode;
|
|
7277
|
+
this._masked = (buf[1] & 128) === 128;
|
|
7278
|
+
if (this._isServer) {
|
|
7279
|
+
if (!this._masked) {
|
|
7280
|
+
const error = this.createError(RangeError, "MASK must be set", true, 1002, "WS_ERR_EXPECTED_MASK");
|
|
7281
|
+
cb(error);
|
|
7282
|
+
return;
|
|
7283
|
+
}
|
|
7284
|
+
} else if (this._masked) {
|
|
7285
|
+
const error = this.createError(RangeError, "MASK must be clear", true, 1002, "WS_ERR_UNEXPECTED_MASK");
|
|
7286
|
+
cb(error);
|
|
7287
|
+
return;
|
|
7288
|
+
}
|
|
7289
|
+
if (this._payloadLength === 126)
|
|
7290
|
+
this._state = GET_PAYLOAD_LENGTH_16;
|
|
7291
|
+
else if (this._payloadLength === 127)
|
|
7292
|
+
this._state = GET_PAYLOAD_LENGTH_64;
|
|
7293
|
+
else
|
|
7294
|
+
this.haveLength(cb);
|
|
7295
|
+
}
|
|
7296
|
+
getPayloadLength16(cb) {
|
|
7297
|
+
if (this._bufferedBytes < 2) {
|
|
7298
|
+
this._loop = false;
|
|
7299
|
+
return;
|
|
7300
|
+
}
|
|
7301
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
7302
|
+
this.haveLength(cb);
|
|
7303
|
+
}
|
|
7304
|
+
getPayloadLength64(cb) {
|
|
7305
|
+
if (this._bufferedBytes < 8) {
|
|
7306
|
+
this._loop = false;
|
|
7307
|
+
return;
|
|
7308
|
+
}
|
|
7309
|
+
const buf = this.consume(8);
|
|
7310
|
+
const num = buf.readUInt32BE(0);
|
|
7311
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
7312
|
+
const error = this.createError(RangeError, "Unsupported WebSocket frame: payload length > 2^53 - 1", false, 1009, "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");
|
|
7313
|
+
cb(error);
|
|
7314
|
+
return;
|
|
7315
|
+
}
|
|
7316
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
7317
|
+
this.haveLength(cb);
|
|
7318
|
+
}
|
|
7319
|
+
haveLength(cb) {
|
|
7320
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
7321
|
+
this._totalPayloadLength += this._payloadLength;
|
|
7322
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
7323
|
+
const error = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
7324
|
+
cb(error);
|
|
7325
|
+
return;
|
|
7326
|
+
}
|
|
7327
|
+
}
|
|
7328
|
+
if (this._masked)
|
|
7329
|
+
this._state = GET_MASK;
|
|
7330
|
+
else
|
|
7331
|
+
this._state = GET_DATA;
|
|
7332
|
+
}
|
|
7333
|
+
getMask() {
|
|
7334
|
+
if (this._bufferedBytes < 4) {
|
|
7335
|
+
this._loop = false;
|
|
7336
|
+
return;
|
|
7337
|
+
}
|
|
7338
|
+
this._mask = this.consume(4);
|
|
7339
|
+
this._state = GET_DATA;
|
|
7340
|
+
}
|
|
7341
|
+
getData(cb) {
|
|
7342
|
+
let data = EMPTY_BUFFER;
|
|
7343
|
+
if (this._payloadLength) {
|
|
7344
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
7345
|
+
this._loop = false;
|
|
7346
|
+
return;
|
|
7347
|
+
}
|
|
7348
|
+
data = this.consume(this._payloadLength);
|
|
7349
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
7350
|
+
unmask(data, this._mask);
|
|
7351
|
+
}
|
|
7352
|
+
}
|
|
7353
|
+
if (this._opcode > 7) {
|
|
7354
|
+
this.controlMessage(data, cb);
|
|
7355
|
+
return;
|
|
7356
|
+
}
|
|
7357
|
+
if (this._compressed) {
|
|
7358
|
+
this._state = INFLATING;
|
|
7359
|
+
this.decompress(data, cb);
|
|
7360
|
+
return;
|
|
7361
|
+
}
|
|
7362
|
+
if (data.length) {
|
|
7363
|
+
this._messageLength = this._totalPayloadLength;
|
|
7364
|
+
this._fragments.push(data);
|
|
7365
|
+
}
|
|
7366
|
+
this.dataMessage(cb);
|
|
7367
|
+
}
|
|
7368
|
+
decompress(data, cb) {
|
|
7369
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
7370
|
+
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
|
7371
|
+
if (err)
|
|
7372
|
+
return cb(err);
|
|
7373
|
+
if (buf.length) {
|
|
7374
|
+
this._messageLength += buf.length;
|
|
7375
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
7376
|
+
const error = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
7377
|
+
cb(error);
|
|
7378
|
+
return;
|
|
7379
|
+
}
|
|
7380
|
+
this._fragments.push(buf);
|
|
7381
|
+
}
|
|
7382
|
+
this.dataMessage(cb);
|
|
7383
|
+
if (this._state === GET_INFO)
|
|
7384
|
+
this.startLoop(cb);
|
|
7385
|
+
});
|
|
7386
|
+
}
|
|
7387
|
+
dataMessage(cb) {
|
|
7388
|
+
if (!this._fin) {
|
|
7389
|
+
this._state = GET_INFO;
|
|
7390
|
+
return;
|
|
7391
|
+
}
|
|
7392
|
+
const messageLength = this._messageLength;
|
|
7393
|
+
const fragments = this._fragments;
|
|
7394
|
+
this._totalPayloadLength = 0;
|
|
7395
|
+
this._messageLength = 0;
|
|
7396
|
+
this._fragmented = 0;
|
|
7397
|
+
this._fragments = [];
|
|
7398
|
+
if (this._opcode === 2) {
|
|
7399
|
+
let data;
|
|
7400
|
+
if (this._binaryType === "nodebuffer") {
|
|
7401
|
+
data = concat(fragments, messageLength);
|
|
7402
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
7403
|
+
data = toArrayBuffer(concat(fragments, messageLength));
|
|
7404
|
+
} else if (this._binaryType === "blob") {
|
|
7405
|
+
data = new Blob(fragments);
|
|
7406
|
+
} else {
|
|
7407
|
+
data = fragments;
|
|
7408
|
+
}
|
|
7409
|
+
if (this._allowSynchronousEvents) {
|
|
7410
|
+
this.emit("message", data, true);
|
|
7411
|
+
this._state = GET_INFO;
|
|
7412
|
+
} else {
|
|
7413
|
+
this._state = DEFER_EVENT;
|
|
7414
|
+
setImmediate(() => {
|
|
7415
|
+
this.emit("message", data, true);
|
|
7416
|
+
this._state = GET_INFO;
|
|
7417
|
+
this.startLoop(cb);
|
|
7418
|
+
});
|
|
7419
|
+
}
|
|
7420
|
+
} else {
|
|
7421
|
+
const buf = concat(fragments, messageLength);
|
|
7422
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
7423
|
+
const error = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
7424
|
+
cb(error);
|
|
7425
|
+
return;
|
|
7426
|
+
}
|
|
7427
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
7428
|
+
this.emit("message", buf, false);
|
|
7429
|
+
this._state = GET_INFO;
|
|
7430
|
+
} else {
|
|
7431
|
+
this._state = DEFER_EVENT;
|
|
7432
|
+
setImmediate(() => {
|
|
7433
|
+
this.emit("message", buf, false);
|
|
7434
|
+
this._state = GET_INFO;
|
|
7435
|
+
this.startLoop(cb);
|
|
7436
|
+
});
|
|
7437
|
+
}
|
|
7438
|
+
}
|
|
7439
|
+
}
|
|
7440
|
+
controlMessage(data, cb) {
|
|
7441
|
+
if (this._opcode === 8) {
|
|
7442
|
+
if (data.length === 0) {
|
|
7443
|
+
this._loop = false;
|
|
7444
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
7445
|
+
this.end();
|
|
7446
|
+
} else {
|
|
7447
|
+
const code = data.readUInt16BE(0);
|
|
7448
|
+
if (!isValidStatusCode(code)) {
|
|
7449
|
+
const error = this.createError(RangeError, `invalid status code ${code}`, true, 1002, "WS_ERR_INVALID_CLOSE_CODE");
|
|
7450
|
+
cb(error);
|
|
7451
|
+
return;
|
|
7452
|
+
}
|
|
7453
|
+
const buf = new FastBuffer(data.buffer, data.byteOffset + 2, data.length - 2);
|
|
7454
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
7455
|
+
const error = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
7456
|
+
cb(error);
|
|
7457
|
+
return;
|
|
7458
|
+
}
|
|
7459
|
+
this._loop = false;
|
|
7460
|
+
this.emit("conclude", code, buf);
|
|
7461
|
+
this.end();
|
|
7462
|
+
}
|
|
7463
|
+
this._state = GET_INFO;
|
|
7464
|
+
return;
|
|
7465
|
+
}
|
|
7466
|
+
if (this._allowSynchronousEvents) {
|
|
7467
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
7468
|
+
this._state = GET_INFO;
|
|
7469
|
+
} else {
|
|
7470
|
+
this._state = DEFER_EVENT;
|
|
7471
|
+
setImmediate(() => {
|
|
7472
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
7473
|
+
this._state = GET_INFO;
|
|
7474
|
+
this.startLoop(cb);
|
|
7475
|
+
});
|
|
7476
|
+
}
|
|
7477
|
+
}
|
|
7478
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
7479
|
+
this._loop = false;
|
|
7480
|
+
this._errored = true;
|
|
7481
|
+
const err = new ErrorCtor(prefix ? `Invalid WebSocket frame: ${message}` : message);
|
|
7482
|
+
Error.captureStackTrace(err, this.createError);
|
|
7483
|
+
err.code = errorCode;
|
|
7484
|
+
err[kStatusCode] = statusCode;
|
|
7485
|
+
return err;
|
|
7486
|
+
}
|
|
7487
|
+
}
|
|
7488
|
+
module.exports = Receiver;
|
|
7489
|
+
});
|
|
7490
|
+
|
|
7491
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/sender.js
|
|
7492
|
+
var require_sender = __commonJS((exports, module) => {
|
|
7493
|
+
var { Duplex } = __require("stream");
|
|
7494
|
+
var { randomFillSync } = __require("crypto");
|
|
7495
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
7496
|
+
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
|
|
7497
|
+
var { isBlob, isValidStatusCode } = require_validation2();
|
|
7498
|
+
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
7499
|
+
var kByteLength = Symbol("kByteLength");
|
|
7500
|
+
var maskBuffer = Buffer.alloc(4);
|
|
7501
|
+
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
7502
|
+
var randomPool;
|
|
7503
|
+
var randomPoolPointer = RANDOM_POOL_SIZE;
|
|
7504
|
+
var DEFAULT = 0;
|
|
7505
|
+
var DEFLATING = 1;
|
|
7506
|
+
var GET_BLOB_DATA = 2;
|
|
7507
|
+
|
|
7508
|
+
class Sender {
|
|
7509
|
+
constructor(socket, extensions, generateMask) {
|
|
7510
|
+
this._extensions = extensions || {};
|
|
7511
|
+
if (generateMask) {
|
|
7512
|
+
this._generateMask = generateMask;
|
|
7513
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
7514
|
+
}
|
|
7515
|
+
this._socket = socket;
|
|
7516
|
+
this._firstFragment = true;
|
|
7517
|
+
this._compress = false;
|
|
7518
|
+
this._bufferedBytes = 0;
|
|
7519
|
+
this._queue = [];
|
|
7520
|
+
this._state = DEFAULT;
|
|
7521
|
+
this.onerror = NOOP;
|
|
7522
|
+
this[kWebSocket] = undefined;
|
|
7523
|
+
}
|
|
7524
|
+
static frame(data, options) {
|
|
7525
|
+
let mask;
|
|
7526
|
+
let merge = false;
|
|
7527
|
+
let offset = 2;
|
|
7528
|
+
let skipMasking = false;
|
|
7529
|
+
if (options.mask) {
|
|
7530
|
+
mask = options.maskBuffer || maskBuffer;
|
|
7531
|
+
if (options.generateMask) {
|
|
7532
|
+
options.generateMask(mask);
|
|
7533
|
+
} else {
|
|
7534
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
7535
|
+
if (randomPool === undefined) {
|
|
7536
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
7537
|
+
}
|
|
7538
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
7539
|
+
randomPoolPointer = 0;
|
|
7540
|
+
}
|
|
7541
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
7542
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
7543
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
7544
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
7545
|
+
}
|
|
7546
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
7547
|
+
offset = 6;
|
|
7548
|
+
}
|
|
7549
|
+
let dataLength;
|
|
7550
|
+
if (typeof data === "string") {
|
|
7551
|
+
if ((!options.mask || skipMasking) && options[kByteLength] !== undefined) {
|
|
7552
|
+
dataLength = options[kByteLength];
|
|
7553
|
+
} else {
|
|
7554
|
+
data = Buffer.from(data);
|
|
7555
|
+
dataLength = data.length;
|
|
7556
|
+
}
|
|
7557
|
+
} else {
|
|
7558
|
+
dataLength = data.length;
|
|
7559
|
+
merge = options.mask && options.readOnly && !skipMasking;
|
|
7560
|
+
}
|
|
7561
|
+
let payloadLength = dataLength;
|
|
7562
|
+
if (dataLength >= 65536) {
|
|
7563
|
+
offset += 8;
|
|
7564
|
+
payloadLength = 127;
|
|
7565
|
+
} else if (dataLength > 125) {
|
|
7566
|
+
offset += 2;
|
|
7567
|
+
payloadLength = 126;
|
|
7568
|
+
}
|
|
7569
|
+
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
|
7570
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
7571
|
+
if (options.rsv1)
|
|
7572
|
+
target[0] |= 64;
|
|
7573
|
+
target[1] = payloadLength;
|
|
7574
|
+
if (payloadLength === 126) {
|
|
7575
|
+
target.writeUInt16BE(dataLength, 2);
|
|
7576
|
+
} else if (payloadLength === 127) {
|
|
7577
|
+
target[2] = target[3] = 0;
|
|
7578
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
7579
|
+
}
|
|
7580
|
+
if (!options.mask)
|
|
7581
|
+
return [target, data];
|
|
7582
|
+
target[1] |= 128;
|
|
7583
|
+
target[offset - 4] = mask[0];
|
|
7584
|
+
target[offset - 3] = mask[1];
|
|
7585
|
+
target[offset - 2] = mask[2];
|
|
7586
|
+
target[offset - 1] = mask[3];
|
|
7587
|
+
if (skipMasking)
|
|
7588
|
+
return [target, data];
|
|
7589
|
+
if (merge) {
|
|
7590
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
7591
|
+
return [target];
|
|
7592
|
+
}
|
|
7593
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
7594
|
+
return [target, data];
|
|
7595
|
+
}
|
|
7596
|
+
close(code, data, mask, cb) {
|
|
7597
|
+
let buf;
|
|
7598
|
+
if (code === undefined) {
|
|
7599
|
+
buf = EMPTY_BUFFER;
|
|
7600
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
7601
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
7602
|
+
} else if (data === undefined || !data.length) {
|
|
7603
|
+
buf = Buffer.allocUnsafe(2);
|
|
7604
|
+
buf.writeUInt16BE(code, 0);
|
|
7605
|
+
} else {
|
|
7606
|
+
const length = Buffer.byteLength(data);
|
|
7607
|
+
if (length > 123) {
|
|
7608
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
7609
|
+
}
|
|
7610
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
7611
|
+
buf.writeUInt16BE(code, 0);
|
|
7612
|
+
if (typeof data === "string") {
|
|
7613
|
+
buf.write(data, 2);
|
|
7614
|
+
} else {
|
|
7615
|
+
buf.set(data, 2);
|
|
7616
|
+
}
|
|
7617
|
+
}
|
|
7618
|
+
const options = {
|
|
7619
|
+
[kByteLength]: buf.length,
|
|
7620
|
+
fin: true,
|
|
7621
|
+
generateMask: this._generateMask,
|
|
7622
|
+
mask,
|
|
7623
|
+
maskBuffer: this._maskBuffer,
|
|
7624
|
+
opcode: 8,
|
|
7625
|
+
readOnly: false,
|
|
7626
|
+
rsv1: false
|
|
7627
|
+
};
|
|
7628
|
+
if (this._state !== DEFAULT) {
|
|
7629
|
+
this.enqueue([this.dispatch, buf, false, options, cb]);
|
|
7630
|
+
} else {
|
|
7631
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
7632
|
+
}
|
|
7633
|
+
}
|
|
7634
|
+
ping(data, mask, cb) {
|
|
7635
|
+
let byteLength;
|
|
7636
|
+
let readOnly;
|
|
7637
|
+
if (typeof data === "string") {
|
|
7638
|
+
byteLength = Buffer.byteLength(data);
|
|
7639
|
+
readOnly = false;
|
|
7640
|
+
} else if (isBlob(data)) {
|
|
7641
|
+
byteLength = data.size;
|
|
7642
|
+
readOnly = false;
|
|
7643
|
+
} else {
|
|
7644
|
+
data = toBuffer(data);
|
|
7645
|
+
byteLength = data.length;
|
|
7646
|
+
readOnly = toBuffer.readOnly;
|
|
7647
|
+
}
|
|
7648
|
+
if (byteLength > 125) {
|
|
7649
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
7650
|
+
}
|
|
7651
|
+
const options = {
|
|
7652
|
+
[kByteLength]: byteLength,
|
|
7653
|
+
fin: true,
|
|
7654
|
+
generateMask: this._generateMask,
|
|
7655
|
+
mask,
|
|
7656
|
+
maskBuffer: this._maskBuffer,
|
|
7657
|
+
opcode: 9,
|
|
7658
|
+
readOnly,
|
|
7659
|
+
rsv1: false
|
|
7660
|
+
};
|
|
7661
|
+
if (isBlob(data)) {
|
|
7662
|
+
if (this._state !== DEFAULT) {
|
|
7663
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
7664
|
+
} else {
|
|
7665
|
+
this.getBlobData(data, false, options, cb);
|
|
7666
|
+
}
|
|
7667
|
+
} else if (this._state !== DEFAULT) {
|
|
7668
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
7669
|
+
} else {
|
|
7670
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
7671
|
+
}
|
|
7672
|
+
}
|
|
7673
|
+
pong(data, mask, cb) {
|
|
7674
|
+
let byteLength;
|
|
7675
|
+
let readOnly;
|
|
7676
|
+
if (typeof data === "string") {
|
|
7677
|
+
byteLength = Buffer.byteLength(data);
|
|
7678
|
+
readOnly = false;
|
|
7679
|
+
} else if (isBlob(data)) {
|
|
7680
|
+
byteLength = data.size;
|
|
7681
|
+
readOnly = false;
|
|
7682
|
+
} else {
|
|
7683
|
+
data = toBuffer(data);
|
|
7684
|
+
byteLength = data.length;
|
|
7685
|
+
readOnly = toBuffer.readOnly;
|
|
7686
|
+
}
|
|
7687
|
+
if (byteLength > 125) {
|
|
7688
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
7689
|
+
}
|
|
7690
|
+
const options = {
|
|
7691
|
+
[kByteLength]: byteLength,
|
|
7692
|
+
fin: true,
|
|
7693
|
+
generateMask: this._generateMask,
|
|
7694
|
+
mask,
|
|
7695
|
+
maskBuffer: this._maskBuffer,
|
|
7696
|
+
opcode: 10,
|
|
7697
|
+
readOnly,
|
|
7698
|
+
rsv1: false
|
|
7699
|
+
};
|
|
7700
|
+
if (isBlob(data)) {
|
|
7701
|
+
if (this._state !== DEFAULT) {
|
|
7702
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
7703
|
+
} else {
|
|
7704
|
+
this.getBlobData(data, false, options, cb);
|
|
7705
|
+
}
|
|
7706
|
+
} else if (this._state !== DEFAULT) {
|
|
7707
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
7708
|
+
} else {
|
|
7709
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
7710
|
+
}
|
|
7711
|
+
}
|
|
7712
|
+
send(data, options, cb) {
|
|
7713
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
7714
|
+
let opcode = options.binary ? 2 : 1;
|
|
7715
|
+
let rsv1 = options.compress;
|
|
7716
|
+
let byteLength;
|
|
7717
|
+
let readOnly;
|
|
7718
|
+
if (typeof data === "string") {
|
|
7719
|
+
byteLength = Buffer.byteLength(data);
|
|
7720
|
+
readOnly = false;
|
|
7721
|
+
} else if (isBlob(data)) {
|
|
7722
|
+
byteLength = data.size;
|
|
7723
|
+
readOnly = false;
|
|
7724
|
+
} else {
|
|
7725
|
+
data = toBuffer(data);
|
|
7726
|
+
byteLength = data.length;
|
|
7727
|
+
readOnly = toBuffer.readOnly;
|
|
7728
|
+
}
|
|
7729
|
+
if (this._firstFragment) {
|
|
7730
|
+
this._firstFragment = false;
|
|
7731
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
7732
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
7733
|
+
}
|
|
7734
|
+
this._compress = rsv1;
|
|
7735
|
+
} else {
|
|
7736
|
+
rsv1 = false;
|
|
7737
|
+
opcode = 0;
|
|
7738
|
+
}
|
|
7739
|
+
if (options.fin)
|
|
7740
|
+
this._firstFragment = true;
|
|
7741
|
+
const opts = {
|
|
7742
|
+
[kByteLength]: byteLength,
|
|
7743
|
+
fin: options.fin,
|
|
7744
|
+
generateMask: this._generateMask,
|
|
7745
|
+
mask: options.mask,
|
|
7746
|
+
maskBuffer: this._maskBuffer,
|
|
7747
|
+
opcode,
|
|
7748
|
+
readOnly,
|
|
7749
|
+
rsv1
|
|
7750
|
+
};
|
|
7751
|
+
if (isBlob(data)) {
|
|
7752
|
+
if (this._state !== DEFAULT) {
|
|
7753
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
7754
|
+
} else {
|
|
7755
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
7756
|
+
}
|
|
7757
|
+
} else if (this._state !== DEFAULT) {
|
|
7758
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
7759
|
+
} else {
|
|
7760
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
7761
|
+
}
|
|
7762
|
+
}
|
|
7763
|
+
getBlobData(blob, compress, options, cb) {
|
|
7764
|
+
this._bufferedBytes += options[kByteLength];
|
|
7765
|
+
this._state = GET_BLOB_DATA;
|
|
7766
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
7767
|
+
if (this._socket.destroyed) {
|
|
7768
|
+
const err = new Error("The socket was closed while the blob was being read");
|
|
7769
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
7770
|
+
return;
|
|
7771
|
+
}
|
|
7772
|
+
this._bufferedBytes -= options[kByteLength];
|
|
7773
|
+
const data = toBuffer(arrayBuffer);
|
|
7774
|
+
if (!compress) {
|
|
7775
|
+
this._state = DEFAULT;
|
|
7776
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
7777
|
+
this.dequeue();
|
|
7778
|
+
} else {
|
|
7779
|
+
this.dispatch(data, compress, options, cb);
|
|
7780
|
+
}
|
|
7781
|
+
}).catch((err) => {
|
|
7782
|
+
process.nextTick(onError, this, err, cb);
|
|
7783
|
+
});
|
|
7784
|
+
}
|
|
7785
|
+
dispatch(data, compress, options, cb) {
|
|
7786
|
+
if (!compress) {
|
|
7787
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
7788
|
+
return;
|
|
7789
|
+
}
|
|
7790
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
7791
|
+
this._bufferedBytes += options[kByteLength];
|
|
7792
|
+
this._state = DEFLATING;
|
|
7793
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
|
7794
|
+
if (this._socket.destroyed) {
|
|
7795
|
+
const err = new Error("The socket was closed while data was being compressed");
|
|
7796
|
+
callCallbacks(this, err, cb);
|
|
7797
|
+
return;
|
|
7798
|
+
}
|
|
7799
|
+
this._bufferedBytes -= options[kByteLength];
|
|
7800
|
+
this._state = DEFAULT;
|
|
7801
|
+
options.readOnly = false;
|
|
7802
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
7803
|
+
this.dequeue();
|
|
7804
|
+
});
|
|
7805
|
+
}
|
|
7806
|
+
dequeue() {
|
|
7807
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
7808
|
+
const params = this._queue.shift();
|
|
7809
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
7810
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
7811
|
+
}
|
|
7812
|
+
}
|
|
7813
|
+
enqueue(params) {
|
|
7814
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
7815
|
+
this._queue.push(params);
|
|
7816
|
+
}
|
|
7817
|
+
sendFrame(list, cb) {
|
|
7818
|
+
if (list.length === 2) {
|
|
7819
|
+
this._socket.cork();
|
|
7820
|
+
this._socket.write(list[0]);
|
|
7821
|
+
this._socket.write(list[1], cb);
|
|
7822
|
+
this._socket.uncork();
|
|
7823
|
+
} else {
|
|
7824
|
+
this._socket.write(list[0], cb);
|
|
7825
|
+
}
|
|
7826
|
+
}
|
|
7827
|
+
}
|
|
7828
|
+
module.exports = Sender;
|
|
7829
|
+
function callCallbacks(sender, err, cb) {
|
|
7830
|
+
if (typeof cb === "function")
|
|
7831
|
+
cb(err);
|
|
7832
|
+
for (let i = 0;i < sender._queue.length; i++) {
|
|
7833
|
+
const params = sender._queue[i];
|
|
7834
|
+
const callback = params[params.length - 1];
|
|
7835
|
+
if (typeof callback === "function")
|
|
7836
|
+
callback(err);
|
|
7837
|
+
}
|
|
7838
|
+
}
|
|
7839
|
+
function onError(sender, err, cb) {
|
|
7840
|
+
callCallbacks(sender, err, cb);
|
|
7841
|
+
sender.onerror(err);
|
|
7842
|
+
}
|
|
7843
|
+
});
|
|
7844
|
+
|
|
7845
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/event-target.js
|
|
7846
|
+
var require_event_target = __commonJS((exports, module) => {
|
|
7847
|
+
var { kForOnEventAttribute, kListener } = require_constants();
|
|
7848
|
+
var kCode = Symbol("kCode");
|
|
7849
|
+
var kData = Symbol("kData");
|
|
7850
|
+
var kError = Symbol("kError");
|
|
7851
|
+
var kMessage = Symbol("kMessage");
|
|
7852
|
+
var kReason = Symbol("kReason");
|
|
7853
|
+
var kTarget = Symbol("kTarget");
|
|
7854
|
+
var kType = Symbol("kType");
|
|
7855
|
+
var kWasClean = Symbol("kWasClean");
|
|
7856
|
+
|
|
7857
|
+
class Event {
|
|
7858
|
+
constructor(type) {
|
|
7859
|
+
this[kTarget] = null;
|
|
7860
|
+
this[kType] = type;
|
|
7861
|
+
}
|
|
7862
|
+
get target() {
|
|
7863
|
+
return this[kTarget];
|
|
7864
|
+
}
|
|
7865
|
+
get type() {
|
|
7866
|
+
return this[kType];
|
|
7867
|
+
}
|
|
7868
|
+
}
|
|
7869
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
7870
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
7871
|
+
|
|
7872
|
+
class CloseEvent extends Event {
|
|
7873
|
+
constructor(type, options = {}) {
|
|
7874
|
+
super(type);
|
|
7875
|
+
this[kCode] = options.code === undefined ? 0 : options.code;
|
|
7876
|
+
this[kReason] = options.reason === undefined ? "" : options.reason;
|
|
7877
|
+
this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
|
|
7878
|
+
}
|
|
7879
|
+
get code() {
|
|
7880
|
+
return this[kCode];
|
|
7881
|
+
}
|
|
7882
|
+
get reason() {
|
|
7883
|
+
return this[kReason];
|
|
7884
|
+
}
|
|
7885
|
+
get wasClean() {
|
|
7886
|
+
return this[kWasClean];
|
|
7887
|
+
}
|
|
7888
|
+
}
|
|
7889
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
7890
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
7891
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
7892
|
+
|
|
7893
|
+
class ErrorEvent extends Event {
|
|
7894
|
+
constructor(type, options = {}) {
|
|
7895
|
+
super(type);
|
|
7896
|
+
this[kError] = options.error === undefined ? null : options.error;
|
|
7897
|
+
this[kMessage] = options.message === undefined ? "" : options.message;
|
|
7898
|
+
}
|
|
7899
|
+
get error() {
|
|
7900
|
+
return this[kError];
|
|
7901
|
+
}
|
|
7902
|
+
get message() {
|
|
7903
|
+
return this[kMessage];
|
|
7904
|
+
}
|
|
7905
|
+
}
|
|
7906
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
7907
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
7908
|
+
|
|
7909
|
+
class MessageEvent extends Event {
|
|
7910
|
+
constructor(type, options = {}) {
|
|
7911
|
+
super(type);
|
|
7912
|
+
this[kData] = options.data === undefined ? null : options.data;
|
|
7913
|
+
}
|
|
7914
|
+
get data() {
|
|
7915
|
+
return this[kData];
|
|
7916
|
+
}
|
|
7917
|
+
}
|
|
7918
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
7919
|
+
var EventTarget = {
|
|
7920
|
+
addEventListener(type, handler, options = {}) {
|
|
7921
|
+
for (const listener of this.listeners(type)) {
|
|
7922
|
+
if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
7923
|
+
return;
|
|
7924
|
+
}
|
|
7925
|
+
}
|
|
7926
|
+
let wrapper;
|
|
7927
|
+
if (type === "message") {
|
|
7928
|
+
wrapper = function onMessage(data, isBinary) {
|
|
7929
|
+
const event = new MessageEvent("message", {
|
|
7930
|
+
data: isBinary ? data : data.toString()
|
|
7931
|
+
});
|
|
7932
|
+
event[kTarget] = this;
|
|
7933
|
+
callListener(handler, this, event);
|
|
7934
|
+
};
|
|
7935
|
+
} else if (type === "close") {
|
|
7936
|
+
wrapper = function onClose(code, message) {
|
|
7937
|
+
const event = new CloseEvent("close", {
|
|
7938
|
+
code,
|
|
7939
|
+
reason: message.toString(),
|
|
7940
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
7941
|
+
});
|
|
7942
|
+
event[kTarget] = this;
|
|
7943
|
+
callListener(handler, this, event);
|
|
7944
|
+
};
|
|
7945
|
+
} else if (type === "error") {
|
|
7946
|
+
wrapper = function onError(error) {
|
|
7947
|
+
const event = new ErrorEvent("error", {
|
|
7948
|
+
error,
|
|
7949
|
+
message: error.message
|
|
7950
|
+
});
|
|
7951
|
+
event[kTarget] = this;
|
|
7952
|
+
callListener(handler, this, event);
|
|
7953
|
+
};
|
|
7954
|
+
} else if (type === "open") {
|
|
7955
|
+
wrapper = function onOpen() {
|
|
7956
|
+
const event = new Event("open");
|
|
7957
|
+
event[kTarget] = this;
|
|
7958
|
+
callListener(handler, this, event);
|
|
7959
|
+
};
|
|
7960
|
+
} else {
|
|
7961
|
+
return;
|
|
7962
|
+
}
|
|
7963
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
7964
|
+
wrapper[kListener] = handler;
|
|
7965
|
+
if (options.once) {
|
|
7966
|
+
this.once(type, wrapper);
|
|
7967
|
+
} else {
|
|
7968
|
+
this.on(type, wrapper);
|
|
7969
|
+
}
|
|
7970
|
+
},
|
|
7971
|
+
removeEventListener(type, handler) {
|
|
7972
|
+
for (const listener of this.listeners(type)) {
|
|
7973
|
+
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
7974
|
+
this.removeListener(type, listener);
|
|
7975
|
+
break;
|
|
7976
|
+
}
|
|
7977
|
+
}
|
|
7978
|
+
}
|
|
7979
|
+
};
|
|
7980
|
+
module.exports = {
|
|
7981
|
+
CloseEvent,
|
|
7982
|
+
ErrorEvent,
|
|
7983
|
+
Event,
|
|
7984
|
+
EventTarget,
|
|
7985
|
+
MessageEvent
|
|
7986
|
+
};
|
|
7987
|
+
function callListener(listener, thisArg, event) {
|
|
7988
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
7989
|
+
listener.handleEvent.call(listener, event);
|
|
7990
|
+
} else {
|
|
7991
|
+
listener.call(thisArg, event);
|
|
7992
|
+
}
|
|
7993
|
+
}
|
|
7994
|
+
});
|
|
7995
|
+
|
|
7996
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/extension.js
|
|
7997
|
+
var require_extension = __commonJS((exports, module) => {
|
|
7998
|
+
var { tokenChars } = require_validation2();
|
|
7999
|
+
function push(dest, name, elem) {
|
|
8000
|
+
if (dest[name] === undefined)
|
|
8001
|
+
dest[name] = [elem];
|
|
8002
|
+
else
|
|
8003
|
+
dest[name].push(elem);
|
|
8004
|
+
}
|
|
8005
|
+
function parse3(header) {
|
|
8006
|
+
const offers = Object.create(null);
|
|
8007
|
+
let params = Object.create(null);
|
|
8008
|
+
let mustUnescape = false;
|
|
8009
|
+
let isEscaping = false;
|
|
8010
|
+
let inQuotes = false;
|
|
8011
|
+
let extensionName;
|
|
8012
|
+
let paramName;
|
|
8013
|
+
let start = -1;
|
|
8014
|
+
let code = -1;
|
|
8015
|
+
let end = -1;
|
|
8016
|
+
let i = 0;
|
|
8017
|
+
for (;i < header.length; i++) {
|
|
8018
|
+
code = header.charCodeAt(i);
|
|
8019
|
+
if (extensionName === undefined) {
|
|
8020
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
8021
|
+
if (start === -1)
|
|
8022
|
+
start = i;
|
|
8023
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
8024
|
+
if (end === -1 && start !== -1)
|
|
8025
|
+
end = i;
|
|
8026
|
+
} else if (code === 59 || code === 44) {
|
|
8027
|
+
if (start === -1) {
|
|
8028
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8029
|
+
}
|
|
8030
|
+
if (end === -1)
|
|
8031
|
+
end = i;
|
|
8032
|
+
const name = header.slice(start, end);
|
|
8033
|
+
if (code === 44) {
|
|
8034
|
+
push(offers, name, params);
|
|
8035
|
+
params = Object.create(null);
|
|
8036
|
+
} else {
|
|
8037
|
+
extensionName = name;
|
|
8038
|
+
}
|
|
8039
|
+
start = end = -1;
|
|
8040
|
+
} else {
|
|
8041
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8042
|
+
}
|
|
8043
|
+
} else if (paramName === undefined) {
|
|
8044
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
8045
|
+
if (start === -1)
|
|
8046
|
+
start = i;
|
|
8047
|
+
} else if (code === 32 || code === 9) {
|
|
8048
|
+
if (end === -1 && start !== -1)
|
|
8049
|
+
end = i;
|
|
8050
|
+
} else if (code === 59 || code === 44) {
|
|
8051
|
+
if (start === -1) {
|
|
8052
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8053
|
+
}
|
|
8054
|
+
if (end === -1)
|
|
8055
|
+
end = i;
|
|
8056
|
+
push(params, header.slice(start, end), true);
|
|
8057
|
+
if (code === 44) {
|
|
8058
|
+
push(offers, extensionName, params);
|
|
8059
|
+
params = Object.create(null);
|
|
8060
|
+
extensionName = undefined;
|
|
8061
|
+
}
|
|
8062
|
+
start = end = -1;
|
|
8063
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
8064
|
+
paramName = header.slice(start, i);
|
|
8065
|
+
start = end = -1;
|
|
8066
|
+
} else {
|
|
8067
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8068
|
+
}
|
|
8069
|
+
} else {
|
|
8070
|
+
if (isEscaping) {
|
|
8071
|
+
if (tokenChars[code] !== 1) {
|
|
8072
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8073
|
+
}
|
|
8074
|
+
if (start === -1)
|
|
8075
|
+
start = i;
|
|
8076
|
+
else if (!mustUnescape)
|
|
8077
|
+
mustUnescape = true;
|
|
8078
|
+
isEscaping = false;
|
|
8079
|
+
} else if (inQuotes) {
|
|
8080
|
+
if (tokenChars[code] === 1) {
|
|
8081
|
+
if (start === -1)
|
|
8082
|
+
start = i;
|
|
8083
|
+
} else if (code === 34 && start !== -1) {
|
|
8084
|
+
inQuotes = false;
|
|
8085
|
+
end = i;
|
|
8086
|
+
} else if (code === 92) {
|
|
8087
|
+
isEscaping = true;
|
|
8088
|
+
} else {
|
|
8089
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8090
|
+
}
|
|
8091
|
+
} else if (code === 34 && header.charCodeAt(i - 1) === 61) {
|
|
8092
|
+
inQuotes = true;
|
|
8093
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
8094
|
+
if (start === -1)
|
|
8095
|
+
start = i;
|
|
8096
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
8097
|
+
if (end === -1)
|
|
8098
|
+
end = i;
|
|
8099
|
+
} else if (code === 59 || code === 44) {
|
|
8100
|
+
if (start === -1) {
|
|
8101
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8102
|
+
}
|
|
8103
|
+
if (end === -1)
|
|
8104
|
+
end = i;
|
|
8105
|
+
let value = header.slice(start, end);
|
|
8106
|
+
if (mustUnescape) {
|
|
8107
|
+
value = value.replace(/\\/g, "");
|
|
8108
|
+
mustUnescape = false;
|
|
8109
|
+
}
|
|
8110
|
+
push(params, paramName, value);
|
|
8111
|
+
if (code === 44) {
|
|
8112
|
+
push(offers, extensionName, params);
|
|
8113
|
+
params = Object.create(null);
|
|
8114
|
+
extensionName = undefined;
|
|
8115
|
+
}
|
|
8116
|
+
paramName = undefined;
|
|
8117
|
+
start = end = -1;
|
|
8118
|
+
} else {
|
|
8119
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
8120
|
+
}
|
|
8121
|
+
}
|
|
8122
|
+
}
|
|
8123
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) {
|
|
8124
|
+
throw new SyntaxError("Unexpected end of input");
|
|
8125
|
+
}
|
|
8126
|
+
if (end === -1)
|
|
8127
|
+
end = i;
|
|
8128
|
+
const token = header.slice(start, end);
|
|
8129
|
+
if (extensionName === undefined) {
|
|
8130
|
+
push(offers, token, params);
|
|
8131
|
+
} else {
|
|
8132
|
+
if (paramName === undefined) {
|
|
8133
|
+
push(params, token, true);
|
|
8134
|
+
} else if (mustUnescape) {
|
|
8135
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
8136
|
+
} else {
|
|
8137
|
+
push(params, paramName, token);
|
|
8138
|
+
}
|
|
8139
|
+
push(offers, extensionName, params);
|
|
8140
|
+
}
|
|
8141
|
+
return offers;
|
|
8142
|
+
}
|
|
8143
|
+
function format(extensions) {
|
|
8144
|
+
return Object.keys(extensions).map((extension) => {
|
|
8145
|
+
let configurations = extensions[extension];
|
|
8146
|
+
if (!Array.isArray(configurations))
|
|
8147
|
+
configurations = [configurations];
|
|
8148
|
+
return configurations.map((params) => {
|
|
8149
|
+
return [extension].concat(Object.keys(params).map((k) => {
|
|
8150
|
+
let values = params[k];
|
|
8151
|
+
if (!Array.isArray(values))
|
|
8152
|
+
values = [values];
|
|
8153
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
8154
|
+
})).join("; ");
|
|
8155
|
+
}).join(", ");
|
|
8156
|
+
}).join(", ");
|
|
8157
|
+
}
|
|
8158
|
+
module.exports = { format, parse: parse3 };
|
|
8159
|
+
});
|
|
8160
|
+
|
|
8161
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/websocket.js
|
|
8162
|
+
var require_websocket = __commonJS((exports, module) => {
|
|
8163
|
+
var EventEmitter = __require("events");
|
|
8164
|
+
var https = __require("https");
|
|
8165
|
+
var http = __require("http");
|
|
8166
|
+
var net = __require("net");
|
|
8167
|
+
var tls = __require("tls");
|
|
8168
|
+
var { randomBytes, createHash } = __require("crypto");
|
|
8169
|
+
var { Duplex, Readable } = __require("stream");
|
|
8170
|
+
var { URL: URL2 } = __require("url");
|
|
8171
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
8172
|
+
var Receiver = require_receiver();
|
|
8173
|
+
var Sender = require_sender();
|
|
8174
|
+
var { isBlob } = require_validation2();
|
|
8175
|
+
var {
|
|
8176
|
+
BINARY_TYPES,
|
|
8177
|
+
CLOSE_TIMEOUT,
|
|
8178
|
+
EMPTY_BUFFER,
|
|
8179
|
+
GUID,
|
|
8180
|
+
kForOnEventAttribute,
|
|
8181
|
+
kListener,
|
|
8182
|
+
kStatusCode,
|
|
8183
|
+
kWebSocket,
|
|
8184
|
+
NOOP
|
|
8185
|
+
} = require_constants();
|
|
8186
|
+
var {
|
|
8187
|
+
EventTarget: { addEventListener, removeEventListener }
|
|
8188
|
+
} = require_event_target();
|
|
8189
|
+
var { format, parse: parse3 } = require_extension();
|
|
8190
|
+
var { toBuffer } = require_buffer_util();
|
|
8191
|
+
var kAborted = Symbol("kAborted");
|
|
8192
|
+
var protocolVersions = [8, 13];
|
|
8193
|
+
var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
8194
|
+
var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
8195
|
+
|
|
8196
|
+
class WebSocket extends EventEmitter {
|
|
8197
|
+
constructor(address, protocols, options) {
|
|
8198
|
+
super();
|
|
8199
|
+
this._binaryType = BINARY_TYPES[0];
|
|
8200
|
+
this._closeCode = 1006;
|
|
8201
|
+
this._closeFrameReceived = false;
|
|
8202
|
+
this._closeFrameSent = false;
|
|
8203
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
8204
|
+
this._closeTimer = null;
|
|
8205
|
+
this._errorEmitted = false;
|
|
8206
|
+
this._extensions = {};
|
|
8207
|
+
this._paused = false;
|
|
8208
|
+
this._protocol = "";
|
|
8209
|
+
this._readyState = WebSocket.CONNECTING;
|
|
8210
|
+
this._receiver = null;
|
|
8211
|
+
this._sender = null;
|
|
8212
|
+
this._socket = null;
|
|
8213
|
+
if (address !== null) {
|
|
8214
|
+
this._bufferedAmount = 0;
|
|
8215
|
+
this._isServer = false;
|
|
8216
|
+
this._redirects = 0;
|
|
8217
|
+
if (protocols === undefined) {
|
|
8218
|
+
protocols = [];
|
|
8219
|
+
} else if (!Array.isArray(protocols)) {
|
|
8220
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
8221
|
+
options = protocols;
|
|
8222
|
+
protocols = [];
|
|
8223
|
+
} else {
|
|
8224
|
+
protocols = [protocols];
|
|
8225
|
+
}
|
|
8226
|
+
}
|
|
8227
|
+
initAsClient(this, address, protocols, options);
|
|
8228
|
+
} else {
|
|
8229
|
+
this._autoPong = options.autoPong;
|
|
8230
|
+
this._closeTimeout = options.closeTimeout;
|
|
8231
|
+
this._isServer = true;
|
|
8232
|
+
}
|
|
8233
|
+
}
|
|
8234
|
+
get binaryType() {
|
|
8235
|
+
return this._binaryType;
|
|
8236
|
+
}
|
|
8237
|
+
set binaryType(type) {
|
|
8238
|
+
if (!BINARY_TYPES.includes(type))
|
|
8239
|
+
return;
|
|
8240
|
+
this._binaryType = type;
|
|
8241
|
+
if (this._receiver)
|
|
8242
|
+
this._receiver._binaryType = type;
|
|
8243
|
+
}
|
|
8244
|
+
get bufferedAmount() {
|
|
8245
|
+
if (!this._socket)
|
|
8246
|
+
return this._bufferedAmount;
|
|
8247
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
8248
|
+
}
|
|
8249
|
+
get extensions() {
|
|
8250
|
+
return Object.keys(this._extensions).join();
|
|
8251
|
+
}
|
|
8252
|
+
get isPaused() {
|
|
8253
|
+
return this._paused;
|
|
8254
|
+
}
|
|
8255
|
+
get onclose() {
|
|
8256
|
+
return null;
|
|
8257
|
+
}
|
|
8258
|
+
get onerror() {
|
|
8259
|
+
return null;
|
|
8260
|
+
}
|
|
8261
|
+
get onopen() {
|
|
8262
|
+
return null;
|
|
8263
|
+
}
|
|
8264
|
+
get onmessage() {
|
|
8265
|
+
return null;
|
|
8266
|
+
}
|
|
8267
|
+
get protocol() {
|
|
8268
|
+
return this._protocol;
|
|
8269
|
+
}
|
|
8270
|
+
get readyState() {
|
|
8271
|
+
return this._readyState;
|
|
8272
|
+
}
|
|
8273
|
+
get url() {
|
|
8274
|
+
return this._url;
|
|
8275
|
+
}
|
|
8276
|
+
setSocket(socket, head, options) {
|
|
8277
|
+
const receiver = new Receiver({
|
|
8278
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
8279
|
+
binaryType: this.binaryType,
|
|
8280
|
+
extensions: this._extensions,
|
|
8281
|
+
isServer: this._isServer,
|
|
8282
|
+
maxPayload: options.maxPayload,
|
|
8283
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
8284
|
+
});
|
|
8285
|
+
const sender = new Sender(socket, this._extensions, options.generateMask);
|
|
8286
|
+
this._receiver = receiver;
|
|
8287
|
+
this._sender = sender;
|
|
8288
|
+
this._socket = socket;
|
|
8289
|
+
receiver[kWebSocket] = this;
|
|
8290
|
+
sender[kWebSocket] = this;
|
|
8291
|
+
socket[kWebSocket] = this;
|
|
8292
|
+
receiver.on("conclude", receiverOnConclude);
|
|
8293
|
+
receiver.on("drain", receiverOnDrain);
|
|
8294
|
+
receiver.on("error", receiverOnError);
|
|
8295
|
+
receiver.on("message", receiverOnMessage);
|
|
8296
|
+
receiver.on("ping", receiverOnPing);
|
|
8297
|
+
receiver.on("pong", receiverOnPong);
|
|
8298
|
+
sender.onerror = senderOnError;
|
|
8299
|
+
if (socket.setTimeout)
|
|
8300
|
+
socket.setTimeout(0);
|
|
8301
|
+
if (socket.setNoDelay)
|
|
8302
|
+
socket.setNoDelay();
|
|
8303
|
+
if (head.length > 0)
|
|
8304
|
+
socket.unshift(head);
|
|
8305
|
+
socket.on("close", socketOnClose);
|
|
8306
|
+
socket.on("data", socketOnData);
|
|
8307
|
+
socket.on("end", socketOnEnd);
|
|
8308
|
+
socket.on("error", socketOnError);
|
|
8309
|
+
this._readyState = WebSocket.OPEN;
|
|
8310
|
+
this.emit("open");
|
|
8311
|
+
}
|
|
8312
|
+
emitClose() {
|
|
8313
|
+
if (!this._socket) {
|
|
8314
|
+
this._readyState = WebSocket.CLOSED;
|
|
8315
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
8316
|
+
return;
|
|
8317
|
+
}
|
|
8318
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
8319
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
8320
|
+
}
|
|
8321
|
+
this._receiver.removeAllListeners();
|
|
8322
|
+
this._readyState = WebSocket.CLOSED;
|
|
8323
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
8324
|
+
}
|
|
8325
|
+
close(code, data) {
|
|
8326
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
8327
|
+
return;
|
|
8328
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8329
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
8330
|
+
abortHandshake(this, this._req, msg);
|
|
8331
|
+
return;
|
|
8332
|
+
}
|
|
8333
|
+
if (this.readyState === WebSocket.CLOSING) {
|
|
8334
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
8335
|
+
this._socket.end();
|
|
8336
|
+
}
|
|
8337
|
+
return;
|
|
8338
|
+
}
|
|
8339
|
+
this._readyState = WebSocket.CLOSING;
|
|
8340
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
8341
|
+
if (err)
|
|
8342
|
+
return;
|
|
8343
|
+
this._closeFrameSent = true;
|
|
8344
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
8345
|
+
this._socket.end();
|
|
8346
|
+
}
|
|
8347
|
+
});
|
|
8348
|
+
setCloseTimer(this);
|
|
8349
|
+
}
|
|
8350
|
+
pause() {
|
|
8351
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
8352
|
+
return;
|
|
8353
|
+
}
|
|
8354
|
+
this._paused = true;
|
|
8355
|
+
this._socket.pause();
|
|
8356
|
+
}
|
|
8357
|
+
ping(data, mask, cb) {
|
|
8358
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8359
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
8360
|
+
}
|
|
8361
|
+
if (typeof data === "function") {
|
|
8362
|
+
cb = data;
|
|
8363
|
+
data = mask = undefined;
|
|
8364
|
+
} else if (typeof mask === "function") {
|
|
8365
|
+
cb = mask;
|
|
8366
|
+
mask = undefined;
|
|
8367
|
+
}
|
|
8368
|
+
if (typeof data === "number")
|
|
8369
|
+
data = data.toString();
|
|
8370
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
8371
|
+
sendAfterClose(this, data, cb);
|
|
8372
|
+
return;
|
|
8373
|
+
}
|
|
8374
|
+
if (mask === undefined)
|
|
8375
|
+
mask = !this._isServer;
|
|
8376
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
8377
|
+
}
|
|
8378
|
+
pong(data, mask, cb) {
|
|
8379
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8380
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
8381
|
+
}
|
|
8382
|
+
if (typeof data === "function") {
|
|
8383
|
+
cb = data;
|
|
8384
|
+
data = mask = undefined;
|
|
8385
|
+
} else if (typeof mask === "function") {
|
|
8386
|
+
cb = mask;
|
|
8387
|
+
mask = undefined;
|
|
8388
|
+
}
|
|
8389
|
+
if (typeof data === "number")
|
|
8390
|
+
data = data.toString();
|
|
8391
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
8392
|
+
sendAfterClose(this, data, cb);
|
|
8393
|
+
return;
|
|
8394
|
+
}
|
|
8395
|
+
if (mask === undefined)
|
|
8396
|
+
mask = !this._isServer;
|
|
8397
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
8398
|
+
}
|
|
8399
|
+
resume() {
|
|
8400
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
8401
|
+
return;
|
|
8402
|
+
}
|
|
8403
|
+
this._paused = false;
|
|
8404
|
+
if (!this._receiver._writableState.needDrain)
|
|
8405
|
+
this._socket.resume();
|
|
8406
|
+
}
|
|
8407
|
+
send(data, options, cb) {
|
|
8408
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8409
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
8410
|
+
}
|
|
8411
|
+
if (typeof options === "function") {
|
|
8412
|
+
cb = options;
|
|
8413
|
+
options = {};
|
|
8414
|
+
}
|
|
8415
|
+
if (typeof data === "number")
|
|
8416
|
+
data = data.toString();
|
|
8417
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
8418
|
+
sendAfterClose(this, data, cb);
|
|
8419
|
+
return;
|
|
8420
|
+
}
|
|
8421
|
+
const opts = {
|
|
8422
|
+
binary: typeof data !== "string",
|
|
8423
|
+
mask: !this._isServer,
|
|
8424
|
+
compress: true,
|
|
8425
|
+
fin: true,
|
|
8426
|
+
...options
|
|
8427
|
+
};
|
|
8428
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
8429
|
+
opts.compress = false;
|
|
8430
|
+
}
|
|
8431
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
8432
|
+
}
|
|
8433
|
+
terminate() {
|
|
8434
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
8435
|
+
return;
|
|
8436
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
8437
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
8438
|
+
abortHandshake(this, this._req, msg);
|
|
8439
|
+
return;
|
|
8440
|
+
}
|
|
8441
|
+
if (this._socket) {
|
|
8442
|
+
this._readyState = WebSocket.CLOSING;
|
|
8443
|
+
this._socket.destroy();
|
|
8444
|
+
}
|
|
8445
|
+
}
|
|
8446
|
+
}
|
|
8447
|
+
Object.defineProperty(WebSocket, "CONNECTING", {
|
|
8448
|
+
enumerable: true,
|
|
8449
|
+
value: readyStates.indexOf("CONNECTING")
|
|
8450
|
+
});
|
|
8451
|
+
Object.defineProperty(WebSocket.prototype, "CONNECTING", {
|
|
8452
|
+
enumerable: true,
|
|
8453
|
+
value: readyStates.indexOf("CONNECTING")
|
|
8454
|
+
});
|
|
8455
|
+
Object.defineProperty(WebSocket, "OPEN", {
|
|
8456
|
+
enumerable: true,
|
|
8457
|
+
value: readyStates.indexOf("OPEN")
|
|
8458
|
+
});
|
|
8459
|
+
Object.defineProperty(WebSocket.prototype, "OPEN", {
|
|
8460
|
+
enumerable: true,
|
|
8461
|
+
value: readyStates.indexOf("OPEN")
|
|
8462
|
+
});
|
|
8463
|
+
Object.defineProperty(WebSocket, "CLOSING", {
|
|
8464
|
+
enumerable: true,
|
|
8465
|
+
value: readyStates.indexOf("CLOSING")
|
|
8466
|
+
});
|
|
8467
|
+
Object.defineProperty(WebSocket.prototype, "CLOSING", {
|
|
8468
|
+
enumerable: true,
|
|
8469
|
+
value: readyStates.indexOf("CLOSING")
|
|
8470
|
+
});
|
|
8471
|
+
Object.defineProperty(WebSocket, "CLOSED", {
|
|
8472
|
+
enumerable: true,
|
|
8473
|
+
value: readyStates.indexOf("CLOSED")
|
|
8474
|
+
});
|
|
8475
|
+
Object.defineProperty(WebSocket.prototype, "CLOSED", {
|
|
8476
|
+
enumerable: true,
|
|
8477
|
+
value: readyStates.indexOf("CLOSED")
|
|
8478
|
+
});
|
|
8479
|
+
[
|
|
8480
|
+
"binaryType",
|
|
8481
|
+
"bufferedAmount",
|
|
8482
|
+
"extensions",
|
|
8483
|
+
"isPaused",
|
|
8484
|
+
"protocol",
|
|
8485
|
+
"readyState",
|
|
8486
|
+
"url"
|
|
8487
|
+
].forEach((property) => {
|
|
8488
|
+
Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
|
|
8489
|
+
});
|
|
8490
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
8491
|
+
Object.defineProperty(WebSocket.prototype, `on${method}`, {
|
|
8492
|
+
enumerable: true,
|
|
8493
|
+
get() {
|
|
8494
|
+
for (const listener of this.listeners(method)) {
|
|
8495
|
+
if (listener[kForOnEventAttribute])
|
|
8496
|
+
return listener[kListener];
|
|
8497
|
+
}
|
|
8498
|
+
return null;
|
|
8499
|
+
},
|
|
8500
|
+
set(handler) {
|
|
8501
|
+
for (const listener of this.listeners(method)) {
|
|
8502
|
+
if (listener[kForOnEventAttribute]) {
|
|
8503
|
+
this.removeListener(method, listener);
|
|
8504
|
+
break;
|
|
8505
|
+
}
|
|
8506
|
+
}
|
|
8507
|
+
if (typeof handler !== "function")
|
|
8508
|
+
return;
|
|
8509
|
+
this.addEventListener(method, handler, {
|
|
8510
|
+
[kForOnEventAttribute]: true
|
|
8511
|
+
});
|
|
8512
|
+
}
|
|
8513
|
+
});
|
|
8514
|
+
});
|
|
8515
|
+
WebSocket.prototype.addEventListener = addEventListener;
|
|
8516
|
+
WebSocket.prototype.removeEventListener = removeEventListener;
|
|
8517
|
+
module.exports = WebSocket;
|
|
8518
|
+
function initAsClient(websocket, address, protocols, options) {
|
|
8519
|
+
const opts = {
|
|
8520
|
+
allowSynchronousEvents: true,
|
|
8521
|
+
autoPong: true,
|
|
8522
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
8523
|
+
protocolVersion: protocolVersions[1],
|
|
8524
|
+
maxPayload: 100 * 1024 * 1024,
|
|
8525
|
+
skipUTF8Validation: false,
|
|
8526
|
+
perMessageDeflate: true,
|
|
8527
|
+
followRedirects: false,
|
|
8528
|
+
maxRedirects: 10,
|
|
8529
|
+
...options,
|
|
8530
|
+
socketPath: undefined,
|
|
8531
|
+
hostname: undefined,
|
|
8532
|
+
protocol: undefined,
|
|
8533
|
+
timeout: undefined,
|
|
8534
|
+
method: "GET",
|
|
8535
|
+
host: undefined,
|
|
8536
|
+
path: undefined,
|
|
8537
|
+
port: undefined
|
|
8538
|
+
};
|
|
8539
|
+
websocket._autoPong = opts.autoPong;
|
|
8540
|
+
websocket._closeTimeout = opts.closeTimeout;
|
|
8541
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
8542
|
+
throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} ` + `(supported versions: ${protocolVersions.join(", ")})`);
|
|
8543
|
+
}
|
|
8544
|
+
let parsedUrl;
|
|
8545
|
+
if (address instanceof URL2) {
|
|
8546
|
+
parsedUrl = address;
|
|
8547
|
+
} else {
|
|
8548
|
+
try {
|
|
8549
|
+
parsedUrl = new URL2(address);
|
|
8550
|
+
} catch {
|
|
8551
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
8552
|
+
}
|
|
8553
|
+
}
|
|
8554
|
+
if (parsedUrl.protocol === "http:") {
|
|
8555
|
+
parsedUrl.protocol = "ws:";
|
|
8556
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
8557
|
+
parsedUrl.protocol = "wss:";
|
|
8558
|
+
}
|
|
8559
|
+
websocket._url = parsedUrl.href;
|
|
8560
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
8561
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
8562
|
+
let invalidUrlMessage;
|
|
8563
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
8564
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", ` + '"http:", "https:", or "ws+unix:"';
|
|
8565
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
8566
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
8567
|
+
} else if (parsedUrl.hash) {
|
|
8568
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
8569
|
+
}
|
|
8570
|
+
if (invalidUrlMessage) {
|
|
8571
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
8572
|
+
if (websocket._redirects === 0) {
|
|
8573
|
+
throw err;
|
|
8574
|
+
} else {
|
|
8575
|
+
emitErrorAndClose(websocket, err);
|
|
8576
|
+
return;
|
|
8577
|
+
}
|
|
8578
|
+
}
|
|
8579
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
8580
|
+
const key = randomBytes(16).toString("base64");
|
|
8581
|
+
const request = isSecure ? https.request : http.request;
|
|
8582
|
+
const protocolSet = new Set;
|
|
8583
|
+
let perMessageDeflate;
|
|
8584
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
8585
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
8586
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
8587
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
8588
|
+
opts.headers = {
|
|
8589
|
+
...opts.headers,
|
|
8590
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
8591
|
+
"Sec-WebSocket-Key": key,
|
|
8592
|
+
Connection: "Upgrade",
|
|
8593
|
+
Upgrade: "websocket"
|
|
8594
|
+
};
|
|
8595
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
8596
|
+
opts.timeout = opts.handshakeTimeout;
|
|
8597
|
+
if (opts.perMessageDeflate) {
|
|
8598
|
+
perMessageDeflate = new PerMessageDeflate({
|
|
8599
|
+
...opts.perMessageDeflate,
|
|
8600
|
+
isServer: false,
|
|
8601
|
+
maxPayload: opts.maxPayload
|
|
8602
|
+
});
|
|
8603
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({
|
|
8604
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
8605
|
+
});
|
|
8606
|
+
}
|
|
8607
|
+
if (protocols.length) {
|
|
8608
|
+
for (const protocol of protocols) {
|
|
8609
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
8610
|
+
throw new SyntaxError("An invalid or duplicated subprotocol was specified");
|
|
8611
|
+
}
|
|
8612
|
+
protocolSet.add(protocol);
|
|
8613
|
+
}
|
|
8614
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
8615
|
+
}
|
|
8616
|
+
if (opts.origin) {
|
|
8617
|
+
if (opts.protocolVersion < 13) {
|
|
8618
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
8619
|
+
} else {
|
|
8620
|
+
opts.headers.Origin = opts.origin;
|
|
8621
|
+
}
|
|
8622
|
+
}
|
|
8623
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
8624
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
8625
|
+
}
|
|
8626
|
+
if (isIpcUrl) {
|
|
8627
|
+
const parts = opts.path.split(":");
|
|
8628
|
+
opts.socketPath = parts[0];
|
|
8629
|
+
opts.path = parts[1];
|
|
8630
|
+
}
|
|
8631
|
+
let req;
|
|
8632
|
+
if (opts.followRedirects) {
|
|
8633
|
+
if (websocket._redirects === 0) {
|
|
8634
|
+
websocket._originalIpc = isIpcUrl;
|
|
8635
|
+
websocket._originalSecure = isSecure;
|
|
8636
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
8637
|
+
const headers = options && options.headers;
|
|
8638
|
+
options = { ...options, headers: {} };
|
|
8639
|
+
if (headers) {
|
|
8640
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
8641
|
+
options.headers[key2.toLowerCase()] = value;
|
|
8642
|
+
}
|
|
8643
|
+
}
|
|
8644
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
8645
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
8646
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
8647
|
+
delete opts.headers.authorization;
|
|
8648
|
+
delete opts.headers.cookie;
|
|
8649
|
+
if (!isSameHost)
|
|
8650
|
+
delete opts.headers.host;
|
|
8651
|
+
opts.auth = undefined;
|
|
8652
|
+
}
|
|
8653
|
+
}
|
|
8654
|
+
if (opts.auth && !options.headers.authorization) {
|
|
8655
|
+
options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
8656
|
+
}
|
|
8657
|
+
req = websocket._req = request(opts);
|
|
8658
|
+
if (websocket._redirects) {
|
|
8659
|
+
websocket.emit("redirect", websocket.url, req);
|
|
8660
|
+
}
|
|
8661
|
+
} else {
|
|
8662
|
+
req = websocket._req = request(opts);
|
|
8663
|
+
}
|
|
8664
|
+
if (opts.timeout) {
|
|
8665
|
+
req.on("timeout", () => {
|
|
8666
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
8667
|
+
});
|
|
8668
|
+
}
|
|
8669
|
+
req.on("error", (err) => {
|
|
8670
|
+
if (req === null || req[kAborted])
|
|
8671
|
+
return;
|
|
8672
|
+
req = websocket._req = null;
|
|
8673
|
+
emitErrorAndClose(websocket, err);
|
|
8674
|
+
});
|
|
8675
|
+
req.on("response", (res) => {
|
|
8676
|
+
const location = res.headers.location;
|
|
8677
|
+
const statusCode = res.statusCode;
|
|
8678
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
8679
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
8680
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
8681
|
+
return;
|
|
8682
|
+
}
|
|
8683
|
+
req.abort();
|
|
8684
|
+
let addr;
|
|
8685
|
+
try {
|
|
8686
|
+
addr = new URL2(location, address);
|
|
8687
|
+
} catch (e) {
|
|
8688
|
+
const err = new SyntaxError(`Invalid URL: ${location}`);
|
|
8689
|
+
emitErrorAndClose(websocket, err);
|
|
8690
|
+
return;
|
|
8691
|
+
}
|
|
8692
|
+
initAsClient(websocket, addr, protocols, options);
|
|
8693
|
+
} else if (!websocket.emit("unexpected-response", req, res)) {
|
|
8694
|
+
abortHandshake(websocket, req, `Unexpected server response: ${res.statusCode}`);
|
|
8695
|
+
}
|
|
8696
|
+
});
|
|
8697
|
+
req.on("upgrade", (res, socket, head) => {
|
|
8698
|
+
websocket.emit("upgrade", res);
|
|
8699
|
+
if (websocket.readyState !== WebSocket.CONNECTING)
|
|
8700
|
+
return;
|
|
8701
|
+
req = websocket._req = null;
|
|
8702
|
+
const upgrade = res.headers.upgrade;
|
|
8703
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
8704
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
8705
|
+
return;
|
|
8706
|
+
}
|
|
8707
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
8708
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
8709
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
8710
|
+
return;
|
|
8711
|
+
}
|
|
8712
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
8713
|
+
let protError;
|
|
8714
|
+
if (serverProt !== undefined) {
|
|
8715
|
+
if (!protocolSet.size) {
|
|
8716
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
8717
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
8718
|
+
protError = "Server sent an invalid subprotocol";
|
|
8719
|
+
}
|
|
8720
|
+
} else if (protocolSet.size) {
|
|
8721
|
+
protError = "Server sent no subprotocol";
|
|
8722
|
+
}
|
|
8723
|
+
if (protError) {
|
|
8724
|
+
abortHandshake(websocket, socket, protError);
|
|
8725
|
+
return;
|
|
8726
|
+
}
|
|
8727
|
+
if (serverProt)
|
|
8728
|
+
websocket._protocol = serverProt;
|
|
8729
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
8730
|
+
if (secWebSocketExtensions !== undefined) {
|
|
8731
|
+
if (!perMessageDeflate) {
|
|
8732
|
+
const message = "Server sent a Sec-WebSocket-Extensions header but no extension " + "was requested";
|
|
8733
|
+
abortHandshake(websocket, socket, message);
|
|
8734
|
+
return;
|
|
8735
|
+
}
|
|
8736
|
+
let extensions;
|
|
8737
|
+
try {
|
|
8738
|
+
extensions = parse3(secWebSocketExtensions);
|
|
8739
|
+
} catch (err) {
|
|
8740
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
8741
|
+
abortHandshake(websocket, socket, message);
|
|
8742
|
+
return;
|
|
8743
|
+
}
|
|
8744
|
+
const extensionNames = Object.keys(extensions);
|
|
8745
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
8746
|
+
const message = "Server indicated an extension that was not requested";
|
|
8747
|
+
abortHandshake(websocket, socket, message);
|
|
8748
|
+
return;
|
|
8749
|
+
}
|
|
8750
|
+
try {
|
|
8751
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
8752
|
+
} catch (err) {
|
|
8753
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
8754
|
+
abortHandshake(websocket, socket, message);
|
|
8755
|
+
return;
|
|
8756
|
+
}
|
|
8757
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
8758
|
+
}
|
|
8759
|
+
websocket.setSocket(socket, head, {
|
|
8760
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
8761
|
+
generateMask: opts.generateMask,
|
|
8762
|
+
maxPayload: opts.maxPayload,
|
|
8763
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
8764
|
+
});
|
|
8765
|
+
});
|
|
8766
|
+
if (opts.finishRequest) {
|
|
8767
|
+
opts.finishRequest(req, websocket);
|
|
8768
|
+
} else {
|
|
8769
|
+
req.end();
|
|
8770
|
+
}
|
|
8771
|
+
}
|
|
8772
|
+
function emitErrorAndClose(websocket, err) {
|
|
8773
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8774
|
+
websocket._errorEmitted = true;
|
|
8775
|
+
websocket.emit("error", err);
|
|
8776
|
+
websocket.emitClose();
|
|
8777
|
+
}
|
|
8778
|
+
function netConnect(options) {
|
|
8779
|
+
options.path = options.socketPath;
|
|
8780
|
+
return net.connect(options);
|
|
8781
|
+
}
|
|
8782
|
+
function tlsConnect(options) {
|
|
8783
|
+
options.path = undefined;
|
|
8784
|
+
if (!options.servername && options.servername !== "") {
|
|
8785
|
+
options.servername = net.isIP(options.host) ? "" : options.host;
|
|
8786
|
+
}
|
|
8787
|
+
return tls.connect(options);
|
|
8788
|
+
}
|
|
8789
|
+
function abortHandshake(websocket, stream, message) {
|
|
8790
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8791
|
+
const err = new Error(message);
|
|
8792
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
8793
|
+
if (stream.setHeader) {
|
|
8794
|
+
stream[kAborted] = true;
|
|
8795
|
+
stream.abort();
|
|
8796
|
+
if (stream.socket && !stream.socket.destroyed) {
|
|
8797
|
+
stream.socket.destroy();
|
|
8798
|
+
}
|
|
8799
|
+
process.nextTick(emitErrorAndClose, websocket, err);
|
|
8800
|
+
} else {
|
|
8801
|
+
stream.destroy(err);
|
|
8802
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
8803
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
8804
|
+
}
|
|
8805
|
+
}
|
|
8806
|
+
function sendAfterClose(websocket, data, cb) {
|
|
8807
|
+
if (data) {
|
|
8808
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
8809
|
+
if (websocket._socket)
|
|
8810
|
+
websocket._sender._bufferedBytes += length;
|
|
8811
|
+
else
|
|
8812
|
+
websocket._bufferedAmount += length;
|
|
8813
|
+
}
|
|
8814
|
+
if (cb) {
|
|
8815
|
+
const err = new Error(`WebSocket is not open: readyState ${websocket.readyState} ` + `(${readyStates[websocket.readyState]})`);
|
|
8816
|
+
process.nextTick(cb, err);
|
|
8817
|
+
}
|
|
8818
|
+
}
|
|
8819
|
+
function receiverOnConclude(code, reason) {
|
|
8820
|
+
const websocket = this[kWebSocket];
|
|
8821
|
+
websocket._closeFrameReceived = true;
|
|
8822
|
+
websocket._closeMessage = reason;
|
|
8823
|
+
websocket._closeCode = code;
|
|
8824
|
+
if (websocket._socket[kWebSocket] === undefined)
|
|
8825
|
+
return;
|
|
8826
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
8827
|
+
process.nextTick(resume, websocket._socket);
|
|
8828
|
+
if (code === 1005)
|
|
8829
|
+
websocket.close();
|
|
8830
|
+
else
|
|
8831
|
+
websocket.close(code, reason);
|
|
8832
|
+
}
|
|
8833
|
+
function receiverOnDrain() {
|
|
8834
|
+
const websocket = this[kWebSocket];
|
|
8835
|
+
if (!websocket.isPaused)
|
|
8836
|
+
websocket._socket.resume();
|
|
8837
|
+
}
|
|
8838
|
+
function receiverOnError(err) {
|
|
8839
|
+
const websocket = this[kWebSocket];
|
|
8840
|
+
if (websocket._socket[kWebSocket] !== undefined) {
|
|
8841
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
8842
|
+
process.nextTick(resume, websocket._socket);
|
|
8843
|
+
websocket.close(err[kStatusCode]);
|
|
8844
|
+
}
|
|
8845
|
+
if (!websocket._errorEmitted) {
|
|
8846
|
+
websocket._errorEmitted = true;
|
|
8847
|
+
websocket.emit("error", err);
|
|
8848
|
+
}
|
|
8849
|
+
}
|
|
8850
|
+
function receiverOnFinish() {
|
|
8851
|
+
this[kWebSocket].emitClose();
|
|
8852
|
+
}
|
|
8853
|
+
function receiverOnMessage(data, isBinary) {
|
|
8854
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
8855
|
+
}
|
|
8856
|
+
function receiverOnPing(data) {
|
|
8857
|
+
const websocket = this[kWebSocket];
|
|
8858
|
+
if (websocket._autoPong)
|
|
8859
|
+
websocket.pong(data, !this._isServer, NOOP);
|
|
8860
|
+
websocket.emit("ping", data);
|
|
8861
|
+
}
|
|
8862
|
+
function receiverOnPong(data) {
|
|
8863
|
+
this[kWebSocket].emit("pong", data);
|
|
8864
|
+
}
|
|
8865
|
+
function resume(stream) {
|
|
8866
|
+
stream.resume();
|
|
8867
|
+
}
|
|
8868
|
+
function senderOnError(err) {
|
|
8869
|
+
const websocket = this[kWebSocket];
|
|
8870
|
+
if (websocket.readyState === WebSocket.CLOSED)
|
|
8871
|
+
return;
|
|
8872
|
+
if (websocket.readyState === WebSocket.OPEN) {
|
|
8873
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8874
|
+
setCloseTimer(websocket);
|
|
8875
|
+
}
|
|
8876
|
+
this._socket.end();
|
|
8877
|
+
if (!websocket._errorEmitted) {
|
|
8878
|
+
websocket._errorEmitted = true;
|
|
8879
|
+
websocket.emit("error", err);
|
|
8880
|
+
}
|
|
8881
|
+
}
|
|
8882
|
+
function setCloseTimer(websocket) {
|
|
8883
|
+
websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), websocket._closeTimeout);
|
|
8884
|
+
}
|
|
8885
|
+
function socketOnClose() {
|
|
8886
|
+
const websocket = this[kWebSocket];
|
|
8887
|
+
this.removeListener("close", socketOnClose);
|
|
8888
|
+
this.removeListener("data", socketOnData);
|
|
8889
|
+
this.removeListener("end", socketOnEnd);
|
|
8890
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8891
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
|
|
8892
|
+
const chunk = this.read(this._readableState.length);
|
|
8893
|
+
websocket._receiver.write(chunk);
|
|
8894
|
+
}
|
|
8895
|
+
websocket._receiver.end();
|
|
8896
|
+
this[kWebSocket] = undefined;
|
|
8897
|
+
clearTimeout(websocket._closeTimer);
|
|
8898
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
|
|
8899
|
+
websocket.emitClose();
|
|
8900
|
+
} else {
|
|
8901
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
8902
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
8903
|
+
}
|
|
8904
|
+
}
|
|
8905
|
+
function socketOnData(chunk) {
|
|
8906
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
8907
|
+
this.pause();
|
|
8908
|
+
}
|
|
8909
|
+
}
|
|
8910
|
+
function socketOnEnd() {
|
|
8911
|
+
const websocket = this[kWebSocket];
|
|
8912
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8913
|
+
websocket._receiver.end();
|
|
8914
|
+
this.end();
|
|
8915
|
+
}
|
|
8916
|
+
function socketOnError() {
|
|
8917
|
+
const websocket = this[kWebSocket];
|
|
8918
|
+
this.removeListener("error", socketOnError);
|
|
8919
|
+
this.on("error", NOOP);
|
|
8920
|
+
if (websocket) {
|
|
8921
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
8922
|
+
this.destroy();
|
|
8923
|
+
}
|
|
8924
|
+
}
|
|
8925
|
+
});
|
|
8926
|
+
|
|
8927
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/stream.js
|
|
8928
|
+
var require_stream = __commonJS((exports, module) => {
|
|
8929
|
+
var WebSocket = require_websocket();
|
|
8930
|
+
var { Duplex } = __require("stream");
|
|
8931
|
+
function emitClose(stream) {
|
|
8932
|
+
stream.emit("close");
|
|
8933
|
+
}
|
|
8934
|
+
function duplexOnEnd() {
|
|
8935
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
8936
|
+
this.destroy();
|
|
8937
|
+
}
|
|
8938
|
+
}
|
|
8939
|
+
function duplexOnError(err) {
|
|
8940
|
+
this.removeListener("error", duplexOnError);
|
|
8941
|
+
this.destroy();
|
|
8942
|
+
if (this.listenerCount("error") === 0) {
|
|
8943
|
+
this.emit("error", err);
|
|
8944
|
+
}
|
|
8945
|
+
}
|
|
8946
|
+
function createWebSocketStream(ws, options) {
|
|
8947
|
+
let terminateOnDestroy = true;
|
|
8948
|
+
const duplex = new Duplex({
|
|
8949
|
+
...options,
|
|
8950
|
+
autoDestroy: false,
|
|
8951
|
+
emitClose: false,
|
|
8952
|
+
objectMode: false,
|
|
8953
|
+
writableObjectMode: false
|
|
8954
|
+
});
|
|
8955
|
+
ws.on("message", function message(msg, isBinary) {
|
|
8956
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
8957
|
+
if (!duplex.push(data))
|
|
8958
|
+
ws.pause();
|
|
8959
|
+
});
|
|
8960
|
+
ws.once("error", function error(err) {
|
|
8961
|
+
if (duplex.destroyed)
|
|
8962
|
+
return;
|
|
8963
|
+
terminateOnDestroy = false;
|
|
8964
|
+
duplex.destroy(err);
|
|
8965
|
+
});
|
|
8966
|
+
ws.once("close", function close() {
|
|
8967
|
+
if (duplex.destroyed)
|
|
8968
|
+
return;
|
|
8969
|
+
duplex.push(null);
|
|
8970
|
+
});
|
|
8971
|
+
duplex._destroy = function(err, callback) {
|
|
8972
|
+
if (ws.readyState === ws.CLOSED) {
|
|
8973
|
+
callback(err);
|
|
8974
|
+
process.nextTick(emitClose, duplex);
|
|
8975
|
+
return;
|
|
8976
|
+
}
|
|
8977
|
+
let called = false;
|
|
8978
|
+
ws.once("error", function error(err2) {
|
|
8979
|
+
called = true;
|
|
8980
|
+
callback(err2);
|
|
8981
|
+
});
|
|
8982
|
+
ws.once("close", function close() {
|
|
8983
|
+
if (!called)
|
|
8984
|
+
callback(err);
|
|
8985
|
+
process.nextTick(emitClose, duplex);
|
|
8986
|
+
});
|
|
8987
|
+
if (terminateOnDestroy)
|
|
8988
|
+
ws.terminate();
|
|
8989
|
+
};
|
|
8990
|
+
duplex._final = function(callback) {
|
|
8991
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
8992
|
+
ws.once("open", function open() {
|
|
8993
|
+
duplex._final(callback);
|
|
8994
|
+
});
|
|
8995
|
+
return;
|
|
8996
|
+
}
|
|
8997
|
+
if (ws._socket === null)
|
|
8998
|
+
return;
|
|
8999
|
+
if (ws._socket._writableState.finished) {
|
|
9000
|
+
callback();
|
|
9001
|
+
if (duplex._readableState.endEmitted)
|
|
9002
|
+
duplex.destroy();
|
|
9003
|
+
} else {
|
|
9004
|
+
ws._socket.once("finish", function finish() {
|
|
9005
|
+
callback();
|
|
9006
|
+
});
|
|
9007
|
+
ws.close();
|
|
9008
|
+
}
|
|
9009
|
+
};
|
|
9010
|
+
duplex._read = function() {
|
|
9011
|
+
if (ws.isPaused)
|
|
9012
|
+
ws.resume();
|
|
9013
|
+
};
|
|
9014
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
9015
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
9016
|
+
ws.once("open", function open() {
|
|
9017
|
+
duplex._write(chunk, encoding, callback);
|
|
9018
|
+
});
|
|
9019
|
+
return;
|
|
9020
|
+
}
|
|
9021
|
+
ws.send(chunk, callback);
|
|
9022
|
+
};
|
|
9023
|
+
duplex.on("end", duplexOnEnd);
|
|
9024
|
+
duplex.on("error", duplexOnError);
|
|
9025
|
+
return duplex;
|
|
9026
|
+
}
|
|
9027
|
+
module.exports = createWebSocketStream;
|
|
9028
|
+
});
|
|
9029
|
+
|
|
9030
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/subprotocol.js
|
|
9031
|
+
var require_subprotocol = __commonJS((exports, module) => {
|
|
9032
|
+
var { tokenChars } = require_validation2();
|
|
9033
|
+
function parse3(header) {
|
|
9034
|
+
const protocols = new Set;
|
|
9035
|
+
let start = -1;
|
|
9036
|
+
let end = -1;
|
|
9037
|
+
let i = 0;
|
|
9038
|
+
for (i;i < header.length; i++) {
|
|
9039
|
+
const code = header.charCodeAt(i);
|
|
9040
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
9041
|
+
if (start === -1)
|
|
9042
|
+
start = i;
|
|
9043
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
9044
|
+
if (end === -1 && start !== -1)
|
|
9045
|
+
end = i;
|
|
9046
|
+
} else if (code === 44) {
|
|
9047
|
+
if (start === -1) {
|
|
9048
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
9049
|
+
}
|
|
9050
|
+
if (end === -1)
|
|
9051
|
+
end = i;
|
|
9052
|
+
const protocol2 = header.slice(start, end);
|
|
9053
|
+
if (protocols.has(protocol2)) {
|
|
9054
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
9055
|
+
}
|
|
9056
|
+
protocols.add(protocol2);
|
|
9057
|
+
start = end = -1;
|
|
9058
|
+
} else {
|
|
9059
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
9060
|
+
}
|
|
9061
|
+
}
|
|
9062
|
+
if (start === -1 || end !== -1) {
|
|
9063
|
+
throw new SyntaxError("Unexpected end of input");
|
|
9064
|
+
}
|
|
9065
|
+
const protocol = header.slice(start, i);
|
|
9066
|
+
if (protocols.has(protocol)) {
|
|
9067
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
9068
|
+
}
|
|
9069
|
+
protocols.add(protocol);
|
|
9070
|
+
return protocols;
|
|
9071
|
+
}
|
|
9072
|
+
module.exports = { parse: parse3 };
|
|
9073
|
+
});
|
|
9074
|
+
|
|
9075
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/lib/websocket-server.js
|
|
9076
|
+
var require_websocket_server = __commonJS((exports, module) => {
|
|
9077
|
+
var EventEmitter = __require("events");
|
|
9078
|
+
var http = __require("http");
|
|
9079
|
+
var { Duplex } = __require("stream");
|
|
9080
|
+
var { createHash } = __require("crypto");
|
|
9081
|
+
var extension = require_extension();
|
|
9082
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
9083
|
+
var subprotocol = require_subprotocol();
|
|
9084
|
+
var WebSocket = require_websocket();
|
|
9085
|
+
var { CLOSE_TIMEOUT, GUID, kWebSocket } = require_constants();
|
|
9086
|
+
var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
9087
|
+
var RUNNING = 0;
|
|
9088
|
+
var CLOSING = 1;
|
|
9089
|
+
var CLOSED = 2;
|
|
9090
|
+
|
|
9091
|
+
class WebSocketServer extends EventEmitter {
|
|
9092
|
+
constructor(options, callback) {
|
|
9093
|
+
super();
|
|
9094
|
+
options = {
|
|
9095
|
+
allowSynchronousEvents: true,
|
|
9096
|
+
autoPong: true,
|
|
9097
|
+
maxPayload: 100 * 1024 * 1024,
|
|
9098
|
+
skipUTF8Validation: false,
|
|
9099
|
+
perMessageDeflate: false,
|
|
9100
|
+
handleProtocols: null,
|
|
9101
|
+
clientTracking: true,
|
|
9102
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
9103
|
+
verifyClient: null,
|
|
9104
|
+
noServer: false,
|
|
9105
|
+
backlog: null,
|
|
9106
|
+
server: null,
|
|
9107
|
+
host: null,
|
|
9108
|
+
path: null,
|
|
9109
|
+
port: null,
|
|
9110
|
+
WebSocket,
|
|
9111
|
+
...options
|
|
9112
|
+
};
|
|
9113
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
|
|
9114
|
+
throw new TypeError('One and only one of the "port", "server", or "noServer" options ' + "must be specified");
|
|
9115
|
+
}
|
|
9116
|
+
if (options.port != null) {
|
|
9117
|
+
this._server = http.createServer((req, res) => {
|
|
9118
|
+
const body = http.STATUS_CODES[426];
|
|
9119
|
+
res.writeHead(426, {
|
|
9120
|
+
"Content-Length": body.length,
|
|
9121
|
+
"Content-Type": "text/plain"
|
|
9122
|
+
});
|
|
9123
|
+
res.end(body);
|
|
9124
|
+
});
|
|
9125
|
+
this._server.listen(options.port, options.host, options.backlog, callback);
|
|
9126
|
+
} else if (options.server) {
|
|
9127
|
+
this._server = options.server;
|
|
9128
|
+
}
|
|
9129
|
+
if (this._server) {
|
|
9130
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
9131
|
+
this._removeListeners = addListeners(this._server, {
|
|
9132
|
+
listening: this.emit.bind(this, "listening"),
|
|
9133
|
+
error: this.emit.bind(this, "error"),
|
|
9134
|
+
upgrade: (req, socket, head) => {
|
|
9135
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
9136
|
+
}
|
|
9137
|
+
});
|
|
9138
|
+
}
|
|
9139
|
+
if (options.perMessageDeflate === true)
|
|
9140
|
+
options.perMessageDeflate = {};
|
|
9141
|
+
if (options.clientTracking) {
|
|
9142
|
+
this.clients = new Set;
|
|
9143
|
+
this._shouldEmitClose = false;
|
|
9144
|
+
}
|
|
9145
|
+
this.options = options;
|
|
9146
|
+
this._state = RUNNING;
|
|
9147
|
+
}
|
|
9148
|
+
address() {
|
|
9149
|
+
if (this.options.noServer) {
|
|
9150
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
9151
|
+
}
|
|
9152
|
+
if (!this._server)
|
|
9153
|
+
return null;
|
|
9154
|
+
return this._server.address();
|
|
9155
|
+
}
|
|
9156
|
+
close(cb) {
|
|
9157
|
+
if (this._state === CLOSED) {
|
|
9158
|
+
if (cb) {
|
|
9159
|
+
this.once("close", () => {
|
|
9160
|
+
cb(new Error("The server is not running"));
|
|
9161
|
+
});
|
|
9162
|
+
}
|
|
9163
|
+
process.nextTick(emitClose, this);
|
|
9164
|
+
return;
|
|
9165
|
+
}
|
|
9166
|
+
if (cb)
|
|
9167
|
+
this.once("close", cb);
|
|
9168
|
+
if (this._state === CLOSING)
|
|
9169
|
+
return;
|
|
9170
|
+
this._state = CLOSING;
|
|
9171
|
+
if (this.options.noServer || this.options.server) {
|
|
9172
|
+
if (this._server) {
|
|
9173
|
+
this._removeListeners();
|
|
9174
|
+
this._removeListeners = this._server = null;
|
|
9175
|
+
}
|
|
9176
|
+
if (this.clients) {
|
|
9177
|
+
if (!this.clients.size) {
|
|
9178
|
+
process.nextTick(emitClose, this);
|
|
9179
|
+
} else {
|
|
9180
|
+
this._shouldEmitClose = true;
|
|
9181
|
+
}
|
|
9182
|
+
} else {
|
|
9183
|
+
process.nextTick(emitClose, this);
|
|
9184
|
+
}
|
|
9185
|
+
} else {
|
|
9186
|
+
const server = this._server;
|
|
9187
|
+
this._removeListeners();
|
|
9188
|
+
this._removeListeners = this._server = null;
|
|
9189
|
+
server.close(() => {
|
|
9190
|
+
emitClose(this);
|
|
9191
|
+
});
|
|
9192
|
+
}
|
|
9193
|
+
}
|
|
9194
|
+
shouldHandle(req) {
|
|
9195
|
+
if (this.options.path) {
|
|
9196
|
+
const index = req.url.indexOf("?");
|
|
9197
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
|
9198
|
+
if (pathname !== this.options.path)
|
|
9199
|
+
return false;
|
|
9200
|
+
}
|
|
9201
|
+
return true;
|
|
9202
|
+
}
|
|
9203
|
+
handleUpgrade(req, socket, head, cb) {
|
|
9204
|
+
socket.on("error", socketOnError);
|
|
9205
|
+
const key = req.headers["sec-websocket-key"];
|
|
9206
|
+
const upgrade = req.headers.upgrade;
|
|
9207
|
+
const version = +req.headers["sec-websocket-version"];
|
|
9208
|
+
if (req.method !== "GET") {
|
|
9209
|
+
const message = "Invalid HTTP method";
|
|
9210
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
|
|
9211
|
+
return;
|
|
9212
|
+
}
|
|
9213
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
9214
|
+
const message = "Invalid Upgrade header";
|
|
9215
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
9216
|
+
return;
|
|
9217
|
+
}
|
|
9218
|
+
if (key === undefined || !keyRegex.test(key)) {
|
|
9219
|
+
const message = "Missing or invalid Sec-WebSocket-Key header";
|
|
9220
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
9221
|
+
return;
|
|
9222
|
+
}
|
|
9223
|
+
if (version !== 13 && version !== 8) {
|
|
9224
|
+
const message = "Missing or invalid Sec-WebSocket-Version header";
|
|
9225
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
|
|
9226
|
+
"Sec-WebSocket-Version": "13, 8"
|
|
9227
|
+
});
|
|
9228
|
+
return;
|
|
9229
|
+
}
|
|
9230
|
+
if (!this.shouldHandle(req)) {
|
|
9231
|
+
abortHandshake(socket, 400);
|
|
9232
|
+
return;
|
|
9233
|
+
}
|
|
9234
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
9235
|
+
let protocols = new Set;
|
|
9236
|
+
if (secWebSocketProtocol !== undefined) {
|
|
9237
|
+
try {
|
|
9238
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
9239
|
+
} catch (err) {
|
|
9240
|
+
const message = "Invalid Sec-WebSocket-Protocol header";
|
|
9241
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
9242
|
+
return;
|
|
9243
|
+
}
|
|
9244
|
+
}
|
|
9245
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
9246
|
+
const extensions = {};
|
|
9247
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== undefined) {
|
|
9248
|
+
const perMessageDeflate = new PerMessageDeflate({
|
|
9249
|
+
...this.options.perMessageDeflate,
|
|
9250
|
+
isServer: true,
|
|
9251
|
+
maxPayload: this.options.maxPayload
|
|
9252
|
+
});
|
|
9253
|
+
try {
|
|
9254
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
9255
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
9256
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
9257
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
9258
|
+
}
|
|
9259
|
+
} catch (err) {
|
|
9260
|
+
const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
9261
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
9262
|
+
return;
|
|
9263
|
+
}
|
|
9264
|
+
}
|
|
9265
|
+
if (this.options.verifyClient) {
|
|
9266
|
+
const info = {
|
|
9267
|
+
origin: req.headers[`${version === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
9268
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
9269
|
+
req
|
|
9270
|
+
};
|
|
9271
|
+
if (this.options.verifyClient.length === 2) {
|
|
9272
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
9273
|
+
if (!verified) {
|
|
9274
|
+
return abortHandshake(socket, code || 401, message, headers);
|
|
9275
|
+
}
|
|
9276
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
9277
|
+
});
|
|
9278
|
+
return;
|
|
9279
|
+
}
|
|
9280
|
+
if (!this.options.verifyClient(info))
|
|
9281
|
+
return abortHandshake(socket, 401);
|
|
9282
|
+
}
|
|
9283
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
9284
|
+
}
|
|
9285
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
9286
|
+
if (!socket.readable || !socket.writable)
|
|
9287
|
+
return socket.destroy();
|
|
9288
|
+
if (socket[kWebSocket]) {
|
|
9289
|
+
throw new Error("server.handleUpgrade() was called more than once with the same " + "socket, possibly due to a misconfiguration");
|
|
9290
|
+
}
|
|
9291
|
+
if (this._state > RUNNING)
|
|
9292
|
+
return abortHandshake(socket, 503);
|
|
9293
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
9294
|
+
const headers = [
|
|
9295
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
9296
|
+
"Upgrade: websocket",
|
|
9297
|
+
"Connection: Upgrade",
|
|
9298
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
9299
|
+
];
|
|
9300
|
+
const ws = new this.options.WebSocket(null, undefined, this.options);
|
|
9301
|
+
if (protocols.size) {
|
|
9302
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
9303
|
+
if (protocol) {
|
|
9304
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
9305
|
+
ws._protocol = protocol;
|
|
9306
|
+
}
|
|
9307
|
+
}
|
|
9308
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
9309
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
9310
|
+
const value = extension.format({
|
|
9311
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
9312
|
+
});
|
|
9313
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
9314
|
+
ws._extensions = extensions;
|
|
9315
|
+
}
|
|
9316
|
+
this.emit("headers", headers, req);
|
|
9317
|
+
socket.write(headers.concat(`\r
|
|
9318
|
+
`).join(`\r
|
|
9319
|
+
`));
|
|
9320
|
+
socket.removeListener("error", socketOnError);
|
|
9321
|
+
ws.setSocket(socket, head, {
|
|
9322
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
9323
|
+
maxPayload: this.options.maxPayload,
|
|
9324
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
9325
|
+
});
|
|
9326
|
+
if (this.clients) {
|
|
9327
|
+
this.clients.add(ws);
|
|
9328
|
+
ws.on("close", () => {
|
|
9329
|
+
this.clients.delete(ws);
|
|
9330
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
9331
|
+
process.nextTick(emitClose, this);
|
|
9332
|
+
}
|
|
9333
|
+
});
|
|
9334
|
+
}
|
|
9335
|
+
cb(ws, req);
|
|
9336
|
+
}
|
|
9337
|
+
}
|
|
9338
|
+
module.exports = WebSocketServer;
|
|
9339
|
+
function addListeners(server, map2) {
|
|
9340
|
+
for (const event of Object.keys(map2))
|
|
9341
|
+
server.on(event, map2[event]);
|
|
9342
|
+
return function removeListeners() {
|
|
9343
|
+
for (const event of Object.keys(map2)) {
|
|
9344
|
+
server.removeListener(event, map2[event]);
|
|
9345
|
+
}
|
|
9346
|
+
};
|
|
9347
|
+
}
|
|
9348
|
+
function emitClose(server) {
|
|
9349
|
+
server._state = CLOSED;
|
|
9350
|
+
server.emit("close");
|
|
9351
|
+
}
|
|
9352
|
+
function socketOnError() {
|
|
9353
|
+
this.destroy();
|
|
9354
|
+
}
|
|
9355
|
+
function abortHandshake(socket, code, message, headers) {
|
|
9356
|
+
message = message || http.STATUS_CODES[code];
|
|
9357
|
+
headers = {
|
|
9358
|
+
Connection: "close",
|
|
9359
|
+
"Content-Type": "text/html",
|
|
9360
|
+
"Content-Length": Buffer.byteLength(message),
|
|
9361
|
+
...headers
|
|
9362
|
+
};
|
|
9363
|
+
socket.once("finish", socket.destroy);
|
|
9364
|
+
socket.end(`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
9365
|
+
` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join(`\r
|
|
9366
|
+
`) + `\r
|
|
9367
|
+
\r
|
|
9368
|
+
` + message);
|
|
9369
|
+
}
|
|
9370
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
9371
|
+
if (server.listenerCount("wsClientError")) {
|
|
9372
|
+
const err = new Error(message);
|
|
9373
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
9374
|
+
server.emit("wsClientError", err, socket, req);
|
|
9375
|
+
} else {
|
|
9376
|
+
abortHandshake(socket, code, message, headers);
|
|
9377
|
+
}
|
|
9378
|
+
}
|
|
6511
9379
|
});
|
|
6512
9380
|
|
|
6513
9381
|
// ../../node_modules/.bun/zod@4.3.6/node_modules/zod/v3/helpers/util.js
|
|
@@ -14851,8 +17719,8 @@ class StdioServerTransport {
|
|
|
14851
17719
|
}
|
|
14852
17720
|
|
|
14853
17721
|
// src/mcp.ts
|
|
14854
|
-
import { existsSync as
|
|
14855
|
-
import { join as
|
|
17722
|
+
import { existsSync as existsSync5, readFileSync as readFileSync3, readdirSync as readdirSync2 } from "node:fs";
|
|
17723
|
+
import { join as join6 } from "node:path";
|
|
14856
17724
|
|
|
14857
17725
|
// src/utils/scan.ts
|
|
14858
17726
|
import { readdirSync, existsSync, readFileSync } from "node:fs";
|
|
@@ -14918,6 +17786,720 @@ async function scanCodebase(root) {
|
|
|
14918
17786
|
return scan;
|
|
14919
17787
|
}
|
|
14920
17788
|
|
|
17789
|
+
// src/copilot/tools.ts
|
|
17790
|
+
import { join as join5 } from "node:path";
|
|
17791
|
+
|
|
17792
|
+
// src/copilot/session.ts
|
|
17793
|
+
import { randomUUID } from "node:crypto";
|
|
17794
|
+
import { join as join2 } from "node:path";
|
|
17795
|
+
var sessions = new Map;
|
|
17796
|
+
function createSession(baseUrl, mode, outputDir) {
|
|
17797
|
+
const id = randomUUID().slice(0, 8);
|
|
17798
|
+
const session = {
|
|
17799
|
+
id,
|
|
17800
|
+
baseUrl,
|
|
17801
|
+
mode,
|
|
17802
|
+
startedAt: new Date().toISOString(),
|
|
17803
|
+
outputDir,
|
|
17804
|
+
pages: new Map,
|
|
17805
|
+
topology: [],
|
|
17806
|
+
actionLog: [],
|
|
17807
|
+
annotations: [],
|
|
17808
|
+
pendingProposal: null,
|
|
17809
|
+
voiceQueue: [],
|
|
17810
|
+
companionPort: null,
|
|
17811
|
+
overlayPid: null
|
|
17812
|
+
};
|
|
17813
|
+
ensureDir(join2(outputDir, "screenshots"));
|
|
17814
|
+
ensureDir(join2(outputDir, "sections"));
|
|
17815
|
+
sessions.set(id, session);
|
|
17816
|
+
return session;
|
|
17817
|
+
}
|
|
17818
|
+
function getSession(id) {
|
|
17819
|
+
return sessions.get(id);
|
|
17820
|
+
}
|
|
17821
|
+
function destroySession(id) {
|
|
17822
|
+
sessions.delete(id);
|
|
17823
|
+
}
|
|
17824
|
+
function addPage(session, page) {
|
|
17825
|
+
session.pages.set(page.url, page);
|
|
17826
|
+
session.topology = buildTopology(session.pages);
|
|
17827
|
+
}
|
|
17828
|
+
function logAction(session, action, result, source = "agent") {
|
|
17829
|
+
session.actionLog.push({
|
|
17830
|
+
action,
|
|
17831
|
+
timestamp: new Date().toISOString(),
|
|
17832
|
+
result,
|
|
17833
|
+
source
|
|
17834
|
+
});
|
|
17835
|
+
}
|
|
17836
|
+
function enqueueVoiceInput(session, input) {
|
|
17837
|
+
session.voiceQueue.push(input);
|
|
17838
|
+
}
|
|
17839
|
+
function drainVoiceQueue(session) {
|
|
17840
|
+
const commands = session.voiceQueue.filter((v) => v.type === "command");
|
|
17841
|
+
const comments = session.voiceQueue.filter((v) => v.type === "comment");
|
|
17842
|
+
for (const comment of comments) {
|
|
17843
|
+
session.annotations.push(comment);
|
|
17844
|
+
}
|
|
17845
|
+
session.voiceQueue = [];
|
|
17846
|
+
return { commands, comments };
|
|
17847
|
+
}
|
|
17848
|
+
function setProposal(session, action, reason) {
|
|
17849
|
+
const proposal = {
|
|
17850
|
+
id: randomUUID().slice(0, 8),
|
|
17851
|
+
action,
|
|
17852
|
+
reason,
|
|
17853
|
+
timestamp: new Date().toISOString(),
|
|
17854
|
+
status: "pending"
|
|
17855
|
+
};
|
|
17856
|
+
session.pendingProposal = proposal;
|
|
17857
|
+
return proposal;
|
|
17858
|
+
}
|
|
17859
|
+
function resolveProposal(session, approved, feedback) {
|
|
17860
|
+
if (session.pendingProposal) {
|
|
17861
|
+
session.pendingProposal.status = approved ? "approved" : "rejected";
|
|
17862
|
+
session.pendingProposal.feedback = feedback;
|
|
17863
|
+
}
|
|
17864
|
+
}
|
|
17865
|
+
function serializeSession(session) {
|
|
17866
|
+
return {
|
|
17867
|
+
id: session.id,
|
|
17868
|
+
baseUrl: session.baseUrl,
|
|
17869
|
+
mode: session.mode,
|
|
17870
|
+
startedAt: session.startedAt,
|
|
17871
|
+
outputDir: session.outputDir,
|
|
17872
|
+
pages: Array.from(session.pages.values()).map((p) => ({
|
|
17873
|
+
url: p.url,
|
|
17874
|
+
path: p.path,
|
|
17875
|
+
title: p.title,
|
|
17876
|
+
slug: p.slug,
|
|
17877
|
+
status: p.status,
|
|
17878
|
+
elements: p.elements.length,
|
|
17879
|
+
outgoingLinks: p.outgoingLinks.length,
|
|
17880
|
+
hasAnalysis: !!p.analysis
|
|
17881
|
+
})),
|
|
17882
|
+
topology: session.topology,
|
|
17883
|
+
stats: {
|
|
17884
|
+
pagesExplored: Array.from(session.pages.values()).filter((p) => p.status === "visited").length,
|
|
17885
|
+
totalAnnotations: session.annotations.length,
|
|
17886
|
+
totalActions: session.actionLog.length
|
|
17887
|
+
},
|
|
17888
|
+
annotations: session.annotations
|
|
17889
|
+
};
|
|
17890
|
+
}
|
|
17891
|
+
|
|
17892
|
+
// src/copilot/companion/server.ts
|
|
17893
|
+
import { createServer } from "node:http";
|
|
17894
|
+
import { readFileSync as readFileSync2, existsSync as existsSync2 } from "node:fs";
|
|
17895
|
+
import { resolve, dirname } from "node:path";
|
|
17896
|
+
import { fileURLToPath } from "node:url";
|
|
17897
|
+
|
|
17898
|
+
// ../../node_modules/.bun/ws@8.20.0/node_modules/ws/wrapper.mjs
|
|
17899
|
+
var import_stream = __toESM(require_stream(), 1);
|
|
17900
|
+
var import_extension = __toESM(require_extension(), 1);
|
|
17901
|
+
var import_permessage_deflate = __toESM(require_permessage_deflate(), 1);
|
|
17902
|
+
var import_receiver = __toESM(require_receiver(), 1);
|
|
17903
|
+
var import_sender = __toESM(require_sender(), 1);
|
|
17904
|
+
var import_subprotocol = __toESM(require_subprotocol(), 1);
|
|
17905
|
+
var import_websocket = __toESM(require_websocket(), 1);
|
|
17906
|
+
var import_websocket_server = __toESM(require_websocket_server(), 1);
|
|
17907
|
+
|
|
17908
|
+
// src/copilot/companion/state.ts
|
|
17909
|
+
function createCompanionState(mode) {
|
|
17910
|
+
return {
|
|
17911
|
+
mode,
|
|
17912
|
+
currentPage: null,
|
|
17913
|
+
actionLog: [],
|
|
17914
|
+
pendingProposal: null,
|
|
17915
|
+
voiceQueue: []
|
|
17916
|
+
};
|
|
17917
|
+
}
|
|
17918
|
+
|
|
17919
|
+
// src/copilot/companion/server.ts
|
|
17920
|
+
var __dirname2 = dirname(fileURLToPath(import.meta.url));
|
|
17921
|
+
var uiDir = resolve(__dirname2, "ui");
|
|
17922
|
+
var state;
|
|
17923
|
+
var clients = new Set;
|
|
17924
|
+
var _httpServer = null;
|
|
17925
|
+
var _wss = null;
|
|
17926
|
+
function broadcast(msg) {
|
|
17927
|
+
const data = JSON.stringify(msg);
|
|
17928
|
+
for (const ws of clients) {
|
|
17929
|
+
if (ws.readyState === 1)
|
|
17930
|
+
ws.send(data);
|
|
17931
|
+
}
|
|
17932
|
+
}
|
|
17933
|
+
function broadcastState() {
|
|
17934
|
+
broadcast({ type: "state:update", state });
|
|
17935
|
+
}
|
|
17936
|
+
function startCompanionServer(mode, port = 0) {
|
|
17937
|
+
state = createCompanionState(mode);
|
|
17938
|
+
return new Promise((resolvePromise, reject) => {
|
|
17939
|
+
const httpServer = createServer((req, res) => {
|
|
17940
|
+
if (req.url === "/" || req.url === "/companion") {
|
|
17941
|
+
const htmlPath = resolve(uiDir, "companion.html");
|
|
17942
|
+
if (existsSync2(htmlPath)) {
|
|
17943
|
+
res.writeHead(200, { "Content-Type": "text/html", "Access-Control-Allow-Origin": "*" });
|
|
17944
|
+
res.end(readFileSync2(htmlPath));
|
|
17945
|
+
} else {
|
|
17946
|
+
res.writeHead(200, { "Content-Type": "text/html", "Access-Control-Allow-Origin": "*" });
|
|
17947
|
+
res.end("<html><body><h1>Companion UI not found</h1></body></html>");
|
|
17948
|
+
}
|
|
17949
|
+
return;
|
|
17950
|
+
}
|
|
17951
|
+
res.writeHead(404);
|
|
17952
|
+
res.end("Not found");
|
|
17953
|
+
});
|
|
17954
|
+
const wss = new import_websocket_server.default({ server: httpServer });
|
|
17955
|
+
wss.on("connection", (ws) => {
|
|
17956
|
+
clients.add(ws);
|
|
17957
|
+
ws.send(JSON.stringify({ type: "state:update", state }));
|
|
17958
|
+
ws.on("message", (raw) => {
|
|
17959
|
+
let msg;
|
|
17960
|
+
try {
|
|
17961
|
+
msg = JSON.parse(raw.toString());
|
|
17962
|
+
} catch {
|
|
17963
|
+
return;
|
|
17964
|
+
}
|
|
17965
|
+
switch (msg.type) {
|
|
17966
|
+
case "voice:input": {
|
|
17967
|
+
const input = {
|
|
17968
|
+
type: msg.inputType,
|
|
17969
|
+
text: msg.text,
|
|
17970
|
+
timestamp: new Date().toISOString(),
|
|
17971
|
+
pageSlug: state.currentPage?.url
|
|
17972
|
+
};
|
|
17973
|
+
state.voiceQueue.push(input);
|
|
17974
|
+
broadcastState();
|
|
17975
|
+
break;
|
|
17976
|
+
}
|
|
17977
|
+
case "text:input": {
|
|
17978
|
+
const input = {
|
|
17979
|
+
type: "command",
|
|
17980
|
+
text: msg.text,
|
|
17981
|
+
timestamp: new Date().toISOString()
|
|
17982
|
+
};
|
|
17983
|
+
state.voiceQueue.push(input);
|
|
17984
|
+
broadcastState();
|
|
17985
|
+
break;
|
|
17986
|
+
}
|
|
17987
|
+
case "proposal:respond": {
|
|
17988
|
+
if (state.pendingProposal) {
|
|
17989
|
+
state.pendingProposal.status = msg.approved ? "approved" : "rejected";
|
|
17990
|
+
state.pendingProposal.feedback = msg.feedback;
|
|
17991
|
+
broadcastState();
|
|
17992
|
+
}
|
|
17993
|
+
break;
|
|
17994
|
+
}
|
|
17995
|
+
case "mode:change": {
|
|
17996
|
+
state.mode = msg.mode;
|
|
17997
|
+
broadcastState();
|
|
17998
|
+
break;
|
|
17999
|
+
}
|
|
18000
|
+
}
|
|
18001
|
+
});
|
|
18002
|
+
ws.on("close", () => {
|
|
18003
|
+
clients.delete(ws);
|
|
18004
|
+
});
|
|
18005
|
+
});
|
|
18006
|
+
_httpServer = httpServer;
|
|
18007
|
+
_wss = wss;
|
|
18008
|
+
httpServer.listen(port, "127.0.0.1", () => {
|
|
18009
|
+
const addr = httpServer.address();
|
|
18010
|
+
const actualPort = typeof addr === "object" && addr ? addr.port : port;
|
|
18011
|
+
resolvePromise({ port: actualPort, url: `http://localhost:${actualPort}` });
|
|
18012
|
+
});
|
|
18013
|
+
httpServer.on("error", reject);
|
|
18014
|
+
});
|
|
18015
|
+
}
|
|
18016
|
+
function stopCompanionServer() {
|
|
18017
|
+
broadcast({ type: "session:end" });
|
|
18018
|
+
for (const ws of clients) {
|
|
18019
|
+
try {
|
|
18020
|
+
ws.close();
|
|
18021
|
+
} catch {}
|
|
18022
|
+
}
|
|
18023
|
+
clients.clear();
|
|
18024
|
+
if (_wss) {
|
|
18025
|
+
_wss.close();
|
|
18026
|
+
_wss = null;
|
|
18027
|
+
}
|
|
18028
|
+
if (_httpServer) {
|
|
18029
|
+
_httpServer.close();
|
|
18030
|
+
_httpServer = null;
|
|
18031
|
+
}
|
|
18032
|
+
state = createCompanionState("auto");
|
|
18033
|
+
}
|
|
18034
|
+
function updateCurrentPage(url, title, screenshotPath) {
|
|
18035
|
+
if (state) {
|
|
18036
|
+
state.currentPage = { url, title, screenshotPath };
|
|
18037
|
+
broadcastState();
|
|
18038
|
+
}
|
|
18039
|
+
}
|
|
18040
|
+
function addActionToLog(entry) {
|
|
18041
|
+
if (state) {
|
|
18042
|
+
state.actionLog.push(entry);
|
|
18043
|
+
if (state.actionLog.length > 100)
|
|
18044
|
+
state.actionLog = state.actionLog.slice(-100);
|
|
18045
|
+
broadcastState();
|
|
18046
|
+
}
|
|
18047
|
+
}
|
|
18048
|
+
function setCompanionProposal(proposal) {
|
|
18049
|
+
if (state) {
|
|
18050
|
+
state.pendingProposal = proposal;
|
|
18051
|
+
broadcastState();
|
|
18052
|
+
}
|
|
18053
|
+
}
|
|
18054
|
+
function getCompanionVoiceQueue() {
|
|
18055
|
+
if (!state)
|
|
18056
|
+
return [];
|
|
18057
|
+
const queue = [...state.voiceQueue];
|
|
18058
|
+
state.voiceQueue = [];
|
|
18059
|
+
return queue;
|
|
18060
|
+
}
|
|
18061
|
+
function getCompanionProposalResult() {
|
|
18062
|
+
if (!state?.pendingProposal)
|
|
18063
|
+
return null;
|
|
18064
|
+
if (state.pendingProposal.status === "pending")
|
|
18065
|
+
return null;
|
|
18066
|
+
const result = { ...state.pendingProposal };
|
|
18067
|
+
state.pendingProposal = null;
|
|
18068
|
+
return result;
|
|
18069
|
+
}
|
|
18070
|
+
|
|
18071
|
+
// src/copilot/overlay-launcher.ts
|
|
18072
|
+
import { existsSync as existsSync4 } from "node:fs";
|
|
18073
|
+
import { join as join4 } from "node:path";
|
|
18074
|
+
import { homedir } from "node:os";
|
|
18075
|
+
import { spawn } from "node:child_process";
|
|
18076
|
+
|
|
18077
|
+
// src/copilot/overlay-download.ts
|
|
18078
|
+
import { createWriteStream, mkdirSync, existsSync as existsSync3, chmodSync } from "node:fs";
|
|
18079
|
+
import { join as join3 } from "node:path";
|
|
18080
|
+
import { pipeline as pipeline2 } from "node:stream/promises";
|
|
18081
|
+
import { createHash } from "node:crypto";
|
|
18082
|
+
import { readFile, unlink } from "node:fs/promises";
|
|
18083
|
+
import { createInterface } from "node:readline";
|
|
18084
|
+
import { Readable } from "node:stream";
|
|
18085
|
+
var OVERLAY_VERSION = "0.1.0";
|
|
18086
|
+
var GITHUB_REPO = "anthropics/qa-intelligence";
|
|
18087
|
+
function resolveArch() {
|
|
18088
|
+
switch (process.arch) {
|
|
18089
|
+
case "arm64":
|
|
18090
|
+
return "aarch64";
|
|
18091
|
+
case "x64":
|
|
18092
|
+
return "x86_64";
|
|
18093
|
+
default:
|
|
18094
|
+
return null;
|
|
18095
|
+
}
|
|
18096
|
+
}
|
|
18097
|
+
async function confirm(message) {
|
|
18098
|
+
if (!process.stdin.isTTY)
|
|
18099
|
+
return false;
|
|
18100
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
18101
|
+
return new Promise((resolve2) => {
|
|
18102
|
+
rl.question(`${message} (Y/n) `, (answer) => {
|
|
18103
|
+
rl.close();
|
|
18104
|
+
resolve2(answer.trim().toLowerCase() !== "n");
|
|
18105
|
+
});
|
|
18106
|
+
});
|
|
18107
|
+
}
|
|
18108
|
+
async function fetchWithProgress(url, destPath) {
|
|
18109
|
+
const response = await fetch(url, { redirect: "follow" });
|
|
18110
|
+
if (!response.ok)
|
|
18111
|
+
throw new Error(`Download failed: ${response.status} ${response.statusText}`);
|
|
18112
|
+
const contentLength = Number(response.headers.get("content-length") || 0);
|
|
18113
|
+
const body = response.body;
|
|
18114
|
+
if (!body)
|
|
18115
|
+
throw new Error("No response body");
|
|
18116
|
+
let downloaded = 0;
|
|
18117
|
+
const fileStream = createWriteStream(destPath);
|
|
18118
|
+
const reader = body.getReader();
|
|
18119
|
+
const nodeStream = new Readable({
|
|
18120
|
+
async read() {
|
|
18121
|
+
const { done, value } = await reader.read();
|
|
18122
|
+
if (done) {
|
|
18123
|
+
this.push(null);
|
|
18124
|
+
} else {
|
|
18125
|
+
downloaded += value.byteLength;
|
|
18126
|
+
if (contentLength > 0) {
|
|
18127
|
+
const pct = Math.round(downloaded / contentLength * 100);
|
|
18128
|
+
const mb = (downloaded / 1024 / 1024).toFixed(1);
|
|
18129
|
+
process.stdout.write(`\r Downloading... ${mb} MB (${pct}%)`);
|
|
18130
|
+
}
|
|
18131
|
+
this.push(Buffer.from(value));
|
|
18132
|
+
}
|
|
18133
|
+
}
|
|
18134
|
+
});
|
|
18135
|
+
await pipeline2(nodeStream, fileStream);
|
|
18136
|
+
if (contentLength > 0)
|
|
18137
|
+
process.stdout.write(`
|
|
18138
|
+
`);
|
|
18139
|
+
}
|
|
18140
|
+
async function verifyChecksum(filePath, expectedHash) {
|
|
18141
|
+
const data = await readFile(filePath);
|
|
18142
|
+
const hash = createHash("sha256").update(data).digest("hex");
|
|
18143
|
+
return hash === expectedHash;
|
|
18144
|
+
}
|
|
18145
|
+
async function downloadOverlay(binDir, options) {
|
|
18146
|
+
if (process.platform !== "darwin") {
|
|
18147
|
+
if (!options?.silent) {
|
|
18148
|
+
console.log(" Native overlay is only available on macOS.");
|
|
18149
|
+
}
|
|
18150
|
+
return false;
|
|
18151
|
+
}
|
|
18152
|
+
const arch = resolveArch();
|
|
18153
|
+
if (!arch) {
|
|
18154
|
+
if (!options?.silent) {
|
|
18155
|
+
console.log(` Unsupported architecture: ${process.arch}`);
|
|
18156
|
+
}
|
|
18157
|
+
return false;
|
|
18158
|
+
}
|
|
18159
|
+
const shouldDownload = await confirm(`
|
|
18160
|
+
Native overlay not found. Download Zefiro Overlay? (~10 MB)`);
|
|
18161
|
+
if (!shouldDownload)
|
|
18162
|
+
return false;
|
|
18163
|
+
if (!existsSync3(binDir)) {
|
|
18164
|
+
mkdirSync(binDir, { recursive: true });
|
|
18165
|
+
}
|
|
18166
|
+
const assetName = `zefiro-overlay-${OVERLAY_VERSION}-darwin-${arch}.tar.gz`;
|
|
18167
|
+
const checksumName = `zefiro-overlay-${OVERLAY_VERSION}-checksums.sha256`;
|
|
18168
|
+
const baseUrl = `https://github.com/${GITHUB_REPO}/releases/download/overlay-v${OVERLAY_VERSION}`;
|
|
18169
|
+
const tarPath = join3(binDir, assetName);
|
|
18170
|
+
const binPath = join3(binDir, "zefiro-overlay");
|
|
18171
|
+
try {
|
|
18172
|
+
await fetchWithProgress(`${baseUrl}/${assetName}`, tarPath);
|
|
18173
|
+
try {
|
|
18174
|
+
const checksumResponse = await fetch(`${baseUrl}/${checksumName}`);
|
|
18175
|
+
if (checksumResponse.ok) {
|
|
18176
|
+
const checksumText = await checksumResponse.text();
|
|
18177
|
+
const expectedHash = checksumText.split(`
|
|
18178
|
+
`).find((line) => line.includes(assetName))?.split(/\s+/)[0];
|
|
18179
|
+
if (expectedHash) {
|
|
18180
|
+
const valid = await verifyChecksum(tarPath, expectedHash);
|
|
18181
|
+
if (!valid) {
|
|
18182
|
+
console.log(" Checksum verification failed. Download may be corrupted.");
|
|
18183
|
+
await unlink(tarPath).catch(() => {});
|
|
18184
|
+
return false;
|
|
18185
|
+
}
|
|
18186
|
+
}
|
|
18187
|
+
}
|
|
18188
|
+
} catch {}
|
|
18189
|
+
const { execSync } = await import("node:child_process");
|
|
18190
|
+
execSync(`tar -xzf "${tarPath}" -C "${binDir}"`, { stdio: "ignore" });
|
|
18191
|
+
if (existsSync3(binPath)) {
|
|
18192
|
+
chmodSync(binPath, 493);
|
|
18193
|
+
}
|
|
18194
|
+
await unlink(tarPath).catch(() => {});
|
|
18195
|
+
console.log(` Installed to ${binPath}`);
|
|
18196
|
+
return true;
|
|
18197
|
+
} catch (err) {
|
|
18198
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
18199
|
+
if (!options?.silent) {
|
|
18200
|
+
console.log(` Download failed: ${message}`);
|
|
18201
|
+
}
|
|
18202
|
+
await unlink(tarPath).catch(() => {});
|
|
18203
|
+
return false;
|
|
18204
|
+
}
|
|
18205
|
+
}
|
|
18206
|
+
|
|
18207
|
+
// src/copilot/overlay-launcher.ts
|
|
18208
|
+
var OVERLAY_BIN_NAME = "zefiro-overlay";
|
|
18209
|
+
function searchPaths() {
|
|
18210
|
+
const home = homedir();
|
|
18211
|
+
return [
|
|
18212
|
+
join4(home, ".qai", "bin", OVERLAY_BIN_NAME),
|
|
18213
|
+
join4("/opt", "homebrew", "bin", OVERLAY_BIN_NAME),
|
|
18214
|
+
join4("/usr", "local", "bin", OVERLAY_BIN_NAME)
|
|
18215
|
+
];
|
|
18216
|
+
}
|
|
18217
|
+
async function resolveOverlayBinary(options) {
|
|
18218
|
+
const envPath = process.env.ZEFIRO_OVERLAY_PATH;
|
|
18219
|
+
if (envPath) {
|
|
18220
|
+
return existsSync4(envPath) ? envPath : null;
|
|
18221
|
+
}
|
|
18222
|
+
if (process.env.ZEFIRO_NO_OVERLAY === "1")
|
|
18223
|
+
return null;
|
|
18224
|
+
for (const p of searchPaths()) {
|
|
18225
|
+
if (existsSync4(p))
|
|
18226
|
+
return p;
|
|
18227
|
+
}
|
|
18228
|
+
if (options?.skipDownload)
|
|
18229
|
+
return null;
|
|
18230
|
+
if (!process.stdin.isTTY)
|
|
18231
|
+
return null;
|
|
18232
|
+
const binDir = join4(homedir(), ".qai", "bin");
|
|
18233
|
+
const downloaded = await downloadOverlay(binDir, { silent: options?.silent });
|
|
18234
|
+
if (downloaded) {
|
|
18235
|
+
return join4(binDir, OVERLAY_BIN_NAME);
|
|
18236
|
+
}
|
|
18237
|
+
return null;
|
|
18238
|
+
}
|
|
18239
|
+
function spawnOverlay(binPath, wsPort) {
|
|
18240
|
+
try {
|
|
18241
|
+
const child = spawn(binPath, ["--ws-port", String(wsPort)], {
|
|
18242
|
+
detached: true,
|
|
18243
|
+
stdio: "ignore"
|
|
18244
|
+
});
|
|
18245
|
+
child.unref();
|
|
18246
|
+
return child;
|
|
18247
|
+
} catch {
|
|
18248
|
+
return null;
|
|
18249
|
+
}
|
|
18250
|
+
}
|
|
18251
|
+
|
|
18252
|
+
// src/copilot/tools.ts
|
|
18253
|
+
var browsers = new Map;
|
|
18254
|
+
function syncAction(action, result, source = "agent") {
|
|
18255
|
+
addActionToLog({ action, timestamp: new Date().toISOString(), result, source });
|
|
18256
|
+
}
|
|
18257
|
+
async function handleCopilotStart(params) {
|
|
18258
|
+
const mode = params.mode || "auto";
|
|
18259
|
+
const cwd = process.cwd();
|
|
18260
|
+
const outputDir = params.outputDir ? join5(cwd, params.outputDir) : join5(cwd, "copilot-report");
|
|
18261
|
+
const session = createSession(params.baseUrl, mode, outputDir);
|
|
18262
|
+
const browser = new AgentBrowser({
|
|
18263
|
+
session: `copilot-${session.id}`,
|
|
18264
|
+
headed: params.headed ?? true
|
|
18265
|
+
});
|
|
18266
|
+
browsers.set(session.id, browser);
|
|
18267
|
+
if (params.authState) {
|
|
18268
|
+
const config = {
|
|
18269
|
+
baseUrl: params.baseUrl,
|
|
18270
|
+
outputDir,
|
|
18271
|
+
maxPages: params.maxPages ?? 100,
|
|
18272
|
+
excludePatterns: [],
|
|
18273
|
+
headed: params.headed ?? true,
|
|
18274
|
+
waitStrategy: "networkidle",
|
|
18275
|
+
waitDelay: 1500,
|
|
18276
|
+
pageTimeout: 30000,
|
|
18277
|
+
auth: { method: "state-file", stateFile: params.authState },
|
|
18278
|
+
sessionName: `copilot-${session.id}`
|
|
18279
|
+
};
|
|
18280
|
+
await authenticate(browser, config);
|
|
18281
|
+
}
|
|
18282
|
+
await browser.setViewport(1440, 900);
|
|
18283
|
+
const companion = await startCompanionServer(mode, 0);
|
|
18284
|
+
session.companionPort = companion.port;
|
|
18285
|
+
const overlayBin = await resolveOverlayBinary({ silent: true });
|
|
18286
|
+
if (overlayBin) {
|
|
18287
|
+
const child = spawnOverlay(overlayBin, companion.port);
|
|
18288
|
+
if (child?.pid)
|
|
18289
|
+
session.overlayPid = child.pid;
|
|
18290
|
+
}
|
|
18291
|
+
await browser.open(params.baseUrl);
|
|
18292
|
+
await browser.waitForLoad("networkidle", 1500);
|
|
18293
|
+
logAction(session, `copilot_start: ${params.baseUrl} (mode: ${mode})`);
|
|
18294
|
+
syncAction(`copilot_start: ${params.baseUrl} (mode: ${mode})`);
|
|
18295
|
+
return {
|
|
18296
|
+
session_id: session.id,
|
|
18297
|
+
mode: session.mode,
|
|
18298
|
+
base_url: params.baseUrl,
|
|
18299
|
+
output_dir: outputDir,
|
|
18300
|
+
companion_url: `http://localhost:${companion.port}`
|
|
18301
|
+
};
|
|
18302
|
+
}
|
|
18303
|
+
async function handleCopilotStop(params) {
|
|
18304
|
+
const session = getSession(params.session_id);
|
|
18305
|
+
if (!session)
|
|
18306
|
+
throw new Error(`Session not found: ${params.session_id}`);
|
|
18307
|
+
const browser = browsers.get(params.session_id);
|
|
18308
|
+
const state2 = {
|
|
18309
|
+
baseUrl: session.baseUrl,
|
|
18310
|
+
startedAt: session.startedAt,
|
|
18311
|
+
completedAt: new Date().toISOString(),
|
|
18312
|
+
pages: session.pages,
|
|
18313
|
+
queue: [],
|
|
18314
|
+
topology: session.topology
|
|
18315
|
+
};
|
|
18316
|
+
const readme = generateReadme(state2);
|
|
18317
|
+
writeFile(join5(session.outputDir, "README.md"), readme);
|
|
18318
|
+
const html = generateIndexHtml(state2);
|
|
18319
|
+
writeFile(join5(session.outputDir, "index.html"), html);
|
|
18320
|
+
logAction(session, "copilot_stop: report generated");
|
|
18321
|
+
syncAction("copilot_stop: report generated");
|
|
18322
|
+
if (browser) {
|
|
18323
|
+
await browser.close();
|
|
18324
|
+
browsers.delete(params.session_id);
|
|
18325
|
+
}
|
|
18326
|
+
stopCompanionServer();
|
|
18327
|
+
if (session.overlayPid) {
|
|
18328
|
+
try {
|
|
18329
|
+
process.kill(session.overlayPid);
|
|
18330
|
+
} catch {}
|
|
18331
|
+
}
|
|
18332
|
+
const reportPath = join5(session.outputDir, "README.md");
|
|
18333
|
+
destroySession(params.session_id);
|
|
18334
|
+
return { report_path: reportPath, output_dir: session.outputDir };
|
|
18335
|
+
}
|
|
18336
|
+
function requireBrowser(sessionId) {
|
|
18337
|
+
const session = getSession(sessionId);
|
|
18338
|
+
if (!session)
|
|
18339
|
+
throw new Error(`Session not found: ${sessionId}`);
|
|
18340
|
+
const browser = browsers.get(sessionId);
|
|
18341
|
+
if (!browser)
|
|
18342
|
+
throw new Error(`Browser not found for session: ${sessionId}`);
|
|
18343
|
+
return { browser, session };
|
|
18344
|
+
}
|
|
18345
|
+
async function handleCopilotNavigate(params) {
|
|
18346
|
+
const { browser, session } = requireBrowser(params.session_id);
|
|
18347
|
+
await browser.open(params.url);
|
|
18348
|
+
await browser.waitForLoad("networkidle", 1500);
|
|
18349
|
+
const actualUrl = await browser.getUrl();
|
|
18350
|
+
const title = await browser.getTitle();
|
|
18351
|
+
logAction(session, `navigate: ${params.url}`, `title: ${title}, actual: ${actualUrl}`);
|
|
18352
|
+
syncAction(`navigate: ${params.url}`, `title: ${title}, actual: ${actualUrl}`);
|
|
18353
|
+
updateCurrentPage(actualUrl, title);
|
|
18354
|
+
return { title, actual_url: actualUrl, redirected: actualUrl !== params.url };
|
|
18355
|
+
}
|
|
18356
|
+
async function handleCopilotSnapshot(params) {
|
|
18357
|
+
const { browser, session } = requireBrowser(params.session_id);
|
|
18358
|
+
const elements = await browser.snapshot();
|
|
18359
|
+
const url = await browser.getUrl();
|
|
18360
|
+
const title = await browser.getTitle();
|
|
18361
|
+
logAction(session, `snapshot: ${elements.length} elements`);
|
|
18362
|
+
syncAction(`snapshot: ${elements.length} elements`);
|
|
18363
|
+
return {
|
|
18364
|
+
url,
|
|
18365
|
+
title,
|
|
18366
|
+
elements: elements.map((e) => ({ ref: e.ref, role: e.role, label: e.label })),
|
|
18367
|
+
element_count: elements.length
|
|
18368
|
+
};
|
|
18369
|
+
}
|
|
18370
|
+
async function handleCopilotScreenshot(params) {
|
|
18371
|
+
const { browser, session } = requireBrowser(params.session_id);
|
|
18372
|
+
const url = await browser.getUrl();
|
|
18373
|
+
const slug = urlToSlug(url, session.baseUrl);
|
|
18374
|
+
const label = params.label ?? "default";
|
|
18375
|
+
const filename = `${slug}-${label}.png`;
|
|
18376
|
+
const filePath = join5(session.outputDir, "screenshots", slug, `${label}.png`);
|
|
18377
|
+
ensureDir(join5(session.outputDir, "screenshots", slug));
|
|
18378
|
+
await browser.screenshot(filePath);
|
|
18379
|
+
logAction(session, `screenshot: ${filename}`);
|
|
18380
|
+
syncAction(`screenshot: ${filename}`);
|
|
18381
|
+
return { file_path: filePath, slug, label };
|
|
18382
|
+
}
|
|
18383
|
+
async function handleCopilotClick(params) {
|
|
18384
|
+
const { browser, session } = requireBrowser(params.session_id);
|
|
18385
|
+
const urlBefore = await browser.getUrl();
|
|
18386
|
+
await browser.click(params.ref);
|
|
18387
|
+
await browser.waitForLoad("load", 500);
|
|
18388
|
+
const urlAfter = await browser.getUrl();
|
|
18389
|
+
const navigated = urlAfter !== urlBefore;
|
|
18390
|
+
logAction(session, `click: ${params.ref}`, navigated ? `navigated to ${urlAfter}` : "no navigation");
|
|
18391
|
+
syncAction(`click: ${params.ref}`, navigated ? `navigated to ${urlAfter}` : "no navigation");
|
|
18392
|
+
return { success: true, navigated, new_url: navigated ? urlAfter : undefined };
|
|
18393
|
+
}
|
|
18394
|
+
async function handleCopilotFill(params) {
|
|
18395
|
+
const { browser, session } = requireBrowser(params.session_id);
|
|
18396
|
+
await browser.fill(params.ref, params.text);
|
|
18397
|
+
logAction(session, `fill: ${params.ref} = "${params.text}"`);
|
|
18398
|
+
syncAction(`fill: ${params.ref} = "${params.text}"`);
|
|
18399
|
+
return { success: true };
|
|
18400
|
+
}
|
|
18401
|
+
async function handleCopilotGetLinks(params) {
|
|
18402
|
+
const { browser, session } = requireBrowser(params.session_id);
|
|
18403
|
+
const elements = await browser.snapshot();
|
|
18404
|
+
const linkElements = elements.filter((e) => e.role === "link");
|
|
18405
|
+
const currentUrl = await browser.getUrl();
|
|
18406
|
+
const visited = new Set(Array.from(session.pages.keys()));
|
|
18407
|
+
const links = [];
|
|
18408
|
+
for (const el of linkElements) {
|
|
18409
|
+
try {
|
|
18410
|
+
const href = await browser.getAttribute(el.ref, "href");
|
|
18411
|
+
if (href) {
|
|
18412
|
+
const normalized = normalizeUrl(href, currentUrl);
|
|
18413
|
+
links.push({ ref: el.ref, label: el.label, url: normalized, visited: visited.has(normalized) });
|
|
18414
|
+
}
|
|
18415
|
+
} catch {
|
|
18416
|
+
links.push({ ref: el.ref, label: el.label, visited: false });
|
|
18417
|
+
}
|
|
18418
|
+
}
|
|
18419
|
+
logAction(session, `get_links: ${links.length} links found`);
|
|
18420
|
+
syncAction(`get_links: ${links.length} links found`);
|
|
18421
|
+
return { links, total: links.length, unvisited: links.filter((l) => !l.visited).length };
|
|
18422
|
+
}
|
|
18423
|
+
async function handleCopilotRecordPage(params) {
|
|
18424
|
+
const { browser, session } = requireBrowser(params.session_id);
|
|
18425
|
+
const url = await browser.getUrl();
|
|
18426
|
+
const title = await browser.getTitle();
|
|
18427
|
+
const normalizedUrl = normalizeUrl(url, session.baseUrl);
|
|
18428
|
+
let page = session.pages.get(normalizedUrl);
|
|
18429
|
+
if (!page) {
|
|
18430
|
+
page = {
|
|
18431
|
+
url: normalizedUrl,
|
|
18432
|
+
path: new URL(normalizedUrl).pathname,
|
|
18433
|
+
title,
|
|
18434
|
+
slug: params.slug,
|
|
18435
|
+
depth: new URL(normalizedUrl).pathname.split("/").filter(Boolean).length,
|
|
18436
|
+
screenshot: `screenshots/${params.slug}/default.png`,
|
|
18437
|
+
elements: [],
|
|
18438
|
+
outgoingLinks: [],
|
|
18439
|
+
status: "visited",
|
|
18440
|
+
visitedAt: new Date().toISOString()
|
|
18441
|
+
};
|
|
18442
|
+
}
|
|
18443
|
+
page.analysis = params.analysis;
|
|
18444
|
+
page.status = "visited";
|
|
18445
|
+
page.visitedAt = new Date().toISOString();
|
|
18446
|
+
addPage(session, page);
|
|
18447
|
+
const relatedPages = Array.from(session.pages.values()).filter((p) => p.status === "visited").map((p) => ({ slug: p.slug, name: p.analysis?.pageName ?? p.title, path: p.path }));
|
|
18448
|
+
const pageAnnotations = session.annotations.filter((a) => a.pageSlug === params.slug).map((a) => ({ text: a.text, timestamp: a.timestamp }));
|
|
18449
|
+
const markdown = generateSectionMarkdown(page, relatedPages, pageAnnotations);
|
|
18450
|
+
writeFile(join5(session.outputDir, "sections", `${params.slug}.md`), markdown);
|
|
18451
|
+
logAction(session, `record_page: ${params.slug}`, `components: ${params.analysis.sections?.length ?? 0}`);
|
|
18452
|
+
syncAction(`record_page: ${params.slug}`, `components: ${params.analysis.sections?.length ?? 0}`);
|
|
18453
|
+
return { success: true, page_url: normalizedUrl, slug: params.slug };
|
|
18454
|
+
}
|
|
18455
|
+
async function handleCopilotGetSession(params) {
|
|
18456
|
+
const session = getSession(params.session_id);
|
|
18457
|
+
if (!session)
|
|
18458
|
+
throw new Error(`Session not found: ${params.session_id}`);
|
|
18459
|
+
return serializeSession(session);
|
|
18460
|
+
}
|
|
18461
|
+
async function handleCopilotCheckInput(params) {
|
|
18462
|
+
const session = getSession(params.session_id);
|
|
18463
|
+
if (!session)
|
|
18464
|
+
throw new Error(`Session not found: ${params.session_id}`);
|
|
18465
|
+
const companionInputs = getCompanionVoiceQueue();
|
|
18466
|
+
for (const input of companionInputs) {
|
|
18467
|
+
enqueueVoiceInput(session, input);
|
|
18468
|
+
}
|
|
18469
|
+
const { commands, comments } = drainVoiceQueue(session);
|
|
18470
|
+
const companionProposal = getCompanionProposalResult();
|
|
18471
|
+
let proposalResult = null;
|
|
18472
|
+
if (companionProposal && companionProposal.status !== "pending") {
|
|
18473
|
+
proposalResult = {
|
|
18474
|
+
id: companionProposal.id,
|
|
18475
|
+
action: companionProposal.action,
|
|
18476
|
+
approved: companionProposal.status === "approved",
|
|
18477
|
+
feedback: companionProposal.feedback
|
|
18478
|
+
};
|
|
18479
|
+
resolveProposal(session, companionProposal.status === "approved", companionProposal.feedback);
|
|
18480
|
+
}
|
|
18481
|
+
return {
|
|
18482
|
+
commands: commands.map((c) => ({ text: c.text, timestamp: c.timestamp })),
|
|
18483
|
+
comments: comments.map((c) => ({ text: c.text, timestamp: c.timestamp, pageSlug: c.pageSlug })),
|
|
18484
|
+
proposal_result: proposalResult,
|
|
18485
|
+
mode: session.mode
|
|
18486
|
+
};
|
|
18487
|
+
}
|
|
18488
|
+
async function handleCopilotProposeAction(params) {
|
|
18489
|
+
const session = getSession(params.session_id);
|
|
18490
|
+
if (!session)
|
|
18491
|
+
throw new Error(`Session not found: ${params.session_id}`);
|
|
18492
|
+
const proposal = setProposal(session, params.action, params.reason);
|
|
18493
|
+
setCompanionProposal(proposal);
|
|
18494
|
+
return {
|
|
18495
|
+
proposal_id: proposal.id,
|
|
18496
|
+
action: params.action,
|
|
18497
|
+
reason: params.reason,
|
|
18498
|
+
status: "pending",
|
|
18499
|
+
message: "Proposal sent to companion UI. Call copilot_check_input to get the result."
|
|
18500
|
+
};
|
|
18501
|
+
}
|
|
18502
|
+
|
|
14921
18503
|
// src/mcp.ts
|
|
14922
18504
|
var SERVER_INSTRUCTIONS = `
|
|
14923
18505
|
# Zefiro — Browser-Based Application Explorer
|
|
@@ -14949,6 +18531,31 @@ app-report/
|
|
|
14949
18531
|
- \`zefiro_explore\` — explore a web application via BFS, generating documentation and screenshots
|
|
14950
18532
|
- \`zefiro_read_docs\` — read generated documentation (README or specific section files)
|
|
14951
18533
|
- \`zefiro_scan_codebase\` — scan project for test files, configs, and path aliases (static analysis helper)
|
|
18534
|
+
|
|
18535
|
+
## Copilot — Interactive Browser Co-Pilot
|
|
18536
|
+
|
|
18537
|
+
Use these tools when the user asks to explore, map, or document a running web application interactively.
|
|
18538
|
+
|
|
18539
|
+
### Workflow
|
|
18540
|
+
1. \`zefiro_copilot_start({ baseUrl, mode })\` — Launch browser + session
|
|
18541
|
+
2. Navigate + snapshot + analyze pages using browser tools
|
|
18542
|
+
3. Record findings with \`zefiro_copilot_record_page\`
|
|
18543
|
+
4. Check for human input periodically with \`zefiro_copilot_check_input\`
|
|
18544
|
+
5. Generate report with \`zefiro_copilot_stop\`
|
|
18545
|
+
|
|
18546
|
+
### Copilot Tools
|
|
18547
|
+
- \`zefiro_copilot_start\` — Start a copilot session (launches browser)
|
|
18548
|
+
- \`zefiro_copilot_stop\` — Stop session, generate report, close browser
|
|
18549
|
+
- \`zefiro_copilot_navigate\` — Navigate to a URL
|
|
18550
|
+
- \`zefiro_copilot_snapshot\` — Get accessibility tree of current page
|
|
18551
|
+
- \`zefiro_copilot_screenshot\` — Capture screenshot
|
|
18552
|
+
- \`zefiro_copilot_click\` — Click element by ref
|
|
18553
|
+
- \`zefiro_copilot_fill\` — Fill input field
|
|
18554
|
+
- \`zefiro_copilot_get_links\` — Get outgoing links from current page
|
|
18555
|
+
- \`zefiro_copilot_record_page\` — Save page analysis to session
|
|
18556
|
+
- \`zefiro_copilot_get_session\` — Get session state (explored pages, stats)
|
|
18557
|
+
- \`zefiro_copilot_check_input\` — Read pending human input (voice/text)
|
|
18558
|
+
- \`zefiro_copilot_propose_action\` — ASSIST mode: propose action for approval
|
|
14952
18559
|
`.trim();
|
|
14953
18560
|
var server = new McpServer({ name: "zefiro", version: "0.7.4" }, { instructions: SERVER_INSTRUCTIONS });
|
|
14954
18561
|
server.registerTool("zefiro_explore", {
|
|
@@ -14962,12 +18569,12 @@ server.registerTool("zefiro_explore", {
|
|
|
14962
18569
|
})
|
|
14963
18570
|
}, async ({ baseUrl, maxPages, outputDir, headed }) => {
|
|
14964
18571
|
try {
|
|
14965
|
-
const { runExploration } = await import("./explorer-
|
|
14966
|
-
const { generateReadme, generateIndexHtml } = await import("./report-
|
|
14967
|
-
const { writeFile, ensureDir } = await import("./fs-9dhtxzmg.js");
|
|
18572
|
+
const { runExploration } = await import("./explorer-g6bmbak1.js");
|
|
18573
|
+
const { generateReadme: generateReadme2, generateIndexHtml: generateIndexHtml2 } = await import("./report-sdtah1f4.js");
|
|
18574
|
+
const { writeFile: writeFile2, ensureDir: ensureDir2 } = await import("./fs-9dhtxzmg.js");
|
|
14968
18575
|
const cwd = process.cwd();
|
|
14969
|
-
const outDir = outputDir ?
|
|
14970
|
-
|
|
18576
|
+
const outDir = outputDir ? join6(cwd, outputDir) : join6(cwd, "app-report");
|
|
18577
|
+
ensureDir2(outDir);
|
|
14971
18578
|
const config = {
|
|
14972
18579
|
baseUrl,
|
|
14973
18580
|
outputDir: outDir,
|
|
@@ -14980,13 +18587,13 @@ server.registerTool("zefiro_explore", {
|
|
|
14980
18587
|
auth: { method: "manual" },
|
|
14981
18588
|
sessionName: "zefiro"
|
|
14982
18589
|
};
|
|
14983
|
-
const
|
|
14984
|
-
const readme =
|
|
14985
|
-
|
|
14986
|
-
const html =
|
|
14987
|
-
|
|
14988
|
-
const visited = Array.from(
|
|
14989
|
-
const errors = Array.from(
|
|
18590
|
+
const state2 = await runExploration(config);
|
|
18591
|
+
const readme = generateReadme2(state2);
|
|
18592
|
+
writeFile2(join6(outDir, "README.md"), readme);
|
|
18593
|
+
const html = generateIndexHtml2(state2);
|
|
18594
|
+
writeFile2(join6(outDir, "index.html"), html);
|
|
18595
|
+
const visited = Array.from(state2.pages.values()).filter((p) => p.status === "visited");
|
|
18596
|
+
const errors = Array.from(state2.pages.values()).filter((p) => p.status === "error");
|
|
14990
18597
|
return {
|
|
14991
18598
|
content: [{
|
|
14992
18599
|
type: "text",
|
|
@@ -15018,13 +18625,13 @@ server.registerTool("zefiro_read_docs", {
|
|
|
15018
18625
|
}, async ({ section, outputDir }) => {
|
|
15019
18626
|
try {
|
|
15020
18627
|
const cwd = process.cwd();
|
|
15021
|
-
const outDir = outputDir ?
|
|
18628
|
+
const outDir = outputDir ? join6(cwd, outputDir) : join6(cwd, "app-report");
|
|
15022
18629
|
if (section) {
|
|
15023
|
-
const sectionPath =
|
|
15024
|
-
if (!
|
|
15025
|
-
const sectionsDir =
|
|
18630
|
+
const sectionPath = join6(outDir, "sections", `${section}.md`);
|
|
18631
|
+
if (!existsSync5(sectionPath)) {
|
|
18632
|
+
const sectionsDir = join6(outDir, "sections");
|
|
15026
18633
|
let available = [];
|
|
15027
|
-
if (
|
|
18634
|
+
if (existsSync5(sectionsDir)) {
|
|
15028
18635
|
available = readdirSync2(sectionsDir).filter((f) => f.endsWith(".md")).map((f) => f.replace(".md", ""));
|
|
15029
18636
|
}
|
|
15030
18637
|
return {
|
|
@@ -15035,10 +18642,10 @@ server.registerTool("zefiro_read_docs", {
|
|
|
15035
18642
|
isError: true
|
|
15036
18643
|
};
|
|
15037
18644
|
}
|
|
15038
|
-
return { content: [{ type: "text", text:
|
|
18645
|
+
return { content: [{ type: "text", text: readFileSync3(sectionPath, "utf-8") }] };
|
|
15039
18646
|
}
|
|
15040
|
-
const readmePath =
|
|
15041
|
-
if (!
|
|
18647
|
+
const readmePath = join6(outDir, "README.md");
|
|
18648
|
+
if (!existsSync5(readmePath)) {
|
|
15042
18649
|
return {
|
|
15043
18650
|
content: [{
|
|
15044
18651
|
type: "text",
|
|
@@ -15047,7 +18654,7 @@ server.registerTool("zefiro_read_docs", {
|
|
|
15047
18654
|
isError: true
|
|
15048
18655
|
};
|
|
15049
18656
|
}
|
|
15050
|
-
return { content: [{ type: "text", text:
|
|
18657
|
+
return { content: [{ type: "text", text: readFileSync3(readmePath, "utf-8") }] };
|
|
15051
18658
|
} catch (err) {
|
|
15052
18659
|
return { content: [{ type: "text", text: `Error reading docs: ${err.message}` }], isError: true };
|
|
15053
18660
|
}
|
|
@@ -15067,6 +18674,209 @@ server.registerTool("zefiro_scan_codebase", {
|
|
|
15067
18674
|
return { content: [{ type: "text", text: `Error scanning codebase: ${err.message}` }], isError: true };
|
|
15068
18675
|
}
|
|
15069
18676
|
});
|
|
18677
|
+
server.registerTool("zefiro_copilot_start", {
|
|
18678
|
+
title: "Start Copilot Session",
|
|
18679
|
+
description: "Start an interactive copilot session. Launches a browser and creates a session. " + "Returns a session_id to use with all other copilot tools.",
|
|
18680
|
+
inputSchema: exports_external.object({
|
|
18681
|
+
baseUrl: exports_external.string().describe("Base URL of the application (e.g., http://localhost:3000)"),
|
|
18682
|
+
mode: exports_external.enum(["auto", "assist", "manual"]).optional().describe("Copilot mode: auto (AI drives), assist (AI proposes, human approves), manual (human drives)"),
|
|
18683
|
+
headed: exports_external.boolean().optional().describe("Show browser window (default: true)"),
|
|
18684
|
+
authState: exports_external.string().optional().describe("Path to Playwright auth state JSON file"),
|
|
18685
|
+
outputDir: exports_external.string().optional().describe("Output directory for report (default: copilot-report)")
|
|
18686
|
+
})
|
|
18687
|
+
}, async (params) => {
|
|
18688
|
+
try {
|
|
18689
|
+
const result = await handleCopilotStart(params);
|
|
18690
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18691
|
+
} catch (err) {
|
|
18692
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18693
|
+
}
|
|
18694
|
+
});
|
|
18695
|
+
server.registerTool("zefiro_copilot_stop", {
|
|
18696
|
+
title: "Stop Copilot Session",
|
|
18697
|
+
description: "Stop a copilot session, generate the final report, and close the browser.",
|
|
18698
|
+
inputSchema: exports_external.object({
|
|
18699
|
+
session_id: exports_external.string().describe("Session ID returned by copilot_start")
|
|
18700
|
+
})
|
|
18701
|
+
}, async (params) => {
|
|
18702
|
+
try {
|
|
18703
|
+
const result = await handleCopilotStop(params);
|
|
18704
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18705
|
+
} catch (err) {
|
|
18706
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18707
|
+
}
|
|
18708
|
+
});
|
|
18709
|
+
server.registerTool("zefiro_copilot_navigate", {
|
|
18710
|
+
title: "Navigate to URL",
|
|
18711
|
+
description: "Navigate the copilot browser to a specific URL.",
|
|
18712
|
+
inputSchema: exports_external.object({
|
|
18713
|
+
session_id: exports_external.string().describe("Session ID"),
|
|
18714
|
+
url: exports_external.string().describe("URL to navigate to")
|
|
18715
|
+
})
|
|
18716
|
+
}, async (params) => {
|
|
18717
|
+
try {
|
|
18718
|
+
const result = await handleCopilotNavigate(params);
|
|
18719
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18720
|
+
} catch (err) {
|
|
18721
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18722
|
+
}
|
|
18723
|
+
});
|
|
18724
|
+
server.registerTool("zefiro_copilot_snapshot", {
|
|
18725
|
+
title: "Get Accessibility Snapshot",
|
|
18726
|
+
description: "Get the accessibility tree of the current page. Returns element refs, roles, and labels.",
|
|
18727
|
+
inputSchema: exports_external.object({
|
|
18728
|
+
session_id: exports_external.string().describe("Session ID")
|
|
18729
|
+
})
|
|
18730
|
+
}, async (params) => {
|
|
18731
|
+
try {
|
|
18732
|
+
const result = await handleCopilotSnapshot(params);
|
|
18733
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18734
|
+
} catch (err) {
|
|
18735
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18736
|
+
}
|
|
18737
|
+
});
|
|
18738
|
+
server.registerTool("zefiro_copilot_screenshot", {
|
|
18739
|
+
title: "Capture Screenshot",
|
|
18740
|
+
description: "Capture a screenshot of the current page.",
|
|
18741
|
+
inputSchema: exports_external.object({
|
|
18742
|
+
session_id: exports_external.string().describe("Session ID"),
|
|
18743
|
+
label: exports_external.string().optional().describe('Label for the screenshot (default: "default")')
|
|
18744
|
+
})
|
|
18745
|
+
}, async (params) => {
|
|
18746
|
+
try {
|
|
18747
|
+
const result = await handleCopilotScreenshot(params);
|
|
18748
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18749
|
+
} catch (err) {
|
|
18750
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18751
|
+
}
|
|
18752
|
+
});
|
|
18753
|
+
server.registerTool("zefiro_copilot_click", {
|
|
18754
|
+
title: "Click Element",
|
|
18755
|
+
description: "Click an element by its accessibility ref (from snapshot).",
|
|
18756
|
+
inputSchema: exports_external.object({
|
|
18757
|
+
session_id: exports_external.string().describe("Session ID"),
|
|
18758
|
+
ref: exports_external.string().describe("Element ref from snapshot")
|
|
18759
|
+
})
|
|
18760
|
+
}, async (params) => {
|
|
18761
|
+
try {
|
|
18762
|
+
const result = await handleCopilotClick(params);
|
|
18763
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18764
|
+
} catch (err) {
|
|
18765
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18766
|
+
}
|
|
18767
|
+
});
|
|
18768
|
+
server.registerTool("zefiro_copilot_fill", {
|
|
18769
|
+
title: "Fill Input Field",
|
|
18770
|
+
description: "Fill a text input field by its accessibility ref.",
|
|
18771
|
+
inputSchema: exports_external.object({
|
|
18772
|
+
session_id: exports_external.string().describe("Session ID"),
|
|
18773
|
+
ref: exports_external.string().describe("Element ref from snapshot"),
|
|
18774
|
+
text: exports_external.string().describe("Text to fill into the input")
|
|
18775
|
+
})
|
|
18776
|
+
}, async (params) => {
|
|
18777
|
+
try {
|
|
18778
|
+
const result = await handleCopilotFill(params);
|
|
18779
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18780
|
+
} catch (err) {
|
|
18781
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18782
|
+
}
|
|
18783
|
+
});
|
|
18784
|
+
server.registerTool("zefiro_copilot_get_links", {
|
|
18785
|
+
title: "Get Page Links",
|
|
18786
|
+
description: "Get all outgoing links from the current page, with visited status.",
|
|
18787
|
+
inputSchema: exports_external.object({
|
|
18788
|
+
session_id: exports_external.string().describe("Session ID")
|
|
18789
|
+
})
|
|
18790
|
+
}, async (params) => {
|
|
18791
|
+
try {
|
|
18792
|
+
const result = await handleCopilotGetLinks(params);
|
|
18793
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18794
|
+
} catch (err) {
|
|
18795
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18796
|
+
}
|
|
18797
|
+
});
|
|
18798
|
+
server.registerTool("zefiro_copilot_record_page", {
|
|
18799
|
+
title: "Record Page Analysis",
|
|
18800
|
+
description: "Save a page analysis to the session. Call after analyzing a page via snapshot/screenshot.",
|
|
18801
|
+
inputSchema: exports_external.object({
|
|
18802
|
+
session_id: exports_external.string().describe("Session ID"),
|
|
18803
|
+
slug: exports_external.string().describe('URL slug for the page (e.g., "dashboard", "settings-general")'),
|
|
18804
|
+
analysis: exports_external.object({
|
|
18805
|
+
pageName: exports_external.string().describe("Human-readable page name"),
|
|
18806
|
+
description: exports_external.string().describe("Brief description of the page"),
|
|
18807
|
+
route: exports_external.string().describe("Route pattern (e.g., /dashboard, /settings/:tab)"),
|
|
18808
|
+
sections: exports_external.array(exports_external.object({
|
|
18809
|
+
name: exports_external.string(),
|
|
18810
|
+
type: exports_external.string(),
|
|
18811
|
+
elements: exports_external.array(exports_external.object({
|
|
18812
|
+
name: exports_external.string(),
|
|
18813
|
+
type: exports_external.string(),
|
|
18814
|
+
required: exports_external.boolean().optional(),
|
|
18815
|
+
default: exports_external.string().nullable().optional(),
|
|
18816
|
+
description: exports_external.string()
|
|
18817
|
+
})).optional().default([])
|
|
18818
|
+
})).default([]),
|
|
18819
|
+
actions: exports_external.array(exports_external.string()).default([]),
|
|
18820
|
+
navigation: exports_external.object({
|
|
18821
|
+
tabs: exports_external.array(exports_external.string()).optional().default([]),
|
|
18822
|
+
breadcrumbs: exports_external.array(exports_external.string()).optional().default([]),
|
|
18823
|
+
sidebarItems: exports_external.array(exports_external.string()).optional().default([])
|
|
18824
|
+
}).optional().default({ tabs: [], breadcrumbs: [], sidebarItems: [] }),
|
|
18825
|
+
observations: exports_external.array(exports_external.string()).default([])
|
|
18826
|
+
}).describe("Structured page analysis")
|
|
18827
|
+
})
|
|
18828
|
+
}, async (params) => {
|
|
18829
|
+
try {
|
|
18830
|
+
const result = await handleCopilotRecordPage(params);
|
|
18831
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18832
|
+
} catch (err) {
|
|
18833
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18834
|
+
}
|
|
18835
|
+
});
|
|
18836
|
+
server.registerTool("zefiro_copilot_get_session", {
|
|
18837
|
+
title: "Get Session State",
|
|
18838
|
+
description: "Get the current session state including explored pages, stats, and mode.",
|
|
18839
|
+
inputSchema: exports_external.object({
|
|
18840
|
+
session_id: exports_external.string().describe("Session ID")
|
|
18841
|
+
})
|
|
18842
|
+
}, async (params) => {
|
|
18843
|
+
try {
|
|
18844
|
+
const result = await handleCopilotGetSession(params);
|
|
18845
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18846
|
+
} catch (err) {
|
|
18847
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18848
|
+
}
|
|
18849
|
+
});
|
|
18850
|
+
server.registerTool("zefiro_copilot_check_input", {
|
|
18851
|
+
title: "Check Human Input",
|
|
18852
|
+
description: "Read pending human input (voice commands, text comments, proposal results). " + "Call periodically during exploration to check for user guidance.",
|
|
18853
|
+
inputSchema: exports_external.object({
|
|
18854
|
+
session_id: exports_external.string().describe("Session ID")
|
|
18855
|
+
})
|
|
18856
|
+
}, async (params) => {
|
|
18857
|
+
try {
|
|
18858
|
+
const result = await handleCopilotCheckInput(params);
|
|
18859
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18860
|
+
} catch (err) {
|
|
18861
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18862
|
+
}
|
|
18863
|
+
});
|
|
18864
|
+
server.registerTool("zefiro_copilot_propose_action", {
|
|
18865
|
+
title: "Propose Action (Assist Mode)",
|
|
18866
|
+
description: "In ASSIST mode, propose an action for human approval before executing. " + "Call copilot_check_input later to get the approval result.",
|
|
18867
|
+
inputSchema: exports_external.object({
|
|
18868
|
+
session_id: exports_external.string().describe("Session ID"),
|
|
18869
|
+
action: exports_external.string().describe("Description of the proposed action"),
|
|
18870
|
+
reason: exports_external.string().describe("Why this action is recommended")
|
|
18871
|
+
})
|
|
18872
|
+
}, async (params) => {
|
|
18873
|
+
try {
|
|
18874
|
+
const result = await handleCopilotProposeAction(params);
|
|
18875
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
18876
|
+
} catch (err) {
|
|
18877
|
+
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
18878
|
+
}
|
|
18879
|
+
});
|
|
15070
18880
|
async function main() {
|
|
15071
18881
|
const transport = new StdioServerTransport;
|
|
15072
18882
|
await server.connect(transport);
|