Let modifyNpmInfoRequestHeaders return the header collection as well.

This commit is contained in:
Sander Declerck 2025-11-24 15:22:41 +01:00
parent 5834229427
commit 44ee58aa9b
No known key found for this signature in database
3 changed files with 18 additions and 10 deletions

View file

@ -10,14 +10,14 @@ import { EventEmitter } from "events";
* @typedef {Object} RequestInterceptionContext
* @property {string} targetUrl
* @property {(packageName: string | undefined, version: string | undefined) => void} blockMalware
* @property {(modificationFunc: (headers: NodeJS.Dict<string | string[]>) => void) => void} modifyRequestHeaders
* @property {(modificationFunc: (headers: NodeJS.Dict<string | string[]>) => NodeJS.Dict<string | string[]>) => void} modifyRequestHeaders
* @property {(modificationFunc: (body: Buffer, headers: NodeJS.Dict<string | string[]> | undefined) => Buffer) => void} modifyBody
* @property {() => RequestInterceptionHandler} build
*
*
* @typedef {Object} RequestInterceptionHandler
* @property {{statusCode: number, message: string} | undefined} blockResponse
* @property {(headers: NodeJS.Dict<string | string[]> | undefined) => void} modifyRequestHeaders
* @property {(headers: NodeJS.Dict<string | string[]> | undefined) => NodeJS.Dict<string | string[]> | undefined} modifyRequestHeaders
* @property {() => boolean} modifiesResponse
* @property {(body: Buffer, headers: NodeJS.Dict<string | string[]> | undefined) => Buffer} modifyBody
*/
@ -65,7 +65,7 @@ function buildInterceptor(requestHandlers) {
function createRequestContext(targetUrl, eventEmitter) {
/** @type {{statusCode: number, message: string} | undefined} */
let blockResponse = undefined;
/** @type {Array<(headers: NodeJS.Dict<string | string[]>) => void>} */
/** @type {Array<(headers: NodeJS.Dict<string | string[]>) => NodeJS.Dict<string | string[]>>} */
let reqheaderModificationFuncs = [];
/** @type {Array<(body: Buffer, headers: NodeJS.Dict<string | string[]> | undefined) => Buffer>} */
let modifyBodyFuncs = [];
@ -91,13 +91,18 @@ function createRequestContext(targetUrl, eventEmitter) {
/** @returns {RequestInterceptionHandler} */
function build() {
/** @param {NodeJS.Dict<string | string[]> | undefined} headers */
/**
* @param {NodeJS.Dict<string | string[]> | undefined} headers
* @returns {NodeJS.Dict<string | string[]> | undefined}
*/
function modifyRequestHeaders(headers) {
if (!headers) return;
for (const func of reqheaderModificationFuncs) {
func(headers);
if (headers) {
for (const func of reqheaderModificationFuncs) {
func(headers);
}
}
return headers;
}
/**

View file

@ -8,6 +8,7 @@ const state = {
/**
* @param {NodeJS.Dict<string | string[]>} headers
* @returns {NodeJS.Dict<string | string[]>}
*/
export function modifyNpmInfoRequestHeaders(headers) {
const accept = getHeaderValueAsString(headers, "accept");
@ -17,6 +18,7 @@ export function modifyNpmInfoRequestHeaders(headers) {
// Force the registry to return the full metadata by changing the Accept header.
headers["accept"] = "application/json";
}
return headers;
}
/**

View file

@ -151,11 +151,12 @@ function forwardRequest(req, hostname, res, requestHandler) {
* @returns {import("http").ClientRequest}
*/
function createProxyRequest(hostname, req, res, requestHandler) {
const headers = { ...req.headers };
/** @type {NodeJS.Dict<string | string[]> | undefined} */
let headers = { ...req.headers };
if (headers.host) {
delete headers.host;
}
requestHandler.modifyRequestHeaders(headers);
headers = requestHandler.modifyRequestHeaders(headers);
/** @type {import("http").RequestOptions} */
const options = {