| 6 | const Gateway = require('functionscript').Daemon.Gateway; |
| 7 | |
| 8 | class LocalGateway extends Gateway { |
| 9 | |
| 10 | constructor (cfg) { |
| 11 | cfg = cfg || {}; |
| 12 | cfg.name = 'LocalGateway'; |
| 13 | cfg.defaultTimeout = 120000; |
| 14 | super(cfg); |
| 15 | this._maxResultLogLength = 128; |
| 16 | } |
| 17 | |
| 18 | formatName (name) { |
| 19 | return chalk.grey(`[${chalk.green(this.name)}]`); |
| 20 | } |
| 21 | |
| 22 | formatRequest (req) { |
| 23 | return chalk.grey(`(${chalk.yellow(req ? (req._background ? chalk.bold('bg:') : '') + req._uuid.split('-')[0] : 'GLOBAL')}) ${this.routename(req)}`); |
| 24 | } |
| 25 | |
| 26 | formatMessage (message, logType) { |
| 27 | let color = {result: 'cyan', error: 'red'}[logType] || 'grey'; |
| 28 | return chalk[color](super.formatMessage(message, logType)); |
| 29 | } |
| 30 | |
| 31 | service (serviceName) { |
| 32 | this.serviceName = serviceName.replace(/^\//gi, ''); |
| 33 | } |
| 34 | |
| 35 | environment (env) { |
| 36 | Object.keys(env).forEach(key => process.env[key] = env[key]); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | listen (port, callback, opts) { |
| 41 | port = port || this.port; |
| 42 | process.env.STDLIB_LOCAL_PORT = port; |
| 43 | super.listen(port, callback, opts); |
| 44 | } |
| 45 | |
| 46 | createContext (req, definitions, params, data, buffer) { |
| 47 | let context = super.createContext(req, definitions, params, data, buffer); |
| 48 | context.service = {}; |
| 49 | context.service.name = this.serviceName; |
| 50 | context.service.path = this.serviceName.split('/'); |
| 51 | context.service.version = null; |
| 52 | context.service.environment = 'local'; |
| 53 | context.service.identifier = `${context.service.path.join('.')}[@${context.service.version || context.service.environment}]`; |
| 54 | context.service.uuid = '000000'; |
| 55 | context.service.hash = '000000'; |
| 56 | context.providers = context.providers || {}; |
| 57 | return context; |
| 58 | } |
| 59 | |
| 60 | resolve (req, res, buffer, callback) { |
| 61 | let urlinfo = url.parse(req.url, true); |
| 62 | let pathname = urlinfo.pathname; |
| 63 | if (this.serviceName && pathname.indexOf(this.serviceName) !== 1) { |
| 64 | let e = new Error(`Local Service Not Loaded: ${pathname}`); |
| 65 | e.statusCode = 404; |
nothing calls this directly
no outgoing calls
no test coverage detected