@socketsecurity/lib 6.0.8 → 6.0.10
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/CHANGELOG.md +28 -0
- package/README.md +8 -9
- package/dist/abort/signal.d.ts +1 -1
- package/dist/abort/signal.js +1 -1
- package/dist/ai/agent-context.d.mts +1 -1
- package/dist/ai/agent-context.js +3 -2
- package/dist/ai/backends.d.mts +3 -3
- package/dist/ai/backends.js +2 -2
- package/dist/ai/billing-context.d.mts +74 -0
- package/dist/ai/billing-context.js +129 -0
- package/dist/ai/credentials.d.mts +39 -5
- package/dist/ai/credentials.js +61 -6
- package/dist/ai/discover.d.mts +4 -0
- package/dist/ai/discover.js +4 -0
- package/dist/ai/exec.d.mts +1 -1
- package/dist/ai/exec.js +1 -1
- package/dist/ai/http.d.mts +8 -8
- package/dist/ai/http.js +6 -6
- package/dist/ai/profiles.d.mts +9 -10
- package/dist/ai/profiles.js +0 -5
- package/dist/ai/role.d.mts +58 -0
- package/dist/ai/role.js +96 -0
- package/dist/ai/route-heuristic.d.mts +177 -0
- package/dist/ai/route-heuristic.js +194 -0
- package/dist/ai/route.d.mts +31 -17
- package/dist/ai/route.js +35 -19
- package/dist/ai/spawn.d.mts +34 -1
- package/dist/ai/spawn.js +115 -3
- package/dist/ai/subagent-status.d.mts +2 -0
- package/dist/ai/subagent-status.js +2 -0
- package/dist/ai/tier.d.mts +1 -1
- package/dist/ai/types.d.mts +19 -2
- package/dist/ai/worktree.d.mts +1 -1
- package/dist/ai/worktree.js +3 -2
- package/dist/argv/parse.d.ts +2 -0
- package/dist/argv/parse.js +2 -0
- package/dist/bin/_internal.d.ts +1 -1
- package/dist/bin/_internal.js +1 -1
- package/dist/bin/prim.cjs +19253 -13937
- package/dist/bin/resolve.js +1 -1
- package/dist/bin/shadow.js +2 -1
- package/dist/bin/which.d.ts +28 -3
- package/dist/bin/which.js +45 -3
- package/dist/cacache/_internal.d.ts +10 -2
- package/dist/cacache/_internal.js +6 -10
- package/dist/cache/ttl/_internal.d.ts +33 -0
- package/dist/cache/ttl/_internal.js +68 -0
- package/dist/cache/ttl/browser.d.ts +56 -0
- package/dist/cache/ttl/browser.js +257 -0
- package/dist/cache/ttl/store.js +11 -34
- package/dist/cache/ttl/types.d.ts +46 -3
- package/dist/checks/primordials-defaults.d.ts +5 -5
- package/dist/checks/primordials-defaults.js +5 -5
- package/dist/checks/primordials.d.ts +1 -1
- package/dist/checks/primordials.js +25 -17
- package/dist/cli/check-primordials.d.ts +6 -1
- package/dist/cli/check-primordials.js +14 -5
- package/dist/cli/check.js +1 -1
- package/dist/cli/socket-lib.d.ts +1 -1
- package/dist/cli/socket-lib.js +3 -3
- package/dist/colors/socket-palette.d.ts +1 -1
- package/dist/compression/brotli.d.ts +6 -2
- package/dist/compression/brotli.js +5 -1
- package/dist/compression/gzip.d.ts +6 -2
- package/dist/compression/gzip.js +7 -2
- package/dist/config/layers.d.ts +53 -0
- package/dist/config/layers.js +83 -0
- package/dist/constants/agents.d.ts +3 -0
- package/dist/constants/agents.js +3 -0
- package/dist/constants/node.d.ts +14 -0
- package/dist/constants/node.js +14 -0
- package/dist/constants/package-default-node-range.js +1 -5
- package/dist/constants/packages.d.ts +5 -0
- package/dist/constants/packages.js +5 -0
- package/dist/constants/platform.d.ts +1 -1
- package/dist/constants/platform.js +1 -1
- package/dist/constants/socket.js +1 -1
- package/dist/constants/typescript.d.ts +5 -0
- package/dist/constants/typescript.js +5 -0
- package/dist/debug/_internal.d.ts +22 -7
- package/dist/debug/_internal.js +33 -15
- package/dist/debug/caller-info.js +1 -1
- package/dist/debug/namespace.js +11 -8
- package/dist/debug/output.d.ts +12 -1
- package/dist/debug/output.js +33 -16
- package/dist/dlx/binary-cache.js +1 -1
- package/dist/dlx/binary-resolution.js +8 -5
- package/dist/dlx/firewall.js +1 -1
- package/dist/dlx/lockfile.js +1 -1
- package/dist/eco/npm/npm/parse-lockfile.js +3 -2
- package/dist/eco/npm/vlt/exec.d.ts +1 -1
- package/dist/eco/npm/vlt/exec.js +1 -1
- package/dist/eco/npm/yarnpkg/yarn/parse-lockfile.js +1 -1
- package/dist/env/agents.d.ts +6 -0
- package/dist/env/agents.js +29 -0
- package/dist/env/flags.d.ts +12 -0
- package/dist/env/flags.js +23 -0
- package/dist/env/github-status.js +2 -2
- package/dist/env/providers.d.ts +6 -0
- package/dist/env/providers.js +23 -0
- package/dist/env/proxy.js +1 -1
- package/dist/env/rewire.d.ts +37 -3
- package/dist/env/rewire.js +60 -29
- package/dist/env/runtimes.d.ts +9 -0
- package/dist/env/runtimes.js +53 -0
- package/dist/env/socket-cli.d.ts +5 -2
- package/dist/env/socket-cli.js +6 -3
- package/dist/env/xdg.d.ts +17 -0
- package/dist/env/xdg.js +21 -1
- package/dist/errors/predicates.d.ts +3 -2
- package/dist/errors/predicates.js +5 -3
- package/dist/events/exit/_internal.d.ts +1 -23
- package/dist/external/@npmcli/package-json.js +3570 -4905
- package/dist/external/@npmcli/promise-spawn.js +2 -2
- package/dist/external/@sinclair/typebox/value.js +5 -4
- package/dist/external/@socketregistry/yocto-spinner.js +3 -3
- package/dist/external/@yarnpkg/extensions.js +1 -1
- package/dist/external/adm-zip.d.ts +82 -0
- package/dist/external/adm-zip.js +2 -2
- package/dist/external/cacache.d.ts +87 -0
- package/dist/external/debug.d.ts +22 -0
- package/dist/external/debug.js +11 -4
- package/dist/external/del.d.ts +1 -0
- package/dist/external/external-pack.d.ts +12 -0
- package/dist/external/external-pack.js +42 -35
- package/dist/external/fast-glob.d.ts +8 -0
- package/dist/external/fast-sort.d.ts +1 -0
- package/dist/external/get-east-asian-width.d.ts +5 -0
- package/dist/external/get-east-asian-width.js +8 -1
- package/dist/external/has-flag.d.ts +1 -0
- package/dist/external/inquirer-pack.d.ts +18 -0
- package/dist/external/libnpmexec.d.ts +35 -0
- package/dist/external/libnpmpack.d.ts +1 -0
- package/dist/external/make-fetch-happen.d.ts +13 -0
- package/dist/external/normalize-package-data.d.ts +4 -0
- package/dist/external/npm-core.d.ts +14 -0
- package/dist/external/npm-pack.d.ts +24 -0
- package/dist/external/npm-pack.js +28934 -31885
- package/dist/external/npm-package-arg.d.ts +4 -0
- package/dist/external/p-map.js +8 -1
- package/dist/external/pacote.d.ts +15 -0
- package/dist/external/pico-pack.d.ts +16 -0
- package/dist/external/pico-pack.js +38 -23
- package/dist/external/picomatch.d.ts +96 -0
- package/dist/external/pony-cause.d.ts +14 -0
- package/dist/external/semver.d.ts +60 -0
- package/dist/external/semver.js +1459 -4
- package/dist/external/shell-quote.d.ts +51 -0
- package/dist/external/shell-quote.js +55 -15
- package/dist/external/signal-exit.d.ts +1 -0
- package/dist/external/spdx-correct.d.ts +4 -0
- package/dist/external/spdx-expression-parse.d.ts +4 -0
- package/dist/external/spdx-pack.d.ts +10 -0
- package/dist/external/std-env.d.ts +284 -0
- package/dist/external/std-env.js +323 -0
- package/dist/external/streaming-iterables.d.ts +11 -0
- package/dist/external/supports-color.d.ts +1 -0
- package/dist/external/tar-fs.d.ts +31 -0
- package/dist/external/tar-fs.js +63 -16
- package/dist/external/validate-npm-package-name.d.ts +4 -0
- package/dist/external/which.d.ts +25 -0
- package/dist/external/which.js +2 -2
- package/dist/external/yargs-parser.d.ts +2 -0
- package/dist/external/yargs-parser.js +11 -3
- package/dist/external/yoctocolors-cjs.d.ts +51 -0
- package/dist/external-tools/bazel/asset-names.d.ts +1 -1
- package/dist/external-tools/bazel/from-download.d.ts +1 -1
- package/dist/external-tools/bazel/resolve.d.ts +3 -0
- package/dist/external-tools/bazel/resolve.js +3 -0
- package/dist/external-tools/cdxgen/asset-names.d.ts +5 -5
- package/dist/external-tools/cdxgen/asset-names.js +3 -3
- package/dist/external-tools/cdxgen/from-download.d.ts +2 -2
- package/dist/external-tools/cdxgen/from-download.js +1 -1
- package/dist/external-tools/cdxgen/resolve.d.ts +4 -1
- package/dist/external-tools/cdxgen/resolve.js +4 -1
- package/dist/external-tools/cdxgen/types.d.ts +1 -1
- package/dist/external-tools/from-download.d.ts +2 -2
- package/dist/external-tools/from-download.js +1 -1
- package/dist/external-tools/janus/asset-names.d.ts +4 -1
- package/dist/external-tools/janus/asset-names.js +3 -0
- package/dist/external-tools/janus/from-download.d.ts +3 -3
- package/dist/external-tools/janus/from-download.js +2 -2
- package/dist/external-tools/janus/resolve.d.ts +3 -0
- package/dist/external-tools/janus/resolve.js +3 -0
- package/dist/external-tools/jre/asset-names.d.ts +3 -1
- package/dist/external-tools/jre/asset-names.js +2 -0
- package/dist/external-tools/jre/detect-platform-arch.js +1 -1
- package/dist/external-tools/jre/from-download.d.ts +1 -1
- package/dist/external-tools/manifest.d.ts +5 -3
- package/dist/external-tools/manifest.js +5 -3
- package/dist/external-tools/opengrep/asset-names.d.ts +1 -1
- package/dist/external-tools/opengrep/resolve.d.ts +3 -0
- package/dist/external-tools/opengrep/resolve.js +3 -0
- package/dist/external-tools/python/asset-names.d.ts +3 -1
- package/dist/external-tools/python/asset-names.js +8 -4
- package/dist/external-tools/python/pin.js +3 -1
- package/dist/external-tools/python/resolve.d.ts +3 -0
- package/dist/external-tools/python/resolve.js +3 -0
- package/dist/external-tools/python/uv-install.d.ts +89 -0
- package/dist/external-tools/python/uv-install.js +165 -0
- package/dist/external-tools/sbt/resolve.d.ts +3 -0
- package/dist/external-tools/sbt/resolve.js +3 -0
- package/dist/external-tools/skillspector/from-uv.d.ts +30 -0
- package/dist/external-tools/skillspector/from-uv.js +56 -0
- package/dist/external-tools/skillspector/resolve.d.ts +18 -3
- package/dist/external-tools/skillspector/resolve.js +21 -8
- package/dist/external-tools/skillspector/types.d.ts +3 -1
- package/dist/external-tools/synp/resolve.d.ts +3 -0
- package/dist/external-tools/synp/resolve.js +3 -0
- package/dist/external-tools/trivy/asset-names.d.ts +1 -1
- package/dist/external-tools/trivy/resolve.d.ts +3 -0
- package/dist/external-tools/trivy/resolve.js +3 -0
- package/dist/external-tools/trufflehog/asset-names.d.ts +4 -1
- package/dist/external-tools/trufflehog/asset-names.js +3 -0
- package/dist/external-tools/trufflehog/from-download.d.ts +1 -1
- package/dist/external-tools/trufflehog/resolve.d.ts +3 -0
- package/dist/external-tools/trufflehog/resolve.js +3 -0
- package/dist/external-tools/uv/asset-names.d.ts +36 -0
- package/dist/external-tools/uv/asset-names.js +73 -0
- package/dist/external-tools/uv/from-download.d.ts +17 -0
- package/dist/external-tools/uv/from-download.js +50 -0
- package/dist/external-tools/uv/from-path.d.ts +5 -0
- package/dist/external-tools/uv/from-path.js +22 -0
- package/dist/external-tools/uv/from-vfs.d.ts +7 -0
- package/dist/external-tools/uv/from-vfs.js +26 -0
- package/dist/external-tools/uv/resolve.d.ts +28 -0
- package/dist/external-tools/uv/resolve.js +64 -0
- package/dist/external-tools/uv/types.d.ts +24 -0
- package/dist/external-tools/uv/types.js +2 -0
- package/dist/fleet/repo-config.d.ts +55 -0
- package/dist/fleet/repo-config.js +81 -0
- package/dist/fs/_internal.d.ts +1 -1
- package/dist/fs/_internal.js +1 -1
- package/dist/fs/access.d.ts +2 -0
- package/dist/fs/access.js +2 -0
- package/dist/fs/copy.d.ts +88 -0
- package/dist/fs/copy.js +89 -0
- package/dist/fs/find.js +1 -2
- package/dist/fs/inspect.d.ts +2 -2
- package/dist/fs/read-file.js +3 -4
- package/dist/fs/safe.js +2 -2
- package/dist/git/_internal.js +7 -3
- package/dist/github/commit.d.ts +92 -0
- package/dist/github/commit.js +113 -0
- package/dist/github/ghsa.js +1 -1
- package/dist/github/refs-graphql.js +1 -1
- package/dist/github/refs.js +10 -15
- package/dist/github/request.js +1 -1
- package/dist/github/token.d.ts +5 -2
- package/dist/github/token.js +6 -4
- package/dist/github/workflow-runs.d.ts +107 -0
- package/dist/github/workflow-runs.js +98 -0
- package/dist/globs/_internal.js +1 -1
- package/dist/http-request/_internal.d.ts +1 -1
- package/dist/http-request/browser.d.ts +6 -0
- package/dist/http-request/browser.js +4 -3
- package/dist/http-request/checksum-file.js +1 -1
- package/dist/http-request/download.js +2 -0
- package/dist/http-request/headers.d.ts +1 -1
- package/dist/http-request/headers.js +2 -2
- package/dist/http-request/request-attempt.js +6 -3
- package/dist/http-request/request-types.d.ts +10 -1
- package/dist/http-request/request.js +2 -2
- package/dist/http-request/user-agent.d.ts +23 -2
- package/dist/http-request/user-agent.js +48 -6
- package/dist/integrity.d.ts +127 -78
- package/dist/integrity.js +170 -84
- package/dist/json/format.js +4 -1
- package/dist/logger/_internal.d.ts +13 -15
- package/dist/logger/_internal.js +21 -21
- package/dist/logger/colors.d.ts +10 -5
- package/dist/logger/colors.js +7 -14
- package/dist/logger/console-methods.js +2 -2
- package/dist/logger/console.d.ts +1 -0
- package/dist/logger/console.js +9 -9
- package/dist/logger/node.js +2 -1
- package/dist/logger/symbols-builder.js +7 -8
- package/dist/logger/symbols.js +8 -8
- package/dist/native-messaging/install.js +1 -1
- package/dist/native-messaging/rate-limit.d.ts +4 -1
- package/dist/native-messaging/rate-limit.js +4 -1
- package/dist/node/events.d.ts +3 -0
- package/dist/node/events.js +3 -0
- package/dist/node/fs.d.ts +1 -1
- package/dist/node/module.d.ts +73 -2
- package/dist/node/module.js +97 -12
- package/dist/node/timers-promises.d.ts +3 -0
- package/dist/node/timers-promises.js +3 -0
- package/dist/node/url.d.ts +3 -0
- package/dist/node/url.js +3 -0
- package/dist/npm/meta-cache.d.ts +183 -0
- package/dist/npm/meta-cache.js +318 -0
- package/dist/npm/meta-slice.d.ts +27 -0
- package/dist/npm/meta-slice.js +68 -0
- package/dist/npm/meta-types.d.ts +282 -0
- package/dist/npm/meta-types.js +2 -0
- package/dist/npm/meta.d.ts +125 -0
- package/dist/npm/meta.js +347 -0
- package/dist/npm/registry.d.ts +212 -0
- package/dist/npm/registry.js +284 -0
- package/dist/objects/getters.d.ts +1 -1
- package/dist/objects/getters.js +1 -1
- package/dist/objects/predicates.js +1 -1
- package/dist/packages/exports.js +2 -2
- package/dist/packages/isolation.js +1 -1
- package/dist/packages/manifest.d.ts +29 -0
- package/dist/packages/manifest.js +44 -9
- package/dist/packages/metadata-extensions.js +2 -4
- package/dist/packages/normalize.js +1 -1
- package/dist/packages/provenance.d.ts +8 -0
- package/dist/packages/provenance.js +10 -2
- package/dist/packages/specs.js +1 -1
- package/dist/packages/tarball.js +1 -2
- package/dist/packages/validation.d.ts +0 -10
- package/dist/packages/validation.js +0 -14
- package/dist/paths/_internal.d.ts +1 -7
- package/dist/paths/_internal.js +9 -15
- package/dist/paths/conversion.js +1 -1
- package/dist/paths/exts.d.ts +30 -0
- package/dist/paths/exts.js +30 -0
- package/dist/paths/filenames.d.ts +6 -0
- package/dist/paths/filenames.js +6 -0
- package/dist/paths/globs.d.ts +6 -0
- package/dist/paths/globs.js +6 -0
- package/dist/paths/normalize.js +1 -1
- package/dist/paths/predicates.js +2 -1
- package/dist/paths/resolve.js +19 -14
- package/dist/paths/socket.d.ts +81 -30
- package/dist/paths/socket.js +102 -28
- package/dist/perf/metrics.js +1 -1
- package/dist/perf/report.js +5 -3
- package/dist/pkg-ext/data.js +0 -2
- package/dist/primordials/globals.d.ts +1 -1
- package/dist/primordials/globals.js +1 -1
- package/dist/primordials/headers.d.ts +13 -1
- package/dist/primordials/headers.js +13 -1
- package/dist/primordials/process.d.ts +1 -1
- package/dist/primordials/process.js +1 -1
- package/dist/primordials/url.d.ts +18 -0
- package/dist/primordials/url.js +18 -0
- package/dist/process/spawn/child.js +3 -5
- package/dist/promises/_internal.d.ts +1 -1
- package/dist/promises/_internal.js +1 -2
- package/dist/promises/options.js +3 -3
- package/dist/regexps/spec.js +1 -1
- package/dist/releases/socket-btm-binary-naming.js +12 -1
- package/dist/schema/validate.js +1 -1
- package/dist/secrets/_internal.d.ts +1 -1
- package/dist/secrets/_internal.js +1 -1
- package/dist/secrets/broker.d.ts +35 -0
- package/dist/secrets/broker.js +72 -0
- package/dist/secrets/find.d.ts +8 -6
- package/dist/secrets/find.js +17 -3
- package/dist/secrets/keychain.d.ts +10 -1
- package/dist/secrets/keychain.js +10 -1
- package/dist/secrets/rc.js +1 -1
- package/dist/secrets/socket-api-token.d.ts +1 -1
- package/dist/secrets/socket-api-token.js +1 -1
- package/dist/secrets/types.d.ts +1 -1
- package/dist/smol/detect.js +1 -1
- package/dist/smol/http.d.ts +2 -0
- package/dist/smol/http.js +3 -1
- package/dist/smol/https.d.ts +2 -0
- package/dist/smol/https.js +3 -1
- package/dist/smol/manifest.js +1 -1
- package/dist/smol/path.js +1 -1
- package/dist/smol/primordial.js +1 -1
- package/dist/smol/purl.js +1 -1
- package/dist/smol/versions.js +1 -1
- package/dist/smol/vfs.js +1 -1
- package/dist/sorts/_internal.d.ts +2 -4
- package/dist/sorts/_internal.js +1 -7
- package/dist/sorts/semver.js +2 -2
- package/dist/spinner/create-spinner-class.js +1 -1
- package/dist/spinner/format.js +2 -4
- package/dist/stdio/footer.js +8 -7
- package/dist/stdio/progress.js +1 -1
- package/dist/stdio/prompts.d.ts +8 -23
- package/dist/stdio/prompts.js +28 -52
- package/dist/strings/format.js +1 -1
- package/dist/strings/predicates.d.ts +2 -2
- package/dist/strings/search.js +2 -2
- package/dist/strings/types.d.ts +4 -0
- package/dist/strings/width.d.ts +1 -0
- package/dist/strings/width.js +17 -8
- package/dist/themes/context.d.ts +18 -3
- package/dist/themes/context.js +28 -14
- package/dist/versions/_internal.d.ts +43 -13
- package/dist/versions/_internal.js +27 -15
- package/dist/versions/compare.d.ts +16 -10
- package/dist/versions/compare.js +36 -16
- package/dist/versions/parse.js +1 -1
- package/dist/versions/range.js +3 -2
- package/dist/words/pluralize.js +4 -1
- package/llms.txt +63 -0
- package/package.json +271 -40
package/dist/bin/resolve.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* Socket Lib - Built with rolldown */
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_constants_platform = require('../constants/platform.js');
|
|
5
4
|
const require_primordials_string = require('../primordials/string.js');
|
|
5
|
+
const require_constants_platform = require('../constants/platform.js');
|
|
6
6
|
const require_paths_normalize = require('../paths/normalize.js');
|
|
7
7
|
const require_node_fs = require('../node/fs.js');
|
|
8
8
|
const require_node_path = require('../node/path.js');
|
package/dist/bin/shadow.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* Socket Lib - Built with rolldown */
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
let _socketsecurity_lib_paths_normalize = require("@socketsecurity/lib/paths/normalize");
|
|
4
5
|
|
|
5
6
|
//#region src/bin/shadow.ts
|
|
6
7
|
/**
|
|
@@ -20,7 +21,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
20
21
|
*/
|
|
21
22
|
function isShadowBinPath(dirPath) {
|
|
22
23
|
if (!dirPath) return false;
|
|
23
|
-
return
|
|
24
|
+
return (0, _socketsecurity_lib_paths_normalize.normalizePath)(dirPath).includes("node_modules/.bin");
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
//#endregion
|
package/dist/bin/which.d.ts
CHANGED
|
@@ -8,9 +8,11 @@
|
|
|
8
8
|
* through `resolveRealBinSync` so the caller gets the underlying script path
|
|
9
9
|
* (e.g., `npm-cli.js`) rather than the wrapper. Default `nothrow: true` so a
|
|
10
10
|
* missing binary returns `undefined` instead of bubbling a `which` package
|
|
11
|
-
* error.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* error. `whichLocalBin` resolves a tool from the project-local
|
|
12
|
+
* `node_modules/.bin` (the inverse of `findRealBin`, which skips it). Caching
|
|
13
|
+
* matches `_internal.binPathCache` and `binPathAllCache`. Both caches
|
|
14
|
+
* validate hits with `existsSync` so a tool reinstall mid-session doesn't
|
|
15
|
+
* return a stale path.
|
|
14
16
|
*/
|
|
15
17
|
import type { WhichOptions } from './types';
|
|
16
18
|
/**
|
|
@@ -47,6 +49,29 @@ import type { WhichOptions } from './types';
|
|
|
47
49
|
* path, the original path, or null if not found.
|
|
48
50
|
*/
|
|
49
51
|
export declare function which(binName: string, options?: WhichOptions): Promise<string | string[] | null>;
|
|
52
|
+
/**
|
|
53
|
+
* Resolve a tool installed in the project-local `node_modules/.bin` to its
|
|
54
|
+
* ABSOLUTE path. This is the inverse of findRealBin: that helper SKIPS
|
|
55
|
+
* node_modules/.bin (shadow bins) to find the real package manager behind a
|
|
56
|
+
* tool's shim, whereas whichLocalBin WANTS the local bin — it is for spawning a
|
|
57
|
+
* dev dependency's own CLI (oxlint, vitest, tsc) directly instead of through
|
|
58
|
+
* `pnpm exec`. Returns the platform-correct absolute path (the .cmd / .exe shim
|
|
59
|
+
* on Windows, the symlink on POSIX), falling back to a plain PATH lookup, or
|
|
60
|
+
* undefined when the tool resolves nowhere. `options.cwd` overrides the project
|
|
61
|
+
* root whose node_modules/.bin is searched (default process.cwd()); an explicit
|
|
62
|
+
* `options.path` replaces that local bin dir entirely.
|
|
63
|
+
*
|
|
64
|
+
* In npm's terminology this is "local" (vs "global" with `-g`): a locally
|
|
65
|
+
* installed package's executables are linked into node_modules/.bin. See
|
|
66
|
+
* https://docs.npmjs.com/cli/v11/configuring-npm/folders#executables.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ;```typescript
|
|
70
|
+
* whichLocalBin('oxlint') // '/repo/node_modules/.bin/oxlint'
|
|
71
|
+
* whichLocalBin('missing') // undefined
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function whichLocalBin(binName: string, options?: WhichOptions): string | undefined;
|
|
50
75
|
/**
|
|
51
76
|
* Find a binary in the system PATH and resolve to the real underlying script
|
|
52
77
|
* asynchronously. Resolves wrapper scripts (.cmd, .ps1, shell scripts) to the
|
package/dist/bin/which.js
CHANGED
|
@@ -5,8 +5,11 @@ const require_runtime = require('../_virtual/_rolldown/runtime.js');
|
|
|
5
5
|
const require_paths_predicates = require('../paths/predicates.js');
|
|
6
6
|
const require_primordials_array = require('../primordials/array.js');
|
|
7
7
|
const require_node_fs = require('../node/fs.js');
|
|
8
|
+
const require_node_path = require('../node/path.js');
|
|
8
9
|
const require_bin__internal = require('./_internal.js');
|
|
9
10
|
const require_bin_resolve = require('./resolve.js');
|
|
11
|
+
let node_process = require("node:process");
|
|
12
|
+
node_process = require_runtime.__toESM(node_process);
|
|
10
13
|
let src_external_which = require("../external/which");
|
|
11
14
|
src_external_which = require_runtime.__toESM(src_external_which);
|
|
12
15
|
|
|
@@ -21,9 +24,11 @@ src_external_which = require_runtime.__toESM(src_external_which);
|
|
|
21
24
|
* through `resolveRealBinSync` so the caller gets the underlying script path
|
|
22
25
|
* (e.g., `npm-cli.js`) rather than the wrapper. Default `nothrow: true` so a
|
|
23
26
|
* missing binary returns `undefined` instead of bubbling a `which` package
|
|
24
|
-
* error.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
+
* error. `whichLocalBin` resolves a tool from the project-local
|
|
28
|
+
* `node_modules/.bin` (the inverse of `findRealBin`, which skips it). Caching
|
|
29
|
+
* matches `_internal.binPathCache` and `binPathAllCache`. Both caches
|
|
30
|
+
* validate hits with `existsSync` so a tool reinstall mid-session doesn't
|
|
31
|
+
* return a stale path.
|
|
27
32
|
*/
|
|
28
33
|
/**
|
|
29
34
|
* Find an executable in the system PATH asynchronously.
|
|
@@ -67,6 +72,42 @@ async function which(binName, options) {
|
|
|
67
72
|
}
|
|
68
73
|
}
|
|
69
74
|
/**
|
|
75
|
+
* Resolve a tool installed in the project-local `node_modules/.bin` to its
|
|
76
|
+
* ABSOLUTE path. This is the inverse of findRealBin: that helper SKIPS
|
|
77
|
+
* node_modules/.bin (shadow bins) to find the real package manager behind a
|
|
78
|
+
* tool's shim, whereas whichLocalBin WANTS the local bin — it is for spawning a
|
|
79
|
+
* dev dependency's own CLI (oxlint, vitest, tsc) directly instead of through
|
|
80
|
+
* `pnpm exec`. Returns the platform-correct absolute path (the .cmd / .exe shim
|
|
81
|
+
* on Windows, the symlink on POSIX), falling back to a plain PATH lookup, or
|
|
82
|
+
* undefined when the tool resolves nowhere. `options.cwd` overrides the project
|
|
83
|
+
* root whose node_modules/.bin is searched (default process.cwd()); an explicit
|
|
84
|
+
* `options.path` replaces that local bin dir entirely.
|
|
85
|
+
*
|
|
86
|
+
* In npm's terminology this is "local" (vs "global" with `-g`): a locally
|
|
87
|
+
* installed package's executables are linked into node_modules/.bin. See
|
|
88
|
+
* https://docs.npmjs.com/cli/v11/configuring-npm/folders#executables.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ;```typescript
|
|
92
|
+
* whichLocalBin('oxlint') // '/repo/node_modules/.bin/oxlint'
|
|
93
|
+
* whichLocalBin('missing') // undefined
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
function whichLocalBin(binName, options) {
|
|
97
|
+
const opts = {
|
|
98
|
+
__proto__: null,
|
|
99
|
+
...options
|
|
100
|
+
};
|
|
101
|
+
const path = require_node_path.getNodePath();
|
|
102
|
+
const local = whichSync(binName, {
|
|
103
|
+
nothrow: true,
|
|
104
|
+
path: opts.path ?? path.join(opts.cwd ?? node_process.default.cwd(), "node_modules", ".bin")
|
|
105
|
+
});
|
|
106
|
+
if (typeof local === "string") return local;
|
|
107
|
+
const found = whichSync(binName, { nothrow: true });
|
|
108
|
+
return typeof found === "string" ? found : void 0;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
70
111
|
* Find a binary in the system PATH and resolve to the real underlying script
|
|
71
112
|
* asynchronously. Resolves wrapper scripts (.cmd, .ps1, shell scripts) to the
|
|
72
113
|
* actual .js files they execute.
|
|
@@ -215,6 +256,7 @@ function whichSync(binName, options) {
|
|
|
215
256
|
|
|
216
257
|
//#endregion
|
|
217
258
|
exports.which = which;
|
|
259
|
+
exports.whichLocalBin = whichLocalBin;
|
|
218
260
|
exports.whichReal = whichReal;
|
|
219
261
|
exports.whichRealSync = whichRealSync;
|
|
220
262
|
exports.whichSync = whichSync;
|
|
@@ -2,9 +2,17 @@
|
|
|
2
2
|
* @file Accessor for the vendored `cacache` module. Centralised so other
|
|
3
3
|
* `cacache/*` leaves stay decoupled from the import path — if the vendoring
|
|
4
4
|
* moves, only this leaf updates.
|
|
5
|
+
* Snapshot safety: the vendored `cacache` resolves through the npm-pack
|
|
6
|
+
* bundle, whose module-eval constructs a live native `[Foreign]` handle.
|
|
7
|
+
* Importing it at module load pins that handle into the heap and aborts
|
|
8
|
+
* `node --build-snapshot`, so the require is deferred to first use and
|
|
9
|
+
* memoized.
|
|
5
10
|
*/
|
|
11
|
+
import type cacache from '../external/cacache';
|
|
6
12
|
/**
|
|
7
|
-
* Get the cacache module for cache operations.
|
|
13
|
+
* Get the cacache module for cache operations. Required lazily on first call so
|
|
14
|
+
* importing a `cacache/*` leaf does not pull in the native-handle-bearing
|
|
15
|
+
* npm-pack bundle.
|
|
8
16
|
*
|
|
9
17
|
* @example
|
|
10
18
|
* ;```typescript
|
|
@@ -12,4 +20,4 @@
|
|
|
12
20
|
* const entries = await cacache.ls(cacheDir)
|
|
13
21
|
* ```
|
|
14
22
|
*/
|
|
15
|
-
export declare function getCacache():
|
|
23
|
+
export declare function getCacache(): typeof cacache;
|
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* Socket Lib - Built with rolldown */
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_runtime = require('../_virtual/_rolldown/runtime.js');
|
|
5
|
-
let src_external_cacache = require("../external/cacache");
|
|
6
|
-
src_external_cacache = require_runtime.__toESM(src_external_cacache);
|
|
7
4
|
|
|
8
5
|
//#region src/cacache/_internal.ts
|
|
6
|
+
let cached;
|
|
9
7
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*/
|
|
14
|
-
/**
|
|
15
|
-
* Get the cacache module for cache operations.
|
|
8
|
+
* Get the cacache module for cache operations. Required lazily on first call so
|
|
9
|
+
* importing a `cacache/*` leaf does not pull in the native-handle-bearing
|
|
10
|
+
* npm-pack bundle.
|
|
16
11
|
*
|
|
17
12
|
* @example
|
|
18
13
|
* ;```typescript
|
|
@@ -21,7 +16,8 @@ src_external_cacache = require_runtime.__toESM(src_external_cacache);
|
|
|
21
16
|
* ```
|
|
22
17
|
*/
|
|
23
18
|
function getCacache() {
|
|
24
|
-
|
|
19
|
+
if (cached === void 0) cached = require("../external/cacache");
|
|
20
|
+
return cached;
|
|
25
21
|
}
|
|
26
22
|
|
|
27
23
|
//#endregion
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Private internals shared by the `cache/ttl/*` stores — the TTL /
|
|
3
|
+
* prefix / memo-cap defaults, the clock-skew-aware expiry predicate, the
|
|
4
|
+
* wildcard key matcher, and the LRU insertion-order setter. One owner so the
|
|
5
|
+
* node store (`./store`, cacache-backed) and the browser store
|
|
6
|
+
* (`./browser`, adapter-backed) cannot drift on expiry or matching
|
|
7
|
+
* semantics. Pure helpers over primordials only — no `node:*`, no
|
|
8
|
+
* `process`, so both stores stay importable from browser bundles.
|
|
9
|
+
*/
|
|
10
|
+
import type { TtlCacheEntry } from './types';
|
|
11
|
+
export declare const DEFAULT_MEMO_MAX_SIZE = 1000;
|
|
12
|
+
export declare const DEFAULT_PREFIX = "ttl-cache";
|
|
13
|
+
export declare const DEFAULT_TTL_MS: number;
|
|
14
|
+
/**
|
|
15
|
+
* Create a matcher for a key pattern (with wildcard support) against FULL
|
|
16
|
+
* (prefixed) cache keys. Without a wildcard the pattern is a plain prefix
|
|
17
|
+
* match; with wildcards the pattern is anchored both ends so `foo*bar`
|
|
18
|
+
* matches exactly `foo<anything>bar`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createKeyMatcher(prefix: string, pattern: string): (fullKey: string) => boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Check if an entry is expired for the given ttl. Also detects clock skew by
|
|
23
|
+
* treating a suspiciously far-future `expiresAt` (more than 10 seconds past
|
|
24
|
+
* the expected `now + ttl` horizon) as expired.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isExpiredEntry(entry: TtlCacheEntry<unknown>, ttl: number): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Set an entry in a memo Map capped at `maxSize`, using the Map's
|
|
29
|
+
* insertion-order semantics as the LRU list: an existing key is deleted first
|
|
30
|
+
* so the re-insert moves it to the tail, and when the cap is hit the oldest
|
|
31
|
+
* entry (first key in iteration order) is evicted.
|
|
32
|
+
*/
|
|
33
|
+
export declare function lruSet(map: Map<string, TtlCacheEntry<unknown>>, maxSize: number, key: string, entry: TtlCacheEntry<unknown>): void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Socket Lib - Built with rolldown */
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_primordials_string = require('../../primordials/string.js');
|
|
5
|
+
const require_primordials_regexp = require('../../primordials/regexp.js');
|
|
6
|
+
const require_primordials_date = require('../../primordials/date.js');
|
|
7
|
+
|
|
8
|
+
//#region src/cache/ttl/_internal.ts
|
|
9
|
+
/**
|
|
10
|
+
* @file Private internals shared by the `cache/ttl/*` stores — the TTL /
|
|
11
|
+
* prefix / memo-cap defaults, the clock-skew-aware expiry predicate, the
|
|
12
|
+
* wildcard key matcher, and the LRU insertion-order setter. One owner so the
|
|
13
|
+
* node store (`./store`, cacache-backed) and the browser store
|
|
14
|
+
* (`./browser`, adapter-backed) cannot drift on expiry or matching
|
|
15
|
+
* semantics. Pure helpers over primordials only — no `node:*`, no
|
|
16
|
+
* `process`, so both stores stay importable from browser bundles.
|
|
17
|
+
*/
|
|
18
|
+
const DEFAULT_MEMO_MAX_SIZE = 1e3;
|
|
19
|
+
const DEFAULT_PREFIX = "ttl-cache";
|
|
20
|
+
const DEFAULT_TTL_MS = 300 * 1e3;
|
|
21
|
+
const MAX_FUTURE_SKEW_MS = 1e4;
|
|
22
|
+
/**
|
|
23
|
+
* Create a matcher for a key pattern (with wildcard support) against FULL
|
|
24
|
+
* (prefixed) cache keys. Without a wildcard the pattern is a plain prefix
|
|
25
|
+
* match; with wildcards the pattern is anchored both ends so `foo*bar`
|
|
26
|
+
* matches exactly `foo<anything>bar`.
|
|
27
|
+
*/
|
|
28
|
+
function createKeyMatcher(prefix, pattern) {
|
|
29
|
+
const fullPattern = `${prefix}:${pattern}`;
|
|
30
|
+
if (!pattern.includes("*")) return (fullKey) => require_primordials_string.StringPrototypeStartsWith(fullKey, fullPattern);
|
|
31
|
+
const regex = new require_primordials_regexp.RegExpCtor(`^${require_primordials_string.StringPrototypeReplaceAll(require_primordials_string.StringPrototypeReplaceAll(fullPattern, /[.+?^${}()|[\]\\]/g, "\\$&"), "*", ".*")}$`);
|
|
32
|
+
return (fullKey) => require_primordials_regexp.RegExpPrototypeTest(regex, fullKey);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Check if an entry is expired for the given ttl. Also detects clock skew by
|
|
36
|
+
* treating a suspiciously far-future `expiresAt` (more than 10 seconds past
|
|
37
|
+
* the expected `now + ttl` horizon) as expired.
|
|
38
|
+
*/
|
|
39
|
+
function isExpiredEntry(entry, ttl) {
|
|
40
|
+
const now = require_primordials_date.DateNow();
|
|
41
|
+
if (entry.expiresAt > now + ttl + MAX_FUTURE_SKEW_MS) return true;
|
|
42
|
+
return now > entry.expiresAt;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Set an entry in a memo Map capped at `maxSize`, using the Map's
|
|
46
|
+
* insertion-order semantics as the LRU list: an existing key is deleted first
|
|
47
|
+
* so the re-insert moves it to the tail, and when the cap is hit the oldest
|
|
48
|
+
* entry (first key in iteration order) is evicted.
|
|
49
|
+
*/
|
|
50
|
+
function lruSet(map, maxSize, key, entry) {
|
|
51
|
+
if (map.has(key)) map.delete(key);
|
|
52
|
+
else if (map.size >= maxSize) {
|
|
53
|
+
const oldest = map.keys().next().value;
|
|
54
|
+
/* c8 ignore start - defensive unreachable branch */
|
|
55
|
+
if (oldest === void 0) return;
|
|
56
|
+
/* c8 ignore stop */
|
|
57
|
+
map.delete(oldest);
|
|
58
|
+
}
|
|
59
|
+
map.set(key, entry);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
exports.DEFAULT_MEMO_MAX_SIZE = DEFAULT_MEMO_MAX_SIZE;
|
|
64
|
+
exports.DEFAULT_PREFIX = DEFAULT_PREFIX;
|
|
65
|
+
exports.DEFAULT_TTL_MS = DEFAULT_TTL_MS;
|
|
66
|
+
exports.createKeyMatcher = createKeyMatcher;
|
|
67
|
+
exports.isExpiredEntry = isExpiredEntry;
|
|
68
|
+
exports.lruSet = lruSet;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file `createBrowserTtlCache` — browser-safe sibling of `createTtlCache`
|
|
3
|
+
* (`./store`). Same two-tier design and `TtlCache` contract, but the
|
|
4
|
+
* persistent tier is an injected `TtlCacheStorage` adapter (wrap
|
|
5
|
+
* `chrome.storage.local`, `sessionStorage` / `localStorage`, IndexedDB, …)
|
|
6
|
+
* instead of cacache, so the module's import graph stays primordials-only —
|
|
7
|
+
* safe for Chrome MV3 service workers, content scripts, popups, and web
|
|
8
|
+
* workers. Shared semantics live in `./_internal` (one owner, so the node
|
|
9
|
+
* and browser stores cannot drift): TTL default, clock-skew detection, LRU
|
|
10
|
+
* memo eviction via Map insertion order, prefix namespacing, wildcard
|
|
11
|
+
* matching. `getOrFetch` deduplicates concurrent fetches for the same key
|
|
12
|
+
* (thundering-herd protection), and single-key methods throw on `*`.
|
|
13
|
+
* Differences from `./store` (cacache-specific pieces that don't map):
|
|
14
|
+
*
|
|
15
|
+
* - No cacache `ls.stream` — `getAll` / `deleteAll` / `clear` enumerate the
|
|
16
|
+
* persistent tier only when the adapter provides the optional `keys()`
|
|
17
|
+
* (e.g. `chrome.storage.local.get(null)`, `sessionStorage.key(i)`). Without
|
|
18
|
+
* it they still cover the memo tier, and delete those memo keys from
|
|
19
|
+
* storage too; storage-only entries from previous sessions are then
|
|
20
|
+
* unreachable by wildcard but still expire per-entry on read.
|
|
21
|
+
* - No storage supplied ⇒ memo-only cache (all operations stay correct).
|
|
22
|
+
* - A corrupt or shape-invalid storage entry is treated as a miss and deleted
|
|
23
|
+
* best-effort; storage failures never throw — the memo tier is the source
|
|
24
|
+
* of truth, mirroring store.ts's cacache error handling.
|
|
25
|
+
*/
|
|
26
|
+
import type { BrowserTtlCacheOptions, TtlCache } from './types';
|
|
27
|
+
/**
|
|
28
|
+
* Create a browser-safe TTL cache instance. Same contract as
|
|
29
|
+
* `createTtlCache`, with the persistent tier backed by the injected
|
|
30
|
+
* `storage` adapter (or absent, for a memo-only cache).
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ;```typescript
|
|
34
|
+
* const cache = createBrowserTtlCache({
|
|
35
|
+
* prefix: 'my-extension',
|
|
36
|
+
* storage: {
|
|
37
|
+
* async getItem(key) {
|
|
38
|
+
* return (await chrome.storage.local.get(key))[key]
|
|
39
|
+
* },
|
|
40
|
+
* async keys() {
|
|
41
|
+
* return Object.keys(await chrome.storage.local.get(null))
|
|
42
|
+
* },
|
|
43
|
+
* async removeItem(key) {
|
|
44
|
+
* await chrome.storage.local.remove(key)
|
|
45
|
+
* },
|
|
46
|
+
* async setItem(key, value) {
|
|
47
|
+
* await chrome.storage.local.set({ [key]: value })
|
|
48
|
+
* },
|
|
49
|
+
* },
|
|
50
|
+
* ttl: 60_000,
|
|
51
|
+
* })
|
|
52
|
+
* await cache.set('key', { value: 42 })
|
|
53
|
+
* const data = await cache.get('key') // { value: 42 }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function createBrowserTtlCache(options?: BrowserTtlCacheOptions | undefined): TtlCache;
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Socket Lib - Built with rolldown */
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_primordials_error = require('../../primordials/error.js');
|
|
5
|
+
const require_primordials_string = require('../../primordials/string.js');
|
|
6
|
+
const require_primordials_math = require('../../primordials/math.js');
|
|
7
|
+
const require_primordials_map_set = require('../../primordials/map-set.js');
|
|
8
|
+
const require_primordials_date = require('../../primordials/date.js');
|
|
9
|
+
const require_primordials_json = require('../../primordials/json.js');
|
|
10
|
+
const require_cache_ttl__internal = require('./_internal.js');
|
|
11
|
+
|
|
12
|
+
//#region src/cache/ttl/browser.ts
|
|
13
|
+
/**
|
|
14
|
+
* @file `createBrowserTtlCache` — browser-safe sibling of `createTtlCache`
|
|
15
|
+
* (`./store`). Same two-tier design and `TtlCache` contract, but the
|
|
16
|
+
* persistent tier is an injected `TtlCacheStorage` adapter (wrap
|
|
17
|
+
* `chrome.storage.local`, `sessionStorage` / `localStorage`, IndexedDB, …)
|
|
18
|
+
* instead of cacache, so the module's import graph stays primordials-only —
|
|
19
|
+
* safe for Chrome MV3 service workers, content scripts, popups, and web
|
|
20
|
+
* workers. Shared semantics live in `./_internal` (one owner, so the node
|
|
21
|
+
* and browser stores cannot drift): TTL default, clock-skew detection, LRU
|
|
22
|
+
* memo eviction via Map insertion order, prefix namespacing, wildcard
|
|
23
|
+
* matching. `getOrFetch` deduplicates concurrent fetches for the same key
|
|
24
|
+
* (thundering-herd protection), and single-key methods throw on `*`.
|
|
25
|
+
* Differences from `./store` (cacache-specific pieces that don't map):
|
|
26
|
+
*
|
|
27
|
+
* - No cacache `ls.stream` — `getAll` / `deleteAll` / `clear` enumerate the
|
|
28
|
+
* persistent tier only when the adapter provides the optional `keys()`
|
|
29
|
+
* (e.g. `chrome.storage.local.get(null)`, `sessionStorage.key(i)`). Without
|
|
30
|
+
* it they still cover the memo tier, and delete those memo keys from
|
|
31
|
+
* storage too; storage-only entries from previous sessions are then
|
|
32
|
+
* unreachable by wildcard but still expire per-entry on read.
|
|
33
|
+
* - No storage supplied ⇒ memo-only cache (all operations stay correct).
|
|
34
|
+
* - A corrupt or shape-invalid storage entry is treated as a miss and deleted
|
|
35
|
+
* best-effort; storage failures never throw — the memo tier is the source
|
|
36
|
+
* of truth, mirroring store.ts's cacache error handling.
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Create a browser-safe TTL cache instance. Same contract as
|
|
40
|
+
* `createTtlCache`, with the persistent tier backed by the injected
|
|
41
|
+
* `storage` adapter (or absent, for a memo-only cache).
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ;```typescript
|
|
45
|
+
* const cache = createBrowserTtlCache({
|
|
46
|
+
* prefix: 'my-extension',
|
|
47
|
+
* storage: {
|
|
48
|
+
* async getItem(key) {
|
|
49
|
+
* return (await chrome.storage.local.get(key))[key]
|
|
50
|
+
* },
|
|
51
|
+
* async keys() {
|
|
52
|
+
* return Object.keys(await chrome.storage.local.get(null))
|
|
53
|
+
* },
|
|
54
|
+
* async removeItem(key) {
|
|
55
|
+
* await chrome.storage.local.remove(key)
|
|
56
|
+
* },
|
|
57
|
+
* async setItem(key, value) {
|
|
58
|
+
* await chrome.storage.local.set({ [key]: value })
|
|
59
|
+
* },
|
|
60
|
+
* },
|
|
61
|
+
* ttl: 60_000,
|
|
62
|
+
* })
|
|
63
|
+
* await cache.set('key', { value: 42 })
|
|
64
|
+
* const data = await cache.get('key') // { value: 42 }
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
function createBrowserTtlCache(options) {
|
|
68
|
+
const opts = {
|
|
69
|
+
__proto__: null,
|
|
70
|
+
memoize: true,
|
|
71
|
+
memoMaxSize: require_cache_ttl__internal.DEFAULT_MEMO_MAX_SIZE,
|
|
72
|
+
prefix: require_cache_ttl__internal.DEFAULT_PREFIX,
|
|
73
|
+
storage: void 0,
|
|
74
|
+
ttl: require_cache_ttl__internal.DEFAULT_TTL_MS,
|
|
75
|
+
...options
|
|
76
|
+
};
|
|
77
|
+
if (opts.prefix?.includes("*")) throw new require_primordials_error.TypeErrorCtor("Cache prefix cannot contain wildcards (*). Use clear({ prefix: \"pattern*\" }) for wildcard matching.");
|
|
78
|
+
const { storage } = opts;
|
|
79
|
+
const memoCache = new require_primordials_map_set.MapCtor();
|
|
80
|
+
const memoMaxSize = require_primordials_math.MathMax(1, opts.memoMaxSize ?? 1e3);
|
|
81
|
+
const prefix = opts.prefix ?? "ttl-cache";
|
|
82
|
+
const ttl = opts.ttl ?? 3e5;
|
|
83
|
+
const fullPrefix = `${prefix}:`;
|
|
84
|
+
/**
|
|
85
|
+
* Build full cache key with prefix.
|
|
86
|
+
*/
|
|
87
|
+
function buildKey(key) {
|
|
88
|
+
return `${fullPrefix}${key}`;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Best-effort deletion from the storage adapter — failures are swallowed.
|
|
92
|
+
*/
|
|
93
|
+
async function removeQuietly(fullKey) {
|
|
94
|
+
if (!storage) return;
|
|
95
|
+
try {
|
|
96
|
+
await storage.removeItem(fullKey);
|
|
97
|
+
} catch {}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* List the storage adapter's keys via its optional `keys()`. Returns an
|
|
101
|
+
* empty list when the adapter has no enumeration or when it fails.
|
|
102
|
+
*/
|
|
103
|
+
async function listStorageKeys() {
|
|
104
|
+
if (!storage?.keys) return [];
|
|
105
|
+
try {
|
|
106
|
+
return await storage.keys();
|
|
107
|
+
} catch {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Read + parse + validate one entry from the storage adapter. Returns the
|
|
113
|
+
* live entry, or undefined on miss / corrupt / shape-invalid / expired —
|
|
114
|
+
* deleting dead entries best-effort.
|
|
115
|
+
*/
|
|
116
|
+
async function readStorageEntry(fullKey) {
|
|
117
|
+
if (!storage) return;
|
|
118
|
+
let raw;
|
|
119
|
+
try {
|
|
120
|
+
raw = await storage.getItem(fullKey);
|
|
121
|
+
} catch {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (typeof raw !== "string") return;
|
|
125
|
+
let parsed;
|
|
126
|
+
try {
|
|
127
|
+
parsed = require_primordials_json.JSONParse(raw);
|
|
128
|
+
} catch {
|
|
129
|
+
await removeQuietly(fullKey);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (parsed === null || typeof parsed !== "object" || typeof parsed.expiresAt !== "number") {
|
|
133
|
+
await removeQuietly(fullKey);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const entry = parsed;
|
|
137
|
+
if (require_cache_ttl__internal.isExpiredEntry(entry, ttl)) {
|
|
138
|
+
await removeQuietly(fullKey);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
return entry;
|
|
142
|
+
}
|
|
143
|
+
async function get(key) {
|
|
144
|
+
if (require_primordials_string.StringPrototypeIncludes(key, "*")) throw new require_primordials_error.TypeErrorCtor("Cache key cannot contain wildcards (*). Use getAll(pattern) to retrieve multiple entries.");
|
|
145
|
+
const fullKey = buildKey(key);
|
|
146
|
+
if (opts.memoize) {
|
|
147
|
+
const memoEntry = memoCache.get(fullKey);
|
|
148
|
+
if (memoEntry && !require_cache_ttl__internal.isExpiredEntry(memoEntry, ttl)) {
|
|
149
|
+
require_cache_ttl__internal.lruSet(memoCache, memoMaxSize, fullKey, memoEntry);
|
|
150
|
+
return memoEntry.data;
|
|
151
|
+
}
|
|
152
|
+
if (memoEntry) memoCache.delete(fullKey);
|
|
153
|
+
}
|
|
154
|
+
const entry = await readStorageEntry(fullKey);
|
|
155
|
+
if (entry) {
|
|
156
|
+
if (opts.memoize) require_cache_ttl__internal.lruSet(memoCache, memoMaxSize, fullKey, entry);
|
|
157
|
+
return entry.data;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async function getAll(pattern) {
|
|
161
|
+
const results = new require_primordials_map_set.MapCtor();
|
|
162
|
+
const matches = require_cache_ttl__internal.createKeyMatcher(prefix, pattern);
|
|
163
|
+
if (opts.memoize) for (const { 0: fullKey, 1: entry } of memoCache.entries()) {
|
|
164
|
+
if (!matches(fullKey)) continue;
|
|
165
|
+
if (require_cache_ttl__internal.isExpiredEntry(entry, ttl)) {
|
|
166
|
+
memoCache.delete(fullKey);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
results.set(require_primordials_string.StringPrototypeSlice(fullKey, fullPrefix.length), entry.data);
|
|
170
|
+
}
|
|
171
|
+
for (const fullKey of await listStorageKeys()) {
|
|
172
|
+
if (!require_primordials_string.StringPrototypeStartsWith(fullKey, fullPrefix) || !matches(fullKey)) continue;
|
|
173
|
+
const originalKey = require_primordials_string.StringPrototypeSlice(fullKey, fullPrefix.length);
|
|
174
|
+
if (results.has(originalKey)) continue;
|
|
175
|
+
const entry = await readStorageEntry(fullKey);
|
|
176
|
+
if (!entry) continue;
|
|
177
|
+
results.set(originalKey, entry.data);
|
|
178
|
+
if (opts.memoize) require_cache_ttl__internal.lruSet(memoCache, memoMaxSize, fullKey, entry);
|
|
179
|
+
}
|
|
180
|
+
return results;
|
|
181
|
+
}
|
|
182
|
+
async function set(key, data) {
|
|
183
|
+
if (require_primordials_string.StringPrototypeIncludes(key, "*")) throw new require_primordials_error.TypeErrorCtor("Cache key cannot contain wildcards (*). Wildcards are only supported in clear({ prefix: \"pattern*\" }).");
|
|
184
|
+
const fullKey = buildKey(key);
|
|
185
|
+
const entry = {
|
|
186
|
+
data,
|
|
187
|
+
expiresAt: require_primordials_date.DateNow() + ttl
|
|
188
|
+
};
|
|
189
|
+
if (opts.memoize) require_cache_ttl__internal.lruSet(memoCache, memoMaxSize, fullKey, entry);
|
|
190
|
+
if (storage) try {
|
|
191
|
+
await storage.setItem(fullKey, require_primordials_json.JSONStringify(entry));
|
|
192
|
+
} catch {}
|
|
193
|
+
}
|
|
194
|
+
const inflightRequests = new require_primordials_map_set.MapCtor();
|
|
195
|
+
async function getOrFetch(key, fetcher) {
|
|
196
|
+
const fullKey = buildKey(key);
|
|
197
|
+
const preexisting = inflightRequests.get(fullKey);
|
|
198
|
+
if (preexisting) return await preexisting;
|
|
199
|
+
const cached = await get(key);
|
|
200
|
+
if (cached !== void 0) return cached;
|
|
201
|
+
const rechecked = inflightRequests.get(fullKey);
|
|
202
|
+
if (rechecked) return await rechecked;
|
|
203
|
+
const promise = (async () => {
|
|
204
|
+
try {
|
|
205
|
+
const data = await fetcher();
|
|
206
|
+
await set(key, data);
|
|
207
|
+
return data;
|
|
208
|
+
} finally {
|
|
209
|
+
inflightRequests.delete(fullKey);
|
|
210
|
+
}
|
|
211
|
+
})();
|
|
212
|
+
inflightRequests.set(fullKey, promise);
|
|
213
|
+
return await promise;
|
|
214
|
+
}
|
|
215
|
+
async function deleteEntry(key) {
|
|
216
|
+
if (require_primordials_string.StringPrototypeIncludes(key, "*")) throw new require_primordials_error.TypeErrorCtor("Cache key cannot contain wildcards (*). Use deleteAll(pattern) to remove multiple entries.");
|
|
217
|
+
const fullKey = buildKey(key);
|
|
218
|
+
memoCache.delete(fullKey);
|
|
219
|
+
await removeQuietly(fullKey);
|
|
220
|
+
}
|
|
221
|
+
async function deleteAll(pattern) {
|
|
222
|
+
const matches = pattern ? require_cache_ttl__internal.createKeyMatcher(prefix, pattern) : void 0;
|
|
223
|
+
const removedKeys = new require_primordials_map_set.SetCtor();
|
|
224
|
+
for (const fullKey of memoCache.keys()) if (!matches || matches(fullKey)) removedKeys.add(fullKey);
|
|
225
|
+
for (const fullKey of await listStorageKeys()) {
|
|
226
|
+
if (!require_primordials_string.StringPrototypeStartsWith(fullKey, fullPrefix)) continue;
|
|
227
|
+
if (!matches || matches(fullKey)) removedKeys.add(fullKey);
|
|
228
|
+
}
|
|
229
|
+
for (const fullKey of removedKeys) {
|
|
230
|
+
memoCache.delete(fullKey);
|
|
231
|
+
await removeQuietly(fullKey);
|
|
232
|
+
}
|
|
233
|
+
return removedKeys.size;
|
|
234
|
+
}
|
|
235
|
+
async function clear(clearOptions) {
|
|
236
|
+
if ({
|
|
237
|
+
__proto__: null,
|
|
238
|
+
...clearOptions
|
|
239
|
+
}.memoOnly) {
|
|
240
|
+
memoCache.clear();
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
await deleteAll();
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
clear,
|
|
247
|
+
delete: deleteEntry,
|
|
248
|
+
deleteAll,
|
|
249
|
+
get,
|
|
250
|
+
getAll,
|
|
251
|
+
getOrFetch,
|
|
252
|
+
set
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
257
|
+
exports.createBrowserTtlCache = createBrowserTtlCache;
|