Skip to content

Commit

Permalink
Merge pull request #454 from recurly/uuids
Browse files Browse the repository at this point in the history
Adds deviceId and sessionId
  • Loading branch information
snodgrass23 authored May 16, 2018
2 parents 90bfc3e + 2ebe151 commit 3265e59
Show file tree
Hide file tree
Showing 21 changed files with 395 additions and 255 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry = "https://registry.npmjs.org/"
9 changes: 7 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ var staticConfig = {
colors: true,
autoWatch: true,
browsers: [
'PhantomJS'
// 'ChromeDebug'
// 'PhantomJS'
'ChromeDebug'
// 'FirefoxDebug'
// 'IE11 - Win7'
],
singleRun: true,
Expand All @@ -25,6 +26,10 @@ var staticConfig = {
ChromeDebug: {
base: 'Chrome',
flags: ['--auto-open-devtools-for-tabs']
},
FirefoxDebug: {
base: 'Firefox',
flags: ['-devtools']
}
},
client: {
Expand Down
11 changes: 10 additions & 1 deletion lib/recurly.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import clone from 'component-clone';
import Emitter from 'component-emitter';
import uuid from 'uuid/v4';
import errors from './errors';
import version from './version';
import bankAccount from './recurly/bank-account';
Expand All @@ -20,10 +21,10 @@ import {Bus} from './recurly/bus';
import {Fraud} from './recurly/fraud';
import {HostedFields, FIELD_TYPES} from './recurly/hosted-fields';
import {Request} from './recurly/request';
import {storage} from './util/web-storage';
import CheckoutPricing from './recurly/pricing/checkout';
import SubscriptionPricing from './recurly/pricing/subscription';
import deepAssign from './util/deep-assign';
import uuid from './util/uuid';

// Ensure recurly.css is included in the build
import recurlyCSS from './recurly.css';
Expand Down Expand Up @@ -124,6 +125,14 @@ export class Recurly extends Emitter {
return !!this.config.parent;
}

get deviceId () {
return storage({ key: 'deviceId', otherwise: uuid() });
}

get sessionId () {
return storage({ scope: 'session', key: 'sessionId', otherwise: uuid() });
}

/**
* Assembles the API endpoint
*
Expand Down
4 changes: 2 additions & 2 deletions lib/recurly/bus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Emitter from 'component-emitter';
import {Message} from './message';
import uuid from '../util/uuid';
import uuid from 'uuid/v4';

/**
* Hosted field generic message bus
Expand All @@ -15,7 +15,7 @@ export class Bus extends Emitter {
constructor (options = {}) {
super();
this.id = uuid();
this.debug = require('debug')(`recurly:bus:${this.id}`);
this.debug = require('debug')(`recurly:bus:${this.id.split('-')[0]}`);
this.emitters = [];
this.recipients = [];
this.receive = this.receive.bind(this);
Expand Down
8 changes: 4 additions & 4 deletions lib/recurly/frame.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import qs from 'qs';
import Emitter from 'component-emitter';
import uuid from '../util/uuid';
import uuid from 'uuid/v4';
import errors from '../errors';

const debug = require('debug')('recurly:frame');
Expand All @@ -24,9 +24,9 @@ export function factory (options) {
class Frame extends Emitter {
constructor (options) {
super();
this.id = uuid();
this.recurly = options.recurly;
this.name = `recurly-frame-${this.recurly.id}-${this.id}`;
this.id = `${this.recurly.id.split('-')[0]}-${uuid().split('-')[0]}`;
this.name = `recurly-frame-${this.id}`;
this.width = options.width || DEFAULTS.width;
this.height = options.height || DEFAULTS.height;
this.prepare(options.path, options.payload);
Expand Down Expand Up @@ -67,7 +67,7 @@ class Frame extends Emitter {
let relay = document.createElement('iframe');
relay.width = relay.height = 0;
relay.src = this.recurly.url('/relay');
relay.name = `recurly-relay-${this.recurly.id}-${this.id}`;
relay.name = `recurly-relay-${this.id}`;
relay.style.display = 'none';
relay.onload = () => this.create();
window.document.body.appendChild(relay);
Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/pricing/checkout/attachment.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Emitter from 'component-emitter';
import find from 'component-find';
import Promise from 'promise';
import uuid from 'uuid/v4';
import SubscriptionPricing from '../subscription';
import dom from '../../../util/dom';
import flatten from '../../../util/flatten';
import groupBy from '../../../util/group-by';
import uuid from '../../../util/uuid';
import {ignoreNotFound} from '../subscription/attachment';

const debug = require('debug')('recurly:pricing:checkout:attachment');
Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/pricing/checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import isEmpty from 'lodash.isempty';
import isFinite from 'lodash.isfinite';
import Promise from 'promise';
import uniq from 'array-unique';
import uuid from 'uuid/v4';
import errors from '../../../errors';
import uuid from '../../../util/uuid';
import {Pricing} from '../';
import PricingPromise from '../promise';
import SubscriptionPricing from '../subscription';
Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/pricing/subscription/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import uuid from 'uuid/v4';
import {Pricing, findByCode} from '../';
import PricingPromise from '../promise';
import Calculations from './calculations';
import Attachment from './attachment';
import errors from '../../../errors';
import uuid from '../../../util/uuid';

const debug = require('debug')('recurly:pricing:subscription-pricing');

Expand Down
12 changes: 10 additions & 2 deletions lib/recurly/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export class Request {
return this.recurly.isParent ? this.recurly.version : this.recurly.config.parentVersion;
}

get deviceId () {
return this.recurly.deviceId;
}

get sessionId () {
return this.recurly.sessionId;
}

get shouldUseXHR () {
return !!this.recurly.config.cors;
}
Expand Down Expand Up @@ -76,10 +84,10 @@ export class Request {
throw errors('not-configured');
}

const { version, key, timeout } = this;
const { version, key, deviceId, sessionId, timeout } = this;
const url = this.recurly.url(route);

data = deepAssign({}, data, { version, key });
data = deepAssign({}, data, { version, key, deviceId, sessionId });

if (this.shouldUseXHR) {
return this.xhr({ method, url, data, done, timeout });
Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import each from 'component-each';
import {normalize} from '../util/normalize';
import uuid from '../util/uuid';
import uuid from 'uuid/v4';
import errors from '../errors';

const debug = require('debug')('recurly:token');
Expand Down
11 changes: 0 additions & 11 deletions lib/util/uuid.js

This file was deleted.

23 changes: 23 additions & 0 deletions lib/util/web-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const NAMESPACE = '__recurly__';

/**
* Fetches a value from web storage, optionally setting its value
* if it is not already set
*
* @param {String} options.scope 'session' or 'local'
* @param {String} options.key item key
* @param {DOMString} [options.otherwise] fallback
* @return {Mixed} item value
*/
export function storage ({ scope = 'local', key, otherwise }) {
const store = scope === 'session' ? window.sessionStorage : window.localStorage;
let qualifiedKey = `${NAMESPACE}.${key}`;
let value = store.getItem(qualifiedKey);

if (value === null && !~[null, undefined].indexOf(otherwise)) {
value = otherwise;
store.setItem(qualifiedKey, value);
}

return value;
}
Loading

0 comments on commit 3265e59

Please sign in to comment.