barejs 0.1.63 → 0.2.3
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 +8 -8
- package/dist/index.js +145 -143
- package/dist/node.js +145 -143
- package/dist/types/core/bare.d.ts +34 -4
- package/dist/types/core/context.d.ts +3 -2
- package/dist/types/runtime/types.d.ts +2 -0
- package/dist/types/runtime/uws.d.ts +13 -0
- package/dist/types/utils/openapi.d.ts +3 -1
- package/dist/types/utils/static.d.ts +4 -0
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/src/core/bare.ts +232 -13
- package/src/core/context.ts +36 -3
- package/src/runtime/bun.ts +39 -31
- package/src/runtime/node.ts +27 -3
- package/src/runtime/types.ts +2 -0
- package/src/runtime/uws.ts +655 -0
- package/src/security/auth.ts +11 -6
- package/src/utils/openapi.ts +19 -2
- package/src/utils/static.ts +37 -3
package/src/runtime/bun.ts
CHANGED
|
@@ -11,39 +11,47 @@ export const bunDriver: RuntimeDriver = {
|
|
|
11
11
|
serve(opts: ServeOptions): ServerHandle {
|
|
12
12
|
const wsCfg = opts.ws;
|
|
13
13
|
const routes = opts.routes;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
matched
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
14
|
+
let server: any;
|
|
15
|
+
try {
|
|
16
|
+
server = wsCfg
|
|
17
|
+
? Bun.serve<WebSocketRouteHandler>({
|
|
18
|
+
port: opts.port,
|
|
19
|
+
hostname: opts.hostname,
|
|
20
|
+
reusePort: opts.reusePort,
|
|
21
|
+
maxRequestBodySize: opts.maxRequestBodySize,
|
|
22
|
+
...(routes ? { routes } : {}),
|
|
23
|
+
fetch(req: Request, srv: any) {
|
|
24
|
+
if (req.headers.get('upgrade')?.toLowerCase() === 'websocket') {
|
|
25
|
+
const matched = matchWsRoute(wsCfg.routes, req.url);
|
|
26
|
+
if (
|
|
27
|
+
matched &&
|
|
28
|
+
srv.upgrade(req, {
|
|
29
|
+
data: matched.params ? { ...matched.handler, params: matched.params } : matched.handler
|
|
30
|
+
})
|
|
31
|
+
) {
|
|
32
|
+
return undefined as any;
|
|
33
|
+
}
|
|
31
34
|
}
|
|
35
|
+
return opts.fetch(req, srv?.requestIP?.(req)?.address);
|
|
36
|
+
},
|
|
37
|
+
websocket: wsCfg.adapter
|
|
38
|
+
})
|
|
39
|
+
: Bun.serve({
|
|
40
|
+
port: opts.port,
|
|
41
|
+
hostname: opts.hostname,
|
|
42
|
+
reusePort: opts.reusePort,
|
|
43
|
+
maxRequestBodySize: opts.maxRequestBodySize,
|
|
44
|
+
...(routes ? { routes } : {}),
|
|
45
|
+
fetch(req: Request, srv: any) {
|
|
46
|
+
return opts.fetch(req, srv?.requestIP?.(req)?.address);
|
|
32
47
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
reusePort: opts.reusePort,
|
|
41
|
-
maxRequestBodySize: opts.maxRequestBodySize,
|
|
42
|
-
...(routes ? { routes } : {}),
|
|
43
|
-
fetch(req: Request, srv: any) {
|
|
44
|
-
return opts.fetch(req, srv?.requestIP?.(req)?.address);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
48
|
+
});
|
|
49
|
+
} catch (err: any) {
|
|
50
|
+
if (err?.code === 'EADDRINUSE' || /in use/i.test(err?.message || '')) {
|
|
51
|
+
throw new Error(`[BareJS] Port ${opts.port} is already in use on ${opts.hostname}. Choose a different port or stop the existing process.`);
|
|
52
|
+
}
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
47
55
|
|
|
48
56
|
return {
|
|
49
57
|
stop(closeActiveConnections = false) {
|
package/src/runtime/node.ts
CHANGED
|
@@ -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') {
|
|
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) {
|
|
@@ -452,9 +462,23 @@ export const nodeDriver: RuntimeDriver = {
|
|
|
452
462
|
}
|
|
453
463
|
server.listen(listenOpts);
|
|
454
464
|
|
|
455
|
-
const ready = new Promise<void>((resolve) => {
|
|
456
|
-
if (server.listening)
|
|
457
|
-
|
|
465
|
+
const ready = new Promise<void>((resolve, reject) => {
|
|
466
|
+
if (server.listening) {
|
|
467
|
+
resolve();
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const onErr = (err: any) => {
|
|
471
|
+
if (err?.code === 'EADDRINUSE') {
|
|
472
|
+
reject(new Error(`[BareJS] Port ${opts.port} is already in use on ${opts.hostname}. Choose a different port or stop the existing process.`));
|
|
473
|
+
} else {
|
|
474
|
+
reject(err);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
server.once('error', onErr);
|
|
478
|
+
server.once('listening', () => {
|
|
479
|
+
server.removeListener('error', onErr);
|
|
480
|
+
resolve();
|
|
481
|
+
});
|
|
458
482
|
});
|
|
459
483
|
|
|
460
484
|
const stopFn = (closeActiveConnections = false) =>
|
package/src/runtime/types.ts
CHANGED
|
@@ -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: {
|