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.9.63-alpha-20241212160430-140b66f028923f44368e67598b6b66f47f690454 to 0.9.63-rc-20241213082454-841c7fefb06e73c59003a91397fe3b1830666c7f

16

cjs/createServerAdapter.js

@@ -189,3 +189,3 @@ "use strict";

: defaultServerContext;
const ctrl = new fetchAPI.AbortController();
const signal = new utils_js_1.ServerAdapterRequestAbortSignal();
const originalResEnd = res.end.bind(res);

@@ -199,6 +199,6 @@ let resEnded = false;

originalOnAborted(function () {
ctrl.abort();
signal.sendAbort();
});
res.onAborted = function (cb) {
ctrl.signal.addEventListener('abort', cb, { once: true });
signal.addEventListener('abort', cb);
};

@@ -209,3 +209,3 @@ const request = (0, uwebsockets_js_1.getRequestFromUWSRequest)({

fetchAPI,
ctrl,
signal,
});

@@ -223,4 +223,4 @@ let response$;

.then(response => {
if (!ctrl.signal.aborted && !resEnded) {
return (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response, ctrl, fetchAPI);
if (!signal.aborted && !resEnded) {
return (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response, signal, fetchAPI);
}

@@ -233,4 +233,4 @@ })

try {
if (!ctrl.signal.aborted && !resEnded) {
return (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response$, ctrl, fetchAPI);
if (!signal.aborted && !resEnded) {
return (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response$, signal, fetchAPI);
}

@@ -237,0 +237,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Response = void 0;
exports.DisposableSymbols = exports.Response = void 0;
const tslib_1 = require("tslib");

@@ -15,1 +15,3 @@ tslib_1.__exportStar(require("./createServerAdapter.js"), exports);

Object.defineProperty(exports, "Response", { enumerable: true, get: function () { return fetch_1.Response; } });
var disposablestack_1 = require("@whatwg-node/disposablestack");
Object.defineProperty(exports, "DisposableSymbols", { enumerable: true, get: function () { return disposablestack_1.DisposableSymbols; } });
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.decompressedResponseMap = exports.nodeRequestResponseMap = void 0;
exports.decompressedResponseMap = exports.nodeRequestResponseMap = exports.ServerAdapterRequestAbortSignal = void 0;
exports.isAsyncIterable = isAsyncIterable;

@@ -72,5 +72,36 @@ exports.normalizeNodeRequest = normalizeNodeRequest;

}
class ServerAdapterRequestAbortSignal extends EventTarget {
aborted = false;
_onabort = null;
reason;
throwIfAborted() {
if (this.aborted) {
throw this.reason;
}
}
sendAbort() {
this.reason = new DOMException('This operation was aborted', 'AbortError');
this.aborted = true;
this.dispatchEvent(new Event('abort'));
}
get onabort() {
return this._onabort;
}
set onabort(value) {
this._onabort = value;
if (value) {
this.addEventListener('abort', value);
}
else {
this.removeEventListener('abort', value);
}
}
any(signals) {
return AbortSignal.any([...signals]);
}
}
exports.ServerAdapterRequestAbortSignal = ServerAdapterRequestAbortSignal;
let bunNodeCompatModeWarned = false;
exports.nodeRequestResponseMap = new WeakMap();
function normalizeNodeRequest(nodeRequest, fetchAPI, registerAbortCtrl) {
function normalizeNodeRequest(nodeRequest, fetchAPI, registerSignal) {
const rawRequest = nodeRequest.raw || nodeRequest.req || nodeRequest;

@@ -98,9 +129,19 @@ let fullUrl = buildFullUrl(rawRequest);

if (nodeResponse?.once) {
const controller = new fetchAPI.AbortController();
signal = controller.signal;
registerAbortCtrl?.(controller);
let sendAbortSignal;
// If ponyfilled
if (fetchAPI.Request !== globalThis.Request) {
const newSignal = new ServerAdapterRequestAbortSignal();
registerSignal?.(newSignal);
signal = newSignal;
sendAbortSignal = () => signal.sendAbort();
}
else {
const controller = new AbortController();
signal = controller.signal;
sendAbortSignal = () => controller.abort();
}
const closeEventListener = () => {
if (signal && !signal.aborted) {
rawRequest.aborted = true;
controller.abort();
sendAbortSignal();
}

@@ -442,3 +483,3 @@ };

deferred$.reject(abortSignal.reason);
}, { once: true });
});
response$

@@ -445,0 +486,0 @@ .then(function fetchSuccessHandler(res) {

@@ -12,3 +12,3 @@ "use strict";

}
function getRequestFromUWSRequest({ req, res, fetchAPI, ctrl }) {
function getRequestFromUWSRequest({ req, res, fetchAPI, signal }) {
const method = req.getMethod();

@@ -47,5 +47,5 @@ let duplex;

duplex = 'half';
ctrl.signal.addEventListener('abort', () => {
signal.addEventListener('abort', () => {
stop();
}, { once: true });
});
let readableStream;

@@ -102,3 +102,3 @@ getReadableStream = () => {

},
signal: ctrl.signal,
signal,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment

@@ -177,3 +177,3 @@ // @ts-ignore - not in the TS types yet

}
function sendResponseToUwsOpts(uwsResponse, fetchResponse, ctrl, fetchAPI) {
function sendResponseToUwsOpts(uwsResponse, fetchResponse, signal, fetchAPI) {
if (!fetchResponse) {

@@ -185,3 +185,3 @@ uwsResponse.writeStatus('404 Not Found');

const bufferOfRes = fetchResponse._buffer;
if (ctrl.signal.aborted) {
if (signal.aborted) {
return;

@@ -216,13 +216,13 @@ }

}
ctrl.signal.addEventListener('abort', () => {
signal.addEventListener('abort', () => {
if (!fetchResponse.body?.locked) {
fetchResponse.body?.cancel(ctrl.signal.reason);
fetchResponse.body?.cancel(signal.reason);
}
}, { once: true });
});
return fetchResponse.body
.pipeTo(createWritableFromUWS(uwsResponse, fetchAPI), {
signal: ctrl.signal,
signal,
})
.catch(err => {
if (ctrl.signal.aborted) {
if (signal.aborted) {
return;

@@ -229,0 +229,0 @@ }

import { AsyncDisposableStack, DisposableSymbols } from '@whatwg-node/disposablestack';
import * as DefaultFetchAPI from '@whatwg-node/fetch';
import { completeAssign, ensureDisposableStackRegisteredForTerminateEvents, handleAbortSignalAndPromiseResponse, handleErrorFromRequestHandler, isFetchEvent, isNodeRequest, isolateObject, isPromise, isRequestInit, isServerResponse, iterateAsyncVoid, nodeRequestResponseMap, normalizeNodeRequest, sendNodeResponse, } from './utils.js';
import { completeAssign, ensureDisposableStackRegisteredForTerminateEvents, handleAbortSignalAndPromiseResponse, handleErrorFromRequestHandler, isFetchEvent, isNodeRequest, isolateObject, isPromise, isRequestInit, isServerResponse, iterateAsyncVoid, nodeRequestResponseMap, normalizeNodeRequest, sendNodeResponse, ServerAdapterRequestAbortSignal, } from './utils.js';
import { fakePromise, getRequestFromUWSRequest, isUWSResponse, sendResponseToUwsOpts, } from './uwebsockets.js';

@@ -185,3 +185,3 @@ // Required for envs like nextjs edge runtime

: defaultServerContext;
const ctrl = new fetchAPI.AbortController();
const signal = new ServerAdapterRequestAbortSignal();
const originalResEnd = res.end.bind(res);

@@ -195,6 +195,6 @@ let resEnded = false;

originalOnAborted(function () {
ctrl.abort();
signal.sendAbort();
});
res.onAborted = function (cb) {
ctrl.signal.addEventListener('abort', cb, { once: true });
signal.addEventListener('abort', cb);
};

@@ -205,3 +205,3 @@ const request = getRequestFromUWSRequest({

fetchAPI,
ctrl,
signal,
});

@@ -219,4 +219,4 @@ let response$;

.then(response => {
if (!ctrl.signal.aborted && !resEnded) {
return sendResponseToUwsOpts(res, response, ctrl, fetchAPI);
if (!signal.aborted && !resEnded) {
return sendResponseToUwsOpts(res, response, signal, fetchAPI);
}

@@ -229,4 +229,4 @@ })

try {
if (!ctrl.signal.aborted && !resEnded) {
return sendResponseToUwsOpts(res, response$, ctrl, fetchAPI);
if (!signal.aborted && !resEnded) {
return sendResponseToUwsOpts(res, response$, signal, fetchAPI);
}

@@ -233,0 +233,0 @@ }

@@ -10,1 +10,2 @@ export * from './createServerAdapter.js';

export { Response } from '@whatwg-node/fetch';
export { DisposableSymbols } from '@whatwg-node/disposablestack';

@@ -50,5 +50,35 @@ export function isAsyncIterable(body) {

}
export class ServerAdapterRequestAbortSignal extends EventTarget {
aborted = false;
_onabort = null;
reason;
throwIfAborted() {
if (this.aborted) {
throw this.reason;
}
}
sendAbort() {
this.reason = new DOMException('This operation was aborted', 'AbortError');
this.aborted = true;
this.dispatchEvent(new Event('abort'));
}
get onabort() {
return this._onabort;
}
set onabort(value) {
this._onabort = value;
if (value) {
this.addEventListener('abort', value);
}
else {
this.removeEventListener('abort', value);
}
}
any(signals) {
return AbortSignal.any([...signals]);
}
}
let bunNodeCompatModeWarned = false;
export const nodeRequestResponseMap = new WeakMap();
export function normalizeNodeRequest(nodeRequest, fetchAPI, registerAbortCtrl) {
export function normalizeNodeRequest(nodeRequest, fetchAPI, registerSignal) {
const rawRequest = nodeRequest.raw || nodeRequest.req || nodeRequest;

@@ -76,9 +106,19 @@ let fullUrl = buildFullUrl(rawRequest);

if (nodeResponse?.once) {
const controller = new fetchAPI.AbortController();
signal = controller.signal;
registerAbortCtrl?.(controller);
let sendAbortSignal;
// If ponyfilled
if (fetchAPI.Request !== globalThis.Request) {
const newSignal = new ServerAdapterRequestAbortSignal();
registerSignal?.(newSignal);
signal = newSignal;
sendAbortSignal = () => signal.sendAbort();
}
else {
const controller = new AbortController();
signal = controller.signal;
sendAbortSignal = () => controller.abort();
}
const closeEventListener = () => {
if (signal && !signal.aborted) {
rawRequest.aborted = true;
controller.abort();
sendAbortSignal();
}

@@ -420,3 +460,3 @@ };

deferred$.reject(abortSignal.reason);
}, { once: true });
});
response$

@@ -423,0 +463,0 @@ .then(function fetchSuccessHandler(res) {

@@ -5,3 +5,3 @@ import { isPromise } from './utils.js';

}
export function getRequestFromUWSRequest({ req, res, fetchAPI, ctrl }) {
export function getRequestFromUWSRequest({ req, res, fetchAPI, signal }) {
const method = req.getMethod();

@@ -40,5 +40,5 @@ let duplex;

duplex = 'half';
ctrl.signal.addEventListener('abort', () => {
signal.addEventListener('abort', () => {
stop();
}, { once: true });
});
let readableStream;

@@ -95,3 +95,3 @@ getReadableStream = () => {

},
signal: ctrl.signal,
signal,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment

@@ -170,3 +170,3 @@ // @ts-ignore - not in the TS types yet

}
export function sendResponseToUwsOpts(uwsResponse, fetchResponse, ctrl, fetchAPI) {
export function sendResponseToUwsOpts(uwsResponse, fetchResponse, signal, fetchAPI) {
if (!fetchResponse) {

@@ -178,3 +178,3 @@ uwsResponse.writeStatus('404 Not Found');

const bufferOfRes = fetchResponse._buffer;
if (ctrl.signal.aborted) {
if (signal.aborted) {
return;

@@ -209,13 +209,13 @@ }

}
ctrl.signal.addEventListener('abort', () => {
signal.addEventListener('abort', () => {
if (!fetchResponse.body?.locked) {
fetchResponse.body?.cancel(ctrl.signal.reason);
fetchResponse.body?.cancel(signal.reason);
}
}, { once: true });
});
return fetchResponse.body
.pipeTo(createWritableFromUWS(uwsResponse, fetchAPI), {
signal: ctrl.signal,
signal,
})
.catch(err => {
if (ctrl.signal.aborted) {
if (signal.aborted) {
return;

@@ -222,0 +222,0 @@ }

{
"name": "@whatwg-node/server",
"version": "0.9.63-alpha-20241212160430-140b66f028923f44368e67598b6b66f47f690454",
"version": "0.9.63-rc-20241213082454-841c7fefb06e73c59003a91397fe3b1830666c7f",
"description": "Fetch API compliant HTTP Server adapter",

@@ -8,3 +8,3 @@ "sideEffects": false,

"@whatwg-node/disposablestack": "^0.0.5",
"@whatwg-node/fetch": "0.11.0-alpha-20241212160430-140b66f028923f44368e67598b6b66f47f690454",
"@whatwg-node/fetch": "^0.10.0",
"tslib": "^2.6.3"

@@ -11,0 +11,0 @@ },

@@ -10,1 +10,2 @@ export * from './createServerAdapter.js';

export { Response } from '@whatwg-node/fetch';
export { DisposableSymbols } from '@whatwg-node/disposablestack';

@@ -23,4 +23,14 @@ import type { IncomingMessage, ServerResponse } from 'http';

export type NodeResponse = ServerResponse | Http2ServerResponse;
export declare class ServerAdapterRequestAbortSignal extends EventTarget implements AbortSignal {
aborted: boolean;
private _onabort;
reason: any;
throwIfAborted(): void;
sendAbort(): void;
get onabort(): ((this: AbortSignal, ev: Event) => any) | null;
set onabort(value: ((this: AbortSignal, ev: Event) => any) | null);
any(signals: Iterable<AbortSignal>): AbortSignal;
}
export declare const nodeRequestResponseMap: WeakMap<NodeRequest, NodeResponse>;
export declare function normalizeNodeRequest(nodeRequest: NodeRequest, fetchAPI: FetchAPI, registerAbortCtrl?: (ctrl: AbortController) => void): Request;
export declare function normalizeNodeRequest(nodeRequest: NodeRequest, fetchAPI: FetchAPI, registerSignal?: (signal: ServerAdapterRequestAbortSignal) => void): Request;
export declare function isReadable(stream: any): stream is Readable;

@@ -27,0 +37,0 @@ export declare function isNodeRequest(request: any): request is NodeRequest;

import type { FetchAPI } from './types.js';
import { ServerAdapterRequestAbortSignal } from './utils.js';
export interface UWSRequest {

@@ -26,8 +27,8 @@ getMethod(): string;

fetchAPI: FetchAPI;
ctrl: AbortController;
signal: AbortSignal;
}
export declare function getRequestFromUWSRequest({ req, res, fetchAPI, ctrl }: GetRequestFromUWSOpts): Request;
export declare function getRequestFromUWSRequest({ req, res, fetchAPI, signal }: GetRequestFromUWSOpts): Request;
export declare function createWritableFromUWS(uwsResponse: UWSResponse, fetchAPI: FetchAPI): WritableStream<any>;
export declare function sendResponseToUwsOpts(uwsResponse: UWSResponse, fetchResponse: Response, ctrl: AbortController, fetchAPI: FetchAPI): Promise<void> | undefined;
export declare function sendResponseToUwsOpts(uwsResponse: UWSResponse, fetchResponse: Response, signal: ServerAdapterRequestAbortSignal, fetchAPI: FetchAPI): Promise<void> | undefined;
export declare function fakePromise<T>(value: T): Promise<T>;
export {};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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