UNPKG

7.06 kBJavaScriptView Raw
1"use strict";
2// Copyright 2018 Google LLC
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14var __importDefault = (this && this.__importDefault) || function (mod) {
15 return (mod && mod.__esModule) ? mod : { "default": mod };
16};
17var _a;
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.GaxiosError = exports.GAXIOS_ERROR_SYMBOL = void 0;
20exports.defaultErrorRedactor = defaultErrorRedactor;
21const url_1 = require("url");
22const util_1 = require("./util");
23const extend_1 = __importDefault(require("extend"));
24/**
25 * Support `instanceof` operator for `GaxiosError`s in different versions of this library.
26 *
27 * @see {@link GaxiosError[Symbol.hasInstance]}
28 */
29exports.GAXIOS_ERROR_SYMBOL = Symbol.for(`${util_1.pkg.name}-gaxios-error`);
30/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
31class GaxiosError extends Error {
32 /**
33 * Support `instanceof` operator for `GaxiosError` across builds/duplicated files.
34 *
35 * @see {@link GAXIOS_ERROR_SYMBOL}
36 * @see {@link GaxiosError[GAXIOS_ERROR_SYMBOL]}
37 */
38 static [(_a = exports.GAXIOS_ERROR_SYMBOL, Symbol.hasInstance)](instance) {
39 if (instance &&
40 typeof instance === 'object' &&
41 exports.GAXIOS_ERROR_SYMBOL in instance &&
42 instance[exports.GAXIOS_ERROR_SYMBOL] === util_1.pkg.version) {
43 return true;
44 }
45 // fallback to native
46 return Function.prototype[Symbol.hasInstance].call(GaxiosError, instance);
47 }
48 constructor(message, config, response, error) {
49 var _b;
50 super(message);
51 this.config = config;
52 this.response = response;
53 this.error = error;
54 /**
55 * Support `instanceof` operator for `GaxiosError` across builds/duplicated files.
56 *
57 * @see {@link GAXIOS_ERROR_SYMBOL}
58 * @see {@link GaxiosError[Symbol.hasInstance]}
59 * @see {@link https://github.com/microsoft/TypeScript/issues/13965#issuecomment-278570200}
60 * @see {@link https://stackoverflow.com/questions/46618852/require-and-instanceof}
61 * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/@@hasInstance#reverting_to_default_instanceof_behavior}
62 */
63 this[_a] = util_1.pkg.version;
64 // deep-copy config as we do not want to mutate
65 // the existing config for future retries/use
66 this.config = (0, extend_1.default)(true, {}, config);
67 if (this.response) {
68 this.response.config = (0, extend_1.default)(true, {}, this.response.config);
69 }
70 if (this.response) {
71 try {
72 this.response.data = translateData(this.config.responseType, (_b = this.response) === null || _b === void 0 ? void 0 : _b.data);
73 }
74 catch (_c) {
75 // best effort - don't throw an error within an error
76 // we could set `this.response.config.responseType = 'unknown'`, but
77 // that would mutate future calls with this config object.
78 }
79 this.status = this.response.status;
80 }
81 if (error && 'code' in error && error.code) {
82 this.code = error.code;
83 }
84 if (config.errorRedactor) {
85 config.errorRedactor({
86 config: this.config,
87 response: this.response,
88 });
89 }
90 }
91}
92exports.GaxiosError = GaxiosError;
93function translateData(responseType, data) {
94 switch (responseType) {
95 case 'stream':
96 return data;
97 case 'json':
98 return JSON.parse(JSON.stringify(data));
99 case 'arraybuffer':
100 return JSON.parse(Buffer.from(data).toString('utf8'));
101 case 'blob':
102 return JSON.parse(data.text());
103 default:
104 return data;
105 }
106}
107/**
108 * An experimental error redactor.
109 *
110 * @param config Config to potentially redact properties of
111 * @param response Config to potentially redact properties of
112 *
113 * @experimental
114 */
115function defaultErrorRedactor(data) {
116 const REDACT = '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.';
117 function redactHeaders(headers) {
118 if (!headers)
119 return;
120 for (const key of Object.keys(headers)) {
121 // any casing of `Authentication`
122 if (/^authentication$/i.test(key)) {
123 headers[key] = REDACT;
124 }
125 // any casing of `Authorization`
126 if (/^authorization$/i.test(key)) {
127 headers[key] = REDACT;
128 }
129 // anything containing secret, such as 'client secret'
130 if (/secret/i.test(key)) {
131 headers[key] = REDACT;
132 }
133 }
134 }
135 function redactString(obj, key) {
136 if (typeof obj === 'object' &&
137 obj !== null &&
138 typeof obj[key] === 'string') {
139 const text = obj[key];
140 if (/grant_type=/i.test(text) ||
141 /assertion=/i.test(text) ||
142 /secret/i.test(text)) {
143 obj[key] = REDACT;
144 }
145 }
146 }
147 function redactObject(obj) {
148 if (typeof obj === 'object' && obj !== null) {
149 if ('grant_type' in obj) {
150 obj['grant_type'] = REDACT;
151 }
152 if ('assertion' in obj) {
153 obj['assertion'] = REDACT;
154 }
155 if ('client_secret' in obj) {
156 obj['client_secret'] = REDACT;
157 }
158 }
159 }
160 if (data.config) {
161 redactHeaders(data.config.headers);
162 redactString(data.config, 'data');
163 redactObject(data.config.data);
164 redactString(data.config, 'body');
165 redactObject(data.config.body);
166 try {
167 const url = new url_1.URL('', data.config.url);
168 if (url.searchParams.has('token')) {
169 url.searchParams.set('token', REDACT);
170 }
171 if (url.searchParams.has('client_secret')) {
172 url.searchParams.set('client_secret', REDACT);
173 }
174 data.config.url = url.toString();
175 }
176 catch (_b) {
177 // ignore error - no need to parse an invalid URL
178 }
179 }
180 if (data.response) {
181 defaultErrorRedactor({ config: data.response.config });
182 redactHeaders(data.response.headers);
183 redactString(data.response, 'data');
184 redactObject(data.response.data);
185 }
186 return data;
187}
188//# sourceMappingURL=common.js.map
\No newline at end of file