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

unmock-hash

v0.0.55

Published

[![npm](https://img.shields.io/npm/v/unmock-hash.svg)][npmjs] [![CircleCI](https://circleci.com/gh/unmock/unmock-js.svg?style=svg)](https://circleci.com/gh/unmock/unmock-js) [![codecov](https://codecov.io/gh/unmock/unmock-js/branch/dev/graph/badge.svg)]

Downloads

30

Readme

unmock-hash

npm CircleCI codecov Known Vulnerabilities

Node JS reference implementation of the unmock hash function.

Versions

v0

The hash function combines together seven required parameters and one optional parameter in order to give every mock created by unmock a unique hash. The hash function is the primary means of determining that two mocks are intended to be the same.

Parameters

Below is the typescript specification for the parameters of the v0 hash function.

{
  body: string | {};
  headers: IStringMap;
  hostname: string;
  method: string;
  path: string;
  story: string[];
  user_id: string;
  signature?: string;
}

| Name | Description | | --------- | ----------- | | body | The body of the incoming request, represented as a string. A non-existent body is represented by an empty object | | headers | The headers of the incoming request, represented as a dictionary of strings. Note that, unless otherwise requested (see below), header keys are case sensitive. | | hostname | The host of the incoming request, ie www.example.com or api.foo.com. | | method | The method. Note that the method, unlike header keys, is always case invariant, meaning that GET is the same as get. | | path | The path of the request, ie /api/v2/foo. | | story | An array of unmock requests that precede this one, ordered from most to least recent. For example, ["a835de14", "bfa619cc"] means that a mock with hash "a835de14" came directly before the current one, and "bfa619cc" before that. | | user_id | The user_id used when interacting with unmock.io. This is always checked against the server when authenticated. As a convention, the unauthenticated user name is always called "MOSES".| | signature | An optional string used to sign requests. This allows two requests that are otherwise exactly the same to be differentiated by the user. Can be anything, but in general, it should be something difficult to guess. |

Ignore

The hash function can ignore certain parameters when generating a hash, allowing for two objects to be considered similar. This is especially useful when dealing with URLs that need to resolve to the same hash in spite of minor differences. For example, all post requests to a given path can be conflated by setting ignore to:

["story", { headers: "Content-Length" }, "body"]

This means that the body, the length of the body, and what preceded the request will be disregarded when making the hash. However, on the other hand, if the POST changes to GET, this will be picked up by the hash function.

The ignore object is expressed in typescript as such:


export interface IIgnoreObjectV0 {
  [key: string]: string | undefined | string[] | IStringMap;
  body?: string;
  headers?: string | string[] | IStringMap;
  hostname?: string;
  method?: string;
  path?: string;
  story?: string | string[];
  user_id?: string;
  signature?: string;
}

type IgnoreFieldV0 =
  | "body"
  | "headers"
  | "hostname"
  | "method"
  | "path"
  | "story"
  | "user_id"
  | "signature";
type SingleIgnoreV0 = IIgnoreObjectV0 | IgnoreFieldV0;
type IgnoreV0 = SingleIgnoreV0 | SingleIgnoreV0[];

This means that ignore can be a single string, ie headers, in which case the headers can be ignored. It can also be a an array of parameters to ignore, ie ["headers", "user_id"]. In some cases, more fine-grained control is needed, in which case the IIgnoreObjectV0 can be used. Each parameter of the object points to a string that is used as a RegExp for filtering. For example, {path: "^/api/v2/foo/[a-zA-Z0-9]*$"} will conflate the final alphanumeric path of /api/v2/foo/ to the same hash. Headers and stories work the same way, but mirroring their object structure to allow fine grained control for individual headers (ie ignoring all fields containing user-agent) or stories (ie ignoring all occurances of "a835de14" in a story).

Actions

A number of additional actions are available that, like ignore, conflate similar types of requsets to a single hash. These are defined in typescript as:

type ActionsV0 =
  | "make-header-keys-lowercase"
  | "deserialize-json-body"
  | "deserialize-x-www-form-urlencoded-body";

This corresponds to the following effects on the hashable object:

| Action | Effect | | ------ | ------ | | make-header-keys-lowercase | Makes all header keys lowercase, which is a sensible default, as header keys are not supposed to be case sensitive but unfortunately are treated as such in some exotic circumstances. | | deserialize-json-body | Deserializes the body into JSON if the Content-Type: application/json header, or one of its variants, is present. | | deserialize-x-www-form-urlencoded-body | Deserializes the body into key-value pairs if the Content-Type: application/x-www-form-urlencoded-body header, or one of its variants, is present. |