@openclaw/whatsapp 2026.7.1-beta.2 → 2026.7.1-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{access-control-88UPniDc.js → access-control-DYE5dXna.js} +5 -3
- package/dist/{accounts-DgViSyJx.js → accounts-4YgwroRU.js} +1 -1
- package/dist/{action-runtime-hqza0J8B.js → action-runtime-DPahAqxV.js} +2 -2
- package/dist/action-runtime-api.js +1 -1
- package/dist/action-runtime.runtime.js +1 -1
- package/dist/{active-listener-CFwkn3ho.js → active-listener-B4SDebQ6.js} +1 -1
- package/dist/api.js +14 -13
- package/dist/{approval-handler.runtime-9_SV7vhc.js → approval-handler.runtime-DNWS4aYQ.js} +1 -1
- package/dist/auth-presence.js +1 -1
- package/dist/{auth-store-ECx4h57q.js → auth-store-Db-wfApd.js} +23 -13
- package/dist/call-tool-api.js +233 -0
- package/dist/{channel-fcXxWXic.js → channel-BWp7h1uY.js} +44 -18
- package/dist/channel-config-api.js +1 -1
- package/dist/channel-plugin-api.js +1 -1
- package/dist/{channel-react-action-Dgy9uUMl.js → channel-react-action-CKJHL_Wf.js} +3 -3
- package/dist/{channel.runtime-B4ThooGF.js → channel.runtime-CAssfx9p.js} +5 -5
- package/dist/{channel.setup-C_WEdyJR.js → channel.setup-CURc5rEt.js} +2 -2
- package/dist/{config-schema-CROZuhT-.js → config-schema-mBX2nmn1.js} +4 -0
- package/dist/{connection-controller-u-tfAFhJ.js → connection-controller-DkbrFSIn.js} +6 -5
- package/dist/connection-controller-registry-TSX_udJp.js +22 -0
- package/dist/contract-api.js +1 -1
- package/dist/{quoted-message-CpA_ujXK.js → group-session-key-BhC1RQOE.js} +31 -2
- package/dist/{identity-CmsTmkKR.js → identity-Dqft3mFA.js} +2 -1
- package/dist/index.js +9 -2
- package/dist/light-runtime-api.js +3 -3
- package/dist/{login-8lHwLmXj.js → login-C48y0jlx.js} +14 -7
- package/dist/{login-qr-BEZZqrTb.js → login-qr-BjId7dll.js} +7 -7
- package/dist/login-qr-runtime.js +1 -1
- package/dist/{monitor-BqkJ4MpT.js → monitor-DD8bXohk.js} +249 -160
- package/dist/runtime-api.js +9 -9
- package/dist/{send-i8Xk5mBQ.js → send-C5CpFcBG.js} +27 -9
- package/dist/{send-api-DndiBJhN.js → send-api-DLlXl8CH.js} +6 -5
- package/dist/{session-WU4SVP8f.js → session-DriaHt7V.js} +3 -3
- package/dist/{session-errors-CbsoQqoy.js → session-errors-BAj9D2La.js} +1 -22
- package/dist/{setup-core-KjSBbWX4.js → setup-core-DH-DFPLY.js} +4 -4
- package/dist/{setup-finalize-WedNP30s.js → setup-finalize-c8dl34sO.js} +3 -3
- package/dist/setup-plugin-api.js +1 -1
- package/dist/{setup-surface-IOdkbWsQ.js → setup-surface-DlWb0Z09.js} +3 -3
- package/dist/{socket-timing-DvMNkIge.js → socket-timing-DhbVFjah.js} +17 -5
- package/dist/{text-runtime-C88QkuU9.js → targets-runtime-C-GiVn6Y.js} +2 -3
- package/dist/text-runtime-DdX6-mC_.js +4 -0
- package/node_modules/@cacheable/memory/README.md +177 -0
- package/node_modules/@cacheable/memory/dist/index.cjs +867 -820
- package/node_modules/@cacheable/memory/dist/index.d.cts +382 -280
- package/node_modules/@cacheable/memory/dist/index.d.mts +412 -0
- package/node_modules/@cacheable/memory/dist/index.mjs +841 -0
- package/node_modules/@cacheable/memory/package.json +9 -9
- package/node_modules/@cacheable/utils/README.md +342 -8
- package/node_modules/@cacheable/utils/dist/index.cjs +1322 -619
- package/node_modules/@cacheable/utils/dist/index.d.cts +769 -173
- package/node_modules/@cacheable/utils/dist/index.d.mts +903 -0
- package/node_modules/@cacheable/utils/dist/index.mjs +1355 -0
- package/node_modules/@cacheable/utils/package.json +8 -8
- package/node_modules/@types/node/README.md +1 -1
- package/node_modules/@types/node/assert/strict.d.ts +10 -1
- package/node_modules/@types/node/assert.d.ts +144 -17
- package/node_modules/@types/node/async_hooks.d.ts +9 -9
- package/node_modules/@types/node/buffer.d.ts +64 -30
- package/node_modules/@types/node/child_process.d.ts +17 -21
- package/node_modules/@types/node/cluster.d.ts +13 -13
- package/node_modules/@types/node/console.d.ts +1 -1
- package/node_modules/@types/node/crypto.d.ts +471 -575
- package/node_modules/@types/node/diagnostics_channel.d.ts +274 -32
- package/node_modules/@types/node/dns.d.ts +15 -15
- package/node_modules/@types/node/events.d.ts +3 -3
- package/node_modules/@types/node/ffi.d.ts +486 -0
- package/node_modules/@types/node/fs/promises.d.ts +7 -4
- package/node_modules/@types/node/fs.d.ts +24 -18
- package/node_modules/@types/node/globals.d.ts +4 -4
- package/node_modules/@types/node/http.d.ts +30 -5
- package/node_modules/@types/node/http2.d.ts +5 -1
- package/node_modules/@types/node/index.d.ts +1 -3
- package/node_modules/@types/node/inspector.d.ts +1 -1
- package/node_modules/@types/node/module.d.ts +16 -45
- package/node_modules/@types/node/net.d.ts +1 -1
- package/node_modules/@types/node/os.d.ts +2 -2
- package/node_modules/@types/node/package.json +4 -4
- package/node_modules/@types/node/perf_hooks.d.ts +1 -9
- package/node_modules/@types/node/process.d.ts +25 -12
- package/node_modules/@types/node/quic.d.ts +92 -11
- package/node_modules/@types/node/readline.d.ts +12 -11
- package/node_modules/@types/node/repl.d.ts +10 -10
- package/node_modules/@types/node/sqlite.d.ts +56 -1
- package/node_modules/@types/node/stream/iter.d.ts +150 -0
- package/node_modules/@types/node/stream/promises.d.ts +2 -2
- package/node_modules/@types/node/stream/web.d.ts +1 -1
- package/node_modules/@types/node/stream.d.ts +37 -5
- package/node_modules/@types/node/test/reporters.d.ts +1 -1
- package/node_modules/@types/node/test.d.ts +120 -10
- package/node_modules/@types/node/tls.d.ts +1 -1
- package/node_modules/@types/node/ts5.6/index.d.ts +2 -4
- package/node_modules/@types/node/ts5.7/index.d.ts +1 -3
- package/node_modules/@types/node/url.d.ts +3 -3
- package/node_modules/@types/node/util/types.d.ts +1 -1
- package/node_modules/@types/node/util.d.ts +22 -22
- package/node_modules/@types/node/v8.d.ts +85 -3
- package/node_modules/@types/node/vm.d.ts +9 -9
- package/node_modules/@types/node/web-globals/domexception.d.ts +15 -0
- package/node_modules/@types/node/worker_threads.d.ts +10 -12
- package/node_modules/@types/node/zlib.d.ts +3 -3
- package/node_modules/cacheable/README.md +309 -30
- package/node_modules/cacheable/dist/index.cjs +1307 -1153
- package/node_modules/cacheable/dist/index.d.cts +741 -469
- package/node_modules/cacheable/dist/index.d.mts +766 -0
- package/node_modules/cacheable/dist/index.mjs +1210 -0
- package/node_modules/cacheable/package.json +10 -10
- package/node_modules/content-type/README.md +32 -57
- package/node_modules/content-type/dist/index.d.ts +26 -0
- package/node_modules/content-type/dist/index.js +170 -0
- package/node_modules/content-type/dist/index.js.map +1 -0
- package/node_modules/content-type/package.json +35 -25
- package/node_modules/media-typer/README.md +19 -33
- package/node_modules/media-typer/dist/index.d.ts +29 -0
- package/node_modules/media-typer/dist/index.js +64 -0
- package/node_modules/media-typer/dist/index.js.map +1 -0
- package/node_modules/media-typer/package.json +37 -22
- package/node_modules/music-metadata/lib/id3v1/ID3v1Parser.js +2 -2
- package/node_modules/music-metadata/lib/mp4/AtomToken.js +65 -13
- package/node_modules/music-metadata/lib/wav/WaveChunk.d.ts +10 -0
- package/node_modules/music-metadata/lib/wav/WaveChunk.js +13 -3
- package/node_modules/music-metadata/lib/wav/WaveParser.d.ts +1 -0
- package/node_modules/music-metadata/lib/wav/WaveParser.js +6 -5
- package/node_modules/music-metadata/package.json +5 -5
- package/node_modules/typebox/build/compile/validator.d.mts +2 -7
- package/node_modules/typebox/build/compile/validator.mjs +6 -36
- package/node_modules/typebox/build/error/errors.d.mts +1 -7
- package/node_modules/typebox/build/format/_idna.mjs +36 -6
- package/node_modules/typebox/build/format/_puny.mjs +4 -1
- package/node_modules/typebox/build/format/uuid.d.mts +2 -2
- package/node_modules/typebox/build/format/uuid.mjs +3 -3
- package/node_modules/typebox/build/guard/emit.d.mts +0 -4
- package/node_modules/typebox/build/guard/emit.mjs +0 -8
- package/node_modules/typebox/build/guard/guard.d.mts +4 -8
- package/node_modules/typebox/build/guard/guard.mjs +5 -13
- package/node_modules/typebox/build/schema/engine/boolean.d.mts +3 -3
- package/node_modules/typebox/build/schema/engine/boolean.mjs +4 -4
- package/node_modules/typebox/build/schema/engine/index.d.mts +0 -1
- package/node_modules/typebox/build/schema/engine/index.mjs +0 -1
- package/node_modules/typebox/build/schema/engine/schema.mjs +5 -10
- package/node_modules/typebox/build/schema/engine/type.mjs +14 -18
- package/node_modules/typebox/build/schema/types/dependencies.mjs +1 -1
- package/node_modules/typebox/build/schema/types/index.d.mts +0 -1
- package/node_modules/typebox/build/schema/types/index.mjs +0 -1
- package/node_modules/typebox/build/schema/types/schema.d.mts +1 -1
- package/node_modules/typebox/build/schema/types/schema.mjs +2 -2
- package/node_modules/typebox/build/system/locale/ar_001.mjs +0 -1
- package/node_modules/typebox/build/system/locale/bn_BD.mjs +0 -1
- package/node_modules/typebox/build/system/locale/cs_CZ.mjs +0 -1
- package/node_modules/typebox/build/system/locale/de_DE.mjs +0 -1
- package/node_modules/typebox/build/system/locale/el_GR.mjs +0 -1
- package/node_modules/typebox/build/system/locale/en_US.mjs +0 -1
- package/node_modules/typebox/build/system/locale/es_419.mjs +0 -1
- package/node_modules/typebox/build/system/locale/es_AR.mjs +0 -1
- package/node_modules/typebox/build/system/locale/es_ES.mjs +0 -1
- package/node_modules/typebox/build/system/locale/es_MX.mjs +0 -1
- package/node_modules/typebox/build/system/locale/fa_IR.mjs +0 -1
- package/node_modules/typebox/build/system/locale/fil_PH.mjs +0 -1
- package/node_modules/typebox/build/system/locale/fr_CA.mjs +0 -1
- package/node_modules/typebox/build/system/locale/fr_FR.mjs +0 -1
- package/node_modules/typebox/build/system/locale/ha_NG.mjs +0 -1
- package/node_modules/typebox/build/system/locale/hi_IN.mjs +0 -1
- package/node_modules/typebox/build/system/locale/hu_HU.mjs +0 -1
- package/node_modules/typebox/build/system/locale/id_ID.mjs +0 -1
- package/node_modules/typebox/build/system/locale/it_IT.mjs +0 -1
- package/node_modules/typebox/build/system/locale/ja_JP.mjs +0 -1
- package/node_modules/typebox/build/system/locale/ko_KR.mjs +0 -1
- package/node_modules/typebox/build/system/locale/ms_MY.mjs +0 -1
- package/node_modules/typebox/build/system/locale/nl_NL.mjs +0 -1
- package/node_modules/typebox/build/system/locale/pl_PL.mjs +0 -1
- package/node_modules/typebox/build/system/locale/pt_BR.mjs +0 -1
- package/node_modules/typebox/build/system/locale/pt_PT.mjs +0 -1
- package/node_modules/typebox/build/system/locale/ro_RO.mjs +0 -1
- package/node_modules/typebox/build/system/locale/ru_RU.mjs +0 -1
- package/node_modules/typebox/build/system/locale/sv_SE.mjs +0 -1
- package/node_modules/typebox/build/system/locale/sw_TZ.mjs +0 -1
- package/node_modules/typebox/build/system/locale/th_TH.mjs +0 -1
- package/node_modules/typebox/build/system/locale/tr_TR.mjs +0 -1
- package/node_modules/typebox/build/system/locale/uk_UA.mjs +0 -1
- package/node_modules/typebox/build/system/locale/ur_PK.mjs +0 -1
- package/node_modules/typebox/build/system/locale/vi_VN.mjs +0 -1
- package/node_modules/typebox/build/system/locale/yo_NG.mjs +0 -1
- package/node_modules/typebox/build/system/locale/zh_Hans.mjs +0 -1
- package/node_modules/typebox/build/system/locale/zh_Hant.mjs +0 -1
- package/node_modules/typebox/build/system/memory/clone.d.mts +2 -3
- package/node_modules/typebox/build/system/memory/clone.mjs +73 -26
- package/node_modules/typebox/build/system/settings/settings.d.mts +15 -0
- package/node_modules/typebox/build/system/settings/settings.mjs +3 -1
- package/node_modules/typebox/build/type/action/_add_immutable.d.mts +11 -0
- package/node_modules/typebox/build/type/action/_add_immutable.mjs +11 -0
- package/node_modules/typebox/build/type/action/_add_optional.d.mts +11 -0
- package/node_modules/typebox/build/type/action/_add_optional.mjs +11 -0
- package/node_modules/typebox/build/type/action/_add_readonly.d.mts +11 -0
- package/node_modules/typebox/build/type/action/_add_readonly.mjs +11 -0
- package/node_modules/typebox/build/type/action/_remove_immutable.d.mts +11 -0
- package/node_modules/typebox/build/type/action/_remove_immutable.mjs +11 -0
- package/node_modules/typebox/build/type/action/_remove_optional.d.mts +11 -0
- package/node_modules/typebox/build/type/action/_remove_optional.mjs +11 -0
- package/node_modules/typebox/build/type/action/_remove_readonly.d.mts +11 -0
- package/node_modules/typebox/build/type/action/_remove_readonly.mjs +11 -0
- package/node_modules/typebox/build/type/action/conditional.d.mts +2 -3
- package/node_modules/typebox/build/type/action/conditional.mjs +2 -1
- package/node_modules/typebox/build/type/action/index.d.mts +7 -4
- package/node_modules/typebox/build/type/action/index.mjs +7 -4
- package/node_modules/typebox/build/type/action/mapped.d.mts +2 -3
- package/node_modules/typebox/build/type/action/mapped.mjs +2 -1
- package/node_modules/typebox/build/type/action/module.d.mts +8 -7
- package/node_modules/typebox/build/type/action/module.mjs +7 -6
- package/node_modules/typebox/build/type/action/readonly_object.d.mts +0 -5
- package/node_modules/typebox/build/type/action/readonly_object.mjs +0 -5
- package/node_modules/typebox/build/type/action/with.d.mts +11 -0
- package/node_modules/typebox/build/type/action/with.mjs +11 -0
- package/node_modules/typebox/build/type/engine/call/distribute_arguments.mjs +1 -1
- package/node_modules/typebox/build/type/engine/call/instantiate.d.mts +1 -3
- package/node_modules/typebox/build/type/engine/call/instantiate.mjs +3 -2
- package/node_modules/typebox/build/type/engine/call/resolve_arguments.mjs +2 -2
- package/node_modules/typebox/build/type/engine/constructor_parameters/instantiate.d.mts +3 -4
- package/node_modules/typebox/build/type/engine/constructor_parameters/instantiate.mjs +2 -1
- package/node_modules/typebox/build/type/engine/cyclic/candidates.d.mts +2 -3
- package/node_modules/typebox/build/type/engine/cyclic/candidates.mjs +3 -6
- package/node_modules/typebox/build/type/engine/cyclic/check.d.mts +6 -9
- package/node_modules/typebox/build/type/engine/cyclic/check.mjs +10 -16
- package/node_modules/typebox/build/type/engine/cyclic/dependencies.d.mts +1 -4
- package/node_modules/typebox/build/type/engine/cyclic/dependencies.mjs +9 -15
- package/node_modules/typebox/build/type/engine/cyclic/extends.d.mts +1 -4
- package/node_modules/typebox/build/type/engine/cyclic/extends.mjs +8 -14
- package/node_modules/typebox/build/type/engine/cyclic/instantiate.d.mts +2 -5
- package/node_modules/typebox/build/type/engine/cyclic/instantiate.mjs +3 -2
- package/node_modules/typebox/build/type/engine/enum/index.d.mts +0 -1
- package/node_modules/typebox/build/type/engine/enum/index.mjs +0 -1
- package/node_modules/typebox/build/type/engine/evaluate/broaden.d.mts +1 -2
- package/node_modules/typebox/build/type/engine/evaluate/broaden.mjs +2 -6
- package/node_modules/typebox/build/type/engine/evaluate/composite.d.mts +10 -6
- package/node_modules/typebox/build/type/engine/evaluate/composite.mjs +10 -6
- package/node_modules/typebox/build/type/engine/evaluate/distribute.d.mts +2 -1
- package/node_modules/typebox/build/type/engine/evaluate/distribute.mjs +3 -3
- package/node_modules/typebox/build/type/engine/evaluate/evaluate.d.mts +17 -5
- package/node_modules/typebox/build/type/engine/evaluate/evaluate.mjs +34 -8
- package/node_modules/typebox/build/type/engine/exclude/operation.d.mts +5 -6
- package/node_modules/typebox/build/type/engine/exclude/operation.mjs +11 -12
- package/node_modules/typebox/build/type/engine/extract/operation.d.mts +5 -6
- package/node_modules/typebox/build/type/engine/extract/operation.mjs +11 -12
- package/node_modules/typebox/build/type/engine/immutable/instantiate_add.d.mts +10 -0
- package/node_modules/typebox/build/type/engine/immutable/instantiate_add.mjs +14 -0
- package/node_modules/typebox/build/type/engine/immutable/instantiate_remove.d.mts +10 -0
- package/node_modules/typebox/build/type/engine/immutable/instantiate_remove.mjs +14 -0
- package/node_modules/typebox/build/type/engine/index.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/index.mjs +2 -2
- package/node_modules/typebox/build/type/engine/indexable/from_dependent.d.mts +5 -0
- package/node_modules/typebox/build/type/engine/indexable/from_dependent.mjs +8 -0
- package/node_modules/typebox/build/type/engine/indexable/from_enum.d.mts +3 -3
- package/node_modules/typebox/build/type/engine/indexable/from_enum.mjs +4 -4
- package/node_modules/typebox/build/type/engine/indexable/from_template_literal.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/indexable/from_template_literal.mjs +3 -3
- package/node_modules/typebox/build/type/engine/indexable/from_type.d.mts +3 -1
- package/node_modules/typebox/build/type/engine/indexable/from_type.mjs +9 -6
- package/node_modules/typebox/build/type/engine/indexed/instantiate.d.mts +2 -1
- package/node_modules/typebox/build/type/engine/indexed/instantiate.mjs +5 -2
- package/node_modules/typebox/build/type/engine/instantiate.d.mts +44 -31
- package/node_modules/typebox/build/type/engine/instantiate.mjs +82 -83
- package/node_modules/typebox/build/type/engine/interface/instantiate.d.mts +3 -6
- package/node_modules/typebox/build/type/engine/intrinsics/from_template_literal.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/intrinsics/from_template_literal.mjs +3 -3
- package/node_modules/typebox/build/type/engine/keyof/instantiate.d.mts +2 -1
- package/node_modules/typebox/build/type/engine/keyof/instantiate.mjs +5 -1
- package/node_modules/typebox/build/type/engine/mapped/mapped_operation.d.mts +4 -4
- package/node_modules/typebox/build/type/engine/mapped/mapped_operation.mjs +3 -3
- package/node_modules/typebox/build/type/engine/mapped/mapped_variants.d.mts +5 -4
- package/node_modules/typebox/build/type/engine/mapped/mapped_variants.mjs +10 -5
- package/node_modules/typebox/build/type/engine/module/instantiate.d.mts +7 -9
- package/node_modules/typebox/build/type/engine/module/instantiate.mjs +17 -15
- package/node_modules/typebox/build/type/engine/object/from_dependent.d.mts +5 -0
- package/node_modules/typebox/build/type/engine/object/from_dependent.mjs +8 -0
- package/node_modules/typebox/build/type/engine/object/from_type.d.mts +3 -1
- package/node_modules/typebox/build/type/engine/object/from_type.mjs +8 -5
- package/node_modules/typebox/build/type/engine/object/from_union.mjs +2 -2
- package/node_modules/typebox/build/type/engine/optional/instantiate_add.d.mts +10 -0
- package/node_modules/typebox/build/type/engine/optional/instantiate_add.mjs +14 -0
- package/node_modules/typebox/build/type/engine/optional/instantiate_remove.d.mts +10 -0
- package/node_modules/typebox/build/type/engine/optional/instantiate_remove.mjs +14 -0
- package/node_modules/typebox/build/type/engine/parameters/instantiate.d.mts +3 -4
- package/node_modules/typebox/build/type/engine/parameters/instantiate.mjs +2 -1
- package/node_modules/typebox/build/type/engine/partial/from_dependent.d.mts +5 -0
- package/node_modules/typebox/build/type/engine/partial/from_dependent.mjs +8 -0
- package/node_modules/typebox/build/type/engine/partial/from_intersect.d.mts +1 -1
- package/node_modules/typebox/build/type/engine/partial/from_intersect.mjs +3 -2
- package/node_modules/typebox/build/type/engine/partial/from_object.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/partial/from_object.mjs +2 -2
- package/node_modules/typebox/build/type/engine/partial/from_type.d.mts +3 -1
- package/node_modules/typebox/build/type/engine/partial/from_type.mjs +7 -4
- package/node_modules/typebox/build/type/engine/priority/index.d.mts +1 -0
- package/node_modules/typebox/build/type/engine/priority/index.mjs +1 -0
- package/node_modules/typebox/build/type/engine/priority/priority.d.mts +20 -0
- package/node_modules/typebox/build/type/engine/priority/priority.mjs +28 -0
- package/node_modules/typebox/build/type/engine/readonly/instantiate_add.d.mts +10 -0
- package/node_modules/typebox/build/type/engine/readonly/instantiate_add.mjs +14 -0
- package/node_modules/typebox/build/type/engine/readonly/instantiate_remove.d.mts +10 -0
- package/node_modules/typebox/build/type/engine/readonly/instantiate_remove.mjs +14 -0
- package/node_modules/typebox/build/type/engine/readonly_object/from_array.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/readonly_object/from_array.mjs +2 -2
- package/node_modules/typebox/build/type/engine/readonly_object/from_dependent.d.mts +5 -0
- package/node_modules/typebox/build/type/engine/readonly_object/from_dependent.mjs +8 -0
- package/node_modules/typebox/build/type/engine/readonly_object/from_intersect.d.mts +1 -1
- package/node_modules/typebox/build/type/engine/readonly_object/from_intersect.mjs +3 -2
- package/node_modules/typebox/build/type/engine/readonly_object/from_object.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/readonly_object/from_object.mjs +2 -2
- package/node_modules/typebox/build/type/engine/readonly_object/from_tuple.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/readonly_object/from_tuple.mjs +2 -2
- package/node_modules/typebox/build/type/engine/readonly_object/from_type.d.mts +3 -1
- package/node_modules/typebox/build/type/engine/readonly_object/from_type.mjs +8 -5
- package/node_modules/typebox/build/type/engine/record/from_key_enum.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/record/from_key_enum.mjs +2 -2
- package/node_modules/typebox/build/type/engine/record/from_key_template_literal.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/record/from_key_template_literal.mjs +2 -2
- package/node_modules/typebox/build/type/engine/ref/instantiate.d.mts +3 -3
- package/node_modules/typebox/build/type/engine/ref/instantiate.mjs +6 -6
- package/node_modules/typebox/build/type/engine/required/from_dependent.d.mts +5 -0
- package/node_modules/typebox/build/type/engine/required/from_dependent.mjs +8 -0
- package/node_modules/typebox/build/type/engine/required/from_intersect.d.mts +1 -1
- package/node_modules/typebox/build/type/engine/required/from_intersect.mjs +3 -2
- package/node_modules/typebox/build/type/engine/required/from_object.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/required/from_object.mjs +2 -2
- package/node_modules/typebox/build/type/engine/required/from_type.d.mts +3 -1
- package/node_modules/typebox/build/type/engine/required/from_type.mjs +7 -4
- package/node_modules/typebox/build/type/engine/template_literal/decode.mjs +3 -3
- package/node_modules/typebox/build/type/engine/template_literal/encode.d.mts +2 -2
- package/node_modules/typebox/build/type/engine/template_literal/encode.mjs +6 -6
- package/node_modules/typebox/build/type/engine/template_literal/is_finite.mjs +1 -1
- package/node_modules/typebox/build/type/engine/template_literal/is_pattern.mjs +1 -0
- package/node_modules/typebox/build/type/engine/this/expand_this.d.mts +1 -4
- package/node_modules/typebox/build/type/engine/this/expand_this.mjs +7 -13
- package/node_modules/typebox/build/type/engine/with/instantiate.d.mts +8 -0
- package/node_modules/typebox/build/type/engine/{options → with}/instantiate.mjs +5 -5
- package/node_modules/typebox/build/type/extends/dependent.d.mts +6 -0
- package/node_modules/typebox/build/type/extends/dependent.mjs +6 -0
- package/node_modules/typebox/build/type/extends/enum.d.mts +4 -4
- package/node_modules/typebox/build/type/extends/enum.mjs +3 -2
- package/node_modules/typebox/build/type/extends/extends_left.d.mts +5 -7
- package/node_modules/typebox/build/type/extends/extends_left.mjs +24 -27
- package/node_modules/typebox/build/type/extends/extends_right.d.mts +7 -5
- package/node_modules/typebox/build/type/extends/extends_right.mjs +20 -15
- package/node_modules/typebox/build/type/extends/inference.mjs +1 -1
- package/node_modules/typebox/build/type/extends/object.d.mts +8 -1
- package/node_modules/typebox/build/type/extends/object.mjs +25 -3
- package/node_modules/typebox/build/type/extends/parameters.mjs +2 -2
- package/node_modules/typebox/build/type/extends/record.d.mts +13 -0
- package/node_modules/typebox/build/type/extends/record.mjs +23 -0
- package/node_modules/typebox/build/type/extends/template_literal.d.mts +3 -3
- package/node_modules/typebox/build/type/extends/template_literal.mjs +3 -3
- package/node_modules/typebox/build/type/extends/tuple.d.mts +3 -6
- package/node_modules/typebox/build/type/extends/tuple.mjs +6 -5
- package/node_modules/typebox/build/type/extends/union.mjs +2 -2
- package/node_modules/typebox/build/type/script/mapping.d.mts +84 -99
- package/node_modules/typebox/build/type/script/mapping.mjs +81 -85
- package/node_modules/typebox/build/type/script/parser.d.mts +40 -40
- package/node_modules/typebox/build/type/script/parser.mjs +22 -22
- package/node_modules/typebox/build/type/script/script.d.mts +3 -4
- package/node_modules/typebox/build/type/script/script.mjs +3 -2
- package/node_modules/typebox/build/type/script/token/internal/guard.d.mts +1 -1
- package/node_modules/typebox/build/type/script/token/internal/guard.mjs +1 -1
- package/node_modules/typebox/build/type/script/token/internal/take.mjs +1 -1
- package/node_modules/typebox/build/type/script/token/until.mjs +2 -2
- package/node_modules/typebox/build/type/types/_codec.d.mts +12 -12
- package/node_modules/typebox/build/type/types/_codec.mjs +0 -1
- package/node_modules/typebox/build/type/types/_immutable.d.mts +2 -9
- package/node_modules/typebox/build/type/types/_immutable.mjs +2 -10
- package/node_modules/typebox/build/type/types/_optional.d.mts +2 -9
- package/node_modules/typebox/build/type/types/_optional.mjs +2 -11
- package/node_modules/typebox/build/type/types/_readonly.d.mts +2 -9
- package/node_modules/typebox/build/type/types/_readonly.mjs +2 -10
- package/node_modules/typebox/build/type/types/_refine.d.mts +8 -10
- package/node_modules/typebox/build/type/types/_refine.mjs +1 -2
- package/node_modules/typebox/build/type/types/call.d.mts +3 -3
- package/node_modules/typebox/build/type/types/call.mjs +3 -2
- package/node_modules/typebox/build/type/types/deferred.d.mts +1 -0
- package/node_modules/typebox/build/type/types/deferred.mjs +1 -1
- package/node_modules/typebox/build/type/types/dependent.d.mts +17 -0
- package/node_modules/typebox/build/type/types/dependent.mjs +24 -0
- package/node_modules/typebox/build/type/types/enum.d.mts +3 -1
- package/node_modules/typebox/build/type/types/enum.mjs +5 -0
- package/node_modules/typebox/build/type/types/index.d.mts +1 -4
- package/node_modules/typebox/build/type/types/index.mjs +1 -4
- package/node_modules/typebox/build/type/types/number.d.mts +1 -1
- package/node_modules/typebox/build/type/types/number.mjs +1 -1
- package/node_modules/typebox/build/type/types/record.d.mts +13 -7
- package/node_modules/typebox/build/type/types/record.mjs +15 -9
- package/node_modules/typebox/build/type/types/static.d.mts +2 -5
- package/node_modules/typebox/build/typebox.d.mts +4 -8
- package/node_modules/typebox/build/typebox.mjs +4 -8
- package/node_modules/typebox/build/value/clean/clean.mjs +4 -2
- package/node_modules/typebox/build/value/clean/from_intersect.mjs +2 -2
- package/node_modules/typebox/build/value/clean/from_type.mjs +8 -10
- package/node_modules/typebox/build/value/clean/from_union.d.mts +1 -1
- package/node_modules/typebox/build/value/clean/from_union.mjs +1 -2
- package/node_modules/typebox/build/value/clone/clone.mjs +2 -73
- package/node_modules/typebox/build/value/codec/decode.mjs +4 -2
- package/node_modules/typebox/build/value/codec/encode.mjs +4 -2
- package/node_modules/typebox/build/value/codec/from_array.mjs +1 -4
- package/node_modules/typebox/build/value/codec/from_object.mjs +1 -4
- package/node_modules/typebox/build/value/codec/from_record.mjs +2 -7
- package/node_modules/typebox/build/value/codec/from_tuple.mjs +1 -4
- package/node_modules/typebox/build/value/codec/from_union.mjs +2 -3
- package/node_modules/typebox/build/value/convert/from_enum.mjs +3 -4
- package/node_modules/typebox/build/value/convert/from_template_literal.d.mts +1 -1
- package/node_modules/typebox/build/value/convert/from_template_literal.mjs +2 -3
- package/node_modules/typebox/build/value/convert/from_type.mjs +19 -21
- package/node_modules/typebox/build/value/create/from_enum.mjs +2 -2
- package/node_modules/typebox/build/value/create/from_type.mjs +23 -31
- package/node_modules/typebox/build/value/default/from_object.mjs +1 -1
- package/node_modules/typebox/build/value/default/from_type.mjs +8 -10
- package/node_modules/typebox/build/value/index.d.mts +0 -1
- package/node_modules/typebox/build/value/index.mjs +0 -1
- package/node_modules/typebox/build/value/repair/from_enum.mjs +2 -3
- package/node_modules/typebox/build/value/repair/from_type.mjs +24 -22
- package/node_modules/typebox/build/value/shared/union_priority_sort.d.mts +6 -2
- package/node_modules/typebox/build/value/shared/union_priority_sort.mjs +62 -26
- package/node_modules/typebox/build/value/value.d.mts +0 -1
- package/node_modules/typebox/build/value/value.mjs +0 -1
- package/node_modules/typebox/package.json +25 -25
- package/node_modules/typebox/readme.md +97 -80
- package/node_modules/undici-types/agent.d.ts +0 -2
- package/node_modules/undici-types/client.d.ts +25 -19
- package/node_modules/undici-types/dispatcher.d.ts +7 -33
- package/node_modules/undici-types/dispatcher1-wrapper.d.ts +7 -0
- package/node_modules/undici-types/fetch.d.ts +24 -4
- package/node_modules/undici-types/h2c-client.d.ts +6 -6
- package/node_modules/undici-types/handlers.d.ts +1 -2
- package/node_modules/undici-types/header.d.ts +5 -0
- package/node_modules/undici-types/index.d.ts +3 -1
- package/node_modules/undici-types/interceptors.d.ts +1 -1
- package/node_modules/undici-types/package.json +1 -1
- package/node_modules/undici-types/pool.d.ts +0 -2
- package/node_modules/undici-types/proxy-agent.d.ts +2 -2
- package/node_modules/undici-types/round-robin-pool.d.ts +0 -2
- package/node_modules/undici-types/snapshot-agent.d.ts +4 -0
- package/node_modules/undici-types/socks5-proxy-agent.d.ts +2 -2
- package/node_modules/undici-types/webidl.d.ts +0 -1
- package/npm-shrinkwrap.json +49 -41
- package/openclaw.plugin.json +12 -0
- package/package.json +5 -5
- package/node_modules/@cacheable/memory/dist/index.d.ts +0 -310
- package/node_modules/@cacheable/memory/dist/index.js +0 -807
- package/node_modules/@cacheable/utils/dist/index.d.ts +0 -307
- package/node_modules/@cacheable/utils/dist/index.js +0 -630
- package/node_modules/@types/node/compatibility/iterators.d.ts +0 -21
- package/node_modules/cacheable/dist/index.d.ts +0 -494
- package/node_modules/cacheable/dist/index.js +0 -1141
- package/node_modules/content-type/HISTORY.md +0 -29
- package/node_modules/content-type/index.js +0 -225
- package/node_modules/media-typer/HISTORY.md +0 -50
- package/node_modules/media-typer/index.js +0 -143
- package/node_modules/typebox/build/schema/engine/_guard.d.mts +0 -6
- package/node_modules/typebox/build/schema/engine/_guard.mjs +0 -26
- package/node_modules/typebox/build/schema/types/_guard.d.mts +0 -10
- package/node_modules/typebox/build/schema/types/_guard.mjs +0 -16
- package/node_modules/typebox/build/type/action/_optional.d.mts +0 -19
- package/node_modules/typebox/build/type/action/_optional.mjs +0 -40
- package/node_modules/typebox/build/type/action/_readonly.d.mts +0 -19
- package/node_modules/typebox/build/type/action/_readonly.mjs +0 -40
- package/node_modules/typebox/build/type/action/awaited.d.mts +0 -15
- package/node_modules/typebox/build/type/action/awaited.mjs +0 -15
- package/node_modules/typebox/build/type/action/options.d.mts +0 -11
- package/node_modules/typebox/build/type/action/options.mjs +0 -11
- package/node_modules/typebox/build/type/engine/awaited/instantiate.d.mts +0 -11
- package/node_modules/typebox/build/type/engine/awaited/instantiate.mjs +0 -20
- package/node_modules/typebox/build/type/engine/enum/enum_to_union.d.mts +0 -14
- package/node_modules/typebox/build/type/engine/enum/enum_to_union.mjs +0 -24
- package/node_modules/typebox/build/type/engine/options/index.d.mts +0 -1
- package/node_modules/typebox/build/type/engine/options/index.mjs +0 -1
- package/node_modules/typebox/build/type/engine/options/instantiate.d.mts +0 -8
- package/node_modules/typebox/build/type/extends/async_iterator.d.mts +0 -7
- package/node_modules/typebox/build/type/extends/async_iterator.mjs +0 -9
- package/node_modules/typebox/build/type/extends/iterator.d.mts +0 -7
- package/node_modules/typebox/build/type/extends/iterator.mjs +0 -9
- package/node_modules/typebox/build/type/extends/promise.d.mts +0 -7
- package/node_modules/typebox/build/type/extends/promise.mjs +0 -9
- package/node_modules/typebox/build/type/types/async_iterator.d.mts +0 -20
- package/node_modules/typebox/build/type/types/async_iterator.mjs +0 -28
- package/node_modules/typebox/build/type/types/base.d.mts +0 -50
- package/node_modules/typebox/build/type/types/base.mjs +0 -84
- package/node_modules/typebox/build/type/types/iterator.d.mts +0 -20
- package/node_modules/typebox/build/type/types/iterator.mjs +0 -28
- package/node_modules/typebox/build/type/types/promise.d.mts +0 -21
- package/node_modules/typebox/build/type/types/promise.mjs +0 -29
- package/node_modules/typebox/build/value/clean/from_base.d.mts +0 -2
- package/node_modules/typebox/build/value/clean/from_base.mjs +0 -4
- package/node_modules/typebox/build/value/convert/from_base.d.mts +0 -2
- package/node_modules/typebox/build/value/convert/from_base.mjs +0 -4
- package/node_modules/typebox/build/value/create/from_async_iterator.d.mts +0 -2
- package/node_modules/typebox/build/value/create/from_async_iterator.mjs +0 -5
- package/node_modules/typebox/build/value/create/from_base.d.mts +0 -2
- package/node_modules/typebox/build/value/create/from_base.mjs +0 -4
- package/node_modules/typebox/build/value/create/from_iterator.d.mts +0 -2
- package/node_modules/typebox/build/value/create/from_iterator.mjs +0 -5
- package/node_modules/typebox/build/value/create/from_promise.d.mts +0 -2
- package/node_modules/typebox/build/value/create/from_promise.mjs +0 -5
- package/node_modules/typebox/build/value/default/from_base.d.mts +0 -2
- package/node_modules/typebox/build/value/default/from_base.mjs +0 -5
- package/node_modules/typebox/build/value/mutate/error.d.mts +0 -3
- package/node_modules/typebox/build/value/mutate/error.mjs +0 -6
- package/node_modules/typebox/build/value/mutate/from_array.d.mts +0 -2
- package/node_modules/typebox/build/value/mutate/from_array.mjs +0 -16
- package/node_modules/typebox/build/value/mutate/from_object.d.mts +0 -2
- package/node_modules/typebox/build/value/mutate/from_object.mjs +0 -40
- package/node_modules/typebox/build/value/mutate/from_unknown.d.mts +0 -2
- package/node_modules/typebox/build/value/mutate/from_unknown.mjs +0 -7
- package/node_modules/typebox/build/value/mutate/from_value.d.mts +0 -2
- package/node_modules/typebox/build/value/mutate/from_value.mjs +0 -12
- package/node_modules/typebox/build/value/mutate/index.d.mts +0 -1
- package/node_modules/typebox/build/value/mutate/index.mjs +0 -1
- package/node_modules/typebox/build/value/mutate/mutate.d.mts +0 -11
- package/node_modules/typebox/build/value/mutate/mutate.mjs +0 -41
- package/node_modules/typebox/build/value/repair/from_base.d.mts +0 -2
- package/node_modules/typebox/build/value/repair/from_base.mjs +0 -8
- package/dist/{approval-resolver-e1hAVRRy.js → approval-resolver-DF-lu8k5.js} +1 -1
- /package/node_modules/typebox/build/type/engine/{awaited → with}/index.d.mts +0 -0
- /package/node_modules/typebox/build/type/engine/{awaited → with}/index.mjs +0 -0
|
@@ -1,1157 +1,1311 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _cacheable_memory = require("@cacheable/memory");
|
|
3
|
+
let _cacheable_utils = require("@cacheable/utils");
|
|
4
|
+
let hookified = require("hookified");
|
|
5
|
+
let keyv = require("keyv");
|
|
6
|
+
let qified = require("qified");
|
|
7
|
+
//#region src/enums.ts
|
|
8
|
+
let CacheableHooks = /* @__PURE__ */ function(CacheableHooks) {
|
|
9
|
+
CacheableHooks["BEFORE_SET"] = "BEFORE_SET";
|
|
10
|
+
CacheableHooks["AFTER_SET"] = "AFTER_SET";
|
|
11
|
+
CacheableHooks["BEFORE_SET_MANY"] = "BEFORE_SET_MANY";
|
|
12
|
+
CacheableHooks["AFTER_SET_MANY"] = "AFTER_SET_MANY";
|
|
13
|
+
CacheableHooks["BEFORE_GET"] = "BEFORE_GET";
|
|
14
|
+
CacheableHooks["AFTER_GET"] = "AFTER_GET";
|
|
15
|
+
CacheableHooks["BEFORE_GET_MANY"] = "BEFORE_GET_MANY";
|
|
16
|
+
CacheableHooks["AFTER_GET_MANY"] = "AFTER_GET_MANY";
|
|
17
|
+
CacheableHooks["BEFORE_SECONDARY_SETS_PRIMARY"] = "BEFORE_SECONDARY_SETS_PRIMARY";
|
|
18
|
+
return CacheableHooks;
|
|
19
|
+
}({});
|
|
20
|
+
let CacheableEvents = /* @__PURE__ */ function(CacheableEvents) {
|
|
21
|
+
CacheableEvents["ERROR"] = "error";
|
|
22
|
+
CacheableEvents["CACHE_HIT"] = "cache:hit";
|
|
23
|
+
CacheableEvents["CACHE_MISS"] = "cache:miss";
|
|
24
|
+
return CacheableEvents;
|
|
25
|
+
}({});
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/sync.ts
|
|
28
|
+
/**
|
|
29
|
+
* Events emitted by CacheableSync
|
|
30
|
+
*/
|
|
31
|
+
let CacheableSyncEvents = /* @__PURE__ */ function(CacheableSyncEvents) {
|
|
32
|
+
CacheableSyncEvents["ERROR"] = "error";
|
|
33
|
+
CacheableSyncEvents["SET"] = "cache:set";
|
|
34
|
+
CacheableSyncEvents["DELETE"] = "cache:delete";
|
|
35
|
+
return CacheableSyncEvents;
|
|
36
|
+
}({});
|
|
37
|
+
/**
|
|
38
|
+
* CacheableSync provides synchronization capabilities for cacheable items
|
|
39
|
+
* using message providers from Qified
|
|
40
|
+
*/
|
|
41
|
+
var CacheableSync = class extends hookified.Hookified {
|
|
42
|
+
_qified = new qified.Qified();
|
|
43
|
+
_namespace;
|
|
44
|
+
_storage;
|
|
45
|
+
_cacheId;
|
|
46
|
+
/**
|
|
47
|
+
* Creates an instance of CacheableSync
|
|
48
|
+
* @param options - Configuration options for CacheableSync
|
|
49
|
+
*/
|
|
50
|
+
constructor(options) {
|
|
51
|
+
super(options);
|
|
52
|
+
this._namespace = options.namespace;
|
|
53
|
+
this._qified = this.createQified(options.qified);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Gets the Qified instance used for synchronization
|
|
57
|
+
* @returns The Qified instance
|
|
58
|
+
*/
|
|
59
|
+
get qified() {
|
|
60
|
+
return this._qified;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Sets the Qified instance used for synchronization
|
|
64
|
+
* @param value - Either an existing Qified instance or MessageProvider(s)
|
|
65
|
+
*/
|
|
66
|
+
set qified(value) {
|
|
67
|
+
this._qified = this.createQified(value);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Gets the namespace for sync events
|
|
71
|
+
* @returns The namespace or undefined if not set
|
|
72
|
+
*/
|
|
73
|
+
get namespace() {
|
|
74
|
+
return this._namespace;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Sets the namespace for sync events and resubscribes if needed
|
|
78
|
+
* @param namespace - The namespace string or function
|
|
79
|
+
*/
|
|
80
|
+
set namespace(namespace) {
|
|
81
|
+
if (this._storage && this._cacheId) {
|
|
82
|
+
const oldSetEvent = this.getPrefixedEvent("cache:set");
|
|
83
|
+
const oldDeleteEvent = this.getPrefixedEvent("cache:delete");
|
|
84
|
+
this._qified.unsubscribeMessage(oldSetEvent);
|
|
85
|
+
this._qified.unsubscribeMessage(oldDeleteEvent);
|
|
86
|
+
}
|
|
87
|
+
this._namespace = namespace;
|
|
88
|
+
if (this._storage && this._cacheId) this.subscribe(this._storage, this._cacheId);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Publishes a cache event to all the cache instances
|
|
92
|
+
* @param data - The cache item data containing cacheId, key, value, and optional ttl
|
|
93
|
+
*/
|
|
94
|
+
async publish(event, data) {
|
|
95
|
+
const eventName = this.getPrefixedEvent(event);
|
|
96
|
+
await this._qified.publish(eventName, {
|
|
97
|
+
id: crypto.randomUUID(),
|
|
98
|
+
data
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Subscribes to sync events and updates the provided storage
|
|
103
|
+
* @param storage - The Keyv storage instance to update
|
|
104
|
+
* @param cacheId - The cache ID to identify this instance
|
|
105
|
+
*/
|
|
106
|
+
subscribe(storage, cacheId) {
|
|
107
|
+
this._storage = storage;
|
|
108
|
+
this._cacheId = cacheId;
|
|
109
|
+
const setEvent = this.getPrefixedEvent("cache:set");
|
|
110
|
+
const deleteEvent = this.getPrefixedEvent("cache:delete");
|
|
111
|
+
this._qified.subscribe(setEvent, { handler: async (message) => {
|
|
112
|
+
const data = message.data;
|
|
113
|
+
if (data.cacheId !== cacheId) await storage.set(data.key, data.value, data.ttl);
|
|
114
|
+
} });
|
|
115
|
+
this._qified.subscribe(deleteEvent, { handler: async (message) => {
|
|
116
|
+
const data = message.data;
|
|
117
|
+
if (data.cacheId !== cacheId) await storage.delete(data.key);
|
|
118
|
+
} });
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Creates or returns a Qified instance from the provided value
|
|
122
|
+
* @param value - Either an existing Qified instance or MessageProvider(s)
|
|
123
|
+
* @returns A Qified instance configured with the provided message provider(s)
|
|
124
|
+
*/
|
|
125
|
+
createQified(value) {
|
|
126
|
+
if (value instanceof qified.Qified) return value;
|
|
127
|
+
return new qified.Qified({ messageProviders: Array.isArray(value) ? value : [value] });
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets the namespace prefix to use for event names
|
|
131
|
+
* @returns The resolved namespace string or undefined
|
|
132
|
+
*/
|
|
133
|
+
getNamespace() {
|
|
134
|
+
if (typeof this._namespace === "function") return this._namespace();
|
|
135
|
+
return this._namespace;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Prefixes an event name with the namespace if one is set
|
|
139
|
+
* @param event - The event to prefix
|
|
140
|
+
* @returns The prefixed event name or the original event
|
|
141
|
+
*/
|
|
142
|
+
getPrefixedEvent(event) {
|
|
143
|
+
const ns = this.getNamespace();
|
|
144
|
+
return ns ? `${ns}::${event}` : event;
|
|
145
|
+
}
|
|
9
146
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/index.ts
|
|
149
|
+
var Cacheable = class Cacheable extends hookified.Hookified {
|
|
150
|
+
static _instance;
|
|
151
|
+
_primary = (0, _cacheable_memory.createKeyv)();
|
|
152
|
+
_secondary;
|
|
153
|
+
_nonBlocking = false;
|
|
154
|
+
_ttl;
|
|
155
|
+
_maxTtl;
|
|
156
|
+
_stats = new _cacheable_utils.Stats({ enabled: false });
|
|
157
|
+
_namespace;
|
|
158
|
+
_cacheId = Math.random().toString(36).slice(2);
|
|
159
|
+
_sync;
|
|
160
|
+
_tags = this.createCacheTags();
|
|
161
|
+
/**
|
|
162
|
+
* Creates a new cacheable instance
|
|
163
|
+
* @param {CacheableOptions} [options] The options for the cacheable instance
|
|
164
|
+
*/
|
|
165
|
+
constructor(options) {
|
|
166
|
+
super();
|
|
167
|
+
if (options?.primary) this.setPrimary(options.primary);
|
|
168
|
+
if (options?.secondary) this.setSecondary(options.secondary);
|
|
169
|
+
if (options?.nonBlocking) this._nonBlocking = options.nonBlocking;
|
|
170
|
+
if (options?.stats) this._stats.enabled = options.stats;
|
|
171
|
+
if (options?.ttl) this.setTtl(options.ttl);
|
|
172
|
+
if (options?.maxTtl !== void 0) this.setMaxTtl(options.maxTtl);
|
|
173
|
+
if (options?.cacheId) this._cacheId = options.cacheId;
|
|
174
|
+
if (options?.namespace) {
|
|
175
|
+
this._namespace = options.namespace;
|
|
176
|
+
this._primary.namespace = this.getNameSpace();
|
|
177
|
+
if (this._secondary) this._secondary.namespace = this.getNameSpace();
|
|
178
|
+
}
|
|
179
|
+
if (options?.tags) this._tags.enabled = true;
|
|
180
|
+
if (options?.sync) {
|
|
181
|
+
this._sync = options.sync instanceof CacheableSync ? options.sync : new CacheableSync({
|
|
182
|
+
...options.sync,
|
|
183
|
+
namespace: options.namespace
|
|
184
|
+
});
|
|
185
|
+
this._sync.subscribe(this._primary, this._cacheId);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Gets a shared static (singleton) instance of {@link Cacheable}. The first call creates the
|
|
190
|
+
* instance using the provided options; every later call returns that same instance. Passing
|
|
191
|
+
* `options` again after the instance already exists does NOT reconfigure it — the options are
|
|
192
|
+
* ignored and a {@link CacheableEvents.ERROR} event is emitted on the instance to surface the
|
|
193
|
+
* conflict (listen with `instance.on("error", ...)`). To reconfigure, replace it via
|
|
194
|
+
* {@link Cacheable.setStaticInstance} (clear with `undefined`, then call this again).
|
|
195
|
+
*
|
|
196
|
+
* Note: this package ships separate CommonJS and ESM builds, so an app that loads both formats
|
|
197
|
+
* gets one shared instance per build. For a single shared cache, use one module format or share
|
|
198
|
+
* an explicit instance via {@link Cacheable.setStaticInstance}.
|
|
199
|
+
* @param {CacheableOptions} [options] Options applied only when the instance is first created
|
|
200
|
+
* @returns {Cacheable} The shared static instance
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* const cache = Cacheable.getStaticInstance({ ttl: "1h" });
|
|
204
|
+
* await cache.set("key", "value");
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
static getStaticInstance(options) {
|
|
208
|
+
if (options && Cacheable._instance) {
|
|
209
|
+
Cacheable._instance.emit("error", /* @__PURE__ */ new Error("Cacheable static instance is already initialized; the options passed were ignored. To reconfigure, use Cacheable.setStaticInstance()."));
|
|
210
|
+
return Cacheable._instance;
|
|
211
|
+
}
|
|
212
|
+
Cacheable._instance ??= new Cacheable(options);
|
|
213
|
+
return Cacheable._instance;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Sets or clears the shared static instance returned by {@link Cacheable.getStaticInstance}.
|
|
217
|
+
* Pass a {@link Cacheable} instance to make it the shared instance, or `undefined` to clear it
|
|
218
|
+
* so the next {@link Cacheable.getStaticInstance} call creates a fresh one. Clearing only drops
|
|
219
|
+
* the reference — it does not `disconnect()` or `clear()` the previous instance, so disconnect
|
|
220
|
+
* it first if it holds open connections.
|
|
221
|
+
* @param {Cacheable} [instance] The instance to share, or `undefined` to clear it
|
|
222
|
+
*/
|
|
223
|
+
static setStaticInstance(instance) {
|
|
224
|
+
Cacheable._instance = instance;
|
|
225
|
+
}
|
|
226
|
+
onHook(event, handler) {
|
|
227
|
+
super.onHook(event, handler);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* The namespace for the cacheable instance
|
|
231
|
+
* @returns {string | (() => string) | undefined} The namespace for the cacheable instance
|
|
232
|
+
*/
|
|
233
|
+
get namespace() {
|
|
234
|
+
return this._namespace;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Sets the namespace for the cacheable instance
|
|
238
|
+
* @param {string | (() => string) | undefined} namespace The namespace for the cacheable instance
|
|
239
|
+
* @returns {void}
|
|
240
|
+
*/
|
|
241
|
+
set namespace(namespace) {
|
|
242
|
+
this._namespace = namespace;
|
|
243
|
+
this._primary.namespace = this.getNameSpace();
|
|
244
|
+
if (this._secondary) this._secondary.namespace = this.getNameSpace();
|
|
245
|
+
if (this._sync) this._sync.namespace = namespace;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* The statistics for the cacheable instance
|
|
249
|
+
* @returns {CacheableStats} The statistics for the cacheable instance
|
|
250
|
+
*/
|
|
251
|
+
get stats() {
|
|
252
|
+
return this._stats;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The primary store for the cacheable instance
|
|
256
|
+
* @returns {Keyv} The primary store for the cacheable instance
|
|
257
|
+
*/
|
|
258
|
+
get primary() {
|
|
259
|
+
return this._primary;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Sets the primary store for the cacheable instance
|
|
263
|
+
* @param {Keyv} primary The primary store for the cacheable instance
|
|
264
|
+
*/
|
|
265
|
+
set primary(primary) {
|
|
266
|
+
this._primary = primary;
|
|
267
|
+
this._tags = this.createCacheTags();
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* The secondary store for the cacheable instance
|
|
271
|
+
* @returns {Keyv | undefined} The secondary store for the cacheable instance
|
|
272
|
+
*/
|
|
273
|
+
get secondary() {
|
|
274
|
+
return this._secondary;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
|
|
278
|
+
* @param {Keyv | undefined} secondary The secondary store for the cacheable instance
|
|
279
|
+
* @returns {void}
|
|
280
|
+
*/
|
|
281
|
+
set secondary(secondary) {
|
|
282
|
+
this._secondary = secondary;
|
|
283
|
+
this._tags = this.createCacheTags();
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Gets whether the secondary store is non-blocking mode. It is set to false by default.
|
|
287
|
+
* If it is set to true then the secondary store will not block the primary store.
|
|
288
|
+
*
|
|
289
|
+
* [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
|
|
290
|
+
*
|
|
291
|
+
* @returns {boolean} Whether the cacheable instance is non-blocking
|
|
292
|
+
*/
|
|
293
|
+
get nonBlocking() {
|
|
294
|
+
return this._nonBlocking;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Sets whether the secondary store is non-blocking mode. It is set to false by default.
|
|
298
|
+
* If it is set to true then the secondary store will not block the primary store.
|
|
299
|
+
*
|
|
300
|
+
* [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
|
|
301
|
+
*
|
|
302
|
+
* @param {boolean} nonBlocking Whether the cacheable instance is non-blocking
|
|
303
|
+
* @returns {void}
|
|
304
|
+
*/
|
|
305
|
+
set nonBlocking(nonBlocking) {
|
|
306
|
+
this._nonBlocking = nonBlocking;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* The time-to-live for the cacheable instance and will be used as the default value.
|
|
310
|
+
* can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
|
|
311
|
+
* or undefined if there is no time-to-live.
|
|
312
|
+
*
|
|
313
|
+
* [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
|
|
314
|
+
*
|
|
315
|
+
* @returns {number | string | undefined} The time-to-live for the cacheable instance in milliseconds, human-readable format or undefined
|
|
316
|
+
* @example
|
|
317
|
+
* ```typescript
|
|
318
|
+
* const cacheable = new Cacheable({ ttl: '1h' });
|
|
319
|
+
* console.log(cacheable.ttl); // 1h
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
get ttl() {
|
|
323
|
+
return this._ttl;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Sets the time-to-live for the cacheable instance and will be used as the default value.
|
|
327
|
+
* If you set a number it is miliseconds, if you set a string it is a human-readable
|
|
328
|
+
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that
|
|
329
|
+
* there is no time-to-live.
|
|
330
|
+
*
|
|
331
|
+
* [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
|
|
332
|
+
*
|
|
333
|
+
* @param {number | string | undefined} ttl The time-to-live for the cacheable instance
|
|
334
|
+
* @example
|
|
335
|
+
* ```typescript
|
|
336
|
+
* const cacheable = new Cacheable();
|
|
337
|
+
* cacheable.ttl = '1h'; // Set the time-to-live to 1 hour
|
|
338
|
+
* ```
|
|
339
|
+
* or setting the time-to-live in milliseconds
|
|
340
|
+
* ```typescript
|
|
341
|
+
* const cacheable = new Cacheable();
|
|
342
|
+
* cacheable.ttl = 3600000; // Set the time-to-live to 1 hour
|
|
343
|
+
* ```
|
|
344
|
+
*/
|
|
345
|
+
set ttl(ttl) {
|
|
346
|
+
this.setTtl(ttl);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Gets the maximum time-to-live for the cacheable instance. When set, any TTL that exceeds this
|
|
350
|
+
* value is capped to maxTtl. Entries with no TTL will also be capped to maxTtl.
|
|
351
|
+
* Can be a number in milliseconds or a human-readable format such as `1s`, `1m`, `1h`, `1d`.
|
|
352
|
+
* Default is `undefined` (no maximum).
|
|
353
|
+
*
|
|
354
|
+
* @returns {number | string | undefined} The maximum time-to-live or undefined if not set
|
|
355
|
+
* @example
|
|
356
|
+
* ```typescript
|
|
357
|
+
* const cacheable = new Cacheable({ maxTtl: '1h' });
|
|
358
|
+
* console.log(cacheable.maxTtl); // '1h'
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
361
|
+
get maxTtl() {
|
|
362
|
+
return this._maxTtl;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Sets the maximum time-to-live for the cacheable instance. When set, any TTL that exceeds this
|
|
366
|
+
* value is capped to maxTtl. Entries with no TTL will also be capped to maxTtl.
|
|
367
|
+
* If you set a number it is milliseconds, if you set a string it is a human-readable
|
|
368
|
+
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined disables the maximum.
|
|
369
|
+
*
|
|
370
|
+
* @param {number | string | undefined} maxTtl The maximum time-to-live
|
|
371
|
+
* @example
|
|
372
|
+
* ```typescript
|
|
373
|
+
* const cacheable = new Cacheable();
|
|
374
|
+
* cacheable.maxTtl = '1h'; // Set the max TTL to 1 hour
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
set maxTtl(maxTtl) {
|
|
378
|
+
this.setMaxTtl(maxTtl);
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
|
|
382
|
+
* If it is not set then it will be a random string that is generated
|
|
383
|
+
* @returns {string} The cacheId for the cacheable instance
|
|
384
|
+
*/
|
|
385
|
+
get cacheId() {
|
|
386
|
+
return this._cacheId;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Sets the cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
|
|
390
|
+
* If it is not set then it will be a random string that is generated
|
|
391
|
+
* @param {string} cacheId The cacheId for the cacheable instance
|
|
392
|
+
*/
|
|
393
|
+
set cacheId(cacheId) {
|
|
394
|
+
this._cacheId = cacheId;
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Gets the sync instance for the cacheable instance
|
|
398
|
+
* @returns {CacheableSync | undefined} The sync instance for the cacheable instance
|
|
399
|
+
*/
|
|
400
|
+
get sync() {
|
|
401
|
+
return this._sync;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Sets the sync instance for the cacheable instance
|
|
405
|
+
* @param {CacheableSync | undefined} sync The sync instance for the cacheable instance
|
|
406
|
+
*/
|
|
407
|
+
set sync(sync) {
|
|
408
|
+
this._sync = sync;
|
|
409
|
+
if (this._sync) this._sync.subscribe(this._primary, this._cacheId);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* The tag service for the cacheable instance, used for tag-based invalidation. It is created
|
|
413
|
+
* by default in the constructor and persists tag metadata in the secondary store when one is
|
|
414
|
+
* configured (so invalidations are shared across instances), otherwise the primary store.
|
|
415
|
+
*
|
|
416
|
+
* The service starts disabled so untagged workloads pay no extra store reads, and must be
|
|
417
|
+
* explicitly enabled to use tags — via the `tags: true` option or the `tags.enabled`
|
|
418
|
+
* property. While disabled, all tag operations are no-ops. Enable it on every instance that
|
|
419
|
+
* shares the store so behavior is consistent across distributed instances. While enabled,
|
|
420
|
+
* `get` / `getMany` perform tag freshness checks and remove stale entries.
|
|
421
|
+
*
|
|
422
|
+
* [Learn more about tag-based invalidation](https://cacheable.org/docs/cacheable/#tag-based-invalidation).
|
|
423
|
+
*
|
|
424
|
+
* @returns {CacheTags} The tag service for the cacheable instance
|
|
425
|
+
* @example
|
|
426
|
+
* ```typescript
|
|
427
|
+
* const cache = new Cacheable({ tags: true });
|
|
428
|
+
* await cache.set('page:/products', html, { tags: ['entity:42'] });
|
|
429
|
+
* await cache.tags.invalidateTag('entity:42');
|
|
430
|
+
* await cache.get('page:/products'); // undefined
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
433
|
+
get tags() {
|
|
434
|
+
return this._tags;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Creates the tag service backed by the secondary store when one is configured, otherwise the
|
|
438
|
+
* primary store, preserving the enabled state of any previous service and reporting
|
|
439
|
+
* non-blocking failures as error events.
|
|
440
|
+
*/
|
|
441
|
+
createCacheTags() {
|
|
442
|
+
return new _cacheable_utils.CacheTags({
|
|
443
|
+
store: this._secondary ?? this._primary,
|
|
444
|
+
enabled: this._tags?.enabled ?? false,
|
|
445
|
+
onError: (error) => {
|
|
446
|
+
this.emit("error", error);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Sets the primary store for the cacheable instance
|
|
452
|
+
* @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
|
|
453
|
+
* @returns {void}
|
|
454
|
+
*/
|
|
455
|
+
setPrimary(primary) {
|
|
456
|
+
if ((0, _cacheable_utils.isKeyvInstance)(primary)) this._primary = primary;
|
|
457
|
+
else this._primary = new keyv.Keyv(primary);
|
|
458
|
+
/* v8 ignore next -- @preserve */
|
|
459
|
+
this._primary.on("error", (error) => {
|
|
460
|
+
this.emit("error", error);
|
|
461
|
+
});
|
|
462
|
+
this._tags = this.createCacheTags();
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
|
|
466
|
+
* @param {Keyv | KeyvStoreAdapter} secondary The secondary store for the cacheable instance
|
|
467
|
+
* @returns {void}
|
|
468
|
+
*/
|
|
469
|
+
setSecondary(secondary) {
|
|
470
|
+
if ((0, _cacheable_utils.isKeyvInstance)(secondary)) this._secondary = secondary;
|
|
471
|
+
else this._secondary = new keyv.Keyv(secondary);
|
|
472
|
+
/* v8 ignore next -- @preserve */
|
|
473
|
+
this._secondary.on("error", (error) => {
|
|
474
|
+
this.emit("error", error);
|
|
475
|
+
});
|
|
476
|
+
this._tags = this.createCacheTags();
|
|
477
|
+
}
|
|
478
|
+
getNameSpace() {
|
|
479
|
+
if (typeof this._namespace === "function") return this._namespace();
|
|
480
|
+
return this._namespace;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Retrieves an entry from the cache.
|
|
484
|
+
*
|
|
485
|
+
* Checks the primary store first; if not found and a secondary store is configured,
|
|
486
|
+
* it will fetch from the secondary, repopulate the primary, and return the result.
|
|
487
|
+
*
|
|
488
|
+
* @typeParam T - The expected type of the stored value.
|
|
489
|
+
* @param {string} key - The cache key to retrieve.
|
|
490
|
+
* @param {GetOptions} - options such as to bypass `nonBlocking` for this call
|
|
491
|
+
* @returns {Promise<T | undefined>}
|
|
492
|
+
* A promise that resolves to the cached value if found, or `undefined`.
|
|
493
|
+
*/
|
|
494
|
+
async get(key, options) {
|
|
495
|
+
return (await this.getRaw(key, options))?.value;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Retrieves the raw entry from the cache including metadata like expiration.
|
|
499
|
+
*
|
|
500
|
+
* Checks the primary store first; if not found and a secondary store is configured,
|
|
501
|
+
* it will fetch from the secondary, repopulate the primary, and return the result.
|
|
502
|
+
*
|
|
503
|
+
* @typeParam T - The expected type of the stored value.
|
|
504
|
+
* @param {string} key - The cache key to retrieve.
|
|
505
|
+
* @param {GetOptions} - options such as to bypass `nonBlocking` for this call
|
|
506
|
+
* @returns {Promise<StoredDataRaw<T>>}
|
|
507
|
+
* A promise that resolves to the full raw data object if found, or undefined.
|
|
508
|
+
*/
|
|
509
|
+
async getRaw(key, options) {
|
|
510
|
+
let result;
|
|
511
|
+
try {
|
|
512
|
+
await this.hook("BEFORE_GET", key);
|
|
513
|
+
result = await this._primary.getRaw(key);
|
|
514
|
+
let ttl;
|
|
515
|
+
if (result) this.emit("cache:hit", {
|
|
516
|
+
key,
|
|
517
|
+
value: result.value,
|
|
518
|
+
store: "primary"
|
|
519
|
+
});
|
|
520
|
+
else this.emit("cache:miss", {
|
|
521
|
+
key,
|
|
522
|
+
store: "primary"
|
|
523
|
+
});
|
|
524
|
+
const nonBlocking = options?.nonBlocking ?? this._nonBlocking;
|
|
525
|
+
if (!result && this._secondary) {
|
|
526
|
+
let secondaryProcessResult;
|
|
527
|
+
if (nonBlocking) secondaryProcessResult = await this.processSecondaryForGetRawNonBlocking(this._primary, this._secondary, key);
|
|
528
|
+
else secondaryProcessResult = await this.processSecondaryForGetRaw(this._primary, this._secondary, key);
|
|
529
|
+
if (secondaryProcessResult) {
|
|
530
|
+
result = secondaryProcessResult.result;
|
|
531
|
+
ttl = secondaryProcessResult.ttl;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (result && this._tags.enabled && await this._tags.isKeyStale(key)) {
|
|
535
|
+
await this.delete(key);
|
|
536
|
+
result = void 0;
|
|
537
|
+
}
|
|
538
|
+
await this.hook("AFTER_GET", {
|
|
539
|
+
key,
|
|
540
|
+
result,
|
|
541
|
+
ttl
|
|
542
|
+
});
|
|
543
|
+
} catch (error) {
|
|
544
|
+
this.emit("error", error);
|
|
545
|
+
}
|
|
546
|
+
if (this.stats.enabled) {
|
|
547
|
+
if (result) this._stats.incrementHits();
|
|
548
|
+
else this._stats.incrementMisses();
|
|
549
|
+
this.stats.incrementGets();
|
|
550
|
+
}
|
|
551
|
+
return result;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Retrieves multiple raw entries from the cache including metadata like expiration.
|
|
555
|
+
*
|
|
556
|
+
* Checks the primary store for each key; if a key is missing and a secondary store is configured,
|
|
557
|
+
* it will fetch from the secondary store, repopulate the primary store, and return the results.
|
|
558
|
+
*
|
|
559
|
+
* @typeParam T - The expected type of the stored values.
|
|
560
|
+
* @param {string[]} keys - The cache keys to retrieve.
|
|
561
|
+
* @param {GetOptions} - options such as to bypass `nonBlocking` on this call
|
|
562
|
+
* @returns {Promise<Array<StoredDataRaw<T>>>}
|
|
563
|
+
* A promise that resolves to an array of raw data objects.
|
|
564
|
+
*/
|
|
565
|
+
async getManyRaw(keys, options) {
|
|
566
|
+
let result = [];
|
|
567
|
+
try {
|
|
568
|
+
await this.hook("BEFORE_GET_MANY", keys);
|
|
569
|
+
result = await this._primary.getManyRaw(keys);
|
|
570
|
+
for (const [i, key] of keys.entries()) if (result[i]) this.emit("cache:hit", {
|
|
571
|
+
key,
|
|
572
|
+
value: result[i].value,
|
|
573
|
+
store: "primary"
|
|
574
|
+
});
|
|
575
|
+
else this.emit("cache:miss", {
|
|
576
|
+
key,
|
|
577
|
+
store: "primary"
|
|
578
|
+
});
|
|
579
|
+
const nonBlocking = options?.nonBlocking ?? this._nonBlocking;
|
|
580
|
+
if (this._secondary) if (nonBlocking) await this.processSecondaryForGetManyRawNonBlocking(this._primary, this._secondary, keys, result);
|
|
581
|
+
else await this.processSecondaryForGetManyRaw(this._primary, this._secondary, keys, result);
|
|
582
|
+
if (this._tags.enabled) {
|
|
583
|
+
const presentKeys = keys.filter((_, i) => result[i] !== void 0);
|
|
584
|
+
const staleKeys = await this._tags.getStaleKeys(presentKeys);
|
|
585
|
+
if (staleKeys.length > 0) {
|
|
586
|
+
const staleSet = new Set(staleKeys);
|
|
587
|
+
for (const [i, key] of keys.entries()) if (staleSet.has(key)) result[i] = void 0;
|
|
588
|
+
await this.deleteMany(staleKeys);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
await this.hook("AFTER_GET_MANY", {
|
|
592
|
+
keys,
|
|
593
|
+
result
|
|
594
|
+
});
|
|
595
|
+
} catch (error) {
|
|
596
|
+
this.emit("error", error);
|
|
597
|
+
}
|
|
598
|
+
if (this.stats.enabled) {
|
|
599
|
+
for (const item of result) if (item) this._stats.incrementHits();
|
|
600
|
+
else this._stats.incrementMisses();
|
|
601
|
+
this.stats.incrementGets();
|
|
602
|
+
}
|
|
603
|
+
return result;
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Retrieves multiple entries from the cache.
|
|
607
|
+
* Checks the primary store for each key; if a key is missing and a secondary store is configured,
|
|
608
|
+
* it will fetch from the secondary store, repopulate the primary store, and return the results.
|
|
609
|
+
*
|
|
610
|
+
* @typeParam T - The expected type of the stored values.
|
|
611
|
+
* @param {string[]} keys - The cache keys to retrieve.
|
|
612
|
+
* @param {GetOptions} - options such as to bypass `nonBlocking` on this call
|
|
613
|
+
* @returns {Promise<Array<T | undefined>>}
|
|
614
|
+
* A promise that resolves to an array of cached values or `undefined` for misses.
|
|
615
|
+
*/
|
|
616
|
+
async getMany(keys, options) {
|
|
617
|
+
return (await this.getManyRaw(keys, options)).map((item) => item?.value);
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Sets the value of the key. If the secondary store is set then it will also set the value in the secondary store.
|
|
621
|
+
* @param {string} key the key to set the value of
|
|
622
|
+
* @param {T} value The value to set
|
|
623
|
+
* @param {number | string | SetOptions} [ttlOrOptions] set a number it is miliseconds, set a string it is a human-readable
|
|
624
|
+
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live.
|
|
625
|
+
* You can also pass a {@link SetOptions} object such as `{ ttl: '1h', tags: ['user:42'] }` to associate the entry with
|
|
626
|
+
* tags for tag-based invalidation. To give each store its own TTL for this operation, pass a per-store object as the
|
|
627
|
+
* `ttl`, such as `{ ttl: { primary: '10s', secondary: '5m' } }`.
|
|
628
|
+
* @returns {boolean} Whether the value was set
|
|
629
|
+
*/
|
|
630
|
+
async set(key, value, ttlOrOptions) {
|
|
631
|
+
let result = false;
|
|
632
|
+
const options = typeof ttlOrOptions === "object" && ttlOrOptions !== null ? ttlOrOptions : { ttl: ttlOrOptions ?? void 0 };
|
|
633
|
+
const nonBlocking = options.nonBlocking ?? this._nonBlocking;
|
|
634
|
+
const { primary: explicitPrimaryTtl, secondary: explicitSecondaryTtl } = (0, _cacheable_utils.resolvePerStoreTtl)(options.ttl);
|
|
635
|
+
const maxTtlMs = (0, _cacheable_utils.shorthandToMilliseconds)(this._maxTtl);
|
|
636
|
+
try {
|
|
637
|
+
let primaryTtl = (0, _cacheable_utils.getCascadingTtl)(this._ttl, this._primary.ttl, explicitPrimaryTtl);
|
|
638
|
+
primaryTtl = this.capTtl(primaryTtl, maxTtlMs);
|
|
639
|
+
let hookTtl = primaryTtl;
|
|
640
|
+
let ttlOverridden = false;
|
|
641
|
+
const item = {
|
|
642
|
+
key,
|
|
643
|
+
value,
|
|
644
|
+
tags: options.tags,
|
|
645
|
+
get ttl() {
|
|
646
|
+
return hookTtl;
|
|
647
|
+
},
|
|
648
|
+
set ttl(value) {
|
|
649
|
+
hookTtl = value;
|
|
650
|
+
ttlOverridden = true;
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
await this.hook("BEFORE_SET", item);
|
|
654
|
+
let primaryTtlEffective;
|
|
655
|
+
let secondaryTtlEffective;
|
|
656
|
+
if (!ttlOverridden) {
|
|
657
|
+
primaryTtlEffective = primaryTtl;
|
|
658
|
+
secondaryTtlEffective = this._secondary ? (0, _cacheable_utils.getCascadingTtl)(this._ttl, this._secondary.ttl, explicitSecondaryTtl) : void 0;
|
|
659
|
+
} else if (typeof hookTtl === "object" && hookTtl !== null) {
|
|
660
|
+
const { primary: hookPrimaryTtl, secondary: hookSecondaryTtl } = (0, _cacheable_utils.resolvePerStoreTtl)(hookTtl);
|
|
661
|
+
primaryTtlEffective = hookPrimaryTtl ?? (0, _cacheable_utils.getCascadingTtl)(this._ttl, this._primary.ttl, explicitPrimaryTtl);
|
|
662
|
+
secondaryTtlEffective = this._secondary ? hookSecondaryTtl ?? (0, _cacheable_utils.getCascadingTtl)(this._ttl, this._secondary.ttl, explicitSecondaryTtl) : void 0;
|
|
663
|
+
} else {
|
|
664
|
+
const hookScalarTtl = (0, _cacheable_utils.shorthandToMilliseconds)(hookTtl);
|
|
665
|
+
primaryTtlEffective = hookScalarTtl;
|
|
666
|
+
secondaryTtlEffective = this._secondary ? hookScalarTtl : void 0;
|
|
667
|
+
}
|
|
668
|
+
primaryTtlEffective = this.capTtl(primaryTtlEffective, maxTtlMs);
|
|
669
|
+
secondaryTtlEffective = this.capTtl(secondaryTtlEffective, maxTtlMs);
|
|
670
|
+
hookTtl = primaryTtlEffective;
|
|
671
|
+
const tagTtl = this.maxStoreTtl(primaryTtlEffective, secondaryTtlEffective);
|
|
672
|
+
const promises = [];
|
|
673
|
+
promises.push(this._primary.set(item.key, item.value, primaryTtlEffective));
|
|
674
|
+
if (this._secondary) promises.push(this._secondary.set(item.key, item.value, secondaryTtlEffective));
|
|
675
|
+
if (nonBlocking) {
|
|
676
|
+
result = await Promise.race(promises);
|
|
677
|
+
for (const promise of promises) promise.catch((error) => {
|
|
678
|
+
this.emit("error", error);
|
|
679
|
+
});
|
|
680
|
+
} else result = (await Promise.all(promises))[0];
|
|
681
|
+
if (this._tags.enabled) if (item.tags && item.tags.length > 0) await this._tags.setKeyTags(item.key, item.tags, {
|
|
682
|
+
ttl: tagTtl,
|
|
683
|
+
nonBlocking
|
|
684
|
+
});
|
|
685
|
+
else await this._tags.removeKeys([item.key], { nonBlocking });
|
|
686
|
+
await this.hook("AFTER_SET", item);
|
|
687
|
+
if (this._sync && result) await this._sync.publish("cache:set", {
|
|
688
|
+
cacheId: this._cacheId,
|
|
689
|
+
key: item.key,
|
|
690
|
+
value: item.value,
|
|
691
|
+
ttl: primaryTtlEffective
|
|
692
|
+
});
|
|
693
|
+
} catch (error) {
|
|
694
|
+
this.emit("error", error);
|
|
695
|
+
}
|
|
696
|
+
if (this.stats.enabled) {
|
|
697
|
+
this.stats.incrementKSize(key);
|
|
698
|
+
this.stats.incrementCount();
|
|
699
|
+
this.stats.incrementVSize(value);
|
|
700
|
+
this.stats.incrementSets();
|
|
701
|
+
}
|
|
702
|
+
return result;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Sets the values of the keys. If the secondary store is set then it will also set the values in the secondary store.
|
|
706
|
+
* Items can include `tags` to associate the entry with tags for tag-based invalidation.
|
|
707
|
+
* @param {CacheableSetItem[]} items The items to set
|
|
708
|
+
* @returns {boolean} Whether the values were set
|
|
709
|
+
*/
|
|
710
|
+
async setMany(items) {
|
|
711
|
+
let result = false;
|
|
712
|
+
try {
|
|
713
|
+
await this.hook("BEFORE_SET_MANY", items);
|
|
714
|
+
result = await this.setManyKeyv(this._primary, items, "primary");
|
|
715
|
+
if (this._secondary) if (this._nonBlocking)
|
|
716
|
+
/* v8 ignore next -- @preserve */
|
|
717
|
+
this.setManyKeyv(this._secondary, items, "secondary").catch((error) => {
|
|
718
|
+
/* v8 ignore next -- @preserve */
|
|
719
|
+
this.emit("error", error);
|
|
720
|
+
});
|
|
721
|
+
else await this.setManyKeyv(this._secondary, items, "secondary");
|
|
722
|
+
if (this._tags.enabled) await this.setManyKeyTags(items);
|
|
723
|
+
await this.hook("AFTER_SET_MANY", items);
|
|
724
|
+
if (this._sync && result) {
|
|
725
|
+
const maxTtlMs = (0, _cacheable_utils.shorthandToMilliseconds)(this._maxTtl);
|
|
726
|
+
for (const item of items) await this._sync.publish("cache:set", {
|
|
727
|
+
cacheId: this._cacheId,
|
|
728
|
+
key: item.key,
|
|
729
|
+
value: item.value,
|
|
730
|
+
ttl: this.resolveStoreTtl(item.ttl, this._primary.ttl, "primary", maxTtlMs)
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
} catch (error) {
|
|
734
|
+
this.emit("error", error);
|
|
735
|
+
}
|
|
736
|
+
if (this.stats.enabled) for (const item of items) {
|
|
737
|
+
this.stats.incrementKSize(item.key);
|
|
738
|
+
this.stats.incrementCount();
|
|
739
|
+
this.stats.incrementVSize(item.value);
|
|
740
|
+
}
|
|
741
|
+
return result;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Takes the value of the key and deletes the key. If the key does not exist then it will return undefined.
|
|
745
|
+
* @param {string} key The key to take the value of
|
|
746
|
+
* @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
|
|
747
|
+
*/
|
|
748
|
+
async take(key) {
|
|
749
|
+
const result = await this.get(key);
|
|
750
|
+
await this.delete(key);
|
|
751
|
+
return result;
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Takes the values of the keys and deletes the keys. If the key does not exist then it will return undefined.
|
|
755
|
+
* @param {string[]} keys The keys to take the values of
|
|
756
|
+
* @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
|
|
757
|
+
*/
|
|
758
|
+
async takeMany(keys) {
|
|
759
|
+
const result = await this.getMany(keys);
|
|
760
|
+
await this.deleteMany(keys);
|
|
761
|
+
return result;
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Checks if the key exists in the primary store. If it does not exist then it will check the secondary store.
|
|
765
|
+
* @param {string} key The key to check
|
|
766
|
+
* @returns {Promise<boolean>} Whether the key exists
|
|
767
|
+
*/
|
|
768
|
+
async has(key) {
|
|
769
|
+
const promises = [];
|
|
770
|
+
promises.push(this._primary.has(key));
|
|
771
|
+
if (this._secondary) promises.push(this._secondary.has(key));
|
|
772
|
+
const resultAll = await Promise.all(promises);
|
|
773
|
+
for (const result of resultAll) if (result) return true;
|
|
774
|
+
return false;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Checks if the keys exist in the primary store. If it does not exist then it will check the secondary store.
|
|
778
|
+
* @param {string[]} keys The keys to check
|
|
779
|
+
* @returns {Promise<boolean[]>} Whether the keys exist
|
|
780
|
+
*/
|
|
781
|
+
async hasMany(keys) {
|
|
782
|
+
const result = await this._primary.hasMany(keys);
|
|
783
|
+
const missingKeys = [];
|
|
784
|
+
for (const [i, key] of keys.entries()) if (!result[i] && this._secondary) missingKeys.push(key);
|
|
785
|
+
if (missingKeys.length > 0 && this._secondary) {
|
|
786
|
+
const secondary = await this._secondary.hasMany(keys);
|
|
787
|
+
for (const [i, _key] of keys.entries()) if (!result[i] && secondary[i]) result[i] = secondary[i];
|
|
788
|
+
}
|
|
789
|
+
return result;
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Deletes the key from the primary store. If the secondary store is set then it will also delete the key from the secondary store.
|
|
793
|
+
* @param {string} key The key to delete
|
|
794
|
+
* @returns {Promise<boolean>} Whether the key was deleted
|
|
795
|
+
*/
|
|
796
|
+
async delete(key) {
|
|
797
|
+
let result = false;
|
|
798
|
+
const promises = [];
|
|
799
|
+
if (this.stats.enabled) {
|
|
800
|
+
const statResult = await this._primary.get(key);
|
|
801
|
+
/* v8 ignore next -- @preserve */
|
|
802
|
+
if (statResult) {
|
|
803
|
+
this.stats.decreaseKSize(key);
|
|
804
|
+
this.stats.decreaseVSize(statResult);
|
|
805
|
+
this.stats.decreaseCount();
|
|
806
|
+
this.stats.incrementDeletes();
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
promises.push(this._primary.delete(key));
|
|
810
|
+
if (this._secondary) promises.push(this._secondary.delete(key));
|
|
811
|
+
if (this.nonBlocking) {
|
|
812
|
+
result = await Promise.race(promises);
|
|
813
|
+
for (const promise of promises) promise.catch((error) => {
|
|
814
|
+
this.emit("error", error);
|
|
815
|
+
});
|
|
816
|
+
} else result = (await Promise.all(promises))[0];
|
|
817
|
+
if (this._tags.enabled) await this._tags.removeKeys([key], { nonBlocking: this.nonBlocking });
|
|
818
|
+
if (this._sync && result) await this._sync.publish("cache:delete", {
|
|
819
|
+
cacheId: this._cacheId,
|
|
820
|
+
key
|
|
821
|
+
});
|
|
822
|
+
return result;
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Deletes the keys from the primary store. If the secondary store is set then it will also delete the keys from the secondary store.
|
|
826
|
+
* @param {string[]} keys The keys to delete
|
|
827
|
+
* @returns {Promise<boolean>} Whether the keys were deleted
|
|
828
|
+
*/
|
|
829
|
+
async deleteMany(keys) {
|
|
830
|
+
if (this.stats.enabled) {
|
|
831
|
+
const statResult = await this._primary.get(keys);
|
|
832
|
+
for (const key of keys) {
|
|
833
|
+
this.stats.decreaseKSize(key);
|
|
834
|
+
this.stats.decreaseVSize(statResult);
|
|
835
|
+
this.stats.decreaseCount();
|
|
836
|
+
this.stats.incrementDeletes();
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
const result = await this._primary.deleteMany(keys);
|
|
840
|
+
if (this._secondary) if (this._nonBlocking) this._secondary.deleteMany(keys).catch((error) => {
|
|
841
|
+
this.emit("error", error);
|
|
842
|
+
});
|
|
843
|
+
else await this._secondary.deleteMany(keys);
|
|
844
|
+
if (this._tags.enabled) await this._tags.removeKeys(keys, { nonBlocking: this._nonBlocking });
|
|
845
|
+
if (this._sync && result) for (const key of keys) await this._sync.publish("cache:delete", {
|
|
846
|
+
cacheId: this._cacheId,
|
|
847
|
+
key
|
|
848
|
+
});
|
|
849
|
+
return result;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* Clears the primary store. If the secondary store is set then it will also clear the secondary store.
|
|
853
|
+
* @returns {Promise<void>}
|
|
854
|
+
*/
|
|
855
|
+
async clear() {
|
|
856
|
+
const promises = [];
|
|
857
|
+
promises.push(this._primary.clear());
|
|
858
|
+
if (this._secondary) promises.push(this._secondary.clear());
|
|
859
|
+
await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
|
|
860
|
+
if (this.stats.enabled) {
|
|
861
|
+
this._stats.resetStoreValues();
|
|
862
|
+
this._stats.incrementClears();
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Disconnects the primary store. If the secondary store is set then it will also disconnect the secondary store.
|
|
867
|
+
* @returns {Promise<void>}
|
|
868
|
+
*/
|
|
869
|
+
async disconnect() {
|
|
870
|
+
const promises = [];
|
|
871
|
+
promises.push(this._primary.disconnect());
|
|
872
|
+
/* v8 ignore next -- @preserve */
|
|
873
|
+
if (this._secondary) promises.push(this._secondary.disconnect());
|
|
874
|
+
promises.push(this._sync?.qified.disconnect());
|
|
875
|
+
await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Wraps a function with caching
|
|
879
|
+
*
|
|
880
|
+
* [Learn more about wrapping functions](https://cacheable.org/docs/cacheable/#wrap--memoization-for-sync-and-async-functions).
|
|
881
|
+
* @param {Function} function_ The function to wrap
|
|
882
|
+
* @param {WrapOptions} [options] The options for the wrap function
|
|
883
|
+
* @returns {Function} The wrapped function
|
|
884
|
+
*/
|
|
885
|
+
wrap(function_, options) {
|
|
886
|
+
return (0, _cacheable_utils.wrap)(function_, {
|
|
887
|
+
ttl: options?.ttl ?? this._ttl,
|
|
888
|
+
keyPrefix: options?.keyPrefix,
|
|
889
|
+
createKey: options?.createKey,
|
|
890
|
+
cacheErrors: options?.cacheErrors,
|
|
891
|
+
cache: {
|
|
892
|
+
get: async (key) => this.get(key),
|
|
893
|
+
/* v8 ignore next -- @preserve */
|
|
894
|
+
has: async (key) => this.has(key),
|
|
895
|
+
set: async (key, value, ttl) => {
|
|
896
|
+
await this.set(key, value, { ttl });
|
|
897
|
+
},
|
|
898
|
+
/* v8 ignore next -- @preserve */
|
|
899
|
+
on: (event, listener) => {
|
|
900
|
+
this.on(event, listener);
|
|
901
|
+
},
|
|
902
|
+
/* v8 ignore next -- @preserve */
|
|
903
|
+
emit: (event, ...args) => this.emit(event, ...args)
|
|
904
|
+
},
|
|
905
|
+
cacheId: this._cacheId,
|
|
906
|
+
serialize: options?.serialize
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Retrieves the value associated with the given key from the cache. If the key is not found,
|
|
911
|
+
* invokes the provided function to calculate the value, stores it in the cache, and then returns it.
|
|
912
|
+
*
|
|
913
|
+
* @param {GetOrSetKey} key - The key to retrieve or set in the cache. This can also be a function that returns a string key.
|
|
914
|
+
* If a function is provided, it will be called with the cache options to generate the key.
|
|
915
|
+
* @param {() => Promise<T>} function_ - The asynchronous function that computes the value to be cached if the key does not exist.
|
|
916
|
+
* @param {GetOrSetFunctionOptions} [options] - Optional settings for caching, such as the time to live (TTL) or whether to cache errors.
|
|
917
|
+
* @return {Promise<T | undefined>} - A promise that resolves to the cached or newly computed value, or undefined if an error occurs and caching is not configured for errors.
|
|
918
|
+
*/
|
|
919
|
+
async getOrSet(key, function_, options) {
|
|
920
|
+
const getOptions = options?.nonBlocking === void 0 ? void 0 : { nonBlocking: options.nonBlocking };
|
|
921
|
+
return (0, _cacheable_utils.getOrSet)(key, function_, {
|
|
922
|
+
cache: {
|
|
923
|
+
get: async (key) => this.get(key, getOptions),
|
|
924
|
+
/* v8 ignore next -- @preserve */
|
|
925
|
+
has: async (key) => this.has(key),
|
|
926
|
+
set: async (key, value, ttl) => {
|
|
927
|
+
await this.set(key, value, { ttl });
|
|
928
|
+
},
|
|
929
|
+
/* v8 ignore next -- @preserve */
|
|
930
|
+
on: (event, listener) => {
|
|
931
|
+
/* v8 ignore next -- @preserve */
|
|
932
|
+
this.on(event, listener);
|
|
933
|
+
},
|
|
934
|
+
emit: (event, ...args) => this.emit(event, ...args)
|
|
935
|
+
},
|
|
936
|
+
cacheId: this._cacheId,
|
|
937
|
+
ttl: options?.ttl ?? this._ttl,
|
|
938
|
+
cacheErrors: options?.cacheErrors,
|
|
939
|
+
throwErrors: options?.throwErrors,
|
|
940
|
+
nonBlocking: options?.nonBlocking
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Will hash an object asynchronously using the specified cryptographic algorithm.
|
|
945
|
+
* Use this for cryptographic algorithms (SHA-256, SHA-384, SHA-512).
|
|
946
|
+
* For non-cryptographic algorithms, use hashSync() for better performance.
|
|
947
|
+
* @param {any} object the object to hash
|
|
948
|
+
* @param {string} algorithm the hash algorithm to use. The default is 'SHA-256'
|
|
949
|
+
* @returns {Promise<string>} the hash of the object
|
|
950
|
+
*/
|
|
951
|
+
async hash(object, algorithm = _cacheable_utils.HashAlgorithm.SHA256) {
|
|
952
|
+
return (0, _cacheable_utils.hash)(object, { algorithm });
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Will hash an object synchronously using the specified non-cryptographic algorithm.
|
|
956
|
+
* Use this for non-cryptographic algorithms (DJB2, FNV1, MURMER, CRC32).
|
|
957
|
+
* For cryptographic algorithms, use hash() instead.
|
|
958
|
+
* @param {any} object the object to hash
|
|
959
|
+
* @param {string} algorithm the hash algorithm to use. The default is 'djb2'
|
|
960
|
+
* @returns {string} the hash of the object
|
|
961
|
+
*/
|
|
962
|
+
hashSync(object, algorithm = _cacheable_utils.HashAlgorithm.DJB2) {
|
|
963
|
+
return (0, _cacheable_utils.hashSync)(object, { algorithm });
|
|
964
|
+
}
|
|
965
|
+
async setManyKeyv(keyv$1, items, store) {
|
|
966
|
+
const maxTtlMs = (0, _cacheable_utils.shorthandToMilliseconds)(this._maxTtl);
|
|
967
|
+
const entries = [];
|
|
968
|
+
for (const item of items) {
|
|
969
|
+
const finalTtl = this.resolveStoreTtl(item.ttl, keyv$1.ttl, store, maxTtlMs);
|
|
970
|
+
entries.push({
|
|
971
|
+
key: item.key,
|
|
972
|
+
value: item.value,
|
|
973
|
+
ttl: finalTtl
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
await keyv$1.setMany(entries);
|
|
977
|
+
return true;
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Writes tag snapshots for `setMany` items that carry tags and removes any previous snapshots
|
|
981
|
+
* for items that do not.
|
|
982
|
+
*/
|
|
983
|
+
async setManyKeyTags(items) {
|
|
984
|
+
const maxTtlMs = (0, _cacheable_utils.shorthandToMilliseconds)(this._maxTtl);
|
|
985
|
+
const promises = [];
|
|
986
|
+
const untaggedKeys = [];
|
|
987
|
+
for (const item of items) {
|
|
988
|
+
if (!item.tags || item.tags.length === 0) {
|
|
989
|
+
untaggedKeys.push(item.key);
|
|
990
|
+
continue;
|
|
991
|
+
}
|
|
992
|
+
const primaryTtl = this.resolveStoreTtl(item.ttl, this._primary.ttl, "primary", maxTtlMs);
|
|
993
|
+
const secondaryTtl = this._secondary ? this.resolveStoreTtl(item.ttl, this._secondary.ttl, "secondary", maxTtlMs) : void 0;
|
|
994
|
+
const ttl = this.maxStoreTtl(primaryTtl, secondaryTtl);
|
|
995
|
+
promises.push(this._tags.setKeyTags(item.key, item.tags, {
|
|
996
|
+
ttl,
|
|
997
|
+
nonBlocking: this._nonBlocking
|
|
998
|
+
}));
|
|
999
|
+
}
|
|
1000
|
+
if (untaggedKeys.length > 0) promises.push(this._tags.removeKeys(untaggedKeys, { nonBlocking: this._nonBlocking }));
|
|
1001
|
+
await Promise.all(promises);
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* Processes a single key from secondary store for getRaw operation
|
|
1005
|
+
* @param primary - the primary store to use
|
|
1006
|
+
* @param secondary - the secondary store to use
|
|
1007
|
+
* @param key - The key to retrieve from secondary store
|
|
1008
|
+
* @returns Promise containing the result and TTL information
|
|
1009
|
+
*/
|
|
1010
|
+
async processSecondaryForGetRaw(primary, secondary, key) {
|
|
1011
|
+
const secondaryResult = await secondary.getRaw(key);
|
|
1012
|
+
if (secondaryResult?.value) {
|
|
1013
|
+
this.emit("cache:hit", {
|
|
1014
|
+
key,
|
|
1015
|
+
value: secondaryResult.value,
|
|
1016
|
+
store: "secondary"
|
|
1017
|
+
});
|
|
1018
|
+
const ttl = (0, _cacheable_utils.calculateTtlFromExpiration)((0, _cacheable_utils.getCascadingTtl)(this._ttl, this._primary.ttl), secondaryResult.expires ?? void 0);
|
|
1019
|
+
const setItem = {
|
|
1020
|
+
key,
|
|
1021
|
+
value: secondaryResult.value,
|
|
1022
|
+
ttl
|
|
1023
|
+
};
|
|
1024
|
+
await this.hook("BEFORE_SECONDARY_SETS_PRIMARY", setItem);
|
|
1025
|
+
await primary.set(setItem.key, setItem.value, (0, _cacheable_utils.resolvePerStoreTtl)(setItem.ttl).primary);
|
|
1026
|
+
return {
|
|
1027
|
+
result: secondaryResult,
|
|
1028
|
+
ttl
|
|
1029
|
+
};
|
|
1030
|
+
} else {
|
|
1031
|
+
this.emit("cache:miss", {
|
|
1032
|
+
key,
|
|
1033
|
+
store: "secondary"
|
|
1034
|
+
});
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Processes a single key from secondary store for getRaw operation in non-blocking mode
|
|
1040
|
+
* Non-blocking mode means we don't wait for secondary operations that update primary store
|
|
1041
|
+
* @param primary - the primary store to use
|
|
1042
|
+
* @param secondary - the secondary store to use
|
|
1043
|
+
* @param key - The key to retrieve from secondary store
|
|
1044
|
+
* @returns Promise containing the result and TTL information
|
|
1045
|
+
*/
|
|
1046
|
+
async processSecondaryForGetRawNonBlocking(primary, secondary, key) {
|
|
1047
|
+
const secondaryResult = await secondary.getRaw(key);
|
|
1048
|
+
if (secondaryResult?.value) {
|
|
1049
|
+
this.emit("cache:hit", {
|
|
1050
|
+
key,
|
|
1051
|
+
value: secondaryResult.value,
|
|
1052
|
+
store: "secondary"
|
|
1053
|
+
});
|
|
1054
|
+
const ttl = (0, _cacheable_utils.calculateTtlFromExpiration)((0, _cacheable_utils.getCascadingTtl)(this._ttl, this._primary.ttl), secondaryResult.expires ?? void 0);
|
|
1055
|
+
const setItem = {
|
|
1056
|
+
key,
|
|
1057
|
+
value: secondaryResult.value,
|
|
1058
|
+
ttl
|
|
1059
|
+
};
|
|
1060
|
+
/* v8 ignore next -- @preserve */
|
|
1061
|
+
this.hook("BEFORE_SECONDARY_SETS_PRIMARY", setItem).then(async () => {
|
|
1062
|
+
await primary.set(setItem.key, setItem.value, (0, _cacheable_utils.resolvePerStoreTtl)(setItem.ttl).primary);
|
|
1063
|
+
}).catch((error) => {
|
|
1064
|
+
/* v8 ignore next -- @preserve */
|
|
1065
|
+
this.emit("error", error);
|
|
1066
|
+
});
|
|
1067
|
+
return {
|
|
1068
|
+
result: secondaryResult,
|
|
1069
|
+
ttl
|
|
1070
|
+
};
|
|
1071
|
+
} else {
|
|
1072
|
+
this.emit("cache:miss", {
|
|
1073
|
+
key,
|
|
1074
|
+
store: "secondary"
|
|
1075
|
+
});
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
/**
|
|
1080
|
+
* Processes missing keys from secondary store for getManyRaw operation
|
|
1081
|
+
* @param primary - the primary store to use
|
|
1082
|
+
* @param secondary - the secondary store to use
|
|
1083
|
+
* @param keys - The original array of keys requested
|
|
1084
|
+
* @param result - The result array from primary store (will be modified)
|
|
1085
|
+
* @returns Promise<void>
|
|
1086
|
+
*/
|
|
1087
|
+
async processSecondaryForGetManyRaw(primary, secondary, keys, result) {
|
|
1088
|
+
const missingKeys = [];
|
|
1089
|
+
for (const [i, key] of keys.entries()) if (!result[i]) missingKeys.push(key);
|
|
1090
|
+
const secondaryResults = await secondary.getManyRaw(missingKeys);
|
|
1091
|
+
let secondaryIndex = 0;
|
|
1092
|
+
for await (const [i, key] of keys.entries()) if (!result[i]) {
|
|
1093
|
+
const secondaryResult = secondaryResults[secondaryIndex];
|
|
1094
|
+
if (secondaryResult && secondaryResult.value !== void 0) {
|
|
1095
|
+
result[i] = secondaryResult;
|
|
1096
|
+
this.emit("cache:hit", {
|
|
1097
|
+
key,
|
|
1098
|
+
value: secondaryResult.value,
|
|
1099
|
+
store: "secondary"
|
|
1100
|
+
});
|
|
1101
|
+
const cascadeTtl = (0, _cacheable_utils.getCascadingTtl)(this._ttl, this._primary.ttl);
|
|
1102
|
+
let { expires } = secondaryResult;
|
|
1103
|
+
/* v8 ignore next -- @preserve */
|
|
1104
|
+
if (expires === null) expires = void 0;
|
|
1105
|
+
const ttl = (0, _cacheable_utils.calculateTtlFromExpiration)(cascadeTtl, expires);
|
|
1106
|
+
const setItem = {
|
|
1107
|
+
key,
|
|
1108
|
+
value: secondaryResult.value,
|
|
1109
|
+
ttl
|
|
1110
|
+
};
|
|
1111
|
+
await this.hook("BEFORE_SECONDARY_SETS_PRIMARY", setItem);
|
|
1112
|
+
await primary.set(setItem.key, setItem.value, (0, _cacheable_utils.resolvePerStoreTtl)(setItem.ttl).primary);
|
|
1113
|
+
} else this.emit("cache:miss", {
|
|
1114
|
+
key,
|
|
1115
|
+
store: "secondary"
|
|
1116
|
+
});
|
|
1117
|
+
secondaryIndex++;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Processes missing keys from secondary store for getManyRaw operation in non-blocking mode
|
|
1122
|
+
* Non-blocking mode means we don't wait for secondary operations that update primary store
|
|
1123
|
+
* @param secondary - the secondary store to use
|
|
1124
|
+
* @param keys - The original array of keys requested
|
|
1125
|
+
* @param result - The result array from primary store (will be modified)
|
|
1126
|
+
* @returns Promise<void>
|
|
1127
|
+
*/
|
|
1128
|
+
async processSecondaryForGetManyRawNonBlocking(primary, secondary, keys, result) {
|
|
1129
|
+
const missingKeys = [];
|
|
1130
|
+
for (const [i, key] of keys.entries()) if (!result[i]) missingKeys.push(key);
|
|
1131
|
+
const secondaryResults = await secondary.getManyRaw(missingKeys);
|
|
1132
|
+
let secondaryIndex = 0;
|
|
1133
|
+
for await (const [i, key] of keys.entries()) if (!result[i]) {
|
|
1134
|
+
const secondaryResult = secondaryResults[secondaryIndex];
|
|
1135
|
+
if (secondaryResult && secondaryResult.value !== void 0) {
|
|
1136
|
+
result[i] = secondaryResult;
|
|
1137
|
+
this.emit("cache:hit", {
|
|
1138
|
+
key,
|
|
1139
|
+
value: secondaryResult.value,
|
|
1140
|
+
store: "secondary"
|
|
1141
|
+
});
|
|
1142
|
+
const cascadeTtl = (0, _cacheable_utils.getCascadingTtl)(this._ttl, this._primary.ttl);
|
|
1143
|
+
let { expires } = secondaryResult;
|
|
1144
|
+
/* v8 ignore next -- @preserve */
|
|
1145
|
+
if (expires === null) expires = void 0;
|
|
1146
|
+
const ttl = (0, _cacheable_utils.calculateTtlFromExpiration)(cascadeTtl, expires);
|
|
1147
|
+
const setItem = {
|
|
1148
|
+
key,
|
|
1149
|
+
value: secondaryResult.value,
|
|
1150
|
+
ttl
|
|
1151
|
+
};
|
|
1152
|
+
/* v8 ignore next -- @preserve */
|
|
1153
|
+
this.hook("BEFORE_SECONDARY_SETS_PRIMARY", setItem).then(async () => {
|
|
1154
|
+
await primary.set(setItem.key, setItem.value, (0, _cacheable_utils.resolvePerStoreTtl)(setItem.ttl).primary);
|
|
1155
|
+
}).catch((error) => {
|
|
1156
|
+
/* v8 ignore next -- @preserve */
|
|
1157
|
+
this.emit("error", error);
|
|
1158
|
+
});
|
|
1159
|
+
} else this.emit("cache:miss", {
|
|
1160
|
+
key,
|
|
1161
|
+
store: "secondary"
|
|
1162
|
+
});
|
|
1163
|
+
secondaryIndex++;
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
setTtl(ttl) {
|
|
1167
|
+
if (typeof ttl === "string" || ttl === void 0) this._ttl = ttl;
|
|
1168
|
+
else if (ttl > 0) this._ttl = ttl;
|
|
1169
|
+
else this._ttl = void 0;
|
|
1170
|
+
}
|
|
1171
|
+
setMaxTtl(maxTtl) {
|
|
1172
|
+
if (typeof maxTtl === "string" || maxTtl === void 0) this._maxTtl = maxTtl;
|
|
1173
|
+
else if (maxTtl > 0) this._maxTtl = maxTtl;
|
|
1174
|
+
else this._maxTtl = void 0;
|
|
1175
|
+
}
|
|
1176
|
+
capTtl(ttl, maxTtlMs) {
|
|
1177
|
+
if (ttl !== void 0 && (Number.isNaN(ttl) || ttl < 0)) ttl = void 0;
|
|
1178
|
+
if (maxTtlMs === void 0) return ttl;
|
|
1179
|
+
if (ttl === void 0) return maxTtlMs;
|
|
1180
|
+
return Math.min(ttl, maxTtlMs);
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Resolves the ttl for a tag snapshot so it outlives the longest-lived copy of the value across
|
|
1184
|
+
* the stores. With a secondary store the snapshot uses the larger of the two ttls and never
|
|
1185
|
+
* expires if either copy never expires; with only a primary store it tracks the primary ttl.
|
|
1186
|
+
* @param primaryTtl - the resolved primary store ttl in milliseconds, or undefined for no expiry
|
|
1187
|
+
* @param secondaryTtl - the resolved secondary store ttl in milliseconds, or undefined for no expiry
|
|
1188
|
+
* @returns {number | undefined} The tag snapshot ttl in milliseconds, or undefined for no expiry
|
|
1189
|
+
*/
|
|
1190
|
+
maxStoreTtl(primaryTtl, secondaryTtl) {
|
|
1191
|
+
if (!this._secondary) return primaryTtl;
|
|
1192
|
+
if (!primaryTtl || !secondaryTtl) return;
|
|
1193
|
+
return Math.max(primaryTtl, secondaryTtl);
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Resolves the effective ttl actually written to one store for a `setMany` item: the per-store
|
|
1197
|
+
* explicit value (a scalar applies to both stores; a `{ primary, secondary }` object is honored
|
|
1198
|
+
* per field) cascaded with the store default and instance ttl, then capped by maxTtl.
|
|
1199
|
+
* @param itemTtl - the item's ttl (number, shorthand string, or per-store object)
|
|
1200
|
+
* @param storeTtl - the target store's default ttl in milliseconds
|
|
1201
|
+
* @param store - which store's field to resolve from a per-store object
|
|
1202
|
+
* @param maxTtlMs - the resolved maxTtl in milliseconds, or undefined for no cap
|
|
1203
|
+
* @returns {number | undefined} The effective ttl in milliseconds, or undefined for no expiry
|
|
1204
|
+
*/
|
|
1205
|
+
resolveStoreTtl(itemTtl, storeTtl, store, maxTtlMs) {
|
|
1206
|
+
const explicitTtl = (0, _cacheable_utils.resolvePerStoreTtl)(itemTtl)[store];
|
|
1207
|
+
return this.capTtl((0, _cacheable_utils.getCascadingTtl)(this._ttl, storeTtl, explicitTtl), maxTtlMs);
|
|
1208
|
+
}
|
|
17
1209
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
CacheableEvents: () => CacheableEvents,
|
|
25
|
-
CacheableHooks: () => CacheableHooks,
|
|
26
|
-
CacheableMemory: () => import_memory2.CacheableMemory,
|
|
27
|
-
CacheableStats: () => import_utils2.Stats,
|
|
28
|
-
CacheableSync: () => CacheableSync,
|
|
29
|
-
CacheableSyncEvents: () => CacheableSyncEvents,
|
|
30
|
-
HashAlgorithm: () => import_utils2.HashAlgorithm,
|
|
31
|
-
Keyv: () => import_keyv2.Keyv,
|
|
32
|
-
KeyvCacheableMemory: () => import_memory2.KeyvCacheableMemory,
|
|
33
|
-
KeyvHooks: () => import_keyv2.KeyvHooks,
|
|
34
|
-
calculateTtlFromExpiration: () => import_utils2.calculateTtlFromExpiration,
|
|
35
|
-
createKeyv: () => import_memory2.createKeyv,
|
|
36
|
-
getCascadingTtl: () => import_utils2.getCascadingTtl,
|
|
37
|
-
getOrSet: () => import_utils2.getOrSet,
|
|
38
|
-
hash: () => import_utils2.hash,
|
|
39
|
-
shorthandToMilliseconds: () => import_utils2.shorthandToMilliseconds,
|
|
40
|
-
shorthandToTime: () => import_utils2.shorthandToTime,
|
|
41
|
-
wrap: () => import_utils2.wrap,
|
|
42
|
-
wrapSync: () => import_utils2.wrapSync
|
|
1210
|
+
//#endregion
|
|
1211
|
+
Object.defineProperty(exports, "CacheTags", {
|
|
1212
|
+
enumerable: true,
|
|
1213
|
+
get: function() {
|
|
1214
|
+
return _cacheable_utils.CacheTags;
|
|
1215
|
+
}
|
|
43
1216
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
data
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Subscribes to sync events and updates the provided storage
|
|
143
|
-
* @param storage - The Keyv storage instance to update
|
|
144
|
-
* @param cacheId - The cache ID to identify this instance
|
|
145
|
-
*/
|
|
146
|
-
subscribe(storage, cacheId) {
|
|
147
|
-
this._storage = storage;
|
|
148
|
-
this._cacheId = cacheId;
|
|
149
|
-
const setEvent = this.getPrefixedEvent("cache:set" /* SET */);
|
|
150
|
-
const deleteEvent = this.getPrefixedEvent("cache:delete" /* DELETE */);
|
|
151
|
-
this._qified.subscribe(setEvent, {
|
|
152
|
-
handler: async (message) => {
|
|
153
|
-
const data = message.data;
|
|
154
|
-
if (data.cacheId !== cacheId) {
|
|
155
|
-
await storage.set(data.key, data.value, data.ttl);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
this._qified.subscribe(deleteEvent, {
|
|
160
|
-
handler: async (message) => {
|
|
161
|
-
const data = message.data;
|
|
162
|
-
if (data.cacheId !== cacheId) {
|
|
163
|
-
await storage.delete(data.key);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Creates or returns a Qified instance from the provided value
|
|
170
|
-
* @param value - Either an existing Qified instance or MessageProvider(s)
|
|
171
|
-
* @returns A Qified instance configured with the provided message provider(s)
|
|
172
|
-
*/
|
|
173
|
-
createQified(value) {
|
|
174
|
-
if (value instanceof import_qified.Qified) {
|
|
175
|
-
return value;
|
|
176
|
-
}
|
|
177
|
-
const providers = Array.isArray(value) ? value : [value];
|
|
178
|
-
return new import_qified.Qified({ messageProviders: providers });
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Gets the namespace prefix to use for event names
|
|
182
|
-
* @returns The resolved namespace string or undefined
|
|
183
|
-
*/
|
|
184
|
-
getNamespace() {
|
|
185
|
-
if (typeof this._namespace === "function") {
|
|
186
|
-
return this._namespace();
|
|
187
|
-
}
|
|
188
|
-
return this._namespace;
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Prefixes an event name with the namespace if one is set
|
|
192
|
-
* @param event - The event to prefix
|
|
193
|
-
* @returns The prefixed event name or the original event
|
|
194
|
-
*/
|
|
195
|
-
getPrefixedEvent(event) {
|
|
196
|
-
const ns = this.getNamespace();
|
|
197
|
-
return ns ? `${ns}::${event}` : event;
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
// src/index.ts
|
|
202
|
-
var import_memory2 = require("@cacheable/memory");
|
|
203
|
-
var import_utils2 = require("@cacheable/utils");
|
|
204
|
-
var import_keyv2 = require("keyv");
|
|
205
|
-
var Cacheable = class extends import_hookified2.Hookified {
|
|
206
|
-
_primary = (0, import_memory.createKeyv)();
|
|
207
|
-
_secondary;
|
|
208
|
-
_nonBlocking = false;
|
|
209
|
-
_ttl;
|
|
210
|
-
_stats = new import_utils.Stats({ enabled: false });
|
|
211
|
-
_namespace;
|
|
212
|
-
_cacheId = Math.random().toString(36).slice(2);
|
|
213
|
-
_sync;
|
|
214
|
-
/**
|
|
215
|
-
* Creates a new cacheable instance
|
|
216
|
-
* @param {CacheableOptions} [options] The options for the cacheable instance
|
|
217
|
-
*/
|
|
218
|
-
constructor(options) {
|
|
219
|
-
super();
|
|
220
|
-
if (options?.primary) {
|
|
221
|
-
this.setPrimary(options.primary);
|
|
222
|
-
}
|
|
223
|
-
if (options?.secondary) {
|
|
224
|
-
this.setSecondary(options.secondary);
|
|
225
|
-
}
|
|
226
|
-
if (options?.nonBlocking) {
|
|
227
|
-
this._nonBlocking = options.nonBlocking;
|
|
228
|
-
}
|
|
229
|
-
if (options?.stats) {
|
|
230
|
-
this._stats.enabled = options.stats;
|
|
231
|
-
}
|
|
232
|
-
if (options?.ttl) {
|
|
233
|
-
this.setTtl(options.ttl);
|
|
234
|
-
}
|
|
235
|
-
if (options?.cacheId) {
|
|
236
|
-
this._cacheId = options.cacheId;
|
|
237
|
-
}
|
|
238
|
-
if (options?.namespace) {
|
|
239
|
-
this._namespace = options.namespace;
|
|
240
|
-
this._primary.namespace = this.getNameSpace();
|
|
241
|
-
if (this._secondary) {
|
|
242
|
-
this._secondary.namespace = this.getNameSpace();
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
if (options?.sync) {
|
|
246
|
-
this._sync = options.sync instanceof CacheableSync ? options.sync : new CacheableSync({
|
|
247
|
-
...options.sync,
|
|
248
|
-
namespace: options.namespace
|
|
249
|
-
});
|
|
250
|
-
this._sync.subscribe(this._primary, this._cacheId);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* The namespace for the cacheable instance
|
|
255
|
-
* @returns {string | (() => string) | undefined} The namespace for the cacheable instance
|
|
256
|
-
*/
|
|
257
|
-
get namespace() {
|
|
258
|
-
return this._namespace;
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Sets the namespace for the cacheable instance
|
|
262
|
-
* @param {string | (() => string) | undefined} namespace The namespace for the cacheable instance
|
|
263
|
-
* @returns {void}
|
|
264
|
-
*/
|
|
265
|
-
set namespace(namespace) {
|
|
266
|
-
this._namespace = namespace;
|
|
267
|
-
this._primary.namespace = this.getNameSpace();
|
|
268
|
-
if (this._secondary) {
|
|
269
|
-
this._secondary.namespace = this.getNameSpace();
|
|
270
|
-
}
|
|
271
|
-
if (this._sync) {
|
|
272
|
-
this._sync.namespace = namespace;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* The statistics for the cacheable instance
|
|
277
|
-
* @returns {CacheableStats} The statistics for the cacheable instance
|
|
278
|
-
*/
|
|
279
|
-
get stats() {
|
|
280
|
-
return this._stats;
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* The primary store for the cacheable instance
|
|
284
|
-
* @returns {Keyv} The primary store for the cacheable instance
|
|
285
|
-
*/
|
|
286
|
-
get primary() {
|
|
287
|
-
return this._primary;
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Sets the primary store for the cacheable instance
|
|
291
|
-
* @param {Keyv} primary The primary store for the cacheable instance
|
|
292
|
-
*/
|
|
293
|
-
set primary(primary) {
|
|
294
|
-
this._primary = primary;
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* The secondary store for the cacheable instance
|
|
298
|
-
* @returns {Keyv | undefined} The secondary store for the cacheable instance
|
|
299
|
-
*/
|
|
300
|
-
get secondary() {
|
|
301
|
-
return this._secondary;
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
|
|
305
|
-
* @param {Keyv | undefined} secondary The secondary store for the cacheable instance
|
|
306
|
-
* @returns {void}
|
|
307
|
-
*/
|
|
308
|
-
set secondary(secondary) {
|
|
309
|
-
this._secondary = secondary;
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Gets whether the secondary store is non-blocking mode. It is set to false by default.
|
|
313
|
-
* If it is set to true then the secondary store will not block the primary store.
|
|
314
|
-
*
|
|
315
|
-
* [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
|
|
316
|
-
*
|
|
317
|
-
* @returns {boolean} Whether the cacheable instance is non-blocking
|
|
318
|
-
*/
|
|
319
|
-
get nonBlocking() {
|
|
320
|
-
return this._nonBlocking;
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Sets whether the secondary store is non-blocking mode. It is set to false by default.
|
|
324
|
-
* If it is set to true then the secondary store will not block the primary store.
|
|
325
|
-
*
|
|
326
|
-
* [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
|
|
327
|
-
*
|
|
328
|
-
* @param {boolean} nonBlocking Whether the cacheable instance is non-blocking
|
|
329
|
-
* @returns {void}
|
|
330
|
-
*/
|
|
331
|
-
set nonBlocking(nonBlocking) {
|
|
332
|
-
this._nonBlocking = nonBlocking;
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* The time-to-live for the cacheable instance and will be used as the default value.
|
|
336
|
-
* can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
|
|
337
|
-
* or undefined if there is no time-to-live.
|
|
338
|
-
*
|
|
339
|
-
* [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
|
|
340
|
-
*
|
|
341
|
-
* @returns {number | string | undefined} The time-to-live for the cacheable instance in milliseconds, human-readable format or undefined
|
|
342
|
-
* @example
|
|
343
|
-
* ```typescript
|
|
344
|
-
* const cacheable = new Cacheable({ ttl: '1h' });
|
|
345
|
-
* console.log(cacheable.ttl); // 1h
|
|
346
|
-
* ```
|
|
347
|
-
*/
|
|
348
|
-
get ttl() {
|
|
349
|
-
return this._ttl;
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Sets the time-to-live for the cacheable instance and will be used as the default value.
|
|
353
|
-
* If you set a number it is miliseconds, if you set a string it is a human-readable
|
|
354
|
-
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that
|
|
355
|
-
* there is no time-to-live.
|
|
356
|
-
*
|
|
357
|
-
* [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
|
|
358
|
-
*
|
|
359
|
-
* @param {number | string | undefined} ttl The time-to-live for the cacheable instance
|
|
360
|
-
* @example
|
|
361
|
-
* ```typescript
|
|
362
|
-
* const cacheable = new Cacheable();
|
|
363
|
-
* cacheable.ttl = '1h'; // Set the time-to-live to 1 hour
|
|
364
|
-
* ```
|
|
365
|
-
* or setting the time-to-live in milliseconds
|
|
366
|
-
* ```typescript
|
|
367
|
-
* const cacheable = new Cacheable();
|
|
368
|
-
* cacheable.ttl = 3600000; // Set the time-to-live to 1 hour
|
|
369
|
-
* ```
|
|
370
|
-
*/
|
|
371
|
-
set ttl(ttl) {
|
|
372
|
-
this.setTtl(ttl);
|
|
373
|
-
}
|
|
374
|
-
/**
|
|
375
|
-
* The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
|
|
376
|
-
* If it is not set then it will be a random string that is generated
|
|
377
|
-
* @returns {string} The cacheId for the cacheable instance
|
|
378
|
-
*/
|
|
379
|
-
get cacheId() {
|
|
380
|
-
return this._cacheId;
|
|
381
|
-
}
|
|
382
|
-
/**
|
|
383
|
-
* Sets the cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
|
|
384
|
-
* If it is not set then it will be a random string that is generated
|
|
385
|
-
* @param {string} cacheId The cacheId for the cacheable instance
|
|
386
|
-
*/
|
|
387
|
-
set cacheId(cacheId) {
|
|
388
|
-
this._cacheId = cacheId;
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Gets the sync instance for the cacheable instance
|
|
392
|
-
* @returns {CacheableSync | undefined} The sync instance for the cacheable instance
|
|
393
|
-
*/
|
|
394
|
-
get sync() {
|
|
395
|
-
return this._sync;
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* Sets the sync instance for the cacheable instance
|
|
399
|
-
* @param {CacheableSync | undefined} sync The sync instance for the cacheable instance
|
|
400
|
-
*/
|
|
401
|
-
set sync(sync) {
|
|
402
|
-
this._sync = sync;
|
|
403
|
-
if (this._sync) {
|
|
404
|
-
this._sync.subscribe(this._primary, this._cacheId);
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* Sets the primary store for the cacheable instance
|
|
409
|
-
* @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
|
|
410
|
-
* @returns {void}
|
|
411
|
-
*/
|
|
412
|
-
setPrimary(primary) {
|
|
413
|
-
if ((0, import_utils.isKeyvInstance)(primary)) {
|
|
414
|
-
this._primary = primary;
|
|
415
|
-
} else {
|
|
416
|
-
this._primary = new import_keyv.Keyv(primary);
|
|
417
|
-
}
|
|
418
|
-
this._primary.on("error", (error) => {
|
|
419
|
-
this.emit("error" /* ERROR */, error);
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
|
|
424
|
-
* @param {Keyv | KeyvStoreAdapter} secondary The secondary store for the cacheable instance
|
|
425
|
-
* @returns {void}
|
|
426
|
-
*/
|
|
427
|
-
setSecondary(secondary) {
|
|
428
|
-
if ((0, import_utils.isKeyvInstance)(secondary)) {
|
|
429
|
-
this._secondary = secondary;
|
|
430
|
-
} else {
|
|
431
|
-
this._secondary = new import_keyv.Keyv(secondary);
|
|
432
|
-
}
|
|
433
|
-
this._secondary.on("error", (error) => {
|
|
434
|
-
this.emit("error" /* ERROR */, error);
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
getNameSpace() {
|
|
438
|
-
if (typeof this._namespace === "function") {
|
|
439
|
-
return this._namespace();
|
|
440
|
-
}
|
|
441
|
-
return this._namespace;
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Retrieves an entry from the cache.
|
|
445
|
-
*
|
|
446
|
-
* Checks the primary store first; if not found and a secondary store is configured,
|
|
447
|
-
* it will fetch from the secondary, repopulate the primary, and return the result.
|
|
448
|
-
*
|
|
449
|
-
* @typeParam T - The expected type of the stored value.
|
|
450
|
-
* @param {string} key - The cache key to retrieve.
|
|
451
|
-
* @param {GetOptions} - options such as to bypass `nonBlocking` for this call
|
|
452
|
-
* @returns {Promise<T | undefined>}
|
|
453
|
-
* A promise that resolves to the cached value if found, or `undefined`.
|
|
454
|
-
*/
|
|
455
|
-
async get(key, options) {
|
|
456
|
-
const result = await this.getRaw(key, options);
|
|
457
|
-
return result?.value;
|
|
458
|
-
}
|
|
459
|
-
/**
|
|
460
|
-
* Retrieves the raw entry from the cache including metadata like expiration.
|
|
461
|
-
*
|
|
462
|
-
* Checks the primary store first; if not found and a secondary store is configured,
|
|
463
|
-
* it will fetch from the secondary, repopulate the primary, and return the result.
|
|
464
|
-
*
|
|
465
|
-
* @typeParam T - The expected type of the stored value.
|
|
466
|
-
* @param {string} key - The cache key to retrieve.
|
|
467
|
-
* @param {GetOptions} - options such as to bypass `nonBlocking` for this call
|
|
468
|
-
* @returns {Promise<StoredDataRaw<T>>}
|
|
469
|
-
* A promise that resolves to the full raw data object if found, or undefined.
|
|
470
|
-
*/
|
|
471
|
-
async getRaw(key, options) {
|
|
472
|
-
let result;
|
|
473
|
-
try {
|
|
474
|
-
await this.hook("BEFORE_GET" /* BEFORE_GET */, key);
|
|
475
|
-
result = await this._primary.getRaw(key);
|
|
476
|
-
let ttl;
|
|
477
|
-
if (result) {
|
|
478
|
-
this.emit("cache:hit" /* CACHE_HIT */, {
|
|
479
|
-
key,
|
|
480
|
-
value: result.value,
|
|
481
|
-
store: "primary"
|
|
482
|
-
});
|
|
483
|
-
} else {
|
|
484
|
-
this.emit("cache:miss" /* CACHE_MISS */, { key, store: "primary" });
|
|
485
|
-
}
|
|
486
|
-
const nonBlocking = options?.nonBlocking ?? this._nonBlocking;
|
|
487
|
-
if (!result && this._secondary) {
|
|
488
|
-
let secondaryProcessResult;
|
|
489
|
-
if (nonBlocking) {
|
|
490
|
-
secondaryProcessResult = await this.processSecondaryForGetRawNonBlocking(
|
|
491
|
-
this._primary,
|
|
492
|
-
this._secondary,
|
|
493
|
-
key
|
|
494
|
-
);
|
|
495
|
-
} else {
|
|
496
|
-
secondaryProcessResult = await this.processSecondaryForGetRaw(
|
|
497
|
-
this._primary,
|
|
498
|
-
this._secondary,
|
|
499
|
-
key
|
|
500
|
-
);
|
|
501
|
-
}
|
|
502
|
-
if (secondaryProcessResult) {
|
|
503
|
-
result = secondaryProcessResult.result;
|
|
504
|
-
ttl = secondaryProcessResult.ttl;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
await this.hook("AFTER_GET" /* AFTER_GET */, { key, result, ttl });
|
|
508
|
-
} catch (error) {
|
|
509
|
-
this.emit("error" /* ERROR */, error);
|
|
510
|
-
}
|
|
511
|
-
if (this.stats.enabled) {
|
|
512
|
-
if (result) {
|
|
513
|
-
this._stats.incrementHits();
|
|
514
|
-
} else {
|
|
515
|
-
this._stats.incrementMisses();
|
|
516
|
-
}
|
|
517
|
-
this.stats.incrementGets();
|
|
518
|
-
}
|
|
519
|
-
return result;
|
|
520
|
-
}
|
|
521
|
-
/**
|
|
522
|
-
* Retrieves multiple raw entries from the cache including metadata like expiration.
|
|
523
|
-
*
|
|
524
|
-
* Checks the primary store for each key; if a key is missing and a secondary store is configured,
|
|
525
|
-
* it will fetch from the secondary store, repopulate the primary store, and return the results.
|
|
526
|
-
*
|
|
527
|
-
* @typeParam T - The expected type of the stored values.
|
|
528
|
-
* @param {string[]} keys - The cache keys to retrieve.
|
|
529
|
-
* @param {GetOptions} - options such as to bypass `nonBlocking` on this call
|
|
530
|
-
* @returns {Promise<Array<StoredDataRaw<T>>>}
|
|
531
|
-
* A promise that resolves to an array of raw data objects.
|
|
532
|
-
*/
|
|
533
|
-
async getManyRaw(keys, options) {
|
|
534
|
-
let result = [];
|
|
535
|
-
try {
|
|
536
|
-
await this.hook("BEFORE_GET_MANY" /* BEFORE_GET_MANY */, keys);
|
|
537
|
-
result = await this._primary.getManyRaw(keys);
|
|
538
|
-
for (const [i, key] of keys.entries()) {
|
|
539
|
-
if (result[i]) {
|
|
540
|
-
this.emit("cache:hit" /* CACHE_HIT */, {
|
|
541
|
-
key,
|
|
542
|
-
value: result[i].value,
|
|
543
|
-
store: "primary"
|
|
544
|
-
});
|
|
545
|
-
} else {
|
|
546
|
-
this.emit("cache:miss" /* CACHE_MISS */, { key, store: "primary" });
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
const nonBlocking = options?.nonBlocking ?? this._nonBlocking;
|
|
550
|
-
if (this._secondary) {
|
|
551
|
-
if (nonBlocking) {
|
|
552
|
-
await this.processSecondaryForGetManyRawNonBlocking(
|
|
553
|
-
this._primary,
|
|
554
|
-
this._secondary,
|
|
555
|
-
keys,
|
|
556
|
-
result
|
|
557
|
-
);
|
|
558
|
-
} else {
|
|
559
|
-
await this.processSecondaryForGetManyRaw(
|
|
560
|
-
this._primary,
|
|
561
|
-
this._secondary,
|
|
562
|
-
keys,
|
|
563
|
-
result
|
|
564
|
-
);
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
await this.hook("AFTER_GET_MANY" /* AFTER_GET_MANY */, { keys, result });
|
|
568
|
-
} catch (error) {
|
|
569
|
-
this.emit("error" /* ERROR */, error);
|
|
570
|
-
}
|
|
571
|
-
if (this.stats.enabled) {
|
|
572
|
-
for (const item of result) {
|
|
573
|
-
if (item) {
|
|
574
|
-
this._stats.incrementHits();
|
|
575
|
-
} else {
|
|
576
|
-
this._stats.incrementMisses();
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
this.stats.incrementGets();
|
|
580
|
-
}
|
|
581
|
-
return result;
|
|
582
|
-
}
|
|
583
|
-
/**
|
|
584
|
-
* Retrieves multiple entries from the cache.
|
|
585
|
-
* Checks the primary store for each key; if a key is missing and a secondary store is configured,
|
|
586
|
-
* it will fetch from the secondary store, repopulate the primary store, and return the results.
|
|
587
|
-
*
|
|
588
|
-
* @typeParam T - The expected type of the stored values.
|
|
589
|
-
* @param {string[]} keys - The cache keys to retrieve.
|
|
590
|
-
* @param {GetOptions} - options such as to bypass `nonBlocking` on this call
|
|
591
|
-
* @returns {Promise<Array<T | undefined>>}
|
|
592
|
-
* A promise that resolves to an array of cached values or `undefined` for misses.
|
|
593
|
-
*/
|
|
594
|
-
async getMany(keys, options) {
|
|
595
|
-
const result = await this.getManyRaw(keys, options);
|
|
596
|
-
return result.map((item) => item?.value);
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* Sets the value of the key. If the secondary store is set then it will also set the value in the secondary store.
|
|
600
|
-
* @param {string} key the key to set the value of
|
|
601
|
-
* @param {T} value The value to set
|
|
602
|
-
* @param {number | string} [ttl] set a number it is miliseconds, set a string it is a human-readable
|
|
603
|
-
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live.
|
|
604
|
-
* @returns {boolean} Whether the value was set
|
|
605
|
-
*/
|
|
606
|
-
async set(key, value, ttl) {
|
|
607
|
-
let result = false;
|
|
608
|
-
const explicitTtl = (0, import_utils.shorthandToMilliseconds)(ttl);
|
|
609
|
-
try {
|
|
610
|
-
const primaryTtl = (0, import_utils.getCascadingTtl)(
|
|
611
|
-
this._ttl,
|
|
612
|
-
this._primary.ttl,
|
|
613
|
-
explicitTtl
|
|
614
|
-
);
|
|
615
|
-
const item = { key, value, ttl: primaryTtl };
|
|
616
|
-
await this.hook("BEFORE_SET" /* BEFORE_SET */, item);
|
|
617
|
-
const hookOverridden = item.ttl !== primaryTtl;
|
|
618
|
-
const promises = [];
|
|
619
|
-
promises.push(this._primary.set(item.key, item.value, item.ttl));
|
|
620
|
-
if (this._secondary) {
|
|
621
|
-
const secondaryTtl = hookOverridden ? item.ttl : (0, import_utils.getCascadingTtl)(this._ttl, this._secondary.ttl, explicitTtl);
|
|
622
|
-
promises.push(this._secondary.set(item.key, item.value, secondaryTtl));
|
|
623
|
-
}
|
|
624
|
-
if (this._nonBlocking) {
|
|
625
|
-
result = await Promise.race(promises);
|
|
626
|
-
for (const promise of promises) {
|
|
627
|
-
promise.catch((error) => {
|
|
628
|
-
this.emit("error" /* ERROR */, error);
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
} else {
|
|
632
|
-
const results = await Promise.all(promises);
|
|
633
|
-
result = results[0];
|
|
634
|
-
}
|
|
635
|
-
await this.hook("AFTER_SET" /* AFTER_SET */, item);
|
|
636
|
-
if (this._sync && result) {
|
|
637
|
-
await this._sync.publish("cache:set" /* SET */, {
|
|
638
|
-
cacheId: this._cacheId,
|
|
639
|
-
key: item.key,
|
|
640
|
-
value: item.value,
|
|
641
|
-
ttl: item.ttl
|
|
642
|
-
});
|
|
643
|
-
}
|
|
644
|
-
} catch (error) {
|
|
645
|
-
this.emit("error" /* ERROR */, error);
|
|
646
|
-
}
|
|
647
|
-
if (this.stats.enabled) {
|
|
648
|
-
this.stats.incrementKSize(key);
|
|
649
|
-
this.stats.incrementCount();
|
|
650
|
-
this.stats.incrementVSize(value);
|
|
651
|
-
this.stats.incrementSets();
|
|
652
|
-
}
|
|
653
|
-
return result;
|
|
654
|
-
}
|
|
655
|
-
/**
|
|
656
|
-
* Sets the values of the keys. If the secondary store is set then it will also set the values in the secondary store.
|
|
657
|
-
* @param {CacheableItem[]} items The items to set
|
|
658
|
-
* @returns {boolean} Whether the values were set
|
|
659
|
-
*/
|
|
660
|
-
async setMany(items) {
|
|
661
|
-
let result = false;
|
|
662
|
-
try {
|
|
663
|
-
await this.hook("BEFORE_SET_MANY" /* BEFORE_SET_MANY */, items);
|
|
664
|
-
result = await this.setManyKeyv(this._primary, items);
|
|
665
|
-
if (this._secondary) {
|
|
666
|
-
if (this._nonBlocking) {
|
|
667
|
-
this.setManyKeyv(this._secondary, items).catch((error) => {
|
|
668
|
-
this.emit("error" /* ERROR */, error);
|
|
669
|
-
});
|
|
670
|
-
} else {
|
|
671
|
-
await this.setManyKeyv(this._secondary, items);
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
await this.hook("AFTER_SET_MANY" /* AFTER_SET_MANY */, items);
|
|
675
|
-
if (this._sync && result) {
|
|
676
|
-
for (const item of items) {
|
|
677
|
-
await this._sync.publish("cache:set" /* SET */, {
|
|
678
|
-
cacheId: this._cacheId,
|
|
679
|
-
key: item.key,
|
|
680
|
-
value: item.value,
|
|
681
|
-
ttl: (0, import_utils.shorthandToMilliseconds)(item.ttl)
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
} catch (error) {
|
|
686
|
-
this.emit("error" /* ERROR */, error);
|
|
687
|
-
}
|
|
688
|
-
if (this.stats.enabled) {
|
|
689
|
-
for (const item of items) {
|
|
690
|
-
this.stats.incrementKSize(item.key);
|
|
691
|
-
this.stats.incrementCount();
|
|
692
|
-
this.stats.incrementVSize(item.value);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
return result;
|
|
696
|
-
}
|
|
697
|
-
/**
|
|
698
|
-
* Takes the value of the key and deletes the key. If the key does not exist then it will return undefined.
|
|
699
|
-
* @param {string} key The key to take the value of
|
|
700
|
-
* @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
|
|
701
|
-
*/
|
|
702
|
-
async take(key) {
|
|
703
|
-
const result = await this.get(key);
|
|
704
|
-
await this.delete(key);
|
|
705
|
-
return result;
|
|
706
|
-
}
|
|
707
|
-
/**
|
|
708
|
-
* Takes the values of the keys and deletes the keys. If the key does not exist then it will return undefined.
|
|
709
|
-
* @param {string[]} keys The keys to take the values of
|
|
710
|
-
* @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
|
|
711
|
-
*/
|
|
712
|
-
async takeMany(keys) {
|
|
713
|
-
const result = await this.getMany(keys);
|
|
714
|
-
await this.deleteMany(keys);
|
|
715
|
-
return result;
|
|
716
|
-
}
|
|
717
|
-
/**
|
|
718
|
-
* Checks if the key exists in the primary store. If it does not exist then it will check the secondary store.
|
|
719
|
-
* @param {string} key The key to check
|
|
720
|
-
* @returns {Promise<boolean>} Whether the key exists
|
|
721
|
-
*/
|
|
722
|
-
async has(key) {
|
|
723
|
-
const promises = [];
|
|
724
|
-
promises.push(this._primary.has(key));
|
|
725
|
-
if (this._secondary) {
|
|
726
|
-
promises.push(this._secondary.has(key));
|
|
727
|
-
}
|
|
728
|
-
const resultAll = await Promise.all(promises);
|
|
729
|
-
for (const result of resultAll) {
|
|
730
|
-
if (result) {
|
|
731
|
-
return true;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
return false;
|
|
735
|
-
}
|
|
736
|
-
/**
|
|
737
|
-
* Checks if the keys exist in the primary store. If it does not exist then it will check the secondary store.
|
|
738
|
-
* @param {string[]} keys The keys to check
|
|
739
|
-
* @returns {Promise<boolean[]>} Whether the keys exist
|
|
740
|
-
*/
|
|
741
|
-
async hasMany(keys) {
|
|
742
|
-
const result = await this._primary.hasMany(keys);
|
|
743
|
-
const missingKeys = [];
|
|
744
|
-
for (const [i, key] of keys.entries()) {
|
|
745
|
-
if (!result[i] && this._secondary) {
|
|
746
|
-
missingKeys.push(key);
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
if (missingKeys.length > 0 && this._secondary) {
|
|
750
|
-
const secondary = await this._secondary.hasMany(keys);
|
|
751
|
-
for (const [i, _key] of keys.entries()) {
|
|
752
|
-
if (!result[i] && secondary[i]) {
|
|
753
|
-
result[i] = secondary[i];
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
return result;
|
|
758
|
-
}
|
|
759
|
-
/**
|
|
760
|
-
* Deletes the key from the primary store. If the secondary store is set then it will also delete the key from the secondary store.
|
|
761
|
-
* @param {string} key The key to delete
|
|
762
|
-
* @returns {Promise<boolean>} Whether the key was deleted
|
|
763
|
-
*/
|
|
764
|
-
async delete(key) {
|
|
765
|
-
let result = false;
|
|
766
|
-
const promises = [];
|
|
767
|
-
if (this.stats.enabled) {
|
|
768
|
-
const statResult = await this._primary.get(key);
|
|
769
|
-
if (statResult) {
|
|
770
|
-
this.stats.decreaseKSize(key);
|
|
771
|
-
this.stats.decreaseVSize(statResult);
|
|
772
|
-
this.stats.decreaseCount();
|
|
773
|
-
this.stats.incrementDeletes();
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
promises.push(this._primary.delete(key));
|
|
777
|
-
if (this._secondary) {
|
|
778
|
-
promises.push(this._secondary.delete(key));
|
|
779
|
-
}
|
|
780
|
-
if (this.nonBlocking) {
|
|
781
|
-
result = await Promise.race(promises);
|
|
782
|
-
for (const promise of promises) {
|
|
783
|
-
promise.catch((error) => {
|
|
784
|
-
this.emit("error" /* ERROR */, error);
|
|
785
|
-
});
|
|
786
|
-
}
|
|
787
|
-
} else {
|
|
788
|
-
const resultAll = await Promise.all(promises);
|
|
789
|
-
result = resultAll[0];
|
|
790
|
-
}
|
|
791
|
-
if (this._sync && result) {
|
|
792
|
-
await this._sync.publish("cache:delete" /* DELETE */, {
|
|
793
|
-
cacheId: this._cacheId,
|
|
794
|
-
key
|
|
795
|
-
});
|
|
796
|
-
}
|
|
797
|
-
return result;
|
|
798
|
-
}
|
|
799
|
-
/**
|
|
800
|
-
* Deletes the keys from the primary store. If the secondary store is set then it will also delete the keys from the secondary store.
|
|
801
|
-
* @param {string[]} keys The keys to delete
|
|
802
|
-
* @returns {Promise<boolean>} Whether the keys were deleted
|
|
803
|
-
*/
|
|
804
|
-
async deleteMany(keys) {
|
|
805
|
-
if (this.stats.enabled) {
|
|
806
|
-
const statResult = await this._primary.get(keys);
|
|
807
|
-
for (const key of keys) {
|
|
808
|
-
this.stats.decreaseKSize(key);
|
|
809
|
-
this.stats.decreaseVSize(statResult);
|
|
810
|
-
this.stats.decreaseCount();
|
|
811
|
-
this.stats.incrementDeletes();
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
const result = await this._primary.deleteMany(keys);
|
|
815
|
-
if (this._secondary) {
|
|
816
|
-
if (this._nonBlocking) {
|
|
817
|
-
this._secondary.deleteMany(keys).catch((error) => {
|
|
818
|
-
this.emit("error" /* ERROR */, error);
|
|
819
|
-
});
|
|
820
|
-
} else {
|
|
821
|
-
await this._secondary.deleteMany(keys);
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
if (this._sync && result) {
|
|
825
|
-
for (const key of keys) {
|
|
826
|
-
await this._sync.publish("cache:delete" /* DELETE */, {
|
|
827
|
-
cacheId: this._cacheId,
|
|
828
|
-
key
|
|
829
|
-
});
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
return result;
|
|
833
|
-
}
|
|
834
|
-
/**
|
|
835
|
-
* Clears the primary store. If the secondary store is set then it will also clear the secondary store.
|
|
836
|
-
* @returns {Promise<void>}
|
|
837
|
-
*/
|
|
838
|
-
async clear() {
|
|
839
|
-
const promises = [];
|
|
840
|
-
promises.push(this._primary.clear());
|
|
841
|
-
if (this._secondary) {
|
|
842
|
-
promises.push(this._secondary.clear());
|
|
843
|
-
}
|
|
844
|
-
await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
|
|
845
|
-
if (this.stats.enabled) {
|
|
846
|
-
this._stats.resetStoreValues();
|
|
847
|
-
this._stats.incrementClears();
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
/**
|
|
851
|
-
* Disconnects the primary store. If the secondary store is set then it will also disconnect the secondary store.
|
|
852
|
-
* @returns {Promise<void>}
|
|
853
|
-
*/
|
|
854
|
-
async disconnect() {
|
|
855
|
-
const promises = [];
|
|
856
|
-
promises.push(this._primary.disconnect());
|
|
857
|
-
if (this._secondary) {
|
|
858
|
-
promises.push(this._secondary.disconnect());
|
|
859
|
-
}
|
|
860
|
-
promises.push(this._sync?.qified.disconnect());
|
|
861
|
-
await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
|
|
862
|
-
}
|
|
863
|
-
/**
|
|
864
|
-
* Wraps a function with caching
|
|
865
|
-
*
|
|
866
|
-
* [Learn more about wrapping functions](https://cacheable.org/docs/cacheable/#wrap--memoization-for-sync-and-async-functions).
|
|
867
|
-
* @param {Function} function_ The function to wrap
|
|
868
|
-
* @param {WrapOptions} [options] The options for the wrap function
|
|
869
|
-
* @returns {Function} The wrapped function
|
|
870
|
-
*/
|
|
871
|
-
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
872
|
-
wrap(function_, options) {
|
|
873
|
-
const cacheAdapter = {
|
|
874
|
-
get: async (key) => this.get(key),
|
|
875
|
-
/* v8 ignore next -- @preserve */
|
|
876
|
-
has: async (key) => this.has(key),
|
|
877
|
-
set: async (key, value, ttl) => {
|
|
878
|
-
await this.set(key, value, ttl);
|
|
879
|
-
},
|
|
880
|
-
/* v8 ignore next -- @preserve */
|
|
881
|
-
on: (event, listener) => {
|
|
882
|
-
this.on(event, listener);
|
|
883
|
-
},
|
|
884
|
-
/* v8 ignore next -- @preserve */
|
|
885
|
-
emit: (event, ...args) => this.emit(event, ...args)
|
|
886
|
-
};
|
|
887
|
-
const wrapOptions = {
|
|
888
|
-
ttl: options?.ttl ?? this._ttl,
|
|
889
|
-
keyPrefix: options?.keyPrefix,
|
|
890
|
-
createKey: options?.createKey,
|
|
891
|
-
cacheErrors: options?.cacheErrors,
|
|
892
|
-
cache: cacheAdapter,
|
|
893
|
-
cacheId: this._cacheId,
|
|
894
|
-
serialize: options?.serialize
|
|
895
|
-
};
|
|
896
|
-
return (0, import_utils.wrap)(function_, wrapOptions);
|
|
897
|
-
}
|
|
898
|
-
/**
|
|
899
|
-
* Retrieves the value associated with the given key from the cache. If the key is not found,
|
|
900
|
-
* invokes the provided function to calculate the value, stores it in the cache, and then returns it.
|
|
901
|
-
*
|
|
902
|
-
* @param {GetOrSetKey} key - The key to retrieve or set in the cache. This can also be a function that returns a string key.
|
|
903
|
-
* If a function is provided, it will be called with the cache options to generate the key.
|
|
904
|
-
* @param {() => Promise<T>} function_ - The asynchronous function that computes the value to be cached if the key does not exist.
|
|
905
|
-
* @param {GetOrSetFunctionOptions} [options] - Optional settings for caching, such as the time to live (TTL) or whether to cache errors.
|
|
906
|
-
* @return {Promise<T | undefined>} - A promise that resolves to the cached or newly computed value, or undefined if an error occurs and caching is not configured for errors.
|
|
907
|
-
*/
|
|
908
|
-
async getOrSet(key, function_, options) {
|
|
909
|
-
const getOptions = options?.nonBlocking === void 0 ? void 0 : { nonBlocking: options.nonBlocking };
|
|
910
|
-
const cacheAdapter = {
|
|
911
|
-
get: async (key2) => this.get(key2, getOptions),
|
|
912
|
-
/* v8 ignore next -- @preserve */
|
|
913
|
-
has: async (key2) => this.has(key2),
|
|
914
|
-
set: async (key2, value, ttl) => {
|
|
915
|
-
await this.set(key2, value, ttl);
|
|
916
|
-
},
|
|
917
|
-
/* v8 ignore next -- @preserve */
|
|
918
|
-
on: (event, listener) => {
|
|
919
|
-
this.on(event, listener);
|
|
920
|
-
},
|
|
921
|
-
emit: (event, ...args) => this.emit(event, ...args)
|
|
922
|
-
};
|
|
923
|
-
const getOrSetOptions = {
|
|
924
|
-
cache: cacheAdapter,
|
|
925
|
-
cacheId: this._cacheId,
|
|
926
|
-
ttl: options?.ttl ?? this._ttl,
|
|
927
|
-
cacheErrors: options?.cacheErrors,
|
|
928
|
-
throwErrors: options?.throwErrors,
|
|
929
|
-
nonBlocking: options?.nonBlocking
|
|
930
|
-
};
|
|
931
|
-
return (0, import_utils.getOrSet)(key, function_, getOrSetOptions);
|
|
932
|
-
}
|
|
933
|
-
/**
|
|
934
|
-
* Will hash an object asynchronously using the specified cryptographic algorithm.
|
|
935
|
-
* Use this for cryptographic algorithms (SHA-256, SHA-384, SHA-512).
|
|
936
|
-
* For non-cryptographic algorithms, use hashSync() for better performance.
|
|
937
|
-
* @param {any} object the object to hash
|
|
938
|
-
* @param {string} algorithm the hash algorithm to use. The default is 'SHA-256'
|
|
939
|
-
* @returns {Promise<string>} the hash of the object
|
|
940
|
-
*/
|
|
941
|
-
async hash(object, algorithm = import_utils.HashAlgorithm.SHA256) {
|
|
942
|
-
return (0, import_utils.hash)(object, { algorithm });
|
|
943
|
-
}
|
|
944
|
-
/**
|
|
945
|
-
* Will hash an object synchronously using the specified non-cryptographic algorithm.
|
|
946
|
-
* Use this for non-cryptographic algorithms (DJB2, FNV1, MURMER, CRC32).
|
|
947
|
-
* For cryptographic algorithms, use hash() instead.
|
|
948
|
-
* @param {any} object the object to hash
|
|
949
|
-
* @param {string} algorithm the hash algorithm to use. The default is 'djb2'
|
|
950
|
-
* @returns {string} the hash of the object
|
|
951
|
-
*/
|
|
952
|
-
hashSync(object, algorithm = import_utils.HashAlgorithm.DJB2) {
|
|
953
|
-
return (0, import_utils.hashSync)(object, { algorithm });
|
|
954
|
-
}
|
|
955
|
-
async setManyKeyv(keyv, items) {
|
|
956
|
-
const entries = [];
|
|
957
|
-
for (const item of items) {
|
|
958
|
-
const finalTtl = (0, import_utils.getCascadingTtl)(
|
|
959
|
-
this._ttl,
|
|
960
|
-
keyv.ttl,
|
|
961
|
-
(0, import_utils.shorthandToMilliseconds)(item.ttl)
|
|
962
|
-
);
|
|
963
|
-
entries.push({ key: item.key, value: item.value, ttl: finalTtl });
|
|
964
|
-
}
|
|
965
|
-
await keyv.setMany(entries);
|
|
966
|
-
return true;
|
|
967
|
-
}
|
|
968
|
-
/**
|
|
969
|
-
* Processes a single key from secondary store for getRaw operation
|
|
970
|
-
* @param primary - the primary store to use
|
|
971
|
-
* @param secondary - the secondary store to use
|
|
972
|
-
* @param key - The key to retrieve from secondary store
|
|
973
|
-
* @returns Promise containing the result and TTL information
|
|
974
|
-
*/
|
|
975
|
-
async processSecondaryForGetRaw(primary, secondary, key) {
|
|
976
|
-
const secondaryResult = await secondary.getRaw(key);
|
|
977
|
-
if (secondaryResult?.value) {
|
|
978
|
-
this.emit("cache:hit" /* CACHE_HIT */, {
|
|
979
|
-
key,
|
|
980
|
-
value: secondaryResult.value,
|
|
981
|
-
store: "secondary"
|
|
982
|
-
});
|
|
983
|
-
const cascadeTtl = (0, import_utils.getCascadingTtl)(this._ttl, this._primary.ttl);
|
|
984
|
-
const expires = secondaryResult.expires ?? void 0;
|
|
985
|
-
const ttl = (0, import_utils.calculateTtlFromExpiration)(cascadeTtl, expires);
|
|
986
|
-
const setItem = { key, value: secondaryResult.value, ttl };
|
|
987
|
-
await this.hook("BEFORE_SECONDARY_SETS_PRIMARY" /* BEFORE_SECONDARY_SETS_PRIMARY */, setItem);
|
|
988
|
-
await primary.set(setItem.key, setItem.value, setItem.ttl);
|
|
989
|
-
return { result: secondaryResult, ttl };
|
|
990
|
-
} else {
|
|
991
|
-
this.emit("cache:miss" /* CACHE_MISS */, { key, store: "secondary" });
|
|
992
|
-
return void 0;
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
/**
|
|
996
|
-
* Processes a single key from secondary store for getRaw operation in non-blocking mode
|
|
997
|
-
* Non-blocking mode means we don't wait for secondary operations that update primary store
|
|
998
|
-
* @param primary - the primary store to use
|
|
999
|
-
* @param secondary - the secondary store to use
|
|
1000
|
-
* @param key - The key to retrieve from secondary store
|
|
1001
|
-
* @returns Promise containing the result and TTL information
|
|
1002
|
-
*/
|
|
1003
|
-
async processSecondaryForGetRawNonBlocking(primary, secondary, key) {
|
|
1004
|
-
const secondaryResult = await secondary.getRaw(key);
|
|
1005
|
-
if (secondaryResult?.value) {
|
|
1006
|
-
this.emit("cache:hit" /* CACHE_HIT */, {
|
|
1007
|
-
key,
|
|
1008
|
-
value: secondaryResult.value,
|
|
1009
|
-
store: "secondary"
|
|
1010
|
-
});
|
|
1011
|
-
const cascadeTtl = (0, import_utils.getCascadingTtl)(this._ttl, this._primary.ttl);
|
|
1012
|
-
const expires = secondaryResult.expires ?? void 0;
|
|
1013
|
-
const ttl = (0, import_utils.calculateTtlFromExpiration)(cascadeTtl, expires);
|
|
1014
|
-
const setItem = { key, value: secondaryResult.value, ttl };
|
|
1015
|
-
this.hook("BEFORE_SECONDARY_SETS_PRIMARY" /* BEFORE_SECONDARY_SETS_PRIMARY */, setItem).then(async () => {
|
|
1016
|
-
await primary.set(setItem.key, setItem.value, setItem.ttl);
|
|
1017
|
-
}).catch((error) => {
|
|
1018
|
-
this.emit("error" /* ERROR */, error);
|
|
1019
|
-
});
|
|
1020
|
-
return { result: secondaryResult, ttl };
|
|
1021
|
-
} else {
|
|
1022
|
-
this.emit("cache:miss" /* CACHE_MISS */, { key, store: "secondary" });
|
|
1023
|
-
return void 0;
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
/**
|
|
1027
|
-
* Processes missing keys from secondary store for getManyRaw operation
|
|
1028
|
-
* @param primary - the primary store to use
|
|
1029
|
-
* @param secondary - the secondary store to use
|
|
1030
|
-
* @param keys - The original array of keys requested
|
|
1031
|
-
* @param result - The result array from primary store (will be modified)
|
|
1032
|
-
* @returns Promise<void>
|
|
1033
|
-
*/
|
|
1034
|
-
async processSecondaryForGetManyRaw(primary, secondary, keys, result) {
|
|
1035
|
-
const missingKeys = [];
|
|
1036
|
-
for (const [i, key] of keys.entries()) {
|
|
1037
|
-
if (!result[i]) {
|
|
1038
|
-
missingKeys.push(key);
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
const secondaryResults = await secondary.getManyRaw(missingKeys);
|
|
1042
|
-
let secondaryIndex = 0;
|
|
1043
|
-
for await (const [i, key] of keys.entries()) {
|
|
1044
|
-
if (!result[i]) {
|
|
1045
|
-
const secondaryResult = secondaryResults[secondaryIndex];
|
|
1046
|
-
if (secondaryResult && secondaryResult.value !== void 0) {
|
|
1047
|
-
result[i] = secondaryResult;
|
|
1048
|
-
this.emit("cache:hit" /* CACHE_HIT */, {
|
|
1049
|
-
key,
|
|
1050
|
-
value: secondaryResult.value,
|
|
1051
|
-
store: "secondary"
|
|
1052
|
-
});
|
|
1053
|
-
const cascadeTtl = (0, import_utils.getCascadingTtl)(this._ttl, this._primary.ttl);
|
|
1054
|
-
let { expires } = secondaryResult;
|
|
1055
|
-
if (expires === null) {
|
|
1056
|
-
expires = void 0;
|
|
1057
|
-
}
|
|
1058
|
-
const ttl = (0, import_utils.calculateTtlFromExpiration)(cascadeTtl, expires);
|
|
1059
|
-
const setItem = { key, value: secondaryResult.value, ttl };
|
|
1060
|
-
await this.hook(
|
|
1061
|
-
"BEFORE_SECONDARY_SETS_PRIMARY" /* BEFORE_SECONDARY_SETS_PRIMARY */,
|
|
1062
|
-
setItem
|
|
1063
|
-
);
|
|
1064
|
-
await primary.set(setItem.key, setItem.value, setItem.ttl);
|
|
1065
|
-
} else {
|
|
1066
|
-
this.emit("cache:miss" /* CACHE_MISS */, {
|
|
1067
|
-
key,
|
|
1068
|
-
store: "secondary"
|
|
1069
|
-
});
|
|
1070
|
-
}
|
|
1071
|
-
secondaryIndex++;
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
/**
|
|
1076
|
-
* Processes missing keys from secondary store for getManyRaw operation in non-blocking mode
|
|
1077
|
-
* Non-blocking mode means we don't wait for secondary operations that update primary store
|
|
1078
|
-
* @param secondary - the secondary store to use
|
|
1079
|
-
* @param keys - The original array of keys requested
|
|
1080
|
-
* @param result - The result array from primary store (will be modified)
|
|
1081
|
-
* @returns Promise<void>
|
|
1082
|
-
*/
|
|
1083
|
-
async processSecondaryForGetManyRawNonBlocking(primary, secondary, keys, result) {
|
|
1084
|
-
const missingKeys = [];
|
|
1085
|
-
for (const [i, key] of keys.entries()) {
|
|
1086
|
-
if (!result[i]) {
|
|
1087
|
-
missingKeys.push(key);
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
const secondaryResults = await secondary.getManyRaw(missingKeys);
|
|
1091
|
-
let secondaryIndex = 0;
|
|
1092
|
-
for await (const [i, key] of keys.entries()) {
|
|
1093
|
-
if (!result[i]) {
|
|
1094
|
-
const secondaryResult = secondaryResults[secondaryIndex];
|
|
1095
|
-
if (secondaryResult && secondaryResult.value !== void 0) {
|
|
1096
|
-
result[i] = secondaryResult;
|
|
1097
|
-
this.emit("cache:hit" /* CACHE_HIT */, {
|
|
1098
|
-
key,
|
|
1099
|
-
value: secondaryResult.value,
|
|
1100
|
-
store: "secondary"
|
|
1101
|
-
});
|
|
1102
|
-
const cascadeTtl = (0, import_utils.getCascadingTtl)(this._ttl, this._primary.ttl);
|
|
1103
|
-
let { expires } = secondaryResult;
|
|
1104
|
-
if (expires === null) {
|
|
1105
|
-
expires = void 0;
|
|
1106
|
-
}
|
|
1107
|
-
const ttl = (0, import_utils.calculateTtlFromExpiration)(cascadeTtl, expires);
|
|
1108
|
-
const setItem = { key, value: secondaryResult.value, ttl };
|
|
1109
|
-
this.hook("BEFORE_SECONDARY_SETS_PRIMARY" /* BEFORE_SECONDARY_SETS_PRIMARY */, setItem).then(async () => {
|
|
1110
|
-
await primary.set(setItem.key, setItem.value, setItem.ttl);
|
|
1111
|
-
}).catch((error) => {
|
|
1112
|
-
this.emit("error" /* ERROR */, error);
|
|
1113
|
-
});
|
|
1114
|
-
} else {
|
|
1115
|
-
this.emit("cache:miss" /* CACHE_MISS */, {
|
|
1116
|
-
key,
|
|
1117
|
-
store: "secondary"
|
|
1118
|
-
});
|
|
1119
|
-
}
|
|
1120
|
-
secondaryIndex++;
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
setTtl(ttl) {
|
|
1125
|
-
if (typeof ttl === "string" || ttl === void 0) {
|
|
1126
|
-
this._ttl = ttl;
|
|
1127
|
-
} else if (ttl > 0) {
|
|
1128
|
-
this._ttl = ttl;
|
|
1129
|
-
} else {
|
|
1130
|
-
this._ttl = void 0;
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
};
|
|
1134
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
1135
|
-
0 && (module.exports = {
|
|
1136
|
-
Cacheable,
|
|
1137
|
-
CacheableEvents,
|
|
1138
|
-
CacheableHooks,
|
|
1139
|
-
CacheableMemory,
|
|
1140
|
-
CacheableStats,
|
|
1141
|
-
CacheableSync,
|
|
1142
|
-
CacheableSyncEvents,
|
|
1143
|
-
HashAlgorithm,
|
|
1144
|
-
Keyv,
|
|
1145
|
-
KeyvCacheableMemory,
|
|
1146
|
-
KeyvHooks,
|
|
1147
|
-
calculateTtlFromExpiration,
|
|
1148
|
-
createKeyv,
|
|
1149
|
-
getCascadingTtl,
|
|
1150
|
-
getOrSet,
|
|
1151
|
-
hash,
|
|
1152
|
-
shorthandToMilliseconds,
|
|
1153
|
-
shorthandToTime,
|
|
1154
|
-
wrap,
|
|
1155
|
-
wrapSync
|
|
1217
|
+
exports.Cacheable = Cacheable;
|
|
1218
|
+
exports.CacheableEvents = CacheableEvents;
|
|
1219
|
+
exports.CacheableHooks = CacheableHooks;
|
|
1220
|
+
Object.defineProperty(exports, "CacheableMemory", {
|
|
1221
|
+
enumerable: true,
|
|
1222
|
+
get: function() {
|
|
1223
|
+
return _cacheable_memory.CacheableMemory;
|
|
1224
|
+
}
|
|
1225
|
+
});
|
|
1226
|
+
Object.defineProperty(exports, "CacheableStats", {
|
|
1227
|
+
enumerable: true,
|
|
1228
|
+
get: function() {
|
|
1229
|
+
return _cacheable_utils.Stats;
|
|
1230
|
+
}
|
|
1231
|
+
});
|
|
1232
|
+
exports.CacheableSync = CacheableSync;
|
|
1233
|
+
exports.CacheableSyncEvents = CacheableSyncEvents;
|
|
1234
|
+
Object.defineProperty(exports, "HashAlgorithm", {
|
|
1235
|
+
enumerable: true,
|
|
1236
|
+
get: function() {
|
|
1237
|
+
return _cacheable_utils.HashAlgorithm;
|
|
1238
|
+
}
|
|
1239
|
+
});
|
|
1240
|
+
Object.defineProperty(exports, "Keyv", {
|
|
1241
|
+
enumerable: true,
|
|
1242
|
+
get: function() {
|
|
1243
|
+
return keyv.Keyv;
|
|
1244
|
+
}
|
|
1245
|
+
});
|
|
1246
|
+
Object.defineProperty(exports, "KeyvCacheableMemory", {
|
|
1247
|
+
enumerable: true,
|
|
1248
|
+
get: function() {
|
|
1249
|
+
return _cacheable_memory.KeyvCacheableMemory;
|
|
1250
|
+
}
|
|
1251
|
+
});
|
|
1252
|
+
Object.defineProperty(exports, "KeyvHooks", {
|
|
1253
|
+
enumerable: true,
|
|
1254
|
+
get: function() {
|
|
1255
|
+
return keyv.KeyvHooks;
|
|
1256
|
+
}
|
|
1257
|
+
});
|
|
1258
|
+
Object.defineProperty(exports, "calculateTtlFromExpiration", {
|
|
1259
|
+
enumerable: true,
|
|
1260
|
+
get: function() {
|
|
1261
|
+
return _cacheable_utils.calculateTtlFromExpiration;
|
|
1262
|
+
}
|
|
1263
|
+
});
|
|
1264
|
+
Object.defineProperty(exports, "createKeyv", {
|
|
1265
|
+
enumerable: true,
|
|
1266
|
+
get: function() {
|
|
1267
|
+
return _cacheable_memory.createKeyv;
|
|
1268
|
+
}
|
|
1269
|
+
});
|
|
1270
|
+
Object.defineProperty(exports, "getCascadingTtl", {
|
|
1271
|
+
enumerable: true,
|
|
1272
|
+
get: function() {
|
|
1273
|
+
return _cacheable_utils.getCascadingTtl;
|
|
1274
|
+
}
|
|
1275
|
+
});
|
|
1276
|
+
Object.defineProperty(exports, "getOrSet", {
|
|
1277
|
+
enumerable: true,
|
|
1278
|
+
get: function() {
|
|
1279
|
+
return _cacheable_utils.getOrSet;
|
|
1280
|
+
}
|
|
1281
|
+
});
|
|
1282
|
+
Object.defineProperty(exports, "hash", {
|
|
1283
|
+
enumerable: true,
|
|
1284
|
+
get: function() {
|
|
1285
|
+
return _cacheable_utils.hash;
|
|
1286
|
+
}
|
|
1287
|
+
});
|
|
1288
|
+
Object.defineProperty(exports, "shorthandToMilliseconds", {
|
|
1289
|
+
enumerable: true,
|
|
1290
|
+
get: function() {
|
|
1291
|
+
return _cacheable_utils.shorthandToMilliseconds;
|
|
1292
|
+
}
|
|
1293
|
+
});
|
|
1294
|
+
Object.defineProperty(exports, "shorthandToTime", {
|
|
1295
|
+
enumerable: true,
|
|
1296
|
+
get: function() {
|
|
1297
|
+
return _cacheable_utils.shorthandToTime;
|
|
1298
|
+
}
|
|
1299
|
+
});
|
|
1300
|
+
Object.defineProperty(exports, "wrap", {
|
|
1301
|
+
enumerable: true,
|
|
1302
|
+
get: function() {
|
|
1303
|
+
return _cacheable_utils.wrap;
|
|
1304
|
+
}
|
|
1305
|
+
});
|
|
1306
|
+
Object.defineProperty(exports, "wrapSync", {
|
|
1307
|
+
enumerable: true,
|
|
1308
|
+
get: function() {
|
|
1309
|
+
return _cacheable_utils.wrapSync;
|
|
1310
|
+
}
|
|
1156
1311
|
});
|
|
1157
|
-
/* v8 ignore next -- @preserve */
|