{"version":3,"file":"progress.min.mjs","sources":["../../../src/addons/progress.ts"],"sourcesContent":["import type { ConfiguredMiddleware, WretchAddon, WretchResponseChain } from \"../types.js\"\n\nexport interface ProgressResolver {\n  /**\n   * Provides a way to register a callback to be invoked one or multiple times during the download.\n   * The callback receives the current progress as two arguments, the number of bytes loaded and the total number of bytes to load.\n   *\n   * _Under the hood: this method adds a middleware to the chain that will intercept the response and replace the body with a new one that will emit the progress event._\n   *\n   * ```js\n   * import ProgressAddon from \"wretch/addons/progress\"\n   *\n   * wretch(\"some_url\")\n   *   // Register the addon\n   *   .addon(ProgressAddon())\n   *   .get()\n   *   // Log the progress as a percentage of completion\n   *   .progress((loaded, total) => console.log(`${(loaded / total * 100).toFixed(0)}%`))\n   * ```\n   *\n   * @param onProgress - A callback that will be called one or multiple times with the number of bytes loaded and the total number of bytes to load.\n   */\n  progress: <T, C extends ProgressResolver, R>(\n    this: C & WretchResponseChain<T, C, R>,\n    onProgress: (loaded: number, total: number) => void\n  ) => this\n}\n\n/**\n * Adds the ability to monitor progress when downloading a response.\n *\n * _Compatible with all platforms implementing the [TransformStream WebAPI](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream#browser_compatibility)._\n *\n * ```js\n * import ProgressAddon from \"wretch/addons/progress\"\n *\n * wretch(\"some_url\")\n *   // Register the addon\n *   .addon(ProgressAddon())\n *   .get()\n *   // Log the progress as a percentage of completion\n *   .progress((loaded, total) => console.log(`${(loaded / total * 100).toFixed(0)}%`))\n * ```\n */\nconst progress: () => WretchAddon<unknown, ProgressResolver> = () => {\n  const cb = {\n    ref: null\n  }\n\n  const transformMiddleware: ConfiguredMiddleware = next => (url, opts) => {\n    let loaded = 0\n    let total = 0\n    return next(url, opts).then(response => {\n      try {\n        const contentLength = response.headers.get(\"content-length\")\n        total = contentLength ? +contentLength : null\n        const transform = new TransformStream({\n          transform(chunk, controller) {\n            loaded += chunk.length\n            if (total < loaded) {\n              total = loaded\n            }\n            if (cb.ref) {\n              cb.ref(loaded, total)\n            }\n            controller.enqueue(chunk)\n          }\n        })\n        return new Response(response.body.pipeThrough(transform), response)\n      } catch (e) {\n        return response\n      }\n    })\n  }\n\n  return {\n    beforeRequest(wretch) {\n      return wretch._middlewares.push(transformMiddleware)\n    },\n    resolver: {\n      progress(onProgress: (loaded: number, total: number) => void) {\n        cb.ref = onProgress\n        return this\n      }\n    },\n  }\n}\n\nexport default progress\n"],"names":["progress","cb","ref","transformMiddleware","next","url","opts","loaded","total","then","response","contentLength","headers","get","transform","TransformStream","chunk","controller","length","enqueue","Response","body","pipeThrough","e","beforeRequest","wretch","_middlewares","push","resolver","onProgress","this"],"mappings":"AA4CM,MAAAA,EAAyD,KAC7D,MAAMC,EAAK,CACTC,IAAK,MAGDC,EAA4CC,GAAQ,CAACC,EAAKC,KAC9D,IAAIC,EAAS,EACTC,EAAQ,EACZ,OAAOJ,EAAKC,EAAKC,GAAMG,MAAKC,IAC1B,IACE,MAAMC,EAAgBD,EAASE,QAAQC,IAAI,kBAC3CL,EAAQG,GAAiBA,EAAgB,KACzC,MAAMG,EAAY,IAAIC,gBAAgB,CACpCD,UAAUE,EAAOC,GACfV,GAAUS,EAAME,OACZV,EAAQD,IACVC,EAAQD,GAENN,EAAGC,KACLD,EAAGC,IAAIK,EAAQC,GAEjBS,EAAWE,QAAQH,EACpB,IAEH,OAAO,IAAII,SAASV,EAASW,KAAKC,YAAYR,GAAYJ,EAG3D,CAFC,MAAOa,GACP,OAAOb,CACR,IACD,EAGJ,MAAO,CACLc,cAAcC,GACLA,EAAOC,aAAaC,KAAKxB,GAElCyB,SAAU,CACR5B,SAAS6B,GAEP,OADA5B,EAAGC,IAAM2B,EACFC,IACR,GAEJ"}