Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@whatwg-node/server

Package Overview
Dependencies
Maintainers
1
Versions
735
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@whatwg-node/server - npm Package Compare versions

Comparing version 0.4.5 to 0.4.6-alpha-20220921172951-5c43d9d

23

index.d.ts

@@ -20,11 +20,16 @@ /// <reference types="node" />

*/
fetch(request: Request, ...ctx: any[]): Promise<Response> | Response;
fetch(urlStr: string, ...ctx: any[]): Promise<Response> | Response;
fetch(urlStr: string, init: RequestInit, ...ctx: any[]): Promise<Response> | Response;
fetch(url: URL, ...ctx: any[]): Promise<Response> | Response;
fetch(url: URL, init: RequestInit, ...ctx: any[]): Promise<Response> | Response;
fetch(request: Request, ctx: TServerContext): Promise<Response> | Response;
fetch(request: Request, ...ctx: Partial<TServerContext>[]): Promise<Response> | Response;
fetch(urlStr: string, ctx: TServerContext): Promise<Response> | Response;
fetch(urlStr: string, ...ctx: Partial<TServerContext>[]): Promise<Response> | Response;
fetch(urlStr: string, init: RequestInit, ctx: TServerContext): Promise<Response> | Response;
fetch(urlStr: string, init: RequestInit, ...ctx: Partial<TServerContext>[]): Promise<Response> | Response;
fetch(url: URL, ctx: TServerContext): Promise<Response> | Response;
fetch(url: URL, ...ctx: Partial<TServerContext>[]): Promise<Response> | Response;
fetch(url: URL, init: RequestInit, ctx: TServerContext): Promise<Response> | Response;
fetch(url: URL, init: RequestInit, ...ctx: Partial<TServerContext>[]): Promise<Response> | Response;
/**
* This function takes Node's request object and returns a WHATWG Fetch spec compliant `Response` object.
**/
handleNodeRequest(nodeRequest: NodeRequest, serverContext: TServerContext): Promise<Response> | Response;
handleNodeRequest(nodeRequest: NodeRequest, ctx: TServerContext): Promise<Response> | Response;
/**

@@ -37,6 +42,6 @@ * A request listener function that can be used with any Node server variation.

*/
handle: RequestListener & ServerAdapterObject<TServerContext, TBaseObject>['fetch'];
handle: ServerAdapterObject<TServerContext, TBaseObject>['requestListener'] & ServerAdapterObject<TServerContext, TBaseObject>['fetch'];
}
export declare type ServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>> = TBaseObject & RequestListener & ServerAdapterObject<TServerContext, TBaseObject>['fetch'] & ServerAdapterObject<TServerContext, TBaseObject>;
export declare type ServerAdapterRequestHandler<TServerContext> = (request: Request, serverContext: TServerContext) => Promise<Response> | Response;
export declare type ServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>> = TBaseObject & ServerAdapterObject<TServerContext, TBaseObject>['requestListener'] & ServerAdapterObject<TServerContext, TBaseObject>['fetch'] & ServerAdapterObject<TServerContext, TBaseObject>;
export declare type ServerAdapterRequestHandler<TServerContext> = (request: Request, ctx: TServerContext) => Promise<Response> | Response;
export declare type DefaultServerAdapterContext = {

@@ -43,0 +48,0 @@ req: NodeRequest;

@@ -157,2 +157,19 @@ 'use strict';

}
function isRequestInit(val) {
return (val != null &&
typeof val === 'object' &&
('body' in val ||
'cache' in val ||
'credentials' in val ||
'headers' in val ||
'integrity' in val ||
'keepalive' in val ||
'method' in val ||
'mode' in val ||
'redirect' in val ||
'referrer' in val ||
'referrerPolicy' in val ||
'signal' in val ||
'window' in val));
}

@@ -174,5 +191,5 @@ /// <reference lib="webworker" />

const handleRequest = typeof serverAdapterBaseObject === 'function' ? serverAdapterBaseObject : serverAdapterBaseObject.handle;
function handleNodeRequest(nodeRequest, serverContext) {
function handleNodeRequest(nodeRequest, ctx) {
const request = normalizeNodeRequest(nodeRequest, RequestCtor);
return handleRequest(request, serverContext);
return handleRequest(request, ctx);
}

@@ -200,12 +217,19 @@ async function requestListener(nodeRequest, serverResponse) {

}
return response;
}
function handleEvent(event) {
function handleEvent(event, ...ctx) {
if (!event.respondWith || !event.request) {
throw new TypeError(`Expected FetchEvent, got ${event}`);
}
const response$ = handleRequest(event.request, event);
let serverContext = {};
if ((ctx === null || ctx === void 0 ? void 0 : ctx.length) > 0) {
serverContext = Object.assign({}, serverContext, ...ctx);
}
const response$ = handleRequest(event.request, serverContext);
event.respondWith(response$);
return response$;
}
function handleRequestWithWaitUntil(request, ctx = {}, ...rest) {
function handleRequestWithWaitUntil(request, ctx) {
var _a;
const extendedCtx = ctx;
if ('process' in globalThis && ((_a = process.versions) === null || _a === void 0 ? void 0 : _a['bun']) != null) {

@@ -215,12 +239,9 @@ // This is required for bun

}
if ((rest === null || rest === void 0 ? void 0 : rest.length) > 0) {
ctx = Object.assign({}, ctx, ...rest);
}
if (!ctx.waitUntil) {
if (!extendedCtx.waitUntil) {
const waitUntilPromises = [];
ctx.waitUntil = (p) => {
extendedCtx.waitUntil = (p) => {
waitUntilPromises.push(p);
};
const response$ = handleRequest(request, {
...ctx,
...extendedCtx,
waitUntil(p) {

@@ -235,28 +256,47 @@ waitUntilPromises.push(p);

}
return handleRequest(request, ctx);
return handleRequest(request, extendedCtx);
}
function genericRequestHandler(input, ctx, ...rest) {
const fetchFn = (input, initOrCtx, ...ctx) => {
let init;
let serverContext = {};
if (isRequestInit(initOrCtx)) {
init = initOrCtx;
}
else {
init = {};
serverContext = Object.assign({}, serverContext, initOrCtx);
}
if ((ctx === null || ctx === void 0 ? void 0 : ctx.length) > 0) {
serverContext = Object.assign({}, serverContext, ...ctx);
}
if (typeof input === 'string' || input instanceof URL) {
return handleRequestWithWaitUntil(new RequestCtor(input, init), serverContext);
}
return handleRequestWithWaitUntil(input, serverContext);
};
const genericRequestHandler = (input, initOrCtxOrRes, ...ctx) => {
// If it is a Node request
if (isReadable(input) && ctx != null && isServerResponse(ctx)) {
return requestListener(input, ctx);
if (isReadable(input) && isServerResponse(initOrCtxOrRes)) {
return requestListener(input, initOrCtxOrRes);
}
if (isServerResponse(initOrCtxOrRes)) {
throw new Error('Got Node response without Node request');
}
// Is input a container object over Request?
if (input.request) {
if (typeof input === 'object' && 'request' in input) {
// Is it FetchEvent?
if (input.respondWith) {
return handleEvent(input);
if ('respondWith' in input) {
return handleEvent(input, isRequestInit(initOrCtxOrRes) ? {} : initOrCtxOrRes, ...ctx);
}
// In this input is also the context
return handleRequestWithWaitUntil(input.request, input, ...rest);
return fetchFn(
// @ts-expect-error input can indeed be a Request
input.request, initOrCtxOrRes, ...ctx);
}
// Or is it Request itself?
// Then ctx is present and it is the context
return handleRequestWithWaitUntil(input, ctx, ...rest);
}
function fetchFn(input, init, ...ctx) {
if (typeof input === 'string' || input instanceof URL) {
return handleRequestWithWaitUntil(new RequestCtor(input, init), ...ctx);
}
return handleRequestWithWaitUntil(input, init, ...ctx);
}
return fetchFn(
// @ts-expect-error input can indeed string | Request | URL
input, initOrCtxOrRes, ...ctx);
};
const adapterObj = {

@@ -285,8 +325,8 @@ handleRequest,

}
const genericRequestHandlerProp = genericRequestHandler[prop];
if (genericRequestHandlerProp) {
if (genericRequestHandlerProp.bind) {
return genericRequestHandlerProp.bind(genericRequestHandler);
const handleProp = genericRequestHandler[prop];
if (handleProp) {
if (handleProp.bind) {
return handleProp.bind(genericRequestHandler);
}
return genericRequestHandlerProp;
return handleProp;
}

@@ -306,5 +346,5 @@ if (serverAdapterBaseObject) {

},
});
}); // 😡
}
exports.createServerAdapter = createServerAdapter;
{
"name": "@whatwg-node/server",
"version": "0.4.5",
"version": "0.4.6-alpha-20220921172951-5c43d9d",
"description": "Fetch API compliant HTTP Server adapter",

@@ -5,0 +5,0 @@ "sideEffects": false,

@@ -24,1 +24,2 @@ /// <reference types="node" />

export declare function sendNodeResponse({ headers, status, statusText, body }: Response, serverResponse: ServerResponse): Promise<void>;
export declare function isRequestInit(val: unknown): val is RequestInit;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc