@grpc/proto-loader
Advanced tools
Comparing version 0.5.3 to 0.5.4
@@ -20,8 +20,8 @@ "use strict"; | ||
*/ | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var Protobuf = require("protobufjs"); | ||
var descriptor = require("protobufjs/ext/descriptor"); | ||
var camelCase = require("lodash.camelcase"); | ||
var descriptorOptions = { | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const Protobuf = require("protobufjs"); | ||
const descriptor = require("protobufjs/ext/descriptor"); | ||
const camelCase = require("lodash.camelcase"); | ||
const descriptorOptions = { | ||
longs: String, | ||
@@ -32,3 +32,3 @@ enums: String, | ||
oneofs: true, | ||
json: true | ||
json: true, | ||
}; | ||
@@ -44,4 +44,5 @@ function joinName(baseName, name) { | ||
function isHandledReflectionObject(obj) { | ||
return obj instanceof Protobuf.Service || obj instanceof Protobuf.Type || | ||
obj instanceof Protobuf.Enum; | ||
return (obj instanceof Protobuf.Service || | ||
obj instanceof Protobuf.Type || | ||
obj instanceof Protobuf.Enum); | ||
} | ||
@@ -52,3 +53,3 @@ function isNamespaceBase(obj) { | ||
function getAllHandledReflectionObjects(obj, parentName) { | ||
var objName = joinName(parentName, obj.name); | ||
const objName = joinName(parentName, obj.name); | ||
if (isHandledReflectionObject(obj)) { | ||
@@ -60,6 +61,6 @@ return [[objName, obj]]; | ||
return Object.keys(obj.nested) | ||
.map(function (name) { | ||
.map(name => { | ||
return getAllHandledReflectionObjects(obj.nested[name], objName); | ||
}) | ||
.reduce(function (accumulator, currentValue) { return accumulator.concat(currentValue); }, []); | ||
.reduce((accumulator, currentValue) => accumulator.concat(currentValue), []); | ||
} | ||
@@ -76,3 +77,3 @@ } | ||
return function serialize(arg) { | ||
var message = cls.fromObject(arg); | ||
const message = cls.fromObject(arg); | ||
return cls.encode(message).finish(); | ||
@@ -84,4 +85,4 @@ }; | ||
* can assume that the resolved request and response types are non-null */ | ||
var requestType = method.resolvedRequestType; | ||
var responseType = method.resolvedResponseType; | ||
const requestType = method.resolvedRequestType; | ||
const responseType = method.resolvedResponseType; | ||
return { | ||
@@ -98,11 +99,9 @@ path: '/' + serviceName + '/' + method.name, | ||
requestType: createMessageDefinition(requestType, fileDescriptors), | ||
responseType: createMessageDefinition(responseType, fileDescriptors) | ||
responseType: createMessageDefinition(responseType, fileDescriptors), | ||
}; | ||
} | ||
function createServiceDefinition(service, name, options, fileDescriptors) { | ||
var def = {}; | ||
for (var _i = 0, _a = service.methodsArray; _i < _a.length; _i++) { | ||
var method = _a[_i]; | ||
def[method.name] = | ||
createMethodDefinition(method, name, options, fileDescriptors); | ||
const def = {}; | ||
for (const method of service.methodsArray) { | ||
def[method.name] = createMethodDefinition(method, name, options, fileDescriptors); | ||
} | ||
@@ -112,15 +111,15 @@ return def; | ||
function createMessageDefinition(message, fileDescriptors) { | ||
var messageDescriptor = message.toDescriptor('proto3'); | ||
const messageDescriptor = message.toDescriptor('proto3'); | ||
return { | ||
format: 'Protocol Buffer 3 DescriptorProto', | ||
type: messageDescriptor.$type.toObject(messageDescriptor, descriptorOptions), | ||
fileDescriptorProtos: fileDescriptors | ||
fileDescriptorProtos: fileDescriptors, | ||
}; | ||
} | ||
function createEnumDefinition(enumType, fileDescriptors) { | ||
var enumDescriptor = enumType.toDescriptor('proto3'); | ||
const enumDescriptor = enumType.toDescriptor('proto3'); | ||
return { | ||
format: 'Protocol Buffer 3 EnumDescriptorProto', | ||
type: enumDescriptor.$type.toObject(enumDescriptor, descriptorOptions), | ||
fileDescriptorProtos: fileDescriptors | ||
fileDescriptorProtos: fileDescriptors, | ||
}; | ||
@@ -150,10 +149,7 @@ } | ||
function createPackageDefinition(root, options) { | ||
var def = {}; | ||
const def = {}; | ||
root.resolveAll(); | ||
var descriptorList = root.toDescriptor('proto3').file; | ||
var bufferList = descriptorList.map(function (value) { | ||
return Buffer.from(descriptor.FileDescriptorProto.encode(value).finish()); | ||
}); | ||
for (var _i = 0, _a = getAllHandledReflectionObjects(root, ''); _i < _a.length; _i++) { | ||
var _b = _a[_i], name = _b[0], obj = _b[1]; | ||
const descriptorList = root.toDescriptor('proto3').file; | ||
const bufferList = descriptorList.map(value => Buffer.from(descriptor.FileDescriptorProto.encode(value).finish())); | ||
for (const [name, obj] of getAllHandledReflectionObjects(root, '')) { | ||
def[name] = createDefinition(obj, name, options, bufferList); | ||
@@ -164,10 +160,9 @@ } | ||
function addIncludePathResolver(root, includePaths) { | ||
var originalResolvePath = root.resolvePath; | ||
root.resolvePath = function (origin, target) { | ||
const originalResolvePath = root.resolvePath; | ||
root.resolvePath = (origin, target) => { | ||
if (path.isAbsolute(target)) { | ||
return target; | ||
} | ||
for (var _i = 0, includePaths_1 = includePaths; _i < includePaths_1.length; _i++) { | ||
var directory = includePaths_1[_i]; | ||
var fullPath = path.join(directory, target); | ||
for (const directory of includePaths) { | ||
const fullPath = path.join(directory, target); | ||
try { | ||
@@ -181,2 +176,3 @@ fs.accessSync(fullPath, fs.constants.R_OK); | ||
} | ||
process.emitWarning(`${target} not found in any of the include paths ${includePaths}`); | ||
return originalResolvePath(origin, target); | ||
@@ -210,6 +206,6 @@ }; | ||
function load(filename, options) { | ||
var root = new Protobuf.Root(); | ||
const root = new Protobuf.Root(); | ||
options = options || {}; | ||
if (!!options.includeDirs) { | ||
if (!(Array.isArray(options.includeDirs))) { | ||
if (!Array.isArray(options.includeDirs)) { | ||
return Promise.reject(new Error('The includeDirs option must be an array')); | ||
@@ -219,3 +215,3 @@ } | ||
} | ||
return root.load(filename, options).then(function (loadedRoot) { | ||
return root.load(filename, options).then(loadedRoot => { | ||
loadedRoot.resolveAll(); | ||
@@ -227,6 +223,6 @@ return createPackageDefinition(root, options); | ||
function loadSync(filename, options) { | ||
var root = new Protobuf.Root(); | ||
const root = new Protobuf.Root(); | ||
options = options || {}; | ||
if (!!options.includeDirs) { | ||
if (!(Array.isArray(options.includeDirs))) { | ||
if (!Array.isArray(options.includeDirs)) { | ||
throw new Error('The includeDirs option must be an array'); | ||
@@ -236,3 +232,3 @@ } | ||
} | ||
var loadedRoot = root.loadSync(filename, options); | ||
const loadedRoot = root.loadSync(filename, options); | ||
loadedRoot.resolveAll(); | ||
@@ -246,12 +242,10 @@ return createPackageDefinition(root, options); | ||
// and wrappers. compiler/plugin is excluded in Protobuf.js and here. | ||
var wellKnownProtos = ['api', 'descriptor', 'source_context', 'type']; | ||
var sourceDir = path.join(path.dirname(require.resolve('protobufjs')), 'google', 'protobuf'); | ||
for (var _i = 0, wellKnownProtos_1 = wellKnownProtos; _i < wellKnownProtos_1.length; _i++) { | ||
var proto = wellKnownProtos_1[_i]; | ||
var file = path.join(sourceDir, proto + ".proto"); | ||
var descriptor_1 = Protobuf.loadSync(file).toJSON(); | ||
// @ts-ignore | ||
Protobuf.common(proto, descriptor_1.nested.google.nested); | ||
const wellKnownProtos = ['api', 'descriptor', 'source_context', 'type']; | ||
const sourceDir = path.join(path.dirname(require.resolve('protobufjs')), 'google', 'protobuf'); | ||
for (const proto of wellKnownProtos) { | ||
const file = path.join(sourceDir, `${proto}.proto`); | ||
const descriptor = Protobuf.loadSync(file).toJSON(); | ||
Protobuf.common(proto, descriptor.nested.google.nested); | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@grpc/proto-loader", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"author": "Google Inc.", | ||
@@ -49,3 +49,3 @@ "contributors": [ | ||
"clang-format": "^1.2.2", | ||
"gts": "^0.5.3", | ||
"gts": "^1.1.0", | ||
"typescript": "~3.3.3333" | ||
@@ -52,0 +52,0 @@ }, |
27655
312