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

Update user agent computation to handle environments without process. #1219

Merged
merged 1 commit into from
Aug 16, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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