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

Cannot set property length of [object Object] #79

Closed
oroce opened this issue Sep 16, 2015 · 25 comments
Closed

Cannot set property length of [object Object] #79

oroce opened this issue Sep 16, 2015 · 25 comments
Labels

Comments

@oroce
Copy link

oroce commented Sep 16, 2015

Buffer 3.5.0 breaks in Chrome (45.0.2454.85 (Official Build) (64-bit))

What I'm getting is

Uncaught TypeError: Cannot set property length of [object Object] which has only a getter

It happens when buffer tries to set .prototype.length to undefined, here: https://github.com/feross/buffer/blob/master/index.js#L372

I discovered this while I was rolling out our app's new version. Later I'm gonna try to put together a small demo.

(I'm using browserify 11.11.0 which uses buffer@3.5.0)

@byronanderson
Copy link

+1, causing me to need to start using npm shrinkwrap to get buffer 3.4.3

@CrisFavero
Copy link

+1

@feross
Copy link
Owner

feross commented Sep 16, 2015

Can someone share code to reproduce this error? I can't trigger it and the whole test suite passes on every supported browser...

@dcousens
Copy link
Collaborator

Assuming this bug can be confirmed, what is the best course of action anyway?

@dcousens dcousens added the bug label Sep 16, 2015
@feross
Copy link
Owner

feross commented Sep 16, 2015

Depends on the cause. We can either try to fix it, or revert the instanceof improvement I shipped in v3.5.0.

@CrisFavero
Copy link

The code i had that was causing this to show up was while using webpack and was unfortunately several libraries deep where it was triggered so i don't have a simple test case I know the file that directly had threw the error while using a buffer was https://github.com/primus/primus/blob/master/transformer.js likely not very helpful but you might be able to see a use case that would break it which is more obvious to you having written the buffer library

@nolanlawson
Copy link
Collaborator

@marioatlp Can you provide a stacktrace so we know where transformer was calling Buffer?

@oroce
Copy link
Author

oroce commented Sep 17, 2015

haha it seems I could reproduce the issue. It's not buffer but babelify when it is used as global transform breaks buffer.

Showcase: (check the console)

Code: https://github.com/purposeindustries/buffer-bug79

ps: you can solve the global transform by simply ignoring buffer: browserify index.js --global-transform [ babelify --ignore buffer ] -o dist.js

@jlbruno
Copy link

jlbruno commented Sep 21, 2015

Looks like we're getting the same issue.
Also using webpack and babel together.

@oroce
Copy link
Author

oroce commented Sep 21, 2015

@jlbruno in webpack: add node_modules to the exclude list

module: {
  loaders: [
    {
      test: /\.jsx?$/,
      exclude: /node_modules/,
      loader: 'babel'
    }
  ]
}

@CrisFavero
Copy link

@oroce That solution is only helpful if the issue is a .jsx file in node_modules my issue was in standard JS. an alternative work around until this is fixed is to add to package.json dependencies section
"buffer": "3.4.3",
and to add to your webpack config file:

plugins: [
        new webpack.ContextReplacementPlugin(/buffer/, require('buffer'))
    ]

@jlbruno
Copy link

jlbruno commented Sep 21, 2015

Ok, thanks....looks like 3 solutions have worked. Shrinkwrap (locking buffer to 3.4.3 by editing node-libs-browser package.json), excluding node_modules, and adding buffer at a top level dependency locked to v 3.4.3.

Thanks all. Time to figure out which is the least of the evils.

@lostrouter
Copy link

found one other solution for webpack based on

ps: you can solve the global transform by simply ignoring buffer: browserify index.js --global-transform [ babelify --ignore buffer ] -o dist.js

loaders: [
    { test: /\.jsx$/, loader: 'babel?stage=0&ignore=buffer' }
]

@nelix
Copy link

nelix commented Oct 27, 2015

@lostrouter I would recommend not running babel on third party code, as often the transforms can break code that is unaware of es6. Such as making modules option strict by default.

@Ryuno-Ki
Copy link

I cross-referenced this issue against browserify, too. They should at least be aware of this issue.

Since I use traceur instead of babel, my solution was traceur --script ajar.js --out out/ajar.es6.js && browserify out/ajar.es6.js --ignore buffer -o out/ajar.js, whereby ajar.js is the main entry point in that project.

@mikemaccana
Copy link

3.4.3 still has the broken code:

$ grep version node_modules/buffer/package.json
  "version": "3.4.3"
$ grep 'length = undefined' node_modules/buffer/index.js
Buffer.prototype.length = undefined

@JSteunou
Copy link

Same here using webpack with https://github.com/mistadikay/strict-loader

<exception>: TypeError: Cannot set property length of [object Object] which has only a getter at Object.eval (webpack:///../~/webpack/~/node-libs-browser/~/buffer/index.js?:376:25) at eval (webpack:///../~/webpack/~/node-libs-browser/~/buffer/index.js?:1548:30) at ...
BP: undefined
Buffer: Buffer(arg)
INVALID_BASE64_RE: undefined
MAX_ARGUMENTS_LENGTH: undefined
SlowBuffer: SlowBuffer(subject, encoding)
allocate: allocate(that, length)
asciiSlice: asciiSlice(buf, start, end)
asciiToBytes: asciiToBytes(str)
asciiWrite: asciiWrite(buf, string, offset, length)
base64: Object
base64Slice: base64Slice(buf, start, end)
base64ToBytes: base64ToBytes(str)
base64Write: base64Write(buf, string, offset, length)
base64clean: base64clean(str)
binarySlice: binarySlice(buf, start, end)
binaryWrite: binaryWrite(buf, string, offset, length)
blitBuffer: blitBuffer(src, dst, offset, length)
byteLength: byteLength(string, encoding)
checkIEEE754: checkIEEE754(buf, value, offset, ext, max, min)
checkInt: checkInt(buf, value, offset, ext, max, min)
checkOffset: checkOffset(offset, ext, length)
checked: checked(length)
decodeCodePointsArray: decodeCodePointsArray(codePoints)
fromArray: fromArray(that, array)
fromArrayBuffer: fromArrayBuffer(that, array)
fromArrayLike: fromArrayLike(that, array)
fromBuffer: fromBuffer(that, buffer)
fromJsonObject: fromJsonObject(that, object)
fromNumber: fromNumber(that, length)
fromObject: fromObject(that, object)
fromString: fromString(that, string, encoding)
fromTypedArray: fromTypedArray(that, array)
global: Window
hexSlice: hexSlice(buf, start, end)
hexWrite: hexWrite(buf, string, offset, length)
ieee754: Object
isArray: isArray()
kMaxLength: kMaxLength()
objectWriteUInt16: objectWriteUInt16(buf, value, offset, littleEndian)
objectWriteUInt32: objectWriteUInt32(buf, value, offset, littleEndian)
rootParent: Object
slowToString: slowToString(encoding, start, end)
stringtrim: stringtrim(str)
this: Object
toHex: toHex(n)
typedArraySupport: typedArraySupport()
ucs2Write: ucs2Write(buf, string, offset, length)
utf8Slice: utf8Slice(buf, start, end)
utf8ToBytes: utf8ToBytes(string, units)
utf8Write: utf8Write(buf, string, offset, length)
utf16leSlice: utf16leSlice(buf, start, end)
utf16leToBytes: utf16leToBytes(str, units)
writeDouble: writeDouble(buf, value, offset, littleEndian, noAssert)
writeFloat: writeFloat(buf, value, offset, littleEndian, noAssert)

@JSteunou
Copy link

Ok I ran the test suit inside the same browser I have the bug in, and it is all good. Maybe someone (webpack?) is doing a freeze on the object, or something else.

@JSteunou
Copy link

Find it, don't add / use strict mode on Buffer.js mistadikay/strict-loader#5 (comment)

@scottburch
Copy link

I did a horrible patch in webpack for this issue that others might be able to use:

---- strict-stripper.js ------
module.exports = source => source.replace(/['"]use strict['"]/, '');

---- in webpack.config.js (loaders section) ------
{test: /buffer/, loader: ${__dirname}/strict-stripper.js},

@nolanlawson
Copy link
Collaborator

So I take it the steps to fix this bug are:

  1. Add "use strict" to the top of index.js
  2. Re-run the tests
  3. Fix stuff until it works

In general, it looks like this could be fixed by simply hoisting the function Buffer () { ... } declaration to the top of the file.

@nolanlawson
Copy link
Collaborator

I can confirm that I can reproduce this by just adding 'use strict' to the top of index.js and then running npm run test-browser-local. I'm not sure how to fix it.

@nolanlawson
Copy link
Collaborator

^ fixed!

@JSteunou
Copy link

nicely done, thank you!

feross added a commit that referenced this issue Dec 25, 2015
Proper strict mode support. Fixes #79
@feross
Copy link
Owner

feross commented Dec 25, 2015

Nicely done! Fix released as 3.6.0. And just like that, we're down to 0 open issues and 0 open PRs! 👍

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

No branches or pull requests