Server : nginx/1.20.1 System : Linux ccpf-production-2021 5.4.0-148-generic #165-Ubuntu SMP Tue Apr 18 08:53:12 UTC 2023 x86_64 User : forge ( 1000) PHP Version : 7.4.21 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /lib/node_modules/pm2/node_modules/@pm2/js-api/src/ |
'use strict'
const Endpoint = require('./endpoint')
const logger = require('debug')('kmjs:namespace')
module.exports = class Namespace {
constructor (mapping, opts) {
logger(`initialization namespace ${opts.name}`)
this.name = opts.name
this.http = opts.http
this.endpoints = []
this.namespaces = []
logger(`building namespace ${opts.name}`)
for (let name in mapping) {
let child = mapping[name]
if (typeof mapping === 'object' && !child.route) {
// ignore the 'default' namespace that should bind to the parent namespace
if (name === 'default') {
// create the namespace
const defaultNamespace = new Namespace(child, { name, http: this.http, services: opts.services })
this.namespaces.push(defaultNamespace)
// bind property of the default namespace to this namespace
for (let key in defaultNamespace) {
if (key === 'name' || key === 'opts') continue
this[key] = defaultNamespace[key]
}
continue
}
// if the parent namespace is a object, the child are namespace too
this.addNamespace(new Namespace(child, { name, http: this.http, services: opts.services }))
} else {
// otherwise its an endpoint
if (child.service && opts.services && opts.services[child.service.name]) {
child.service.baseURL = opts.services[child.service.name]
}
this.addEndpoint(new Endpoint(child))
}
}
// logging namespaces
if (this.namespaces.length > 0) {
logger(`namespace ${this.name} contains namespaces : \n${this.namespaces.map(namespace => namespace.name).join('\n')}\n`)
}
// logging endpoints
if (this.endpoints.length > 0) {
logger(`Namespace ${this.name} contains endpoints : \n${this.endpoints.map(endpoint => endpoint.route.name).join('\n')}\n`)
}
}
addNamespace (namespace) {
if (!namespace || namespace.name === this.name) {
throw new Error(`A namespace must not have the same name as the parent namespace`)
}
if (!(namespace instanceof Namespace)) {
throw new Error(`addNamespace only accept Namespace instance`)
}
this.namespaces.push(namespace)
this[namespace.name] = namespace
}
addEndpoint (endpoint) {
if (!endpoint || endpoint.name === this.name) {
throw new Error(`A endpoint must not have the same name as a namespace`)
}
if (!(endpoint instanceof Endpoint)) {
throw new Error(`addNamespace only accept Namespace instance`)
}
this.endpoints.push(endpoint)
this[endpoint.name] = endpoint.build(this.http)
}
}