diff --git a/package.json b/package.json index 4c23815..798f551 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint": "aegir lint", "test:node": "aegir test -t node", "test:browser": "aegir test -t browser -t webworker", - "build": "aegir build", + "prepare": "aegir build --no-bundle", "docs": "aegir docs", "release": "aegir release", "release-minor": "aegir release --type minor", @@ -36,18 +36,26 @@ "url": "https://github.com/libp2p/js-libp2p-record/issues" }, "homepage": "https://github.com/libp2p/js-libp2p-record", + "files": [ + "src", + "dist" + ], + "eslintConfig": { + "extends": "ipfs" + }, + "types": "dist/src/index.d.ts", "devDependencies": { - "aegir": "^25.0.0", - "libp2p-crypto": "^0.18.0", + "aegir": "^30.3.0", + "libp2p-crypto": "^0.19.0", "multibase": "^3.0.0", "peer-id": "^0.14.0" }, "dependencies": { - "err-code": "^2.0.0", + "err-code": "^3.0.0", "multihashes": "^3.0.1", - "multihashing-async": "^2.0.1", + "multihashing-async": "^2.1.0", "protons": "^2.0.0", - "uint8arrays": "^1.1.0" + "uint8arrays": "^2.0.5" }, "contributors": [ "Vasco Santos ", diff --git a/src/record.js b/src/record.js index f80f3ff..3bf2124 100644 --- a/src/record.js +++ b/src/record.js @@ -1,16 +1,21 @@ 'use strict' +// @ts-ignore const protons = require('protons') const pb = protons(require('./record.proto')).Record const utils = require('./utils') +/** + * @typedef {{ key: Uint8Array, value: Uint8Array, timeReceived: string }} ProtobufRecord + */ + class Record { /** * @param {Uint8Array} [key] * @param {Uint8Array} [value] - * @param {Date} [recvtime] + * @param {Date} [timeReceived] */ - constructor (key, value, recvtime) { + constructor (key, value, timeReceived) { if (!(key instanceof Uint8Array)) { throw new Error('key must be a Uint8Array') } @@ -21,20 +26,15 @@ class Record { this.key = key this.value = value - this.timeReceived = recvtime + this.timeReceived = timeReceived } - /** - * @returns {Uint8Array} - */ serialize () { return pb.encode(this.prepareSerialize()) } /** * Return the object format ready to be given to the protobuf library. - * - * @returns {Object} */ prepareSerialize () { return { @@ -48,7 +48,6 @@ class Record { * Decode a protobuf encoded record. * * @param {Uint8Array} raw - * @returns {Record} */ static deserialize (raw) { const dec = pb.decode(raw) @@ -58,8 +57,7 @@ class Record { /** * Create a record from the raw object returned from the protobuf library. * - * @param {Object} obj - * @returns {Record} + * @param {ProtobufRecord} obj */ static fromDeserialized (obj) { let recvtime diff --git a/src/selection.js b/src/selection.js index c2a7f3b..0754030 100644 --- a/src/selection.js +++ b/src/selection.js @@ -6,10 +6,9 @@ const uint8ArrayToString = require('uint8arrays/to-string') /** * Select the best record out of the given records. * - * @param {Object} selectors + * @param {{ [key: string]: function (Uint8Array, Uint8Array[]): number }} selectors * @param {Uint8Array} k * @param {Array} records - * @returns {number} - The index of the best record. */ const bestRecord = (selectors, k, records) => { if (records.length === 0) { diff --git a/src/selectors/public-key.js b/src/selectors/public-key.js index db0790b..ee62ef3 100644 --- a/src/selectors/public-key.js +++ b/src/selectors/public-key.js @@ -7,7 +7,6 @@ * * @param {Uint8Array} k * @param {Array} records - * @returns {number} */ const publicKeySelector = (k, records) => { return 0 diff --git a/src/utils.js b/src/utils.js index 73490fc..b9bd032 100644 --- a/src/utils.js +++ b/src/utils.js @@ -5,7 +5,6 @@ * string. * * @param {Date} time - * @returns {string} */ module.exports.toRFC3339 = (time) => { const year = time.getUTCFullYear() @@ -25,7 +24,6 @@ module.exports.toRFC3339 = (time) => { * JavaScript Date object. * * @param {string} time - * @returns {Date} */ module.exports.parseRFC3339 = (time) => { const rfc3339Matcher = new RegExp( diff --git a/src/validator.js b/src/validator.js index 43452dd..405aa83 100644 --- a/src/validator.js +++ b/src/validator.js @@ -3,14 +3,17 @@ const errcode = require('err-code') const uint8ArrayToString = require('uint8arrays/to-string') +/** + * @typedef {import('./record')} Record + */ + /** * Checks a record and ensures it is still valid. * It runs the needed validators. * If verification fails the returned Promise will reject with the error. * - * @param {Object} validators + * @param {{ [key: string]: { func: (key: Uint8Array, value: Uint8Array) => Promise }}} validators * @param {Record} record - * @returns {Promise} */ const verifyRecord = (validators, record) => { const key = record.key diff --git a/src/validators/public-key.js b/src/validators/public-key.js index 55c653d..5dcd7a9 100644 --- a/src/validators/public-key.js +++ b/src/validators/public-key.js @@ -13,7 +13,6 @@ const uint8ArrayEquals = require('uint8arrays/equals') * * @param {Uint8Array} key - A valid key is of the form `'/pk/'` * @param {Uint8Array} publicKey - The public key to validate against (protobuf encoded). - * @returns {Promise} */ const validatePublicKeyRecord = async (key, publicKey) => { if (!(key instanceof Uint8Array)) { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..07e8db2 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "test", + "src" + ] +}