-
Notifications
You must be signed in to change notification settings - Fork 205
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
Packaging: Don't use global Buffer
object
#733
Conversation
'shared-node-browser': true, | ||
}, | ||
extends: ['airbnb-base', 'prettier', 'plugin:import/typescript'], | ||
extends: [ | ||
'airbnb-base', | ||
'prettier', | ||
'plugin:import/typescript', | ||
'eslint:recommended', | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes aren't explicitly necessary, but I thought I would include them since I'm making changes to our eslint settings.
shared-node-browser
causes eslint to only recognize global variables present in both node and browser contexts, instead of either.- I removed
mocha
from the global list of environments in favor of adding/* eslint-env mocha */
at the top of each mocha test file. This way we won't accidentally reference mocha global variables from our production files. eslint:recommended
contains a set of recommended rules. It only caused one new issue in our codebase (which I fixed by configuringno-constant-condition
below), so I figured it was a good idea to include.
Buffer
objectBuffer
object
FWIW I just tested against a fresh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shipit!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The buffer package recommends importing buffer with a trailing slash - do we also want to do that here?
@algochoi good question. In this case we want to import just In fact I wanted to specify this more explicitly by importing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Summary
This PR makes the SDK no longer rely on a global
Buffer
object. See #708 for more context and for why this caused undesirable behavior.I'm proud to report that this PR allows the SDK to be installed in a
create-react-app
project without ejecting or any additional configuration changes.*Specifics
My comment in #730 (comment) expands on how our previous behavior causes problems for
create-react-app
.Now, instead of using the global
Buffer
variable in our code and relying on a webpack plugin to set it up properly, we explicitly importBuffer
from thebuffer
package. On node, this gives us the same thing as the globalBuffer
variable, and on the browser, this gives us our in-browser buffer package.In order to enforce this change in our repo, I made some changes to our eslint rules. The new rule
no-restricted-globals
causes eslint to fail if it detects any reference to the globalBuffer
variable, which should catch any problems going forward.*There's one situation which unfortunately we can't solve in this SDK. Webpack v4 projects forcibly import an old version of the npm
buffer
package. Despite npm installing the higher version ofbuffer
that we've specified in our package.json, webpack gives us the old one. I've given up trying to get around this for now, and I've changed the offending code causing #730 to use DataViews instead of Buffer. This solves the immediate problem, but it's possible other issues may arise from using an older version ofbuffer
with our library.I've opened #734 as a possible longer term solution to this problem.