fix Buffers.skip in the browser

This commit is contained in:
Manuel Araoz 2014-04-10 18:52:13 -03:00
parent b58d5c5084
commit 3cbcbd54cb
4 changed files with 17 additions and 9 deletions

View File

@ -9,8 +9,7 @@ exports.patch = function(Buffers) {
} }
var pos = this.pos(i); var pos = this.pos(i);
this.buffers = this.buffers.slice(pos.buf); this.buffers = this.buffers.slice(pos.buf);
this.buffers[0].length -= pos.offset; this.buffers[0] = new Buffer(this.buffers[0].slice(pos.offset));
this.buffers[0].offset += pos.offset;
this.length -= i; this.length -= i;
}; };
}; };

View File

@ -14,6 +14,7 @@ requireWhenAccessed('bignum', 'bignum');
requireWhenAccessed('base58', 'base58-native'); requireWhenAccessed('base58', 'base58-native');
requireWhenAccessed('bufferput', 'bufferput'); requireWhenAccessed('bufferput', 'bufferput');
requireWhenAccessed('buffertools', 'buffertools'); requireWhenAccessed('buffertools', 'buffertools');
requireWhenAccessed('Buffers.monkey', './Buffers.monkey');
requireWhenAccessed('config', './config'); requireWhenAccessed('config', './config');
requireWhenAccessed('const', './const'); requireWhenAccessed('const', './const');
requireWhenAccessed('Deserialize', './Deserialize'); requireWhenAccessed('Deserialize', './Deserialize');

View File

@ -101,6 +101,9 @@ var createBitcore = function(opts) {
b.require(opts.dir + 'base58-native', { b.require(opts.dir + 'base58-native', {
expose: 'base58-native' expose: 'base58-native'
}); });
b.require(opts.dir + 'buffers', {
expose: 'buffers'
});
b.require('./' + opts.dir + 'bitcore', { b.require('./' + opts.dir + 'bitcore', {
expose: 'bitcore' expose: 'bitcore'
}); });

View File

@ -17,6 +17,8 @@ var Address = bitcore.Address;
var networks = bitcore.networks; var networks = bitcore.networks;
var WalletKey = bitcore.WalletKey; var WalletKey = bitcore.WalletKey;
var Buffers = require('buffers'); var Buffers = require('buffers');
var m = bitcore['Buffers.monkey'] || require('../Buffers.monkey');
m.patch(Buffers);
describe('Miscelaneous stuff', function() { describe('Miscelaneous stuff', function() {
it('should initialze the config object', function() { it('should initialze the config object', function() {
@ -53,25 +55,28 @@ describe('Miscelaneous stuff', function() {
var bufs; var bufs;
beforeEach(function() { beforeEach(function() {
bufs = new Buffers(); bufs = new Buffers();
bufs.push(new Buffer([1, 2, 3])); bufs.push(new Buffer('aaaaaa', 'hex'));
bufs.push(new Buffer([4, 5, 6, 7])); bufs.push(new Buffer('bbbb', 'hex'));
bufs.push(new Buffer([8, 9, 10])); bufs.push(new Buffer('cc', 'hex'));
}); });
it('should monkey patch the Buffers class', function() { it('should monkey patch the Buffers class', function() {
require('../Buffers.monkey').patch(Buffers);
should.exist(bufs.skip); should.exist(bufs.skip);
}); });
it('should work for 0', function() { it('should work for 0', function() {
bufs.skip(0); bufs.skip(0);
bufs.toBuffer().toHex().should.equal('0102030405060708090a'); bufs.toBuffer().toHex().should.equal('aaaaaabbbbcc');
}); });
it('should work for length', function() { it('should work for length', function() {
bufs.skip(bufs.length); bufs.skip(bufs.length);
bufs.toBuffer().toHex().should.equal(''); bufs.toBuffer().toHex().should.equal('');
}); });
it('should work for middle values', function() { it('should work for middle values', function() {
bufs.skip(5); bufs.skip(4);
bufs.toBuffer().toHex().should.equal('060708090a'); bufs.toBuffer().toHex().should.equal('bbcc');
bufs.skip(1);
bufs.toBuffer().toHex().should.equal('cc');
bufs.skip(1);
bufs.toBuffer().toHex().should.equal('');
}); });
}); });
// bignum // bignum