All files / server/common proto.js

100% Statements 27/27
60% Branches 3/5
100% Functions 8/8
100% Lines 24/24

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    2x 2x 2x 2x   2x 2x 2x 2x     2x 2x   2x 2x 4x 4x 4x 4x     4x     2x 2x 2x   2x 2x 2x 2x    
'use strict'
 
const grpc = require('grpc')
const path = require('path')
const protobufjs = require('protobufjs')
const recursive = require('recursive-readdir')
 
exports.load = async (filenames, paths = ['protos']) => {
  let allprotos = {}
  Eif (typeof filenames === 'string') {
    filenames = [filenames]
  }
 
  const protosdirs = paths.map(
    protopath => path.normalize(path.join(__dirname, '..', '..', '..', protopath))
  )
  const root = new protobufjs.Root()
  root.resolvePath = (origin, target) => {
    let foundValue = ''
    Object.keys(allprotos).forEach(base => {
      allprotos[base].forEach(filename => {
        if (filename === target) foundValue = path.join(base, filename)
      })
    })
    return foundValue
  }
 
  const results = await Promise.all(protosdirs.map(path => recursive(path)))
  protosdirs.map((key, index) => {
    allprotos[key] = results[index].map(fullpath => path.relative(key, fullpath))
  })
  const proto = await protobufjs.load(filenames, root)
  const ret = grpc.loadObject(proto, { protobufjsVersion: 6 })
  ret.rootProto = proto
  return ret
}