-
-
Notifications
You must be signed in to change notification settings - Fork 730
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
Is there an alternative to Buffer.isBuffer? #39
Comments
If you change the check from |
Just having a Played around a little, and this seems to fool browserify, but I wonder for how long? Also not sure about correctness across all platforms - but maybe this is worth investigating further? if ("Buffer" in global) {
return global["Buffer"].isBuffer(obj);
} |
I assume you used string keys because you tried testing for |
Nah, I used the string key because I'm that badass :) Indeed, the non-string approach works as well. |
Regarding that commit... Browserify DOES get fooled this way, but we need to keep in mind, that browsers don't define Using exports.isBuffer = function (obj) {
return (function () {
if (typeof this.Buffer !== 'undefined') {
return this.Buffer.isBuffer(obj);
}
else {
return false;
}
}())
}; The above seems to work correctly. I can probably even come up and PR some unit tests for this... Just need to also find an easy way to run them in the browser... |
OK, I think it's worth reverting commit 613b50a and adding Now, technically, I think, this would be a breaking change? I wonder if anyone at all relies on this behavior [passing |
@dymonaz if we revert 613b50a, and did If someone is not using Buffer elsewhere in their browserify application, then there is no change. If they are, then Buffer is not undefined, so there is no change. I'm pretty sure the only difference then is that we aren't going to optimistically require buffer, which is a win. I was just about to submit (a different and inferior) fix to this problem, but this one looks better to me, and get's a big 👍 from me. |
Not exactly. If someone is using After the change is made, browserify will not do the magic, therefore This needs some testing... |
Ah, gotcha, because browserify doesn't make Buffer a global it just adds it to the arg list of the modules function wrapper? Hmm Philip Roberts
|
@nlf how gross would it be to do something like this? exports.isBuffer = function (obj) {
return obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj);
}; |
I love Javascript :) |
Kind of gross, but seems like it might be the simplest solution in this case. |
(facepalm) I just ran this as a test with our code... and sure - when there's no |
OK, so here's the node way of checking for So, the |
Closed via #41 |
FYI, $ browserify -r buffer | uglifyjs -c -m | wc -c
16435
$ browserify -r buffer | uglifyjs -c -m | gzip | wc -c
5267 In a large app, it's probably not worth worrying about. For a little module like this one, I totally get the desire to avoid unnecessary dependencies. |
@dymonaz Regarding the "The Buffer constructor returns instances of |
Thanks for pitching in! What would go wrong if you overwrote |
@dymonaz have you tested with qs@2.2.5 or greater? I think this problem should be solved already. Does it not work? |
Ah, I see. Let me know what you find. I'll reopen this issue for now so I remember it's still not fixed. |
I can confirm - on 2.3.1, after removing all the hacks I had in place, the @feross would you care to set |
@dymonaz Sure, I don't mind doing that. I'll just need to confirm that there's no effect on performance. |
Change made for ljharb/qs#39 (comment)
@dymonaz I made the It's published as |
It's available in browserify immediately, because semver. |
Thank you, will test and report! |
All good! |
Sweet!
|
Now published as a standalone module in case others want to use the same trick easily: https://www.npmjs.org/package/is-buffer |
Change made for ljharb/qs#39 (comment)
https://github.com/hapijs/qs/blob/master/lib/utils.js#L134 contains a call to
Buffer.isBuffer
. This means that whenqs@2.x
is browserified (we're still on 0.6 for the client side...), the whole Buffer module is pulled in - which adds an extra 40kb (~20kb minified) of code in the bundle - and all of it for just this one function, which is only used once.I'll try to see if there's a workaround on the browserify side of things, but the question still stands...The workaround in browserify is to create a fake file which exports
Buffer.isBuffer = () => false
andexpose
that as"buffer"
. It would still be nice to not have to use this workaround...The text was updated successfully, but these errors were encountered: