Expose "expires" field for objects

This commit is contained in:
Kagami Hiiragi 2015-03-07 21:05:39 +03:00
parent f29f32b3aa
commit da0eb28eb6
3 changed files with 13 additions and 0 deletions

View File

@ -94,6 +94,7 @@ var getpubkey = exports.getpubkey = {
* @typedef {Object} DecodeResult * @typedef {Object} DecodeResult
* @property {Buffer} nonce - A 8-byte object nonce * @property {Buffer} nonce - A 8-byte object nonce
* @property {number} ttl - Time to live in seconds * @property {number} ttl - Time to live in seconds
* @property {Date} expires - Object expiration date
* @property {number} type - Object type * @property {number} type - Object type
* @property {number} version - Object version * @property {number} version - Object version
* @property {number} stream - Object stream * @property {number} stream - Object stream
@ -254,6 +255,7 @@ var pubkey = exports.pubkey = {
* @typedef {Object} DecodeResult * @typedef {Object} DecodeResult
* @property {Buffer} nonce - A 8-byte object nonce * @property {Buffer} nonce - A 8-byte object nonce
* @property {number} ttl - Time to live in seconds * @property {number} ttl - Time to live in seconds
* @property {Date} expires - Object expiration date
* @property {number} type - Object type * @property {number} type - Object type
* @property {number} version - Object version * @property {number} version - Object version
* @property {number} stream - Object stream * @property {number} stream - Object stream
@ -587,6 +589,7 @@ var msg = exports.msg = {
* @typedef {Object} DecodeResult * @typedef {Object} DecodeResult
* @property {Buffer} nonce - A 8-byte object nonce * @property {Buffer} nonce - A 8-byte object nonce
* @property {number} ttl - Time to live in seconds * @property {number} ttl - Time to live in seconds
* @property {Date} expires - Object expiration date
* @property {number} type - Object type * @property {number} type - Object type
* @property {number} version - Object version * @property {number} version - Object version
* @property {number} stream - Object stream * @property {number} stream - Object stream
@ -877,6 +880,7 @@ var broadcast = exports.broadcast = {
* @typedef {Object} DecodeResult * @typedef {Object} DecodeResult
* @property {Buffer} nonce - A 8-byte object nonce * @property {Buffer} nonce - A 8-byte object nonce
* @property {number} ttl - Time to live in seconds * @property {number} ttl - Time to live in seconds
* @property {Date} expires - Object expiration date
* @property {number} type - Object type * @property {number} type - Object type
* @property {number} version - Object version * @property {number} version - Object version
* @property {number} stream - Object stream * @property {number} stream - Object stream

View File

@ -304,6 +304,7 @@ var object = exports.object = {
* @typedef {Object} DecodeResult * @typedef {Object} DecodeResult
* @property {Buffer} nonce - A 8-byte object nonce * @property {Buffer} nonce - A 8-byte object nonce
* @property {number} ttl - Time to live in seconds * @property {number} ttl - Time to live in seconds
* @property {Date} expires - Object expiration date
* @property {number} type - Object type * @property {number} type - Object type
* @property {number} version - Object version * @property {number} version - Object version
* @property {number} stream - Object stream * @property {number} stream - Object stream
@ -347,6 +348,7 @@ var object = exports.object = {
// TTL. // TTL.
var expiresTime = util.readTimestamp64BE(buf.slice(8, 16)); var expiresTime = util.readTimestamp64BE(buf.slice(8, 16));
var expires = new Date(expiresTime * 1000);
var ttl = expiresTime - util.tnow(); var ttl = expiresTime - util.tnow();
assert(ttl <= 2430000, "expiresTime is too far in the future"); assert(ttl <= 2430000, "expiresTime is too far in the future");
if (!opts.allowExpired) { if (!opts.allowExpired) {
@ -375,6 +377,7 @@ var object = exports.object = {
return { return {
nonce: nonce, nonce: nonce,
ttl: ttl, ttl: ttl,
expires: expires,
type: type, type: type,
version: decodedVersion.value, version: decodedVersion.value,
stream: decodedStream.value, stream: decodedStream.value,

View File

@ -187,6 +187,7 @@ describe("Common structures", function() {
describe("object", function() { describe("object", function() {
it("should encode and decode", function() { it("should encode and decode", function() {
var nonce = Buffer(8); var nonce = Buffer(8);
var now = new Date().getTime();
var res = object.decode(object.encode({ var res = object.decode(object.encode({
nonce: nonce, nonce: nonce,
ttl: 100, ttl: 100,
@ -197,6 +198,8 @@ describe("Common structures", function() {
expect(bufferEqual(nonce, res.nonce)).to.be.true; expect(bufferEqual(nonce, res.nonce)).to.be.true;
expect(res.ttl).to.be.at.most(100); expect(res.ttl).to.be.at.most(100);
expect(res.expires.getTime()).to.be.at.least(now);
expect(res.expires.getTime()).to.be.at.most(now + 100*1000);
expect(res.type).to.equal(2); expect(res.type).to.equal(2);
expect(res.version).to.equal(1); expect(res.version).to.equal(1);
expect(res.stream).to.equal(1); expect(res.stream).to.equal(1);
@ -789,6 +792,7 @@ describe("Object types", function() {
describe("pubkey", function() { describe("pubkey", function() {
it("should encode and decode pubkey v2", function() { it("should encode and decode pubkey v2", function() {
var now = new Date().getTime();
return pubkey.encodeAsync({ return pubkey.encodeAsync({
ttl: 123, ttl: 123,
from: from, from: from,
@ -799,6 +803,8 @@ describe("Object types", function() {
return pubkey.decodeAsync(buf, skipPow); return pubkey.decodeAsync(buf, skipPow);
}).then(function(res) { }).then(function(res) {
expect(res.ttl).to.be.at.most(123); expect(res.ttl).to.be.at.most(123);
expect(res.expires.getTime()).to.be.at.least(now);
expect(res.expires.getTime()).to.be.at.most(now + 123*1000);
expect(res.type).to.equal(object.PUBKEY); expect(res.type).to.equal(object.PUBKEY);
expect(res.version).to.equal(2); expect(res.version).to.equal(2);
expect(res.stream).to.equal(1); expect(res.stream).to.equal(1);