@whatwg-node/server 0.10.0-alpha-20241125144944-df106a17746126e3c8089afa16af852a4483c8b3 → 0.10.0-alpha-20250205010145-5ab5edf75d82f8174c999dc970ad9a042ea5547d

Sign up to get free protection for your applications and to get access to all the features.
package/esm/utils.js CHANGED
@@ -47,39 +47,9 @@ function isRequestBody(body) {
47
47
  }
48
48
  return false;
49
49
  }
50
- export class ServerAdapterRequestAbortSignal extends EventTarget {
51
- aborted = false;
52
- _onabort = null;
53
- reason;
54
- throwIfAborted() {
55
- if (this.aborted) {
56
- throw this.reason;
57
- }
58
- }
59
- sendAbort() {
60
- this.reason = new DOMException('This operation was aborted', 'AbortError');
61
- this.aborted = true;
62
- this.dispatchEvent(new Event('abort'));
63
- }
64
- get onabort() {
65
- return this._onabort;
66
- }
67
- set onabort(value) {
68
- this._onabort = value;
69
- if (value) {
70
- this.addEventListener('abort', value);
71
- }
72
- else {
73
- this.removeEventListener('abort', value);
74
- }
75
- }
76
- any(signals) {
77
- return AbortSignal.any([...signals]);
78
- }
79
- }
80
50
  let bunNodeCompatModeWarned = false;
81
51
  export const nodeRequestResponseMap = new WeakMap();
82
- export function normalizeNodeRequest(nodeRequest, fetchAPI, registerSignal) {
52
+ export function normalizeNodeRequest(nodeRequest, fetchAPI) {
83
53
  const rawRequest = nodeRequest.raw || nodeRequest.req || nodeRequest;
84
54
  let fullUrl = buildFullUrl(rawRequest);
85
55
  if (nodeRequest.query) {
@@ -89,7 +59,6 @@ export function normalizeNodeRequest(nodeRequest, fetchAPI, registerSignal) {
89
59
  }
90
60
  fullUrl = url.toString();
91
61
  }
92
- let signal;
93
62
  const nodeResponse = nodeRequestResponseMap.get(nodeRequest);
94
63
  nodeRequestResponseMap.delete(nodeRequest);
95
64
  let normalizedHeaders = nodeRequest.headers;
@@ -101,24 +70,12 @@ export function normalizeNodeRequest(nodeRequest, fetchAPI, registerSignal) {
101
70
  }
102
71
  }
103
72
  }
73
+ const controller = new AbortController();
104
74
  if (nodeResponse?.once) {
105
- let sendAbortSignal;
106
- // If ponyfilled
107
- if (fetchAPI.Request !== globalThis.Request) {
108
- const newSignal = new ServerAdapterRequestAbortSignal();
109
- registerSignal?.(newSignal);
110
- signal = newSignal;
111
- sendAbortSignal = () => signal.sendAbort();
112
- }
113
- else {
114
- const controller = new AbortController();
115
- signal = controller.signal;
116
- sendAbortSignal = () => controller.abort();
117
- }
118
75
  const closeEventListener = () => {
119
- if (signal && !signal.aborted) {
120
- rawRequest.aborted = true;
121
- sendAbortSignal();
76
+ if (!controller.signal.aborted) {
77
+ Object.defineProperty(rawRequest, 'aborted', { value: true });
78
+ controller.abort(nodeResponse.errored ?? undefined);
122
79
  }
123
80
  };
124
81
  nodeResponse.once('error', closeEventListener);
@@ -131,7 +88,7 @@ export function normalizeNodeRequest(nodeRequest, fetchAPI, registerSignal) {
131
88
  return new fetchAPI.Request(fullUrl, {
132
89
  method: nodeRequest.method,
133
90
  headers: normalizedHeaders,
134
- signal,
91
+ signal: controller.signal,
135
92
  });
136
93
  }
137
94
  /**
@@ -144,16 +101,16 @@ export function normalizeNodeRequest(nodeRequest, fetchAPI, registerSignal) {
144
101
  if (maybeParsedBody != null && Object.keys(maybeParsedBody).length > 0) {
145
102
  if (isRequestBody(maybeParsedBody)) {
146
103
  return new fetchAPI.Request(fullUrl, {
147
- method: nodeRequest.method,
104
+ method: nodeRequest.method || 'GET',
148
105
  headers: normalizedHeaders,
149
106
  body: maybeParsedBody,
150
- signal,
107
+ signal: controller.signal,
151
108
  });
152
109
  }
153
110
  const request = new fetchAPI.Request(fullUrl, {
154
- method: nodeRequest.method,
111
+ method: nodeRequest.method || 'GET',
155
112
  headers: normalizedHeaders,
156
- signal,
113
+ signal: controller.signal,
157
114
  });
158
115
  if (!request.headers.get('content-type')?.includes('json')) {
159
116
  request.headers.set('content-type', 'application/json; charset=utf-8');
@@ -198,16 +155,17 @@ It will affect your performance. Please check our Bun integration recipe, and av
198
155
  rawRequest.destroy(e);
199
156
  },
200
157
  }),
201
- signal,
158
+ signal: controller.signal,
202
159
  });
203
160
  }
204
161
  // perf: instead of spreading the object, we can just pass it as is and it performs better
205
162
  return new fetchAPI.Request(fullUrl, {
206
163
  method: nodeRequest.method,
207
164
  headers: normalizedHeaders,
165
+ signal: controller.signal,
166
+ // @ts-expect-error - AsyncIterable is supported as body
208
167
  body: rawRequest,
209
168
  duplex: 'half',
210
- signal,
211
169
  });
212
170
  }
213
171
  export function isReadable(stream) {
@@ -253,13 +211,25 @@ async function sendAsyncIterable(serverResponse, asyncIterable) {
253
211
  if (closed) {
254
212
  break;
255
213
  }
256
- if (!serverResponse
257
- // @ts-expect-error http and http2 writes are actually compatible
258
- .write(chunk)) {
259
- if (closed) {
260
- break;
214
+ const shouldBreak = await new Promise(resolve => {
215
+ if (!serverResponse
216
+ // @ts-expect-error http and http2 writes are actually compatible
217
+ .write(chunk, err => {
218
+ if (err) {
219
+ resolve(true);
220
+ }
221
+ })) {
222
+ if (closed) {
223
+ resolve(true);
224
+ return;
225
+ }
226
+ serverResponse.once('drain', () => {
227
+ resolve(false);
228
+ });
261
229
  }
262
- await new Promise(resolve => serverResponse.once('drain', resolve));
230
+ });
231
+ if (shouldBreak) {
232
+ break;
263
233
  }
264
234
  }
265
235
  endResponse(serverResponse);
@@ -320,15 +290,15 @@ export function sendNodeResponse(fetchResponse, serverResponse, nodeRequest) {
320
290
  return;
321
291
  }
322
292
  if (isReadableStream(fetchBody)) {
323
- return sendReadableStream(serverResponse, fetchBody);
293
+ return sendReadableStream(nodeRequest, serverResponse, fetchBody);
324
294
  }
325
295
  if (isAsyncIterable(fetchBody)) {
326
296
  return sendAsyncIterable(serverResponse, fetchBody);
327
297
  }
328
298
  }
329
- async function sendReadableStream(serverResponse, readableStream) {
299
+ async function sendReadableStream(nodeRequest, serverResponse, readableStream) {
330
300
  const reader = readableStream.getReader();
331
- serverResponse.req.once('error', err => {
301
+ nodeRequest?.once?.('error', err => {
332
302
  reader.cancel(err);
333
303
  });
334
304
  while (true) {
@@ -451,17 +421,24 @@ export function createDeferredPromise() {
451
421
  };
452
422
  }
453
423
  export function handleAbortSignalAndPromiseResponse(response$, abortSignal) {
424
+ if (abortSignal?.aborted) {
425
+ throw abortSignal.reason;
426
+ }
454
427
  if (isPromise(response$) && abortSignal) {
455
428
  const deferred$ = createDeferredPromise();
456
- abortSignal.addEventListener('abort', function abortSignalFetchErrorHandler() {
429
+ function abortSignalFetchErrorHandler() {
457
430
  deferred$.reject(abortSignal.reason);
458
- });
431
+ }
432
+ abortSignal.addEventListener('abort', abortSignalFetchErrorHandler, { once: true });
459
433
  response$
460
434
  .then(function fetchSuccessHandler(res) {
461
435
  deferred$.resolve(res);
462
436
  })
463
437
  .catch(function fetchErrorHandler(err) {
464
438
  deferred$.reject(err);
439
+ })
440
+ .finally(() => {
441
+ abortSignal.removeEventListener('abort', abortSignalFetchErrorHandler);
465
442
  });
466
443
  return deferred$.promise;
467
444
  }
@@ -524,7 +501,7 @@ export function handleResponseDecompression(response, fetchAPI) {
524
501
  }
525
502
  return decompressedResponse;
526
503
  }
527
- const terminateEvents = ['SIGINT', 'SIGTERM', 'exit'];
504
+ const terminateEvents = ['SIGINT', 'exit', 'SIGTERM'];
528
505
  const disposableStacks = new Set();
529
506
  let eventListenerRegistered = false;
530
507
  function ensureEventListenerForDisposableStacks() {
@@ -534,10 +511,7 @@ function ensureEventListenerForDisposableStacks() {
534
511
  eventListenerRegistered = true;
535
512
  for (const event of terminateEvents) {
536
513
  globalThis.process.once(event, function terminateHandler() {
537
- return Promise.allSettled([...disposableStacks].map(stack => !stack.disposed &&
538
- stack.disposeAsync().catch(e => {
539
- console.error('Error while disposing:', e);
540
- })));
514
+ return Promise.allSettled([...disposableStacks].map(stack => !stack.disposed && stack.disposeAsync()));
541
515
  });
542
516
  }
543
517
  }
@@ -2,7 +2,7 @@ import { isPromise } from './utils.js';
2
2
  export function isUWSResponse(res) {
3
3
  return !!res.onData;
4
4
  }
5
- export function getRequestFromUWSRequest({ req, res, fetchAPI, signal }) {
5
+ export function getRequestFromUWSRequest({ req, res, fetchAPI, controller, }) {
6
6
  const method = req.getMethod();
7
7
  let duplex;
8
8
  const chunks = [];
@@ -36,9 +36,9 @@ export function getRequestFromUWSRequest({ req, res, fetchAPI, signal }) {
36
36
  let getReadableStream;
37
37
  if (method !== 'get' && method !== 'head') {
38
38
  duplex = 'half';
39
- signal.addEventListener('abort', () => {
39
+ controller.signal.addEventListener('abort', () => {
40
40
  stop();
41
- });
41
+ }, { once: true });
42
42
  let readableStream;
43
43
  getReadableStream = () => {
44
44
  if (!readableStream) {
@@ -90,7 +90,7 @@ export function getRequestFromUWSRequest({ req, res, fetchAPI, signal }) {
90
90
  get body() {
91
91
  return getBody();
92
92
  },
93
- signal,
93
+ signal: controller.signal,
94
94
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
95
95
  // @ts-ignore - not in the TS types yet
96
96
  duplex,
@@ -164,14 +164,14 @@ export function createWritableFromUWS(uwsResponse, fetchAPI) {
164
164
  },
165
165
  });
166
166
  }
167
- export function sendResponseToUwsOpts(uwsResponse, fetchResponse, signal, fetchAPI) {
167
+ export function sendResponseToUwsOpts(uwsResponse, fetchResponse, controller, fetchAPI) {
168
168
  if (!fetchResponse) {
169
169
  uwsResponse.writeStatus('404 Not Found');
170
170
  uwsResponse.end();
171
171
  return;
172
172
  }
173
173
  const bufferOfRes = fetchResponse._buffer;
174
- if (signal.aborted) {
174
+ if (controller.signal.aborted) {
175
175
  return;
176
176
  }
177
177
  uwsResponse.cork(() => {
@@ -201,17 +201,17 @@ export function sendResponseToUwsOpts(uwsResponse, fetchResponse, signal, fetchA
201
201
  if (bufferOfRes || !fetchResponse.body) {
202
202
  return;
203
203
  }
204
- signal.addEventListener('abort', () => {
204
+ controller.signal.addEventListener('abort', () => {
205
205
  if (!fetchResponse.body?.locked) {
206
- fetchResponse.body?.cancel(signal.reason);
206
+ fetchResponse.body?.cancel(controller.signal.reason);
207
207
  }
208
- });
208
+ }, { once: true });
209
209
  return fetchResponse.body
210
210
  .pipeTo(createWritableFromUWS(uwsResponse, fetchAPI), {
211
- signal,
211
+ signal: controller.signal,
212
212
  })
213
213
  .catch(err => {
214
- if (signal.aborted) {
214
+ if (controller.signal.aborted) {
215
215
  return;
216
216
  }
217
217
  throw err;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/server",
3
- "version": "0.10.0-alpha-20241125144944-df106a17746126e3c8089afa16af852a4483c8b3",
3
+ "version": "0.10.0-alpha-20250205010145-5ab5edf75d82f8174c999dc970ad9a042ea5547d",
4
4
  "description": "Fetch API compliant HTTP Server adapter",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -3,6 +3,14 @@ import { FetchAPI, ServerAdapter, ServerAdapterBaseObject, ServerAdapterRequestH
3
3
  export interface ServerAdapterOptions<TServerContext> {
4
4
  plugins?: ServerAdapterPlugin<TServerContext>[];
5
5
  fetchAPI?: Partial<FetchAPI>;
6
+ /**
7
+ * Node.js only!
8
+ *
9
+ * If true, the server adapter will dispose itself when the process is terminated.
10
+ * If false, you have to dispose the server adapter by using the `dispose` method,
11
+ * or [Explicit Resource Management](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html)
12
+ */
13
+ disposeOnProcessTerminate?: boolean;
6
14
  }
7
15
  declare function createServerAdapter<TServerContext = {}, THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>>(serverAdapterRequestHandler: THandleRequest, options?: ServerAdapterOptions<TServerContext>): ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, THandleRequest>>;
8
16
  declare function createServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>>(serverAdapterBaseObject: TBaseObject, options?: ServerAdapterOptions<TServerContext>): ServerAdapter<TServerContext, TBaseObject>;
@@ -3,6 +3,14 @@ import { FetchAPI, ServerAdapter, ServerAdapterBaseObject, ServerAdapterRequestH
3
3
  export interface ServerAdapterOptions<TServerContext> {
4
4
  plugins?: ServerAdapterPlugin<TServerContext>[];
5
5
  fetchAPI?: Partial<FetchAPI>;
6
+ /**
7
+ * Node.js only!
8
+ *
9
+ * If true, the server adapter will dispose itself when the process is terminated.
10
+ * If false, you have to dispose the server adapter by using the `dispose` method,
11
+ * or [Explicit Resource Management](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html)
12
+ */
13
+ disposeOnProcessTerminate?: boolean;
6
14
  }
7
15
  declare function createServerAdapter<TServerContext = {}, THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>>(serverAdapterRequestHandler: THandleRequest, options?: ServerAdapterOptions<TServerContext>): ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, THandleRequest>>;
8
16
  declare function createServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>>(serverAdapterBaseObject: TBaseObject, options?: ServerAdapterOptions<TServerContext>): ServerAdapter<TServerContext, TBaseObject>;
@@ -7,3 +7,4 @@ export * from './plugins/useErrorHandling.cjs';
7
7
  export * from './plugins/useContentEncoding.cjs';
8
8
  export * from './uwebsockets.cjs';
9
9
  export { Response } from '@whatwg-node/fetch';
10
+ export { DisposableSymbols } from '@whatwg-node/disposablestack';
@@ -7,3 +7,4 @@ export * from './plugins/useErrorHandling.js';
7
7
  export * from './plugins/useContentEncoding.js';
8
8
  export * from './uwebsockets.js';
9
9
  export { Response } from '@whatwg-node/fetch';
10
+ export { DisposableSymbols } from '@whatwg-node/disposablestack';
@@ -1,10 +1,43 @@
1
1
  import { FetchAPI, ServerAdapterRequestHandler, type ServerAdapterInitialContext } from '../types.cjs';
2
- export type ServerAdapterPlugin<TServerContext = {}> = {
2
+ export interface ServerAdapterPlugin<TServerContext = {}> {
3
+ /**
4
+ * This hook is invoked for ANY incoming HTTP request. Here you can manipulate the request,
5
+ * create a short circuit before the request handler takes it over.
6
+ *
7
+ * Warning: Exceptions thrown by this hook are not caught.
8
+ * This means they will buble up to the HTTP server underlying implementation.
9
+ * For example, the `node:http` server crashes the entire process on uncaught exceptions.
10
+ */
3
11
  onRequest?: OnRequestHook<TServerContext & ServerAdapterInitialContext>;
12
+ /**
13
+ * This hook is invoked after a HTTP request (both GraphQL and NON GraphQL) has been processed
14
+ * and after the response has been forwarded to the client. Here you can perform any cleanup
15
+ * or logging operations, or you can manipulate the outgoing response object.
16
+ *
17
+ * Warning: Exceptions thrown by this hook are not caught.
18
+ * This means they will buble up to the HTTP server underlying implementation.
19
+ * For example, the `node:http` server crashes the entire process on uncaught exceptions.
20
+ */
4
21
  onResponse?: OnResponseHook<TServerContext & ServerAdapterInitialContext>;
22
+ /**
23
+ * This hook is invoked when the server is being disposed.
24
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
25
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
26
+ */
5
27
  [Symbol.dispose]?: () => void;
28
+ /**
29
+ * This hook is invoked when the server is being disposed.
30
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
31
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
32
+ */
6
33
  [Symbol.asyncDispose]?: () => PromiseLike<void> | void;
7
- } | undefined;
34
+ /**
35
+ * This hook is invoked when the server is being disposed.
36
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
37
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
38
+ */
39
+ onDispose?: () => PromiseLike<void> | void;
40
+ }
8
41
  export type OnRequestHook<TServerContext> = (payload: OnRequestEventPayload<TServerContext>) => Promise<void> | void;
9
42
  export interface OnRequestEventPayload<TServerContext> {
10
43
  request: Request;
@@ -1,10 +1,43 @@
1
1
  import { FetchAPI, ServerAdapterRequestHandler, type ServerAdapterInitialContext } from '../types.js';
2
- export type ServerAdapterPlugin<TServerContext = {}> = {
2
+ export interface ServerAdapterPlugin<TServerContext = {}> {
3
+ /**
4
+ * This hook is invoked for ANY incoming HTTP request. Here you can manipulate the request,
5
+ * create a short circuit before the request handler takes it over.
6
+ *
7
+ * Warning: Exceptions thrown by this hook are not caught.
8
+ * This means they will buble up to the HTTP server underlying implementation.
9
+ * For example, the `node:http` server crashes the entire process on uncaught exceptions.
10
+ */
3
11
  onRequest?: OnRequestHook<TServerContext & ServerAdapterInitialContext>;
12
+ /**
13
+ * This hook is invoked after a HTTP request (both GraphQL and NON GraphQL) has been processed
14
+ * and after the response has been forwarded to the client. Here you can perform any cleanup
15
+ * or logging operations, or you can manipulate the outgoing response object.
16
+ *
17
+ * Warning: Exceptions thrown by this hook are not caught.
18
+ * This means they will buble up to the HTTP server underlying implementation.
19
+ * For example, the `node:http` server crashes the entire process on uncaught exceptions.
20
+ */
4
21
  onResponse?: OnResponseHook<TServerContext & ServerAdapterInitialContext>;
22
+ /**
23
+ * This hook is invoked when the server is being disposed.
24
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
25
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
26
+ */
5
27
  [Symbol.dispose]?: () => void;
28
+ /**
29
+ * This hook is invoked when the server is being disposed.
30
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
31
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
32
+ */
6
33
  [Symbol.asyncDispose]?: () => PromiseLike<void> | void;
7
- } | undefined;
34
+ /**
35
+ * This hook is invoked when the server is being disposed.
36
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
37
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
38
+ */
39
+ onDispose?: () => PromiseLike<void> | void;
40
+ }
8
41
  export type OnRequestHook<TServerContext> = (payload: OnRequestEventPayload<TServerContext>) => Promise<void> | void;
9
42
  export interface OnRequestEventPayload<TServerContext> {
10
43
  request: Request;
@@ -4,9 +4,9 @@ export declare class HTTPError extends Error {
4
4
  status: number;
5
5
  message: string;
6
6
  headers: HeadersInit;
7
- details?: any;
7
+ details?: any | undefined;
8
8
  name: string;
9
- constructor(status: number | undefined, message: string, headers?: HeadersInit, details?: any);
9
+ constructor(status: number | undefined, message: string, headers?: HeadersInit, details?: any | undefined);
10
10
  }
11
- export type ErrorHandler<TServerContext> = (e: any, request: Request, ctx: TServerContext) => Response | Promise<Response>;
11
+ export type ErrorHandler<TServerContext> = (e: any, request: Request, ctx: TServerContext) => Response | Promise<Response> | void;
12
12
  export declare function useErrorHandling<TServerContext>(onError?: ErrorHandler<TServerContext>): ServerAdapterPlugin<TServerContext>;
@@ -4,9 +4,9 @@ export declare class HTTPError extends Error {
4
4
  status: number;
5
5
  message: string;
6
6
  headers: HeadersInit;
7
- details?: any;
7
+ details?: any | undefined;
8
8
  name: string;
9
- constructor(status: number | undefined, message: string, headers?: HeadersInit, details?: any);
9
+ constructor(status: number | undefined, message: string, headers?: HeadersInit, details?: any | undefined);
10
10
  }
11
- export type ErrorHandler<TServerContext> = (e: any, request: Request, ctx: TServerContext) => Response | Promise<Response>;
11
+ export type ErrorHandler<TServerContext> = (e: any, request: Request, ctx: TServerContext) => Response | Promise<Response> | void;
12
12
  export declare function useErrorHandling<TServerContext>(onError?: ErrorHandler<TServerContext>): ServerAdapterPlugin<TServerContext>;
@@ -1,4 +1,4 @@
1
- import type { RequestListener } from 'http';
1
+ import type { RequestListener } from 'node:http';
2
2
  import type { NodeRequest, NodeResponse } from './utils.cjs';
3
3
  import { UWSHandler, UWSRequest, UWSResponse } from './uwebsockets.cjs';
4
4
  export interface FetchEvent extends Event {
@@ -1,4 +1,4 @@
1
- import type { RequestListener } from 'http';
1
+ import type { RequestListener } from 'node:http';
2
2
  import type { NodeRequest, NodeResponse } from './utils.js';
3
3
  import { UWSHandler, UWSRequest, UWSResponse } from './uwebsockets.js';
4
4
  export interface FetchEvent extends Event {
@@ -1,37 +1,27 @@
1
- import type { IncomingMessage, ServerResponse } from 'http';
2
- import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
3
- import type { Socket } from 'net';
4
- import type { Readable } from 'stream';
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
3
+ import type { Socket } from 'node:net';
4
+ import type { Readable } from 'node:stream';
5
5
  import type { FetchAPI, FetchEvent } from './types.cjs';
6
6
  export declare function isAsyncIterable(body: any): body is AsyncIterable<any>;
7
7
  export interface NodeRequest {
8
- protocol?: string;
9
- hostname?: string;
10
- body?: any;
11
- url?: string;
12
- originalUrl?: string;
13
- method?: string;
14
- headers?: any;
15
- req?: IncomingMessage | Http2ServerRequest;
16
- raw?: IncomingMessage | Http2ServerRequest;
17
- socket?: Socket;
18
- query?: any;
8
+ protocol?: string | undefined;
9
+ hostname?: string | undefined;
10
+ body?: any | undefined;
11
+ url?: string | undefined;
12
+ originalUrl?: string | undefined;
13
+ method?: string | undefined;
14
+ headers?: any | undefined;
15
+ req?: IncomingMessage | Http2ServerRequest | undefined;
16
+ raw?: IncomingMessage | Http2ServerRequest | undefined;
17
+ socket?: Socket | undefined;
18
+ query?: any | undefined;
19
19
  once?(event: string, listener: (...args: any[]) => void): void;
20
- aborted?: boolean;
20
+ aborted?: boolean | undefined;
21
21
  }
22
22
  export type NodeResponse = ServerResponse | Http2ServerResponse;
23
- export declare class ServerAdapterRequestAbortSignal extends EventTarget implements AbortSignal {
24
- aborted: boolean;
25
- private _onabort;
26
- reason: any;
27
- throwIfAborted(): void;
28
- sendAbort(): void;
29
- get onabort(): ((this: AbortSignal, ev: Event) => any) | null;
30
- set onabort(value: ((this: AbortSignal, ev: Event) => any) | null);
31
- any(signals: Iterable<AbortSignal>): AbortSignal;
32
- }
33
23
  export declare const nodeRequestResponseMap: WeakMap<NodeRequest, NodeResponse>;
34
- export declare function normalizeNodeRequest(nodeRequest: NodeRequest, fetchAPI: FetchAPI, registerSignal?: (signal: ServerAdapterRequestAbortSignal) => void): Request;
24
+ export declare function normalizeNodeRequest(nodeRequest: NodeRequest, fetchAPI: FetchAPI): Request;
35
25
  export declare function isReadable(stream: any): stream is Readable;
36
26
  export declare function isNodeRequest(request: any): request is NodeRequest;
37
27
  export declare function isServerResponse(stream: any): stream is NodeResponse;
@@ -50,7 +40,7 @@ export interface DeferredPromise<T = void> {
50
40
  reject: (reason: any) => void;
51
41
  }
52
42
  export declare function createDeferredPromise<T = void>(): DeferredPromise<T>;
53
- export declare function handleAbortSignalAndPromiseResponse(response$: Promise<Response> | Response, abortSignal?: AbortSignal | null): Response | Promise<Response>;
43
+ export declare function handleAbortSignalAndPromiseResponse(response$: Promise<Response> | Response, abortSignal: AbortSignal): Response | Promise<Response>;
54
44
  export declare const decompressedResponseMap: WeakMap<Response, Response>;
55
45
  export declare function getSupportedEncodings(fetchAPI: FetchAPI): CompressionFormat[];
56
46
  export declare function handleResponseDecompression(response: Response, fetchAPI: FetchAPI): Response;
@@ -1,37 +1,27 @@
1
- import type { IncomingMessage, ServerResponse } from 'http';
2
- import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
3
- import type { Socket } from 'net';
4
- import type { Readable } from 'stream';
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
3
+ import type { Socket } from 'node:net';
4
+ import type { Readable } from 'node:stream';
5
5
  import type { FetchAPI, FetchEvent } from './types.js';
6
6
  export declare function isAsyncIterable(body: any): body is AsyncIterable<any>;
7
7
  export interface NodeRequest {
8
- protocol?: string;
9
- hostname?: string;
10
- body?: any;
11
- url?: string;
12
- originalUrl?: string;
13
- method?: string;
14
- headers?: any;
15
- req?: IncomingMessage | Http2ServerRequest;
16
- raw?: IncomingMessage | Http2ServerRequest;
17
- socket?: Socket;
18
- query?: any;
8
+ protocol?: string | undefined;
9
+ hostname?: string | undefined;
10
+ body?: any | undefined;
11
+ url?: string | undefined;
12
+ originalUrl?: string | undefined;
13
+ method?: string | undefined;
14
+ headers?: any | undefined;
15
+ req?: IncomingMessage | Http2ServerRequest | undefined;
16
+ raw?: IncomingMessage | Http2ServerRequest | undefined;
17
+ socket?: Socket | undefined;
18
+ query?: any | undefined;
19
19
  once?(event: string, listener: (...args: any[]) => void): void;
20
- aborted?: boolean;
20
+ aborted?: boolean | undefined;
21
21
  }
22
22
  export type NodeResponse = ServerResponse | Http2ServerResponse;
23
- export declare class ServerAdapterRequestAbortSignal extends EventTarget implements AbortSignal {
24
- aborted: boolean;
25
- private _onabort;
26
- reason: any;
27
- throwIfAborted(): void;
28
- sendAbort(): void;
29
- get onabort(): ((this: AbortSignal, ev: Event) => any) | null;
30
- set onabort(value: ((this: AbortSignal, ev: Event) => any) | null);
31
- any(signals: Iterable<AbortSignal>): AbortSignal;
32
- }
33
23
  export declare const nodeRequestResponseMap: WeakMap<NodeRequest, NodeResponse>;
34
- export declare function normalizeNodeRequest(nodeRequest: NodeRequest, fetchAPI: FetchAPI, registerSignal?: (signal: ServerAdapterRequestAbortSignal) => void): Request;
24
+ export declare function normalizeNodeRequest(nodeRequest: NodeRequest, fetchAPI: FetchAPI): Request;
35
25
  export declare function isReadable(stream: any): stream is Readable;
36
26
  export declare function isNodeRequest(request: any): request is NodeRequest;
37
27
  export declare function isServerResponse(stream: any): stream is NodeResponse;
@@ -50,7 +40,7 @@ export interface DeferredPromise<T = void> {
50
40
  reject: (reason: any) => void;
51
41
  }
52
42
  export declare function createDeferredPromise<T = void>(): DeferredPromise<T>;
53
- export declare function handleAbortSignalAndPromiseResponse(response$: Promise<Response> | Response, abortSignal?: AbortSignal | null): Response | Promise<Response>;
43
+ export declare function handleAbortSignalAndPromiseResponse(response$: Promise<Response> | Response, abortSignal: AbortSignal): Response | Promise<Response>;
54
44
  export declare const decompressedResponseMap: WeakMap<Response, Response>;
55
45
  export declare function getSupportedEncodings(fetchAPI: FetchAPI): CompressionFormat[];
56
46
  export declare function handleResponseDecompression(response: Response, fetchAPI: FetchAPI): Response;
@@ -1,5 +1,4 @@
1
1
  import type { FetchAPI } from './types.cjs';
2
- import { ServerAdapterRequestAbortSignal } from './utils.cjs';
3
2
  export interface UWSRequest {
4
3
  getMethod(): string;
5
4
  forEach(callback: (key: string, value: string) => void): void;
@@ -24,10 +23,10 @@ interface GetRequestFromUWSOpts {
24
23
  req: UWSRequest;
25
24
  res: UWSResponse;
26
25
  fetchAPI: FetchAPI;
27
- signal: AbortSignal;
26
+ controller: AbortController;
28
27
  }
29
- export declare function getRequestFromUWSRequest({ req, res, fetchAPI, signal }: GetRequestFromUWSOpts): Request;
28
+ export declare function getRequestFromUWSRequest({ req, res, fetchAPI, controller, }: GetRequestFromUWSOpts): Request;
30
29
  export declare function createWritableFromUWS(uwsResponse: UWSResponse, fetchAPI: FetchAPI): WritableStream<any>;
31
- export declare function sendResponseToUwsOpts(uwsResponse: UWSResponse, fetchResponse: Response, signal: ServerAdapterRequestAbortSignal, fetchAPI: FetchAPI): Promise<void> | undefined;
30
+ export declare function sendResponseToUwsOpts(uwsResponse: UWSResponse, fetchResponse: Response, controller: AbortController, fetchAPI: FetchAPI): Promise<void> | undefined;
32
31
  export declare function fakePromise<T>(value: T): Promise<T>;
33
32
  export {};
@@ -1,5 +1,4 @@
1
1
  import type { FetchAPI } from './types.js';
2
- import { ServerAdapterRequestAbortSignal } from './utils.js';
3
2
  export interface UWSRequest {
4
3
  getMethod(): string;
5
4
  forEach(callback: (key: string, value: string) => void): void;
@@ -24,10 +23,10 @@ interface GetRequestFromUWSOpts {
24
23
  req: UWSRequest;
25
24
  res: UWSResponse;
26
25
  fetchAPI: FetchAPI;
27
- signal: AbortSignal;
26
+ controller: AbortController;
28
27
  }
29
- export declare function getRequestFromUWSRequest({ req, res, fetchAPI, signal }: GetRequestFromUWSOpts): Request;
28
+ export declare function getRequestFromUWSRequest({ req, res, fetchAPI, controller, }: GetRequestFromUWSOpts): Request;
30
29
  export declare function createWritableFromUWS(uwsResponse: UWSResponse, fetchAPI: FetchAPI): WritableStream<any>;
31
- export declare function sendResponseToUwsOpts(uwsResponse: UWSResponse, fetchResponse: Response, signal: ServerAdapterRequestAbortSignal, fetchAPI: FetchAPI): Promise<void> | undefined;
30
+ export declare function sendResponseToUwsOpts(uwsResponse: UWSResponse, fetchResponse: Response, controller: AbortController, fetchAPI: FetchAPI): Promise<void> | undefined;
32
31
  export declare function fakePromise<T>(value: T): Promise<T>;
33
32
  export {};