# All files register.js

87.12% Statements 88/101
73.33% Branches 11/15
100% Functions 2/2
87.12% Lines 88/101

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
    1021x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    12x
    12x
    3x
    3x
    3x
    9x
    9x
    9x
    12x
    3x
    3x
    3x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    30x
    78x
    78x
    78x
    78x
    78x
    30x
    12x
    12x
    12x
    12x
    1x
    1x
    12x
    12x
    3x
    3x
    3x
    3x
    9x
    9x
    9x
    9x
    12x

    12x
    12x

    12x
    12x
    12x
    12x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    12x
     import { promisify } from 'util'
    import makeDebug from 'debug'
    import { DEFAULT_EVENTS, isDiscoveredService, getServicePath, getService } from './utils.js'
    import RemoteService from './service.js'

    const debug = makeDebug('feathers-distributed:register')

    export async function registerApplication (app, applicationDescriptor) {
      // Create the request/events manager for remote services only
      if (applicationDescriptor.uuid === app.uuid) {
        debug('Ignoring service requester/events publisher creation for local app with uuid ' + app.uuid)
        return
      }

      const key = applicationDescriptor.key
      // Already registered
      if (app.serviceRequesters[key]) {
        debug('Ignoring already registered remote app with uuid ' + app.uuid + ' and key ' + key)
        return
      }
      debug('Registering remote app with uuid ' + app.uuid + ' and key ' + key)
      // Create the request manager to remote services
      app.serviceRequesters[key] = new app.cote.Requester({
        name: 'feathers services requester',
        namespace: key,
        key,
        requests: ['find', 'get', 'create', 'update', 'patch', 'remove', 'healthcheck'],
        timeout: app.distributionOptions.timeout || 20000
      }, app.coteOptions)
      debug('Service requester ready for remote app with uuid ' + applicationDescriptor.uuid + ' and key ' + key +
            ' for app with uuid ' + app.uuid + ' and key ' + app.distributionKey)
      // Wait before instanciating new component to avoid too much concurrency on port allocation
      await promisify(setTimeout)(app.distributionOptions.componentDelay)
      // Subscriber to listen to events from other nodes
      const events = app.distributionOptions.distributedEvents || DEFAULT_EVENTS
      app.serviceEventsSubscribers[key] = new app.cote.Subscriber({
        name: 'feathers services events subscriber',
        namespace: key,
        key,
        subscribesTo: events
      }, app.coteOptions)
      events.forEach(event => {
        app.serviceEventsSubscribers[key].on(event, object => {
          debug(`Dispatching ${event} remote service event on path ` + object.path, object)
          const servicePath = getServicePath(app, object)
          const service = getService(app, servicePath)
          // Ensure we don't have any local service with the same name to avoid infinite looping
          if (service && service.remote) service.emit(event, object)
        })
      })
      debug('Service events subscriber ready for remote app with uuid ' + applicationDescriptor.uuid + ' and key ' + key +
            ' for app with uuid ' + app.uuid + ' and key ' + app.distributionKey)
    }

    export function registerService (app, serviceDescriptor) {
      // Do not register our own services
      if (serviceDescriptor.uuid === app.uuid) {
        debug('Ignoring local service registration on path ' + serviceDescriptor.path + ' for app with uuid ' +
              app.uuid + ' and key ' + app.distributionKey)
        return
      }
      // Retrieve target service path as it could be different from remote one in case of alias
      const servicePath = getServicePath(app, serviceDescriptor)
      const service = getService(app, servicePath)

      if (service) {
        if (service instanceof RemoteService) {
          debug('Already registered service as remote on path ' + servicePath + ' for app with uuid ' +
                app.uuid + ' and key ' + app.distributionKey)
        } else {
          debug('Already registered local service on path ' + servicePath + ' for app with uuid ' +
                app.uuid + ' and key ' + app.distributionKey)
        }
        return
      }
      // Skip services we are not interested into
      if (!isDiscoveredService(app, serviceDescriptor)) {
        debug('Ignoring remote service on path ' + serviceDescriptor.path + ' for app with uuid ' +
              app.uuid + ' and key ' + app.distributionKey)
        return
      }

      // Initialize our service by providing any required middleware
      let args = [servicePath]
      if (app.distributionOptions.middlewares.before) args = args.concat(app.distributionOptions.middlewares.before)
      args.push(new RemoteService(app, serviceDescriptor), serviceDescriptor)
      if (app.distributionOptions.middlewares.after) args = args.concat(app.distributionOptions.middlewares.after)
      app.use(...args)
      debug('Registered remote service on path ' + servicePath + ' for app with uuid ' +
            app.uuid + ' and key ' + app.distributionKey, serviceDescriptor)

      // registering hook object on every remote service
      if (app.distributionOptions.hooks) {
        app.service(servicePath).hooks(app.distributionOptions.hooks)
      }
      debug('Registered hooks on remote service on path ' + servicePath + ' for app with uuid ' +
            app.uuid + ' and key ' + app.distributionKey)

      // Dispatch an event internally through node so that async processes can run
      app.emit('service', Object.assign({}, serviceDescriptor, { path: servicePath }))
    }