Comparing version 1.1.0 to 1.2.0
147
atoms.js
@@ -23,3 +23,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
@@ -41,21 +40,21 @@ var ReadResult = require('./base').ReadResult; | ||
AtomRW.prototype.byteLength = function byteLength() { | ||
return new LengthResult(null, this.width); | ||
AtomRW.prototype.poolByteLength = function byteLength(destResult) { | ||
return destResult.reset(null, this.width); | ||
}; | ||
AtomRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
AtomRW.prototype.poolReadFrom = function readFrom(destResult, buffer, offset) { | ||
var remain = buffer.length - offset; | ||
if (remain < this.width) { | ||
return ReadResult.shortError(this.width, remain, offset); | ||
return ReadResult.poolShortError(destResult, this.width, remain, offset); | ||
} | ||
return this.readAtomFrom(buffer, offset); | ||
return this.readAtomFrom(destResult, buffer, offset); | ||
}; | ||
AtomRW.prototype.writeInto = function writeInto(value, buffer, offset) { | ||
AtomRW.prototype.poolWriteInto = function writeInto(destResult, value, buffer, offset) { | ||
var remain = buffer.length - offset; | ||
// istanbul ignore next | ||
if (remain < this.width) { | ||
return WriteResult.shortError(this.width, remain, offset); | ||
WriteResult.poolShortError(destResult, this.width, remain, offset); | ||
} | ||
return this.writeAtomInto(value, buffer, offset); | ||
return this.writeAtomInto(destResult, value, buffer, offset); | ||
}; | ||
@@ -74,8 +73,8 @@ | ||
IntegerRW.prototype.writeInto = function writeInto(value, buffer, offset) { | ||
IntegerRW.prototype.poolWriteInto = function poolWriteInto(destResult, value, buffer, offset) { | ||
if (typeof value !== 'number') { | ||
return WriteResult.error(errors.expected(value, 'a number')); | ||
return destResult.reset(errors.expected(value, 'a number')); | ||
} | ||
if (value < this.min || value > this.max) { | ||
return WriteResult.error(errors.RangeError({ | ||
return destResult.reset(errors.RangeError({ | ||
value: value, | ||
@@ -88,119 +87,119 @@ min: this.min, | ||
if (remain < this.width) { | ||
return WriteResult.shortError(this.width, remain, offset); | ||
return WriteResult.poolShortError(destResult, this.width, remain, offset); | ||
} | ||
return this.writeAtomInto(value, buffer, offset); | ||
return this.writeAtomInto(destResult, value, buffer, offset); | ||
}; | ||
var Int8 = IntegerRW(1, -0x80, 0x7f, | ||
function readInt8From(buffer, offset) { | ||
function readInt8From(destResult, buffer, offset) { | ||
var value = buffer.readInt8(offset, true); | ||
return new ReadResult(null, offset + 1, value); | ||
return destResult.reset(null, offset + 1, value); | ||
}, | ||
function writeInt8Into(value, buffer, offset) { | ||
function writeInt8Into(destResult, value, buffer, offset) { | ||
buffer.writeInt8(value, offset, true); | ||
return new WriteResult(null, offset + 1); | ||
return destResult.reset(null, offset + 1); | ||
}); | ||
var Int16BE = IntegerRW(2, -0x8000, 0x7fff, | ||
function readInt16BEFrom(buffer, offset) { | ||
function readInt16BEFrom(destResult, buffer, offset) { | ||
var value = buffer.readInt16BE(offset, true); | ||
return new ReadResult(null, offset + 2, value); | ||
return destResult.reset(null, offset + 2, value); | ||
}, | ||
function writeInt16BEInto(value, buffer, offset) { | ||
function writeInt16BEInto(destResult, value, buffer, offset) { | ||
buffer.writeInt16BE(value, offset, true); | ||
return new WriteResult(null, offset + 2); | ||
return destResult.reset(null, offset + 2); | ||
}); | ||
var Int32BE = IntegerRW(4, -0x80000000, 0x7fffffff, | ||
function readInt32BEFrom(buffer, offset) { | ||
function readInt32BEFrom(destResult, buffer, offset) { | ||
var value = buffer.readInt32BE(offset, true); | ||
return new ReadResult(null, offset + 4, value); | ||
return destResult.reset(null, offset + 4, value); | ||
}, | ||
function writeInt32BEInto(value, buffer, offset) { | ||
function writeInt32BEInto(destResult, value, buffer, offset) { | ||
buffer.writeInt32BE(value, offset, true); | ||
return new WriteResult(null, offset + 4); | ||
return destResult.reset(null, offset + 4); | ||
}); | ||
var Int16LE = IntegerRW(2, -0x8000, 0x7fff, | ||
function readInt16LEFrom(buffer, offset) { | ||
function readInt16LEFrom(destResult, buffer, offset) { | ||
var value = buffer.readInt16LE(offset, true); | ||
return new ReadResult(null, offset + 2, value); | ||
return destResult.reset(null, offset + 2, value); | ||
}, | ||
function writeInt16LEInto(value, buffer, offset) { | ||
function writeInt16LEInto(destResult, value, buffer, offset) { | ||
buffer.writeInt16LE(value, offset, true); | ||
return new WriteResult(null, offset + 2); | ||
return destResult.reset(null, offset + 2); | ||
}); | ||
var Int32LE = IntegerRW(4, -0x80000000, 0x7fffffff, | ||
function readInt32LEFrom(buffer, offset) { | ||
function readInt32LEFrom(destResult, buffer, offset) { | ||
var value = buffer.readInt32LE(offset, true); | ||
return new ReadResult(null, offset + 4, value); | ||
return destResult.reset(null, offset + 4, value); | ||
}, | ||
function writeInt32LEInto(value, buffer, offset) { | ||
function writeInt32LEInto(destResult, value, buffer, offset) { | ||
buffer.writeInt32LE(value, offset, true); | ||
return new WriteResult(null, offset + 4); | ||
return destResult.reset(null, offset + 4); | ||
}); | ||
var UInt8 = IntegerRW(1, 0, 0xff, | ||
function readUInt8From(buffer, offset) { | ||
function readUInt8From(destResult, buffer, offset) { | ||
var value = buffer.readUInt8(offset, true); | ||
return new ReadResult(null, offset + 1, value); | ||
return destResult.reset(null, offset + 1, value); | ||
}, | ||
function writeUInt8Into(value, buffer, offset) { | ||
function writeUInt8Into(destResult, value, buffer, offset) { | ||
buffer.writeUInt8(value, offset, true); | ||
return new WriteResult(null, offset + 1); | ||
return destResult.reset(null, offset + 1); | ||
}); | ||
var UInt16BE = IntegerRW(2, 0, 0xffff, | ||
function readUInt16BEFrom(buffer, offset) { | ||
function readUInt16BEFrom(destResult, buffer, offset) { | ||
var value = buffer.readUInt16BE(offset, true); | ||
return new ReadResult(null, offset + 2, value); | ||
return destResult.reset(null, offset + 2, value); | ||
}, | ||
function writeUInt16BEInto(value, buffer, offset) { | ||
function writeUInt16BEInto(destResult, value, buffer, offset) { | ||
buffer.writeUInt16BE(value, offset, true); | ||
return new WriteResult(null, offset + 2); | ||
return destResult.reset(null, offset + 2); | ||
}); | ||
var UInt32BE = IntegerRW(4, 0, 0xffffffff, | ||
function readUInt32BEFrom(buffer, offset) { | ||
function readUInt32BEFrom(destResult, buffer, offset) { | ||
var value = buffer.readUInt32BE(offset, true); | ||
return new ReadResult(null, offset + 4, value); | ||
return destResult.reset(null, offset + 4, value); | ||
}, | ||
function writeUInt32BEInto(value, buffer, offset) { | ||
function writeUInt32BEInto(destResult, value, buffer, offset) { | ||
buffer.writeUInt32BE(value, offset, true); | ||
return new WriteResult(null, offset + 4); | ||
return destResult.reset(null, offset + 4); | ||
}); | ||
var UInt16LE = IntegerRW(2, 0, 0xffff, | ||
function readUInt16LEFrom(buffer, offset) { | ||
function readUInt16LEFrom(destResult, buffer, offset) { | ||
var value = buffer.readUInt16LE(offset, true); | ||
return new ReadResult(null, offset + 2, value); | ||
return destResult.reset(null, offset + 2, value); | ||
}, | ||
function writeUInt16LEInto(value, buffer, offset) { | ||
function writeUInt16LEInto(destResult, value, buffer, offset) { | ||
buffer.writeUInt16LE(value, offset, true); | ||
return new WriteResult(null, offset + 2); | ||
return destResult.reset(null, offset + 2); | ||
}); | ||
var UInt32LE = IntegerRW(4, 0, 0xffffffff, | ||
function readUInt32LEFrom(buffer, offset) { | ||
function readUInt32LEFrom(destResult, buffer, offset) { | ||
var value = buffer.readUInt32LE(offset, true); | ||
return new ReadResult(null, offset + 4, value); | ||
return destResult.reset(null, offset + 4, value); | ||
}, | ||
function writeUInt32LEInto(value, buffer, offset) { | ||
function writeUInt32LEInto(destResult, value, buffer, offset) { | ||
buffer.writeUInt32LE(value, offset, true); | ||
return new WriteResult(null, offset + 4); | ||
return destResult.reset(null, offset + 4); | ||
}); | ||
var FloatLE = AtomRW(4, | ||
function readFloatLEFrom(buffer, offset) { | ||
function readFloatLEFrom(destResult, buffer, offset) { | ||
var value = buffer.readFloatLE(offset, true); | ||
return new ReadResult(null, offset + 4, value); | ||
return destResult.reset(null, offset + 4, value); | ||
}, | ||
function writeFloatLEInto(value, buffer, offset) { | ||
function writeFloatLEInto(destResult, value, buffer, offset) { | ||
// istanbul ignore if | ||
if (typeof value !== 'number') { | ||
return WriteResult.error(errors.expected(value, 'a number')); | ||
return destResult.reset(errors.expected(value, 'a number'), null); | ||
} else { | ||
buffer.writeFloatLE(value, offset); | ||
return new WriteResult(null, offset + 4); | ||
return destResult.reset(null, offset + 4); | ||
} | ||
@@ -210,13 +209,13 @@ }); | ||
var FloatBE = AtomRW(4, | ||
function readFloatBEFrom(buffer, offset) { | ||
function readFloatBEFrom(destResult, buffer, offset) { | ||
var value = buffer.readFloatBE(offset, true); | ||
return new ReadResult(null, offset + 4, value); | ||
return destResult.reset(null, offset + 4, value); | ||
}, | ||
function writeFloatBEInto(value, buffer, offset) { | ||
function writeFloatBEInto(destResult, value, buffer, offset) { | ||
// istanbul ignore if | ||
if (typeof value !== 'number') { | ||
return WriteResult.error(errors.expected(value, 'a number')); | ||
return destResult.reset(errors.expected(value, 'a number'), null); | ||
} else { | ||
buffer.writeFloatBE(value, offset); | ||
return new WriteResult(null, offset + 4); | ||
return destResult.reset(null, offset + 4); | ||
} | ||
@@ -226,13 +225,13 @@ }); | ||
var DoubleLE = AtomRW(8, | ||
function readDoubleLEFrom(buffer, offset) { | ||
function readDoubleLEFrom(destResult, buffer, offset) { | ||
var value = buffer.readDoubleLE(offset, true); | ||
return new ReadResult(null, offset + 8, value); | ||
return destResult.reset(null, offset + 8, value); | ||
}, | ||
function writeDoubleLEInto(value, buffer, offset) { | ||
function writeDoubleLEInto(destResult, value, buffer, offset) { | ||
// istanbul ignore if | ||
if (typeof value !== 'number') { | ||
return WriteResult.error(errors.expected(value, 'a number')); | ||
return destResult.reset(errors.expected(value, 'a number'), null); | ||
} else { | ||
buffer.writeDoubleLE(value, offset); | ||
return new WriteResult(null, offset + 8); | ||
return destResult.reset(null, offset + 8); | ||
} | ||
@@ -242,13 +241,13 @@ }); | ||
var DoubleBE = AtomRW(8, | ||
function readDoubleBEFrom(buffer, offset) { | ||
function readDoubleBEFrom(destResult, buffer, offset) { | ||
var value = buffer.readDoubleBE(offset, true); | ||
return new ReadResult(null, offset + 8, value); | ||
return destResult.reset(null, offset + 8, value); | ||
}, | ||
function writeDoubleBEInto(value, buffer, offset) { | ||
function writeDoubleBEInto(destResult, value, buffer, offset) { | ||
// istanbul ignore if | ||
if (typeof value !== 'number') { | ||
return WriteResult.error(errors.expected(value, 'a number')); | ||
return destResult.reset(errors.expected(value, 'a number'), null); | ||
} else { | ||
buffer.writeDoubleBE(value, offset); | ||
return new WriteResult(null, offset + 8); | ||
return destResult.reset(null, offset + 8); | ||
} | ||
@@ -255,0 +254,0 @@ }); |
187
base.js
@@ -20,3 +20,6 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
// THE SOFTWARE. | ||
'use strict'; | ||
var assert = require('assert'); | ||
var errors = require('./errors'); | ||
@@ -29,11 +32,83 @@ | ||
function BufferRW(byteLength, readFrom, writeInto) { | ||
function BufferRW(byteLength, readFrom, writeInto, isPooled) { | ||
if (!(this instanceof BufferRW)) { | ||
return new BufferRW(byteLength, readFrom, writeInto); | ||
return new BufferRW(byteLength, readFrom, writeInto, isPooled); | ||
} | ||
if (typeof byteLength === 'function') this.byteLength = byteLength; | ||
if (typeof readFrom === 'function') this.readFrom = readFrom; | ||
if (typeof writeInto === 'function') this.writeInto = writeInto; | ||
// istanbul ignore else | ||
if (byteLength && readFrom && writeInto) { | ||
assert(typeof byteLength === 'function', 'expected byteLength to be function'); | ||
assert(typeof readFrom === 'function', 'expected readFrom to be function'); | ||
assert(typeof writeInto === 'function', 'expected writeInto to be function'); | ||
// istanbul ignore else | ||
if (isPooled) { | ||
this.poolByteLength = byteLength; | ||
this.poolReadFrom = readFrom; | ||
this.poolWriteInto = writeInto; | ||
} else { | ||
this.byteLength = byteLength; | ||
this.readFrom = readFrom; | ||
this.writeInto = writeInto; | ||
} | ||
} else { | ||
// Args weren't specified. Expect either pool methods or regular | ||
// methods to be overriden. | ||
assert( | ||
this.poolReadFrom !== BufferRW.prototype.poolReadFrom || | ||
this.readFrom !== BufferRW.prototype.readFrom, | ||
'expected either poolReadFrom or readFrom to be overriden' | ||
); | ||
assert( | ||
this.poolWriteInto !== BufferRW.prototype.poolWriteInto || | ||
this.writeInto !== BufferRW.prototype.writeInto, | ||
'expected either poolWriteInto or writeInto to be overriden' | ||
); | ||
assert( | ||
this.poolByteLength !== BufferRW.prototype.poolByteLength || | ||
this.byteLength !== BufferRW.prototype.byteLength, | ||
'expected either poolByteLength or byteLength to be overriden' | ||
); | ||
} | ||
} | ||
BufferRW.prototype.readFrom = function readFrom(arg1, arg2, arg3) { | ||
assert(this.poolReadFrom !== BufferRW.prototype.poolReadFrom, 'poolReadFrom is overridden'); | ||
var readResult = new ReadResult(); | ||
this.poolReadFrom(readResult, arg1, arg2, arg3); | ||
return readResult; | ||
}; | ||
BufferRW.prototype.writeInto = function writeInto(value, buffer, offset) { | ||
assert(this.poolWriteInto !== BufferRW.prototype.poolWriteInto, 'poolWriteInto is overridden'); | ||
var writeResult = new WriteResult(); | ||
this.poolWriteInto(writeResult, value, buffer, offset); | ||
return writeResult; | ||
}; | ||
BufferRW.prototype.byteLength = function byteLength(arg1, arg2, arg3) { | ||
assert(this.poolbyteLength !== BufferRW.prototype.poolByteLength, 'poolByteLength is overridden'); | ||
var lengthResult = new LengthResult(); | ||
this.poolByteLength(lengthResult, arg1, arg2, arg3); | ||
return lengthResult; | ||
}; | ||
// istanbul ignore next | ||
BufferRW.prototype.poolReadFrom = function poolReadFrom(destResult, arg1, arg2, arg3) { | ||
var res = this.readFrom(arg1, arg2, arg3); | ||
return destResult.copyFrom(res); | ||
}; | ||
// istanbul ignore next | ||
BufferRW.prototype.poolWriteInto = function poolWriteInto(destResult, value, buffer, offset) { | ||
var res = this.writeInto(value, buffer, offset); | ||
return destResult.copyFrom(res); | ||
}; | ||
// istanbul ignore next | ||
BufferRW.prototype.poolByteLength = function poolByteLength(destResult, arg1, arg2, arg3) { | ||
var res = this.byteLength(arg1, arg2, arg3); | ||
return destResult.copyFrom(res); | ||
}; | ||
function LengthResult(err, length) { | ||
@@ -44,2 +119,16 @@ this.err = err || null; | ||
LengthResult.prototype.reset = function reset(err, length) { | ||
this.err = err; | ||
this.length = length; | ||
return this; | ||
}; | ||
// istanbul ignore next | ||
LengthResult.prototype.copyFrom = function copyFrom(srcRes) { | ||
this.err = srcRes.err; | ||
this.length = srcRes.length; | ||
return this; | ||
}; | ||
// istanbul ignore next | ||
LengthResult.error = function error(err, length) { | ||
@@ -49,2 +138,3 @@ return new LengthResult(err, length); | ||
// istanbul ignore next | ||
LengthResult.just = function just(length) { | ||
@@ -59,2 +149,15 @@ return new LengthResult(null, length); | ||
WriteResult.prototype.reset = function reset(err, offset) { | ||
this.err = err; | ||
this.offset = offset; | ||
return this; | ||
}; | ||
// istanbul ignore next | ||
WriteResult.prototype.copyFrom = function copyFrom(srcResult) { | ||
this.err = srcResult.err; | ||
this.offset = srcResult.offset; | ||
}; | ||
// istanbul ignore next | ||
WriteResult.error = function error(err, offset) { | ||
@@ -65,8 +168,17 @@ return new WriteResult(err, offset); | ||
// istanbul ignore next | ||
WriteResult.rangedError = function rangedError(err, start, end, value) { | ||
/*jshint maxparams:6*/ | ||
WriteResult.poolRangedError = function poolRangedError(destResult, err, start, end, value) { | ||
assert(typeof destResult === 'object' && destResult.constructor.name === 'WriteResult'); | ||
err.offest = start; | ||
err.endOffset = end; | ||
return new WriteResult(err, start, value); | ||
return destResult.reset(err, start, value); | ||
}; | ||
// istanbul ignore next | ||
WriteResult.rangedError = function rangedError(err, start, end, value) { | ||
return WriteResult.poolRangedError(new WriteResult(), start, end, value); | ||
}; | ||
// istanbul ignore next | ||
WriteResult.just = function just(offset) { | ||
@@ -76,4 +188,13 @@ return new WriteResult(null, offset); | ||
// istanbul ignore next | ||
WriteResult.shortError = function shortError(expected, actual, offset) { | ||
return new WriteResult(new errors.ShortBuffer({ | ||
return WriteResult.poolShortError(new WriteResult(), expected, actual, offset); | ||
}; | ||
// istanbul ignore next | ||
WriteResult.poolShortError = function poolShortError(destResult, expected, actual, offset) { | ||
assert(typeof destResult === 'object' && destResult.constructor.name === 'WriteResult'); | ||
return destResult.reset(new errors.ShortBuffer({ | ||
expected: expected, | ||
@@ -88,5 +209,23 @@ actual: actual, | ||
this.offset = offset || 0; | ||
// istanbul ignore next | ||
this.value = value === undefined ? null : value; | ||
} | ||
// istanbul ignore next | ||
ReadResult.prototype.copyFrom = function copyFrom(srcResult) { | ||
this.err = srcResult.err; | ||
this.offset = srcResult.offset; | ||
this.value = srcResult.value; | ||
return this; | ||
}; | ||
// istanbul ignore next | ||
ReadResult.prototype.reset = function reset(err, offset, value) { | ||
this.err = err; | ||
this.offset = offset; | ||
this.value = value; | ||
return this; | ||
}; | ||
// istanbul ignore next | ||
ReadResult.error = function error(err, offset, value) { | ||
@@ -97,8 +236,16 @@ return new ReadResult(err, offset, value); | ||
// istanbul ignore next | ||
ReadResult.rangedError = function rangedError(err, start, end, value) { | ||
ReadResult.poolRangedError = function poolRangedError(destResult, err, start, end, value) { | ||
assert(typeof destResult === 'object' && destResult.constructor.name === 'ReadResult'); | ||
err.offest = start; | ||
err.endOffset = end; | ||
return new ReadResult(err, start, value); | ||
return destResult.reset(err, start, value); | ||
}; | ||
// istanbul ignore next | ||
ReadResult.rangedError = function rangedError(err, start, end, value) { | ||
return ReadResult.poolRangedError(new ReadResult(), err, start, end, value); | ||
}; | ||
// istanbul ignore next | ||
ReadResult.just = function just(offset, value) { | ||
@@ -108,11 +255,19 @@ return new ReadResult(null, offset, value); | ||
ReadResult.shortError = function shortError(expected, actual, offset, endOffset) { | ||
// istanbul ignore next | ||
ReadResult.shortError = function shortError(destResult, expected, actual, offset, endOffset) { | ||
return ReadResult.poolShortError(new ReadResult(), expected, actual, offset, endOffset); | ||
}; | ||
ReadResult.poolShortError = function poolShortError(destResult, expected, actual, offset, endOffset) { | ||
assert(typeof destResult === 'object' && destResult.constructor.name === 'ReadResult'); | ||
var err; | ||
if (endOffset === undefined) { | ||
return new ReadResult(new errors.ShortBuffer({ | ||
err = new errors.ShortBuffer({ | ||
expected: expected, | ||
actual: actual, | ||
offset: offset | ||
}), offset); | ||
}); | ||
} else { | ||
return new ReadResult(new errors.ShortBufferRanged({ | ||
err = new errors.ShortBufferRanged({ | ||
expected: expected, | ||
@@ -122,4 +277,6 @@ actual: actual, | ||
endOffset: endOffset | ||
}), offset); | ||
}); | ||
} | ||
return destResult.reset(err, offset); | ||
}; |
@@ -20,2 +20,3 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
// THE SOFTWARE. | ||
'use strict'; | ||
@@ -26,4 +27,2 @@ module.exports = FixedWidthRW; | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
@@ -38,20 +37,21 @@ var BufferRW = require('./base').BufferRW; | ||
this.length = length; | ||
BufferRW.call(this, null, readFrom, writeInto); | ||
// BufferRW.call(this); | ||
} | ||
inherits(FixedWidthRW, BufferRW); | ||
FixedWidthRW.prototype.byteLength = function byteLength(slice) { | ||
FixedWidthRW.prototype.poolByteLength = function poolByteLength(destResult, slice) { | ||
if (slice.length !== this.length) { | ||
return LengthResult.error(errors.FixedLengthMismatch({ | ||
return destResult.reset(errors.FixedLengthMismatch({ | ||
expected: this.length, | ||
got: slice.length | ||
})); | ||
}), null); | ||
} else { | ||
return new LengthResult(null, this.length); | ||
return destResult.reset(null, this.length); | ||
} | ||
}; | ||
FixedWidthRW.prototype.writeInto = function writeInto(slice, buffer, offset) { | ||
FixedWidthRW.prototype.poolWriteInto = function poolWriteInto(destResult, slice, buffer, offset) { | ||
if (slice.length !== this.length) { | ||
return WriteResult.error(errors.FixedLengthMismatch({ | ||
return destResult.reset(errors.FixedLengthMismatch({ | ||
expected: this.length, | ||
@@ -62,13 +62,14 @@ got: slice.length | ||
slice.copy(buffer, offset); | ||
return new WriteResult(null, offset + this.length); | ||
//return new WriteResult(null, offset + this.length); | ||
return destResult.reset(null, offset + this.length); | ||
}; | ||
FixedWidthRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
FixedWidthRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) { | ||
var end = offset + this.length; | ||
if (end > buffer.length) { | ||
return ReadResult.shortError(this.length, buffer.length - offset, offset); | ||
return ReadResult.poolShortError(destResult, this.length, buffer.length - offset, offset); | ||
} else { | ||
var res = new ReadResult(null, end, buffer.slice(offset, end)); | ||
return res; | ||
//var res = new ReadResult(null, end, buffer.slice(offset, end)); | ||
return destResult.reset(null, end, buffer.slice(offset, end)); | ||
} | ||
}; |
@@ -279,1 +279,5 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
module.exports.intoBufferResult = intoBufferResult; | ||
module.exports.makeAnnotatedBuffer = makeAnnotatedBuffer; | ||
module.exports.checkAllReadFrom = checkAllReadFrom; | ||
module.exports.annotateError = annotateError; |
12
null.js
@@ -21,12 +21,12 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
var AtomRW = require('./atoms').AtomRW; | ||
function nullWriteInto(val, buffer, offset) { | ||
return new WriteResult(null, offset); | ||
function nullWriteInto(destResult, val, buffer, offset) { | ||
//return new WriteResult(null, offset); | ||
return destResult.reset(null, offset); | ||
} | ||
function nullReadFrom(buffer, offset) { | ||
return new ReadResult(null, offset, null); | ||
function nullReadFrom(destResult, buffer, offset) { | ||
//return new ReadResult(null, offset, null); | ||
return destResult.reset(null, offset, null); | ||
} | ||
@@ -33,0 +33,0 @@ |
{ | ||
"name": "bufrw", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Buffer Reading and Writing", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -25,5 +25,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
var BufferRW = require('./base').BufferRW; | ||
@@ -38,28 +35,31 @@ var errors = require('./errors'); | ||
this.repeatedrw = repeatedrw; | ||
BufferRW.call(this); | ||
} | ||
inherits(RepeatRW, BufferRW); | ||
RepeatRW.prototype.byteLength = function byteLength(values) { | ||
RepeatRW.prototype.poolByteLength = function poolByteLength(destResult, values) { | ||
if (!Array.isArray(values)) { | ||
return LengthResult.error(errors.expected(values, 'an array')); | ||
return destResult.reset(errors.expected(values, 'an array')); | ||
} | ||
var res = this.countrw.byteLength(values.length); | ||
var res = this.countrw.poolByteLength(destResult, values.length); | ||
if (res.err) return res; | ||
var length = res.length; | ||
for (var i = 0; i < values.length; i++) { | ||
var partres = this.repeatedrw.byteLength(values[i]); | ||
var partres = this.repeatedrw.poolByteLength(destResult, values[i]); | ||
if (partres.err) return partres; | ||
res.length += partres.length; | ||
length += res.length; | ||
} | ||
return res; | ||
return destResult.reset(null, length); | ||
}; | ||
RepeatRW.prototype.writeInto = function writeInto(values, buffer, offset) { | ||
RepeatRW.prototype.poolWriteInto = function poolWriteInto(destResult, values, buffer, offset) { | ||
if (!Array.isArray(values)) { | ||
return WriteResult.error(errors.expected(values, 'an array'), offset); | ||
return destResult.reset(errors.expected(values, 'an array'), offset); | ||
} | ||
var res = this.countrw.writeInto(values.length, buffer, offset); | ||
var res = this.countrw.poolWriteInto(destResult, values.length, buffer, offset); | ||
if (res.err) return res; | ||
offset = res.offset; | ||
for (var i = 0; i < values.length; i++) { | ||
res = this.repeatedrw.writeInto(values[i], buffer, offset); | ||
res = this.repeatedrw.poolWriteInto(destResult, values[i], buffer, offset); | ||
if (res.err) return res; | ||
@@ -71,4 +71,4 @@ offset = res.offset; | ||
RepeatRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
var res = this.countrw.readFrom(buffer, offset); | ||
RepeatRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) { | ||
var res = this.countrw.poolReadFrom(destResult, buffer, offset); | ||
if (res.err) return res; | ||
@@ -79,8 +79,21 @@ offset = res.offset; | ||
for (var i = 0; i < count; i++) { | ||
res = this.repeatedrw.readFrom(buffer, offset); | ||
res = this.repeatedrw.poolReadFrom(destResult, buffer, offset); | ||
if (res.err) return res; | ||
offset = res.offset; | ||
values[i] = res.value; | ||
if (Array.isArray(res.value)) values[i] = res.value.slice(0); | ||
else if (typeof res.value === 'object') values[i] = shallowCopy(res.value); | ||
else values[i] = res.value; | ||
} | ||
return new ReadResult(null, offset, values); | ||
return destResult.reset(null, offset, values); | ||
}; | ||
function shallowCopy(obj) { | ||
var keys = Object.keys(obj); | ||
var i; | ||
var dest = {}; | ||
for (i = 0; i < keys.length; i++) { | ||
dest[keys[i]] = obj[keys[i]]; | ||
} | ||
return dest; | ||
} |
@@ -25,4 +25,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
@@ -40,40 +38,43 @@ var BufferRW = require('./base').BufferRW; | ||
this.rws = rws; | ||
BufferRW.call(this); | ||
} | ||
inherits(SeriesRW, BufferRW); | ||
SeriesRW.prototype.byteLength = function byteLength(values) { | ||
SeriesRW.prototype.poolByteLength = function poolByteLength(destResult, values) { | ||
if (!Array.isArray(values) && values !== null) { | ||
return LengthResult.error(errors.expected(values, 'an array or null')); | ||
return destResult.reset(errors.expected(values, 'an array or null')); | ||
} | ||
var length = 0; | ||
for (var i = 0; i < this.rws.length; i++) { | ||
var res = this.rws[i].byteLength(values && values[i]); | ||
if (res.err) return res; | ||
length += res.length; | ||
this.rws[i].poolByteLength(destResult, values && values[i]); | ||
if (destResult.err) return destResult; | ||
length += destResult.length; | ||
} | ||
return new LengthResult(null, length); | ||
return destResult.reset(null, length); | ||
}; | ||
SeriesRW.prototype.writeInto = function writeInto(values, buffer, offset) { | ||
SeriesRW.prototype.poolWriteInto = function poolWriteInto(destResult, values, buffer, offset) { | ||
if (!Array.isArray(values) && values !== null) { | ||
return WriteResult.error(errors.expected(values, 'an array or null'), offset); | ||
return destResult.reset(errors.expected(values, 'an array or null'), offset); | ||
} | ||
var res = new WriteResult(null, offset); | ||
for (var i = 0; i < this.rws.length; i++) { | ||
res = this.rws[i].writeInto(values && values[i], buffer, offset); | ||
if (res.err) return res; | ||
offset = res.offset; | ||
this.rws[i].poolWriteInto(destResult, values && values[i], buffer, offset); | ||
if (destResult.err) return destResult; | ||
offset = destResult.offset; | ||
} | ||
return res; | ||
return destResult; | ||
}; | ||
SeriesRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
var values = new Array(this.rws.length); | ||
var readResult = new ReadResult(); | ||
SeriesRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) { | ||
if (!Array.isArray(destResult.value)) { | ||
destResult.value = new Array(this.rws.length); | ||
} | ||
for (var i = 0; i < this.rws.length; i++) { | ||
var res = this.rws[i].readFrom(buffer, offset); | ||
if (res.err) return res; | ||
offset = res.offset; | ||
values[i] = res.value; | ||
this.rws[i].poolReadFrom(readResult, buffer, offset); | ||
if (readResult.err) return destResult.copyFrom(readResult); | ||
offset = readResult.offset; | ||
destResult.value[i] = readResult.value; | ||
} | ||
return new ReadResult(null, offset, values); | ||
return destResult.reset(null, offset, destResult.value); | ||
}; |
16
skip.js
@@ -25,4 +25,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
@@ -40,19 +38,19 @@ var FixedWidthRW = require('./fixed_width_rw'); | ||
SkipRW.prototype.byteLength = function byteLength() { | ||
return new LengthResult(null, this.length); | ||
SkipRW.prototype.poolByteLength = function poolByteLength(destResult) { | ||
return destResult.reset(null, this.length); | ||
}; | ||
SkipRW.prototype.writeInto = function writeInto(val, buffer, offset) { | ||
SkipRW.prototype.poolWriteInto = function poolWriteInto(destResult, val, buffer, offset) { | ||
var end = offset + this.length; | ||
buffer.fill(this.fill, offset, end); | ||
return new WriteResult(null, end); | ||
return destResult.reset(null, end); | ||
}; | ||
SkipRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
SkipRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) { | ||
var end = offset + this.length; | ||
if (end > buffer.length) { | ||
return ReadResult.shortError(this.length, buffer.length - offset, offset); | ||
return ReadResult.poolShortError(destResult, this.length, buffer.length - offset, offset); | ||
} else { | ||
return new ReadResult(null, end, null); | ||
return destResult.reset(null, end, null); | ||
} | ||
}; |
@@ -25,3 +25,4 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var errors = require('../errors'); | ||
var fromBufferResult = require('../interface').fromBufferResult; | ||
var iface = require('../interface'); | ||
var ReadResult = require('../base').ReadResult; | ||
@@ -82,4 +83,5 @@ module.exports = ReadMachine; | ||
var pendReadRes = new ReadResult(); | ||
ReadMachine.prototype.pend = function pend() { | ||
var sizeRes = this.sizeRW.readFrom(this.buffer, 0); | ||
var sizeRes = this.sizeRW.poolReadFrom(pendReadRes, this.buffer, 0); | ||
var err = sizeRes.err; | ||
@@ -101,2 +103,4 @@ if (!err && !sizeRes.value) { | ||
var seekReadRes = new ReadResult(); | ||
var seekReadRes2 = new ReadResult(); | ||
ReadMachine.prototype.seek = function seek() { | ||
@@ -115,8 +119,14 @@ var chunk = this.buffer.shift(this.expecting); | ||
var res = fromBufferResult(this.chunkRW, chunk, 0); | ||
if (res.err) { | ||
return res.err; | ||
// pooled inline of fromBufferResult | ||
this.chunkRW.poolReadFrom(seekReadRes, chunk, 0); | ||
iface.checkAllReadFrom(seekReadRes, chunk); | ||
if (seekReadRes.err) { | ||
var annBuf = iface.makeAnnotatedBuffer(chunk, 0, false); | ||
this.chunkRW.poolReadFrom(seekReadRes2, annBuf, 0); | ||
iface.checkAllReadFrom(seekReadRes2, chunk); | ||
iface.annotateError(seekReadRes, seekReadRes2, 0, annBuf); | ||
return seekReadRes.err; | ||
} else { | ||
this.emit(res.value); | ||
return null; | ||
this.emit(seekReadRes.value); | ||
return; | ||
} | ||
@@ -123,0 +133,0 @@ }; |
@@ -20,2 +20,3 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
// THE SOFTWARE. | ||
'use strict'; | ||
@@ -26,7 +27,5 @@ module.exports = StringRW; | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
var errors = require('./errors'); | ||
var VariableBufferRW = require('./variable_buffer_rw'); | ||
var BufferRW = require('./base').BufferRW; | ||
@@ -37,11 +36,15 @@ function StringRW(sizerw, encoding) { | ||
} | ||
this.sizerw = sizerw; | ||
this.encoding = encoding || 'utf8'; | ||
VariableBufferRW.call(this, sizerw); | ||
if (!this.sizerw.width) { | ||
this.writeInto = this.writeVariableWidthInto; | ||
this.poolWriteInto = this.poolWriteVariableWidthInto; | ||
} else { | ||
this.poolWriteInto = this.poolWriteFixedWidthInto; | ||
} | ||
BufferRW.call(this); | ||
} | ||
inherits(StringRW, VariableBufferRW); | ||
inherits(StringRW, BufferRW); | ||
StringRW.prototype.byteLength = function byteLength(str) { | ||
StringRW.prototype.poolByteLength = function poolByteLength(destResult, str) { | ||
var length = 0; | ||
@@ -51,10 +54,11 @@ if (typeof str === 'string') { | ||
} else if (str !== null && str !== undefined) { | ||
return LengthResult.error(errors.expected(str, 'string, null, or undefined')); | ||
return destResult.reset(errors.expected(str, 'string, null, or undefined'), null); | ||
} | ||
var len = this.sizerw.byteLength(length); | ||
if (len.err) return len; | ||
return new LengthResult(null, len.length + length); | ||
this.sizerw.poolByteLength(destResult, length); | ||
if (destResult.err) return destResult; | ||
return destResult.reset(null, destResult.length + length); | ||
}; | ||
StringRW.prototype.writeInto = function writeFixedWidthInto(str, buffer, offset) { | ||
StringRW.prototype.poolWriteFixedWidthInto = | ||
function poolWriteFixedWidthInto(destResult, str, buffer, offset) { | ||
var start = offset + this.sizerw.width; | ||
@@ -65,11 +69,12 @@ var length = 0; | ||
} else if (str !== null && str !== undefined) { | ||
return WriteResult.error(errors.expected(str, 'string, null, or undefined'), offset); | ||
return destResult.reset(errors.expected(str, 'string, null, or undefined'), offset); | ||
} | ||
var res = this.sizerw.writeInto(length, buffer, offset); | ||
this.sizerw.poolWriteInto(destResult, length, buffer, offset); | ||
// istanbul ignore if | ||
if (res.err) return res; | ||
return new WriteResult(null, start + length); | ||
if (destResult.err) return destResult; | ||
return destResult.reset(null, start + length); | ||
}; | ||
StringRW.prototype.writeVariableWidthInto = function writeVariableWidthInto(str, buffer, offset) { | ||
StringRW.prototype.poolWriteVariableWidthInto = | ||
function poolWriteVariableWidthInto(destResult, str, buffer, offset) { | ||
var size = 0; | ||
@@ -79,5 +84,5 @@ if (typeof str === 'string') { | ||
} else if (str !== null && str !== undefined) { | ||
return WriteResult.error(errors.expected(str, 'string, null, or undefined'), offset); | ||
return destResult.reset(errors.expected(str, 'string, null, or undefined'), offset); | ||
} | ||
var res = this.sizerw.writeInto(size, buffer, offset); | ||
var res = this.sizerw.poolWriteInto(destResult, size, buffer, offset); | ||
if (res.err) return res; | ||
@@ -91,4 +96,4 @@ offset = res.offset; | ||
StringRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
var res = this.sizerw.readFrom(buffer, offset); | ||
StringRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) { | ||
var res = this.sizerw.poolReadFrom(destResult, buffer, offset); | ||
if (res.err) return res; | ||
@@ -98,3 +103,3 @@ var length = res.value; | ||
if (remain < length) { | ||
return ReadResult.shortError(length, remain, offset, res.offset); | ||
return ReadResult.poolShortError(destResult, length, remain, offset, res.offset); | ||
} else { | ||
@@ -104,4 +109,4 @@ offset = res.offset; | ||
var str = buffer.toString(this.encoding, offset, end); | ||
return new ReadResult(null, end, str); | ||
return destResult.reset(null, end, str); | ||
} | ||
}; |
@@ -25,4 +25,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
@@ -65,3 +63,3 @@ var BufferRW = require('./base').BufferRW; | ||
StructRW.prototype.byteLength = function byteLength(obj) { | ||
StructRW.prototype.poolByteLength = function poolByteLength(destResult, obj) { | ||
var length = 0; | ||
@@ -72,24 +70,29 @@ for (var i = 0; i < this.fields.length; i++) { | ||
if (field.name && !obj.hasOwnProperty(field.name)) { | ||
return LengthResult.error(errors.MissingStructField({ | ||
return destResult.reset(errors.MissingStructField({ | ||
field: field.name, | ||
struct: this.cons.name | ||
})); | ||
}), null); | ||
} | ||
var value = field.name && obj && obj[field.name]; | ||
var res; | ||
if (field.call) { | ||
if (!field.call.byteLength) continue; | ||
res = field.call.byteLength(obj); | ||
if (field.call.poolByteLength) { | ||
field.call.poolByteLength(destResult, obj); | ||
} else if (field.call.byteLength) { | ||
var res = field.call.byteLength(obj); | ||
destResult.copyFrom(res); | ||
} else { | ||
continue; | ||
} | ||
} else { | ||
res = field.rw.byteLength(value); | ||
field.rw.poolByteLength(destResult, value); | ||
} | ||
if (res.err) return res; | ||
length += res.length; | ||
if (destResult.err) return destResult; | ||
length += destResult.length; | ||
} | ||
return new LengthResult(null, length); | ||
return destResult.reset(null, length); | ||
}; | ||
StructRW.prototype.writeInto = function writeInto(obj, buffer, offset) { | ||
var res = new WriteResult(null, offset); | ||
StructRW.prototype.poolWriteInto = function poolWriteInto(destResult, obj, buffer, offset) { | ||
destResult.reset(null, offset); | ||
for (var i = 0; i < this.fields.length; i++) { | ||
@@ -99,6 +102,6 @@ var field = this.fields[i]; | ||
if (field.name && !obj.hasOwnProperty(field.name)) { | ||
return WriteResult.error(errors.MissingStructField({ | ||
return destResult.reset(errors.MissingStructField({ | ||
field: field.name, | ||
struct: this.cons.name | ||
})); | ||
}), null); | ||
} | ||
@@ -108,31 +111,49 @@ | ||
if (field.call) { | ||
if (!field.call.writeInto) continue; | ||
res = field.call.writeInto(obj, buffer, offset); | ||
if (field.call.poolWriteInto) { | ||
field.call.poolWriteInto(destResult, obj, buffer, offset); | ||
} else if (field.call.writeInto) { | ||
var res = field.call.writeInto(obj, buffer, offset); | ||
destResult.copyFrom(res); | ||
} else { | ||
continue; | ||
} | ||
} else { | ||
res = field.rw.writeInto(value, buffer, offset); | ||
field.rw.poolWriteInto(destResult, value, buffer, offset); | ||
} | ||
if (res.err) return res; | ||
offset = res.offset; | ||
if (destResult.err) return destResult; | ||
offset = destResult.offset; | ||
} | ||
return res; | ||
return destResult; | ||
}; | ||
StructRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
var obj = new this.cons(); | ||
var readRes = new ReadResult(); | ||
StructRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) { | ||
if (typeof destResult.value === 'object' && destResult.value !== null) { | ||
if (destResult.value.constructor !== this.cons) { | ||
destResult.value = new this.cons(); | ||
} | ||
} else { | ||
destResult.value = new this.cons(); | ||
} | ||
for (var i = 0; i < this.fields.length; i++) { | ||
var field = this.fields[i]; | ||
var res; | ||
if (field.call) { | ||
if (!field.call.readFrom) continue; | ||
res = field.call.readFrom(obj, buffer, offset); | ||
if (field.call.poolReadFrom) { | ||
field.call.poolReadFrom(readRes, destResult.value, buffer, offset); | ||
} else if (field.call.readFrom) { | ||
var res = field.call.readFrom(destResult.value, buffer, offset); | ||
readRes.copyFrom(res); | ||
} else { | ||
continue; | ||
} | ||
} else { | ||
res = field.rw.readFrom(buffer, offset); | ||
field.rw.poolReadFrom(readRes, buffer, offset); | ||
} | ||
if (res.err) return res; | ||
offset = res.offset; | ||
if (readRes.err) return destResult.copyFrom(readRes); | ||
offset = readRes.offset; | ||
if (field.name) { | ||
obj[field.name] = res.value; | ||
destResult.value[field.name] = readRes.value; | ||
} | ||
} | ||
return new ReadResult(null, offset, obj); | ||
return destResult.reset(null, offset, destResult.value); | ||
}; |
@@ -25,5 +25,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
var BufferRW = require('./base').BufferRW; | ||
@@ -46,8 +43,10 @@ var errors = require('./errors'); | ||
if (opts.structMode) { | ||
this.readFrom = this.structReadFrom; | ||
this.poolReadFrom = this.structReadFrom; | ||
} | ||
BufferRW.call(this); | ||
} | ||
inherits(BufferRW, BufferRW); | ||
inherits(SwitchRW, BufferRW); | ||
SwitchRW.prototype.byteLength = function byteLength(obj) { | ||
SwitchRW.prototype.poolByteLength = function poolByteLength(destResult, obj) { | ||
var val = obj[this.valKey]; | ||
@@ -57,14 +56,18 @@ var data = obj[this.dataKey]; | ||
if (datarw === undefined) { | ||
return LengthResult.error(errors.WriteInvalidSwitchValue({ | ||
return destResult.reset(errors.WriteInvalidSwitchValue({ | ||
value: val | ||
})); | ||
} | ||
var vallen = this.valrw.byteLength(val); | ||
if (vallen.err) return vallen; | ||
var caselen = datarw.byteLength(data); | ||
if (caselen.err) return caselen; | ||
return LengthResult.just(vallen.length + caselen.length); | ||
this.valrw.poolByteLength(destResult, val); | ||
if (destResult.err) return destResult; | ||
var vallen = destResult.length; | ||
datarw.poolByteLength(destResult, data); | ||
if (destResult.err) return destResult; | ||
var caselen = destResult.length; | ||
return destResult.reset(null, caselen + vallen); | ||
}; | ||
SwitchRW.prototype.writeInto = function writeInto(obj, buffer, offset) { | ||
SwitchRW.prototype.poolWriteInto = function poolWriteInto(destResult, obj, buffer, offset) { | ||
var val = obj[this.valKey]; | ||
@@ -74,14 +77,14 @@ var data = obj[this.dataKey]; | ||
if (datarw === undefined) { | ||
return WriteResult.error(errors.WriteInvalidSwitchValue({ | ||
return destResult.reset(errors.WriteInvalidSwitchValue({ | ||
value: val | ||
}), offset); | ||
} | ||
var res = this.valrw.writeInto(val, buffer, offset); | ||
var res = this.valrw.poolWriteInto(destResult, val, buffer, offset); | ||
if (res.err) return res; | ||
res = datarw.writeInto(data, buffer, res.offset); | ||
res = datarw.poolWriteInto(destResult, data, buffer, res.offset); | ||
return res; | ||
}; | ||
SwitchRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
var res = this.valrw.readFrom(buffer, offset); | ||
SwitchRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) { | ||
var res = this.valrw.poolReadFrom(destResult, buffer, offset); | ||
if (res.err) return res; | ||
@@ -92,7 +95,7 @@ offset = res.offset; | ||
if (datarw === undefined) { | ||
return ReadResult.error(errors.ReadInvalidSwitchValue({ | ||
return destResult.reset(errors.ReadInvalidSwitchValue({ | ||
value: val | ||
}), offset); | ||
} | ||
res = datarw.readFrom(buffer, offset); | ||
res = datarw.poolReadFrom(destResult, buffer, offset); | ||
if (res.err) return res; | ||
@@ -102,8 +105,9 @@ offset = res.offset; | ||
var obj = new this.cons(val, data); | ||
return ReadResult.just(offset, obj); | ||
return destResult.reset(null, offset, obj); | ||
}; | ||
// istanbul ignore next TODO | ||
SwitchRW.prototype.structReadFrom = function readFrom(obj, buffer, offset) { | ||
var res = this.valrw.readFrom(buffer, offset); | ||
SwitchRW.prototype.poolStructReadFrom = | ||
function poolStructReadFrom(destResult, obj, buffer, offset) { | ||
var res = this.valrw.poolReadFrom(destResult, buffer, offset); | ||
if (res.err) return res; | ||
@@ -114,3 +118,3 @@ offset = res.offset; | ||
if (datarw === undefined) { | ||
return ReadResult.error(errors.ReadInvalidSwitchValue({ | ||
return destResult.reset(errors.ReadInvalidSwitchValue({ | ||
value: val | ||
@@ -120,3 +124,3 @@ }), offset); | ||
obj[this.valKey] = val; | ||
res = datarw.readFrom(buffer, offset); | ||
res = datarw.poolReadFrom(destResult, buffer, offset); | ||
if (!res.err) { | ||
@@ -123,0 +127,0 @@ obj[this.dataKey] = res.value; |
@@ -110,2 +110,3 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var err = res.err; | ||
if (err) { | ||
@@ -112,0 +113,0 @@ if (testCase.error) { |
@@ -26,31 +26,37 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var iface = require('../interface'); | ||
var LengthResult = require('../base').LengthResult; | ||
var WriteResult = require('../base').WriteResult; | ||
var ReadResult = require('../base').ReadResult; | ||
var BufferRW = require('../base').BufferRW; | ||
var byteRW = { | ||
byteLength: function() {return LengthResult.just(1);}, | ||
writeInto: function(b, buffer, offset) { | ||
poolByteLength: function(destResult) {return destResult.reset(null, 1);}, | ||
poolWriteInto: function(destResult, b, buffer, offset) { | ||
buffer[offset] = b; | ||
return WriteResult.just(++offset); | ||
return destResult.reset(null, ++offset); | ||
}, | ||
readFrom: function(buffer, offset) { | ||
poolReadFrom: function(destResult, buffer, offset) { | ||
var b = buffer[offset]; | ||
return ReadResult.just(++offset, b); | ||
return destResult.reset(null, ++offset, b); | ||
}, | ||
}; | ||
byteRW.__proto__ = BufferRW.prototype; | ||
var lengthErrorRW = { | ||
byteLength: function() {return new LengthResult(new Error('boom'));} | ||
poolByteLength: function(destResult) {return destResult.reset(new Error('boom'));} | ||
}; | ||
lengthErrorRW.__proto__ = BufferRW.prototype; | ||
var writeErrorRW = { | ||
byteLength: function() {return new LengthResult.just(0);}, | ||
writeInto: function() {return new WriteResult.error(new Error('bang'));} | ||
poolByteLength: function(destResult) {return destResult.reset(null, 0);}, | ||
poolWriteInto: function(destResult) {return destResult.reset(new Error('bang'));} | ||
}; | ||
writeErrorRW.__proto__ = BufferRW.prototype; | ||
var readErrorRW = { | ||
readFrom: function() {return new ReadResult.error(new Error('zot'));} | ||
poolReadFrom: function(destResult) {return destResult.reset(new Error('zot'));} | ||
}; | ||
readErrorRW.__proto__ = BufferRW.prototype; | ||
test('byteLength', function t(assert) { | ||
@@ -57,0 +63,0 @@ assert.deepEqual( |
@@ -28,18 +28,20 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var RepeatRW = require('../repeat'); | ||
var SeriesRW = require('../series'); | ||
var ReadResult = require('../base').ReadResult; | ||
var StructRW = require('../struct'); | ||
var LengthResult = require('../base').LengthResult; | ||
var WriteResult = require('../base').WriteResult; | ||
var ReadResult = require('../base').ReadResult; | ||
var brokenRW = { | ||
byteLength: function() { | ||
return new LengthResult(new Error('boom')); | ||
poolByteLength: function(destResult) { | ||
return destResult.reset(new Error('boom')); | ||
}, | ||
writeInto: function(val, buffer, offset) { | ||
return new WriteResult(new Error('bang'), offset); | ||
poolWriteInto: function(destResult, val, buffer, offset) { | ||
return destResult.reset(new Error('bang'), offset); | ||
}, | ||
readFrom: function(buffer, offset) { | ||
return new ReadResult(new Error('bork'), offset); | ||
poolReadFrom: function(destResult, buffer, offset) { | ||
return destResult.reset(new Error('bork'), offset); | ||
}, | ||
}; | ||
brokenRW.prototype = require('../base').BufferRW.prototype; | ||
// n:1 (x<Int8>){n} | ||
@@ -123,1 +125,40 @@ var tinyIntList = RepeatRW(atoms.UInt8, atoms.Int8); | ||
])); | ||
test('RepeatRW: properly handles repeated array rws', function t(assert) { | ||
var thing = RepeatRW(atoms.UInt8, SeriesRW(atoms.UInt8, atoms.UInt8)); | ||
var buf = new Buffer([0x01, 0x02, 0x03]); | ||
var readResult = new ReadResult(); | ||
thing.poolReadFrom(readResult, buf, 0); | ||
assert.deepEquals(readResult.value, [[2, 3]]); | ||
assert.end(); | ||
}); | ||
function Loc(lat, lng) { | ||
if (!(this instanceof Loc)) { | ||
return new Loc(lat, lng); | ||
} | ||
var self = this; | ||
self.lat = lat || 0; | ||
self.lng = lng || 0; | ||
} | ||
var consLoc = StructRW(Loc, { | ||
lat: atoms.DoubleBE, | ||
lng: atoms.DoubleBE | ||
}); | ||
test('RepeatRW: properly handles repeated object rws', function t(assert) { | ||
var thing = RepeatRW(atoms.UInt8, consLoc); | ||
var buf = new Buffer([0x01, 0x40, 0x42, 0xe3, 0x43, 0x7c, 0x56, 0x92, 0xb4, | ||
0xc0, 0x5e, 0x9a, 0xb8, 0xa1, 0x9c, 0x9d, 0x5a]); | ||
var readResult = new ReadResult(); | ||
thing.poolReadFrom(readResult, buf, 0); | ||
assert.deepEquals(readResult.value, [{lat: 37.775497, lng: -122.417519}]); | ||
assert.end(); | ||
}); |
@@ -27,17 +27,16 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var BufferRW = require('../base').BufferRW; | ||
var LengthResult = require('../base').LengthResult; | ||
var WriteResult = require('../base').WriteResult; | ||
var ReadResult = require('../base').ReadResult; | ||
var brokenRW = { | ||
byteLength: function() { | ||
return new LengthResult(new Error('boom')); | ||
poolByteLength: function(destResult) { | ||
return destResult.reset(new Error('boom')); | ||
}, | ||
writeInto: function(val, buffer, offset) { | ||
return new WriteResult(new Error('bang'), offset); | ||
poolWriteInto: function(destResult, val, buffer, offset) { | ||
return destResult.reset(new Error('bang'), offset); | ||
}, | ||
readFrom: function(buffer, offset) { | ||
return new ReadResult(new Error('bork'), offset); | ||
poolReadFrom: function(destResult, buffer, offset) { | ||
return destResult.reset(new Error('bork'), offset); | ||
}, | ||
}; | ||
brokenRW.prototype = BufferRW.prototype; | ||
var atoms = require('../atoms'); | ||
@@ -48,12 +47,13 @@ var SeriesRW = require('../series'); | ||
return new BufferRW( | ||
rw.byteLength.bind(rw), | ||
rw.readFrom.bind(rw), | ||
writeInto); | ||
function writeInto(value, buffer, offset) { | ||
rw.poolByteLength.bind(rw), | ||
rw.poolReadFrom.bind(rw), | ||
writeInto, | ||
true); | ||
function writeInto(destResult, value, buffer, offset) { | ||
if (value === null || value === undefined) { | ||
var end = offset + rw.width; | ||
buffer.fill(0, offset, end); | ||
return WriteResult.just(end); | ||
return destResult.reset(null, end); | ||
} else { | ||
return rw.writeInto(value, buffer, offset); | ||
return rw.poolWriteInto(destResult, value, buffer, offset); | ||
} | ||
@@ -60,0 +60,0 @@ } |
@@ -31,6 +31,6 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var intoBuffer = require('../../interface').intoBuffer; | ||
var ReadResult = require('../../base').ReadResult; | ||
var UInt8 = require('../../atoms').UInt8; | ||
var StringRW = require('../../string_rw'); | ||
var SeriesRW = require('../../series'); | ||
var BufferRW = require('../../base').BufferRW; | ||
@@ -40,8 +40,8 @@ var str1 = StringRW(UInt8); | ||
var readErrorRW = { | ||
width: 1, | ||
readFrom: function() { | ||
return ReadResult.error(new Error('boom')); | ||
} | ||
poolReadFrom: function(destResult) {return destResult.reset(new Error('boom'), 0);}, | ||
width: 1 | ||
}; | ||
readErrorRW.__proto__ = BufferRW.prototype; | ||
var buffers = []; | ||
@@ -48,0 +48,0 @@ var expectedFrames = []; |
@@ -31,7 +31,6 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var intoBuffer = require('../../interface').intoBuffer; | ||
var LengthResult = require('../../base').LengthResult; | ||
var WriteResult = require('../../base').WriteResult; | ||
var UInt8 = require('../../atoms').UInt8; | ||
var StringRW = require('../../string_rw'); | ||
var SeriesRW = require('../../series'); | ||
var BufferRW = require('../../base').BufferRW; | ||
@@ -41,10 +40,12 @@ var str1 = StringRW(UInt8); | ||
var writeErrorRW = { | ||
byteLength: function() { | ||
return LengthResult.just(0); | ||
poolByteLength: function(destResult) { | ||
return destResult.reset(null, 0); | ||
}, | ||
writeInto: function() { | ||
return WriteResult.error(new Error('boom')); | ||
poolWriteInto: function(destResult) { | ||
return destResult.reset(new Error('boom'), 0); | ||
} | ||
}; | ||
writeErrorRW.__proto__ = BufferRW.prototype; | ||
var frames = []; | ||
@@ -51,0 +52,0 @@ var expectedBuffers = []; |
@@ -20,3 +20,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
// THE SOFTWARE. | ||
'use strict'; | ||
@@ -27,17 +26,15 @@ | ||
var LengthResult = require('../base').LengthResult; | ||
var WriteResult = require('../base').WriteResult; | ||
var ReadResult = require('../base').ReadResult; | ||
var brokenRW = { | ||
byteLength: function() { | ||
return new LengthResult(new Error('boom')); | ||
poolByteLength: function(destResult) { | ||
return destResult.reset(new Error('boom')); | ||
}, | ||
writeInto: function(val, buffer, offset) { | ||
return new WriteResult(new Error('bang'), offset); | ||
poolWriteInto: function(destResult, val, buffer, offset) { | ||
return destResult.reset(new Error('bang'), offset); | ||
}, | ||
readFrom: function(buffer, offset) { | ||
return new ReadResult(new Error('bork'), offset); | ||
poolReadFrom: function(destResult, buffer, offset) { | ||
return destResult.reset(new Error('bork'), offset); | ||
}, | ||
}; | ||
brokenRW.prototype = require('../base').BufferRW.prototype; | ||
var atoms = require('../atoms'); | ||
@@ -44,0 +41,0 @@ var StringRW = require('../string_rw'); |
@@ -78,22 +78,23 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var lengthRes = new LengthResult(); | ||
Frame.rw = StructRW(Frame, [ | ||
{call: { | ||
byteLength: function(frame) { | ||
var res = str1.byteLength(frame.mess); | ||
poolByteLength: function(destResult, frame) { | ||
var res = str1.poolByteLength(destResult, frame.mess); | ||
if (res.err) return res; | ||
frame.size = res.length + UInt16BE.width; | ||
if (frame.size > 10) { | ||
return LengthResult.error(new Error('arbitrary length limit')); | ||
return destResult.reset(new Error('arbitrary length limit'), null); | ||
} else { | ||
return LengthResult.just(0); | ||
return destResult.reset(null, 0); | ||
} | ||
}, | ||
writeInto: function(frame, buffer, offset) { | ||
var res = str1.byteLength(frame.mess); | ||
poolWriteInto: function(destResult, frame, buffer, offset) { | ||
var res = str1.poolByteLength(lengthRes, frame.mess); | ||
if (res.err) return res; | ||
frame.size = res.length + UInt16BE.width; | ||
if (buffer.length - offset < frame.size) { | ||
return WriteResult.error(new Error('not enough room')); | ||
return destResult.reset(new Error('not enough room'), null); | ||
} else { | ||
return WriteResult.just(0); | ||
return destResult.reset(null, 0); | ||
} | ||
@@ -105,7 +106,7 @@ } | ||
{call: { | ||
readFrom: function(frame, buffer, offset) { | ||
poolReadFrom: function(destResult, frame, buffer, offset) { | ||
if (offset < buffer.length) { | ||
return ReadResult.error(new Error('frame data past message')); | ||
return destResult.reset(new Error('frame data past message'), offset); | ||
} else { | ||
return ReadResult.just(offset); | ||
return destResult.reset(null, offset); | ||
} | ||
@@ -182,1 +183,86 @@ } | ||
])); | ||
function NonPoolFrame(mess) { | ||
this.size = 0; | ||
this.mess = mess || ''; | ||
} | ||
NonPoolFrame.rw = StructRW(NonPoolFrame, [ | ||
{call: { | ||
byteLength: function(frame) { | ||
var res = str1.byteLength(frame.mess); | ||
if (res.err) return res; | ||
frame.size = res.length + UInt16BE.width; | ||
if (frame.size > 10) { | ||
return new LengthResult(new Error('arbitrary length limit'), null); | ||
} else { | ||
return new LengthResult(null, 0); | ||
} | ||
}, | ||
writeInto: function(frame, buffer, offset) { | ||
var res = str1.byteLength(frame.mess); | ||
if (res.err) return res; | ||
frame.size = res.length + UInt16BE.width; | ||
if (buffer.length - offset < frame.size) { | ||
return new WriteResult(new Error('not enough room'), null); | ||
} else { | ||
return new WriteResult(null, 0); | ||
} | ||
} | ||
}}, | ||
{name: 'size', rw: UInt16BE}, | ||
{name: 'mess', rw: str1}, | ||
{call: { | ||
readFrom: function(frame, buffer, offset) { | ||
if (offset < buffer.length) { | ||
return ReadResult.error(new Error('frame data past message'), offset); | ||
} else { | ||
return ReadResult.just(offset); | ||
} | ||
} | ||
}} | ||
]); | ||
test('StructRW: non pooled frame', testRW.cases(NonPoolFrame.rw, [ | ||
[new NonPoolFrame('cat'), [0x00, 0x06, 0x03, 0x63, 0x61, 0x74]], | ||
// provoke call error paths | ||
{ | ||
lengthTest: { | ||
value: new NonPoolFrame('what even is this?'), | ||
error: { | ||
message: 'arbitrary length limit' | ||
} | ||
}, | ||
writeTest: { | ||
value: new NonPoolFrame('what even is this?'), | ||
length: 2, | ||
error: { | ||
message: 'not enough room' | ||
} | ||
}, | ||
readTest: { | ||
bytes: [0x00, 0x00, 0x00, 0xff], | ||
error: { | ||
message: 'frame data past message' | ||
} | ||
} | ||
} | ||
])); | ||
test('structrw poolreadfrom correctly allocates new obj', function t(assert) { | ||
var buf = new Buffer([0x00, 0x06, 0x03, 0x63, 0x61, 0x74]); | ||
var destResult = new ReadResult(null, 0, {a: 'b'}); | ||
NonPoolFrame.rw.poolReadFrom(destResult, buf, 0); | ||
assert.equal(destResult.value.constructor, NonPoolFrame); | ||
assert.end(); | ||
}); | ||
test('structrw poolreadfrom correctly reuses obj', function t(assert) { | ||
var buf = new Buffer([0x00, 0x06, 0x03, 0x63, 0x61, 0x74]); | ||
var obj = new NonPoolFrame(); | ||
var destResult = new ReadResult(null, 0, obj); | ||
NonPoolFrame.rw.poolReadFrom(destResult, buf, 0); | ||
assert.equal(destResult.value, obj); | ||
assert.end(); | ||
}); |
@@ -30,17 +30,16 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('../base').LengthResult; | ||
var WriteResult = require('../base').WriteResult; | ||
var ReadResult = require('../base').ReadResult; | ||
var brokenRW = { | ||
byteLength: function() { | ||
return new LengthResult(new Error('boom')); | ||
poolByteLength: function(destResult) { | ||
return destResult.reset(new Error('boom')); | ||
}, | ||
writeInto: function(val, buffer, offset) { | ||
return new WriteResult(new Error('bang'), offset); | ||
poolWriteInto: function(destResult, val, buffer, offset) { | ||
return destResult.reset(new Error('bang'), offset); | ||
}, | ||
readFrom: function(buffer, offset) { | ||
return new ReadResult(new Error('bork'), offset); | ||
poolReadFrom: function(destResult, buffer, offset) { | ||
return destResult.reset(new Error('bork'), offset); | ||
}, | ||
}; | ||
brokenRW.prototype = require('../base').BufferRW.prototype; | ||
var numbers = SwitchRW(atoms.UInt8, { | ||
@@ -47,0 +46,0 @@ 0: atoms.UInt8, |
@@ -27,5 +27,3 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('../base').LengthResult; | ||
var WriteResult = require('../base').WriteResult; | ||
var ReadResult = require('../base').ReadResult; | ||
var BufferRW = require('../base').BufferRW; | ||
@@ -38,25 +36,29 @@ var BangError = TypedError({ | ||
var dummyRW = { | ||
byteLength: function() { | ||
return new LengthResult.just(0); | ||
poolByteLength: function(destResult) { | ||
return destResult.reset(null, 0); | ||
}, | ||
writeInto: function(val, buffer, offset) { | ||
return new WriteResult.just(offset); | ||
poolWriteInto: function(destResult, val, buffer, offset) { | ||
return destResult.reset(null, offset); | ||
}, | ||
readFrom: function(buffer, offset) { | ||
return new ReadResult.just(offset, null); | ||
poolReadFrom: function(destResult, buffer, offset) { | ||
return destResult.reset(null, offset, null); | ||
}, | ||
}; | ||
dummyRW.__proto__ = BufferRW.prototype; | ||
var brokenRW = { | ||
byteLength: function() { | ||
return new LengthResult(new Error('boom')); | ||
poolByteLength: function(destResult) { | ||
return destResult.reset(new Error('boom')); | ||
}, | ||
writeInto: function(val, buffer, offset) { | ||
return new WriteResult(BangError(), offset); | ||
poolWriteInto: function(destResult, val, buffer, offset) { | ||
return destResult.reset(new BangError(), offset); | ||
}, | ||
readFrom: function(buffer, offset) { | ||
return new ReadResult(new Error('bork'), offset); | ||
poolReadFrom: function(destResult, buffer, offset) { | ||
return destResult.reset(new Error('bork'), offset); | ||
}, | ||
}; | ||
brokenRW.__proto__ = BufferRW.prototype; | ||
test('testRW: checks cases', function t(assert) { | ||
@@ -63,0 +65,0 @@ assert.throws(function badTest() { |
@@ -26,17 +26,17 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('../base').LengthResult; | ||
var WriteResult = require('../base').WriteResult; | ||
var ReadResult = require('../base').ReadResult; | ||
var BufferRW = require('../base').BufferRW; | ||
var brokenRW = { | ||
byteLength: function() { | ||
return new LengthResult(new Error('boom')); | ||
poolByteLength: function(destResult) { | ||
return destResult.reset(new Error('boom')); | ||
}, | ||
writeInto: function(val, buffer, offset) { | ||
return new WriteResult(new Error('bang'), offset); | ||
poolWriteInto: function(destResult, val, buffer, offset) { | ||
return destResult.reset(new Error('bang'), offset); | ||
}, | ||
readFrom: function(buffer, offset) { | ||
return new ReadResult(new Error('bork'), offset); | ||
poolReadFrom: function(destResult, buffer, offset) { | ||
return destResult.reset(new Error('bork'), offset); | ||
}, | ||
}; | ||
brokenRW.prototype = BufferRW.prototype; | ||
var atoms = require('../atoms'); | ||
@@ -43,0 +43,0 @@ var VariableBufferRW = require('../variable_buffer_rw'); |
@@ -25,4 +25,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
var ReadResult = require('./base').ReadResult; | ||
@@ -38,3 +36,5 @@ var BufferRW = require('./base').BufferRW; | ||
if (lazy) { | ||
this.readFrom = this.lazyReadFrom; | ||
this.poolReadFrom = this.lazyPoolReadFrom; | ||
} else { | ||
this.poolReadFrom = this.eagerPoolReadFrom; | ||
} | ||
@@ -45,3 +45,3 @@ BufferRW.call(this); | ||
VariableBufferRW.prototype.byteLength = function byteLength(buf) { | ||
VariableBufferRW.prototype.poolByteLength = function poolByteLength(destResult, buf) { | ||
var length = 0; | ||
@@ -53,10 +53,10 @@ if (Buffer.isBuffer(buf)) { | ||
} else { | ||
return LengthResult.error(errors.expected(buf, 'buffer, null, or undefined')); | ||
return destResult.reset(errors.expected(buf, 'buffer, null, or undefined'), null); | ||
} | ||
var len = this.sizerw.byteLength(length); | ||
if (len.err) return len; | ||
return new LengthResult(null, len.length + length); | ||
this.sizerw.poolByteLength(destResult, length); | ||
if (destResult.err) return destResult; | ||
return destResult.reset(null, destResult.length + length); | ||
}; | ||
VariableBufferRW.prototype.writeInto = function writeInto(buf, buffer, offset) { | ||
VariableBufferRW.prototype.poolWriteInto = function poolWriteInto(destResult, buf, buffer, offset) { | ||
var start = offset + this.sizerw.width; | ||
@@ -69,38 +69,37 @@ var length = 0; | ||
} else { | ||
return WriteResult.error(errors.expected(buf, 'buffer, null, or undefined'), offset); | ||
return destResult.reset(errors.expected(buf, 'buffer, null, or undefined'), offset); | ||
} | ||
var res = this.sizerw.writeInto(length, buffer, offset); | ||
if (res.err) return res; | ||
return new WriteResult(null, start + length); | ||
this.sizerw.poolWriteInto(destResult, length, buffer, offset); | ||
if (destResult.err) return destResult; | ||
return destResult.reset(null, start + length); | ||
}; | ||
VariableBufferRW.prototype.readFrom = | ||
VariableBufferRW.prototype.eagerReadFrom = function eagerReadFrom(buffer, offset) { | ||
var res = this.sizerw.readFrom(buffer, offset); | ||
if (res.err) return res; | ||
var length = res.value; | ||
var remain = buffer.length - res.offset; | ||
VariableBufferRW.prototype.eagerPoolReadFrom = function eagerPoolReadFrom(destResult, buffer, offset) { | ||
this.sizerw.poolReadFrom(destResult, buffer, offset); | ||
if (destResult.err) return destResult; | ||
var length = destResult.value; | ||
var remain = buffer.length - destResult.offset; | ||
if (remain < length) { | ||
return ReadResult.shortError(length, remain, offset, res.offset); | ||
return ReadResult.poolShortError(destResult, length, remain, offset, destResult.offset); | ||
} else { | ||
offset = res.offset; | ||
offset = destResult.offset; | ||
var buf = Buffer(length); | ||
buffer.copy(buf, 0, offset); | ||
return new ReadResult(null, offset + length, buf); | ||
return destResult.reset(null, offset + length, buf); | ||
} | ||
}; | ||
VariableBufferRW.prototype.lazyReadFrom = function lazyReadFrom(buffer, offset) { | ||
var res = this.sizerw.readFrom(buffer, offset); | ||
if (res.err) return res; | ||
var length = res.value; | ||
var remain = buffer.length - res.offset; | ||
VariableBufferRW.prototype.lazyPoolReadFrom = function lazyPoolReadFrom(destResult, buffer, offset) { | ||
this.sizerw.poolReadFrom(destResult, buffer, offset); | ||
if (destResult.err) return destResult; | ||
var length = destResult.value; | ||
var remain = buffer.length - destResult.offset; | ||
if (remain < length) { | ||
return ReadResult.shortError(length, remain, offset, res.offset); | ||
return ReadResult.poolShortError(destResult, length, remain, offset, destResult.offset); | ||
} else { | ||
offset = res.offset; | ||
offset = destResult.offset; | ||
var end = offset + length; | ||
var buf = buffer.slice(offset, end); | ||
return new ReadResult(null, end, buf); | ||
return destResult.reset(null, end, buf); | ||
} | ||
}; |
@@ -23,3 +23,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var LengthResult = require('./base').LengthResult; | ||
var WriteResult = require('./base').WriteResult; | ||
@@ -35,19 +34,20 @@ var ReadResult = require('./base').ReadResult; | ||
readUnsignedVarIntFrom, | ||
writeUnsignedVarIntInto); | ||
writeUnsignedVarIntInto, | ||
true); | ||
function unsignedVarIntByteLength(n) { | ||
function unsignedVarIntByteLength(destResult, n) { | ||
if (typeof n !== 'number' || n < 0) { | ||
// TODO: integer check | ||
return LengthResult.error(errors.expected(n, 'unsigned integer')); | ||
return destResult.reset(errors.expected(n, 'unsigned integer')); | ||
} | ||
if (n === 0) LengthResult.just(1); | ||
if (n === 0) return destResult.reset(null, 1); | ||
var needed = Math.ceil(countBits(n) / 7); | ||
return LengthResult.just(needed); | ||
return destResult.reset(null, needed); | ||
} | ||
function writeUnsignedVarIntInto(n, buffer, offset) { | ||
function writeUnsignedVarIntInto(destResult, n, buffer, offset) { | ||
if (typeof n !== 'number' || n < 0) { | ||
// TODO: integer check | ||
return WriteResult.error(errors.expected(n, 'unsigned integer')); | ||
return destResult.reset(errors.expected(n, 'unsigned integer'), null); | ||
} | ||
@@ -61,3 +61,3 @@ | ||
var remain = buffer.length - offset; | ||
return WriteResult.shortError(needed, remain, offset); | ||
return WriteResult.poolShortError(destResult, needed, remain, offset); | ||
} | ||
@@ -74,6 +74,6 @@ | ||
return WriteResult.just(end); | ||
return destResult.reset(null, end); | ||
} | ||
function readUnsignedVarIntFrom(buffer, offset) { | ||
function readUnsignedVarIntFrom(destResult, buffer, offset) { | ||
var start = offset; | ||
@@ -86,7 +86,7 @@ var n = 0; | ||
if (!(b & 0x80)) { | ||
return ReadResult.just(offset, n); | ||
return destResult.reset(null, offset, n); | ||
} | ||
} | ||
var got = offset - start; | ||
return ReadResult.shortError(got + 1, got, start, offset); | ||
return ReadResult.poolShortError(destResult, got + 1, got, start, offset); | ||
} | ||
@@ -98,2 +98,2 @@ | ||
return res; | ||
} | ||
} |
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
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
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
188709
4658
1