@grpc/proto-loader
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -20,2 +20,14 @@ /// <reference types="node" /> | ||
import * as Protobuf from 'protobufjs'; | ||
import * as descriptor from 'protobufjs/ext/descriptor'; | ||
declare module 'protobufjs' { | ||
interface Type { | ||
toDescriptor(protoVersion: string): Protobuf.Message<descriptor.IDescriptorProto> & descriptor.IDescriptorProto; | ||
} | ||
interface Root { | ||
toDescriptor(protoVersion: string): Protobuf.Message<descriptor.IFileDescriptorSet> & descriptor.IFileDescriptorSet; | ||
} | ||
interface Enum { | ||
toDescriptor(protoVersion: string): Protobuf.Message<descriptor.IEnumDescriptorProto> & descriptor.IEnumDescriptorProto; | ||
} | ||
} | ||
export interface Serialize<T> { | ||
@@ -27,2 +39,13 @@ (value: T): Buffer; | ||
} | ||
export interface ProtobufTypeDefinition { | ||
format: string; | ||
type: object; | ||
fileDescriptorProtos: Buffer[]; | ||
} | ||
export interface MessageTypeDefinition extends ProtobufTypeDefinition { | ||
format: 'Protocol Buffer 3 DescriptorProto'; | ||
} | ||
export interface EnumTypeDefinition extends ProtobufTypeDefinition { | ||
format: 'Protocol Buffer 3 EnumDescriptorProto'; | ||
} | ||
export interface MethodDefinition<RequestType, ResponseType> { | ||
@@ -37,2 +60,4 @@ path: string; | ||
originalName?: string; | ||
requestType: MessageTypeDefinition; | ||
responseType: MessageTypeDefinition; | ||
} | ||
@@ -42,4 +67,5 @@ export interface ServiceDefinition { | ||
} | ||
export declare type AnyDefinition = ServiceDefinition | MessageTypeDefinition | EnumTypeDefinition; | ||
export interface PackageDefinition { | ||
[index: string]: ServiceDefinition; | ||
[index: string]: AnyDefinition; | ||
} | ||
@@ -46,0 +72,0 @@ export declare type Options = Protobuf.IParseOptions & Protobuf.IConversionOptions & { |
@@ -21,5 +21,14 @@ "use strict"; | ||
var Protobuf = require("protobufjs"); | ||
var descriptor = require("protobufjs/ext/descriptor"); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var _ = require("lodash"); | ||
var camelCase = require("lodash.camelcase"); | ||
var descriptorOptions = { | ||
longs: String, | ||
enums: String, | ||
bytes: String, | ||
defaults: true, | ||
oneofs: true, | ||
json: true | ||
}; | ||
function joinName(baseName, name) { | ||
@@ -33,17 +42,21 @@ if (baseName === '') { | ||
} | ||
function getAllServices(obj, parentName) { | ||
function isHandledReflectionObject(obj) { | ||
return obj instanceof Protobuf.Service || obj instanceof Protobuf.Type || obj instanceof Protobuf.Enum; | ||
} | ||
function isNamespaceBase(obj) { | ||
return obj instanceof Protobuf.Namespace || obj instanceof Protobuf.Root; | ||
} | ||
function getAllHandledReflectionObjects(obj, parentName) { | ||
var objName = joinName(parentName, obj.name); | ||
if (obj.hasOwnProperty('methods')) { | ||
if (isHandledReflectionObject(obj)) { | ||
return [[objName, obj]]; | ||
} | ||
else { | ||
return obj.nestedArray.map(function (child) { | ||
if (child.hasOwnProperty('nested')) { | ||
return getAllServices(child, objName); | ||
} | ||
else { | ||
return []; | ||
} | ||
}).reduce(function (accumulator, currentValue) { return accumulator.concat(currentValue); }, []); | ||
if (isNamespaceBase(obj) && typeof obj.nested !== undefined) { | ||
return Object.keys(obj.nested).map(function (name) { | ||
return getAllHandledReflectionObjects(obj.nested[name], objName); | ||
}).reduce(function (accumulator, currentValue) { return accumulator.concat(currentValue); }, []); | ||
} | ||
} | ||
return []; | ||
} | ||
@@ -62,2 +75,6 @@ function createDeserializer(cls, options) { | ||
function createMethodDefinition(method, serviceName, options) { | ||
/* This is only ever called after the corresponding root.resolveAll(), so we | ||
* can assume that the resolved request and response types are non-null */ | ||
var requestType = method.resolvedRequestType; | ||
var responseType = method.resolvedResponseType; | ||
return { | ||
@@ -67,8 +84,10 @@ path: '/' + serviceName + '/' + method.name, | ||
responseStream: !!method.responseStream, | ||
requestSerialize: createSerializer(method.resolvedRequestType), | ||
requestDeserialize: createDeserializer(method.resolvedRequestType, options), | ||
responseSerialize: createSerializer(method.resolvedResponseType), | ||
responseDeserialize: createDeserializer(method.resolvedResponseType, options), | ||
requestSerialize: createSerializer(requestType), | ||
requestDeserialize: createDeserializer(requestType, options), | ||
responseSerialize: createSerializer(responseType), | ||
responseDeserialize: createDeserializer(responseType, options), | ||
// TODO(murgatroid99): Find a better way to handle this | ||
originalName: _.camelCase(method.name) | ||
originalName: camelCase(method.name), | ||
requestType: createMessageDefinition(requestType), | ||
responseType: createMessageDefinition(responseType) | ||
}; | ||
@@ -84,7 +103,55 @@ } | ||
} | ||
var fileDescriptorCache = new Map(); | ||
function getFileDescriptors(root) { | ||
if (fileDescriptorCache.has(root)) { | ||
return fileDescriptorCache.get(root); | ||
} | ||
else { | ||
var descriptorList = root.toDescriptor('proto3').file; | ||
var bufferList = descriptorList.map(function (value) { return Buffer.from(descriptor.FileDescriptorProto.encode(value).finish()); }); | ||
fileDescriptorCache.set(root, bufferList); | ||
return bufferList; | ||
} | ||
} | ||
function createMessageDefinition(message) { | ||
var messageDescriptor = message.toDescriptor('proto3'); | ||
return { | ||
format: 'Protocol Buffer 3 DescriptorProto', | ||
type: messageDescriptor.$type.toObject(messageDescriptor, descriptorOptions), | ||
fileDescriptorProtos: getFileDescriptors(message.root) | ||
}; | ||
} | ||
function createEnumDefinition(enumType) { | ||
var enumDescriptor = enumType.toDescriptor('proto3'); | ||
return { | ||
format: 'Protocol Buffer 3 EnumDescriptorProto', | ||
type: enumDescriptor.$type.toObject(enumDescriptor, descriptorOptions), | ||
fileDescriptorProtos: getFileDescriptors(enumType.root) | ||
}; | ||
} | ||
/** | ||
* function createDefinition(obj: Protobuf.Service, name: string, options: Options): ServiceDefinition; | ||
* function createDefinition(obj: Protobuf.Type, name: string, options: Options): MessageTypeDefinition; | ||
* function createDefinition(obj: Protobuf.Enum, name: string, options: Options): EnumTypeDefinition; | ||
*/ | ||
function createDefinition(obj, name, options) { | ||
if (obj instanceof Protobuf.Service) { | ||
return createServiceDefinition(obj, name, options); | ||
} | ||
else if (obj instanceof Protobuf.Type) { | ||
return createMessageDefinition(obj); | ||
} | ||
else if (obj instanceof Protobuf.Enum) { | ||
return createEnumDefinition(obj); | ||
} | ||
else { | ||
throw new Error('Type mismatch in reflection object handling'); | ||
} | ||
} | ||
function createPackageDefinition(root, options) { | ||
var def = {}; | ||
for (var _i = 0, _a = getAllServices(root, ''); _i < _a.length; _i++) { | ||
var _b = _a[_i], name = _b[0], service = _b[1]; | ||
def[name] = createServiceDefinition(service, name, options); | ||
root.resolveAll(); | ||
for (var _i = 0, _a = getAllHandledReflectionObjects(root, ''); _i < _a.length; _i++) { | ||
var _b = _a[_i], name = _b[0], obj = _b[1]; | ||
def[name] = createDefinition(obj, name, options); | ||
} | ||
@@ -94,3 +161,7 @@ return def; | ||
function addIncludePathResolver(root, includePaths) { | ||
var originalResolvePath = root.resolvePath; | ||
root.resolvePath = function (origin, target) { | ||
if (path.isAbsolute(target)) { | ||
return target; | ||
} | ||
for (var _i = 0, includePaths_1 = includePaths; _i < includePaths_1.length; _i++) { | ||
@@ -107,3 +178,3 @@ var directory = includePaths_1[_i]; | ||
} | ||
return null; | ||
return originalResolvePath(origin, target); | ||
}; | ||
@@ -110,0 +181,0 @@ } |
{ | ||
"name": "@grpc/proto-loader", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"author": "Google Inc.", | ||
@@ -41,8 +41,8 @@ "contributors": [ | ||
"dependencies": { | ||
"@types/lodash": "^4.14.104", | ||
"@types/node": "^9.4.6", | ||
"lodash": "^4.17.5", | ||
"lodash.camelcase": "^4.3.0", | ||
"protobufjs": "^6.8.6" | ||
}, | ||
"devDependencies": { | ||
"@types/lodash.camelcase": "^4.3.4", | ||
"@types/node": "^10.12.5", | ||
"clang-format": "^1.2.2", | ||
@@ -49,0 +49,0 @@ "gts": "^0.5.3", |
# gRPC Protobuf Loader | ||
A utility package for loading `.proto` files for use with gRPC, using the latest Protobuf.js package. | ||
Please refer to [protobuf.js' documentation](https://github.com/dcodeIO/protobuf.js/blob/master/README.md) | ||
to understands its features and limitations. | ||
@@ -5,0 +7,0 @@ ## Installation |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16380
2
323
54
5
+ Addedlodash.camelcase@^4.3.0
+ Addedlodash.camelcase@4.3.0(transitive)
- Removed@types/lodash@^4.14.104
- Removed@types/node@^9.4.6
- Removedlodash@^4.17.5
- Removed@types/lodash@4.17.13(transitive)
- Removed@types/node@9.6.61(transitive)
- Removedlodash@4.17.21(transitive)