intent-hub 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +7 -0
- package/.turbo/cache/019f5ae385027cb1-meta.json +1 -0
- package/.turbo/cache/019f5ae385027cb1.tar.zst +0 -0
- package/.turbo/cache/040af6112a552a64-meta.json +1 -0
- package/.turbo/cache/040af6112a552a64.tar.zst +0 -0
- package/.turbo/cache/11195eac3ca5c6ce-meta.json +1 -0
- package/.turbo/cache/11195eac3ca5c6ce.tar.zst +0 -0
- package/.turbo/cache/13d11166efdf11cf-meta.json +1 -0
- package/.turbo/cache/13d11166efdf11cf.tar.zst +0 -0
- package/.turbo/cache/19af1af3b136706c-meta.json +1 -0
- package/.turbo/cache/19af1af3b136706c.tar.zst +0 -0
- package/.turbo/cache/1d33efac91c05b50-meta.json +1 -0
- package/.turbo/cache/1d33efac91c05b50.tar.zst +0 -0
- package/.turbo/cache/200b85a612af2d13-meta.json +1 -0
- package/.turbo/cache/200b85a612af2d13.tar.zst +0 -0
- package/.turbo/cache/210308c9ea929858-meta.json +1 -0
- package/.turbo/cache/210308c9ea929858.tar.zst +0 -0
- package/.turbo/cache/38df8e44c617835e-meta.json +1 -0
- package/.turbo/cache/38df8e44c617835e.tar.zst +0 -0
- package/.turbo/cache/3e449de5ef60a7a0-meta.json +1 -0
- package/.turbo/cache/3e449de5ef60a7a0.tar.zst +0 -0
- package/.turbo/cache/51ff024a97c2b4f5-meta.json +1 -0
- package/.turbo/cache/51ff024a97c2b4f5.tar.zst +0 -0
- package/.turbo/cache/54bc756eeebb377a-meta.json +1 -0
- package/.turbo/cache/54bc756eeebb377a.tar.zst +0 -0
- package/.turbo/cache/5ed6a840acafc873-meta.json +1 -0
- package/.turbo/cache/5ed6a840acafc873.tar.zst +0 -0
- package/.turbo/cache/6702dc24e5ca3c2e-meta.json +1 -0
- package/.turbo/cache/6702dc24e5ca3c2e.tar.zst +0 -0
- package/.turbo/cache/725c72cf71ea854f-meta.json +1 -0
- package/.turbo/cache/725c72cf71ea854f.tar.zst +0 -0
- package/.turbo/cache/7344ca28d348037a-meta.json +1 -0
- package/.turbo/cache/7344ca28d348037a.tar.zst +0 -0
- package/.turbo/cache/748fb444cdc0b78c-meta.json +1 -0
- package/.turbo/cache/748fb444cdc0b78c.tar.zst +0 -0
- package/.turbo/cache/789677c36fe7fb98-meta.json +1 -0
- package/.turbo/cache/789677c36fe7fb98.tar.zst +0 -0
- package/.turbo/cache/89ff6c6f38dd4a18-meta.json +1 -0
- package/.turbo/cache/89ff6c6f38dd4a18.tar.zst +0 -0
- package/.turbo/cache/8dbc92d00de0c92e-meta.json +1 -0
- package/.turbo/cache/8dbc92d00de0c92e.tar.zst +0 -0
- package/.turbo/cache/8eb03f40082b9441-meta.json +1 -0
- package/.turbo/cache/8eb03f40082b9441.tar.zst +0 -0
- package/.turbo/cache/9157134d4b916017-meta.json +1 -0
- package/.turbo/cache/9157134d4b916017.tar.zst +0 -0
- package/.turbo/cache/94219ffd32b48e93-meta.json +1 -0
- package/.turbo/cache/94219ffd32b48e93.tar.zst +0 -0
- package/.turbo/cache/95c1d160b4fa84eb-meta.json +1 -0
- package/.turbo/cache/95c1d160b4fa84eb.tar.zst +0 -0
- package/.turbo/cache/998833ea02dfb225-meta.json +1 -0
- package/.turbo/cache/998833ea02dfb225.tar.zst +0 -0
- package/.turbo/cache/a5974ef6ade3eb90-meta.json +1 -0
- package/.turbo/cache/a5974ef6ade3eb90.tar.zst +0 -0
- package/.turbo/cache/aab811809257decb-meta.json +1 -0
- package/.turbo/cache/aab811809257decb.tar.zst +0 -0
- package/.turbo/cache/ab2f82a54da854fd-meta.json +1 -0
- package/.turbo/cache/ab2f82a54da854fd.tar.zst +0 -0
- package/.turbo/cache/abbf4d95d62a7303-meta.json +1 -0
- package/.turbo/cache/abbf4d95d62a7303.tar.zst +0 -0
- package/.turbo/cache/af4441f519f9ce50-meta.json +1 -0
- package/.turbo/cache/af4441f519f9ce50.tar.zst +0 -0
- package/.turbo/cache/b9b85aaaf03d00a6-meta.json +1 -0
- package/.turbo/cache/b9b85aaaf03d00a6.tar.zst +0 -0
- package/.turbo/cache/cd58ee8721bbfed7-meta.json +1 -0
- package/.turbo/cache/cd58ee8721bbfed7.tar.zst +0 -0
- package/.turbo/cache/d285e48b8afa30b5-meta.json +1 -0
- package/.turbo/cache/d285e48b8afa30b5.tar.zst +0 -0
- package/.turbo/cache/d33e90229142acce-meta.json +1 -0
- package/.turbo/cache/d33e90229142acce.tar.zst +0 -0
- package/.turbo/cache/d57839a0d3b04540-meta.json +1 -0
- package/.turbo/cache/d57839a0d3b04540.tar.zst +0 -0
- package/.turbo/cache/d8554ef2c8b6e5eb-meta.json +1 -0
- package/.turbo/cache/d8554ef2c8b6e5eb.tar.zst +0 -0
- package/.turbo/cache/dc7375b51290e102-meta.json +1 -0
- package/.turbo/cache/dc7375b51290e102.tar.zst +0 -0
- package/.turbo/cache/e5310fe547fdbf0a-meta.json +1 -0
- package/.turbo/cache/e5310fe547fdbf0a.tar.zst +0 -0
- package/.turbo/cache/f12bb5f2f188758d-meta.json +1 -0
- package/.turbo/cache/f12bb5f2f188758d.tar.zst +0 -0
- package/.turbo/cache/f2db5af0c0b4d23f-meta.json +1 -0
- package/.turbo/cache/f2db5af0c0b4d23f.tar.zst +0 -0
- package/.turbo/cache/f8935ade01a88cd7-meta.json +1 -0
- package/.turbo/cache/f8935ade01a88cd7.tar.zst +0 -0
- package/.turbo/cache/f982b8dd966f823a-meta.json +1 -0
- package/.turbo/cache/f982b8dd966f823a.tar.zst +0 -0
- package/.turbo/cache/f9d4036dd350ba1a-meta.json +1 -0
- package/.turbo/cache/f9d4036dd350ba1a.tar.zst +0 -0
- package/README.md +661 -0
- package/README_ko.md +577 -0
- package/bun.lock +135 -0
- package/package.json +26 -0
- package/packages/agent/.turbo/turbo-build.log +5 -0
- package/packages/agent/.turbo/turbo-typecheck.log +1 -0
- package/packages/agent/dist/connection/hub-client.d.ts +33 -0
- package/packages/agent/dist/connection/hub-client.d.ts.map +1 -0
- package/packages/agent/dist/connection/index.d.ts +2 -0
- package/packages/agent/dist/connection/index.d.ts.map +1 -0
- package/packages/agent/dist/hooks/index.d.ts +3 -0
- package/packages/agent/dist/hooks/index.d.ts.map +1 -0
- package/packages/agent/dist/hooks/intent-hub-hooks.d.ts +47 -0
- package/packages/agent/dist/hooks/intent-hub-hooks.d.ts.map +1 -0
- package/packages/agent/dist/index.d.ts +6 -0
- package/packages/agent/dist/index.d.ts.map +1 -0
- package/packages/agent/dist/index.js +3315 -0
- package/packages/agent/dist/plugin/index.d.ts +3 -0
- package/packages/agent/dist/plugin/index.d.ts.map +1 -0
- package/packages/agent/dist/plugin/intent-hub-plugin.d.ts +54 -0
- package/packages/agent/dist/plugin/intent-hub-plugin.d.ts.map +1 -0
- package/packages/agent/package.json +32 -0
- package/packages/agent/src/connection/hub-client.ts +152 -0
- package/packages/agent/src/connection/index.ts +1 -0
- package/packages/agent/src/hooks/index.ts +2 -0
- package/packages/agent/src/hooks/intent-hub-hooks.ts +245 -0
- package/packages/agent/src/index.ts +5 -0
- package/packages/agent/src/plugin/index.ts +2 -0
- package/packages/agent/src/plugin/intent-hub-plugin.ts +153 -0
- package/packages/agent/tsconfig.json +9 -0
- package/packages/hub/.turbo/turbo-build.log +6 -0
- package/packages/hub/.turbo/turbo-typecheck.log +1 -0
- package/packages/hub/dist/api/dashboard.d.ts +17 -0
- package/packages/hub/dist/api/dashboard.d.ts.map +1 -0
- package/packages/hub/dist/cli.d.ts +3 -0
- package/packages/hub/dist/cli.d.ts.map +1 -0
- package/packages/hub/dist/cli.js +7719 -0
- package/packages/hub/dist/core/conflict-detector.d.ts +36 -0
- package/packages/hub/dist/core/conflict-detector.d.ts.map +1 -0
- package/packages/hub/dist/core/index.d.ts +7 -0
- package/packages/hub/dist/core/index.d.ts.map +1 -0
- package/packages/hub/dist/core/intent-analyzer.d.ts +8 -0
- package/packages/hub/dist/core/intent-analyzer.d.ts.map +1 -0
- package/packages/hub/dist/core/lock-manager.d.ts +13 -0
- package/packages/hub/dist/core/lock-manager.d.ts.map +1 -0
- package/packages/hub/dist/core/orchestrator.d.ts +46 -0
- package/packages/hub/dist/core/orchestrator.d.ts.map +1 -0
- package/packages/hub/dist/index.d.ts +9 -0
- package/packages/hub/dist/index.d.ts.map +1 -0
- package/packages/hub/dist/index.js +4686 -0
- package/packages/hub/dist/llm/index.d.ts +7 -0
- package/packages/hub/dist/llm/index.d.ts.map +1 -0
- package/packages/hub/dist/llm/negotiation-engine.d.ts +40 -0
- package/packages/hub/dist/llm/negotiation-engine.d.ts.map +1 -0
- package/packages/hub/dist/llm/provider.d.ts +46 -0
- package/packages/hub/dist/llm/provider.d.ts.map +1 -0
- package/packages/hub/dist/llm/smart-analyzer.d.ts +20 -0
- package/packages/hub/dist/llm/smart-analyzer.d.ts.map +1 -0
- package/packages/hub/dist/server/hub-server.d.ts +35 -0
- package/packages/hub/dist/server/hub-server.d.ts.map +1 -0
- package/packages/hub/dist/server/index.d.ts +5 -0
- package/packages/hub/dist/server/index.d.ts.map +1 -0
- package/packages/hub/dist/server/message-handler.d.ts +18 -0
- package/packages/hub/dist/server/message-handler.d.ts.map +1 -0
- package/packages/hub/dist/server/smart-hub-server.d.ts +43 -0
- package/packages/hub/dist/server/smart-hub-server.d.ts.map +1 -0
- package/packages/hub/dist/state/index.d.ts +2 -0
- package/packages/hub/dist/state/index.d.ts.map +1 -0
- package/packages/hub/dist/state/session-manager.d.ts +19 -0
- package/packages/hub/dist/state/session-manager.d.ts.map +1 -0
- package/packages/hub/dist/tunnel/index.d.ts +14 -0
- package/packages/hub/dist/tunnel/index.d.ts.map +1 -0
- package/packages/hub/package.json +54 -0
- package/packages/hub/src/api/dashboard.ts +261 -0
- package/packages/hub/src/cli.ts +193 -0
- package/packages/hub/src/core/conflict-detector.ts +138 -0
- package/packages/hub/src/core/index.ts +6 -0
- package/packages/hub/src/core/intent-analyzer.ts +112 -0
- package/packages/hub/src/core/lock-manager.ts +95 -0
- package/packages/hub/src/core/orchestrator.ts +255 -0
- package/packages/hub/src/index.ts +8 -0
- package/packages/hub/src/llm/index.ts +17 -0
- package/packages/hub/src/llm/negotiation-engine.ts +297 -0
- package/packages/hub/src/llm/provider.ts +175 -0
- package/packages/hub/src/llm/smart-analyzer.ts +169 -0
- package/packages/hub/src/server/hub-server.ts +219 -0
- package/packages/hub/src/server/index.ts +4 -0
- package/packages/hub/src/server/message-handler.ts +111 -0
- package/packages/hub/src/server/smart-hub-server.ts +374 -0
- package/packages/hub/src/state/index.ts +1 -0
- package/packages/hub/src/state/session-manager.ts +59 -0
- package/packages/hub/src/tunnel/index.ts +153 -0
- package/packages/hub/tsconfig.json +9 -0
- package/packages/shared/.turbo/turbo-build.log +5 -0
- package/packages/shared/.turbo/turbo-typecheck.log +1 -0
- package/packages/shared/dist/index.d.ts +3 -0
- package/packages/shared/dist/index.d.ts.map +1 -0
- package/packages/shared/dist/index.js +50 -0
- package/packages/shared/dist/types/domain.d.ts +50 -0
- package/packages/shared/dist/types/domain.d.ts.map +1 -0
- package/packages/shared/dist/types/index.d.ts +4 -0
- package/packages/shared/dist/types/index.d.ts.map +1 -0
- package/packages/shared/dist/types/intent.d.ts +24 -0
- package/packages/shared/dist/types/intent.d.ts.map +1 -0
- package/packages/shared/dist/types/message.d.ts +151 -0
- package/packages/shared/dist/types/message.d.ts.map +1 -0
- package/packages/shared/dist/utils/id.d.ts +6 -0
- package/packages/shared/dist/utils/id.d.ts.map +1 -0
- package/packages/shared/dist/utils/index.d.ts +3 -0
- package/packages/shared/dist/utils/index.d.ts.map +1 -0
- package/packages/shared/dist/utils/message.d.ts +5 -0
- package/packages/shared/dist/utils/message.d.ts.map +1 -0
- package/packages/shared/package.json +33 -0
- package/packages/shared/src/index.ts +2 -0
- package/packages/shared/src/types/domain.ts +57 -0
- package/packages/shared/src/types/index.ts +3 -0
- package/packages/shared/src/types/intent.ts +34 -0
- package/packages/shared/src/types/message.ts +188 -0
- package/packages/shared/src/utils/id.ts +21 -0
- package/packages/shared/src/utils/index.ts +2 -0
- package/packages/shared/src/utils/message.ts +30 -0
- package/packages/shared/tsconfig.json +9 -0
- package/scripts/test-e2e.ts +194 -0
- package/scripts/test-mvp2.ts +167 -0
- package/scripts/test-mvp3.ts +405 -0
- package/tsconfig.json +19 -0
- package/turbo.json +22 -0
|
@@ -0,0 +1,3315 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
+
for (let key of __getOwnPropNames(mod))
|
|
11
|
+
if (!__hasOwnProp.call(to, key))
|
|
12
|
+
__defProp(to, key, {
|
|
13
|
+
get: () => mod[key],
|
|
14
|
+
enumerable: true
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
19
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
20
|
+
|
|
21
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/constants.js
|
|
22
|
+
var require_constants = __commonJS((exports, module) => {
|
|
23
|
+
var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
|
|
24
|
+
var hasBlob = typeof Blob !== "undefined";
|
|
25
|
+
if (hasBlob)
|
|
26
|
+
BINARY_TYPES.push("blob");
|
|
27
|
+
module.exports = {
|
|
28
|
+
BINARY_TYPES,
|
|
29
|
+
CLOSE_TIMEOUT: 30000,
|
|
30
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
31
|
+
GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
32
|
+
hasBlob,
|
|
33
|
+
kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
|
|
34
|
+
kListener: Symbol("kListener"),
|
|
35
|
+
kStatusCode: Symbol("status-code"),
|
|
36
|
+
kWebSocket: Symbol("websocket"),
|
|
37
|
+
NOOP: () => {}
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/buffer-util.js
|
|
42
|
+
var require_buffer_util = __commonJS((exports, module) => {
|
|
43
|
+
var { EMPTY_BUFFER } = require_constants();
|
|
44
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
45
|
+
function concat(list, totalLength) {
|
|
46
|
+
if (list.length === 0)
|
|
47
|
+
return EMPTY_BUFFER;
|
|
48
|
+
if (list.length === 1)
|
|
49
|
+
return list[0];
|
|
50
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
51
|
+
let offset = 0;
|
|
52
|
+
for (let i = 0;i < list.length; i++) {
|
|
53
|
+
const buf = list[i];
|
|
54
|
+
target.set(buf, offset);
|
|
55
|
+
offset += buf.length;
|
|
56
|
+
}
|
|
57
|
+
if (offset < totalLength) {
|
|
58
|
+
return new FastBuffer(target.buffer, target.byteOffset, offset);
|
|
59
|
+
}
|
|
60
|
+
return target;
|
|
61
|
+
}
|
|
62
|
+
function _mask(source, mask, output, offset, length) {
|
|
63
|
+
for (let i = 0;i < length; i++) {
|
|
64
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function _unmask(buffer, mask) {
|
|
68
|
+
for (let i = 0;i < buffer.length; i++) {
|
|
69
|
+
buffer[i] ^= mask[i & 3];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function toArrayBuffer(buf) {
|
|
73
|
+
if (buf.length === buf.buffer.byteLength) {
|
|
74
|
+
return buf.buffer;
|
|
75
|
+
}
|
|
76
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
77
|
+
}
|
|
78
|
+
function toBuffer(data) {
|
|
79
|
+
toBuffer.readOnly = true;
|
|
80
|
+
if (Buffer.isBuffer(data))
|
|
81
|
+
return data;
|
|
82
|
+
let buf;
|
|
83
|
+
if (data instanceof ArrayBuffer) {
|
|
84
|
+
buf = new FastBuffer(data);
|
|
85
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
86
|
+
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
87
|
+
} else {
|
|
88
|
+
buf = Buffer.from(data);
|
|
89
|
+
toBuffer.readOnly = false;
|
|
90
|
+
}
|
|
91
|
+
return buf;
|
|
92
|
+
}
|
|
93
|
+
module.exports = {
|
|
94
|
+
concat,
|
|
95
|
+
mask: _mask,
|
|
96
|
+
toArrayBuffer,
|
|
97
|
+
toBuffer,
|
|
98
|
+
unmask: _unmask
|
|
99
|
+
};
|
|
100
|
+
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
101
|
+
try {
|
|
102
|
+
const bufferUtil = (()=>{throw new Error("Cannot require module "+"bufferutil");})();
|
|
103
|
+
module.exports.mask = function(source, mask, output, offset, length) {
|
|
104
|
+
if (length < 48)
|
|
105
|
+
_mask(source, mask, output, offset, length);
|
|
106
|
+
else
|
|
107
|
+
bufferUtil.mask(source, mask, output, offset, length);
|
|
108
|
+
};
|
|
109
|
+
module.exports.unmask = function(buffer, mask) {
|
|
110
|
+
if (buffer.length < 32)
|
|
111
|
+
_unmask(buffer, mask);
|
|
112
|
+
else
|
|
113
|
+
bufferUtil.unmask(buffer, mask);
|
|
114
|
+
};
|
|
115
|
+
} catch (e) {}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/limiter.js
|
|
120
|
+
var require_limiter = __commonJS((exports, module) => {
|
|
121
|
+
var kDone = Symbol("kDone");
|
|
122
|
+
var kRun = Symbol("kRun");
|
|
123
|
+
|
|
124
|
+
class Limiter {
|
|
125
|
+
constructor(concurrency) {
|
|
126
|
+
this[kDone] = () => {
|
|
127
|
+
this.pending--;
|
|
128
|
+
this[kRun]();
|
|
129
|
+
};
|
|
130
|
+
this.concurrency = concurrency || Infinity;
|
|
131
|
+
this.jobs = [];
|
|
132
|
+
this.pending = 0;
|
|
133
|
+
}
|
|
134
|
+
add(job) {
|
|
135
|
+
this.jobs.push(job);
|
|
136
|
+
this[kRun]();
|
|
137
|
+
}
|
|
138
|
+
[kRun]() {
|
|
139
|
+
if (this.pending === this.concurrency)
|
|
140
|
+
return;
|
|
141
|
+
if (this.jobs.length) {
|
|
142
|
+
const job = this.jobs.shift();
|
|
143
|
+
this.pending++;
|
|
144
|
+
job(this[kDone]);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
module.exports = Limiter;
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/permessage-deflate.js
|
|
152
|
+
var require_permessage_deflate = __commonJS((exports, module) => {
|
|
153
|
+
var zlib = __require("zlib");
|
|
154
|
+
var bufferUtil = require_buffer_util();
|
|
155
|
+
var Limiter = require_limiter();
|
|
156
|
+
var { kStatusCode } = require_constants();
|
|
157
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
158
|
+
var TRAILER = Buffer.from([0, 0, 255, 255]);
|
|
159
|
+
var kPerMessageDeflate = Symbol("permessage-deflate");
|
|
160
|
+
var kTotalLength = Symbol("total-length");
|
|
161
|
+
var kCallback = Symbol("callback");
|
|
162
|
+
var kBuffers = Symbol("buffers");
|
|
163
|
+
var kError = Symbol("error");
|
|
164
|
+
var zlibLimiter;
|
|
165
|
+
|
|
166
|
+
class PerMessageDeflate {
|
|
167
|
+
constructor(options, isServer, maxPayload) {
|
|
168
|
+
this._maxPayload = maxPayload | 0;
|
|
169
|
+
this._options = options || {};
|
|
170
|
+
this._threshold = this._options.threshold !== undefined ? this._options.threshold : 1024;
|
|
171
|
+
this._isServer = !!isServer;
|
|
172
|
+
this._deflate = null;
|
|
173
|
+
this._inflate = null;
|
|
174
|
+
this.params = null;
|
|
175
|
+
if (!zlibLimiter) {
|
|
176
|
+
const concurrency = this._options.concurrencyLimit !== undefined ? this._options.concurrencyLimit : 10;
|
|
177
|
+
zlibLimiter = new Limiter(concurrency);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
static get extensionName() {
|
|
181
|
+
return "permessage-deflate";
|
|
182
|
+
}
|
|
183
|
+
offer() {
|
|
184
|
+
const params = {};
|
|
185
|
+
if (this._options.serverNoContextTakeover) {
|
|
186
|
+
params.server_no_context_takeover = true;
|
|
187
|
+
}
|
|
188
|
+
if (this._options.clientNoContextTakeover) {
|
|
189
|
+
params.client_no_context_takeover = true;
|
|
190
|
+
}
|
|
191
|
+
if (this._options.serverMaxWindowBits) {
|
|
192
|
+
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
|
193
|
+
}
|
|
194
|
+
if (this._options.clientMaxWindowBits) {
|
|
195
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
196
|
+
} else if (this._options.clientMaxWindowBits == null) {
|
|
197
|
+
params.client_max_window_bits = true;
|
|
198
|
+
}
|
|
199
|
+
return params;
|
|
200
|
+
}
|
|
201
|
+
accept(configurations) {
|
|
202
|
+
configurations = this.normalizeParams(configurations);
|
|
203
|
+
this.params = this._isServer ? this.acceptAsServer(configurations) : this.acceptAsClient(configurations);
|
|
204
|
+
return this.params;
|
|
205
|
+
}
|
|
206
|
+
cleanup() {
|
|
207
|
+
if (this._inflate) {
|
|
208
|
+
this._inflate.close();
|
|
209
|
+
this._inflate = null;
|
|
210
|
+
}
|
|
211
|
+
if (this._deflate) {
|
|
212
|
+
const callback = this._deflate[kCallback];
|
|
213
|
+
this._deflate.close();
|
|
214
|
+
this._deflate = null;
|
|
215
|
+
if (callback) {
|
|
216
|
+
callback(new Error("The deflate stream was closed while data was being processed"));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
acceptAsServer(offers) {
|
|
221
|
+
const opts = this._options;
|
|
222
|
+
const accepted = offers.find((params) => {
|
|
223
|
+
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) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
return true;
|
|
227
|
+
});
|
|
228
|
+
if (!accepted) {
|
|
229
|
+
throw new Error("None of the extension offers can be accepted");
|
|
230
|
+
}
|
|
231
|
+
if (opts.serverNoContextTakeover) {
|
|
232
|
+
accepted.server_no_context_takeover = true;
|
|
233
|
+
}
|
|
234
|
+
if (opts.clientNoContextTakeover) {
|
|
235
|
+
accepted.client_no_context_takeover = true;
|
|
236
|
+
}
|
|
237
|
+
if (typeof opts.serverMaxWindowBits === "number") {
|
|
238
|
+
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
|
239
|
+
}
|
|
240
|
+
if (typeof opts.clientMaxWindowBits === "number") {
|
|
241
|
+
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
|
242
|
+
} else if (accepted.client_max_window_bits === true || opts.clientMaxWindowBits === false) {
|
|
243
|
+
delete accepted.client_max_window_bits;
|
|
244
|
+
}
|
|
245
|
+
return accepted;
|
|
246
|
+
}
|
|
247
|
+
acceptAsClient(response) {
|
|
248
|
+
const params = response[0];
|
|
249
|
+
if (this._options.clientNoContextTakeover === false && params.client_no_context_takeover) {
|
|
250
|
+
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
|
251
|
+
}
|
|
252
|
+
if (!params.client_max_window_bits) {
|
|
253
|
+
if (typeof this._options.clientMaxWindowBits === "number") {
|
|
254
|
+
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
|
255
|
+
}
|
|
256
|
+
} else if (this._options.clientMaxWindowBits === false || typeof this._options.clientMaxWindowBits === "number" && params.client_max_window_bits > this._options.clientMaxWindowBits) {
|
|
257
|
+
throw new Error('Unexpected or invalid parameter "client_max_window_bits"');
|
|
258
|
+
}
|
|
259
|
+
return params;
|
|
260
|
+
}
|
|
261
|
+
normalizeParams(configurations) {
|
|
262
|
+
configurations.forEach((params) => {
|
|
263
|
+
Object.keys(params).forEach((key) => {
|
|
264
|
+
let value = params[key];
|
|
265
|
+
if (value.length > 1) {
|
|
266
|
+
throw new Error(`Parameter "${key}" must have only a single value`);
|
|
267
|
+
}
|
|
268
|
+
value = value[0];
|
|
269
|
+
if (key === "client_max_window_bits") {
|
|
270
|
+
if (value !== true) {
|
|
271
|
+
const num = +value;
|
|
272
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
273
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
274
|
+
}
|
|
275
|
+
value = num;
|
|
276
|
+
} else if (!this._isServer) {
|
|
277
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
278
|
+
}
|
|
279
|
+
} else if (key === "server_max_window_bits") {
|
|
280
|
+
const num = +value;
|
|
281
|
+
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
|
282
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
283
|
+
}
|
|
284
|
+
value = num;
|
|
285
|
+
} else if (key === "client_no_context_takeover" || key === "server_no_context_takeover") {
|
|
286
|
+
if (value !== true) {
|
|
287
|
+
throw new TypeError(`Invalid value for parameter "${key}": ${value}`);
|
|
288
|
+
}
|
|
289
|
+
} else {
|
|
290
|
+
throw new Error(`Unknown parameter "${key}"`);
|
|
291
|
+
}
|
|
292
|
+
params[key] = value;
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
return configurations;
|
|
296
|
+
}
|
|
297
|
+
decompress(data, fin, callback) {
|
|
298
|
+
zlibLimiter.add((done) => {
|
|
299
|
+
this._decompress(data, fin, (err, result) => {
|
|
300
|
+
done();
|
|
301
|
+
callback(err, result);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
compress(data, fin, callback) {
|
|
306
|
+
zlibLimiter.add((done) => {
|
|
307
|
+
this._compress(data, fin, (err, result) => {
|
|
308
|
+
done();
|
|
309
|
+
callback(err, result);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
_decompress(data, fin, callback) {
|
|
314
|
+
const endpoint = this._isServer ? "client" : "server";
|
|
315
|
+
if (!this._inflate) {
|
|
316
|
+
const key = `${endpoint}_max_window_bits`;
|
|
317
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
318
|
+
this._inflate = zlib.createInflateRaw({
|
|
319
|
+
...this._options.zlibInflateOptions,
|
|
320
|
+
windowBits
|
|
321
|
+
});
|
|
322
|
+
this._inflate[kPerMessageDeflate] = this;
|
|
323
|
+
this._inflate[kTotalLength] = 0;
|
|
324
|
+
this._inflate[kBuffers] = [];
|
|
325
|
+
this._inflate.on("error", inflateOnError);
|
|
326
|
+
this._inflate.on("data", inflateOnData);
|
|
327
|
+
}
|
|
328
|
+
this._inflate[kCallback] = callback;
|
|
329
|
+
this._inflate.write(data);
|
|
330
|
+
if (fin)
|
|
331
|
+
this._inflate.write(TRAILER);
|
|
332
|
+
this._inflate.flush(() => {
|
|
333
|
+
const err = this._inflate[kError];
|
|
334
|
+
if (err) {
|
|
335
|
+
this._inflate.close();
|
|
336
|
+
this._inflate = null;
|
|
337
|
+
callback(err);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const data2 = bufferUtil.concat(this._inflate[kBuffers], this._inflate[kTotalLength]);
|
|
341
|
+
if (this._inflate._readableState.endEmitted) {
|
|
342
|
+
this._inflate.close();
|
|
343
|
+
this._inflate = null;
|
|
344
|
+
} else {
|
|
345
|
+
this._inflate[kTotalLength] = 0;
|
|
346
|
+
this._inflate[kBuffers] = [];
|
|
347
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
348
|
+
this._inflate.reset();
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
callback(null, data2);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
_compress(data, fin, callback) {
|
|
355
|
+
const endpoint = this._isServer ? "server" : "client";
|
|
356
|
+
if (!this._deflate) {
|
|
357
|
+
const key = `${endpoint}_max_window_bits`;
|
|
358
|
+
const windowBits = typeof this.params[key] !== "number" ? zlib.Z_DEFAULT_WINDOWBITS : this.params[key];
|
|
359
|
+
this._deflate = zlib.createDeflateRaw({
|
|
360
|
+
...this._options.zlibDeflateOptions,
|
|
361
|
+
windowBits
|
|
362
|
+
});
|
|
363
|
+
this._deflate[kTotalLength] = 0;
|
|
364
|
+
this._deflate[kBuffers] = [];
|
|
365
|
+
this._deflate.on("data", deflateOnData);
|
|
366
|
+
}
|
|
367
|
+
this._deflate[kCallback] = callback;
|
|
368
|
+
this._deflate.write(data);
|
|
369
|
+
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
|
370
|
+
if (!this._deflate) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
let data2 = bufferUtil.concat(this._deflate[kBuffers], this._deflate[kTotalLength]);
|
|
374
|
+
if (fin) {
|
|
375
|
+
data2 = new FastBuffer(data2.buffer, data2.byteOffset, data2.length - 4);
|
|
376
|
+
}
|
|
377
|
+
this._deflate[kCallback] = null;
|
|
378
|
+
this._deflate[kTotalLength] = 0;
|
|
379
|
+
this._deflate[kBuffers] = [];
|
|
380
|
+
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
|
381
|
+
this._deflate.reset();
|
|
382
|
+
}
|
|
383
|
+
callback(null, data2);
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
module.exports = PerMessageDeflate;
|
|
388
|
+
function deflateOnData(chunk) {
|
|
389
|
+
this[kBuffers].push(chunk);
|
|
390
|
+
this[kTotalLength] += chunk.length;
|
|
391
|
+
}
|
|
392
|
+
function inflateOnData(chunk) {
|
|
393
|
+
this[kTotalLength] += chunk.length;
|
|
394
|
+
if (this[kPerMessageDeflate]._maxPayload < 1 || this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload) {
|
|
395
|
+
this[kBuffers].push(chunk);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
this[kError] = new RangeError("Max payload size exceeded");
|
|
399
|
+
this[kError].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH";
|
|
400
|
+
this[kError][kStatusCode] = 1009;
|
|
401
|
+
this.removeListener("data", inflateOnData);
|
|
402
|
+
this.reset();
|
|
403
|
+
}
|
|
404
|
+
function inflateOnError(err) {
|
|
405
|
+
this[kPerMessageDeflate]._inflate = null;
|
|
406
|
+
if (this[kError]) {
|
|
407
|
+
this[kCallback](this[kError]);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
err[kStatusCode] = 1007;
|
|
411
|
+
this[kCallback](err);
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/validation.js
|
|
416
|
+
var require_validation = __commonJS((exports, module) => {
|
|
417
|
+
var { isUtf8 } = __require("buffer");
|
|
418
|
+
var { hasBlob } = require_constants();
|
|
419
|
+
var tokenChars = [
|
|
420
|
+
0,
|
|
421
|
+
0,
|
|
422
|
+
0,
|
|
423
|
+
0,
|
|
424
|
+
0,
|
|
425
|
+
0,
|
|
426
|
+
0,
|
|
427
|
+
0,
|
|
428
|
+
0,
|
|
429
|
+
0,
|
|
430
|
+
0,
|
|
431
|
+
0,
|
|
432
|
+
0,
|
|
433
|
+
0,
|
|
434
|
+
0,
|
|
435
|
+
0,
|
|
436
|
+
0,
|
|
437
|
+
0,
|
|
438
|
+
0,
|
|
439
|
+
0,
|
|
440
|
+
0,
|
|
441
|
+
0,
|
|
442
|
+
0,
|
|
443
|
+
0,
|
|
444
|
+
0,
|
|
445
|
+
0,
|
|
446
|
+
0,
|
|
447
|
+
0,
|
|
448
|
+
0,
|
|
449
|
+
0,
|
|
450
|
+
0,
|
|
451
|
+
0,
|
|
452
|
+
0,
|
|
453
|
+
1,
|
|
454
|
+
0,
|
|
455
|
+
1,
|
|
456
|
+
1,
|
|
457
|
+
1,
|
|
458
|
+
1,
|
|
459
|
+
1,
|
|
460
|
+
0,
|
|
461
|
+
0,
|
|
462
|
+
1,
|
|
463
|
+
1,
|
|
464
|
+
0,
|
|
465
|
+
1,
|
|
466
|
+
1,
|
|
467
|
+
0,
|
|
468
|
+
1,
|
|
469
|
+
1,
|
|
470
|
+
1,
|
|
471
|
+
1,
|
|
472
|
+
1,
|
|
473
|
+
1,
|
|
474
|
+
1,
|
|
475
|
+
1,
|
|
476
|
+
1,
|
|
477
|
+
1,
|
|
478
|
+
0,
|
|
479
|
+
0,
|
|
480
|
+
0,
|
|
481
|
+
0,
|
|
482
|
+
0,
|
|
483
|
+
0,
|
|
484
|
+
0,
|
|
485
|
+
1,
|
|
486
|
+
1,
|
|
487
|
+
1,
|
|
488
|
+
1,
|
|
489
|
+
1,
|
|
490
|
+
1,
|
|
491
|
+
1,
|
|
492
|
+
1,
|
|
493
|
+
1,
|
|
494
|
+
1,
|
|
495
|
+
1,
|
|
496
|
+
1,
|
|
497
|
+
1,
|
|
498
|
+
1,
|
|
499
|
+
1,
|
|
500
|
+
1,
|
|
501
|
+
1,
|
|
502
|
+
1,
|
|
503
|
+
1,
|
|
504
|
+
1,
|
|
505
|
+
1,
|
|
506
|
+
1,
|
|
507
|
+
1,
|
|
508
|
+
1,
|
|
509
|
+
1,
|
|
510
|
+
1,
|
|
511
|
+
0,
|
|
512
|
+
0,
|
|
513
|
+
0,
|
|
514
|
+
1,
|
|
515
|
+
1,
|
|
516
|
+
1,
|
|
517
|
+
1,
|
|
518
|
+
1,
|
|
519
|
+
1,
|
|
520
|
+
1,
|
|
521
|
+
1,
|
|
522
|
+
1,
|
|
523
|
+
1,
|
|
524
|
+
1,
|
|
525
|
+
1,
|
|
526
|
+
1,
|
|
527
|
+
1,
|
|
528
|
+
1,
|
|
529
|
+
1,
|
|
530
|
+
1,
|
|
531
|
+
1,
|
|
532
|
+
1,
|
|
533
|
+
1,
|
|
534
|
+
1,
|
|
535
|
+
1,
|
|
536
|
+
1,
|
|
537
|
+
1,
|
|
538
|
+
1,
|
|
539
|
+
1,
|
|
540
|
+
1,
|
|
541
|
+
1,
|
|
542
|
+
1,
|
|
543
|
+
0,
|
|
544
|
+
1,
|
|
545
|
+
0,
|
|
546
|
+
1,
|
|
547
|
+
0
|
|
548
|
+
];
|
|
549
|
+
function isValidStatusCode(code) {
|
|
550
|
+
return code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3000 && code <= 4999;
|
|
551
|
+
}
|
|
552
|
+
function _isValidUTF8(buf) {
|
|
553
|
+
const len = buf.length;
|
|
554
|
+
let i = 0;
|
|
555
|
+
while (i < len) {
|
|
556
|
+
if ((buf[i] & 128) === 0) {
|
|
557
|
+
i++;
|
|
558
|
+
} else if ((buf[i] & 224) === 192) {
|
|
559
|
+
if (i + 1 === len || (buf[i + 1] & 192) !== 128 || (buf[i] & 254) === 192) {
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
i += 2;
|
|
563
|
+
} else if ((buf[i] & 240) === 224) {
|
|
564
|
+
if (i + 2 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || buf[i] === 224 && (buf[i + 1] & 224) === 128 || buf[i] === 237 && (buf[i + 1] & 224) === 160) {
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
i += 3;
|
|
568
|
+
} else if ((buf[i] & 248) === 240) {
|
|
569
|
+
if (i + 3 >= len || (buf[i + 1] & 192) !== 128 || (buf[i + 2] & 192) !== 128 || (buf[i + 3] & 192) !== 128 || buf[i] === 240 && (buf[i + 1] & 240) === 128 || buf[i] === 244 && buf[i + 1] > 143 || buf[i] > 244) {
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
i += 4;
|
|
573
|
+
} else {
|
|
574
|
+
return false;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return true;
|
|
578
|
+
}
|
|
579
|
+
function isBlob(value) {
|
|
580
|
+
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");
|
|
581
|
+
}
|
|
582
|
+
module.exports = {
|
|
583
|
+
isBlob,
|
|
584
|
+
isValidStatusCode,
|
|
585
|
+
isValidUTF8: _isValidUTF8,
|
|
586
|
+
tokenChars
|
|
587
|
+
};
|
|
588
|
+
if (isUtf8) {
|
|
589
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
590
|
+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
|
|
591
|
+
};
|
|
592
|
+
} else if (!process.env.WS_NO_UTF_8_VALIDATE) {
|
|
593
|
+
try {
|
|
594
|
+
const isValidUTF8 = (()=>{throw new Error("Cannot require module "+"utf-8-validate");})();
|
|
595
|
+
module.exports.isValidUTF8 = function(buf) {
|
|
596
|
+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
|
597
|
+
};
|
|
598
|
+
} catch (e) {}
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/receiver.js
|
|
603
|
+
var require_receiver = __commonJS((exports, module) => {
|
|
604
|
+
var { Writable } = __require("stream");
|
|
605
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
606
|
+
var {
|
|
607
|
+
BINARY_TYPES,
|
|
608
|
+
EMPTY_BUFFER,
|
|
609
|
+
kStatusCode,
|
|
610
|
+
kWebSocket
|
|
611
|
+
} = require_constants();
|
|
612
|
+
var { concat, toArrayBuffer, unmask } = require_buffer_util();
|
|
613
|
+
var { isValidStatusCode, isValidUTF8 } = require_validation();
|
|
614
|
+
var FastBuffer = Buffer[Symbol.species];
|
|
615
|
+
var GET_INFO = 0;
|
|
616
|
+
var GET_PAYLOAD_LENGTH_16 = 1;
|
|
617
|
+
var GET_PAYLOAD_LENGTH_64 = 2;
|
|
618
|
+
var GET_MASK = 3;
|
|
619
|
+
var GET_DATA = 4;
|
|
620
|
+
var INFLATING = 5;
|
|
621
|
+
var DEFER_EVENT = 6;
|
|
622
|
+
|
|
623
|
+
class Receiver extends Writable {
|
|
624
|
+
constructor(options = {}) {
|
|
625
|
+
super();
|
|
626
|
+
this._allowSynchronousEvents = options.allowSynchronousEvents !== undefined ? options.allowSynchronousEvents : true;
|
|
627
|
+
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
628
|
+
this._extensions = options.extensions || {};
|
|
629
|
+
this._isServer = !!options.isServer;
|
|
630
|
+
this._maxPayload = options.maxPayload | 0;
|
|
631
|
+
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
632
|
+
this[kWebSocket] = undefined;
|
|
633
|
+
this._bufferedBytes = 0;
|
|
634
|
+
this._buffers = [];
|
|
635
|
+
this._compressed = false;
|
|
636
|
+
this._payloadLength = 0;
|
|
637
|
+
this._mask = undefined;
|
|
638
|
+
this._fragmented = 0;
|
|
639
|
+
this._masked = false;
|
|
640
|
+
this._fin = false;
|
|
641
|
+
this._opcode = 0;
|
|
642
|
+
this._totalPayloadLength = 0;
|
|
643
|
+
this._messageLength = 0;
|
|
644
|
+
this._fragments = [];
|
|
645
|
+
this._errored = false;
|
|
646
|
+
this._loop = false;
|
|
647
|
+
this._state = GET_INFO;
|
|
648
|
+
}
|
|
649
|
+
_write(chunk, encoding, cb) {
|
|
650
|
+
if (this._opcode === 8 && this._state == GET_INFO)
|
|
651
|
+
return cb();
|
|
652
|
+
this._bufferedBytes += chunk.length;
|
|
653
|
+
this._buffers.push(chunk);
|
|
654
|
+
this.startLoop(cb);
|
|
655
|
+
}
|
|
656
|
+
consume(n) {
|
|
657
|
+
this._bufferedBytes -= n;
|
|
658
|
+
if (n === this._buffers[0].length)
|
|
659
|
+
return this._buffers.shift();
|
|
660
|
+
if (n < this._buffers[0].length) {
|
|
661
|
+
const buf = this._buffers[0];
|
|
662
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
663
|
+
return new FastBuffer(buf.buffer, buf.byteOffset, n);
|
|
664
|
+
}
|
|
665
|
+
const dst = Buffer.allocUnsafe(n);
|
|
666
|
+
do {
|
|
667
|
+
const buf = this._buffers[0];
|
|
668
|
+
const offset = dst.length - n;
|
|
669
|
+
if (n >= buf.length) {
|
|
670
|
+
dst.set(this._buffers.shift(), offset);
|
|
671
|
+
} else {
|
|
672
|
+
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
|
673
|
+
this._buffers[0] = new FastBuffer(buf.buffer, buf.byteOffset + n, buf.length - n);
|
|
674
|
+
}
|
|
675
|
+
n -= buf.length;
|
|
676
|
+
} while (n > 0);
|
|
677
|
+
return dst;
|
|
678
|
+
}
|
|
679
|
+
startLoop(cb) {
|
|
680
|
+
this._loop = true;
|
|
681
|
+
do {
|
|
682
|
+
switch (this._state) {
|
|
683
|
+
case GET_INFO:
|
|
684
|
+
this.getInfo(cb);
|
|
685
|
+
break;
|
|
686
|
+
case GET_PAYLOAD_LENGTH_16:
|
|
687
|
+
this.getPayloadLength16(cb);
|
|
688
|
+
break;
|
|
689
|
+
case GET_PAYLOAD_LENGTH_64:
|
|
690
|
+
this.getPayloadLength64(cb);
|
|
691
|
+
break;
|
|
692
|
+
case GET_MASK:
|
|
693
|
+
this.getMask();
|
|
694
|
+
break;
|
|
695
|
+
case GET_DATA:
|
|
696
|
+
this.getData(cb);
|
|
697
|
+
break;
|
|
698
|
+
case INFLATING:
|
|
699
|
+
case DEFER_EVENT:
|
|
700
|
+
this._loop = false;
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
} while (this._loop);
|
|
704
|
+
if (!this._errored)
|
|
705
|
+
cb();
|
|
706
|
+
}
|
|
707
|
+
getInfo(cb) {
|
|
708
|
+
if (this._bufferedBytes < 2) {
|
|
709
|
+
this._loop = false;
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
const buf = this.consume(2);
|
|
713
|
+
if ((buf[0] & 48) !== 0) {
|
|
714
|
+
const error = this.createError(RangeError, "RSV2 and RSV3 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_2_3");
|
|
715
|
+
cb(error);
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
const compressed = (buf[0] & 64) === 64;
|
|
719
|
+
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
|
720
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
721
|
+
cb(error);
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
this._fin = (buf[0] & 128) === 128;
|
|
725
|
+
this._opcode = buf[0] & 15;
|
|
726
|
+
this._payloadLength = buf[1] & 127;
|
|
727
|
+
if (this._opcode === 0) {
|
|
728
|
+
if (compressed) {
|
|
729
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
730
|
+
cb(error);
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
if (!this._fragmented) {
|
|
734
|
+
const error = this.createError(RangeError, "invalid opcode 0", true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
735
|
+
cb(error);
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
this._opcode = this._fragmented;
|
|
739
|
+
} else if (this._opcode === 1 || this._opcode === 2) {
|
|
740
|
+
if (this._fragmented) {
|
|
741
|
+
const error = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
742
|
+
cb(error);
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
this._compressed = compressed;
|
|
746
|
+
} else if (this._opcode > 7 && this._opcode < 11) {
|
|
747
|
+
if (!this._fin) {
|
|
748
|
+
const error = this.createError(RangeError, "FIN must be set", true, 1002, "WS_ERR_EXPECTED_FIN");
|
|
749
|
+
cb(error);
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
if (compressed) {
|
|
753
|
+
const error = this.createError(RangeError, "RSV1 must be clear", true, 1002, "WS_ERR_UNEXPECTED_RSV_1");
|
|
754
|
+
cb(error);
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1) {
|
|
758
|
+
const error = this.createError(RangeError, `invalid payload length ${this._payloadLength}`, true, 1002, "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");
|
|
759
|
+
cb(error);
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
} else {
|
|
763
|
+
const error = this.createError(RangeError, `invalid opcode ${this._opcode}`, true, 1002, "WS_ERR_INVALID_OPCODE");
|
|
764
|
+
cb(error);
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
if (!this._fin && !this._fragmented)
|
|
768
|
+
this._fragmented = this._opcode;
|
|
769
|
+
this._masked = (buf[1] & 128) === 128;
|
|
770
|
+
if (this._isServer) {
|
|
771
|
+
if (!this._masked) {
|
|
772
|
+
const error = this.createError(RangeError, "MASK must be set", true, 1002, "WS_ERR_EXPECTED_MASK");
|
|
773
|
+
cb(error);
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
} else if (this._masked) {
|
|
777
|
+
const error = this.createError(RangeError, "MASK must be clear", true, 1002, "WS_ERR_UNEXPECTED_MASK");
|
|
778
|
+
cb(error);
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
if (this._payloadLength === 126)
|
|
782
|
+
this._state = GET_PAYLOAD_LENGTH_16;
|
|
783
|
+
else if (this._payloadLength === 127)
|
|
784
|
+
this._state = GET_PAYLOAD_LENGTH_64;
|
|
785
|
+
else
|
|
786
|
+
this.haveLength(cb);
|
|
787
|
+
}
|
|
788
|
+
getPayloadLength16(cb) {
|
|
789
|
+
if (this._bufferedBytes < 2) {
|
|
790
|
+
this._loop = false;
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
this._payloadLength = this.consume(2).readUInt16BE(0);
|
|
794
|
+
this.haveLength(cb);
|
|
795
|
+
}
|
|
796
|
+
getPayloadLength64(cb) {
|
|
797
|
+
if (this._bufferedBytes < 8) {
|
|
798
|
+
this._loop = false;
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
const buf = this.consume(8);
|
|
802
|
+
const num = buf.readUInt32BE(0);
|
|
803
|
+
if (num > Math.pow(2, 53 - 32) - 1) {
|
|
804
|
+
const error = this.createError(RangeError, "Unsupported WebSocket frame: payload length > 2^53 - 1", false, 1009, "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");
|
|
805
|
+
cb(error);
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
|
809
|
+
this.haveLength(cb);
|
|
810
|
+
}
|
|
811
|
+
haveLength(cb) {
|
|
812
|
+
if (this._payloadLength && this._opcode < 8) {
|
|
813
|
+
this._totalPayloadLength += this._payloadLength;
|
|
814
|
+
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
|
815
|
+
const error = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
816
|
+
cb(error);
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
if (this._masked)
|
|
821
|
+
this._state = GET_MASK;
|
|
822
|
+
else
|
|
823
|
+
this._state = GET_DATA;
|
|
824
|
+
}
|
|
825
|
+
getMask() {
|
|
826
|
+
if (this._bufferedBytes < 4) {
|
|
827
|
+
this._loop = false;
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
this._mask = this.consume(4);
|
|
831
|
+
this._state = GET_DATA;
|
|
832
|
+
}
|
|
833
|
+
getData(cb) {
|
|
834
|
+
let data = EMPTY_BUFFER;
|
|
835
|
+
if (this._payloadLength) {
|
|
836
|
+
if (this._bufferedBytes < this._payloadLength) {
|
|
837
|
+
this._loop = false;
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
data = this.consume(this._payloadLength);
|
|
841
|
+
if (this._masked && (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0) {
|
|
842
|
+
unmask(data, this._mask);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (this._opcode > 7) {
|
|
846
|
+
this.controlMessage(data, cb);
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (this._compressed) {
|
|
850
|
+
this._state = INFLATING;
|
|
851
|
+
this.decompress(data, cb);
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
if (data.length) {
|
|
855
|
+
this._messageLength = this._totalPayloadLength;
|
|
856
|
+
this._fragments.push(data);
|
|
857
|
+
}
|
|
858
|
+
this.dataMessage(cb);
|
|
859
|
+
}
|
|
860
|
+
decompress(data, cb) {
|
|
861
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
862
|
+
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
|
863
|
+
if (err)
|
|
864
|
+
return cb(err);
|
|
865
|
+
if (buf.length) {
|
|
866
|
+
this._messageLength += buf.length;
|
|
867
|
+
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
|
868
|
+
const error = this.createError(RangeError, "Max payload size exceeded", false, 1009, "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");
|
|
869
|
+
cb(error);
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
this._fragments.push(buf);
|
|
873
|
+
}
|
|
874
|
+
this.dataMessage(cb);
|
|
875
|
+
if (this._state === GET_INFO)
|
|
876
|
+
this.startLoop(cb);
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
dataMessage(cb) {
|
|
880
|
+
if (!this._fin) {
|
|
881
|
+
this._state = GET_INFO;
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
const messageLength = this._messageLength;
|
|
885
|
+
const fragments = this._fragments;
|
|
886
|
+
this._totalPayloadLength = 0;
|
|
887
|
+
this._messageLength = 0;
|
|
888
|
+
this._fragmented = 0;
|
|
889
|
+
this._fragments = [];
|
|
890
|
+
if (this._opcode === 2) {
|
|
891
|
+
let data;
|
|
892
|
+
if (this._binaryType === "nodebuffer") {
|
|
893
|
+
data = concat(fragments, messageLength);
|
|
894
|
+
} else if (this._binaryType === "arraybuffer") {
|
|
895
|
+
data = toArrayBuffer(concat(fragments, messageLength));
|
|
896
|
+
} else if (this._binaryType === "blob") {
|
|
897
|
+
data = new Blob(fragments);
|
|
898
|
+
} else {
|
|
899
|
+
data = fragments;
|
|
900
|
+
}
|
|
901
|
+
if (this._allowSynchronousEvents) {
|
|
902
|
+
this.emit("message", data, true);
|
|
903
|
+
this._state = GET_INFO;
|
|
904
|
+
} else {
|
|
905
|
+
this._state = DEFER_EVENT;
|
|
906
|
+
setImmediate(() => {
|
|
907
|
+
this.emit("message", data, true);
|
|
908
|
+
this._state = GET_INFO;
|
|
909
|
+
this.startLoop(cb);
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
} else {
|
|
913
|
+
const buf = concat(fragments, messageLength);
|
|
914
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
915
|
+
const error = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
916
|
+
cb(error);
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
if (this._state === INFLATING || this._allowSynchronousEvents) {
|
|
920
|
+
this.emit("message", buf, false);
|
|
921
|
+
this._state = GET_INFO;
|
|
922
|
+
} else {
|
|
923
|
+
this._state = DEFER_EVENT;
|
|
924
|
+
setImmediate(() => {
|
|
925
|
+
this.emit("message", buf, false);
|
|
926
|
+
this._state = GET_INFO;
|
|
927
|
+
this.startLoop(cb);
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
controlMessage(data, cb) {
|
|
933
|
+
if (this._opcode === 8) {
|
|
934
|
+
if (data.length === 0) {
|
|
935
|
+
this._loop = false;
|
|
936
|
+
this.emit("conclude", 1005, EMPTY_BUFFER);
|
|
937
|
+
this.end();
|
|
938
|
+
} else {
|
|
939
|
+
const code = data.readUInt16BE(0);
|
|
940
|
+
if (!isValidStatusCode(code)) {
|
|
941
|
+
const error = this.createError(RangeError, `invalid status code ${code}`, true, 1002, "WS_ERR_INVALID_CLOSE_CODE");
|
|
942
|
+
cb(error);
|
|
943
|
+
return;
|
|
944
|
+
}
|
|
945
|
+
const buf = new FastBuffer(data.buffer, data.byteOffset + 2, data.length - 2);
|
|
946
|
+
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
|
947
|
+
const error = this.createError(Error, "invalid UTF-8 sequence", true, 1007, "WS_ERR_INVALID_UTF8");
|
|
948
|
+
cb(error);
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
this._loop = false;
|
|
952
|
+
this.emit("conclude", code, buf);
|
|
953
|
+
this.end();
|
|
954
|
+
}
|
|
955
|
+
this._state = GET_INFO;
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
if (this._allowSynchronousEvents) {
|
|
959
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
960
|
+
this._state = GET_INFO;
|
|
961
|
+
} else {
|
|
962
|
+
this._state = DEFER_EVENT;
|
|
963
|
+
setImmediate(() => {
|
|
964
|
+
this.emit(this._opcode === 9 ? "ping" : "pong", data);
|
|
965
|
+
this._state = GET_INFO;
|
|
966
|
+
this.startLoop(cb);
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
createError(ErrorCtor, message, prefix, statusCode, errorCode) {
|
|
971
|
+
this._loop = false;
|
|
972
|
+
this._errored = true;
|
|
973
|
+
const err = new ErrorCtor(prefix ? `Invalid WebSocket frame: ${message}` : message);
|
|
974
|
+
Error.captureStackTrace(err, this.createError);
|
|
975
|
+
err.code = errorCode;
|
|
976
|
+
err[kStatusCode] = statusCode;
|
|
977
|
+
return err;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
module.exports = Receiver;
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/sender.js
|
|
984
|
+
var require_sender = __commonJS((exports, module) => {
|
|
985
|
+
var { Duplex } = __require("stream");
|
|
986
|
+
var { randomFillSync } = __require("crypto");
|
|
987
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
988
|
+
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
|
|
989
|
+
var { isBlob, isValidStatusCode } = require_validation();
|
|
990
|
+
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
991
|
+
var kByteLength = Symbol("kByteLength");
|
|
992
|
+
var maskBuffer = Buffer.alloc(4);
|
|
993
|
+
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
994
|
+
var randomPool;
|
|
995
|
+
var randomPoolPointer = RANDOM_POOL_SIZE;
|
|
996
|
+
var DEFAULT = 0;
|
|
997
|
+
var DEFLATING = 1;
|
|
998
|
+
var GET_BLOB_DATA = 2;
|
|
999
|
+
|
|
1000
|
+
class Sender {
|
|
1001
|
+
constructor(socket, extensions, generateMask) {
|
|
1002
|
+
this._extensions = extensions || {};
|
|
1003
|
+
if (generateMask) {
|
|
1004
|
+
this._generateMask = generateMask;
|
|
1005
|
+
this._maskBuffer = Buffer.alloc(4);
|
|
1006
|
+
}
|
|
1007
|
+
this._socket = socket;
|
|
1008
|
+
this._firstFragment = true;
|
|
1009
|
+
this._compress = false;
|
|
1010
|
+
this._bufferedBytes = 0;
|
|
1011
|
+
this._queue = [];
|
|
1012
|
+
this._state = DEFAULT;
|
|
1013
|
+
this.onerror = NOOP;
|
|
1014
|
+
this[kWebSocket] = undefined;
|
|
1015
|
+
}
|
|
1016
|
+
static frame(data, options) {
|
|
1017
|
+
let mask;
|
|
1018
|
+
let merge = false;
|
|
1019
|
+
let offset = 2;
|
|
1020
|
+
let skipMasking = false;
|
|
1021
|
+
if (options.mask) {
|
|
1022
|
+
mask = options.maskBuffer || maskBuffer;
|
|
1023
|
+
if (options.generateMask) {
|
|
1024
|
+
options.generateMask(mask);
|
|
1025
|
+
} else {
|
|
1026
|
+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
|
|
1027
|
+
if (randomPool === undefined) {
|
|
1028
|
+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
|
|
1029
|
+
}
|
|
1030
|
+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
|
|
1031
|
+
randomPoolPointer = 0;
|
|
1032
|
+
}
|
|
1033
|
+
mask[0] = randomPool[randomPoolPointer++];
|
|
1034
|
+
mask[1] = randomPool[randomPoolPointer++];
|
|
1035
|
+
mask[2] = randomPool[randomPoolPointer++];
|
|
1036
|
+
mask[3] = randomPool[randomPoolPointer++];
|
|
1037
|
+
}
|
|
1038
|
+
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
|
1039
|
+
offset = 6;
|
|
1040
|
+
}
|
|
1041
|
+
let dataLength;
|
|
1042
|
+
if (typeof data === "string") {
|
|
1043
|
+
if ((!options.mask || skipMasking) && options[kByteLength] !== undefined) {
|
|
1044
|
+
dataLength = options[kByteLength];
|
|
1045
|
+
} else {
|
|
1046
|
+
data = Buffer.from(data);
|
|
1047
|
+
dataLength = data.length;
|
|
1048
|
+
}
|
|
1049
|
+
} else {
|
|
1050
|
+
dataLength = data.length;
|
|
1051
|
+
merge = options.mask && options.readOnly && !skipMasking;
|
|
1052
|
+
}
|
|
1053
|
+
let payloadLength = dataLength;
|
|
1054
|
+
if (dataLength >= 65536) {
|
|
1055
|
+
offset += 8;
|
|
1056
|
+
payloadLength = 127;
|
|
1057
|
+
} else if (dataLength > 125) {
|
|
1058
|
+
offset += 2;
|
|
1059
|
+
payloadLength = 126;
|
|
1060
|
+
}
|
|
1061
|
+
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
|
1062
|
+
target[0] = options.fin ? options.opcode | 128 : options.opcode;
|
|
1063
|
+
if (options.rsv1)
|
|
1064
|
+
target[0] |= 64;
|
|
1065
|
+
target[1] = payloadLength;
|
|
1066
|
+
if (payloadLength === 126) {
|
|
1067
|
+
target.writeUInt16BE(dataLength, 2);
|
|
1068
|
+
} else if (payloadLength === 127) {
|
|
1069
|
+
target[2] = target[3] = 0;
|
|
1070
|
+
target.writeUIntBE(dataLength, 4, 6);
|
|
1071
|
+
}
|
|
1072
|
+
if (!options.mask)
|
|
1073
|
+
return [target, data];
|
|
1074
|
+
target[1] |= 128;
|
|
1075
|
+
target[offset - 4] = mask[0];
|
|
1076
|
+
target[offset - 3] = mask[1];
|
|
1077
|
+
target[offset - 2] = mask[2];
|
|
1078
|
+
target[offset - 1] = mask[3];
|
|
1079
|
+
if (skipMasking)
|
|
1080
|
+
return [target, data];
|
|
1081
|
+
if (merge) {
|
|
1082
|
+
applyMask(data, mask, target, offset, dataLength);
|
|
1083
|
+
return [target];
|
|
1084
|
+
}
|
|
1085
|
+
applyMask(data, mask, data, 0, dataLength);
|
|
1086
|
+
return [target, data];
|
|
1087
|
+
}
|
|
1088
|
+
close(code, data, mask, cb) {
|
|
1089
|
+
let buf;
|
|
1090
|
+
if (code === undefined) {
|
|
1091
|
+
buf = EMPTY_BUFFER;
|
|
1092
|
+
} else if (typeof code !== "number" || !isValidStatusCode(code)) {
|
|
1093
|
+
throw new TypeError("First argument must be a valid error code number");
|
|
1094
|
+
} else if (data === undefined || !data.length) {
|
|
1095
|
+
buf = Buffer.allocUnsafe(2);
|
|
1096
|
+
buf.writeUInt16BE(code, 0);
|
|
1097
|
+
} else {
|
|
1098
|
+
const length = Buffer.byteLength(data);
|
|
1099
|
+
if (length > 123) {
|
|
1100
|
+
throw new RangeError("The message must not be greater than 123 bytes");
|
|
1101
|
+
}
|
|
1102
|
+
buf = Buffer.allocUnsafe(2 + length);
|
|
1103
|
+
buf.writeUInt16BE(code, 0);
|
|
1104
|
+
if (typeof data === "string") {
|
|
1105
|
+
buf.write(data, 2);
|
|
1106
|
+
} else {
|
|
1107
|
+
buf.set(data, 2);
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
const options = {
|
|
1111
|
+
[kByteLength]: buf.length,
|
|
1112
|
+
fin: true,
|
|
1113
|
+
generateMask: this._generateMask,
|
|
1114
|
+
mask,
|
|
1115
|
+
maskBuffer: this._maskBuffer,
|
|
1116
|
+
opcode: 8,
|
|
1117
|
+
readOnly: false,
|
|
1118
|
+
rsv1: false
|
|
1119
|
+
};
|
|
1120
|
+
if (this._state !== DEFAULT) {
|
|
1121
|
+
this.enqueue([this.dispatch, buf, false, options, cb]);
|
|
1122
|
+
} else {
|
|
1123
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
ping(data, mask, cb) {
|
|
1127
|
+
let byteLength;
|
|
1128
|
+
let readOnly;
|
|
1129
|
+
if (typeof data === "string") {
|
|
1130
|
+
byteLength = Buffer.byteLength(data);
|
|
1131
|
+
readOnly = false;
|
|
1132
|
+
} else if (isBlob(data)) {
|
|
1133
|
+
byteLength = data.size;
|
|
1134
|
+
readOnly = false;
|
|
1135
|
+
} else {
|
|
1136
|
+
data = toBuffer(data);
|
|
1137
|
+
byteLength = data.length;
|
|
1138
|
+
readOnly = toBuffer.readOnly;
|
|
1139
|
+
}
|
|
1140
|
+
if (byteLength > 125) {
|
|
1141
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
1142
|
+
}
|
|
1143
|
+
const options = {
|
|
1144
|
+
[kByteLength]: byteLength,
|
|
1145
|
+
fin: true,
|
|
1146
|
+
generateMask: this._generateMask,
|
|
1147
|
+
mask,
|
|
1148
|
+
maskBuffer: this._maskBuffer,
|
|
1149
|
+
opcode: 9,
|
|
1150
|
+
readOnly,
|
|
1151
|
+
rsv1: false
|
|
1152
|
+
};
|
|
1153
|
+
if (isBlob(data)) {
|
|
1154
|
+
if (this._state !== DEFAULT) {
|
|
1155
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
1156
|
+
} else {
|
|
1157
|
+
this.getBlobData(data, false, options, cb);
|
|
1158
|
+
}
|
|
1159
|
+
} else if (this._state !== DEFAULT) {
|
|
1160
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
1161
|
+
} else {
|
|
1162
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
pong(data, mask, cb) {
|
|
1166
|
+
let byteLength;
|
|
1167
|
+
let readOnly;
|
|
1168
|
+
if (typeof data === "string") {
|
|
1169
|
+
byteLength = Buffer.byteLength(data);
|
|
1170
|
+
readOnly = false;
|
|
1171
|
+
} else if (isBlob(data)) {
|
|
1172
|
+
byteLength = data.size;
|
|
1173
|
+
readOnly = false;
|
|
1174
|
+
} else {
|
|
1175
|
+
data = toBuffer(data);
|
|
1176
|
+
byteLength = data.length;
|
|
1177
|
+
readOnly = toBuffer.readOnly;
|
|
1178
|
+
}
|
|
1179
|
+
if (byteLength > 125) {
|
|
1180
|
+
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
1181
|
+
}
|
|
1182
|
+
const options = {
|
|
1183
|
+
[kByteLength]: byteLength,
|
|
1184
|
+
fin: true,
|
|
1185
|
+
generateMask: this._generateMask,
|
|
1186
|
+
mask,
|
|
1187
|
+
maskBuffer: this._maskBuffer,
|
|
1188
|
+
opcode: 10,
|
|
1189
|
+
readOnly,
|
|
1190
|
+
rsv1: false
|
|
1191
|
+
};
|
|
1192
|
+
if (isBlob(data)) {
|
|
1193
|
+
if (this._state !== DEFAULT) {
|
|
1194
|
+
this.enqueue([this.getBlobData, data, false, options, cb]);
|
|
1195
|
+
} else {
|
|
1196
|
+
this.getBlobData(data, false, options, cb);
|
|
1197
|
+
}
|
|
1198
|
+
} else if (this._state !== DEFAULT) {
|
|
1199
|
+
this.enqueue([this.dispatch, data, false, options, cb]);
|
|
1200
|
+
} else {
|
|
1201
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
send(data, options, cb) {
|
|
1205
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1206
|
+
let opcode = options.binary ? 2 : 1;
|
|
1207
|
+
let rsv1 = options.compress;
|
|
1208
|
+
let byteLength;
|
|
1209
|
+
let readOnly;
|
|
1210
|
+
if (typeof data === "string") {
|
|
1211
|
+
byteLength = Buffer.byteLength(data);
|
|
1212
|
+
readOnly = false;
|
|
1213
|
+
} else if (isBlob(data)) {
|
|
1214
|
+
byteLength = data.size;
|
|
1215
|
+
readOnly = false;
|
|
1216
|
+
} else {
|
|
1217
|
+
data = toBuffer(data);
|
|
1218
|
+
byteLength = data.length;
|
|
1219
|
+
readOnly = toBuffer.readOnly;
|
|
1220
|
+
}
|
|
1221
|
+
if (this._firstFragment) {
|
|
1222
|
+
this._firstFragment = false;
|
|
1223
|
+
if (rsv1 && perMessageDeflate && perMessageDeflate.params[perMessageDeflate._isServer ? "server_no_context_takeover" : "client_no_context_takeover"]) {
|
|
1224
|
+
rsv1 = byteLength >= perMessageDeflate._threshold;
|
|
1225
|
+
}
|
|
1226
|
+
this._compress = rsv1;
|
|
1227
|
+
} else {
|
|
1228
|
+
rsv1 = false;
|
|
1229
|
+
opcode = 0;
|
|
1230
|
+
}
|
|
1231
|
+
if (options.fin)
|
|
1232
|
+
this._firstFragment = true;
|
|
1233
|
+
const opts = {
|
|
1234
|
+
[kByteLength]: byteLength,
|
|
1235
|
+
fin: options.fin,
|
|
1236
|
+
generateMask: this._generateMask,
|
|
1237
|
+
mask: options.mask,
|
|
1238
|
+
maskBuffer: this._maskBuffer,
|
|
1239
|
+
opcode,
|
|
1240
|
+
readOnly,
|
|
1241
|
+
rsv1
|
|
1242
|
+
};
|
|
1243
|
+
if (isBlob(data)) {
|
|
1244
|
+
if (this._state !== DEFAULT) {
|
|
1245
|
+
this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
|
|
1246
|
+
} else {
|
|
1247
|
+
this.getBlobData(data, this._compress, opts, cb);
|
|
1248
|
+
}
|
|
1249
|
+
} else if (this._state !== DEFAULT) {
|
|
1250
|
+
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
|
1251
|
+
} else {
|
|
1252
|
+
this.dispatch(data, this._compress, opts, cb);
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
getBlobData(blob, compress, options, cb) {
|
|
1256
|
+
this._bufferedBytes += options[kByteLength];
|
|
1257
|
+
this._state = GET_BLOB_DATA;
|
|
1258
|
+
blob.arrayBuffer().then((arrayBuffer) => {
|
|
1259
|
+
if (this._socket.destroyed) {
|
|
1260
|
+
const err = new Error("The socket was closed while the blob was being read");
|
|
1261
|
+
process.nextTick(callCallbacks, this, err, cb);
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
this._bufferedBytes -= options[kByteLength];
|
|
1265
|
+
const data = toBuffer(arrayBuffer);
|
|
1266
|
+
if (!compress) {
|
|
1267
|
+
this._state = DEFAULT;
|
|
1268
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
1269
|
+
this.dequeue();
|
|
1270
|
+
} else {
|
|
1271
|
+
this.dispatch(data, compress, options, cb);
|
|
1272
|
+
}
|
|
1273
|
+
}).catch((err) => {
|
|
1274
|
+
process.nextTick(onError, this, err, cb);
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
dispatch(data, compress, options, cb) {
|
|
1278
|
+
if (!compress) {
|
|
1279
|
+
this.sendFrame(Sender.frame(data, options), cb);
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
|
1283
|
+
this._bufferedBytes += options[kByteLength];
|
|
1284
|
+
this._state = DEFLATING;
|
|
1285
|
+
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
|
1286
|
+
if (this._socket.destroyed) {
|
|
1287
|
+
const err = new Error("The socket was closed while data was being compressed");
|
|
1288
|
+
callCallbacks(this, err, cb);
|
|
1289
|
+
return;
|
|
1290
|
+
}
|
|
1291
|
+
this._bufferedBytes -= options[kByteLength];
|
|
1292
|
+
this._state = DEFAULT;
|
|
1293
|
+
options.readOnly = false;
|
|
1294
|
+
this.sendFrame(Sender.frame(buf, options), cb);
|
|
1295
|
+
this.dequeue();
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
dequeue() {
|
|
1299
|
+
while (this._state === DEFAULT && this._queue.length) {
|
|
1300
|
+
const params = this._queue.shift();
|
|
1301
|
+
this._bufferedBytes -= params[3][kByteLength];
|
|
1302
|
+
Reflect.apply(params[0], this, params.slice(1));
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
enqueue(params) {
|
|
1306
|
+
this._bufferedBytes += params[3][kByteLength];
|
|
1307
|
+
this._queue.push(params);
|
|
1308
|
+
}
|
|
1309
|
+
sendFrame(list, cb) {
|
|
1310
|
+
if (list.length === 2) {
|
|
1311
|
+
this._socket.cork();
|
|
1312
|
+
this._socket.write(list[0]);
|
|
1313
|
+
this._socket.write(list[1], cb);
|
|
1314
|
+
this._socket.uncork();
|
|
1315
|
+
} else {
|
|
1316
|
+
this._socket.write(list[0], cb);
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
module.exports = Sender;
|
|
1321
|
+
function callCallbacks(sender, err, cb) {
|
|
1322
|
+
if (typeof cb === "function")
|
|
1323
|
+
cb(err);
|
|
1324
|
+
for (let i = 0;i < sender._queue.length; i++) {
|
|
1325
|
+
const params = sender._queue[i];
|
|
1326
|
+
const callback = params[params.length - 1];
|
|
1327
|
+
if (typeof callback === "function")
|
|
1328
|
+
callback(err);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
function onError(sender, err, cb) {
|
|
1332
|
+
callCallbacks(sender, err, cb);
|
|
1333
|
+
sender.onerror(err);
|
|
1334
|
+
}
|
|
1335
|
+
});
|
|
1336
|
+
|
|
1337
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/event-target.js
|
|
1338
|
+
var require_event_target = __commonJS((exports, module) => {
|
|
1339
|
+
var { kForOnEventAttribute, kListener } = require_constants();
|
|
1340
|
+
var kCode = Symbol("kCode");
|
|
1341
|
+
var kData = Symbol("kData");
|
|
1342
|
+
var kError = Symbol("kError");
|
|
1343
|
+
var kMessage = Symbol("kMessage");
|
|
1344
|
+
var kReason = Symbol("kReason");
|
|
1345
|
+
var kTarget = Symbol("kTarget");
|
|
1346
|
+
var kType = Symbol("kType");
|
|
1347
|
+
var kWasClean = Symbol("kWasClean");
|
|
1348
|
+
|
|
1349
|
+
class Event {
|
|
1350
|
+
constructor(type) {
|
|
1351
|
+
this[kTarget] = null;
|
|
1352
|
+
this[kType] = type;
|
|
1353
|
+
}
|
|
1354
|
+
get target() {
|
|
1355
|
+
return this[kTarget];
|
|
1356
|
+
}
|
|
1357
|
+
get type() {
|
|
1358
|
+
return this[kType];
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
Object.defineProperty(Event.prototype, "target", { enumerable: true });
|
|
1362
|
+
Object.defineProperty(Event.prototype, "type", { enumerable: true });
|
|
1363
|
+
|
|
1364
|
+
class CloseEvent extends Event {
|
|
1365
|
+
constructor(type, options = {}) {
|
|
1366
|
+
super(type);
|
|
1367
|
+
this[kCode] = options.code === undefined ? 0 : options.code;
|
|
1368
|
+
this[kReason] = options.reason === undefined ? "" : options.reason;
|
|
1369
|
+
this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
|
|
1370
|
+
}
|
|
1371
|
+
get code() {
|
|
1372
|
+
return this[kCode];
|
|
1373
|
+
}
|
|
1374
|
+
get reason() {
|
|
1375
|
+
return this[kReason];
|
|
1376
|
+
}
|
|
1377
|
+
get wasClean() {
|
|
1378
|
+
return this[kWasClean];
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
Object.defineProperty(CloseEvent.prototype, "code", { enumerable: true });
|
|
1382
|
+
Object.defineProperty(CloseEvent.prototype, "reason", { enumerable: true });
|
|
1383
|
+
Object.defineProperty(CloseEvent.prototype, "wasClean", { enumerable: true });
|
|
1384
|
+
|
|
1385
|
+
class ErrorEvent extends Event {
|
|
1386
|
+
constructor(type, options = {}) {
|
|
1387
|
+
super(type);
|
|
1388
|
+
this[kError] = options.error === undefined ? null : options.error;
|
|
1389
|
+
this[kMessage] = options.message === undefined ? "" : options.message;
|
|
1390
|
+
}
|
|
1391
|
+
get error() {
|
|
1392
|
+
return this[kError];
|
|
1393
|
+
}
|
|
1394
|
+
get message() {
|
|
1395
|
+
return this[kMessage];
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
Object.defineProperty(ErrorEvent.prototype, "error", { enumerable: true });
|
|
1399
|
+
Object.defineProperty(ErrorEvent.prototype, "message", { enumerable: true });
|
|
1400
|
+
|
|
1401
|
+
class MessageEvent extends Event {
|
|
1402
|
+
constructor(type, options = {}) {
|
|
1403
|
+
super(type);
|
|
1404
|
+
this[kData] = options.data === undefined ? null : options.data;
|
|
1405
|
+
}
|
|
1406
|
+
get data() {
|
|
1407
|
+
return this[kData];
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
Object.defineProperty(MessageEvent.prototype, "data", { enumerable: true });
|
|
1411
|
+
var EventTarget = {
|
|
1412
|
+
addEventListener(type, handler, options = {}) {
|
|
1413
|
+
for (const listener of this.listeners(type)) {
|
|
1414
|
+
if (!options[kForOnEventAttribute] && listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
let wrapper;
|
|
1419
|
+
if (type === "message") {
|
|
1420
|
+
wrapper = function onMessage(data, isBinary) {
|
|
1421
|
+
const event = new MessageEvent("message", {
|
|
1422
|
+
data: isBinary ? data : data.toString()
|
|
1423
|
+
});
|
|
1424
|
+
event[kTarget] = this;
|
|
1425
|
+
callListener(handler, this, event);
|
|
1426
|
+
};
|
|
1427
|
+
} else if (type === "close") {
|
|
1428
|
+
wrapper = function onClose(code, message) {
|
|
1429
|
+
const event = new CloseEvent("close", {
|
|
1430
|
+
code,
|
|
1431
|
+
reason: message.toString(),
|
|
1432
|
+
wasClean: this._closeFrameReceived && this._closeFrameSent
|
|
1433
|
+
});
|
|
1434
|
+
event[kTarget] = this;
|
|
1435
|
+
callListener(handler, this, event);
|
|
1436
|
+
};
|
|
1437
|
+
} else if (type === "error") {
|
|
1438
|
+
wrapper = function onError(error) {
|
|
1439
|
+
const event = new ErrorEvent("error", {
|
|
1440
|
+
error,
|
|
1441
|
+
message: error.message
|
|
1442
|
+
});
|
|
1443
|
+
event[kTarget] = this;
|
|
1444
|
+
callListener(handler, this, event);
|
|
1445
|
+
};
|
|
1446
|
+
} else if (type === "open") {
|
|
1447
|
+
wrapper = function onOpen() {
|
|
1448
|
+
const event = new Event("open");
|
|
1449
|
+
event[kTarget] = this;
|
|
1450
|
+
callListener(handler, this, event);
|
|
1451
|
+
};
|
|
1452
|
+
} else {
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1455
|
+
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
|
1456
|
+
wrapper[kListener] = handler;
|
|
1457
|
+
if (options.once) {
|
|
1458
|
+
this.once(type, wrapper);
|
|
1459
|
+
} else {
|
|
1460
|
+
this.on(type, wrapper);
|
|
1461
|
+
}
|
|
1462
|
+
},
|
|
1463
|
+
removeEventListener(type, handler) {
|
|
1464
|
+
for (const listener of this.listeners(type)) {
|
|
1465
|
+
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
|
1466
|
+
this.removeListener(type, listener);
|
|
1467
|
+
break;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
};
|
|
1472
|
+
module.exports = {
|
|
1473
|
+
CloseEvent,
|
|
1474
|
+
ErrorEvent,
|
|
1475
|
+
Event,
|
|
1476
|
+
EventTarget,
|
|
1477
|
+
MessageEvent
|
|
1478
|
+
};
|
|
1479
|
+
function callListener(listener, thisArg, event) {
|
|
1480
|
+
if (typeof listener === "object" && listener.handleEvent) {
|
|
1481
|
+
listener.handleEvent.call(listener, event);
|
|
1482
|
+
} else {
|
|
1483
|
+
listener.call(thisArg, event);
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
});
|
|
1487
|
+
|
|
1488
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/extension.js
|
|
1489
|
+
var require_extension = __commonJS((exports, module) => {
|
|
1490
|
+
var { tokenChars } = require_validation();
|
|
1491
|
+
function push(dest, name, elem) {
|
|
1492
|
+
if (dest[name] === undefined)
|
|
1493
|
+
dest[name] = [elem];
|
|
1494
|
+
else
|
|
1495
|
+
dest[name].push(elem);
|
|
1496
|
+
}
|
|
1497
|
+
function parse(header) {
|
|
1498
|
+
const offers = Object.create(null);
|
|
1499
|
+
let params = Object.create(null);
|
|
1500
|
+
let mustUnescape = false;
|
|
1501
|
+
let isEscaping = false;
|
|
1502
|
+
let inQuotes = false;
|
|
1503
|
+
let extensionName;
|
|
1504
|
+
let paramName;
|
|
1505
|
+
let start = -1;
|
|
1506
|
+
let code = -1;
|
|
1507
|
+
let end = -1;
|
|
1508
|
+
let i = 0;
|
|
1509
|
+
for (;i < header.length; i++) {
|
|
1510
|
+
code = header.charCodeAt(i);
|
|
1511
|
+
if (extensionName === undefined) {
|
|
1512
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
1513
|
+
if (start === -1)
|
|
1514
|
+
start = i;
|
|
1515
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
1516
|
+
if (end === -1 && start !== -1)
|
|
1517
|
+
end = i;
|
|
1518
|
+
} else if (code === 59 || code === 44) {
|
|
1519
|
+
if (start === -1) {
|
|
1520
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
1521
|
+
}
|
|
1522
|
+
if (end === -1)
|
|
1523
|
+
end = i;
|
|
1524
|
+
const name = header.slice(start, end);
|
|
1525
|
+
if (code === 44) {
|
|
1526
|
+
push(offers, name, params);
|
|
1527
|
+
params = Object.create(null);
|
|
1528
|
+
} else {
|
|
1529
|
+
extensionName = name;
|
|
1530
|
+
}
|
|
1531
|
+
start = end = -1;
|
|
1532
|
+
} else {
|
|
1533
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
1534
|
+
}
|
|
1535
|
+
} else if (paramName === undefined) {
|
|
1536
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
1537
|
+
if (start === -1)
|
|
1538
|
+
start = i;
|
|
1539
|
+
} else if (code === 32 || code === 9) {
|
|
1540
|
+
if (end === -1 && start !== -1)
|
|
1541
|
+
end = i;
|
|
1542
|
+
} else if (code === 59 || code === 44) {
|
|
1543
|
+
if (start === -1) {
|
|
1544
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
1545
|
+
}
|
|
1546
|
+
if (end === -1)
|
|
1547
|
+
end = i;
|
|
1548
|
+
push(params, header.slice(start, end), true);
|
|
1549
|
+
if (code === 44) {
|
|
1550
|
+
push(offers, extensionName, params);
|
|
1551
|
+
params = Object.create(null);
|
|
1552
|
+
extensionName = undefined;
|
|
1553
|
+
}
|
|
1554
|
+
start = end = -1;
|
|
1555
|
+
} else if (code === 61 && start !== -1 && end === -1) {
|
|
1556
|
+
paramName = header.slice(start, i);
|
|
1557
|
+
start = end = -1;
|
|
1558
|
+
} else {
|
|
1559
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
1560
|
+
}
|
|
1561
|
+
} else {
|
|
1562
|
+
if (isEscaping) {
|
|
1563
|
+
if (tokenChars[code] !== 1) {
|
|
1564
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
1565
|
+
}
|
|
1566
|
+
if (start === -1)
|
|
1567
|
+
start = i;
|
|
1568
|
+
else if (!mustUnescape)
|
|
1569
|
+
mustUnescape = true;
|
|
1570
|
+
isEscaping = false;
|
|
1571
|
+
} else if (inQuotes) {
|
|
1572
|
+
if (tokenChars[code] === 1) {
|
|
1573
|
+
if (start === -1)
|
|
1574
|
+
start = i;
|
|
1575
|
+
} else if (code === 34 && start !== -1) {
|
|
1576
|
+
inQuotes = false;
|
|
1577
|
+
end = i;
|
|
1578
|
+
} else if (code === 92) {
|
|
1579
|
+
isEscaping = true;
|
|
1580
|
+
} else {
|
|
1581
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
1582
|
+
}
|
|
1583
|
+
} else if (code === 34 && header.charCodeAt(i - 1) === 61) {
|
|
1584
|
+
inQuotes = true;
|
|
1585
|
+
} else if (end === -1 && tokenChars[code] === 1) {
|
|
1586
|
+
if (start === -1)
|
|
1587
|
+
start = i;
|
|
1588
|
+
} else if (start !== -1 && (code === 32 || code === 9)) {
|
|
1589
|
+
if (end === -1)
|
|
1590
|
+
end = i;
|
|
1591
|
+
} else if (code === 59 || code === 44) {
|
|
1592
|
+
if (start === -1) {
|
|
1593
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
1594
|
+
}
|
|
1595
|
+
if (end === -1)
|
|
1596
|
+
end = i;
|
|
1597
|
+
let value = header.slice(start, end);
|
|
1598
|
+
if (mustUnescape) {
|
|
1599
|
+
value = value.replace(/\\/g, "");
|
|
1600
|
+
mustUnescape = false;
|
|
1601
|
+
}
|
|
1602
|
+
push(params, paramName, value);
|
|
1603
|
+
if (code === 44) {
|
|
1604
|
+
push(offers, extensionName, params);
|
|
1605
|
+
params = Object.create(null);
|
|
1606
|
+
extensionName = undefined;
|
|
1607
|
+
}
|
|
1608
|
+
paramName = undefined;
|
|
1609
|
+
start = end = -1;
|
|
1610
|
+
} else {
|
|
1611
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
if (start === -1 || inQuotes || code === 32 || code === 9) {
|
|
1616
|
+
throw new SyntaxError("Unexpected end of input");
|
|
1617
|
+
}
|
|
1618
|
+
if (end === -1)
|
|
1619
|
+
end = i;
|
|
1620
|
+
const token = header.slice(start, end);
|
|
1621
|
+
if (extensionName === undefined) {
|
|
1622
|
+
push(offers, token, params);
|
|
1623
|
+
} else {
|
|
1624
|
+
if (paramName === undefined) {
|
|
1625
|
+
push(params, token, true);
|
|
1626
|
+
} else if (mustUnescape) {
|
|
1627
|
+
push(params, paramName, token.replace(/\\/g, ""));
|
|
1628
|
+
} else {
|
|
1629
|
+
push(params, paramName, token);
|
|
1630
|
+
}
|
|
1631
|
+
push(offers, extensionName, params);
|
|
1632
|
+
}
|
|
1633
|
+
return offers;
|
|
1634
|
+
}
|
|
1635
|
+
function format(extensions) {
|
|
1636
|
+
return Object.keys(extensions).map((extension) => {
|
|
1637
|
+
let configurations = extensions[extension];
|
|
1638
|
+
if (!Array.isArray(configurations))
|
|
1639
|
+
configurations = [configurations];
|
|
1640
|
+
return configurations.map((params) => {
|
|
1641
|
+
return [extension].concat(Object.keys(params).map((k) => {
|
|
1642
|
+
let values = params[k];
|
|
1643
|
+
if (!Array.isArray(values))
|
|
1644
|
+
values = [values];
|
|
1645
|
+
return values.map((v) => v === true ? k : `${k}=${v}`).join("; ");
|
|
1646
|
+
})).join("; ");
|
|
1647
|
+
}).join(", ");
|
|
1648
|
+
}).join(", ");
|
|
1649
|
+
}
|
|
1650
|
+
module.exports = { format, parse };
|
|
1651
|
+
});
|
|
1652
|
+
|
|
1653
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/websocket.js
|
|
1654
|
+
var require_websocket = __commonJS((exports, module) => {
|
|
1655
|
+
var EventEmitter = __require("events");
|
|
1656
|
+
var https = __require("https");
|
|
1657
|
+
var http = __require("http");
|
|
1658
|
+
var net = __require("net");
|
|
1659
|
+
var tls = __require("tls");
|
|
1660
|
+
var { randomBytes, createHash } = __require("crypto");
|
|
1661
|
+
var { Duplex, Readable } = __require("stream");
|
|
1662
|
+
var { URL } = __require("url");
|
|
1663
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
1664
|
+
var Receiver = require_receiver();
|
|
1665
|
+
var Sender = require_sender();
|
|
1666
|
+
var { isBlob } = require_validation();
|
|
1667
|
+
var {
|
|
1668
|
+
BINARY_TYPES,
|
|
1669
|
+
CLOSE_TIMEOUT,
|
|
1670
|
+
EMPTY_BUFFER,
|
|
1671
|
+
GUID,
|
|
1672
|
+
kForOnEventAttribute,
|
|
1673
|
+
kListener,
|
|
1674
|
+
kStatusCode,
|
|
1675
|
+
kWebSocket,
|
|
1676
|
+
NOOP
|
|
1677
|
+
} = require_constants();
|
|
1678
|
+
var {
|
|
1679
|
+
EventTarget: { addEventListener, removeEventListener }
|
|
1680
|
+
} = require_event_target();
|
|
1681
|
+
var { format, parse } = require_extension();
|
|
1682
|
+
var { toBuffer } = require_buffer_util();
|
|
1683
|
+
var kAborted = Symbol("kAborted");
|
|
1684
|
+
var protocolVersions = [8, 13];
|
|
1685
|
+
var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
1686
|
+
var subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
|
|
1687
|
+
|
|
1688
|
+
class WebSocket extends EventEmitter {
|
|
1689
|
+
constructor(address, protocols, options) {
|
|
1690
|
+
super();
|
|
1691
|
+
this._binaryType = BINARY_TYPES[0];
|
|
1692
|
+
this._closeCode = 1006;
|
|
1693
|
+
this._closeFrameReceived = false;
|
|
1694
|
+
this._closeFrameSent = false;
|
|
1695
|
+
this._closeMessage = EMPTY_BUFFER;
|
|
1696
|
+
this._closeTimer = null;
|
|
1697
|
+
this._errorEmitted = false;
|
|
1698
|
+
this._extensions = {};
|
|
1699
|
+
this._paused = false;
|
|
1700
|
+
this._protocol = "";
|
|
1701
|
+
this._readyState = WebSocket.CONNECTING;
|
|
1702
|
+
this._receiver = null;
|
|
1703
|
+
this._sender = null;
|
|
1704
|
+
this._socket = null;
|
|
1705
|
+
if (address !== null) {
|
|
1706
|
+
this._bufferedAmount = 0;
|
|
1707
|
+
this._isServer = false;
|
|
1708
|
+
this._redirects = 0;
|
|
1709
|
+
if (protocols === undefined) {
|
|
1710
|
+
protocols = [];
|
|
1711
|
+
} else if (!Array.isArray(protocols)) {
|
|
1712
|
+
if (typeof protocols === "object" && protocols !== null) {
|
|
1713
|
+
options = protocols;
|
|
1714
|
+
protocols = [];
|
|
1715
|
+
} else {
|
|
1716
|
+
protocols = [protocols];
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
initAsClient(this, address, protocols, options);
|
|
1720
|
+
} else {
|
|
1721
|
+
this._autoPong = options.autoPong;
|
|
1722
|
+
this._closeTimeout = options.closeTimeout;
|
|
1723
|
+
this._isServer = true;
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
get binaryType() {
|
|
1727
|
+
return this._binaryType;
|
|
1728
|
+
}
|
|
1729
|
+
set binaryType(type) {
|
|
1730
|
+
if (!BINARY_TYPES.includes(type))
|
|
1731
|
+
return;
|
|
1732
|
+
this._binaryType = type;
|
|
1733
|
+
if (this._receiver)
|
|
1734
|
+
this._receiver._binaryType = type;
|
|
1735
|
+
}
|
|
1736
|
+
get bufferedAmount() {
|
|
1737
|
+
if (!this._socket)
|
|
1738
|
+
return this._bufferedAmount;
|
|
1739
|
+
return this._socket._writableState.length + this._sender._bufferedBytes;
|
|
1740
|
+
}
|
|
1741
|
+
get extensions() {
|
|
1742
|
+
return Object.keys(this._extensions).join();
|
|
1743
|
+
}
|
|
1744
|
+
get isPaused() {
|
|
1745
|
+
return this._paused;
|
|
1746
|
+
}
|
|
1747
|
+
get onclose() {
|
|
1748
|
+
return null;
|
|
1749
|
+
}
|
|
1750
|
+
get onerror() {
|
|
1751
|
+
return null;
|
|
1752
|
+
}
|
|
1753
|
+
get onopen() {
|
|
1754
|
+
return null;
|
|
1755
|
+
}
|
|
1756
|
+
get onmessage() {
|
|
1757
|
+
return null;
|
|
1758
|
+
}
|
|
1759
|
+
get protocol() {
|
|
1760
|
+
return this._protocol;
|
|
1761
|
+
}
|
|
1762
|
+
get readyState() {
|
|
1763
|
+
return this._readyState;
|
|
1764
|
+
}
|
|
1765
|
+
get url() {
|
|
1766
|
+
return this._url;
|
|
1767
|
+
}
|
|
1768
|
+
setSocket(socket, head, options) {
|
|
1769
|
+
const receiver = new Receiver({
|
|
1770
|
+
allowSynchronousEvents: options.allowSynchronousEvents,
|
|
1771
|
+
binaryType: this.binaryType,
|
|
1772
|
+
extensions: this._extensions,
|
|
1773
|
+
isServer: this._isServer,
|
|
1774
|
+
maxPayload: options.maxPayload,
|
|
1775
|
+
skipUTF8Validation: options.skipUTF8Validation
|
|
1776
|
+
});
|
|
1777
|
+
const sender = new Sender(socket, this._extensions, options.generateMask);
|
|
1778
|
+
this._receiver = receiver;
|
|
1779
|
+
this._sender = sender;
|
|
1780
|
+
this._socket = socket;
|
|
1781
|
+
receiver[kWebSocket] = this;
|
|
1782
|
+
sender[kWebSocket] = this;
|
|
1783
|
+
socket[kWebSocket] = this;
|
|
1784
|
+
receiver.on("conclude", receiverOnConclude);
|
|
1785
|
+
receiver.on("drain", receiverOnDrain);
|
|
1786
|
+
receiver.on("error", receiverOnError);
|
|
1787
|
+
receiver.on("message", receiverOnMessage);
|
|
1788
|
+
receiver.on("ping", receiverOnPing);
|
|
1789
|
+
receiver.on("pong", receiverOnPong);
|
|
1790
|
+
sender.onerror = senderOnError;
|
|
1791
|
+
if (socket.setTimeout)
|
|
1792
|
+
socket.setTimeout(0);
|
|
1793
|
+
if (socket.setNoDelay)
|
|
1794
|
+
socket.setNoDelay();
|
|
1795
|
+
if (head.length > 0)
|
|
1796
|
+
socket.unshift(head);
|
|
1797
|
+
socket.on("close", socketOnClose);
|
|
1798
|
+
socket.on("data", socketOnData);
|
|
1799
|
+
socket.on("end", socketOnEnd);
|
|
1800
|
+
socket.on("error", socketOnError);
|
|
1801
|
+
this._readyState = WebSocket.OPEN;
|
|
1802
|
+
this.emit("open");
|
|
1803
|
+
}
|
|
1804
|
+
emitClose() {
|
|
1805
|
+
if (!this._socket) {
|
|
1806
|
+
this._readyState = WebSocket.CLOSED;
|
|
1807
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
1808
|
+
return;
|
|
1809
|
+
}
|
|
1810
|
+
if (this._extensions[PerMessageDeflate.extensionName]) {
|
|
1811
|
+
this._extensions[PerMessageDeflate.extensionName].cleanup();
|
|
1812
|
+
}
|
|
1813
|
+
this._receiver.removeAllListeners();
|
|
1814
|
+
this._readyState = WebSocket.CLOSED;
|
|
1815
|
+
this.emit("close", this._closeCode, this._closeMessage);
|
|
1816
|
+
}
|
|
1817
|
+
close(code, data) {
|
|
1818
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
1819
|
+
return;
|
|
1820
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
1821
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
1822
|
+
abortHandshake(this, this._req, msg);
|
|
1823
|
+
return;
|
|
1824
|
+
}
|
|
1825
|
+
if (this.readyState === WebSocket.CLOSING) {
|
|
1826
|
+
if (this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted)) {
|
|
1827
|
+
this._socket.end();
|
|
1828
|
+
}
|
|
1829
|
+
return;
|
|
1830
|
+
}
|
|
1831
|
+
this._readyState = WebSocket.CLOSING;
|
|
1832
|
+
this._sender.close(code, data, !this._isServer, (err) => {
|
|
1833
|
+
if (err)
|
|
1834
|
+
return;
|
|
1835
|
+
this._closeFrameSent = true;
|
|
1836
|
+
if (this._closeFrameReceived || this._receiver._writableState.errorEmitted) {
|
|
1837
|
+
this._socket.end();
|
|
1838
|
+
}
|
|
1839
|
+
});
|
|
1840
|
+
setCloseTimer(this);
|
|
1841
|
+
}
|
|
1842
|
+
pause() {
|
|
1843
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
1844
|
+
return;
|
|
1845
|
+
}
|
|
1846
|
+
this._paused = true;
|
|
1847
|
+
this._socket.pause();
|
|
1848
|
+
}
|
|
1849
|
+
ping(data, mask, cb) {
|
|
1850
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
1851
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
1852
|
+
}
|
|
1853
|
+
if (typeof data === "function") {
|
|
1854
|
+
cb = data;
|
|
1855
|
+
data = mask = undefined;
|
|
1856
|
+
} else if (typeof mask === "function") {
|
|
1857
|
+
cb = mask;
|
|
1858
|
+
mask = undefined;
|
|
1859
|
+
}
|
|
1860
|
+
if (typeof data === "number")
|
|
1861
|
+
data = data.toString();
|
|
1862
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
1863
|
+
sendAfterClose(this, data, cb);
|
|
1864
|
+
return;
|
|
1865
|
+
}
|
|
1866
|
+
if (mask === undefined)
|
|
1867
|
+
mask = !this._isServer;
|
|
1868
|
+
this._sender.ping(data || EMPTY_BUFFER, mask, cb);
|
|
1869
|
+
}
|
|
1870
|
+
pong(data, mask, cb) {
|
|
1871
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
1872
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
1873
|
+
}
|
|
1874
|
+
if (typeof data === "function") {
|
|
1875
|
+
cb = data;
|
|
1876
|
+
data = mask = undefined;
|
|
1877
|
+
} else if (typeof mask === "function") {
|
|
1878
|
+
cb = mask;
|
|
1879
|
+
mask = undefined;
|
|
1880
|
+
}
|
|
1881
|
+
if (typeof data === "number")
|
|
1882
|
+
data = data.toString();
|
|
1883
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
1884
|
+
sendAfterClose(this, data, cb);
|
|
1885
|
+
return;
|
|
1886
|
+
}
|
|
1887
|
+
if (mask === undefined)
|
|
1888
|
+
mask = !this._isServer;
|
|
1889
|
+
this._sender.pong(data || EMPTY_BUFFER, mask, cb);
|
|
1890
|
+
}
|
|
1891
|
+
resume() {
|
|
1892
|
+
if (this.readyState === WebSocket.CONNECTING || this.readyState === WebSocket.CLOSED) {
|
|
1893
|
+
return;
|
|
1894
|
+
}
|
|
1895
|
+
this._paused = false;
|
|
1896
|
+
if (!this._receiver._writableState.needDrain)
|
|
1897
|
+
this._socket.resume();
|
|
1898
|
+
}
|
|
1899
|
+
send(data, options, cb) {
|
|
1900
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
1901
|
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
|
1902
|
+
}
|
|
1903
|
+
if (typeof options === "function") {
|
|
1904
|
+
cb = options;
|
|
1905
|
+
options = {};
|
|
1906
|
+
}
|
|
1907
|
+
if (typeof data === "number")
|
|
1908
|
+
data = data.toString();
|
|
1909
|
+
if (this.readyState !== WebSocket.OPEN) {
|
|
1910
|
+
sendAfterClose(this, data, cb);
|
|
1911
|
+
return;
|
|
1912
|
+
}
|
|
1913
|
+
const opts = {
|
|
1914
|
+
binary: typeof data !== "string",
|
|
1915
|
+
mask: !this._isServer,
|
|
1916
|
+
compress: true,
|
|
1917
|
+
fin: true,
|
|
1918
|
+
...options
|
|
1919
|
+
};
|
|
1920
|
+
if (!this._extensions[PerMessageDeflate.extensionName]) {
|
|
1921
|
+
opts.compress = false;
|
|
1922
|
+
}
|
|
1923
|
+
this._sender.send(data || EMPTY_BUFFER, opts, cb);
|
|
1924
|
+
}
|
|
1925
|
+
terminate() {
|
|
1926
|
+
if (this.readyState === WebSocket.CLOSED)
|
|
1927
|
+
return;
|
|
1928
|
+
if (this.readyState === WebSocket.CONNECTING) {
|
|
1929
|
+
const msg = "WebSocket was closed before the connection was established";
|
|
1930
|
+
abortHandshake(this, this._req, msg);
|
|
1931
|
+
return;
|
|
1932
|
+
}
|
|
1933
|
+
if (this._socket) {
|
|
1934
|
+
this._readyState = WebSocket.CLOSING;
|
|
1935
|
+
this._socket.destroy();
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
Object.defineProperty(WebSocket, "CONNECTING", {
|
|
1940
|
+
enumerable: true,
|
|
1941
|
+
value: readyStates.indexOf("CONNECTING")
|
|
1942
|
+
});
|
|
1943
|
+
Object.defineProperty(WebSocket.prototype, "CONNECTING", {
|
|
1944
|
+
enumerable: true,
|
|
1945
|
+
value: readyStates.indexOf("CONNECTING")
|
|
1946
|
+
});
|
|
1947
|
+
Object.defineProperty(WebSocket, "OPEN", {
|
|
1948
|
+
enumerable: true,
|
|
1949
|
+
value: readyStates.indexOf("OPEN")
|
|
1950
|
+
});
|
|
1951
|
+
Object.defineProperty(WebSocket.prototype, "OPEN", {
|
|
1952
|
+
enumerable: true,
|
|
1953
|
+
value: readyStates.indexOf("OPEN")
|
|
1954
|
+
});
|
|
1955
|
+
Object.defineProperty(WebSocket, "CLOSING", {
|
|
1956
|
+
enumerable: true,
|
|
1957
|
+
value: readyStates.indexOf("CLOSING")
|
|
1958
|
+
});
|
|
1959
|
+
Object.defineProperty(WebSocket.prototype, "CLOSING", {
|
|
1960
|
+
enumerable: true,
|
|
1961
|
+
value: readyStates.indexOf("CLOSING")
|
|
1962
|
+
});
|
|
1963
|
+
Object.defineProperty(WebSocket, "CLOSED", {
|
|
1964
|
+
enumerable: true,
|
|
1965
|
+
value: readyStates.indexOf("CLOSED")
|
|
1966
|
+
});
|
|
1967
|
+
Object.defineProperty(WebSocket.prototype, "CLOSED", {
|
|
1968
|
+
enumerable: true,
|
|
1969
|
+
value: readyStates.indexOf("CLOSED")
|
|
1970
|
+
});
|
|
1971
|
+
[
|
|
1972
|
+
"binaryType",
|
|
1973
|
+
"bufferedAmount",
|
|
1974
|
+
"extensions",
|
|
1975
|
+
"isPaused",
|
|
1976
|
+
"protocol",
|
|
1977
|
+
"readyState",
|
|
1978
|
+
"url"
|
|
1979
|
+
].forEach((property) => {
|
|
1980
|
+
Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
|
|
1981
|
+
});
|
|
1982
|
+
["open", "error", "close", "message"].forEach((method) => {
|
|
1983
|
+
Object.defineProperty(WebSocket.prototype, `on${method}`, {
|
|
1984
|
+
enumerable: true,
|
|
1985
|
+
get() {
|
|
1986
|
+
for (const listener of this.listeners(method)) {
|
|
1987
|
+
if (listener[kForOnEventAttribute])
|
|
1988
|
+
return listener[kListener];
|
|
1989
|
+
}
|
|
1990
|
+
return null;
|
|
1991
|
+
},
|
|
1992
|
+
set(handler) {
|
|
1993
|
+
for (const listener of this.listeners(method)) {
|
|
1994
|
+
if (listener[kForOnEventAttribute]) {
|
|
1995
|
+
this.removeListener(method, listener);
|
|
1996
|
+
break;
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
if (typeof handler !== "function")
|
|
2000
|
+
return;
|
|
2001
|
+
this.addEventListener(method, handler, {
|
|
2002
|
+
[kForOnEventAttribute]: true
|
|
2003
|
+
});
|
|
2004
|
+
}
|
|
2005
|
+
});
|
|
2006
|
+
});
|
|
2007
|
+
WebSocket.prototype.addEventListener = addEventListener;
|
|
2008
|
+
WebSocket.prototype.removeEventListener = removeEventListener;
|
|
2009
|
+
module.exports = WebSocket;
|
|
2010
|
+
function initAsClient(websocket, address, protocols, options) {
|
|
2011
|
+
const opts = {
|
|
2012
|
+
allowSynchronousEvents: true,
|
|
2013
|
+
autoPong: true,
|
|
2014
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
2015
|
+
protocolVersion: protocolVersions[1],
|
|
2016
|
+
maxPayload: 100 * 1024 * 1024,
|
|
2017
|
+
skipUTF8Validation: false,
|
|
2018
|
+
perMessageDeflate: true,
|
|
2019
|
+
followRedirects: false,
|
|
2020
|
+
maxRedirects: 10,
|
|
2021
|
+
...options,
|
|
2022
|
+
socketPath: undefined,
|
|
2023
|
+
hostname: undefined,
|
|
2024
|
+
protocol: undefined,
|
|
2025
|
+
timeout: undefined,
|
|
2026
|
+
method: "GET",
|
|
2027
|
+
host: undefined,
|
|
2028
|
+
path: undefined,
|
|
2029
|
+
port: undefined
|
|
2030
|
+
};
|
|
2031
|
+
websocket._autoPong = opts.autoPong;
|
|
2032
|
+
websocket._closeTimeout = opts.closeTimeout;
|
|
2033
|
+
if (!protocolVersions.includes(opts.protocolVersion)) {
|
|
2034
|
+
throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} ` + `(supported versions: ${protocolVersions.join(", ")})`);
|
|
2035
|
+
}
|
|
2036
|
+
let parsedUrl;
|
|
2037
|
+
if (address instanceof URL) {
|
|
2038
|
+
parsedUrl = address;
|
|
2039
|
+
} else {
|
|
2040
|
+
try {
|
|
2041
|
+
parsedUrl = new URL(address);
|
|
2042
|
+
} catch (e) {
|
|
2043
|
+
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
if (parsedUrl.protocol === "http:") {
|
|
2047
|
+
parsedUrl.protocol = "ws:";
|
|
2048
|
+
} else if (parsedUrl.protocol === "https:") {
|
|
2049
|
+
parsedUrl.protocol = "wss:";
|
|
2050
|
+
}
|
|
2051
|
+
websocket._url = parsedUrl.href;
|
|
2052
|
+
const isSecure = parsedUrl.protocol === "wss:";
|
|
2053
|
+
const isIpcUrl = parsedUrl.protocol === "ws+unix:";
|
|
2054
|
+
let invalidUrlMessage;
|
|
2055
|
+
if (parsedUrl.protocol !== "ws:" && !isSecure && !isIpcUrl) {
|
|
2056
|
+
invalidUrlMessage = `The URL's protocol must be one of "ws:", "wss:", ` + '"http:", "https:", or "ws+unix:"';
|
|
2057
|
+
} else if (isIpcUrl && !parsedUrl.pathname) {
|
|
2058
|
+
invalidUrlMessage = "The URL's pathname is empty";
|
|
2059
|
+
} else if (parsedUrl.hash) {
|
|
2060
|
+
invalidUrlMessage = "The URL contains a fragment identifier";
|
|
2061
|
+
}
|
|
2062
|
+
if (invalidUrlMessage) {
|
|
2063
|
+
const err = new SyntaxError(invalidUrlMessage);
|
|
2064
|
+
if (websocket._redirects === 0) {
|
|
2065
|
+
throw err;
|
|
2066
|
+
} else {
|
|
2067
|
+
emitErrorAndClose(websocket, err);
|
|
2068
|
+
return;
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
const defaultPort = isSecure ? 443 : 80;
|
|
2072
|
+
const key = randomBytes(16).toString("base64");
|
|
2073
|
+
const request = isSecure ? https.request : http.request;
|
|
2074
|
+
const protocolSet = new Set;
|
|
2075
|
+
let perMessageDeflate;
|
|
2076
|
+
opts.createConnection = opts.createConnection || (isSecure ? tlsConnect : netConnect);
|
|
2077
|
+
opts.defaultPort = opts.defaultPort || defaultPort;
|
|
2078
|
+
opts.port = parsedUrl.port || defaultPort;
|
|
2079
|
+
opts.host = parsedUrl.hostname.startsWith("[") ? parsedUrl.hostname.slice(1, -1) : parsedUrl.hostname;
|
|
2080
|
+
opts.headers = {
|
|
2081
|
+
...opts.headers,
|
|
2082
|
+
"Sec-WebSocket-Version": opts.protocolVersion,
|
|
2083
|
+
"Sec-WebSocket-Key": key,
|
|
2084
|
+
Connection: "Upgrade",
|
|
2085
|
+
Upgrade: "websocket"
|
|
2086
|
+
};
|
|
2087
|
+
opts.path = parsedUrl.pathname + parsedUrl.search;
|
|
2088
|
+
opts.timeout = opts.handshakeTimeout;
|
|
2089
|
+
if (opts.perMessageDeflate) {
|
|
2090
|
+
perMessageDeflate = new PerMessageDeflate(opts.perMessageDeflate !== true ? opts.perMessageDeflate : {}, false, opts.maxPayload);
|
|
2091
|
+
opts.headers["Sec-WebSocket-Extensions"] = format({
|
|
2092
|
+
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
2093
|
+
});
|
|
2094
|
+
}
|
|
2095
|
+
if (protocols.length) {
|
|
2096
|
+
for (const protocol of protocols) {
|
|
2097
|
+
if (typeof protocol !== "string" || !subprotocolRegex.test(protocol) || protocolSet.has(protocol)) {
|
|
2098
|
+
throw new SyntaxError("An invalid or duplicated subprotocol was specified");
|
|
2099
|
+
}
|
|
2100
|
+
protocolSet.add(protocol);
|
|
2101
|
+
}
|
|
2102
|
+
opts.headers["Sec-WebSocket-Protocol"] = protocols.join(",");
|
|
2103
|
+
}
|
|
2104
|
+
if (opts.origin) {
|
|
2105
|
+
if (opts.protocolVersion < 13) {
|
|
2106
|
+
opts.headers["Sec-WebSocket-Origin"] = opts.origin;
|
|
2107
|
+
} else {
|
|
2108
|
+
opts.headers.Origin = opts.origin;
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
if (parsedUrl.username || parsedUrl.password) {
|
|
2112
|
+
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
|
|
2113
|
+
}
|
|
2114
|
+
if (isIpcUrl) {
|
|
2115
|
+
const parts = opts.path.split(":");
|
|
2116
|
+
opts.socketPath = parts[0];
|
|
2117
|
+
opts.path = parts[1];
|
|
2118
|
+
}
|
|
2119
|
+
let req;
|
|
2120
|
+
if (opts.followRedirects) {
|
|
2121
|
+
if (websocket._redirects === 0) {
|
|
2122
|
+
websocket._originalIpc = isIpcUrl;
|
|
2123
|
+
websocket._originalSecure = isSecure;
|
|
2124
|
+
websocket._originalHostOrSocketPath = isIpcUrl ? opts.socketPath : parsedUrl.host;
|
|
2125
|
+
const headers = options && options.headers;
|
|
2126
|
+
options = { ...options, headers: {} };
|
|
2127
|
+
if (headers) {
|
|
2128
|
+
for (const [key2, value] of Object.entries(headers)) {
|
|
2129
|
+
options.headers[key2.toLowerCase()] = value;
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
} else if (websocket.listenerCount("redirect") === 0) {
|
|
2133
|
+
const isSameHost = isIpcUrl ? websocket._originalIpc ? opts.socketPath === websocket._originalHostOrSocketPath : false : websocket._originalIpc ? false : parsedUrl.host === websocket._originalHostOrSocketPath;
|
|
2134
|
+
if (!isSameHost || websocket._originalSecure && !isSecure) {
|
|
2135
|
+
delete opts.headers.authorization;
|
|
2136
|
+
delete opts.headers.cookie;
|
|
2137
|
+
if (!isSameHost)
|
|
2138
|
+
delete opts.headers.host;
|
|
2139
|
+
opts.auth = undefined;
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
if (opts.auth && !options.headers.authorization) {
|
|
2143
|
+
options.headers.authorization = "Basic " + Buffer.from(opts.auth).toString("base64");
|
|
2144
|
+
}
|
|
2145
|
+
req = websocket._req = request(opts);
|
|
2146
|
+
if (websocket._redirects) {
|
|
2147
|
+
websocket.emit("redirect", websocket.url, req);
|
|
2148
|
+
}
|
|
2149
|
+
} else {
|
|
2150
|
+
req = websocket._req = request(opts);
|
|
2151
|
+
}
|
|
2152
|
+
if (opts.timeout) {
|
|
2153
|
+
req.on("timeout", () => {
|
|
2154
|
+
abortHandshake(websocket, req, "Opening handshake has timed out");
|
|
2155
|
+
});
|
|
2156
|
+
}
|
|
2157
|
+
req.on("error", (err) => {
|
|
2158
|
+
if (req === null || req[kAborted])
|
|
2159
|
+
return;
|
|
2160
|
+
req = websocket._req = null;
|
|
2161
|
+
emitErrorAndClose(websocket, err);
|
|
2162
|
+
});
|
|
2163
|
+
req.on("response", (res) => {
|
|
2164
|
+
const location = res.headers.location;
|
|
2165
|
+
const statusCode = res.statusCode;
|
|
2166
|
+
if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
|
|
2167
|
+
if (++websocket._redirects > opts.maxRedirects) {
|
|
2168
|
+
abortHandshake(websocket, req, "Maximum redirects exceeded");
|
|
2169
|
+
return;
|
|
2170
|
+
}
|
|
2171
|
+
req.abort();
|
|
2172
|
+
let addr;
|
|
2173
|
+
try {
|
|
2174
|
+
addr = new URL(location, address);
|
|
2175
|
+
} catch (e) {
|
|
2176
|
+
const err = new SyntaxError(`Invalid URL: ${location}`);
|
|
2177
|
+
emitErrorAndClose(websocket, err);
|
|
2178
|
+
return;
|
|
2179
|
+
}
|
|
2180
|
+
initAsClient(websocket, addr, protocols, options);
|
|
2181
|
+
} else if (!websocket.emit("unexpected-response", req, res)) {
|
|
2182
|
+
abortHandshake(websocket, req, `Unexpected server response: ${res.statusCode}`);
|
|
2183
|
+
}
|
|
2184
|
+
});
|
|
2185
|
+
req.on("upgrade", (res, socket, head) => {
|
|
2186
|
+
websocket.emit("upgrade", res);
|
|
2187
|
+
if (websocket.readyState !== WebSocket.CONNECTING)
|
|
2188
|
+
return;
|
|
2189
|
+
req = websocket._req = null;
|
|
2190
|
+
const upgrade = res.headers.upgrade;
|
|
2191
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
2192
|
+
abortHandshake(websocket, socket, "Invalid Upgrade header");
|
|
2193
|
+
return;
|
|
2194
|
+
}
|
|
2195
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
2196
|
+
if (res.headers["sec-websocket-accept"] !== digest) {
|
|
2197
|
+
abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
|
|
2198
|
+
return;
|
|
2199
|
+
}
|
|
2200
|
+
const serverProt = res.headers["sec-websocket-protocol"];
|
|
2201
|
+
let protError;
|
|
2202
|
+
if (serverProt !== undefined) {
|
|
2203
|
+
if (!protocolSet.size) {
|
|
2204
|
+
protError = "Server sent a subprotocol but none was requested";
|
|
2205
|
+
} else if (!protocolSet.has(serverProt)) {
|
|
2206
|
+
protError = "Server sent an invalid subprotocol";
|
|
2207
|
+
}
|
|
2208
|
+
} else if (protocolSet.size) {
|
|
2209
|
+
protError = "Server sent no subprotocol";
|
|
2210
|
+
}
|
|
2211
|
+
if (protError) {
|
|
2212
|
+
abortHandshake(websocket, socket, protError);
|
|
2213
|
+
return;
|
|
2214
|
+
}
|
|
2215
|
+
if (serverProt)
|
|
2216
|
+
websocket._protocol = serverProt;
|
|
2217
|
+
const secWebSocketExtensions = res.headers["sec-websocket-extensions"];
|
|
2218
|
+
if (secWebSocketExtensions !== undefined) {
|
|
2219
|
+
if (!perMessageDeflate) {
|
|
2220
|
+
const message = "Server sent a Sec-WebSocket-Extensions header but no extension " + "was requested";
|
|
2221
|
+
abortHandshake(websocket, socket, message);
|
|
2222
|
+
return;
|
|
2223
|
+
}
|
|
2224
|
+
let extensions;
|
|
2225
|
+
try {
|
|
2226
|
+
extensions = parse(secWebSocketExtensions);
|
|
2227
|
+
} catch (err) {
|
|
2228
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
2229
|
+
abortHandshake(websocket, socket, message);
|
|
2230
|
+
return;
|
|
2231
|
+
}
|
|
2232
|
+
const extensionNames = Object.keys(extensions);
|
|
2233
|
+
if (extensionNames.length !== 1 || extensionNames[0] !== PerMessageDeflate.extensionName) {
|
|
2234
|
+
const message = "Server indicated an extension that was not requested";
|
|
2235
|
+
abortHandshake(websocket, socket, message);
|
|
2236
|
+
return;
|
|
2237
|
+
}
|
|
2238
|
+
try {
|
|
2239
|
+
perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
|
|
2240
|
+
} catch (err) {
|
|
2241
|
+
const message = "Invalid Sec-WebSocket-Extensions header";
|
|
2242
|
+
abortHandshake(websocket, socket, message);
|
|
2243
|
+
return;
|
|
2244
|
+
}
|
|
2245
|
+
websocket._extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
2246
|
+
}
|
|
2247
|
+
websocket.setSocket(socket, head, {
|
|
2248
|
+
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
2249
|
+
generateMask: opts.generateMask,
|
|
2250
|
+
maxPayload: opts.maxPayload,
|
|
2251
|
+
skipUTF8Validation: opts.skipUTF8Validation
|
|
2252
|
+
});
|
|
2253
|
+
});
|
|
2254
|
+
if (opts.finishRequest) {
|
|
2255
|
+
opts.finishRequest(req, websocket);
|
|
2256
|
+
} else {
|
|
2257
|
+
req.end();
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
function emitErrorAndClose(websocket, err) {
|
|
2261
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
2262
|
+
websocket._errorEmitted = true;
|
|
2263
|
+
websocket.emit("error", err);
|
|
2264
|
+
websocket.emitClose();
|
|
2265
|
+
}
|
|
2266
|
+
function netConnect(options) {
|
|
2267
|
+
options.path = options.socketPath;
|
|
2268
|
+
return net.connect(options);
|
|
2269
|
+
}
|
|
2270
|
+
function tlsConnect(options) {
|
|
2271
|
+
options.path = undefined;
|
|
2272
|
+
if (!options.servername && options.servername !== "") {
|
|
2273
|
+
options.servername = net.isIP(options.host) ? "" : options.host;
|
|
2274
|
+
}
|
|
2275
|
+
return tls.connect(options);
|
|
2276
|
+
}
|
|
2277
|
+
function abortHandshake(websocket, stream, message) {
|
|
2278
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
2279
|
+
const err = new Error(message);
|
|
2280
|
+
Error.captureStackTrace(err, abortHandshake);
|
|
2281
|
+
if (stream.setHeader) {
|
|
2282
|
+
stream[kAborted] = true;
|
|
2283
|
+
stream.abort();
|
|
2284
|
+
if (stream.socket && !stream.socket.destroyed) {
|
|
2285
|
+
stream.socket.destroy();
|
|
2286
|
+
}
|
|
2287
|
+
process.nextTick(emitErrorAndClose, websocket, err);
|
|
2288
|
+
} else {
|
|
2289
|
+
stream.destroy(err);
|
|
2290
|
+
stream.once("error", websocket.emit.bind(websocket, "error"));
|
|
2291
|
+
stream.once("close", websocket.emitClose.bind(websocket));
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
function sendAfterClose(websocket, data, cb) {
|
|
2295
|
+
if (data) {
|
|
2296
|
+
const length = isBlob(data) ? data.size : toBuffer(data).length;
|
|
2297
|
+
if (websocket._socket)
|
|
2298
|
+
websocket._sender._bufferedBytes += length;
|
|
2299
|
+
else
|
|
2300
|
+
websocket._bufferedAmount += length;
|
|
2301
|
+
}
|
|
2302
|
+
if (cb) {
|
|
2303
|
+
const err = new Error(`WebSocket is not open: readyState ${websocket.readyState} ` + `(${readyStates[websocket.readyState]})`);
|
|
2304
|
+
process.nextTick(cb, err);
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
function receiverOnConclude(code, reason) {
|
|
2308
|
+
const websocket = this[kWebSocket];
|
|
2309
|
+
websocket._closeFrameReceived = true;
|
|
2310
|
+
websocket._closeMessage = reason;
|
|
2311
|
+
websocket._closeCode = code;
|
|
2312
|
+
if (websocket._socket[kWebSocket] === undefined)
|
|
2313
|
+
return;
|
|
2314
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
2315
|
+
process.nextTick(resume, websocket._socket);
|
|
2316
|
+
if (code === 1005)
|
|
2317
|
+
websocket.close();
|
|
2318
|
+
else
|
|
2319
|
+
websocket.close(code, reason);
|
|
2320
|
+
}
|
|
2321
|
+
function receiverOnDrain() {
|
|
2322
|
+
const websocket = this[kWebSocket];
|
|
2323
|
+
if (!websocket.isPaused)
|
|
2324
|
+
websocket._socket.resume();
|
|
2325
|
+
}
|
|
2326
|
+
function receiverOnError(err) {
|
|
2327
|
+
const websocket = this[kWebSocket];
|
|
2328
|
+
if (websocket._socket[kWebSocket] !== undefined) {
|
|
2329
|
+
websocket._socket.removeListener("data", socketOnData);
|
|
2330
|
+
process.nextTick(resume, websocket._socket);
|
|
2331
|
+
websocket.close(err[kStatusCode]);
|
|
2332
|
+
}
|
|
2333
|
+
if (!websocket._errorEmitted) {
|
|
2334
|
+
websocket._errorEmitted = true;
|
|
2335
|
+
websocket.emit("error", err);
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
function receiverOnFinish() {
|
|
2339
|
+
this[kWebSocket].emitClose();
|
|
2340
|
+
}
|
|
2341
|
+
function receiverOnMessage(data, isBinary) {
|
|
2342
|
+
this[kWebSocket].emit("message", data, isBinary);
|
|
2343
|
+
}
|
|
2344
|
+
function receiverOnPing(data) {
|
|
2345
|
+
const websocket = this[kWebSocket];
|
|
2346
|
+
if (websocket._autoPong)
|
|
2347
|
+
websocket.pong(data, !this._isServer, NOOP);
|
|
2348
|
+
websocket.emit("ping", data);
|
|
2349
|
+
}
|
|
2350
|
+
function receiverOnPong(data) {
|
|
2351
|
+
this[kWebSocket].emit("pong", data);
|
|
2352
|
+
}
|
|
2353
|
+
function resume(stream) {
|
|
2354
|
+
stream.resume();
|
|
2355
|
+
}
|
|
2356
|
+
function senderOnError(err) {
|
|
2357
|
+
const websocket = this[kWebSocket];
|
|
2358
|
+
if (websocket.readyState === WebSocket.CLOSED)
|
|
2359
|
+
return;
|
|
2360
|
+
if (websocket.readyState === WebSocket.OPEN) {
|
|
2361
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
2362
|
+
setCloseTimer(websocket);
|
|
2363
|
+
}
|
|
2364
|
+
this._socket.end();
|
|
2365
|
+
if (!websocket._errorEmitted) {
|
|
2366
|
+
websocket._errorEmitted = true;
|
|
2367
|
+
websocket.emit("error", err);
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
function setCloseTimer(websocket) {
|
|
2371
|
+
websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), websocket._closeTimeout);
|
|
2372
|
+
}
|
|
2373
|
+
function socketOnClose() {
|
|
2374
|
+
const websocket = this[kWebSocket];
|
|
2375
|
+
this.removeListener("close", socketOnClose);
|
|
2376
|
+
this.removeListener("data", socketOnData);
|
|
2377
|
+
this.removeListener("end", socketOnEnd);
|
|
2378
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
2379
|
+
if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
|
|
2380
|
+
const chunk = this.read(this._readableState.length);
|
|
2381
|
+
websocket._receiver.write(chunk);
|
|
2382
|
+
}
|
|
2383
|
+
websocket._receiver.end();
|
|
2384
|
+
this[kWebSocket] = undefined;
|
|
2385
|
+
clearTimeout(websocket._closeTimer);
|
|
2386
|
+
if (websocket._receiver._writableState.finished || websocket._receiver._writableState.errorEmitted) {
|
|
2387
|
+
websocket.emitClose();
|
|
2388
|
+
} else {
|
|
2389
|
+
websocket._receiver.on("error", receiverOnFinish);
|
|
2390
|
+
websocket._receiver.on("finish", receiverOnFinish);
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
function socketOnData(chunk) {
|
|
2394
|
+
if (!this[kWebSocket]._receiver.write(chunk)) {
|
|
2395
|
+
this.pause();
|
|
2396
|
+
}
|
|
2397
|
+
}
|
|
2398
|
+
function socketOnEnd() {
|
|
2399
|
+
const websocket = this[kWebSocket];
|
|
2400
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
2401
|
+
websocket._receiver.end();
|
|
2402
|
+
this.end();
|
|
2403
|
+
}
|
|
2404
|
+
function socketOnError() {
|
|
2405
|
+
const websocket = this[kWebSocket];
|
|
2406
|
+
this.removeListener("error", socketOnError);
|
|
2407
|
+
this.on("error", NOOP);
|
|
2408
|
+
if (websocket) {
|
|
2409
|
+
websocket._readyState = WebSocket.CLOSING;
|
|
2410
|
+
this.destroy();
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
});
|
|
2414
|
+
|
|
2415
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/stream.js
|
|
2416
|
+
var require_stream = __commonJS((exports, module) => {
|
|
2417
|
+
var WebSocket = require_websocket();
|
|
2418
|
+
var { Duplex } = __require("stream");
|
|
2419
|
+
function emitClose(stream) {
|
|
2420
|
+
stream.emit("close");
|
|
2421
|
+
}
|
|
2422
|
+
function duplexOnEnd() {
|
|
2423
|
+
if (!this.destroyed && this._writableState.finished) {
|
|
2424
|
+
this.destroy();
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
function duplexOnError(err) {
|
|
2428
|
+
this.removeListener("error", duplexOnError);
|
|
2429
|
+
this.destroy();
|
|
2430
|
+
if (this.listenerCount("error") === 0) {
|
|
2431
|
+
this.emit("error", err);
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
function createWebSocketStream(ws, options) {
|
|
2435
|
+
let terminateOnDestroy = true;
|
|
2436
|
+
const duplex = new Duplex({
|
|
2437
|
+
...options,
|
|
2438
|
+
autoDestroy: false,
|
|
2439
|
+
emitClose: false,
|
|
2440
|
+
objectMode: false,
|
|
2441
|
+
writableObjectMode: false
|
|
2442
|
+
});
|
|
2443
|
+
ws.on("message", function message(msg, isBinary) {
|
|
2444
|
+
const data = !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
|
2445
|
+
if (!duplex.push(data))
|
|
2446
|
+
ws.pause();
|
|
2447
|
+
});
|
|
2448
|
+
ws.once("error", function error(err) {
|
|
2449
|
+
if (duplex.destroyed)
|
|
2450
|
+
return;
|
|
2451
|
+
terminateOnDestroy = false;
|
|
2452
|
+
duplex.destroy(err);
|
|
2453
|
+
});
|
|
2454
|
+
ws.once("close", function close() {
|
|
2455
|
+
if (duplex.destroyed)
|
|
2456
|
+
return;
|
|
2457
|
+
duplex.push(null);
|
|
2458
|
+
});
|
|
2459
|
+
duplex._destroy = function(err, callback) {
|
|
2460
|
+
if (ws.readyState === ws.CLOSED) {
|
|
2461
|
+
callback(err);
|
|
2462
|
+
process.nextTick(emitClose, duplex);
|
|
2463
|
+
return;
|
|
2464
|
+
}
|
|
2465
|
+
let called = false;
|
|
2466
|
+
ws.once("error", function error(err2) {
|
|
2467
|
+
called = true;
|
|
2468
|
+
callback(err2);
|
|
2469
|
+
});
|
|
2470
|
+
ws.once("close", function close() {
|
|
2471
|
+
if (!called)
|
|
2472
|
+
callback(err);
|
|
2473
|
+
process.nextTick(emitClose, duplex);
|
|
2474
|
+
});
|
|
2475
|
+
if (terminateOnDestroy)
|
|
2476
|
+
ws.terminate();
|
|
2477
|
+
};
|
|
2478
|
+
duplex._final = function(callback) {
|
|
2479
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
2480
|
+
ws.once("open", function open() {
|
|
2481
|
+
duplex._final(callback);
|
|
2482
|
+
});
|
|
2483
|
+
return;
|
|
2484
|
+
}
|
|
2485
|
+
if (ws._socket === null)
|
|
2486
|
+
return;
|
|
2487
|
+
if (ws._socket._writableState.finished) {
|
|
2488
|
+
callback();
|
|
2489
|
+
if (duplex._readableState.endEmitted)
|
|
2490
|
+
duplex.destroy();
|
|
2491
|
+
} else {
|
|
2492
|
+
ws._socket.once("finish", function finish() {
|
|
2493
|
+
callback();
|
|
2494
|
+
});
|
|
2495
|
+
ws.close();
|
|
2496
|
+
}
|
|
2497
|
+
};
|
|
2498
|
+
duplex._read = function() {
|
|
2499
|
+
if (ws.isPaused)
|
|
2500
|
+
ws.resume();
|
|
2501
|
+
};
|
|
2502
|
+
duplex._write = function(chunk, encoding, callback) {
|
|
2503
|
+
if (ws.readyState === ws.CONNECTING) {
|
|
2504
|
+
ws.once("open", function open() {
|
|
2505
|
+
duplex._write(chunk, encoding, callback);
|
|
2506
|
+
});
|
|
2507
|
+
return;
|
|
2508
|
+
}
|
|
2509
|
+
ws.send(chunk, callback);
|
|
2510
|
+
};
|
|
2511
|
+
duplex.on("end", duplexOnEnd);
|
|
2512
|
+
duplex.on("error", duplexOnError);
|
|
2513
|
+
return duplex;
|
|
2514
|
+
}
|
|
2515
|
+
module.exports = createWebSocketStream;
|
|
2516
|
+
});
|
|
2517
|
+
|
|
2518
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/subprotocol.js
|
|
2519
|
+
var require_subprotocol = __commonJS((exports, module) => {
|
|
2520
|
+
var { tokenChars } = require_validation();
|
|
2521
|
+
function parse(header) {
|
|
2522
|
+
const protocols = new Set;
|
|
2523
|
+
let start = -1;
|
|
2524
|
+
let end = -1;
|
|
2525
|
+
let i = 0;
|
|
2526
|
+
for (i;i < header.length; i++) {
|
|
2527
|
+
const code = header.charCodeAt(i);
|
|
2528
|
+
if (end === -1 && tokenChars[code] === 1) {
|
|
2529
|
+
if (start === -1)
|
|
2530
|
+
start = i;
|
|
2531
|
+
} else if (i !== 0 && (code === 32 || code === 9)) {
|
|
2532
|
+
if (end === -1 && start !== -1)
|
|
2533
|
+
end = i;
|
|
2534
|
+
} else if (code === 44) {
|
|
2535
|
+
if (start === -1) {
|
|
2536
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2537
|
+
}
|
|
2538
|
+
if (end === -1)
|
|
2539
|
+
end = i;
|
|
2540
|
+
const protocol2 = header.slice(start, end);
|
|
2541
|
+
if (protocols.has(protocol2)) {
|
|
2542
|
+
throw new SyntaxError(`The "${protocol2}" subprotocol is duplicated`);
|
|
2543
|
+
}
|
|
2544
|
+
protocols.add(protocol2);
|
|
2545
|
+
start = end = -1;
|
|
2546
|
+
} else {
|
|
2547
|
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
|
2548
|
+
}
|
|
2549
|
+
}
|
|
2550
|
+
if (start === -1 || end !== -1) {
|
|
2551
|
+
throw new SyntaxError("Unexpected end of input");
|
|
2552
|
+
}
|
|
2553
|
+
const protocol = header.slice(start, i);
|
|
2554
|
+
if (protocols.has(protocol)) {
|
|
2555
|
+
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
|
2556
|
+
}
|
|
2557
|
+
protocols.add(protocol);
|
|
2558
|
+
return protocols;
|
|
2559
|
+
}
|
|
2560
|
+
module.exports = { parse };
|
|
2561
|
+
});
|
|
2562
|
+
|
|
2563
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/lib/websocket-server.js
|
|
2564
|
+
var require_websocket_server = __commonJS((exports, module) => {
|
|
2565
|
+
var EventEmitter = __require("events");
|
|
2566
|
+
var http = __require("http");
|
|
2567
|
+
var { Duplex } = __require("stream");
|
|
2568
|
+
var { createHash } = __require("crypto");
|
|
2569
|
+
var extension = require_extension();
|
|
2570
|
+
var PerMessageDeflate = require_permessage_deflate();
|
|
2571
|
+
var subprotocol = require_subprotocol();
|
|
2572
|
+
var WebSocket = require_websocket();
|
|
2573
|
+
var { CLOSE_TIMEOUT, GUID, kWebSocket } = require_constants();
|
|
2574
|
+
var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
|
2575
|
+
var RUNNING = 0;
|
|
2576
|
+
var CLOSING = 1;
|
|
2577
|
+
var CLOSED = 2;
|
|
2578
|
+
|
|
2579
|
+
class WebSocketServer extends EventEmitter {
|
|
2580
|
+
constructor(options, callback) {
|
|
2581
|
+
super();
|
|
2582
|
+
options = {
|
|
2583
|
+
allowSynchronousEvents: true,
|
|
2584
|
+
autoPong: true,
|
|
2585
|
+
maxPayload: 100 * 1024 * 1024,
|
|
2586
|
+
skipUTF8Validation: false,
|
|
2587
|
+
perMessageDeflate: false,
|
|
2588
|
+
handleProtocols: null,
|
|
2589
|
+
clientTracking: true,
|
|
2590
|
+
closeTimeout: CLOSE_TIMEOUT,
|
|
2591
|
+
verifyClient: null,
|
|
2592
|
+
noServer: false,
|
|
2593
|
+
backlog: null,
|
|
2594
|
+
server: null,
|
|
2595
|
+
host: null,
|
|
2596
|
+
path: null,
|
|
2597
|
+
port: null,
|
|
2598
|
+
WebSocket,
|
|
2599
|
+
...options
|
|
2600
|
+
};
|
|
2601
|
+
if (options.port == null && !options.server && !options.noServer || options.port != null && (options.server || options.noServer) || options.server && options.noServer) {
|
|
2602
|
+
throw new TypeError('One and only one of the "port", "server", or "noServer" options ' + "must be specified");
|
|
2603
|
+
}
|
|
2604
|
+
if (options.port != null) {
|
|
2605
|
+
this._server = http.createServer((req, res) => {
|
|
2606
|
+
const body = http.STATUS_CODES[426];
|
|
2607
|
+
res.writeHead(426, {
|
|
2608
|
+
"Content-Length": body.length,
|
|
2609
|
+
"Content-Type": "text/plain"
|
|
2610
|
+
});
|
|
2611
|
+
res.end(body);
|
|
2612
|
+
});
|
|
2613
|
+
this._server.listen(options.port, options.host, options.backlog, callback);
|
|
2614
|
+
} else if (options.server) {
|
|
2615
|
+
this._server = options.server;
|
|
2616
|
+
}
|
|
2617
|
+
if (this._server) {
|
|
2618
|
+
const emitConnection = this.emit.bind(this, "connection");
|
|
2619
|
+
this._removeListeners = addListeners(this._server, {
|
|
2620
|
+
listening: this.emit.bind(this, "listening"),
|
|
2621
|
+
error: this.emit.bind(this, "error"),
|
|
2622
|
+
upgrade: (req, socket, head) => {
|
|
2623
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
|
2624
|
+
}
|
|
2625
|
+
});
|
|
2626
|
+
}
|
|
2627
|
+
if (options.perMessageDeflate === true)
|
|
2628
|
+
options.perMessageDeflate = {};
|
|
2629
|
+
if (options.clientTracking) {
|
|
2630
|
+
this.clients = new Set;
|
|
2631
|
+
this._shouldEmitClose = false;
|
|
2632
|
+
}
|
|
2633
|
+
this.options = options;
|
|
2634
|
+
this._state = RUNNING;
|
|
2635
|
+
}
|
|
2636
|
+
address() {
|
|
2637
|
+
if (this.options.noServer) {
|
|
2638
|
+
throw new Error('The server is operating in "noServer" mode');
|
|
2639
|
+
}
|
|
2640
|
+
if (!this._server)
|
|
2641
|
+
return null;
|
|
2642
|
+
return this._server.address();
|
|
2643
|
+
}
|
|
2644
|
+
close(cb) {
|
|
2645
|
+
if (this._state === CLOSED) {
|
|
2646
|
+
if (cb) {
|
|
2647
|
+
this.once("close", () => {
|
|
2648
|
+
cb(new Error("The server is not running"));
|
|
2649
|
+
});
|
|
2650
|
+
}
|
|
2651
|
+
process.nextTick(emitClose, this);
|
|
2652
|
+
return;
|
|
2653
|
+
}
|
|
2654
|
+
if (cb)
|
|
2655
|
+
this.once("close", cb);
|
|
2656
|
+
if (this._state === CLOSING)
|
|
2657
|
+
return;
|
|
2658
|
+
this._state = CLOSING;
|
|
2659
|
+
if (this.options.noServer || this.options.server) {
|
|
2660
|
+
if (this._server) {
|
|
2661
|
+
this._removeListeners();
|
|
2662
|
+
this._removeListeners = this._server = null;
|
|
2663
|
+
}
|
|
2664
|
+
if (this.clients) {
|
|
2665
|
+
if (!this.clients.size) {
|
|
2666
|
+
process.nextTick(emitClose, this);
|
|
2667
|
+
} else {
|
|
2668
|
+
this._shouldEmitClose = true;
|
|
2669
|
+
}
|
|
2670
|
+
} else {
|
|
2671
|
+
process.nextTick(emitClose, this);
|
|
2672
|
+
}
|
|
2673
|
+
} else {
|
|
2674
|
+
const server = this._server;
|
|
2675
|
+
this._removeListeners();
|
|
2676
|
+
this._removeListeners = this._server = null;
|
|
2677
|
+
server.close(() => {
|
|
2678
|
+
emitClose(this);
|
|
2679
|
+
});
|
|
2680
|
+
}
|
|
2681
|
+
}
|
|
2682
|
+
shouldHandle(req) {
|
|
2683
|
+
if (this.options.path) {
|
|
2684
|
+
const index = req.url.indexOf("?");
|
|
2685
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
|
2686
|
+
if (pathname !== this.options.path)
|
|
2687
|
+
return false;
|
|
2688
|
+
}
|
|
2689
|
+
return true;
|
|
2690
|
+
}
|
|
2691
|
+
handleUpgrade(req, socket, head, cb) {
|
|
2692
|
+
socket.on("error", socketOnError);
|
|
2693
|
+
const key = req.headers["sec-websocket-key"];
|
|
2694
|
+
const upgrade = req.headers.upgrade;
|
|
2695
|
+
const version = +req.headers["sec-websocket-version"];
|
|
2696
|
+
if (req.method !== "GET") {
|
|
2697
|
+
const message = "Invalid HTTP method";
|
|
2698
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
|
|
2699
|
+
return;
|
|
2700
|
+
}
|
|
2701
|
+
if (upgrade === undefined || upgrade.toLowerCase() !== "websocket") {
|
|
2702
|
+
const message = "Invalid Upgrade header";
|
|
2703
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
2704
|
+
return;
|
|
2705
|
+
}
|
|
2706
|
+
if (key === undefined || !keyRegex.test(key)) {
|
|
2707
|
+
const message = "Missing or invalid Sec-WebSocket-Key header";
|
|
2708
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
2709
|
+
return;
|
|
2710
|
+
}
|
|
2711
|
+
if (version !== 13 && version !== 8) {
|
|
2712
|
+
const message = "Missing or invalid Sec-WebSocket-Version header";
|
|
2713
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
|
|
2714
|
+
"Sec-WebSocket-Version": "13, 8"
|
|
2715
|
+
});
|
|
2716
|
+
return;
|
|
2717
|
+
}
|
|
2718
|
+
if (!this.shouldHandle(req)) {
|
|
2719
|
+
abortHandshake(socket, 400);
|
|
2720
|
+
return;
|
|
2721
|
+
}
|
|
2722
|
+
const secWebSocketProtocol = req.headers["sec-websocket-protocol"];
|
|
2723
|
+
let protocols = new Set;
|
|
2724
|
+
if (secWebSocketProtocol !== undefined) {
|
|
2725
|
+
try {
|
|
2726
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
|
2727
|
+
} catch (err) {
|
|
2728
|
+
const message = "Invalid Sec-WebSocket-Protocol header";
|
|
2729
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
2730
|
+
return;
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
const secWebSocketExtensions = req.headers["sec-websocket-extensions"];
|
|
2734
|
+
const extensions = {};
|
|
2735
|
+
if (this.options.perMessageDeflate && secWebSocketExtensions !== undefined) {
|
|
2736
|
+
const perMessageDeflate = new PerMessageDeflate(this.options.perMessageDeflate, true, this.options.maxPayload);
|
|
2737
|
+
try {
|
|
2738
|
+
const offers = extension.parse(secWebSocketExtensions);
|
|
2739
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
|
2740
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
|
2741
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
|
2742
|
+
}
|
|
2743
|
+
} catch (err) {
|
|
2744
|
+
const message = "Invalid or unacceptable Sec-WebSocket-Extensions header";
|
|
2745
|
+
abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
|
|
2746
|
+
return;
|
|
2747
|
+
}
|
|
2748
|
+
}
|
|
2749
|
+
if (this.options.verifyClient) {
|
|
2750
|
+
const info = {
|
|
2751
|
+
origin: req.headers[`${version === 8 ? "sec-websocket-origin" : "origin"}`],
|
|
2752
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
|
2753
|
+
req
|
|
2754
|
+
};
|
|
2755
|
+
if (this.options.verifyClient.length === 2) {
|
|
2756
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
|
2757
|
+
if (!verified) {
|
|
2758
|
+
return abortHandshake(socket, code || 401, message, headers);
|
|
2759
|
+
}
|
|
2760
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
2761
|
+
});
|
|
2762
|
+
return;
|
|
2763
|
+
}
|
|
2764
|
+
if (!this.options.verifyClient(info))
|
|
2765
|
+
return abortHandshake(socket, 401);
|
|
2766
|
+
}
|
|
2767
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
|
2768
|
+
}
|
|
2769
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
|
2770
|
+
if (!socket.readable || !socket.writable)
|
|
2771
|
+
return socket.destroy();
|
|
2772
|
+
if (socket[kWebSocket]) {
|
|
2773
|
+
throw new Error("server.handleUpgrade() was called more than once with the same " + "socket, possibly due to a misconfiguration");
|
|
2774
|
+
}
|
|
2775
|
+
if (this._state > RUNNING)
|
|
2776
|
+
return abortHandshake(socket, 503);
|
|
2777
|
+
const digest = createHash("sha1").update(key + GUID).digest("base64");
|
|
2778
|
+
const headers = [
|
|
2779
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
2780
|
+
"Upgrade: websocket",
|
|
2781
|
+
"Connection: Upgrade",
|
|
2782
|
+
`Sec-WebSocket-Accept: ${digest}`
|
|
2783
|
+
];
|
|
2784
|
+
const ws = new this.options.WebSocket(null, undefined, this.options);
|
|
2785
|
+
if (protocols.size) {
|
|
2786
|
+
const protocol = this.options.handleProtocols ? this.options.handleProtocols(protocols, req) : protocols.values().next().value;
|
|
2787
|
+
if (protocol) {
|
|
2788
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
2789
|
+
ws._protocol = protocol;
|
|
2790
|
+
}
|
|
2791
|
+
}
|
|
2792
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
|
2793
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
|
2794
|
+
const value = extension.format({
|
|
2795
|
+
[PerMessageDeflate.extensionName]: [params]
|
|
2796
|
+
});
|
|
2797
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
|
2798
|
+
ws._extensions = extensions;
|
|
2799
|
+
}
|
|
2800
|
+
this.emit("headers", headers, req);
|
|
2801
|
+
socket.write(headers.concat(`\r
|
|
2802
|
+
`).join(`\r
|
|
2803
|
+
`));
|
|
2804
|
+
socket.removeListener("error", socketOnError);
|
|
2805
|
+
ws.setSocket(socket, head, {
|
|
2806
|
+
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
2807
|
+
maxPayload: this.options.maxPayload,
|
|
2808
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
|
2809
|
+
});
|
|
2810
|
+
if (this.clients) {
|
|
2811
|
+
this.clients.add(ws);
|
|
2812
|
+
ws.on("close", () => {
|
|
2813
|
+
this.clients.delete(ws);
|
|
2814
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
|
2815
|
+
process.nextTick(emitClose, this);
|
|
2816
|
+
}
|
|
2817
|
+
});
|
|
2818
|
+
}
|
|
2819
|
+
cb(ws, req);
|
|
2820
|
+
}
|
|
2821
|
+
}
|
|
2822
|
+
module.exports = WebSocketServer;
|
|
2823
|
+
function addListeners(server, map) {
|
|
2824
|
+
for (const event of Object.keys(map))
|
|
2825
|
+
server.on(event, map[event]);
|
|
2826
|
+
return function removeListeners() {
|
|
2827
|
+
for (const event of Object.keys(map)) {
|
|
2828
|
+
server.removeListener(event, map[event]);
|
|
2829
|
+
}
|
|
2830
|
+
};
|
|
2831
|
+
}
|
|
2832
|
+
function emitClose(server) {
|
|
2833
|
+
server._state = CLOSED;
|
|
2834
|
+
server.emit("close");
|
|
2835
|
+
}
|
|
2836
|
+
function socketOnError() {
|
|
2837
|
+
this.destroy();
|
|
2838
|
+
}
|
|
2839
|
+
function abortHandshake(socket, code, message, headers) {
|
|
2840
|
+
message = message || http.STATUS_CODES[code];
|
|
2841
|
+
headers = {
|
|
2842
|
+
Connection: "close",
|
|
2843
|
+
"Content-Type": "text/html",
|
|
2844
|
+
"Content-Length": Buffer.byteLength(message),
|
|
2845
|
+
...headers
|
|
2846
|
+
};
|
|
2847
|
+
socket.once("finish", socket.destroy);
|
|
2848
|
+
socket.end(`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r
|
|
2849
|
+
` + Object.keys(headers).map((h) => `${h}: ${headers[h]}`).join(`\r
|
|
2850
|
+
`) + `\r
|
|
2851
|
+
\r
|
|
2852
|
+
` + message);
|
|
2853
|
+
}
|
|
2854
|
+
function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
|
|
2855
|
+
if (server.listenerCount("wsClientError")) {
|
|
2856
|
+
const err = new Error(message);
|
|
2857
|
+
Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
|
|
2858
|
+
server.emit("wsClientError", err, socket, req);
|
|
2859
|
+
} else {
|
|
2860
|
+
abortHandshake(socket, code, message, headers);
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
});
|
|
2864
|
+
|
|
2865
|
+
// ../../node_modules/.bun/ws@8.19.0/node_modules/ws/wrapper.mjs
|
|
2866
|
+
var import_stream = __toESM(require_stream(), 1);
|
|
2867
|
+
var import_receiver = __toESM(require_receiver(), 1);
|
|
2868
|
+
var import_sender = __toESM(require_sender(), 1);
|
|
2869
|
+
var import_websocket = __toESM(require_websocket(), 1);
|
|
2870
|
+
var import_websocket_server = __toESM(require_websocket_server(), 1);
|
|
2871
|
+
var wrapper_default = import_websocket.default;
|
|
2872
|
+
|
|
2873
|
+
// ../shared/dist/index.js
|
|
2874
|
+
import { randomUUID } from "crypto";
|
|
2875
|
+
function generateMessageId() {
|
|
2876
|
+
return `msg_${randomUUID()}`;
|
|
2877
|
+
}
|
|
2878
|
+
function createMessage(type, payload) {
|
|
2879
|
+
return {
|
|
2880
|
+
type,
|
|
2881
|
+
timestamp: Date.now(),
|
|
2882
|
+
messageId: generateMessageId(),
|
|
2883
|
+
...payload
|
|
2884
|
+
};
|
|
2885
|
+
}
|
|
2886
|
+
function parseMessage(data) {
|
|
2887
|
+
try {
|
|
2888
|
+
const parsed = JSON.parse(data);
|
|
2889
|
+
if (typeof parsed.type === "string" && typeof parsed.timestamp === "number") {
|
|
2890
|
+
return parsed;
|
|
2891
|
+
}
|
|
2892
|
+
return null;
|
|
2893
|
+
} catch {
|
|
2894
|
+
return null;
|
|
2895
|
+
}
|
|
2896
|
+
}
|
|
2897
|
+
function serializeMessage(message2) {
|
|
2898
|
+
return JSON.stringify(message2);
|
|
2899
|
+
}
|
|
2900
|
+
|
|
2901
|
+
// src/connection/hub-client.ts
|
|
2902
|
+
class HubClient {
|
|
2903
|
+
options;
|
|
2904
|
+
ws = null;
|
|
2905
|
+
sessionId = null;
|
|
2906
|
+
reconnectTimer = null;
|
|
2907
|
+
heartbeatTimer = null;
|
|
2908
|
+
isConnecting = false;
|
|
2909
|
+
shouldReconnect = true;
|
|
2910
|
+
constructor(options) {
|
|
2911
|
+
this.options = options;
|
|
2912
|
+
}
|
|
2913
|
+
connect() {
|
|
2914
|
+
if (this.ws || this.isConnecting)
|
|
2915
|
+
return;
|
|
2916
|
+
this.isConnecting = true;
|
|
2917
|
+
this.shouldReconnect = true;
|
|
2918
|
+
try {
|
|
2919
|
+
this.ws = new wrapper_default(this.options.hubUrl);
|
|
2920
|
+
this.ws.on("open", () => {
|
|
2921
|
+
this.isConnecting = false;
|
|
2922
|
+
this.sendConnect();
|
|
2923
|
+
this.startHeartbeat();
|
|
2924
|
+
});
|
|
2925
|
+
this.ws.on("message", (data) => {
|
|
2926
|
+
const message = parseMessage(data.toString());
|
|
2927
|
+
if (message) {
|
|
2928
|
+
this.handleMessage(message);
|
|
2929
|
+
}
|
|
2930
|
+
});
|
|
2931
|
+
this.ws.on("close", () => {
|
|
2932
|
+
this.cleanup();
|
|
2933
|
+
this.options.onDisconnected?.();
|
|
2934
|
+
this.scheduleReconnect();
|
|
2935
|
+
});
|
|
2936
|
+
this.ws.on("error", (error) => {
|
|
2937
|
+
this.options.onError?.(error);
|
|
2938
|
+
});
|
|
2939
|
+
} catch (error) {
|
|
2940
|
+
this.isConnecting = false;
|
|
2941
|
+
this.options.onError?.(error);
|
|
2942
|
+
this.scheduleReconnect();
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
disconnect() {
|
|
2946
|
+
this.shouldReconnect = false;
|
|
2947
|
+
this.cleanup();
|
|
2948
|
+
if (this.ws) {
|
|
2949
|
+
this.ws.close();
|
|
2950
|
+
this.ws = null;
|
|
2951
|
+
}
|
|
2952
|
+
}
|
|
2953
|
+
submitIntent(prompt) {
|
|
2954
|
+
if (!this.ws || this.ws.readyState !== wrapper_default.OPEN) {
|
|
2955
|
+
throw new Error("Not connected to hub");
|
|
2956
|
+
}
|
|
2957
|
+
const message = createMessage("agent:intent:submit", {
|
|
2958
|
+
payload: { prompt }
|
|
2959
|
+
});
|
|
2960
|
+
this.ws.send(serializeMessage(message));
|
|
2961
|
+
}
|
|
2962
|
+
isConnected() {
|
|
2963
|
+
return this.ws !== null && this.ws.readyState === wrapper_default.OPEN;
|
|
2964
|
+
}
|
|
2965
|
+
getSessionId() {
|
|
2966
|
+
return this.sessionId;
|
|
2967
|
+
}
|
|
2968
|
+
sendConnect() {
|
|
2969
|
+
if (!this.ws)
|
|
2970
|
+
return;
|
|
2971
|
+
const message = createMessage("agent:connect", {
|
|
2972
|
+
payload: {
|
|
2973
|
+
userId: this.options.userId,
|
|
2974
|
+
username: this.options.username,
|
|
2975
|
+
projectPath: this.options.projectPath
|
|
2976
|
+
}
|
|
2977
|
+
});
|
|
2978
|
+
this.ws.send(serializeMessage(message));
|
|
2979
|
+
}
|
|
2980
|
+
handleMessage(message) {
|
|
2981
|
+
if (message.type === "hub:connected") {
|
|
2982
|
+
this.sessionId = message.payload.sessionId;
|
|
2983
|
+
this.options.onConnected?.(this.sessionId);
|
|
2984
|
+
}
|
|
2985
|
+
this.options.onMessage?.(message);
|
|
2986
|
+
}
|
|
2987
|
+
startHeartbeat() {
|
|
2988
|
+
this.heartbeatTimer = setInterval(() => {
|
|
2989
|
+
if (this.ws && this.ws.readyState === wrapper_default.OPEN) {
|
|
2990
|
+
const message = createMessage("agent:heartbeat", {});
|
|
2991
|
+
this.ws.send(serializeMessage(message));
|
|
2992
|
+
}
|
|
2993
|
+
}, 30000);
|
|
2994
|
+
}
|
|
2995
|
+
cleanup() {
|
|
2996
|
+
if (this.heartbeatTimer) {
|
|
2997
|
+
clearInterval(this.heartbeatTimer);
|
|
2998
|
+
this.heartbeatTimer = null;
|
|
2999
|
+
}
|
|
3000
|
+
if (this.reconnectTimer) {
|
|
3001
|
+
clearTimeout(this.reconnectTimer);
|
|
3002
|
+
this.reconnectTimer = null;
|
|
3003
|
+
}
|
|
3004
|
+
this.sessionId = null;
|
|
3005
|
+
this.isConnecting = false;
|
|
3006
|
+
}
|
|
3007
|
+
scheduleReconnect() {
|
|
3008
|
+
if (!this.shouldReconnect)
|
|
3009
|
+
return;
|
|
3010
|
+
const interval = this.options.reconnectInterval ?? 5000;
|
|
3011
|
+
this.reconnectTimer = setTimeout(() => {
|
|
3012
|
+
this.ws = null;
|
|
3013
|
+
this.connect();
|
|
3014
|
+
}, interval);
|
|
3015
|
+
}
|
|
3016
|
+
}
|
|
3017
|
+
// src/plugin/intent-hub-plugin.ts
|
|
3018
|
+
class IntentHubPlugin {
|
|
3019
|
+
client = null;
|
|
3020
|
+
config = null;
|
|
3021
|
+
currentLocks = [];
|
|
3022
|
+
messageCallbacks = new Set;
|
|
3023
|
+
connectedCallbacks = new Set;
|
|
3024
|
+
disconnectedCallbacks = new Set;
|
|
3025
|
+
lockAcquiredCallbacks = new Set;
|
|
3026
|
+
lockBlockedCallbacks = new Set;
|
|
3027
|
+
lockUpdateCallbacks = new Set;
|
|
3028
|
+
initialize(config) {
|
|
3029
|
+
this.config = config;
|
|
3030
|
+
this.client = new HubClient({
|
|
3031
|
+
hubUrl: config.hubUrl,
|
|
3032
|
+
userId: config.userId,
|
|
3033
|
+
username: config.username,
|
|
3034
|
+
projectPath: config.projectPath,
|
|
3035
|
+
onConnected: (sessionId) => {
|
|
3036
|
+
this.connectedCallbacks.forEach((cb) => cb(sessionId));
|
|
3037
|
+
},
|
|
3038
|
+
onDisconnected: () => {
|
|
3039
|
+
this.disconnectedCallbacks.forEach((cb) => cb());
|
|
3040
|
+
},
|
|
3041
|
+
onMessage: (message) => {
|
|
3042
|
+
this.handleMessage(message);
|
|
3043
|
+
this.messageCallbacks.forEach((cb) => cb(message));
|
|
3044
|
+
},
|
|
3045
|
+
onError: (error) => {
|
|
3046
|
+
console.error("[IntentHub] Connection error:", error.message);
|
|
3047
|
+
}
|
|
3048
|
+
});
|
|
3049
|
+
}
|
|
3050
|
+
connect() {
|
|
3051
|
+
if (!this.client) {
|
|
3052
|
+
throw new Error("Plugin not initialized. Call initialize() first.");
|
|
3053
|
+
}
|
|
3054
|
+
this.client.connect();
|
|
3055
|
+
}
|
|
3056
|
+
disconnect() {
|
|
3057
|
+
this.client?.disconnect();
|
|
3058
|
+
}
|
|
3059
|
+
submitIntent(prompt) {
|
|
3060
|
+
if (!this.client) {
|
|
3061
|
+
throw new Error("Plugin not initialized");
|
|
3062
|
+
}
|
|
3063
|
+
this.client.submitIntent(prompt);
|
|
3064
|
+
}
|
|
3065
|
+
isConnected() {
|
|
3066
|
+
return this.client?.isConnected() ?? false;
|
|
3067
|
+
}
|
|
3068
|
+
getSessionId() {
|
|
3069
|
+
return this.client?.getSessionId() ?? null;
|
|
3070
|
+
}
|
|
3071
|
+
getCurrentLocks() {
|
|
3072
|
+
return [...this.currentLocks];
|
|
3073
|
+
}
|
|
3074
|
+
onMessage(callback) {
|
|
3075
|
+
this.messageCallbacks.add(callback);
|
|
3076
|
+
return () => this.messageCallbacks.delete(callback);
|
|
3077
|
+
}
|
|
3078
|
+
onConnected(callback) {
|
|
3079
|
+
this.connectedCallbacks.add(callback);
|
|
3080
|
+
return () => this.connectedCallbacks.delete(callback);
|
|
3081
|
+
}
|
|
3082
|
+
onDisconnected(callback) {
|
|
3083
|
+
this.disconnectedCallbacks.add(callback);
|
|
3084
|
+
return () => this.disconnectedCallbacks.delete(callback);
|
|
3085
|
+
}
|
|
3086
|
+
onLockAcquired(callback) {
|
|
3087
|
+
this.lockAcquiredCallbacks.add(callback);
|
|
3088
|
+
return () => this.lockAcquiredCallbacks.delete(callback);
|
|
3089
|
+
}
|
|
3090
|
+
onLockBlocked(callback) {
|
|
3091
|
+
this.lockBlockedCallbacks.add(callback);
|
|
3092
|
+
return () => this.lockBlockedCallbacks.delete(callback);
|
|
3093
|
+
}
|
|
3094
|
+
onLockUpdate(callback) {
|
|
3095
|
+
this.lockUpdateCallbacks.add(callback);
|
|
3096
|
+
return () => this.lockUpdateCallbacks.delete(callback);
|
|
3097
|
+
}
|
|
3098
|
+
handleMessage(message) {
|
|
3099
|
+
switch (message.type) {
|
|
3100
|
+
case "hub:lock:acquired": {
|
|
3101
|
+
const payload = message.payload;
|
|
3102
|
+
this.lockAcquiredCallbacks.forEach((cb) => cb(payload.intentId, payload.domains));
|
|
3103
|
+
break;
|
|
3104
|
+
}
|
|
3105
|
+
case "hub:lock:blocked": {
|
|
3106
|
+
const payload = message.payload;
|
|
3107
|
+
this.lockBlockedCallbacks.forEach((cb) => cb({
|
|
3108
|
+
intentId: payload.intentId,
|
|
3109
|
+
blockedBy: payload.blockedBy
|
|
3110
|
+
}));
|
|
3111
|
+
break;
|
|
3112
|
+
}
|
|
3113
|
+
case "broadcast:lock:update": {
|
|
3114
|
+
const payload = message.payload;
|
|
3115
|
+
this.currentLocks = payload.locks;
|
|
3116
|
+
this.lockUpdateCallbacks.forEach((cb) => cb(this.currentLocks));
|
|
3117
|
+
break;
|
|
3118
|
+
}
|
|
3119
|
+
}
|
|
3120
|
+
}
|
|
3121
|
+
}
|
|
3122
|
+
var intentHub = new IntentHubPlugin;
|
|
3123
|
+
// src/hooks/intent-hub-hooks.ts
|
|
3124
|
+
class IntentHubHooks {
|
|
3125
|
+
client = null;
|
|
3126
|
+
config;
|
|
3127
|
+
currentIntent = null;
|
|
3128
|
+
blockedDomains = new Set;
|
|
3129
|
+
pendingDecisions = new Map;
|
|
3130
|
+
constructor(config) {
|
|
3131
|
+
this.config = config;
|
|
3132
|
+
if (config.autoConnect !== false) {
|
|
3133
|
+
this.connect();
|
|
3134
|
+
}
|
|
3135
|
+
}
|
|
3136
|
+
connect() {
|
|
3137
|
+
this.client = new HubClient({
|
|
3138
|
+
hubUrl: this.config.hubUrl,
|
|
3139
|
+
userId: this.config.userId,
|
|
3140
|
+
username: this.config.username,
|
|
3141
|
+
projectPath: this.config.projectPath,
|
|
3142
|
+
onConnected: (sessionId) => {
|
|
3143
|
+
console.log(`[IntentHub] Connected to hub (session: ${sessionId})`);
|
|
3144
|
+
},
|
|
3145
|
+
onDisconnected: () => {
|
|
3146
|
+
console.log("[IntentHub] Disconnected from hub");
|
|
3147
|
+
},
|
|
3148
|
+
onMessage: (message) => this.handleMessage(message),
|
|
3149
|
+
onError: (error) => {
|
|
3150
|
+
console.error("[IntentHub] Connection error:", error.message);
|
|
3151
|
+
}
|
|
3152
|
+
});
|
|
3153
|
+
this.client.connect();
|
|
3154
|
+
}
|
|
3155
|
+
disconnect() {
|
|
3156
|
+
this.client?.disconnect();
|
|
3157
|
+
this.client = null;
|
|
3158
|
+
}
|
|
3159
|
+
async preToolUse(context) {
|
|
3160
|
+
const editTools = ["edit", "write", "Edit", "Write", "MultiEdit"];
|
|
3161
|
+
if (!editTools.includes(context.toolName)) {
|
|
3162
|
+
return { allow: true };
|
|
3163
|
+
}
|
|
3164
|
+
const filePath = context.toolInput["filePath"];
|
|
3165
|
+
if (!filePath) {
|
|
3166
|
+
return { allow: true };
|
|
3167
|
+
}
|
|
3168
|
+
const domain = this.inferDomainFromPath(filePath);
|
|
3169
|
+
if (this.blockedDomains.has(domain)) {
|
|
3170
|
+
return {
|
|
3171
|
+
allow: false,
|
|
3172
|
+
message: `⚠️ [IntentHub] This domain (${domain}) is currently being worked on by another team member. Please wait or work on a different area.`
|
|
3173
|
+
};
|
|
3174
|
+
}
|
|
3175
|
+
if (!this.currentIntent && this.client?.isConnected()) {
|
|
3176
|
+
const prompt = `Editing file: ${filePath}`;
|
|
3177
|
+
this.client.submitIntent(prompt);
|
|
3178
|
+
}
|
|
3179
|
+
return { allow: true };
|
|
3180
|
+
}
|
|
3181
|
+
async postToolUse(context, result) {
|
|
3182
|
+
const editTools = ["edit", "write", "Edit", "Write", "MultiEdit"];
|
|
3183
|
+
if (!editTools.includes(context.toolName)) {
|
|
3184
|
+
return;
|
|
3185
|
+
}
|
|
3186
|
+
}
|
|
3187
|
+
async onUserPromptSubmit(prompt) {
|
|
3188
|
+
if (!this.client?.isConnected()) {
|
|
3189
|
+
return {};
|
|
3190
|
+
}
|
|
3191
|
+
this.client.submitIntent(prompt);
|
|
3192
|
+
await this.waitForResponse(1000);
|
|
3193
|
+
if (this.blockedDomains.size > 0) {
|
|
3194
|
+
const domains = Array.from(this.blockedDomains).join(", ");
|
|
3195
|
+
return {
|
|
3196
|
+
message: `⚠️ [IntentHub] Note: The following domains are currently locked by other team members: ${domains}. Your work may be queued.`
|
|
3197
|
+
};
|
|
3198
|
+
}
|
|
3199
|
+
return {};
|
|
3200
|
+
}
|
|
3201
|
+
async onStop() {
|
|
3202
|
+
if (this.currentIntent) {
|
|
3203
|
+
this.currentIntent = null;
|
|
3204
|
+
}
|
|
3205
|
+
this.blockedDomains.clear();
|
|
3206
|
+
}
|
|
3207
|
+
submitDecision(conflictId, choice) {
|
|
3208
|
+
const pending = this.pendingDecisions.get(conflictId);
|
|
3209
|
+
if (pending) {
|
|
3210
|
+
pending.resolve(choice);
|
|
3211
|
+
this.pendingDecisions.delete(conflictId);
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
3214
|
+
handleMessage(message) {
|
|
3215
|
+
switch (message.type) {
|
|
3216
|
+
case "hub:lock:acquired": {
|
|
3217
|
+
const payload = message.payload;
|
|
3218
|
+
this.currentIntent = payload.intentId;
|
|
3219
|
+
this.config.onNotification?.("lock:acquired", {
|
|
3220
|
+
domains: payload.domains
|
|
3221
|
+
});
|
|
3222
|
+
break;
|
|
3223
|
+
}
|
|
3224
|
+
case "hub:lock:blocked": {
|
|
3225
|
+
const payload = message.payload;
|
|
3226
|
+
for (const block of payload.blockedBy) {
|
|
3227
|
+
this.blockedDomains.add(block.domain);
|
|
3228
|
+
}
|
|
3229
|
+
this.config.onNotification?.("lock:blocked", {
|
|
3230
|
+
blockedBy: payload.blockedBy
|
|
3231
|
+
});
|
|
3232
|
+
break;
|
|
3233
|
+
}
|
|
3234
|
+
case "hub:negotiation:start": {
|
|
3235
|
+
const payload = message.payload;
|
|
3236
|
+
this.config.onNotification?.("negotiation:start", payload);
|
|
3237
|
+
break;
|
|
3238
|
+
}
|
|
3239
|
+
case "hub:negotiation:resolved": {
|
|
3240
|
+
const payload = message.payload;
|
|
3241
|
+
this.blockedDomains.clear();
|
|
3242
|
+
this.config.onNotification?.("negotiation:resolved", payload);
|
|
3243
|
+
break;
|
|
3244
|
+
}
|
|
3245
|
+
case "hub:decision:required": {
|
|
3246
|
+
const payload = message.payload;
|
|
3247
|
+
this.config.onNotification?.("decision:required", payload);
|
|
3248
|
+
break;
|
|
3249
|
+
}
|
|
3250
|
+
case "broadcast:lock:update": {
|
|
3251
|
+
const payload = message.payload;
|
|
3252
|
+
this.blockedDomains.clear();
|
|
3253
|
+
for (const lock of payload.locks) {
|
|
3254
|
+
if (lock.username !== this.config.username) {
|
|
3255
|
+
this.blockedDomains.add(lock.domainId);
|
|
3256
|
+
}
|
|
3257
|
+
}
|
|
3258
|
+
this.config.onNotification?.("locks:updated", { locks: payload.locks });
|
|
3259
|
+
break;
|
|
3260
|
+
}
|
|
3261
|
+
case "broadcast:user:joined": {
|
|
3262
|
+
const payload = message.payload;
|
|
3263
|
+
this.config.onNotification?.("user:joined", payload);
|
|
3264
|
+
break;
|
|
3265
|
+
}
|
|
3266
|
+
case "broadcast:user:left": {
|
|
3267
|
+
const payload = message.payload;
|
|
3268
|
+
this.config.onNotification?.("user:left", payload);
|
|
3269
|
+
break;
|
|
3270
|
+
}
|
|
3271
|
+
}
|
|
3272
|
+
}
|
|
3273
|
+
inferDomainFromPath(filePath) {
|
|
3274
|
+
const lower = filePath.toLowerCase();
|
|
3275
|
+
if (lower.includes("/auth/") || lower.includes("auth."))
|
|
3276
|
+
return "auth";
|
|
3277
|
+
if (lower.includes("/user/") || lower.includes("user."))
|
|
3278
|
+
return "user";
|
|
3279
|
+
if (lower.includes("/payment/") || lower.includes("payment."))
|
|
3280
|
+
return "payment";
|
|
3281
|
+
if (lower.includes("/api/") || lower.includes("route"))
|
|
3282
|
+
return "api";
|
|
3283
|
+
if (lower.includes("/component") || lower.includes(".tsx") || lower.includes(".jsx"))
|
|
3284
|
+
return "ui";
|
|
3285
|
+
if (lower.includes("/test/") || lower.includes(".test.") || lower.includes(".spec."))
|
|
3286
|
+
return "test";
|
|
3287
|
+
if (lower.includes("config") || lower.includes(".env"))
|
|
3288
|
+
return "config";
|
|
3289
|
+
if (lower.includes("/db/") || lower.includes("database") || lower.includes("migration"))
|
|
3290
|
+
return "database";
|
|
3291
|
+
return "general";
|
|
3292
|
+
}
|
|
3293
|
+
waitForResponse(ms) {
|
|
3294
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3295
|
+
}
|
|
3296
|
+
isConnected() {
|
|
3297
|
+
return this.client?.isConnected() ?? false;
|
|
3298
|
+
}
|
|
3299
|
+
getBlockedDomains() {
|
|
3300
|
+
return Array.from(this.blockedDomains);
|
|
3301
|
+
}
|
|
3302
|
+
getCurrentIntent() {
|
|
3303
|
+
return this.currentIntent;
|
|
3304
|
+
}
|
|
3305
|
+
}
|
|
3306
|
+
function createIntentHubHooks(config) {
|
|
3307
|
+
return new IntentHubHooks(config);
|
|
3308
|
+
}
|
|
3309
|
+
export {
|
|
3310
|
+
intentHub,
|
|
3311
|
+
createIntentHubHooks,
|
|
3312
|
+
IntentHubPlugin,
|
|
3313
|
+
IntentHubHooks,
|
|
3314
|
+
HubClient
|
|
3315
|
+
};
|