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

object-id-mask

v1.0.0

Published

Easy way to mask your public auto incrementing ids and still get back the object name and id

Downloads

3

Readme

Object Id Mask

What is this?

A little npm module that uses the Hashids library to mask IDs. This adds on a version and a object type id so if you just get an id you can figure out for what object type it was for and can have different versions based on salt.

Why would I use this instead of Hashids?

  • Hashids said they are not always backwards compatable between versions so this handles the version issue.
  • It wraps the object type in as well so user id 1 and group id 1 are not the same hash.
  • Use this means you can tell qhat object this id belongs to from the id type. This could be a hardcoded file you run in your API or in the database.

Examples

To mask your id for object user:

var ObjectIdMask = require('object-id-mask')({
  salt: 'this is a secret, you should set it or I use a system default'
});

var user_type_id = 1; //Could get this from the database it is up to you.
var user = {
  id: 1,
  email: '[email protected]',
  name: 'John';
  last_name: 'Doe'
}

user.id = ObjectIdMask.encode(user.id, user_type_id);

return user;

Now if you get this object back you can decode it like this:

var user = req.body;
var user_type_id = 1; //Could get this from the database it is up to you.

user.id = ObjectIdMask.decode(user.id, user_type_id);

You can add your own versions as well as override the default version to yours:

var ObjectIdMask = require('object-id-mask')({
  default_version: 'Custom 1',
  versions: {
    'Custom 1': {
      salt: 'this is my secret',
      getEncoder: function(object_name, config) {
        //This needs to return an object like this
        return {
          encode: function(object_type_id, id) { //Takes postive integer numbers only
            // returns a encrypted string
          },
          decode: function(encryptedString) {
            // return a number (the Id)
          }
        };
      }
    }
  }
});

We will use just plain numbers as we move up in versions so if you do customize it make sure you add some namespace in the version.

You can also override the default delimiter with whatever you want. Here is how you can override the delimiter:

var ObjectIdMask = require('object-id-mask')({
  delimiter: ':',
  salt: 'this is my secret'
});

var user_type_id = 1;

ObjectIdMask.decode(22, user_type_id);
//This returns Version:Hash isntead of Version-Hash