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.
@@ -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
- const server = wsCfg
15
- ? Bun.serve<WebSocketRouteHandler>({
16
- port: opts.port,
17
- hostname: opts.hostname,
18
- reusePort: opts.reusePort,
19
- maxRequestBodySize: opts.maxRequestBodySize,
20
- ...(routes ? { routes } : {}),
21
- fetch(req: Request, srv: any) {
22
- if (req.headers.get('upgrade')?.toLowerCase() === 'websocket') {
23
- const matched = matchWsRoute(wsCfg.routes, req.url);
24
- if (
25
- matched &&
26
- srv.upgrade(req, {
27
- data: matched.params ? { ...matched.handler, params: matched.params } : matched.handler
28
- })
29
- ) {
30
- return undefined as any;
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
- return opts.fetch(req, srv?.requestIP?.(req)?.address);
34
- },
35
- websocket: wsCfg.adapter
36
- })
37
- : Bun.serve({
38
- port: opts.port,
39
- hostname: opts.hostname,
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) {
@@ -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) resolve();
457
- else server.once('listening', () => resolve());
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) =>
@@ -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: {