From d1dbafdedc8fb6682736d15bb72af97d9ce13bc4 Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Tue, 13 Jun 2023 23:00:52 +0400 Subject: [PATCH] pkg: add lint-types and tsconfig to the project. --- .github/workflows/build.yml | 5 +- lib/encoding.js | 5 ++ lib/reader.js | 24 ++++--- lib/staticwriter.js | 79 ++++++++++++++++++++--- lib/struct.js | 123 ++++++++++++++++++++++++++++++++++++ lib/writer.js | 83 +++++++++++++++++++++--- package-lock.json | 15 +++-- package.json | 4 +- tsconfig.json | 31 +++++++++ 9 files changed, 335 insertions(+), 34 deletions(-) create mode 100644 tsconfig.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2828722..17ef241 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,7 @@ jobs: node-version: 20.x - name: Install tools - run: npm install --location=global bslint + run: npm install --location=global bslint typescript - name: Install dependencies run: npm install @@ -24,6 +24,9 @@ jobs: - name: Lint run: npm run lint + - name: Lint types + run: npm run lint-types + test: name: Test runs-on: ubuntu-latest diff --git a/lib/encoding.js b/lib/encoding.js index 9dfbd7a..2f74d35 100644 --- a/lib/encoding.js +++ b/lib/encoding.js @@ -29,9 +29,14 @@ const BIG_ENDIAN = F328_ARRAY[3] === 0; // eslint-disable-next-line const BI = typeof BigInt === 'function' ? BigInt : (function(x) { return 0; }); + +// @ts-ignore const BIG_U56_MAX = (BI(1) << BI(56)) - BI(1); +// @ts-ignore const BIG_U64_MAX = (BI(1) << BI(64)) - BI(1); +// @ts-ignore const BIG_U128_MAX = (BI(1) << BI(128)) - BI(1); +// @ts-ignore const BIG_U256_MAX = (BI(1) << BI(256)) - BI(1); /* diff --git a/lib/reader.js b/lib/reader.js index d939774..89430ec 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -22,6 +22,9 @@ const EMPTY = Buffer.alloc(0); */ class BufferReader { + /** @type {Buffer} */ + data; + /** * Create a buffer reader. * @constructor @@ -53,7 +56,7 @@ class BufferReader { /** * Get total size of passed-in Buffer. - * @returns {Buffer} + * @returns {Number} */ getSize() { @@ -115,7 +118,7 @@ class BufferReader { /** * Stop reading. Pop the start position off the stack * and return the data read. - * @param {Bolean?} zeroCopy - Do a fast buffer + * @param {Boolean} [zeroCopy=false] Do a fast buffer * slice instead of allocating a new buffer (warning: * may cause memory leaks if not used with care). * @returns {Buffer} Data read. @@ -880,7 +883,7 @@ class BufferReader { /** * Read N bytes (will do a fast slice if zero copy). * @param {Number} size - * @param {Bolean?} zeroCopy - Do a fast buffer + * @param {Boolean?} [zeroCopy = false] - Do a fast buffer * slice instead of allocating a new buffer (warning: * may cause memory leaks if not used with care). * @returns {Buffer} @@ -908,7 +911,7 @@ class BufferReader { /** * Read a varint number of bytes (will do a fast slice if zero copy). - * @param {Bolean?} zeroCopy - Do a fast buffer + * @param {Boolean} [zeroCopy=false] - Do a fast buffer * slice instead of allocating a new buffer (warning: * may cause memory leaks if not used with care). * @returns {Buffer} @@ -930,6 +933,7 @@ class BufferReader { this.check(size); const data = this.data.slice(0, this.offset + size); + // @ts-ignore const br = new this.constructor(data); br.offset = this.offset; @@ -942,7 +946,7 @@ class BufferReader { /** * Read a string. * @param {Number} size - * @param {String} enc - Any buffer-supported encoding. + * @param {BufferEncoding} enc - Any buffer-supported encoding. * @returns {String} */ @@ -964,8 +968,8 @@ class BufferReader { /** * Read a 32-byte hash. - * @param {String} enc - `"hex"` or `null`. - * @returns {Hash|Buffer} + * @param {BufferEncoding} enc - `"hex"` or `null`. + * @returns {Buffer|String} */ readHash(enc) { @@ -976,7 +980,7 @@ class BufferReader { /** * Read string of a varint length. - * @param {String} enc - Any buffer-supported encoding. + * @param {BufferEncoding} enc - Any buffer-supported encoding. * @param {Number?} limit - Size limit. * @returns {String} */ @@ -998,7 +1002,7 @@ class BufferReader { /** * Read a null-terminated string. - * @param {String} enc - Any buffer-supported encoding. + * @param {BufferEncoding} enc - Any buffer-supported encoding. * @returns {String} */ @@ -1027,7 +1031,7 @@ class BufferReader { /** * Create a checksum from the last start position. - * @param {Function} hash + * @param {Function|Object} hash * @returns {Number} Checksum. */ diff --git a/lib/staticwriter.js b/lib/staticwriter.js index 402b9e2..3cf9908 100644 --- a/lib/staticwriter.js +++ b/lib/staticwriter.js @@ -27,7 +27,7 @@ class StaticWriter { /** * Statically allocated buffer writer. * @constructor - * @param {Number|Buffer} options + * @param {Number|Buffer} [options] */ constructor(options) { @@ -50,7 +50,8 @@ class StaticWriter { /** * Initialize options. - * @param {Object} options + * @param {Number|Buffer} options + * @returns {StaticWriter} */ init(options) { @@ -163,6 +164,7 @@ class StaticWriter { /** * Seek to relative offset. * @param {Number} off + * @returns {StaticWriter} */ seek(off) { @@ -179,6 +181,7 @@ class StaticWriter { /** * Destroy the buffer writer. + * @returns {StaticWriter} */ destroy() { @@ -190,6 +193,7 @@ class StaticWriter { /** * Write uint8. * @param {Number} value + * @returns {StaticWriter} */ writeU8(value) { @@ -201,6 +205,7 @@ class StaticWriter { /** * Write uint16le. * @param {Number} value + * @returns {StaticWriter} */ writeU16(value) { @@ -212,6 +217,7 @@ class StaticWriter { /** * Write uint16be. * @param {Number} value + * @returns {StaticWriter} */ writeU16BE(value) { @@ -223,6 +229,7 @@ class StaticWriter { /** * Write uint24le. * @param {Number} value + * @returns {StaticWriter} */ writeU24(value) { @@ -234,6 +241,7 @@ class StaticWriter { /** * Write uint24be. * @param {Number} value + * @returns {StaticWriter} */ writeU24BE(value) { @@ -245,6 +253,7 @@ class StaticWriter { /** * Write uint32le. * @param {Number} value + * @returns {StaticWriter} */ writeU32(value) { @@ -256,6 +265,7 @@ class StaticWriter { /** * Write uint32be. * @param {Number} value + * @returns {StaticWriter} */ writeU32BE(value) { @@ -267,6 +277,7 @@ class StaticWriter { /** * Write uint40le. * @param {Number} value + * @returns {StaticWriter} */ writeU40(value) { @@ -278,6 +289,7 @@ class StaticWriter { /** * Write uint40be. * @param {Number} value + * @returns {StaticWriter} */ writeU40BE(value) { @@ -289,6 +301,7 @@ class StaticWriter { /** * Write uint48le. * @param {Number} value + * @returns {StaticWriter} */ writeU48(value) { @@ -300,6 +313,7 @@ class StaticWriter { /** * Write uint48be. * @param {Number} value + * @returns {StaticWriter} */ writeU48BE(value) { @@ -311,6 +325,7 @@ class StaticWriter { /** * Write uint56le. * @param {Number} value + * @returns {StaticWriter} */ writeU56(value) { @@ -322,6 +337,7 @@ class StaticWriter { /** * Write uint56be. * @param {Number} value + * @returns {StaticWriter} */ writeU56BE(value) { @@ -333,6 +349,7 @@ class StaticWriter { /** * Write uint56le. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigU56(value) { @@ -344,6 +361,7 @@ class StaticWriter { /** * Write uint56be. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigU56BE(value) { @@ -355,6 +373,7 @@ class StaticWriter { /** * Write uint64le. * @param {Number} value + * @returns {StaticWriter} */ writeU64(value) { @@ -366,6 +385,7 @@ class StaticWriter { /** * Write uint64be. * @param {Number} value + * @returns {StaticWriter} */ writeU64BE(value) { @@ -377,6 +397,7 @@ class StaticWriter { /** * Write uint64le. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigU64(value) { @@ -388,6 +409,7 @@ class StaticWriter { /** * Write uint64be. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigU64BE(value) { @@ -399,6 +421,7 @@ class StaticWriter { /** * Write uint128le. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigU128(value) { @@ -410,6 +433,7 @@ class StaticWriter { /** * Write uint128be. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigU128BE(value) { @@ -421,6 +445,7 @@ class StaticWriter { /** * Write uint256le. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigU256(value) { @@ -432,6 +457,7 @@ class StaticWriter { /** * Write uint256be. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigU256BE(value) { @@ -443,6 +469,7 @@ class StaticWriter { /** * Write int8. * @param {Number} value + * @returns {StaticWriter} */ writeI8(value) { @@ -454,6 +481,7 @@ class StaticWriter { /** * Write int16le. * @param {Number} value + * @returns {StaticWriter} */ writeI16(value) { @@ -465,6 +493,7 @@ class StaticWriter { /** * Write int16be. * @param {Number} value + * @returns {StaticWriter} */ writeI16BE(value) { @@ -476,6 +505,7 @@ class StaticWriter { /** * Write int24le. * @param {Number} value + * @returns {StaticWriter} */ writeI24(value) { @@ -487,6 +517,7 @@ class StaticWriter { /** * Write int24be. * @param {Number} value + * @returns {StaticWriter} */ writeI24BE(value) { @@ -498,6 +529,7 @@ class StaticWriter { /** * Write int32le. * @param {Number} value + * @returns {StaticWriter} */ writeI32(value) { @@ -509,6 +541,7 @@ class StaticWriter { /** * Write int32be. * @param {Number} value + * @returns {StaticWriter} */ writeI32BE(value) { @@ -520,6 +553,7 @@ class StaticWriter { /** * Write int40le. * @param {Number} value + * @returns {StaticWriter} */ writeI40(value) { @@ -531,6 +565,7 @@ class StaticWriter { /** * Write int40be. * @param {Number} value + * @returns {StaticWriter} */ writeI40BE(value) { @@ -542,6 +577,7 @@ class StaticWriter { /** * Write int48le. * @param {Number} value + * @returns {StaticWriter} */ writeI48(value) { @@ -553,6 +589,7 @@ class StaticWriter { /** * Write int48be. * @param {Number} value + * @returns {StaticWriter} */ writeI48BE(value) { @@ -564,6 +601,7 @@ class StaticWriter { /** * Write int56le. * @param {Number} value + * @returns {StaticWriter} */ writeI56(value) { @@ -575,6 +613,7 @@ class StaticWriter { /** * Write int56be. * @param {Number} value + * @returns {StaticWriter} */ writeI56BE(value) { @@ -586,6 +625,7 @@ class StaticWriter { /** * Write int56le. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigI56(value) { @@ -597,6 +637,7 @@ class StaticWriter { /** * Write int56be. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigI56BE(value) { @@ -608,6 +649,7 @@ class StaticWriter { /** * Write int64le. * @param {Number} value + * @returns {StaticWriter} */ writeI64(value) { @@ -619,6 +661,7 @@ class StaticWriter { /** * Write int64be. * @param {Number} value + * @returns {StaticWriter} */ writeI64BE(value) { @@ -630,6 +673,7 @@ class StaticWriter { /** * Write int64le. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigI64(value) { @@ -641,6 +685,7 @@ class StaticWriter { /** * Write int64be. * @param {BigInt} value + * @returns {StaticWriter} */ writeBigI64BE(value) { @@ -652,6 +697,7 @@ class StaticWriter { /** * Write float le. * @param {Number} value + * @returns {StaticWriter} */ writeFloat(value) { @@ -663,6 +709,7 @@ class StaticWriter { /** * Write float be. * @param {Number} value + * @returns {StaticWriter} */ writeFloatBE(value) { @@ -674,6 +721,7 @@ class StaticWriter { /** * Write double le. * @param {Number} value + * @returns {StaticWriter} */ writeDouble(value) { @@ -685,6 +733,7 @@ class StaticWriter { /** * Write double be. * @param {Number} value + * @returns {StaticWriter} */ writeDoubleBE(value) { @@ -696,6 +745,7 @@ class StaticWriter { /** * Write a varint. * @param {Number} value + * @returns {StaticWriter} */ writeVarint(value) { @@ -706,6 +756,7 @@ class StaticWriter { /** * Write a varint (type 2). * @param {Number} value + * @returns {StaticWriter} */ writeVarint2(value) { @@ -716,6 +767,7 @@ class StaticWriter { /** * Write bytes. * @param {Buffer} value + * @returns {StaticWriter} */ writeBytes(value) { @@ -730,6 +782,7 @@ class StaticWriter { /** * Write bytes with a varint length before them. * @param {Buffer} value + * @returns {StaticWriter} */ writeVarBytes(value) { @@ -746,6 +799,7 @@ class StaticWriter { * @param {Buffer} value * @param {Number} start * @param {Number} end + * @returns {StaticWriter} */ copy(value, start, end) { @@ -763,7 +817,8 @@ class StaticWriter { /** * Write string to buffer. * @param {String} value - * @param {String?} enc - Any buffer-supported encoding. + * @param {BufferEncoding} [enc='binary'] - Any buffer-supported encoding. + * @returns {StaticWriter} */ writeString(value, enc) { @@ -787,7 +842,8 @@ class StaticWriter { /** * Write a 32 byte hash. - * @param {Hash} value + * @param {Buffer|String} value + * @returns {StaticWriter} */ writeHash(value) { @@ -808,8 +864,9 @@ class StaticWriter { /** * Write a string with a varint length before it. - * @param {String} - * @param {String?} enc - Any buffer-supported encoding. + * @param {String} value + * @param {BufferEncoding} [enc='binary'] - Any buffer-supported encoding. + * @returns {StaticWriter} */ writeVarString(value, enc) { @@ -835,8 +892,9 @@ class StaticWriter { /** * Write a null-terminated string. - * @param {String|Buffer} - * @param {String?} enc - Any buffer-supported encoding. + * @param {String} value + * @param {BufferEncoding} [enc='binary'] - Any buffer-supported encoding. + * @returns {StaticWriter} */ writeNullString(value, enc) { @@ -847,7 +905,8 @@ class StaticWriter { /** * Calculate and write a checksum for the data written so far. - * @param {Function} hash + * @param {Function|Object} hash + * @returns {StaticWriter} */ writeChecksum(hash) { @@ -870,6 +929,7 @@ class StaticWriter { * Fill N bytes with value. * @param {Number} value * @param {Number} size + * @returns {StaticWriter} */ fill(value, size) { @@ -891,6 +951,7 @@ class StaticWriter { * Pad N bytes with value. * @param {Number} size * @param {Number} [value=0x00] + * @returns {StaticWriter} */ pad(size, value = 0x00) { diff --git a/lib/struct.js b/lib/struct.js index faa1cdd..910dc8f 100644 --- a/lib/struct.js +++ b/lib/struct.js @@ -19,12 +19,22 @@ const {custom} = require('./custom'); class Struct { constructor() {} + /** + * @param {this} obj + * @returns {this} + */ + inject(obj) { enforce(obj instanceof this.constructor, 'obj', 'struct'); return this.decode(obj.encode()); } + /** + * @returns {this} + */ + clone() { + // @ts-ignore const copy = new this.constructor(); return copy.inject(this); } @@ -33,42 +43,95 @@ class Struct { * Bindable */ + /** + * @param {*} [extra] + * @returns {Number} + */ + getSize(extra) { return -1; } + /** + * @param {BufferWriter|StaticWriter} bw + * @param {*} [extra] + * @returns {BufferWriter|StaticWriter} + */ + write(bw, extra) { return bw; } + /** + * @param {BufferReader} br + * @param {*} [extra] + * @returns {this} + */ + read(br, extra) { return this; } + /** + * @returns {String} + */ + toString() { return Object.prototype.toString.call(this); } + /** + * @param {String} str + * @param {*} [extra] + * @returns {this} + */ + fromString(str, extra) { return this; } + /** + * @returns {Object} + */ + getJSON() { return this; } + /** + * @param {Object} json + * @param {*} [extra] + * @returns {this} + */ + fromJSON(json, extra) { return this; } + /** + * @param {Object} options + * @param {*} [extra] + * @returns {this} + */ + fromOptions(options, extra) { return this; } + /** + * @param {Object} options + * @param {*} [extra] + * @returns {this} + */ + from(options, extra) { return this.fromOptions(options, extra); } + /** + * @returns {*} + */ + format() { return this.getJSON(); } @@ -77,6 +140,11 @@ class Struct { * API */ + /** + * @param {*} [extra] + * @returns {Buffer} + */ + encode(extra) { const size = this.getSize(extra); const bw = size === -1 @@ -88,6 +156,12 @@ class Struct { return bw.render(); } + /** + * @param {Buffer} data + * @param {*} [extra] + * @returns {this} + */ + decode(data, extra) { const br = new BufferReader(data); @@ -96,10 +170,21 @@ class Struct { return this; } + /** + * @param {*} [extra] + * @returns {String} + */ + toHex(extra) { return this.encode(extra).toString('hex'); } + /** + * @param {String} str + * @param {*} [extra] + * @returns {this} + */ + fromHex(str, extra) { enforce(typeof str === 'string', 'str', 'string'); @@ -112,10 +197,21 @@ class Struct { return this.decode(data, extra); } + /** + * @param {*} [extra] + * @returns {String} + */ + toBase64(extra) { return this.encode(extra).toString('base64'); } + /** + * @param {String} str + * @param {*} [extra] + * @returns {this} + */ + fromBase64(str, extra) { enforce(typeof str === 'string', 'str', 'string'); @@ -127,6 +223,10 @@ class Struct { return this.decode(data, extra); } + /** + * @returns {Object} + */ + toJSON() { return this.getJSON(); } @@ -175,18 +275,41 @@ class Struct { * Aliases */ + /** + * @param {BufferWriter|StaticWriter} bw + * @param {*} [extra] + * @returns {BufferWriter|StaticWriter} + */ + toWriter(bw, extra) { return this.write(bw, extra); } + /** + * @param {BufferReader} br + * @param {*} [extra] + * @returns {this} + */ + fromReader(br, extra) { return this.read(br, extra); } + /** + * @param {*} [extra] + * @returns {Buffer} + */ + toRaw(extra) { return this.encode(extra); } + /** + * @param {Buffer} data + * @param {*} [extra] + * @returns {this} + */ + fromRaw(data, extra) { return this.decode(data, extra); } diff --git a/lib/writer.js b/lib/writer.js index 9ca2863..e6e2d4c 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -307,7 +307,8 @@ class BufferWriter { /** * Seek to relative offset. - * @param {Number} offset + * @param {Number} off + * @returns {BufferWriter} */ seek(off) { @@ -324,6 +325,7 @@ class BufferWriter { /** * Destroy the buffer writer. Remove references to `ops`. + * @returns {BufferWriter} */ destroy() { @@ -335,6 +337,7 @@ class BufferWriter { /** * Write uint8. * @param {Number} value + * @returns {BufferWriter} */ writeU8(value) { @@ -346,6 +349,7 @@ class BufferWriter { /** * Write uint16le. * @param {Number} value + * @returns {BufferWriter} */ writeU16(value) { @@ -357,6 +361,7 @@ class BufferWriter { /** * Write uint16be. * @param {Number} value + * @returns {BufferWriter} */ writeU16BE(value) { @@ -368,6 +373,7 @@ class BufferWriter { /** * Write uint24le. * @param {Number} value + * @returns {BufferWriter} */ writeU24(value) { @@ -379,6 +385,7 @@ class BufferWriter { /** * Write uint24be. * @param {Number} value + * @returns {BufferWriter} */ writeU24BE(value) { @@ -390,6 +397,7 @@ class BufferWriter { /** * Write uint32le. * @param {Number} value + * @returns {BufferWriter} */ writeU32(value) { @@ -401,6 +409,7 @@ class BufferWriter { /** * Write uint32be. * @param {Number} value + * @returns {BufferWriter} */ writeU32BE(value) { @@ -412,6 +421,7 @@ class BufferWriter { /** * Write uint40le. * @param {Number} value + * @returns {BufferWriter} */ writeU40(value) { @@ -423,6 +433,7 @@ class BufferWriter { /** * Write uint40be. * @param {Number} value + * @returns {BufferWriter} */ writeU40BE(value) { @@ -434,6 +445,7 @@ class BufferWriter { /** * Write uint48le. * @param {Number} value + * @returns {BufferWriter} */ writeU48(value) { @@ -445,6 +457,7 @@ class BufferWriter { /** * Write uint48be. * @param {Number} value + * @returns {BufferWriter} */ writeU48BE(value) { @@ -456,6 +469,7 @@ class BufferWriter { /** * Write uint56le. * @param {Number} value + * @returns {BufferWriter} */ writeU56(value) { @@ -467,6 +481,7 @@ class BufferWriter { /** * Write uint56be. * @param {Number} value + * @returns {BufferWriter} */ writeU56BE(value) { @@ -478,6 +493,7 @@ class BufferWriter { /** * Write uint56le. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigU56(value) { @@ -489,6 +505,7 @@ class BufferWriter { /** * Write uint56be. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigU56BE(value) { @@ -500,6 +517,7 @@ class BufferWriter { /** * Write uint64le. * @param {Number} value + * @returns {BufferWriter} */ writeU64(value) { @@ -511,6 +529,7 @@ class BufferWriter { /** * Write uint64be. * @param {Number} value + * @returns {BufferWriter} */ writeU64BE(value) { @@ -522,6 +541,7 @@ class BufferWriter { /** * Write uint64le. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigU64(value) { @@ -533,6 +553,7 @@ class BufferWriter { /** * Write uint64be. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigU64BE(value) { @@ -544,6 +565,7 @@ class BufferWriter { /** * Write uint128le. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigU128(value) { @@ -555,6 +577,7 @@ class BufferWriter { /** * Write uint128be. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigU128BE(value) { @@ -566,6 +589,7 @@ class BufferWriter { /** * Write uint256le. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigU256(value) { @@ -577,6 +601,7 @@ class BufferWriter { /** * Write uint256be. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigU256BE(value) { @@ -588,6 +613,7 @@ class BufferWriter { /** * Write int8. * @param {Number} value + * @returns {BufferWriter} */ writeI8(value) { @@ -599,6 +625,7 @@ class BufferWriter { /** * Write int16le. * @param {Number} value + * @returns {BufferWriter} */ writeI16(value) { @@ -610,6 +637,7 @@ class BufferWriter { /** * Write int16be. * @param {Number} value + * @returns {BufferWriter} */ writeI16BE(value) { @@ -621,6 +649,7 @@ class BufferWriter { /** * Write int24le. * @param {Number} value + * @returns {BufferWriter} */ writeI24(value) { @@ -632,6 +661,7 @@ class BufferWriter { /** * Write int24be. * @param {Number} value + * @returns {BufferWriter} */ writeI24BE(value) { @@ -643,6 +673,7 @@ class BufferWriter { /** * Write int32le. * @param {Number} value + * @returns {BufferWriter} */ writeI32(value) { @@ -654,6 +685,7 @@ class BufferWriter { /** * Write int32be. * @param {Number} value + * @returns {BufferWriter} */ writeI32BE(value) { @@ -665,6 +697,7 @@ class BufferWriter { /** * Write int40le. * @param {Number} value + * @returns {BufferWriter} */ writeI40(value) { @@ -676,6 +709,7 @@ class BufferWriter { /** * Write int40be. * @param {Number} value + * @returns {BufferWriter} */ writeI40BE(value) { @@ -687,6 +721,7 @@ class BufferWriter { /** * Write int48le. * @param {Number} value + * @returns {BufferWriter} */ writeI48(value) { @@ -698,6 +733,7 @@ class BufferWriter { /** * Write int48be. * @param {Number} value + * @returns {BufferWriter} */ writeI48BE(value) { @@ -709,6 +745,7 @@ class BufferWriter { /** * Write int56le. * @param {Number} value + * @returns {BufferWriter} */ writeI56(value) { @@ -720,6 +757,7 @@ class BufferWriter { /** * Write int56be. * @param {Number} value + * @returns {BufferWriter} */ writeI56BE(value) { @@ -731,6 +769,7 @@ class BufferWriter { /** * Write int56le. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigI56(value) { @@ -742,6 +781,7 @@ class BufferWriter { /** * Write int56be. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigI56BE(value) { @@ -753,6 +793,7 @@ class BufferWriter { /** * Write int64le. * @param {Number} value + * @returns {BufferWriter} */ writeI64(value) { @@ -764,6 +805,7 @@ class BufferWriter { /** * Write int64be. * @param {Number} value + * @returns {BufferWriter} */ writeI64BE(value) { @@ -775,6 +817,7 @@ class BufferWriter { /** * Write int64le. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigI64(value) { @@ -786,6 +829,7 @@ class BufferWriter { /** * Write int64be. * @param {BigInt} value + * @returns {BufferWriter} */ writeBigI64BE(value) { @@ -797,6 +841,7 @@ class BufferWriter { /** * Write float le. * @param {Number} value + * @returns {BufferWriter} */ writeFloat(value) { @@ -808,6 +853,7 @@ class BufferWriter { /** * Write float be. * @param {Number} value + * @returns {BufferWriter} */ writeFloatBE(value) { @@ -819,6 +865,7 @@ class BufferWriter { /** * Write double le. * @param {Number} value + * @returns {BufferWriter} */ writeDouble(value) { @@ -830,6 +877,7 @@ class BufferWriter { /** * Write double be. * @param {Number} value + * @returns {BufferWriter} */ writeDoubleBE(value) { @@ -841,6 +889,7 @@ class BufferWriter { /** * Write a varint. * @param {Number} value + * @returns {BufferWriter} */ writeVarint(value) { @@ -852,6 +901,7 @@ class BufferWriter { /** * Write a varint (type 2). * @param {Number} value + * @returns {BufferWriter} */ writeVarint2(value) { @@ -863,6 +913,7 @@ class BufferWriter { /** * Write bytes. * @param {Buffer} value + * @returns {BufferWriter} */ writeBytes(value) { @@ -880,6 +931,7 @@ class BufferWriter { /** * Write bytes with a varint length before them. * @param {Buffer} value + * @returns {BufferWriter} */ writeVarBytes(value) { @@ -902,6 +954,7 @@ class BufferWriter { * @param {Buffer} value * @param {Number} start * @param {Number} end + * @returns {BufferWriter} */ copy(value, start, end) { @@ -920,7 +973,8 @@ class BufferWriter { /** * Write string to buffer. * @param {String} value - * @param {String?} enc - Any buffer-supported encoding. + * @param {BufferEncoding} [enc='binary'] - Any buffer-supported encoding. + * @returns {BufferWriter} */ writeString(value, enc) { @@ -941,7 +995,8 @@ class BufferWriter { /** * Write a 32 byte hash. - * @param {Hash} value + * @param {Buffer|String} value + * @returns {BufferWriter} */ writeHash(value) { @@ -961,8 +1016,9 @@ class BufferWriter { /** * Write a string with a varint length before it. - * @param {String} - * @param {String?} enc - Any buffer-supported encoding. + * @param {String} value + * @param {BufferEncoding} [enc='binary'] - Any buffer-supported encoding. + * @returns {BufferWriter} */ writeVarString(value, enc) { @@ -990,8 +1046,9 @@ class BufferWriter { /** * Write a null-terminated string. - * @param {String|Buffer} - * @param {String?} enc - Any buffer-supported encoding. + * @param {String} value + * @param {BufferEncoding} [enc='binary'] - Any buffer-supported encoding. + * @returns {BufferWriter} */ writeNullString(value, enc) { @@ -1002,12 +1059,14 @@ class BufferWriter { /** * Calculate and write a checksum for the data written so far. - * @param {Function} hash + * @param {Function|Object} hash + * @returns {BufferWriter} */ writeChecksum(hash) { - if (hash && typeof hash.digest === 'function') + if (hash && typeof hash.digest === 'function') { hash = hash.digest.bind(hash); + } enforce(typeof hash === 'function', 'hash', 'function'); @@ -1021,6 +1080,7 @@ class BufferWriter { * Fill N bytes with value. * @param {Number} value * @param {Number} size + * @returns {BufferWriter} */ fill(value, size) { @@ -1040,6 +1100,7 @@ class BufferWriter { * Pad N bytes with value. * @param {Number} size * @param {Number} [value=0x00] + * @returns {BufferWriter} */ pad(size, value = 0x00) { @@ -1052,6 +1113,10 @@ class BufferWriter { */ class WriteOp { + /** + * @param {Number} type + */ + constructor(type) { this.type = type; } diff --git a/package-lock.json b/package-lock.json index bf12e2e..d784aec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,19 @@ { "name": "bufio", - "version": "1.0.7", + "version": "1.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "bufio", - "version": "1.0.7", + "version": "1.2.0", "license": "MIT", "devDependencies": { - "bmocha": "^2.1.0" + "bmocha": "^2.1.0", + "bts-type-deps": "^0.0.3" }, "engines": { - "node": ">=8.0.0" + "node": ">=14.0.0" } }, "node_modules/bmocha": { @@ -27,6 +28,12 @@ "engines": { "node": ">=8.0.0" } + }, + "node_modules/bts-type-deps": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/bts-type-deps/-/bts-type-deps-0.0.3.tgz", + "integrity": "sha512-OQHGWhX5amae6Vj6ShlGaQu0sNCICgJ5YspNZPRzfR5RobrD+wjm5vkZK/J3EH5b/ymxqSWo9VkiFNpCxjaG2Q==", + "dev": true } } } diff --git a/package.json b/package.json index 49e27c4..d1a3c5d 100644 --- a/package.json +++ b/package.json @@ -16,10 +16,12 @@ "main": "./lib/bufio.js", "scripts": { "lint": "eslint lib/ test/", + "lint-types": "tsc -p .", "test": "bmocha --reporter spec test/*-test.js" }, "devDependencies": { - "bmocha": "^2.1.0" + "bmocha": "^2.1.0", + "bts-type-deps": "^0.0.3" }, "engines": { "node": ">=14.0.0" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..da7a94b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,31 @@ +{ + "include": [ + "lib/**/*.js" + ], + "compilerOptions": { + "rootDir": ".", + "target": "ES2020", + "lib": [ + "ES2020" + ], + + "noEmit": true, + + "allowJs": true, + "checkJs": true, + "maxNodeModuleJsDepth": 10, + + "module": "commonjs", + "moduleResolution": "node", + "resolveJsonModule": true, + + "stripInternal": true, + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": false, + + "typeRoots": [ + "node_modules/bts-type-deps/types" + ] + } +}