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

feat: add maxLength to allow controlling max buffer length during decode #9

Merged
merged 7 commits into from
Apr 12, 2017

Conversation

dryajov
Copy link
Contributor

@dryajov dryajov commented Apr 8, 2017

@dignifiedquire adding this since we need to control the max msg size in circuit.

FYI, some tests are failing, but they were failing before my changes, I haven't yet taken a look at it, will do soon, but you might know whats going on.

@daviddias daviddias mentioned this pull request Apr 8, 2017
53 tasks
@dryajov
Copy link
Contributor Author

dryajov commented Apr 8, 2017

One more thing, I have tentatively set maxLength to 1024 as a default, but we might want to increase that to something larger if its too low of a number.

src/decode.js Outdated
@@ -10,6 +10,7 @@ exports.decodeFromReader = decodeFromReader

const MSB = 0x80
const isEndByte = (byte) => !(byte & MSB)
const MAX_LENGTH = 1024
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go currently used 4mb as the max length, so lets make that the default

@dignifiedquire
Copy link
Owner

@dryajov I've updated master to have passing tests again. Please rebase onto it so I can merge

@dryajov
Copy link
Contributor Author

dryajov commented Apr 11, 2017

@dignifiedquire thanks, I'll update the length!

@dryajov
Copy link
Contributor Author

dryajov commented Apr 11, 2017

@dignifiedquire think this is ready now?

README.md Outdated
@@ -70,6 +70,7 @@ Returns a pull-stream through.
- `opts: Object`, optional
- `fixed: false`:
- `bytes: 4`: If `fixed` is `true` this is the amount of bytes used for the prefix.
- `maxLength`: If provided, will not decode messages longer than the size specified
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should mention the default length

encoded
).to.be.eql([
Buffer.concat([
new Buffer(varint.encode('hello '.length)),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use Buffer.from instead

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or Buffer.alloc(number)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I just make the change everywhere in the tests while I'm at it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, also that will require using safe-buffer, for node 4 compat

@dryajov
Copy link
Contributor Author

dryajov commented Apr 11, 2017

@dignifiedquire any reason to not just do require('safe-buffer') and let it polyfill automatically here - https:/dignifiedquire/pull-length-prefixed/blob/master/src/encode.js#L3?

@dryajov
Copy link
Contributor Author

dryajov commented Apr 11, 2017

tests are failing on node 4 even tho I'm pulling safe-buffer, looking into it.

@dryajov
Copy link
Contributor Author

dryajov commented Apr 11, 2017

Seems like in node 4, if the fill value is something other than a string and no encoding is explicitly specified, then it will not encode it with anything and simply fill in the raw bytes - https:/nodejs/node/blob/v4.x/lib/buffer.js#L109, which is different from how later versions behave, where buffer is also defaulted to 'utf8'.

package.json Outdated
@@ -32,6 +32,7 @@
},
"homepage": "https:/dignifiedquire/pull-length-prefixed#readme",
"dependencies": {
"mocha": "^3.2.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for catching that, leftovers from my tests figuring out what's wrong with node 4.

@dignifiedquire dignifiedquire merged commit 7810ade into dignifiedquire:master Apr 12, 2017
@dryajov
Copy link
Contributor Author

dryajov commented May 11, 2017

@dignifiedquire can we get this published to npm please 😄? This is needed for libp2p/js-libp2p-circuit#9

@dignifiedquire
Copy link
Owner

sorry for the delay, published a new version @dryajov

@dryajov dryajov mentioned this pull request Aug 16, 2017
48 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants