# All files publish.js

100% Statements 71/71
89.47% Branches 17/19
100% Functions 2/2
100% Lines 71/71

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
    721x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    24x
    24x
    24x
    24x
    24x
    9x
    9x
    9x
    9x
    15x
    15x
    15x
    15x
    15x
    24x
    24x
    24x
    24x
    24x
    12x
    12x
    12x
    12x
    3x
    24x
    2x
    6x
    6x
    26x
    26x
    26x
    26x
    26x
    26x
    26x
    6x
    6x
    6x
    2x
    2x
    2x
    2x
    24x
    24x
    24x
    24x
    24x
    1x
    1x
    8x
    8x
    4x
    4x
    14x
    4x
    4x
    4x
    4x
    4x
    8x
     import { stripSlashes } from '@feathersjs/commons'
    import feathers from '@feathersjs/feathers'
    import makeDebug from 'debug'
    import { DEFAULT_EVENTS, isInternalService } from './utils.js'

    const { getServiceOptions } = feathers
    const debug = makeDebug('feathers-distributed:publish')
    // Get the unique global symbol to store event listeners on a service object
    const EVENT_LISTENER_KEY = Symbol.for('event-listener')

    export function publishService (app, path) {
      // App not yet initialized, publishing will occur again once done
      if (!app.servicePublisher) return
      const service = app.service(path)
      if (!service || (typeof service !== 'object')) return
      if (service.remote) {
        debug('Ignoring remote service publication on path ' + path + ' for app with uuid ' +
              app.uuid + ' and key ' + app.distributionKey)
        return
      }
      const options = Object.assign(getServiceOptions(service), service.options)
      const serviceDescriptor = {
        uuid: app.uuid,
        key: app.distributionKey,
        path: stripSlashes(path),
        events: options.distributedEvents || options.events.concat(DEFAULT_EVENTS),
        methods: options.distributedMethods || options.methods // Default methods already included here unlike events
      }
      // Skip internal services
      if (isInternalService(app, serviceDescriptor)) {
        debug('Ignoring local service on path ' + serviceDescriptor.path + ' for app with uuid ' +
              app.uuid + ' and key ' + app.distributionKey)
        return
      }
      // Setup event listeners whenever required and if not already done
      if (app.distributionOptions.publishEvents && serviceDescriptor.events.length && !service[EVENT_LISTENER_KEY]) {
        serviceDescriptor.events.forEach(event => {
          // Publish events whenever required
          service.on(event, object => {
            if (app.serviceEventsPublisher) {
              debug(`Publishing ${event} local service event on path ` + serviceDescriptor.path +
                    ' for app with uuid ' + app.uuid + ' and key ' + app.distributionKey, object)
              app.serviceEventsPublisher.publish(event, Object.assign({
                path: serviceDescriptor.path, key: app.distributionKey
              }, object))
            }
          })
          // Tag service so that we will not install listeners twice
          service[EVENT_LISTENER_KEY] = true
        })
        debug('Publish callbacks registered for local service events on path ' + serviceDescriptor.path +
              ' for app with uuid ' + app.uuid + ' and key ' + app.distributionKey, serviceDescriptor.events)
      }
      // Publish new local service
      app.servicePublisher.publish('service', serviceDescriptor)
      debug('Published local service on path ' + serviceDescriptor.path + ' for app with uuid ' +
            app.uuid + ' and key ' + app.distributionKey, serviceDescriptor)
    }

    export function publishServices (app) {
      // Add a timeout so that the publisher/subscriber has been initialized on the node
      if (app.applicationPublicationTimeout) return
      app.applicationPublicationTimeout = setTimeout(_ => {
        Object.getOwnPropertyNames(app.services).forEach(path => {
          publishService(app, path)
        })
        // Reset timeout so that next queued publication will be scheduled
        app.applicationPublicationTimeout = null
      }, app.distributionOptions.publicationDelay)
      debug('Scheduled local services publishing for app with uuid ' + app.uuid + ' and key ' + app.distributionKey)
    }