@whatwg-node/server
Advanced tools
Comparing version 0.4.10-alpha-20220927131110-4cfdcdb to 0.4.10-alpha-20220927134607-bd5c990
@@ -5,3 +5,3 @@ /// <reference types="node" /> | ||
import { NodeRequest } from './utils'; | ||
export interface ServerAdapterBaseObject<TServerContext> { | ||
export interface ServerAdapterBaseObject<TServerContext, THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>> { | ||
/** | ||
@@ -11,9 +11,9 @@ * An async function that takes `Request` and the server context and returns a `Response`. | ||
*/ | ||
handle: ServerAdapterRequestHandler<TServerContext>; | ||
handle: THandleRequest; | ||
} | ||
export interface ServerAdapterObject<TServerContext> extends EventListenerObject { | ||
export interface ServerAdapterObject<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext, ServerAdapterRequestHandler<TServerContext>>> extends EventListenerObject { | ||
/** | ||
* A basic request listener that takes a `Request` with the server context and returns a `Response`. | ||
*/ | ||
handleRequest: ServerAdapterRequestHandler<TServerContext>; | ||
handleRequest: TBaseObject['handle']; | ||
/** | ||
@@ -40,5 +40,2 @@ * WHATWG Fetch spec compliant `fetch` function that can be used for testing purposes. | ||
requestListener: RequestListener; | ||
/** | ||
* Flexible and generic request handler for all environments. | ||
*/ | ||
handle(req: NodeRequest, res: ServerResponse, ...ctx: Partial<TServerContext>[]): Promise<void>; | ||
@@ -51,11 +48,11 @@ handle(request: Request, ...ctx: Partial<TServerContext>[]): Promise<Response> | Response; | ||
} | ||
export declare type ServerAdapter<TServerContext> = ServerAdapterObject<TServerContext>['handle'] & ServerAdapterObject<TServerContext>; | ||
export declare type ServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>> = TBaseObject & ServerAdapterObject<TServerContext, TBaseObject>['handle'] & ServerAdapterObject<TServerContext, TBaseObject>; | ||
export declare type ServerAdapterRequestHandler<TServerContext> = (request: Request, ctx: TServerContext) => Promise<Response> | Response; | ||
export interface NodeServerAdapterContext { | ||
export declare type DefaultServerAdapterContext = { | ||
req: NodeRequest; | ||
res: ServerResponse; | ||
waitUntil(promise: Promise<unknown>): void; | ||
} | ||
declare function createServerAdapter<TServerContext>(serverAdapterRequestHandler: ServerAdapterRequestHandler<TServerContext>, RequestCtor?: typeof Request): ServerAdapter<TServerContext>; | ||
declare function createServerAdapter<TServerContext>(serverAdapterBaseObject: ServerAdapterBaseObject<TServerContext>, RequestCtor?: typeof Request): ServerAdapter<TServerContext>; | ||
}; | ||
declare function createServerAdapter<TServerContext = DefaultServerAdapterContext, THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>>(serverAdapterRequestHandler: THandleRequest, RequestCtor?: typeof Request): ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, THandleRequest>>; | ||
declare function createServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>>(serverAdapterBaseObject: TBaseObject, RequestCtor?: typeof Request): ServerAdapter<TServerContext, TBaseObject>; | ||
export { createServerAdapter }; |
33
index.js
@@ -129,5 +129,2 @@ 'use strict'; | ||
} | ||
function isReadableStream(stream) { | ||
return stream != null && stream.getReader != null; | ||
} | ||
function isFetchEvent(event) { | ||
@@ -157,28 +154,2 @@ return event != null && event.request != null && event.respondWith != null; | ||
} | ||
else if (isReadableStream(body)) { | ||
const reader = body.getReader(); | ||
serverResponse.once('close', () => { | ||
reader.cancel().finally(() => { | ||
reader.releaseLock(); | ||
body.cancel(); | ||
}); | ||
}); | ||
// eslint-disable-next-line no-inner-declarations | ||
function pump() { | ||
reader | ||
.read() | ||
.then(({ done, value }) => { | ||
if (done) { | ||
serverResponse.end(resolve); | ||
return; | ||
} | ||
serverResponse.write(value, pump); | ||
}) | ||
.catch(error => { | ||
console.error(error); | ||
serverResponse.end(resolve); | ||
}); | ||
} | ||
pump(); | ||
} | ||
else if (isAsyncIterable(body)) { | ||
@@ -235,3 +206,3 @@ for await (const chunk of body) { | ||
const waitUntilPromises = []; | ||
const nodeServerContext = { | ||
const defaultServerContext = { | ||
req: nodeRequest, | ||
@@ -243,3 +214,3 @@ res: serverResponse, | ||
}; | ||
const response = await handleNodeRequest(nodeRequest, nodeServerContext, ...ctx); | ||
const response = await handleNodeRequest(nodeRequest, defaultServerContext, ...ctx); | ||
if (response) { | ||
@@ -246,0 +217,0 @@ await sendNodeResponse(response, serverResponse); |
{ | ||
"name": "@whatwg-node/server", | ||
"version": "0.4.10-alpha-20220927131110-4cfdcdb", | ||
"version": "0.4.10-alpha-20220927134607-bd5c990", | ||
"description": "Fetch API compliant HTTP Server adapter", | ||
@@ -10,3 +10,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@whatwg-node/fetch": "0.4.6-alpha-20220927131110-4cfdcdb", | ||
"@whatwg-node/fetch": "0.4.6-alpha-20220927134607-bd5c990", | ||
"tslib": "^2.3.1" | ||
@@ -13,0 +13,0 @@ }, |
Sorry, the diff of this file is not supported yet
42154
742
+ Added@whatwg-node/fetch@0.4.6-alpha-20220927134607-bd5c990(transitive)
- Removed@whatwg-node/fetch@0.4.6-alpha-20220927131110-4cfdcdb(transitive)
Updated@whatwg-node/fetch@0.4.6-alpha-20220927134607-bd5c990