Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove uuid dep, include a prefix in generated idempotency keys #671

Merged
merged 1 commit into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const http = require('http');
const https = require('https');
const path = require('path');
const uuid = require('uuid/v4');

const utils = require('./utils');
const Error = require('./Error');
Expand Down Expand Up @@ -265,7 +264,7 @@ StripeResource.prototype = {
_defaultIdempotencyKey(method) {
// If this is a POST and we allow multiple retries, ensure an idempotency key.
if (method === 'POST' && this._stripe.getMaxNetworkRetries() > 0) {
return uuid();
return `stripe-node-retry-${utils.uuid4()}`;
}
return null;
},
Expand Down
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ const utils = (module.exports = {

return result;
},

/**
* https://stackoverflow.com/a/2117523
*/
uuid4: () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cute, love it

const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
},
});

function emitWarning(warning) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
},
"dependencies": {
"lodash.isplainobject": "^4.0.6",
"qs": "^6.6.0",
"uuid": "^3.3.2"
"qs": "^6.6.0"
},
"license": "MIT",
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions test/StripeResource.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const utils = require('../testUtils');
const uuid = require('uuid/v4');

const nock = require('nock');

Expand Down Expand Up @@ -357,7 +356,7 @@ describe('StripeResource', () => {
});

it('should reuse the given idempotency key provided for retries', (done) => {
const key = uuid();
const key = 'i-am-a-key';
let headers;

nock(`https://${options.host}`)
Expand Down