UNPKG

@clearbit-dcp/clearbit.js-core

Version:

The hassle-free way to integrate analytics into any web application.

133 lines (106 loc) 2.52 kB
'use strict'; /** * Module dependencies. */ var bindAll = require('bind-all'); var clone = require('@ndhoule/clone'); var cookie = require('component-cookie'); var debug = require('debug')('analytics.js:cookie'); var defaults = require('@ndhoule/defaults'); var json = require('json3'); var topDomain = require('@segment/top-domain'); /** * Initialize a new `Cookie` with `options`. * * @param {Object} options */ function Cookie(options) { this.options(options); } /** * Get or set the cookie options. * * @param {Object} options * @field {Number} maxage (1 year) * @field {String} domain * @field {String} path * @field {Boolean} secure */ Cookie.prototype.options = function(options) { if (arguments.length === 0) return this._options; options = options || {}; var domain = '.' + topDomain(window.location.href); if (domain === '.') domain = null; this._options = defaults(options, { // default to a year maxage: 31536000000, path: '/', domain: domain }); // http://curl.haxx.se/rfc/cookie_spec.html // https://publicsuffix.org/list/effective_tld_names.dat // // try setting a dummy cookie with the options // if the cookie isn't set, it probably means // that the domain is on the public suffix list // like myapp.herokuapp.com or localhost / ip. this.set('cb:test', true); if (!this.get('cb:test')) { debug('fallback to domain=null'); this._options.domain = null; } this.remove('cb:test'); }; /** * Set a `key` and `value` in our cookie. * * @param {String} key * @param {Object} value * @return {Boolean} saved */ Cookie.prototype.set = function(key, value) { try { value = json.stringify(value); cookie(key, value, clone(this._options)); return true; } catch (e) { return false; } }; /** * Get a value from our cookie by `key`. * * @param {String} key * @return {Object} value */ Cookie.prototype.get = function(key) { try { var value = cookie(key); value = value ? json.parse(value) : null; return value; } catch (e) { return null; } }; /** * Remove a value from our cookie by `key`. * * @param {String} key * @return {Boolean} removed */ Cookie.prototype.remove = function(key) { try { cookie(key, null, clone(this._options)); return true; } catch (e) { return false; } }; /** * Expose the cookie singleton. */ module.exports = bindAll(new Cookie()); /** * Expose the `Cookie` constructor. */ module.exports.Cookie = Cookie;