{"version":3,"file":"cache-revalidation-handler.mjs","sources":["../../../lib/handler/cache-revalidation-handler.js"],"sourcesContent":["'use strict'\n\nconst assert = require('node:assert')\n\n/**\n * This takes care of revalidation requests we send to the origin. If we get\n *  a response indicating that what we have is cached (via a HTTP 304), we can\n *  continue using the cached value. Otherwise, we'll receive the new response\n *  here, which we then just pass on to the next handler (most likely a\n *  CacheHandler). Note that this assumes the proper headers were already\n *  included in the request to tell the origin that we want to revalidate the\n *  response (i.e. if-modified-since or if-none-match).\n *\n * @see https://www.rfc-editor.org/rfc/rfc9111.html#name-validation\n *\n * @implements {import('../../types/dispatcher.d.ts').default.DispatchHandler}\n */\nclass CacheRevalidationHandler {\n  #successful = false\n\n  /**\n   * @type {((success: boolean, context?: any, statusCode?: number, headers?: import('../../types/header.d.ts').IncomingHttpHeaders) => void) | null}\n   */\n  #callback\n\n  /**\n   * @type {(import('../../types/dispatcher.d.ts').default.DispatchHandler)}\n   */\n  #handler\n\n  #context\n\n  /**\n   * @type {boolean}\n   */\n  #allowErrorStatusCodes\n\n  /**\n   * @param {(success: boolean, context?: any, statusCode?: number, headers?: import('../../types/header.d.ts').IncomingHttpHeaders) => void} callback Function to call if the cached value is valid\n   * @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler\n   * @param {boolean} allowErrorStatusCodes\n   */\n  constructor (callback, handler, allowErrorStatusCodes) {\n    if (typeof callback !== 'function') {\n      throw new TypeError('callback must be a function')\n    }\n\n    this.#callback = callback\n    this.#handler = handler\n    this.#allowErrorStatusCodes = allowErrorStatusCodes\n  }\n\n  onRequestStart (_, context) {\n    this.#successful = false\n    this.#context = context\n  }\n\n  onRequestUpgrade (controller, statusCode, headers, socket) {\n    this.#handler.onRequestUpgrade?.(controller, statusCode, headers, socket)\n  }\n\n  onResponseStart (\n    controller,\n    statusCode,\n    headers,\n    statusMessage\n  ) {\n    assert(this.#callback != null)\n\n    // https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-a-validation-respo\n    // https://datatracker.ietf.org/doc/html/rfc5861#section-4\n    this.#successful = statusCode === 304 ||\n      (this.#allowErrorStatusCodes && statusCode >= 500 && statusCode <= 504)\n    this.#callback(this.#successful, this.#context, statusCode, headers)\n    this.#callback = null\n\n    if (this.#successful) {\n      return true\n    }\n\n    this.#handler.onRequestStart?.(controller, this.#context)\n    this.#handler.onResponseStart?.(\n      controller,\n      statusCode,\n      headers,\n      statusMessage\n    )\n  }\n\n  onResponseData (controller, chunk) {\n    if (this.#successful) {\n      return\n    }\n\n    return this.#handler.onResponseData?.(controller, chunk)\n  }\n\n  onResponseEnd (controller, trailers) {\n    if (this.#successful) {\n      return\n    }\n\n    this.#handler.onResponseEnd?.(controller, trailers)\n  }\n\n  onResponseError (controller, err) {\n    if (this.#successful) {\n      return\n    }\n\n    if (this.#callback) {\n      // Serve the stale cached response on a connection error, per stale-if-error:\n      //  RFC 5861 counts an unreachable origin (a would-be 5xx) as an error.\n      // https://datatracker.ietf.org/doc/html/rfc5861#section-4\n      if (this.#allowErrorStatusCodes) {\n        this.#successful = true\n        this.#callback(true, this.#context)\n        this.#callback = null\n        return\n      }\n\n      this.#callback(false)\n      this.#callback = null\n    }\n\n    if (typeof this.#handler.onResponseError === 'function') {\n      this.#handler.onResponseError(controller, err)\n    } else {\n      throw err\n    }\n  }\n}\n\nmodule.exports = CacheRevalidationHandler\n"],"names":["assert","require$$0","cacheRevalidationHandler","successful","callback","handler","context","allowErrorStatusCodes","constructor","TypeError","this","onRequestStart","_","onRequestUpgrade","controller","statusCode","headers","socket","onResponseStart","statusMessage","onResponseData","chunk","onResponseEnd","trailers","onResponseError","err"],"mappings":"iDAEA,MAAMA,OAASC;;;;;;;;;;;;;GAmIf,IAAAC,yBApHA,MACEC,aAAc;;;MAKdC;;;KAKAC,SAEAC;;;KAKAC;;;;;KAOA,WAAAC,CAAaJ,SAAUC,QAASE,uBAC9B,GAAwB,mBAAbH,SACT,MAAM,IAAIK,UAAU,+BAGtBC,MAAKN,SAAYA,SACjBM,M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