@hncode/hncode 0.8.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/README.md +30 -0
  2. package/README_CN.md +27 -0
  3. package/package.json +1 -1
  4. package/src/agent.js +172 -43
  5. package/src/bootstrap.js +63 -56
  6. package/src/ci.js +247 -247
  7. package/src/colors.js +5 -1
  8. package/src/commands.js +121 -0
  9. package/src/composer.js +31 -1
  10. package/src/config.js +177 -45
  11. package/src/context.js +29 -0
  12. package/src/files.js +32 -0
  13. package/src/fx.js +19 -2
  14. package/src/i18n.js +180 -131
  15. package/src/index.js +558 -77
  16. package/src/permission-gate.js +103 -0
  17. package/src/plugin.js +221 -2
  18. package/src/prompt-presets.js +36 -0
  19. package/src/protocol.js +184 -0
  20. package/src/registry-browser.js +14 -3
  21. package/src/remote.js +117 -5
  22. package/src/schedule.js +148 -11
  23. package/src/serve.js +613 -0
  24. package/src/session-client.js +148 -0
  25. package/src/session-host.js +52 -0
  26. package/src/session.js +26 -3
  27. package/src/settings-schema.js +2 -2
  28. package/src/status-line.js +48 -0
  29. package/src/term.js +20 -1
  30. package/src/toml.js +18 -4
  31. package/src/tool-line.js +13 -6
  32. package/src/tool-status.js +45 -0
  33. package/src/tools/bash.js +43 -24
  34. package/src/tools/edit.js +10 -5
  35. package/src/tools/web-search.js +8 -5
  36. package/src/tools/write.js +28 -3
  37. package/src/tui.js +498 -1397
  38. package/src/ws.js +248 -0
  39. package/src/session-hub.js +0 -445
  40. package/src/web-auth.js +0 -98
  41. package/src/web-daemon-cli.js +0 -63
  42. package/src/web-daemon-client.js +0 -203
  43. package/src/web-daemon.js +0 -894
  44. package/src/web-live.js +0 -250
  45. package/src/web-public/app.css +0 -1524
  46. package/src/web-public/app.js +0 -2969
  47. package/src/web-public/complete.js +0 -154
  48. package/src/web-public/fonts/JetBrainsMono-Bold.woff2 +0 -0
  49. package/src/web-public/fonts/JetBrainsMono-Regular.woff2 +0 -0
  50. package/src/web-public/fonts/noto-sans-sc-100-400-normal.woff2 +0 -0
  51. package/src/web-public/fonts/noto-sans-sc-101-400-normal.woff2 +0 -0
  52. package/src/web-public/fonts/noto-sans-sc-102-400-normal.woff2 +0 -0
  53. package/src/web-public/fonts/noto-sans-sc-103-400-normal.woff2 +0 -0
  54. package/src/web-public/fonts/noto-sans-sc-104-400-normal.woff2 +0 -0
  55. package/src/web-public/fonts/noto-sans-sc-105-400-normal.woff2 +0 -0
  56. package/src/web-public/fonts/noto-sans-sc-106-400-normal.woff2 +0 -0
  57. package/src/web-public/fonts/noto-sans-sc-107-400-normal.woff2 +0 -0
  58. package/src/web-public/fonts/noto-sans-sc-108-400-normal.woff2 +0 -0
  59. package/src/web-public/fonts/noto-sans-sc-109-400-normal.woff2 +0 -0
  60. package/src/web-public/fonts/noto-sans-sc-110-400-normal.woff2 +0 -0
  61. package/src/web-public/fonts/noto-sans-sc-111-400-normal.woff2 +0 -0
  62. package/src/web-public/fonts/noto-sans-sc-112-400-normal.woff2 +0 -0
  63. package/src/web-public/fonts/noto-sans-sc-113-400-normal.woff2 +0 -0
  64. package/src/web-public/fonts/noto-sans-sc-114-400-normal.woff2 +0 -0
  65. package/src/web-public/fonts/noto-sans-sc-115-400-normal.woff2 +0 -0
  66. package/src/web-public/fonts/noto-sans-sc-116-400-normal.woff2 +0 -0
  67. package/src/web-public/fonts/noto-sans-sc-117-400-normal.woff2 +0 -0
  68. package/src/web-public/fonts/noto-sans-sc-118-400-normal.woff2 +0 -0
  69. package/src/web-public/fonts/noto-sans-sc-119-400-normal.woff2 +0 -0
  70. package/src/web-public/fonts/noto-sans-sc-21-400-normal.woff2 +0 -0
  71. package/src/web-public/fonts/noto-sans-sc-22-400-normal.woff2 +0 -0
  72. package/src/web-public/fonts/noto-sans-sc-23-400-normal.woff2 +0 -0
  73. package/src/web-public/fonts/noto-sans-sc-24-400-normal.woff2 +0 -0
  74. package/src/web-public/fonts/noto-sans-sc-25-400-normal.woff2 +0 -0
  75. package/src/web-public/fonts/noto-sans-sc-26-400-normal.woff2 +0 -0
  76. package/src/web-public/fonts/noto-sans-sc-27-400-normal.woff2 +0 -0
  77. package/src/web-public/fonts/noto-sans-sc-28-400-normal.woff2 +0 -0
  78. package/src/web-public/fonts/noto-sans-sc-29-400-normal.woff2 +0 -0
  79. package/src/web-public/fonts/noto-sans-sc-30-400-normal.woff2 +0 -0
  80. package/src/web-public/fonts/noto-sans-sc-31-400-normal.woff2 +0 -0
  81. package/src/web-public/fonts/noto-sans-sc-32-400-normal.woff2 +0 -0
  82. package/src/web-public/fonts/noto-sans-sc-33-400-normal.woff2 +0 -0
  83. package/src/web-public/fonts/noto-sans-sc-34-400-normal.woff2 +0 -0
  84. package/src/web-public/fonts/noto-sans-sc-35-400-normal.woff2 +0 -0
  85. package/src/web-public/fonts/noto-sans-sc-36-400-normal.woff2 +0 -0
  86. package/src/web-public/fonts/noto-sans-sc-37-400-normal.woff2 +0 -0
  87. package/src/web-public/fonts/noto-sans-sc-38-400-normal.woff2 +0 -0
  88. package/src/web-public/fonts/noto-sans-sc-39-400-normal.woff2 +0 -0
  89. package/src/web-public/fonts/noto-sans-sc-4-400-normal.woff2 +0 -0
  90. package/src/web-public/fonts/noto-sans-sc-40-400-normal.woff2 +0 -0
  91. package/src/web-public/fonts/noto-sans-sc-41-400-normal.woff2 +0 -0
  92. package/src/web-public/fonts/noto-sans-sc-42-400-normal.woff2 +0 -0
  93. package/src/web-public/fonts/noto-sans-sc-43-400-normal.woff2 +0 -0
  94. package/src/web-public/fonts/noto-sans-sc-44-400-normal.woff2 +0 -0
  95. package/src/web-public/fonts/noto-sans-sc-45-400-normal.woff2 +0 -0
  96. package/src/web-public/fonts/noto-sans-sc-46-400-normal.woff2 +0 -0
  97. package/src/web-public/fonts/noto-sans-sc-47-400-normal.woff2 +0 -0
  98. package/src/web-public/fonts/noto-sans-sc-48-400-normal.woff2 +0 -0
  99. package/src/web-public/fonts/noto-sans-sc-49-400-normal.woff2 +0 -0
  100. package/src/web-public/fonts/noto-sans-sc-5-400-normal.woff2 +0 -0
  101. package/src/web-public/fonts/noto-sans-sc-50-400-normal.woff2 +0 -0
  102. package/src/web-public/fonts/noto-sans-sc-51-400-normal.woff2 +0 -0
  103. package/src/web-public/fonts/noto-sans-sc-52-400-normal.woff2 +0 -0
  104. package/src/web-public/fonts/noto-sans-sc-53-400-normal.woff2 +0 -0
  105. package/src/web-public/fonts/noto-sans-sc-54-400-normal.woff2 +0 -0
  106. package/src/web-public/fonts/noto-sans-sc-55-400-normal.woff2 +0 -0
  107. package/src/web-public/fonts/noto-sans-sc-56-400-normal.woff2 +0 -0
  108. package/src/web-public/fonts/noto-sans-sc-57-400-normal.woff2 +0 -0
  109. package/src/web-public/fonts/noto-sans-sc-58-400-normal.woff2 +0 -0
  110. package/src/web-public/fonts/noto-sans-sc-59-400-normal.woff2 +0 -0
  111. package/src/web-public/fonts/noto-sans-sc-6-400-normal.woff2 +0 -0
  112. package/src/web-public/fonts/noto-sans-sc-60-400-normal.woff2 +0 -0
  113. package/src/web-public/fonts/noto-sans-sc-61-400-normal.woff2 +0 -0
  114. package/src/web-public/fonts/noto-sans-sc-62-400-normal.woff2 +0 -0
  115. package/src/web-public/fonts/noto-sans-sc-63-400-normal.woff2 +0 -0
  116. package/src/web-public/fonts/noto-sans-sc-64-400-normal.woff2 +0 -0
  117. package/src/web-public/fonts/noto-sans-sc-65-400-normal.woff2 +0 -0
  118. package/src/web-public/fonts/noto-sans-sc-66-400-normal.woff2 +0 -0
  119. package/src/web-public/fonts/noto-sans-sc-67-400-normal.woff2 +0 -0
  120. package/src/web-public/fonts/noto-sans-sc-68-400-normal.woff2 +0 -0
  121. package/src/web-public/fonts/noto-sans-sc-69-400-normal.woff2 +0 -0
  122. package/src/web-public/fonts/noto-sans-sc-70-400-normal.woff2 +0 -0
  123. package/src/web-public/fonts/noto-sans-sc-71-400-normal.woff2 +0 -0
  124. package/src/web-public/fonts/noto-sans-sc-72-400-normal.woff2 +0 -0
  125. package/src/web-public/fonts/noto-sans-sc-73-400-normal.woff2 +0 -0
  126. package/src/web-public/fonts/noto-sans-sc-74-400-normal.woff2 +0 -0
  127. package/src/web-public/fonts/noto-sans-sc-75-400-normal.woff2 +0 -0
  128. package/src/web-public/fonts/noto-sans-sc-76-400-normal.woff2 +0 -0
  129. package/src/web-public/fonts/noto-sans-sc-77-400-normal.woff2 +0 -0
  130. package/src/web-public/fonts/noto-sans-sc-78-400-normal.woff2 +0 -0
  131. package/src/web-public/fonts/noto-sans-sc-79-400-normal.woff2 +0 -0
  132. package/src/web-public/fonts/noto-sans-sc-80-400-normal.woff2 +0 -0
  133. package/src/web-public/fonts/noto-sans-sc-81-400-normal.woff2 +0 -0
  134. package/src/web-public/fonts/noto-sans-sc-82-400-normal.woff2 +0 -0
  135. package/src/web-public/fonts/noto-sans-sc-83-400-normal.woff2 +0 -0
  136. package/src/web-public/fonts/noto-sans-sc-84-400-normal.woff2 +0 -0
  137. package/src/web-public/fonts/noto-sans-sc-85-400-normal.woff2 +0 -0
  138. package/src/web-public/fonts/noto-sans-sc-86-400-normal.woff2 +0 -0
  139. package/src/web-public/fonts/noto-sans-sc-87-400-normal.woff2 +0 -0
  140. package/src/web-public/fonts/noto-sans-sc-88-400-normal.woff2 +0 -0
  141. package/src/web-public/fonts/noto-sans-sc-89-400-normal.woff2 +0 -0
  142. package/src/web-public/fonts/noto-sans-sc-90-400-normal.woff2 +0 -0
  143. package/src/web-public/fonts/noto-sans-sc-91-400-normal.woff2 +0 -0
  144. package/src/web-public/fonts/noto-sans-sc-97-400-normal.woff2 +0 -0
  145. package/src/web-public/fonts/noto-sans-sc-98-400-normal.woff2 +0 -0
  146. package/src/web-public/fonts/noto-sans-sc-99-400-normal.woff2 +0 -0
  147. package/src/web-public/fonts/noto-sans-sc.css +0 -775
  148. package/src/web-public/fonts.css +0 -31
  149. package/src/web-public/home.css +0 -222
  150. package/src/web-public/home.html +0 -33
  151. package/src/web-public/home.js +0 -160
  152. package/src/web-public/i18n.js +0 -983
  153. package/src/web-public/index.html +0 -196
  154. package/src/web-public/login.css +0 -123
  155. package/src/web-public/login.html +0 -25
  156. package/src/web-public/login.js +0 -46
  157. package/src/web-public/settings.js +0 -290
  158. package/src/web-ws.js +0 -226
  159. package/src/web.js +0 -430
package/src/ws.js ADDED
@@ -0,0 +1,248 @@
1
+ // A zero-dependency WebSocket server.
2
+ //
3
+ // WHY THIS EXISTS
4
+ // ---------------
5
+ // The project ships with NO runtime dependencies (see package.json), so a WebSocket upgrade
6
+ // cannot use the `ws` package. RFC 6455 is implemented here by hand: the SHA-1 handshake uses
7
+ // node:crypto, and the data framing (text frames, ~7-byte masks, 16/64-bit lengths) is a small
8
+ // loop.
9
+ //
10
+ // It works on Node 20 and later, which matters: `WebSocket` became a global only in Node 22, so
11
+ // the client half cannot be relied on either.
12
+ //
13
+ // This is deliberately the smallest subset a local front end needs: text messages in, text
14
+ // messages out, ping/pong, close. No fragmentation (the peer never fragments), no binary,
15
+ // no compression. Compression is NOT negotiated, so the peer sends plain frames.
16
+ //
17
+ // WHAT IS NOT HERE, AND WHY
18
+ // -------------------------
19
+ // * No permessage-deflate. It would save bandwidth on a loopback socket that sends a few
20
+ // kilobytes a second, and it would cost a negotiated extension on every connection.
21
+ // * No fragmentation support. A frame that arrives split is skipped rather than reassembled,
22
+ // and the peer is told nothing. A local peer does not fragment; a proxy that does is a case
23
+ // worth failing loudly about later, not worth half-supporting now.
24
+ //
25
+ // THE HANDSHAKE
26
+ // -------------
27
+ // The client's `Sec-WebSocket-Key` is answered with the GUID from RFC 6455 §4.2.2, hashed with
28
+ // SHA-1 and base64. That is the whole of the "verify the peer speaks WebSocket" step; there is
29
+ // no token here because the server only listens on the loopback interface.
30
+
31
+ import http from 'node:http';
32
+ import crypto from 'node:crypto';
33
+
34
+ const KEEPALIVE_MS = 25_000;
35
+
36
+ const WS_MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
37
+
38
+ // Hard cap on a single frame's payload. A peer can declare a 64-bit length; allocating that
39
+ // much would throw RangeError and crash the process, and buffering a slow trickle of an
40
+ // oversized frame would exhaust memory.
41
+ const MAX_FRAME = 64 * 1024 * 1024;
42
+
43
+ /** A single accepted, open WebSocket connection. */
44
+ export class WsConn {
45
+ constructor(socket, onClose) {
46
+ this.socket = socket;
47
+ this._done = onClose; // internal close sink, fired once when the socket dies
48
+ this.open = true;
49
+ // The caller registers a message handler.
50
+ this.onMessage = null;
51
+ socket.on('data', (d) => this._onData(d));
52
+ socket.on('close', () => this._close());
53
+ socket.on('error', () => this._close());
54
+ // Server->client keepalive: an idle proxy drops a silent WebSocket, the front end
55
+ // reconnects and re-reads the whole snapshot. A repeated ping keeps the path alive; the
56
+ // timer dies with the socket in _close().
57
+ this._ka = setInterval(() => { try { this.ping(); } catch { /* closing */ } }, KEEPALIVE_MS);
58
+ if (this._ka.unref) this._ka.unref();
59
+ }
60
+
61
+ _buffer = Buffer.alloc(0);
62
+
63
+ _onData(chunk) {
64
+ this._buffer = Buffer.concat([this._buffer, chunk]);
65
+ // Guard against a peer trickling an oversized frame: the incomplete-frame path would
66
+ // otherwise keep appending to the buffer without bound.
67
+ if (this._buffer.length > MAX_FRAME) { try { this._close(); } catch { /* gone */ } return; }
68
+ // Try to parse as many complete frames as the buffer holds.
69
+ for (;;) {
70
+ const parsed = this._parseFrame(this._buffer);
71
+ if (!parsed) return;
72
+ const { consumed, data } = parsed;
73
+ this._buffer = this._buffer.subarray(consumed);
74
+ if (parsed.fatal) { try { this._close(); } catch { /* gone */ } return; }
75
+ if (this.onMessage) {
76
+ try { this.onMessage(data); } catch { /* a handler must not kill the conn */ }
77
+ }
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Parse one WS frame from the front of `buf`.
83
+ *
84
+ * Returns null when the frame is incomplete; otherwise `{ consumed, data }` with `data` the
85
+ * UTF-8 string. `fatal` marks a frame that cannot be parsed at all — the connection is then
86
+ * dropped rather than the parser guessing a length, which could swallow the frames after it.
87
+ */
88
+ _parseFrame(buf) {
89
+ if (buf.length < 2) return null;
90
+ const fin = (buf[0] & 0x80) !== 0;
91
+ const opcode = buf[0] & 0x0f;
92
+ const masked = (buf[1] & 0x80) !== 0;
93
+ let len = buf[1] & 0x7f;
94
+ let off = 2;
95
+ if (len === 126) {
96
+ if (buf.length < off + 2) return null;
97
+ len = buf.readUInt16BE(off);
98
+ off += 2;
99
+ } else if (len === 127) {
100
+ if (buf.length < off + 8) return null;
101
+ const hi = buf.readUInt32BE(off);
102
+ const lo = buf.readUInt32BE(off + 4);
103
+ len = hi * 4294967296 + lo;
104
+ off += 8;
105
+ }
106
+ if (len > MAX_FRAME) return { consumed: 1, data: '', fatal: true };
107
+ // Only masked frames from a client; anything else means the stream is corrupt.
108
+ if (!masked) return { consumed: 1, data: '', fatal: true };
109
+ const maskLen = 4;
110
+ if (buf.length < off + maskLen) return null;
111
+ const mask = buf.subarray(off, off + maskLen);
112
+ off += maskLen;
113
+ if (buf.length < off + len) return null;
114
+ const payload = Buffer.alloc(len);
115
+ for (let i = 0; i < len; i++) payload[i] = buf[off + i] ^ mask[i & 3];
116
+ // opcode 0x9 = ping, 0xA = pong, 0x8 = close. We answer pings; skip the rest.
117
+ if (!fin) return { consumed: off + len, data: '' }; // no fragmentation support
118
+ let text = '';
119
+ if (opcode === 0x1) { // text frame
120
+ text = payload.toString('utf8');
121
+ } else if (opcode === 0x9) { // ping -> pong
122
+ this._writeFrame(0xA, payload);
123
+ } else if (opcode === 0x8) { // close -> close back
124
+ this._writeFrame(0x8, Buffer.alloc(0));
125
+ this._close();
126
+ }
127
+ return { consumed: off + len, data: text };
128
+ }
129
+
130
+ /**
131
+ * Send a text message as an unmasked frame, per RFC 6455 (server->client frames are never
132
+ * masked).
133
+ */
134
+ send(text) {
135
+ if (!this.open) return;
136
+ this._writeFrame(0x1, Buffer.from(String(text), 'utf8'));
137
+ }
138
+
139
+ /** Send a JSON value as a text message. */
140
+ sendJson(value) {
141
+ try { this.send(JSON.stringify(value)); } catch { /* a value that cannot be serialised */ }
142
+ }
143
+
144
+ /**
145
+ * Send a ping control frame (opcode 0x9). The peer answers with a pong, keeping the socket
146
+ * and any proxy between us and it alive.
147
+ */
148
+ ping() {
149
+ if (!this.open) return;
150
+ this._writeFrame(0x9, Buffer.alloc(0));
151
+ }
152
+
153
+ _writeFrame(opcode, payload) {
154
+ const len = payload.length;
155
+ let header;
156
+ if (len < 126) {
157
+ header = Buffer.alloc(2);
158
+ header[1] = len;
159
+ } else if (len < 65536) {
160
+ header = Buffer.alloc(4);
161
+ header[1] = 126;
162
+ header.writeUInt16BE(len, 2);
163
+ } else {
164
+ header = Buffer.alloc(10);
165
+ header[1] = 127;
166
+ header.writeUInt32BE(Math.floor(len / 4294967296), 2);
167
+ header.writeUInt32BE(len >>> 0, 6);
168
+ }
169
+ header[0] = 0x80 | opcode; // FIN + opcode
170
+ try { this.socket.write(Buffer.concat([header, payload])); } catch { /* the socket died */ }
171
+ }
172
+
173
+ close() {
174
+ if (!this.open) return;
175
+ try { this._writeFrame(0x8, Buffer.alloc(0)); } catch { /* already gone */ }
176
+ this._close();
177
+ }
178
+
179
+ _close() {
180
+ if (!this.open) return;
181
+ this.open = false;
182
+ if (this._ka) { clearInterval(this._ka); this._ka = null; }
183
+ try { this.socket.destroy(); } catch { /* already destroyed */ }
184
+ if (this._done) { const fn = this._done; this._done = null; try { fn(); } catch { /* gone */ } }
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Attach a WebSocket upgrade handler to an http.Server.
190
+ *
191
+ * opts.path the URL path to accept (default '/api/ws')
192
+ * opts.onConnection(conn) — called for each accepted connection
193
+ * opts.onUpgrade(req) — optional; return a string to reject (sent as the HTTP status
194
+ * for the upgrade), or null/undefined to accept.
195
+ */
196
+ export function attachWebSocket(server, opts = {}) {
197
+ const { path = '/api/ws', onConnection, onUpgrade } = opts;
198
+ server.on('upgrade', (req, socket, head) => {
199
+ if (!isUpgradeRequest(req) || !pathMatches(req, path)) {
200
+ try { socket.destroy(); } catch { /* already gone */ }
201
+ return;
202
+ }
203
+ if (onUpgrade) {
204
+ const reject = onUpgrade(req);
205
+ if (reject) {
206
+ try {
207
+ socket.write(`HTTP/1.1 ${reject} Forbidden\r\nConnection: close\r\n\r\n`);
208
+ socket.destroy();
209
+ } catch { /* already gone */ }
210
+ return;
211
+ }
212
+ }
213
+ const key = String(req.headers['sec-websocket-key'] || '');
214
+ const accept = crypto.createHash('sha1').update(key + WS_MAGIC).digest('base64');
215
+ try {
216
+ socket.write(
217
+ 'HTTP/1.1 101 Switching Protocols\r\n'
218
+ + 'Upgrade: websocket\r\n'
219
+ + 'Connection: Upgrade\r\n'
220
+ + `Sec-WebSocket-Accept: ${accept}\r\n`
221
+ + '\r\n',
222
+ );
223
+ } catch { try { socket.destroy(); } catch { /* gone */ } return; }
224
+ socket.setNoDelay(true);
225
+ const conn = new WsConn(socket, null);
226
+ if (head && head.length) conn._onData(head);
227
+ if (onConnection) {
228
+ try { onConnection(conn); } catch { /* the handler must not kill the server */ }
229
+ }
230
+ });
231
+ }
232
+
233
+ /**
234
+ * Is the incoming request a WebSocket upgrade for our path? Lets an http server's normal request
235
+ * handler skip it (the upgrade event is handled separately).
236
+ */
237
+ export function isUpgradeRequest(req) {
238
+ const up = String(req.headers.upgrade || '').toLowerCase();
239
+ return up === 'websocket' && String(req.headers.connection || '').toLowerCase().includes('upgrade');
240
+ }
241
+
242
+ /** Does the request's path match, ignoring the query string? */
243
+ function pathMatches(req, want) {
244
+ const raw = String(req.url || '/');
245
+ const at = raw.indexOf('?');
246
+ const p = at === -1 ? raw : raw.slice(0, at);
247
+ return p === want;
248
+ }