@krisanalfa/bunest-adapter 0.3.0 → 0.4.0
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 +80 -1
- package/dist/bun.adapter.d.ts +7 -66
- package/dist/bun.internal.types.d.ts +22 -0
- package/dist/bun.preflight-http-server.d.ts +8 -26
- package/dist/bun.request.d.ts +41 -1
- package/dist/bun.response.d.ts +28 -0
- package/dist/bun.server-instance.d.ts +131 -0
- package/dist/bun.ws-adapter.d.ts +1 -11
- package/dist/index.d.ts +2 -1
- package/dist/index.js +422 -316
- package/dist/index.js.map +11 -10
- package/package.json +15 -7
- package/dist/internal.types.d.ts +0 -8
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ This project provides a native Bun adapter for NestJS, allowing developers to le
|
|
|
15
15
|
- [Validation](#validation)
|
|
16
16
|
- [File Uploads](#file-uploads)
|
|
17
17
|
- [Streaming Responses](#streaming-responses)
|
|
18
|
+
- [Server-Sent Events (SSE)](#server-sent-events-sse)
|
|
18
19
|
- [Versioning](#versioning)
|
|
19
20
|
- [CORS](#cors)
|
|
20
21
|
- [Cookies](#cookies)
|
|
@@ -375,6 +376,67 @@ class FilesController {
|
|
|
375
376
|
}
|
|
376
377
|
```
|
|
377
378
|
|
|
379
|
+
#### Server-Sent Events (SSE)
|
|
380
|
+
|
|
381
|
+
Full support for [Server-Sent Events](https://docs.nestjs.com/techniques/server-sent-events) using the `@Sse()` decorator. SSE allows servers to push real-time updates to clients over HTTP:
|
|
382
|
+
|
|
383
|
+
```ts
|
|
384
|
+
import { Controller, Sse, MessageEvent } from '@nestjs/common';
|
|
385
|
+
import { Observable, interval, map } from 'rxjs';
|
|
386
|
+
|
|
387
|
+
@Controller()
|
|
388
|
+
class EventsController {
|
|
389
|
+
@Sse('/sse')
|
|
390
|
+
sendEvents(): Observable<MessageEvent> {
|
|
391
|
+
return interval(1000).pipe(
|
|
392
|
+
map(num => ({
|
|
393
|
+
data: `SSE message ${num.toString()}`,
|
|
394
|
+
})),
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
**Client Connection Example:**
|
|
401
|
+
|
|
402
|
+
```ts
|
|
403
|
+
const eventSource = new EventSource('http://localhost:3000/sse');
|
|
404
|
+
|
|
405
|
+
eventSource.onopen = () => {
|
|
406
|
+
console.log('SSE connection opened');
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
eventSource.onmessage = (event) => {
|
|
410
|
+
console.log('Received:', event.data); // "SSE message 0", "SSE message 1", etc.
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
eventSource.onerror = (error) => {
|
|
414
|
+
console.error('SSE error:', error);
|
|
415
|
+
eventSource.close();
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
// Close the connection when done
|
|
419
|
+
// eventSource.close();
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
**For HTTPS/Secure Connections:**
|
|
423
|
+
|
|
424
|
+
```ts
|
|
425
|
+
import { EventSource } from 'eventsource'; // npm package for Node.js
|
|
426
|
+
|
|
427
|
+
const eventSource = new EventSource('https://localhost:3000/sse', {
|
|
428
|
+
fetch: (url, init) => fetch(url, {
|
|
429
|
+
...init,
|
|
430
|
+
tls: { rejectUnauthorized: false }, // For self-signed certificates
|
|
431
|
+
}),
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
eventSource.onmessage = (event) => {
|
|
435
|
+
console.log('Received:', event.data);
|
|
436
|
+
};
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
|
|
378
440
|
#### Versioning
|
|
379
441
|
|
|
380
442
|
Full API [versioning](https://docs.nestjs.com/techniques/versioning) support (URI, Header, Media Type, Custom):
|
|
@@ -1251,6 +1313,23 @@ oha -c 125 -n 1000000 --no-tui "http://127.0.0.1:3000/"
|
|
|
1251
1313
|
|
|
1252
1314
|
> **Pure Bun** is the fastest at **80,743 req/s**. **Nest + Bun + Native Bun Adapter** achieves **~86%** of Pure Bun's performance while providing full NestJS features, and is **~5x faster** than Nest + Node + Express. Compared to Bun with Express adapter, the native Bun adapter is **~1.6x faster**.
|
|
1253
1315
|
|
|
1316
|
+
Bonus if you use Unix sockets:
|
|
1317
|
+
```
|
|
1318
|
+
Summary:
|
|
1319
|
+
Success rate: 100.00%
|
|
1320
|
+
Total: 8298.2243 ms
|
|
1321
|
+
Slowest: 5.2326 ms
|
|
1322
|
+
Fastest: 0.2857 ms
|
|
1323
|
+
Average: 1.0361 ms
|
|
1324
|
+
Requests/sec: 120507.7092
|
|
1325
|
+
|
|
1326
|
+
Total data: 21.93 MiB
|
|
1327
|
+
Size/request: 23 B
|
|
1328
|
+
Size/sec: 2.64 MiB
|
|
1329
|
+
```
|
|
1330
|
+
|
|
1331
|
+
As you can see, using Unix sockets boosts the performance further to **120,508 req/s**, which is **~1.5x faster** than TCP. Since Bun `fetch` supports Unix sockets, you can leverage this for inter-process communication on the same machine.
|
|
1332
|
+
|
|
1254
1333
|
### WebSocket Benchmark
|
|
1255
1334
|
|
|
1256
1335
|
WebSocket benchmarks run using the custom benchmark script in `benchmarks/ws.benchmark.ts`.
|
|
@@ -1321,7 +1400,7 @@ Contributions are welcome! Please open issues or submit pull requests for bug fi
|
|
|
1321
1400
|
## Future Plans
|
|
1322
1401
|
|
|
1323
1402
|
- Enhanced trusted proxy configuration for host header handling
|
|
1324
|
-
-
|
|
1403
|
+
- Improved documentation and examples
|
|
1325
1404
|
- Release automation via CI/CD pipelines
|
|
1326
1405
|
|
|
1327
1406
|
## License
|
package/dist/bun.adapter.d.ts
CHANGED
|
@@ -1,63 +1,19 @@
|
|
|
1
1
|
import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/external/cors-options.interface.js';
|
|
2
2
|
import { ErrorHandler, RequestHandler } from '@nestjs/common/interfaces/index.js';
|
|
3
|
-
import { Server } from 'bun';
|
|
4
3
|
import { NestApplicationOptions, RequestMethod, VersioningOptions } from '@nestjs/common';
|
|
4
|
+
import { Server } from 'bun';
|
|
5
5
|
import { AbstractHttpAdapter } from '@nestjs/core';
|
|
6
6
|
import { VersionValue } from '@nestjs/common/interfaces/version-options.interface.js';
|
|
7
|
-
import { ServerOptions } from './internal.types.js';
|
|
7
|
+
import { BunWsClientData, ServerOptions } from './bun.internal.types.js';
|
|
8
8
|
import { BunPreflightHttpServer } from './bun.preflight-http-server.js';
|
|
9
9
|
import { BunRequest } from './bun.request.js';
|
|
10
10
|
import { BunResponse } from './bun.response.js';
|
|
11
|
+
import { BunServerInstance } from './bun.server-instance.js';
|
|
11
12
|
export declare class BunAdapter extends AbstractHttpAdapter<Server<unknown>, BunRequest, BunResponse> {
|
|
12
|
-
protected bunServeOptions: ServerOptions
|
|
13
|
+
protected bunServeOptions: ServerOptions<BunWsClientData>;
|
|
13
14
|
private readonly logger;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
private readonly routes;
|
|
17
|
-
private readonly routeHandlers;
|
|
18
|
-
private notFoundHandler;
|
|
19
|
-
private readonly wsHandlers;
|
|
20
|
-
private readonly wsMiddlewareEngine;
|
|
21
|
-
private wsOptions;
|
|
22
|
-
private useWs;
|
|
23
|
-
private useWsCors;
|
|
24
|
-
private wsCorsHeaders?;
|
|
25
|
-
constructor(bunServeOptions?: ServerOptions);
|
|
26
|
-
use(middleware: RequestHandler<BunRequest, BunResponse>): void;
|
|
27
|
-
private createHttpMethodHandler;
|
|
28
|
-
get(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
29
|
-
get(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
30
|
-
post(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
31
|
-
post(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
32
|
-
put(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
33
|
-
put(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
34
|
-
patch(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
35
|
-
patch(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
36
|
-
delete(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
37
|
-
delete(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
38
|
-
head(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
39
|
-
head(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
40
|
-
options(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
41
|
-
options(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
42
|
-
private createUnsupportedMethod;
|
|
43
|
-
all(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
44
|
-
all(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
45
|
-
propfind(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
46
|
-
propfind(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
47
|
-
proppatch(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
48
|
-
proppatch(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
49
|
-
mkcol(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
50
|
-
mkcol(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
51
|
-
copy(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
52
|
-
copy(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
53
|
-
move(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
54
|
-
move(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
55
|
-
lock(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
56
|
-
lock(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
57
|
-
unlock(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
58
|
-
unlock(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
59
|
-
search(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
60
|
-
search(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
15
|
+
protected instance: BunServerInstance;
|
|
16
|
+
constructor(bunServeOptions?: ServerOptions<BunWsClientData>);
|
|
61
17
|
useStaticAssets(...args: unknown[]): void;
|
|
62
18
|
setViewEngine(engine: string): void;
|
|
63
19
|
render(response: unknown, view: string, options: unknown): void;
|
|
@@ -94,20 +50,5 @@ export declare class BunAdapter extends AbstractHttpAdapter<Server<unknown>, Bun
|
|
|
94
50
|
* @param callback Optional callback to invoke once the server is listening.
|
|
95
51
|
*/
|
|
96
52
|
listen(port: string | number, hostname: string, callback?: () => void): void;
|
|
97
|
-
private
|
|
98
|
-
private static omitKeys;
|
|
99
|
-
private isWebSocketUpgradeRequest;
|
|
100
|
-
private handleWebSocketCors;
|
|
101
|
-
private upgradeWebSocket;
|
|
102
|
-
private setupWebSocketIfNeeded;
|
|
103
|
-
private createServer;
|
|
104
|
-
private delegateRouteHandler;
|
|
105
|
-
private ensureRouteExists;
|
|
106
|
-
private prepareRequestHandler;
|
|
107
|
-
private createRouteFetchHandler;
|
|
108
|
-
private createVersioningHandlers;
|
|
109
|
-
private executeHandlerChain;
|
|
110
|
-
private createChainedHandlerForVersioningResolution;
|
|
111
|
-
private mapRequestMethodToString;
|
|
112
|
-
private parseRouteHandler;
|
|
53
|
+
private configureTls;
|
|
113
54
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CorsOptionsDelegate, CorsOptions as NestCorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface.js';
|
|
2
|
+
import { Serve, Server, ServerWebSocket, WebSocketHandler } from 'bun';
|
|
3
|
+
import { BunRequest } from './bun.request.js';
|
|
4
|
+
export interface WsOptions extends Pick<WebSocketHandler<unknown>, 'maxPayloadLength' | 'idleTimeout' | 'backpressureLimit' | 'closeOnBackpressureLimit' | 'sendPings' | 'publishToSelf' | 'perMessageDeflate'> {
|
|
5
|
+
cors?: true | NestCorsOptions | CorsOptionsDelegate<BunRequest>;
|
|
6
|
+
clientDataFactory?: (req: BunRequest) => unknown;
|
|
7
|
+
}
|
|
8
|
+
export type ServerOptions<TWebSocketData = unknown> = Pick<Serve.Options<TWebSocketData>, 'development' | 'maxRequestBodySize' | 'idleTimeout' | 'id' | 'tls' | 'websocket' | 'port' | 'hostname'>;
|
|
9
|
+
export type WsData = string | Buffer | ArrayBuffer | Buffer[];
|
|
10
|
+
export interface WsHandlers {
|
|
11
|
+
onOpen: ((ws: ServerWebSocket<unknown>) => void) | undefined;
|
|
12
|
+
onMessage: ((ws: ServerWebSocket<unknown>, message: WsData, server: Server<unknown>) => void) | undefined;
|
|
13
|
+
onClose: ((ws: ServerWebSocket<unknown>, code: number, reason: string) => void) | undefined;
|
|
14
|
+
}
|
|
15
|
+
export interface BunWsClientData {
|
|
16
|
+
/** Called when a message is received - matches bun.adapter.ts onMessageInternal */
|
|
17
|
+
onMessageInternal?: (message: WsData) => void;
|
|
18
|
+
/** Called when the connection closes - matches bun.adapter.ts onCloseInternal */
|
|
19
|
+
onCloseInternal?: () => void;
|
|
20
|
+
/** Called by NestJS for disconnect handling */
|
|
21
|
+
onDisconnect?: (ws: ServerWebSocket<unknown>) => void;
|
|
22
|
+
}
|
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
import { Server, ServerWebSocket } from 'bun';
|
|
2
2
|
import { BaseWsInstance } from '@nestjs/websockets';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
setWsOptions(options: WsOptions): void;
|
|
6
|
-
getBunHttpServerInstance(): Server<unknown>;
|
|
7
|
-
getWsHandlers(): {
|
|
8
|
-
onOpen: ((ws: ServerWebSocket<unknown>) => void) | undefined;
|
|
9
|
-
onMessage: ((ws: ServerWebSocket<unknown>, message: string | ArrayBuffer | Buffer | Buffer[], server: Server<unknown>) => void) | undefined;
|
|
10
|
-
onClose: ((ws: ServerWebSocket<unknown>, code: number, reason: string) => void) | undefined;
|
|
11
|
-
};
|
|
12
|
-
getBunServerOptions(): Pick<ServerOptions, 'port' | 'hostname'>;
|
|
13
|
-
}
|
|
3
|
+
import { WsData, WsOptions } from './bun.internal.types.js';
|
|
4
|
+
import { BunServerInstance } from './bun.server-instance.js';
|
|
14
5
|
/**
|
|
15
6
|
* Bun HTTP server placeholder used before the actual server instance is created.
|
|
16
7
|
* This class provides compatibility methods expected by NestJS framework.
|
|
@@ -20,8 +11,8 @@ interface BunHttpAdapter {
|
|
|
20
11
|
* the server creation until NestJS calls the listen method.
|
|
21
12
|
*/
|
|
22
13
|
export declare class BunPreflightHttpServer implements BaseWsInstance {
|
|
23
|
-
private readonly
|
|
24
|
-
constructor(
|
|
14
|
+
private readonly serverInstance;
|
|
15
|
+
constructor(serverInstance: BunServerInstance);
|
|
25
16
|
on(event: string, callback: Function): void;
|
|
26
17
|
/**
|
|
27
18
|
* NestJS compatibility methods
|
|
@@ -38,25 +29,16 @@ export declare class BunPreflightHttpServer implements BaseWsInstance {
|
|
|
38
29
|
*/
|
|
39
30
|
stop(force?: boolean): Promise<void>;
|
|
40
31
|
address(): {
|
|
41
|
-
address:
|
|
42
|
-
port:
|
|
43
|
-
} | {
|
|
44
|
-
address: string | undefined;
|
|
45
|
-
port: number | undefined;
|
|
32
|
+
address: string;
|
|
33
|
+
port: number;
|
|
46
34
|
};
|
|
47
35
|
setWsOptions(options: WsOptions): void;
|
|
48
36
|
registerWsOpenHandler(handler: (ws: ServerWebSocket<unknown>) => void): void;
|
|
49
|
-
registerWsMessageHandler(handler: (ws: ServerWebSocket<unknown>, message:
|
|
37
|
+
registerWsMessageHandler(handler: (ws: ServerWebSocket<unknown>, message: WsData, server: Server<unknown>) => void): void;
|
|
50
38
|
registerWsCloseHandler(handler: (ws: ServerWebSocket<unknown>, code: number, reason: string) => void): void;
|
|
51
|
-
|
|
52
|
-
onOpen: ((ws: ServerWebSocket<unknown>) => void) | undefined;
|
|
53
|
-
onMessage: ((ws: ServerWebSocket<unknown>, message: string | ArrayBuffer | Buffer | Buffer[], server: Server<unknown>) => void) | undefined;
|
|
54
|
-
onClose: ((ws: ServerWebSocket<unknown>, code: number, reason: string) => void) | undefined;
|
|
55
|
-
};
|
|
56
|
-
getBunServer(): Server<unknown>;
|
|
39
|
+
getBunServer(): Server<unknown> | null;
|
|
57
40
|
/**
|
|
58
41
|
* Proxy method for WebSocket server close
|
|
59
42
|
*/
|
|
60
43
|
close(): Promise<void>;
|
|
61
44
|
}
|
|
62
|
-
export {};
|
package/dist/bun.request.d.ts
CHANGED
|
@@ -39,6 +39,9 @@ export declare class BunRequest {
|
|
|
39
39
|
*/
|
|
40
40
|
get socket(): {
|
|
41
41
|
encrypted: boolean;
|
|
42
|
+
setKeepAlive: () => void;
|
|
43
|
+
setNoDelay: () => void;
|
|
44
|
+
setTimeout: () => void;
|
|
42
45
|
};
|
|
43
46
|
/**
|
|
44
47
|
* Gets the URL path and query string of the request.
|
|
@@ -235,7 +238,7 @@ export declare class BunRequest {
|
|
|
235
238
|
* console.log(user); // { id: 1, name: 'John' }
|
|
236
239
|
* ```
|
|
237
240
|
*/
|
|
238
|
-
get(key: string):
|
|
241
|
+
get<T>(key: string): T | undefined;
|
|
239
242
|
/**
|
|
240
243
|
* Sets a custom setting/property in the request.
|
|
241
244
|
* Useful for passing data between middleware and handlers.
|
|
@@ -358,5 +361,42 @@ export declare class BunRequest {
|
|
|
358
361
|
* ```
|
|
359
362
|
*/
|
|
360
363
|
clone(): BunRequest;
|
|
364
|
+
/**
|
|
365
|
+
* Stub method for Node.js EventEmitter compatibility.
|
|
366
|
+
* This is a no-op method provided for compatibility with Node.js HTTP request objects.
|
|
367
|
+
* Required for SSE and other streaming scenarios where NestJS listens for request events.
|
|
368
|
+
*
|
|
369
|
+
* @param event - The event name
|
|
370
|
+
* @param listener - The event listener function
|
|
371
|
+
* @returns This request object for chaining
|
|
372
|
+
*/
|
|
373
|
+
on(event: string, listener: (...args: unknown[]) => void): this;
|
|
374
|
+
/**
|
|
375
|
+
* Stub method for Node.js EventEmitter compatibility.
|
|
376
|
+
* This is a no-op method provided for compatibility with Node.js HTTP request objects.
|
|
377
|
+
*
|
|
378
|
+
* @param event - The event name
|
|
379
|
+
* @param listener - The event listener function
|
|
380
|
+
* @returns This request object for chaining
|
|
381
|
+
*/
|
|
382
|
+
once(event: string, listener: (...args: unknown[]) => void): this;
|
|
383
|
+
/**
|
|
384
|
+
* Stub method for Node.js EventEmitter compatibility.
|
|
385
|
+
* This is a no-op method provided for compatibility with Node.js HTTP request objects.
|
|
386
|
+
*
|
|
387
|
+
* @param event - The event name
|
|
388
|
+
* @param listener - The event listener function
|
|
389
|
+
* @returns This request object for chaining
|
|
390
|
+
*/
|
|
391
|
+
off(event: string, listener: (...args: unknown[]) => void): this;
|
|
392
|
+
/**
|
|
393
|
+
* Stub method for Node.js EventEmitter compatibility.
|
|
394
|
+
* This is a no-op method provided for compatibility with Node.js HTTP request objects.
|
|
395
|
+
*
|
|
396
|
+
* @param event - The event name
|
|
397
|
+
* @param args - Event arguments
|
|
398
|
+
* @returns True to indicate the event was handled
|
|
399
|
+
*/
|
|
400
|
+
emit(event: string, ...args: unknown[]): boolean;
|
|
361
401
|
}
|
|
362
402
|
export {};
|
package/dist/bun.response.d.ts
CHANGED
|
@@ -18,11 +18,18 @@ export declare class BunResponse {
|
|
|
18
18
|
private readonly response;
|
|
19
19
|
private readonly cookieMap;
|
|
20
20
|
private static readonly textDecoder;
|
|
21
|
+
/**
|
|
22
|
+
* Property for Node.js Writable stream compatibility.
|
|
23
|
+
* Indicates this object can be written to.
|
|
24
|
+
*/
|
|
25
|
+
readonly writable = true;
|
|
21
26
|
private headers;
|
|
22
27
|
private statusCode;
|
|
23
28
|
private ended;
|
|
24
29
|
private cookieHeaders;
|
|
25
30
|
private chunks;
|
|
31
|
+
private streamWriter;
|
|
32
|
+
private textEncoder;
|
|
26
33
|
constructor();
|
|
27
34
|
private get headersMap();
|
|
28
35
|
/**
|
|
@@ -205,6 +212,21 @@ export declare class BunResponse {
|
|
|
205
212
|
* @returns This response object for chaining
|
|
206
213
|
*/
|
|
207
214
|
once(event: string, listener: (...args: unknown[]) => void): this;
|
|
215
|
+
/**
|
|
216
|
+
* Stub method for Node.js EventEmitter compatibility.
|
|
217
|
+
* This is a no-op method provided for compatibility with Node.js streams and HTTP response objects.
|
|
218
|
+
* Required when streams are piped to the response object (e.g., for SSE).
|
|
219
|
+
*
|
|
220
|
+
* @param event - The event name
|
|
221
|
+
* @param args - Event arguments
|
|
222
|
+
* @returns True to indicate the event was handled
|
|
223
|
+
*/
|
|
224
|
+
emit(event: string, ...args: unknown[]): boolean;
|
|
225
|
+
/**
|
|
226
|
+
* Property for Node.js Writable stream compatibility.
|
|
227
|
+
* Indicates whether the stream has ended.
|
|
228
|
+
*/
|
|
229
|
+
get writableEnded(): boolean;
|
|
208
230
|
/**
|
|
209
231
|
* Stub method for Node.js HTTP response compatibility.
|
|
210
232
|
* This method writes data to the response stream.
|
|
@@ -234,6 +256,12 @@ export declare class BunResponse {
|
|
|
234
256
|
* ```
|
|
235
257
|
*/
|
|
236
258
|
write(chunk: unknown): boolean;
|
|
259
|
+
/**
|
|
260
|
+
* Initializes streaming mode by creating a TransformStream and resolving the response.
|
|
261
|
+
* This is used for SSE and other streaming scenarios where data needs to be sent
|
|
262
|
+
* before end() is called.
|
|
263
|
+
*/
|
|
264
|
+
private initializeStreamingMode;
|
|
237
265
|
/**
|
|
238
266
|
* Gets the value of a response header.
|
|
239
267
|
* Header lookup is case-insensitive.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/external/cors-options.interface.js';
|
|
2
|
+
import { RequestMethod } from '@nestjs/common';
|
|
3
|
+
import { Server, ServerWebSocket } from 'bun';
|
|
4
|
+
import { RequestHandler } from '@nestjs/common/interfaces/index.js';
|
|
5
|
+
import { BunWsClientData, ServerOptions, WsData, WsOptions } from './bun.internal.types.js';
|
|
6
|
+
import { BunMiddlewareEngine } from './bun.middleware-engine.js';
|
|
7
|
+
import { BunRequest } from './bun.request.js';
|
|
8
|
+
import { BunResponse } from './bun.response.js';
|
|
9
|
+
/**
|
|
10
|
+
* BunServerInstance is the actual server instance that handles route registrations,
|
|
11
|
+
* WebSocket functionalities, and the listen method. This class is passed to
|
|
12
|
+
* AbstractHttpAdapter.setInstance() and implements the methods that NestJS expects
|
|
13
|
+
* from an HTTP server instance (similar to Express app or Fastify instance).
|
|
14
|
+
*/
|
|
15
|
+
export declare class BunServerInstance {
|
|
16
|
+
private readonly bunServeOptions;
|
|
17
|
+
private readonly logger;
|
|
18
|
+
private readonly middlewareEngine;
|
|
19
|
+
private useVersioning;
|
|
20
|
+
private readonly routes;
|
|
21
|
+
private readonly routeHandlers;
|
|
22
|
+
private notFoundHandler;
|
|
23
|
+
private readonly wsHandlers;
|
|
24
|
+
private readonly wsMiddlewareEngine;
|
|
25
|
+
private wsOptions;
|
|
26
|
+
private useWs;
|
|
27
|
+
private useWsCors;
|
|
28
|
+
private httpServer;
|
|
29
|
+
constructor(bunServeOptions: ServerOptions<BunWsClientData>);
|
|
30
|
+
use(maybePath: string | RequestHandler<BunRequest, BunResponse>, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
31
|
+
private createHttpMethodHandler;
|
|
32
|
+
get(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
33
|
+
post(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
34
|
+
put(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
35
|
+
patch(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
36
|
+
delete(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
37
|
+
head(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
38
|
+
options(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
39
|
+
private createUnsupportedMethod;
|
|
40
|
+
all(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
41
|
+
propfind(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
42
|
+
proppatch(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
43
|
+
mkcol(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
44
|
+
copy(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
45
|
+
move(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
46
|
+
lock(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
47
|
+
unlock(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
48
|
+
search(pathOrHandler: unknown, maybeHandler?: RequestHandler<BunRequest, BunResponse>): void;
|
|
49
|
+
/**
|
|
50
|
+
* Start listening on the specified port and hostname.
|
|
51
|
+
* @param port The port number or Unix socket path to listen on.
|
|
52
|
+
* @param hostnameOrCallback The hostname to bind to or the callback function.
|
|
53
|
+
* @param maybeCallback Optional callback to invoke once the server is listening.
|
|
54
|
+
*/
|
|
55
|
+
listen(port: string | number, hostnameOrCallback?: string | (() => void), maybeCallback?: () => void): Server<unknown>;
|
|
56
|
+
/**
|
|
57
|
+
* NestJS compatibility methods - stop the server
|
|
58
|
+
*/
|
|
59
|
+
stop(force?: boolean): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Get the address of the server
|
|
62
|
+
*/
|
|
63
|
+
address(): {
|
|
64
|
+
address: string;
|
|
65
|
+
port: number;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Set WebSocket options
|
|
69
|
+
*/
|
|
70
|
+
setWsOptions(options: WsOptions): void;
|
|
71
|
+
/**
|
|
72
|
+
* Register WebSocket open handler
|
|
73
|
+
*/
|
|
74
|
+
registerWsOpenHandler(handler: (ws: ServerWebSocket<unknown>) => void): void;
|
|
75
|
+
/**
|
|
76
|
+
* Register WebSocket message handler
|
|
77
|
+
*/
|
|
78
|
+
registerWsMessageHandler(handler: (ws: ServerWebSocket<unknown>, message: WsData, server: Server<unknown>) => void): void;
|
|
79
|
+
/**
|
|
80
|
+
* Register WebSocket close handler
|
|
81
|
+
*/
|
|
82
|
+
registerWsCloseHandler(handler: (ws: ServerWebSocket<unknown>, code: number, reason: string) => void): void;
|
|
83
|
+
/**
|
|
84
|
+
* Get the underlying Bun server
|
|
85
|
+
*/
|
|
86
|
+
getBunServer(): Server<unknown> | null;
|
|
87
|
+
/**
|
|
88
|
+
* Proxy method for WebSocket server close (BaseWsInstance)
|
|
89
|
+
*/
|
|
90
|
+
close(): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Get the middleware engine
|
|
93
|
+
*/
|
|
94
|
+
getMiddlewareEngine(): BunMiddlewareEngine;
|
|
95
|
+
/**
|
|
96
|
+
* Set the not found handler
|
|
97
|
+
*/
|
|
98
|
+
setNotFoundHandler(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
99
|
+
/**
|
|
100
|
+
* Enable versioning support
|
|
101
|
+
*/
|
|
102
|
+
setUseVersioning(value: boolean): void;
|
|
103
|
+
/**
|
|
104
|
+
* Register body parser middleware
|
|
105
|
+
*/
|
|
106
|
+
registerParserMiddleware(prefix?: string, rawBody?: boolean): void;
|
|
107
|
+
/**
|
|
108
|
+
* Create middleware factory for NestJS
|
|
109
|
+
*/
|
|
110
|
+
createMiddlewareFactory(requestMethod: RequestMethod): (path: string, callback: Function) => void;
|
|
111
|
+
/**
|
|
112
|
+
* Enable CORS middleware
|
|
113
|
+
*/
|
|
114
|
+
enableCors(corsOptions?: CorsOptions | CorsOptionsDelegate<BunRequest>, prefix?: string): void;
|
|
115
|
+
private static isNumericPort;
|
|
116
|
+
private static omitKeys;
|
|
117
|
+
private isWebSocketUpgradeRequest;
|
|
118
|
+
private provideCorsHeaders;
|
|
119
|
+
private upgradeWebSocket;
|
|
120
|
+
private setupWebSocketIfNeeded;
|
|
121
|
+
private createServer;
|
|
122
|
+
private delegateRouteHandler;
|
|
123
|
+
private ensureRouteExists;
|
|
124
|
+
private prepareRequestHandler;
|
|
125
|
+
private createRouteFetchHandler;
|
|
126
|
+
private createVersioningHandlers;
|
|
127
|
+
private executeHandlerChain;
|
|
128
|
+
private createChainedHandlerForVersioningResolution;
|
|
129
|
+
private mapRequestMethodToString;
|
|
130
|
+
private parseRouteHandler;
|
|
131
|
+
}
|
package/dist/bun.ws-adapter.d.ts
CHANGED
|
@@ -2,9 +2,8 @@ import { AbstractWsAdapter, BaseWsInstance, MessageMappingProperties } from '@ne
|
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
3
|
import { INestApplicationContext } from '@nestjs/common';
|
|
4
4
|
import { ServerWebSocket } from 'bun';
|
|
5
|
+
import { BunWsClientData, WsData, WsOptions } from './bun.internal.types.js';
|
|
5
6
|
import { BunPreflightHttpServer } from './bun.preflight-http-server.js';
|
|
6
|
-
import { WsOptions } from './internal.types.js';
|
|
7
|
-
export type WsData = string | Buffer | ArrayBuffer | Buffer[];
|
|
8
7
|
export type WsMessageParser<TData = unknown> = (data: WsData) => WsParsedData<TData>;
|
|
9
8
|
export interface WsParsedData<TData = unknown> {
|
|
10
9
|
event: string;
|
|
@@ -13,15 +12,6 @@ export interface WsParsedData<TData = unknown> {
|
|
|
13
12
|
export interface BunWsAdapterOptions extends WsOptions {
|
|
14
13
|
messageParser?: WsMessageParser;
|
|
15
14
|
}
|
|
16
|
-
/** Internal data stored on each WebSocket connection - must match bun.adapter.ts */
|
|
17
|
-
interface BunWsClientData {
|
|
18
|
-
/** Called when a message is received - matches bun.adapter.ts onMessageInternal */
|
|
19
|
-
onMessageInternal?: (message: WsData) => void;
|
|
20
|
-
/** Called when the connection closes - matches bun.adapter.ts onCloseInternal */
|
|
21
|
-
onCloseInternal?: () => void;
|
|
22
|
-
/** Called by NestJS for disconnect handling */
|
|
23
|
-
onDisconnect?: (ws: ServerWebSocket<unknown>) => void;
|
|
24
|
-
}
|
|
25
15
|
type BunWsClient = ServerWebSocket<BunWsClientData> & BaseWsInstance;
|
|
26
16
|
/**
|
|
27
17
|
* High-performance WebSocket adapter for Bun runtime with NestJS.
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './bun.adapter.js';
|
|
|
2
2
|
export * from './bun.request.js';
|
|
3
3
|
export * from './bun.response.js';
|
|
4
4
|
export * from './bun.file.interceptor.js';
|
|
5
|
-
export * from './internal.types.js';
|
|
5
|
+
export * from './bun.internal.types.js';
|
|
6
6
|
export * from './bun.preflight-http-server.js';
|
|
7
|
+
export * from './bun.server-instance.js';
|
|
7
8
|
export * from './bun.ws-adapter.js';
|