From 4e25c6e51c77406003e8ceb68e21c8e7cb583da0 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Mon, 30 Nov 2020 13:59:09 -0800 Subject: [PATCH] fix: add type annotations to improve innference --- src/connection/status.js | 6 +++--- src/pubsub/signature-policy.js | 4 ++-- src/pubsub/utils.js | 19 ++++++++++++------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/connection/status.js b/src/connection/status.js index 120dbad..e585ba8 100644 --- a/src/connection/status.js +++ b/src/connection/status.js @@ -1,7 +1,7 @@ 'use strict' module.exports = { - OPEN: 'open', - CLOSING: 'closing', - CLOSED: 'closed' + OPEN: /** @type {'open'} */('open'), + CLOSING: /** @type {'closing'} */('closing'), + CLOSED: /** @type {'closed'} */('closed') } diff --git a/src/pubsub/signature-policy.js b/src/pubsub/signature-policy.js index 0b5fa8c..d81d8b3 100644 --- a/src/pubsub/signature-policy.js +++ b/src/pubsub/signature-policy.js @@ -13,7 +13,7 @@ exports.SignaturePolicy = { * * Enforce the fields to be present, reject otherwise. * * Propagate only if the fields are valid and signature can be verified, reject otherwise. */ - StrictSign: 'StrictSign', + StrictSign: /** @type {'StrictSign'} */ ('StrictSign'), /** * On the producing side: * * Build messages without the signature, key, from and seqno fields. @@ -24,5 +24,5 @@ exports.SignaturePolicy = { * * Propagate only if the fields are absent, reject otherwise. * * A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash. */ - StrictNoSign: 'StrictNoSign' + StrictNoSign: /** @type {'StrictNoSign'} */ 'StrictNoSign' } diff --git a/src/pubsub/utils.js b/src/pubsub/utils.js index eab7168..ae1b6b7 100644 --- a/src/pubsub/utils.js +++ b/src/pubsub/utils.js @@ -71,8 +71,9 @@ exports.anyMatch = (a, b) => { /** * Make everything an array. * - * @param {any} maybeArray - * @returns {Array} + * @template T + * @param {T|T[]} maybeArray + * @returns {T[]} * @private */ exports.ensureArray = (maybeArray) => { @@ -85,9 +86,11 @@ exports.ensureArray = (maybeArray) => { /** * Ensures `message.from` is base58 encoded - * @param {object} message - * @param {String} peerId - * @return {object} + * + * @template {Object} T + * @param {T} message + * @param {string} [peerId] + * @return {T & {from?: string, peerId?: string }} */ exports.normalizeInRpcMessage = (message, peerId) => { const m = Object.assign({}, message) @@ -101,8 +104,10 @@ exports.normalizeInRpcMessage = (message, peerId) => { } /** - * @param {object} message - * @return {object} + * @template {Object} T + * + * @param {T} message + * @return {T & {from?: Uint8Array, data?: Uint8Array}} */ exports.normalizeOutRpcMessage = (message) => { const m = Object.assign({}, message)