|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.makeClientConstructor = makeClientConstructor; |
|
exports.loadPackageDefinition = loadPackageDefinition; |
|
const client_1 = require("./client"); |
|
|
|
|
|
|
|
|
|
|
|
const requesterFuncs = { |
|
unary: client_1.Client.prototype.makeUnaryRequest, |
|
server_stream: client_1.Client.prototype.makeServerStreamRequest, |
|
client_stream: client_1.Client.prototype.makeClientStreamRequest, |
|
bidi: client_1.Client.prototype.makeBidiStreamRequest, |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function isPrototypePolluted(key) { |
|
return ['__proto__', 'prototype', 'constructor'].includes(key); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function makeClientConstructor(methods, serviceName, classOptions) { |
|
if (!classOptions) { |
|
classOptions = {}; |
|
} |
|
class ServiceClientImpl extends client_1.Client { |
|
} |
|
Object.keys(methods).forEach(name => { |
|
if (isPrototypePolluted(name)) { |
|
return; |
|
} |
|
const attrs = methods[name]; |
|
let methodType; |
|
|
|
if (typeof name === 'string' && name.charAt(0) === '$') { |
|
throw new Error('Method names cannot start with $'); |
|
} |
|
if (attrs.requestStream) { |
|
if (attrs.responseStream) { |
|
methodType = 'bidi'; |
|
} |
|
else { |
|
methodType = 'client_stream'; |
|
} |
|
} |
|
else { |
|
if (attrs.responseStream) { |
|
methodType = 'server_stream'; |
|
} |
|
else { |
|
methodType = 'unary'; |
|
} |
|
} |
|
const serialize = attrs.requestSerialize; |
|
const deserialize = attrs.responseDeserialize; |
|
const methodFunc = partial(requesterFuncs[methodType], attrs.path, serialize, deserialize); |
|
ServiceClientImpl.prototype[name] = methodFunc; |
|
|
|
Object.assign(ServiceClientImpl.prototype[name], attrs); |
|
if (attrs.originalName && !isPrototypePolluted(attrs.originalName)) { |
|
ServiceClientImpl.prototype[attrs.originalName] = |
|
ServiceClientImpl.prototype[name]; |
|
} |
|
}); |
|
ServiceClientImpl.service = methods; |
|
ServiceClientImpl.serviceName = serviceName; |
|
return ServiceClientImpl; |
|
} |
|
function partial(fn, path, serialize, deserialize) { |
|
|
|
return function (...args) { |
|
return fn.call(this, path, serialize, deserialize, ...args); |
|
}; |
|
} |
|
function isProtobufTypeDefinition(obj) { |
|
return 'format' in obj; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function loadPackageDefinition(packageDef) { |
|
const result = {}; |
|
for (const serviceFqn in packageDef) { |
|
if (Object.prototype.hasOwnProperty.call(packageDef, serviceFqn)) { |
|
const service = packageDef[serviceFqn]; |
|
const nameComponents = serviceFqn.split('.'); |
|
if (nameComponents.some((comp) => isPrototypePolluted(comp))) { |
|
continue; |
|
} |
|
const serviceName = nameComponents[nameComponents.length - 1]; |
|
let current = result; |
|
for (const packageName of nameComponents.slice(0, -1)) { |
|
if (!current[packageName]) { |
|
current[packageName] = {}; |
|
} |
|
current = current[packageName]; |
|
} |
|
if (isProtobufTypeDefinition(service)) { |
|
current[serviceName] = service; |
|
} |
|
else { |
|
current[serviceName] = makeClientConstructor(service, serviceName, {}); |
|
} |
|
} |
|
} |
|
return result; |
|
} |
|
|