| 1 |
"use strict"; |
| 2 |
var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 |
return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 |
}; |
| 5 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 |
exports.fetchWithRetry = fetchWithRetry; |
| 7 |
/* |
| 8 |
Copyright 2023 The Sigstore Authors. |
| 9 |
|
| 10 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 |
you may not use this file except in compliance with the License. |
| 12 |
You may obtain a copy of the License at |
| 13 |
|
| 14 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
|
| 16 |
Unless required by applicable law or agreed to in writing, software |
| 17 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 18 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 |
See the License for the specific language governing permissions and |
| 20 |
limitations under the License. |
| 21 |
*/ |
| 22 |
const promise_retry_1 = require("@gar/promise-retry"); |
| 23 |
const http2_1 = require("http2"); |
| 24 |
const make_fetch_happen_1 = __importDefault(require("make-fetch-happen")); |
| 25 |
const proc_log_1 = require("proc-log"); |
| 26 |
const util_1 = require("../util"); |
| 27 |
const error_1 = require("./error"); |
| 28 |
const { HTTP2_HEADER_LOCATION, HTTP2_HEADER_CONTENT_TYPE, HTTP2_HEADER_USER_AGENT, HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_TOO_MANY_REQUESTS, HTTP_STATUS_REQUEST_TIMEOUT, } = http2_1.constants; |
| 29 |
async function fetchWithRetry(url, options) { |
| 30 |
return (0, promise_retry_1.promiseRetry)(async (retry, attemptNum) => { |
| 31 |
const method = options.method || 'POST'; |
| 32 |
const headers = { |
| 33 |
[HTTP2_HEADER_USER_AGENT]: util_1.ua.getUserAgent(), |
| 34 |
...options.headers, |
| 35 |
}; |
| 36 |
const response = await (0, make_fetch_happen_1.default)(url, { |
| 37 |
method, |
| 38 |
headers, |
| 39 |
body: options.body, |
| 40 |
timeout: options.timeout, |
| 41 |
retry: false, // We're handling retries ourselves |
| 42 |
}).catch((reason) => { |
| 43 |
proc_log_1.log.http('fetch', `${method} ${url} attempt ${attemptNum} failed with ${reason}`); |
| 44 |
return retry(reason); |
| 45 |
}); |
| 46 |
if (response.ok) { |
| 47 |
return response; |
| 48 |
} |
| 49 |
else { |
| 50 |
const error = await errorFromResponse(response); |
| 51 |
proc_log_1.log.http('fetch', `${method} ${url} attempt ${attemptNum} failed with ${response.status}`); |
| 52 |
if (retryable(response.status)) { |
| 53 |
return retry(error); |
| 54 |
} |
| 55 |
else { |
| 56 |
throw error; |
| 57 |
} |
| 58 |
} |
| 59 |
}, retryOpts(options.retry)); |
| 60 |
} |
| 61 |
// Translate a Response into an HTTPError instance. This will attempt to parse |
| 62 |
// the response body for a message, but will default to the statusText if none |
| 63 |
// is found. |
| 64 |
const errorFromResponse = async (response) => { |
| 65 |
let message = response.statusText; |
| 66 |
const location = response.headers.get(HTTP2_HEADER_LOCATION) || undefined; |
| 67 |
const contentType = response.headers.get(HTTP2_HEADER_CONTENT_TYPE); |
| 68 |
// If response type is JSON, try to parse the body for a message |
| 69 |
if (contentType?.includes('application/json')) { |
| 70 |
try { |
| 71 |
const body = await response.json(); |
| 72 |
message = body.message || message; |
| 73 |
} |
| 74 |
catch (e) { |
| 75 |
// ignore |
| 76 |
} |
| 77 |
} |
| 78 |
return new error_1.HTTPError({ |
| 79 |
status: response.status, |
| 80 |
message: message, |
| 81 |
location: location, |
| 82 |
}); |
| 83 |
}; |
| 84 |
// Determine if a status code is retryable. This includes 5xx errors, 408, and |
| 85 |
// 429. |
| 86 |
const retryable = (status) => [HTTP_STATUS_REQUEST_TIMEOUT, HTTP_STATUS_TOO_MANY_REQUESTS].includes(status) || status >= HTTP_STATUS_INTERNAL_SERVER_ERROR; |
| 87 |
// Normalize the retry options to the format expected by promise-retry |
| 88 |
const retryOpts = (retry) => { |
| 89 |
if (typeof retry === 'boolean') { |
| 90 |
return { retries: retry ? 1 : 0 }; |
| 91 |
} |
| 92 |
else if (typeof retry === 'number') { |
| 93 |
return { retries: retry }; |
| 94 |
} |
| 95 |
else { |
| 96 |
return { retries: 0, ...retry }; |
| 97 |
} |
| 98 |
}; |