barejs 0.1.63 → 0.2.1

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.
@@ -134,9 +134,11 @@ export declare class BareJS<Derived extends Record<string, any> = {}, Routes ext
134
134
  name: string;
135
135
  trustProxy(enabled?: boolean): this;
136
136
  private buildBunRoutes;
137
+ private buildUwsRoutes;
137
138
  listen(port?: number, hostname?: string, reusePort?: boolean, options?: {
138
139
  maxRequestBodySize?: number;
139
140
  trustProxy?: boolean;
141
+ engine?: 'auto' | 'http' | 'uws';
140
142
  }): any;
141
143
  }
142
144
  export { Context, typebox, t };
@@ -6,6 +6,8 @@ export interface ServeOptions {
6
6
  maxRequestBodySize: number;
7
7
  fetch: (req: Request, peerIp?: string) => Response | Promise<Response>;
8
8
  routes?: Record<string, any>;
9
+ engine?: 'auto' | 'http' | 'uws';
10
+ uwsRoutes?: any[];
9
11
  ws?: {
10
12
  routes: Map<string, WebSocketRouteHandler>;
11
13
  adapter: {
@@ -0,0 +1,13 @@
1
+ import type { ServeOptions, ServerHandle } from './types';
2
+ export interface UwsRouteEntry {
3
+ method: string;
4
+ path: string;
5
+ paramNames: string[];
6
+ rawHandler: Function;
7
+ isPureStaticString: boolean;
8
+ isTrulyZeroArg: boolean;
9
+ }
10
+ export declare function tryLoadUws(): any | null;
11
+ export declare function serveUws(opts: ServeOptions & {
12
+ uwsRoutes?: UwsRouteEntry[];
13
+ }, uwsMod: any): ServerHandle;
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 **Full Multi-Runtime Test Suite (427 tests / 6,279 assertions verified in CI across Bun, Node.js 20/22, and Deno 2)**.
3
+ > BareJS is an ultra-fast, zero-allocation JIT-compiled web framework for **Bun** (`>= 1.1` with `Bun.serve({ routes })`), **Node.js** (`>= 20` via `barejs/node` with optional `uWebSockets.js` auto-detection), 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 (427 tests / 6,279 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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barejs",
3
- "version": "0.1.63",
3
+ "version": "0.2.1",
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/core/bare.ts CHANGED
@@ -1342,11 +1342,75 @@ export class BareJS<
1342
1342
  return count > 0 ? routes : undefined;
1343
1343
  }
1344
1344
 
1345
+ private buildUwsRoutes(): Array<{
1346
+ method: string;
1347
+ path: string;
1348
+ paramNames: string[];
1349
+ rawHandler: Function;
1350
+ isPureStaticString: boolean;
1351
+ isTrulyZeroArg: boolean;
1352
+ }> | undefined {
1353
+ if (
1354
+ this._useLog ||
1355
+ this.groupMiddleware.length > 0 ||
1356
+ this.errorHandler !== undefined ||
1357
+ this._trustProxyBox.val ||
1358
+ this._hasDerive ||
1359
+ this.wsRoutes.size > 0 ||
1360
+ this.routes.length === 0
1361
+ ) {
1362
+ return undefined;
1363
+ }
1364
+
1365
+ const uwsRoutes: Array<{
1366
+ method: string;
1367
+ path: string;
1368
+ paramNames: string[];
1369
+ rawHandler: Function;
1370
+ isPureStaticString: boolean;
1371
+ isTrulyZeroArg: boolean;
1372
+ }> = [];
1373
+ const normStatic = (p: string) => {
1374
+ const c = p.replace(/\/+/g, '/');
1375
+ return c.length > 1 && c.endsWith('/') ? c.slice(0, -1) : (c || '/');
1376
+ };
1377
+
1378
+ for (const route of this.routes) {
1379
+ if (route.method !== 'GET' && route.method !== 'HEAD' && route.method !== 'DELETE' && route.method !== 'OPTIONS') continue;
1380
+ if (route.path.includes('*') || route.path.includes('?') || route.path.includes('(')) continue;
1381
+ if (route.handlers.length !== 1) continue;
1382
+
1383
+ const rawHandler = route.handlers[0] as any;
1384
+ if (rawHandler?.__idempotencyCommit || rawHandler?.__isDerive || rawHandler?.__isCors) continue;
1385
+
1386
+ const normPath = normStatic(route.path);
1387
+ const paramNames: string[] = [];
1388
+ if (normPath.includes(':')) {
1389
+ const segs = normPath.split('/');
1390
+ for (let i = 0; i < segs.length; i++) {
1391
+ const seg = segs[i]!;
1392
+ if (seg.startsWith(':')) paramNames.push(seg.slice(1));
1393
+ }
1394
+ }
1395
+
1396
+ uwsRoutes.push({
1397
+ method: route.method,
1398
+ path: normPath,
1399
+ paramNames,
1400
+ rawHandler,
1401
+ isPureStaticString: isPureStaticStringFn(rawHandler),
1402
+ isTrulyZeroArg: isTrulyZeroArg(rawHandler)
1403
+ });
1404
+ }
1405
+
1406
+ return uwsRoutes.length > 0 ? uwsRoutes : undefined;
1407
+ }
1408
+
1345
1409
  public listen(
1346
1410
  port?: number,
1347
1411
  hostname: string = '0.0.0.0',
1348
1412
  reusePort: boolean = true,
1349
- options: { maxRequestBodySize?: number; trustProxy?: boolean } = {}
1413
+ options: { maxRequestBodySize?: number; trustProxy?: boolean; engine?: 'auto' | 'http' | 'uws' } = {}
1350
1414
  ) {
1351
1415
  if (options.trustProxy !== undefined) {
1352
1416
  this.trustProxy(options.trustProxy);
@@ -1375,6 +1439,7 @@ export class BareJS<
1375
1439
  const self = this;
1376
1440
  const hasWs = wsRoutes.size > 0;
1377
1441
  const bunRoutes = runtime.name === 'bun' ? this.buildBunRoutes() : undefined;
1442
+ const uwsRoutes = runtime.name === 'node' && options.engine !== 'http' ? this.buildUwsRoutes() : undefined;
1378
1443
 
1379
1444
  const server = runtime.serve({
1380
1445
  port: finalPort,
@@ -1382,6 +1447,8 @@ export class BareJS<
1382
1447
  reusePort,
1383
1448
  maxRequestBodySize,
1384
1449
  routes: bunRoutes,
1450
+ engine: options.engine,
1451
+ uwsRoutes,
1385
1452
  fetch(req: Request, peerIp?: string) {
1386
1453
  return self.fetch(req, peerIp);
1387
1454
  },
@@ -6,6 +6,7 @@ import { Readable } from 'node:stream';
6
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
+ import { serveUws, tryLoadUws } from './uws';
9
10
 
10
11
  const SCRYPT_N = 16384;
11
12
  const SCRYPT_R = 8;
@@ -196,6 +197,15 @@ export const nodeDriver: RuntimeDriver = {
196
197
  name: 'node',
197
198
 
198
199
  serve(opts: ServeOptions): ServerHandle {
200
+ if (opts.engine !== 'http' && !opts.ws) {
201
+ const uwsMod = tryLoadUws();
202
+ if (uwsMod) {
203
+ return serveUws(opts, uwsMod);
204
+ }
205
+ if (opts.engine === 'uws') {
206
+ throw new Error("[BareJS] engine: 'uws' was requested, but 'uWebSockets.js' is not installed.");
207
+ }
208
+ }
199
209
  const server = createServer(async (req, res) => {
200
210
  const parsed = toWebRequest(req, opts.maxRequestBodySize);
201
211
  if (parsed.errorStatus === 413 || !parsed.request) {
@@ -7,6 +7,8 @@ export interface ServeOptions {
7
7
  maxRequestBodySize: number;
8
8
  fetch: (req: Request, peerIp?: string) => Response | Promise<Response>;
9
9
  routes?: Record<string, any>;
10
+ engine?: 'auto' | 'http' | 'uws';
11
+ uwsRoutes?: any[];
10
12
  ws?: {
11
13
  routes: Map<string, WebSocketRouteHandler>;
12
14
  adapter: {