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AAmB,EAAG,aACtC,EAAG,mBAAmB,EAAQ,GAC9B,EAAY,EACL,YAAW,IAAM,EAAW,GAAW,KAAK,GAAS,MAAM,IAAS,OAE7E,EAAkB,GAAG,WAAY,GAAQ,EAAG,sBAAsB,KAC3D,EAAkB,QAAQ,EAAQ,EAAM,GAAU,EAAC,EAAK,EAAQ,IACjE,GACF,IACO,EAAW,GAAW,KAAK,GAAS,MAAM,IACxC,EACF,EAAO,IAEd,EAAG,OACI,EAAQ,QAIhB,CAEO,CAAW,EACpB,EAGF,OAAO,QAAU","file":"/npm/@verady/rpcsend@0.0.7/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@verady/rpcsend@0.0.7/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const jayson = require('jayson');\n\nclass RPCSend {\n  /**\n   * @param {String} connectUri - url where rpc lives\n   * @returns {Class} RPCSend\n   */\n  constructor(method, connectUri, port, logMechanism) {\n    if (!method) {\n      throw new Error('one to one kinda relationship, need a method for every new service');\n    }\n\n    if (!connectUri) {\n      process.stdout.write(`no connect uri, so setting to local`);\n    }\n\n    this.method = method;\n    this.connection = connectUri;\n    this.port = port;\n    this.running = false;\n    this.log = logMechanism;\n  }\n\n  /**\n   * @returns {String} connection\n   */\n  getConnection() {\n    return this.connection;\n  }\n\n  /**\n   * @returns {Number} port\n   */\n  getPort() {\n    return this.port;\n  }\n\n  /**\n   * @param {Number} timeout - ms\n   * @returns {Object || null} jayson client connection\n   */\n  getService(timeout) {\n    return this.startService(timeout);\n  }\n\n  /**\n   * @param {Number} timeout - ms\n   * @returns {Object} jayson client connection\n   */\n  startService(timeout) {\n    const hostname = this.getConnection();\n    const port = this.getPort();\n\n    return (hostname && !port) ?\n      jayson.client.http(hostname) :\n      jayson.client.http({ hostname, port, timeout });\n  }\n\n  /**\n   * @returns {Boolean}\n   */\n  isRunning() {\n    return this.running;\n  }\n\n  /**\n   * @returns {String} method name\n   */\n  getMethod() {\n    return this.method;\n  }\n\n  /**\n   * @returns {Void}\n   */\n  setRunning() {\n    this.running = true;\n  }\n\n  /**\n   * @returns {Void}\n   */\n  stop() {\n    this.running = false;\n  }\n\n  /**\n   * @param {Error} err - the error from the timeout\n   * @return {Void}\n   */\n  handleRequestTimeouts(err) {\n    if (this.log) {\n      this.log.error(err);\n    } else {\n      process.stderr.write(err);\n    }\n  }\n\n  /**\n   * @param {String} method - the rpc method running\n   * @param {Array} args - the arguments passed to request\n   * @returns {Void}\n   */\n  handleExceededTrys(method, args) {\n    const errorMessage = `rpc service error with method ${method}(${args})`;\n    if (this.log) {\n      this.log.error(errorMessage);\n    } else {\n      process.stderr.write(errorMessage);\n    }\n  }\n\n  /**\n     * initiates an rpc forever, logging error output along the way\n     * @param {Object} opts - args, trysBeforeError, timeout\n     * @param {String} method - name of remove method to call\n     * @param {Array<Object || Array || String || Number || null>} args - used by the remote server\n     * @returns {Promise} promise\n   */\n  run(opts) {\n    const { args, cb } = opts;\n\n    const timeout = opts.timeout || 20000;\n    const trysBeforeError = opts.trysBeforeError || 3;\n    const me = this;\n    const continuousService = this.getService(timeout);\n    const method = this.getMethod();\n\n    /**\n     * @param {Number} trys - the amount of times the rpc has been attempted\n     * @returns {Function} recursive {Promise}\n     */\n    function continuous(trys) {\n      let trysToUse = trys;\n      return new Promise((resolve, reject) => {\n\tif (trysToUse === trysBeforeError && me.isRunning()) {\n\t  me.handleExceededTrys(method, args);\n\t  trysToUse = 0;\n\t  return setTimeout(() => continuous(trysToUse).then(resolve).catch(reject), 5000);\n\t}\n\tcontinuousService.on('timeout', (err) => me.handleRequestTimeouts(err));\n\treturn continuousService.request(method, args, cb ? cb : (err, rpcErr, result) => {\n\t  if (err) {\n\t    trysToUse++;\n\t    return continuous(trysToUse).then(resolve).catch(reject);\n\t  } else if (rpcErr) {\n\t    return reject(rpcErr);\n\t  } else {\n\t    me.stop();\n\t    return resolve(result);\n\t  }\n\t});\n      });\n    }\n    this.setRunning();\n    return continuous(0);\n  }\n}\n\nmodule.exports = RPCSend;\n"]}