# All files service.js

88.42% Statements 107/121
68.18% Branches 15/22
100% Functions 9/9
88.42% Lines 107/121

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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    1221x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    45x
    45x
    9x
    9x
    9x
    1x
    1x
    1x
    6x
    6x
    6x
    4x
    4x
    6x
    2x
    2x
    6x
    1x
    1x
    12x
    12x
    12x
    12x
    12x
    12x

    12x
    1x
    1x
    4x
    4x
    4x
    4x
    4x
    4x

    4x
    1x
    1x
    4x
    4x
    4x
    4x
    4x
    4x

    4x
    1x
    1x
    4x
    4x
    4x
    4x
    4x
    4x

    4x
    1x
    1x
    4x
    4x
    4x
    4x
    4x
    4x

    4x
    1x
    1x
    5x
    5x
    5x
    5x
    5x
    5x

    5x
    1x
    1x
    1x
    3x
    3x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    3x
    1x
    1x
    1x
     import makeDebug from 'debug'
    import errors from '@feathersjs/errors'
    import { DEFAULT_METHODS } from './utils.js'

    const { convert } = errors
    const debug = makeDebug('feathers-distributed:service')

    // This is the Feathers service abstraction for a cote requester on remote
    class RemoteService {
      constructor (app, options) {
        // Keep track of partition key
        this.key = options.key
        this.requester = app.serviceRequesters[options.key]
        this.path = options.path
        // This flag indicates to the plugin this is a remote service
        this.remote = true
        this.events = options.events
        this.methods = options.methods
        if (this.methods) {
          this.methods.forEach(method => {
          // If custom method add the corresponding function to service interface
            if (!DEFAULT_METHODS.includes(method)) this[method] = this.generateCustomMethod(method)
          })
        }
      }

      // Perform requests to other nodes
      async find (params) {
        debug('Requesting find() remote service on path ' + this.path + ' with key ' + this.key, params)
        try {
          const result = await this.requester.send({ type: 'find', key: this.key, path: this.path, params })
          debug('Successfully find() remote service on path ' + this.path + ' with key ' + this.key)
          return result
        } catch (error) {
          throw convert(error)
        }
      }

      async get (id, params) {
        debug('Requesting get() remote service on path ' + this.path + ' with key ' + this.key, id, params)
        try {
          const result = await this.requester.send({ type: 'get', key: this.key, path: this.path, id, params })
          debug('Successfully get() remote service on path ' + this.path + ' with key ' + this.key)
          return result
        } catch (error) {
          throw convert(error)
        }
      }

      async create (data, params) {
        debug('Requesting create() remote service on path ' + this.path + ' with key ' + this.key, data, params)
        try {
          const result = await this.requester.send({ type: 'create', key: this.key, path: this.path, data, params })
          debug('Successfully create() remote service on path ' + this.path + ' with key ' + this.key)
          return result
        } catch (error) {
          throw convert(error)
        }
      }

      async update (id, data, params) {
        debug('Requesting update() remote service on path ' + this.path + ' with key ' + this.key, id, data, params)
        try {
          const result = await this.requester.send({ type: 'update', key: this.key, path: this.path, id, data, params })
          debug('Successfully update() remote service on path ' + this.path + ' with key ' + this.key)
          return result
        } catch (error) {
          throw convert(error)
        }
      }

      async patch (id, data, params) {
        debug('Requesting patch() remote service on path ' + this.path + ' with key ' + this.key, id, data, params)
        try {
          const result = await this.requester.send({ type: 'patch', key: this.key, path: this.path, id, data, params })
          debug('Successfully patch() remote service on path ' + this.path + ' with key ' + this.key)
          return result
        } catch (error) {
          throw convert(error)
        }
      }

      async remove (id, params) {
        debug('Requesting remove() remote service on path ' + this.path + ' with key ' + this.key, id, params)
        try {
          const result = await this.requester.send({ type: 'remove', key: this.key, path: this.path, id, params })
          debug('Successfully remove() remote service on path ' + this.path + ' with key ' + this.key)
          return result
        } catch (error) {
          throw convert(error)
        }
      }

      async healthcheck () {
        debug('Requesting healthcheck() remote service on path ' + this.path + ' with key ' + this.key)
        try {
          const result = await this.requester.send({ type: 'healthcheck', key: this.key, path: this.path })
          debug('Successfully healthcheck() remote service on path ' + this.path + ' with key ' + this.key)
          return result
        } catch (error) {
          throw convert(error)
        }
      }

      // Custom method call
      generateCustomMethod (method) {
        debug(`Generating ${method}() remote service on path ` + this.path + ' with key ' + this.key)
        return async function (data, params) {
          debug(`Requesting ${method}() remote service on path ` + this.path + ' with key ' + this.key, data, params)
          try {
            const result = await this.requester.send({ type: method, key: this.key, path: this.path, data, params })
            debug(`Successfully ${method}() remote service on path ` + this.path + ' with key ' + this.key)
            return result
          } catch (error) {
            throw convert(error)
          }
        }
      }
    }

    export default RemoteService