Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

browser-cookies

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browser-cookies

Tiny cookies library for the browser

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

browser-cookies

Tiny cookies library for the browser

NPM Version NPM Downloads Build Status Coveralls Status Dev Dependencies Status

Features

  • Clean and easy to use API
  • Small footprint (minified and gzipped ~ 0.5kB)
  • No dependencies
  • RFC6265 compliant
  • Cross browser support
  • Unit tests
  • Supports CommonJS (e.g. Browserify)

Browser compatibility

Cross browser support is verified on real browsers using automated testing:
Sauce Test Status

Installation

Using NPM
npm install browser-cookies

Using Bower
bower install browser-cookies

Usage

var cookies = require('browser-cookies');

cookies.set('firstName', 'Lisa');
cookies.set('firstName', 'Lisa', {expires: 365}); // Expires after 1 year
cookies.set('firstName', 'Lisa', {secure: true, domain: 'www.example.org'});

cookies.get('firstName'); // Returns cookie value (or null)

cookies.erase('firstName'); // Removes cookie

More examples

API

cookies.set( name, value [, options] )

Method to save a cookie

  • name (string) the name of the cookie to save
  • value (string) the value to save
  • options (object) may contain any of the properties specified in options below. If an option is not specified, the value configured in cookies.defaults will be used.

cookies.get( name )

Method that returns a cookie value, or null if the cookie is not found

  • name (string) the name of the cookie to retrieve

cookies.erase( name [, options] )

Method to remove a cookie

  • name (string) the name of the cookie to remove
  • options (object) may contain the domain and path properties specified in options below. If an option is not specified, the value configured in cookies.defaults will be used.

cookies.defaults

This object may be used to change the default value of each option specified in options below.

Options

Options may be set globally using cookies.defaults or passed as function argument, see the Examples section below and the API reference above for details.

NameTypeDefaultDescription
expiresNumber or Date0Number of days until the cookie expires, if set to 0 the cookie will expire at the end of the session. Alternatively a Date object may be passed such as new Date(2018, 3, 27).
domainString""The domain from where the cookie is readable. If not specified, the current domain will be used.
pathString"/"The path from where the cookie is readable, the default value of "/" allows the cookie to be readable from all paths. If set to "" the cookie will only be readable from the current browser path.

Note that cookies don't support relative paths such as ../../some/path, paths must be absolute like /some/path.
secureBooleanfalseIf true the cookie will only be transmitted over secure protocols like https.
httponlyBooleanfalseIf true the cookie may only be read by the web server.

This option may be set to prevent malicious scripts from accessing cookies, not all browsers support this feature yet.

Examples

Count the number of a visits to a page:

var cookies = require('browser-cookies');

// Get cookie value
var visits = cookies.get('count');
console.log("You've been here " + parseInt(visits) + " times before!");

// Increment the counter and set (or update) the cookie
cookies.set('count', parseInt(visits) + 1, {expires: 365});

JSON may be saved by converting the JSON object into a string:

var cookies = require('browser-cookies');

// Store JSON data
var user = {firstName: 'Sofia', lastName: 'Dueñas'};
cookies.set('user', JSON.stringify(user))

// Retrieve JSON data
var userString = cookies.get('user');
alert('Hi ' + JSON.parse(userString).firstName);

The default value of cookie options may be changed:

var cookies = require('browser-cookies');

// Override defaults
cookies.defaults.secure = true;
cookies.defaults.expires = 7;

// 'secure' option enabled and cookie expires in 7 days
cookies.set('FirstName', 'John')

// 'secure' option enabled and cookie expires in 30 days
cookies.set('LastName', 'Smith', {expires: 30})

Todo's

  • Additional tests:
    • More test cases to verify proper encoding/decoding (stubbed and non-stubbed).
    • More bad weather scenarios.
    • Mobile browser testing (Disabled automated testing for mobile browsers because the results varied per run).
    • Manually verify support on old browsers that that still need to be supported (i.e. IE6)?
  • Distribution:
    • Generate build (development build + minified version) for use without a loader.
  • Cross browser consistency:
    • When a domain is not specified most browsers only allow an exact domain match, but IE sends cookies to all subdomains. Perhaps save cookies to all subdomains by default for consistent behavior amongst all browsers? Would need to investigate whether something like window.location.hostname is cross-browser supported though. Or check how other cookie libs solved this. But first of all need to decide on the desired behavior.

Development

This design goal is to provide to smallest possible size (when minified and gzipped) for the given API, while remaining compliant to RFC6265 and providing cross-browser compatibility and consistency.

Development setup (requires node and git to be installed):

git clone https://github.com/voltace/browser-cookies.git
cd browser-cookies
npm install         # Install dev dependencies
npm run test:local  # Run unit tests locally (takes ~5 seconds)
npm run build       # Create minified version

Feel free to add an issue on GitHub for any questions, bug or feature request you may have.

License

Public Domain (UNLICENSE)

Keywords

FAQs

Package last updated on 09 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc