From dd2b624b55e8146d703de549826601851e24ecdc Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Mon, 26 Jun 2023 22:49:13 +0400 Subject: [PATCH] pkg: add minimal type lints. --- .github/workflows/node.js.yml | 8 +- lib/aes.js | 8 +- lib/encoding/asn1.js | 242 ++++++++++++++++++++------- lib/encoding/openssl.js | 11 ++ lib/encoding/pem.js | 2 +- lib/encoding/pkcs1.js | 9 + lib/encoding/sec1.js | 10 ++ lib/encoding/x509.js | 2 +- lib/internal/hmac.js | 53 +++++- lib/js/aead.js | 8 +- lib/js/base32.js | 8 +- lib/js/base58.js | 2 +- lib/js/bech32.js | 2 +- lib/js/blake2b.js | 10 +- lib/js/bn.js | 48 ++++-- lib/js/cash32.js | 2 +- lib/js/cipher.js | 4 + lib/js/ciphers/modes.js | 18 +- lib/js/dsa.js | 14 +- lib/js/ecdsa.js | 2 +- lib/js/elliptic.js | 24 ++- lib/js/hash-drbg.js | 2 +- lib/js/pbkdf2.js | 16 +- lib/js/rsa.js | 6 +- lib/js/salsa20.js | 2 +- lib/js/sha512.js | 14 +- lib/keccak.js | 8 +- lib/native/bcrypt.js | 4 +- lib/native/bn.js | 1 + lib/native/dsa.js | 4 +- lib/native/rsa.js | 6 +- lib/native/schnorr-libsecp256k1.js | 4 +- lib/native/secp256k1-libsecp256k1.js | 2 +- lib/pgp.js | 41 +++++ lib/ssh.js | 50 +++++- package-lock.json | 21 ++- package.json | 8 +- tsconfig.json | 31 ++++ 38 files changed, 553 insertions(+), 154 deletions(-) create mode 100644 tsconfig.json diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 3b89a24b2..3c8e7ff73 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -14,11 +14,17 @@ 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 - name: Lint run: npm run lint + - name: Lint types + run: npm run lint-types + general: name: Javascript & Bigint runs-on: ubuntu-latest diff --git a/lib/aes.js b/lib/aes.js index 0998d59f6..7edaabdb1 100644 --- a/lib/aes.js +++ b/lib/aes.js @@ -6,7 +6,11 @@ 'use strict'; +let AES; + if (process.env.NODE_BACKEND === 'js') - module.exports = require('./js/aes'); + AES = require('./js/aes'); else - module.exports = require('./native/aes'); + AES = require('./native/aes'); + +module.exports = AES; diff --git a/lib/encoding/asn1.js b/lib/encoding/asn1.js index 5cf24eba8..98c6fa4be 100644 --- a/lib/encoding/asn1.js +++ b/lib/encoding/asn1.js @@ -33,10 +33,14 @@ 'use strict'; -const assert = require('../internal/assert'); +const assert = require('bsert'); const bio = require('bufio'); const objects = require('../internal/objects'); +/** @typedef {import('bufio').BufferWriter} BufferWriter */ +/** @typedef {import('bufio').StaticWriter} StaticWriter */ +/** @typedef {import('bufio').BufferReader} BufferReader */ + /* * Constants */ @@ -63,7 +67,11 @@ const MAX_OFFSET = 50400; // UTC+14:00 // CHARSTRING: 29 // BMPSTRING: 30 -const types = { +/** + * @enum {Number} + */ + +const asnTypes = { BOOLEAN: 1, INTEGER: 2, BITSTRING: 3, @@ -134,6 +142,14 @@ class Node extends bio.Struct { this.flags = 0; } + /** + * @returns {asnTypes} + */ + + get type() { + throw new Error('Abstract class.'); + } + get mode() { return this.flags & MODE; } @@ -167,6 +183,11 @@ class Node extends bio.Struct { return false; } + /** + * @param {Number} target + * @returns {this} + */ + explicit(target) { assert((target >>> 0) === target); this.mode = EXPLICIT; @@ -174,6 +195,11 @@ class Node extends bio.Struct { return this; } + /** + * @param {Number} target + * @returns {this} + */ + implicit(target) { assert((target >>> 0) === target); this.mode = IMPLICIT; @@ -191,6 +217,11 @@ class Node extends bio.Struct { return false; } + /** + * @param {*} [extra] + * @returns {Number} + */ + getBodySize(extra) { return 0; } @@ -199,6 +230,12 @@ class Node extends bio.Struct { return bw; } + /** + * @param {BufferReader} br + * @param {any} [extra] + * @returns {this} + */ + readBody(br, extra) { return this; } @@ -215,14 +252,25 @@ class Node extends bio.Struct { return this.readBody(br, extra); } + /** + * @returns {this} + */ + set() { return this; } + /** + * @param {*} [options] + * @param {...any} [extra] + * @returns {this} + */ + from(options, ...extra) { if (options == null) return this; + // @ts-ignore return this.set(options, ...extra); } @@ -268,14 +316,14 @@ class Node extends bio.Struct { // fall through } case NORMAL: { - const primitive = this.type !== types.SEQUENCE - && this.type !== types.SET; + const primitive = this.type !== asnTypes.SEQUENCE + && this.type !== asnTypes.SET; writeHeader(bw, this.type, classes.UNIVERSAL, primitive, body); break; } case IMPLICIT: { - const primitive = this.type !== types.SEQUENCE - && this.type !== types.SET; + const primitive = this.type !== asnTypes.SEQUENCE + && this.type !== asnTypes.SET; writeHeader(bw, this.target, classes.CONTEXT, primitive, body); break; } @@ -319,8 +367,8 @@ class Node extends bio.Struct { if (hdr.cls !== classes.UNIVERSAL) return this.error(`Unexpected class: ${hdr.cls}.`); - const primitive = this.type !== types.SEQUENCE - && this.type !== types.SET; + const primitive = this.type !== asnTypes.SEQUENCE + && this.type !== asnTypes.SET; if (hdr.primitive !== primitive) return this.error('Unexpected primitive flag.'); @@ -352,8 +400,8 @@ class Node extends bio.Struct { if (hdr.cls !== classes.CONTEXT) return this.error(`Unexpected class: ${hdr.cls}.`); - const primitive = this.type !== types.SEQUENCE - && this.type !== types.SET; + const primitive = this.type !== asnTypes.SEQUENCE + && this.type !== asnTypes.SET; if (hdr.primitive !== primitive) return this.error('Unexpected primitive flag.'); @@ -374,6 +422,14 @@ class Node extends bio.Struct { } } + /** + * @returns {String|Object} + */ + + format() { + return `<${this.constructor.name}>`; + } + fromArray(value) { return this; } @@ -415,7 +471,7 @@ class Sequence extends Node { } get type() { - return types.SEQUENCE; + return asnTypes.SEQUENCE; } }; @@ -431,7 +487,7 @@ class Set extends Node { } get type() { - return types.SET; + return asnTypes.SET; } }; @@ -440,6 +496,9 @@ class Set extends Node { */ class Any extends Node { + /** @type {Node} */ + node; + constructor(...options) { super(); this.node = new Null(); @@ -451,10 +510,20 @@ class Any extends Node { return true; } + /** + * @param {Number} target + * @returns {this} + */ + explicit(target) { throw new Error('Cannot set explicit on any.'); } + /** + * @param {Number} target + * @returns {this} + */ + implicit(target) { throw new Error('Cannot set implicit on any.'); } @@ -501,12 +570,24 @@ class Any extends Node { return this.node.getBodySize(extra); } + /** + * @param {BufferWriter|StaticWriter} bw + * @param {*} [extra] + * @returns {BufferWriter|StaticWriter} + */ + writeBody(bw, extra) { this.node.flags = this.flags; this.node.writeBody(bw, extra); return bw; } + /** + * @param {BufferReader} br + * @param {*} [extra] + * @returns {this} + */ + readBody(br, extra) { this.node.flags = this.flags; this.node.readBody(br, extra); @@ -526,14 +607,24 @@ class Any extends Node { } clean() { - return this.node.type === types.NULL; + return this.node.type === asnTypes.NULL; } + /** + * @returns {String} + */ + format() { - return { - type: this.constructor.name, - node: this.node - }; + return `<${this.constructor.name}: ${this.node.format()}>`; + } + + /** + * @param {BufferReader} br + * @returns {Any} + */ + + static read(br) { + return new this().read(br); } } @@ -553,6 +644,10 @@ class Choice extends Node { return this.node.type; } + /** + * @returns {asnTypes[]} + */ + choices() { throw new Error('Unimplemented.'); } @@ -616,10 +711,7 @@ class Choice extends Node { } format() { - return { - type: this.constructor.name, - node: this.node - }; + return `<${this.constructor.name}: ${this.node.format()}>`; } } @@ -627,13 +719,17 @@ class Choice extends Node { * String */ -const Str = class String extends Node { +class Str extends Node { constructor(...options) { super(); this.value = ''; this.from(...options); } + /** + * @returns {BufferEncoding} + */ + get encoding() { return 'binary'; } @@ -647,23 +743,29 @@ const Str = class String extends Node { return bw; } - readBody(br) { + /** + * @param {BufferReader} br + * @param {*} extra + * @returns {this} + */ + + readBody(br, extra) { const str = br.readString(br.left(), this.encoding); switch (this.type) { - case types.NUMSTRING: { + case asnTypes.NUMSTRING: { if (!isNumString(str)) throw new Error('Invalid num string.'); break; } - case types.PRINTSTRING: { + case asnTypes.PRINTSTRING: { if (!isPrintString(str)) throw new Error('Invalid print string.'); break; } - case types.IA5STRING: { + case asnTypes.IA5STRING: { if (!isIA5String(str)) throw new Error('Invalid print string.'); break; @@ -675,6 +777,11 @@ const Str = class String extends Node { return this; } + /** + * @param {String} [value] + * @returns {this} + */ + set(value) { if (value == null) value = ''; @@ -699,7 +806,7 @@ const Str = class String extends Node { * Boolean */ -const Bool = class Boolean extends Node { +class Bool extends Node { constructor(...options) { super(); this.value = false; @@ -707,7 +814,7 @@ const Bool = class Boolean extends Node { } get type() { - return types.BOOLEAN; + return asnTypes.BOOLEAN; } getBodySize() { @@ -719,6 +826,11 @@ const Bool = class Boolean extends Node { return bw; } + /** + * @param {BufferReader} br + * @return {this} + */ + readBody(br) { if (br.left() !== 1) throw new Error('Non-minimal boolean.'); @@ -766,7 +878,7 @@ class Integer extends Node { } get type() { - return types.INTEGER; + return asnTypes.INTEGER; } getBodySize() { @@ -1010,7 +1122,7 @@ class BitString extends Node { } get type() { - return types.BITSTRING; + return asnTypes.BITSTRING; } getBodySize() { @@ -1135,7 +1247,7 @@ class OctString extends Node { } get type() { - return types.OCTSTRING; + return asnTypes.OCTSTRING; } getBodySize() { @@ -1188,7 +1300,7 @@ class Null extends Node { } get type() { - return types.NULL; + return asnTypes.NULL; } getBodySize() { @@ -1227,7 +1339,7 @@ class OID extends Node { } get type() { - return types.OID; + return asnTypes.OID; } getBodySize() { @@ -1440,7 +1552,7 @@ class Enum extends Integer { } get type() { - return types.ENUM; + return asnTypes.ENUM; } } @@ -1454,9 +1566,13 @@ class Utf8String extends Str { } get type() { - return types.UTF8STRING; + return asnTypes.UTF8STRING; } + /** + * @returns {BufferEncoding} + */ + get encoding() { return 'utf8'; } @@ -1474,7 +1590,7 @@ class RawSequence extends Node { } get type() { - return types.SEQUENCE; + return asnTypes.SEQUENCE; } getBodySize() { @@ -1546,7 +1662,7 @@ class RawSequence extends Node { } format() { - return this.toArray(); + return `<${this.constructor.name}: ${this.toArray().join(',')}>`; } } @@ -1560,7 +1676,7 @@ class RawSet extends RawSequence { } get type() { - return types.SET; + return asnTypes.SET; } } @@ -1574,7 +1690,7 @@ class NumString extends Str { } get type() { - return types.NUMSTRING; + return asnTypes.NUMSTRING; } } @@ -1588,7 +1704,7 @@ class PrintString extends Str { } get type() { - return types.PRINTSTRING; + return asnTypes.PRINTSTRING; } } @@ -1602,7 +1718,7 @@ class T61String extends Str { } get type() { - return types.T61STRING; + return asnTypes.T61STRING; } } @@ -1616,7 +1732,7 @@ class IA5String extends Str { } get type() { - return types.IA5STRING; + return asnTypes.IA5STRING; } } @@ -1707,7 +1823,7 @@ class UTCTime extends Time { } get type() { - return types.UTCTIME; + return asnTypes.UTCTIME; } getBodySize() { @@ -1765,7 +1881,7 @@ class GenTime extends Time { } get type() { - return types.GENTIME; + return asnTypes.GENTIME; } getBodySize() { @@ -1822,7 +1938,7 @@ class GenString extends Str { } get type() { - return types.GENSTRING; + return asnTypes.GENSTRING; } } @@ -1834,39 +1950,39 @@ function typeToClass(type) { assert((type >>> 0) === type); switch (type) { - case types.BOOLEAN: + case asnTypes.BOOLEAN: return Bool; - case types.INTEGER: + case asnTypes.INTEGER: return Integer; - case types.BITSTRING: + case asnTypes.BITSTRING: return BitString; - case types.OCTSTRING: + case asnTypes.OCTSTRING: return OctString; - case types.NULL: + case asnTypes.NULL: return Null; - case types.OID: + case asnTypes.OID: return OID; - case types.ENUM: + case asnTypes.ENUM: return Enum; - case types.UTF8STRING: + case asnTypes.UTF8STRING: return Utf8String; - case types.SEQUENCE: + case asnTypes.SEQUENCE: return RawSequence; - case types.SET: + case asnTypes.SET: return RawSet; - case types.NUMSTRING: + case asnTypes.NUMSTRING: return NumString; - case types.PRINTSTRING: + case asnTypes.PRINTSTRING: return PrintString; - case types.T61STRING: + case asnTypes.T61STRING: return T61String; - case types.IA5STRING: + case asnTypes.IA5STRING: return IA5String; - case types.UTCTIME: + case asnTypes.UTCTIME: return UTCTime; - case types.GENTIME: + case asnTypes.GENTIME: return GenTime; - case types.GENSTRING: + case asnTypes.GENSTRING: return GenString; default: throw new Error(`Unknown type: ${type}.`); @@ -2389,7 +2505,7 @@ exports.EMPTY = EMPTY; exports.ZERO = ZERO; exports.EMPTY_OID = EMPTY_OID; -exports.types = types; +exports.types = asnTypes; exports.typesByVal = typesByVal; exports.classes = classes; exports.classesByVal = classesByVal; diff --git a/lib/encoding/openssl.js b/lib/encoding/openssl.js index 5f7bd072d..fc0d57acd 100644 --- a/lib/encoding/openssl.js +++ b/lib/encoding/openssl.js @@ -15,6 +15,8 @@ const asn1 = require('./asn1'); const pem = require('./pem'); +/** @typedef {import('bufio').BufferReader} BufferReader */ + /** * DSAParams */ @@ -235,6 +237,15 @@ class DSAPrivateKey extends asn1.Sequence { x: this.x }; } + + /** + * @param {Buffer} data + * @returns {DSAPrivateKey} + */ + + static decode(data) { + return new this().decode(data); + } } /* diff --git a/lib/encoding/pem.js b/lib/encoding/pem.js index 78d85d533..abc31df96 100644 --- a/lib/encoding/pem.js +++ b/lib/encoding/pem.js @@ -63,7 +63,7 @@ class PEMBlock { if (it.done) throw new Error('No PEM data found.'); - const block = it.value; + const block = /** @type {PEMBlock} */(it.value); this.type = block.type; this.headers = block.headers; diff --git a/lib/encoding/pkcs1.js b/lib/encoding/pkcs1.js index 686c6d25b..6785fddc9 100644 --- a/lib/encoding/pkcs1.js +++ b/lib/encoding/pkcs1.js @@ -183,6 +183,15 @@ class RSAPrivateKey extends asn1.Sequence { qi: this.qi }; } + + /** + * @param {Buffer} data + * @returns {RSAPrivateKey} + */ + + static decode(data) { + return new this().decode(data); + } } /* diff --git a/lib/encoding/sec1.js b/lib/encoding/sec1.js index 1ce950875..d9eac0712 100644 --- a/lib/encoding/sec1.js +++ b/lib/encoding/sec1.js @@ -107,6 +107,16 @@ class ECPrivateKey extends asn1.Sequence { publicKey: this.publicKey }; } + + /** + * Decode ECPrivateKey. + * @param {Buffer} data + * @returns {ECPrivateKey} + */ + + static decode(data) { + return new this().decode(data); + } } /** diff --git a/lib/encoding/x509.js b/lib/encoding/x509.js index 44ed1b4a8..6c63f9974 100644 --- a/lib/encoding/x509.js +++ b/lib/encoding/x509.js @@ -351,7 +351,7 @@ class RDN extends asn1.Set { this.attributes[0].read(br); while (br.left()) { - const attr = Attribute.read(br); + const attr = /** @type {Attribute} */(Attribute.read(br)); this.attributes.push(attr); } diff --git a/lib/internal/hmac.js b/lib/internal/hmac.js index 37636e8fc..109def597 100644 --- a/lib/internal/hmac.js +++ b/lib/internal/hmac.js @@ -17,11 +17,55 @@ const assert = require('../internal/assert'); +/** + * @callback HashInit + * @param {Buffer} [data] + * @returns {Hash} + */ + +/** + * @callback HashUpdate + * @param {Buffer} data + * @returns {Hash} + */ + +/** + * @callback HashFinal + * @param {Buffer} [data] + * @returns {Buffer} + */ + +/** + * Hash + * @typedef {Object} Hash + * @property {HashInit} init + * @property {HashUpdate} update + * @property {HashFinal} final + */ + /** * HMAC */ class HMAC { + /** @type {Function} */ + hash; + + /** @type {Number} */ + size; + + /** @type {Array} */ + x; + + /** @type {Array} */ + y; + + /** @type {Hash} */ + inner; + + /** @type {Hash} */ + outer; + /** * Create an HMAC. * @param {Function} Hash @@ -41,13 +85,15 @@ class HMAC { this.x = x; this.y = y; + // @ts-ignore this.inner = new Hash(); + // @ts-ignore this.outer = new Hash(); } /** * Initialize HMAC context. - * @param {Buffer} data + * @param {Buffer} key */ init(key) { @@ -56,7 +102,10 @@ class HMAC { // Shorten key if (key.length > this.size) { const Hash = this.hash; - const h = new Hash(); + // @ts-ignore + const hi = new Hash(); + + const h = /** @type {Hash} */(hi); h.init(...this.x); h.update(key); diff --git a/lib/js/aead.js b/lib/js/aead.js index d0a7193f4..0b9953206 100644 --- a/lib/js/aead.js +++ b/lib/js/aead.js @@ -64,7 +64,7 @@ class AEAD { /** * Update the aad (will be finalized * on an encrypt/decrypt call). - * @param {Buffer} aad + * @param {Buffer} data */ aad(data) { @@ -241,7 +241,7 @@ class AEAD { * @param {Buffer} key * @param {Buffer} iv * @param {Buffer} msg - * @param {Buffer?} aad + * @param {Buffer} [aad] * @returns {Buffer} tag */ @@ -264,7 +264,7 @@ class AEAD { * @param {Buffer} iv * @param {Buffer} msg * @param {Buffer} tag - * @param {Buffer?} aad + * @param {Buffer} [aad] * @returns {Boolean} */ @@ -287,7 +287,7 @@ class AEAD { * @param {Buffer} iv * @param {Buffer} msg * @param {Buffer} tag - * @param {Buffer?} aad + * @param {Buffer} [aad] * @returns {Boolean} */ diff --git a/lib/js/base32.js b/lib/js/base32.js index 6143fa95a..a03a94e66 100644 --- a/lib/js/base32.js +++ b/lib/js/base32.js @@ -206,7 +206,7 @@ function _decode(str, table, unpad) { if (left > 0) throw new Error('Invalid base32 string.'); - if (str.length !== i + (-mode & 7) * unpad) + if (str.length !== i + (-mode & 7) * Number(unpad)) throw new Error('Invalid base32 string.'); for (; i < str.length; i++) { @@ -306,7 +306,7 @@ function _test(str, table, unpad) { break; } - if (str.length !== i + (-mode & 7) * unpad) + if (str.length !== i + (-mode & 7) * Number(unpad)) return false; for (; i < str.length; i++) { @@ -345,7 +345,7 @@ function decode(str, unpad = false) { * Test a base32 string. * @param {String} str * @param {Boolean} [unpad=false] - * @returns {Buffer} + * @returns {Boolean} */ function test(str, unpad = false) { @@ -378,7 +378,7 @@ function decodeHex(str, unpad = false) { * Test a base32 hex string. * @param {String} str * @param {Boolean} [unpad=false] - * @returns {Buffer} + * @returns {Boolean} */ function testHex(str, unpad = false) { diff --git a/lib/js/base58.js b/lib/js/base58.js index 85a28a526..8919b3669 100644 --- a/lib/js/base58.js +++ b/lib/js/base58.js @@ -174,7 +174,7 @@ function decode(str) { /** * Test whether the string is a base58 string. * @param {String} str - * @returns {Buffer} + * @returns {Boolean} */ function test(str) { diff --git a/lib/js/bech32.js b/lib/js/bech32.js index 1903e1874..d218eff91 100644 --- a/lib/js/bech32.js +++ b/lib/js/bech32.js @@ -312,7 +312,7 @@ class BECH32 { assert(srcbits >= 1 && srcbits <= 8); assert(dstbits >= 1 && dstbits <= 8); - return ((len * srcbits + (dstbits - 1) * (pad | 0)) / dstbits) >>> 0; + return ((len * srcbits + (dstbits - 1) * Number(pad)) / dstbits) >>> 0; } /** diff --git a/lib/js/blake2b.js b/lib/js/blake2b.js index 6705b7370..91126be53 100644 --- a/lib/js/blake2b.js +++ b/lib/js/blake2b.js @@ -283,10 +283,16 @@ BLAKE2b.ctx = new BLAKE2b(); * Helpers */ +/** + * @param {Number[]} v + * @param {Number} a + * @param {Number} b + */ + function sum64i(v, a, b) { const o0 = v[a + 0] + v[b + 0]; const o1 = v[a + 1] + v[b + 1]; - const c = (o0 >= 0x100000000) | 0; + const c = Number(o0 >= 0x100000000) | 0; v[a + 0] = o0; v[a + 1] = o1 + c; @@ -295,7 +301,7 @@ function sum64i(v, a, b) { function sum64w(v, a, b0, b1) { const o0 = v[a + 0] + b0; const o1 = v[a + 1] + b1; - const c = (o0 >= 0x100000000) | 0; + const c = Number(o0 >= 0x100000000) | 0; v[a + 0] = o0; v[a + 1] = o1 + c; diff --git a/lib/js/bn.js b/lib/js/bn.js index fcd35a1e3..a9591b0ea 100644 --- a/lib/js/bn.js +++ b/lib/js/bn.js @@ -241,7 +241,7 @@ class BN { iaddn(num) { enforce(isSMI(num), 'num', 'smi'); - const negative = (num < 0) | 0; + const negative = Number(num < 0) | 0; if (negative) num = -num; @@ -379,7 +379,7 @@ class BN { isubn(num) { enforce(isSMI(num), 'num', 'smi'); - const negative = (num < 0) | 0; + const negative = Number(num < 0) | 0; if (negative) num = -num; @@ -443,7 +443,7 @@ class BN { imuln(num) { enforce(isSMI(num), 'num', 'smi'); - const neg = (num < 0) | 0; + const neg = Number(num < 0) | 0; if (neg) num = -num; @@ -736,7 +736,7 @@ class BN { enforce(isSMI(num), 'num', 'smi'); nonzero(num !== 0); - const neg = (num < 0) | 0; + const neg = Number(num < 0) | 0; if (neg) num = -num; @@ -1137,7 +1137,7 @@ class BN { iandn(num) { enforce(isSMI(num), 'num', 'smi'); - if ((this.negative | (num < 0)) !== 0) + if ((this.negative | Number(num < 0)) !== 0) return this.iand(new BN(num)); this.words[0] &= num; @@ -1157,7 +1157,7 @@ class BN { andrn(num) { enforce(isSMI(num), 'num', 'smi'); - if ((this.negative | (num < 0)) !== 0) { + if ((this.negative | Number(num < 0)) !== 0) { const n = this.iand(new BN(num)); if (n.length > 1) @@ -1257,7 +1257,7 @@ class BN { iorn(num) { enforce(isSMI(num), 'num', 'smi'); - if ((this.negative | (num < 0)) !== 0) + if ((this.negative | Number(num < 0)) !== 0) return this.ior(new BN(num)); this.words[0] |= num; @@ -1355,7 +1355,7 @@ class BN { ixorn(num) { enforce(isSMI(num), 'num', 'smi'); - if ((this.negative | (num < 0)) !== 0) + if ((this.negative | Number(num < 0)) !== 0) return this.ixor(new BN(num)); this.words[0] ^= num; @@ -1857,7 +1857,7 @@ class BN { cmpn(num) { enforce(isSMI(num), 'num', 'smi'); - const negative = (num < 0) | 0; + const negative = Number(num < 0) | 0; if (this.negative !== negative) return negative - this.negative; @@ -1960,7 +1960,7 @@ class BN { if (a === b) continue; - return (a > b) - (a < b); + return Number(a > b) - Number(a < b); } return 0; @@ -1977,7 +1977,7 @@ class BN { if (num < 0) num = -num; - return (w > num) - (w < num); + return Number(w > num) - Number(w < num); } /* @@ -3090,6 +3090,10 @@ next: return this.toDouble(); } + /** + * @returns {bigint} + */ + toBigInt() { if (!HAS_BIGINT) throw new Error('BigInt is not supported!'); @@ -3331,7 +3335,7 @@ next: enforce(isInteger(num), 'num', 'integer'); enforce(endian === 'be' || endian === 'le', 'endian', 'endianness'); - const neg = (num < 0) | 0; + const neg = Number(num < 0) | 0; if (neg) num = -num; @@ -3372,7 +3376,7 @@ next: if (!isFinite(num)) num = 0; - const neg = (num <= -1) | 0; + const neg = Number(num <= -1) | 0; if (num < 0) num = -num; @@ -3415,7 +3419,7 @@ next: // You know the implementation has a // problem when strings are twice // as fast as bigints. - const start = (num < BigInt(0)) | 0; + const start = Number(num < BigInt(0)) | 0; this._fromHex(num.toString(16), start); this.negative = start; @@ -3691,7 +3695,7 @@ next: if (typeof num === 'object') { if (BN.isBN(num)) - return this.fromBN(num, endian); + return this.fromBN(num); if ((num.length >>> 0) === num.length) return this.fromArrayLike(num, endian); @@ -4215,6 +4219,7 @@ class Prime116 extends Prime { let t = b; let m = 0; + // @ts-ignore while (t.cmpn(1) !== 0 && m < k) { t = red.sqr(t); m += 1; @@ -6266,6 +6271,11 @@ function bigMulTo(self, num, out) { return out._strip(); } +/** + * @param {BN} x + * @param {BN} y + */ + function jumboMulTo(x, y, out) { // v8 has a 2147483519 bit max (~256mb). if (!HAS_BIGINT || x.length + y.length > 82595519) @@ -6275,9 +6285,9 @@ function jumboMulTo(x, y, out) { const mask = BigInt(0x3ffffff); const shift = BigInt(26); - let z = x.toBigInt() * y.toBigInt(); + let z = (x.toBigInt() * y.toBigInt()); - const neg = (z < zero) | 0; + const neg = Number(z < zero) | 0; if (neg) z = -z; @@ -6914,8 +6924,10 @@ function comb10MulTo(self, num, out) { } // Polyfill comb. -if (!Math.imul) +if (!Math.imul) { + // @ts-ignore comb10MulTo = smallMulTo; +} /* * Expose diff --git a/lib/js/cash32.js b/lib/js/cash32.js index cef52a5f7..9653149cc 100644 --- a/lib/js/cash32.js +++ b/lib/js/cash32.js @@ -372,7 +372,7 @@ function convertSize(len, srcbits, dstbits, pad) { assert(srcbits >= 1 && srcbits <= 8); assert(dstbits >= 1 && dstbits <= 8); - return ((len * srcbits + (dstbits - 1) * (pad | 0)) / dstbits) >>> 0; + return ((len * srcbits + (dstbits - 1) * (Number(pad) | 0)) / dstbits) >>> 0; } /** diff --git a/lib/js/cipher.js b/lib/js/cipher.js index 547a4b523..371440826 100644 --- a/lib/js/cipher.js +++ b/lib/js/cipher.js @@ -149,6 +149,10 @@ class CipherBase { return this.ctx.crypt(output, input); } + /** + * @returns {Buffer} + */ + final() { return this.ctx.final(); } diff --git a/lib/js/ciphers/modes.js b/lib/js/ciphers/modes.js index 7e2dabbf3..deca83468 100644 --- a/lib/js/ciphers/modes.js +++ b/lib/js/ciphers/modes.js @@ -263,6 +263,10 @@ class Block extends Mode { return output; } + /** + * @returns {Buffer} + */ + final() { if (this.blockPos === -1) throw new Error('Cipher is not initialized.'); @@ -301,6 +305,10 @@ class Block extends Mode { throw new Error('Not implemented.'); } + /** + * @returns {Buffer} + */ + _final() { throw new Error('Not implemented.'); } @@ -362,6 +370,10 @@ class Stream extends Mode { return output; } + /** + * @returns {Buffer} + */ + final() { if (this.pos === -1) throw new Error('Cipher is not initialized.'); @@ -391,6 +403,10 @@ class Stream extends Mode { throw new Error('Not implemented.'); } + /** + * @returns {Buffer} + */ + _final() { throw new Error('Not implemented.'); } @@ -1547,7 +1563,7 @@ class CCM extends Stream { const M = tagLen; const N = 15 - L; - const Adata = (aad && aad.length > 0) | 0; + const Adata = Number(aad && aad.length > 0) | 0; const block = Buffer.alloc(16); if (M < 4 || M > 16 || (M & 1) !== 0) diff --git a/lib/js/dsa.js b/lib/js/dsa.js index d4abe33f7..c41696329 100644 --- a/lib/js/dsa.js +++ b/lib/js/dsa.js @@ -519,7 +519,7 @@ function paramsGenerate(bits) { /** * Generate params. * @param {Number} [bits=2048] - * @returns {Buffer} + * @returns {Promise} */ async function paramsGenerateAsync(bits) { @@ -665,7 +665,7 @@ function privateKeyGenerate(bits) { /** * Generate private key. * @param {Number} [bits=2048] - * @returns {Buffer} + * @returns {Promise} */ async function privateKeyGenerateAsync(bits) { @@ -767,7 +767,7 @@ function privateKeyImport(json) { /** * Export a private key in OpenSSL ASN.1 format. * @param {Buffer} key - * @returns {Buffer} + * @returns {Object} */ function privateKeyExport(key) { @@ -955,8 +955,8 @@ function signDER(msg, key) { * Sign a message. * @private * @param {Buffer} msg - * @param {Buffer} key - * @returns {Signature} + * @param {DSAPrivateKey} key + * @returns {DSASignature} */ function _sign(msg, key) { @@ -1125,8 +1125,8 @@ function verifyDER(msg, sig, key) { * Verify a signature. * @private * @param {Buffer} msg - * @param {Signature} S - * @param {Buffer} key + * @param {DSASignature} S + * @param {DSAPublicKey} key * @returns {Boolean} */ diff --git a/lib/js/ecdsa.js b/lib/js/ecdsa.js index d95ede061..950ffaaca 100644 --- a/lib/js/ecdsa.js +++ b/lib/js/ecdsa.js @@ -478,7 +478,7 @@ class ECDSA { if (s.isZero()) continue; - let param = R.isOdd() | (!x.eq(r) << 1); + let param = R.isOdd() | (Number(!x.eq(r)) << 1); if (s.cmp(nh) > 0) { s.ineg().imod(n); diff --git a/lib/js/elliptic.js b/lib/js/elliptic.js index 324840c58..f8e61957f 100644 --- a/lib/js/elliptic.js +++ b/lib/js/elliptic.js @@ -154,6 +154,7 @@ * [ECPM] Elliptic Curve Point Multiplication (wikipedia) * https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication */ +// @ts-nocheck 'use strict'; @@ -276,7 +277,7 @@ class Curve { // Precalculate encoding length. this.fieldSize = this.p.byteLength(); this.fieldBits = this.p.bitLength(); - this.adjustedSize = this.fieldSize + ((this.fieldBits & 7) === 0); + this.adjustedSize = this.fieldSize + Number((this.fieldBits & 7) === 0); this.signBit = this.adjustedSize * 8 - 1; this.mask = 0xff; @@ -361,6 +362,7 @@ class Curve { let len = h; while (out.length < len) { + /** @type {Point} */ let p; x.redIAdd(this.one); @@ -383,7 +385,8 @@ class Curve { p.normalize(); - for (const point of [p, p.neg()]) { + for (const _point of [p, p.neg()]) { + const point = /** @type {Point} */(_point); const key = point.key(); if (!set.has(key)) { @@ -686,6 +689,10 @@ class Curve { return x.toRed(this.red); } + /** + * @returns {Point} + */ + point(x, y) { throw new Error('Not implemented.'); } @@ -734,6 +741,10 @@ class Curve { throw new Error('Not implemented.'); } + /** + * @returns {Point} + */ + pointFromX(x, sign) { throw new Error('Not implemented.'); } @@ -1539,10 +1550,19 @@ class Point { throw new Error('Not implemented.'); } + /** + * @param {Point} point + * @returns {Number} + */ + cmp(point) { throw new Error('Not implemented.'); } + /** + * @returns {Boolean} + */ + isInfinity() { throw new Error('Not implemented.'); } diff --git a/lib/js/hash-drbg.js b/lib/js/hash-drbg.js index 953e71b44..75b95b259 100644 --- a/lib/js/hash-drbg.js +++ b/lib/js/hash-drbg.js @@ -151,7 +151,7 @@ class HashDRBG { assert((len >>> 0) === len); assert((prepend & 0xff) === prepend); - const pre = (prepend !== 0xff) | 0; + const pre = Number(prepend !== 0xff) | 0; const data = Buffer.alloc(5 + pre + input.length); data[0] = 0x01; diff --git a/lib/js/pbkdf2.js b/lib/js/pbkdf2.js index 166d25b53..42273ff3b 100644 --- a/lib/js/pbkdf2.js +++ b/lib/js/pbkdf2.js @@ -18,9 +18,19 @@ const assert = require('../internal/assert'); const crypto = global.crypto || global.msCrypto || {}; const subtle = crypto.subtle || {}; +/** + * @typedef {Object} Hash + * @property {String} id + * @property {Number} size + * @property {Number} bits + * @property {Number} blockSize + * @property {Function} digest + * @property {Function} mac + */ + /** * Perform key derivation using PBKDF2. - * @param {Function} hash + * @param {Hash} hash * @param {Buffer} pass * @param {Buffer} salt * @param {Number} iter @@ -84,12 +94,12 @@ function derive(hash, pass, salt, iter, len) { /** * Execute pbkdf2 asynchronously. - * @param {Function} hash + * @param {Hash} hash * @param {Buffer} pass * @param {Buffer} salt * @param {Number} iter * @param {Number} len - * @returns {Promise} + * @returns {Promise} */ async function deriveAsync(hash, pass, salt, iter, len) { diff --git a/lib/js/rsa.js b/lib/js/rsa.js index ff1a4a4c3..7d01b6a62 100644 --- a/lib/js/rsa.js +++ b/lib/js/rsa.js @@ -746,7 +746,7 @@ function privateKeyGenerate(bits, exponent) { * Generate a private key. * @param {Number} [bits=2048] * @param {Number} [exponent=65537] - * @returns {Buffer} Private key. + * @returns {Promise} Private key. */ async function privateKeyGenerateAsync(bits, exponent) { @@ -1335,7 +1335,7 @@ function _verifyPSS(hash, msg, sig, key, saltLen) { * @param {Object} hash * @param {Buffer} msg * @param {Buffer} key - * @param {Buffer?} label + * @param {Buffer} [label] * @returns {Buffer} */ @@ -1387,7 +1387,7 @@ function encryptOAEP(hash, msg, key, label) { * @param {Object} hash * @param {Buffer} msg * @param {Buffer} key - * @param {Buffer?} label + * @param {Buffer} [label] * @returns {Buffer} */ diff --git a/lib/js/salsa20.js b/lib/js/salsa20.js index 3b0e04283..c3f08d112 100644 --- a/lib/js/salsa20.js +++ b/lib/js/salsa20.js @@ -45,7 +45,7 @@ class Salsa20 { * Initialize salsa20 with a key, nonce, and counter. * @param {Buffer} key * @param {Buffer} nonce - * @param {Number} counter + * @param {Number} [counter=0] */ init(key, nonce, counter) { diff --git a/lib/js/sha512.js b/lib/js/sha512.js index 6d150ebc6..db9f1a992 100644 --- a/lib/js/sha512.js +++ b/lib/js/sha512.js @@ -386,13 +386,13 @@ function sum64_4_hi(ah, al, bh, bl, ch, cl, dh, dl) { let lo = al; lo = (lo + bl) >>> 0; - carry += (lo < al); + carry += Number(lo < al); lo = (lo + cl) >>> 0; - carry += (lo < cl); + carry += Number(lo < cl); lo = (lo + dl) >>> 0; - carry += (lo < dl); + carry += Number(lo < dl); const hi = ah + bh + ch + dh + carry; @@ -409,16 +409,16 @@ function sum64_5_hi(ah, al, bh, bl, ch, cl, dh, dl, eh, el) { let lo = al; lo = (lo + bl) >>> 0; - carry += (lo < al); + carry += Number(lo < al); lo = (lo + cl) >>> 0; - carry += (lo < cl); + carry += Number(lo < cl); lo = (lo + dl) >>> 0; - carry += (lo < dl); + carry += Number(lo < dl); lo = (lo + el) >>> 0; - carry += (lo < el); + carry += Number(lo < el); const hi = ah + bh + ch + dh + eh + carry; diff --git a/lib/keccak.js b/lib/keccak.js index 25bcb8a19..9e8bf7ffb 100644 --- a/lib/keccak.js +++ b/lib/keccak.js @@ -6,7 +6,11 @@ 'use strict'; +let Keccak; + if (process.env.NODE_BACKEND === 'js') - module.exports = require('./js/keccak'); + Keccak = require('./js/keccak'); else - module.exports = require('./native/keccak'); + Keccak = require('./native/keccak'); + +module.exports = Keccak; diff --git a/lib/native/bcrypt.js b/lib/native/bcrypt.js index 96ba699e3..6cf046e55 100644 --- a/lib/native/bcrypt.js +++ b/lib/native/bcrypt.js @@ -23,9 +23,9 @@ function generate(pass, salt, rounds, minor = 'b') { assert(typeof minor === 'string'); assert(minor.length === 1); - minor = minor.charCodeAt(0) & 0x7f; + const minorn = minor.charCodeAt(0) & 0x7f; - return binding.bcrypt_generate(pass, salt, rounds, minor); + return binding.bcrypt_generate(pass, salt, rounds, minorn); } function verify(pass, record) { diff --git a/lib/native/bn.js b/lib/native/bn.js index 4d73fb259..f8aaae87b 100644 --- a/lib/native/bn.js +++ b/lib/native/bn.js @@ -36,6 +36,7 @@ * https://github.com/v8/v8/blob/master/src/objects/bigint.cc */ +// @ts-nocheck /* eslint valid-typeof: "off" */ 'use strict'; diff --git a/lib/native/dsa.js b/lib/native/dsa.js index 7850a93bb..54311fd6e 100644 --- a/lib/native/dsa.js +++ b/lib/native/dsa.js @@ -41,7 +41,7 @@ function paramsGenerate(bits) { /** * Generate params. * @param {Number} [bits=2048] - * @returns {Buffer} + * @returns {Promise} */ async function paramsGenerateAsync(bits) { @@ -151,7 +151,7 @@ function privateKeyGenerate(bits) { /** * Generate private key. * @param {Number} [bits=2048] - * @returns {Buffer} + * @returns {Promise} */ async function privateKeyGenerateAsync(bits) { diff --git a/lib/native/rsa.js b/lib/native/rsa.js index b8a38b3d7..38fe97158 100644 --- a/lib/native/rsa.js +++ b/lib/native/rsa.js @@ -55,7 +55,7 @@ function privateKeyGenerate(bits, exponent) { * Generate a private key. * @param {Number} [bits=2048] * @param {Number} [exponent=65537] - * @returns {Buffer} Private key. + * @returns {Promise} Private key. */ async function privateKeyGenerateAsync(bits, exponent) { @@ -343,7 +343,7 @@ function verifyPSS(hash, msg, sig, key, saltLen = -1) { * @param {Object} hash * @param {Buffer} msg * @param {Buffer} key - * @param {Buffer?} label + * @param {Buffer} [label] * @returns {Buffer} */ @@ -367,7 +367,7 @@ function encryptOAEP(hash, msg, key, label) { * @param {Object} hash * @param {Buffer} msg * @param {Buffer} key - * @param {Buffer?} label + * @param {Buffer} [label] * @returns {Buffer} */ diff --git a/lib/native/schnorr-libsecp256k1.js b/lib/native/schnorr-libsecp256k1.js index e1a1d1efa..7619a8219 100644 --- a/lib/native/schnorr-libsecp256k1.js +++ b/lib/native/schnorr-libsecp256k1.js @@ -135,7 +135,7 @@ function publicKeyFromUniform(bytes) { /** * Run public key through Shallue-van de Woestijne inverse. * @param {Buffer} key - * @param {Number?} hint + * @param {Number} [hint] * @returns {Buffer} */ @@ -300,7 +300,7 @@ function publicKeyCombine(keys) { * Sign a message. * @param {Buffer} msg * @param {Buffer} key - * @param {Buffer?} aux + * @param {Buffer} [aux] * @returns {Buffer} */ diff --git a/lib/native/secp256k1-libsecp256k1.js b/lib/native/secp256k1-libsecp256k1.js index ca7d1151c..816d4586d 100644 --- a/lib/native/secp256k1-libsecp256k1.js +++ b/lib/native/secp256k1-libsecp256k1.js @@ -158,7 +158,7 @@ function publicKeyFromUniform(bytes, compress = true) { /** * Run public key through Shallue-van de Woestijne inverse. * @param {Buffer} key - * @param {Number?} hint + * @param {Number} [hint] * @returns {Buffer} */ diff --git a/lib/pgp.js b/lib/pgp.js index 72adaca19..9b8252d08 100644 --- a/lib/pgp.js +++ b/lib/pgp.js @@ -31,6 +31,8 @@ const SHA384 = require('./sha384'); const SHA512 = require('./sha512'); const pgpdf = require('./internal/pgpdf'); +/** @typedef {import('bufio').BufferReader} BufferReader */ + /* * Constants */ @@ -314,6 +316,9 @@ class PGPMessage extends bio.Struct { */ class PGPPacket extends bio.Struct { + /** @type {PGPBody} */ + body; + constructor() { super(); this.type = 0; @@ -529,6 +534,15 @@ class PGPUnknown extends PGPBody { data: this.data.toString('hex') }; } + + /** + * @param {BufferReader} br + * @returns {PGPUnknown} + */ + + static read(br) { + return new this().read(br); + } } /** @@ -1076,6 +1090,15 @@ class PGPPublicKey extends PGPBody { } } } + + /** + * @param {BufferReader} br + * @returns {PGPPublicKey} + */ + + static read(br) { + return new this().read(br); + } } /** @@ -1146,6 +1169,15 @@ class PGPPrivateKey extends PGPBody { data }; } + + /** + * @param {BufferReader} br + * @returns {PGPPrivateKey} + */ + + static read(br) { + return new this().read(br); + } } /** @@ -1747,6 +1779,15 @@ class PGPUserID extends PGPBody { id: this.id }; } + + /** + * @param {BufferReader} br + * @returns {PGPUserID} + */ + + static read(br) { + return new this().read(br); + } } /** diff --git a/lib/ssh.js b/lib/ssh.js index a54ab3e22..29e66354a 100644 --- a/lib/ssh.js +++ b/lib/ssh.js @@ -12,7 +12,7 @@ 'use strict'; -const assert = require('./internal/assert'); +const assert = require('bsert'); const bio = require('bufio'); const base64 = require('./encoding/base64'); const {padLeft, padRight} = require('./encoding/util'); @@ -30,10 +30,19 @@ const p521 = require('./p521'); const ed25519 = require('./ed25519'); const BN = require('./bn'); +/** @typedef {import('bufio').BufferReader} BufferReader */ +/** @typedef {import('bufio').StaticWriter} StaticWriter */ + /* * Constants */ +/** @typedef {String} keyType */ + +/** + * @enum {keyType} + */ + const keyTypes = { DSA: 'ssh-dss', RSA: 'ssh-rsa', @@ -267,6 +276,10 @@ class SSHPublicKey extends bio.Struct { return this; } + /** + * @returns {Object} + */ + format() { switch (this.type) { case keyTypes.DSA: { @@ -349,6 +362,11 @@ class SSHPrivateKey extends bio.Struct { return typeToCurve[this.type]; } + /** + * @param {String} [passwd=null] + * @returns {Buffer} + */ + encodeSSH(passwd) { assert(passwd == null || typeof passwd === 'string'); @@ -356,7 +374,7 @@ class SSHPrivateKey extends bio.Struct { const pub = new SSHPublicKey(); const priv = new RawPrivateKey(); - const bw = bio.write(8192); + const bw = /** @type {StaticWriter} */(bio.write(8192)); bw.writeString(AUTH_MAGIC); bw.writeU8(0); @@ -442,6 +460,12 @@ class SSHPrivateKey extends bio.Struct { return bw.slice(); } + /** + * @param {Buffer} data + * @param {String} [passwd=null] + * @returns {this} + */ + decodeSSH(data, passwd) { const br = bio.read(data); const magic = br.readString(14, 'binary'); @@ -456,7 +480,7 @@ class SSHPrivateKey extends bio.Struct { throw new Error('Too many SSH keys.'); const pubRaw = readBytes(br); - const publicKey = SSHPublicKey.decode(pubRaw); + const publicKey = /** @type {SSHPublicKey} */(SSHPublicKey.decode(pubRaw)); let privRaw = readBytes(br); @@ -470,7 +494,7 @@ class SSHPrivateKey extends bio.Struct { privRaw = decrypt(privRaw, cipher, passwd, kdf.salt, kdf.rounds); } - const priv = RawPrivateKey.decode(privRaw); + const priv = /** @type {RawPrivateKey} */(RawPrivateKey.decode(privRaw)); if (priv.type !== publicKey.type) throw new Error('Public/private mismatch.'); @@ -634,6 +658,13 @@ class SSHPrivateKey extends bio.Struct { return block.toString(); } + /** + * @override + * @param {String} str + * @param {String} [passwd] + * @returns {this} + */ + fromString(str, passwd) { const block = PEMBlock.fromString(str); @@ -828,7 +859,7 @@ class KDFOptions extends bio.Struct { return bw; } - read(br) { + read(br, extra) { this.name = readString(br); const child = readChild(br); @@ -846,6 +877,15 @@ class KDFOptions extends bio.Struct { return this; } + + /** + * @param {BufferReader} br + * @returns {KDFOptions} + */ + + static read(br) { + return new this().read(br); + } } /** diff --git a/package-lock.json b/package-lock.json index 9a791b803..ad6bf21e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,13 @@ "hasInstallScript": true, "license": "MIT", "dependencies": { - "bufio": "~1.0.7", + "bufio": "~1.2.1", "loady": "~0.0.5" }, "devDependencies": { - "bmocha": "^2.1.4", - "bsert": "~0.0.10" + "bmocha": "^2.1.8", + "bsert": "~0.0.12", + "bts-type-deps": "^0.0.3" }, "engines": { "node": ">=14.0.0" @@ -43,12 +44,18 @@ "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 + }, "node_modules/bufio": { - "version": "1.0.7", - "resolved": "https://registry.npmjs.org/bufio/-/bufio-1.0.7.tgz", - "integrity": "sha512-bd1dDQhiC+bEbEfg56IdBv7faWa6OipMs/AFFFvtFnB3wAYjlwQpQRZ0pm6ZkgtfL0pILRXhKxOiQj6UzoMR7A==", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/bufio/-/bufio-1.2.1.tgz", + "integrity": "sha512-9oR3zNdupcg/Ge2sSHQF3GX+kmvL/fTPvD0nd5AGLq8SjUYnTz+SlFjK/GXidndbZtIj+pVKXiWeR9w6e9wKCA==", "engines": { - "node": ">=8.0.0" + "node": ">=14.0.0" } }, "node_modules/loady": { diff --git a/package.json b/package.json index 61b098fa1..356a84de5 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "scripts": { "install": "node-gyp rebuild", "lint": "eslint bench/*.js lib/ scripts/ test/", + "lint-types": "tsc -p .", "test": "bmocha -S test/*-test.js", "test-browser": "bmocha -B js -H -S test/*-test.js", "test-js": "bmocha -B js -S test/*-test.js", @@ -34,12 +35,13 @@ "test-all": "npm run test-browser && npm run test-js && npm run test-bigint && npm run test-torsion && npm run test-native" }, "dependencies": { - "bufio": "~1.0.7", + "bufio": "~1.2.1", "loady": "~0.0.5" }, "devDependencies": { - "bmocha": "^2.1.4", - "bsert": "~0.0.10" + "bmocha": "^2.1.8", + "bsert": "~0.0.12", + "bts-type-deps": "^0.0.3" }, "engines": { "node": ">=14.0.0" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..da7a94b1e --- /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" + ] + } +}