All files request.ts

73.33% Statements 22/30
76.47% Branches 13/17
62.5% Functions 5/8
73.33% Lines 22/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72    1x 1x                         20x 76x 76x 76x 76x   76x                                                 1x 56x 75x 22x     34x       11x 1x 10x 3x 7x 1x   6x   11x       1x  
"use strict"
 
import { EventEmitter } from "events"
import { utils } from "@swtc/utils"
 
/**
 * request server and account info without secret
 * @param remote
 * @param command
 * @constructor
 */
class Request extends EventEmitter {
  public message
  private _remote
  private _command
  private _filter
  constructor(remote, command = null, filter = v => v) {
    super()
    this._remote = remote
    this._command = command
    this._filter = filter
    // directly modify message is supported
    this.message = {}
  }
 
  public async submitPromise() {
    return new Promise((resolve, reject) => {
      for (const key in this.message) {
        if (this.message[key] instanceof Error) {
          reject(this.message[key].message)
        }
      }
      this._remote._submit(
        this._command,
        this.message,
        this._filter,
        (error, result) => {
          if (error) {
            reject(error)
          } else {
            resolve(result)
          }
        }
      )
    })
  }
 
  public submit(callback = m => m) {
    for (const key in this.message) {
      if (this.message[key] instanceof Error) {
        return callback(this.message[key].message)
      }
    }
    this._remote._submit(this._command, this.message, this._filter, callback)
  }
 
  public selectLedger(ledger) {
    if (typeof ledger === "string" && ~utils.LEDGER_STATES.indexOf(ledger)) {
      this.message.ledger_index = ledger
    } else if (Number(ledger)) {
      this.message.ledger_index = Number(ledger)
    } else if (/^[A-F0-9]+$/.test(ledger)) {
      this.message.ledger_hash = ledger
    } else {
      this.message.ledger_index = "validated"
    }
    return this
  }
}
 
export { Request }