Comparing version 2.4.0 to 2.5.0
35
i64.js
@@ -116,4 +116,3 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
function I64LongRW() { | ||
} | ||
function I64LongRW() {} | ||
@@ -132,5 +131,29 @@ util.inherits(I64LongRW, I64RW); | ||
function I64BufferRW() { | ||
} | ||
function I64DateRW() {} | ||
util.inherits(I64DateRW, I64RW); | ||
I64DateRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
var long = Long.fromBits( | ||
buffer.readInt32BE(offset + 4, 4, true), | ||
buffer.readInt32BE(offset + 0, 4, true) | ||
); | ||
var ms = long.toNumber(); | ||
var value = new Date(ms); | ||
return new bufrw.ReadResult(null, offset + 8, value); | ||
}; | ||
I64DateRW.prototype.writeInto = function writeInto(value, buffer, offset) { | ||
var self = this; | ||
if (typeof value === 'string') { | ||
value = Date.parse(value); | ||
} | ||
value = Long.fromNumber(+value); | ||
return self.writeObjectInt64Into(value, buffer, offset); | ||
}; | ||
var i64DateRW = new I64DateRW(); | ||
function I64BufferRW() {} | ||
util.inherits(I64BufferRW, I64RW); | ||
@@ -151,2 +174,5 @@ | ||
self.surface = Long; | ||
} else if (annotations && annotations['js.type'] === 'Date') { | ||
self.rw = i64DateRW; | ||
self.surface = Date; | ||
} else { | ||
@@ -164,2 +190,3 @@ self.rw = i64BufferRW; | ||
module.exports.I64LongRW = I64LongRW; | ||
module.exports.I64DateRW = I64DateRW; | ||
module.exports.ThriftI64 = ThriftI64; |
{ | ||
"name": "thriftrw", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "thrift encoding/decoding using bufrw", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -368,7 +368,8 @@ # thriftrw | ||
integers. | ||
ThriftRW decodes 64 bit integers into a Buffer, but can coerce various types | ||
down to i64 for purposes of expressing them as JSON. | ||
ThriftRW decodes 64 bit integers into a Buffer by default, and can coerce | ||
various types down to i64 for purposes of expressing them as JSON or plain | ||
JavaScript objects. | ||
- A number up to the maximum integer precision available in JavaScript. | ||
- A `{hi, lo}` pair of 32 bit precision integers. | ||
- A `{hi, lo}` or `{high, low}` pair of 32 bit precision integers. | ||
- A 16 digit hexadecimal string, like `0102030405060708`. | ||
@@ -378,8 +379,16 @@ - An array of 8 byte values. Ranges are not checked, but coerced. | ||
In a future version, we are likely to expose `js.type` annotations to read any | ||
of these forms off the wire. | ||
Currently, they can only be written. | ||
ThriftRW supports a type annotation for i64 that reifies i64 as an instance of | ||
[Long][], an object that models a 64 bit number as a `{high, low}` pair of 32 | ||
bit numbers and implements methods for common mathematical operators. | ||
For users that need to operate on i64 as a numbers, use the [Long][] package. | ||
```thrift | ||
typedef i64 (js.type = 'Long') long | ||
``` | ||
Please use the [Long][] package for operating with such numbers. | ||
A backward-incompatible release may make this form the default for reading. | ||
With the Long type annotation, ThriftRW performs the following conversions on | ||
your behalf. | ||
```js | ||
@@ -400,7 +409,23 @@ var Long = require('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. | ||
### Timestamps | ||
The convention for expressing timestamps at Uber is to use an i64 for | ||
UTC milliseconds since the UNIX epoch. | ||
Previously, some services were found spending an unreasonable amount of time | ||
parsing timestamp strings. | ||
ThriftRW supports a `(js.type = 'Date')` annotation on i64. | ||
```thrift | ||
typedef i64 (js.type = 'Date') timestamp | ||
``` | ||
This will reify timestamps as a JavaScript Date and ThriftRW will accept an | ||
ISO-8601 timestamp or milliseconds since the epoch (as returned by | ||
`Date.now()`) in place of a Date. | ||
For [TCurl][], this means that you can both read and write ISO-8601 for | ||
timestamps. | ||
[TCurl]: https://github.com/uber/tcurl | ||
## Releated | ||
@@ -407,0 +432,0 @@ |
@@ -103,3 +103,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
for (;;) { | ||
// typeid | ||
@@ -106,0 +105,0 @@ // istanbul ignore if |
@@ -32,2 +32,4 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var skipType = require('./skip').skipType; | ||
var ThriftUnrecognizedException = require('./unrecognized-exception') | ||
.ThriftUnrecognizedException; | ||
@@ -38,2 +40,4 @@ var LengthResult = bufrw.LengthResult; | ||
var readType = require('./read').readType; | ||
function ThriftField(def, struct) { | ||
@@ -375,2 +379,20 @@ var self = this; | ||
// keep unrecognized files from the future if it could be an | ||
// unrecognized exception. | ||
if (!self.spec.fieldsById[id] && self.spec.isResult) { | ||
result = readType(buffer, offset, typeid); | ||
// result = skipType(buffer, offset, typeid); | ||
// istanbul ignore if | ||
if (result.err) { | ||
return result; | ||
} | ||
offset = result.offset; | ||
self.spec.set( | ||
struct, | ||
'failure', | ||
new ThriftUnrecognizedException(result.value) | ||
); | ||
continue; | ||
} | ||
// skip unrecognized fields from THE FUTURE | ||
@@ -377,0 +399,0 @@ if (!self.spec.fieldsById[id]) { |
@@ -40,2 +40,3 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var longRW = thrift.getType('long').rw; | ||
var dateRW = thrift.getType('timestamp').rw; | ||
@@ -49,3 +50,3 @@ var bufferCases = [ | ||
test('I64RW', testRW.cases(bufferRW, bufferCases)); | ||
test('I64BufferRW', testRW.cases(bufferRW, bufferCases)); | ||
@@ -59,4 +60,21 @@ var longCases = [ | ||
test('I64RW', testRW.cases(longRW, longCases)); | ||
test('I64LongRW', testRW.cases(longRW, longCases)); | ||
var dateCases = [ | ||
[ | ||
new Date(0), | ||
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] | ||
], | ||
[ | ||
new Date(1), | ||
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01] | ||
], | ||
[ | ||
new Date(1000), | ||
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8] | ||
] | ||
]; | ||
test('I64DateRW', testRW.cases(dateRW, dateCases)); | ||
test('coerce string', function t(assert) { | ||
@@ -159,2 +177,17 @@ var buffer = new Buffer(8); | ||
test('coerce date string', function t(assert) { | ||
var buffer = new Buffer(8); | ||
buffer.fill(0xff); | ||
dateRW.writeInto('1970-01-01T00:00:00.000', buffer, 0); | ||
assert.deepEquals(buffer, new Buffer([0, 0, 0, 0, 0, 0, 0, 0]), 'coerces date string'); | ||
assert.end(); | ||
}); | ||
test('coerce number to date', function t(assert) { | ||
var buffer = new Buffer(8); | ||
dateRW.writeInto(0, buffer, 0); | ||
assert.deepEquals(buffer, new Buffer([0, 0, 0, 0, 0, 0, 0, 0]), 'coerces number to date'); | ||
assert.end(); | ||
}); | ||
test('ThriftI64', testThrift(ThriftI64, bufferRW, TYPE.I64)); |
@@ -53,1 +53,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
require('./enum'); | ||
require('./unrecognized-exception'); |
Sorry, the diff of this file is not supported yet
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
373196
116
6644
470
16