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C;IACpC,OAAO,CAAC,IAAI,GAAG,GAAG,GAAG,CAAC,QAAQ,IAAI,GAAG,GAAG,GAAG,CAAC,MAAM,IAAI,EAAE,EAAE,CAAC;IAC3D,OAAO,OAAO,CAAC,QAAQ,CAAC;IACxB,OAAO,OAAO,CAAC,MAAM,CAAC;IACtB,OAAO,OAAO,CAAC;AAChB,CAAC,CAAC;AAEF,MAAM,kBAAkB,GAAG,CAAC,GAAQ,EAAE,EAAE;AACvC,6CAA6C;AAC7C,yBAAyB;AACzB,iCAAiC;AACjC,6DAA6D;AAC7D,2CAA2C;AAC3C,gCAAgC;AAChC,qCAAqC;AACrC,CAAC;IACA,QAAQ,EAAE,GAAG,CAAC,QAAQ;IACtB,IAAI,EAAE,GAAG,CAAC,IAAI;IACd,QAAQ,EAAE,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,IAAI,IAAI,WAAW;IACjD,IAAI,EAAE,GAAG,CAAC,IAAI;IACd,QAAQ,EAAE,GAAG,CAAC,QAAQ;IACtB,MAAM,EAAE,GAAG,CAAC,MAAM;CAClB,CAAC,CAAC;AAEJ,MAAM,cAAc,GAAG,CAAC,OAA4B,EAAE,EAAE;IACvD,MAAM,MAAM,GAAQ,EAAE,CAAC;IACvB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE;QACxC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;KAC3C;IAED,OAAO,MAAM,CAAC;AACf,CAAC,CAAC;AAEF,eAAe,gBAAgB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,MAAM,CAAC,MAAM,UAAU,GAAG,YAAY,CAAC","sourcesContent":["import EventEmitter from 'node:events';\nimport urlLib from 'node:url';\nimport crypto from 'node:crypto';\nimport stream, {PassThrough as PassThroughStream} from 'node:stream';\nimport {IncomingMessage} from 'node:http';\nimport normalizeUrl from 'normalize-url';\nimport getStream from 'get-stream';\nimport CachePolicy from 'http-cache-semantics';\nimport Response from 'responselike';\nimport Keyv from 'keyv';\nimport mimicResponse from 'mimic-response';\nimport {RequestFn, StorageAdapter, CacheResponse, CacheValue, CacheableOptions, UrlOption, CacheError, RequestError, Emitter, CacheableRequestFunction} from './types.js';\n\ntype Func = (...args: any[]) => any;\n\nclass CacheableRequest {\n\tcache: StorageAdapter;\n\tcacheRequest: RequestFn;\n\thooks: Map = new Map();\n\tconstructor(cacheRequest: RequestFn, cacheAdapter?: StorageAdapter | string) {\n\t\tif (cacheAdapter instanceof Keyv) {\n\t\t\tthis.cache = cacheAdapter;\n\t\t} else if (typeof cacheAdapter === 'string') {\n\t\t\tthis.cache = new Keyv({\n\t\t\t\turi: cacheAdapter,\n\t\t\t\tnamespace: 'cacheable-request',\n\t\t\t});\n\t\t} else {\n\t\t\tthis.cache = new Keyv({\n\t\t\t\tstore: cacheAdapter,\n\t\t\t\tnamespace: 'cacheable-request',\n\t\t\t});\n\t\t}\n\n\t\tthis.request = this.request.bind(this);\n\t\tthis.cacheRequest = cacheRequest;\n\t}\n\n\trequest = () => (options: CacheableOptions,\n\t\tcb?: (response: CacheResponse) => void): Emitter => {\n\t\tlet url;\n\t\tif (typeof options === 'string') {\n\t\t\turl = normalizeUrlObject(urlLib.parse(options));\n\t\t\toptions = {};\n\t\t} else if (options instanceof urlLib.URL) {\n\t\t\turl = normalizeUrlObject(urlLib.parse(options.toString()));\n\t\t\toptions = {};\n\t\t} else {\n\t\t\tconst [pathname, ...searchParts] = (options.path ?? '').split('?');\n\t\t\tconst search = searchParts.length > 0\n\t\t\t\t? `?${searchParts.join('?')}`\n\t\t\t\t: '';\n\t\t\turl = normalizeUrlObject({...options, pathname, search});\n\t\t}\n\n\t\toptions = {\n\t\t\theaders: {},\n\t\t\tmethod: 'GET',\n\t\t\tcache: true,\n\t\t\tstrictTtl: false,\n\t\t\tautomaticFailover: false,\n\t\t\t...options,\n\t\t\t...urlObjectToRequestOptions(url),\n\t\t};\n\t\toptions.headers = Object.fromEntries(entries(options.headers).map(([key, value]) => [(key as string).toLowerCase(), value]));\n\t\tconst ee: Emitter = new EventEmitter() as Emitter;\n\t\tconst normalizedUrlString = normalizeUrl(urlLib.format(url), {\n\t\t\tstripWWW: false, // eslint-disable-line @typescript-eslint/naming-convention\n\t\t\tremoveTrailingSlash: false,\n\t\t\tstripAuthentication: false,\n\t\t});\n\t\tlet key = `${options.method}:${normalizedUrlString}`;\n\t\t// POST, PATCH, and PUT requests may be cached, depending on the response\n\t\t// cache-control headers. As a result, the body of the request should be\n\t\t// added to the cache key in order to avoid collisions.\n\t\tif (options.body && options.method !== undefined && ['POST', 'PATCH', 'PUT'].includes(options.method)) {\n\t\t\tif (options.body instanceof stream.Readable) {\n\t\t\t\t// Streamed bodies should completely skip the cache because they may\n\t\t\t\t// or may not be hashable and in either case the stream would need to\n\t\t\t\t// close before the cache key could be generated.\n\t\t\t\toptions.cache = false;\n\t\t\t} else {\n\t\t\t\tkey += `:${crypto.createHash('md5').update(options.body).digest('hex')}`;\n\t\t\t}\n\t\t}\n\n\t\tlet revalidate: any = false;\n\t\tlet madeRequest = false;\n\t\tconst makeRequest = (options_: any) => {\n\t\t\tmadeRequest = true;\n\t\t\tlet requestErrored = false;\n\t\t\tlet requestErrorCallback: (...args: any[]) => void = () => {/* do nothing */};\n\n\t\t\tconst requestErrorPromise = new Promise(resolve => {\n\t\t\t\trequestErrorCallback = () => {\n\t\t\t\t\tif (!requestErrored) {\n\t\t\t\t\t\trequestErrored = true;\n\t\t\t\t\t\tresolve();\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t});\n\t\t\tconst handler = async (response: any) => {\n\t\t\t\tif (revalidate) {\n\t\t\t\t\tresponse.status = response.statusCode;\n\t\t\t\t\tconst revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(options_, response);\n\t\t\t\t\tif (!revalidatedPolicy.modified) {\n\t\t\t\t\t\tresponse.resume();\n\t\t\t\t\t\tawait new Promise(resolve => {\n\t\t\t\t\t\t\t// Skipping 'error' handler cause 'error' event should't be emitted for 304 response\n\t\t\t\t\t\t\tresponse\n\t\t\t\t\t\t\t\t.once('end', resolve);\n\t\t\t\t\t\t});\n\t\t\t\t\t\tconst headers = convertHeaders(revalidatedPolicy.policy.responseHeaders());\n\t\t\t\t\t\tresponse = new Response({statusCode: revalidate.statusCode, headers, body: revalidate.body, url: revalidate.url});\n\t\t\t\t\t\tresponse.cachePolicy = revalidatedPolicy.policy;\n\t\t\t\t\t\tresponse.fromCache = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (!response.fromCache) {\n\t\t\t\t\tresponse.cachePolicy = new CachePolicy(options_, response, options_);\n\t\t\t\t\tresponse.fromCache = false;\n\t\t\t\t}\n\n\t\t\t\tlet clonedResponse;\n\t\t\t\tif (options_.cache && response.cachePolicy.storable()) {\n\t\t\t\t\tclonedResponse = cloneResponse(response);\n\t\t\t\t\t(async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst bodyPromise = getStream.buffer(response);\n\t\t\t\t\t\t\tawait Promise.race([\n\t\t\t\t\t\t\t\trequestErrorPromise,\n\t\t\t\t\t\t\t\tnew Promise(resolve => response.once('end', resolve)), // eslint-disable-line no-promise-executor-return\n\t\t\t\t\t\t\t\tnew Promise(resolve => response.once('close', resolve)), // eslint-disable-line no-promise-executor-return\n\t\t\t\t\t\t\t]);\n\t\t\t\t\t\t\tconst body = await bodyPromise;\n\t\t\t\t\t\t\tlet value: CacheValue = {\n\t\t\t\t\t\t\t\turl: response.url,\n\t\t\t\t\t\t\t\tstatusCode: response.fromCache ? revalidate.statusCode : response.statusCode,\n\t\t\t\t\t\t\t\tbody,\n\t\t\t\t\t\t\t\tcachePolicy: response.cachePolicy.toObject(),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tlet ttl = options_.strictTtl ? response.cachePolicy.timeToLive() : undefined;\n\t\t\t\t\t\t\tif (options_.maxTtl) {\n\t\t\t\t\t\t\t\tttl = ttl ? Math.min(ttl, options_.maxTtl) : options_.maxTtl;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (this.hooks.size > 0) {\n\t\t\t\t\t\t\t\t/* eslint-disable no-await-in-loop */\n\t\t\t\t\t\t\t\tfor (const key_ of this.hooks.keys()) {\n\t\t\t\t\t\t\t\t\tvalue = await this.runHook(key_, value, response);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t/* eslint-enable no-await-in-loop */\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tawait this.cache.set(key, value, ttl);\n\t\t\t\t\t\t} catch (error: any) {\n\t\t\t\t\t\t\tee.emit('error', new CacheError(error));\n\t\t\t\t\t\t}\n\t\t\t\t\t})();\n\t\t\t\t} else if (options_.cache && revalidate) {\n\t\t\t\t\t(async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tawait this.cache.delete(key);\n\t\t\t\t\t\t} catch (error: any) {\n\t\t\t\t\t\t\tee.emit('error', new CacheError(error));\n\t\t\t\t\t\t}\n\t\t\t\t\t})();\n\t\t\t\t}\n\n\t\t\t\tee.emit('response', clonedResponse ?? response);\n\t\t\t\tif (typeof cb === 'function') {\n\t\t\t\t\tcb(clonedResponse ?? response);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\ttry {\n\t\t\t\tconst request_ = this.cacheRequest(options_, handler);\n\t\t\t\trequest_.once('error', requestErrorCallback);\n\t\t\t\trequest_.once('abort', requestErrorCallback);\n\t\t\t\trequest_.once('destroy', requestErrorCallback);\n\t\t\t\tee.emit('request', request_);\n\t\t\t} catch (error: any) {\n\t\t\t\tee.emit('error', new RequestError(error));\n\t\t\t}\n\t\t};\n\n\t\t(async () => {\n\t\t\tconst get = async (options_: any) => {\n\t\t\t\tawait Promise.resolve();\n\t\t\t\tconst cacheEntry = options_.cache ? await this.cache.get(key) : undefined;\n\n\t\t\t\tif (cacheEntry === undefined && !options_.forceRefresh) {\n\t\t\t\t\tmakeRequest(options_);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst policy = CachePolicy.fromObject(cacheEntry.cachePolicy);\n\t\t\t\tif (policy.satisfiesWithoutRevalidation(options_) && !options_.forceRefresh) {\n\t\t\t\t\tconst headers = convertHeaders(policy.responseHeaders());\n\t\t\t\t\tconst response: any = new Response({statusCode: cacheEntry.statusCode, headers, body: cacheEntry.body, url: cacheEntry.url});\n\t\t\t\t\tresponse.cachePolicy = policy;\n\t\t\t\t\tresponse.fromCache = true;\n\t\t\t\t\tee.emit('response', response);\n\t\t\t\t\tif (typeof cb === 'function') {\n\t\t\t\t\t\tcb(response);\n\t\t\t\t\t}\n\t\t\t\t} else if (policy.satisfiesWithoutRevalidation(options_) && Date.now() >= policy.timeToLive() && options_.forceRefresh) {\n\t\t\t\t\tawait this.cache.delete(key);\n\t\t\t\t\toptions_.headers = policy.revalidationHeaders(options_);\n\t\t\t\t\tmakeRequest(options_);\n\t\t\t\t} else {\n\t\t\t\t\trevalidate = cacheEntry;\n\t\t\t\t\toptions_.headers = policy.revalidationHeaders(options_);\n\t\t\t\t\tmakeRequest(options_);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst errorHandler = (error: Error) => ee.emit('error', new CacheError(error));\n\t\t\tif (this.cache instanceof Keyv) {\n\t\t\t\tconst cachek = this.cache;\n\t\t\t\tcachek.once('error', errorHandler);\n\t\t\t\tee.on('error', () => cachek.removeListener('error', errorHandler));\n\t\t\t\tee.on('response', () => cachek.removeListener('error', errorHandler));\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tawait get(options);\n\t\t\t} catch (error: any) {\n\t\t\t\tif (options.automaticFailover && !madeRequest) {\n\t\t\t\t\tmakeRequest(options);\n\t\t\t\t}\n\n\t\t\t\tee.emit('error', new CacheError(error));\n\t\t\t}\n\t\t})();\n\n\t\treturn ee;\n\t};\n\n\taddHook = (name: string, fn: Func) => {\n\t\tif (!this.hooks.has(name)) {\n\t\t\tthis.hooks.set(name, fn);\n\t\t}\n\t};\n\n\tremoveHook = (name: string) => this.hooks.delete(name);\n\n\tgetHook = (name: string) => this.hooks.get(name);\n\n\trunHook = async (name: string, ...args: any[]): Promise => this.hooks.get(name)?.(...args);\n}\n\nconst entries = Object.entries as (object: T) => Array<[keyof T, T[keyof T]]>;\n\nconst cloneResponse = (response: IncomingMessage) => {\n\tconst clone = new PassThroughStream({autoDestroy: false});\n\tmimicResponse(response, clone);\n\n\treturn response.pipe(clone);\n};\n\nconst urlObjectToRequestOptions = (url: any) => {\n\tconst options: UrlOption = {...url};\n\toptions.path = `${url.pathname || '/'}${url.search || ''}`;\n\tdelete options.pathname;\n\tdelete options.search;\n\treturn options;\n};\n\nconst normalizeUrlObject = (url: any) =>\n\t// If url was parsed by url.parse or new URL:\n\t// - hostname will be set\n\t// - host will be hostname[:port]\n\t// - port will be set if it was explicit in the parsed string\n\t// Otherwise, url was from request options:\n\t// - hostname or host may be set\n\t// - host shall not have port encoded\n\t({\n\t\tprotocol: url.protocol,\n\t\tauth: url.auth,\n\t\thostname: url.hostname || url.host || 'localhost',\n\t\tport: url.port,\n\t\tpathname: url.pathname,\n\t\tsearch: url.search,\n\t});\n\nconst convertHeaders = (headers: CachePolicy.Headers) => {\n\tconst result: any = [];\n\tfor (const name of Object.keys(headers)) {\n\t\tresult[name.toLowerCase()] = headers[name];\n\t}\n\n\treturn result;\n};\n\nexport default CacheableRequest;\nexport * from './types.js';\nexport const onResponse = 'onResponse';\n"]}