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 'buffer' to v5.x #1678

Merged
merged 2 commits into from Jan 25, 2017
Merged

Update 'buffer' to v5.x #1678

merged 2 commits into from Jan 25, 2017

Conversation

feross
Copy link
Member

@feross feross commented Jan 24, 2017

Browser support

buffer v5.x drops support for IE8-10.

This allows us to remove the Object implementation and rely on a single
fast implementation based on Typed Arrays, greatly simplifying the maintanence of the
buffer package.

Typed arrays are supported by 97.08% of the U.S. browser market and 92.02% of the
global browser market. Source: http://caniuse.com/#search=typed This number
continues to increase at a steady rate each month.

If IE8-10 support is critical to your web app, you can continue to rely on
browserify v13.

Bundle size

This change shrinks the size of buffer modestly.

55.1kb -> 52.1kb (full size, with comments)
7.0kb -> 6.7kb (minified, gzipped)

Bug fixes

buffer v5.x contains several important fixes including:

Semver

This should be released as semver major.

@ghost
Copy link

ghost commented Jan 25, 2017

This looks good to me. Another option for users would be to depend on the buffer package explicitly or to do:

npm install buffer@^4
browserify -r buffer/:buffer main.js > bundle.js

Users can also include a typed array polyfill: https://github.com/inexorabletash/polyfill/blob/master/typedarray.js

buffer v5 requires Uint8Array to be defined. apparently it is
not defined by default in node v0.10.
@feross feross merged commit d7ea3dd into master Jan 25, 2017
@feross feross deleted the buffer-5 branch January 25, 2017 04:23
@lorenzos
Copy link

lorenzos commented Jan 25, 2017

Sorry to bother you, but I'm not sure if this means IE8-10 compatibility is compromised for every bundle I build using browserify, of it's only when I use require('buffer') in my app (which I don't). Can you clarify, please?

@ghost
Copy link

ghost commented Jan 25, 2017

This patch only affects when your code uses Buffer as a global or when you (or some module you use) does require('buffer').

@feross
Copy link
Member Author

feross commented Jan 25, 2017

@lorenzos One way to quickly find out of you're using Buffer in your app (even indirectly) is to grep your bundle file for a string that appears in buffer/index.js, like "TYPED_ARRAY_SUPPORT".

If running grep "TYPED_ARRAY_SUPPORT" bundle.js returns nothing then buffer is not included in your bundle.js.

@JDvorak
Copy link

JDvorak commented Jan 26, 2017

Is there a recommended solution for those of us who are targeting audiences restricted to those versions of IE (e.g. certain federal agencies of the USA)? Sticking to v13 is only a viable option if hotfixes can be expected if security problems arise.

@feross
Copy link
Member Author

feross commented Jan 26, 2017

@JDvorak Browserify usually runs in development (as a build step), not on production servers, so security issues are unlikely.

To continue using an older version of buffer, you can do the following:

$ npm install buffer@4 --save
$ browserify -r buffer/:buffer main.js > bundle.js

@feross
Copy link
Member Author

feross commented Jan 26, 2017

@JDvorak Out of curiosity, what is the oldest version of IE that government agencies (or your agency) is required to support?

@lorenzos
Copy link

@substack @feross Thank you very much for your explanation, I checked with grep, I do not use it.

@JDvorak
Copy link

JDvorak commented Jan 26, 2017 via email

@tgabi333
Copy link

IE10 supports Uint8Array so it should work on IE10, only IE8 and IE9 is left behind.

@tgabi333
Copy link

Anyway, IE8 support was dropped when introduced Array.isArray method in 13.0.1.

@JDvorak
Copy link

JDvorak commented Jan 30, 2017 via email

@feross
Copy link
Member Author

feross commented Jan 31, 2017

@tgabi333 Unfortunately, IE10's Uint8Array support is buggy, where sometimes slice() returns the wrong length buffer. This gave us trouble to make buffer pass all the tests from Node.js core, so IE10 was actually also using the Object implementation before buffer v5.

I thought about letting IE10 use the typed array implementation now -- since a buggy implementation would be better than none. But IE10 also lacks support for __proto__, which makes it difficult to subclass typed arrays with the methods we need for buffer.

boneskull added a commit to mochajs/mocha that referenced this pull request Sep 27, 2017
boneskull added a commit to mochajs/mocha that referenced this pull request Sep 27, 2017
boneskull added a commit to mochajs/mocha that referenced this pull request Sep 27, 2017
boneskull added a commit to mochajs/mocha that referenced this pull request Sep 28, 2017
boneskull added a commit to mochajs/mocha that referenced this pull request Sep 29, 2017
boneskull added a commit to mochajs/mocha that referenced this pull request Sep 29, 2017
boneskull added a commit to mochajs/mocha that referenced this pull request Sep 29, 2017
@dwighthouse
Copy link

dwighthouse commented Jan 30, 2019

This looks good to me. Another option for users would be to depend on the buffer package explicitly or to do:

npm install buffer@^4
browserify -r buffer/:buffer main.js > bundle.js

@substack How do you accomplish this buffer replacement with buffer 4 when using the Browserify API, not command line? This doesn't appear to work:

var b = browserify({
    entries: testEntry,
    debug: false,
});
b.require('buffer/:buffer');
b.transform('babelify', {
    plugins: [
        ['module-resolver', { root: [rootPath] }],
        'babel-plugin-transform-esm-to-cjs',
    ],
});

It errors out: Error: Cannot find module 'buffer/:buffer'

I'm trying to use Tape with older browsers, which recommends Browserify as the build tool, which thanks to this PR, no longer supports older IEs, as described here.

@ljharb
Copy link
Member

ljharb commented Jan 30, 2019

One way or another, we need to restore IE support of packages using Buffer by default here. What paths are available?

@dwighthouse
Copy link

Also, I hesitate to use the method of adding a global polyfill for Uint8Array or some other type not supported in older browsers, since my library is focused on types themselves. Having these polyfills globally visible could affect the purity of my testing environment, which already falls back when certain types are not detected.

Alternatively, I could increase the complexity of my type testing to check un-polyfillable parts of the types.

@goto-bus-stop
Copy link
Member

goto-bus-stop commented Jan 30, 2019

FWIW, the API equivalent to -r buffer/:buffer is

b.require(require.resolve('buffer/'), { expose: 'buffer' })

@dwighthouse
Copy link

@goto-bus-stop Thank you. This code does indeed cause the older version of Buffer to be used. However, it also causes the start of the built file to contain:

"use strict";

require = function () {
...

Since it's in strict mode, and require isn't already defined in the file, this causes an error: Uncaught ReferenceError: require is not defined.

Changing it manually to use the window reference does work, however:

"use strict";

window.require = function () {
...

However, having to manually go in and do string replacements on built files is a bad precedent.

I don't need to expose buffer externally, only do the reference swap as described above. I'm curious why Browserify would create invalid code here.

@goto-bus-stop
Copy link
Member

goto-bus-stop commented Jan 30, 2019

is your build process adding "use strict" somehow? bare browserify should not do that. browserify's code is not invalid, but it doesn't work in strict mode. it does not add "use strict" to bundles because not every node module expects to be run in strict mode. Modules that do expect it already contain "use strict" at the top, and browserify correctly keeps that in each individual module.

I don't need to expose buffer externally, only do the reference swap as described above.

this is indeed a side effect of the current 'solution' for aliasing, and it's not ideal.

@dwighthouse
Copy link

@goto-bus-stop You are right. One instance of Babel was adding it.

Ouch, ok.

sgilroy pushed a commit to TwineHealth/mocha that referenced this pull request Feb 27, 2019
@ghost
Copy link

ghost commented May 4, 2020

i have the same problem any solution please.i use truffle drizzle-react-native

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants