1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.ChaosHandler = void 0;
|
10 | var tslib_1 = require("tslib");
|
11 | var MiddlewareControl_1 = require("./MiddlewareControl");
|
12 | var MiddlewareUtil_1 = require("./MiddlewareUtil");
|
13 | var ChaosHandlerData_1 = require("./options/ChaosHandlerData");
|
14 | var ChaosHandlerOptions_1 = require("./options/ChaosHandlerOptions");
|
15 | var ChaosStrategy_1 = require("./options/ChaosStrategy");
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var ChaosHandler = (function () {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function ChaosHandler(options, manualMap) {
|
32 | if (options === void 0) { options = new ChaosHandlerOptions_1.ChaosHandlerOptions(); }
|
33 | this.options = options;
|
34 | this.manualMap = manualMap;
|
35 | }
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | ChaosHandler.prototype.createResponseHeaders = function (chaosHandlerOptions, requestID, requestDate) {
|
45 | var responseHeader = chaosHandlerOptions.headers ? new Headers(chaosHandlerOptions.headers) : new Headers();
|
46 | responseHeader.append("Cache-Control", "no-store");
|
47 | responseHeader.append("request-id", requestID);
|
48 | responseHeader.append("client-request-id", requestID);
|
49 | responseHeader.append("x-ms-ags-diagnostic", "");
|
50 | responseHeader.append("Date", requestDate);
|
51 | responseHeader.append("Strict-Transport-Security", "");
|
52 | if (chaosHandlerOptions.statusCode === 429) {
|
53 |
|
54 | responseHeader.append("retry-after", "3");
|
55 | }
|
56 | return responseHeader;
|
57 | };
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | ChaosHandler.prototype.createResponseBody = function (chaosHandlerOptions, requestID, requestDate) {
|
67 | if (chaosHandlerOptions.responseBody) {
|
68 | return chaosHandlerOptions.responseBody;
|
69 | }
|
70 | var body;
|
71 | if (chaosHandlerOptions.statusCode >= 400) {
|
72 | var codeMessage = ChaosHandlerData_1.httpStatusCode[chaosHandlerOptions.statusCode];
|
73 | var errMessage = chaosHandlerOptions.statusMessage;
|
74 | body = {
|
75 | error: {
|
76 | code: codeMessage,
|
77 | message: errMessage,
|
78 | innerError: {
|
79 | "request-id": requestID,
|
80 | date: requestDate,
|
81 | },
|
82 | },
|
83 | };
|
84 | }
|
85 | else {
|
86 | body = {};
|
87 | }
|
88 | return body;
|
89 | };
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | ChaosHandler.prototype.createResponse = function (chaosHandlerOptions, context) {
|
97 | var requestURL = context.request;
|
98 | var requestID = (0, MiddlewareUtil_1.generateUUID)();
|
99 | var requestDate = new Date();
|
100 | var responseHeader = this.createResponseHeaders(chaosHandlerOptions, requestID, requestDate.toString());
|
101 | var responseBody = this.createResponseBody(chaosHandlerOptions, requestID, requestDate.toString());
|
102 | var init = { url: requestURL, status: chaosHandlerOptions.statusCode, statusText: chaosHandlerOptions.statusMessage, headers: responseHeader };
|
103 | context.response = new Response(typeof responseBody === "string" ? responseBody : JSON.stringify(responseBody), init);
|
104 | };
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | ChaosHandler.prototype.sendRequest = function (chaosHandlerOptions, context) {
|
113 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
114 | return tslib_1.__generator(this, function (_a) {
|
115 | switch (_a.label) {
|
116 | case 0:
|
117 | this.setStatusCode(chaosHandlerOptions, context.request, context.options.method);
|
118 | if (!((chaosHandlerOptions.chaosStrategy === ChaosStrategy_1.ChaosStrategy.MANUAL && !this.nextMiddleware) || Math.floor(Math.random() * 100) < chaosHandlerOptions.chaosPercentage)) return [3 , 1];
|
119 | this.createResponse(chaosHandlerOptions, context);
|
120 | return [3 , 3];
|
121 | case 1:
|
122 | if (!this.nextMiddleware) return [3 , 3];
|
123 | return [4 , this.nextMiddleware.execute(context)];
|
124 | case 2:
|
125 | _a.sent();
|
126 | _a.label = 3;
|
127 | case 3: return [2 ];
|
128 | }
|
129 | });
|
130 | });
|
131 | };
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | ChaosHandler.prototype.getRandomStatusCode = function (requestMethod) {
|
139 | var statusCodeArray = ChaosHandlerData_1.methodStatusCode[requestMethod];
|
140 | return statusCodeArray[Math.floor(Math.random() * statusCodeArray.length)];
|
141 | };
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | ChaosHandler.prototype.getRelativeURL = function (urlMethod) {
|
149 | var pattern = /https?:\/\/graph\.microsoft\.com\/[^/]+(.+?)(\?|$)/;
|
150 | var relativeURL;
|
151 | if (pattern.exec(urlMethod) !== null) {
|
152 | relativeURL = pattern.exec(urlMethod)[1];
|
153 | }
|
154 | return relativeURL;
|
155 | };
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | ChaosHandler.prototype.setStatusCode = function (chaosHandlerOptions, requestURL, requestMethod) {
|
164 | var _this = this;
|
165 | if (chaosHandlerOptions.chaosStrategy === ChaosStrategy_1.ChaosStrategy.MANUAL) {
|
166 | if (chaosHandlerOptions.statusCode === undefined) {
|
167 |
|
168 | var relativeURL_1 = this.getRelativeURL(requestURL);
|
169 | if (this.manualMap.get(relativeURL_1) !== undefined) {
|
170 |
|
171 | if (this.manualMap.get(relativeURL_1).get(requestMethod) !== undefined) {
|
172 | chaosHandlerOptions.statusCode = this.manualMap.get(relativeURL_1).get(requestMethod);
|
173 | }
|
174 |
|
175 | }
|
176 | else {
|
177 |
|
178 | this.manualMap.forEach(function (value, key) {
|
179 | var regexURL = new RegExp(key + "$");
|
180 | if (regexURL.test(relativeURL_1)) {
|
181 | if (_this.manualMap.get(key).get(requestMethod) !== undefined) {
|
182 | chaosHandlerOptions.statusCode = _this.manualMap.get(key).get(requestMethod);
|
183 | }
|
184 |
|
185 | }
|
186 | });
|
187 | }
|
188 |
|
189 | }
|
190 | }
|
191 | else {
|
192 |
|
193 | chaosHandlerOptions.statusCode = this.getRandomStatusCode(requestMethod);
|
194 |
|
195 | }
|
196 | };
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | ChaosHandler.prototype.getOptions = function (context) {
|
204 | var options;
|
205 | if (context.middlewareControl instanceof MiddlewareControl_1.MiddlewareControl) {
|
206 | options = context.middlewareControl.getMiddlewareOptions(ChaosHandlerOptions_1.ChaosHandlerOptions);
|
207 | }
|
208 | if (typeof options === "undefined") {
|
209 | options = Object.assign(new ChaosHandlerOptions_1.ChaosHandlerOptions(), this.options);
|
210 | }
|
211 | return options;
|
212 | };
|
213 | |
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | ChaosHandler.prototype.execute = function (context) {
|
221 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
222 | var chaosHandlerOptions;
|
223 | return tslib_1.__generator(this, function (_a) {
|
224 | switch (_a.label) {
|
225 | case 0:
|
226 | chaosHandlerOptions = this.getOptions(context);
|
227 | return [4 , this.sendRequest(chaosHandlerOptions, context)];
|
228 | case 1: return [2 , _a.sent()];
|
229 | }
|
230 | });
|
231 | });
|
232 | };
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | ChaosHandler.prototype.setNext = function (next) {
|
240 | this.nextMiddleware = next;
|
241 | };
|
242 | return ChaosHandler;
|
243 | }());
|
244 | exports.ChaosHandler = ChaosHandler;
|
245 |
|
\ | No newline at end of file |