UNPKG

6.5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var plugin_pb_1 = require("google-protobuf/google/protobuf/compiler/plugin_pb");
4var WellKnown_1 = require("../WellKnown");
5var FieldTypes_1 = require("../ts/FieldTypes");
6var util_1 = require("../util");
7function createFile(output, filename) {
8 var file = new plugin_pb_1.CodeGeneratorResponse.File();
9 file.setName(filename);
10 file.setContent(output);
11 return file;
12}
13exports.createFile = createFile;
14function getCallingTypes(method, exportMap) {
15 var inputType = method.getInputType() || util_1.throwError("Missing input type");
16 var outputType = method.getOutputType() || util_1.throwError("Missing output type");
17 return {
18 requestType: FieldTypes_1.getFieldType(FieldTypes_1.MESSAGE_TYPE, inputType.slice(1), "", exportMap),
19 responseType: FieldTypes_1.getFieldType(FieldTypes_1.MESSAGE_TYPE, outputType.slice(1), "", exportMap),
20 };
21}
22function isUsed(fileDescriptor, pseudoNamespace, exportMap) {
23 return fileDescriptor.getServiceList().some(function (service) {
24 return service.getMethodList().some(function (method) {
25 var callingTypes = getCallingTypes(method, exportMap);
26 var namespacePackage = pseudoNamespace + ".";
27 return (callingTypes.requestType.indexOf(namespacePackage) === 0 ||
28 callingTypes.responseType.indexOf(namespacePackage) === 0);
29 });
30 });
31}
32var RPCDescriptor = (function () {
33 function RPCDescriptor(grpcService, protoService, exportMap) {
34 this.grpcService = grpcService;
35 this.protoService = protoService;
36 this.exportMap = exportMap;
37 }
38 Object.defineProperty(RPCDescriptor.prototype, "name", {
39 get: function () {
40 return this.protoService.getName() || util_1.throwError("Missing service name");
41 },
42 enumerable: true,
43 configurable: true
44 });
45 Object.defineProperty(RPCDescriptor.prototype, "qualifiedName", {
46 get: function () {
47 return (this.grpcService.packageName ? this.grpcService.packageName + "." : "") + this.name;
48 },
49 enumerable: true,
50 configurable: true
51 });
52 Object.defineProperty(RPCDescriptor.prototype, "methods", {
53 get: function () {
54 var _this = this;
55 return this.protoService.getMethodList()
56 .map(function (method) {
57 var callingTypes = getCallingTypes(method, _this.exportMap);
58 var methodName = method.getName() || util_1.throwError("Missing method name");
59 var nameAsCamelCase = methodName[0].toLowerCase() + methodName.substr(1);
60 var clientStreaming = method.getClientStreaming();
61 if (clientStreaming === undefined)
62 util_1.throwError("Missing client streaming");
63 var serverStreaming = method.getServerStreaming();
64 if (serverStreaming === undefined)
65 util_1.throwError("Missing server streaming");
66 return {
67 nameAsPascalCase: methodName,
68 nameAsCamelCase: nameAsCamelCase,
69 functionName: util_1.normaliseFieldObjectName(nameAsCamelCase),
70 serviceName: _this.name,
71 requestStream: clientStreaming || false,
72 responseStream: serverStreaming || false,
73 requestType: callingTypes.requestType,
74 responseType: callingTypes.responseType,
75 };
76 });
77 },
78 enumerable: true,
79 configurable: true
80 });
81 return RPCDescriptor;
82}());
83exports.RPCDescriptor = RPCDescriptor;
84var GrpcServiceDescriptor = (function () {
85 function GrpcServiceDescriptor(fileDescriptor, exportMap) {
86 this.fileDescriptor = fileDescriptor;
87 this.exportMap = exportMap;
88 var fileDescriptorName = fileDescriptor.getName() || util_1.throwError("Missing file descriptor name for service");
89 this.pathToRoot = util_1.getPathToRoot(fileDescriptorName);
90 }
91 Object.defineProperty(GrpcServiceDescriptor.prototype, "filename", {
92 get: function () {
93 return this.fileDescriptor.getName() || util_1.throwError("Missing file descriptor name for service");
94 },
95 enumerable: true,
96 configurable: true
97 });
98 Object.defineProperty(GrpcServiceDescriptor.prototype, "packageName", {
99 get: function () {
100 return this.fileDescriptor.getPackage() || "";
101 },
102 enumerable: true,
103 configurable: true
104 });
105 Object.defineProperty(GrpcServiceDescriptor.prototype, "imports", {
106 get: function () {
107 var _this = this;
108 var dependencies = this.fileDescriptor.getDependencyList()
109 .filter(function (dependency) { return isUsed(_this.fileDescriptor, util_1.filePathToPseudoNamespace(dependency), _this.exportMap); })
110 .map(function (dependency) {
111 var namespace = util_1.filePathToPseudoNamespace(dependency);
112 if (dependency in WellKnown_1.WellKnownTypesMap) {
113 return {
114 namespace: namespace,
115 path: WellKnown_1.WellKnownTypesMap[dependency],
116 };
117 }
118 else {
119 return {
120 namespace: namespace,
121 path: "" + _this.pathToRoot + util_1.replaceProtoSuffix(util_1.replaceProtoSuffix(dependency))
122 };
123 }
124 });
125 var hostProto = {
126 namespace: util_1.filePathToPseudoNamespace(this.filename),
127 path: "" + this.pathToRoot + util_1.replaceProtoSuffix(this.filename),
128 };
129 return [hostProto].concat(dependencies);
130 },
131 enumerable: true,
132 configurable: true
133 });
134 Object.defineProperty(GrpcServiceDescriptor.prototype, "services", {
135 get: function () {
136 var _this = this;
137 return this.fileDescriptor.getServiceList()
138 .map(function (service) {
139 return new RPCDescriptor(_this, service, _this.exportMap);
140 });
141 },
142 enumerable: true,
143 configurable: true
144 });
145 return GrpcServiceDescriptor;
146}());
147exports.GrpcServiceDescriptor = GrpcServiceDescriptor;
148//# sourceMappingURL=common.js.map
\No newline at end of file