Add support for large integers#44
Merged
Merged
Conversation
|
Why not use indutny/bn.js? |
Contributor
Author
I might, after I have explored what else I had in mind, and once I get to the benchmarks on this one, if this one happens. With a type hook system this won't even be necessary, because people would be able to choose their own way of parsing types, if need be. |
355cbb4 to
e8f5665
Compare
Contributor
Author
|
So... it's been a while, but I got around to trying out that idea I had, which is basically a middleware-style way of adding support for other types, i.e.: var assert = require('assert')
var Bencode = require('./index')
var BigNumber = require('bignumber.js')
// OR var BigNumber = require('bn.js')
// The current API still works as before,
// but can't have any new types added to it globally,
// to avoid trashing other things using it (in deduplicated bundles, for example)
Bencode.encode({ something: 'with a value' })
Bencode.decode( buffer )
// Create a new instance of Bencode (which uses it's own set of encoder/decoder),
var bignumBencode = new Bencode()
// Register a type check, encode & decode function for a new type
bignumBencode.use('bignumber', 'number', {
check: function (value) {
return value instanceof BigNumber
},
encode: function (buffers, data) {
buffers.push(new Buffer('i' + data.toString(10) + 'e'))
},
decode: function () {
var end = this.find(0x65)
var number = this.data.toString('ascii', this.position + 1, end)
this.position += end + 1 - this.position
return new BigNumber(number, 10)
}
})
// Profit
var value = new BigNumber('1234567890123456789', 10)
var encoded = bignumBencode.encode(value)
var decoded = bignumBencode.decode(encoded)
assert.deepEqual( value, decoded )It has added some complexity, but the benchmarks surprisingly still show pretty much the same numbers, if not even better ones. I still need to add tests for this though. |
Contributor
|
"Branch protection options saved" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[WIP]: Adds support for large
2^53+integers viabignumber.jsas proposed in #35.Not sure this is the best way of doing it – been thinking about a way to hook certain types or define custom ones, similiar to the mechanic
msgpackuses – which I'll attempt to explore as well.Closes #35