barejs 0.1.61 → 0.1.62
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 +38 -17
- package/dist/index.js +22 -22
- package/dist/node.js +22 -22
- package/llms.txt +3 -2
- package/package.json +7 -2
- package/src/runtime/node.ts +32 -12
- package/src/security/hash.ts +4 -0
package/llms.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# BareJS (`barejs`) — AI Quick Reference (`llms.txt`)
|
|
2
2
|
|
|
3
|
-
> BareJS is an ultra-fast, zero-allocation JIT-compiled web framework for **Bun** (`>= 1.1`), **Node.js** (`>= 20` via `barejs/node`), and **Deno** with built-in security (Argon2id/scrypt, HS256/RS256/ES256 JWT, HMAC-signed HttpOnly cookies, CSRF, Rate Limiting, Circuit Breaker, Idempotency-Key double-charge protection, Singleflight request coalescing, Mass-Assignment `stripUnknown`, Native Gzip/Brotli/Zstd `compress`, `app.cron`, Security Headers, DoS limits) and **
|
|
3
|
+
> BareJS is an ultra-fast, zero-allocation JIT-compiled web framework for **Bun** (`>= 1.1`), **Node.js** (`>= 20` via `barejs/node`), and **Deno** with built-in security (Argon2id/scrypt, HS256/RS256/ES256 JWT, HMAC-signed HttpOnly cookies, CSRF, Rate Limiting, Circuit Breaker, Idempotency-Key double-charge protection, Singleflight request coalescing, Mass-Assignment `stripUnknown`, Native Gzip/Brotli/Zstd `compress`, `app.cron`, Security Headers, DoS limits) and **Full Multi-Runtime Test Suite (425 tests / 6,255 assertions verified in CI across Bun, Node.js 20/22, and Deno 2)**.
|
|
4
4
|
|
|
5
5
|
## 1. Installation & Minimal App (Bun, Node.js, Deno)
|
|
6
6
|
```ts
|
|
@@ -121,7 +121,8 @@ app.get('/sso', bareAuth({ algorithms: ['RS256', 'ES256'], jwksUri: 'https://aut
|
|
|
121
121
|
```
|
|
122
122
|
|
|
123
123
|
## 6. Real-Time, RPC, OpenAPI & Client
|
|
124
|
-
- **WebSocket**: `app.ws('/ws/chat', { open(ws){}, message(ws, msg){}, close(ws){}, drain(ws){} })`
|
|
124
|
+
- **WebSocket**: `app.ws('/ws/chat', { open(ws){}, message(ws, msg){}, close(ws){}, drain(ws){} })` (Native on Bun, built-in RFC 6455 or optional `ws` on Node.js, `Deno.upgradeWebSocket` on Deno)
|
|
125
|
+
- **SSE**: `ctx.sse(async (send, close) => { send(data, event?, id?); })` or `ctx.sse(asyncIterable)` (works across Bun, Node.js, Deno)
|
|
125
126
|
- **JSON-RPC 2.0**: `app.rpc('/rpc', { add: ({ a, b }, ctx) => a + b })` (supports single, batch, notifications, and standard `-32700`/`-32600`/`-32601`/`-32603` error codes)
|
|
126
127
|
- **OpenAPI 3.1 + Scalar UI**: `app.docs('/docs', { title: 'My API', version: '1.0.0' })` (serves `/docs` & `/docs/json` with full `params`, `query`, `headers`, `body`, `response`, and `detail`)
|
|
127
128
|
- **Type-Safe Client (URL or Zero-Network In-Memory `app`)**:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barejs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.62",
|
|
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",
|
|
@@ -30,6 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"test": "bun test",
|
|
33
|
+
"test:node": "NODE_OPTIONS=--expose-gc vitest run && node tests/node-smoke.mjs",
|
|
34
|
+
"test:deno": "deno test --allow-all --unstable-sloppy-imports --no-check --v8-flags=--expose-gc --config deno.test.json --ignore=tests/playwright.test.ts,tests/production-db.test.ts tests/ && deno run --allow-net --allow-read --allow-write --allow-env tests/deno-smoke.ts",
|
|
33
35
|
"benchs": "bun run benchmarks/index.ts",
|
|
34
36
|
"bench": "bun run benchmarks/head-to-head.ts",
|
|
35
37
|
"dev": "bun --watch Example/index.ts",
|
|
@@ -67,7 +69,7 @@
|
|
|
67
69
|
"zod": "^3.0.0",
|
|
68
70
|
"ws": "^8.0.0",
|
|
69
71
|
"@node-rs/argon2": "^2.0.0",
|
|
70
|
-
"bcryptjs": "
|
|
72
|
+
"bcryptjs": ""
|
|
71
73
|
},
|
|
72
74
|
"peerDependenciesMeta": {
|
|
73
75
|
"@sinclair/typebox": {
|
|
@@ -89,11 +91,14 @@
|
|
|
89
91
|
"devDependencies": {
|
|
90
92
|
"@sinclair/typebox": "^0.34.47",
|
|
91
93
|
"@types/bun": "^1.3.5",
|
|
94
|
+
"bcryptjs": "^2.4.3",
|
|
92
95
|
"elysia": "latest",
|
|
93
96
|
"hono": "latest",
|
|
94
97
|
"mitata": "^1.0.34",
|
|
95
98
|
"playwright-core": "^1.65.1",
|
|
96
99
|
"typescript": "latest",
|
|
100
|
+
"vitest": "^3.2.4",
|
|
101
|
+
"ws": "^8.0.0",
|
|
97
102
|
"zod": "^3.25.76"
|
|
98
103
|
},
|
|
99
104
|
"files": [
|
package/src/runtime/node.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { createServer, type IncomingMessage, type ServerResponse } from 'node:ht
|
|
|
3
3
|
import { createReadStream } from 'node:fs';
|
|
4
4
|
import { extname } from 'node:path';
|
|
5
5
|
import { Readable } from 'node:stream';
|
|
6
|
-
import
|
|
6
|
+
import * as zlib from 'node:zlib';
|
|
7
7
|
import type { RuntimeDriver, RuntimeFileHandle, RuntimePasswordConfig, ServeOptions, ServerHandle } from './types';
|
|
8
8
|
import { matchWsRoute } from '../core/ws-match';
|
|
9
9
|
|
|
@@ -452,27 +452,47 @@ export const nodeDriver: RuntimeDriver = {
|
|
|
452
452
|
}
|
|
453
453
|
server.listen(listenOpts);
|
|
454
454
|
|
|
455
|
-
const
|
|
456
|
-
|
|
455
|
+
const ready = new Promise<void>((resolve) => {
|
|
456
|
+
if (server.listening) resolve();
|
|
457
|
+
else server.once('listening', () => resolve());
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
const stopFn = (closeActiveConnections = false) =>
|
|
461
|
+
new Promise<void>((resolve) => {
|
|
462
|
+
if (closeActiveConnections && typeof (server as any).closeAllConnections === 'function') {
|
|
463
|
+
(server as any).closeAllConnections();
|
|
464
|
+
}
|
|
465
|
+
server.close(() => resolve());
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
Object.defineProperty(server, 'port', {
|
|
469
|
+
get() {
|
|
470
|
+
const a = server.address();
|
|
471
|
+
return typeof a === 'object' && a ? a.port : opts.port;
|
|
472
|
+
},
|
|
473
|
+
configurable: true
|
|
474
|
+
});
|
|
475
|
+
(server as any).stop = stopFn;
|
|
476
|
+
(server as any).ready = ready;
|
|
457
477
|
|
|
458
478
|
return {
|
|
459
|
-
stop
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
}
|
|
464
|
-
server.close(() => resolve());
|
|
465
|
-
});
|
|
479
|
+
stop: stopFn,
|
|
480
|
+
get port() {
|
|
481
|
+
const a = server.address();
|
|
482
|
+
return typeof a === 'object' && a ? a.port : opts.port;
|
|
466
483
|
},
|
|
467
|
-
port: boundPort,
|
|
468
484
|
raw: server
|
|
469
485
|
};
|
|
470
486
|
},
|
|
471
487
|
|
|
472
488
|
gzip(data: Uint8Array, level: number): Uint8Array {
|
|
473
|
-
return new Uint8Array(gzipSync(data, { level }));
|
|
489
|
+
return new Uint8Array(zlib.gzipSync(data, { level }));
|
|
474
490
|
},
|
|
475
491
|
|
|
492
|
+
zstd: typeof (zlib as any).zstdCompressSync === 'function'
|
|
493
|
+
? (data: Uint8Array): Uint8Array => new Uint8Array((zlib as any).zstdCompressSync(data))
|
|
494
|
+
: undefined,
|
|
495
|
+
|
|
476
496
|
openFile(path: string): RuntimeFileHandle {
|
|
477
497
|
const ext = extname(path).toLowerCase();
|
|
478
498
|
const mime = MIME_MAP[ext] || 'application/octet-stream';
|
package/src/security/hash.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { runtime } from '../runtime';
|
|
|
4
4
|
|
|
5
5
|
export const Hash = {
|
|
6
6
|
make: (data: string): Promise<string> => {
|
|
7
|
+
if (!data) return Promise.reject(new Error('Password cannot be empty'));
|
|
7
8
|
return runtime.password.hash(data, BARE_CONFIG.hash);
|
|
8
9
|
},
|
|
9
10
|
|
|
@@ -25,6 +26,9 @@ export const Hash = {
|
|
|
25
26
|
const expectedCost = Math.max(4, Math.min(31, timeCost));
|
|
26
27
|
return !costMatch || Number(costMatch[1]) !== expectedCost;
|
|
27
28
|
}
|
|
29
|
+
if (hash.startsWith('$scrypt$')) {
|
|
30
|
+
return !hash.startsWith('$scrypt$N=16384,r=8,p=1$');
|
|
31
|
+
}
|
|
28
32
|
if (!hash.startsWith(`$${algorithm}$`)) return true;
|
|
29
33
|
const mMatch = hash.match(/(?:^|[,$])m=(\d+)(?:[,$]|$)/);
|
|
30
34
|
const tMatch = hash.match(/(?:^|[,$])t=(\d+)(?:[,$]|$)/);
|