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

serializerr

v1.0.3

Published

Convert Errors & Objects into an easily-serialized vanilla Object.

Downloads

290,205

Readme

serializerr

Convert Errors & Objects into an easily-serialized vanilla Object.

Build Status

serializerr creates a vanilla Object with a flattened prototype chain & any non-enumerable properties mapped to enumerable properties.

This allows Error objects to be serialised to JSON without losing important data.

Installation

npm install serializerr

Usage


var wellSerializedError = JSON.parse(JSON.stringify(
  serializerr(error)
))

console.log(wellSerializedError.name) // Error
console.log(wellSerializedError.message) // an error occurred
console.log(wellSerializedError.stack) // Error: an error occurred\n  at Test.<anonymous> ...

Example


var serializerr = require('serializerr')

var error = new Error('an error')

// simulate transferring an Error object over the wire as JSON
// without first passing through serializerr
var poorlySerializedError = JSON.parse(JSON.stringify(error))

// oh dear:
console.log(poorlySerializedError.name) // undefined
console.log(poorlySerializedError.message) // undefined
console.log(poorlySerializedError.stack) // undefined

// bring forth the serializerr
var errorObject = serializerr(error)

var wellSerializedError = JSON.parse(JSON.stringify(errorObject))

// properties are conserved!
console.log(wellSerializedError.name) // Error
console.log(wellSerializedError.message) // an error occurred
console.log(wellSerializedError.stack) // Error: an error occurred\n  at Test.<anonymous> ...

// note errorObject is a vanilla Object, not an Error
errorObject instanceof Error // false

Why

If you've ever tried to send an Error over a JSON-encoded connection you've probably been surprised to find all the useful information is sapped out of it; all the juicy properties like name, message & stack are non-enumerable thus they are not included in the stringified JSON. This may be non-standard behaviour, as I could not locate any mention in either the ES5.1 or the ES6 spec about it, but Error properties are non-enumerable both in V8 (Chrome/io.js/Node.js) & SpiderMonkey (Firefox).

I believe Error property non-enumerability was added as a security measure to prevent stack traces and other sensitive information accidentally leaking, but it's not uncommon to actually want to send the data in Error objects over the wire.

serializerr makes an Object suitable for serializing to & from JSON. Not restricted to use with Errors, will work with any Object.

Notes on 'ize' vs 'ise'

Name was selected as programming world is mostly Americanised, and npm search does not seem to do effective stemming.

This decision came with strong feelings of guilt and shame about what I thought was blasphemous Americanised spelling, but it turns out this is a misconception thus I am pardoned:

The -ize spelling is often incorrectly seen as an Americanism in Britain. However, the Oxford English Dictionary (OED) recommends -ize and notes that the -ise spelling is from French.

From Wikipedia: American and British English spelling differences

License

ISC