npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@juntoz/pino-serializer-shorten

v0.0.1

Published

Create a custom wrapper serializer for pino to reduce the footprint of the req object

Downloads

24

Readme

pino-serializer-shorten

Create a custom wrapper serializer for pino to reduce the footprint of the req object.

What it basically does is to shorten the values that comes in the req.headers object.

For now.

The idea is that, in the future, we can also extend it to filter err and res objects, and other fields as well.

How to install?

npm i @juntoz/pino-serializer-shorten

How to use?

First require the package.

The entrypoint is a method that accepts an options object and returns the serializers object as pino-http requires.

const getSerializers = require('../index');

var kpl = require('koa-pino-logger')({
    // ... other options
    serializers: getSerializers(options)
});

koaApp.use(kpl);

This package uses the official pino-std-serializers package, so it should be fully compliant with pino and pino-http.

NOTE: my app uses koa, so I have not tried it in other type of applications using pino.

Options

The options object default values and structure is:

{
    // max length that the serializer allows. If the string is bigger, it shortens its length
    allowedMaxLength: 40,
    // when the string exceeds the max length, present the first X characters
    first: 10,
    // when the string exceeds the max length, present the last X characters
    last: 10,
    // when the string exceeds the max length, separate the first and last characters with this string
    separator: '...',
    // configuration for the req serializer
    req: {
        // enabled?. Default: true
        enabled: true,
        // whether to parse the cookie header
        parseCookie: true
    }
}

Parsing the cookie

When the setting req.parseCookie == true, the library will detect the req.headers.cookie field and it will apply a special logic.

Each request could receive multiple cookies, therefore it may be important to know what is inside, while still keeping your logs short.

If the setting is true, it will apply the same settings to each cookie and return the short version of each value.

If the setting is false, it will treat the field as a simple string and apply the shorten logic.

For example, for this req.headers.cookie

{
    "headers": {
        // ...
        "cookie": `.AspNetCore.Cookies=CfDJ8DVa95S6dSJInuv1bB_0X5SXNHo7j_pXq7xvhaz71trWk ciUTIfhfZeuHA6SZtgY6kn6Jdq-V3LhpkKtX6TLdQUaRyd_9p0OC7cykE7GH6eCCFHZzF hIbMCtW31d5FtFR1_X0Q8Xy-U2BBdYR6sA08OhNF0XGaCICp17MBt20Ngf67oTjlZxAjh gBJYtX_376RU-kVlKI7OLTWj12CNm_l7eQ; MyCookie=123456789012345678901234 56789012345678901234567890`
    }
};

If true, it will come up as:

cookie: .AspNetCore.Cookies=CfDJ8..._l7eQ; MyCookie=12345...67890

where each cookie is kept and the contents are shortened.

If false, on the other hand, it will treat it as a simple string

cookie: .AspN...67890

Test

Use this

npm test

The tests are basically verifying that the conversion logic works. It does not seem to require any test involving pino or pino-http.

The Future

What other features are in the pipeline?

  1. Extend to err and res objects, although these two are not that large usually.
  2. Select which headers to apply the shortened logic and which should not.
  3. Select which root fields to apply this logic.
  4. Parsing Cookies to have its own settings of shortening.
  5. Try to make it more performant (run some benchmarks, tune the logic, not use lodash or cookie readers, etc).
  6. Remove all headers instead of shorten them.