request-headers-filter
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -9,5 +9,13 @@ /** | ||
import * as http from 'http'; | ||
/// <reference types="node" /> | ||
import { IncomingHttpHeaders, IncomingMessage, OutgoingMessage } from 'http'; | ||
export function filterHeaders(incomingMessage: http.IncomingMessage, headersToFilter?: Array<string|RegExp>, save?: boolean): void; | ||
export function saveHeaders(incomingMessage: http.IncomingMessage, serverResponse: http.ServerResponse, headersToSave?: string[]): void; | ||
/** | ||
* Truncate unrequired headers from request | ||
*/ | ||
export declare function filterHeaders(incomingMessage: IncomingHttpHeaders, headersList: string[], save?: boolean): void; | ||
/** | ||
* Save all or only required headers from request to response | ||
*/ | ||
export declare function saveHeaders(incomingMessage: IncomingMessage, serverResponse: OutgoingMessage, headersToSave: string[]): void; |
50
index.js
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/** | ||
@@ -16,13 +18,20 @@ * @license | ||
*/ | ||
function filterHeaders(incomingMessage, headersList, save = false) { | ||
const headers = incomingMessage.headers; | ||
const regexps = headersList.map(headerName => new RegExp(headerName)); | ||
Object.keys(headers).map(header => { | ||
regexps.some(regexp => regexp.test(header)) ^ save | ||
? delete headers[header] | ||
: void 0; | ||
function filterHeaders(incomingMessage, headersList, save) { | ||
save = save || false; | ||
var headers = incomingMessage.headers; | ||
var regexps = headersList.map(function (headerName) { | ||
return new RegExp(headerName); | ||
}); | ||
Object.keys(headers).map(function (header) { | ||
regexps.some(function (regexp) { | ||
return regexp.test(header); | ||
}) ^ save ? delete headers[header] : void 0; | ||
}); | ||
} | ||
exports.filterHeaders = filterHeaders; | ||
/** | ||
@@ -37,16 +46,22 @@ * Save all or only required headers from request to response | ||
function saveHeaders(incomingMessage, serverResponse, headersToSave) { | ||
incomingMessage.on('response', (response) => { | ||
incomingMessage.on('response', function (response) { | ||
// Back-up all required headers | ||
const savedHeaders = headersToSave | ||
? headersToSave.map(rawHeader => { | ||
const header = rawHeader.toLowerCase(); | ||
var savedHeaders = headersToSave | ||
? headersToSave.map(function (rawHeader) { | ||
var header = rawHeader.toLowerCase(); | ||
return [header, response.headers[header]]; | ||
}).filter(header => header[1] !== void 0) : Object.entries(response.headers); | ||
}).filter(function (header) { | ||
return header[1] !== void 0; | ||
}) | ||
: Object.keys(response.headers).map(function (header) { | ||
return [header, response.headers[header]]; | ||
}); | ||
// Clean-up response headers | ||
for (let k in response.headers) delete response.headers[k]; | ||
for (var k in response.headers) { | ||
delete response.headers[k]; | ||
} | ||
// Apply saved headers to response | ||
savedHeaders.map(header => { | ||
savedHeaders.map(function (header) { | ||
serverResponse.setHeader(header[0], header[1]); | ||
@@ -57,5 +72,2 @@ }); | ||
module.exports = { | ||
filterHeaders, | ||
saveHeaders | ||
}; | ||
exports.saveHeaders = saveHeaders; |
{ | ||
"name": "request-headers-filter", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Filter headers from request which will be passed to new request and/or response", | ||
@@ -34,2 +34,5 @@ "main": "index.js", | ||
}, | ||
"engines": { | ||
"node": ">= 4" | ||
}, | ||
"homepage": "https://github.com/L2jLiga/request-headers-filter#readme", | ||
@@ -36,0 +39,0 @@ "devDependencies": { |
@@ -42,3 +42,3 @@ ## Request headers filter | ||
1. When you [want to save several headers](test/filter-specify-headers-from-response.js) | ||
1. When you [want to save several headers](test/filter-specify-headers-from-response.spec.js) | ||
1. When you [use Transform stream to change response](test/save-headers-with-transform-stream.spec.js) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6877
77