@linebridge/engine-neo 1.0.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.
@@ -0,0 +1,306 @@
1
+ /**
2
+ * @fileoverview Full HTTP response wrapper for the Neo engine.
3
+ *
4
+ * Wraps a uWS HttpResponse and provides Express-style methods for status codes,
5
+ * headers, cookies (with signed cookie support), streaming, file serving,
6
+ * Server-Sent Events, redirects, and JSON/HTML shortcuts.
7
+ */
8
+ import SSEventStream from "./SSEventStream.js";
9
+ import type { HttpResponse } from "uWebSockets.js";
10
+ import type { EngineAdaptor } from "linebridge/classes/EngineAdaptor/index";
11
+ import type { Route } from "linebridge/classes/Route/index";
12
+ import type { Server } from "linebridge/server";
13
+ import type { Response as BaseHttpResponse } from "linebridge/classes/Handler/http";
14
+ import type Request from "./request.js";
15
+ type EventHandler = (...args: any[]) => void;
16
+ /**
17
+ * HTTP Response class wrapping a uWS HttpResponse.
18
+ *
19
+ * Provides methods for status codes, headers, cookies, streaming, file serving,
20
+ * event listeners (abort, close, finish), and SSE.
21
+ *
22
+ * @typeParam TServer - The server type this response belongs to.
23
+ */
24
+ export default class Response<TServer = Server<any>> implements BaseHttpResponse {
25
+ constructor();
26
+ /** Active SSE stream, if the client requested it. */
27
+ _sse: SSEventStream | null;
28
+ /** Per-response local storage for middleware communication. */
29
+ _locals: any;
30
+ /** The matched route. */
31
+ route: Route<TServer> | null;
32
+ /** Whether the response is in streaming mode. */
33
+ _streaming: boolean;
34
+ /** Tracks the furthest middleware index executed to prevent double-next(). */
35
+ _middleware_cursor: number;
36
+ /** The paired Request instance. */
37
+ _wrapped_request: Request;
38
+ /** The uWS upgrade socket (WebSocket upgrade requests only). */
39
+ _upgrade_socket: any;
40
+ /** The raw uWS HttpResponse object. */
41
+ _raw_response: HttpResponse | null;
42
+ /** HTTP status code (defaults to 200). */
43
+ _status_code: number;
44
+ /** Custom status message (overrides the default from STATUS_CODES). */
45
+ _status_message: string | null;
46
+ /** Response headers (key → value or key → string[]). */
47
+ _headers: Record<string, string | string[]>;
48
+ /** Set-Cookie headers (name → serialized cookie string). */
49
+ _cookies: Record<string, string> | null;
50
+ /** Whether the response has been sent. */
51
+ completed: boolean;
52
+ /** Whether headers have been written to the wire. */
53
+ initiated: boolean;
54
+ /** If true, the response body is wrapped in uWS `cork()` for batching. */
55
+ _cork: boolean;
56
+ /** Has `cork()` already been applied? Prevents double-corking. */
57
+ _corked: boolean;
58
+ /** The drain handler registered for backpressure-aware streaming. */
59
+ _drain_handler: ((offset: number) => boolean) | null;
60
+ /** Event listeners (abort, close, finish). */
61
+ _events: Record<string, EventHandler[]> | null;
62
+ /**
63
+ * The engine that owns this response.
64
+ */
65
+ get engine(): EngineAdaptor | null;
66
+ /**
67
+ * Creates a Response instance and registers the uWS `onAborted` callback.
68
+ *
69
+ * The `onAborted` handler marks the response as completed, cleans up the
70
+ * pending request counter, stops body parsing on the paired request, and
71
+ * fires abort/close event listeners.
72
+ */
73
+ static create<TServer extends Server<any>>(raw_response: HttpResponse, route: Route<TServer>, request: any, socket: any): Response<TServer>;
74
+ /**
75
+ * Registers an event listener on the response.
76
+ * Supported events: `"abort"`, `"close"`, `"finish"`.
77
+ */
78
+ on(event: string, handler: EventHandler): this;
79
+ /**
80
+ * Registers an event listener that fires only once.
81
+ */
82
+ once(event: string, handler: EventHandler): this;
83
+ /**
84
+ * Removes an event listener.
85
+ */
86
+ off(event: string, handler: EventHandler): this;
87
+ listenerCount(event: string): number;
88
+ /**
89
+ * Tracks the current middleware cursor position.
90
+ *
91
+ * Throws if a middleware tries to advance backwards (i.e. double-next detection).
92
+ *
93
+ * @returns The new cursor position.
94
+ */
95
+ _track_middleware_cursor(position: number): number;
96
+ /**
97
+ * Wraps a handler in uWS `cork()` for batched writes.
98
+ * All writes inside the handler are sent in a single TCP segment.
99
+ */
100
+ atomic(handler: Function): this;
101
+ /**
102
+ * Sets the HTTP status code. Optionally sets a custom status message.
103
+ */
104
+ status(code: number, message?: string): this;
105
+ /**
106
+ * Sets the `Content-Type` header from a file extension or MIME string.
107
+ * Prefix the value with a dot to indicate a file extension (e.g. `".html"`).
108
+ */
109
+ type(mime_type: string): this;
110
+ /**
111
+ * Sets a response header. If the header already exists and `overwrite` is false,
112
+ * the values are accumulated as an array.
113
+ */
114
+ header(name: string, value: string | string[], overwrite?: boolean): this;
115
+ /**
116
+ * Sets a `Set-Cookie` header.
117
+ *
118
+ * @param name - Cookie name.
119
+ * @param value - Cookie value. Pass `null` to delete the cookie.
120
+ * @param expiry - Expiry time in milliseconds from now.
121
+ * @param options - Cookie options (secure, sameSite, path, etc.).
122
+ * @param sign_cookie - If true and `options.secret` is set, signs the cookie value.
123
+ */
124
+ cookie(name: string, value: string | null, expiry?: number | null, options?: any, sign_cookie?: boolean): this;
125
+ /**
126
+ * Upgrades the HTTP connection to a WebSocket.
127
+ *
128
+ * @param context - User data to attach to the WebSocket context.
129
+ */
130
+ upgrade(context?: any): this | undefined;
131
+ /**
132
+ * Writes the status line and all accumulated headers to the uWS response.
133
+ *
134
+ * Returns `false` if headers were already initiated (idempotent).
135
+ */
136
+ _initiate_response(): boolean;
137
+ /**
138
+ * Registers a drain handler for streaming responses.
139
+ *
140
+ * @param handler - Called when uWS signals the socket is writable.
141
+ * Must return `true` if the chunk was successfully written.
142
+ */
143
+ drain(handler: (offset: number) => boolean): void;
144
+ /**
145
+ * Sends the response body and marks the response as completed.
146
+ *
147
+ * Supports corked (batched) and uncorked paths, streaming mode, and
148
+ * `endWithoutBody` when a custom `content-length` header is set.
149
+ *
150
+ * Fires `finish` and `close` event listeners.
151
+ */
152
+ send(body?: any, close_connection?: boolean): this;
153
+ _uws_write_chunk(chunk: any, total_size?: number): [boolean, boolean];
154
+ /**
155
+ * Writes a chunk to the response body.
156
+ * Initiates headers if not yet sent. Returns false under backpressure.
157
+ */
158
+ write(chunk: string): boolean;
159
+ /**
160
+ * Streams a single chunk with backpressure support.
161
+ * If the chunk doesn't fit in the uWS send buffer, it registers a drain handler.
162
+ */
163
+ _stream_chunk(chunk: any, total_size?: number): Promise<void>;
164
+ /**
165
+ * Streams a Readable to the client chunk by chunk.
166
+ *
167
+ * @param readable - A Node.js Readable stream.
168
+ * @param total_size - Total size hint for uWS `tryEnd`.
169
+ */
170
+ stream(readable: any, total_size?: number): Promise<void>;
171
+ /**
172
+ * Immediately closes the response (hard abort).
173
+ */
174
+ close(): void;
175
+ /**
176
+ * Sends a 302 redirect response.
177
+ */
178
+ redirect(url: string): boolean;
179
+ /**
180
+ * Fast-path send used by the `_invokeHandler`.
181
+ * Skips cork/uncork logic and writes directly.
182
+ */
183
+ _sendFast(body: any): void;
184
+ /**
185
+ * Sends a JSON response. Sets `Content-Type: application/json` and
186
+ * serializes the body with `JSON.stringify`.
187
+ */
188
+ json(body: any): this;
189
+ /**
190
+ * Sends an HTML response. Sets `Content-Type: text/html`.
191
+ */
192
+ html(body: any): this;
193
+ /**
194
+ * Per-response local storage for middleware communication.
195
+ */
196
+ get locals(): Record<string, any>;
197
+ /**
198
+ * The raw uWS HttpResponse.
199
+ */
200
+ get raw(): HttpResponse | null;
201
+ /**
202
+ * Whether the response has been aborted by the client.
203
+ */
204
+ get aborted(): boolean;
205
+ /**
206
+ * The uWS upgrade socket handle (WebSocket upgrade requests only).
207
+ */
208
+ get upgrade_socket(): any;
209
+ /**
210
+ * Accessor for Server-Sent Events.
211
+ *
212
+ * Only available on GET requests. Creates a lazy SSEventStream on first access.
213
+ */
214
+ get sse(): SSEventStream | undefined;
215
+ /**
216
+ * The current uWS write offset. Returns `-1` if the response is completed.
217
+ */
218
+ get write_offset(): number;
219
+ /**
220
+ * The HTTP status code.
221
+ */
222
+ get statusCode(): number;
223
+ set statusCode(value: number);
224
+ /**
225
+ * The HTTP status message.
226
+ */
227
+ get statusMessage(): string | null;
228
+ set statusMessage(value: string | null);
229
+ /**
230
+ * Whether response headers have been sent.
231
+ */
232
+ get headersSent(): boolean;
233
+ /**
234
+ * Appends values to a header (alias for `header()`).
235
+ */
236
+ append(name: string, values: any): this;
237
+ /**
238
+ * Sets a header value (alias for `header()` with overwrite behaviour).
239
+ */
240
+ setHeader(name: string, values: any): this;
241
+ /**
242
+ * Writes multiple headers at once from an object.
243
+ */
244
+ writeHeaders(headers: any): void;
245
+ /**
246
+ * Alias for `writeHeaders()`.
247
+ */
248
+ setHeaders(headers: any): void;
249
+ /**
250
+ * Writes multiple values for a single header.
251
+ */
252
+ writeHeaderValues(name: string, values: any): void;
253
+ /**
254
+ * Returns the value(s) of a header.
255
+ */
256
+ getHeader(name: string): string | string[];
257
+ /**
258
+ * Removes a header.
259
+ */
260
+ removeHeader(name: string): void;
261
+ /**
262
+ * Sets a cookie (shorthand for `cookie()`).
263
+ */
264
+ setCookie(name: string, value: any, options: any): this;
265
+ /**
266
+ * Checks if a cookie has been queued.
267
+ */
268
+ hasCookie(name: string): boolean;
269
+ /**
270
+ * Removes a cookie by setting it to null.
271
+ */
272
+ removeCookie(name: string): this;
273
+ /**
274
+ * Alias for `removeCookie()`.
275
+ */
276
+ clearCookie(name: string): this;
277
+ /**
278
+ * Alias for `send()`.
279
+ */
280
+ end(data?: any): this;
281
+ /**
282
+ * Returns the header value(s) for a given name.
283
+ */
284
+ get(name: string): string | undefined;
285
+ /**
286
+ * Sets the `Link` header from an object mapping rel → URL.
287
+ */
288
+ links(links: any): void;
289
+ /**
290
+ * Sets the `Location` header, used for redirects.
291
+ */
292
+ location(path: string): this;
293
+ /**
294
+ * Sends a status code response.
295
+ */
296
+ sendStatus(status_code: number): this;
297
+ /**
298
+ * Sets headers using either `(key, value)` or `({ key: value })`.
299
+ */
300
+ set(field: any, value: any): void;
301
+ /**
302
+ * Sets the `Vary` header.
303
+ */
304
+ vary(name: string): this;
305
+ }
306
+ export {};