android-emulator-webrtc 1.0.18 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -204
- package/dist/components/emulator/emulator.d.ts +64 -0
- package/dist/components/emulator/emulator.js +161 -173
- package/dist/components/emulator/net/emulator_status.d.ts +45 -0
- package/dist/components/emulator/net/emulator_status.js +35 -42
- package/dist/components/emulator/net/logcat.js +37 -54
- package/dist/components/emulator/net/logger.d.ts +7 -0
- package/dist/components/emulator/net/logger.js +20 -0
- package/dist/components/emulator/net/ws_jsep_protocol_driver.d.ts +213 -0
- package/dist/components/emulator/net/ws_jsep_protocol_driver.js +640 -0
- package/dist/components/emulator/views/event_handler.d.ts +37 -0
- package/dist/components/emulator/views/event_handler.js +274 -260
- package/dist/components/emulator/views/webrtc_view.d.ts +27 -0
- package/dist/components/emulator/views/webrtc_view.js +42 -20
- package/dist/index.d.ts +8 -0
- package/dist/index.js +0 -7
- package/dist/proto/emulator_controller_pb.d.ts +1 -0
- package/dist/proto/emulator_controller_pb.js +146 -287
- package/dist/src/components/emulator/emulator.d.ts +64 -0
- package/dist/src/components/emulator/net/emulator_status.d.ts +45 -0
- package/dist/src/components/emulator/net/logger.d.ts +7 -0
- package/dist/src/components/emulator/net/ws_jsep_protocol_driver.d.ts +213 -0
- package/dist/src/components/emulator/views/event_handler.d.ts +37 -0
- package/dist/src/components/emulator/views/webrtc_view.d.ts +27 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/proto/emulator_controller_pb.d.ts +1 -0
- package/package.json +17 -17
- package/proto/emulator_controller.proto +746 -181
- package/.vscode/launch.json +0 -21
- package/.vscode/settings.json +0 -3
- package/CONTRIBUTING.md +0 -28
- package/Makefile +0 -123
- package/android-emulator-webrtc.code-workspace +0 -7
- package/api_descriptor.pb +0 -0
- package/babel.config.js +0 -16
- package/cloudbuild.yaml +0 -41
- package/cloudbuilders/.gcloudignore +0 -1
- package/cloudbuilders/Dockerfile +0 -20
- package/cloudbuilders/README.md +0 -29
- package/cloudbuilders/cloudbuild.yaml +0 -19
- package/dist/components/emulator/net/jsep_protocol_driver.js +0 -365
- package/dist/components/emulator/views/simple_png_view.js +0 -110
- package/dist/proto/emulator_controller_grpc_web_pb.js +0 -1601
- package/dist/proto/emulator_web_client.js +0 -176
- package/dist/proto/rtc_service_grpc_web_pb.js +0 -231
- package/dist/proto/rtc_service_pb.js +0 -338
- package/emulator/index.js +0 -1
- package/eslint_prefix.py +0 -37
- package/gen_md_doc.js +0 -40
- package/npmrc.enc +0 -0
- package/proto/rtc_service.proto +0 -117
- package/protoc-plugin/Makefile +0 -31
- package/protoc-plugin/grpc_generator.cc +0 -1755
- package/src/components/emulator/emulator.js +0 -217
- package/src/components/emulator/net/emulator_status.js +0 -94
- package/src/components/emulator/net/jsep_protocol_driver.js +0 -364
- package/src/components/emulator/net/logcat.js +0 -155
- package/src/components/emulator/views/event_handler.js +0 -268
- package/src/components/emulator/views/simple_png_view.js +0 -105
- package/src/components/emulator/views/webrtc_view.js +0 -142
- package/src/index.js +0 -20
- package/src/proto/emulator_web_client.js +0 -140
- package/test/cloudbuild.yaml +0 -41
- package/test/emulator.test.js +0 -125
- package/test/event_handler.test.js +0 -91
- package/test/fake_events.js +0 -50
- package/test/jsep_protocol_driver.test.js +0 -228
- package/test/simple_png_view.test.js +0 -145
- package/test/touch_event_handler.test.js +0 -118
|
@@ -1,1755 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* Copyright 2018 Google LLC
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
#include <google/protobuf/compiler/code_generator.h>
|
|
20
|
-
#include <google/protobuf/compiler/plugin.h>
|
|
21
|
-
#include <google/protobuf/compiler/plugin.pb.h>
|
|
22
|
-
#include <google/protobuf/descriptor.h>
|
|
23
|
-
#include <google/protobuf/descriptor.pb.h>
|
|
24
|
-
#include <google/protobuf/io/printer.h>
|
|
25
|
-
#include <google/protobuf/io/zero_copy_stream.h>
|
|
26
|
-
|
|
27
|
-
#include <algorithm>
|
|
28
|
-
#include <iterator>
|
|
29
|
-
#include <set>
|
|
30
|
-
#include <string>
|
|
31
|
-
|
|
32
|
-
using google::protobuf::Descriptor;
|
|
33
|
-
using google::protobuf::EnumDescriptor;
|
|
34
|
-
using google::protobuf::FieldDescriptor;
|
|
35
|
-
using google::protobuf::FileDescriptor;
|
|
36
|
-
using google::protobuf::MethodDescriptor;
|
|
37
|
-
using google::protobuf::ServiceDescriptor;
|
|
38
|
-
using google::protobuf::FieldOptions;
|
|
39
|
-
using google::protobuf::OneofDescriptor;
|
|
40
|
-
using google::protobuf::compiler::CodeGenerator;
|
|
41
|
-
using google::protobuf::compiler::GeneratorContext;
|
|
42
|
-
using google::protobuf::compiler::ParseGeneratorParameter;
|
|
43
|
-
using google::protobuf::compiler::PluginMain;
|
|
44
|
-
using google::protobuf::compiler::Version;
|
|
45
|
-
using google::protobuf::io::Printer;
|
|
46
|
-
using google::protobuf::io::ZeroCopyOutputStream;
|
|
47
|
-
|
|
48
|
-
namespace grpc {
|
|
49
|
-
namespace web {
|
|
50
|
-
namespace {
|
|
51
|
-
|
|
52
|
-
using std::string;
|
|
53
|
-
|
|
54
|
-
enum Mode {
|
|
55
|
-
OP = 0, // first party google3 one platform services
|
|
56
|
-
GRPCWEB = 1, // client using the application/grpc-web wire format
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
enum ImportStyle {
|
|
60
|
-
CLOSURE = 0, // goog.require("grpc.web.*")
|
|
61
|
-
COMMONJS = 1, // const grpcWeb = require("grpc-web")
|
|
62
|
-
TYPESCRIPT = 2, // import * as grpcWeb from 'grpc-web'
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const char GRPC_PROMISE[] = "grpc.web.promise.GrpcWebPromise";
|
|
66
|
-
|
|
67
|
-
const char* kKeyword[] = {
|
|
68
|
-
"abstract", "boolean", "break", "byte", "case",
|
|
69
|
-
"catch", "char", "class", "const", "continue",
|
|
70
|
-
"debugger", "default", "delete", "do", "double",
|
|
71
|
-
"else", "enum", "export", "extends", "false",
|
|
72
|
-
"final", "finally", "float", "for", "function",
|
|
73
|
-
"goto", "if", "implements", "import", "in",
|
|
74
|
-
"instanceof", "int", "interface", "long", "native",
|
|
75
|
-
"new", "null", "package", "private", "protected",
|
|
76
|
-
"public", "return", "short", "static", "super",
|
|
77
|
-
"switch", "synchronized", "this", "throw", "throws",
|
|
78
|
-
"transient", "try", "typeof", "var", "void",
|
|
79
|
-
"volatile", "while", "with",
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
// Edit the version here prior to release
|
|
83
|
-
static const std::string GRPC_WEB_VERSION = "1.4.2";
|
|
84
|
-
|
|
85
|
-
string GetProtocVersion(GeneratorContext* context) {
|
|
86
|
-
Version compiler_version;
|
|
87
|
-
context->GetCompilerVersion(&compiler_version);
|
|
88
|
-
return std::to_string(compiler_version.major()) + "." +
|
|
89
|
-
std::to_string(compiler_version.minor()) + "." +
|
|
90
|
-
std::to_string(compiler_version.patch()) +
|
|
91
|
-
compiler_version.suffix();
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
bool IsReserved(const string& ident) {
|
|
95
|
-
for (size_t i = 0; i < sizeof(kKeyword) / sizeof(kKeyword[0]); i++) {
|
|
96
|
-
if (ident == kKeyword[i]) {
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
string GetModeVar(const Mode mode) {
|
|
104
|
-
switch (mode) {
|
|
105
|
-
case OP:
|
|
106
|
-
return "OP";
|
|
107
|
-
case GRPCWEB:
|
|
108
|
-
return "GrpcWeb";
|
|
109
|
-
}
|
|
110
|
-
return "";
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
string GetDeserializeMethodName(std::map<string, string> vars) {
|
|
114
|
-
if (vars["mode"] == GetModeVar(Mode::OP) && vars["binary"] == "false") {
|
|
115
|
-
return "deserialize";
|
|
116
|
-
}
|
|
117
|
-
return "deserializeBinary";
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
string GetSerializeMethodName(std::map<string, string> vars) {
|
|
121
|
-
if (vars["mode"] == GetModeVar(Mode::OP) && vars["binary"] == "false") {
|
|
122
|
-
return "serialize";
|
|
123
|
-
}
|
|
124
|
-
return "serializeBinary";
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
std::string GetSerializeMethodReturnType(std::map<string, string> vars) {
|
|
128
|
-
if (vars["mode"] == GetModeVar(Mode::OP) && vars["binary"] == "false") {
|
|
129
|
-
return "string";
|
|
130
|
-
}
|
|
131
|
-
return "!Uint8Array";
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
string LowercaseFirstLetter(string s) {
|
|
135
|
-
if (s.empty()) {
|
|
136
|
-
return s;
|
|
137
|
-
}
|
|
138
|
-
s[0] = ::tolower(s[0]);
|
|
139
|
-
return s;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
string Lowercase(string s) {
|
|
143
|
-
if (s.empty()) {
|
|
144
|
-
return s;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
for (size_t i = 0; i < s.size(); i++) {
|
|
148
|
-
s[i] = ::tolower(s[i]);
|
|
149
|
-
}
|
|
150
|
-
return s;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
string UppercaseFirstLetter(string s) {
|
|
154
|
-
if (s.empty()) {
|
|
155
|
-
return s;
|
|
156
|
-
}
|
|
157
|
-
s[0] = ::toupper(s[0]);
|
|
158
|
-
return s;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
string Uppercase(string s) {
|
|
162
|
-
if (s.empty()) {
|
|
163
|
-
return s;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
for (size_t i = 0; i < s.size(); i++) {
|
|
167
|
-
s[i] = ::toupper(s[i]);
|
|
168
|
-
}
|
|
169
|
-
return s;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// The following 5 functions were copied from
|
|
173
|
-
// google/protobuf/src/google/protobuf/stubs/strutil.h
|
|
174
|
-
|
|
175
|
-
inline bool HasPrefixString(const string& str, const string& prefix) {
|
|
176
|
-
return str.size() >= prefix.size() &&
|
|
177
|
-
str.compare(0, prefix.size(), prefix) == 0;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Strips the given prefix from the string, as well as the remaining leading dot
|
|
181
|
-
// if it exists.
|
|
182
|
-
inline string StripPrefixString(const string& str, const string& prefix) {
|
|
183
|
-
if (!HasPrefixString(str, prefix)) {
|
|
184
|
-
return str;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
string remaining_str = str.substr(prefix.size());
|
|
188
|
-
if (!remaining_str.empty() && remaining_str[0] == '.') {
|
|
189
|
-
remaining_str = remaining_str.substr(1);
|
|
190
|
-
}
|
|
191
|
-
return remaining_str;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
inline bool HasSuffixString(const string& str, const string& suffix) {
|
|
195
|
-
return str.size() >= suffix.size() &&
|
|
196
|
-
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
inline string StripSuffixString(const string& str, const string& suffix) {
|
|
200
|
-
if (HasSuffixString(str, suffix)) {
|
|
201
|
-
return str.substr(0, str.size() - suffix.size());
|
|
202
|
-
} else {
|
|
203
|
-
return str;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
void ReplaceCharacters(string* s, const char* remove, char replacewith) {
|
|
208
|
-
const char* str_start = s->c_str();
|
|
209
|
-
const char* str = str_start;
|
|
210
|
-
for (str = strpbrk(str, remove); str != nullptr;
|
|
211
|
-
str = strpbrk(str + 1, remove)) {
|
|
212
|
-
(*s)[str - str_start] = replacewith;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// The following function was copied from
|
|
217
|
-
// google/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc
|
|
218
|
-
|
|
219
|
-
string StripProto(const string& filename) {
|
|
220
|
-
if (HasSuffixString(filename, ".protodevel")) {
|
|
221
|
-
return StripSuffixString(filename, ".protodevel");
|
|
222
|
-
} else {
|
|
223
|
-
return StripSuffixString(filename, ".proto");
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// The following 6 functions were copied from
|
|
228
|
-
// google/protobuf/src/google/protobuf/compiler/js/js_generator.cc
|
|
229
|
-
|
|
230
|
-
char ToLowerASCII(char c) {
|
|
231
|
-
if (c >= 'A' && c <= 'Z') {
|
|
232
|
-
return (c - 'A') + 'a';
|
|
233
|
-
} else {
|
|
234
|
-
return c;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
std::vector<string> ParseLowerUnderscore(const string& input) {
|
|
239
|
-
std::vector<string> words;
|
|
240
|
-
string running = "";
|
|
241
|
-
for (size_t i = 0; i < input.size(); i++) {
|
|
242
|
-
if (input[i] == '_') {
|
|
243
|
-
if (!running.empty()) {
|
|
244
|
-
words.push_back(running);
|
|
245
|
-
running.clear();
|
|
246
|
-
}
|
|
247
|
-
} else {
|
|
248
|
-
running += ToLowerASCII(input[i]);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
if (!running.empty()) {
|
|
252
|
-
words.push_back(running);
|
|
253
|
-
}
|
|
254
|
-
return words;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
string ToUpperCamel(const std::vector<string>& words) {
|
|
258
|
-
string result;
|
|
259
|
-
for (size_t i = 0; i < words.size(); i++) {
|
|
260
|
-
string word = words[i];
|
|
261
|
-
if (word[0] >= 'a' && word[0] <= 'z') {
|
|
262
|
-
word[0] = (word[0] - 'a') + 'A';
|
|
263
|
-
}
|
|
264
|
-
result += word;
|
|
265
|
-
}
|
|
266
|
-
return result;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// Returns the alias we assign to the module of the given .proto filename
|
|
270
|
-
// when importing.
|
|
271
|
-
string ModuleAlias(const string& filename) {
|
|
272
|
-
// This scheme could technically cause problems if a file includes any 2 of:
|
|
273
|
-
// foo/bar_baz.proto
|
|
274
|
-
// foo_bar_baz.proto
|
|
275
|
-
// foo_bar/baz.proto
|
|
276
|
-
//
|
|
277
|
-
// We'll worry about this problem if/when we actually see it. This name isn't
|
|
278
|
-
// exposed to users so we can change it later if we need to.
|
|
279
|
-
string basename = StripProto(filename);
|
|
280
|
-
ReplaceCharacters(&basename, "-", '$');
|
|
281
|
-
ReplaceCharacters(&basename, "/", '_');
|
|
282
|
-
ReplaceCharacters(&basename, ".", '_');
|
|
283
|
-
return basename + "_pb";
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
string JSMessageType(const Descriptor* desc, const FileDescriptor* file) {
|
|
287
|
-
string class_name = StripPrefixString(desc->full_name(), desc->file()->package());
|
|
288
|
-
if (desc->file() == file) {
|
|
289
|
-
// [for protobuf .d.ts files only] Do not add the module prefix for local
|
|
290
|
-
// messages.
|
|
291
|
-
return class_name;
|
|
292
|
-
}
|
|
293
|
-
return ModuleAlias(desc->file()->name()) + "." + class_name;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
string JSMessageType(const Descriptor* desc) {
|
|
297
|
-
return JSMessageType(desc, nullptr);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
string JSElementType(const FieldDescriptor* desc, const FileDescriptor* file) {
|
|
301
|
-
switch (desc->type()) {
|
|
302
|
-
case FieldDescriptor::TYPE_DOUBLE:
|
|
303
|
-
case FieldDescriptor::TYPE_FLOAT:
|
|
304
|
-
case FieldDescriptor::TYPE_INT32:
|
|
305
|
-
case FieldDescriptor::TYPE_UINT32:
|
|
306
|
-
case FieldDescriptor::TYPE_SINT32:
|
|
307
|
-
case FieldDescriptor::TYPE_FIXED32:
|
|
308
|
-
case FieldDescriptor::TYPE_SFIXED32:
|
|
309
|
-
return "number";
|
|
310
|
-
|
|
311
|
-
case FieldDescriptor::TYPE_INT64:
|
|
312
|
-
case FieldDescriptor::TYPE_UINT64:
|
|
313
|
-
case FieldDescriptor::TYPE_SINT64:
|
|
314
|
-
case FieldDescriptor::TYPE_FIXED64:
|
|
315
|
-
case FieldDescriptor::TYPE_SFIXED64:
|
|
316
|
-
if (desc->options().jstype() == FieldOptions::JS_STRING) {
|
|
317
|
-
return "string";
|
|
318
|
-
} else {
|
|
319
|
-
return "number";
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
case FieldDescriptor::TYPE_BOOL:
|
|
323
|
-
return "boolean";
|
|
324
|
-
|
|
325
|
-
case FieldDescriptor::TYPE_STRING:
|
|
326
|
-
return "string";
|
|
327
|
-
|
|
328
|
-
case FieldDescriptor::TYPE_BYTES:
|
|
329
|
-
return "Uint8Array | string";
|
|
330
|
-
|
|
331
|
-
case FieldDescriptor::TYPE_ENUM:
|
|
332
|
-
if (desc->enum_type()->file() == file) {
|
|
333
|
-
// [for protobuf .d.ts files only] Do not add the module prefix for
|
|
334
|
-
// local messages.
|
|
335
|
-
return StripPrefixString(desc->enum_type()->full_name(),
|
|
336
|
-
desc->enum_type()->file()->package());
|
|
337
|
-
}
|
|
338
|
-
return ModuleAlias(desc->enum_type()->file()->name()) + "." +
|
|
339
|
-
StripPrefixString(desc->enum_type()->full_name(),
|
|
340
|
-
desc->enum_type()->file()->package());
|
|
341
|
-
|
|
342
|
-
case FieldDescriptor::TYPE_MESSAGE:
|
|
343
|
-
return JSMessageType(desc->message_type(), file);
|
|
344
|
-
|
|
345
|
-
default:
|
|
346
|
-
return "{}";
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
string JSFieldType(const FieldDescriptor* desc, const FileDescriptor* file) {
|
|
351
|
-
string js_field_type = JSElementType(desc, file);
|
|
352
|
-
if (desc->is_map()) {
|
|
353
|
-
string key_type = JSFieldType(desc->message_type()->field(0), file);
|
|
354
|
-
string value_type = JSFieldType(desc->message_type()->field(1), file);
|
|
355
|
-
return "jspb.Map<" + key_type + ", " + value_type + ">";
|
|
356
|
-
}
|
|
357
|
-
if (desc->is_repeated()) {
|
|
358
|
-
return "Array<" + js_field_type + ">";
|
|
359
|
-
}
|
|
360
|
-
return js_field_type;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
string AsObjectFieldType(const FieldDescriptor* desc,
|
|
364
|
-
const FileDescriptor* file) {
|
|
365
|
-
if (desc->type() != FieldDescriptor::TYPE_MESSAGE) {
|
|
366
|
-
return JSFieldType(desc, file);
|
|
367
|
-
}
|
|
368
|
-
if (desc->is_map()) {
|
|
369
|
-
const Descriptor* message = desc->message_type();
|
|
370
|
-
string key_type = AsObjectFieldType(message->field(0), file);
|
|
371
|
-
string value_type = AsObjectFieldType(message->field(1), file);
|
|
372
|
-
return "Array<[" + key_type + ", " + value_type + "]>";
|
|
373
|
-
}
|
|
374
|
-
string field_type = JSMessageType(desc->message_type(), file) + ".AsObject";
|
|
375
|
-
if (desc->is_repeated()) {
|
|
376
|
-
return "Array<" + field_type + ">";
|
|
377
|
-
}
|
|
378
|
-
return field_type;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
string JSElementName(const FieldDescriptor* desc) {
|
|
382
|
-
return ToUpperCamel(ParseLowerUnderscore(desc->name()));
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
string JSFieldName(const FieldDescriptor* desc) {
|
|
386
|
-
string js_field_name = JSElementName(desc);
|
|
387
|
-
if (desc->is_map()) {
|
|
388
|
-
js_field_name += "Map";
|
|
389
|
-
} else if (desc->is_repeated()) {
|
|
390
|
-
js_field_name += "List";
|
|
391
|
-
}
|
|
392
|
-
return js_field_name;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// Like ToUpperCamel except the first letter is not converted.
|
|
396
|
-
string ToCamelCase(const std::vector<string>& words) {
|
|
397
|
-
if (words.empty()) {
|
|
398
|
-
return "";
|
|
399
|
-
}
|
|
400
|
-
string result = words[0];
|
|
401
|
-
return result + ToUpperCamel(std::vector<string>(
|
|
402
|
-
words.begin() + 1, words.begin() + words.size()));
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// Like JSFieldName, but with first letter not uppercased
|
|
406
|
-
string CamelCaseJSFieldName(const FieldDescriptor* desc) {
|
|
407
|
-
string js_field_name = ToCamelCase(ParseLowerUnderscore(desc->name()));
|
|
408
|
-
if (desc->is_map()) {
|
|
409
|
-
js_field_name += "Map";
|
|
410
|
-
} else if (desc->is_repeated()) {
|
|
411
|
-
js_field_name += "List";
|
|
412
|
-
}
|
|
413
|
-
return js_field_name;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// Returns the name of the message with a leading dot and taking into account
|
|
417
|
-
// nesting, for example ".OuterMessage.InnerMessage", or returns empty if
|
|
418
|
-
// descriptor is null. This function does not handle namespacing, only message
|
|
419
|
-
// nesting.
|
|
420
|
-
string GetNestedMessageName(const Descriptor* descriptor) {
|
|
421
|
-
if (descriptor == nullptr) {
|
|
422
|
-
return "";
|
|
423
|
-
}
|
|
424
|
-
string result =
|
|
425
|
-
StripPrefixString(descriptor->full_name(), descriptor->file()->package());
|
|
426
|
-
// Add a leading dot if one is not already present.
|
|
427
|
-
if (!result.empty() && result[0] != '.') {
|
|
428
|
-
result = "." + result;
|
|
429
|
-
}
|
|
430
|
-
return result;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
// Given a filename like foo/bar/baz.proto, returns the root directory
|
|
434
|
-
// path ../../
|
|
435
|
-
string GetRootPath(const string& from_filename, const string& to_filename) {
|
|
436
|
-
if (HasPrefixString(to_filename, "google/protobuf")) {
|
|
437
|
-
// Well-known types (.proto files in the google/protobuf directory) are
|
|
438
|
-
// assumed to come from the 'google-protobuf' npm package. We may want to
|
|
439
|
-
// generalize this exception later by letting others put generated code in
|
|
440
|
-
// their own npm packages.
|
|
441
|
-
return "google-protobuf/";
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
size_t slashes = std::count(from_filename.begin(), from_filename.end(), '/');
|
|
445
|
-
if (slashes == 0) {
|
|
446
|
-
return "./";
|
|
447
|
-
}
|
|
448
|
-
string result = "";
|
|
449
|
-
for (size_t i = 0; i < slashes; i++) {
|
|
450
|
-
result += "../";
|
|
451
|
-
}
|
|
452
|
-
return result;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
// Splits path immediately following the final slash, separating it into a
|
|
456
|
-
// directory and file name component. Directory will contain the last
|
|
457
|
-
// slash, if it's not empty.
|
|
458
|
-
// If there is no slash in path, Split returns an empty directory and
|
|
459
|
-
// basename set to path.
|
|
460
|
-
// Output values have the property that path = directory + basename.
|
|
461
|
-
void PathSplit(const string& path, string* directory, string* basename) {
|
|
462
|
-
string::size_type last_slash = path.rfind('/');
|
|
463
|
-
if (last_slash == string::npos) {
|
|
464
|
-
if (directory) {
|
|
465
|
-
*directory = "";
|
|
466
|
-
}
|
|
467
|
-
if (basename) {
|
|
468
|
-
*basename = path;
|
|
469
|
-
}
|
|
470
|
-
} else {
|
|
471
|
-
if (directory) {
|
|
472
|
-
*directory = path.substr(0, last_slash + 1);
|
|
473
|
-
}
|
|
474
|
-
if (basename) {
|
|
475
|
-
*basename = path.substr(last_slash + 1);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
// Returns the basename of a file.
|
|
481
|
-
string GetBasename(string filename) {
|
|
482
|
-
string basename;
|
|
483
|
-
PathSplit(filename, nullptr, &basename);
|
|
484
|
-
return basename;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
// Finds all message types used in all services in the file. Return results as a
|
|
488
|
-
// map of full names to descriptors to get sorted results and deterministic
|
|
489
|
-
// build outputs.
|
|
490
|
-
std::map<string, const Descriptor*> GetAllMessages(const FileDescriptor* file) {
|
|
491
|
-
std::map<string, const Descriptor*> messages;
|
|
492
|
-
for (int s = 0; s < file->service_count(); ++s) {
|
|
493
|
-
const ServiceDescriptor* service = file->service(s);
|
|
494
|
-
for (int m = 0; m < service->method_count(); ++m) {
|
|
495
|
-
const MethodDescriptor* method = service->method(m);
|
|
496
|
-
messages[method->input_type()->full_name()] = method->input_type();
|
|
497
|
-
messages[method->output_type()->full_name()] = method->output_type();
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
return messages;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
void PrintClosureDependencies(Printer* printer, const FileDescriptor* file) {
|
|
505
|
-
for (const auto& entry : GetAllMessages(file)) {
|
|
506
|
-
printer->Print("goog.require('proto.$full_name$');\n", "full_name",
|
|
507
|
-
entry.second->full_name());
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
void PrintCommonJsMessagesDeps(Printer* printer, const FileDescriptor* file) {
|
|
512
|
-
std::map<string, string> vars;
|
|
513
|
-
|
|
514
|
-
for (int i = 0; i < file->dependency_count(); i++) {
|
|
515
|
-
const string& name = file->dependency(i)->name();
|
|
516
|
-
vars["alias"] = ModuleAlias(name);
|
|
517
|
-
vars["dep_filename"] = GetRootPath(file->name(), name) + StripProto(name);
|
|
518
|
-
// we need to give each cross-file import an alias
|
|
519
|
-
printer->Print(vars, "\nvar $alias$ = require('$dep_filename$_pb.js')\n");
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
const string& package = file->package();
|
|
523
|
-
vars["package_name"] = package;
|
|
524
|
-
|
|
525
|
-
if (!package.empty()) {
|
|
526
|
-
size_t offset = 0;
|
|
527
|
-
size_t dotIndex = package.find('.');
|
|
528
|
-
|
|
529
|
-
printer->Print(vars, "const proto = {};\n");
|
|
530
|
-
|
|
531
|
-
while (dotIndex != string::npos) {
|
|
532
|
-
vars["current_package_ns"] = package.substr(0, dotIndex);
|
|
533
|
-
printer->Print(vars, "proto.$current_package_ns$ = {};\n");
|
|
534
|
-
|
|
535
|
-
offset = dotIndex + 1;
|
|
536
|
-
dotIndex = package.find('.', offset);
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
// need to import the messages from our own file
|
|
541
|
-
vars["filename"] = GetBasename(StripProto(file->name()));
|
|
542
|
-
|
|
543
|
-
if (!package.empty()) {
|
|
544
|
-
printer->Print(vars,
|
|
545
|
-
"proto.$package_name$ = require('./$filename$_pb.js');\n\n");
|
|
546
|
-
} else {
|
|
547
|
-
printer->Print(vars, "const proto = require('./$filename$_pb.js');\n\n");
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
void PrintES6Imports(Printer* printer, const FileDescriptor* file) {
|
|
552
|
-
std::map<string, string> vars;
|
|
553
|
-
|
|
554
|
-
printer->Print("import * as grpcWeb from 'grpc-web';\n\n");
|
|
555
|
-
|
|
556
|
-
std::set<string> imports;
|
|
557
|
-
for (const auto& entry : GetAllMessages(file)) {
|
|
558
|
-
const string& proto_filename = entry.second->file()->name();
|
|
559
|
-
string dep_filename = GetRootPath(file->name(), proto_filename) + StripProto(proto_filename);
|
|
560
|
-
if (imports.find(dep_filename) != imports.end()) {
|
|
561
|
-
continue;
|
|
562
|
-
}
|
|
563
|
-
imports.insert(dep_filename);
|
|
564
|
-
// We need to give each cross-file import an alias.
|
|
565
|
-
printer->Print("import * as $alias$ from '$dep_filename$_pb'; // proto import: \"$proto_filename$\"\n",
|
|
566
|
-
"alias", ModuleAlias(proto_filename),
|
|
567
|
-
"dep_filename", dep_filename,
|
|
568
|
-
"proto_filename", proto_filename);
|
|
569
|
-
}
|
|
570
|
-
printer->Print("\n\n");
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
|
|
574
|
-
std::map<string, string> vars) {
|
|
575
|
-
PrintES6Imports(printer, file);
|
|
576
|
-
for (int service_index = 0; service_index < file->service_count();
|
|
577
|
-
++service_index) {
|
|
578
|
-
printer->Print("export class ");
|
|
579
|
-
const ServiceDescriptor* service = file->service(service_index);
|
|
580
|
-
vars["service_name"] = service->name();
|
|
581
|
-
printer->Print(vars, "$service_name$Client {\n");
|
|
582
|
-
printer->Indent();
|
|
583
|
-
printer->Print(
|
|
584
|
-
"client_: grpcWeb.AbstractClientBase;\n"
|
|
585
|
-
"hostname_: string;\n"
|
|
586
|
-
"credentials_: null | { [index: string]: string; };\n"
|
|
587
|
-
"options_: null | { [index: string]: any; };\n\n"
|
|
588
|
-
"constructor (hostname: string,\n"
|
|
589
|
-
" credentials?: null | { [index: string]: string; },\n"
|
|
590
|
-
" options?: null | { [index: string]: any; }) {\n");
|
|
591
|
-
printer->Indent();
|
|
592
|
-
printer->Print("if (!options) options = {};\n");
|
|
593
|
-
printer->Print("if (!credentials) credentials = {};\n");
|
|
594
|
-
if (vars["mode"] == GetModeVar(Mode::GRPCWEB)) {
|
|
595
|
-
printer->Print(vars, "options['format'] = '$format$';\n\n");
|
|
596
|
-
}
|
|
597
|
-
printer->Print(vars,
|
|
598
|
-
"this.client_ = new grpcWeb.$mode$ClientBase(options);\n"
|
|
599
|
-
"this.hostname_ = hostname.replace(/\\/+$$/, '');\n"
|
|
600
|
-
"this.credentials_ = credentials;\n"
|
|
601
|
-
"this.options_ = options;\n");
|
|
602
|
-
printer->Outdent();
|
|
603
|
-
printer->Print("}\n\n");
|
|
604
|
-
|
|
605
|
-
for (int method_index = 0; method_index < service->method_count();
|
|
606
|
-
++method_index) {
|
|
607
|
-
const MethodDescriptor* method = service->method(method_index);
|
|
608
|
-
vars["js_method_name"] = LowercaseFirstLetter(method->name());
|
|
609
|
-
vars["method_name"] = method->name();
|
|
610
|
-
vars["input_type"] = JSMessageType(method->input_type());
|
|
611
|
-
vars["output_type"] = JSMessageType(method->output_type());
|
|
612
|
-
vars["serialize_func_name"] = GetSerializeMethodName(vars);
|
|
613
|
-
vars["deserialize_func_name"] = GetDeserializeMethodName(vars);
|
|
614
|
-
vars["method_type"] = method->server_streaming()
|
|
615
|
-
? "grpcWeb.MethodType.SERVER_STREAMING"
|
|
616
|
-
: "grpcWeb.MethodType.UNARY";
|
|
617
|
-
if (!method->client_streaming()) {
|
|
618
|
-
printer->Print(vars,
|
|
619
|
-
"methodDescriptor$method_name$ = "
|
|
620
|
-
"new grpcWeb.MethodDescriptor(\n");
|
|
621
|
-
printer->Indent();
|
|
622
|
-
printer->Print(vars,
|
|
623
|
-
"'/$package_dot$$service_name$/$method_name$',\n"
|
|
624
|
-
"$method_type$,\n"
|
|
625
|
-
"$input_type$,\n"
|
|
626
|
-
"$output_type$,\n"
|
|
627
|
-
"(request: $input_type$) => {\n"
|
|
628
|
-
" return request.$serialize_func_name$();\n"
|
|
629
|
-
"},\n"
|
|
630
|
-
"$output_type$.$deserialize_func_name$\n");
|
|
631
|
-
printer->Outdent();
|
|
632
|
-
printer->Print(");\n\n");
|
|
633
|
-
if (method->server_streaming()) {
|
|
634
|
-
printer->Print(vars, "$js_method_name$(\n");
|
|
635
|
-
printer->Indent();
|
|
636
|
-
printer->Print(vars,
|
|
637
|
-
"request: $input_type$,\n"
|
|
638
|
-
"metadata?: grpcWeb.Metadata): "
|
|
639
|
-
"grpcWeb.ClientReadableStream<$output_type$> {\n");
|
|
640
|
-
printer->Print(vars, "return this.client_.serverStreaming(\n");
|
|
641
|
-
printer->Indent();
|
|
642
|
-
printer->Print(vars,
|
|
643
|
-
"this.hostname_ +\n"
|
|
644
|
-
" '/$package_dot$$service_name$/$method_name$',\n"
|
|
645
|
-
"request,\n"
|
|
646
|
-
"metadata || {},\n"
|
|
647
|
-
"this.methodDescriptor$method_name$);\n");
|
|
648
|
-
printer->Outdent();
|
|
649
|
-
printer->Outdent();
|
|
650
|
-
printer->Print("}\n\n");
|
|
651
|
-
} else {
|
|
652
|
-
printer->Print(vars, "$js_method_name$(\n");
|
|
653
|
-
printer->Indent();
|
|
654
|
-
printer->Print(vars,
|
|
655
|
-
"request: $input_type$,\n"
|
|
656
|
-
"metadata: grpcWeb.Metadata | null): "
|
|
657
|
-
"$promise$<$output_type$>;\n\n");
|
|
658
|
-
printer->Outdent();
|
|
659
|
-
|
|
660
|
-
printer->Print(vars, "$js_method_name$(\n");
|
|
661
|
-
printer->Indent();
|
|
662
|
-
printer->Print(vars,
|
|
663
|
-
"request: $input_type$,\n"
|
|
664
|
-
"metadata: grpcWeb.Metadata | null,\n"
|
|
665
|
-
"callback: (err: grpcWeb.RpcError,\n"
|
|
666
|
-
" response: $output_type$) => void): "
|
|
667
|
-
"grpcWeb.ClientReadableStream<$output_type$>;\n\n");
|
|
668
|
-
printer->Outdent();
|
|
669
|
-
|
|
670
|
-
printer->Print(vars, "$js_method_name$(\n");
|
|
671
|
-
printer->Indent();
|
|
672
|
-
printer->Print(vars,
|
|
673
|
-
"request: $input_type$,\n"
|
|
674
|
-
"metadata: grpcWeb.Metadata | null,\n"
|
|
675
|
-
"callback?: (err: grpcWeb.RpcError,\n"
|
|
676
|
-
" response: $output_type$) => void) {\n");
|
|
677
|
-
printer->Print(vars, "if (callback !== undefined) {\n");
|
|
678
|
-
printer->Indent();
|
|
679
|
-
printer->Print(vars, "return this.client_.rpcCall(\n");
|
|
680
|
-
printer->Indent();
|
|
681
|
-
printer->Print(vars,
|
|
682
|
-
"this.hostname_ +\n"
|
|
683
|
-
" '/$package_dot$$service_name$/$method_name$',\n"
|
|
684
|
-
"request,\n"
|
|
685
|
-
"metadata || {},\n"
|
|
686
|
-
"this.methodDescriptor$method_name$,\n"
|
|
687
|
-
"callback);\n");
|
|
688
|
-
printer->Outdent();
|
|
689
|
-
printer->Outdent();
|
|
690
|
-
printer->Print(vars,
|
|
691
|
-
"}\n"
|
|
692
|
-
"return this.client_.unaryCall(\n");
|
|
693
|
-
printer->Print(vars,
|
|
694
|
-
"this.hostname_ +\n"
|
|
695
|
-
" '/$package_dot$$service_name$/$method_name$',\n"
|
|
696
|
-
"request,\n"
|
|
697
|
-
"metadata || {},\n"
|
|
698
|
-
"this.methodDescriptor$method_name$);\n");
|
|
699
|
-
printer->Outdent();
|
|
700
|
-
printer->Print("}\n\n");
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
printer->Outdent();
|
|
705
|
-
printer->Print("}\n\n");
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
void PrintGrpcWebDtsClientClass(Printer* printer, const FileDescriptor* file,
|
|
710
|
-
const string& client_type) {
|
|
711
|
-
std::map<string, string> vars;
|
|
712
|
-
vars["client_type"] = client_type;
|
|
713
|
-
vars["promise"] = "Promise";
|
|
714
|
-
for (int service_index = 0; service_index < file->service_count();
|
|
715
|
-
++service_index) {
|
|
716
|
-
printer->Print("export class ");
|
|
717
|
-
const ServiceDescriptor* service = file->service(service_index);
|
|
718
|
-
vars["service_name"] = service->name();
|
|
719
|
-
printer->Print(vars, "$service_name$$client_type$ {\n");
|
|
720
|
-
printer->Indent();
|
|
721
|
-
printer->Print(
|
|
722
|
-
"constructor (hostname: string,\n"
|
|
723
|
-
" credentials?: null | { [index: string]: string; },\n"
|
|
724
|
-
" options?: null | { [index: string]: any; });\n\n");
|
|
725
|
-
for (int method_index = 0; method_index < service->method_count();
|
|
726
|
-
++method_index) {
|
|
727
|
-
const MethodDescriptor* method = service->method(method_index);
|
|
728
|
-
vars["js_method_name"] = LowercaseFirstLetter(method->name());
|
|
729
|
-
vars["input_type"] = JSMessageType(method->input_type());
|
|
730
|
-
vars["output_type"] = JSMessageType(method->output_type());
|
|
731
|
-
if (!method->client_streaming()) {
|
|
732
|
-
if (method->server_streaming()) {
|
|
733
|
-
printer->Print(vars, "$js_method_name$(\n");
|
|
734
|
-
printer->Indent();
|
|
735
|
-
printer->Print(vars,
|
|
736
|
-
"request: $input_type$,\n"
|
|
737
|
-
"metadata?: grpcWeb.Metadata\n");
|
|
738
|
-
printer->Outdent();
|
|
739
|
-
printer->Print(vars,
|
|
740
|
-
"): grpcWeb.ClientReadableStream<$output_type$>;\n\n");
|
|
741
|
-
} else {
|
|
742
|
-
if (vars["client_type"] == "PromiseClient") {
|
|
743
|
-
printer->Print(vars, "$js_method_name$(\n");
|
|
744
|
-
printer->Indent();
|
|
745
|
-
printer->Print(vars,
|
|
746
|
-
"request: $input_type$,\n"
|
|
747
|
-
"metadata?: grpcWeb.Metadata\n");
|
|
748
|
-
printer->Outdent();
|
|
749
|
-
printer->Print(vars, "): $promise$<$output_type$>;\n\n");
|
|
750
|
-
} else {
|
|
751
|
-
printer->Print(vars, "$js_method_name$(\n");
|
|
752
|
-
printer->Indent();
|
|
753
|
-
printer->Print(vars,
|
|
754
|
-
"request: $input_type$,\n"
|
|
755
|
-
"metadata: grpcWeb.Metadata | undefined,\n"
|
|
756
|
-
"callback: (err: grpcWeb.RpcError,\n"
|
|
757
|
-
" response: $output_type$) => void\n");
|
|
758
|
-
printer->Outdent();
|
|
759
|
-
printer->Print(vars,
|
|
760
|
-
"): grpcWeb.ClientReadableStream<$output_type$>;");
|
|
761
|
-
printer->Print("\n\n");
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
printer->Outdent();
|
|
767
|
-
printer->Print("}\n\n");
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) {
|
|
772
|
-
PrintES6Imports(printer, file);
|
|
773
|
-
PrintGrpcWebDtsClientClass(printer, file, "Client");
|
|
774
|
-
PrintGrpcWebDtsClientClass(printer, file, "PromiseClient");
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
void PrintProtoDtsEnum(Printer* printer, const EnumDescriptor* desc) {
|
|
778
|
-
std::map<string, string> vars;
|
|
779
|
-
vars["enum_name"] = desc->name();
|
|
780
|
-
|
|
781
|
-
printer->Print(vars, "export enum $enum_name$ { \n");
|
|
782
|
-
printer->Indent();
|
|
783
|
-
for (int i = 0; i < desc->value_count(); i++) {
|
|
784
|
-
vars["value_name"] = Uppercase(desc->value(i)->name());
|
|
785
|
-
vars["value_number"] = std::to_string(desc->value(i)->number());
|
|
786
|
-
printer->Print(vars, "$value_name$ = $value_number$,\n");
|
|
787
|
-
}
|
|
788
|
-
printer->Outdent();
|
|
789
|
-
printer->Print("}\n");
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
void PrintProtoDtsOneofCase(Printer* printer, const OneofDescriptor* desc) {
|
|
793
|
-
std::map<string, string> vars;
|
|
794
|
-
vars["oneof_name"] = ToUpperCamel(ParseLowerUnderscore(desc->name()));
|
|
795
|
-
vars["oneof_name_upper"] = Uppercase(desc->name());
|
|
796
|
-
|
|
797
|
-
printer->Print(vars, "export enum $oneof_name$Case { \n");
|
|
798
|
-
printer->Indent();
|
|
799
|
-
printer->Print(vars, "$oneof_name_upper$_NOT_SET = 0,\n");
|
|
800
|
-
for (int i = 0; i < desc->field_count(); i++) {
|
|
801
|
-
const FieldDescriptor* field = desc->field(i);
|
|
802
|
-
vars["field_name"] = Uppercase(field->name());
|
|
803
|
-
vars["field_number"] = std::to_string(field->number());
|
|
804
|
-
printer->Print(vars, "$field_name$ = $field_number$,\n");
|
|
805
|
-
}
|
|
806
|
-
printer->Outdent();
|
|
807
|
-
printer->Print("}\n");
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc,
|
|
811
|
-
const FileDescriptor* file) {
|
|
812
|
-
const string& class_name = desc->name();
|
|
813
|
-
std::map<string, string> vars;
|
|
814
|
-
vars["class_name"] = class_name;
|
|
815
|
-
|
|
816
|
-
printer->Print(vars, "export class $class_name$ extends jspb.Message {\n");
|
|
817
|
-
printer->Indent();
|
|
818
|
-
for (int i = 0; i < desc->field_count(); i++) {
|
|
819
|
-
const FieldDescriptor* field = desc->field(i);
|
|
820
|
-
vars["js_field_name"] = JSFieldName(field);
|
|
821
|
-
vars["js_field_type"] = JSFieldType(field, file);
|
|
822
|
-
if (field->type() != FieldDescriptor::TYPE_MESSAGE ||
|
|
823
|
-
field->is_repeated()) {
|
|
824
|
-
printer->Print(vars, "get$js_field_name$(): $js_field_type$;\n");
|
|
825
|
-
} else {
|
|
826
|
-
printer->Print(vars,
|
|
827
|
-
"get$js_field_name$(): $js_field_type$ | undefined;\n");
|
|
828
|
-
}
|
|
829
|
-
if (field->type() == FieldDescriptor::TYPE_BYTES && !field->is_repeated()) {
|
|
830
|
-
printer->Print(vars,
|
|
831
|
-
"get$js_field_name$_asU8(): Uint8Array;\n"
|
|
832
|
-
"get$js_field_name$_asB64(): string;\n");
|
|
833
|
-
}
|
|
834
|
-
if (!field->is_map() && (field->type() != FieldDescriptor::TYPE_MESSAGE ||
|
|
835
|
-
field->is_repeated())) {
|
|
836
|
-
printer->Print(vars,
|
|
837
|
-
"set$js_field_name$(value: $js_field_type$): "
|
|
838
|
-
"$class_name$;\n");
|
|
839
|
-
} else if (!field->is_map()) {
|
|
840
|
-
printer->Print(vars,
|
|
841
|
-
"set$js_field_name$(value?: $js_field_type$): "
|
|
842
|
-
"$class_name$;\n");
|
|
843
|
-
}
|
|
844
|
-
if (field->has_optional_keyword() ||
|
|
845
|
-
(field->type() == FieldDescriptor::TYPE_MESSAGE &&
|
|
846
|
-
!field->is_repeated() && !field->is_map())) {
|
|
847
|
-
printer->Print(vars, "has$js_field_name$(): boolean;\n");
|
|
848
|
-
}
|
|
849
|
-
if (field->type() == FieldDescriptor::TYPE_MESSAGE || field->has_optional_keyword() ||
|
|
850
|
-
field->is_repeated() || field->is_map()) {
|
|
851
|
-
printer->Print(vars, "clear$js_field_name$(): $class_name$;\n");
|
|
852
|
-
}
|
|
853
|
-
if (field->is_repeated() && !field->is_map()) {
|
|
854
|
-
vars["js_field_name"] = JSElementName(field);
|
|
855
|
-
vars["js_field_type"] = JSElementType(field, file);
|
|
856
|
-
if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
|
|
857
|
-
printer->Print(vars,
|
|
858
|
-
"add$js_field_name$(value: $js_field_type$, "
|
|
859
|
-
"index?: number): $class_name$;\n");
|
|
860
|
-
} else {
|
|
861
|
-
printer->Print(vars,
|
|
862
|
-
"add$js_field_name$(value?: $js_field_type$, "
|
|
863
|
-
"index?: number): $js_field_type$;\n");
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
printer->Print("\n");
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
for (int i = 0; i < desc->oneof_decl_count(); i++) {
|
|
871
|
-
const OneofDescriptor* oneof = desc->oneof_decl(i);
|
|
872
|
-
if (!oneof->is_synthetic()) {
|
|
873
|
-
vars["js_oneof_name"] = ToUpperCamel(ParseLowerUnderscore(oneof->name()));
|
|
874
|
-
printer->Print(
|
|
875
|
-
vars, "get$js_oneof_name$Case(): $class_name$.$js_oneof_name$Case;\n");
|
|
876
|
-
printer->Print("\n");
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
printer->Print(
|
|
881
|
-
vars,
|
|
882
|
-
"serializeBinary(): Uint8Array;\n"
|
|
883
|
-
"toObject(includeInstance?: boolean): "
|
|
884
|
-
"$class_name$.AsObject;\n"
|
|
885
|
-
"static toObject(includeInstance: boolean, msg: $class_name$): "
|
|
886
|
-
"$class_name$.AsObject;\n"
|
|
887
|
-
"static serializeBinaryToWriter(message: $class_name$, writer: "
|
|
888
|
-
"jspb.BinaryWriter): void;\n"
|
|
889
|
-
"static deserializeBinary(bytes: Uint8Array): $class_name$;\n"
|
|
890
|
-
"static deserializeBinaryFromReader(message: $class_name$, reader: "
|
|
891
|
-
"jspb.BinaryReader): $class_name$;\n");
|
|
892
|
-
printer->Outdent();
|
|
893
|
-
printer->Print("}\n\n");
|
|
894
|
-
|
|
895
|
-
printer->Print(vars, "export namespace $class_name$ {\n");
|
|
896
|
-
printer->Indent();
|
|
897
|
-
printer->Print("export type AsObject = {\n");
|
|
898
|
-
printer->Indent();
|
|
899
|
-
for (int i = 0; i < desc->field_count(); i++) {
|
|
900
|
-
const FieldDescriptor* field = desc->field(i);
|
|
901
|
-
string js_field_name = CamelCaseJSFieldName(field);
|
|
902
|
-
if (IsReserved(js_field_name)) {
|
|
903
|
-
js_field_name = "pb_" + js_field_name;
|
|
904
|
-
}
|
|
905
|
-
vars["js_field_name"] = js_field_name;
|
|
906
|
-
vars["js_field_type"] = AsObjectFieldType(field, file);
|
|
907
|
-
if ((field->type() != FieldDescriptor::TYPE_MESSAGE && !field->has_optional_keyword()) ||
|
|
908
|
-
field->is_repeated()) {
|
|
909
|
-
printer->Print(vars, "$js_field_name$: $js_field_type$,\n");
|
|
910
|
-
} else {
|
|
911
|
-
printer->Print(vars, "$js_field_name$?: $js_field_type$,\n");
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
printer->Outdent();
|
|
915
|
-
printer->Print("}\n");
|
|
916
|
-
|
|
917
|
-
for (int i = 0; i < desc->nested_type_count(); i++) {
|
|
918
|
-
if (desc->nested_type(i)->options().map_entry()) {
|
|
919
|
-
continue;
|
|
920
|
-
}
|
|
921
|
-
printer->Print("\n");
|
|
922
|
-
PrintProtoDtsMessage(printer, desc->nested_type(i), file);
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
for (int i = 0; i < desc->enum_type_count(); i++) {
|
|
926
|
-
printer->Print("\n");
|
|
927
|
-
PrintProtoDtsEnum(printer, desc->enum_type(i));
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
for (int i = 0; i < desc->oneof_decl_count(); i++) {
|
|
931
|
-
printer->Print("\n");
|
|
932
|
-
PrintProtoDtsOneofCase(printer, desc->oneof_decl(i));
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
printer->Outdent();
|
|
936
|
-
printer->Print("}\n\n");
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
void PrintProtoDtsFile(Printer* printer, const FileDescriptor* file) {
|
|
940
|
-
printer->Print("import * as jspb from 'google-protobuf'\n\n");
|
|
941
|
-
|
|
942
|
-
for (int i = 0; i < file->dependency_count(); i++) {
|
|
943
|
-
const string& name = file->dependency(i)->name();
|
|
944
|
-
// We need to give each cross-file import an alias.
|
|
945
|
-
printer->Print("import * as $alias$ from '$dep_filename$_pb';\n", "alias",
|
|
946
|
-
ModuleAlias(name), "dep_filename",
|
|
947
|
-
GetRootPath(file->name(), name) + StripProto(name));
|
|
948
|
-
}
|
|
949
|
-
printer->Print("\n\n");
|
|
950
|
-
|
|
951
|
-
for (int i = 0; i < file->message_type_count(); i++) {
|
|
952
|
-
PrintProtoDtsMessage(printer, file->message_type(i), file);
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
for (int i = 0; i < file->enum_type_count(); i++) {
|
|
956
|
-
PrintProtoDtsEnum(printer, file->enum_type(i));
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
|
|
960
|
-
void PrintFileHeader(Printer* printer, const std::map<string, string>& vars) {
|
|
961
|
-
printer->Print(
|
|
962
|
-
vars,
|
|
963
|
-
"/**\n"
|
|
964
|
-
" * @fileoverview gRPC-Web generated client stub for $package$\n"
|
|
965
|
-
" * @enhanceable\n"
|
|
966
|
-
" * @public\n"
|
|
967
|
-
" */\n\n"
|
|
968
|
-
"// Code generated by protoc-gen-grpc-web. DO NOT EDIT.\n"
|
|
969
|
-
"// versions:\n"
|
|
970
|
-
"// \tprotoc-gen-grpc-web v$version$\n"
|
|
971
|
-
"// \tprotoc v$protoc_version$\n"
|
|
972
|
-
"// source: $source_file$\n\n\n"
|
|
973
|
-
"/* eslint-disable */\n"
|
|
974
|
-
"// @ts-nocheck\n\n\n");
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
void PrintMethodDescriptorFile(Printer* printer,
|
|
978
|
-
std::map<string, string> vars) {
|
|
979
|
-
printer->Print(
|
|
980
|
-
vars,
|
|
981
|
-
"/**\n"
|
|
982
|
-
" * @fileoverview gRPC-Web generated MethodDescriptors for $package$\n");
|
|
983
|
-
if (vars["plugins"].empty()) {
|
|
984
|
-
printer->Print(" * @enhanceable\n");
|
|
985
|
-
}
|
|
986
|
-
printer->Print(
|
|
987
|
-
" * @public\n"
|
|
988
|
-
" */\n\n"
|
|
989
|
-
"// Code generated by protoc-gen-grpc-web. DO NOT EDIT.\n"
|
|
990
|
-
"// versions:\n"
|
|
991
|
-
"// \tprotoc-gen-grpc-web v$version$\n"
|
|
992
|
-
"// \tprotoc v$protoc_version$\n"
|
|
993
|
-
"// source: $source_file$\n\n\n"
|
|
994
|
-
"/* eslint-disable */\n"
|
|
995
|
-
"// @ts-nocheck\n\n\n");
|
|
996
|
-
|
|
997
|
-
printer->Print(vars,
|
|
998
|
-
"goog.provide('proto.$package_dot$$class_name$.$"
|
|
999
|
-
"method_name$MethodDescriptor');\n\n");
|
|
1000
|
-
if (!vars["plugins"].empty()) {
|
|
1001
|
-
printer->Print(vars,
|
|
1002
|
-
"goog.require('$plugins$.$package_dot$$class_name$.$"
|
|
1003
|
-
"method_name$MethodDescriptor');\n");
|
|
1004
|
-
}
|
|
1005
|
-
printer->Print(vars, "goog.require('grpc.web.MethodDescriptor');\n");
|
|
1006
|
-
printer->Print(vars, "goog.require('grpc.web.MethodType');\n");
|
|
1007
|
-
printer->Print(vars, "goog.require('$in_type$');\n");
|
|
1008
|
-
if (vars["out_type"] != vars["in_type"]) {
|
|
1009
|
-
printer->Print(vars, "goog.require('$out_type$');\n");
|
|
1010
|
-
}
|
|
1011
|
-
printer->Print(vars, "\n\ngoog.scope(function() {\n\n");
|
|
1012
|
-
|
|
1013
|
-
printer->Print(
|
|
1014
|
-
vars,
|
|
1015
|
-
"/**\n"
|
|
1016
|
-
" * @const\n"
|
|
1017
|
-
" * @type {!grpc.web.MethodDescriptor<\n"
|
|
1018
|
-
" * !proto.$in$,\n"
|
|
1019
|
-
" * !proto.$out$>}\n"
|
|
1020
|
-
" */\n"
|
|
1021
|
-
"proto.$package_dot$$class_name$.$method_name$MethodDescriptor = \n");
|
|
1022
|
-
printer->Indent();
|
|
1023
|
-
printer->Indent();
|
|
1024
|
-
printer->Print(vars, "new grpc.web.MethodDescriptor(\n");
|
|
1025
|
-
printer->Indent();
|
|
1026
|
-
printer->Indent();
|
|
1027
|
-
printer->Print(vars,
|
|
1028
|
-
"'/$package_dot$$service_name$/$method_name$',\n"
|
|
1029
|
-
"$method_type$,\n"
|
|
1030
|
-
"$in_type$,\n");
|
|
1031
|
-
printer->Print(vars,
|
|
1032
|
-
"$out_type$,\n"
|
|
1033
|
-
"/**\n"
|
|
1034
|
-
" * @param {!proto.$in$} request\n");
|
|
1035
|
-
printer->Print(
|
|
1036
|
-
(" * @return {" + GetSerializeMethodReturnType(vars) + "}\n").c_str());
|
|
1037
|
-
printer->Print(
|
|
1038
|
-
" */\n"
|
|
1039
|
-
"function(request) {\n");
|
|
1040
|
-
printer->Print(
|
|
1041
|
-
(" return request." + GetSerializeMethodName(vars) + "();\n").c_str());
|
|
1042
|
-
printer->Print("},\n");
|
|
1043
|
-
printer->Print(vars,
|
|
1044
|
-
("$out_type$." + GetDeserializeMethodName(vars)).c_str());
|
|
1045
|
-
printer->Print(vars, ");\n\n\n");
|
|
1046
|
-
printer->Outdent();
|
|
1047
|
-
printer->Outdent();
|
|
1048
|
-
printer->Outdent();
|
|
1049
|
-
printer->Outdent();
|
|
1050
|
-
printer->Print("}); // goog.scope\n\n");
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
void PrintServiceConstructor(Printer* printer, std::map<string, string> vars,
|
|
1054
|
-
bool is_promise) {
|
|
1055
|
-
vars["is_promise"] = is_promise ? "Promise" : "";
|
|
1056
|
-
printer->Print(vars,
|
|
1057
|
-
"/**\n"
|
|
1058
|
-
" * @param {string} hostname\n"
|
|
1059
|
-
" * @param {?Object} credentials\n"
|
|
1060
|
-
" * @param {?grpc.web.ClientOptions} options\n"
|
|
1061
|
-
" * @constructor\n"
|
|
1062
|
-
" * @struct\n"
|
|
1063
|
-
" * @final\n"
|
|
1064
|
-
" */\n"
|
|
1065
|
-
"proto.$package_dot$$service_name$$is_promise$Client =\n"
|
|
1066
|
-
" function(hostname, credentials, options) {\n"
|
|
1067
|
-
" if (!options) options = {};\n");
|
|
1068
|
-
if (vars["mode"] == GetModeVar(Mode::GRPCWEB)) {
|
|
1069
|
-
printer->Print(vars, " options.format = '$format$';\n\n");
|
|
1070
|
-
}
|
|
1071
|
-
if (vars["mode"] == GetModeVar(Mode::OP)) {
|
|
1072
|
-
printer->Print(
|
|
1073
|
-
vars,
|
|
1074
|
-
" /**\n"
|
|
1075
|
-
" * @private @const {!grpc.web.$mode$ClientBase} The client\n"
|
|
1076
|
-
" */\n"
|
|
1077
|
-
" this.client_ = new grpc.web.$mode$ClientBase(options, "
|
|
1078
|
-
"$binary$);\n\n");
|
|
1079
|
-
} else {
|
|
1080
|
-
printer->Print(
|
|
1081
|
-
vars,
|
|
1082
|
-
" /**\n"
|
|
1083
|
-
" * @private @const {!grpc.web.$mode$ClientBase} The client\n"
|
|
1084
|
-
" */\n"
|
|
1085
|
-
" this.client_ = new grpc.web.$mode$ClientBase(options);\n\n");
|
|
1086
|
-
}
|
|
1087
|
-
printer->PrintRaw(
|
|
1088
|
-
" /**\n"
|
|
1089
|
-
" * @private @const {string} The hostname\n"
|
|
1090
|
-
" */\n"
|
|
1091
|
-
" this.hostname_ = hostname.replace(/\\/+$/, '');\n\n"
|
|
1092
|
-
"};\n\n\n");
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
void PrintMethodDescriptor(Printer* printer, std::map<string, string> vars) {
|
|
1096
|
-
printer->Print(vars,
|
|
1097
|
-
"/**\n"
|
|
1098
|
-
" * @const\n"
|
|
1099
|
-
" * @type {!grpc.web.MethodDescriptor<\n"
|
|
1100
|
-
" * !proto.$in$,\n"
|
|
1101
|
-
" * !proto.$out$>}\n"
|
|
1102
|
-
" */\n"
|
|
1103
|
-
"const methodDescriptor_$service_name$_$method_name$ = "
|
|
1104
|
-
"new grpc.web.MethodDescriptor(\n");
|
|
1105
|
-
printer->Indent();
|
|
1106
|
-
printer->Print(vars,
|
|
1107
|
-
"'/$package_dot$$service_name$/$method_name$',\n"
|
|
1108
|
-
"$method_type$,\n"
|
|
1109
|
-
"$in_type$,\n");
|
|
1110
|
-
printer->Print(vars,
|
|
1111
|
-
"$out_type$,\n"
|
|
1112
|
-
"/**\n"
|
|
1113
|
-
" * @param {!proto.$in$} request\n");
|
|
1114
|
-
printer->Print(
|
|
1115
|
-
(" * @return {" + GetSerializeMethodReturnType(vars) + "}\n").c_str());
|
|
1116
|
-
printer->Print(
|
|
1117
|
-
" */\n"
|
|
1118
|
-
"function(request) {\n");
|
|
1119
|
-
printer->Print(
|
|
1120
|
-
(" return request." + GetSerializeMethodName(vars) + "();\n").c_str());
|
|
1121
|
-
printer->Print("},\n");
|
|
1122
|
-
printer->Print(
|
|
1123
|
-
vars, ("$out_type$." + GetDeserializeMethodName(vars) + "\n").c_str());
|
|
1124
|
-
printer->Outdent();
|
|
1125
|
-
printer->Print(vars, ");\n\n\n");
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
void PrintUnaryCall(Printer* printer, std::map<string, string> vars) {
|
|
1129
|
-
printer->Print(
|
|
1130
|
-
vars,
|
|
1131
|
-
"/**\n"
|
|
1132
|
-
" * @param {!proto.$in$} request The\n"
|
|
1133
|
-
" * request proto\n"
|
|
1134
|
-
" * @param {?Object<string, string>} metadata User defined\n"
|
|
1135
|
-
" * call metadata\n"
|
|
1136
|
-
" * @param {function(?grpc.web.RpcError,"
|
|
1137
|
-
" ?proto.$out$)}\n"
|
|
1138
|
-
" * callback The callback function(error, response)\n"
|
|
1139
|
-
" * @return {!grpc.web.ClientReadableStream<!proto.$out$>|undefined}\n"
|
|
1140
|
-
" * The XHR Node Readable Stream\n"
|
|
1141
|
-
" */\n"
|
|
1142
|
-
"proto.$package_dot$$service_name$Client.prototype.$js_method_name$ =\n");
|
|
1143
|
-
printer->Indent();
|
|
1144
|
-
printer->Print(vars,
|
|
1145
|
-
" function(request, metadata, callback) {\n"
|
|
1146
|
-
"return this.client_.rpcCall(this.hostname_ +\n");
|
|
1147
|
-
printer->Indent();
|
|
1148
|
-
printer->Indent();
|
|
1149
|
-
if (vars["mode"] == GetModeVar(Mode::OP)) {
|
|
1150
|
-
printer->Print(vars,
|
|
1151
|
-
"'/$$rpc/$package_dot$$service_name$/$method_name$',\n");
|
|
1152
|
-
} else {
|
|
1153
|
-
printer->Print(vars, "'/$package_dot$$service_name$/$method_name$',\n");
|
|
1154
|
-
}
|
|
1155
|
-
printer->Print(vars,
|
|
1156
|
-
"request,\n"
|
|
1157
|
-
"metadata || {},\n"
|
|
1158
|
-
"$method_descriptor$,\n"
|
|
1159
|
-
"callback);\n");
|
|
1160
|
-
printer->Outdent();
|
|
1161
|
-
printer->Outdent();
|
|
1162
|
-
printer->Outdent();
|
|
1163
|
-
printer->Print("};\n\n\n");
|
|
1164
|
-
}
|
|
1165
|
-
|
|
1166
|
-
void PrintPromiseUnaryCall(Printer* printer, std::map<string, string> vars) {
|
|
1167
|
-
printer->Print(vars,
|
|
1168
|
-
"/**\n"
|
|
1169
|
-
" * @param {!proto.$in$} request The\n"
|
|
1170
|
-
" * request proto\n"
|
|
1171
|
-
" * @param {?Object<string, string>=} metadata User defined\n"
|
|
1172
|
-
" * call metadata\n"
|
|
1173
|
-
" * @return {!$promise$<!proto.$out$>}\n"
|
|
1174
|
-
" * Promise that resolves to the response\n"
|
|
1175
|
-
" */\n"
|
|
1176
|
-
"proto.$package_dot$$service_name$PromiseClient.prototype"
|
|
1177
|
-
".$js_method_name$ =\n");
|
|
1178
|
-
printer->Indent();
|
|
1179
|
-
printer->Print(vars,
|
|
1180
|
-
" function(request, metadata) {\n"
|
|
1181
|
-
"return this.client_.unaryCall(this.hostname_ +\n");
|
|
1182
|
-
printer->Indent();
|
|
1183
|
-
printer->Indent();
|
|
1184
|
-
if (vars["mode"] == GetModeVar(Mode::OP)) {
|
|
1185
|
-
printer->Print(vars,
|
|
1186
|
-
"'/$$rpc/$package_dot$$service_name$/$method_name$',\n");
|
|
1187
|
-
} else {
|
|
1188
|
-
printer->Print(vars, "'/$package_dot$$service_name$/$method_name$',\n");
|
|
1189
|
-
}
|
|
1190
|
-
printer->Print(vars,
|
|
1191
|
-
"request,\n"
|
|
1192
|
-
"metadata || {},\n"
|
|
1193
|
-
"$method_descriptor$);\n");
|
|
1194
|
-
printer->Outdent();
|
|
1195
|
-
printer->Outdent();
|
|
1196
|
-
printer->Outdent();
|
|
1197
|
-
printer->Print("};\n\n\n");
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
|
-
void PrintServerStreamingCall(Printer* printer, std::map<string, string> vars) {
|
|
1201
|
-
printer->Print(vars,
|
|
1202
|
-
"/**\n"
|
|
1203
|
-
" * @param {!proto.$in$} request The request proto\n"
|
|
1204
|
-
" * @param {?Object<string, string>=} metadata User defined\n"
|
|
1205
|
-
" * call metadata\n"
|
|
1206
|
-
" * @return {!grpc.web.ClientReadableStream<!proto.$out$>}\n"
|
|
1207
|
-
" * The XHR Node Readable Stream\n"
|
|
1208
|
-
" */\n"
|
|
1209
|
-
"proto.$package_dot$$service_name$$client_type$.prototype."
|
|
1210
|
-
"$js_method_name$ =\n");
|
|
1211
|
-
printer->Indent();
|
|
1212
|
-
printer->Print(
|
|
1213
|
-
" function(request, metadata) {\n"
|
|
1214
|
-
"return this.client_.serverStreaming(this.hostname_ +\n");
|
|
1215
|
-
printer->Indent();
|
|
1216
|
-
printer->Indent();
|
|
1217
|
-
if (vars["mode"] == GetModeVar(Mode::OP)) {
|
|
1218
|
-
printer->Print(vars,
|
|
1219
|
-
"'/$$rpc/$package_dot$$service_name$/$method_name$',\n");
|
|
1220
|
-
} else {
|
|
1221
|
-
printer->Print(vars, "'/$package_dot$$service_name$/$method_name$',\n");
|
|
1222
|
-
}
|
|
1223
|
-
printer->Print(vars,
|
|
1224
|
-
"request,\n"
|
|
1225
|
-
"metadata || {},\n"
|
|
1226
|
-
"$method_descriptor$);\n");
|
|
1227
|
-
printer->Outdent();
|
|
1228
|
-
printer->Outdent();
|
|
1229
|
-
printer->Outdent();
|
|
1230
|
-
printer->Print("};\n\n\n");
|
|
1231
|
-
}
|
|
1232
|
-
|
|
1233
|
-
void PrintMultipleFilesMode(const FileDescriptor* file, string file_name,
|
|
1234
|
-
GeneratorContext* context,
|
|
1235
|
-
std::map<string, string> vars) {
|
|
1236
|
-
std::map<string, string> method_descriptors;
|
|
1237
|
-
bool has_server_streaming = false;
|
|
1238
|
-
|
|
1239
|
-
// Print MethodDescriptor files.
|
|
1240
|
-
for (int i = 0; i < file->service_count(); ++i) {
|
|
1241
|
-
const ServiceDescriptor* service = file->service(i);
|
|
1242
|
-
vars["service_name"] = service->name();
|
|
1243
|
-
vars["class_name"] = LowercaseFirstLetter(service->name());
|
|
1244
|
-
|
|
1245
|
-
for (int method_index = 0; method_index < service->method_count();
|
|
1246
|
-
++method_index) {
|
|
1247
|
-
const MethodDescriptor* method = service->method(method_index);
|
|
1248
|
-
string method_file_name = Lowercase(service->name()) + "_" +
|
|
1249
|
-
Lowercase(method->name()) +
|
|
1250
|
-
"_methoddescriptor.js";
|
|
1251
|
-
if (method->server_streaming()) {
|
|
1252
|
-
has_server_streaming = true;
|
|
1253
|
-
}
|
|
1254
|
-
std::unique_ptr<ZeroCopyOutputStream> output(
|
|
1255
|
-
context->Open(method_file_name));
|
|
1256
|
-
Printer printer(output.get(), '$');
|
|
1257
|
-
|
|
1258
|
-
vars["method_name"] = method->name();
|
|
1259
|
-
vars["in"] = method->input_type()->full_name();
|
|
1260
|
-
vars["in_type"] = "proto." + method->input_type()->full_name();
|
|
1261
|
-
vars["out"] = method->output_type()->full_name();
|
|
1262
|
-
vars["out_type"] = "proto." + method->output_type()->full_name();
|
|
1263
|
-
vars["method_type"] = method->server_streaming()
|
|
1264
|
-
? "grpc.web.MethodType.SERVER_STREAMING"
|
|
1265
|
-
: "grpc.web.MethodType.UNARY";
|
|
1266
|
-
|
|
1267
|
-
PrintMethodDescriptorFile(&printer, vars);
|
|
1268
|
-
method_descriptors[service->name() + "." + method->name()] =
|
|
1269
|
-
"proto." + vars["package_dot"] + vars["class_name"] + "." +
|
|
1270
|
-
vars["method_name"] + "MethodDescriptor";
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
|
|
1274
|
-
std::unique_ptr<ZeroCopyOutputStream> output1(
|
|
1275
|
-
context->Open(file_name + "_client_pb.js"));
|
|
1276
|
-
Printer printer1(output1.get(), '$');
|
|
1277
|
-
std::unique_ptr<ZeroCopyOutputStream> output2(
|
|
1278
|
-
context->Open(file_name + "_promise_client_pb.js"));
|
|
1279
|
-
Printer printer2(output2.get(), '$');
|
|
1280
|
-
|
|
1281
|
-
PrintFileHeader(&printer1, vars);
|
|
1282
|
-
PrintFileHeader(&printer2, vars);
|
|
1283
|
-
|
|
1284
|
-
// Print the Promise and callback client.
|
|
1285
|
-
for (int i = 0; i < file->service_count(); ++i) {
|
|
1286
|
-
const ServiceDescriptor* service = file->service(i);
|
|
1287
|
-
vars["service_name"] = service->name();
|
|
1288
|
-
printer1.Print(vars,
|
|
1289
|
-
"goog.provide('proto.$package_dot$$service_name$"
|
|
1290
|
-
"Client');\n\n");
|
|
1291
|
-
printer2.Print(vars,
|
|
1292
|
-
"goog.provide('proto.$package_dot$$service_name$"
|
|
1293
|
-
"PromiseClient');\n\n");
|
|
1294
|
-
}
|
|
1295
|
-
|
|
1296
|
-
if (vars["promise"] == GRPC_PROMISE) {
|
|
1297
|
-
printer2.Print(vars, "goog.require('grpc.web.promise');\n");
|
|
1298
|
-
}
|
|
1299
|
-
std::map<string, string>::iterator it;
|
|
1300
|
-
for (it = method_descriptors.begin(); it != method_descriptors.end(); it++) {
|
|
1301
|
-
vars["import_mtd"] = it->second;
|
|
1302
|
-
printer1.Print(vars, "goog.require('$import_mtd$');\n");
|
|
1303
|
-
printer2.Print(vars, "goog.require('$import_mtd$');\n");
|
|
1304
|
-
}
|
|
1305
|
-
printer1.Print(vars, "goog.require('grpc.web.$mode$ClientBase');\n");
|
|
1306
|
-
printer1.Print(vars, "goog.require('grpc.web.ClientReadableStream');\n");
|
|
1307
|
-
printer1.Print(vars, "goog.require('grpc.web.RpcError');\n");
|
|
1308
|
-
printer2.Print(vars, "goog.require('grpc.web.$mode$ClientBase');\n");
|
|
1309
|
-
if (has_server_streaming) {
|
|
1310
|
-
printer2.Print(vars, "goog.require('grpc.web.ClientReadableStream');\n");
|
|
1311
|
-
}
|
|
1312
|
-
|
|
1313
|
-
PrintClosureDependencies(&printer1, file);
|
|
1314
|
-
PrintClosureDependencies(&printer2, file);
|
|
1315
|
-
|
|
1316
|
-
printer1.Print(vars, "\ngoog.requireType('grpc.web.ClientOptions');\n");
|
|
1317
|
-
printer2.Print(vars, "\ngoog.requireType('grpc.web.ClientOptions');\n");
|
|
1318
|
-
|
|
1319
|
-
printer1.Print("\n\n\n");
|
|
1320
|
-
printer2.Print("\n\n\n");
|
|
1321
|
-
printer1.Print("goog.scope(function() {\n\n");
|
|
1322
|
-
printer2.Print("goog.scope(function() {\n\n");
|
|
1323
|
-
|
|
1324
|
-
for (int service_index = 0; service_index < file->service_count();
|
|
1325
|
-
++service_index) {
|
|
1326
|
-
const ServiceDescriptor* service = file->service(service_index);
|
|
1327
|
-
vars["service_name"] = service->name();
|
|
1328
|
-
PrintServiceConstructor(&printer1, vars, false);
|
|
1329
|
-
PrintServiceConstructor(&printer2, vars, true);
|
|
1330
|
-
|
|
1331
|
-
for (int method_index = 0; method_index < service->method_count();
|
|
1332
|
-
++method_index) {
|
|
1333
|
-
const MethodDescriptor* method = service->method(method_index);
|
|
1334
|
-
const Descriptor* input_type = method->input_type();
|
|
1335
|
-
const Descriptor* output_type = method->output_type();
|
|
1336
|
-
vars["js_method_name"] = LowercaseFirstLetter(method->name());
|
|
1337
|
-
vars["method_name"] = method->name();
|
|
1338
|
-
vars["in"] = input_type->full_name();
|
|
1339
|
-
vars["out"] = output_type->full_name();
|
|
1340
|
-
vars["method_descriptor"] =
|
|
1341
|
-
method_descriptors[service->name() + "." + method->name()];
|
|
1342
|
-
vars["in_type"] = "proto." + input_type->full_name();
|
|
1343
|
-
vars["out_type"] = "proto." + output_type->full_name();
|
|
1344
|
-
|
|
1345
|
-
// Client streaming is not supported yet
|
|
1346
|
-
if (!method->client_streaming()) {
|
|
1347
|
-
if (method->server_streaming()) {
|
|
1348
|
-
vars["method_type"] = "grpc.web.MethodType.SERVER_STREAMING";
|
|
1349
|
-
vars["client_type"] = "Client";
|
|
1350
|
-
PrintServerStreamingCall(&printer1, vars);
|
|
1351
|
-
vars["client_type"] = "PromiseClient";
|
|
1352
|
-
PrintServerStreamingCall(&printer2, vars);
|
|
1353
|
-
} else {
|
|
1354
|
-
vars["method_type"] = "grpc.web.MethodType.UNARY";
|
|
1355
|
-
PrintUnaryCall(&printer1, vars);
|
|
1356
|
-
PrintPromiseUnaryCall(&printer2, vars);
|
|
1357
|
-
}
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
printer1.Print("}); // goog.scope\n\n");
|
|
1362
|
-
printer2.Print("}); // goog.scope\n\n");
|
|
1363
|
-
}
|
|
1364
|
-
|
|
1365
|
-
void PrintClosureES6Imports(Printer* printer, const FileDescriptor* file,
|
|
1366
|
-
string package_dot) {
|
|
1367
|
-
for (int i = 0; i < file->service_count(); ++i) {
|
|
1368
|
-
const ServiceDescriptor* service = file->service(i);
|
|
1369
|
-
|
|
1370
|
-
string service_namespace = "proto." + package_dot + service->name();
|
|
1371
|
-
printer->Print(
|
|
1372
|
-
"import $service_name$Client_import from 'goog:$namespace$';\n",
|
|
1373
|
-
"service_name", service->name(), "namespace",
|
|
1374
|
-
service_namespace + "Client");
|
|
1375
|
-
printer->Print(
|
|
1376
|
-
"import $service_name$PromiseClient_import from 'goog:$namespace$';\n",
|
|
1377
|
-
"service_name", service->name(), "namespace",
|
|
1378
|
-
service_namespace + "PromiseClient");
|
|
1379
|
-
}
|
|
1380
|
-
|
|
1381
|
-
printer->Print("\n\n\n");
|
|
1382
|
-
}
|
|
1383
|
-
|
|
1384
|
-
void PrintGrpcWebClosureES6File(Printer* printer, const FileDescriptor* file) {
|
|
1385
|
-
string package_dot = file->package().empty() ? "" : file->package() + ".";
|
|
1386
|
-
|
|
1387
|
-
printer->Print(
|
|
1388
|
-
"/**\n"
|
|
1389
|
-
" * @fileoverview gRPC-Web generated client stub for '$file$'\n"
|
|
1390
|
-
" */\n"
|
|
1391
|
-
"\n"
|
|
1392
|
-
"// Code generated by protoc-gen-grpc-web. DO NOT EDIT.\n"
|
|
1393
|
-
"// versions:\n"
|
|
1394
|
-
"// \tprotoc-gen-grpc-web v$version$\n"
|
|
1395
|
-
"// \tprotoc v$protoc_version$\n"
|
|
1396
|
-
"// source: $source_file$\n"
|
|
1397
|
-
"\n"
|
|
1398
|
-
"\n",
|
|
1399
|
-
"file", file->name());
|
|
1400
|
-
|
|
1401
|
-
PrintClosureES6Imports(printer, file, package_dot);
|
|
1402
|
-
|
|
1403
|
-
for (int i = 0; i < file->service_count(); ++i) {
|
|
1404
|
-
const ServiceDescriptor* service = file->service(i);
|
|
1405
|
-
|
|
1406
|
-
string service_namespace = "proto." + package_dot + service->name();
|
|
1407
|
-
printer->Print("export const $name$Client = $name$Client_import;\n", "name",
|
|
1408
|
-
service->name());
|
|
1409
|
-
printer->Print(
|
|
1410
|
-
"export const $name$PromiseClient = $name$PromiseClient_import;\n",
|
|
1411
|
-
"name", service->name());
|
|
1412
|
-
}
|
|
1413
|
-
}
|
|
1414
|
-
|
|
1415
|
-
class GeneratorOptions {
|
|
1416
|
-
public:
|
|
1417
|
-
GeneratorOptions();
|
|
1418
|
-
|
|
1419
|
-
bool ParseFromOptions(const string& parameter, string* error);
|
|
1420
|
-
bool ParseFromOptions(const std::vector<std::pair<string, string>>& options,
|
|
1421
|
-
string* error);
|
|
1422
|
-
|
|
1423
|
-
// Returns the name of the output file for |proto_file|.
|
|
1424
|
-
string OutputFile(const string& proto_file) const;
|
|
1425
|
-
|
|
1426
|
-
string mode() const { return mode_; }
|
|
1427
|
-
string plugins() const { return plugins_; }
|
|
1428
|
-
ImportStyle import_style() const { return import_style_; }
|
|
1429
|
-
bool generate_dts() const { return generate_dts_; }
|
|
1430
|
-
bool generate_closure_es6() const { return generate_closure_es6_; }
|
|
1431
|
-
bool multiple_files() const { return multiple_files_; }
|
|
1432
|
-
bool goog_promise() const { return goog_promise_; }
|
|
1433
|
-
|
|
1434
|
-
private:
|
|
1435
|
-
string file_name_;
|
|
1436
|
-
string mode_;
|
|
1437
|
-
string plugins_;
|
|
1438
|
-
ImportStyle import_style_;
|
|
1439
|
-
bool generate_dts_;
|
|
1440
|
-
bool generate_closure_es6_;
|
|
1441
|
-
bool multiple_files_;
|
|
1442
|
-
bool goog_promise_;
|
|
1443
|
-
};
|
|
1444
|
-
|
|
1445
|
-
GeneratorOptions::GeneratorOptions()
|
|
1446
|
-
: file_name_(""),
|
|
1447
|
-
mode_(""),
|
|
1448
|
-
plugins_(""),
|
|
1449
|
-
import_style_(ImportStyle::CLOSURE),
|
|
1450
|
-
generate_dts_(false),
|
|
1451
|
-
generate_closure_es6_(false),
|
|
1452
|
-
multiple_files_(false),
|
|
1453
|
-
goog_promise_(false) {}
|
|
1454
|
-
|
|
1455
|
-
bool GeneratorOptions::ParseFromOptions(const string& parameter,
|
|
1456
|
-
string* error) {
|
|
1457
|
-
std::vector<std::pair<string, string>> options;
|
|
1458
|
-
ParseGeneratorParameter(parameter, &options);
|
|
1459
|
-
return ParseFromOptions(options, error);
|
|
1460
|
-
}
|
|
1461
|
-
|
|
1462
|
-
bool GeneratorOptions::ParseFromOptions(
|
|
1463
|
-
const std::vector<std::pair<string, string>>& options, string* error) {
|
|
1464
|
-
for (const std::pair<string, string>& option : options) {
|
|
1465
|
-
if ("out" == option.first) {
|
|
1466
|
-
file_name_ = option.second;
|
|
1467
|
-
} else if ("mode" == option.first) {
|
|
1468
|
-
mode_ = option.second;
|
|
1469
|
-
} else if ("import_style" == option.first) {
|
|
1470
|
-
if ("closure" == option.second) {
|
|
1471
|
-
import_style_ = ImportStyle::CLOSURE;
|
|
1472
|
-
} else if ("experimental_closure_es6" == option.second) {
|
|
1473
|
-
import_style_ = ImportStyle::CLOSURE;
|
|
1474
|
-
generate_closure_es6_ = true;
|
|
1475
|
-
} else if ("commonjs" == option.second) {
|
|
1476
|
-
import_style_ = ImportStyle::COMMONJS;
|
|
1477
|
-
} else if ("commonjs+dts" == option.second) {
|
|
1478
|
-
import_style_ = ImportStyle::COMMONJS;
|
|
1479
|
-
generate_dts_ = true;
|
|
1480
|
-
} else if ("typescript" == option.second) {
|
|
1481
|
-
import_style_ = ImportStyle::TYPESCRIPT;
|
|
1482
|
-
generate_dts_ = true;
|
|
1483
|
-
} else {
|
|
1484
|
-
*error = "options: invalid import_style - " + option.second;
|
|
1485
|
-
return false;
|
|
1486
|
-
}
|
|
1487
|
-
} else if ("multiple_files" == option.first) {
|
|
1488
|
-
multiple_files_ = "True" == option.second;
|
|
1489
|
-
} else if ("plugins" == option.first) {
|
|
1490
|
-
plugins_ = option.second;
|
|
1491
|
-
} else if ("goog_promise" == option.first) {
|
|
1492
|
-
goog_promise_ = "True" == option.second;
|
|
1493
|
-
} else {
|
|
1494
|
-
*error = "unsupported option: " + option.first;
|
|
1495
|
-
return false;
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
|
|
1499
|
-
if (mode_.empty()) {
|
|
1500
|
-
*error = "options: mode is required";
|
|
1501
|
-
return false;
|
|
1502
|
-
}
|
|
1503
|
-
|
|
1504
|
-
return true;
|
|
1505
|
-
}
|
|
1506
|
-
|
|
1507
|
-
string GeneratorOptions::OutputFile(const string& proto_file) const {
|
|
1508
|
-
if (ImportStyle::TYPESCRIPT == import_style()) {
|
|
1509
|
-
// Never use the value from the 'out' option when generating TypeScript.
|
|
1510
|
-
string directory;
|
|
1511
|
-
string basename;
|
|
1512
|
-
PathSplit(proto_file, &directory, &basename);
|
|
1513
|
-
return directory + UppercaseFirstLetter(StripProto(basename)) +
|
|
1514
|
-
"ServiceClientPb.ts";
|
|
1515
|
-
}
|
|
1516
|
-
if (!file_name_.empty()) {
|
|
1517
|
-
return file_name_;
|
|
1518
|
-
}
|
|
1519
|
-
return StripProto(proto_file) + "_grpc_web_pb.js";
|
|
1520
|
-
}
|
|
1521
|
-
|
|
1522
|
-
class GrpcCodeGenerator : public CodeGenerator {
|
|
1523
|
-
public:
|
|
1524
|
-
GrpcCodeGenerator() {}
|
|
1525
|
-
~GrpcCodeGenerator() override {}
|
|
1526
|
-
|
|
1527
|
-
uint64_t GetSupportedFeatures() const override {
|
|
1528
|
-
// Code generators must explicitly support proto3 optional.
|
|
1529
|
-
return CodeGenerator::FEATURE_PROTO3_OPTIONAL;
|
|
1530
|
-
}
|
|
1531
|
-
|
|
1532
|
-
bool Generate(const FileDescriptor* file, const string& parameter,
|
|
1533
|
-
GeneratorContext* context, string* error) const override {
|
|
1534
|
-
GeneratorOptions generator_options;
|
|
1535
|
-
if (!generator_options.ParseFromOptions(parameter, error)) {
|
|
1536
|
-
return false;
|
|
1537
|
-
}
|
|
1538
|
-
|
|
1539
|
-
std::map<string, string> vars;
|
|
1540
|
-
std::map<string, string> method_descriptors;
|
|
1541
|
-
string package = file->package();
|
|
1542
|
-
vars["package"] = package;
|
|
1543
|
-
vars["package_dot"] = package.empty() ? "" : package + '.';
|
|
1544
|
-
vars["promise"] = "Promise";
|
|
1545
|
-
vars["plugins"] = generator_options.plugins();
|
|
1546
|
-
|
|
1547
|
-
if ("binary" == generator_options.mode()) {
|
|
1548
|
-
vars["mode"] = GetModeVar(Mode::OP);
|
|
1549
|
-
vars["binary"] = "true";
|
|
1550
|
-
} else if ("grpcweb" == generator_options.mode()) {
|
|
1551
|
-
vars["mode"] = GetModeVar(Mode::GRPCWEB);
|
|
1552
|
-
vars["format"] = "binary";
|
|
1553
|
-
} else if ("grpcwebtext" == generator_options.mode()) {
|
|
1554
|
-
vars["mode"] = GetModeVar(Mode::GRPCWEB);
|
|
1555
|
-
vars["format"] = "text";
|
|
1556
|
-
} else if ("jspb" == generator_options.mode()) {
|
|
1557
|
-
vars["mode"] = GetModeVar(Mode::OP);
|
|
1558
|
-
vars["binary"] = "false";
|
|
1559
|
-
if (generator_options.goog_promise()) {
|
|
1560
|
-
vars["promise"] = GRPC_PROMISE;
|
|
1561
|
-
}
|
|
1562
|
-
} else {
|
|
1563
|
-
*error = "options: invalid mode - " + generator_options.mode();
|
|
1564
|
-
return false;
|
|
1565
|
-
}
|
|
1566
|
-
|
|
1567
|
-
if (generator_options.generate_dts()) {
|
|
1568
|
-
string proto_dts_file_name = StripProto(file->name()) + "_pb.d.ts";
|
|
1569
|
-
std::unique_ptr<ZeroCopyOutputStream> proto_dts_output(
|
|
1570
|
-
context->Open(proto_dts_file_name));
|
|
1571
|
-
Printer proto_dts_printer(proto_dts_output.get(), '$');
|
|
1572
|
-
PrintProtoDtsFile(&proto_dts_printer, file);
|
|
1573
|
-
}
|
|
1574
|
-
|
|
1575
|
-
if (!file->service_count()) {
|
|
1576
|
-
// No services, nothing to do.
|
|
1577
|
-
return true;
|
|
1578
|
-
}
|
|
1579
|
-
|
|
1580
|
-
vars["version"] = GRPC_WEB_VERSION;
|
|
1581
|
-
vars["protoc_version"] = GetProtocVersion(context);
|
|
1582
|
-
vars["source_file"] = file->name();
|
|
1583
|
-
|
|
1584
|
-
string file_name = generator_options.OutputFile(file->name());
|
|
1585
|
-
if (generator_options.multiple_files() &&
|
|
1586
|
-
ImportStyle::CLOSURE == generator_options.import_style()) {
|
|
1587
|
-
PrintMultipleFilesMode(file, file_name, context, vars);
|
|
1588
|
-
return true;
|
|
1589
|
-
}
|
|
1590
|
-
|
|
1591
|
-
std::unique_ptr<ZeroCopyOutputStream> output(context->Open(file_name));
|
|
1592
|
-
Printer printer(output.get(), '$');
|
|
1593
|
-
PrintFileHeader(&printer, vars);
|
|
1594
|
-
|
|
1595
|
-
if (ImportStyle::TYPESCRIPT == generator_options.import_style()) {
|
|
1596
|
-
PrintTypescriptFile(&printer, file, vars);
|
|
1597
|
-
return true;
|
|
1598
|
-
}
|
|
1599
|
-
|
|
1600
|
-
for (int i = 0; i < file->service_count(); ++i) {
|
|
1601
|
-
const ServiceDescriptor* service = file->service(i);
|
|
1602
|
-
vars["service_name"] = service->name();
|
|
1603
|
-
switch (generator_options.import_style()) {
|
|
1604
|
-
case ImportStyle::CLOSURE:
|
|
1605
|
-
printer.Print(
|
|
1606
|
-
vars,
|
|
1607
|
-
"goog.provide('proto.$package_dot$$service_name$Client');\n");
|
|
1608
|
-
printer.Print(vars,
|
|
1609
|
-
"goog.provide('proto.$package_dot$$service_name$"
|
|
1610
|
-
"PromiseClient');\n");
|
|
1611
|
-
break;
|
|
1612
|
-
case ImportStyle::COMMONJS:
|
|
1613
|
-
break;
|
|
1614
|
-
case ImportStyle::TYPESCRIPT:
|
|
1615
|
-
break;
|
|
1616
|
-
}
|
|
1617
|
-
}
|
|
1618
|
-
printer.Print("\n");
|
|
1619
|
-
|
|
1620
|
-
switch (generator_options.import_style()) {
|
|
1621
|
-
case ImportStyle::CLOSURE:
|
|
1622
|
-
if (vars["promise"] == GRPC_PROMISE) {
|
|
1623
|
-
printer.Print(vars, "goog.require('grpc.web.promise');\n");
|
|
1624
|
-
}
|
|
1625
|
-
printer.Print(vars, "goog.require('grpc.web.MethodDescriptor');\n");
|
|
1626
|
-
printer.Print(vars, "goog.require('grpc.web.MethodType');\n");
|
|
1627
|
-
printer.Print(vars, "goog.require('grpc.web.$mode$ClientBase');\n");
|
|
1628
|
-
printer.Print(vars, "goog.require('grpc.web.AbstractClientBase');\n");
|
|
1629
|
-
printer.Print(vars, "goog.require('grpc.web.ClientReadableStream');\n");
|
|
1630
|
-
printer.Print(vars, "goog.require('grpc.web.RpcError');\n");
|
|
1631
|
-
|
|
1632
|
-
PrintClosureDependencies(&printer, file);
|
|
1633
|
-
printer.Print(vars, "\ngoog.requireType('grpc.web.ClientOptions');\n");
|
|
1634
|
-
printer.Print("\n\n\n");
|
|
1635
|
-
printer.Print("goog.scope(function() {\n\n");
|
|
1636
|
-
break;
|
|
1637
|
-
case ImportStyle::COMMONJS:
|
|
1638
|
-
printer.Print(vars, "const grpc = {};\n");
|
|
1639
|
-
printer.Print(vars, "grpc.web = require('grpc-web');\n\n");
|
|
1640
|
-
PrintCommonJsMessagesDeps(&printer, file);
|
|
1641
|
-
break;
|
|
1642
|
-
case ImportStyle::TYPESCRIPT:
|
|
1643
|
-
break;
|
|
1644
|
-
}
|
|
1645
|
-
|
|
1646
|
-
for (int service_index = 0; service_index < file->service_count();
|
|
1647
|
-
++service_index) {
|
|
1648
|
-
const ServiceDescriptor* service = file->service(service_index);
|
|
1649
|
-
vars["service_name"] = service->name();
|
|
1650
|
-
PrintServiceConstructor(&printer, vars, false);
|
|
1651
|
-
PrintServiceConstructor(&printer, vars, true);
|
|
1652
|
-
|
|
1653
|
-
for (int method_index = 0; method_index < service->method_count();
|
|
1654
|
-
++method_index) {
|
|
1655
|
-
const MethodDescriptor* method = service->method(method_index);
|
|
1656
|
-
const Descriptor* input_type = method->input_type();
|
|
1657
|
-
const Descriptor* output_type = method->output_type();
|
|
1658
|
-
vars["js_method_name"] = LowercaseFirstLetter(method->name());
|
|
1659
|
-
vars["method_name"] = method->name();
|
|
1660
|
-
vars["in"] = input_type->full_name();
|
|
1661
|
-
vars["out"] = output_type->full_name();
|
|
1662
|
-
vars["method_descriptor"] =
|
|
1663
|
-
"methodDescriptor_" + service->name() + "_" + method->name();
|
|
1664
|
-
|
|
1665
|
-
// Cross-file ref in CommonJS needs to use the module alias instead
|
|
1666
|
-
// of the global name.
|
|
1667
|
-
if (ImportStyle::COMMONJS == generator_options.import_style() &&
|
|
1668
|
-
input_type->file() != file) {
|
|
1669
|
-
vars["in_type"] = ModuleAlias(input_type->file()->name()) +
|
|
1670
|
-
GetNestedMessageName(input_type);
|
|
1671
|
-
} else {
|
|
1672
|
-
vars["in_type"] = "proto." + input_type->full_name();
|
|
1673
|
-
}
|
|
1674
|
-
if (ImportStyle::COMMONJS == generator_options.import_style() &&
|
|
1675
|
-
output_type->file() != file) {
|
|
1676
|
-
vars["out_type"] = ModuleAlias(output_type->file()->name()) +
|
|
1677
|
-
GetNestedMessageName(output_type);
|
|
1678
|
-
} else {
|
|
1679
|
-
vars["out_type"] = "proto." + output_type->full_name();
|
|
1680
|
-
}
|
|
1681
|
-
|
|
1682
|
-
// Client streaming is not supported yet
|
|
1683
|
-
if (!method->client_streaming()) {
|
|
1684
|
-
if (method->server_streaming()) {
|
|
1685
|
-
vars["method_type"] = "grpc.web.MethodType.SERVER_STREAMING";
|
|
1686
|
-
PrintMethodDescriptor(&printer, vars);
|
|
1687
|
-
vars["client_type"] = "Client";
|
|
1688
|
-
PrintServerStreamingCall(&printer, vars);
|
|
1689
|
-
vars["client_type"] = "PromiseClient";
|
|
1690
|
-
PrintServerStreamingCall(&printer, vars);
|
|
1691
|
-
} else {
|
|
1692
|
-
vars["method_type"] = "grpc.web.MethodType.UNARY";
|
|
1693
|
-
PrintMethodDescriptor(&printer, vars);
|
|
1694
|
-
PrintUnaryCall(&printer, vars);
|
|
1695
|
-
PrintPromiseUnaryCall(&printer, vars);
|
|
1696
|
-
}
|
|
1697
|
-
}
|
|
1698
|
-
}
|
|
1699
|
-
}
|
|
1700
|
-
|
|
1701
|
-
switch (generator_options.import_style()) {
|
|
1702
|
-
case ImportStyle::CLOSURE:
|
|
1703
|
-
printer.Print("}); // goog.scope\n\n");
|
|
1704
|
-
break;
|
|
1705
|
-
case ImportStyle::COMMONJS:
|
|
1706
|
-
if (!vars["package"].empty()) {
|
|
1707
|
-
printer.Print(vars, "module.exports = proto.$package$;\n\n");
|
|
1708
|
-
} else {
|
|
1709
|
-
printer.Print(vars, "module.exports = proto;\n\n");
|
|
1710
|
-
}
|
|
1711
|
-
break;
|
|
1712
|
-
case ImportStyle::TYPESCRIPT:
|
|
1713
|
-
break;
|
|
1714
|
-
}
|
|
1715
|
-
|
|
1716
|
-
if (generator_options.generate_dts()) {
|
|
1717
|
-
string grpcweb_dts_file_name =
|
|
1718
|
-
StripProto(file->name()) + "_grpc_web_pb.d.ts";
|
|
1719
|
-
string proto_dts_file_name = StripProto(file->name()) + "_pb.d.ts";
|
|
1720
|
-
|
|
1721
|
-
std::unique_ptr<ZeroCopyOutputStream> grpcweb_dts_output(
|
|
1722
|
-
context->Open(grpcweb_dts_file_name));
|
|
1723
|
-
Printer grpcweb_dts_printer(grpcweb_dts_output.get(), '$');
|
|
1724
|
-
|
|
1725
|
-
PrintGrpcWebDtsFile(&grpcweb_dts_printer, file);
|
|
1726
|
-
}
|
|
1727
|
-
|
|
1728
|
-
if (generator_options.generate_closure_es6()) {
|
|
1729
|
-
string es6_file_name = StripProto(file->name()) + ".pb.grpc-web.js";
|
|
1730
|
-
|
|
1731
|
-
std::unique_ptr<ZeroCopyOutputStream> es6_output(
|
|
1732
|
-
context->Open(es6_file_name));
|
|
1733
|
-
Printer es6_printer(es6_output.get(), '$');
|
|
1734
|
-
|
|
1735
|
-
PrintGrpcWebClosureES6File(&es6_printer, file);
|
|
1736
|
-
}
|
|
1737
|
-
|
|
1738
|
-
return true;
|
|
1739
|
-
}
|
|
1740
|
-
};
|
|
1741
|
-
|
|
1742
|
-
} // namespace
|
|
1743
|
-
} // namespace web
|
|
1744
|
-
} // namespace grpc
|
|
1745
|
-
|
|
1746
|
-
int main(int argc, char* argv[]) {
|
|
1747
|
-
if (argc == 2 && std::string(argv[1]) == "--version") {
|
|
1748
|
-
std::cout << argv[0] << " " << grpc::web::GRPC_WEB_VERSION << std::endl;
|
|
1749
|
-
return 0;
|
|
1750
|
-
}
|
|
1751
|
-
|
|
1752
|
-
grpc::web::GrpcCodeGenerator generator;
|
|
1753
|
-
PluginMain(argc, argv, &generator);
|
|
1754
|
-
return 0;
|
|
1755
|
-
}
|