Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@dotcom-reliability-kit/serialize-request
Advanced tools
A utility function to serialize a request object in a way that's friendly to loggers, view engines, and converting to JSON
A utility function to serialize a request object (Express or IncomingMessage) in a way that's friendly to loggers, view engines, and converting to JSON. This module is part of FT.com Reliability Kit.
Install @dotcom-reliability-kit/serialize-request
as a dependency:
npm install --save @dotcom-reliability-kit/serialize-request
Include in your code:
import serializeRequest from '@dotcom-reliability-kit/serialize-request';
// or
const serializeRequest = require('@dotcom-reliability-kit/serialize-request');
serializeRequest
The serializeRequest
function accepts a request-like object (e.g. an instance of Express.Request
or an object with method
and url
properties) and returns a plain JavaScript object (conforming to the SerializedRequest
type) which contains the relevant properties. The example below assumes that app
is an Express application:
app.get('/fruit/:fruitId', (request, response, next) => {
console.log(serializeRequest(request));
next();
});
// {
// id: 'request137',
// method: 'GET',
// url: '/fruit/feijoa',
// headers: {
// accept: '*/*'
// },
// route: {
// path: '/fruit/:fruitId',
// params: { fruitId: 'feijoa' }
// }
// }
You can also pass in a plain object if you already have one that looks like a request:
serializeRequest({
method: 'get',
url: '/hello'
});
Config options can be passed into the serializeRequest
function as a second argument. It expects an object with any of the keys below.
serializeRequest(request, {
// Config options go here
});
options.includeHeaders
An array of request headers to include in the serialized request object. This must be an Array
of String
s, with each string being a header name. It's important that you do not include headers which include personally-identifiable-information, API keys, or other privileged information. This defaults to ['accept', 'content-type']
.
serializeRequest(request, {
includeHeaders: [
'accept',
'content-length',
'content-type',
'user-agent'
]
});
SerializedRequest
typeThe SerializedRequest
type documents the return value of the serializeRequest
function. It will have the following properties, extracting them from a given request object.
SerializedRequest.id
This is extracted from the request.headers['x-request-id']
property and is always cast to a String
. It defaults to null
.
SerializedRequest.method
This is extracted from the request.method
property and is always cast to a String
and switched to uppercase. It defaults to "-"
.
SerializedRequest.url
This is extracted from the request.url
property and is always cast to a String
. It defaults to "/"
.
SerializedRequest.headers
This is extracted from the request.headers
property and is filtered to only include the headers specified in the includeHeaders
option. It defaults to an empty object.
SerializedRequest.route
This is an object extracted from request.route.path
(string) and request.params
(object) if they are present and conform to the same properties on an Express Request object. It defaults to undefined
.
See the central contributing guide for Reliability Kit.
Licensed under the MIT license.
Copyright © 2022, The Financial Times Ltd.
FAQs
A utility function to serialize a request object in a way that's friendly to loggers, view engines, and converting to JSON
We found that @dotcom-reliability-kit/serialize-request demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.