@ln-zap/proto-loader
Advanced tools
Comparing version 0.5.1 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)) { | ||
@@ -58,8 +59,8 @@ return [[objName, obj]]; | ||
else { | ||
if (isNamespaceBase(obj) && typeof obj.nested !== undefined) { | ||
if (isNamespaceBase(obj) && typeof obj.nested !== 'undefined') { | ||
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,11 +77,11 @@ } | ||
return function serialize(arg) { | ||
var message = cls.fromObject(arg); | ||
const message = cls.fromObject(arg); | ||
return cls.encode(message).finish(); | ||
}; | ||
} | ||
function createMethodDefinition(method, serviceName, options) { | ||
function createMethodDefinition(method, serviceName, options, fileDescriptors) { | ||
/* 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; | ||
const requestType = method.resolvedRequestType; | ||
const responseType = method.resolvedResponseType; | ||
return { | ||
@@ -96,42 +97,27 @@ path: '/' + serviceName + '/' + method.name, | ||
originalName: camelCase(method.name), | ||
requestType: createMessageDefinition(requestType), | ||
responseType: createMessageDefinition(responseType) | ||
requestType: createMessageDefinition(requestType, fileDescriptors), | ||
responseType: createMessageDefinition(responseType, fileDescriptors), | ||
}; | ||
} | ||
function createServiceDefinition(service, name, options) { | ||
var def = {}; | ||
for (var _i = 0, _a = service.methodsArray; _i < _a.length; _i++) { | ||
var method = _a[_i]; | ||
def[method.name] = createMethodDefinition(method, name, options); | ||
function createServiceDefinition(service, name, options, fileDescriptors) { | ||
const def = {}; | ||
for (const method of service.methodsArray) { | ||
def[method.name] = createMethodDefinition(method, name, options, fileDescriptors); | ||
} | ||
return def; | ||
} | ||
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'); | ||
function createMessageDefinition(message, fileDescriptors) { | ||
const messageDescriptor = message.toDescriptor('proto3'); | ||
return { | ||
format: 'Protocol Buffer 3 DescriptorProto', | ||
type: messageDescriptor.$type.toObject(messageDescriptor, descriptorOptions), | ||
fileDescriptorProtos: getFileDescriptors(message.root) | ||
fileDescriptorProtos: fileDescriptors, | ||
}; | ||
} | ||
function createEnumDefinition(enumType) { | ||
var enumDescriptor = enumType.toDescriptor('proto3'); | ||
function createEnumDefinition(enumType, fileDescriptors) { | ||
const enumDescriptor = enumType.toDescriptor('proto3'); | ||
return { | ||
format: 'Protocol Buffer 3 EnumDescriptorProto', | ||
type: enumDescriptor.$type.toObject(enumDescriptor, descriptorOptions), | ||
fileDescriptorProtos: getFileDescriptors(enumType.root) | ||
fileDescriptorProtos: fileDescriptors, | ||
}; | ||
@@ -146,11 +132,11 @@ } | ||
*/ | ||
function createDefinition(obj, name, options) { | ||
function createDefinition(obj, name, options, fileDescriptors) { | ||
if (obj instanceof Protobuf.Service) { | ||
return createServiceDefinition(obj, name, options); | ||
return createServiceDefinition(obj, name, options, fileDescriptors); | ||
} | ||
else if (obj instanceof Protobuf.Type) { | ||
return createMessageDefinition(obj); | ||
return createMessageDefinition(obj, fileDescriptors); | ||
} | ||
else if (obj instanceof Protobuf.Enum) { | ||
return createEnumDefinition(obj); | ||
return createEnumDefinition(obj, fileDescriptors); | ||
} | ||
@@ -162,7 +148,8 @@ else { | ||
function createPackageDefinition(root, options) { | ||
var def = {}; | ||
const def = {}; | ||
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); | ||
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); | ||
} | ||
@@ -172,10 +159,9 @@ return def; | ||
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 { | ||
@@ -189,2 +175,3 @@ fs.accessSync(fullPath, fs.constants.R_OK); | ||
} | ||
process.emitWarning(`${target} not found in any of the include paths ${includePaths}`); | ||
return originalResolvePath(origin, target); | ||
@@ -218,6 +205,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')); | ||
@@ -227,3 +214,3 @@ } | ||
} | ||
return root.load(filename, options).then(function (loadedRoot) { | ||
return root.load(filename, options).then(loadedRoot => { | ||
loadedRoot.resolveAll(); | ||
@@ -235,6 +222,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'); | ||
@@ -244,3 +231,3 @@ } | ||
} | ||
var loadedRoot = root.loadSync(filename, options); | ||
const loadedRoot = root.loadSync(filename, options); | ||
loadedRoot.resolveAll(); | ||
@@ -247,0 +234,0 @@ return createPackageDefinition(root, options); |
{ | ||
"name": "@ln-zap/proto-loader", | ||
"version": "0.5.1", | ||
"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 @@ }, |
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
27009
300