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
736
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.62 to 0.9.63-alpha-20241212150911-36df5929a304522e501744daf167032d4b7994c0

16

cjs/createServerAdapter.js

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

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

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

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

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

fetchAPI,
signal,
ctrl,
});

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

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

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

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

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

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.decompressedResponseMap = exports.nodeRequestResponseMap = exports.ServerAdapterRequestAbortSignal = void 0;
exports.decompressedResponseMap = exports.nodeRequestResponseMap = void 0;
exports.isAsyncIterable = isAsyncIterable;

@@ -72,36 +72,5 @@ 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, registerSignal) {
function normalizeNodeRequest(nodeRequest, fetchAPI, registerAbortCtrl) {
const rawRequest = nodeRequest.raw || nodeRequest.req || nodeRequest;

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

if (nodeResponse?.once) {
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 controller = new fetchAPI.AbortController();
signal = controller.signal;
registerAbortCtrl?.(controller);
const closeEventListener = () => {
if (signal && !signal.aborted) {
rawRequest.aborted = true;
sendAbortSignal();
controller.abort();
}

@@ -148,0 +107,0 @@ };

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

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

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

duplex = 'half';
signal.addEventListener('abort', () => {
ctrl.signal.addEventListener('abort', () => {
stop();

@@ -102,3 +102,3 @@ });

},
signal,
signal: ctrl.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, signal, fetchAPI) {
function sendResponseToUwsOpts(uwsResponse, fetchResponse, ctrl, fetchAPI) {
if (!fetchResponse) {

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

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

@@ -216,5 +216,5 @@ }

}
signal.addEventListener('abort', () => {
ctrl.signal.addEventListener('abort', () => {
if (!fetchResponse.body?.locked) {
fetchResponse.body?.cancel(signal.reason);
fetchResponse.body?.cancel(ctrl.signal.reason);
}

@@ -224,6 +224,6 @@ });

.pipeTo(createWritableFromUWS(uwsResponse, fetchAPI), {
signal,
signal: ctrl.signal,
})
.catch(err => {
if (signal.aborted) {
if (ctrl.signal.aborted) {
return;

@@ -230,0 +230,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, ServerAdapterRequestAbortSignal, } from './utils.js';
import { completeAssign, ensureDisposableStackRegisteredForTerminateEvents, handleAbortSignalAndPromiseResponse, handleErrorFromRequestHandler, isFetchEvent, isNodeRequest, isolateObject, isPromise, isRequestInit, isServerResponse, iterateAsyncVoid, nodeRequestResponseMap, normalizeNodeRequest, sendNodeResponse, } from './utils.js';
import { fakePromise, getRequestFromUWSRequest, isUWSResponse, sendResponseToUwsOpts, } from './uwebsockets.js';

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

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

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

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

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

fetchAPI,
signal,
ctrl,
});

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

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

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

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

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

@@ -50,35 +50,5 @@ 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, registerSignal) {
export function normalizeNodeRequest(nodeRequest, fetchAPI, registerAbortCtrl) {
const rawRequest = nodeRequest.raw || nodeRequest.req || nodeRequest;

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

if (nodeResponse?.once) {
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 controller = new fetchAPI.AbortController();
signal = controller.signal;
registerAbortCtrl?.(controller);
const closeEventListener = () => {
if (signal && !signal.aborted) {
rawRequest.aborted = true;
sendAbortSignal();
controller.abort();
}

@@ -125,0 +85,0 @@ };

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

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

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

duplex = 'half';
signal.addEventListener('abort', () => {
ctrl.signal.addEventListener('abort', () => {
stop();

@@ -95,3 +95,3 @@ });

},
signal,
signal: ctrl.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, signal, fetchAPI) {
export function sendResponseToUwsOpts(uwsResponse, fetchResponse, ctrl, fetchAPI) {
if (!fetchResponse) {

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

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

@@ -209,5 +209,5 @@ }

}
signal.addEventListener('abort', () => {
ctrl.signal.addEventListener('abort', () => {
if (!fetchResponse.body?.locked) {
fetchResponse.body?.cancel(signal.reason);
fetchResponse.body?.cancel(ctrl.signal.reason);
}

@@ -217,6 +217,6 @@ });

.pipeTo(createWritableFromUWS(uwsResponse, fetchAPI), {
signal,
signal: ctrl.signal,
})
.catch(err => {
if (signal.aborted) {
if (ctrl.signal.aborted) {
return;

@@ -223,0 +223,0 @@ }

{
"name": "@whatwg-node/server",
"version": "0.9.62",
"version": "0.9.63-alpha-20241212150911-36df5929a304522e501744daf167032d4b7994c0",
"description": "Fetch API compliant HTTP Server adapter",

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

"@whatwg-node/disposablestack": "^0.0.5",
"@whatwg-node/fetch": "^0.10.0",
"@whatwg-node/fetch": "0.11.0-alpha-20241212150911-36df5929a304522e501744daf167032d4b7994c0",
"tslib": "^2.6.3"

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

@@ -23,14 +23,4 @@ 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, registerSignal?: (signal: ServerAdapterRequestAbortSignal) => void): Request;
export declare function normalizeNodeRequest(nodeRequest: NodeRequest, fetchAPI: FetchAPI, registerAbortCtrl?: (ctrl: AbortController) => void): Request;
export declare function isReadable(stream: any): stream is Readable;

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

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

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

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

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