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

Gracefully handle a missing subprocess module #775

Merged
merged 2 commits into from
Jan 23, 2020
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
20 changes: 19 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
'use strict';

const EventEmitter = require('events').EventEmitter;
const exec = require('child_process').exec;
const qs = require('qs');
const crypto = require('crypto');

const hasOwn = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);

// Certain sandboxed environments (our known example right now are CloudFlare
// Workers) may make `child_process` unavailable. Because `exec` isn't critical
// to the operation of stripe-node, we handle this unavailability gracefully.
let exec = null;
try {
exec = require('child_process').exec;
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}

const OPTIONS_KEYS = [
'apiKey',
'idempotencyKey',
Expand Down Expand Up @@ -340,6 +351,13 @@ const utils = (module.exports = {
* This unifies that interface.
*/
safeExec: (cmd, cb) => {
// Occurs if we couldn't load the `child_process` module, which might
// happen in certain sandboxed environments like a CloudFlare Worker.
if (utils._exec === null) {
cb(new Error('exec not available'), null);
return;
}

try {
utils._exec(cmd, cb);
} catch (e) {
Expand Down
16 changes: 16 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,22 @@ describe('utils', () => {
utils.safeExec('hello', myCb);
expect(calls).to.deep.equal([[myErr, null]]);
});

it('handles being unable to require `child_process`', () => {
utils._exec = null;

var actualErr = null;
var actualRes = null;
function myCb(err, res) {
actualErr = err;
actualRes = res;
}
utils.safeExec('hello', myCb);
expect(actualErr.toString()).to.equal(
new Error('exec not available').toString()
);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd love if someone who is familiar with Node convention could check whether they like this. Basically I found that errors are not treated as equal by Chai even if they have the same error message, so fell back to this toString() solution, but there might be a better way.

Copy link
Contributor

Choose a reason for hiding this comment

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

This works. We should upgrade our testing library to Jest.

expect(actualRes).to.equal(null);
});
});

describe('flattenAndStringify', () => {
Expand Down