@ultimat3/cache 31.35.0 → 31.36.0
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/package.json +2 -2
- package/src/errors.ts +25 -7
- package/src/purge-env.ts +3 -3
- package/src/purge-http.ts +5 -5
- package/src/semantic.ts +6 -1
- package/src/tiers.ts +18 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/cache",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.36.0",
|
|
4
4
|
"description": "Tagged caching: request memo, LRU, Redis, CDN — one invalidation graph",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -43,6 +43,6 @@
|
|
|
43
43
|
"test": "bun test"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@ultimat3/core": "31.
|
|
46
|
+
"@ultimat3/core": "31.36.0"
|
|
47
47
|
}
|
|
48
48
|
}
|
package/src/errors.ts
CHANGED
|
@@ -96,7 +96,7 @@ export class CacheValueUnencodableError extends UltimateError {
|
|
|
96
96
|
/**
|
|
97
97
|
* A LIMIT this package runs on that is not one — a tier's byte budget, its entry ceiling, the
|
|
98
98
|
* similarity floor the semantic tier decides "same question" with, or a purge driver's request
|
|
99
|
-
* timeout. Sibling of the TTL refusal below, over the
|
|
99
|
+
* timeout. Sibling of the TTL refusal below, over the constructor options a tier carries and
|
|
100
100
|
* `CacheTooLargeError`'s `fix:` line already tells an operator to raise.
|
|
101
101
|
*
|
|
102
102
|
* `Number(process.env.CACHE_MAX_BYTES)` on an unset variable is `NaN`, `NaN` is not nullish so
|
|
@@ -120,24 +120,40 @@ const limitEffect = (value: number): string =>
|
|
|
120
120
|
? ', and every comparison against NaN is false, so the limit stops being enforced rather than being enforced wrongly — NaN is what Number(process.env.…) answers for an unset variable, and it is not nullish, so `??` does not catch it'
|
|
121
121
|
: ', and it is applied as given, so the tier is bounded at a value nobody chose';
|
|
122
122
|
|
|
123
|
+
/**
|
|
124
|
+
* The factory each scope's limits are constructor options of — the fix names where the corrected
|
|
125
|
+
* option goes. None is an `app.config.ts` key: the runtime builds every tier with defaults.
|
|
126
|
+
*/
|
|
127
|
+
const LIMIT_OWNER: Readonly<Record<string, string>> = {
|
|
128
|
+
lru: 'lruTier',
|
|
129
|
+
redis: 'redisTier',
|
|
130
|
+
semantic: 'memorySemanticCache',
|
|
131
|
+
fastly: 'fastlyPurgeDriver',
|
|
132
|
+
cloudflare: 'cloudflarePurgeDriver',
|
|
133
|
+
ladder: 'cacheStack',
|
|
134
|
+
};
|
|
135
|
+
|
|
123
136
|
export class CacheLimitInvalidError extends UltimateError {
|
|
124
137
|
constructor(input: {
|
|
125
138
|
option: string;
|
|
126
139
|
value: number;
|
|
127
140
|
tier: string;
|
|
128
141
|
expected: string;
|
|
142
|
+
/** A value in the `expected` domain, pasted as the corrected option: `10_000`, `0.92`. */
|
|
143
|
+
example: string;
|
|
129
144
|
/**
|
|
130
|
-
* Where the value came FROM, when that is not
|
|
131
|
-
* similarity override
|
|
132
|
-
* caller to edit `app.config.ts` names a key that does not exist.
|
|
145
|
+
* Where the value came FROM, when that is not the options object of `LIMIT_OWNER`'s factory
|
|
146
|
+
* for the tier: the similarity override arrives as a `lookup(embedding, override)` argument.
|
|
133
147
|
*/
|
|
134
148
|
source?: string | undefined;
|
|
135
149
|
}) {
|
|
136
|
-
const where =
|
|
150
|
+
const where =
|
|
151
|
+
input.source ??
|
|
152
|
+
`${LIMIT_OWNER[input.tier] ?? input.tier}(...) — a constructor option, never an app.config.ts key`;
|
|
137
153
|
super({
|
|
138
154
|
code: 'X_CACHE_LIMIT_INVALID',
|
|
139
155
|
cause: `the ${input.tier} tier was given ${input.option}=${String(input.value)}; it must be ${input.expected}${limitEffect(input.value)}`,
|
|
140
|
-
fix: `
|
|
156
|
+
fix: `{ ${input.option}: ${input.example} } // ${input.expected}, in ${where}; parse an environment value first: Number(process.env.CACHE_LIMIT) is NaN when the variable is unset`,
|
|
141
157
|
meta: { option: input.option, value: String(input.value), tier: input.tier },
|
|
142
158
|
});
|
|
143
159
|
}
|
|
@@ -181,7 +197,9 @@ export class CacheJitterInvalidError extends UltimateError {
|
|
|
181
197
|
cause: `the ${input.tier} tier was configured with jitterFraction=${String(
|
|
182
198
|
input.jitterFraction,
|
|
183
199
|
)}; a jitter fraction is a finite number in [0, 1)`,
|
|
184
|
-
|
|
200
|
+
// A constructor option of the tier, never an `app.config.ts` key: the runtime builds both
|
|
201
|
+
// tiers with none (`runtime-cache.ts`), so the config path this used to name did not exist.
|
|
202
|
+
fix: `{ jitterFraction: 0.05 } // in the ${input.tier} tier's lruTier({ ... }) or redisTier({ ... }): a value in [0, 1) — 0.05 is the default, 0 disables jitter`,
|
|
185
203
|
meta: { tier: input.tier, jitterFraction: input.jitterFraction },
|
|
186
204
|
});
|
|
187
205
|
}
|
package/src/purge-env.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// on env, never an `app.config.ts` field the boot's one loader would read: the edge is a credential
|
|
4
4
|
// and a deployment's, and one image is purged against a different edge per environment.
|
|
5
5
|
|
|
6
|
-
import { ConfigInvalidError } from '@ultimat3/core';
|
|
6
|
+
import { ConfigInvalidError, renderFixShellArg } from '@ultimat3/core';
|
|
7
7
|
import type { PurgeDriver } from './cdn';
|
|
8
8
|
import { noopPurgeDriver } from './cdn';
|
|
9
9
|
import { cloudflarePurgeDriver } from './purge-cloudflare';
|
|
@@ -51,7 +51,7 @@ function requirePair(env: PurgeEnvironment, selectedBy: string, missingKey: stri
|
|
|
51
51
|
if (value === undefined) {
|
|
52
52
|
throw new ConfigInvalidError({
|
|
53
53
|
cause: `${selectedBy} selects a CDN purge driver, but ${missingKey} is unset — the pair is incomplete`,
|
|
54
|
-
fix: `
|
|
54
|
+
fix: `printf '%s=%s\\n' ${renderFixShellArg(missingKey, '<the unset key>')} '<its value>' >> .env.production # or unset ${selectedBy} to purge nothing`,
|
|
55
55
|
meta: { selectedBy, missing: missingKey },
|
|
56
56
|
});
|
|
57
57
|
}
|
|
@@ -73,7 +73,7 @@ export function selectPurgeDriver(env: PurgeEnvironment): PurgeSelection {
|
|
|
73
73
|
const configured = configuredKeys(env);
|
|
74
74
|
throw new ConfigInvalidError({
|
|
75
75
|
cause: `two CDNs claim the same purge: ${configured.join(', ')} are set`,
|
|
76
|
-
fix:
|
|
76
|
+
fix: "sed -i.bak -E '/^CLOUDFLARE_(API_TOKEN|ZONE_ID)=/d' .env.production # or '/^FASTLY_(API_TOKEN|SERVICE_ID)=/d' — keep one pair: a process purges exactly one edge",
|
|
77
77
|
meta: { configured },
|
|
78
78
|
});
|
|
79
79
|
}
|
package/src/purge-http.ts
CHANGED
|
@@ -46,7 +46,7 @@ export function chunked<T>(
|
|
|
46
46
|
throw new CacheDriverUnavailableError({
|
|
47
47
|
driver,
|
|
48
48
|
cause: `batch size ${String(size)} is not a positive integer, so ${values.length} keys cannot be split into requests`,
|
|
49
|
-
fix: '
|
|
49
|
+
fix: 'chunked(driver, keys, FASTLY_MAX_KEYS_PER_REQUEST) // a positive integer batch size',
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
52
|
const batches: T[][] = [];
|
|
@@ -66,7 +66,7 @@ export function requireCredential(value: string, envKey: string, driver: string)
|
|
|
66
66
|
throw new CacheDriverUnavailableError({
|
|
67
67
|
driver,
|
|
68
68
|
cause: `${envKey} is unset, so the ${driver} purge driver has no credential`,
|
|
69
|
-
fix: `
|
|
69
|
+
fix: `printf '%s=%s\\n' ${renderFixShellArg(envKey, '<the unset key>')} '<its value>' >> .env.production # or use noopPurgeDriver() to purge nothing`,
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -97,18 +97,18 @@ const keyProblem = (key: string): KeyProblem | undefined => {
|
|
|
97
97
|
if (key === '')
|
|
98
98
|
return {
|
|
99
99
|
problem: 'is empty',
|
|
100
|
-
fix: '
|
|
100
|
+
fix: "declareTags(['post']) // a named tag in its declareTags(...) call — an empty surrogate key purges nothing",
|
|
101
101
|
};
|
|
102
102
|
if (UNSAFE_KEY.test(key))
|
|
103
103
|
return {
|
|
104
104
|
problem: 'contains whitespace or a comma, which a CDN reads as a separator',
|
|
105
|
-
fix: 'rename the tag in its declareTags(...) call
|
|
105
|
+
fix: "declareTags(['blog-post']) // rename the tag in its declareTags(...) call: no space or comma in the key",
|
|
106
106
|
};
|
|
107
107
|
const bytes = keyBytes(key);
|
|
108
108
|
if (bytes > MAX_KEY_BYTES)
|
|
109
109
|
return {
|
|
110
110
|
problem: `is ${bytes} bytes, over the ${MAX_KEY_BYTES}-byte key limit`,
|
|
111
|
-
fix: `shorten the tag in its declareTags(...) call to at most ${MAX_KEY_BYTES} UTF-8 bytes — the limit is on the header, so it is BYTES, and a non-ASCII character costs two or three of them each`,
|
|
111
|
+
fix: `declareTags(['post']) // shorten the tag in its declareTags(...) call to at most ${MAX_KEY_BYTES} UTF-8 bytes — the limit is on the header, so it is BYTES, and a non-ASCII character costs two or three of them each`,
|
|
112
112
|
};
|
|
113
113
|
return undefined;
|
|
114
114
|
};
|
package/src/semantic.ts
CHANGED
|
@@ -122,7 +122,12 @@ export function memorySemanticCache(options: SemanticCacheOptions = {}): Semanti
|
|
|
122
122
|
const floor =
|
|
123
123
|
override === undefined
|
|
124
124
|
? threshold
|
|
125
|
-
: assertFiniteSimilarityFloor(
|
|
125
|
+
: assertFiniteSimilarityFloor(
|
|
126
|
+
'semantic',
|
|
127
|
+
'threshold',
|
|
128
|
+
override,
|
|
129
|
+
'the override argument to lookup(embedding, override)',
|
|
130
|
+
);
|
|
126
131
|
let best: SemanticHit<T> | undefined;
|
|
127
132
|
for (const record of live()) {
|
|
128
133
|
const similarity = cosineSimilarity(embedding, record.embedding);
|
package/src/tiers.ts
CHANGED
|
@@ -115,6 +115,7 @@ export function assertFiniteCapacity(tier: TtlScope, option: string, value: numb
|
|
|
115
115
|
option,
|
|
116
116
|
value,
|
|
117
117
|
expected: 'a whole number greater than zero',
|
|
118
|
+
example: '10_000',
|
|
118
119
|
});
|
|
119
120
|
}
|
|
120
121
|
|
|
@@ -129,20 +130,14 @@ export function assertFiniteCapacity(tier: TtlScope, option: string, value: numb
|
|
|
129
130
|
* reading as unchecked while every one of them was screened. `bun run finite-bounds` now reads
|
|
130
131
|
* `SCREENING_CALLEES` in `scripts/lib/finite-screens.ts` — rename freely, move the rows.
|
|
131
132
|
*/
|
|
132
|
-
export function assertFiniteDurationMs(
|
|
133
|
-
tier: string,
|
|
134
|
-
option: string,
|
|
135
|
-
value: number,
|
|
136
|
-
/** Passed only where the value is a CALL argument rather than an `app.config.ts` key. */
|
|
137
|
-
source?: string | undefined,
|
|
138
|
-
): number {
|
|
133
|
+
export function assertFiniteDurationMs(tier: string, option: string, value: number): number {
|
|
139
134
|
if (Number.isSafeInteger(value) && value > 0) return value;
|
|
140
135
|
throw new CacheLimitInvalidError({
|
|
141
136
|
tier,
|
|
142
137
|
option,
|
|
143
138
|
value,
|
|
144
139
|
expected: 'a whole number of milliseconds greater than zero',
|
|
145
|
-
|
|
140
|
+
example: '60_000',
|
|
146
141
|
});
|
|
147
142
|
}
|
|
148
143
|
|
|
@@ -152,9 +147,22 @@ export function assertFiniteDurationMs(
|
|
|
152
147
|
* for every record. Cosine similarity is in [-1, 1] and a floor of 0 already admits an orthogonal
|
|
153
148
|
* vector, so anything outside [0, 1] is a number nobody can have meant.
|
|
154
149
|
*/
|
|
155
|
-
export function assertFiniteSimilarityFloor(
|
|
150
|
+
export function assertFiniteSimilarityFloor(
|
|
151
|
+
tier: TtlScope,
|
|
152
|
+
option: string,
|
|
153
|
+
value: number,
|
|
154
|
+
/** Passed only where the value is a CALL argument rather than a factory option. */
|
|
155
|
+
source?: string | undefined,
|
|
156
|
+
): number {
|
|
156
157
|
if (Number.isFinite(value) && value >= 0 && value <= 1) return value;
|
|
157
|
-
throw new CacheLimitInvalidError({
|
|
158
|
+
throw new CacheLimitInvalidError({
|
|
159
|
+
tier,
|
|
160
|
+
option,
|
|
161
|
+
value,
|
|
162
|
+
expected: 'a number from 0 to 1',
|
|
163
|
+
example: '0.92',
|
|
164
|
+
source,
|
|
165
|
+
});
|
|
158
166
|
}
|
|
159
167
|
|
|
160
168
|
export function assertTtl(
|
|
@@ -281,9 +289,6 @@ export function cacheStack(
|
|
|
281
289
|
'ladder',
|
|
282
290
|
'loadDeadlineMs',
|
|
283
291
|
options.loadDeadlineMs ?? DEFAULT_LOAD_DEADLINE_MS,
|
|
284
|
-
// Caller-owned: it arrives as a `cacheStack({ loadDeadlineMs })` argument, so there is
|
|
285
|
-
// no `app.config.ts` key to send the reader to.
|
|
286
|
-
'the loadDeadlineMs argument to cacheStack(...)',
|
|
287
292
|
),
|
|
288
293
|
schedule: options.schedule,
|
|
289
294
|
// A closed tab's aborted load must not fail every healthy reader that joined it: this is the
|