huaweicloud-devkit 1.0.2-dev.9 → 1.0.2-next.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/.agents/plugins/marketplace.json +20 -20
- package/LICENSE +201 -201
- package/README.md +163 -138
- package/README.zh-CN.md +161 -138
- package/bin/setup.cjs +8 -8
- package/integrations/opencode/commands/huaweicloud-doctor.md +14 -14
- package/integrations/opencode/opencode.json +14 -14
- package/integrations/opencode/skills/huaweicloud-core/SKILL.md +32 -32
- package/package.json +46 -44
- package/plugins/huaweicloud-core/.claude-plugin/plugin.json +44 -44
- package/plugins/huaweicloud-core/.codex-plugin/plugin.json +43 -43
- package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +44 -44
- package/plugins/huaweicloud-core/.mcp.json +15 -15
- package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +44 -44
- package/plugins/huaweicloud-core/hooks/hooks.json +26 -26
- package/plugins/huaweicloud-core/hooks/huaweicloud-safety.py +136 -136
- package/plugins/huaweicloud-core/safety/policy.json +99 -99
- package/plugins/huaweicloud-core/safety/rules/cloud-risk-rules.json +199 -199
- package/plugins/huaweicloud-core/skills/huawei-apig/SKILL.md +164 -164
- package/plugins/huaweicloud-core/skills/huawei-billing/SKILL.md +48 -48
- package/plugins/huaweicloud-core/skills/huawei-cbr/SKILL.md +50 -50
- package/plugins/huaweicloud-core/skills/huawei-cce/SKILL.md +101 -101
- package/plugins/huaweicloud-core/skills/huawei-cloud-eye/SKILL.md +90 -90
- package/plugins/huaweicloud-core/skills/huawei-cloud-find-skills/SKILL.md +190 -190
- package/plugins/huaweicloud-core/skills/huawei-cloud-find-skills/scripts/search-skills.mjs +124 -124
- package/plugins/huaweicloud-core/skills/huawei-cts/SKILL.md +55 -55
- package/plugins/huaweicloud-core/skills/huawei-dds-dcs/SKILL.md +70 -70
- package/plugins/huaweicloud-core/skills/huawei-deployment/SKILL.md +56 -56
- package/plugins/huaweicloud-core/skills/huawei-dew/SKILL.md +97 -97
- package/plugins/huaweicloud-core/skills/huawei-dew/references/csms-usage.md +29 -29
- package/plugins/huaweicloud-core/skills/huawei-dew/references/kms-usage.md +23 -23
- package/plugins/huaweicloud-core/skills/huawei-ecs/SKILL.md +185 -185
- package/plugins/huaweicloud-core/skills/huawei-ecs/references/create-instance.md +133 -133
- package/plugins/huaweicloud-core/skills/huawei-ecs/references/evs.md +31 -31
- package/plugins/huaweicloud-core/skills/huawei-ecs/references/flavors.md +55 -55
- package/plugins/huaweicloud-core/skills/huawei-ecs/references/troubleshooting.md +52 -52
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/SKILL.md +131 -131
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/references/apig-event-format.md +58 -58
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/references/create-function.md +71 -71
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/references/deploy-workflow.md +119 -119
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/references/triggers.md +142 -142
- package/plugins/huaweicloud-core/skills/huawei-gaussdb/SKILL.md +56 -56
- package/plugins/huaweicloud-core/skills/huawei-getting-started/SKILL.md +65 -65
- package/plugins/huaweicloud-core/skills/huawei-iam/SKILL.md +111 -111
- package/plugins/huaweicloud-core/skills/huawei-iam/references/policy-examples.md +122 -122
- package/plugins/huaweicloud-core/skills/huawei-iam/references/quick-ref.md +56 -56
- package/plugins/huaweicloud-core/skills/huawei-modelarts/SKILL.md +65 -65
- package/plugins/huaweicloud-core/skills/huawei-obs/SKILL.md +133 -127
- package/plugins/huaweicloud-core/skills/huawei-obs/references/bucket-create.md +49 -49
- package/plugins/huaweicloud-core/skills/huawei-obs/references/bucket-lifecycle.md +26 -26
- package/plugins/huaweicloud-core/skills/huawei-obs/references/common-pitfalls.md +100 -100
- package/plugins/huaweicloud-core/skills/huawei-obs/references/single-file-share.md +40 -40
- package/plugins/huaweicloud-core/skills/huawei-obs/references/static-website.md +56 -56
- package/plugins/huaweicloud-core/skills/huawei-rds/SKILL.md +224 -224
- package/plugins/huaweicloud-core/skills/huawei-sandbox/SKILL.md +135 -134
- package/plugins/huaweicloud-core/skills/huawei-smn-dms/SKILL.md +69 -69
- package/plugins/huaweicloud-core/skills/huawei-vpc/SKILL.md +84 -84
- package/plugins/huaweicloud-core/skills/huawei-vpc/references/network.md +76 -76
- package/plugins/huaweicloud-core/skills/huawei-vpc/references/security-group.md +59 -59
- package/plugins/huaweicloud-core/skills/huawei-vpc/references/subnet.md +50 -50
- package/plugins/huaweicloud-core/skills/huawei-waf-aad/SKILL.md +63 -63
- package/plugins/huaweicloud-core/skills/huaweicloud-api-and-sdk/SKILL.md +56 -56
- package/plugins/huaweicloud-core/skills/huaweicloud-api-and-sdk/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-capability-discovery/SKILL.md +51 -47
- package/plugins/huaweicloud-core/skills/huaweicloud-capability-discovery/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/SKILL.md +128 -128
- package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-core/SKILL.md +110 -95
- package/plugins/huaweicloud-core/skills/huaweicloud-core/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-core/references/report-issue.md +19 -19
- package/plugins/huaweicloud-core/skills/huaweicloud-core/references/select.md +27 -27
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/SKILL.md +60 -60
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-troubleshooting/SKILL.md +83 -83
- package/plugins/huaweicloud-core/skills/huaweicloud-troubleshooting/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/src/auth/agent-registration.mjs +105 -87
- package/plugins/huaweicloud-core/src/auth/credentials.mjs +95 -95
- package/plugins/huaweicloud-core/src/auth/service.mjs +63 -63
- package/plugins/huaweicloud-core/src/hcloud-cli.mjs +265 -265
- package/plugins/huaweicloud-core/src/mcp-server.mjs +129 -129
- package/plugins/huaweicloud-core/src/proxy/proxy-agent.mjs +155 -0
- package/plugins/huaweicloud-core/src/proxy/proxy-config.mjs +87 -0
- package/plugins/huaweicloud-core/src/risk-rule-engine.mjs +137 -137
- package/plugins/huaweicloud-core/src/safety-policy.mjs +302 -302
- package/plugins/huaweicloud-core/src/sandbox/hdkitservice-api.mjs +91 -87
- package/plugins/huaweicloud-core/src/sandbox/hwlink-api.mjs +162 -153
- package/plugins/huaweicloud-core/src/sandbox/session-manager.mjs +112 -105
- package/plugins/huaweicloud-core/src/search-market.mjs +174 -169
- package/plugins/huaweicloud-core/src/setup-cli.mjs +2017 -1594
- package/plugins/huaweicloud-core/src/tools.mjs +1008 -993
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-exec-client.js +427 -427
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-fair-queue.js +132 -132
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-multiplexer.js +227 -227
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-packet.js +202 -202
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-terminal-channel.js +158 -158
- package/plugins/huaweicloud-core/src/ws-exec/index.js +19 -19
- package/plugins/huaweicloud-core/src/ws-exec/ws-exec-client.js +338 -338
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
import { Buffer } from 'node:buffer';
|
|
2
|
-
|
|
3
|
-
import { FairQueue } from './hwlink-fair-queue.js';
|
|
4
|
-
import { formatPacketOneLine, parsePacket } from './hwlink-packet.js';
|
|
5
|
-
|
|
6
|
-
const WS_OPEN = 1;
|
|
7
|
-
|
|
8
|
-
function addWsListener(ws, event, handler) {
|
|
9
|
-
if (typeof ws.on === 'function') {
|
|
10
|
-
ws.on(event, handler);
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
if (typeof ws.addEventListener === 'function') {
|
|
14
|
-
ws.addEventListener(event, handler);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
throw new Error('WebSocket implementation does not support event listeners');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function closeQuietly(ws) {
|
|
21
|
-
try {
|
|
22
|
-
ws.close();
|
|
23
|
-
} catch {
|
|
24
|
-
// Close is best-effort during error settlement.
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function extractMessageData(eventOrData) {
|
|
29
|
-
if (
|
|
30
|
-
eventOrData &&
|
|
31
|
-
typeof eventOrData === 'object' &&
|
|
32
|
-
!Buffer.isBuffer(eventOrData) &&
|
|
33
|
-
!(eventOrData instanceof ArrayBuffer) &&
|
|
34
|
-
!ArrayBuffer.isView(eventOrData) &&
|
|
35
|
-
'data' in eventOrData
|
|
36
|
-
) {
|
|
37
|
-
return eventOrData.data;
|
|
38
|
-
}
|
|
39
|
-
return eventOrData;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function eventDataToUint8Array(data) {
|
|
43
|
-
if (data instanceof Uint8Array) return Promise.resolve(data);
|
|
44
|
-
if (Buffer.isBuffer(data)) {
|
|
45
|
-
return Promise.resolve(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
|
|
46
|
-
}
|
|
47
|
-
if (data instanceof ArrayBuffer) return Promise.resolve(new Uint8Array(data));
|
|
48
|
-
if (ArrayBuffer.isView(data)) {
|
|
49
|
-
return Promise.resolve(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
|
|
50
|
-
}
|
|
51
|
-
if (data && typeof data.arrayBuffer === 'function') {
|
|
52
|
-
return data.arrayBuffer().then((buffer) => new Uint8Array(buffer));
|
|
53
|
-
}
|
|
54
|
-
return Promise.reject(new Error(`unsupported websocket message data: ${typeof data}`));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function sendBinary(ws, data) {
|
|
58
|
-
if (ws.readyState !== WS_OPEN) return Promise.resolve();
|
|
59
|
-
|
|
60
|
-
return new Promise((resolve, reject) => {
|
|
61
|
-
let settled = false;
|
|
62
|
-
const done = (err) => {
|
|
63
|
-
if (settled) return;
|
|
64
|
-
settled = true;
|
|
65
|
-
if (err) reject(err);
|
|
66
|
-
else resolve();
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
const maybePromise = ws.send(data, done);
|
|
71
|
-
if (maybePromise && typeof maybePromise.then === 'function') {
|
|
72
|
-
maybePromise.then(() => done(), done);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
if (ws.send.length < 2) done();
|
|
76
|
-
} catch (error) {
|
|
77
|
-
done(error);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
class HwlinkWebSocketMultiplexer {
|
|
83
|
-
constructor(url, source, options = {}) {
|
|
84
|
-
const {
|
|
85
|
-
WebSocketImpl = globalThis.WebSocket,
|
|
86
|
-
protocol = 'devenv',
|
|
87
|
-
onFrame,
|
|
88
|
-
trace = false,
|
|
89
|
-
} = options;
|
|
90
|
-
|
|
91
|
-
if (typeof WebSocketImpl !== 'function') {
|
|
92
|
-
throw new Error('global WebSocket is unavailable; use Node.js 22+ or pass WebSocketImpl');
|
|
93
|
-
}
|
|
94
|
-
if (!Number.isInteger(source) || source < -0x80000000 || source > 0xffffffff) {
|
|
95
|
-
throw new Error('hwlink source must be an int32 or uint32 number');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
this.url = url;
|
|
99
|
-
this.source = source;
|
|
100
|
-
this.onFrame = onFrame;
|
|
101
|
-
this.trace = trace;
|
|
102
|
-
this.channels = new Map();
|
|
103
|
-
this.unknownIdentifierHandler = null;
|
|
104
|
-
this.onClose = null;
|
|
105
|
-
this.onError = null;
|
|
106
|
-
this.closed = false;
|
|
107
|
-
this.queue = new FairQueue((data) => this.wsSend(data));
|
|
108
|
-
this.ws = new WebSocketImpl(url, protocol);
|
|
109
|
-
|
|
110
|
-
if ('binaryType' in this.ws) {
|
|
111
|
-
this.ws.binaryType = 'arraybuffer';
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
addWsListener(this.ws, 'open', () => {
|
|
115
|
-
for (const ch of this.channels.values()) ch.onopen();
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
addWsListener(this.ws, 'message', (eventOrData) => {
|
|
119
|
-
this.handleMessage(extractMessageData(eventOrData)).catch((error) => {
|
|
120
|
-
this.handleError(error);
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
addWsListener(this.ws, 'error', (eventOrError) => {
|
|
125
|
-
const error = eventOrError instanceof Error
|
|
126
|
-
? eventOrError
|
|
127
|
-
: new Error('hwlink websocket error');
|
|
128
|
-
this.handleError(error);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
addWsListener(this.ws, 'close', (eventOrCode, maybeReason) => {
|
|
132
|
-
this.closed = true;
|
|
133
|
-
const closeEvent = this.normalizeCloseEvent(eventOrCode, maybeReason);
|
|
134
|
-
for (const ch of this.channels.values()) ch.onclose(closeEvent);
|
|
135
|
-
if (this.onClose) this.onClose(closeEvent);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
normalizeCloseEvent(eventOrCode, maybeReason) {
|
|
140
|
-
if (typeof eventOrCode === 'number') {
|
|
141
|
-
return {
|
|
142
|
-
code: eventOrCode,
|
|
143
|
-
reason: Buffer.isBuffer(maybeReason) ? maybeReason.toString() : String(maybeReason || ''),
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
code: eventOrCode && typeof eventOrCode.code === 'number' ? eventOrCode.code : 0,
|
|
149
|
-
reason: eventOrCode && eventOrCode.reason ? String(eventOrCode.reason) : '',
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
register(ch) {
|
|
154
|
-
this.channels.set(ch.identifier, ch);
|
|
155
|
-
this.queue.register(ch);
|
|
156
|
-
if (this.ws.readyState === WS_OPEN) {
|
|
157
|
-
ch.onopen();
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
unregister(ch) {
|
|
162
|
-
this.channels.delete(ch.identifier);
|
|
163
|
-
this.queue.unregister(ch);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
sendFairly(ch, data) {
|
|
167
|
-
this.queue.sendFairly(ch, data);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
sendImmediately(data) {
|
|
171
|
-
this.queue.sendImmediately(data);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
get readyState() {
|
|
175
|
-
return this.ws.readyState;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async handleMessage(data) {
|
|
179
|
-
const bytes = await eventDataToUint8Array(data);
|
|
180
|
-
const packet = parsePacket(bytes);
|
|
181
|
-
this.reportFrame('in', packet);
|
|
182
|
-
|
|
183
|
-
const ch = this.channels.get(packet.identifier);
|
|
184
|
-
if (ch) {
|
|
185
|
-
ch.onmessage(packet);
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (this.unknownIdentifierHandler) {
|
|
190
|
-
this.unknownIdentifierHandler(packet, this);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
wsSend(data) {
|
|
195
|
-
const packet = parsePacket(data);
|
|
196
|
-
this.reportFrame('out', packet);
|
|
197
|
-
return sendBinary(this.ws, data);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
reportFrame(direction, packet) {
|
|
201
|
-
if (this.onFrame) {
|
|
202
|
-
this.onFrame({ direction, packet, line: formatPacketOneLine(packet) });
|
|
203
|
-
}
|
|
204
|
-
if (this.trace) {
|
|
205
|
-
const arrow = direction === 'in' ? '<-' : '->';
|
|
206
|
-
console.error(`${arrow} ${formatPacketOneLine(packet)}`);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
handleError(error) {
|
|
211
|
-
for (const ch of this.channels.values()) ch.onerror(error);
|
|
212
|
-
if (this.onError) this.onError(error);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
close() {
|
|
216
|
-
if (this.closed) return;
|
|
217
|
-
this.closed = true;
|
|
218
|
-
closeQuietly(this.ws);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export {
|
|
223
|
-
HwlinkWebSocketMultiplexer,
|
|
224
|
-
addWsListener,
|
|
225
|
-
closeQuietly,
|
|
226
|
-
eventDataToUint8Array,
|
|
227
|
-
};
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
|
|
3
|
+
import { FairQueue } from './hwlink-fair-queue.js';
|
|
4
|
+
import { formatPacketOneLine, parsePacket } from './hwlink-packet.js';
|
|
5
|
+
|
|
6
|
+
const WS_OPEN = 1;
|
|
7
|
+
|
|
8
|
+
function addWsListener(ws, event, handler) {
|
|
9
|
+
if (typeof ws.on === 'function') {
|
|
10
|
+
ws.on(event, handler);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (typeof ws.addEventListener === 'function') {
|
|
14
|
+
ws.addEventListener(event, handler);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
throw new Error('WebSocket implementation does not support event listeners');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function closeQuietly(ws) {
|
|
21
|
+
try {
|
|
22
|
+
ws.close();
|
|
23
|
+
} catch {
|
|
24
|
+
// Close is best-effort during error settlement.
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function extractMessageData(eventOrData) {
|
|
29
|
+
if (
|
|
30
|
+
eventOrData &&
|
|
31
|
+
typeof eventOrData === 'object' &&
|
|
32
|
+
!Buffer.isBuffer(eventOrData) &&
|
|
33
|
+
!(eventOrData instanceof ArrayBuffer) &&
|
|
34
|
+
!ArrayBuffer.isView(eventOrData) &&
|
|
35
|
+
'data' in eventOrData
|
|
36
|
+
) {
|
|
37
|
+
return eventOrData.data;
|
|
38
|
+
}
|
|
39
|
+
return eventOrData;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function eventDataToUint8Array(data) {
|
|
43
|
+
if (data instanceof Uint8Array) return Promise.resolve(data);
|
|
44
|
+
if (Buffer.isBuffer(data)) {
|
|
45
|
+
return Promise.resolve(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
|
|
46
|
+
}
|
|
47
|
+
if (data instanceof ArrayBuffer) return Promise.resolve(new Uint8Array(data));
|
|
48
|
+
if (ArrayBuffer.isView(data)) {
|
|
49
|
+
return Promise.resolve(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
|
|
50
|
+
}
|
|
51
|
+
if (data && typeof data.arrayBuffer === 'function') {
|
|
52
|
+
return data.arrayBuffer().then((buffer) => new Uint8Array(buffer));
|
|
53
|
+
}
|
|
54
|
+
return Promise.reject(new Error(`unsupported websocket message data: ${typeof data}`));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function sendBinary(ws, data) {
|
|
58
|
+
if (ws.readyState !== WS_OPEN) return Promise.resolve();
|
|
59
|
+
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
let settled = false;
|
|
62
|
+
const done = (err) => {
|
|
63
|
+
if (settled) return;
|
|
64
|
+
settled = true;
|
|
65
|
+
if (err) reject(err);
|
|
66
|
+
else resolve();
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const maybePromise = ws.send(data, done);
|
|
71
|
+
if (maybePromise && typeof maybePromise.then === 'function') {
|
|
72
|
+
maybePromise.then(() => done(), done);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (ws.send.length < 2) done();
|
|
76
|
+
} catch (error) {
|
|
77
|
+
done(error);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
class HwlinkWebSocketMultiplexer {
|
|
83
|
+
constructor(url, source, options = {}) {
|
|
84
|
+
const {
|
|
85
|
+
WebSocketImpl = globalThis.WebSocket,
|
|
86
|
+
protocol = 'devenv',
|
|
87
|
+
onFrame,
|
|
88
|
+
trace = false,
|
|
89
|
+
} = options;
|
|
90
|
+
|
|
91
|
+
if (typeof WebSocketImpl !== 'function') {
|
|
92
|
+
throw new Error('global WebSocket is unavailable; use Node.js 22+ or pass WebSocketImpl');
|
|
93
|
+
}
|
|
94
|
+
if (!Number.isInteger(source) || source < -0x80000000 || source > 0xffffffff) {
|
|
95
|
+
throw new Error('hwlink source must be an int32 or uint32 number');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this.url = url;
|
|
99
|
+
this.source = source;
|
|
100
|
+
this.onFrame = onFrame;
|
|
101
|
+
this.trace = trace;
|
|
102
|
+
this.channels = new Map();
|
|
103
|
+
this.unknownIdentifierHandler = null;
|
|
104
|
+
this.onClose = null;
|
|
105
|
+
this.onError = null;
|
|
106
|
+
this.closed = false;
|
|
107
|
+
this.queue = new FairQueue((data) => this.wsSend(data));
|
|
108
|
+
this.ws = new WebSocketImpl(url, protocol);
|
|
109
|
+
|
|
110
|
+
if ('binaryType' in this.ws) {
|
|
111
|
+
this.ws.binaryType = 'arraybuffer';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
addWsListener(this.ws, 'open', () => {
|
|
115
|
+
for (const ch of this.channels.values()) ch.onopen();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
addWsListener(this.ws, 'message', (eventOrData) => {
|
|
119
|
+
this.handleMessage(extractMessageData(eventOrData)).catch((error) => {
|
|
120
|
+
this.handleError(error);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
addWsListener(this.ws, 'error', (eventOrError) => {
|
|
125
|
+
const error = eventOrError instanceof Error
|
|
126
|
+
? eventOrError
|
|
127
|
+
: new Error('hwlink websocket error');
|
|
128
|
+
this.handleError(error);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
addWsListener(this.ws, 'close', (eventOrCode, maybeReason) => {
|
|
132
|
+
this.closed = true;
|
|
133
|
+
const closeEvent = this.normalizeCloseEvent(eventOrCode, maybeReason);
|
|
134
|
+
for (const ch of this.channels.values()) ch.onclose(closeEvent);
|
|
135
|
+
if (this.onClose) this.onClose(closeEvent);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
normalizeCloseEvent(eventOrCode, maybeReason) {
|
|
140
|
+
if (typeof eventOrCode === 'number') {
|
|
141
|
+
return {
|
|
142
|
+
code: eventOrCode,
|
|
143
|
+
reason: Buffer.isBuffer(maybeReason) ? maybeReason.toString() : String(maybeReason || ''),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
code: eventOrCode && typeof eventOrCode.code === 'number' ? eventOrCode.code : 0,
|
|
149
|
+
reason: eventOrCode && eventOrCode.reason ? String(eventOrCode.reason) : '',
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
register(ch) {
|
|
154
|
+
this.channels.set(ch.identifier, ch);
|
|
155
|
+
this.queue.register(ch);
|
|
156
|
+
if (this.ws.readyState === WS_OPEN) {
|
|
157
|
+
ch.onopen();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
unregister(ch) {
|
|
162
|
+
this.channels.delete(ch.identifier);
|
|
163
|
+
this.queue.unregister(ch);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
sendFairly(ch, data) {
|
|
167
|
+
this.queue.sendFairly(ch, data);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
sendImmediately(data) {
|
|
171
|
+
this.queue.sendImmediately(data);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
get readyState() {
|
|
175
|
+
return this.ws.readyState;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async handleMessage(data) {
|
|
179
|
+
const bytes = await eventDataToUint8Array(data);
|
|
180
|
+
const packet = parsePacket(bytes);
|
|
181
|
+
this.reportFrame('in', packet);
|
|
182
|
+
|
|
183
|
+
const ch = this.channels.get(packet.identifier);
|
|
184
|
+
if (ch) {
|
|
185
|
+
ch.onmessage(packet);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (this.unknownIdentifierHandler) {
|
|
190
|
+
this.unknownIdentifierHandler(packet, this);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
wsSend(data) {
|
|
195
|
+
const packet = parsePacket(data);
|
|
196
|
+
this.reportFrame('out', packet);
|
|
197
|
+
return sendBinary(this.ws, data);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
reportFrame(direction, packet) {
|
|
201
|
+
if (this.onFrame) {
|
|
202
|
+
this.onFrame({ direction, packet, line: formatPacketOneLine(packet) });
|
|
203
|
+
}
|
|
204
|
+
if (this.trace) {
|
|
205
|
+
const arrow = direction === 'in' ? '<-' : '->';
|
|
206
|
+
console.error(`${arrow} ${formatPacketOneLine(packet)}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
handleError(error) {
|
|
211
|
+
for (const ch of this.channels.values()) ch.onerror(error);
|
|
212
|
+
if (this.onError) this.onError(error);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
close() {
|
|
216
|
+
if (this.closed) return;
|
|
217
|
+
this.closed = true;
|
|
218
|
+
closeQuietly(this.ws);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export {
|
|
223
|
+
HwlinkWebSocketMultiplexer,
|
|
224
|
+
addWsListener,
|
|
225
|
+
closeQuietly,
|
|
226
|
+
eventDataToUint8Array,
|
|
227
|
+
};
|