@krisanalfa/bunest-adapter 0.0.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.
- package/LICENSE +21 -0
- package/README.md +866 -0
- package/dist/bun.adapter.d.ts +93 -0
- package/dist/bun.file.interceptor.d.ts +9 -0
- package/dist/bun.request.d.ts +339 -0
- package/dist/bun.response.d.ts +251 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1089 -0
- package/dist/index.js.map +17 -0
- package/package.json +66 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/external/cors-options.interface.js';
|
|
2
|
+
import { ErrorHandler, RequestHandler } from '@nestjs/common/interfaces/index.js';
|
|
3
|
+
import { NestApplicationOptions, RequestMethod, VersioningOptions } from '@nestjs/common';
|
|
4
|
+
import { Serve, Server } from 'bun';
|
|
5
|
+
import { AbstractHttpAdapter } from '@nestjs/core';
|
|
6
|
+
import { VersionValue } from '@nestjs/common/interfaces/version-options.interface.js';
|
|
7
|
+
import { BunRequest } from './bun.request.js';
|
|
8
|
+
import { BunResponse } from './bun.response.js';
|
|
9
|
+
export declare class BunAdapter extends AbstractHttpAdapter<Server<unknown>, BunRequest, BunResponse> {
|
|
10
|
+
private bunServeOptions;
|
|
11
|
+
private readonly logger;
|
|
12
|
+
private readonly middlewareEngine;
|
|
13
|
+
private useVersioning;
|
|
14
|
+
private readonly routes;
|
|
15
|
+
private readonly routeHandlers;
|
|
16
|
+
private notFoundHandler;
|
|
17
|
+
constructor(bunServeOptions?: Pick<Serve.Options<unknown>, 'development' | 'maxRequestBodySize' | 'idleTimeout' | 'id' | 'tls'>);
|
|
18
|
+
use(middleware: RequestHandler<BunRequest, BunResponse>): void;
|
|
19
|
+
get(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
20
|
+
get(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
21
|
+
post(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
22
|
+
post(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
23
|
+
put(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
24
|
+
put(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
25
|
+
patch(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
26
|
+
patch(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
27
|
+
delete(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
28
|
+
delete(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
29
|
+
head(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
30
|
+
head(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
31
|
+
options(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
32
|
+
options(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
33
|
+
all(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
34
|
+
all(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
35
|
+
propfind(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
36
|
+
propfind(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
37
|
+
proppatch(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
38
|
+
proppatch(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
39
|
+
mkcol(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
40
|
+
mkcol(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
41
|
+
copy(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
42
|
+
copy(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
43
|
+
move(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
44
|
+
move(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
45
|
+
lock(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
46
|
+
lock(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
47
|
+
unlock(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
48
|
+
unlock(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
49
|
+
search(handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
50
|
+
search(path: unknown, handler: RequestHandler<BunRequest, BunResponse>): void;
|
|
51
|
+
useStaticAssets(...args: unknown[]): void;
|
|
52
|
+
setViewEngine(engine: string): void;
|
|
53
|
+
render(response: unknown, view: string, options: unknown): void;
|
|
54
|
+
close(): Promise<void>;
|
|
55
|
+
initHttpServer(options: NestApplicationOptions): void;
|
|
56
|
+
getRequestHostname(request: BunRequest): string;
|
|
57
|
+
getRequestMethod(request: BunRequest): string;
|
|
58
|
+
getRequestUrl(request: BunRequest): string;
|
|
59
|
+
status(response: BunResponse, statusCode: number): void;
|
|
60
|
+
reply(response: BunResponse, body: unknown, statusCode?: number): void;
|
|
61
|
+
end(response: BunResponse, message?: string): void;
|
|
62
|
+
redirect(response: BunResponse, statusCode: number, url: string): void;
|
|
63
|
+
setErrorHandler(handler: ErrorHandler<BunRequest, BunResponse>, prefix?: string): void;
|
|
64
|
+
setNotFoundHandler(handler: RequestHandler<BunRequest, BunResponse>, prefix?: string): void;
|
|
65
|
+
isHeadersSent(response: BunResponse): boolean;
|
|
66
|
+
getHeader(response: BunResponse, name: string): string | null;
|
|
67
|
+
setHeader(response: BunResponse, name: string, value: string): void;
|
|
68
|
+
appendHeader(response: BunResponse, name: string, value: string): void;
|
|
69
|
+
registerParserMiddleware(prefix?: string, rawBody?: boolean): void;
|
|
70
|
+
enableCors(options?: CorsOptions | CorsOptionsDelegate<BunRequest>, prefix?: string): void;
|
|
71
|
+
createMiddlewareFactory(requestMethod: RequestMethod): (path: string, callback: Function) => void;
|
|
72
|
+
getType(): string;
|
|
73
|
+
applyVersionFilter(handler: Function, version: VersionValue, versioningOptions: VersioningOptions): (req: BunRequest, res: BunResponse, next: () => void) => Function;
|
|
74
|
+
/**
|
|
75
|
+
* Start listening on the specified port and hostname.
|
|
76
|
+
* @param port The port number or Unix socket path to listen on.
|
|
77
|
+
* @param callback Optional callback to invoke once the server is listening.
|
|
78
|
+
*/
|
|
79
|
+
listen(port: string | number, callback?: () => void): void;
|
|
80
|
+
/**
|
|
81
|
+
* Start listening on the specified port and hostname.
|
|
82
|
+
* @param port The port number or Unix socket path to listen on.
|
|
83
|
+
* @param hostname The hostname to bind to.
|
|
84
|
+
* @param callback Optional callback to invoke once the server is listening.
|
|
85
|
+
*/
|
|
86
|
+
listen(port: string | number, hostname: string, callback?: () => void): void;
|
|
87
|
+
private delegateRouteHandler;
|
|
88
|
+
private createVersioningHandlers;
|
|
89
|
+
private executeHandlerChain;
|
|
90
|
+
private createChainedHandlerForVersioningResolution;
|
|
91
|
+
private mapRequestMethodToString;
|
|
92
|
+
private parseRouteHandler;
|
|
93
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
|
2
|
+
import { HttpAdapterHost } from '@nestjs/core';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
export declare class BunFileInterceptor implements NestInterceptor {
|
|
5
|
+
private readonly adapter;
|
|
6
|
+
private readonly uploadDir;
|
|
7
|
+
constructor(adapter: HttpAdapterHost);
|
|
8
|
+
intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<unknown>>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { CookieMap, BunRequest as NativeRequest } from 'bun';
|
|
2
|
+
import { ParsedQs } from 'qs';
|
|
3
|
+
type HeadersProxy = Record<string, string> & {
|
|
4
|
+
get: (key: string) => string | null;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* A high-performance request wrapper for Bun's native request object.
|
|
8
|
+
* Provides lazy parsing and caching for optimal performance in NestJS applications.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const bunRequest = new BunRequest(nativeRequest);
|
|
13
|
+
* const pathname = bunRequest.pathname; // Lazily parsed
|
|
14
|
+
* const query = bunRequest.query; // Parsed only when accessed
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare class BunRequest {
|
|
18
|
+
private readonly nativeRequest;
|
|
19
|
+
private _nativeHeaders;
|
|
20
|
+
private _headers;
|
|
21
|
+
private _hostname;
|
|
22
|
+
private _pathname;
|
|
23
|
+
private _query;
|
|
24
|
+
private _body;
|
|
25
|
+
private _rawBody;
|
|
26
|
+
private _file;
|
|
27
|
+
private _files;
|
|
28
|
+
private _settings;
|
|
29
|
+
private readonly _url;
|
|
30
|
+
private readonly _parsedUrl;
|
|
31
|
+
readonly method: string;
|
|
32
|
+
readonly params: Record<string, string>;
|
|
33
|
+
constructor(nativeRequest: NativeRequest);
|
|
34
|
+
/**
|
|
35
|
+
* Gets the full URL of the request.
|
|
36
|
+
*
|
|
37
|
+
* @returns The complete URL string
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const url = request.url; // "http://localhost:3000/api/users?page=1"
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
get url(): string;
|
|
44
|
+
/**
|
|
45
|
+
* Gets the pathname portion of the URL.
|
|
46
|
+
* Uses lazy parsing for optimal performance - the pathname is only extracted when first accessed.
|
|
47
|
+
*
|
|
48
|
+
* @returns The pathname component of the URL
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* // For URL "http://localhost:3000/api/users?page=1"
|
|
52
|
+
* const pathname = request.pathname; // "/api/users"
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
get pathname(): string;
|
|
56
|
+
/**
|
|
57
|
+
* Gets the hostname portion of the URL.
|
|
58
|
+
* Uses lazy parsing - the hostname is only extracted when first accessed.
|
|
59
|
+
*
|
|
60
|
+
* @returns The hostname component of the URL (without port)
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* // For URL "http://localhost:3000/api/users"
|
|
64
|
+
* const hostname = request.hostname; // "localhost"
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
get hostname(): string;
|
|
68
|
+
/**
|
|
69
|
+
* Gets all request headers as a key-value object.
|
|
70
|
+
* Uses lazy parsing - headers are materialized only when first accessed.
|
|
71
|
+
* All header keys are normalized to lowercase.
|
|
72
|
+
*
|
|
73
|
+
* @returns An object containing all headers with a .get() method for efficient lookups
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const headers = request.headers;
|
|
77
|
+
* const contentType = headers['content-type']; // Direct access
|
|
78
|
+
* const auth = headers.get('Authorization'); // Using .get() method
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
get headers(): HeadersProxy;
|
|
82
|
+
/**
|
|
83
|
+
* Gets the parsed query parameters from the URL.
|
|
84
|
+
* Uses lazy parsing - query string is only parsed when first accessed.
|
|
85
|
+
*
|
|
86
|
+
* @returns Parsed query parameters as an object
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* // For URL "http://localhost:3000/api/users?page=1&limit=10"
|
|
90
|
+
* const query = request.query;
|
|
91
|
+
* console.log(query.page); // "1"
|
|
92
|
+
* console.log(query.limit); // "10"
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
get query(): ParsedQs;
|
|
96
|
+
/**
|
|
97
|
+
* Gets the parsed request body.
|
|
98
|
+
*
|
|
99
|
+
* @returns The parsed body content (could be JSON, form data, etc.)
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const body = request.body;
|
|
103
|
+
* console.log(body); // { name: "John", email: "john@example.com" }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
get body(): unknown;
|
|
107
|
+
/**
|
|
108
|
+
* Sets the parsed request body.
|
|
109
|
+
* Typically used by body parser middleware.
|
|
110
|
+
*
|
|
111
|
+
* @param body - The parsed body content to set
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* request.setBody({ name: "John", email: "john@example.com" });
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
setBody(body: unknown): void;
|
|
118
|
+
/**
|
|
119
|
+
* Gets the raw request body as an ArrayBuffer.
|
|
120
|
+
*
|
|
121
|
+
* @returns The raw body data or null if not set
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const rawBody = request.rawBody;
|
|
125
|
+
* if (rawBody) {
|
|
126
|
+
* const text = new TextDecoder().decode(rawBody);
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
get rawBody(): ArrayBuffer | null;
|
|
131
|
+
/**
|
|
132
|
+
* Sets the raw request body as an ArrayBuffer.
|
|
133
|
+
*
|
|
134
|
+
* @param rawBody - The raw body data to set
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const buffer = await request.arrayBuffer();
|
|
138
|
+
* request.setRawBody(buffer);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
setRawBody(rawBody: ArrayBuffer): void;
|
|
142
|
+
/**
|
|
143
|
+
* Gets the uploaded file from the request.
|
|
144
|
+
* Used for single file uploads.
|
|
145
|
+
*
|
|
146
|
+
* @returns The uploaded file or null if no file was uploaded
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const file = request.file;
|
|
150
|
+
* if (file) {
|
|
151
|
+
* console.log(file.name); // "avatar.png"
|
|
152
|
+
* console.log(file.size); // 2048
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
get file(): File | null;
|
|
157
|
+
/**
|
|
158
|
+
* Sets the uploaded file in the request.
|
|
159
|
+
* Typically used by file upload middleware.
|
|
160
|
+
*
|
|
161
|
+
* @param file - The file to set
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* const formData = await request.formData();
|
|
165
|
+
* const file = formData.get('avatar') as File;
|
|
166
|
+
* request.setFile(file);
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
setFile(file: File): void;
|
|
170
|
+
/**
|
|
171
|
+
* Gets all uploaded files from the request.
|
|
172
|
+
* Used for multiple file uploads.
|
|
173
|
+
*
|
|
174
|
+
* @returns Array of uploaded files or null if no files were uploaded
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* const files = request.files;
|
|
178
|
+
* if (files) {
|
|
179
|
+
* files.forEach(file => {
|
|
180
|
+
* console.log(file.name, file.size);
|
|
181
|
+
* });
|
|
182
|
+
* }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
get files(): File[] | null;
|
|
186
|
+
/**
|
|
187
|
+
* Sets multiple uploaded files in the request.
|
|
188
|
+
* Typically used by file upload middleware.
|
|
189
|
+
*
|
|
190
|
+
* @param files - Array of files to set
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* const formData = await request.formData();
|
|
194
|
+
* const files = formData.getAll('attachments') as File[];
|
|
195
|
+
* request.setFiles(files);
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
setFiles(files: File[]): void;
|
|
199
|
+
/**
|
|
200
|
+
* Gets a custom setting/property stored in the request.
|
|
201
|
+
* Useful for passing data between middleware and handlers.
|
|
202
|
+
*
|
|
203
|
+
* @param key - The setting key to retrieve
|
|
204
|
+
* @returns The stored value or undefined if not found
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* // In middleware
|
|
208
|
+
* request.set('user', { id: 1, name: 'John' });
|
|
209
|
+
*
|
|
210
|
+
* // In handler
|
|
211
|
+
* const user = request.get('user');
|
|
212
|
+
* console.log(user); // { id: 1, name: 'John' }
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
get(key: string): unknown;
|
|
216
|
+
/**
|
|
217
|
+
* Sets a custom setting/property in the request.
|
|
218
|
+
* Useful for passing data between middleware and handlers.
|
|
219
|
+
*
|
|
220
|
+
* @param key - The setting key to store
|
|
221
|
+
* @param value - The value to store
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* request.set('user', { id: 1, name: 'John' });
|
|
225
|
+
* request.set('startTime', Date.now());
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
set(key: string, value: unknown): void;
|
|
229
|
+
/**
|
|
230
|
+
* Gets the AbortSignal for the request.
|
|
231
|
+
* Can be used to detect if the request has been cancelled.
|
|
232
|
+
*
|
|
233
|
+
* @returns The request's AbortSignal
|
|
234
|
+
* @example
|
|
235
|
+
* ```typescript
|
|
236
|
+
* const signal = request.signal;
|
|
237
|
+
* signal.addEventListener('abort', () => {
|
|
238
|
+
* console.log('Request was cancelled');
|
|
239
|
+
* });
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
get signal(): AbortSignal;
|
|
243
|
+
/**
|
|
244
|
+
* Gets the cookies from the request.
|
|
245
|
+
*
|
|
246
|
+
* @returns A Map-like object containing all cookies
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* const cookies = request.cookies;
|
|
250
|
+
* const sessionId = cookies.get('sessionId');
|
|
251
|
+
* console.log(sessionId?.value);
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
get cookies(): CookieMap;
|
|
255
|
+
/**
|
|
256
|
+
* Parses the request body as JSON.
|
|
257
|
+
*
|
|
258
|
+
* @returns Promise that resolves to the parsed JSON data
|
|
259
|
+
* @example
|
|
260
|
+
* ```typescript
|
|
261
|
+
* const data = await request.json();
|
|
262
|
+
* console.log(data); // { name: "John", email: "john@example.com" }
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
json(): Promise<unknown>;
|
|
266
|
+
/**
|
|
267
|
+
* Reads the request body as text.
|
|
268
|
+
*
|
|
269
|
+
* @returns Promise that resolves to the body text
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* const text = await request.text();
|
|
273
|
+
* console.log(text); // "Hello, World!"
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
text(): Promise<string>;
|
|
277
|
+
/**
|
|
278
|
+
* Parses the request body as FormData.
|
|
279
|
+
*
|
|
280
|
+
* @returns Promise that resolves to the parsed FormData
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const formData = await request.formData();
|
|
284
|
+
* const name = formData.get('name');
|
|
285
|
+
* console.log(name); // "John"
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
formData(): Promise<FormData>;
|
|
289
|
+
/**
|
|
290
|
+
* Reads the request body as an ArrayBuffer.
|
|
291
|
+
*
|
|
292
|
+
* @returns Promise that resolves to the body as ArrayBuffer
|
|
293
|
+
* @example
|
|
294
|
+
* ```typescript
|
|
295
|
+
* const buffer = await request.arrayBuffer();
|
|
296
|
+
* console.log(buffer.byteLength); // 1024
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
300
|
+
/**
|
|
301
|
+
* Reads the request body as a Blob.
|
|
302
|
+
*
|
|
303
|
+
* @returns Promise that resolves to the body as Blob
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* const blob = await request.blob();
|
|
307
|
+
* console.log(blob.type); // "image/png"
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
blob(): Promise<Blob>;
|
|
311
|
+
/**
|
|
312
|
+
* Reads the request body as a Uint8Array.
|
|
313
|
+
*
|
|
314
|
+
* @returns Promise that resolves to the body as Uint8Array
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* const bytes = await request.bytes();
|
|
318
|
+
* console.log(bytes.length); // 1024
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
bytes(): Promise<Uint8Array>;
|
|
322
|
+
/**
|
|
323
|
+
* Creates a deep clone of the request.
|
|
324
|
+
* Clones both the native request and all cached properties.
|
|
325
|
+
*
|
|
326
|
+
* @returns A new BunRequest instance with cloned data
|
|
327
|
+
* @example
|
|
328
|
+
* ```typescript
|
|
329
|
+
* const originalRequest = new BunRequest(nativeRequest);
|
|
330
|
+
* const clonedRequest = originalRequest.clone();
|
|
331
|
+
*
|
|
332
|
+
* // Modifications to clone don't affect original
|
|
333
|
+
* clonedRequest.set('user', { id: 1 });
|
|
334
|
+
* console.log(originalRequest.get('user')); // undefined
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
clone(): BunRequest;
|
|
338
|
+
}
|
|
339
|
+
export {};
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { Cookie, CookieInit, CookieStoreDeleteOptions } from 'bun';
|
|
2
|
+
/**
|
|
3
|
+
* A high-performance response builder for Bun's native Response object.
|
|
4
|
+
* Provides methods to build responses with headers, cookies, and various body types.
|
|
5
|
+
* Uses lazy initialization and optimized response building for maximum performance.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const response = new BunResponse();
|
|
10
|
+
* response.setStatus(200);
|
|
11
|
+
* response.setHeader('Content-Type', 'application/json');
|
|
12
|
+
* response.cookie('sessionId', 'abc123');
|
|
13
|
+
* response.end({ message: 'Success' });
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare class BunResponse {
|
|
17
|
+
private resolve;
|
|
18
|
+
private readonly response;
|
|
19
|
+
private readonly cookieMap;
|
|
20
|
+
private _headers;
|
|
21
|
+
private statusCode;
|
|
22
|
+
private ended;
|
|
23
|
+
private _cookieHeaderCache;
|
|
24
|
+
constructor();
|
|
25
|
+
private get headersMap();
|
|
26
|
+
/**
|
|
27
|
+
* Sets a cookie in the response.
|
|
28
|
+
* Can be called with either a cookie options object or name-value pair.
|
|
29
|
+
*
|
|
30
|
+
* @param options - Cookie configuration object
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // Using name-value pair
|
|
34
|
+
* response.cookie('sessionId', 'abc123');
|
|
35
|
+
*
|
|
36
|
+
* // Using options object
|
|
37
|
+
* response.cookie({
|
|
38
|
+
* name: 'sessionId',
|
|
39
|
+
* value: 'abc123',
|
|
40
|
+
* httpOnly: true,
|
|
41
|
+
* secure: true,
|
|
42
|
+
* maxAge: 3600
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
cookie(options: CookieInit | Cookie): void;
|
|
47
|
+
/**
|
|
48
|
+
* Sets a cookie in the response using name and value.
|
|
49
|
+
*
|
|
50
|
+
* @param name - The cookie name
|
|
51
|
+
* @param value - The cookie value
|
|
52
|
+
*/
|
|
53
|
+
cookie(name: string, value: string): void;
|
|
54
|
+
/**
|
|
55
|
+
* Deletes a cookie from the response.
|
|
56
|
+
*
|
|
57
|
+
* @param optionsOrName - Cookie name or delete options
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* // Delete by name
|
|
61
|
+
* response.deleteCookie('sessionId');
|
|
62
|
+
*
|
|
63
|
+
* // Delete with options
|
|
64
|
+
* response.deleteCookie({
|
|
65
|
+
* name: 'sessionId',
|
|
66
|
+
* path: '/',
|
|
67
|
+
* domain: 'example.com'
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
deleteCookie(optionsOrName: CookieStoreDeleteOptions | string): void;
|
|
72
|
+
/**
|
|
73
|
+
* Deletes a cookie from the response with additional options.
|
|
74
|
+
*
|
|
75
|
+
* @param optionsOrName - Cookie name
|
|
76
|
+
* @param options - Additional delete options (path, domain, etc.)
|
|
77
|
+
*/
|
|
78
|
+
deleteCookie(optionsOrName: string, options: Omit<CookieStoreDeleteOptions, 'name'>): void;
|
|
79
|
+
/**
|
|
80
|
+
* Sends a redirect response to the specified URL.
|
|
81
|
+
* Ends the response after calling this method.
|
|
82
|
+
*
|
|
83
|
+
* @param url - The URL to redirect to
|
|
84
|
+
* @param statusCode - HTTP status code for the redirect (default: 302)
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // Temporary redirect (302)
|
|
88
|
+
* response.redirect('/login');
|
|
89
|
+
*
|
|
90
|
+
* // Permanent redirect (301)
|
|
91
|
+
* response.redirect('/new-page', 301);
|
|
92
|
+
*
|
|
93
|
+
* // See Other (303)
|
|
94
|
+
* response.redirect('/success', 303);
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
redirect(url: string, statusCode?: number): void;
|
|
98
|
+
/**
|
|
99
|
+
* Ends the response and sends the body to the client.
|
|
100
|
+
* Automatically handles JSON serialization, streams, and binary data.
|
|
101
|
+
* Can only be called once per response.
|
|
102
|
+
*
|
|
103
|
+
* @param body - The response body (JSON, string, Uint8Array, StreamableFile, or undefined)
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* // Send JSON response
|
|
107
|
+
* response.end({ message: 'Success', data: { id: 1 } });
|
|
108
|
+
*
|
|
109
|
+
* // Send empty response
|
|
110
|
+
* response.setStatus(204);
|
|
111
|
+
* response.end();
|
|
112
|
+
*
|
|
113
|
+
* // Send binary data
|
|
114
|
+
* const buffer = new Uint8Array([1, 2, 3]);
|
|
115
|
+
* response.end(buffer);
|
|
116
|
+
*
|
|
117
|
+
* // Send file stream
|
|
118
|
+
* const file = new StreamableFile(stream);
|
|
119
|
+
* response.end(file);
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
end(body?: unknown): void;
|
|
123
|
+
/**
|
|
124
|
+
* Sets a response header.
|
|
125
|
+
* Header names are automatically normalized to lowercase.
|
|
126
|
+
*
|
|
127
|
+
* @param name - The header name
|
|
128
|
+
* @param value - The header value
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* response.setHeader('Content-Type', 'application/json');
|
|
132
|
+
* response.setHeader('Cache-Control', 'no-cache');
|
|
133
|
+
* response.setHeader('X-Custom-Header', 'custom-value');
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
setHeader(name: string, value: string): void;
|
|
137
|
+
/**
|
|
138
|
+
* Gets the value of a response header.
|
|
139
|
+
* Header lookup is case-insensitive.
|
|
140
|
+
*
|
|
141
|
+
* @param name - The header name to retrieve
|
|
142
|
+
* @returns The header value or null if not set
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* response.setHeader('Content-Type', 'application/json');
|
|
146
|
+
* const contentType = response.getHeader('content-type');
|
|
147
|
+
* console.log(contentType); // "application/json"
|
|
148
|
+
*
|
|
149
|
+
* const missing = response.getHeader('X-Missing');
|
|
150
|
+
* console.log(missing); // null
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
getHeader(name: string): string | null;
|
|
154
|
+
/**
|
|
155
|
+
* Appends a value to an existing response header.
|
|
156
|
+
* If the header doesn't exist, it will be created.
|
|
157
|
+
* Multiple values are joined with a comma as per RFC 9110.
|
|
158
|
+
*
|
|
159
|
+
* @param name - The header name
|
|
160
|
+
* @param value - The value to append
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* response.setHeader('Cache-Control', 'no-cache');
|
|
164
|
+
* response.appendHeader('Cache-Control', 'no-store');
|
|
165
|
+
* // Results in: "Cache-Control: no-cache, no-store"
|
|
166
|
+
*
|
|
167
|
+
* response.appendHeader('X-Custom', 'value1');
|
|
168
|
+
* response.appendHeader('X-Custom', 'value2');
|
|
169
|
+
* // Results in: "X-Custom: value1, value2"
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
appendHeader(name: string, value: string): void;
|
|
173
|
+
/**
|
|
174
|
+
* Removes a response header.
|
|
175
|
+
* Header lookup is case-insensitive.
|
|
176
|
+
*
|
|
177
|
+
* @param name - The header name to remove
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* response.setHeader('X-Custom-Header', 'value');
|
|
181
|
+
* response.removeHeader('X-Custom-Header');
|
|
182
|
+
*
|
|
183
|
+
* const header = response.getHeader('X-Custom-Header');
|
|
184
|
+
* console.log(header); // null
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
removeHeader(name: string): void;
|
|
188
|
+
/**
|
|
189
|
+
* Sets the HTTP status code for the response.
|
|
190
|
+
*
|
|
191
|
+
* @param code - The HTTP status code (e.g., 200, 404, 500)
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* response.setStatus(200); // OK
|
|
195
|
+
* response.setStatus(201); // Created
|
|
196
|
+
* response.setStatus(400); // Bad Request
|
|
197
|
+
* response.setStatus(404); // Not Found
|
|
198
|
+
* response.setStatus(500); // Internal Server Error
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
setStatus(code: number): void;
|
|
202
|
+
/**
|
|
203
|
+
* Gets the current HTTP status code of the response.
|
|
204
|
+
*
|
|
205
|
+
* @returns The HTTP status code
|
|
206
|
+
* @example
|
|
207
|
+
* ```typescript
|
|
208
|
+
* response.setStatus(404);
|
|
209
|
+
* const status = response.getStatus();
|
|
210
|
+
* console.log(status); // 404
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
getStatus(): number;
|
|
214
|
+
/**
|
|
215
|
+
* Returns a Promise that resolves to the native Response object.
|
|
216
|
+
* The Promise resolves when end() or redirect() is called.
|
|
217
|
+
*
|
|
218
|
+
* @returns Promise that resolves to the Bun Response object
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* const response = new BunResponse();
|
|
222
|
+
* response.setStatus(200);
|
|
223
|
+
* response.end({ message: 'Success' });
|
|
224
|
+
*
|
|
225
|
+
* const nativeResponse = await response.res();
|
|
226
|
+
* console.log(nativeResponse.status); // 200
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
res(): Promise<Response>;
|
|
230
|
+
/**
|
|
231
|
+
* Checks if the response has been ended.
|
|
232
|
+
* Once ended, no further modifications can be made to the response.
|
|
233
|
+
*
|
|
234
|
+
* @returns true if the response has been ended, false otherwise
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* const response = new BunResponse();
|
|
238
|
+
* console.log(response.isEnded()); // false
|
|
239
|
+
*
|
|
240
|
+
* response.end({ message: 'Done' });
|
|
241
|
+
* console.log(response.isEnded()); // true
|
|
242
|
+
*
|
|
243
|
+
* // This will be ignored since response is already ended
|
|
244
|
+
* response.setHeader('X-Custom', 'value');
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
isEnded(): boolean;
|
|
248
|
+
private buildStreamableResponse;
|
|
249
|
+
private buildJsonResponse;
|
|
250
|
+
private createResponse;
|
|
251
|
+
}
|
package/dist/index.d.ts
ADDED