Comparing version 0.0.0 to 0.0.1
@@ -10,2 +10,3 @@ /*! | ||
const encoding = require('./encoding'); | ||
const EncodingError = require('./error'); | ||
const BufferReader = require('./reader'); | ||
@@ -17,21 +18,30 @@ const BufferWriter = require('./writer'); | ||
Object.setPrototypeOf(exports, encoding); | ||
exports.bufio = exports; | ||
exports.encoding = encoding; | ||
exports.EncodingError = EncodingError; | ||
exports.BufferReader = BufferReader; | ||
exports.reader = (data, zeroCopy) => new BufferReader(data, zeroCopy); | ||
exports.BufferWriter = BufferWriter; | ||
exports.writer = () => new BufferWriter(); | ||
exports.StaticWriter = StaticWriter; | ||
exports.static = (size) => new StaticWriter(size); | ||
exports.pool = (size) => StaticWriter.pool(size); | ||
exports.SizeWriter = SizeWriter; | ||
exports.size = (size) => new SizeWriter(size); | ||
exports.HashWriter = HashWriter; | ||
exports.HashWriter = HashWriter; | ||
exports.hash = (Hash, ...args) => new HashWriter(Hash, ...args); | ||
exports.read = function read(data, zeroCopy) { | ||
return new BufferReader(data, zeroCopy); | ||
}; | ||
exports.write = function write(size) { | ||
return size != null | ||
? new StaticWriter(size) | ||
: new BufferWriter(); | ||
}; | ||
exports.pool = function pool(size) { | ||
return StaticWriter.pool(size); | ||
}; | ||
exports.size = function size(_size) { | ||
return new SizeWriter(_size); | ||
}; | ||
exports.hash = function hash(ctx) { | ||
return new HashWriter(ctx); | ||
}; |
@@ -10,159 +10,19 @@ /*! | ||
const assert = require('assert'); | ||
const {U64, I64} = require('n64'); | ||
const UINT128_MAX = U64.UINT64_MAX.shrn(7); | ||
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER; | ||
const encoding = exports; | ||
const EncodingError = require('./error'); | ||
/** | ||
* An empty buffer. | ||
* @const {Buffer} | ||
* @default | ||
/* | ||
* Constants | ||
*/ | ||
encoding.DUMMY = Buffer.from([0]); | ||
const BASE128_MAX = U64.UINT64_MAX.shrn(7); | ||
const {MAX_SAFE_INTEGER} = Number; | ||
/** | ||
* A hash of all zeroes with a `1` at the | ||
* end (used for the SIGHASH_SINGLE bug). | ||
* @const {Buffer} | ||
* @default | ||
/* | ||
* Module | ||
*/ | ||
encoding.ONE_HASH = Buffer.from( | ||
'0100000000000000000000000000000000000000000000000000000000000000', | ||
'hex' | ||
); | ||
const encoding = exports; | ||
/** | ||
* A hash of all zeroes. | ||
* @const {Buffer} | ||
* @default | ||
*/ | ||
encoding.ZERO_HASH = Buffer.from( | ||
'0000000000000000000000000000000000000000000000000000000000000000', | ||
'hex' | ||
); | ||
/** | ||
* A hash of all 0xff. | ||
* @const {Buffer} | ||
* @default | ||
*/ | ||
encoding.MAX_HASH = Buffer.from( | ||
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', | ||
'hex' | ||
); | ||
/** | ||
* A hash of all zeroes. | ||
* @const {String} | ||
* @default | ||
*/ | ||
encoding.NULL_HASH = | ||
'0000000000000000000000000000000000000000000000000000000000000000'; | ||
/** | ||
* A hash of all 0xff. | ||
* @const {String} | ||
* @default | ||
*/ | ||
encoding.HIGH_HASH = | ||
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'; | ||
/** | ||
* A hash of all zeroes. | ||
* @const {Buffer} | ||
* @default | ||
*/ | ||
encoding.ZERO_HASH160 = Buffer.from( | ||
'0000000000000000000000000000000000000000', | ||
'hex' | ||
); | ||
/** | ||
* A hash of all 0xff. | ||
* @const {String} | ||
* @default | ||
*/ | ||
encoding.MAX_HASH160 = Buffer.from( | ||
'ffffffffffffffffffffffffffffffffffffffff', | ||
'hex' | ||
); | ||
/** | ||
* A hash of all zeroes. | ||
* @const {String} | ||
* @default | ||
*/ | ||
encoding.NULL_HASH160 = '0000000000000000000000000000000000000000'; | ||
/** | ||
* A hash of all 0xff. | ||
* @const {String} | ||
* @default | ||
*/ | ||
encoding.HIGH_HASH160 = 'ffffffffffffffffffffffffffffffffffffffff'; | ||
/** | ||
* A compressed pubkey of all zeroes. | ||
* @const {Buffer} | ||
* @default | ||
*/ | ||
encoding.ZERO_KEY = Buffer.from( | ||
'000000000000000000000000000000000000000000000000000000000000000000', | ||
'hex' | ||
); | ||
/** | ||
* A 73 byte signature of all zeroes. | ||
* @const {Buffer} | ||
* @default | ||
*/ | ||
encoding.ZERO_SIG = Buffer.from('' | ||
+ '0000000000000000000000000000000000000000000000000000000000000000' | ||
+ '0000000000000000000000000000000000000000000000000000000000000000' | ||
+ '000000000000000000', | ||
'hex' | ||
); | ||
/** | ||
* A 64 byte signature of all zeroes. | ||
* @const {Buffer} | ||
* @default | ||
*/ | ||
encoding.ZERO_SIG64 = Buffer.from('' | ||
+ '0000000000000000000000000000000000000000000000000000000000000000' | ||
+ '0000000000000000000000000000000000000000000000000000000000000000', | ||
'hex' | ||
); | ||
/** | ||
* 4 zero bytes. | ||
* @const {Buffer} | ||
* @default | ||
*/ | ||
encoding.ZERO_U32 = Buffer.from('00000000', 'hex'); | ||
/** | ||
* 8 zero bytes. | ||
* @const {Buffer} | ||
* @default | ||
*/ | ||
encoding.ZERO_U64 = Buffer.from('0000000000000000', 'hex'); | ||
/** | ||
* Read uint64le as a js number. | ||
@@ -627,3 +487,3 @@ * @param {Buffer} data | ||
enforce(num.lte(UINT128_MAX), off, 'Number exceeds 2^64-1'); | ||
enforce(num.lte(BASE128_MAX), off, 'Number exceeds 2^64-1'); | ||
@@ -740,146 +600,2 @@ num.ishln(7).iorn(ch & 0x7f); | ||
/** | ||
* Serialize number as a u8. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.u8 = function u8(num) { | ||
const data = Buffer.allocUnsafe(1); | ||
data[0] = num >>> 0; | ||
return data; | ||
}; | ||
/** | ||
* Serialize number as a u16le. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.u16 = function u16(num) { | ||
const data = Buffer.allocUnsafe(2); | ||
data.writeUInt16LE(num, 0, true); | ||
return data; | ||
}; | ||
/** | ||
* Serialize number as a u16be. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.u16be = function u16be(num) { | ||
const data = Buffer.allocUnsafe(2); | ||
data.writeUInt16BE(num, 0, true); | ||
return data; | ||
}; | ||
/** | ||
* Serialize number as a u32le. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.u32 = function u32(num) { | ||
const data = Buffer.allocUnsafe(4); | ||
data.writeUInt32LE(num, 0, true); | ||
return data; | ||
}; | ||
/** | ||
* Serialize number as a u32be. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.u32be = function u32be(num) { | ||
const data = Buffer.allocUnsafe(4); | ||
data.writeUInt32BE(num, 0, true); | ||
return data; | ||
}; | ||
/** | ||
* Serialize number as a u8. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.i8 = encoding.u8; | ||
/** | ||
* Serialize number as a u16le. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.i16 = encoding.u16; | ||
/** | ||
* Serialize number as a u16be. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.i16be = encoding.u16be; | ||
/** | ||
* Serialize number as a u32le. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.i32 = encoding.u32; | ||
/** | ||
* Serialize number as a u32be. | ||
* @param {Number} num | ||
* @returns {Buffer} | ||
*/ | ||
encoding.i32be = encoding.u32be; | ||
/** | ||
* Reverse a hex-string. | ||
* @param {String} str - Hex string. | ||
* @returns {String} Reversed hex string. | ||
*/ | ||
encoding.revHex = function revHex(str) { | ||
assert(typeof str === 'string'); | ||
assert((str.length & 1) === 0); | ||
let out = ''; | ||
for (let i = str.length - 2; i >= 0; i -= 2) | ||
out += str[i] + str[i + 1]; | ||
return out; | ||
}; | ||
/** | ||
* Encoding Error | ||
* @extends {Error} | ||
*/ | ||
class EncodingError extends Error { | ||
/** | ||
* Create an encoding error. | ||
* @constructor | ||
* @param {Number} offset | ||
* @param {String} reason | ||
*/ | ||
constructor(offset, reason, start) { | ||
super(); | ||
this.type = 'EncodingError'; | ||
this.message = `${reason} (offset=${offset}).`; | ||
if (Error.captureStackTrace) | ||
Error.captureStackTrace(this, start || EncodingError); | ||
} | ||
} | ||
encoding.EncodingError = EncodingError; | ||
/* | ||
@@ -939,3 +655,3 @@ * Helpers | ||
if (!value) | ||
throw new encoding.EncodingError(offset, 'Out of bounds read', check); | ||
throw new EncodingError(offset, 'Out of bounds read', check); | ||
} | ||
@@ -945,3 +661,3 @@ | ||
if (!value) | ||
throw new encoding.EncodingError(offset, reason, enforce); | ||
throw new EncodingError(offset, reason, enforce); | ||
} |
@@ -49,8 +49,8 @@ /*! | ||
* @constructor | ||
* @param {Function} Hash | ||
* @param {...Object} args | ||
* @param {Object} ctx | ||
*/ | ||
constructor(Hash, ...args) { | ||
this.ctx = new Hash().init(...args); | ||
constructor(ctx) { | ||
assert(ctx); | ||
this.ctx = ctx; | ||
} | ||
@@ -82,2 +82,4 @@ | ||
final() { | ||
if (this.ctx.digest) | ||
return this.ctx.digest(); | ||
return this.ctx.final(); | ||
@@ -87,2 +89,11 @@ } | ||
/** | ||
* Render the final hash. | ||
* @returns {Buffer} | ||
*/ | ||
render() { | ||
return this.final(); | ||
} | ||
/** | ||
* Get size of data written so far. | ||
@@ -121,2 +132,3 @@ * @returns {Number} | ||
this.ctx.update(POOL8); | ||
return this; | ||
} | ||
@@ -132,2 +144,3 @@ | ||
this.ctx.update(POOL16); | ||
return this; | ||
} | ||
@@ -143,2 +156,3 @@ | ||
this.ctx.update(POOL16); | ||
return this; | ||
} | ||
@@ -154,2 +168,3 @@ | ||
this.ctx.update(POOL32); | ||
return this; | ||
} | ||
@@ -165,2 +180,3 @@ | ||
this.ctx.update(POOL32); | ||
return this; | ||
} | ||
@@ -176,2 +192,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -187,2 +204,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -198,2 +216,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -209,2 +228,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -220,2 +240,3 @@ | ||
this.ctx.update(POOL8); | ||
return this; | ||
} | ||
@@ -231,2 +252,3 @@ | ||
this.ctx.update(POOL16); | ||
return this; | ||
} | ||
@@ -242,2 +264,3 @@ | ||
this.ctx.update(POOL16); | ||
return this; | ||
} | ||
@@ -253,2 +276,3 @@ | ||
this.ctx.update(POOL32); | ||
return this; | ||
} | ||
@@ -264,2 +288,3 @@ | ||
this.ctx.update(POOL32); | ||
return this; | ||
} | ||
@@ -275,2 +300,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -286,2 +312,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -297,2 +324,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -308,2 +336,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -319,2 +348,3 @@ | ||
this.ctx.update(POOL32); | ||
return this; | ||
} | ||
@@ -330,2 +360,3 @@ | ||
this.ctx.update(POOL32); | ||
return this; | ||
} | ||
@@ -341,2 +372,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -352,2 +384,3 @@ | ||
this.ctx.update(POOL64); | ||
return this; | ||
} | ||
@@ -365,2 +398,3 @@ | ||
this.ctx.update(pool); | ||
return this; | ||
} | ||
@@ -378,2 +412,3 @@ | ||
this.ctx.update(pool); | ||
return this; | ||
} | ||
@@ -391,2 +426,3 @@ | ||
this.ctx.update(pool); | ||
return this; | ||
} | ||
@@ -404,2 +440,3 @@ | ||
this.ctx.update(pool); | ||
return this; | ||
} | ||
@@ -414,2 +451,3 @@ | ||
this.ctx.update(value); | ||
return this; | ||
} | ||
@@ -425,2 +463,3 @@ | ||
this.writeBytes(value); | ||
return this; | ||
} | ||
@@ -437,2 +476,3 @@ | ||
this.ctx.update(value.slice(start, end)); | ||
return this; | ||
} | ||
@@ -448,3 +488,3 @@ | ||
if (value.length === 0) | ||
return; | ||
return this; | ||
@@ -455,2 +495,3 @@ if (typeof value === 'string') | ||
this.ctx.update(value); | ||
return this; | ||
} | ||
@@ -467,3 +508,3 @@ | ||
this.writeBytes(value); | ||
return; | ||
return this; | ||
} | ||
@@ -473,2 +514,3 @@ assert(value.length === 64); | ||
this.ctx.update(POOL256); | ||
return this; | ||
} | ||
@@ -485,3 +527,3 @@ | ||
this.writeVarint(0); | ||
return; | ||
return this; | ||
} | ||
@@ -493,2 +535,4 @@ | ||
this.ctx.update(value, enc); | ||
return this; | ||
} | ||
@@ -505,2 +549,3 @@ | ||
this.writeU8(0); | ||
return this; | ||
} | ||
@@ -526,3 +571,3 @@ | ||
if (size === 0) | ||
return; | ||
return this; | ||
@@ -533,3 +578,3 @@ if (size <= 32) { | ||
this.ctx.update(data); | ||
return; | ||
return this; | ||
} | ||
@@ -541,2 +586,4 @@ | ||
this.ctx.update(data); | ||
return this; | ||
} | ||
@@ -543,0 +590,0 @@ } |
@@ -12,3 +12,3 @@ /*! | ||
const encoding = require('./encoding'); | ||
const EncodingError = encoding.EncodingError; | ||
const EncodingError = require('./error'); | ||
@@ -51,3 +51,3 @@ /* | ||
if (!value) | ||
throw new EncodingError(this.offset, 'Out of bounds read', assert); | ||
throw new EncodingError(this.offset, 'Out of bounds read', this.assert); | ||
} | ||
@@ -63,3 +63,3 @@ | ||
if (!value) | ||
throw new EncodingError(this.offset, reason, enforce); | ||
throw new EncodingError(this.offset, reason, this.enforce); | ||
} | ||
@@ -66,0 +66,0 @@ |
@@ -9,3 +9,2 @@ /*! | ||
const assert = require('assert'); | ||
const encoding = require('./encoding'); | ||
@@ -54,2 +53,3 @@ | ||
this.offset += offset; | ||
return this; | ||
} | ||
@@ -63,2 +63,3 @@ | ||
this.offset = 0; | ||
return this; | ||
} | ||
@@ -73,2 +74,3 @@ | ||
this.offset += 1; | ||
return this; | ||
} | ||
@@ -83,2 +85,3 @@ | ||
this.offset += 2; | ||
return this; | ||
} | ||
@@ -93,2 +96,3 @@ | ||
this.offset += 2; | ||
return this; | ||
} | ||
@@ -103,2 +107,3 @@ | ||
this.offset += 4; | ||
return this; | ||
} | ||
@@ -113,2 +118,3 @@ | ||
this.offset += 4; | ||
return this; | ||
} | ||
@@ -123,2 +129,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -133,2 +140,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -143,2 +151,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -153,2 +162,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -163,2 +173,3 @@ | ||
this.offset += 1; | ||
return this; | ||
} | ||
@@ -173,2 +184,3 @@ | ||
this.offset += 2; | ||
return this; | ||
} | ||
@@ -183,2 +195,3 @@ | ||
this.offset += 2; | ||
return this; | ||
} | ||
@@ -193,2 +206,3 @@ | ||
this.offset += 4; | ||
return this; | ||
} | ||
@@ -203,2 +217,3 @@ | ||
this.offset += 4; | ||
return this; | ||
} | ||
@@ -213,2 +228,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -223,2 +239,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -233,2 +250,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -243,2 +261,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -253,2 +272,3 @@ | ||
this.offset += 4; | ||
return this; | ||
} | ||
@@ -263,2 +283,3 @@ | ||
this.offset += 4; | ||
return this; | ||
} | ||
@@ -273,2 +294,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -283,2 +305,3 @@ | ||
this.offset += 8; | ||
return this; | ||
} | ||
@@ -293,2 +316,3 @@ | ||
this.offset += encoding.sizeVarint(value); | ||
return this; | ||
} | ||
@@ -303,2 +327,3 @@ | ||
this.offset += encoding.sizeVarintN(value); | ||
return this; | ||
} | ||
@@ -313,2 +338,3 @@ | ||
this.offset += encoding.sizeVarint2(value); | ||
return this; | ||
} | ||
@@ -323,2 +349,3 @@ | ||
this.offset += encoding.sizeVarint2N(value); | ||
return this; | ||
} | ||
@@ -333,2 +360,3 @@ | ||
this.offset += value.length; | ||
return this; | ||
} | ||
@@ -344,2 +372,3 @@ | ||
this.writeBytes(value); | ||
return this; | ||
} | ||
@@ -356,2 +385,3 @@ | ||
this.offset += end - start; | ||
return this; | ||
} | ||
@@ -367,5 +397,6 @@ | ||
if (value.length === 0) | ||
return; | ||
return this; | ||
this.offset += Buffer.byteLength(value, enc); | ||
return this; | ||
} | ||
@@ -380,2 +411,3 @@ | ||
this.offset += 32; | ||
return this; | ||
} | ||
@@ -392,3 +424,3 @@ | ||
this.offset += 1; | ||
return; | ||
return this; | ||
} | ||
@@ -401,2 +433,3 @@ | ||
this.offset += size; | ||
return this; | ||
} | ||
@@ -413,2 +446,3 @@ | ||
this.writeU8(0); | ||
return this; | ||
} | ||
@@ -423,2 +457,3 @@ | ||
this.offset += 4; | ||
return this; | ||
} | ||
@@ -434,2 +469,3 @@ | ||
this.offset += size; | ||
return this; | ||
} | ||
@@ -436,0 +472,0 @@ } |
@@ -84,2 +84,3 @@ /*! | ||
this.offset += offset; | ||
return this; | ||
} | ||
@@ -94,2 +95,3 @@ | ||
this.offset = 0; | ||
return this; | ||
} | ||
@@ -104,2 +106,3 @@ | ||
this.offset = this.data.writeUInt8(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -114,2 +117,3 @@ | ||
this.offset = this.data.writeUInt16LE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -124,2 +128,3 @@ | ||
this.offset = this.data.writeUInt16BE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -134,2 +139,3 @@ | ||
this.offset = this.data.writeUInt32LE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -144,2 +150,3 @@ | ||
this.offset = this.data.writeUInt32BE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -154,2 +161,3 @@ | ||
this.offset = encoding.writeU64(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -164,2 +172,3 @@ | ||
this.offset = encoding.writeU64BE(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -174,2 +183,3 @@ | ||
this.offset = encoding.writeU64N(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -184,2 +194,3 @@ | ||
this.offset = encoding.writeU64BEN(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -194,2 +205,3 @@ | ||
this.offset = this.data.writeInt8(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -204,2 +216,3 @@ | ||
this.offset = this.data.writeInt16LE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -214,2 +227,3 @@ | ||
this.offset = this.data.writeInt16BE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -224,2 +238,3 @@ | ||
this.offset = this.data.writeInt32LE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -234,2 +249,3 @@ | ||
this.offset = this.data.writeInt32BE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -244,2 +260,3 @@ | ||
this.offset = encoding.writeI64(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -254,2 +271,3 @@ | ||
this.offset = encoding.writeI64BE(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -264,2 +282,3 @@ | ||
this.offset = encoding.writeI64N(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -274,2 +293,3 @@ | ||
this.offset = encoding.writeI64BEN(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -284,2 +304,3 @@ | ||
this.offset = this.data.writeFloatLE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -294,2 +315,3 @@ | ||
this.offset = this.data.writeFloatBE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -304,2 +326,3 @@ | ||
this.offset = this.data.writeDoubleLE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -314,2 +337,3 @@ | ||
this.offset = this.data.writeDoubleBE(value, this.offset, true); | ||
return this; | ||
} | ||
@@ -324,2 +348,3 @@ | ||
this.offset = encoding.writeVarint(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -334,2 +359,3 @@ | ||
this.offset = encoding.writeVarintN(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -344,2 +370,3 @@ | ||
this.offset = encoding.writeVarint2(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -354,2 +381,3 @@ | ||
this.offset = encoding.writeVarint2N(this.data, value, this.offset); | ||
return this; | ||
} | ||
@@ -364,3 +392,3 @@ | ||
if (value.length === 0) | ||
return; | ||
return this; | ||
@@ -370,2 +398,3 @@ value.copy(this.data, this.offset); | ||
this.offset += value.length; | ||
return this; | ||
} | ||
@@ -381,2 +410,3 @@ | ||
this.writeBytes(value); | ||
return this; | ||
} | ||
@@ -395,6 +425,8 @@ | ||
if (len === 0) | ||
return; | ||
return this; | ||
value.copy(this.data, this.offset, start, end); | ||
this.offset += len; | ||
return this; | ||
} | ||
@@ -410,3 +442,3 @@ | ||
if (value.length === 0) | ||
return; | ||
return this; | ||
@@ -418,2 +450,4 @@ const size = Buffer.byteLength(value, enc); | ||
this.offset += size; | ||
return this; | ||
} | ||
@@ -430,3 +464,3 @@ | ||
this.writeBytes(value); | ||
return; | ||
return this; | ||
} | ||
@@ -436,2 +470,3 @@ assert(value.length === 64); | ||
this.offset += 32; | ||
return this; | ||
} | ||
@@ -448,3 +483,3 @@ | ||
this.writeVarint(0); | ||
return; | ||
return this; | ||
} | ||
@@ -458,2 +493,4 @@ | ||
this.offset += size; | ||
return this; | ||
} | ||
@@ -470,2 +507,3 @@ | ||
this.writeU8(0); | ||
return this; | ||
} | ||
@@ -482,2 +520,3 @@ | ||
this.offset += 4; | ||
return this; | ||
} | ||
@@ -495,6 +534,8 @@ | ||
if (size === 0) | ||
return; | ||
return this; | ||
this.data.fill(value, this.offset, this.offset + size); | ||
this.offset += size; | ||
return this; | ||
} | ||
@@ -501,0 +542,0 @@ } |
@@ -158,3 +158,3 @@ /*! | ||
case BYTES: | ||
off += op.value.copy(data, off); | ||
off += op.data.copy(data, off); | ||
break; | ||
@@ -165,3 +165,3 @@ case STR: | ||
case CHECKSUM: | ||
off += op.value(data.slice(0, off)).copy(data, off, 0, 4); | ||
off += op.func(data.slice(0, off)).copy(data, off, 0, 4); | ||
break; | ||
@@ -201,3 +201,4 @@ case FILL: | ||
this.offset += offset; | ||
this.ops.push(new WriteOp(SEEK, offset)); | ||
this.ops.push(new NumberOp(SEEK, offset)); | ||
return this; | ||
} | ||
@@ -212,2 +213,3 @@ | ||
this.offset = 0; | ||
return this; | ||
} | ||
@@ -222,3 +224,4 @@ | ||
this.offset += 1; | ||
this.ops.push(new WriteOp(UI8, value)); | ||
this.ops.push(new NumberOp(UI8, value)); | ||
return this; | ||
} | ||
@@ -233,3 +236,4 @@ | ||
this.offset += 2; | ||
this.ops.push(new WriteOp(UI16, value)); | ||
this.ops.push(new NumberOp(UI16, value)); | ||
return this; | ||
} | ||
@@ -244,3 +248,4 @@ | ||
this.offset += 2; | ||
this.ops.push(new WriteOp(UI16BE, value)); | ||
this.ops.push(new NumberOp(UI16BE, value)); | ||
return this; | ||
} | ||
@@ -255,3 +260,4 @@ | ||
this.offset += 4; | ||
this.ops.push(new WriteOp(UI32, value)); | ||
this.ops.push(new NumberOp(UI32, value)); | ||
return this; | ||
} | ||
@@ -266,3 +272,4 @@ | ||
this.offset += 4; | ||
this.ops.push(new WriteOp(UI32BE, value)); | ||
this.ops.push(new NumberOp(UI32BE, value)); | ||
return this; | ||
} | ||
@@ -277,3 +284,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(UI64, value)); | ||
this.ops.push(new NumberOp(UI64, value)); | ||
return this; | ||
} | ||
@@ -288,3 +296,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(UI64BE, value)); | ||
this.ops.push(new NumberOp(UI64BE, value)); | ||
return this; | ||
} | ||
@@ -299,3 +308,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(UI64N, value)); | ||
this.ops.push(new NumberOp(UI64N, value)); | ||
return this; | ||
} | ||
@@ -310,3 +320,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(UI64BEN, value)); | ||
this.ops.push(new NumberOp(UI64BEN, value)); | ||
return this; | ||
} | ||
@@ -321,3 +332,4 @@ | ||
this.offset += 1; | ||
this.ops.push(new WriteOp(I8, value)); | ||
this.ops.push(new NumberOp(I8, value)); | ||
return this; | ||
} | ||
@@ -332,3 +344,4 @@ | ||
this.offset += 2; | ||
this.ops.push(new WriteOp(I16, value)); | ||
this.ops.push(new NumberOp(I16, value)); | ||
return this; | ||
} | ||
@@ -343,3 +356,4 @@ | ||
this.offset += 2; | ||
this.ops.push(new WriteOp(I16BE, value)); | ||
this.ops.push(new NumberOp(I16BE, value)); | ||
return this; | ||
} | ||
@@ -354,3 +368,4 @@ | ||
this.offset += 4; | ||
this.ops.push(new WriteOp(I32, value)); | ||
this.ops.push(new NumberOp(I32, value)); | ||
return this; | ||
} | ||
@@ -365,3 +380,4 @@ | ||
this.offset += 4; | ||
this.ops.push(new WriteOp(I32BE, value)); | ||
this.ops.push(new NumberOp(I32BE, value)); | ||
return this; | ||
} | ||
@@ -376,3 +392,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(I64, value)); | ||
this.ops.push(new NumberOp(I64, value)); | ||
return this; | ||
} | ||
@@ -387,3 +404,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(I64BE, value)); | ||
this.ops.push(new NumberOp(I64BE, value)); | ||
return this; | ||
} | ||
@@ -398,3 +416,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(I64N, value)); | ||
this.ops.push(new NumberOp(I64N, value)); | ||
return this; | ||
} | ||
@@ -409,3 +428,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(I64BEN, value)); | ||
this.ops.push(new NumberOp(I64BEN, value)); | ||
return this; | ||
} | ||
@@ -420,3 +440,4 @@ | ||
this.offset += 4; | ||
this.ops.push(new WriteOp(FL, value)); | ||
this.ops.push(new NumberOp(FL, value)); | ||
return this; | ||
} | ||
@@ -431,3 +452,4 @@ | ||
this.offset += 4; | ||
this.ops.push(new WriteOp(FLBE, value)); | ||
this.ops.push(new NumberOp(FLBE, value)); | ||
return this; | ||
} | ||
@@ -442,3 +464,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(DBL, value)); | ||
this.ops.push(new NumberOp(DBL, value)); | ||
return this; | ||
} | ||
@@ -453,3 +476,4 @@ | ||
this.offset += 8; | ||
this.ops.push(new WriteOp(DBLBE, value)); | ||
this.ops.push(new NumberOp(DBLBE, value)); | ||
return this; | ||
} | ||
@@ -464,3 +488,4 @@ | ||
this.offset += encoding.sizeVarint(value); | ||
this.ops.push(new WriteOp(VARINT, value)); | ||
this.ops.push(new NumberOp(VARINT, value)); | ||
return this; | ||
} | ||
@@ -475,3 +500,4 @@ | ||
this.offset += encoding.sizeVarintN(value); | ||
this.ops.push(new WriteOp(VARINTN, value)); | ||
this.ops.push(new NumberOp(VARINTN, value)); | ||
return this; | ||
} | ||
@@ -486,3 +512,4 @@ | ||
this.offset += encoding.sizeVarint2(value); | ||
this.ops.push(new WriteOp(VARINT2, value)); | ||
this.ops.push(new NumberOp(VARINT2, value)); | ||
return this; | ||
} | ||
@@ -497,3 +524,4 @@ | ||
this.offset += encoding.sizeVarint2N(value); | ||
this.ops.push(new WriteOp(VARINT2N, value)); | ||
this.ops.push(new NumberOp(VARINT2N, value)); | ||
return this; | ||
} | ||
@@ -508,6 +536,8 @@ | ||
if (value.length === 0) | ||
return; | ||
return this; | ||
this.offset += value.length; | ||
this.ops.push(new WriteOp(BYTES, value)); | ||
this.ops.push(new BufferOp(BYTES, value)); | ||
return this; | ||
} | ||
@@ -522,9 +552,11 @@ | ||
this.offset += encoding.sizeVarint(value.length); | ||
this.ops.push(new WriteOp(VARINT, value.length)); | ||
this.ops.push(new NumberOp(VARINT, value.length)); | ||
if (value.length === 0) | ||
return; | ||
return this; | ||
this.offset += value.length; | ||
this.ops.push(new WriteOp(BYTES, value)); | ||
this.ops.push(new BufferOp(BYTES, value)); | ||
return this; | ||
} | ||
@@ -543,2 +575,3 @@ | ||
this.writeBytes(value); | ||
return this; | ||
} | ||
@@ -554,6 +587,8 @@ | ||
if (value.length === 0) | ||
return; | ||
return this; | ||
this.offset += Buffer.byteLength(value, enc); | ||
this.ops.push(new WriteOp(STR, value, enc)); | ||
this.ops.push(new StringOp(STR, value, enc)); | ||
return this; | ||
} | ||
@@ -570,6 +605,7 @@ | ||
this.writeBytes(value); | ||
return; | ||
return this; | ||
} | ||
assert(value.length === 64); | ||
this.writeString(value, 'hex'); | ||
return this; | ||
} | ||
@@ -585,4 +621,4 @@ | ||
if (value.length === 0) { | ||
this.ops.push(new WriteOp(VARINT, 0)); | ||
return; | ||
this.ops.push(new NumberOp(VARINT, 0)); | ||
return this; | ||
} | ||
@@ -595,5 +631,6 @@ | ||
this.ops.push(new WriteOp(VARINT, size)); | ||
this.ops.push(new NumberOp(VARINT, size)); | ||
this.ops.push(new StringOp(STR, value, enc)); | ||
this.ops.push(new WriteOp(STR, value, enc)); | ||
return this; | ||
} | ||
@@ -610,2 +647,3 @@ | ||
this.writeU8(0); | ||
return this; | ||
} | ||
@@ -620,3 +658,4 @@ | ||
this.offset += 4; | ||
this.ops.push(new WriteOp(CHECKSUM, hash)); | ||
this.ops.push(new FunctionOp(CHECKSUM, hash)); | ||
return this; | ||
} | ||
@@ -634,6 +673,8 @@ | ||
if (size === 0) | ||
return; | ||
return this; | ||
this.offset += size; | ||
this.ops.push(new WriteOp(FILL, value, null, size)); | ||
this.ops.push(new FillOp(FILL, value, size)); | ||
return this; | ||
} | ||
@@ -647,6 +688,40 @@ } | ||
class WriteOp { | ||
constructor(type, value, enc, size) { | ||
constructor(type) { | ||
this.type = type; | ||
} | ||
} | ||
class NumberOp extends WriteOp { | ||
constructor(type, value) { | ||
super(type); | ||
this.value = value; | ||
} | ||
} | ||
class BufferOp extends WriteOp { | ||
constructor(type, data) { | ||
super(type); | ||
this.data = data; | ||
} | ||
} | ||
class StringOp extends WriteOp { | ||
constructor(type, value, enc) { | ||
super(type); | ||
this.value = value; | ||
this.enc = enc; | ||
} | ||
} | ||
class FunctionOp extends WriteOp { | ||
constructor(type, func) { | ||
super(type); | ||
this.func = func; | ||
} | ||
} | ||
class FillOp extends WriteOp { | ||
constructor(type, value, size) { | ||
super(type); | ||
this.value = value; | ||
this.size = size; | ||
@@ -653,0 +728,0 @@ } |
{ | ||
"name": "bufio", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Buffer and serialization utilities for javascript", | ||
@@ -25,6 +25,6 @@ "keywords": [ | ||
"dependencies": { | ||
"n64": "^0.0.18" | ||
"n64": "~0.1.0" | ||
}, | ||
"devDependencies": { | ||
"babelify": "^7.3.0", | ||
"babelify": "^8.0.0", | ||
"babel-core": "^6.26.0", | ||
@@ -34,7 +34,7 @@ "babel-loader": "^7.1.2", | ||
"browserify": "^14.5.0", | ||
"eslint": "^4.9.0", | ||
"eslint": "^4.14.0", | ||
"mocha": "^4.0.1", | ||
"uglifyjs-webpack-plugin": "^1.0.0-beta.3", | ||
"uglifyjs-webpack-plugin": "^1.1.5", | ||
"uglify-es": "^3.1.3", | ||
"webpack": "^3.8.1" | ||
"webpack": "^3.10.0" | ||
}, | ||
@@ -41,0 +41,0 @@ "engines": { |
@@ -8,3 +8,13 @@ # bufio | ||
``` js | ||
const assert = require('assert'); | ||
const bio = require('bufio'); | ||
const bw = bio.write(); | ||
bw.writeU64(100); | ||
bw.writeString('foo', 'ascii'); | ||
const data = bw.render(); | ||
const br = bio.read(data); | ||
assert(br.readU64() === 100); | ||
assert(br.readString('ascii') === 'foo'); | ||
``` | ||
@@ -11,0 +21,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70564
15
32
2993
+ Addedn64@0.1.2(transitive)
- Removedn64@0.0.18(transitive)
Updatedn64@~0.1.0