@nextnext/mcp-server 0.1.7 → 0.3.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.
Files changed (2) hide show
  1. package/dist/index.js +3214 -81
  2. package/package.json +4 -3
package/dist/index.js CHANGED
@@ -43035,6 +43035,2850 @@ var init_esm2 = __esm(() => {
43035
43035
  });
43036
43036
  });
43037
43037
 
43038
+ // node_modules/ws/lib/constants.js
43039
+ var require_constants2 = __commonJS((exports, module) => {
43040
+ var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
43041
+ var hasBlob = typeof Blob !== "undefined";
43042
+ if (hasBlob)
43043
+ BINARY_TYPES.push("blob");
43044
+ module.exports = {
43045
+ BINARY_TYPES,
43046
+ CLOSE_TIMEOUT: 30000,
43047
+ EMPTY_BUFFER: Buffer.alloc(0),
43048
+ GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
43049
+ hasBlob,
43050
+ kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
43051
+ kListener: Symbol("kListener"),
43052
+ kStatusCode: Symbol("status-code"),
43053
+ kWebSocket: Symbol("websocket"),
43054
+ NOOP: () => {}
43055
+ };
43056
+ });
43057
+
43058
+ // node_modules/ws/lib/buffer-util.js
43059
+ var require_buffer_util2 = __commonJS((exports, module) => {
43060
+ var { EMPTY_BUFFER: EMPTY_BUFFER2 } = require_constants2();
43061
+ var FastBuffer = Buffer[Symbol.species];
43062
+ function concat3(list2, totalLength) {
43063
+ if (list2.length === 0)
43064
+ return EMPTY_BUFFER2;
43065
+ if (list2.length === 1)
43066
+ return list2[0];
43067
+ const target = Buffer.allocUnsafe(totalLength);
43068
+ let offset = 0;
43069
+ for (let i3 = 0;i3 < list2.length; i3++) {
43070
+ const buf = list2[i3];
43071
+ target.set(buf, offset);
43072
+ offset += buf.length;
43073
+ }
43074
+ if (offset < totalLength) {
43075
+ return new FastBuffer(target.buffer, target.byteOffset, offset);
43076
+ }
43077
+ return target;
43078
+ }
43079
+ function _mask(source, mask, output4, offset, length) {
43080
+ for (let i3 = 0;i3 < length; i3++) {
43081
+ output4[offset + i3] = source[i3] ^ mask[i3 & 3];
43082
+ }
43083
+ }
43084
+ function _unmask(buffer2, mask) {
43085
+ for (let i3 = 0;i3 < buffer2.length; i3++) {
43086
+ buffer2[i3] ^= mask[i3 & 3];
43087
+ }
43088
+ }
43089
+ function toArrayBuffer2(buf) {
43090
+ if (buf.length === buf.buffer.byteLength) {
43091
+ return buf.buffer;
43092
+ }
43093
+ return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
43094
+ }
43095
+ function toBuffer(data) {
43096
+ toBuffer.readOnly = true;
43097
+ if (Buffer.isBuffer(data))
43098
+ return data;
43099
+ let buf;
43100
+ if (data instanceof ArrayBuffer) {
43101
+ buf = new FastBuffer(data);
43102
+ } else if (ArrayBuffer.isView(data)) {
43103
+ buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
43104
+ } else {
43105
+ buf = Buffer.from(data);
43106
+ toBuffer.readOnly = false;
43107
+ }
43108
+ return buf;
43109
+ }
43110
+ module.exports = {
43111
+ concat: concat3,
43112
+ mask: _mask,
43113
+ toArrayBuffer: toArrayBuffer2,
43114
+ toBuffer,
43115
+ unmask: _unmask
43116
+ };
43117
+ if (!process.env.WS_NO_BUFFER_UTIL) {
43118
+ try {
43119
+ const bufferUtil = require_bufferutil();
43120
+ module.exports.mask = function(source, mask, output4, offset, length) {
43121
+ if (length < 48)
43122
+ _mask(source, mask, output4, offset, length);
43123
+ else
43124
+ bufferUtil.mask(source, mask, output4, offset, length);
43125
+ };
43126
+ module.exports.unmask = function(buffer2, mask) {
43127
+ if (buffer2.length < 32)
43128
+ _unmask(buffer2, mask);
43129
+ else
43130
+ bufferUtil.unmask(buffer2, mask);
43131
+ };
43132
+ } catch (e7) {}
43133
+ }
43134
+ });
43135
+
43136
+ // node_modules/ws/lib/limiter.js
43137
+ var require_limiter2 = __commonJS((exports, module) => {
43138
+ var kDone = Symbol("kDone");
43139
+ var kRun = Symbol("kRun");
43140
+
43141
+ class Limiter {
43142
+ constructor(concurrency) {
43143
+ this[kDone] = () => {
43144
+ this.pending--;
43145
+ this[kRun]();
43146
+ };
43147
+ this.concurrency = concurrency || Infinity;
43148
+ this.jobs = [];
43149
+ this.pending = 0;
43150
+ }
43151
+ add(job) {
43152
+ this.jobs.push(job);
43153
+ this[kRun]();
43154
+ }
43155
+ [kRun]() {
43156
+ if (this.pending === this.concurrency)
43157
+ return;
43158
+ if (this.jobs.length) {
43159
+ const job = this.jobs.shift();
43160
+ this.pending++;
43161
+ job(this[kDone]);
43162
+ }
43163
+ }
43164
+ }
43165
+ module.exports = Limiter;
43166
+ });
43167
+
43168
+ // node_modules/ws/lib/permessage-deflate.js
43169
+ var require_permessage_deflate2 = __commonJS((exports, module) => {
43170
+ var zlib2 = __require("zlib");
43171
+ var bufferUtil = require_buffer_util2();
43172
+ var Limiter = require_limiter2();
43173
+ var { kStatusCode } = require_constants2();
43174
+ var FastBuffer = Buffer[Symbol.species];
43175
+ var TRAILER = Buffer.from([0, 0, 255, 255]);
43176
+ var kPerMessageDeflate = Symbol("permessage-deflate");
43177
+ var kTotalLength = Symbol("total-length");
43178
+ var kCallback = Symbol("callback");
43179
+ var kBuffers = Symbol("buffers");
43180
+ var kError = Symbol("error");
43181
+ var zlibLimiter;
43182
+
43183
+ class PerMessageDeflate {
43184
+ constructor(options, isServer, maxPayload) {
43185
+ this._maxPayload = maxPayload | 0;
43186
+ this._options = options || {};
43187
+ this._threshold = this._options.threshold !== undefined ? this._options.threshold : 1024;
43188
+ this._isServer = !!isServer;
43189
+ this._deflate = null;
43190
+ this._inflate = null;
43191
+ this.params = null;
43192
+ if (!zlibLimiter) {
43193
+ const concurrency = this._options.concurrencyLimit !== undefined ? this._options.concurrencyLimit : 10;
43194
+ zlibLimiter = new Limiter(concurrency);
43195
+ }
43196
+ }
43197
+ static get extensionName() {
43198
+ return "permessage-deflate";
43199
+ }
43200
+ offer() {
43201
+ const params = {};
43202
+ if (this._options.serverNoContextTakeover) {
43203
+ params.server_no_context_takeover = true;
43204
+ }
43205
+ if (this._options.clientNoContextTakeover) {
43206
+ params.client_no_context_takeover = true;
43207
+ }
43208
+ if (this._options.serverMaxWindowBits) {
43209
+ params.server_max_window_bits = this._options.serverMaxWindowBits;
43210
+ }
43211
+ if (this._options.clientMaxWindowBits) {
43212
+ params.client_max_window_bits = this._options.clientMaxWindowBits;
43213
+ } else if (this._options.clientMaxWindowBits == null) {
43214
+ params.client_max_window_bits = true;
43215
+ }
43216
+ return params;
43217
+ }
43218
+ accept(configurations) {
43219
+ configurations = this.normalizeParams(configurations);
43220
+ this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
43221
+ return this.params;
43222
+ }
43223
+ cleanup() {
43224
+ if (this._inflate) {
43225
+ this._inflate.close();
43226
+ this._inflate = null;
43227
+ }
43228
+ if (this._deflate) {
43229
+ const callback = this._deflate[kCallback];
43230
+ this._deflate.close();
43231
+ this._deflate = null;
43232
+ if (callback) {
43233
+ callback(new Error("The deflate stream was closed while data was being processed"));
43234
+ }
43235
+ }
43236
+ }
43237
+ acceptAsServer(offers) {
43238
+ const opts = this._options;
43239
+ const accepted = offers.find((params) => {
43240
+ 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) {
43241
+ return false;
43242
+ }
43243
+ return true;
43244
+ });
43245
+ if (!accepted) {
43246
+ throw new Error("None of the extension offers can be accepted");
43247
+ }
43248
+ if (opts.serverNoContextTakeover) {
43249
+ accepted.server_no_context_takeover = true;
43250
+ }
43251
+ if (opts.clientNoContextTakeover) {
43252
+ accepted.client_no_context_takeover = true;
43253
+ }
43254
+ if (typeof opts.serverMaxWindowBits === "number") {
43255
+ accepted.server_max_window_bits = opts.serverMaxWindowBits;
43256
+ }
43257
+ if (typeof opts.clientMaxWindowBits === "number") {
43258
+ accepted.client_max_window_bits = opts.clientMaxWindowBits;
43259
+ } else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
43260
+ delete accepted.client_max_window_bits;
43261
+ }
43262
+ return accepted;
43263
+ }
43264
+ acceptAsClient(response) {
43265
+ const params = response[0];
43266
+ if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
43267
+ throw new Error('Unexpected parameter "client_no_context_takeover"');
43268
+ }
43269
+ if (!params.client_max_window_bits) {
43270
+ if (typeof this._options.clientMaxWindowBits === "number") {
43271
+ params.client_max_window_bits = this._options.clientMaxWindowBits;
43272
+ }
43273
+ } else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
43274
+ throw new Error('Unexpected or invalid parameter "client_max_window_bits"');
43275
+ }
43276
+ return params;
43277
+ }
43278
+ normalizeParams(configurations) {
43279
+ configurations.forEach((params) => {
43280
+ Object.keys(params).forEach((key) => {
43281
+ let value = params[key];
43282
+ if (value.length > 1) {
43283
+ throw new Error(`Parameter "${key}" must have only a single value`);
43284
+ }
43285
+ value = value[0];
43286
+ if (key === "client_max_window_bits") {
43287
+ if (value !== true) {
43288
+ const num2 = +value;
43289
+ if (!Number.isInteger(num2) || num2 < 8 || num2 > 15) {
43290
+ throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
43291
+ }
43292
+ value = num2;
43293
+ } else if (!this._isServer) {
43294
+ throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
43295
+ }
43296
+ } else if (key === "server_max_window_bits") {
43297
+ const num2 = +value;
43298
+ if (!Number.isInteger(num2) || num2 < 8 || num2 > 15) {
43299
+ throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
43300
+ }
43301
+ value = num2;
43302
+ } else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
43303
+ if (value !== true) {
43304
+ throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
43305
+ }
43306
+ } else {
43307
+ throw new Error(`Unknown parameter "${key}"`);
43308
+ }
43309
+ params[key] = value;
43310
+ });
43311
+ });
43312
+ return configurations;
43313
+ }
43314
+ decompress(data, fin, callback) {
43315
+ zlibLimiter.add((done) => {
43316
+ this._decompress(data, fin, (err, result) => {
43317
+ done();
43318
+ callback(err, result);
43319
+ });
43320
+ });
43321
+ }
43322
+ compress(data, fin, callback) {
43323
+ zlibLimiter.add((done) => {
43324
+ this._compress(data, fin, (err, result) => {
43325
+ done();
43326
+ callback(err, result);
43327
+ });
43328
+ });
43329
+ }
43330
+ _decompress(data, fin, callback) {
43331
+ const endpoint = this._isServer ? "client" : "server";
43332
+ if (!this._inflate) {
43333
+ const key = `${endpoint}_max_window_bits`;
43334
+ const windowBits = typeof this.params[key] !== "number" ? zlib2.Z_DEFAULT_WINDOWBITS : this.params[key];
43335
+ this._inflate = zlib2.createInflateRaw({
43336
+ ...this._options.zlibInflateOptions,
43337
+ windowBits
43338
+ });
43339
+ this._inflate[kPerMessageDeflate] = this;
43340
+ this._inflate[kTotalLength] = 0;
43341
+ this._inflate[kBuffers] = [];
43342
+ this._inflate.on("error", inflateOnError);
43343
+ this._inflate.on("data", inflateOnData);
43344
+ }
43345
+ this._inflate[kCallback] = callback;
43346
+ this._inflate.write(data);
43347
+ if (fin)
43348
+ this._inflate.write(TRAILER);
43349
+ this._inflate.flush(() => {
43350
+ const err = this._inflate[kError];
43351
+ if (err) {
43352
+ this._inflate.close();
43353
+ this._inflate = null;
43354
+ callback(err);
43355
+ return;
43356
+ }
43357
+ const data2 = bufferUtil.concat(this._inflate[kBuffers], this._inflate[kTotalLength]);
43358
+ if (this._inflate._readableState.endEmitted) {
43359
+ this._inflate.close();
43360
+ this._inflate = null;
43361
+ } else {
43362
+ this._inflate[kTotalLength] = 0;
43363
+ this._inflate[kBuffers] = [];
43364
+ if (fin && this.params[`${endpoint}_no_context_takeover`]) {
43365
+ this._inflate.reset();
43366
+ }
43367
+ }
43368
+ callback(null, data2);
43369
+ });
43370
+ }
43371
+ _compress(data, fin, callback) {
43372
+ const endpoint = this._isServer ? "server" : "client";
43373
+ if (!this._deflate) {
43374
+ const key = `${endpoint}_max_window_bits`;
43375
+ const windowBits = typeof this.params[key] !== "number" ? zlib2.Z_DEFAULT_WINDOWBITS : this.params[key];
43376
+ this._deflate = zlib2.createDeflateRaw({
43377
+ ...this._options.zlibDeflateOptions,
43378
+ windowBits
43379
+ });
43380
+ this._deflate[kTotalLength] = 0;
43381
+ this._deflate[kBuffers] = [];
43382
+ this._deflate.on("data", deflateOnData);
43383
+ }
43384
+ this._deflate[kCallback] = callback;
43385
+ this._deflate.write(data);
43386
+ this._deflate.flush(zlib2.Z_SYNC_FLUSH, () => {
43387
+ if (!this._deflate) {
43388
+ return;
43389
+ }
43390
+ let data2 = bufferUtil.concat(this._deflate[kBuffers], this._deflate[kTotalLength]);
43391
+ if (fin) {
43392
+ data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
43393
+ }
43394
+ this._deflate[kCallback] = null;
43395
+ this._deflate[kTotalLength] = 0;
43396
+ this._deflate[kBuffers] = [];
43397
+ if (fin && this.params[`${endpoint}_no_context_takeover`]) {
43398
+ this._deflate.reset();
43399
+ }
43400
+ callback(null, data2);
43401
+ });
43402
+ }
43403
+ }
43404
+ module.exports = PerMessageDeflate;
43405
+ function deflateOnData(chunk) {
43406
+ this[kBuffers].push(chunk);
43407
+ this[kTotalLength] += chunk.length;
43408
+ }
43409
+ function inflateOnData(chunk) {
43410
+ this[kTotalLength] += chunk.length;
43411
+ if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
43412
+ this[kBuffers].push(chunk);
43413
+ return;
43414
+ }
43415
+ this[kError] = new RangeError("Max payload size exceeded");
43416
+ this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
43417
+ this[kError][kStatusCode] = 1009;
43418
+ this.removeListener("data", inflateOnData);
43419
+ this.reset();
43420
+ }
43421
+ function inflateOnError(err) {
43422
+ this[kPerMessageDeflate]._inflate = null;
43423
+ if (this[kError]) {
43424
+ this[kCallback](this[kError]);
43425
+ return;
43426
+ }
43427
+ err[kStatusCode] = 1007;
43428
+ this[kCallback](err);
43429
+ }
43430
+ });
43431
+
43432
+ // node_modules/ws/lib/validation.js
43433
+ var require_validation4 = __commonJS((exports, module) => {
43434
+ var { isUtf8 } = __require("buffer");
43435
+ var { hasBlob } = require_constants2();
43436
+ var tokenChars = [
43437
+ 0,
43438
+ 0,
43439
+ 0,
43440
+ 0,
43441
+ 0,
43442
+ 0,
43443
+ 0,
43444
+ 0,
43445
+ 0,
43446
+ 0,
43447
+ 0,
43448
+ 0,
43449
+ 0,
43450
+ 0,
43451
+ 0,
43452
+ 0,
43453
+ 0,
43454
+ 0,
43455
+ 0,
43456
+ 0,
43457
+ 0,
43458
+ 0,
43459
+ 0,
43460
+ 0,
43461
+ 0,
43462
+ 0,
43463
+ 0,
43464
+ 0,
43465
+ 0,
43466
+ 0,
43467
+ 0,
43468
+ 0,
43469
+ 0,
43470
+ 1,
43471
+ 0,
43472
+ 1,
43473
+ 1,
43474
+ 1,
43475
+ 1,
43476
+ 1,
43477
+ 0,
43478
+ 0,
43479
+ 1,
43480
+ 1,
43481
+ 0,
43482
+ 1,
43483
+ 1,
43484
+ 0,
43485
+ 1,
43486
+ 1,
43487
+ 1,
43488
+ 1,
43489
+ 1,
43490
+ 1,
43491
+ 1,
43492
+ 1,
43493
+ 1,
43494
+ 1,
43495
+ 0,
43496
+ 0,
43497
+ 0,
43498
+ 0,
43499
+ 0,
43500
+ 0,
43501
+ 0,
43502
+ 1,
43503
+ 1,
43504
+ 1,
43505
+ 1,
43506
+ 1,
43507
+ 1,
43508
+ 1,
43509
+ 1,
43510
+ 1,
43511
+ 1,
43512
+ 1,
43513
+ 1,
43514
+ 1,
43515
+ 1,
43516
+ 1,
43517
+ 1,
43518
+ 1,
43519
+ 1,
43520
+ 1,
43521
+ 1,
43522
+ 1,
43523
+ 1,
43524
+ 1,
43525
+ 1,
43526
+ 1,
43527
+ 1,
43528
+ 0,
43529
+ 0,
43530
+ 0,
43531
+ 1,
43532
+ 1,
43533
+ 1,
43534
+ 1,
43535
+ 1,
43536
+ 1,
43537
+ 1,
43538
+ 1,
43539
+ 1,
43540
+ 1,
43541
+ 1,
43542
+ 1,
43543
+ 1,
43544
+ 1,
43545
+ 1,
43546
+ 1,
43547
+ 1,
43548
+ 1,
43549
+ 1,
43550
+ 1,
43551
+ 1,
43552
+ 1,
43553
+ 1,
43554
+ 1,
43555
+ 1,
43556
+ 1,
43557
+ 1,
43558
+ 1,
43559
+ 1,
43560
+ 0,
43561
+ 1,
43562
+ 0,
43563
+ 1,
43564
+ 0
43565
+ ];
43566
+ function isValidStatusCode(code) {
43567
+ return code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3000 && code <= 4999;
43568
+ }
43569
+ function _isValidUTF8(buf) {
43570
+ const len = buf.length;
43571
+ let i3 = 0;
43572
+ while (i3 < len) {
43573
+ if ((buf[i3] & 128) === 0) {
43574
+ i3++;
43575
+ } else if ((buf[i3] & 224) === 192) {
43576
+ if (i3 + 1 === len || (buf[i3 + 1] & 192) !== 128 || (buf[i3] & 254) === 192) {
43577
+ return false;
43578
+ }
43579
+ i3 += 2;
43580
+ } else if ((buf[i3] & 240) === 224) {
43581
+ if (i3 + 2 >= len || (buf[i3 + 1] & 192) !== 128 || (buf[i3 + 2] & 192) !== 128 || buf[i3] === 224 && (buf[i3 + 1] & 224) === 128 || buf[i3] === 237 && (buf[i3 + 1] & 224) === 160) {
43582
+ return false;
43583
+ }
43584
+ i3 += 3;
43585
+ } else if ((buf[i3] & 248) === 240) {
43586
+ if (i3 + 3 >= len || (buf[i3 + 1] & 192) !== 128 || (buf[i3 + 2] & 192) !== 128 || (buf[i3 + 3] & 192) !== 128 || buf[i3] === 240 && (buf[i3 + 1] & 240) === 128 || buf[i3] === 244 && buf[i3 + 1] > 143 || buf[i3] > 244) {
43587
+ return false;
43588
+ }
43589
+ i3 += 4;
43590
+ } else {
43591
+ return false;
43592
+ }
43593
+ }
43594
+ return true;
43595
+ }
43596
+ function isBlob2(value) {
43597
+ 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");
43598
+ }
43599
+ module.exports = {
43600
+ isBlob: isBlob2,
43601
+ isValidStatusCode,
43602
+ isValidUTF8: _isValidUTF8,
43603
+ tokenChars
43604
+ };
43605
+ if (isUtf8) {
43606
+ module.exports.isValidUTF8 = function(buf) {
43607
+ return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
43608
+ };
43609
+ } else if (!process.env.WS_NO_UTF_8_VALIDATE) {
43610
+ try {
43611
+ const isValidUTF8 = require_utf_8_validate();
43612
+ module.exports.isValidUTF8 = function(buf) {
43613
+ return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
43614
+ };
43615
+ } catch (e7) {}
43616
+ }
43617
+ });
43618
+
43619
+ // node_modules/ws/lib/receiver.js
43620
+ var require_receiver2 = __commonJS((exports, module) => {
43621
+ var { Writable } = __require("stream");
43622
+ var PerMessageDeflate = require_permessage_deflate2();
43623
+ var {
43624
+ BINARY_TYPES,
43625
+ EMPTY_BUFFER: EMPTY_BUFFER2,
43626
+ kStatusCode,
43627
+ kWebSocket
43628
+ } = require_constants2();
43629
+ var { concat: concat3, toArrayBuffer: toArrayBuffer2, unmask } = require_buffer_util2();
43630
+ var { isValidStatusCode, isValidUTF8 } = require_validation4();
43631
+ var FastBuffer = Buffer[Symbol.species];
43632
+ var GET_INFO = 0;
43633
+ var GET_PAYLOAD_LENGTH_16 = 1;
43634
+ var GET_PAYLOAD_LENGTH_64 = 2;
43635
+ var GET_MASK = 3;
43636
+ var GET_DATA = 4;
43637
+ var INFLATING = 5;
43638
+ var DEFER_EVENT = 6;
43639
+
43640
+ class Receiver2 extends Writable {
43641
+ constructor(options = {}) {
43642
+ super();
43643
+ this._allowSynchronousEvents = options.allowSynchronousEvents !== undefined ? options.allowSynchronousEvents : true;
43644
+ this._binaryType = options.binaryType || BINARY_TYPES[0];
43645
+ this._extensions = options.extensions || {};
43646
+ this._isServer = !!options.isServer;
43647
+ this._maxPayload = options.maxPayload | 0;
43648
+ this._skipUTF8Validation = !!options.skipUTF8Validation;
43649
+ this[kWebSocket] = undefined;
43650
+ this._bufferedBytes = 0;
43651
+ this._buffers = [];
43652
+ this._compressed = false;
43653
+ this._payloadLength = 0;
43654
+ this._mask = undefined;
43655
+ this._fragmented = 0;
43656
+ this._masked = false;
43657
+ this._fin = false;
43658
+ this._opcode = 0;
43659
+ this._totalPayloadLength = 0;
43660
+ this._messageLength = 0;
43661
+ this._fragments = [];
43662
+ this._errored = false;
43663
+ this._loop = false;
43664
+ this._state = GET_INFO;
43665
+ }
43666
+ _write(chunk, encoding, cb) {
43667
+ if (this._opcode === 8 && this._state == GET_INFO)
43668
+ return cb();
43669
+ this._bufferedBytes += chunk.length;
43670
+ this._buffers.push(chunk);
43671
+ this.startLoop(cb);
43672
+ }
43673
+ consume(n) {
43674
+ this._bufferedBytes -= n;
43675
+ if (n === this._buffers[0].length)
43676
+ return this._buffers.shift();
43677
+ if (n < this._buffers[0].length) {
43678
+ const buf = this._buffers[0];
43679
+ this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
43680
+ return new FastBuffer(buf.buffer, buf.byteOffset, n);
43681
+ }
43682
+ const dst = Buffer.allocUnsafe(n);
43683
+ do {
43684
+ const buf = this._buffers[0];
43685
+ const offset = dst.length - n;
43686
+ if (n >= buf.length) {
43687
+ dst.set(this._buffers.shift(), offset);
43688
+ } else {
43689
+ dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
43690
+ this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
43691
+ }
43692
+ n -= buf.length;
43693
+ } while (n > 0);
43694
+ return dst;
43695
+ }
43696
+ startLoop(cb) {
43697
+ this._loop = true;
43698
+ do {
43699
+ switch (this._state) {
43700
+ case GET_INFO:
43701
+ this.getInfo(cb);
43702
+ break;
43703
+ case GET_PAYLOAD_LENGTH_16:
43704
+ this.getPayloadLength16(cb);
43705
+ break;
43706
+ case GET_PAYLOAD_LENGTH_64:
43707
+ this.getPayloadLength64(cb);
43708
+ break;
43709
+ case GET_MASK:
43710
+ this.getMask();
43711
+ break;
43712
+ case GET_DATA:
43713
+ this.getData(cb);
43714
+ break;
43715
+ case INFLATING:
43716
+ case DEFER_EVENT:
43717
+ this._loop = false;
43718
+ return;
43719
+ }
43720
+ } while (this._loop);
43721
+ if (!this._errored)
43722
+ cb();
43723
+ }
43724
+ getInfo(cb) {
43725
+ if (this._bufferedBytes < 2) {
43726
+ this._loop = false;
43727
+ return;
43728
+ }
43729
+ const buf = this.consume(2);
43730
+ if ((buf[0] & 48) !== 0) {
43731
+ const error2 = this.createError(RangeError, "RSV2 and RSV3 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_2_3");
43732
+ cb(error2);
43733
+ return;
43734
+ }
43735
+ const compressed = (buf[0] & 64) === 64;
43736
+ if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
43737
+ const error2 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
43738
+ cb(error2);
43739
+ return;
43740
+ }
43741
+ this._fin = (buf[0] & 128) === 128;
43742
+ this._opcode = buf[0] & 15;
43743
+ this._payloadLength = buf[1] & 127;
43744
+ if (this._opcode === 0) {
43745
+ if (compressed) {
43746
+ const error2 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
43747
+ cb(error2);
43748
+ return;
43749
+ }
43750
+ if (!this._fragmented) {
43751
+ const error2 = this.createError(RangeError, "invalid opcode 0", true, 1002, "WS_ERR_INVALID_OPCODE");
43752
+ cb(error2);
43753
+ return;
43754
+ }
43755
+ this._opcode = this._fragmented;
43756
+ } else if (this._opcode === 1 || this._opcode === 2) {
43757
+ if (this._fragmented) {
43758
+ const error2 = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
43759
+ cb(error2);
43760
+ return;
43761
+ }
43762
+ this._compressed = compressed;
43763
+ } else if (this._opcode > 7 && this._opcode < 11) {
43764
+ if (!this._fin) {
43765
+ const error2 = this.createError(RangeError, "FIN must be set", true, 1002, "WS_ERR_EXPECTED_FIN");
43766
+ cb(error2);
43767
+ return;
43768
+ }
43769
+ if (compressed) {
43770
+ const error2 = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
43771
+ cb(error2);
43772
+ return;
43773
+ }
43774
+ if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
43775
+ const error2 = this.createError(RangeError, `invalid payload length ${this._payloadLength}`, true, 1002, "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");
43776
+ cb(error2);
43777
+ return;
43778
+ }
43779
+ } else {
43780
+ const error2 = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
43781
+ cb(error2);
43782
+ return;
43783
+ }
43784
+ if (!this._fin && !this._fragmented)
43785
+ this._fragmented = this._opcode;
43786
+ this._masked = (buf[1] & 128) === 128;
43787
+ if (this._isServer) {
43788
+ if (!this._masked) {
43789
+ const error2 = this.createError(RangeError, "MASK must be set", true, 1002, "WS_ERR_EXPECTED_MASK");
43790
+ cb(error2);
43791
+ return;
43792
+ }
43793
+ } else if (this._masked) {
43794
+ const error2 = this.createError(RangeError, "MASK must be clear", true, 1002, "WS_ERR_UNEXPECTED_MASK");
43795
+ cb(error2);
43796
+ return;
43797
+ }
43798
+ if (this._payloadLength === 126)
43799
+ this._state = GET_PAYLOAD_LENGTH_16;
43800
+ else if (this._payloadLength === 127)
43801
+ this._state = GET_PAYLOAD_LENGTH_64;
43802
+ else
43803
+ this.haveLength(cb);
43804
+ }
43805
+ getPayloadLength16(cb) {
43806
+ if (this._bufferedBytes < 2) {
43807
+ this._loop = false;
43808
+ return;
43809
+ }
43810
+ this._payloadLength = this.consume(2).readUInt16BE(0);
43811
+ this.haveLength(cb);
43812
+ }
43813
+ getPayloadLength64(cb) {
43814
+ if (this._bufferedBytes < 8) {
43815
+ this._loop = false;
43816
+ return;
43817
+ }
43818
+ const buf = this.consume(8);
43819
+ const num2 = buf.readUInt32BE(0);
43820
+ if (num2 > Math.pow(2, 53 - 32) - 1) {
43821
+ const error2 = this.createError(RangeError, "Unsupported WebSocket frame: payload length > 2^53 - 1", false, 1009, "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");
43822
+ cb(error2);
43823
+ return;
43824
+ }
43825
+ this._payloadLength = num2 * Math.pow(2, 32) + buf.readUInt32BE(4);
43826
+ this.haveLength(cb);
43827
+ }
43828
+ haveLength(cb) {
43829
+ if (this._payloadLength && this._opcode < 8) {
43830
+ this._totalPayloadLength += this._payloadLength;
43831
+ if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
43832
+ const error2 = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
43833
+ cb(error2);
43834
+ return;
43835
+ }
43836
+ }
43837
+ if (this._masked)
43838
+ this._state = GET_MASK;
43839
+ else
43840
+ this._state = GET_DATA;
43841
+ }
43842
+ getMask() {
43843
+ if (this._bufferedBytes < 4) {
43844
+ this._loop = false;
43845
+ return;
43846
+ }
43847
+ this._mask = this.consume(4);
43848
+ this._state = GET_DATA;
43849
+ }
43850
+ getData(cb) {
43851
+ let data = EMPTY_BUFFER2;
43852
+ if (this._payloadLength) {
43853
+ if (this._bufferedBytes < this._payloadLength) {
43854
+ this._loop = false;
43855
+ return;
43856
+ }
43857
+ data = this.consume(this._payloadLength);
43858
+ if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
43859
+ unmask(data, this._mask);
43860
+ }
43861
+ }
43862
+ if (this._opcode > 7) {
43863
+ this.controlMessage(data, cb);
43864
+ return;
43865
+ }
43866
+ if (this._compressed) {
43867
+ this._state = INFLATING;
43868
+ this.decompress(data, cb);
43869
+ return;
43870
+ }
43871
+ if (data.length) {
43872
+ this._messageLength = this._totalPayloadLength;
43873
+ this._fragments.push(data);
43874
+ }
43875
+ this.dataMessage(cb);
43876
+ }
43877
+ decompress(data, cb) {
43878
+ const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
43879
+ perMessageDeflate.decompress(data, this._fin, (err, buf) => {
43880
+ if (err)
43881
+ return cb(err);
43882
+ if (buf.length) {
43883
+ this._messageLength += buf.length;
43884
+ if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
43885
+ const error2 = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
43886
+ cb(error2);
43887
+ return;
43888
+ }
43889
+ this._fragments.push(buf);
43890
+ }
43891
+ this.dataMessage(cb);
43892
+ if (this._state === GET_INFO)
43893
+ this.startLoop(cb);
43894
+ });
43895
+ }
43896
+ dataMessage(cb) {
43897
+ if (!this._fin) {
43898
+ this._state = GET_INFO;
43899
+ return;
43900
+ }
43901
+ const messageLength = this._messageLength;
43902
+ const fragments = this._fragments;
43903
+ this._totalPayloadLength = 0;
43904
+ this._messageLength = 0;
43905
+ this._fragmented = 0;
43906
+ this._fragments = [];
43907
+ if (this._opcode === 2) {
43908
+ let data;
43909
+ if (this._binaryType === "nodebuffer") {
43910
+ data = concat3(fragments, messageLength);
43911
+ } else if (this._binaryType === "arraybuffer") {
43912
+ data = toArrayBuffer2(concat3(fragments, messageLength));
43913
+ } else if (this._binaryType === "blob") {
43914
+ data = new Blob(fragments);
43915
+ } else {
43916
+ data = fragments;
43917
+ }
43918
+ if (this._allowSynchronousEvents) {
43919
+ this.emit("message", data, true);
43920
+ this._state = GET_INFO;
43921
+ } else {
43922
+ this._state = DEFER_EVENT;
43923
+ setImmediate(() => {
43924
+ this.emit("message", data, true);
43925
+ this._state = GET_INFO;
43926
+ this.startLoop(cb);
43927
+ });
43928
+ }
43929
+ } else {
43930
+ const buf = concat3(fragments, messageLength);
43931
+ if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
43932
+ const error2 = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
43933
+ cb(error2);
43934
+ return;
43935
+ }
43936
+ if (this._state === INFLATING || this._allowSynchronousEvents) {
43937
+ this.emit("message", buf, false);
43938
+ this._state = GET_INFO;
43939
+ } else {
43940
+ this._state = DEFER_EVENT;
43941
+ setImmediate(() => {
43942
+ this.emit("message", buf, false);
43943
+ this._state = GET_INFO;
43944
+ this.startLoop(cb);
43945
+ });
43946
+ }
43947
+ }
43948
+ }
43949
+ controlMessage(data, cb) {
43950
+ if (this._opcode === 8) {
43951
+ if (data.length === 0) {
43952
+ this._loop = false;
43953
+ this.emit("conclude", 1005, EMPTY_BUFFER2);
43954
+ this.end();
43955
+ } else {
43956
+ const code = data.readUInt16BE(0);
43957
+ if (!isValidStatusCode(code)) {
43958
+ const error2 = this.createError(RangeError, `invalid status code ${code}`, true, 1002, "WS_ERR_INVALID_CLOSE_CODE");
43959
+ cb(error2);
43960
+ return;
43961
+ }
43962
+ const buf = new FastBuffer(data.buffer, data.byteOffset + 2, data.length - 2);
43963
+ if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
43964
+ const error2 = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
43965
+ cb(error2);
43966
+ return;
43967
+ }
43968
+ this._loop = false;
43969
+ this.emit("conclude", code, buf);
43970
+ this.end();
43971
+ }
43972
+ this._state = GET_INFO;
43973
+ return;
43974
+ }
43975
+ if (this._allowSynchronousEvents) {
43976
+ this.emit(this._opcode === 9 ? "ping" : "pong", data);
43977
+ this._state = GET_INFO;
43978
+ } else {
43979
+ this._state = DEFER_EVENT;
43980
+ setImmediate(() => {
43981
+ this.emit(this._opcode === 9 ? "ping" : "pong", data);
43982
+ this._state = GET_INFO;
43983
+ this.startLoop(cb);
43984
+ });
43985
+ }
43986
+ }
43987
+ createError(ErrorCtor, message, prefix, statusCode, errorCode) {
43988
+ this._loop = false;
43989
+ this._errored = true;
43990
+ const err = new ErrorCtor(prefix ? `Invalid WebSocket frame: ${message}` : message);
43991
+ Error.captureStackTrace(err, this.createError);
43992
+ err.code = errorCode;
43993
+ err[kStatusCode] = statusCode;
43994
+ return err;
43995
+ }
43996
+ }
43997
+ module.exports = Receiver2;
43998
+ });
43999
+
44000
+ // node_modules/ws/lib/sender.js
44001
+ var require_sender2 = __commonJS((exports, module) => {
44002
+ var { Duplex } = __require("stream");
44003
+ var { randomFillSync } = __require("crypto");
44004
+ var PerMessageDeflate = require_permessage_deflate2();
44005
+ var { EMPTY_BUFFER: EMPTY_BUFFER2, kWebSocket, NOOP } = require_constants2();
44006
+ var { isBlob: isBlob2, isValidStatusCode } = require_validation4();
44007
+ var { mask: applyMask, toBuffer } = require_buffer_util2();
44008
+ var kByteLength = Symbol("kByteLength");
44009
+ var maskBuffer = Buffer.alloc(4);
44010
+ var RANDOM_POOL_SIZE = 8 * 1024;
44011
+ var randomPool;
44012
+ var randomPoolPointer = RANDOM_POOL_SIZE;
44013
+ var DEFAULT = 0;
44014
+ var DEFLATING = 1;
44015
+ var GET_BLOB_DATA = 2;
44016
+
44017
+ class Sender2 {
44018
+ constructor(socket, extensions, generateMask) {
44019
+ this._extensions = extensions || {};
44020
+ if (generateMask) {
44021
+ this._generateMask = generateMask;
44022
+ this._maskBuffer = Buffer.alloc(4);
44023
+ }
44024
+ this._socket = socket;
44025
+ this._firstFragment = true;
44026
+ this._compress = false;
44027
+ this._bufferedBytes = 0;
44028
+ this._queue = [];
44029
+ this._state = DEFAULT;
44030
+ this.onerror = NOOP;
44031
+ this[kWebSocket] = undefined;
44032
+ }
44033
+ static frame(data, options) {
44034
+ let mask;
44035
+ let merge3 = false;
44036
+ let offset = 2;
44037
+ let skipMasking = false;
44038
+ if (options.mask) {
44039
+ mask = options.maskBuffer || maskBuffer;
44040
+ if (options.generateMask) {
44041
+ options.generateMask(mask);
44042
+ } else {
44043
+ if (randomPoolPointer === RANDOM_POOL_SIZE) {
44044
+ if (randomPool === undefined) {
44045
+ randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
44046
+ }
44047
+ randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
44048
+ randomPoolPointer = 0;
44049
+ }
44050
+ mask[0] = randomPool[randomPoolPointer++];
44051
+ mask[1] = randomPool[randomPoolPointer++];
44052
+ mask[2] = randomPool[randomPoolPointer++];
44053
+ mask[3] = randomPool[randomPoolPointer++];
44054
+ }
44055
+ skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
44056
+ offset = 6;
44057
+ }
44058
+ let dataLength;
44059
+ if (typeof data === "string") {
44060
+ if ((!options.mask || skipMasking) && options[kByteLength] !== undefined) {
44061
+ dataLength = options[kByteLength];
44062
+ } else {
44063
+ data = Buffer.from(data);
44064
+ dataLength = data.length;
44065
+ }
44066
+ } else {
44067
+ dataLength = data.length;
44068
+ merge3 = options.mask && options.readOnly && !skipMasking;
44069
+ }
44070
+ let payloadLength = dataLength;
44071
+ if (dataLength >= 65536) {
44072
+ offset += 8;
44073
+ payloadLength = 127;
44074
+ } else if (dataLength > 125) {
44075
+ offset += 2;
44076
+ payloadLength = 126;
44077
+ }
44078
+ const target = Buffer.allocUnsafe(merge3 ? dataLength + offset : offset);
44079
+ target[0] = options.fin ? options.opcode | 128 : options.opcode;
44080
+ if (options.rsv1)
44081
+ target[0] |= 64;
44082
+ target[1] = payloadLength;
44083
+ if (payloadLength === 126) {
44084
+ target.writeUInt16BE(dataLength, 2);
44085
+ } else if (payloadLength === 127) {
44086
+ target[2] = target[3] = 0;
44087
+ target.writeUIntBE(dataLength, 4, 6);
44088
+ }
44089
+ if (!options.mask)
44090
+ return [target, data];
44091
+ target[1] |= 128;
44092
+ target[offset - 4] = mask[0];
44093
+ target[offset - 3] = mask[1];
44094
+ target[offset - 2] = mask[2];
44095
+ target[offset - 1] = mask[3];
44096
+ if (skipMasking)
44097
+ return [target, data];
44098
+ if (merge3) {
44099
+ applyMask(data, mask, target, offset, dataLength);
44100
+ return [target];
44101
+ }
44102
+ applyMask(data, mask, data, 0, dataLength);
44103
+ return [target, data];
44104
+ }
44105
+ close(code, data, mask, cb) {
44106
+ let buf;
44107
+ if (code === undefined) {
44108
+ buf = EMPTY_BUFFER2;
44109
+ } else if (typeof code !== "number" || !isValidStatusCode(code)) {
44110
+ throw new TypeError("First argument must be a valid error code number");
44111
+ } else if (data === undefined || !data.length) {
44112
+ buf = Buffer.allocUnsafe(2);
44113
+ buf.writeUInt16BE(code, 0);
44114
+ } else {
44115
+ const length = Buffer.byteLength(data);
44116
+ if (length > 123) {
44117
+ throw new RangeError("The message must not be greater than 123 bytes");
44118
+ }
44119
+ buf = Buffer.allocUnsafe(2 + length);
44120
+ buf.writeUInt16BE(code, 0);
44121
+ if (typeof data === "string") {
44122
+ buf.write(data, 2);
44123
+ } else {
44124
+ buf.set(data, 2);
44125
+ }
44126
+ }
44127
+ const options = {
44128
+ [kByteLength]: buf.length,
44129
+ fin: true,
44130
+ generateMask: this._generateMask,
44131
+ mask,
44132
+ maskBuffer: this._maskBuffer,
44133
+ opcode: 8,
44134
+ readOnly: false,
44135
+ rsv1: false
44136
+ };
44137
+ if (this._state !== DEFAULT) {
44138
+ this.enqueue([this.dispatch, buf, false, options, cb]);
44139
+ } else {
44140
+ this.sendFrame(Sender2.frame(buf, options), cb);
44141
+ }
44142
+ }
44143
+ ping(data, mask, cb) {
44144
+ let byteLength;
44145
+ let readOnly;
44146
+ if (typeof data === "string") {
44147
+ byteLength = Buffer.byteLength(data);
44148
+ readOnly = false;
44149
+ } else if (isBlob2(data)) {
44150
+ byteLength = data.size;
44151
+ readOnly = false;
44152
+ } else {
44153
+ data = toBuffer(data);
44154
+ byteLength = data.length;
44155
+ readOnly = toBuffer.readOnly;
44156
+ }
44157
+ if (byteLength > 125) {
44158
+ throw new RangeError("The data size must not be greater than 125 bytes");
44159
+ }
44160
+ const options = {
44161
+ [kByteLength]: byteLength,
44162
+ fin: true,
44163
+ generateMask: this._generateMask,
44164
+ mask,
44165
+ maskBuffer: this._maskBuffer,
44166
+ opcode: 9,
44167
+ readOnly,
44168
+ rsv1: false
44169
+ };
44170
+ if (isBlob2(data)) {
44171
+ if (this._state !== DEFAULT) {
44172
+ this.enqueue([this.getBlobData, data, false, options, cb]);
44173
+ } else {
44174
+ this.getBlobData(data, false, options, cb);
44175
+ }
44176
+ } else if (this._state !== DEFAULT) {
44177
+ this.enqueue([this.dispatch, data, false, options, cb]);
44178
+ } else {
44179
+ this.sendFrame(Sender2.frame(data, options), cb);
44180
+ }
44181
+ }
44182
+ pong(data, mask, cb) {
44183
+ let byteLength;
44184
+ let readOnly;
44185
+ if (typeof data === "string") {
44186
+ byteLength = Buffer.byteLength(data);
44187
+ readOnly = false;
44188
+ } else if (isBlob2(data)) {
44189
+ byteLength = data.size;
44190
+ readOnly = false;
44191
+ } else {
44192
+ data = toBuffer(data);
44193
+ byteLength = data.length;
44194
+ readOnly = toBuffer.readOnly;
44195
+ }
44196
+ if (byteLength > 125) {
44197
+ throw new RangeError("The data size must not be greater than 125 bytes");
44198
+ }
44199
+ const options = {
44200
+ [kByteLength]: byteLength,
44201
+ fin: true,
44202
+ generateMask: this._generateMask,
44203
+ mask,
44204
+ maskBuffer: this._maskBuffer,
44205
+ opcode: 10,
44206
+ readOnly,
44207
+ rsv1: false
44208
+ };
44209
+ if (isBlob2(data)) {
44210
+ if (this._state !== DEFAULT) {
44211
+ this.enqueue([this.getBlobData, data, false, options, cb]);
44212
+ } else {
44213
+ this.getBlobData(data, false, options, cb);
44214
+ }
44215
+ } else if (this._state !== DEFAULT) {
44216
+ this.enqueue([this.dispatch, data, false, options, cb]);
44217
+ } else {
44218
+ this.sendFrame(Sender2.frame(data, options), cb);
44219
+ }
44220
+ }
44221
+ send(data, options, cb) {
44222
+ const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
44223
+ let opcode = options.binary ? 2 : 1;
44224
+ let rsv1 = options.compress;
44225
+ let byteLength;
44226
+ let readOnly;
44227
+ if (typeof data === "string") {
44228
+ byteLength = Buffer.byteLength(data);
44229
+ readOnly = false;
44230
+ } else if (isBlob2(data)) {
44231
+ byteLength = data.size;
44232
+ readOnly = false;
44233
+ } else {
44234
+ data = toBuffer(data);
44235
+ byteLength = data.length;
44236
+ readOnly = toBuffer.readOnly;
44237
+ }
44238
+ if (this._firstFragment) {
44239
+ this._firstFragment = false;
44240
+ if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
44241
+ rsv1 = byteLength >= perMessageDeflate._threshold;
44242
+ }
44243
+ this._compress = rsv1;
44244
+ } else {
44245
+ rsv1 = false;
44246
+ opcode = 0;
44247
+ }
44248
+ if (options.fin)
44249
+ this._firstFragment = true;
44250
+ const opts = {
44251
+ [kByteLength]: byteLength,
44252
+ fin: options.fin,
44253
+ generateMask: this._generateMask,
44254
+ mask: options.mask,
44255
+ maskBuffer: this._maskBuffer,
44256
+ opcode,
44257
+ readOnly,
44258
+ rsv1
44259
+ };
44260
+ if (isBlob2(data)) {
44261
+ if (this._state !== DEFAULT) {
44262
+ this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
44263
+ } else {
44264
+ this.getBlobData(data, this._compress, opts, cb);
44265
+ }
44266
+ } else if (this._state !== DEFAULT) {
44267
+ this.enqueue([this.dispatch, data, this._compress, opts, cb]);
44268
+ } else {
44269
+ this.dispatch(data, this._compress, opts, cb);
44270
+ }
44271
+ }
44272
+ getBlobData(blob, compress, options, cb) {
44273
+ this._bufferedBytes += options[kByteLength];
44274
+ this._state = GET_BLOB_DATA;
44275
+ blob.arrayBuffer().then((arrayBuffer) => {
44276
+ if (this._socket.destroyed) {
44277
+ const err = new Error("The socket was closed while the blob was being read");
44278
+ process.nextTick(callCallbacks, this, err, cb);
44279
+ return;
44280
+ }
44281
+ this._bufferedBytes -= options[kByteLength];
44282
+ const data = toBuffer(arrayBuffer);
44283
+ if (!compress) {
44284
+ this._state = DEFAULT;
44285
+ this.sendFrame(Sender2.frame(data, options), cb);
44286
+ this.dequeue();
44287
+ } else {
44288
+ this.dispatch(data, compress, options, cb);
44289
+ }
44290
+ }).catch((err) => {
44291
+ process.nextTick(onError, this, err, cb);
44292
+ });
44293
+ }
44294
+ dispatch(data, compress, options, cb) {
44295
+ if (!compress) {
44296
+ this.sendFrame(Sender2.frame(data, options), cb);
44297
+ return;
44298
+ }
44299
+ const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
44300
+ this._bufferedBytes += options[kByteLength];
44301
+ this._state = DEFLATING;
44302
+ perMessageDeflate.compress(data, options.fin, (_, buf) => {
44303
+ if (this._socket.destroyed) {
44304
+ const err = new Error("The socket was closed while data was being compressed");
44305
+ callCallbacks(this, err, cb);
44306
+ return;
44307
+ }
44308
+ this._bufferedBytes -= options[kByteLength];
44309
+ this._state = DEFAULT;
44310
+ options.readOnly = false;
44311
+ this.sendFrame(Sender2.frame(buf, options), cb);
44312
+ this.dequeue();
44313
+ });
44314
+ }
44315
+ dequeue() {
44316
+ while (this._state === DEFAULT && this._queue.length) {
44317
+ const params = this._queue.shift();
44318
+ this._bufferedBytes -= params[3][kByteLength];
44319
+ Reflect.apply(params[0], this, params.slice(1));
44320
+ }
44321
+ }
44322
+ enqueue(params) {
44323
+ this._bufferedBytes += params[3][kByteLength];
44324
+ this._queue.push(params);
44325
+ }
44326
+ sendFrame(list2, cb) {
44327
+ if (list2.length === 2) {
44328
+ this._socket.cork();
44329
+ this._socket.write(list2[0]);
44330
+ this._socket.write(list2[1], cb);
44331
+ this._socket.uncork();
44332
+ } else {
44333
+ this._socket.write(list2[0], cb);
44334
+ }
44335
+ }
44336
+ }
44337
+ module.exports = Sender2;
44338
+ function callCallbacks(sender, err, cb) {
44339
+ if (typeof cb === "function")
44340
+ cb(err);
44341
+ for (let i3 = 0;i3 < sender._queue.length; i3++) {
44342
+ const params = sender._queue[i3];
44343
+ const callback = params[params.length - 1];
44344
+ if (typeof callback === "function")
44345
+ callback(err);
44346
+ }
44347
+ }
44348
+ function onError(sender, err, cb) {
44349
+ callCallbacks(sender, err, cb);
44350
+ sender.onerror(err);
44351
+ }
44352
+ });
44353
+
44354
+ // node_modules/ws/lib/event-target.js
44355
+ var require_event_target2 = __commonJS((exports, module) => {
44356
+ var { kForOnEventAttribute, kListener } = require_constants2();
44357
+ var kCode = Symbol("kCode");
44358
+ var kData = Symbol("kData");
44359
+ var kError = Symbol("kError");
44360
+ var kMessage = Symbol("kMessage");
44361
+ var kReason = Symbol("kReason");
44362
+ var kTarget = Symbol("kTarget");
44363
+ var kType = Symbol("kType");
44364
+ var kWasClean = Symbol("kWasClean");
44365
+
44366
+ class Event2 {
44367
+ constructor(type) {
44368
+ this[kTarget] = null;
44369
+ this[kType] = type;
44370
+ }
44371
+ get target() {
44372
+ return this[kTarget];
44373
+ }
44374
+ get type() {
44375
+ return this[kType];
44376
+ }
44377
+ }
44378
+ Object.defineProperty(Event2.prototype, "target", { enumerable: true });
44379
+ Object.defineProperty(Event2.prototype, "type", { enumerable: true });
44380
+
44381
+ class CloseEvent extends Event2 {
44382
+ constructor(type, options = {}) {
44383
+ super(type);
44384
+ this[kCode] = options.code === undefined ? 0 : options.code;
44385
+ this[kReason] = options.reason === undefined ? "" : options.reason;
44386
+ this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
44387
+ }
44388
+ get code() {
44389
+ return this[kCode];
44390
+ }
44391
+ get reason() {
44392
+ return this[kReason];
44393
+ }
44394
+ get wasClean() {
44395
+ return this[kWasClean];
44396
+ }
44397
+ }
44398
+ Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
44399
+ Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
44400
+ Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
44401
+
44402
+ class ErrorEvent2 extends Event2 {
44403
+ constructor(type, options = {}) {
44404
+ super(type);
44405
+ this[kError] = options.error === undefined ? null : options.error;
44406
+ this[kMessage] = options.message === undefined ? "" : options.message;
44407
+ }
44408
+ get error() {
44409
+ return this[kError];
44410
+ }
44411
+ get message() {
44412
+ return this[kMessage];
44413
+ }
44414
+ }
44415
+ Object.defineProperty(ErrorEvent2.prototype, "error", { enumerable: true });
44416
+ Object.defineProperty(ErrorEvent2.prototype, "message", { enumerable: true });
44417
+
44418
+ class MessageEvent2 extends Event2 {
44419
+ constructor(type, options = {}) {
44420
+ super(type);
44421
+ this[kData] = options.data === undefined ? null : options.data;
44422
+ }
44423
+ get data() {
44424
+ return this[kData];
44425
+ }
44426
+ }
44427
+ Object.defineProperty(MessageEvent2.prototype, "data", { enumerable: true });
44428
+ var EventTarget2 = {
44429
+ addEventListener(type, handler, options = {}) {
44430
+ for (const listener of this.listeners(type)) {
44431
+ if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
44432
+ return;
44433
+ }
44434
+ }
44435
+ let wrapper;
44436
+ if (type === "message") {
44437
+ wrapper = function onMessage(data, isBinary) {
44438
+ const event = new MessageEvent2("message", {
44439
+ data: isBinary ? data : data.toString()
44440
+ });
44441
+ event[kTarget] = this;
44442
+ callListener(handler, this, event);
44443
+ };
44444
+ } else if (type === "close") {
44445
+ wrapper = function onClose(code, message) {
44446
+ const event = new CloseEvent("close", {
44447
+ code,
44448
+ reason: message.toString(),
44449
+ wasClean: this._closeFrameReceived && this._closeFrameSent
44450
+ });
44451
+ event[kTarget] = this;
44452
+ callListener(handler, this, event);
44453
+ };
44454
+ } else if (type === "error") {
44455
+ wrapper = function onError(error2) {
44456
+ const event = new ErrorEvent2("error", {
44457
+ error: error2,
44458
+ message: error2.message
44459
+ });
44460
+ event[kTarget] = this;
44461
+ callListener(handler, this, event);
44462
+ };
44463
+ } else if (type === "open") {
44464
+ wrapper = function onOpen() {
44465
+ const event = new Event2("open");
44466
+ event[kTarget] = this;
44467
+ callListener(handler, this, event);
44468
+ };
44469
+ } else {
44470
+ return;
44471
+ }
44472
+ wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
44473
+ wrapper[kListener] = handler;
44474
+ if (options.once) {
44475
+ this.once(type, wrapper);
44476
+ } else {
44477
+ this.on(type, wrapper);
44478
+ }
44479
+ },
44480
+ removeEventListener(type, handler) {
44481
+ for (const listener of this.listeners(type)) {
44482
+ if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
44483
+ this.removeListener(type, listener);
44484
+ break;
44485
+ }
44486
+ }
44487
+ }
44488
+ };
44489
+ module.exports = {
44490
+ CloseEvent,
44491
+ ErrorEvent: ErrorEvent2,
44492
+ Event: Event2,
44493
+ EventTarget: EventTarget2,
44494
+ MessageEvent: MessageEvent2
44495
+ };
44496
+ function callListener(listener, thisArg, event) {
44497
+ if (typeof listener === "object" && listener.handleEvent) {
44498
+ listener.handleEvent.call(listener, event);
44499
+ } else {
44500
+ listener.call(thisArg, event);
44501
+ }
44502
+ }
44503
+ });
44504
+
44505
+ // node_modules/ws/lib/extension.js
44506
+ var require_extension2 = __commonJS((exports, module) => {
44507
+ var { tokenChars } = require_validation4();
44508
+ function push(dest, name, elem) {
44509
+ if (dest[name] === undefined)
44510
+ dest[name] = [elem];
44511
+ else
44512
+ dest[name].push(elem);
44513
+ }
44514
+ function parse6(header) {
44515
+ const offers = Object.create(null);
44516
+ let params = Object.create(null);
44517
+ let mustUnescape = false;
44518
+ let isEscaping = false;
44519
+ let inQuotes = false;
44520
+ let extensionName;
44521
+ let paramName;
44522
+ let start = -1;
44523
+ let code = -1;
44524
+ let end = -1;
44525
+ let i3 = 0;
44526
+ for (;i3 < header.length; i3++) {
44527
+ code = header.charCodeAt(i3);
44528
+ if (extensionName === undefined) {
44529
+ if (end === -1 && tokenChars[code] === 1) {
44530
+ if (start === -1)
44531
+ start = i3;
44532
+ } else if (i3 !== 0 && (code === 32 || code === 9)) {
44533
+ if (end === -1 && start !== -1)
44534
+ end = i3;
44535
+ } else if (code === 59 || code === 44) {
44536
+ if (start === -1) {
44537
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
44538
+ }
44539
+ if (end === -1)
44540
+ end = i3;
44541
+ const name = header.slice(start, end);
44542
+ if (code === 44) {
44543
+ push(offers, name, params);
44544
+ params = Object.create(null);
44545
+ } else {
44546
+ extensionName = name;
44547
+ }
44548
+ start = end = -1;
44549
+ } else {
44550
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
44551
+ }
44552
+ } else if (paramName === undefined) {
44553
+ if (end === -1 && tokenChars[code] === 1) {
44554
+ if (start === -1)
44555
+ start = i3;
44556
+ } else if (code === 32 || code === 9) {
44557
+ if (end === -1 && start !== -1)
44558
+ end = i3;
44559
+ } else if (code === 59 || code === 44) {
44560
+ if (start === -1) {
44561
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
44562
+ }
44563
+ if (end === -1)
44564
+ end = i3;
44565
+ push(params, header.slice(start, end), true);
44566
+ if (code === 44) {
44567
+ push(offers, extensionName, params);
44568
+ params = Object.create(null);
44569
+ extensionName = undefined;
44570
+ }
44571
+ start = end = -1;
44572
+ } else if (code === 61 && start !== -1 && end === -1) {
44573
+ paramName = header.slice(start, i3);
44574
+ start = end = -1;
44575
+ } else {
44576
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
44577
+ }
44578
+ } else {
44579
+ if (isEscaping) {
44580
+ if (tokenChars[code] !== 1) {
44581
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
44582
+ }
44583
+ if (start === -1)
44584
+ start = i3;
44585
+ else if (!mustUnescape)
44586
+ mustUnescape = true;
44587
+ isEscaping = false;
44588
+ } else if (inQuotes) {
44589
+ if (tokenChars[code] === 1) {
44590
+ if (start === -1)
44591
+ start = i3;
44592
+ } else if (code === 34 && start !== -1) {
44593
+ inQuotes = false;
44594
+ end = i3;
44595
+ } else if (code === 92) {
44596
+ isEscaping = true;
44597
+ } else {
44598
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
44599
+ }
44600
+ } else if (code === 34 && header.charCodeAt(i3 - 1) === 61) {
44601
+ inQuotes = true;
44602
+ } else if (end === -1 && tokenChars[code] === 1) {
44603
+ if (start === -1)
44604
+ start = i3;
44605
+ } else if (start !== -1 && (code === 32 || code === 9)) {
44606
+ if (end === -1)
44607
+ end = i3;
44608
+ } else if (code === 59 || code === 44) {
44609
+ if (start === -1) {
44610
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
44611
+ }
44612
+ if (end === -1)
44613
+ end = i3;
44614
+ let value = header.slice(start, end);
44615
+ if (mustUnescape) {
44616
+ value = value.replace(/\\/g, "");
44617
+ mustUnescape = false;
44618
+ }
44619
+ push(params, paramName, value);
44620
+ if (code === 44) {
44621
+ push(offers, extensionName, params);
44622
+ params = Object.create(null);
44623
+ extensionName = undefined;
44624
+ }
44625
+ paramName = undefined;
44626
+ start = end = -1;
44627
+ } else {
44628
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
44629
+ }
44630
+ }
44631
+ }
44632
+ if (start === -1 || inQuotes || code === 32 || code === 9) {
44633
+ throw new SyntaxError("Unexpected end of input");
44634
+ }
44635
+ if (end === -1)
44636
+ end = i3;
44637
+ const token = header.slice(start, end);
44638
+ if (extensionName === undefined) {
44639
+ push(offers, token, params);
44640
+ } else {
44641
+ if (paramName === undefined) {
44642
+ push(params, token, true);
44643
+ } else if (mustUnescape) {
44644
+ push(params, paramName, token.replace(/\\/g, ""));
44645
+ } else {
44646
+ push(params, paramName, token);
44647
+ }
44648
+ push(offers, extensionName, params);
44649
+ }
44650
+ return offers;
44651
+ }
44652
+ function format(extensions) {
44653
+ return Object.keys(extensions).map((extension) => {
44654
+ let configurations = extensions[extension];
44655
+ if (!Array.isArray(configurations))
44656
+ configurations = [configurations];
44657
+ return configurations.map((params) => {
44658
+ return [extension].concat(Object.keys(params).map((k) => {
44659
+ let values = params[k];
44660
+ if (!Array.isArray(values))
44661
+ values = [values];
44662
+ return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
44663
+ })).join("; ");
44664
+ }).join(", ");
44665
+ }).join(", ");
44666
+ }
44667
+ module.exports = { format, parse: parse6 };
44668
+ });
44669
+
44670
+ // node_modules/ws/lib/websocket.js
44671
+ var require_websocket2 = __commonJS((exports, module) => {
44672
+ var EventEmitter2 = __require("events");
44673
+ var https2 = __require("https");
44674
+ var http3 = __require("http");
44675
+ var net = __require("net");
44676
+ var tls = __require("tls");
44677
+ var { randomBytes: randomBytes5, createHash } = __require("crypto");
44678
+ var { Duplex, Readable: Readable2 } = __require("stream");
44679
+ var { URL: URL2 } = __require("url");
44680
+ var PerMessageDeflate = require_permessage_deflate2();
44681
+ var Receiver2 = require_receiver2();
44682
+ var Sender2 = require_sender2();
44683
+ var { isBlob: isBlob2 } = require_validation4();
44684
+ var {
44685
+ BINARY_TYPES,
44686
+ CLOSE_TIMEOUT,
44687
+ EMPTY_BUFFER: EMPTY_BUFFER2,
44688
+ GUID,
44689
+ kForOnEventAttribute,
44690
+ kListener,
44691
+ kStatusCode,
44692
+ kWebSocket,
44693
+ NOOP
44694
+ } = require_constants2();
44695
+ var {
44696
+ EventTarget: { addEventListener, removeEventListener }
44697
+ } = require_event_target2();
44698
+ var { format, parse: parse6 } = require_extension2();
44699
+ var { toBuffer } = require_buffer_util2();
44700
+ var kAborted = Symbol("kAborted");
44701
+ var protocolVersions = [8, 13];
44702
+ var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
44703
+ var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
44704
+
44705
+ class WebSocket3 extends EventEmitter2 {
44706
+ constructor(address2, protocols, options) {
44707
+ super();
44708
+ this._binaryType = BINARY_TYPES[0];
44709
+ this._closeCode = 1006;
44710
+ this._closeFrameReceived = false;
44711
+ this._closeFrameSent = false;
44712
+ this._closeMessage = EMPTY_BUFFER2;
44713
+ this._closeTimer = null;
44714
+ this._errorEmitted = false;
44715
+ this._extensions = {};
44716
+ this._paused = false;
44717
+ this._protocol = "";
44718
+ this._readyState = WebSocket3.CONNECTING;
44719
+ this._receiver = null;
44720
+ this._sender = null;
44721
+ this._socket = null;
44722
+ if (address2 !== null) {
44723
+ this._bufferedAmount = 0;
44724
+ this._isServer = false;
44725
+ this._redirects = 0;
44726
+ if (protocols === undefined) {
44727
+ protocols = [];
44728
+ } else if (!Array.isArray(protocols)) {
44729
+ if (typeof protocols === "object" && protocols !== null) {
44730
+ options = protocols;
44731
+ protocols = [];
44732
+ } else {
44733
+ protocols = [protocols];
44734
+ }
44735
+ }
44736
+ initAsClient(this, address2, protocols, options);
44737
+ } else {
44738
+ this._autoPong = options.autoPong;
44739
+ this._closeTimeout = options.closeTimeout;
44740
+ this._isServer = true;
44741
+ }
44742
+ }
44743
+ get binaryType() {
44744
+ return this._binaryType;
44745
+ }
44746
+ set binaryType(type) {
44747
+ if (!BINARY_TYPES.includes(type))
44748
+ return;
44749
+ this._binaryType = type;
44750
+ if (this._receiver)
44751
+ this._receiver._binaryType = type;
44752
+ }
44753
+ get bufferedAmount() {
44754
+ if (!this._socket)
44755
+ return this._bufferedAmount;
44756
+ return this._socket._writableState.length + this._sender._bufferedBytes;
44757
+ }
44758
+ get extensions() {
44759
+ return Object.keys(this._extensions).join();
44760
+ }
44761
+ get isPaused() {
44762
+ return this._paused;
44763
+ }
44764
+ get onclose() {
44765
+ return null;
44766
+ }
44767
+ get onerror() {
44768
+ return null;
44769
+ }
44770
+ get onopen() {
44771
+ return null;
44772
+ }
44773
+ get onmessage() {
44774
+ return null;
44775
+ }
44776
+ get protocol() {
44777
+ return this._protocol;
44778
+ }
44779
+ get readyState() {
44780
+ return this._readyState;
44781
+ }
44782
+ get url() {
44783
+ return this._url;
44784
+ }
44785
+ setSocket(socket, head, options) {
44786
+ const receiver = new Receiver2({
44787
+ allowSynchronousEvents: options.allowSynchronousEvents,
44788
+ binaryType: this.binaryType,
44789
+ extensions: this._extensions,
44790
+ isServer: this._isServer,
44791
+ maxPayload: options.maxPayload,
44792
+ skipUTF8Validation: options.skipUTF8Validation
44793
+ });
44794
+ const sender = new Sender2(socket, this._extensions, options.generateMask);
44795
+ this._receiver = receiver;
44796
+ this._sender = sender;
44797
+ this._socket = socket;
44798
+ receiver[kWebSocket] = this;
44799
+ sender[kWebSocket] = this;
44800
+ socket[kWebSocket] = this;
44801
+ receiver.on("conclude", receiverOnConclude);
44802
+ receiver.on("drain", receiverOnDrain);
44803
+ receiver.on("error", receiverOnError);
44804
+ receiver.on("message", receiverOnMessage);
44805
+ receiver.on("ping", receiverOnPing);
44806
+ receiver.on("pong", receiverOnPong);
44807
+ sender.onerror = senderOnError;
44808
+ if (socket.setTimeout)
44809
+ socket.setTimeout(0);
44810
+ if (socket.setNoDelay)
44811
+ socket.setNoDelay();
44812
+ if (head.length > 0)
44813
+ socket.unshift(head);
44814
+ socket.on("close", socketOnClose);
44815
+ socket.on("data", socketOnData);
44816
+ socket.on("end", socketOnEnd);
44817
+ socket.on("error", socketOnError);
44818
+ this._readyState = WebSocket3.OPEN;
44819
+ this.emit("open");
44820
+ }
44821
+ emitClose() {
44822
+ if (!this._socket) {
44823
+ this._readyState = WebSocket3.CLOSED;
44824
+ this.emit("close", this._closeCode, this._closeMessage);
44825
+ return;
44826
+ }
44827
+ if (this._extensions[PerMessageDeflate.extensionName]) {
44828
+ this._extensions[PerMessageDeflate.extensionName].cleanup();
44829
+ }
44830
+ this._receiver.removeAllListeners();
44831
+ this._readyState = WebSocket3.CLOSED;
44832
+ this.emit("close", this._closeCode, this._closeMessage);
44833
+ }
44834
+ close(code, data) {
44835
+ if (this.readyState === WebSocket3.CLOSED)
44836
+ return;
44837
+ if (this.readyState === WebSocket3.CONNECTING) {
44838
+ const msg = "WebSocket was closed before the connection was established";
44839
+ abortHandshake(this, this._req, msg);
44840
+ return;
44841
+ }
44842
+ if (this.readyState === WebSocket3.CLOSING) {
44843
+ if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
44844
+ this._socket.end();
44845
+ }
44846
+ return;
44847
+ }
44848
+ this._readyState = WebSocket3.CLOSING;
44849
+ this._sender.close(code, data, !this._isServer, (err) => {
44850
+ if (err)
44851
+ return;
44852
+ this._closeFrameSent = true;
44853
+ if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
44854
+ this._socket.end();
44855
+ }
44856
+ });
44857
+ setCloseTimer(this);
44858
+ }
44859
+ pause() {
44860
+ if (this.readyState === WebSocket3.CONNECTING || this.readyState === WebSocket3.CLOSED) {
44861
+ return;
44862
+ }
44863
+ this._paused = true;
44864
+ this._socket.pause();
44865
+ }
44866
+ ping(data, mask, cb) {
44867
+ if (this.readyState === WebSocket3.CONNECTING) {
44868
+ throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
44869
+ }
44870
+ if (typeof data === "function") {
44871
+ cb = data;
44872
+ data = mask = undefined;
44873
+ } else if (typeof mask === "function") {
44874
+ cb = mask;
44875
+ mask = undefined;
44876
+ }
44877
+ if (typeof data === "number")
44878
+ data = data.toString();
44879
+ if (this.readyState !== WebSocket3.OPEN) {
44880
+ sendAfterClose(this, data, cb);
44881
+ return;
44882
+ }
44883
+ if (mask === undefined)
44884
+ mask = !this._isServer;
44885
+ this._sender.ping(data || EMPTY_BUFFER2, mask, cb);
44886
+ }
44887
+ pong(data, mask, cb) {
44888
+ if (this.readyState === WebSocket3.CONNECTING) {
44889
+ throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
44890
+ }
44891
+ if (typeof data === "function") {
44892
+ cb = data;
44893
+ data = mask = undefined;
44894
+ } else if (typeof mask === "function") {
44895
+ cb = mask;
44896
+ mask = undefined;
44897
+ }
44898
+ if (typeof data === "number")
44899
+ data = data.toString();
44900
+ if (this.readyState !== WebSocket3.OPEN) {
44901
+ sendAfterClose(this, data, cb);
44902
+ return;
44903
+ }
44904
+ if (mask === undefined)
44905
+ mask = !this._isServer;
44906
+ this._sender.pong(data || EMPTY_BUFFER2, mask, cb);
44907
+ }
44908
+ resume() {
44909
+ if (this.readyState === WebSocket3.CONNECTING || this.readyState === WebSocket3.CLOSED) {
44910
+ return;
44911
+ }
44912
+ this._paused = false;
44913
+ if (!this._receiver._writableState.needDrain)
44914
+ this._socket.resume();
44915
+ }
44916
+ send(data, options, cb) {
44917
+ if (this.readyState === WebSocket3.CONNECTING) {
44918
+ throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
44919
+ }
44920
+ if (typeof options === "function") {
44921
+ cb = options;
44922
+ options = {};
44923
+ }
44924
+ if (typeof data === "number")
44925
+ data = data.toString();
44926
+ if (this.readyState !== WebSocket3.OPEN) {
44927
+ sendAfterClose(this, data, cb);
44928
+ return;
44929
+ }
44930
+ const opts = {
44931
+ binary: typeof data !== "string",
44932
+ mask: !this._isServer,
44933
+ compress: true,
44934
+ fin: true,
44935
+ ...options
44936
+ };
44937
+ if (!this._extensions[PerMessageDeflate.extensionName]) {
44938
+ opts.compress = false;
44939
+ }
44940
+ this._sender.send(data || EMPTY_BUFFER2, opts, cb);
44941
+ }
44942
+ terminate() {
44943
+ if (this.readyState === WebSocket3.CLOSED)
44944
+ return;
44945
+ if (this.readyState === WebSocket3.CONNECTING) {
44946
+ const msg = "WebSocket was closed before the connection was established";
44947
+ abortHandshake(this, this._req, msg);
44948
+ return;
44949
+ }
44950
+ if (this._socket) {
44951
+ this._readyState = WebSocket3.CLOSING;
44952
+ this._socket.destroy();
44953
+ }
44954
+ }
44955
+ }
44956
+ Object.defineProperty(WebSocket3, "CONNECTING", {
44957
+ enumerable: true,
44958
+ value: readyStates.indexOf("CONNECTING")
44959
+ });
44960
+ Object.defineProperty(WebSocket3.prototype, "CONNECTING", {
44961
+ enumerable: true,
44962
+ value: readyStates.indexOf("CONNECTING")
44963
+ });
44964
+ Object.defineProperty(WebSocket3, "OPEN", {
44965
+ enumerable: true,
44966
+ value: readyStates.indexOf("OPEN")
44967
+ });
44968
+ Object.defineProperty(WebSocket3.prototype, "OPEN", {
44969
+ enumerable: true,
44970
+ value: readyStates.indexOf("OPEN")
44971
+ });
44972
+ Object.defineProperty(WebSocket3, "CLOSING", {
44973
+ enumerable: true,
44974
+ value: readyStates.indexOf("CLOSING")
44975
+ });
44976
+ Object.defineProperty(WebSocket3.prototype, "CLOSING", {
44977
+ enumerable: true,
44978
+ value: readyStates.indexOf("CLOSING")
44979
+ });
44980
+ Object.defineProperty(WebSocket3, "CLOSED", {
44981
+ enumerable: true,
44982
+ value: readyStates.indexOf("CLOSED")
44983
+ });
44984
+ Object.defineProperty(WebSocket3.prototype, "CLOSED", {
44985
+ enumerable: true,
44986
+ value: readyStates.indexOf("CLOSED")
44987
+ });
44988
+ [
44989
+ "binaryType",
44990
+ "bufferedAmount",
44991
+ "extensions",
44992
+ "isPaused",
44993
+ "protocol",
44994
+ "readyState",
44995
+ "url"
44996
+ ].forEach((property) => {
44997
+ Object.defineProperty(WebSocket3.prototype, property, { enumerable: true });
44998
+ });
44999
+ ["open", "error", "close", "message"].forEach((method) => {
45000
+ Object.defineProperty(WebSocket3.prototype, `on${method}`, {
45001
+ enumerable: true,
45002
+ get() {
45003
+ for (const listener of this.listeners(method)) {
45004
+ if (listener[kForOnEventAttribute])
45005
+ return listener[kListener];
45006
+ }
45007
+ return null;
45008
+ },
45009
+ set(handler) {
45010
+ for (const listener of this.listeners(method)) {
45011
+ if (listener[kForOnEventAttribute]) {
45012
+ this.removeListener(method, listener);
45013
+ break;
45014
+ }
45015
+ }
45016
+ if (typeof handler !== "function")
45017
+ return;
45018
+ this.addEventListener(method, handler, {
45019
+ [kForOnEventAttribute]: true
45020
+ });
45021
+ }
45022
+ });
45023
+ });
45024
+ WebSocket3.prototype.addEventListener = addEventListener;
45025
+ WebSocket3.prototype.removeEventListener = removeEventListener;
45026
+ module.exports = WebSocket3;
45027
+ function initAsClient(websocket, address2, protocols, options) {
45028
+ const opts = {
45029
+ allowSynchronousEvents: true,
45030
+ autoPong: true,
45031
+ closeTimeout: CLOSE_TIMEOUT,
45032
+ protocolVersion: protocolVersions[1],
45033
+ maxPayload: 100 * 1024 * 1024,
45034
+ skipUTF8Validation: false,
45035
+ perMessageDeflate: true,
45036
+ followRedirects: false,
45037
+ maxRedirects: 10,
45038
+ ...options,
45039
+ socketPath: undefined,
45040
+ hostname: undefined,
45041
+ protocol: undefined,
45042
+ timeout: undefined,
45043
+ method: "GET",
45044
+ host: undefined,
45045
+ path: undefined,
45046
+ port: undefined
45047
+ };
45048
+ websocket._autoPong = opts.autoPong;
45049
+ websocket._closeTimeout = opts.closeTimeout;
45050
+ if (!protocolVersions.includes(opts.protocolVersion)) {
45051
+ throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} ` + `(supported versions: ${protocolVersions.join(", ")})`);
45052
+ }
45053
+ let parsedUrl;
45054
+ if (address2 instanceof URL2) {
45055
+ parsedUrl = address2;
45056
+ } else {
45057
+ try {
45058
+ parsedUrl = new URL2(address2);
45059
+ } catch (e7) {
45060
+ throw new SyntaxError(`Invalid URL: ${address2}`);
45061
+ }
45062
+ }
45063
+ if (parsedUrl.protocol === "http:") {
45064
+ parsedUrl.protocol = "ws:";
45065
+ } else if (parsedUrl.protocol === "https:") {
45066
+ parsedUrl.protocol = "wss:";
45067
+ }
45068
+ websocket._url = parsedUrl.href;
45069
+ const isSecure = parsedUrl.protocol === "wss:";
45070
+ const isIpcUrl = parsedUrl.protocol === "ws+unix:";
45071
+ let invalidUrlMessage;
45072
+ if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
45073
+ invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", ` + '"http:", "https:", or "ws+unix:"';
45074
+ } else if (isIpcUrl && !parsedUrl.pathname) {
45075
+ invalidUrlMessage = "The URL's pathname is empty";
45076
+ } else if (parsedUrl.hash) {
45077
+ invalidUrlMessage = "The URL contains a fragment identifier";
45078
+ }
45079
+ if (invalidUrlMessage) {
45080
+ const err = new SyntaxError(invalidUrlMessage);
45081
+ if (websocket._redirects === 0) {
45082
+ throw err;
45083
+ } else {
45084
+ emitErrorAndClose(websocket, err);
45085
+ return;
45086
+ }
45087
+ }
45088
+ const defaultPort = isSecure ? 443 : 80;
45089
+ const key = randomBytes5(16).toString("base64");
45090
+ const request = isSecure ? https2.request : http3.request;
45091
+ const protocolSet = new Set;
45092
+ let perMessageDeflate;
45093
+ opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
45094
+ opts.defaultPort = opts.defaultPort || defaultPort;
45095
+ opts.port = parsedUrl.port || defaultPort;
45096
+ opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
45097
+ opts.headers = {
45098
+ ...opts.headers,
45099
+ "Sec-WebSocket-Version": opts.protocolVersion,
45100
+ "Sec-WebSocket-Key": key,
45101
+ Connection: "Upgrade",
45102
+ Upgrade: "websocket"
45103
+ };
45104
+ opts.path = parsedUrl.pathname + parsedUrl.search;
45105
+ opts.timeout = opts.handshakeTimeout;
45106
+ if (opts.perMessageDeflate) {
45107
+ perMessageDeflate = new PerMessageDeflate(opts.perMessageDeflate !== true ? opts.perMessageDeflate : {}, false, opts.maxPayload);
45108
+ opts.headers["Sec-WebSocket-Extensions"] = format({
45109
+ [PerMessageDeflate.extensionName]: perMessageDeflate.offer()
45110
+ });
45111
+ }
45112
+ if (protocols.length) {
45113
+ for (const protocol of protocols) {
45114
+ if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
45115
+ throw new SyntaxError("An invalid or duplicated subprotocol was specified");
45116
+ }
45117
+ protocolSet.add(protocol);
45118
+ }
45119
+ opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
45120
+ }
45121
+ if (opts.origin) {
45122
+ if (opts.protocolVersion < 13) {
45123
+ opts.headers["Sec-WebSocket-Origin"] = opts.origin;
45124
+ } else {
45125
+ opts.headers.Origin = opts.origin;
45126
+ }
45127
+ }
45128
+ if (parsedUrl.username || parsedUrl.password) {
45129
+ opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
45130
+ }
45131
+ if (isIpcUrl) {
45132
+ const parts = opts.path.split(":");
45133
+ opts.socketPath = parts[0];
45134
+ opts.path = parts[1];
45135
+ }
45136
+ let req;
45137
+ if (opts.followRedirects) {
45138
+ if (websocket._redirects === 0) {
45139
+ websocket._originalIpc = isIpcUrl;
45140
+ websocket._originalSecure = isSecure;
45141
+ websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
45142
+ const headers = options && options.headers;
45143
+ options = { ...options, headers: {} };
45144
+ if (headers) {
45145
+ for (const [key2, value] of Object.entries(headers)) {
45146
+ options.headers[key2.toLowerCase()] = value;
45147
+ }
45148
+ }
45149
+ } else if (websocket.listenerCount("redirect") === 0) {
45150
+ const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
45151
+ if (!isSameHost || websocket._originalSecure && !isSecure) {
45152
+ delete opts.headers.authorization;
45153
+ delete opts.headers.cookie;
45154
+ if (!isSameHost)
45155
+ delete opts.headers.host;
45156
+ opts.auth = undefined;
45157
+ }
45158
+ }
45159
+ if (opts.auth && !options.headers.authorization) {
45160
+ options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
45161
+ }
45162
+ req = websocket._req = request(opts);
45163
+ if (websocket._redirects) {
45164
+ websocket.emit("redirect", websocket.url, req);
45165
+ }
45166
+ } else {
45167
+ req = websocket._req = request(opts);
45168
+ }
45169
+ if (opts.timeout) {
45170
+ req.on("timeout", () => {
45171
+ abortHandshake(websocket, req, "Opening handshake has timed out");
45172
+ });
45173
+ }
45174
+ req.on("error", (err) => {
45175
+ if (req === null || req[kAborted])
45176
+ return;
45177
+ req = websocket._req = null;
45178
+ emitErrorAndClose(websocket, err);
45179
+ });
45180
+ req.on("response", (res) => {
45181
+ const location = res.headers.location;
45182
+ const statusCode = res.statusCode;
45183
+ if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
45184
+ if (++websocket._redirects > opts.maxRedirects) {
45185
+ abortHandshake(websocket, req, "Maximum redirects exceeded");
45186
+ return;
45187
+ }
45188
+ req.abort();
45189
+ let addr;
45190
+ try {
45191
+ addr = new URL2(location, address2);
45192
+ } catch (e7) {
45193
+ const err = new SyntaxError(`Invalid URL: ${location}`);
45194
+ emitErrorAndClose(websocket, err);
45195
+ return;
45196
+ }
45197
+ initAsClient(websocket, addr, protocols, options);
45198
+ } else if (!websocket.emit("unexpected-response", req, res)) {
45199
+ abortHandshake(websocket, req, `Unexpected server response: ${res.statusCode}`);
45200
+ }
45201
+ });
45202
+ req.on("upgrade", (res, socket, head) => {
45203
+ websocket.emit("upgrade", res);
45204
+ if (websocket.readyState !== WebSocket3.CONNECTING)
45205
+ return;
45206
+ req = websocket._req = null;
45207
+ const upgrade = res.headers.upgrade;
45208
+ if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
45209
+ abortHandshake(websocket, socket, "Invalid Upgrade header");
45210
+ return;
45211
+ }
45212
+ const digest = createHash("sha1").update(key + GUID).digest("base64");
45213
+ if (res.headers["sec-websocket-accept"] !== digest) {
45214
+ abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
45215
+ return;
45216
+ }
45217
+ const serverProt = res.headers["sec-websocket-protocol"];
45218
+ let protError;
45219
+ if (serverProt !== undefined) {
45220
+ if (!protocolSet.size) {
45221
+ protError = "Server sent a subprotocol but none was requested";
45222
+ } else if (!protocolSet.has(serverProt)) {
45223
+ protError = "Server sent an invalid subprotocol";
45224
+ }
45225
+ } else if (protocolSet.size) {
45226
+ protError = "Server sent no subprotocol";
45227
+ }
45228
+ if (protError) {
45229
+ abortHandshake(websocket, socket, protError);
45230
+ return;
45231
+ }
45232
+ if (serverProt)
45233
+ websocket._protocol = serverProt;
45234
+ const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
45235
+ if (secWebSocketExtensions !== undefined) {
45236
+ if (!perMessageDeflate) {
45237
+ const message = "Server sent a Sec-WebSocket-Extensions header but no extension " + "was requested";
45238
+ abortHandshake(websocket, socket, message);
45239
+ return;
45240
+ }
45241
+ let extensions;
45242
+ try {
45243
+ extensions = parse6(secWebSocketExtensions);
45244
+ } catch (err) {
45245
+ const message = "Invalid Sec-WebSocket-Extensions header";
45246
+ abortHandshake(websocket, socket, message);
45247
+ return;
45248
+ }
45249
+ const extensionNames = Object.keys(extensions);
45250
+ if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
45251
+ const message = "Server indicated an extension that was not requested";
45252
+ abortHandshake(websocket, socket, message);
45253
+ return;
45254
+ }
45255
+ try {
45256
+ perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
45257
+ } catch (err) {
45258
+ const message = "Invalid Sec-WebSocket-Extensions header";
45259
+ abortHandshake(websocket, socket, message);
45260
+ return;
45261
+ }
45262
+ websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
45263
+ }
45264
+ websocket.setSocket(socket, head, {
45265
+ allowSynchronousEvents: opts.allowSynchronousEvents,
45266
+ generateMask: opts.generateMask,
45267
+ maxPayload: opts.maxPayload,
45268
+ skipUTF8Validation: opts.skipUTF8Validation
45269
+ });
45270
+ });
45271
+ if (opts.finishRequest) {
45272
+ opts.finishRequest(req, websocket);
45273
+ } else {
45274
+ req.end();
45275
+ }
45276
+ }
45277
+ function emitErrorAndClose(websocket, err) {
45278
+ websocket._readyState = WebSocket3.CLOSING;
45279
+ websocket._errorEmitted = true;
45280
+ websocket.emit("error", err);
45281
+ websocket.emitClose();
45282
+ }
45283
+ function netConnect(options) {
45284
+ options.path = options.socketPath;
45285
+ return net.connect(options);
45286
+ }
45287
+ function tlsConnect(options) {
45288
+ options.path = undefined;
45289
+ if (!options.servername && options.servername !== "") {
45290
+ options.servername = net.isIP(options.host) ? "" : options.host;
45291
+ }
45292
+ return tls.connect(options);
45293
+ }
45294
+ function abortHandshake(websocket, stream4, message) {
45295
+ websocket._readyState = WebSocket3.CLOSING;
45296
+ const err = new Error(message);
45297
+ Error.captureStackTrace(err, abortHandshake);
45298
+ if (stream4.setHeader) {
45299
+ stream4[kAborted] = true;
45300
+ stream4.abort();
45301
+ if (stream4.socket && !stream4.socket.destroyed) {
45302
+ stream4.socket.destroy();
45303
+ }
45304
+ process.nextTick(emitErrorAndClose, websocket, err);
45305
+ } else {
45306
+ stream4.destroy(err);
45307
+ stream4.once("error", websocket.emit.bind(websocket, "error"));
45308
+ stream4.once("close", websocket.emitClose.bind(websocket));
45309
+ }
45310
+ }
45311
+ function sendAfterClose(websocket, data, cb) {
45312
+ if (data) {
45313
+ const length = isBlob2(data) ? data.size : toBuffer(data).length;
45314
+ if (websocket._socket)
45315
+ websocket._sender._bufferedBytes += length;
45316
+ else
45317
+ websocket._bufferedAmount += length;
45318
+ }
45319
+ if (cb) {
45320
+ const err = new Error(`WebSocket is not open: readyState ${websocket.readyState} ` + `(${readyStates[websocket.readyState]})`);
45321
+ process.nextTick(cb, err);
45322
+ }
45323
+ }
45324
+ function receiverOnConclude(code, reason) {
45325
+ const websocket = this[kWebSocket];
45326
+ websocket._closeFrameReceived = true;
45327
+ websocket._closeMessage = reason;
45328
+ websocket._closeCode = code;
45329
+ if (websocket._socket[kWebSocket] === undefined)
45330
+ return;
45331
+ websocket._socket.removeListener("data", socketOnData);
45332
+ process.nextTick(resume, websocket._socket);
45333
+ if (code === 1005)
45334
+ websocket.close();
45335
+ else
45336
+ websocket.close(code, reason);
45337
+ }
45338
+ function receiverOnDrain() {
45339
+ const websocket = this[kWebSocket];
45340
+ if (!websocket.isPaused)
45341
+ websocket._socket.resume();
45342
+ }
45343
+ function receiverOnError(err) {
45344
+ const websocket = this[kWebSocket];
45345
+ if (websocket._socket[kWebSocket] !== undefined) {
45346
+ websocket._socket.removeListener("data", socketOnData);
45347
+ process.nextTick(resume, websocket._socket);
45348
+ websocket.close(err[kStatusCode]);
45349
+ }
45350
+ if (!websocket._errorEmitted) {
45351
+ websocket._errorEmitted = true;
45352
+ websocket.emit("error", err);
45353
+ }
45354
+ }
45355
+ function receiverOnFinish() {
45356
+ this[kWebSocket].emitClose();
45357
+ }
45358
+ function receiverOnMessage(data, isBinary) {
45359
+ this[kWebSocket].emit("message", data, isBinary);
45360
+ }
45361
+ function receiverOnPing(data) {
45362
+ const websocket = this[kWebSocket];
45363
+ if (websocket._autoPong)
45364
+ websocket.pong(data, !this._isServer, NOOP);
45365
+ websocket.emit("ping", data);
45366
+ }
45367
+ function receiverOnPong(data) {
45368
+ this[kWebSocket].emit("pong", data);
45369
+ }
45370
+ function resume(stream4) {
45371
+ stream4.resume();
45372
+ }
45373
+ function senderOnError(err) {
45374
+ const websocket = this[kWebSocket];
45375
+ if (websocket.readyState === WebSocket3.CLOSED)
45376
+ return;
45377
+ if (websocket.readyState === WebSocket3.OPEN) {
45378
+ websocket._readyState = WebSocket3.CLOSING;
45379
+ setCloseTimer(websocket);
45380
+ }
45381
+ this._socket.end();
45382
+ if (!websocket._errorEmitted) {
45383
+ websocket._errorEmitted = true;
45384
+ websocket.emit("error", err);
45385
+ }
45386
+ }
45387
+ function setCloseTimer(websocket) {
45388
+ websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), websocket._closeTimeout);
45389
+ }
45390
+ function socketOnClose() {
45391
+ const websocket = this[kWebSocket];
45392
+ this.removeListener("close", socketOnClose);
45393
+ this.removeListener("data", socketOnData);
45394
+ this.removeListener("end", socketOnEnd);
45395
+ websocket._readyState = WebSocket3.CLOSING;
45396
+ if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
45397
+ const chunk = this.read(this._readableState.length);
45398
+ websocket._receiver.write(chunk);
45399
+ }
45400
+ websocket._receiver.end();
45401
+ this[kWebSocket] = undefined;
45402
+ clearTimeout(websocket._closeTimer);
45403
+ if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
45404
+ websocket.emitClose();
45405
+ } else {
45406
+ websocket._receiver.on("error", receiverOnFinish);
45407
+ websocket._receiver.on("finish", receiverOnFinish);
45408
+ }
45409
+ }
45410
+ function socketOnData(chunk) {
45411
+ if (!this[kWebSocket]._receiver.write(chunk)) {
45412
+ this.pause();
45413
+ }
45414
+ }
45415
+ function socketOnEnd() {
45416
+ const websocket = this[kWebSocket];
45417
+ websocket._readyState = WebSocket3.CLOSING;
45418
+ websocket._receiver.end();
45419
+ this.end();
45420
+ }
45421
+ function socketOnError() {
45422
+ const websocket = this[kWebSocket];
45423
+ this.removeListener("error", socketOnError);
45424
+ this.on("error", NOOP);
45425
+ if (websocket) {
45426
+ websocket._readyState = WebSocket3.CLOSING;
45427
+ this.destroy();
45428
+ }
45429
+ }
45430
+ });
45431
+
45432
+ // node_modules/ws/lib/stream.js
45433
+ var require_stream2 = __commonJS((exports, module) => {
45434
+ var WebSocket3 = require_websocket2();
45435
+ var { Duplex } = __require("stream");
45436
+ function emitClose(stream4) {
45437
+ stream4.emit("close");
45438
+ }
45439
+ function duplexOnEnd() {
45440
+ if (!this.destroyed && this._writableState.finished) {
45441
+ this.destroy();
45442
+ }
45443
+ }
45444
+ function duplexOnError(err) {
45445
+ this.removeListener("error", duplexOnError);
45446
+ this.destroy();
45447
+ if (this.listenerCount("error") === 0) {
45448
+ this.emit("error", err);
45449
+ }
45450
+ }
45451
+ function createWebSocketStream2(ws, options) {
45452
+ let terminateOnDestroy = true;
45453
+ const duplex = new Duplex({
45454
+ ...options,
45455
+ autoDestroy: false,
45456
+ emitClose: false,
45457
+ objectMode: false,
45458
+ writableObjectMode: false
45459
+ });
45460
+ ws.on("message", function message(msg, isBinary) {
45461
+ const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
45462
+ if (!duplex.push(data))
45463
+ ws.pause();
45464
+ });
45465
+ ws.once("error", function error(err) {
45466
+ if (duplex.destroyed)
45467
+ return;
45468
+ terminateOnDestroy = false;
45469
+ duplex.destroy(err);
45470
+ });
45471
+ ws.once("close", function close() {
45472
+ if (duplex.destroyed)
45473
+ return;
45474
+ duplex.push(null);
45475
+ });
45476
+ duplex._destroy = function(err, callback) {
45477
+ if (ws.readyState === ws.CLOSED) {
45478
+ callback(err);
45479
+ process.nextTick(emitClose, duplex);
45480
+ return;
45481
+ }
45482
+ let called = false;
45483
+ ws.once("error", function error(err2) {
45484
+ called = true;
45485
+ callback(err2);
45486
+ });
45487
+ ws.once("close", function close() {
45488
+ if (!called)
45489
+ callback(err);
45490
+ process.nextTick(emitClose, duplex);
45491
+ });
45492
+ if (terminateOnDestroy)
45493
+ ws.terminate();
45494
+ };
45495
+ duplex._final = function(callback) {
45496
+ if (ws.readyState === ws.CONNECTING) {
45497
+ ws.once("open", function open() {
45498
+ duplex._final(callback);
45499
+ });
45500
+ return;
45501
+ }
45502
+ if (ws._socket === null)
45503
+ return;
45504
+ if (ws._socket._writableState.finished) {
45505
+ callback();
45506
+ if (duplex._readableState.endEmitted)
45507
+ duplex.destroy();
45508
+ } else {
45509
+ ws._socket.once("finish", function finish() {
45510
+ callback();
45511
+ });
45512
+ ws.close();
45513
+ }
45514
+ };
45515
+ duplex._read = function() {
45516
+ if (ws.isPaused)
45517
+ ws.resume();
45518
+ };
45519
+ duplex._write = function(chunk, encoding, callback) {
45520
+ if (ws.readyState === ws.CONNECTING) {
45521
+ ws.once("open", function open() {
45522
+ duplex._write(chunk, encoding, callback);
45523
+ });
45524
+ return;
45525
+ }
45526
+ ws.send(chunk, callback);
45527
+ };
45528
+ duplex.on("end", duplexOnEnd);
45529
+ duplex.on("error", duplexOnError);
45530
+ return duplex;
45531
+ }
45532
+ module.exports = createWebSocketStream2;
45533
+ });
45534
+
45535
+ // node_modules/ws/lib/subprotocol.js
45536
+ var require_subprotocol2 = __commonJS((exports, module) => {
45537
+ var { tokenChars } = require_validation4();
45538
+ function parse6(header) {
45539
+ const protocols = new Set;
45540
+ let start = -1;
45541
+ let end = -1;
45542
+ let i3 = 0;
45543
+ for (i3;i3 < header.length; i3++) {
45544
+ const code = header.charCodeAt(i3);
45545
+ if (end === -1 && tokenChars[code] === 1) {
45546
+ if (start === -1)
45547
+ start = i3;
45548
+ } else if (i3 !== 0 && (code === 32 || code === 9)) {
45549
+ if (end === -1 && start !== -1)
45550
+ end = i3;
45551
+ } else if (code === 44) {
45552
+ if (start === -1) {
45553
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
45554
+ }
45555
+ if (end === -1)
45556
+ end = i3;
45557
+ const protocol2 = header.slice(start, end);
45558
+ if (protocols.has(protocol2)) {
45559
+ throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
45560
+ }
45561
+ protocols.add(protocol2);
45562
+ start = end = -1;
45563
+ } else {
45564
+ throw new SyntaxError(`Unexpected character at index ${i3}`);
45565
+ }
45566
+ }
45567
+ if (start === -1 || end !== -1) {
45568
+ throw new SyntaxError("Unexpected end of input");
45569
+ }
45570
+ const protocol = header.slice(start, i3);
45571
+ if (protocols.has(protocol)) {
45572
+ throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
45573
+ }
45574
+ protocols.add(protocol);
45575
+ return protocols;
45576
+ }
45577
+ module.exports = { parse: parse6 };
45578
+ });
45579
+
45580
+ // node_modules/ws/lib/websocket-server.js
45581
+ var require_websocket_server2 = __commonJS((exports, module) => {
45582
+ var EventEmitter2 = __require("events");
45583
+ var http3 = __require("http");
45584
+ var { Duplex } = __require("stream");
45585
+ var { createHash } = __require("crypto");
45586
+ var extension = require_extension2();
45587
+ var PerMessageDeflate = require_permessage_deflate2();
45588
+ var subprotocol = require_subprotocol2();
45589
+ var WebSocket3 = require_websocket2();
45590
+ var { CLOSE_TIMEOUT, GUID, kWebSocket } = require_constants2();
45591
+ var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
45592
+ var RUNNING = 0;
45593
+ var CLOSING = 1;
45594
+ var CLOSED = 2;
45595
+
45596
+ class WebSocketServer2 extends EventEmitter2 {
45597
+ constructor(options, callback) {
45598
+ super();
45599
+ options = {
45600
+ allowSynchronousEvents: true,
45601
+ autoPong: true,
45602
+ maxPayload: 100 * 1024 * 1024,
45603
+ skipUTF8Validation: false,
45604
+ perMessageDeflate: false,
45605
+ handleProtocols: null,
45606
+ clientTracking: true,
45607
+ closeTimeout: CLOSE_TIMEOUT,
45608
+ verifyClient: null,
45609
+ noServer: false,
45610
+ backlog: null,
45611
+ server: null,
45612
+ host: null,
45613
+ path: null,
45614
+ port: null,
45615
+ WebSocket: WebSocket3,
45616
+ ...options
45617
+ };
45618
+ if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
45619
+ throw new TypeError('One and only one of the "port", "server", or "noServer" options ' + "must be specified");
45620
+ }
45621
+ if (options.port != null) {
45622
+ this._server = http3.createServer((req, res) => {
45623
+ const body = http3.STATUS_CODES[426];
45624
+ res.writeHead(426, {
45625
+ "Content-Length": body.length,
45626
+ "Content-Type": "text/plain"
45627
+ });
45628
+ res.end(body);
45629
+ });
45630
+ this._server.listen(options.port, options.host, options.backlog, callback);
45631
+ } else if (options.server) {
45632
+ this._server = options.server;
45633
+ }
45634
+ if (this._server) {
45635
+ const emitConnection = this.emit.bind(this, "connection");
45636
+ this._removeListeners = addListeners(this._server, {
45637
+ listening: this.emit.bind(this, "listening"),
45638
+ error: this.emit.bind(this, "error"),
45639
+ upgrade: (req, socket, head) => {
45640
+ this.handleUpgrade(req, socket, head, emitConnection);
45641
+ }
45642
+ });
45643
+ }
45644
+ if (options.perMessageDeflate === true)
45645
+ options.perMessageDeflate = {};
45646
+ if (options.clientTracking) {
45647
+ this.clients = new Set;
45648
+ this._shouldEmitClose = false;
45649
+ }
45650
+ this.options = options;
45651
+ this._state = RUNNING;
45652
+ }
45653
+ address() {
45654
+ if (this.options.noServer) {
45655
+ throw new Error('The server is operating in "noServer" mode');
45656
+ }
45657
+ if (!this._server)
45658
+ return null;
45659
+ return this._server.address();
45660
+ }
45661
+ close(cb) {
45662
+ if (this._state === CLOSED) {
45663
+ if (cb) {
45664
+ this.once("close", () => {
45665
+ cb(new Error("The server is not running"));
45666
+ });
45667
+ }
45668
+ process.nextTick(emitClose, this);
45669
+ return;
45670
+ }
45671
+ if (cb)
45672
+ this.once("close", cb);
45673
+ if (this._state === CLOSING)
45674
+ return;
45675
+ this._state = CLOSING;
45676
+ if (this.options.noServer || this.options.server) {
45677
+ if (this._server) {
45678
+ this._removeListeners();
45679
+ this._removeListeners = this._server = null;
45680
+ }
45681
+ if (this.clients) {
45682
+ if (!this.clients.size) {
45683
+ process.nextTick(emitClose, this);
45684
+ } else {
45685
+ this._shouldEmitClose = true;
45686
+ }
45687
+ } else {
45688
+ process.nextTick(emitClose, this);
45689
+ }
45690
+ } else {
45691
+ const server = this._server;
45692
+ this._removeListeners();
45693
+ this._removeListeners = this._server = null;
45694
+ server.close(() => {
45695
+ emitClose(this);
45696
+ });
45697
+ }
45698
+ }
45699
+ shouldHandle(req) {
45700
+ if (this.options.path) {
45701
+ const index2 = req.url.indexOf("?");
45702
+ const pathname = index2 !== -1 ? req.url.slice(0, index2) : req.url;
45703
+ if (pathname !== this.options.path)
45704
+ return false;
45705
+ }
45706
+ return true;
45707
+ }
45708
+ handleUpgrade(req, socket, head, cb) {
45709
+ socket.on("error", socketOnError);
45710
+ const key = req.headers["sec-websocket-key"];
45711
+ const upgrade = req.headers.upgrade;
45712
+ const version5 = +req.headers["sec-websocket-version"];
45713
+ if (req.method !== "GET") {
45714
+ const message = "Invalid HTTP method";
45715
+ abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
45716
+ return;
45717
+ }
45718
+ if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
45719
+ const message = "Invalid Upgrade header";
45720
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
45721
+ return;
45722
+ }
45723
+ if (key === undefined || !keyRegex.test(key)) {
45724
+ const message = "Missing or invalid Sec-WebSocket-Key header";
45725
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
45726
+ return;
45727
+ }
45728
+ if (version5 !== 13 && version5 !== 8) {
45729
+ const message = "Missing or invalid Sec-WebSocket-Version header";
45730
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
45731
+ "Sec-WebSocket-Version": "13, 8"
45732
+ });
45733
+ return;
45734
+ }
45735
+ if (!this.shouldHandle(req)) {
45736
+ abortHandshake(socket, 400);
45737
+ return;
45738
+ }
45739
+ const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
45740
+ let protocols = new Set;
45741
+ if (secWebSocketProtocol !== undefined) {
45742
+ try {
45743
+ protocols = subprotocol.parse(secWebSocketProtocol);
45744
+ } catch (err) {
45745
+ const message = "Invalid Sec-WebSocket-Protocol header";
45746
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
45747
+ return;
45748
+ }
45749
+ }
45750
+ const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
45751
+ const extensions = {};
45752
+ if (this.options.perMessageDeflate && secWebSocketExtensions !== undefined) {
45753
+ const perMessageDeflate = new PerMessageDeflate(this.options.perMessageDeflate, true, this.options.maxPayload);
45754
+ try {
45755
+ const offers = extension.parse(secWebSocketExtensions);
45756
+ if (offers[PerMessageDeflate.extensionName]) {
45757
+ perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
45758
+ extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
45759
+ }
45760
+ } catch (err) {
45761
+ const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
45762
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
45763
+ return;
45764
+ }
45765
+ }
45766
+ if (this.options.verifyClient) {
45767
+ const info = {
45768
+ origin: req.headers[`${version5 === 8 ? "sec-websocket-origin" : "origin"}`],
45769
+ secure: !!(req.socket.authorized || req.socket.encrypted),
45770
+ req
45771
+ };
45772
+ if (this.options.verifyClient.length === 2) {
45773
+ this.options.verifyClient(info, (verified, code, message, headers) => {
45774
+ if (!verified) {
45775
+ return abortHandshake(socket, code || 401, message, headers);
45776
+ }
45777
+ this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
45778
+ });
45779
+ return;
45780
+ }
45781
+ if (!this.options.verifyClient(info))
45782
+ return abortHandshake(socket, 401);
45783
+ }
45784
+ this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
45785
+ }
45786
+ completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
45787
+ if (!socket.readable || !socket.writable)
45788
+ return socket.destroy();
45789
+ if (socket[kWebSocket]) {
45790
+ throw new Error("server.handleUpgrade() was called more than once with the same " + "socket, possibly due to a misconfiguration");
45791
+ }
45792
+ if (this._state > RUNNING)
45793
+ return abortHandshake(socket, 503);
45794
+ const digest = createHash("sha1").update(key + GUID).digest("base64");
45795
+ const headers = [
45796
+ "HTTP/1.1 101 Switching Protocols",
45797
+ "Upgrade: websocket",
45798
+ "Connection: Upgrade",
45799
+ `Sec-WebSocket-Accept: ${digest}`
45800
+ ];
45801
+ const ws = new this.options.WebSocket(null, undefined, this.options);
45802
+ if (protocols.size) {
45803
+ const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
45804
+ if (protocol) {
45805
+ headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
45806
+ ws._protocol = protocol;
45807
+ }
45808
+ }
45809
+ if (extensions[PerMessageDeflate.extensionName]) {
45810
+ const params = extensions[PerMessageDeflate.extensionName].params;
45811
+ const value = extension.format({
45812
+ [PerMessageDeflate.extensionName]: [params]
45813
+ });
45814
+ headers.push(`Sec-WebSocket-Extensions: ${value}`);
45815
+ ws._extensions = extensions;
45816
+ }
45817
+ this.emit("headers", headers, req);
45818
+ socket.write(headers.concat(`\r
45819
+ `).join(`\r
45820
+ `));
45821
+ socket.removeListener("error", socketOnError);
45822
+ ws.setSocket(socket, head, {
45823
+ allowSynchronousEvents: this.options.allowSynchronousEvents,
45824
+ maxPayload: this.options.maxPayload,
45825
+ skipUTF8Validation: this.options.skipUTF8Validation
45826
+ });
45827
+ if (this.clients) {
45828
+ this.clients.add(ws);
45829
+ ws.on("close", () => {
45830
+ this.clients.delete(ws);
45831
+ if (this._shouldEmitClose && !this.clients.size) {
45832
+ process.nextTick(emitClose, this);
45833
+ }
45834
+ });
45835
+ }
45836
+ cb(ws, req);
45837
+ }
45838
+ }
45839
+ module.exports = WebSocketServer2;
45840
+ function addListeners(server, map2) {
45841
+ for (const event of Object.keys(map2))
45842
+ server.on(event, map2[event]);
45843
+ return function removeListeners() {
45844
+ for (const event of Object.keys(map2)) {
45845
+ server.removeListener(event, map2[event]);
45846
+ }
45847
+ };
45848
+ }
45849
+ function emitClose(server) {
45850
+ server._state = CLOSED;
45851
+ server.emit("close");
45852
+ }
45853
+ function socketOnError() {
45854
+ this.destroy();
45855
+ }
45856
+ function abortHandshake(socket, code, message, headers) {
45857
+ message = message || http3.STATUS_CODES[code];
45858
+ headers = {
45859
+ Connection: "close",
45860
+ "Content-Type": "text/html",
45861
+ "Content-Length": Buffer.byteLength(message),
45862
+ ...headers
45863
+ };
45864
+ socket.once("finish", socket.destroy);
45865
+ socket.end(`HTTP/1.1 ${code} ${http3.STATUS_CODES[code]}\r
45866
+ ` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join(`\r
45867
+ `) + `\r
45868
+ \r
45869
+ ` + message);
45870
+ }
45871
+ function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
45872
+ if (server.listenerCount("wsClientError")) {
45873
+ const err = new Error(message);
45874
+ Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
45875
+ server.emit("wsClientError", err, socket, req);
45876
+ } else {
45877
+ abortHandshake(socket, code, message, headers);
45878
+ }
45879
+ }
45880
+ });
45881
+
43038
45882
  // ../../node_modules/@supabase/functions-js/node_modules/tslib/tslib.js
43039
45883
  var require_tslib = __commonJS((exports, module) => {
43040
45884
  var __extends;
@@ -44547,7 +47391,7 @@ var require_version = __commonJS((exports) => {
44547
47391
  });
44548
47392
 
44549
47393
  // ../../node_modules/@supabase/realtime-js/dist/main/lib/constants.js
44550
- var require_constants2 = __commonJS((exports) => {
47394
+ var require_constants3 = __commonJS((exports) => {
44551
47395
  Object.defineProperty(exports, "__esModule", { value: true });
44552
47396
  exports.CONNECTION_STATE = exports.TRANSPORTS = exports.CHANNEL_EVENTS = exports.CHANNEL_STATES = exports.SOCKET_STATES = exports.MAX_PUSH_BUFFER_SIZE = exports.WS_CLOSE_NORMAL = exports.DEFAULT_TIMEOUT = exports.VERSION = exports.DEFAULT_VSN = exports.VSN_2_0_0 = exports.VSN_1_0_0 = exports.DEFAULT_VERSION = undefined;
44553
47397
  var version_1 = require_version();
@@ -44942,7 +47786,7 @@ var require_transformers = __commonJS((exports) => {
44942
47786
  // ../../node_modules/@supabase/realtime-js/dist/main/lib/push.js
44943
47787
  var require_push = __commonJS((exports) => {
44944
47788
  Object.defineProperty(exports, "__esModule", { value: true });
44945
- var constants_1 = require_constants2();
47789
+ var constants_1 = require_constants3();
44946
47790
 
44947
47791
  class Push {
44948
47792
  constructor(channel, event, payload = {}, timeout = constants_1.DEFAULT_TIMEOUT) {
@@ -45210,7 +48054,7 @@ var require_RealtimeChannel = __commonJS((exports) => {
45210
48054
  Object.defineProperty(exports, "__esModule", { value: true });
45211
48055
  exports.REALTIME_CHANNEL_STATES = exports.REALTIME_SUBSCRIBE_STATES = exports.REALTIME_LISTEN_TYPES = exports.REALTIME_POSTGRES_CHANGES_LISTEN_EVENT = undefined;
45212
48056
  var tslib_1 = require_tslib2();
45213
- var constants_1 = require_constants2();
48057
+ var constants_1 = require_constants3();
45214
48058
  var push_1 = tslib_1.__importDefault(require_push());
45215
48059
  var timer_1 = tslib_1.__importDefault(require_timer());
45216
48060
  var RealtimePresence_1 = tslib_1.__importDefault(require_RealtimePresence());
@@ -45706,7 +48550,7 @@ var require_RealtimeClient = __commonJS((exports) => {
45706
48550
  Object.defineProperty(exports, "__esModule", { value: true });
45707
48551
  var tslib_1 = require_tslib2();
45708
48552
  var websocket_factory_1 = tslib_1.__importDefault(require_websocket_factory());
45709
- var constants_1 = require_constants2();
48553
+ var constants_1 = require_constants3();
45710
48554
  var serializer_1 = tslib_1.__importDefault(require_serializer());
45711
48555
  var timer_1 = tslib_1.__importDefault(require_timer());
45712
48556
  var transformers_1 = require_transformers();
@@ -46915,7 +49759,7 @@ var require_version2 = __commonJS((exports) => {
46915
49759
  });
46916
49760
 
46917
49761
  // ../../node_modules/@supabase/auth-js/dist/main/lib/constants.js
46918
- var require_constants3 = __commonJS((exports) => {
49762
+ var require_constants4 = __commonJS((exports) => {
46919
49763
  Object.defineProperty(exports, "__esModule", { value: true });
46920
49764
  exports.JWKS_TTL = exports.BASE64URL_REGEX = exports.API_VERSIONS = exports.API_VERSION_HEADER_NAME = exports.NETWORK_FAILURE = exports.DEFAULT_HEADERS = exports.AUDIENCE = exports.STORAGE_KEY = exports.GOTRUE_URL = exports.EXPIRY_MARGIN_MS = exports.AUTO_REFRESH_TICK_THRESHOLD = exports.AUTO_REFRESH_TICK_DURATION_MS = undefined;
46921
49765
  var version_1 = require_version2();
@@ -47306,7 +50150,7 @@ var require_helpers = __commonJS((exports) => {
47306
50150
  exports.userNotAvailableProxy = userNotAvailableProxy;
47307
50151
  exports.insecureUserWarningProxy = insecureUserWarningProxy;
47308
50152
  exports.deepClone = deepClone;
47309
- var constants_1 = require_constants3();
50153
+ var constants_1 = require_constants4();
47310
50154
  var errors_1 = require_errors3();
47311
50155
  var base64url_1 = require_base64url();
47312
50156
  function expiresAt(expiresIn) {
@@ -47607,7 +50451,7 @@ var require_fetch = __commonJS((exports) => {
47607
50451
  exports._generateLinkResponse = _generateLinkResponse;
47608
50452
  exports._noResolveJsonResponse = _noResolveJsonResponse;
47609
50453
  var tslib_1 = require_tslib3();
47610
- var constants_1 = require_constants3();
50454
+ var constants_1 = require_constants4();
47611
50455
  var helpers_1 = require_helpers();
47612
50456
  var errors_1 = require_errors3();
47613
50457
  var _getErrorMessage2 = (err) => err.msg || err.message || err.error_description || err.error || JSON.stringify(err);
@@ -48940,7 +51784,7 @@ var require_GoTrueClient = __commonJS((exports) => {
48940
51784
  Object.defineProperty(exports, "__esModule", { value: true });
48941
51785
  var tslib_1 = require_tslib3();
48942
51786
  var GoTrueAdminApi_1 = tslib_1.__importDefault(require_GoTrueAdminApi());
48943
- var constants_1 = require_constants3();
51787
+ var constants_1 = require_constants4();
48944
51788
  var errors_1 = require_errors3();
48945
51789
  var fetch_1 = require_fetch();
48946
51790
  var helpers_1 = require_helpers();
@@ -70265,9 +73109,154 @@ class WebSocketClientTransport {
70265
73109
  }
70266
73110
  }
70267
73111
 
73112
+ // src/auth/oauth-token-manager.ts
73113
+ class OAuthTokenManager {
73114
+ tokenCache = new Map;
73115
+ refreshPromises = new Map;
73116
+ logger;
73117
+ constructor(logger = console) {
73118
+ this.logger = logger;
73119
+ }
73120
+ getCacheKey(config2) {
73121
+ return `${config2.tokenUrl}:${config2.clientId}`;
73122
+ }
73123
+ isTokenValid(cached2, refreshBuffer) {
73124
+ const bufferMs = refreshBuffer * 1000;
73125
+ return Date.now() < cached2.expiresAt - bufferMs;
73126
+ }
73127
+ async fetchToken(config2) {
73128
+ this.logger.info(`[OAuth] Fetching new token from ${config2.tokenUrl}`);
73129
+ const body = {
73130
+ client_id: config2.clientId,
73131
+ client_secret: config2.clientSecret,
73132
+ grant_type: config2.grantType
73133
+ };
73134
+ if (config2.scope) {
73135
+ body.scope = config2.scope;
73136
+ }
73137
+ const response = await fetch(config2.tokenUrl, {
73138
+ method: "POST",
73139
+ headers: {
73140
+ "Content-Type": "application/json"
73141
+ },
73142
+ body: JSON.stringify(body)
73143
+ });
73144
+ if (!response.ok) {
73145
+ const errorText = await response.text();
73146
+ throw new Error(`OAuth token request failed: ${response.status} ${errorText}`);
73147
+ }
73148
+ const data = await response.json();
73149
+ if (!data.access_token) {
73150
+ throw new Error("OAuth response missing access_token");
73151
+ }
73152
+ const expiresIn = data.expires_in || 3600;
73153
+ const expiresAt = Date.now() + expiresIn * 1000;
73154
+ this.logger.info(`[OAuth] Token obtained, expires in ${expiresIn}s`);
73155
+ return {
73156
+ accessToken: data.access_token,
73157
+ expiresAt
73158
+ };
73159
+ }
73160
+ async getToken(config2) {
73161
+ const cacheKey = this.getCacheKey(config2);
73162
+ const refreshBuffer = config2.refreshBuffer ?? 300;
73163
+ const cached2 = this.tokenCache.get(cacheKey);
73164
+ if (cached2 && this.isTokenValid(cached2, refreshBuffer)) {
73165
+ return cached2.accessToken;
73166
+ }
73167
+ const existingPromise = this.refreshPromises.get(cacheKey);
73168
+ if (existingPromise) {
73169
+ return existingPromise;
73170
+ }
73171
+ const fetchPromise = this.fetchToken(config2).then((token) => {
73172
+ this.tokenCache.set(cacheKey, token);
73173
+ this.refreshPromises.delete(cacheKey);
73174
+ return token.accessToken;
73175
+ }).catch((error2) => {
73176
+ this.refreshPromises.delete(cacheKey);
73177
+ throw error2;
73178
+ });
73179
+ this.refreshPromises.set(cacheKey, fetchPromise);
73180
+ return fetchPromise;
73181
+ }
73182
+ async getAuthHeaders(config2) {
73183
+ const token = await this.getToken(config2);
73184
+ return {
73185
+ Authorization: `Bearer ${token}`
73186
+ };
73187
+ }
73188
+ clearToken(config2) {
73189
+ const cacheKey = this.getCacheKey(config2);
73190
+ this.tokenCache.delete(cacheKey);
73191
+ this.logger.info(`[OAuth] Token cache cleared for ${config2.clientId}`);
73192
+ }
73193
+ clearAll() {
73194
+ this.tokenCache.clear();
73195
+ this.refreshPromises.clear();
73196
+ this.logger.info("[OAuth] All token caches cleared");
73197
+ }
73198
+ }
73199
+ var oauthTokenManager = new OAuthTokenManager;
73200
+
70268
73201
  // src/transports/factory.ts
70269
73202
  class TransportFactory {
70270
- static create(config2) {
73203
+ static async create(config2) {
73204
+ const transportConfig = this.normalizeTransportConfig(config2);
73205
+ switch (transportConfig.type) {
73206
+ case "stdio": {
73207
+ const stdioParams = {
73208
+ command: transportConfig.command,
73209
+ args: transportConfig.args || [],
73210
+ env: {
73211
+ ...process.env,
73212
+ ...transportConfig.env || {}
73213
+ },
73214
+ ...transportConfig.cwd ? { cwd: transportConfig.cwd } : {}
73215
+ };
73216
+ return new StdioClientTransport(stdioParams);
73217
+ }
73218
+ case "http": {
73219
+ const headers = {
73220
+ ...transportConfig.headers || {}
73221
+ };
73222
+ if (transportConfig.apiKey) {
73223
+ headers["Authorization"] = `Bearer ${transportConfig.apiKey}`;
73224
+ }
73225
+ if (transportConfig.oauth) {
73226
+ const authHeaders = await oauthTokenManager.getAuthHeaders(transportConfig.oauth);
73227
+ Object.assign(headers, authHeaders);
73228
+ }
73229
+ const requestInit = {};
73230
+ if (Object.keys(headers).length > 0) {
73231
+ requestInit.headers = headers;
73232
+ }
73233
+ const transport = new StreamableHTTPClientTransport(new URL(transportConfig.url), { requestInit });
73234
+ if (transportConfig.oauth) {
73235
+ transport._oauthConfig = transportConfig.oauth;
73236
+ transport._baseHeaders = transportConfig.headers;
73237
+ }
73238
+ return transport;
73239
+ }
73240
+ case "sse": {
73241
+ const headers = {
73242
+ ...transportConfig.headers || {}
73243
+ };
73244
+ if (transportConfig.apiKey) {
73245
+ headers["Authorization"] = `Bearer ${transportConfig.apiKey}`;
73246
+ }
73247
+ const requestInit = {};
73248
+ if (Object.keys(headers).length > 0) {
73249
+ requestInit.headers = headers;
73250
+ }
73251
+ return new SSEClientTransport(new URL(transportConfig.sseUrl), { requestInit });
73252
+ }
73253
+ case "websocket":
73254
+ return new WebSocketClientTransport(new URL(transportConfig.url));
73255
+ default:
73256
+ throw new Error(`Unsupported transport type: ${transportConfig.type}`);
73257
+ }
73258
+ }
73259
+ static createSync(config2) {
70271
73260
  const transportConfig = this.normalizeTransportConfig(config2);
70272
73261
  switch (transportConfig.type) {
70273
73262
  case "stdio": {
@@ -70282,10 +73271,35 @@ class TransportFactory {
70282
73271
  };
70283
73272
  return new StdioClientTransport(stdioParams);
70284
73273
  }
70285
- case "http":
70286
- return new StreamableHTTPClientTransport(new URL(transportConfig.url));
70287
- case "sse":
70288
- return new SSEClientTransport(new URL(transportConfig.sseUrl));
73274
+ case "http": {
73275
+ const headers = {
73276
+ ...transportConfig.headers || {}
73277
+ };
73278
+ if (transportConfig.apiKey) {
73279
+ headers["Authorization"] = `Bearer ${transportConfig.apiKey}`;
73280
+ }
73281
+ if (transportConfig.oauth) {
73282
+ console.warn("[TransportFactory] OAuth requires async create(), token will not be included");
73283
+ }
73284
+ const requestInit = {};
73285
+ if (Object.keys(headers).length > 0) {
73286
+ requestInit.headers = headers;
73287
+ }
73288
+ return new StreamableHTTPClientTransport(new URL(transportConfig.url), { requestInit });
73289
+ }
73290
+ case "sse": {
73291
+ const headers = {
73292
+ ...transportConfig.headers || {}
73293
+ };
73294
+ if (transportConfig.apiKey) {
73295
+ headers["Authorization"] = `Bearer ${transportConfig.apiKey}`;
73296
+ }
73297
+ const requestInit = {};
73298
+ if (Object.keys(headers).length > 0) {
73299
+ requestInit.headers = headers;
73300
+ }
73301
+ return new SSEClientTransport(new URL(transportConfig.sseUrl), { requestInit });
73302
+ }
70289
73303
  case "websocket":
70290
73304
  return new WebSocketClientTransport(new URL(transportConfig.url));
70291
73305
  default:
@@ -70394,7 +73408,7 @@ class ServerManager {
70394
73408
  if (configErrors.length > 0) {
70395
73409
  throw new Error(`Configuration validation failed: ${configErrors.join(", ")}`);
70396
73410
  }
70397
- const transport = TransportFactory.create(config2);
73411
+ const transport = await TransportFactory.create(config2);
70398
73412
  const transportName = TransportFactory.getTransportDisplayName(config2);
70399
73413
  this.logger.debug(`Creating ${transportName} transport for server: ${config2.name}`);
70400
73414
  const client = new Client({
@@ -70542,6 +73556,7 @@ class GatewayRegistry {
70542
73556
  try {
70543
73557
  const response = await connection.client.listTools();
70544
73558
  const namespace = connection.config.namespace;
73559
+ const role = connection.config.role || "all";
70545
73560
  for (const tool of response.tools || []) {
70546
73561
  const aggregatedTool = {
70547
73562
  name: this.getNameWithNamespace(tool.name, namespace),
@@ -70549,7 +73564,8 @@ class GatewayRegistry {
70549
73564
  serverId,
70550
73565
  namespace,
70551
73566
  description: tool.description,
70552
- inputSchema: tool.inputSchema
73567
+ inputSchema: tool.inputSchema,
73568
+ role
70553
73569
  };
70554
73570
  if (this.config.settings?.enableToolConflictResolution && this.tools.has(aggregatedTool.name)) {
70555
73571
  const conflictedName = `${aggregatedTool.name}_${serverId}`;
@@ -70558,7 +73574,7 @@ class GatewayRegistry {
70558
73574
  }
70559
73575
  this.tools.set(aggregatedTool.name, aggregatedTool);
70560
73576
  }
70561
- this.logger.debug(`Collected ${response.tools?.length || 0} tools from server ${serverId}`);
73577
+ this.logger.info(`Collected ${response.tools?.length || 0} tools from server ${serverId}`);
70562
73578
  } catch (error2) {
70563
73579
  this.logger.error(`Failed to collect tools from server ${serverId}: ${error2}`);
70564
73580
  }
@@ -70624,7 +73640,7 @@ class GatewayRegistry {
70624
73640
  if (!namespace) {
70625
73641
  return name;
70626
73642
  }
70627
- return `${namespace}:${name}`;
73643
+ return `${namespace}_${name}`;
70628
73644
  }
70629
73645
  getUriWithNamespace(uri, namespace) {
70630
73646
  if (!namespace) {
@@ -70635,12 +73651,15 @@ class GatewayRegistry {
70635
73651
  url2.searchParams.set("namespace", namespace);
70636
73652
  return url2.toString();
70637
73653
  } catch {
70638
- return `${namespace}:${uri}`;
73654
+ return `${namespace}_${uri}`;
70639
73655
  }
70640
73656
  }
70641
73657
  getTools() {
70642
73658
  return Array.from(this.tools.values());
70643
73659
  }
73660
+ getToolsForRole(role) {
73661
+ return Array.from(this.tools.values()).filter((tool) => tool.role === "all" || tool.role === role || !tool.role);
73662
+ }
70644
73663
  getResources() {
70645
73664
  return Array.from(this.resources.values());
70646
73665
  }
@@ -99417,8 +102436,17 @@ var Relay = class extends AbstractRelay {
99417
102436
 
99418
102437
  // src/nostr/client.ts
99419
102438
  init_esm2();
99420
- init_wrapper();
99421
- useWebSocketImplementation(wrapper_default);
102439
+
102440
+ // node_modules/ws/wrapper.mjs
102441
+ var import_stream7 = __toESM(require_stream2(), 1);
102442
+ var import_receiver2 = __toESM(require_receiver2(), 1);
102443
+ var import_sender2 = __toESM(require_sender2(), 1);
102444
+ var import_websocket3 = __toESM(require_websocket2(), 1);
102445
+ var import_websocket_server2 = __toESM(require_websocket_server2(), 1);
102446
+ var wrapper_default2 = import_websocket3.default;
102447
+
102448
+ // src/nostr/client.ts
102449
+ useWebSocketImplementation(wrapper_default2);
99422
102450
 
99423
102451
  class NostrClient {
99424
102452
  config;
@@ -99477,7 +102505,9 @@ class NostrClient {
99477
102505
  product: {
99478
102506
  name: content.product || content.name,
99479
102507
  price: content.price,
99480
- description: content.description
102508
+ currency: content.currency || "USD",
102509
+ description: content.description,
102510
+ checkoutUrl: content.checkoutUrl
99481
102511
  },
99482
102512
  paymentAddress: paymentAddressTag?.[1],
99483
102513
  sellerAgentId: sellerAgentIdTag?.[1],
@@ -102677,24 +105707,45 @@ var createIdentityTool = {
102677
105707
  };
102678
105708
  var getIdentityTool = {
102679
105709
  name: "get_identity",
102680
- description: "Check your current shopping identity and wallet status.",
105710
+ description: "Check your current shopping identity and wallet status. Include backupCode if you have one.",
102681
105711
  inputSchema: {
102682
105712
  type: "object",
102683
- properties: {},
105713
+ properties: {
105714
+ backupCode: {
105715
+ type: "string",
105716
+ description: "Your NextNext backup code (enc1.xxx). Include this if you have one."
105717
+ }
105718
+ },
102684
105719
  required: []
102685
105720
  },
102686
- handler: async () => {
102687
- const identity = getCurrentIdentity();
105721
+ handler: async (args) => {
102688
105722
  const sessionId = getCurrentSessionId();
105723
+ const isStdioMode = sessionId === "stdio-local-session";
105724
+ let identity = null;
105725
+ if (isStdioMode) {
105726
+ identity = getCurrentIdentity();
105727
+ }
105728
+ if (!identity && args.backupCode) {
105729
+ try {
105730
+ await authenticateSession(args.backupCode);
105731
+ identity = getCurrentIdentity();
105732
+ console.error(`[get_identity] Authenticated with backup code`);
105733
+ } catch (error2) {
105734
+ console.error(`[get_identity] Auth failed: ${error2.message}`);
105735
+ }
105736
+ }
102689
105737
  if (!identity) {
102690
105738
  return {
102691
105739
  content: [{
102692
105740
  type: "text",
102693
105741
  text: JSON.stringify({
102694
105742
  status: "no_identity",
102695
- sessionId: sessionId?.slice(0, 8) || "unknown",
102696
- message: "No identity linked to this conversation yet.",
102697
- nextStep: "Call create_identity() if new, or authenticate() with your saved key if returning."
105743
+ message: "No identity found. Include your backupCode if you have one.",
105744
+ solutions: [
105745
+ "1. If returning user: Include backupCode parameter",
105746
+ "2. If new user: Call create_identity() to get a backupCode"
105747
+ ],
105748
+ example: 'get_identity(backupCode: "enc1.xxx...")'
102698
105749
  }, null, 2)
102699
105750
  }]
102700
105751
  };
@@ -102729,40 +105780,68 @@ var identityTools = [
102729
105780
  ];
102730
105781
 
102731
105782
  // src/tools/wallet-tools.ts
105783
+ init_context();
105784
+ function isStdioMode() {
105785
+ return getCurrentSessionId() === "stdio-local-session";
105786
+ }
105787
+ async function getIdentityForTool(backupCode) {
105788
+ if (isStdioMode()) {
105789
+ const sessionIdentity = getCurrentIdentity();
105790
+ if (sessionIdentity) {
105791
+ return sessionIdentity;
105792
+ }
105793
+ }
105794
+ if (backupCode) {
105795
+ try {
105796
+ await authenticateSession(backupCode);
105797
+ const identity = getCurrentIdentity();
105798
+ if (identity) {
105799
+ console.error(`[wallet-tools] Authenticated with backup code`);
105800
+ return identity;
105801
+ }
105802
+ } catch (error2) {
105803
+ console.error(`[wallet-tools] Auth failed: ${error2.message}`);
105804
+ }
105805
+ }
105806
+ return null;
105807
+ }
102732
105808
  function identityRequiredResponse(toolName) {
102733
- console.error(`[${toolName}] No identity found - session may have expired or instance restarted`);
105809
+ console.error(`[${toolName}] No identity - backupCode required`);
102734
105810
  return {
102735
105811
  content: [{
102736
105812
  type: "text",
102737
105813
  text: JSON.stringify({
102738
105814
  status: "identity_required",
102739
- error: "Session expired or no identity linked. This happens when the server restarts.",
102740
- whatHappened: "Your session was lost due to server restart/scaling. Your identity data is safe but needs to be re-linked.",
105815
+ error: "No identity provided. This tool requires your backupCode.",
102741
105816
  solutions: [
102742
- '1. Call authenticate(backupCode: "enc1.xxx...") with your backup code',
102743
- "2. If new user: Call create_identity() first"
105817
+ "1. If returning user: Include backupCode parameter in this tool call",
105818
+ "2. If new user: Call create_identity() first to get a backupCode"
102744
105819
  ],
102745
- hint: "The backup code was given to you when you first created your identity.",
102746
- technicalNote: "For ChatGPT/Claude: Store the backupCode and use it to re-authenticate after errors."
105820
+ example: `${toolName}(..., backupCode: "enc1.xxx...")`,
105821
+ hint: "The backupCode was given to you when you first created your identity. Include it in every tool call."
102747
105822
  }, null, 2)
102748
105823
  }]
102749
105824
  };
102750
105825
  }
102751
105826
  var createBurnerWalletTool = {
102752
105827
  name: "create_burner_wallet",
102753
- description: "Create a new burner wallet for making payments. The wallet needs to be topped up with USDC before use.",
105828
+ description: "Create a new burner wallet for making payments. The wallet needs to be topped up with USDC before use. Include backupCode if you have one.",
102754
105829
  inputSchema: {
102755
105830
  type: "object",
102756
105831
  properties: {
102757
105832
  label: {
102758
105833
  type: "string",
102759
105834
  description: 'Optional label for the wallet (e.g., "shopping", "test")'
105835
+ },
105836
+ backupCode: {
105837
+ type: "string",
105838
+ description: "Your NextNext backup code (enc1.xxx). Include this if you have one. Get one from create_identity if new."
102760
105839
  }
102761
105840
  },
102762
105841
  required: []
102763
105842
  },
102764
105843
  handler: async (args) => {
102765
- const identity = getCurrentIdentity();
105844
+ const identity = await getIdentityForTool(args.backupCode);
102766
105845
  if (!identity) {
102767
105846
  return identityRequiredResponse("create_burner_wallet");
102768
105847
  }
@@ -102791,13 +105870,17 @@ var createBurnerWalletTool = {
102791
105870
  };
102792
105871
  var getBurnerBalanceTool = {
102793
105872
  name: "get_burner_balance",
102794
- description: "Check the USDC and ETH balance of your wallet.",
105873
+ description: "Check the USDC and ETH balance of your wallet. Include backupCode if you have one.",
102795
105874
  inputSchema: {
102796
105875
  type: "object",
102797
105876
  properties: {
102798
105877
  walletId: {
102799
105878
  type: "string",
102800
105879
  description: "Optional: specific wallet ID. If not provided, uses your default identity-linked wallet."
105880
+ },
105881
+ backupCode: {
105882
+ type: "string",
105883
+ description: "Your NextNext backup code (enc1.xxx). Include this if you have one. Get one from create_identity if new."
102801
105884
  }
102802
105885
  },
102803
105886
  required: []
@@ -102805,7 +105888,7 @@ var getBurnerBalanceTool = {
102805
105888
  handler: async (args) => {
102806
105889
  try {
102807
105890
  let wallet;
102808
- const identity = getCurrentIdentity();
105891
+ const identity = await getIdentityForTool(args.backupCode);
102809
105892
  if (args.walletId) {
102810
105893
  wallet = await getBurnerWallet(args.walletId);
102811
105894
  if (!wallet) {
@@ -102857,14 +105940,19 @@ var getBurnerBalanceTool = {
102857
105940
  };
102858
105941
  var listBurnerWalletsTool = {
102859
105942
  name: "list_burner_wallets",
102860
- description: "List all burner wallets linked to your identity.",
105943
+ description: "List all burner wallets linked to your identity. Include backupCode if you have one.",
102861
105944
  inputSchema: {
102862
105945
  type: "object",
102863
- properties: {},
105946
+ properties: {
105947
+ backupCode: {
105948
+ type: "string",
105949
+ description: "Your NextNext backup code (enc1.xxx). Include this if you have one. Get one from create_identity if new."
105950
+ }
105951
+ },
102864
105952
  required: []
102865
105953
  },
102866
- handler: async () => {
102867
- const identity = getCurrentIdentity();
105954
+ handler: async (args) => {
105955
+ const identity = await getIdentityWithFallback(args.backupCode);
102868
105956
  if (!identity) {
102869
105957
  return identityRequiredResponse("list_burner_wallets");
102870
105958
  }
@@ -102889,7 +105977,7 @@ var listBurnerWalletsTool = {
102889
105977
  };
102890
105978
  var paySellerTool = {
102891
105979
  name: "pay_seller",
102892
- description: "Pay a seller using gasless USDC transfer (x402/EIP-3009). Creates a signed authorization that the seller can submit on-chain. The buyer pays no gas.",
105980
+ description: "Pay a seller using gasless USDC transfer (x402/EIP-3009). Creates a signed authorization that the seller can submit on-chain. The buyer pays no gas. Include backupCode if you have one.",
102893
105981
  inputSchema: {
102894
105982
  type: "object",
102895
105983
  properties: {
@@ -102908,13 +105996,17 @@ var paySellerTool = {
102908
105996
  proposalId: {
102909
105997
  type: "string",
102910
105998
  description: "Optional: The proposal ID being paid for (for tracking)"
105999
+ },
106000
+ backupCode: {
106001
+ type: "string",
106002
+ description: "Your NextNext backup code (enc1.xxx). Include this if you have one. Get one from create_identity if new."
102911
106003
  }
102912
106004
  },
102913
106005
  required: ["recipientAddress", "amount"]
102914
106006
  },
102915
106007
  handler: async (args) => {
102916
106008
  try {
102917
- const identity = getCurrentIdentity();
106009
+ const identity = await getIdentityForTool(args.backupCode);
102918
106010
  if (!identity && !args.walletId) {
102919
106011
  return identityRequiredResponse("pay_seller");
102920
106012
  }
@@ -102999,7 +106091,7 @@ function getNostrClient() {
102999
106091
  }
103000
106092
  var postIntentTool = {
103001
106093
  name: "post_intent",
103002
- description: "Post a shopping intent to find products from sellers. Broadcasts your request to the seller network. Pass your backupCode if session identity is lost.",
106094
+ description: "Post a shopping intent to find products from sellers. Broadcasts your request to the seller network. Include backupCode if you have one.",
103003
106095
  inputSchema: {
103004
106096
  type: "object",
103005
106097
  properties: {
@@ -103018,40 +106110,42 @@ var postIntentTool = {
103018
106110
  },
103019
106111
  backupCode: {
103020
106112
  type: "string",
103021
- description: "Optional: Your NextNext backup code (enc1.xxx) for auto-authentication if session expired. This is NOT a password - safe to share."
106113
+ description: "Your NextNext backup code (enc1.xxx). Include this if you have one. Get one from create_identity if new."
103022
106114
  }
103023
106115
  },
103024
106116
  required: ["description", "budget"]
103025
106117
  },
103026
106118
  handler: async (args) => {
103027
- let identity = getCurrentIdentity();
106119
+ const { getCurrentSessionId: getCurrentSessionId2, authenticateSession: authenticateSession2 } = await Promise.resolve().then(() => (init_context(), exports_context));
106120
+ const isStdioMode2 = getCurrentSessionId2() === "stdio-local-session";
106121
+ let identity = null;
106122
+ if (isStdioMode2) {
106123
+ identity = getCurrentIdentity();
106124
+ }
103028
106125
  const backupCode = args.backupCode || args.encryptedKey;
103029
106126
  if (!identity && backupCode) {
103030
106127
  try {
103031
- const { authenticateSession: authenticateSession2 } = await Promise.resolve().then(() => (init_context(), exports_context));
103032
106128
  await authenticateSession2(backupCode);
103033
106129
  identity = getCurrentIdentity();
103034
- console.error(`[post_intent] Auto-authenticated with provided backup code`);
106130
+ console.error(`[post_intent] Authenticated with backup code`);
103035
106131
  } catch (error2) {
103036
- console.error(`[post_intent] Auto-auth failed: ${error2.message}`);
106132
+ console.error(`[post_intent] Auth failed: ${error2.message}`);
103037
106133
  }
103038
106134
  }
103039
106135
  if (!identity) {
103040
- console.error(`[post_intent] No identity found - session may have expired or instance restarted`);
106136
+ console.error(`[post_intent] No identity found`);
103041
106137
  return {
103042
106138
  content: [{
103043
106139
  type: "text",
103044
106140
  text: JSON.stringify({
103045
106141
  status: "identity_required",
103046
- error: "Session expired or no identity linked. This happens when the server restarts.",
103047
- whatHappened: "Your session was lost due to server restart/scaling. Your identity data is safe but needs to be re-linked.",
106142
+ error: "No identity found. Include your backupCode parameter.",
103048
106143
  solutions: [
103049
- '1. If you have your backup code (enc1.xxx): Call authenticate(backupCode: "enc1.xxx...")',
103050
- '2. Or pass backupCode directly to this tool: post_intent(..., backupCode: "enc1.xxx...")',
103051
- "3. If new user: Call create_identity() first"
106144
+ "1. If returning user: Include backupCode parameter in this tool call",
106145
+ "2. If new user: Call create_identity() first to get a backupCode"
103052
106146
  ],
103053
- hint: "The backup code was given to you when you first created your identity. Check your conversation history.",
103054
- technicalNote: "For ChatGPT/Claude: Always include backupCode parameter in tool calls if you have it stored."
106147
+ example: 'post_intent(description: "...", budget: 100, backupCode: "enc1.xxx...")',
106148
+ hint: "The backupCode was given to you when you first created your identity."
103055
106149
  }, null, 2)
103056
106150
  }]
103057
106151
  };
@@ -103163,8 +106257,11 @@ var getProposalsTool = {
103163
106257
  const formattedProposals = proposals.map((p, i3) => ({
103164
106258
  index: i3 + 1,
103165
106259
  product: p.product.name,
103166
- price: `$${p.product.price}`,
106260
+ price: p.product.price,
106261
+ currency: p.product.currency || "USD",
106262
+ priceDisplay: `${p.product.currency || "USD"} ${p.product.price}`,
103167
106263
  description: p.product.description,
106264
+ checkoutUrl: p.product.checkoutUrl,
103168
106265
  sellerPubkey: p.sellerPubkey.slice(0, 16) + "...",
103169
106266
  paymentAddress: p.paymentAddress,
103170
106267
  proposalId: p.id
@@ -103207,6 +106304,7 @@ class GatewayServer {
103207
106304
  logger;
103208
106305
  healthCheckInterval;
103209
106306
  sessionId;
106307
+ role = "buyer";
103210
106308
  constructor(config3, logger = console) {
103211
106309
  this.config = config3;
103212
106310
  this.logger = logger;
@@ -103241,24 +106339,34 @@ class GatewayServer {
103241
106339
  }
103242
106340
  setupHandlers() {
103243
106341
  this.server.setRequestHandler(ListToolsRequestSchema, async () => {
103244
- const proxiedTools = this.registry.getTools().map((tool) => ({
106342
+ const proxiedTools = this.registry.getToolsForRole(this.role).map((tool) => ({
103245
106343
  name: tool.name,
103246
106344
  description: tool.description || `Tool from ${tool.serverId}${tool.namespace ? ` (${tool.namespace})` : ""}`,
103247
106345
  inputSchema: tool.inputSchema
103248
106346
  }));
103249
- const nativeToolsList = nativeTools.map((tool) => ({
103250
- name: tool.name,
103251
- description: tool.description,
103252
- inputSchema: tool.inputSchema
103253
- }));
103254
- const allTools = [...nativeToolsList, ...proxiedTools];
103255
- this.logger.info(`[Gateway] tools/list returning ${allTools.length} tools (${nativeToolsList.length} native, ${proxiedTools.length} proxied)`);
106347
+ let allTools;
106348
+ if (this.role === "buyer") {
106349
+ const nativeToolsList = nativeTools.map((tool) => ({
106350
+ name: tool.name,
106351
+ description: tool.description,
106352
+ inputSchema: tool.inputSchema
106353
+ }));
106354
+ allTools = [...nativeToolsList, ...proxiedTools];
106355
+ this.logger.info(`[Gateway] tools/list (role=${this.role}) returning ${allTools.length} tools (${nativeToolsList.length} native, ${proxiedTools.length} proxied)`);
106356
+ } else {
106357
+ allTools = proxiedTools;
106358
+ this.logger.info(`[Gateway] tools/list (role=${this.role}) returning ${allTools.length} proxied tools`);
106359
+ }
103256
106360
  return { tools: allTools };
103257
106361
  });
103258
106362
  this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
103259
106363
  const { name, arguments: args } = request.params;
103260
106364
  const nativeTool = findNativeTool(name);
103261
106365
  if (nativeTool) {
106366
+ if (this.role !== "buyer") {
106367
+ this.logger.warn(`[Gateway] Seller agent attempted to call native tool: ${name}`);
106368
+ throw new McpError(ErrorCode.MethodNotFound, `Tool '${name}' is not available for seller agents`);
106369
+ }
103262
106370
  this.logger.info(`Calling native tool: ${name} (session: ${this.sessionId?.slice(0, 8) || "none"})`);
103263
106371
  try {
103264
106372
  if (this.sessionId) {
@@ -103274,6 +106382,11 @@ class GatewayServer {
103274
106382
  if (!tool) {
103275
106383
  throw new McpError(ErrorCode.MethodNotFound, `Tool '${name}' not found`);
103276
106384
  }
106385
+ const toolRole = tool.role || "all";
106386
+ if (toolRole !== "all" && toolRole !== this.role) {
106387
+ this.logger.warn(`[Gateway] ${this.role} agent attempted to call ${toolRole}-only tool: ${name}`);
106388
+ throw new McpError(ErrorCode.MethodNotFound, `Tool '${name}' is not available for ${this.role} agents`);
106389
+ }
103277
106390
  if (this.paymentMiddleware) {
103278
106391
  try {
103279
106392
  const headers = request.params._meta?.headers || {};
@@ -103444,6 +106557,13 @@ class GatewayServer {
103444
106557
  getSessionId() {
103445
106558
  return this.sessionId;
103446
106559
  }
106560
+ setRole(role) {
106561
+ this.role = role;
106562
+ this.logger.debug(`Gateway role set: ${role}`);
106563
+ }
106564
+ getRole() {
106565
+ return this.role;
106566
+ }
103447
106567
  async stop() {
103448
106568
  this.logger.info("Stopping MCP Gateway Server...");
103449
106569
  if (this.healthCheckInterval) {
@@ -107485,11 +110605,20 @@ var StdioTransportSchema = exports_external2.object({
107485
110605
  env: exports_external2.record(exports_external2.string()).optional(),
107486
110606
  cwd: exports_external2.string().optional()
107487
110607
  });
110608
+ var OAuthConfigSchema = exports_external2.object({
110609
+ tokenUrl: exports_external2.string().url(),
110610
+ clientId: exports_external2.string(),
110611
+ clientSecret: exports_external2.string(),
110612
+ grantType: exports_external2.enum(["client_credentials"]).default("client_credentials"),
110613
+ scope: exports_external2.string().optional(),
110614
+ refreshBuffer: exports_external2.number().default(300)
110615
+ });
107488
110616
  var HttpTransportSchema = exports_external2.object({
107489
110617
  type: exports_external2.literal("http"),
107490
110618
  url: exports_external2.string().url(),
107491
110619
  headers: exports_external2.record(exports_external2.string()).optional(),
107492
- apiKey: exports_external2.string().optional()
110620
+ apiKey: exports_external2.string().optional(),
110621
+ oauth: OAuthConfigSchema.optional()
107493
110622
  });
107494
110623
  var SseTransportSchema = exports_external2.object({
107495
110624
  type: exports_external2.literal("sse"),
@@ -107548,7 +110677,8 @@ var McpServerConfigSchema = exports_external2.object({
107548
110677
  defaultPricing: ToolPricingSchema.optional(),
107549
110678
  paymentMode: exports_external2.enum(["passthrough", "markup", "absorb"]).optional(),
107550
110679
  markup: exports_external2.string().optional(),
107551
- paymentWallet: exports_external2.string().optional()
110680
+ paymentWallet: exports_external2.string().optional(),
110681
+ role: exports_external2.enum(["buyer", "seller", "all"]).default("all")
107552
110682
  });
107553
110683
  var GatewayConfigSchema = exports_external2.object({
107554
110684
  name: exports_external2.string().default("MCP Gateway"),
@@ -108803,9 +111933,10 @@ class MCPHttpServer {
108803
111933
  this.logger.info(`[MCPServer] ========================================`);
108804
111934
  this.logger.info(`[MCPServer] Listening: http://${this.host}:${this.port}`);
108805
111935
  this.logger.info(`[MCPServer] Endpoints:`);
108806
- this.logger.info(`[MCPServer] POST /mcp - Streamable HTTP`);
108807
- this.logger.info(`[MCPServer] DELETE /mcp - Session termination`);
108808
- this.logger.info(`[MCPServer] GET /health - Health check`);
111936
+ this.logger.info(`[MCPServer] POST /mcp - Buyer agents (native tools)`);
111937
+ this.logger.info(`[MCPServer] POST /mcp/seller - Seller agents (native + catalog)`);
111938
+ this.logger.info(`[MCPServer] DELETE /mcp[/*] - Session termination`);
111939
+ this.logger.info(`[MCPServer] GET /health - Health check`);
108809
111940
  this.logger.info(`[MCPServer] Session timeout: ${MCPHttpServer.SESSION_MAX_IDLE_MS / 1000}s`);
108810
111941
  this.logger.info(`[MCPServer] ========================================`);
108811
111942
  resolve();
@@ -108875,19 +112006,20 @@ class MCPHttpServer {
108875
112006
  }));
108876
112007
  return;
108877
112008
  }
108878
- if (req.method === "POST" && (pathname === "/mcp" || pathname === "/sse")) {
108879
- await this.handleStreamableHTTP(req, res, requestId, requestStart);
112009
+ if (req.method === "POST" && (pathname === "/mcp" || pathname === "/sse" || pathname === "/mcp/seller")) {
112010
+ const role = pathname === "/mcp/seller" ? "seller" : "buyer";
112011
+ await this.handleStreamableHTTP(req, res, requestId, requestStart, role);
108880
112012
  return;
108881
112013
  }
108882
- if (req.method === "DELETE" && pathname === "/mcp") {
112014
+ if (req.method === "DELETE" && (pathname === "/mcp" || pathname === "/mcp/seller")) {
108883
112015
  await this.handleSessionTermination(req, res, requestId);
108884
112016
  return;
108885
112017
  }
108886
- if (req.method === "GET" && pathname === "/mcp") {
108887
- this.logger.warn(`[${requestId}] <-- 405 | GET /mcp not supported`);
112018
+ if (req.method === "GET" && (pathname === "/mcp" || pathname === "/mcp/seller")) {
112019
+ this.logger.warn(`[${requestId}] <-- 405 | GET ${pathname} not supported`);
108888
112020
  res.writeHead(405, { "Content-Type": "application/json" });
108889
112021
  res.end(JSON.stringify({
108890
- error: "Method Not Allowed. Use POST /mcp for Streamable HTTP transport."
112022
+ error: "Method Not Allowed. Use POST for Streamable HTTP transport."
108891
112023
  }));
108892
112024
  return;
108893
112025
  }
@@ -108908,7 +112040,7 @@ class MCPHttpServer {
108908
112040
  }
108909
112041
  }
108910
112042
  }
108911
- async handleStreamableHTTP(req, res, requestId, requestStart) {
112043
+ async handleStreamableHTTP(req, res, requestId, requestStart, role = "buyer") {
108912
112044
  const sessionId = req.headers["mcp-session-id"];
108913
112045
  const sessionShort = sessionId?.slice(0, 8) || "new";
108914
112046
  let body = "";
@@ -108960,8 +112092,9 @@ class MCPHttpServer {
108960
112092
  const newSessionId = this.generateSessionId();
108961
112093
  const clientInfo = message.params?.clientInfo;
108962
112094
  const protocolVersion = message.params?.protocolVersion || "2024-11-05";
108963
- this.logger.info(`[${requestId}] SESSION_CREATE | id=${newSessionId.slice(0, 8)} | client=${clientInfo?.name || "unknown"}@${clientInfo?.version || "?"} | protocol=${protocolVersion}`);
112095
+ this.logger.info(`[${requestId}] SESSION_CREATE | id=${newSessionId.slice(0, 8)} | role=${role} | client=${clientInfo?.name || "unknown"}@${clientInfo?.version || "?"} | protocol=${protocolVersion}`);
108964
112096
  const gateway = new GatewayServer(this.config, this.logger);
112097
+ gateway.setRole(role);
108965
112098
  await gateway.start();
108966
112099
  gateway.setSessionId(newSessionId);
108967
112100
  const transport = new StreamableHTTPServerTransport({