UNPKG

idmp

Version:

A lightweight TypeScript library for deduplicating and caching async function calls with automatic retries, designed for idempotent network requests in React and Node.js.

101 lines (100 loc) 3.5 kB
/*! idmp v4.1.0 | (c) github/haozi | MIT */ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); //#region \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion let crypto = require("crypto"); let fs_extra = require("fs-extra"); fs_extra = __toESM(fs_extra); let idmp = require("idmp"); let json_web3 = require("json-web3"); let os = require("os"); os = __toESM(os); let path = require("path"); path = __toESM(path); //#region src/index.ts var __filename$1 = (0, require("url").fileURLToPath)({}.url); var md5 = (data) => (0, crypto.createHash)("md5").update(data).digest("hex"); var deSerialize = (data) => (0, json_web3.parse_UNSAFE)(data); var prefix = md5(__filename$1); var udf = void 0; var encode = encodeURIComponent; var cacheDir = path.default.resolve(os.default.tmpdir(), "idmp"); var getCachePath = (globalKey) => path.default.resolve(cacheDir, "v4/node", prefix, encode(globalKey)); var setData = async (key, data, maxAge) => { if (!key) return; const cachePath = getCachePath(key); fs_extra.default.ensureFileSync(cachePath); fs_extra.default.outputFileSync(cachePath, (0, json_web3.stringify_UNSAFE)({ t: Date.now(), a: maxAge, d: data })); }; var getData = async (key) => { if (!key) return udf; const cachePath = getCachePath(key); let localData; try { localData = deSerialize(fs_extra.default.readFileSync(cachePath, "utf-8")); } catch {} if (localData === udf) return udf; const { t, a: maxAge, d: data } = localData; if (Date.now() - t > maxAge) { fs_extra.default.removeSync(cachePath); return udf; } return data; }; var fsIdmpWrap = (_idmp, namespace, extraOptions) => { const newIdmp = (globalKey, promiseFunc, options) => { globalKey = `${namespace}_${globalKey}`; const finalOptions = (0, idmp.getOptions)(options); const { useMemoryCache } = extraOptions || {}; return _idmp(globalKey, async () => { const localData = await getData(globalKey); if (localData !== udf) return localData; const memoryData = await promiseFunc(); if (memoryData !== udf) setData(globalKey, memoryData, finalOptions.maxAge); return memoryData; }, { ...options, maxAge: useMemoryCache ? finalOptions.maxAge : 200 }); }; newIdmp.flush = (globalKey) => { _idmp.flush(globalKey); fs_extra.default.removeSync(getCachePath(globalKey)); }; newIdmp.flushAll = () => { _idmp.flushAll(); fs_extra.default.removeSync(cacheDir); }; return newIdmp; }; //#endregion exports.cacheDir = cacheDir; exports.default = fsIdmpWrap; exports.getCachePath = getCachePath;