Skip to content

Commit

Permalink
Merge f48435b into 6b797e3
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Jul 24, 2018
2 parents 6b797e3 + f48435b commit 517b877
Show file tree
Hide file tree
Showing 11 changed files with 2,251 additions and 2,544 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "10"
- "8"
- "8.0"
- "6.9"
Expand Down
44 changes: 37 additions & 7 deletions lib/app.js
Expand Up @@ -5,6 +5,7 @@ const compose = require('mali-compose')
const grpc = require('grpc')
const gi = require('grpc-inspect')
const pMap = require('p-map')
const pl = require('@grpc/proto-loader')

const _ = require('./lo')
const Context = require('./context')
Expand All @@ -13,7 +14,16 @@ const mu = require('./utils')
const Request = require('./request')
const Response = require('./response')

const REMOVE_PROPS = ['grpc', 'middleware', 'handlers', 'servers', 'load', 'proto', 'services', 'methods']
const REMOVE_PROPS = [
'grpc',
'middleware',
'handlers',
'servers',
'load',
'proto',
'services',
'methods'
]
const EE_PROPS = Object.getOwnPropertyNames(new Emitter())

/**
Expand Down Expand Up @@ -75,7 +85,22 @@ class Mali extends Emitter {
*/
addService (path, name, options) {
const load = _.isString(path) || (_.isObject(path) && path.root && path.file)
const proto = load ? this.grpc.load(path, undefined, options) : path

let proto = path
if (load) {
let protoFilePath = path
let loadOptions = _.assign({}, options)

if (_.isObject(path) && path.root && path.file) {
protoFilePath = path.file
if (!loadOptions.includeDirs) {
loadOptions.includeDirs = [path.root]
}
}

const pd = pl.loadSync(protoFilePath, loadOptions)
proto = grpc.loadPackageDefinition(pd)
}

if (load || !mu.isStaticGRPCObject(proto)) {
let descriptor = gi(proto)
Expand All @@ -85,11 +110,10 @@ class Mali extends Emitter {
} else if (_.isArray(name)) {
names = _.intersection(name, names)
}

names.forEach(n => {
const client = descriptor.client(n)
const service = client && client.service
? client.service
: client
const service = client && client.service ? client.service : client
if (service) {
this.services[n] = service
this.middleware[n] = []
Expand Down Expand Up @@ -188,8 +212,8 @@ class Mali extends Emitter {
let serviceName
_.forOwn(this.services, (sd, sn) => {
if (!serviceName) {
const serviceFunctions = _.keys(sd)
if (serviceFunctions.indexOf(mwName) >= 0) {
const serviceFunctions = _.keys(sd).map(f => f.toLowerCase())
if (serviceFunctions.indexOf(mwName.toLowerCase()) >= 0) {
serviceName = sn
}
}
Expand Down Expand Up @@ -221,6 +245,7 @@ class Mali extends Emitter {
}
} else {
let serviceName = service

if (!_.isString(name)) {
fns.unshift(name)
const sNames = _.keys(this.services)
Expand All @@ -232,18 +257,23 @@ class Mali extends Emitter {
serviceName = _.keys(this.services)[0]
}
}

if (!this.services[serviceName]) {
throw new Error(String.raw`Unknown service ${serviceName}`)
}

if (!this.handlers[serviceName]) {
this.handlers[serviceName] = {}
}

if (this.handlers[serviceName][name]) {
throw new Error(String.raw`Handler for ${name} already defined for service ${serviceName}`)
}

if (!this.methods[serviceName][name]) {
throw new Error(String.raw`Unknown method: ${name} for service ${serviceName}`)
}

this.handlers[serviceName][name] = _.concat(this.middleware[serviceName], fns)
}
}
Expand Down

0 comments on commit 517b877

Please sign in to comment.