fetch-retry.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import picocolors from 'picocolors';
  2. import undici, {
  3. interceptors,
  4. Agent,
  5. Request as UndiciRequest
  6. // setGlobalDispatcher
  7. } from 'undici';
  8. import type {
  9. Dispatcher,
  10. Response,
  11. RequestInit,
  12. RequestInfo
  13. } from 'undici';
  14. import { BetterSqlite3CacheStore } from 'undici-cache-store-better-sqlite3';
  15. export type UndiciResponseData<T = unknown> = Dispatcher.ResponseData<T>;
  16. import { inspect } from 'node:util';
  17. import path from 'node:path';
  18. import fs from 'node:fs';
  19. import { CACHE_DIR } from '../constants/dir';
  20. import { isAbortErrorLike } from 'foxts/abort-error';
  21. if (!fs.existsSync(CACHE_DIR)) {
  22. fs.mkdirSync(CACHE_DIR, { recursive: true });
  23. }
  24. const agent = new Agent({
  25. allowH2: false
  26. }).compose(
  27. interceptors.dns({
  28. // disable IPv6
  29. dualStack: false,
  30. affinity: 4
  31. // TODO: proper cacheable-lookup, or even DoH
  32. }),
  33. interceptors.retry({
  34. maxRetries: 5,
  35. minTimeout: 500, // The initial retry delay in milliseconds
  36. maxTimeout: 10 * 1000, // The maximum retry delay in milliseconds
  37. // Undici still uses `statusCodes` as the first gate for HTTP response retries.
  38. // Our custom `retry()` callback only runs after a response status is admitted here,
  39. // so we must list our status codes here before we can read it in our retry callback.
  40. statusCodes: [404, 429, 500, 502, 503, 504],
  41. // TODO: this part of code is only for allow more errors to be retried by default
  42. // This should be removed once https://github.com/nodejs/undici/issues/3728 is implemented
  43. retry(err, { state, opts }, cb) {
  44. const errorCode = 'code' in err ? (err as NodeJS.ErrnoException).code : undefined;
  45. Object.defineProperty(err, '_url', {
  46. value: opts.method + ' ' + opts.origin?.toString() + opts.path
  47. });
  48. // Any code that is not a Undici's originated and allowed to retry
  49. if (
  50. errorCode === 'ERR_UNESCAPED_CHARACTERS'
  51. || errorCode === 'UND_ERR_DESTROYED'
  52. || err.message === 'Request path contains unescaped characters'
  53. || err.name === 'AbortError'
  54. ) {
  55. return cb(err);
  56. }
  57. const statusCode = 'statusCode' in err && typeof err.statusCode === 'number' ? err.statusCode : null;
  58. // bail out if the status code matches one of the following
  59. if (
  60. statusCode != null
  61. && (
  62. statusCode === 401 // Unauthorized, should check credentials instead of retrying
  63. || statusCode === 403 // Forbidden, should check permissions instead of retrying
  64. // || statusCode === 404 // Not Found, should check URL instead of retrying
  65. || statusCode === 405 // Method Not Allowed, should check method instead of retrying
  66. )
  67. ) {
  68. return cb(err);
  69. }
  70. const origin = opts.origin?.toString();
  71. if (statusCode === 404) {
  72. if (origin?.includes('cdn.jsdelivr.net')) {
  73. // continue retry anyway, jsDelivr has recently broken and return HTTP 404 for bad origin
  74. } else {
  75. return cb(err);
  76. }
  77. }
  78. // if (errorCode === 'UND_ERR_REQ_RETRY') {
  79. // return cb(err);
  80. // }
  81. const {
  82. maxRetries = 5,
  83. minTimeout = 500,
  84. maxTimeout = 10 * 1000,
  85. timeoutFactor = 2,
  86. methods = ['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']
  87. } = opts.retryOptions || {};
  88. // If we reached the max number of retries
  89. if (state.counter > maxRetries) {
  90. return cb(err);
  91. }
  92. // If a set of method are provided and the current method is not in the list
  93. if (Array.isArray(methods) && !methods.includes(opts.method)) {
  94. return cb(err);
  95. }
  96. const headers = ('headers' in err && typeof err.headers === 'object') ? err.headers : undefined;
  97. const retryAfterHeader = (headers as Record<string, string> | null | undefined)?.['retry-after'];
  98. let retryAfter = -1;
  99. if (retryAfterHeader) {
  100. retryAfter = Number(retryAfterHeader);
  101. retryAfter = Number.isNaN(retryAfter)
  102. ? calculateRetryAfterHeader(retryAfterHeader)
  103. : retryAfter * 1e3; // Retry-After is in seconds
  104. }
  105. const retryTimeout = retryAfter > 0
  106. ? Math.min(retryAfter, maxTimeout)
  107. : Math.min(minTimeout * (timeoutFactor ** (state.counter - 1)), maxTimeout);
  108. console.log('[fetch retry]', 'schedule retry', { statusCode, retryTimeout, errorCode, url: opts.origin });
  109. // eslint-disable-next-line sukka/prefer-timer-id -- won't leak
  110. setTimeout(() => cb(null), retryTimeout);
  111. }
  112. // errorCodes: ['UND_ERR_HEADERS_TIMEOUT', 'ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN', 'ENETUNREACH', 'EHOSTDOWN', 'EHOSTUNREACH', 'EPIPE', 'ETIMEDOUT']
  113. }),
  114. interceptors.redirect({
  115. maxRedirections: 5
  116. }),
  117. interceptors.cache({
  118. store: new BetterSqlite3CacheStore({
  119. loose: true,
  120. location: path.join(CACHE_DIR, 'undici-better-sqlite3-cache-store.db'),
  121. maxCount: 128,
  122. maxEntrySize: 1024 * 1024 * 100 // 100 MiB
  123. }),
  124. cacheByDefault: 600 // 10 minutes
  125. })
  126. );
  127. function calculateRetryAfterHeader(retryAfter: string) {
  128. const current = Date.now();
  129. return new Date(retryAfter).getTime() - current;
  130. }
  131. export class ResponseError<T extends UndiciResponseData | Response> extends Error {
  132. readonly code: number;
  133. readonly statusCode: number;
  134. readonly url: string;
  135. constructor(public readonly res: T, public readonly info: RequestInfo, ...args: any[]) {
  136. const statusCode = 'statusCode' in res ? res.statusCode : res.status;
  137. super('HTTP ' + statusCode + ' ' + args.map(_ => inspect(_)).join(' '));
  138. this.url = typeof info === 'string'
  139. ? info
  140. : ('url' in info
  141. ? info.url
  142. : info.href);
  143. // eslint-disable-next-line sukka/unicorn/custom-error-definition -- deliberatly use previous name
  144. this.name = this.constructor.name;
  145. this.res = res;
  146. this.code = statusCode;
  147. this.statusCode = statusCode;
  148. }
  149. }
  150. export const defaultRequestInit = {
  151. headers: {
  152. 'User-Agent': 'node-fetch'
  153. }
  154. };
  155. export async function $$fetch(url: RequestInfo, init: RequestInit = defaultRequestInit) {
  156. init.dispatcher = agent;
  157. try {
  158. const res = await undici.fetch(url, init);
  159. if (res.status >= 400) {
  160. throw new ResponseError(res, url);
  161. }
  162. if ((res.status < 200 || res.status > 299) && res.status !== 304) {
  163. throw new ResponseError(res, url);
  164. }
  165. return res;
  166. } catch (err: unknown) {
  167. if (isAbortErrorLike(err)) {
  168. console.log(picocolors.gray('[fetch abort]'), url);
  169. } else {
  170. console.log(picocolors.gray('[fetch fail]'), url, err);
  171. }
  172. throw err;
  173. }
  174. }
  175. export { $$fetch as '~fetch' };
  176. /**
  177. * dohdec constructs its own `Request` object for its `hooks` from `globalThis.Request`
  178. *
  179. * But we are using `undici.fetch` instead of `globalThis.fetch`, hence the version
  180. * mismatch.
  181. *
  182. * undici, on the other hand, use `instanceof Request` internally for narrowing, resulting
  183. * in it treats foreign `Request` objects as `URL` and try to parse them as URLs, causing
  184. * `TypeError: Failed to construct 'URL': [object Request]`
  185. *
  186. * See also https://github.com/nodejs/undici/issues/2155
  187. *
  188. * We already know that dohdec will only pass one `Request` object to `fetch` because
  189. * of its internal `hooks`:
  190. *
  191. * https://github.com/hildjj/dohdec/blob/d2f763db62d46f505d109be12bc697224cd42f93/pkg/dohdec/lib/doh.js#L291
  192. */
  193. export async function fetchForDoH(input: RequestInfo, _init?: RequestInit) {
  194. if (typeof input === 'object' && 'url' in input) {
  195. // Read body as ArrayBuffer before re-wrapping. The original body is a ReadableStream
  196. // from a foreign context (different undici instance / Node.js globals). Passing it
  197. // directly to new UndiciRequest fails undici's instanceof ReadableStream check and
  198. // silently drops the body. ArrayBuffer is a plain value with no cross-context issues,
  199. // and also allows the retry interceptor to re-send the body on retries.
  200. const body = input.body === null ? null : await input.arrayBuffer();
  201. input = new UndiciRequest(input.url, {
  202. method: input.method,
  203. mode: input.mode,
  204. credentials: input.credentials,
  205. cache: input.cache,
  206. redirect: input.redirect,
  207. integrity: input.integrity,
  208. keepalive: input.keepalive,
  209. signal: input.signal,
  210. headers: input.headers,
  211. body,
  212. referrer: '',
  213. referrerPolicy: 'no-referrer',
  214. dispatcher: agent
  215. });
  216. }
  217. // DoH servers may return a valid DNS wire format body with a non-200 status
  218. // (e.g. 503 with a DNS SERVFAIL). Let the DoH client parse the body and decide
  219. // — never throw on HTTP status here.
  220. return undici.fetch(input);
  221. }
  222. /** @deprecated -- undici.requests doesn't support gzip/br/deflate, and has difficulty w/ undidi cache */
  223. export async function requestWithLog(url: string, opt?: Parameters<typeof undici.request>[1]) {
  224. opt ??= {};
  225. opt.dispatcher = agent;
  226. try {
  227. const res = await undici.request(url, opt);
  228. if (res.statusCode >= 400) {
  229. throw new ResponseError(res, url);
  230. }
  231. if ((res.statusCode < 200 || res.statusCode > 299) && res.statusCode !== 304) {
  232. throw new ResponseError(res, url);
  233. }
  234. return res;
  235. } catch (err: unknown) {
  236. if (isAbortErrorLike(err)) {
  237. console.log(picocolors.gray('[fetch abort]'), url);
  238. } else {
  239. console.log(picocolors.gray('[fetch fail]'), url, { name: err instanceof Error ? err.name : undefined }, err);
  240. }
  241. throw err;
  242. }
  243. }