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

Fix User-Agent version bug in Node #132

Merged
merged 1 commit into from
Aug 7, 2019
Merged

Conversation

EvanHahn
Copy link
Contributor

@EvanHahn EvanHahn commented Aug 6, 2019

tl;dr: in Node, we were sending User-Agent: Airtable.js/undefined. This fixes that.

This line "computes" the user agent:

var userAgent = 'Airtable.js/' + process.env.npm_package_version;

This actually has two code paths:

  1. In the browser, we transformed process.env.npm_package_version into the real package version. As far as I know, this bug never manifests itself in browsers.
  2. In Node, process.env.npm_package_version is a value magically set by npm. However, when I tested this with a quick one-off script, that value was undefined. This explains the bug.

There are a number of ways to fix this problem:

  1. Hard-code the version into the source code. While this header is tested, this approach would require manual changes, which could be brittle if you forget to run npm test before releasing.
  2. Figure out why process.env.npm_package_version isn't being set and make sure it's set reliably. Documentation for these values is scant (it's unclear whether the value changes depending on the subpackage you're in or if it reflects the top-level package), and it didn't work even in my simple case, so I was inclined to steer clear of this.
  3. Create a Node build in addition to a browser build. I felt like this solution was overkill, among its other issues.
  4. Use require('./package.json').version. This works fine in Node, but increases the size of the browser build and includes unnecessary package info. (I've also had some issues requiring package.json in the browser in the past, though I can't remember what they were.)
  5. Use require('./package.json').version in Node and process.env.npm_package_version in the browser. This is what I went with.

npm test passes after this change.

*tl;dr: in Node, we were sending `User-Agent: Airtable.js/undefined`. This fixes that.*

[This line](https://github.com/Airtable/airtable.js/blob/d9bf0f68f58ec82ffd63a206866229c61801a8c7/lib/run_action.js#L26) "computes" the user agent:

```js
var userAgent = 'Airtable.js/' + process.env.npm_package_version;
```

This actually has _two_ code paths:

1. In the browser, [we transformed `process.env.npm_package_version`](https://github.com/Airtable/airtable.js/blob/d9bf0f68f58ec82ffd63a206866229c61801a8c7/gruntfile.js#L15-L18) into the real package version. As far as I know, this bug never manifests itself in browsers.
2. In Node, `process.env.npm_package_version` is a value [magically set by npm](https://docs.npmjs.com/misc/scripts#packagejson-vars). However, when I tested this with [a quick one-off script](https://gist.github.com/EvanHahn/e0f712798b1af53af4c2478bc72ab608), that value was `undefined`. This explains the bug.

There are a number of ways to fix this problem:

1. Hard-code the version into the source code. While [this header is tested](https://github.com/Airtable/airtable.js/blob/d9bf0f68f58ec82ffd63a206866229c61801a8c7/test/base.test.js#L30), this approach would require manual changes, which could be brittle if you forget to run `npm test` before releasing.
2. Figure out why `process.env.npm_package_version` isn't being set and make sure it's set reliably. Documentation for these values is scant (it's unclear whether the value changes depending on the subpackage you're in or if it reflects the top-level package), and it didn't work even in my simple case, so I was inclined to steer clear of this.
3. Create a Node build in addition to a browser build. I felt like this solution was overkill, among its other issues.
4. Use `require('./package.json').version`. This works fine in Node, but increases the size of the browser build and includes unnecessary package info. (I've also had some issues requiring `package.json` in the browser in the past, though I can't remember what they were.)
5. Use `require('./package.json').version` in Node and `process.env.npm_package_version` in the browser. This is what I went with.

`npm test` passes after this change.
@EvanHahn EvanHahn force-pushed the fix_useragent_version_in_node branch from 18785bb to 09b4544 Compare August 6, 2019 19:15
@EvanHahn EvanHahn requested review from kasrak and jbbakst August 6, 2019 19:15
@EvanHahn EvanHahn marked this pull request as ready for review August 6, 2019 19:15
Copy link
Contributor

@kasrak kasrak left a comment

Choose a reason for hiding this comment

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

Thanks!

@EvanHahn EvanHahn merged commit 490f902 into master Aug 7, 2019
@EvanHahn EvanHahn deleted the fix_useragent_version_in_node branch August 7, 2019 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants