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

cookieio

v1.0.1

Published

Using JavaScript objects as an interface for working with cookies in the browser

Downloads

6

Readme

CookieIO.js

Using JavaScript objects as an interface for working with cookies in the browser

Description

The library relates key-value pairs of cookie entries to property-value pairs of a JavaScript object and vice versa. It also allows placing constraints on the size of entries being stored.

Usage

Reading entries

var cookies = CookieIO.getObject({/**settings**/});

Writing entries

var obj = {a: "a", b: "b"};
var success = CookieIO.saveObject(obj, {/**settings**/});

Deleting entries

CookieIO.subtractObject({a: "a", b: "b"}); 
CookieIO.subtractObject( CookieIO.getObject() ); //delete all

Default settings

var defaultSettings = {
    /**work modifiers**/
    nEncoder: encodeURIComponent,
    nDecoder: decodeURIComponent,
    vEncoder: encodeURIComponent,
    vDecoder: decodeURIComponent,
    reducer: null,
    rCallLim: 2000,
    globLimit: 2048,
    locLimit: null,
    names: null,
    inverse: false,
    /**cookie attributes**/
    expires: null, days: 365, maxAge: null,
    path: null,
    domain: null,
    secure: null,
    samesite: null,
};
//if a cookie attribute is set to a value *null*
//it means it should be ignored.

Settings description

  • nEncoder: cookie name encoder
  • nDecoder: cookie name decoder
  • vEncoder: cookie value encoder
  • vDecoder: cookie value decoder
  • globLimit: a limit on the overall size of encoded key-value pairs, that is what is transferred to the server
  • locLimit: a limit on the size of encoded key-value pairs for the object passed for saving, if both limits are set both will be taken into account. If it is impossible to place the data within either limit the operation will be aborted.
  • reducer: if an encoded string of entries to be stored exceeds the specified limits this function is called to delete some data from the object until it's possible to place the object into these boundaries. This function accepts the object and is meant to return a smaller object, or null or false if the object cannot be anymore minified to abort the operation. the number of invocation is passed as the second argument, starting with 0.
  • rCallLim: a limit defining a maximal number of reducer invocations, if it exceeded the limit the operation is aborted
  • names: an array listing the names that will be filtered, or null for no filtering
  • inverse: if the value is false, the data with names/keys the filter array contains will only be extracted, otherwise anything but that.
  • expires/days/maxAge: these attributes result in a value of the cookie attribute Expires, defining an expiration date of entries, expires can be assigned to a timestamp of that date or a Date object, days can be assigned to a numeric value defining the lifetime in days and maxAge defines this period in seconds. To specify the lifetime as session-long any of these attributes should be assigned to a value null.

Overriding settings

There are two ways of overriding: creating a new instance with binding a new settings object that will redefine some of the base instance settings or by passing a settings object directly to the invoked method. if some property isn't ever specified it is inherited from the default settings.

var commonCookies = CookieIO.getInstance({days: 700});
var base64Cookies = commonCookies.getInstance({
    vEncoder: commonCookies.base64Encoder,
    vDecoder: commonCookies.base64Decoder,
});
var objectCookies = commonCookies.getInstance({
    vEncoder: commonCookies.objectEncoder,
    vDecoder: commonCookies.objectDecoder,
});

Predefined encoders / decoders

//they also available on instances
CookieIO.base64Encoder //btoa(v)
CookieIO.base64Decoder //atob(v)
CookieIO.objectEncoder //encodeURIComponent(JSON.stringify(v))
CookieIO.objectDecoder //JSON.parse(decodeURIComponent(v))
CookieIO.defaultEncoder //encodeURIComponent
CookieIO.defaultDecoder //decodeURIComponent
CookieIO.passThru //v => v

Default reducer

The default reducer deletes one object property per call starting with ones added most recently until the object gets small enough to meet the constraints or if it is impossible to add a single entry the operation will be aborted.

CookieIO.defaultReducer

Example

var cookieIO = CookieIO.getInstance({
    globLimit: 192,
    days: 90,
});

var regularCookies = cookieIO.getInstance({
    names: ["_reg_a", "_reg_b"],
    vEncoder: cookieIO.base64Encoder,
    vDecoder: cookieIO.base64Decoder,
    locLimit: 48,
});
regularCookies.saveObject({
    "_reg_a": "abcdefghijab",
    "_reg_b": "abcdefghi",
});

var objectCookies = cookieIO.getInstance({
    names: ["_obj_a", "_obj_b" ],
    vEncoder: cookieIO.objectEncoder,
    vDecoder: cookieIO.objectDecoder,
    locLimit: 96,
    reducer: function(obj, callCounter) {
        if ( "_obj_b" in obj ) {
            if ( obj["_obj_b"].length > 0 ) {
                obj["_obj_b"].pop();
                return obj;
            } else {
                delete obj["_obj_b"];
                return obj;
            }
        } else if ( "_obj_a" in obj ) {
            delete obj["_obj_a"];
            return obj;
        } else {
            return null; //abort operation
            //return {}; //delete entries
        }
    },
});
objectCookies.saveObject({
    "_obj_b": [ 10000, 20000, 30000, 40000, 50000 ],
    "_obj_a": { a: [ 0, 1, 2, 3 ], b: "abcd" },
}); //"_obj_b" array will contain only two elements

var temporaryCookies = cookieIO.getInstance({
    names: ["_obj_a", "_obj_b", "_reg_a", "_reg_b"],
    inverse: true,
    expires: null,
    locLimit: 48,
    reducer: cookieIO.defaultReducer,
});
temporaryCookies.saveObject({
    "_tmp_a": "01234567890123456789",
    "_tmp_b": "0123456789",
    "_tmp_c": "0123456789", //won't be saved
});

console.log( temporaryCookies.getObject() );
console.log( regularCookies.getObject() );
console.log( objectCookies.getObject() );

temporaryCookies.subtractObject( temporaryCookies.getObject() );
regularCookies.subtractObject( regularCookies.getObject() );
objectCookies.subtractObject( objectCookies.getObject() );