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