Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

assigning Buffer in repl throws error #8588

Closed
meandavejustice opened this issue Oct 21, 2014 · 8 comments
Closed

assigning Buffer in repl throws error #8588

meandavejustice opened this issue Oct 21, 2014 · 8 comments

Comments

@meandavejustice
Copy link

in a repl if I do

var Buffer = require('buffer');

I get this error

_stream_writable.js:171
  if (Buffer.isBuffer(chunk))
             ^
TypeError: Object #<Object> has no method 'isBuffer'
    at WriteStream.Writable.write (_stream_writable.js:171:14)
    at WriteStream.Socket.write (net.js:615:40)
    at finish (repl.js:291:27)
    at REPLServer.self.eval (repl.js:122:7)
    at repl.js:249:20
    at REPLServer.self.eval (repl.js:122:7)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)

I understand that core modules are included in repl globally, But this behavior seems wrong.

v.0.10.32

@meandavejustice
Copy link
Author

cc/ @chrisdickinson

@odeke-em
Copy link

I've dug through the source. This is because 'isBuffer' was declared as a static method for Buffer, and never exposed, this is on https://github.com/joyent/node/blob/v0.10.32-release/lib/buffer.js#L277 and yet in the newer releases, util.isBuffer is being used inside _stream_writable.js for that check. Any thoughts on exposing this method or better conforming to the new usage of util.isBuffer?

@indutny
Copy link
Member

indutny commented Oct 22, 2014

This is happening because we are using Buffer through-out all of lib/ files without actually doing require('buffer') in them. I guess something like these at the top of all these files should do the trick:

var Buffer = require('buffer').Buffer.

cc @trevnorris

@trevnorris
Copy link

Overriding a documented global is going to lead to issues. Not sure I'd consider this a bug. That being said, I'd okay a patch either replacing Buffer.isBuffer() with util.isBuffer() or what @indutny mentioned.

@odeke-em
Copy link

Roger that. However, doing explicit requires in each of those file won't remedy the name conflict with the global object 'Buffer'.
Also util.isBuffer does not yet exist in util.js before these releases. A simple thought would be to advise avoiding giving names to variables that clash with those in the global namespace.
Irrespective of the requires, as many bug reports could be created by someone getting names of all the objects in the globally present in the repl, and then redefining them.
If a bit of complexity can be tolerated, we can ensure that the globally used objects are non writable with
Object.defineProperty(this, name, {writable: false})
clauses to avoid any future issues of name conflicts within the repl. Let me know what you think.

@indutny
Copy link
Member

indutny commented Oct 22, 2014

@odeke-em I think that we should follow the way that I have proposed :) lib/* should not be using global objects, ideally.

@odeke-em
Copy link

Thank you folks for the good tips. @indutny, kuddos -- I've been able to test this out in code with no more fails, PR with your solution coming up in a few. In deed global objects should not be used in lib/*.

trevnorris pushed a commit that referenced this issue Oct 25, 2014
Fixes usage of global object 'Buffer' in lib/* files by ensuring that
each file does an explicit require('buffer').Buffer.  Previously, when
running a repl, due to usage of global 'Buffer', any redefinition of
Buffer would cause a crash eg var Buffer = {}.

Fixes: #8588
PR-URL: #8603
Reviewed-by: Trevor Norris <[email protected]>
@trevnorris
Copy link

Fixed by 523929c.

mscdex pushed a commit to mscdex/node that referenced this issue Dec 25, 2014
Fixes usage of global object 'Buffer' in lib/* files by ensuring that
each file does an explicit require('buffer').Buffer.  Previously, when
running a repl, due to usage of global 'Buffer', any redefinition of
Buffer would cause a crash eg var Buffer = {}.

Fixes: nodejs#8588
PR-URL: nodejs#8603
Reviewed-by: Trevor Norris <[email protected]>
Fishrock123 pushed a commit to Fishrock123/node that referenced this issue May 22, 2015
Fixes usage of global object 'Buffer' in lib/* files by ensuring that
each file does an explicit require('buffer').Buffer.  Previously, when
running a repl, due to usage of global 'Buffer', any redefinition of
Buffer would cause a crash eg var Buffer = {}.

Fixes: nodejs/node-v0.x-archive#8588
PR-URL: nodejs/node-v0.x-archive#8603
Reviewed-by: Trevor Norris <[email protected]>

Conflicts:
	lib/_stream_readable.js
	lib/_stream_writable.js
	lib/assert.js
	lib/dgram.js
	lib/fs.js
	lib/http.js
	lib/net.js
	lib/readline.js
	lib/tls.js
	lib/zlib.js
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants