Skip to content

Commit

Permalink
Update user agent computation to handle environments without process. (
Browse files Browse the repository at this point in the history
  • Loading branch information
dcr-stripe authored Aug 16, 2021
1 parent 6f9c519 commit 753b77a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ const DEFAULT_TIMEOUT = 80000;

Stripe.PACKAGE_VERSION = require('../package.json').version;

const utils = require('./utils');
const {determineProcessUserAgentProperties, emitWarning} = utils;

Stripe.USER_AGENT = {
bindings_version: Stripe.PACKAGE_VERSION,
lang: 'node',
lang_version: process.version,
platform: process.platform,
publisher: 'stripe',
uname: null,
typescript: false,
...determineProcessUserAgentProperties(),
};

/** @private */
Expand All @@ -44,8 +46,6 @@ const ALLOWED_CONFIG_PROPERTIES = [
];

const EventEmitter = require('events').EventEmitter;
const utils = require('./utils');
const {emitWarning} = utils;

Stripe.StripeResource = require('./StripeResource');
Stripe.resources = resources;
Expand Down
9 changes: 9 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ const utils = (module.exports = {

return n;
},

determineProcessUserAgentProperties: () => {
return typeof process === 'undefined'
? {}
: {
lang_version: process.version,
platform: process.platform,
};
},
});

function emitWarning(warning) {
Expand Down
15 changes: 15 additions & 0 deletions test/stripe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ describe('Stripe Module', function() {
})
).to.eventually.have.property('lang', 'node'));

it('Should return platform and version in the serialized user agent JSON object', async () => {
// Check that the testing environment actually has a process global.
expect(process.version).to.not.be.empty;
expect(process.platform).to.not.be.empty;

const userAgent = await new Promise((resolve, reject) => {
stripe.getClientUserAgent((c) => {
resolve(JSON.parse(c));
});
});

expect(userAgent).to.have.property('lang_version', process.version);
expect(userAgent).to.have.property('platform', process.platform);
});

it('Should include whether typescript: true was passed, respecting reinstantiations', () => {
return new Promise((resolve) => resolve())
.then(() => {
Expand Down

0 comments on commit 753b77a

Please sign in to comment.