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.extractAndSelectServiceConfig = exports.validateServiceConfig = void 0;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const os = require("os");
|
30 | const load_balancer_1 = require("./load-balancer");
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | const TIMEOUT_REGEX = /^\d+(\.\d{1,9})?s$/;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | const CLIENT_LANGUAGE_STRING = 'node';
|
41 | function validateName(obj) {
|
42 | if (!('service' in obj) || typeof obj.service !== 'string') {
|
43 | throw new Error('Invalid method config name: invalid service');
|
44 | }
|
45 | const result = {
|
46 | service: obj.service,
|
47 | };
|
48 | if ('method' in obj) {
|
49 | if (typeof obj.method === 'string') {
|
50 | result.method = obj.method;
|
51 | }
|
52 | else {
|
53 | throw new Error('Invalid method config name: invalid method');
|
54 | }
|
55 | }
|
56 | return result;
|
57 | }
|
58 | function validateMethodConfig(obj) {
|
59 | var _a;
|
60 | const result = {
|
61 | name: [],
|
62 | };
|
63 | if (!('name' in obj) || !Array.isArray(obj.name)) {
|
64 | throw new Error('Invalid method config: invalid name array');
|
65 | }
|
66 | for (const name of obj.name) {
|
67 | result.name.push(validateName(name));
|
68 | }
|
69 | if ('waitForReady' in obj) {
|
70 | if (typeof obj.waitForReady !== 'boolean') {
|
71 | throw new Error('Invalid method config: invalid waitForReady');
|
72 | }
|
73 | result.waitForReady = obj.waitForReady;
|
74 | }
|
75 | if ('timeout' in obj) {
|
76 | if (typeof obj.timeout === 'object') {
|
77 | if (!('seconds' in obj.timeout) ||
|
78 | !(typeof obj.timeout.seconds === 'number')) {
|
79 | throw new Error('Invalid method config: invalid timeout.seconds');
|
80 | }
|
81 | if (!('nanos' in obj.timeout) ||
|
82 | !(typeof obj.timeout.nanos === 'number')) {
|
83 | throw new Error('Invalid method config: invalid timeout.nanos');
|
84 | }
|
85 | result.timeout = obj.timeout;
|
86 | }
|
87 | else if (typeof obj.timeout === 'string' &&
|
88 | TIMEOUT_REGEX.test(obj.timeout)) {
|
89 | const timeoutParts = obj.timeout
|
90 | .substring(0, obj.timeout.length - 1)
|
91 | .split('.');
|
92 | result.timeout = {
|
93 | seconds: timeoutParts[0] | 0,
|
94 | nanos: ((_a = timeoutParts[1]) !== null && _a !== void 0 ? _a : 0) | 0,
|
95 | };
|
96 | }
|
97 | else {
|
98 | throw new Error('Invalid method config: invalid timeout');
|
99 | }
|
100 | }
|
101 | if ('maxRequestBytes' in obj) {
|
102 | if (typeof obj.maxRequestBytes !== 'number') {
|
103 | throw new Error('Invalid method config: invalid maxRequestBytes');
|
104 | }
|
105 | result.maxRequestBytes = obj.maxRequestBytes;
|
106 | }
|
107 | if ('maxResponseBytes' in obj) {
|
108 | if (typeof obj.maxResponseBytes !== 'number') {
|
109 | throw new Error('Invalid method config: invalid maxRequestBytes');
|
110 | }
|
111 | result.maxResponseBytes = obj.maxResponseBytes;
|
112 | }
|
113 | return result;
|
114 | }
|
115 | function validateServiceConfig(obj) {
|
116 | const result = {
|
117 | loadBalancingConfig: [],
|
118 | methodConfig: [],
|
119 | };
|
120 | if ('loadBalancingPolicy' in obj) {
|
121 | if (typeof obj.loadBalancingPolicy === 'string') {
|
122 | result.loadBalancingPolicy = obj.loadBalancingPolicy;
|
123 | }
|
124 | else {
|
125 | throw new Error('Invalid service config: invalid loadBalancingPolicy');
|
126 | }
|
127 | }
|
128 | if ('loadBalancingConfig' in obj) {
|
129 | if (Array.isArray(obj.loadBalancingConfig)) {
|
130 | for (const config of obj.loadBalancingConfig) {
|
131 | result.loadBalancingConfig.push(load_balancer_1.validateLoadBalancingConfig(config));
|
132 | }
|
133 | }
|
134 | else {
|
135 | throw new Error('Invalid service config: invalid loadBalancingConfig');
|
136 | }
|
137 | }
|
138 | if ('methodConfig' in obj) {
|
139 | if (Array.isArray(obj.methodConfig)) {
|
140 | for (const methodConfig of obj.methodConfig) {
|
141 | result.methodConfig.push(validateMethodConfig(methodConfig));
|
142 | }
|
143 | }
|
144 | }
|
145 |
|
146 | const seenMethodNames = [];
|
147 | for (const methodConfig of result.methodConfig) {
|
148 | for (const name of methodConfig.name) {
|
149 | for (const seenName of seenMethodNames) {
|
150 | if (name.service === seenName.service &&
|
151 | name.method === seenName.method) {
|
152 | throw new Error(`Invalid service config: duplicate name ${name.service}/${name.method}`);
|
153 | }
|
154 | }
|
155 | seenMethodNames.push(name);
|
156 | }
|
157 | }
|
158 | return result;
|
159 | }
|
160 | exports.validateServiceConfig = validateServiceConfig;
|
161 | function validateCanaryConfig(obj) {
|
162 | if (!('serviceConfig' in obj)) {
|
163 | throw new Error('Invalid service config choice: missing service config');
|
164 | }
|
165 | const result = {
|
166 | serviceConfig: validateServiceConfig(obj.serviceConfig),
|
167 | };
|
168 | if ('clientLanguage' in obj) {
|
169 | if (Array.isArray(obj.clientLanguage)) {
|
170 | result.clientLanguage = [];
|
171 | for (const lang of obj.clientLanguage) {
|
172 | if (typeof lang === 'string') {
|
173 | result.clientLanguage.push(lang);
|
174 | }
|
175 | else {
|
176 | throw new Error('Invalid service config choice: invalid clientLanguage');
|
177 | }
|
178 | }
|
179 | }
|
180 | else {
|
181 | throw new Error('Invalid service config choice: invalid clientLanguage');
|
182 | }
|
183 | }
|
184 | if ('clientHostname' in obj) {
|
185 | if (Array.isArray(obj.clientHostname)) {
|
186 | result.clientHostname = [];
|
187 | for (const lang of obj.clientHostname) {
|
188 | if (typeof lang === 'string') {
|
189 | result.clientHostname.push(lang);
|
190 | }
|
191 | else {
|
192 | throw new Error('Invalid service config choice: invalid clientHostname');
|
193 | }
|
194 | }
|
195 | }
|
196 | else {
|
197 | throw new Error('Invalid service config choice: invalid clientHostname');
|
198 | }
|
199 | }
|
200 | if ('percentage' in obj) {
|
201 | if (typeof obj.percentage === 'number' &&
|
202 | 0 <= obj.percentage &&
|
203 | obj.percentage <= 100) {
|
204 | result.percentage = obj.percentage;
|
205 | }
|
206 | else {
|
207 | throw new Error('Invalid service config choice: invalid percentage');
|
208 | }
|
209 | }
|
210 |
|
211 | const allowedFields = [
|
212 | 'clientLanguage',
|
213 | 'percentage',
|
214 | 'clientHostname',
|
215 | 'serviceConfig',
|
216 | ];
|
217 | for (const field in obj) {
|
218 | if (!allowedFields.includes(field)) {
|
219 | throw new Error(`Invalid service config choice: unexpected field ${field}`);
|
220 | }
|
221 | }
|
222 | return result;
|
223 | }
|
224 | function validateAndSelectCanaryConfig(obj, percentage) {
|
225 | if (!Array.isArray(obj)) {
|
226 | throw new Error('Invalid service config list');
|
227 | }
|
228 | for (const config of obj) {
|
229 | const validatedConfig = validateCanaryConfig(config);
|
230 | |
231 |
|
232 | if (typeof validatedConfig.percentage === 'number' &&
|
233 | percentage > validatedConfig.percentage) {
|
234 | continue;
|
235 | }
|
236 | if (Array.isArray(validatedConfig.clientHostname)) {
|
237 | let hostnameMatched = false;
|
238 | for (const hostname of validatedConfig.clientHostname) {
|
239 | if (hostname === os.hostname()) {
|
240 | hostnameMatched = true;
|
241 | }
|
242 | }
|
243 | if (!hostnameMatched) {
|
244 | continue;
|
245 | }
|
246 | }
|
247 | if (Array.isArray(validatedConfig.clientLanguage)) {
|
248 | let languageMatched = false;
|
249 | for (const language of validatedConfig.clientLanguage) {
|
250 | if (language === CLIENT_LANGUAGE_STRING) {
|
251 | languageMatched = true;
|
252 | }
|
253 | }
|
254 | if (!languageMatched) {
|
255 | continue;
|
256 | }
|
257 | }
|
258 | return validatedConfig.serviceConfig;
|
259 | }
|
260 | throw new Error('No matching service config found');
|
261 | }
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | function extractAndSelectServiceConfig(txtRecord, percentage) {
|
272 | for (const record of txtRecord) {
|
273 | if (record.length > 0 && record[0].startsWith('grpc_config=')) {
|
274 | |
275 |
|
276 | const recordString = record.join('').substring('grpc_config='.length);
|
277 | const recordJson = JSON.parse(recordString);
|
278 | return validateAndSelectCanaryConfig(recordJson, percentage);
|
279 | }
|
280 | }
|
281 | return null;
|
282 | }
|
283 | exports.extractAndSelectServiceConfig = extractAndSelectServiceConfig;
|
284 |
|
\ | No newline at end of file |