UNPKG

simple-on-disk-cache

Version:

A simple on-disk cache, supporting local and remote filesystem targets, with time based expiration policies.

33 lines 1.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.castToSafeOnDiskCacheKey = void 0; const hash_fns_1 = require("hash-fns"); const serde_fns_1 = require("serde-fns"); /** * .what = casts details about the execution of a procedure into a safe on disk cache key * .why = * - eliminates special characters, which are incompatible with on-disk file names * - ensures to namespace to a procedure name, to avoid cross procedures cache-key collisions * - reminds to use a procedure.version, to enable invalidation of prior versions of procedures */ const castToSafeOnDiskCacheKey = (input) => [ // fn name input.procedure.name, // input preview (dynamodbkeys limit = 2k, s3keys limit = 1k, ondisk limit = 255) (0, serde_fns_1.asSerialJSON)(input.execution.input) .replace(/[{}[\]:,]/gi, '_') .replace(/[^0-9a-z_]/gi, '') .replace(/__+/g, '_') .slice(0, 100) // todo: make the length dependent on the rest of the cache key. fill the 255char limit (consider name, account id, and hash) .replace(/^_/, '') .replace(/_$/, ''), // stringify + replace all non-alphanumeric input, // then, suffix with a unique id of the input + prompt (0, hash_fns_1.asHashSha256Sync)((0, serde_fns_1.asSerialJSON)([ input.execution.input, input.procedure.version ? (0, hash_fns_1.asHashSha256Sync)(input.procedure.version) : null, ])), ].join('.'); exports.castToSafeOnDiskCacheKey = castToSafeOnDiskCacheKey; //# sourceMappingURL=castToSafeOnDiskCacheKey.js.map