-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
47 lines (38 loc) · 1.18 KB
/
loader.js
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
'use strict';
const path = require('path');
const fs = require('fs');
module.exports = (Grown, util) => {
return Grown('GRPC.Loader', {
scan(file, options) {
/* istanbul ignore else */
if (!fs.existsSync(file)) {
throw new Error(`Unable to load protobuf, given '${file}'`);
}
const protoLoader = require('@grpc/proto-loader');
const grpc = require('@grpc/grpc-js');
const protoOptions = {
longs: String,
enums: String,
defaults: true,
oneofs: true,
...options,
};
const packageDefinition = protoLoader.loadSync(file, protoOptions);
const Proto = grpc.loadPackageDefinition(packageDefinition);
const name = path.basename(file);
const map = {};
const ns = this.proto_namespace || 'API';
/* istanbul ignore else */
if (!Proto[ns]) {
throw new Error(`${name}::${ns} package not found`);
}
Object.keys(Proto[ns]).forEach(key => {
/* istanbul ignore else */
if (Proto[ns][key].service && !Proto[ns][key].service.type) {
util.setProp(map, `${ns}.${key}`, Proto[ns][key]);
}
});
return map;
},
});
};