Comparing version 2.3.1 to 2.4.0
122
i64.js
@@ -25,47 +25,57 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var util = require('util'); | ||
var bufrw = require('bufrw'); | ||
var Long = require('long'); | ||
var TYPE = require('./TYPE'); | ||
var errors = require('bufrw/errors'); | ||
var I64RW = bufrw.AtomRW(8, | ||
function readTInt64From(buffer, offset) { | ||
var value = new Buffer(8); | ||
buffer.copy(value, 0, offset, offset + 8); | ||
return new bufrw.ReadResult(null, offset + 8, value); | ||
}, | ||
function writeTInt64Into(value, buffer, offset) { | ||
if (value instanceof Buffer) { | ||
value.copy(buffer, offset, 0, 8); | ||
return new bufrw.WriteResult(null, offset + 8); | ||
} else if (typeof value === 'number') { | ||
buffer.writeInt32BE(0, offset, true); | ||
buffer.writeInt32BE(value, offset + 4, true); | ||
return new bufrw.WriteResult(null, offset + 8); | ||
} else if (Array.isArray(value)) { | ||
return writeArrayInt64Into(value, buffer, offset); | ||
} else if (typeof value === 'string') { | ||
return writeStringInt64Into(value, buffer, offset); | ||
} else if (value && typeof value === 'object') { | ||
return writeObjectInt64Into(value, buffer, offset); | ||
} else { | ||
return bufrw.WriteResult.error(errors.expected(value, | ||
'i64 representation')); | ||
} | ||
}); | ||
// istanbul ignore next | ||
function I64RW() { | ||
} | ||
I64RW.prototype.lengthResult = bufrw.LengthResult.just(8); | ||
I64RW.prototype.byteLength = function byteLength(value) { | ||
var self = this; | ||
return self.lengthResult; | ||
}; | ||
I64RW.prototype.writeInto = function writeInto(value, buffer, offset) { | ||
var self = this; | ||
if (value instanceof Buffer) { | ||
value.copy(buffer, offset, 0, 8); | ||
return new bufrw.WriteResult(null, offset + 8); | ||
} else if (typeof value === 'number') { | ||
buffer.writeInt32BE(value / Math.pow(32), offset, true); | ||
buffer.writeInt32BE(value, offset + 4, true); | ||
return new bufrw.WriteResult(null, offset + 8); | ||
} else if (Array.isArray(value)) { | ||
return self.writeArrayInt64Into(value, buffer, offset); | ||
} else if (typeof value === 'string') { | ||
return self.writeStringInt64Into(value, buffer, offset); | ||
} else if (value && typeof value === 'object') { | ||
return self.writeObjectInt64Into(value, buffer, offset); | ||
} else { | ||
return bufrw.WriteResult.error(errors.expected(value, | ||
'i64 representation')); | ||
} | ||
}; | ||
I64RW.prototype.writeObjectInt64Into = | ||
function writeObjectInt64Into(value, buffer, offset) { | ||
if (typeof value.hi !== 'number') { | ||
if (typeof value.high !== 'number' && typeof value.hi !== 'number') { | ||
return bufrw.WriteResult.error(errors.expected(value, | ||
'{hi, lo} with hi number, or other i64 representation')); | ||
'{hi[gh], lo[w]} with high bits, or other i64 representation')); | ||
} | ||
if (typeof value.lo !== 'number') { | ||
if (typeof value.low !== 'number' && typeof value.lo !== 'number') { | ||
return bufrw.WriteResult.error(errors.expected(value, | ||
'{hi, lo} with lo number, or other i64 representation')); | ||
'{hi[gh], lo[w]} with low bits, or other i64 representation')); | ||
} | ||
// Does not validate range of hi or lo value | ||
buffer.writeInt32BE(value.hi, offset, true); | ||
buffer.writeInt32BE(value.lo, offset + 4, true); | ||
buffer.writeInt32BE(value.high || value.hi, offset, true); | ||
buffer.writeInt32BE(value.low || value.lo, offset + 4, true); | ||
return new bufrw.WriteResult(null, offset + 8); | ||
} | ||
}; | ||
I64RW.prototype.writeArrayInt64Into = | ||
function writeArrayInt64Into(value, buffer, offset) { | ||
@@ -82,4 +92,5 @@ if (value.length !== 8) { | ||
return new bufrw.WriteResult(null, offset + 8); | ||
} | ||
}; | ||
I64RW.prototype.writeStringInt64Into = | ||
function writeStringInt64Into(value, buffer, offset) { | ||
@@ -105,14 +116,49 @@ if (value.length !== 16) { | ||
return new bufrw.WriteResult(null, offset + 8); | ||
}; | ||
function I64LongRW() { | ||
} | ||
// TODO support for reading other supported i64 representations off buffers | ||
// with specified js.type annotation. | ||
function ThriftI64() { } | ||
util.inherits(I64LongRW, I64RW); | ||
ThriftI64.prototype.rw = I64RW; | ||
I64LongRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
var value = Long.fromBits( | ||
buffer.readInt32BE(offset + 4, 4, true), | ||
buffer.readInt32BE(offset, 4, true) | ||
); | ||
return new bufrw.ReadResult(null, offset + 8, value); | ||
}; | ||
var i64LongRW = new I64LongRW(); | ||
function I64BufferRW() { | ||
} | ||
util.inherits(I64BufferRW, I64RW); | ||
I64BufferRW.prototype.readFrom = function readTInt64From(buffer, offset) { | ||
var value = new Buffer(8); | ||
buffer.copy(value, 0, offset, offset + 8); | ||
return new bufrw.ReadResult(null, offset + 8, value); | ||
}; | ||
var i64BufferRW = new I64BufferRW(); | ||
function ThriftI64(annotations) { | ||
var self = this; | ||
if (annotations && annotations['js.type'] === 'Long') { | ||
self.rw = i64LongRW; | ||
self.surface = Long; | ||
} else { | ||
self.rw = i64BufferRW; | ||
self.surface = Buffer; | ||
} | ||
} | ||
ThriftI64.prototype.name = 'i64'; | ||
ThriftI64.prototype.typeid = TYPE.I64; | ||
ThriftI64.prototype.surface = Buffer; | ||
module.exports.I64RW = I64RW; | ||
module.exports.I64BufferRW = I64BufferRW; | ||
module.exports.I64LongRW = I64LongRW; | ||
module.exports.ThriftI64 = ThriftI64; |
{ | ||
"name": "thriftrw", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"description": "thrift encoding/decoding using bufrw", | ||
@@ -22,2 +22,3 @@ "keywords": [], | ||
"error": "^5.1.1", | ||
"long": "^2.4.0", | ||
"pegjs": "^0.8.0" | ||
@@ -24,0 +25,0 @@ }, |
@@ -381,2 +381,24 @@ # thriftrw | ||
For users that need to operate on i64 as a numbers, use the [Long][] package. | ||
```js | ||
var Long = require('long'); | ||
// From a buffer to a long | ||
var buf = new Buffer("0102030405060708", "hex"); | ||
var num = Long.fromBits(buf.readInt32BE(4, true), buf.readInt32BE(0, true)); | ||
// From a long to a buffer | ||
var buf = new Buffer(8); | ||
buf.writeUInt32BE(num.high, 0, 4, true); | ||
buf.writeUInt32BE(num.low, 4, 4, true); | ||
``` | ||
[Long]: https://www.npmjs.com/package/long | ||
A future version of ThriftRW will support the `{high, low}` internal | ||
representation used by Long for writing, and a `js.type = 'Long'` annotation | ||
for reading. | ||
A backward-incompatible release may make this form the default for reading. | ||
## Releated | ||
@@ -383,0 +405,0 @@ |
@@ -24,13 +24,20 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var test = require('tape'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Long = require('long'); | ||
var testRW = require('bufrw/test_rw'); | ||
var testThrift = require('./thrift-test'); | ||
var thriftrw = require('../index'); | ||
var I64RW = thriftrw.I64RW; | ||
var ThriftI64 = thriftrw.ThriftI64; | ||
var TYPE = require('../TYPE'); | ||
var Buffer = require('buffer').Buffer; | ||
var testCases = [ | ||
var thrift = new thriftrw.Thrift({ | ||
source: fs.readFileSync(path.join(__dirname, 'i64.thrift'), 'ascii') | ||
}); | ||
var bufferRW = thrift.getType('bufnum').rw; | ||
var longRW = thrift.getType('long').rw; | ||
var bufferCases = [ | ||
[ | ||
@@ -42,7 +49,16 @@ Buffer([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]), | ||
test('I64RW', testRW.cases(I64RW, testCases)); | ||
test('I64RW', testRW.cases(bufferRW, bufferCases)); | ||
var longCases = [ | ||
[ | ||
Long.fromNumber(Math.pow(2, 53) - 1), | ||
[0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff] | ||
] | ||
]; | ||
test('I64RW', testRW.cases(longRW, longCases)); | ||
test('coerce string', function t(assert) { | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto('0102030405060708', buffer, 0); | ||
var res = bufferRW.writeInto('0102030405060708', buffer, 0); | ||
assert.ifError(res.err, 'write into buffer'); | ||
@@ -56,3 +72,3 @@ assert.equals(res.offset, 8, 'offset after write'); | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto('01020304050607', buffer, 0); | ||
var res = bufferRW.writeInto('01020304050607', buffer, 0); | ||
assert.equals(res.err.message, | ||
@@ -65,3 +81,3 @@ 'invalid argument, expected a string of 16 hex characters, or other i64 representation', 'string length error'); | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto('--------05060708', buffer, 0); | ||
var res = bufferRW.writeInto('--------05060708', buffer, 0); | ||
assert.equals( | ||
@@ -76,3 +92,3 @@ res.err.message, | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto('01020304--------', buffer, 0); | ||
var res = bufferRW.writeInto('01020304--------', buffer, 0); | ||
assert.equals(res.err.message, | ||
@@ -84,8 +100,8 @@ 'invalid argument, expected a string of hex characters, or other i64 representation', | ||
test('coerce {hi, lo} object to i32 on wire', function t(assert) { | ||
test('coerce {hi[gh], lo[w]} object to i32 on wire', function t(assert) { | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto({hi: 1, lo: 2}, buffer, 0); | ||
var res = bufferRW.writeInto({hi: 1, lo: 2}, buffer, 0); | ||
assert.ifError(res.err, 'write into buffer'); | ||
assert.equals(res.offset, 8, 'offset after write'); | ||
assert.deepEquals(buffer, new Buffer('0000000100000002', 'hex'), 'wrote hi, lo to buffer'); | ||
assert.deepEquals(buffer, new Buffer('0000000100000002', 'hex'), 'wrote hi[gh], lo[w] to buffer'); | ||
assert.end(); | ||
@@ -96,5 +112,5 @@ }); | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto({hi: null, lo: 0}, buffer, 0); | ||
var res = bufferRW.writeInto({hi: null, lo: 0}, buffer, 0); | ||
assert.equals(res.err.message, | ||
'invalid argument, expected {hi, lo} with hi number, or other i64 representation', | ||
'invalid argument, expected {hi[gh], lo[w]} with high bits, or other i64 representation', | ||
'validate hi type'); | ||
@@ -106,5 +122,5 @@ assert.end(); | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto({hi: 0, lo: null}, buffer, 0); | ||
var res = bufferRW.writeInto({hi: 0, lo: null}, buffer, 0); | ||
assert.equals(res.err.message, | ||
'invalid argument, expected {hi, lo} with lo number, or other i64 representation', | ||
'invalid argument, expected {hi[gh], lo[w]} with low bits, or other i64 representation', | ||
'validate lo type'); | ||
@@ -116,3 +132,3 @@ assert.end(); | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto(10, buffer, 0); | ||
var res = bufferRW.writeInto(10, buffer, 0); | ||
assert.ifError(res.err, 'write into buffer'); | ||
@@ -126,3 +142,3 @@ assert.equals(res.offset, 8, 'offset after write'); | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto([1, 2, 3, 4, 5, 6, 7, 8], buffer, 0); | ||
var res = bufferRW.writeInto([1, 2, 3, 4, 5, 6, 7, 8], buffer, 0); | ||
assert.ifError(res.err, 'write into buffer'); | ||
@@ -136,3 +152,3 @@ assert.equals(res.offset, 8, 'offset after write'); | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto([1, 2, 3, 4, 5, 6, 7, 8, 9], buffer, 0); | ||
var res = bufferRW.writeInto([1, 2, 3, 4, 5, 6, 7, 8, 9], buffer, 0); | ||
assert.equals(res.err.message, | ||
@@ -146,3 +162,3 @@ 'invalid argument, expected an array of 8 bytes, or other i64 representation', | ||
var buffer = new Buffer(8); | ||
var res = ThriftI64.prototype.rw.writeInto(null, buffer, 0); | ||
var res = bufferRW.writeInto(null, buffer, 0); | ||
assert.equals(res.err.message, 'invalid argument, expected i64 representation'); | ||
@@ -152,2 +168,2 @@ assert.end(); | ||
test('ThriftI64', testThrift(ThriftI64, I64RW, TYPE.I64)); | ||
test('ThriftI64', testThrift(ThriftI64, bufferRW, TYPE.I64)); |
@@ -30,3 +30,3 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
assert.equal(thrift.rw, rw, | ||
ThriftConstructor.name + ' has `rw` method'); | ||
ThriftConstructor.name + ' has `rw` property'); | ||
assert.equal(thrift.typeid, typeid, | ||
@@ -33,0 +33,0 @@ ThriftConstructor.name + ' has string typeid'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
358860
111
6287
445
4
15
+ Addedlong@^2.4.0
+ Addedlong@2.4.0(transitive)