From 435a78d4899c7f76407b5a670a3ef43ec40ad5e7 Mon Sep 17 00:00:00 2001 From: Dominic Charley-Roy Date: Fri, 13 Aug 2021 15:23:34 -0400 Subject: [PATCH] Update user agent computation to handle environments without process. --- lib/stripe.js | 8 ++++---- lib/utils.js | 9 +++++++++ test/stripe.spec.js | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/stripe.js b/lib/stripe.js index 14751435aa..41586c0653 100644 --- a/lib/stripe.js +++ b/lib/stripe.js @@ -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 */ @@ -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; diff --git a/lib/utils.js b/lib/utils.js index ae4c004730..7eed41defd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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) { diff --git a/test/stripe.spec.js b/test/stripe.spec.js index 366a461416..0f5dd5f7c3 100644 --- a/test/stripe.spec.js +++ b/test/stripe.spec.js @@ -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(() => {