Comparing version 0.3.1 to 0.3.2
32
Int64.js
@@ -48,2 +48,3 @@ // Int64.js | ||
* new Int64(buffer[, offset=0]) - Existing Buffer with byte offset | ||
* new Int64(Uint8Array[, offset=0]) - Existing Uint8Array with a byte offset | ||
* new Int64(string) - Hex string (throws if n is outside int64 range) | ||
@@ -57,2 +58,9 @@ * new Int64(number) - Number (throws if n is outside int64 range) | ||
this.offset = a2 || 0; | ||
} else if (Object.prototype.toString.call(a1) == '[object Uint8Array]') { | ||
// Under Browserify, Buffers can extend Uint8Arrays rather than an | ||
// instance of Buffer. We could assume the passed in Uint8Array is actually | ||
// a buffer but that won't handle the case where a raw Uint8Array is passed | ||
// in. We construct a new Buffer just in case. | ||
this.buffer = new Buffer(a1); | ||
this.offset = a2 || 0; | ||
} else { | ||
@@ -199,2 +207,26 @@ this.buffer = this.buffer || new Buffer(8); | ||
/** | ||
* Returns the int64's 8 bytes in a buffer. | ||
* | ||
* @param {bool} [rawBuffer=false] If no offset and this is true, return the internal buffer. Should only be used if | ||
* you're discarding the Int64 afterwards, as it breaks encapsulation. | ||
*/ | ||
toBuffer: function(rawBuffer) { | ||
if (rawBuffer && this.offset === 0) return this.buffer; | ||
var out = new Buffer(8); | ||
this.buffer.copy(out, 0, this.offset, this.offset + 8); | ||
return out; | ||
}, | ||
/** | ||
* Copy 8 bytes of int64 into target buffer at target offset. | ||
* | ||
* @param {Buffer} targetBuffer Buffer to copy into. | ||
* @param {number} [targetOffset=0] Offset into target buffer. | ||
*/ | ||
copy: function(targetBuffer, targetOffset) { | ||
this.buffer.copy(targetBuffer, targetOffset || 0, this.offset, this.offset + 8); | ||
}, | ||
/** | ||
* Pretty output in console.log | ||
@@ -201,0 +233,0 @@ */ |
@@ -11,3 +11,7 @@ { | ||
"main" : "./Int64.js", | ||
"version" : "0.3.1" | ||
"version" : "0.3.2", | ||
"repository": | ||
{ "type" : "git", | ||
"url" : "https://github.com/broofa/node-int64" | ||
} | ||
} |
@@ -70,2 +70,10 @@ JavaScript Numbers are represented as [IEEE 754 double-precision floats](http://steve.hollasch.net/cgindex/coding/ieeefloat.html). Unfortunately, this means they lose integer precision for values beyond +/- 2^^53. For projects that need to accurately handle 64-bit ints, such as [node-thrift](https://github.com/wadey/node-thrift), a performant, Number-like class is needed. Int64 is that class. | ||
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0] | ||
// Pull out into a buffer | ||
> new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).toBuffer() | ||
<Buffer 12 34 56 78 9a bc de f0> | ||
// Or copy into an existing one (at an offset) | ||
> var buf = new Buffer(1024); | ||
> new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).copy(buf, 512); | ||
``` |
27
test.js
@@ -32,1 +32,28 @@ var assert = require('assert'); | ||
} | ||
// Test buffer output | ||
var intUnderTest = new Int64(0xfffaffff, 0xfffff700); | ||
var expectedBuffer = new Buffer([0xff, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xf7, 0x00]); | ||
console.log('Testing '+intUnderTest.toOctetString()+' as Buffer'); | ||
assert.equal(intUnderTest.toBuffer().toString('hex'), expectedBuffer.toString('hex')); | ||
var targetBuffer = new Buffer(8); | ||
intUnderTest.copy(targetBuffer); | ||
assert.equal(targetBuffer.toString('hex'), expectedBuffer.toString('hex')); | ||
// Test construction from existing buffer with offset, and buffer outputs on same. | ||
var sourceBuffer = new Buffer(16); | ||
sourceBuffer.writeUInt32BE(0xfffaffff, 2); | ||
sourceBuffer.writeUInt32BE(0xfffff700, 6); | ||
intUnderTest = new Int64(sourceBuffer, 2); | ||
assert.equal(intUnderTest.toBuffer().toString('hex'), expectedBuffer.toString('hex')); | ||
targetBuffer = new Buffer(16); | ||
intUnderTest.copy(targetBuffer, 4); | ||
assert.equal(targetBuffer.slice(4, 12).toString('hex'), expectedBuffer.toString('hex')); | ||
console.log(new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).toBuffer()); | ||
console.log(new Int64(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).toBuffer()); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
12690
253
79
0
0