Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bufrw

Package Overview
Dependencies
Maintainers
3
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bufrw - npm Package Compare versions

Comparing version 0.9.16 to 0.9.17

errors.js

55

annotated_buffer.js

@@ -43,2 +43,4 @@ // Copyright (c) 2015 Uber Technologies, Inc.

// -- strings
AnnotatedBuffer.prototype.toString = function toString(encoding, start, end) {

@@ -58,2 +60,5 @@ var self = this;

// -- bytes
// istanbul ignore next
AnnotatedBuffer.prototype.copy = function copy(targetBuffer, targetStart, sourceStart, sourceEnd) {

@@ -75,2 +80,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.slice = function slice(start, end) {

@@ -89,2 +95,5 @@ var self = this;

// -- atom readers
// istanbul ignore next
AnnotatedBuffer.prototype.readInt8 = function readInt8(offset, noAssert) {

@@ -116,2 +125,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readUInt16LE = function readUInt16LE(offset, noAssert) {

@@ -143,2 +153,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readUInt32LE = function readUInt32LE(offset, noAssert) {

@@ -157,2 +168,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) {

@@ -171,2 +183,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readInt16LE = function readInt16LE(offset, noAssert) {

@@ -185,2 +198,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readInt16BE = function readInt16BE(offset, noAssert) {

@@ -199,2 +213,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readInt32LE = function readInt32LE(offset, noAssert) {

@@ -213,2 +228,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readInt32BE = function readInt32BE(offset, noAssert) {

@@ -227,2 +243,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) {

@@ -241,2 +258,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) {

@@ -255,2 +273,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {

@@ -269,2 +288,3 @@ var self = this;

// istanbul ignore next
AnnotatedBuffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {

@@ -283,2 +303,4 @@ var self = this;

// -- extras
// istanbul ignore next

@@ -292,6 +314,4 @@ AnnotatedBuffer.prototype.hexdump = function hexdump(options) {

if (options.boldStart === undefined) options.boldStart = true;
if (options.colored) {
options.decorateHexen = colorRegions;
options.decorateHuman = colorRegions;
}
options.decorateHexen = colorRegions;
options.decorateHuman = colorRegions;
var colors = options.colors || ['magenta', 'cyan', 'yellow', 'green'];

@@ -308,3 +328,5 @@ var colorI = 0;

if (ann && ann.start >= start && ann.start < end) {
ann.color = colors[i % colors.length];
if (options.colored) {
ann.color = colors[i % colors.length];
}
parts.push(ann);

@@ -320,9 +342,13 @@ last = i + 1;

}
if (options.colored) {
if (part.color) {
desc = color(desc, part.color);
if (options.highlight) {
desc = options.highlight(part.start, 0, desc);
}
} else if (part.start === part.end) {
desc += '@' + part.start.toString(16);
} else {
desc += '@[' + part.start.toString(16) + ',' +
part.end.toString(16) + ']';
}
if (options.highlight) {
desc = options.highlight(part.start, 0, desc);
}
return desc;

@@ -338,5 +364,6 @@ }).join(' ');

}
if (!ann) return str;
if (i < ann.start) return str;
if (i >= ann.start && i < ann.end) {
if (ann && options.colored &&
i >= ann.start &&
i < ann.end) {
str = stripColor(str);

@@ -346,5 +373,7 @@ str = color(str, colors[colorI]);

}
if (options.highlight) {
str = options.highlight(i, j, str);
}
return str;

@@ -351,0 +380,0 @@ }

@@ -27,2 +27,3 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var BufferRW = require('./base').BufferRW;
var errors = require('./errors');

@@ -58,2 +59,3 @@ function AtomRW(width, readAtomFrom, writeAtomInto) {

var remain = buffer.length - offset;
// istanbul ignore next
if (remain < self.width) {

@@ -65,99 +67,130 @@ return WriteResult.shortError(self.width, remain, offset);

var Int8 = AtomRW(1,
// jshint maxparams:5
function IntegerRW(width, min, max, readAtomFrom, writeAtomInto) {
if (!(this instanceof IntegerRW)) {
return new IntegerRW(width, min, max, readAtomFrom, writeAtomInto);
}
var self = this;
AtomRW.call(self, width, readAtomFrom, writeAtomInto);
self.min = min;
self.max = max;
}
inherits(IntegerRW, AtomRW);
IntegerRW.prototype.writeInto = function writeInto(value, buffer, offset) {
var self = this;
if (typeof value !== 'number') {
return WriteResult.error(errors.expected(value, 'a number'));
}
if (value < self.min || value > self.max) {
return WriteResult.error(errors.RangeError({
value: value,
min: self.min,
max: self.max
}), offset);
}
var remain = buffer.length - offset;
if (remain < self.width) {
return WriteResult.shortError(self.width, remain, offset);
}
return self.writeAtomInto(value, buffer, offset);
};
var Int8 = IntegerRW(1, -0x80, 0x7f,
function readInt8From(buffer, offset) {
var value = buffer.readInt8(offset);
var value = buffer.readInt8(offset, true);
return new ReadResult(null, offset + 1, value);
},
function writeInt8Into(value, buffer, offset) {
buffer.writeInt8(value, offset);
buffer.writeInt8(value, offset, true);
return new WriteResult(null, offset + 1);
});
var Int16BE = AtomRW(2,
var Int16BE = IntegerRW(2, -0x8000, 0x7fff,
function readInt16BEFrom(buffer, offset) {
var value = buffer.readInt16BE(offset);
var value = buffer.readInt16BE(offset, true);
return new ReadResult(null, offset + 2, value);
},
function writeInt16BEInto(value, buffer, offset) {
buffer.writeInt16BE(value, offset);
buffer.writeInt16BE(value, offset, true);
return new WriteResult(null, offset + 2);
});
var Int32BE = AtomRW(4,
var Int32BE = IntegerRW(4, -0x80000000, 0x7fffffff,
function readInt32BEFrom(buffer, offset) {
var value = buffer.readInt32BE(offset);
var value = buffer.readInt32BE(offset, true);
return new ReadResult(null, offset + 4, value);
},
function writeInt32BEInto(value, buffer, offset) {
buffer.writeInt32BE(value, offset);
buffer.writeInt32BE(value, offset, true);
return new WriteResult(null, offset + 4);
});
var Int16LE = AtomRW(2,
var Int16LE = IntegerRW(2, -0x8000, 0x7fff,
function readInt16LEFrom(buffer, offset) {
var value = buffer.readInt16LE(offset);
var value = buffer.readInt16LE(offset, true);
return new ReadResult(null, offset + 2, value);
},
function writeInt16LEInto(value, buffer, offset) {
buffer.writeInt16LE(value, offset);
buffer.writeInt16LE(value, offset, true);
return new WriteResult(null, offset + 2);
});
var Int32LE = AtomRW(4,
var Int32LE = IntegerRW(4, -0x80000000, 0x7fffffff,
function readInt32LEFrom(buffer, offset) {
var value = buffer.readInt32LE(offset);
var value = buffer.readInt32LE(offset, true);
return new ReadResult(null, offset + 4, value);
},
function writeInt32LEInto(value, buffer, offset) {
buffer.writeInt32LE(value, offset);
buffer.writeInt32LE(value, offset, true);
return new WriteResult(null, offset + 4);
});
var UInt8 = AtomRW(1,
var UInt8 = IntegerRW(1, 0, 0xff,
function readUInt8From(buffer, offset) {
var value = buffer.readUInt8(offset);
var value = buffer.readUInt8(offset, true);
return new ReadResult(null, offset + 1, value);
},
function writeUInt8Into(value, buffer, offset) {
buffer.writeUInt8(value, offset);
buffer.writeUInt8(value, offset, true);
return new WriteResult(null, offset + 1);
});
var UInt16BE = AtomRW(2,
var UInt16BE = IntegerRW(2, 0, 0xffff,
function readUInt16BEFrom(buffer, offset) {
var value = buffer.readUInt16BE(offset);
var value = buffer.readUInt16BE(offset, true);
return new ReadResult(null, offset + 2, value);
},
function writeUInt16BEInto(value, buffer, offset) {
buffer.writeUInt16BE(value, offset);
buffer.writeUInt16BE(value, offset, true);
return new WriteResult(null, offset + 2);
});
var UInt32BE = AtomRW(4,
var UInt32BE = IntegerRW(4, 0, 0xffffffff,
function readUInt32BEFrom(buffer, offset) {
var value = buffer.readUInt32BE(offset);
var value = buffer.readUInt32BE(offset, true);
return new ReadResult(null, offset + 4, value);
},
function writeUInt32BEInto(value, buffer, offset) {
buffer.writeUInt32BE(value, offset);
buffer.writeUInt32BE(value, offset, true);
return new WriteResult(null, offset + 4);
});
var UInt16LE = AtomRW(2,
var UInt16LE = IntegerRW(2, 0, 0xffff,
function readUInt16LEFrom(buffer, offset) {
var value = buffer.readUInt16LE(offset);
var value = buffer.readUInt16LE(offset, true);
return new ReadResult(null, offset + 2, value);
},
function writeUInt16LEInto(value, buffer, offset) {
buffer.writeUInt16LE(value, offset);
buffer.writeUInt16LE(value, offset, true);
return new WriteResult(null, offset + 2);
});
var UInt32LE = AtomRW(4,
var UInt32LE = IntegerRW(4, 0, 0xffffffff,
function readUInt32LEFrom(buffer, offset) {
var value = buffer.readUInt32LE(offset);
var value = buffer.readUInt32LE(offset, true);
return new ReadResult(null, offset + 4, value);
},
function writeUInt32LEInto(value, buffer, offset) {
buffer.writeUInt32LE(value, offset);
buffer.writeUInt32LE(value, offset, true);
return new WriteResult(null, offset + 4);

@@ -168,8 +201,13 @@ });

function readFloatLEFrom(buffer, offset) {
var value = buffer.readFloatLE(offset);
var value = buffer.readFloatLE(offset, true);
return new ReadResult(null, offset + 4, value);
},
function writeFloatLEInto(value, buffer, offset) {
buffer.writeFloatLE(value, offset);
return new WriteResult(null, offset + 4);
// istanbul ignore if
if (typeof value !== 'number') {
return WriteResult.error(errors.expected(value, 'a number'));
} else {
buffer.writeFloatLE(value, offset);
return new WriteResult(null, offset + 4);
}
});

@@ -179,8 +217,13 @@

function readFloatBEFrom(buffer, offset) {
var value = buffer.readFloatBE(offset);
var value = buffer.readFloatBE(offset, true);
return new ReadResult(null, offset + 4, value);
},
function writeFloatBEInto(value, buffer, offset) {
buffer.writeFloatBE(value, offset);
return new WriteResult(null, offset + 4);
// istanbul ignore if
if (typeof value !== 'number') {
return WriteResult.error(errors.expected(value, 'a number'));
} else {
buffer.writeFloatBE(value, offset);
return new WriteResult(null, offset + 4);
}
});

@@ -190,8 +233,13 @@

function readDoubleLEFrom(buffer, offset) {
var value = buffer.readDoubleLE(offset);
var value = buffer.readDoubleLE(offset, true);
return new ReadResult(null, offset + 8, value);
},
function writeDoubleLEInto(value, buffer, offset) {
buffer.writeDoubleLE(value, offset);
return new WriteResult(null, offset + 8);
// istanbul ignore if
if (typeof value !== 'number') {
return WriteResult.error(errors.expected(value, 'a number'));
} else {
buffer.writeDoubleLE(value, offset);
return new WriteResult(null, offset + 8);
}
});

@@ -201,8 +249,13 @@

function readDoubleBEFrom(buffer, offset) {
var value = buffer.readDoubleBE(offset);
var value = buffer.readDoubleBE(offset, true);
return new ReadResult(null, offset + 8, value);
},
function writeDoubleBEInto(value, buffer, offset) {
buffer.writeDoubleBE(value, offset);
return new WriteResult(null, offset + 8);
// istanbul ignore if
if (typeof value !== 'number') {
return WriteResult.error(errors.expected(value, 'a number'));
} else {
buffer.writeDoubleBE(value, offset);
return new WriteResult(null, offset + 8);
}
});

@@ -209,0 +262,0 @@

@@ -21,3 +21,3 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var TypedError = require('error/typed');
var errors = require('./errors');

@@ -29,20 +29,2 @@ module.exports.BufferRW = BufferRW;

var ShortBufferError = TypedError({
type: 'short-buffer',
message: 'expected at least {expected} bytes, only have {actual} @{offset}',
expected: null,
actual: null,
buffer: null,
offset: null
});
var ShortBufferRangedError = TypedError({
type: 'short-buffer',
message: 'expected at least {expected} bytes, only have {actual} @[{offset}:{endOffset}]',
expected: null,
actual: null,
offset: null,
endOffset: null
});
function BufferRW(byteLength, readFrom, writeInto) {

@@ -87,3 +69,3 @@ if (!(this instanceof BufferRW)) {

WriteResult.shortError = function shortError(expected, actual, offset) {
return new WriteResult(new ShortBufferError({
return new WriteResult(new errors.ShortBuffer({
expected: expected,

@@ -112,3 +94,3 @@ actual: actual,

if (endOffset === undefined) {
return new ReadResult(new ShortBufferError({
return new ReadResult(new errors.ShortBuffer({
expected: expected,

@@ -119,3 +101,3 @@ actual: actual,

} else {
return new ReadResult(new ShortBufferRangedError({
return new ReadResult(new errors.ShortBufferRanged({
expected: expected,

@@ -122,0 +104,0 @@ actual: actual,

@@ -23,2 +23,4 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var inherits = require('util').inherits;
var LengthResult = require('./base').LengthResult;

@@ -28,14 +30,4 @@ var WriteResult = require('./base').WriteResult;

var BufferRW = require('./base').BufferRW;
var TypedError = require('error/typed');
var inherits = require('util').inherits;
var errors = require('./errors');
var FixedLengthMismatchError = TypedError({
type: 'fixed-length-mismatch',
message: 'supplied length {got} mismatches fixed length {expected}',
expected: null,
got: null
});
// TODO: could support string encoding like prefixed length does
function FixedWidthRW(length, readFrom, writeInto) {

@@ -54,3 +46,3 @@ if (!(this instanceof FixedWidthRW)) {

if (slice.length !== self.length) {
return LengthResult.error(FixedLengthMismatchError({
return LengthResult.error(errors.FixedLengthMismatch({
expected: self.length,

@@ -67,3 +59,3 @@ got: slice.length

if (slice.length !== self.length) {
return WriteResult.error(FixedLengthMismatchError({
return WriteResult.error(errors.FixedLengthMismatch({
expected: self.length,

@@ -70,0 +62,0 @@ got: slice.length

@@ -24,39 +24,47 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var hex = require('hexer');
var TypedError = require('error/typed');
var util = require('util');
var Result = require('./result');
var errors = require('./errors');
var AnnotatedBuffer = require('./annotated_buffer');
var errorHighlighter = require('./error_highlighter');
var ShortReadError = TypedError({
type: 'short-read',
message: 'short read, {remaining} byte left over after consuming {offset}',
remaining: null,
buffer: null,
offset: null
});
function makeAnnotatedBuffer(buffer, start, clear) {
// istanbul ignore if
if (start > 0) buffer = buffer.slice(start);
// istanbul ignore if
if (clear) buffer.fill(0);
return new AnnotatedBuffer(buffer);
}
var ShortWriteError = TypedError({
type: 'short-write',
message: 'short write, {remaining} byte left over after writing {offset}',
remaining: null,
buffer: null,
offset: null
});
function annotateError(res1, res2, start, annBuf) {
// istanbul ignore if
if (!res2.err ||
res2.offset !== res1.offset - start ||
res2.err.type !== res1.err.type ||
res2.err.message !== res1.err.message) {
res1.err = errors.UnstableRW(res1.err, {
otherMessage: res2.err && res2.err.message
});
} else {
res1.err.buffer = annBuf;
}
}
var emptyBuffer = Buffer(0);
function fromBuffer(struct, buffer, offset) {
return fromBufferResult(struct, buffer, offset).toValue();
function fromBuffer(rw, buffer, offset) {
return fromBufferResult(rw, buffer, offset).toValue();
}
function byteLength(struct, value) {
return byteLengthResult(struct, value).toValue();
function byteLength(rw, value) {
return byteLengthResult(rw, value).toValue();
}
function toBuffer(struct, value) {
return toBufferResult(struct, value).toValue();
function toBuffer(rw, value) {
return toBufferResult(rw, value).toValue();
}
function intoBuffer(struct, buffer, value) {
return intoBufferResult(struct, buffer, value).toValue();
function intoBuffer(rw, buffer, value) {
return intoBufferResult(rw, buffer, value).toValue();
}

@@ -67,33 +75,33 @@

/* istanbul ignore next */
function fromBufferTuple(struct, buffer, offset) {
return fromBufferResult(struct, buffer, offset).toTuple();
function fromBufferTuple(rw, buffer, offset) {
return fromBufferResult(rw, buffer, offset).toTuple();
}
/* istanbul ignore next */
function byteLengthTuple(struct, value) {
return byteLengthResult(struct, value).toTuple();
function byteLengthTuple(rw, value) {
return byteLengthResult(rw, value).toTuple();
}
/* istanbul ignore next */
function toBufferTuple(struct, value) {
return toBufferResult(struct, value).toTuple();
function toBufferTuple(rw, value) {
return toBufferResult(rw, value).toTuple();
}
/* istanbul ignore next */
function intoBufferTuple(struct, buffer, value) {
return intoBufferResult(struct, buffer, value).toTuple();
function intoBufferTuple(rw, buffer, value) {
return intoBufferResult(rw, buffer, value).toTuple();
}
function fromBufferResult(struct, buffer, offset) {
offset = offset || 0;
var res = struct.readFrom(buffer, offset);
offset = res.offset;
var err = res.err;
if (!err && offset !== buffer.length) {
err = ShortReadError({
remaining: buffer.length - offset,
function checkAllReadFrom(res, buffer) {
if (!res.err && res.offset !== buffer.length) {
res.err = errors.ShortRead({
remaining: buffer.length - res.offset,
buffer: buffer,
offset: offset
offset: res.offset
});
}
return res;
}
function genericResult(err, value, buffer, offset) {
if (err) {

@@ -103,7 +111,20 @@ if (err.offset === undefined) err.offset = offset;

}
return new Result(err, res.value);
return new Result(err, value);
}
function byteLengthResult(struct, value) {
var lenRes = struct.byteLength(value);
function fromBufferResult(rw, buffer, offset) {
var start = offset || 0;
var res = rw.readFrom(buffer, start);
res = checkAllReadFrom(res, buffer);
if (res.err) {
var annBuf = makeAnnotatedBuffer(buffer, start, false);
var res2 = rw.readFrom(annBuf, 0);
res2 = checkAllReadFrom(res2, buffer);
annotateError(res, res2, start, annBuf);
}
return genericResult(res.err, res.value, buffer, res.offset);
}
function byteLengthResult(rw, value) {
var lenRes = rw.byteLength(value);
if (lenRes.err) return new Result(lenRes.err, 0);

@@ -113,4 +134,4 @@ else return new Result(null, lenRes.length);

function toBufferResult(struct, value) {
var lenRes = struct.byteLength(value);
function toBufferResult(rw, value) {
var lenRes = rw.byteLength(value);
if (lenRes.err) return new Result(lenRes.err, emptyBuffer);

@@ -120,24 +141,22 @@ var length = lenRes.length;

// buffer.fill(0); TODO option
return intoBufferResult(struct, buffer, value);
return intoBufferResult(rw, buffer, value);
}
function intoBufferResult(struct, buffer, value) {
var writeRes = struct.writeInto(value, buffer, 0);
if (writeRes.err) {
// istanbul ignore else
if (!Buffer.isBuffer(writeRes.err.buffer)) writeRes.err.buffer = buffer;
if (typeof writeRes.err.offset !== 'number') writeRes.err.offset = writeRes.offset;
return new Result(writeRes.err, buffer);
}
var offset = writeRes.offset;
if (offset !== buffer.length) {
return new Result(ShortWriteError({
remaining: buffer.length - offset,
function checkAllWroteOver(res, buffer) {
if (!res.err && res.offset !== buffer.length) {
res.err = errors.ShortWrite({
remaining: buffer.length - res.offset,
buffer: buffer,
offset: offset
}), buffer);
offset: res.offset
});
}
return new Result(null, buffer);
return res;
}
function intoBufferResult(rw, buffer, value) {
var res = rw.writeInto(value, buffer, 0);
res = checkAllWroteOver(res, buffer);
return genericResult(res.err, buffer, buffer, res.offset);
}
// istanbul ignore next TODO

@@ -148,3 +167,9 @@ function formatError(err, options) {

var str = util.format('%s: %s\n', name, err.message);
if (Buffer.isBuffer(err.buffer)) {
if (err.buffer && err.buffer.hexdump) {
str += err.buffer.hexdump({
colored: options.color,
boldStart: false,
highlight: options.color ? errorHighlighter(err, options) : null
});
} else if (Buffer.isBuffer(err.buffer)) {
if (options.color) {

@@ -151,0 +176,0 @@ str += formatBufferColored(err, options);

{
"name": "bufrw",
"version": "0.9.16",
"version": "0.9.17",
"description": "Buffer Reading and Writing",

@@ -5,0 +5,0 @@ "keywords": [],

@@ -23,3 +23,2 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var TypedError = require('error/typed');
var inherits = require('util').inherits;

@@ -31,10 +30,4 @@

var BufferRW = require('./base').BufferRW;
var errors = require('./errors');
var InvalidArgumentError = TypedError({
type: 'invalid-argument',
message: 'invalid argument, not an array',
argType: null,
argConstructor: null
});
function RepeatRW(countrw, repeatedrw) {

@@ -52,6 +45,3 @@ if (!(this instanceof RepeatRW)) {

if (!Array.isArray(values)) {
return LengthResult.error(InvalidArgumentError({
argType: typeof values,
argConstructor: values.constructor.name
}));
return LengthResult.error(errors.expected(values, 'an array'));
}

@@ -71,6 +61,3 @@ var self = this;

if (!Array.isArray(values)) {
return WriteResult.error(InvalidArgumentError({
argType: typeof values,
argConstructor: values.constructor.name
}), offset);
return WriteResult.error(errors.expected(values, 'an array'), offset);
}

@@ -77,0 +64,0 @@ var self = this;

@@ -23,6 +23,6 @@ // Copyright (c) 2015 Uber Technologies, Inc.

function Result(error, value) {
function Result(err, value) {
var self = this;
self.err = err || null;
self.value = value;
self.error = error || null;
}

@@ -32,4 +32,4 @@

var self = this;
if (self.error) {
throw self.error;
if (self.err) {
throw self.err;
} else {

@@ -43,3 +43,3 @@ return self.value;

var self = this;
callback(self.error, self.value);
callback(self.err, self.value);
};

@@ -51,3 +51,3 @@

var self = this;
return [self.error, self.value];
return [self.err, self.value];
};

@@ -23,3 +23,2 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var TypedError = require('error/typed');
var inherits = require('util').inherits;

@@ -31,10 +30,4 @@

var BufferRW = require('./base').BufferRW;
var errors = require('./errors');
var InvalidArgumentError = TypedError({
type: 'invalid-argument',
message: 'invalid argument, expected array or null',
argType: null,
argConstructor: null
});
function SeriesRW(rws) {

@@ -54,6 +47,3 @@ if (!Array.isArray(rws) || arguments.length > 1) {

if (!Array.isArray(values) && values !== null) {
return LengthResult.error(InvalidArgumentError({
argType: typeof values,
argConstructor: values.constructor.name
}));
return LengthResult.error(errors.expected(values, 'an array or null'));
}

@@ -73,6 +63,3 @@ var self = this;

if (!Array.isArray(values) && values !== null) {
return WriteResult.error(InvalidArgumentError({
argType: typeof values,
argConstructor: values.constructor.name
}));
return WriteResult.error(errors.expected(values, 'an array or null'), offset);
}

@@ -79,0 +66,0 @@ var res = new WriteResult(null, offset);

@@ -57,3 +57,3 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var res = toBufferResult(self.chunkRW, value);
var err = res.error;
var err = res.err;
var buffer = res.value;

@@ -60,0 +60,0 @@ if (err) {

@@ -73,17 +73,17 @@ // Copyright (c) 2015 Uber Technologies, Inc.

// istanbul ignore next
ConcatReadBuffer.prototype.readUInt8 = function readUInt8(offset) {
ConcatReadBuffer.prototype.readUInt8 = function readUInt8(offset, noAssert) {
var self = this;
return self.buffer.readUInt8(offset);
return self.buffer.readUInt8(offset, noAssert);
};
// istanbul ignore next
ConcatReadBuffer.prototype.readUInt16BE = function readUInt16BE(offset) {
ConcatReadBuffer.prototype.readUInt16BE = function readUInt16BE(offset, noAssert) {
var self = this;
return self.buffer.readUInt16BE(offset);
return self.buffer.readUInt16BE(offset, noAssert);
};
// istanbul ignore next
ConcatReadBuffer.prototype.readUInt32BE = function readUInt32BE(offset) {
ConcatReadBuffer.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) {
var self = this;
return self.buffer.readUInt32BE(offset);
return self.buffer.readUInt32BE(offset, noAssert);
};

@@ -90,0 +90,0 @@

@@ -23,40 +23,6 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var TypedError = require('error/typed');
var ConcatReadBuffer = require('./concat_read_buffer');
var errors = require('../errors');
var fromBufferResult = require('../interface').fromBufferResult;
var ShortReadError = TypedError({
type: 'short-read',
message: 'short read, {remaining} byte left over after consuming {offset}',
remaining: null,
buffer: null,
offset: null
});
var ZeroLengthChunkError = TypedError({
type: 'zero-length-chunk',
message: 'zero length chunk encountered'
});
var BrokenReaderStateError = TypedError({
type: 'broken-reader-state',
message: 'reader in invalid state {state} expecting {expecting} avail {aval}',
state: null,
expecting: null,
avail: null
});
var TruncatedReadError = TypedError({
type: 'truncated-read',
message: 'read truncated by end of stream with {length} bytes in buffer',
length: null,
buffer: null,
state: null,
expecting: null
});
var BadSizeRWError = TypedError({
type: 'bad-size-rw',
message: 'bad sizeRW, expected a fixed-width atom'
});
module.exports = ReadMachine;

@@ -75,3 +41,3 @@

if (typeof sizeRW.width !== 'number') {
throw BadSizeRWError();
throw errors.expected(sizeRW, 'atomic RW');
}

@@ -108,3 +74,3 @@ var self = this;

default:
err = BrokenReaderStateError({
err = errors.BrokenReaderState({
state: self.state,

@@ -125,3 +91,3 @@ expecting: self.expecting,

if (!err && !sizeRes.value) {
err = ZeroLengthChunkError();
err = errors.ZeroLengthChunk();
}

@@ -145,3 +111,3 @@ if (err) {

if (!chunk.length) {
return BrokenReaderStateError({
return errors.BrokenReaderState({
state: self.state,

@@ -154,26 +120,6 @@ expecting: self.expecting,

self.state = States.PendingLength;
var err = self._readChunk(chunk);
if (err) return err;
return null;
};
ReadMachine.prototype._readChunk = function _readChunk(chunk) {
var self = this;
var res = self.chunkRW.readFrom(chunk, 0);
var err = res.err;
// istanbul ignore if
if (!err && res.offset < chunk.length) {
err = ShortReadError({
remaining: chunk.length - res.offset,
buffer: chunk,
offset: res.offset
});
}
if (err) {
// istanbul ignore else
if (err.buffer === undefined) err.buffer = chunk;
if (err.offset === undefined) err.offset = res.offset;
return err;
var res = fromBufferResult(self.chunkRW, chunk, 0);
if (res.err) {
return res.err;
} else {

@@ -192,3 +138,3 @@ self.emit(res.value);

self.state = States.PendingLength;
return TruncatedReadError({
return errors.TruncatedRead({
length: avail,

@@ -195,0 +141,0 @@ state: self.state,

@@ -23,3 +23,2 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var TypedError = require('error/typed');
var inherits = require('util').inherits;

@@ -30,11 +29,5 @@

var ReadResult = require('./base').ReadResult;
var errors = require('./errors');
var VariableBufferRW = require('./variable_buffer_rw');
var InvalidArgumentError = TypedError({
type: 'invalid-argument',
message: 'invalid argument, expected string, null, or undefined',
argType: null,
argConstructor: null
});
function StringRW(sizerw, encoding) {

@@ -56,6 +49,3 @@ if (!(this instanceof StringRW)) {

} else if (str !== null && str !== undefined) {
return LengthResult.error(InvalidArgumentError({
argType: typeof str,
argConstructor: str.constructor.name
}));
return LengthResult.error(errors.expected(str, 'string, null, or undefined'));
}

@@ -74,6 +64,3 @@ var len = self.sizerw.byteLength(length);

} else if (str !== null && str !== undefined) {
return WriteResult.error(InvalidArgumentError({
argType: typeof str,
argConstructor: str.constructor.name
}), offset);
return WriteResult.error(errors.expected(str, 'string, null, or undefined'), offset);
}

@@ -80,0 +67,0 @@ var res = self.sizerw.writeInto(length, buffer, offset);

@@ -24,3 +24,2 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var inherits = require('util').inherits;
var TypedError = require('error/typed');

@@ -31,8 +30,4 @@ var LengthResult = require('./base').LengthResult;

var BufferRW = require('./base').BufferRW;
var errors = require('./errors');
var MissingFieldError = TypedError({
type: 'missing.field',
message: 'missing field {field} on {struct}'
});
function StructRW(cons, fields, opts) {

@@ -77,3 +72,3 @@ if (!(this instanceof StructRW)) {

if (field.name && !obj.hasOwnProperty(field.name)) {
return LengthResult.error(MissingFieldError({
return LengthResult.error(errors.MissingStructField({
field: field.name,

@@ -105,3 +100,3 @@ struct: self.cons.name

if (field.name && !obj.hasOwnProperty(field.name)) {
return WriteResult.error(MissingFieldError({
return WriteResult.error(errors.MissingStructField({
field: field.name,

@@ -108,0 +103,0 @@ struct: self.cons.name

@@ -24,3 +24,2 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var inherits = require('util').inherits;
var TypedError = require('error/typed');

@@ -31,9 +30,4 @@ var LengthResult = require('./base').LengthResult;

var BufferRW = require('./base').BufferRW;
var errors = require('./errors');
var InvalidValueError = TypedError({
type: 'invalid-switch-value',
message: 'invalid switch value {value}',
value: null
});
// TODO: cases should be an es6 map

@@ -61,3 +55,3 @@

if (datarw === undefined) {
return LengthResult.error(InvalidValueError({
return LengthResult.error(errors.InvalidSwitchValue({
value: val

@@ -79,3 +73,3 @@ }));

if (datarw === undefined) {
return WriteResult.error(InvalidValueError({
return WriteResult.error(errors.InvalidSwitchValue({
value: val

@@ -98,3 +92,3 @@ }), offset);

if (datarw === undefined) {
return ReadResult.error(InvalidValueError({
return ReadResult.error(errors.InvalidSwitchValue({
value: val

@@ -101,0 +95,0 @@ }), offset);

@@ -23,9 +23,7 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var hex = require('hexer');
var hexdiff = require('hexer/diff');
var util = require('util');
var formatError = require('./interface').formatError;
var intoBufferResult = require('./interface').intoBufferResult;
var fromBufferResult = require('./interface').fromBufferResult;
var AnnotatedBuffer = require('./annotated_buffer');
var errorHighlighter = require('./error_highlighter');

@@ -110,3 +108,3 @@ module.exports.cases = testCases;

var res = intoBufferResult(self.rw, got, val);
var err = res.error;
var err = res.err;
if (err) {

@@ -129,2 +127,3 @@ if (testCase.error) {

if (got.toString('hex') !== buf.toString('hex')) {
// TODO: re-run write with an annotated buffer
self.assert.comment('expected v actual:\n' +

@@ -143,5 +142,4 @@ hexdiff(buf, got, {colored: true}));

var buffer = Buffer(testCase.bytes);
buffer = new AnnotatedBuffer(buffer);
var res = fromBufferResult(self.rw, buffer);
var err = res.error;
var err = res.err;
var got = res.value;

@@ -174,24 +172,10 @@ if (err) {

var self = this;
var highlight = errorHighlighter(err);
// istanbul ignore next
var errName = err.name || err.constructor.name;
var dump = util.format('%s error %s: %s\n',
kind, errName, err.message);
if (err.buffer && err.buffer.hexdump) {
dump += err.buffer.hexdump({
colored: true,
boldStart: false,
highlight: highlight
});
// istanbul ignore else
} else if (err.buffer) {
dump += hex(err.buffer, {
colored: true,
decorateHexen: highlight,
decorateHuman: highlight
});
}
dump.split(/\n/).forEach(function each(line) {
self.assert.comment(line);
var dump = kind + ' error ' + formatError(err, {
color: true
});
dump.split(/\n/).forEach(function each(line, i, lines) {
if (line.length || i < lines.length - 1) {
self.assert.comment(line);
}
});
};

@@ -198,0 +182,0 @@

@@ -33,2 +33,22 @@ // Copyright (c) 2015 Uber Technologies, Inc.

// value validation
{
writeTest: {
value: null,
error: {
type: 'bufrw.invalid-argument',
message: 'invalid argument, expected a number'
}
}
},
{
writeTest: {
value: 1000,
error: {
type: 'bufrw.range-error',
message: 'value 1000 out of range, min: -128 max: 127'
}
}
},
// truncation

@@ -40,4 +60,4 @@ {

error: {
type: 'short-buffer',
name: 'ShortBufferError',
type: 'bufrw.short-buffer',
name: 'BufrwShortBufferError',
message: 'expected at least 1 bytes, only have 0 @0',

@@ -52,4 +72,4 @@ actual: 0,

error: {
name: 'ShortBufferError',
type: 'short-buffer',
name: 'BufrwShortBufferError',
type: 'bufrw.short-buffer',
message: 'expected at least 1 bytes, only have 0 @0',

@@ -56,0 +76,0 @@ actual: 0,

@@ -38,3 +38,3 @@ // Copyright (c) 2015 Uber Technologies, Inc.

error: {
type: 'fixed-length-mismatch',
type: 'bufrw.fixed-length-mismatch',
message: 'supplied length 1 mismatches fixed length 8',

@@ -48,3 +48,3 @@ expected: 8,

error: {
type: 'fixed-length-mismatch',
type: 'bufrw.fixed-length-mismatch',
message: 'supplied length 1 mismatches fixed length 8',

@@ -58,4 +58,4 @@ expected: 8,

error: {
name: 'ShortBufferError',
type: 'short-buffer',
name: 'BufrwShortBufferError',
type: 'bufrw.short-buffer',
message: 'expected at least 8 bytes, only have 1 @0',

@@ -62,0 +62,0 @@ offset: 0,

@@ -68,4 +68,4 @@ // Copyright (c) 2015 Uber Technologies, Inc.

error: {
type: 'invalid-argument',
message: 'invalid argument, not an array',
type: 'bufrw.invalid-argument',
message: 'invalid argument, expected an array',
argType: 'number',

@@ -78,4 +78,4 @@ argConstructor: 'Number'

error: {
type: 'invalid-argument',
message: 'invalid argument, not an array',
type: 'bufrw.invalid-argument',
message: 'invalid argument, expected an array',
argType: 'number',

@@ -82,0 +82,0 @@ argConstructor: 'Number'

@@ -26,2 +26,3 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var BufferRW = require('../base').BufferRW;
var LengthResult = require('../base').LengthResult;

@@ -45,6 +46,22 @@ var WriteResult = require('../base').WriteResult;

function orFillZero(rw) {
return new BufferRW(
rw.byteLength.bind(rw),
rw.readFrom.bind(rw),
writeInto);
function writeInto(value, buffer, offset) {
if (value === null || value === undefined) {
var end = offset + rw.width;
buffer.fill(0, offset, end);
return WriteResult.just(end);
} else {
return rw.writeInto(value, buffer, offset);
}
}
}
var tinyShortWord = SeriesRW(
atoms.UInt8,
atoms.UInt16BE,
atoms.UInt32BE);
orFillZero(atoms.UInt8),
orFillZero(atoms.UInt16BE),
orFillZero(atoms.UInt32BE));

@@ -71,5 +88,5 @@ test('SeriesRW: tinyShortWord', testRW.cases(tinyShortWord, [

lengthTest: {value: 42, error: {
type: 'invalid-argument',
name: 'InvalidArgumentError',
message: 'invalid argument, expected array or null',
type: 'bufrw.invalid-argument',
name: 'BufrwInvalidArgumentError',
message: 'invalid argument, expected an array or null',
argType: 'number',

@@ -79,5 +96,5 @@ argConstructor: 'Number'

writeTest: {value: 42, error: {
name: 'InvalidArgumentError',
type: 'invalid-argument',
message: 'invalid argument, expected array or null',
name: 'BufrwInvalidArgumentError',
type: 'bufrw.invalid-argument',
message: 'invalid argument, expected an array or null',
argType: 'number',

@@ -84,0 +101,0 @@ argConstructor: 'Number'

@@ -37,4 +37,4 @@ // Copyright (c) 2015 Uber Technologies, Inc.

error: {
name: 'ShortBufferError',
type: 'short-buffer',
name: 'BufrwShortBufferError',
type: 'bufrw.short-buffer',
message: 'expected at least 1 bytes, only have 0 @0',

@@ -58,4 +58,4 @@ offset: 0,

error: {
name: 'ShortBufferError',
type: 'short-buffer',
name: 'BufrwShortBufferError',
type: 'bufrw.short-buffer',
message: 'expected at least 5 bytes, only have 3 @0',

@@ -62,0 +62,0 @@ offset: 0,

@@ -104,4 +104,4 @@ // Copyright (c) 2015 Uber Technologies, Inc.

assert.deepEqual(err, {
type: 'zero-length-chunk',
name: 'ZeroLengthChunkError',
type: 'bufrw.zero-length-chunk',
name: 'BufrwZeroLengthChunkError',
message: 'zero length chunk encountered'

@@ -139,8 +139,14 @@ }, 'expected ZeroLengthChunkError');

assert.deepEqual(err, {
type: 'short-buffer',
name: 'ShortBufferError',
type: 'bufrw.short-buffer',
name: 'BufrwShortBufferError',
message: 'expected at least 1 bytes, only have 0 @[1:2]',
expected: 1,
actual: 0,
buffer: Buffer([2, 1]),
buffer: {
buffer: Buffer([2, 1]),
annotations: [
{ kind: 'read', name: 'UInt8', start: 0, end: 1, value: 2 },
{ kind: 'read', name: 'UInt8', start: 1, end: 2, value: 1 }
]
},
offset: 1,

@@ -154,4 +160,4 @@ endOffset: 2

assert.deepEqual(err, {
type: 'truncated-read',
name: 'TruncatedReadError',
type: 'bufrw.truncated-read',
name: 'BufrwTruncatedReadError',
message: 'read truncated by end of stream with 1 bytes in buffer',

@@ -158,0 +164,0 @@ state: 0,

@@ -78,4 +78,4 @@ // Copyright (c) 2015 Uber Technologies, Inc.

lengthTest: {value: 42, error: {
type: 'invalid-argument',
name: 'InvalidArgumentError',
type: 'bufrw.invalid-argument',
name: 'BufrwInvalidArgumentError',
message: 'invalid argument, expected string, null, or undefined',

@@ -86,4 +86,4 @@ argType: 'number',

writeTest: {value: 42, error: {
name: 'InvalidArgumentError',
type: 'invalid-argument',
name: 'BufrwInvalidArgumentError',
type: 'bufrw.invalid-argument',
message: 'invalid argument, expected string, null, or undefined',

@@ -100,4 +100,4 @@ argType: 'number',

error: {
name: 'ShortBufferError',
type: 'short-buffer',
name: 'BufrwShortBufferError',
type: 'bufrw.short-buffer',
message: 'expected at least 3 bytes, only have 2 @[0:1]',

@@ -104,0 +104,0 @@ offset: 0,

@@ -160,4 +160,4 @@ // Copyright (c) 2015 Uber Technologies, Inc.

error: {
name: 'MissingFieldError',
type: 'missing.field',
name: 'BufrwMissingStructFieldError',
type: 'bufrw.missing.struct-field',
message: 'missing field baz on Thing',

@@ -172,4 +172,4 @@ struct: 'Thing',

error: {
name: 'MissingFieldError',
type: 'missing.field',
name: 'BufrwMissingStructFieldError',
type: 'bufrw.missing.struct-field',
message: 'missing field baz on Thing',

@@ -176,0 +176,0 @@ struct: 'Thing',

@@ -61,3 +61,3 @@ // Copyright (c) 2015 Uber Technologies, Inc.

error: {
type: 'invalid-switch-value',
type: 'bufrw.invalid-switch-value',
message: 'invalid switch value 42',

@@ -71,3 +71,3 @@ value: 42

error: {
type: 'invalid-switch-value',
type: 'bufrw.invalid-switch-value',
message: 'invalid switch value 42',

@@ -80,3 +80,3 @@ value: 42

error: {
type: 'invalid-switch-value',
type: 'bufrw.invalid-switch-value',
message: 'invalid switch value 42',

@@ -83,0 +83,0 @@ value: 42

@@ -88,3 +88,3 @@ // Copyright (c) 2015 Uber Technologies, Inc.

assert.equal(results[6],
'00\x1b[36m:\x1b[0m 00 \x1b[30m\x1b[1m.\x1b[0m');
'00\x1b[36m:\x1b[0m \x1b[31m\x1b[1m00\x1b[0m \x1b[31m\x1b[1m.\x1b[0m');

@@ -91,0 +91,0 @@ assert.end();

@@ -62,4 +62,4 @@ // Copyright (c) 2015 Uber Technologies, Inc.

lengthTest: {value: {}, error: {
type: 'invalid-argument',
name: 'InvalidArgumentError',
type: 'bufrw.invalid-argument',
name: 'BufrwInvalidArgumentError',
message: 'invalid argument, expected buffer, null, or undefined',

@@ -70,4 +70,4 @@ argType: 'object',

writeTest: {value: {}, error: {
name: 'InvalidArgumentError',
type: 'invalid-argument',
name: 'BufrwInvalidArgumentError',
type: 'bufrw.invalid-argument',
message: 'invalid argument, expected buffer, null, or undefined',

@@ -84,4 +84,4 @@ argConstructor: 'Object',

error: {
name: 'ShortBufferError',
type: 'short-buffer',
name: 'BufrwShortBufferError',
type: 'bufrw.short-buffer',
message: 'expected at least 5 bytes, only have 3 @[0:1]',

@@ -88,0 +88,0 @@ offset: 0,

@@ -23,3 +23,2 @@ // Copyright (c) 2015 Uber Technologies, Inc.

var TypedError = require('error/typed');
var inherits = require('util').inherits;

@@ -31,10 +30,4 @@

var BufferRW = require('./base').BufferRW;
var errors = require('./errors');
var InvalidArgumentError = TypedError({
type: 'invalid-argument',
message: 'invalid argument, expected buffer, null, or undefined',
argType: null,
argConstructor: null
});
function VariableBufferRW(sizerw) {

@@ -58,6 +51,3 @@ if (!(this instanceof VariableBufferRW)) {

} else {
return LengthResult.error(InvalidArgumentError({
argType: typeof buf,
argConstructor: buf.constructor.name
}));
return LengthResult.error(errors.expected(buf, 'buffer, null, or undefined'));
}

@@ -78,6 +68,3 @@ var len = self.sizerw.byteLength(length);

} else {
return WriteResult.error(InvalidArgumentError({
argType: typeof buf,
argConstructor: buf.constructor.name
}), offset);
return WriteResult.error(errors.expected(buf, 'buffer, null, or undefined'), offset);
}

@@ -84,0 +71,0 @@ var res = self.sizerw.writeInto(length, buffer, offset);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc