barejs 0.1.60 → 0.1.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/index.js +136 -129
- package/dist/node.js +136 -129
- package/package.json +1 -1
- package/src/runtime/node.ts +184 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barejs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.61",
|
|
4
4
|
"author": "xarhang",
|
|
5
5
|
"description": "Batteries-included web framework for Bun, Node.js, and Deno with O(1) JIT routing, TypeBox/Zod validation, OpenAPI 3.1, WebSockets, JSON-RPC 2.0, and built-in security.",
|
|
6
6
|
"main": "./dist/index.js",
|
package/src/runtime/node.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { randomBytes, scrypt, timingSafeEqual } from 'node:crypto';
|
|
1
|
+
import { createHash, randomBytes, scrypt, timingSafeEqual } from 'node:crypto';
|
|
2
2
|
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
|
|
3
3
|
import { createReadStream } from 'node:fs';
|
|
4
4
|
import { extname } from 'node:path';
|
|
@@ -228,16 +228,189 @@ export const nodeDriver: RuntimeDriver = {
|
|
|
228
228
|
const wsPkg = 'ws';
|
|
229
229
|
wssPromise = import(wsPkg).then(
|
|
230
230
|
(m: any) => new m.WebSocketServer({ noServer: true }),
|
|
231
|
-
() =>
|
|
232
|
-
throw new Error(
|
|
233
|
-
'[BareJS] WebSocket on Node.js requires the optional peer dependency "ws". Install it with: npm install ws'
|
|
234
|
-
);
|
|
235
|
-
}
|
|
231
|
+
() => null
|
|
236
232
|
);
|
|
237
233
|
}
|
|
238
234
|
return wssPromise;
|
|
239
235
|
};
|
|
240
236
|
|
|
237
|
+
const WS_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
238
|
+
const MAX_WS_FRAME = 16 * 1024 * 1024;
|
|
239
|
+
|
|
240
|
+
const buildWsFrame = (opcode: number, payload: Buffer): Buffer => {
|
|
241
|
+
const len = payload.length;
|
|
242
|
+
let header: Buffer;
|
|
243
|
+
if (len < 126) {
|
|
244
|
+
header = Buffer.allocUnsafe(2);
|
|
245
|
+
header[0] = 0x80 | opcode;
|
|
246
|
+
header[1] = len;
|
|
247
|
+
} else if (len < 65536) {
|
|
248
|
+
header = Buffer.allocUnsafe(4);
|
|
249
|
+
header[0] = 0x80 | opcode;
|
|
250
|
+
header[1] = 126;
|
|
251
|
+
header.writeUInt16BE(len, 2);
|
|
252
|
+
} else {
|
|
253
|
+
header = Buffer.allocUnsafe(10);
|
|
254
|
+
header[0] = 0x80 | opcode;
|
|
255
|
+
header[1] = 127;
|
|
256
|
+
header.writeBigUInt64BE(BigInt(len), 2);
|
|
257
|
+
}
|
|
258
|
+
return Buffer.concat([header, payload]);
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const handleNativeWsUpgrade = (req: IncomingMessage, socket: any, head: Buffer, data: any) => {
|
|
262
|
+
const key = req.headers['sec-websocket-key'];
|
|
263
|
+
if (!key || Array.isArray(key)) {
|
|
264
|
+
socket.write('HTTP/1.1 400 Bad Request\r\n\r\n');
|
|
265
|
+
socket.destroy();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const accept = createHash('sha1').update(key.trim() + WS_GUID).digest('base64');
|
|
269
|
+
socket.write(
|
|
270
|
+
'HTTP/1.1 101 Switching Protocols\r\n' +
|
|
271
|
+
'Upgrade: websocket\r\n' +
|
|
272
|
+
'Connection: Upgrade\r\n' +
|
|
273
|
+
`Sec-WebSocket-Accept: ${accept}\r\n\r\n`
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
let closed = false;
|
|
277
|
+
const wsWrapper: any = {
|
|
278
|
+
data,
|
|
279
|
+
send(msg: string | Uint8Array | Buffer) {
|
|
280
|
+
if (closed || socket.destroyed) return;
|
|
281
|
+
if (typeof msg === 'string') {
|
|
282
|
+
socket.write(buildWsFrame(0x1, Buffer.from(msg, 'utf8')));
|
|
283
|
+
} else {
|
|
284
|
+
const buf = Buffer.isBuffer(msg) ? msg : Buffer.from(msg.buffer, msg.byteOffset, msg.byteLength);
|
|
285
|
+
socket.write(buildWsFrame(0x2, buf));
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
close(code = 1000, reason = '') {
|
|
289
|
+
if (closed || socket.destroyed) return;
|
|
290
|
+
closed = true;
|
|
291
|
+
const reasonBuf = Buffer.from(reason, 'utf8');
|
|
292
|
+
const payload = Buffer.allocUnsafe(2 + reasonBuf.length);
|
|
293
|
+
payload.writeUInt16BE(code, 0);
|
|
294
|
+
reasonBuf.copy(payload, 2);
|
|
295
|
+
socket.end(buildWsFrame(0x8, payload));
|
|
296
|
+
wsCfg.adapter.close(wsWrapper, code, reason);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
wsCfg.adapter.open(wsWrapper);
|
|
301
|
+
|
|
302
|
+
let buf: Buffer = head && head.length > 0 ? Buffer.from(head) : Buffer.alloc(0);
|
|
303
|
+
let fragOpcode = 0;
|
|
304
|
+
let fragChunks: Buffer[] = [];
|
|
305
|
+
let fragTotal = 0;
|
|
306
|
+
|
|
307
|
+
const processFrames = () => {
|
|
308
|
+
while (buf.length >= 2) {
|
|
309
|
+
const b0 = buf[0]!;
|
|
310
|
+
const b1 = buf[1]!;
|
|
311
|
+
const fin = (b0 & 0x80) !== 0;
|
|
312
|
+
const opcode = b0 & 0x0f;
|
|
313
|
+
const masked = (b1 & 0x80) !== 0;
|
|
314
|
+
let payloadLen = b1 & 0x7f;
|
|
315
|
+
let offset = 2;
|
|
316
|
+
|
|
317
|
+
if (payloadLen === 126) {
|
|
318
|
+
if (buf.length < 4) return;
|
|
319
|
+
payloadLen = buf.readUInt16BE(2);
|
|
320
|
+
offset = 4;
|
|
321
|
+
} else if (payloadLen === 127) {
|
|
322
|
+
if (buf.length < 10) return;
|
|
323
|
+
const bigLen = buf.readBigUInt64BE(2);
|
|
324
|
+
if (bigLen > BigInt(MAX_WS_FRAME)) {
|
|
325
|
+
wsWrapper.close(1009, 'Message too big');
|
|
326
|
+
socket.destroy();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
payloadLen = Number(bigLen);
|
|
330
|
+
offset = 10;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (payloadLen > MAX_WS_FRAME) {
|
|
334
|
+
wsWrapper.close(1009, 'Message too big');
|
|
335
|
+
socket.destroy();
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const maskLen = masked ? 4 : 0;
|
|
340
|
+
if (buf.length < offset + maskLen + payloadLen) return;
|
|
341
|
+
|
|
342
|
+
let payload = buf.subarray(offset + maskLen, offset + maskLen + payloadLen);
|
|
343
|
+
if (masked) {
|
|
344
|
+
const mask = buf.subarray(offset, offset + 4);
|
|
345
|
+
const unmasked = Buffer.allocUnsafe(payloadLen);
|
|
346
|
+
for (let i = 0; i < payloadLen; i++) {
|
|
347
|
+
unmasked[i] = payload[i]! ^ mask[i & 3]!;
|
|
348
|
+
}
|
|
349
|
+
payload = unmasked;
|
|
350
|
+
} else {
|
|
351
|
+
payload = Buffer.from(payload);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
buf = buf.subarray(offset + maskLen + payloadLen);
|
|
355
|
+
|
|
356
|
+
if (opcode === 0x8) {
|
|
357
|
+
const code = payload.length >= 2 ? payload.readUInt16BE(0) : 1000;
|
|
358
|
+
const reason = payload.length > 2 ? payload.subarray(2).toString('utf8') : '';
|
|
359
|
+
wsWrapper.close(code, reason);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (opcode === 0x9) {
|
|
363
|
+
if (!closed && !socket.destroyed) socket.write(buildWsFrame(0xA, payload));
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (opcode === 0xA) continue;
|
|
367
|
+
|
|
368
|
+
if (opcode !== 0x0) {
|
|
369
|
+
if (fin) {
|
|
370
|
+
wsCfg.adapter.message(wsWrapper, opcode === 0x1 ? payload.toString('utf8') : payload);
|
|
371
|
+
} else {
|
|
372
|
+
fragOpcode = opcode;
|
|
373
|
+
fragChunks = [payload];
|
|
374
|
+
fragTotal = payload.length;
|
|
375
|
+
}
|
|
376
|
+
} else {
|
|
377
|
+
fragTotal += payload.length;
|
|
378
|
+
if (fragTotal > MAX_WS_FRAME) {
|
|
379
|
+
wsWrapper.close(1009, 'Message too big');
|
|
380
|
+
socket.destroy();
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
fragChunks.push(payload);
|
|
384
|
+
if (fin) {
|
|
385
|
+
const full = Buffer.concat(fragChunks);
|
|
386
|
+
fragChunks = [];
|
|
387
|
+
fragTotal = 0;
|
|
388
|
+
wsCfg.adapter.message(wsWrapper, fragOpcode === 0x1 ? full.toString('utf8') : full);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
if (buf.length > 0) processFrames();
|
|
395
|
+
|
|
396
|
+
socket.on('data', (chunk: Buffer) => {
|
|
397
|
+
buf = buf.length === 0 ? chunk : Buffer.concat([buf, chunk]);
|
|
398
|
+
processFrames();
|
|
399
|
+
});
|
|
400
|
+
socket.on('close', () => {
|
|
401
|
+
if (!closed) {
|
|
402
|
+
closed = true;
|
|
403
|
+
wsCfg.adapter.close(wsWrapper, 1006, '');
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
socket.on('error', () => {
|
|
407
|
+
if (!closed) {
|
|
408
|
+
closed = true;
|
|
409
|
+
wsCfg.adapter.close(wsWrapper, 1006, '');
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
|
|
241
414
|
server.on('upgrade', async (req, socket, head) => {
|
|
242
415
|
const matched = matchWsRoute(wsCfg.routes, req.url || '/');
|
|
243
416
|
if (!matched) {
|
|
@@ -246,8 +419,12 @@ export const nodeDriver: RuntimeDriver = {
|
|
|
246
419
|
}
|
|
247
420
|
try {
|
|
248
421
|
const wss = await getWss();
|
|
422
|
+
const data = matched.params ? { ...matched.handler, params: matched.params } : matched.handler;
|
|
423
|
+
if (!wss) {
|
|
424
|
+
handleNativeWsUpgrade(req, socket, head, data);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
249
427
|
wss.handleUpgrade(req, socket, head, (rawWs: any) => {
|
|
250
|
-
const data = matched.params ? { ...matched.handler, params: matched.params } : matched.handler;
|
|
251
428
|
rawWs.data = data;
|
|
252
429
|
wsCfg.adapter.open(rawWs);
|
|
253
430
|
rawWs.on('message', (msg: any, isBinary: boolean) => {
|