UNPKG

wretch

Version:

A tiny wrapper built around fetch with an intuitive syntax.

1 lines 5.04 kB
{"version":3,"file":"perfs.min.mjs","sources":["../../../src/addons/perfs.ts"],"sourcesContent":["import type { WretchResponseChain, WretchAddon } from \"../types.js\"\n\nconst onMatch = (entries, name, callback, _performance) => {\n if (!entries.getEntriesByName)\n return false\n const matches = entries.getEntriesByName(name)\n if (matches && matches.length > 0) {\n callback(matches.reverse()[0])\n if (_performance.clearMeasures)\n _performance.clearMeasures(name)\n utils.callbacks.delete(name)\n\n if (utils.callbacks.size < 1) {\n utils.observer.disconnect()\n if (_performance.clearResourceTimings) {\n _performance.clearResourceTimings()\n }\n }\n return true\n }\n return false\n}\n\nconst lazyObserver = (_performance, _observer) => {\n if (!utils.observer && _performance && _observer) {\n utils.observer = new _observer(entries => {\n utils.callbacks.forEach((callback, name) => {\n onMatch(entries, name, callback, _performance)\n })\n })\n if (_performance.clearResourceTimings)\n _performance.clearResourceTimings()\n }\n return utils.observer\n}\n\nconst utils = {\n callbacks: new Map(),\n observer: null,\n observe: (name, callback, config) => {\n if (!name || !callback)\n return\n\n const _performance = config.polyfill(\"performance\", false)\n const _observer = config.polyfill(\"PerformanceObserver\", false)\n\n if (!lazyObserver(_performance, _observer))\n return\n\n if (!onMatch(_performance, name, callback, _performance)) {\n if (utils.callbacks.size < 1)\n utils.observer.observe({ entryTypes: [\"resource\", \"measure\"] })\n utils.callbacks.set(name, callback)\n }\n\n }\n}\n\nexport interface PerfsAddon {\n /**\n * Performs a callback on the API performance timings of the request.\n *\n * Warning: Still experimental on browsers and node.js\n */\n perfs: <T, C extends PerfsAddon, R>(this: C & WretchResponseChain<T, C, R>, cb?: (timing: any) => void) => this,\n}\n\n/**\n * Adds the ability to measure requests using the Performance Timings API.\n *\n * Uses the Performance API\n * ([browsers](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API) &\n * [node.js](https://nodejs.org/api/perf_hooks.html)) to expose timings related to\n * the underlying request.\n *\n * Browser timings are very accurate, node.js only contains raw measures.\n *\n * ```js\n * import PerfsAddon from \"wretch/addons/perfs\"\n *\n * // Use perfs() before the response types (text, json, ...)\n * wretch(\"...\")\n * .addon(PerfsAddon())\n * .get()\n * .perfs((timings) => {\n * // Will be called when the timings are ready.\n * console.log(timings.startTime);\n * })\n * .res();\n *\n * ```\n *\n * For node.js, there is a little extra work to do :\n *\n * ```js\n * // Node.js only\n * const { performance, PerformanceObserver } = require(\"perf_hooks\");\n *\n * wretch.polyfills({\n * fetch: function (url, opts) {\n * performance.mark(url + \" - begin\");\n * return fetch(url, opts).then(res => {\n * performance.mark(url + \" - end\");\n * setTimeout(() => performance.measure(res.url, url + \" - begin\", url + \" - end\"), 0);\n * return res;\n * });\n * },\n * // other polyfills…\n * performance: performance,\n * PerformanceObserver: PerformanceObserver,\n * });\n * ```\n */\nconst perfs: () => WretchAddon<unknown, PerfsAddon> = () => ({\n resolver: {\n perfs(cb) {\n this._fetchReq.then(res => utils.observe(res.url, cb, this._wretchReq._config)).catch(() => {/* swallow */ })\n return this\n },\n }\n})\n\nexport default perfs\n"],"names":["onMatch","entries","name","callback","_performance","getEntriesByName","matches","length","reverse","clearMeasures","utils","callbacks","delete","size","observer","disconnect","clearResourceTimings","Map","observe","config","polyfill","_observer","forEach","lazyObserver","entryTypes","set","perfs","resolver","cb","this","_fetchReq","then","res","url","_wretchReq","_config","catch"],"mappings":"AAEA,MAAMA,EAAU,CAACC,EAASC,EAAMC,EAAUC,KACxC,IAAKH,EAAQI,iBACX,OAAO,EACT,MAAMC,EAAUL,EAAQI,iBAAiBH,GACzC,OAAII,GAAWA,EAAQC,OAAS,GAC9BJ,EAASG,EAAQE,UAAU,IACvBJ,EAAaK,eACfL,EAAaK,cAAcP,GAC7BQ,EAAMC,UAAUC,OAAOV,GAEnBQ,EAAMC,UAAUE,KAAO,IACzBH,EAAMI,SAASC,aACXX,EAAaY,sBACfZ,EAAaY,wBAGV,GAEF,CAAK,EAgBRN,EAAQ,CACZC,UAAW,IAAIM,IACfH,SAAU,KACVI,QAAS,CAAChB,EAAMC,EAAUgB,KACxB,IAAKjB,IAASC,EACZ,OAEF,MAAMC,EAAee,EAAOC,SAAS,cAAe,GApBnC,EAAChB,EAAciB,MAC7BX,EAAMI,UAAYV,GAAgBiB,IACrCX,EAAMI,SAAW,IAAIO,GAAUpB,IAC7BS,EAAMC,UAAUW,SAAQ,CAACnB,EAAUD,KACjCF,EAAQC,EAASC,EAAMC,EAAUC,EAAa,GAC9C,IAEAA,EAAaY,sBACfZ,EAAaY,wBAEVN,EAAMI,UAaNS,CAAanB,EAFAe,EAAOC,SAAS,sBAAuB,MAKpDpB,EAAQI,EAAcF,EAAMC,EAAUC,KACrCM,EAAMC,UAAUE,KAAO,GACzBH,EAAMI,SAASI,QAAQ,CAAEM,WAAY,CAAC,WAAY,aACpDd,EAAMC,UAAUc,IAAIvB,EAAMC,IAC3B,GA4DCuB,EAAgD,KAAO,CAC3DC,SAAU,CACRD,MAAME,GAEJ,OADAC,KAAKC,UAAUC,MAAKC,GAAOtB,EAAMQ,QAAQc,EAAIC,IAAKL,EAAIC,KAAKK,WAAWC,WAAUC,OAAM,SAC/EP,IACR"}