1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.loadPackageDefinition = exports.makeClientConstructor = void 0;
|
20 | const client_1 = require("./client");
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const requesterFuncs = {
|
27 | unary: client_1.Client.prototype.makeUnaryRequest,
|
28 | server_stream: client_1.Client.prototype.makeServerStreamRequest,
|
29 | client_stream: client_1.Client.prototype.makeClientStreamRequest,
|
30 | bidi: client_1.Client.prototype.makeBidiStreamRequest,
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function isPrototypePolluted(key) {
|
38 | return ['__proto__', 'prototype', 'constructor'].includes(key);
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function makeClientConstructor(methods, serviceName, classOptions) {
|
55 | if (!classOptions) {
|
56 | classOptions = {};
|
57 | }
|
58 | class ServiceClientImpl extends client_1.Client {
|
59 | }
|
60 | Object.keys(methods).forEach((name) => {
|
61 | if (isPrototypePolluted(name)) {
|
62 | return;
|
63 | }
|
64 | const attrs = methods[name];
|
65 | let methodType;
|
66 |
|
67 | if (typeof name === 'string' && name.charAt(0) === '$') {
|
68 | throw new Error('Method names cannot start with $');
|
69 | }
|
70 | if (attrs.requestStream) {
|
71 | if (attrs.responseStream) {
|
72 | methodType = 'bidi';
|
73 | }
|
74 | else {
|
75 | methodType = 'client_stream';
|
76 | }
|
77 | }
|
78 | else {
|
79 | if (attrs.responseStream) {
|
80 | methodType = 'server_stream';
|
81 | }
|
82 | else {
|
83 | methodType = 'unary';
|
84 | }
|
85 | }
|
86 | const serialize = attrs.requestSerialize;
|
87 | const deserialize = attrs.responseDeserialize;
|
88 | const methodFunc = partial(requesterFuncs[methodType], attrs.path, serialize, deserialize);
|
89 | ServiceClientImpl.prototype[name] = methodFunc;
|
90 |
|
91 | Object.assign(ServiceClientImpl.prototype[name], attrs);
|
92 | if (attrs.originalName && !isPrototypePolluted(attrs.originalName)) {
|
93 | ServiceClientImpl.prototype[attrs.originalName] =
|
94 | ServiceClientImpl.prototype[name];
|
95 | }
|
96 | });
|
97 | ServiceClientImpl.service = methods;
|
98 | ServiceClientImpl.serviceName = serviceName;
|
99 | return ServiceClientImpl;
|
100 | }
|
101 | exports.makeClientConstructor = makeClientConstructor;
|
102 | function partial(fn, path, serialize, deserialize) {
|
103 |
|
104 | return function (...args) {
|
105 | return fn.call(this, path, serialize, deserialize, ...args);
|
106 | };
|
107 | }
|
108 | function isProtobufTypeDefinition(obj) {
|
109 | return 'format' in obj;
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | function loadPackageDefinition(packageDef) {
|
117 | const result = {};
|
118 | for (const serviceFqn in packageDef) {
|
119 | if (Object.prototype.hasOwnProperty.call(packageDef, serviceFqn)) {
|
120 | const service = packageDef[serviceFqn];
|
121 | const nameComponents = serviceFqn.split('.');
|
122 | if (nameComponents.some((comp) => isPrototypePolluted(comp))) {
|
123 | continue;
|
124 | }
|
125 | const serviceName = nameComponents[nameComponents.length - 1];
|
126 | let current = result;
|
127 | for (const packageName of nameComponents.slice(0, -1)) {
|
128 | if (!current[packageName]) {
|
129 | current[packageName] = {};
|
130 | }
|
131 | current = current[packageName];
|
132 | }
|
133 | if (isProtobufTypeDefinition(service)) {
|
134 | current[serviceName] = service;
|
135 | }
|
136 | else {
|
137 | current[serviceName] = makeClientConstructor(service, serviceName, {});
|
138 | }
|
139 | }
|
140 | }
|
141 | return result;
|
142 | }
|
143 | exports.loadPackageDefinition = loadPackageDefinition;
|
144 |
|
\ | No newline at end of file |