@nordhavnhq/beacon 0.4.1 → 0.6.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/LICENSE +21 -0
- package/dist/cache-q8uLEGYg.d.cts +165 -0
- package/dist/cache-q8uLEGYg.d.ts +165 -0
- package/dist/file-store.cjs +148 -0
- package/dist/file-store.cjs.map +1 -0
- package/dist/file-store.d.cts +47 -0
- package/dist/file-store.d.ts +47 -0
- package/dist/file-store.js +146 -0
- package/dist/file-store.js.map +1 -0
- package/dist/index.cjs +685 -107
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +301 -130
- package/dist/index.d.ts +301 -130
- package/dist/index.js +685 -107
- package/dist/index.js.map +1 -1
- package/package.json +41 -9
package/dist/index.d.cts
CHANGED
|
@@ -1,121 +1,8 @@
|
|
|
1
|
+
import { b as CacheOptions } from './cache-q8uLEGYg.cjs';
|
|
2
|
+
export { a as CacheEntry, C as CacheStore, c as ContentCache, M as MemoryCacheStore } from './cache-q8uLEGYg.cjs';
|
|
1
3
|
import { C as ChartData } from './chart-CxtDbYAs.cjs';
|
|
2
4
|
export { A as AGGREGATIONS, a as Aggregation, B as BUCKET_MODES, b as BucketMode, c as CHART_AXES, d as CHART_SORTS, e as CHART_TYPES, f as CHART_TYPE_INFO, g as ChartAxis, h as ChartFamily, i as ChartSeriesData, j as ChartSeriesSpec, k as ChartSort, l as ChartSpec, m as ChartType, n as ChartTypeInfo, D as DATE_BUCKET_VALUES, o as DateBucket, P as PALETTE_KEYS, p as PaletteKey, q as chartTypeInfo, r as isBarChart, s as isPartChart } from './chart-CxtDbYAs.cjs';
|
|
3
5
|
|
|
4
|
-
/**
|
|
5
|
-
* Read caching for the content client. GET responses (schema, record lists,
|
|
6
|
-
* single records, queries) are cached by request key and kept fresh by
|
|
7
|
-
* conditional revalidation: each entry stores the studio's `ETag`, which the
|
|
8
|
-
* client sends back as `If-None-Match`. A cheap project-wide stamp gates whether
|
|
9
|
-
* a revalidation is even attempted, so a stable site serves straight from cache.
|
|
10
|
-
*
|
|
11
|
-
* Swap in a custom `CacheStore` to share a cache across client instances or to
|
|
12
|
-
* back it with something persistent.
|
|
13
|
-
*/
|
|
14
|
-
interface CacheEntry {
|
|
15
|
-
value: unknown;
|
|
16
|
-
/**
|
|
17
|
-
* The studio's validator (`ETag`) for this value. Sent back as `If-None-Match`
|
|
18
|
-
* to revalidate: a `304` means the stored value is still current.
|
|
19
|
-
*/
|
|
20
|
-
etag?: string;
|
|
21
|
-
/**
|
|
22
|
-
* The project stamp this entry was last confirmed current against. When it
|
|
23
|
-
* equals the client's latest known stamp, the entry is served without any
|
|
24
|
-
* network call at all.
|
|
25
|
-
*/
|
|
26
|
-
validatedVersion?: string;
|
|
27
|
-
/** Epoch ms when the entry expires. `0` means it never expires. */
|
|
28
|
-
expiresAt: number;
|
|
29
|
-
}
|
|
30
|
-
/** Pluggable backing store. The default is an in-memory, insertion-ordered map. */
|
|
31
|
-
interface CacheStore {
|
|
32
|
-
get(key: string): CacheEntry | undefined;
|
|
33
|
-
set(key: string, entry: CacheEntry): void;
|
|
34
|
-
delete(key: string): void;
|
|
35
|
-
/** Iterate current keys (used for prefix invalidation). */
|
|
36
|
-
keys(): Iterable<string>;
|
|
37
|
-
clear(): void;
|
|
38
|
-
}
|
|
39
|
-
interface CacheOptions {
|
|
40
|
-
/** Master switch. Default `true`. */
|
|
41
|
-
enabled?: boolean;
|
|
42
|
-
/**
|
|
43
|
-
* How long a cached entry survives, in ms. Default `604800000` (7 days).
|
|
44
|
-
*
|
|
45
|
-
* This is NOT how long until content refreshes - revalidation keeps entries
|
|
46
|
-
* current within `revalidate` (seconds) whenever the studio is reachable. The
|
|
47
|
-
* TTL is really the outage window: how long the site keeps serving the
|
|
48
|
-
* last-known content while the studio is unreachable, before an entry is
|
|
49
|
-
* dropped and a read must hit the network again. A generous default so a
|
|
50
|
-
* studio outage degrades to "slightly stale" rather than "site errors".
|
|
51
|
-
*
|
|
52
|
-
* The one case where it bounds staleness directly is when revalidation is off
|
|
53
|
-
* (`revalidate: false`) or the server has no `/version` endpoint: then content
|
|
54
|
-
* can be up to `ttl` old. `0` = never expire.
|
|
55
|
-
*/
|
|
56
|
-
ttl?: number;
|
|
57
|
-
/**
|
|
58
|
-
* Minimum ms between two project-stamp checks. Default `60000` (1 minute) -
|
|
59
|
-
* this is the freshness window for PUBLIC visitors (an edit is picked up on
|
|
60
|
-
* the next check after it lands); an editor viewing a surface preview bypasses
|
|
61
|
-
* the cache entirely and always sees changes instantly.
|
|
62
|
-
*
|
|
63
|
-
* The check fires only on a read and is throttled, so a quiet site barely
|
|
64
|
-
* checks at all; the interval only caps the busy case. Each check costs the
|
|
65
|
-
* studio a small API-key lookup, so raise it (a few minutes) if you run many
|
|
66
|
-
* frontends; lower it for a tighter public window. `false` disables checking
|
|
67
|
-
* and leaves you with pure TTL expiry.
|
|
68
|
-
*/
|
|
69
|
-
revalidate?: number | false;
|
|
70
|
-
/** Soft cap on entries; oldest are evicted first. Default `500`. */
|
|
71
|
-
maxEntries?: number;
|
|
72
|
-
/** Custom backing store. Defaults to an in-memory store. */
|
|
73
|
-
store?: CacheStore;
|
|
74
|
-
}
|
|
75
|
-
/** In-memory store with insertion-order (FIFO) eviction once `maxEntries` is hit. */
|
|
76
|
-
declare class MemoryCacheStore implements CacheStore {
|
|
77
|
-
private maxEntries;
|
|
78
|
-
private map;
|
|
79
|
-
constructor(maxEntries?: number);
|
|
80
|
-
get(key: string): CacheEntry | undefined;
|
|
81
|
-
set(key: string, entry: CacheEntry): void;
|
|
82
|
-
delete(key: string): void;
|
|
83
|
-
keys(): Iterable<string>;
|
|
84
|
-
clear(): void;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Thin cache facade the client talks to. Owns TTL logic, conditional-revalidation
|
|
88
|
-
* bookkeeping (etag + validated stamp), and prefix invalidation, so the client
|
|
89
|
-
* only deals in cache keys.
|
|
90
|
-
*/
|
|
91
|
-
declare class ContentCache {
|
|
92
|
-
readonly enabled: boolean;
|
|
93
|
-
private ttl;
|
|
94
|
-
private store;
|
|
95
|
-
constructor(opts?: CacheOptions);
|
|
96
|
-
/**
|
|
97
|
-
* The live entry for `key`, or `undefined` on miss / TTL expiry. Returns the
|
|
98
|
-
* whole entry (value + etag + validated stamp) so the client can decide
|
|
99
|
-
* whether to serve it, revalidate it, or fall back to it on error.
|
|
100
|
-
*/
|
|
101
|
-
peek(key: string, now: number): CacheEntry | undefined;
|
|
102
|
-
/** Store a freshly fetched value with its validator and the stamp it is current against. */
|
|
103
|
-
set(key: string, value: unknown, opts: {
|
|
104
|
-
etag?: string;
|
|
105
|
-
version?: string;
|
|
106
|
-
}, now: number): void;
|
|
107
|
-
/**
|
|
108
|
-
* Record that an existing entry is still current as of `version` (after a
|
|
109
|
-
* `304`), refreshing its TTL without touching the value. No-op if it is gone.
|
|
110
|
-
*/
|
|
111
|
-
confirm(key: string, version: string | undefined, now: number): void;
|
|
112
|
-
/** Drop one exact key. */
|
|
113
|
-
delete(key: string): void;
|
|
114
|
-
/** Drop every key that starts with `prefix`. */
|
|
115
|
-
deletePrefix(prefix: string): void;
|
|
116
|
-
clear(): void;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
6
|
/**
|
|
120
7
|
* Wire types for the Nordhavn OS content API. These mirror the server DTOs
|
|
121
8
|
* (`server/utils/content/*`, `server/api/content/*`) one-to-one. Slugs are used
|
|
@@ -197,6 +84,22 @@ interface QueryResult<V = unknown> {
|
|
|
197
84
|
shape: QueryShape;
|
|
198
85
|
value: V;
|
|
199
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Response of `POST /files`: one uploaded file, not yet on any record. Hang it
|
|
89
|
+
* on a record by writing `{ uid }` into the file field it was uploaded for;
|
|
90
|
+
* an upload no record takes up within 24 hours is removed.
|
|
91
|
+
*/
|
|
92
|
+
interface UploadedFile {
|
|
93
|
+
uid: string;
|
|
94
|
+
/** Absolute URL of the stored file. */
|
|
95
|
+
url: string;
|
|
96
|
+
/** The file name as uploaded. */
|
|
97
|
+
name: string;
|
|
98
|
+
/** Read from the file's content, not from what the upload declared. */
|
|
99
|
+
mimeType: string;
|
|
100
|
+
/** Size in bytes. */
|
|
101
|
+
size: number;
|
|
102
|
+
}
|
|
200
103
|
|
|
201
104
|
/**
|
|
202
105
|
* Query parameter Nordhavn OS appends when it loads your site in a
|
|
@@ -206,6 +109,18 @@ interface QueryResult<V = unknown> {
|
|
|
206
109
|
declare const PREVIEW_PARAM = "sl-preview";
|
|
207
110
|
/** Hosted Nordhavn OS platform. Used when no `baseUrl` is provided. */
|
|
208
111
|
declare const DEFAULT_BASE_URL = "https://beacon.nordhavn.ai";
|
|
112
|
+
/** A studio failure the client absorbed, handed to `onError`. */
|
|
113
|
+
interface StudioFailure {
|
|
114
|
+
/**
|
|
115
|
+
* What failed: the periodic `stamp-check`, or the `read` that refreshes one
|
|
116
|
+
* cache entry (its cached value was served instead).
|
|
117
|
+
*/
|
|
118
|
+
during: 'stamp-check' | 'read';
|
|
119
|
+
/** Cache key of the entry that could not be refreshed. Only for `read`. */
|
|
120
|
+
key?: string;
|
|
121
|
+
/** What went wrong: a `HavenError`, a `TimeoutError`, a network error. */
|
|
122
|
+
error: unknown;
|
|
123
|
+
}
|
|
209
124
|
interface HavenClientOptions {
|
|
210
125
|
/**
|
|
211
126
|
* Project API key, `slk_...`. Mint one in the project's settings under
|
|
@@ -229,6 +144,43 @@ interface HavenClientOptions {
|
|
|
229
144
|
cache?: boolean | CacheOptions;
|
|
230
145
|
/** Custom `fetch` implementation. Defaults to the global `fetch`. */
|
|
231
146
|
fetch?: typeof fetch;
|
|
147
|
+
/**
|
|
148
|
+
* Give up on a request after this many ms, body included. Default `10000`.
|
|
149
|
+
* `0` disables it, as does `Infinity`.
|
|
150
|
+
*
|
|
151
|
+
* Without one, a studio that accepts the connection but never answers holds
|
|
152
|
+
* every read until the runtime gives up (minutes, in Node). A timed-out read
|
|
153
|
+
* counts as transient, like a `5xx`: the last good value is served. The
|
|
154
|
+
* once-a-minute stamp check uses at most 3 seconds of it, as every read in
|
|
155
|
+
* that moment waits on the check.
|
|
156
|
+
*
|
|
157
|
+
* It covers writes too. A write that times out may still have reached the
|
|
158
|
+
* studio, so do not blindly send it again: you could store it twice.
|
|
159
|
+
*/
|
|
160
|
+
timeout?: number;
|
|
161
|
+
/**
|
|
162
|
+
* A read with nothing cached that gets a `502` or `503` tries once more after
|
|
163
|
+
* this many ms, plus up to as many again at random (so the reads of one page
|
|
164
|
+
* do not all come back at once). Default `200`. `false` turns it off.
|
|
165
|
+
*
|
|
166
|
+
* Often one bad moment, such as a proxy switching to a new studio container;
|
|
167
|
+
* and with nothing cached the error is all the caller would get. Never for a
|
|
168
|
+
* read with a value cached (that value is served), a preview or
|
|
169
|
+
* `cache: false` read, a write, or an answer carrying `Retry-After`.
|
|
170
|
+
*/
|
|
171
|
+
gatewayRetry?: number | false;
|
|
172
|
+
/**
|
|
173
|
+
* Called for every studio failure the client absorbs: a failed stamp check,
|
|
174
|
+
* or a read answered from the cache because its refresh failed. These never
|
|
175
|
+
* reach your code as an error, so this is the only place to notice that the
|
|
176
|
+
* site is running on cached content. Errors that are thrown to the caller
|
|
177
|
+
* (a read with nothing cached, a write) are not repeated here.
|
|
178
|
+
*
|
|
179
|
+
* At most one call per stamp check and one per cache entry per retry pause,
|
|
180
|
+
* so it is safe to log or alert from. Without it the client logs one
|
|
181
|
+
* `console.warn` per outage.
|
|
182
|
+
*/
|
|
183
|
+
onError?: (failure: StudioFailure) => void;
|
|
232
184
|
/** Extra headers merged into every request. */
|
|
233
185
|
headers?: Record<string, string>;
|
|
234
186
|
/**
|
|
@@ -244,10 +196,29 @@ interface ReadOptions {
|
|
|
244
196
|
cache?: boolean;
|
|
245
197
|
/**
|
|
246
198
|
* Locale for this read, overriding the client-level `locale`. Pass an empty
|
|
247
|
-
* string to force the project default even when a client locale is set
|
|
248
|
-
*
|
|
199
|
+
* string to force the project default even when a client locale is set;
|
|
200
|
+
* `undefined` or `null` (a missing header, from `headers.get()`) leaves the
|
|
201
|
+
* client locale in place. Untranslated fields fall back to the default
|
|
202
|
+
* locale. Only record reads use it (`listRecords`, `getRecord`, a dataset's
|
|
203
|
+
* `list` and `get`); `query()`, `schema()`, `nodes()` and `locales()` ignore it.
|
|
249
204
|
*/
|
|
250
|
-
locale?: string;
|
|
205
|
+
locale?: string | null | undefined;
|
|
206
|
+
}
|
|
207
|
+
/** Per-call options for `uploadFile`. */
|
|
208
|
+
interface UploadOptions {
|
|
209
|
+
/**
|
|
210
|
+
* The file name to send, extension included: the studio checks the
|
|
211
|
+
* extension against the field's allowed types. Defaults to the name of a
|
|
212
|
+
* `File`; required for a plain `Blob`.
|
|
213
|
+
*/
|
|
214
|
+
filename?: string;
|
|
215
|
+
/**
|
|
216
|
+
* Give up after this many ms, body included. Defaults to the client's
|
|
217
|
+
* `timeout`, but at least 60 s (an upload may be 10 MB); `0` for none. A
|
|
218
|
+
* client created with `timeout: 0` uploads without one too. A negative
|
|
219
|
+
* number or `NaN` counts as not given.
|
|
220
|
+
*/
|
|
221
|
+
timeout?: number;
|
|
251
222
|
}
|
|
252
223
|
/**
|
|
253
224
|
* Typed client for the Nordhavn OS content API: read and write a project's node
|
|
@@ -264,13 +235,38 @@ declare class HavenClient {
|
|
|
264
235
|
private readonly baseUrl;
|
|
265
236
|
private readonly fetchImpl;
|
|
266
237
|
private readonly headers;
|
|
267
|
-
|
|
268
|
-
private
|
|
269
|
-
|
|
270
|
-
|
|
238
|
+
private readonly cache;
|
|
239
|
+
private readonly versionTracker;
|
|
240
|
+
/**
|
|
241
|
+
* `false` on a preview view (see `forRequest`), so reads bypass the cache by
|
|
242
|
+
* default. The one field such a view has of its own; all the rest is shared.
|
|
243
|
+
*/
|
|
271
244
|
private readDefault;
|
|
272
245
|
/** Client-level default locale for reads (undefined = project default). */
|
|
273
246
|
private readonly locale?;
|
|
247
|
+
/** Request timeout in ms, `0` for none. */
|
|
248
|
+
private readonly timeout;
|
|
249
|
+
/** `cache.waitForFresh` in ms, `Infinity` to wait for the studio. */
|
|
250
|
+
private readonly waitForFresh;
|
|
251
|
+
/** `gatewayRetry` in ms, `undefined` when it is off. */
|
|
252
|
+
private readonly gatewayRetry;
|
|
253
|
+
/**
|
|
254
|
+
* The refresh under way per cache key, so concurrent reads of one key share
|
|
255
|
+
* one request, with the ticket it left on (see `state.ticket`).
|
|
256
|
+
*/
|
|
257
|
+
private readonly inflight;
|
|
258
|
+
/**
|
|
259
|
+
* Per cache key, the ticket of the request whose answer the cache holds.
|
|
260
|
+
* Emptied on every invalidation, capped by `STORED_LIMIT`. See `storable`.
|
|
261
|
+
*/
|
|
262
|
+
private readonly storedBy;
|
|
263
|
+
private readonly onError?;
|
|
264
|
+
/**
|
|
265
|
+
* Everything that changes while the client runs and is not a collection of
|
|
266
|
+
* its own. One object, so that a preview view - which shares this client's
|
|
267
|
+
* fields through its prototype - changes the same values and never a copy.
|
|
268
|
+
*/
|
|
269
|
+
private readonly state;
|
|
274
270
|
constructor(options: HavenClientOptions);
|
|
275
271
|
/**
|
|
276
272
|
* A per-request view of this client that skips the cache when the request
|
|
@@ -305,8 +301,47 @@ declare class HavenClient {
|
|
|
305
301
|
* a locale `code` as the client `locale` option or a per-call `opts.locale`.
|
|
306
302
|
*/
|
|
307
303
|
locales(opts?: ReadOptions): Promise<ContentLocale[]>;
|
|
308
|
-
/**
|
|
304
|
+
/**
|
|
305
|
+
* Resolve the locale for a read: per-call override, else the client default.
|
|
306
|
+
* Only a value overrides: `{ locale: undefined }` (or `null`) is what a site
|
|
307
|
+
* passes when the visitor sent no language, and must leave the client locale standing.
|
|
308
|
+
* `''` is the one way to ask for the project default.
|
|
309
|
+
*/
|
|
309
310
|
private resolveLocale;
|
|
311
|
+
/**
|
|
312
|
+
* The locale a read is requested and cached under: the requested one mapped
|
|
313
|
+
* onto a locale the project is configured with (see `matchLocale`).
|
|
314
|
+
*
|
|
315
|
+
* Sites pass on whatever language the visitor's browser asks for, and the
|
|
316
|
+
* studio answers all of `nl`, `nl-NL` and `nl-BE` with the same content. Keyed
|
|
317
|
+
* by the raw value, each spelling would be its own cache entry and its own
|
|
318
|
+
* request - and a stream of made-up ones would push every real entry out of
|
|
319
|
+
* the cache, the last good values for an outage among them. Mapped first,
|
|
320
|
+
* the cache holds one entry per configured locale, whatever is asked for.
|
|
321
|
+
*
|
|
322
|
+
* Without the list of locales (caching off, an older studio without the
|
|
323
|
+
* route, studio unreachable and never read) the requested value goes to the
|
|
324
|
+
* studio as it is, and the studio resolves it.
|
|
325
|
+
*/
|
|
326
|
+
private readLocale;
|
|
327
|
+
/**
|
|
328
|
+
* The project's locales, or `undefined` when they have never been read and
|
|
329
|
+
* cannot be now. An empty list is an answer like any other: the project has
|
|
330
|
+
* no locales, the studio serves the same content whatever language is asked
|
|
331
|
+
* for, and `matchLocale` then maps every request onto the default.
|
|
332
|
+
*
|
|
333
|
+
* Once the list is known this does not wait for anything: the read maps from
|
|
334
|
+
* the list as it stands, and the list is brought up to date on the side. A
|
|
335
|
+
* read therefore spends its `waitForFresh` on its content alone, and keeps
|
|
336
|
+
* finding that content under the same key while the studio is away. Only
|
|
337
|
+
* the very first localized read waits here, as it has nothing to map from.
|
|
338
|
+
*
|
|
339
|
+
* The exception is `waitForFresh: false`. That setting promises that a read
|
|
340
|
+
* waits for the studio and leaves nothing running when it returns - what a
|
|
341
|
+
* host needs that freezes the process once the response is out. So there
|
|
342
|
+
* the list is brought up to date first, like the content after it.
|
|
343
|
+
*/
|
|
344
|
+
private configuredLocales;
|
|
310
345
|
/** List every record in a dataset (references inflated). Requires read scope. */
|
|
311
346
|
listRecords<T = Record<string, unknown>>(nodeSlug: string, datasetSlug: string, opts?: ReadOptions): Promise<ContentRecord<T>[]>;
|
|
312
347
|
/** Read one record by uid. Requires read scope. Throws 404 if it does not exist. */
|
|
@@ -319,6 +354,43 @@ declare class HavenClient {
|
|
|
319
354
|
* reads of the dataset and of this record.
|
|
320
355
|
*/
|
|
321
356
|
updateRecord<T = Record<string, unknown>>(nodeSlug: string, datasetSlug: string, recordUid: string, data: Record<string, unknown>): Promise<ContentRecord<T>>;
|
|
357
|
+
/**
|
|
358
|
+
* Upload one file for a `file` field, for a form that sends an attachment.
|
|
359
|
+
* Requires write scope on the dataset. Returns the file; hang it on a record
|
|
360
|
+
* in a second call by writing `{ uid }` into that field:
|
|
361
|
+
*
|
|
362
|
+
* ```ts
|
|
363
|
+
* const attachment = form.get('attachment')
|
|
364
|
+
* if (attachment instanceof File && attachment.size > 0) {
|
|
365
|
+
* const file = await studio.uploadFile('contact', 'requests', 'attachment', attachment)
|
|
366
|
+
* await studio.createRecord('contact', 'requests', { name, attachment: { uid: file.uid } })
|
|
367
|
+
* }
|
|
368
|
+
* ```
|
|
369
|
+
*
|
|
370
|
+
* The field decides what it takes: its `mimes` and `max_size` rules, within
|
|
371
|
+
* what the studio accepts from a key at all (pdf, jpg, png, webp; 10 MB). The
|
|
372
|
+
* studio reads the type from the content, so a renamed file is refused. Only
|
|
373
|
+
* top-level fields. A file of the wrong type or size throws `HavenError` 400
|
|
374
|
+
* with a message fit to show; other refusals (400, 403, 404) are setup
|
|
375
|
+
* errors. More than 10 uploads a minute per key throws 429. From a Node
|
|
376
|
+
* `Buffer`, send `new Blob([buffer])` with a `filename`.
|
|
377
|
+
*
|
|
378
|
+
* Server-side only: the key that uploads can write, and must never reach a
|
|
379
|
+
* browser. Not retried, not cached, and it leaves cached reads alone: until a
|
|
380
|
+
* record takes it up, the file belongs to no content. An upload that failed
|
|
381
|
+
* may still have been stored; an unused one is removed after 24 hours.
|
|
382
|
+
*/
|
|
383
|
+
uploadFile(nodeSlug: string, datasetSlug: string, fieldSlug: string, file: Blob, opts?: UploadOptions): Promise<UploadedFile>;
|
|
384
|
+
/**
|
|
385
|
+
* Sends a write and settles the cached reads of its dataset with the outcome.
|
|
386
|
+
*
|
|
387
|
+
* Stored: they are dropped. Refused (an answered 4xx): they are still right
|
|
388
|
+
* and stay. Anything else - a timeout, a dropped connection, a 5xx - leaves
|
|
389
|
+
* open whether the studio carried the write out, so they are marked for a
|
|
390
|
+
* recheck: the next read asks the studio again, and keeps the cached value to
|
|
391
|
+
* fall back on if the studio is in fact down.
|
|
392
|
+
*/
|
|
393
|
+
private write;
|
|
322
394
|
/** Run a saved query (`queries.<slug>`) and return its result. Requires read scope. */
|
|
323
395
|
query<V = unknown>(querySlug: string, opts?: ReadOptions): Promise<QueryResult<V>>;
|
|
324
396
|
/**
|
|
@@ -329,28 +401,125 @@ declare class HavenClient {
|
|
|
329
401
|
/**
|
|
330
402
|
* Clear cached reads. With no argument, clears everything. Pass a scope to
|
|
331
403
|
* clear just part of it: a node slug, or `{ node, dataset }` for one dataset.
|
|
404
|
+
* A scoped clear also drops every saved query result, as any of them may
|
|
405
|
+
* aggregate the content that was cleared.
|
|
332
406
|
*/
|
|
333
407
|
clearCache(scope?: string | {
|
|
334
408
|
node: string;
|
|
335
409
|
dataset?: string;
|
|
336
410
|
}): void;
|
|
337
|
-
/**
|
|
338
|
-
*
|
|
411
|
+
/**
|
|
412
|
+
* Invalidate cached record reads for a dataset (every locale), plus all query
|
|
413
|
+
* results (which may aggregate it). `drop` removes them; `recheck` keeps each
|
|
414
|
+
* value as a fallback but makes the next read ask the studio again.
|
|
415
|
+
*/
|
|
339
416
|
private invalidateDataset;
|
|
417
|
+
/** Drop one key from the cache. */
|
|
418
|
+
private drop;
|
|
419
|
+
/** `drop` for every key that starts with `prefix`. */
|
|
420
|
+
private dropPrefix;
|
|
421
|
+
/** Like `drop`, but the value stays as a fallback (see `ContentCache.expire`). */
|
|
422
|
+
private recheck;
|
|
423
|
+
/** `recheck` for every key that starts with `prefix`. */
|
|
424
|
+
private recheckPrefix;
|
|
425
|
+
/**
|
|
426
|
+
* Called by everything that invalidates cached reads. Every request that is
|
|
427
|
+
* under way at this moment left before it, so its answer may show the
|
|
428
|
+
* content as it was: none of them may store what they come back with
|
|
429
|
+
* (`storable`), and no later read may join one of them (hence the cleared
|
|
430
|
+
* `inflight`). They still answer the reads that were already waiting on them.
|
|
431
|
+
*
|
|
432
|
+
* Deliberately not narrowed to the keys that were invalidated. The price is
|
|
433
|
+
* that an unrelated answer now and then is served without being stored, and
|
|
434
|
+
* asked for once more; what it buys is one rule without exceptions.
|
|
435
|
+
*/
|
|
436
|
+
private invalidated;
|
|
437
|
+
/**
|
|
438
|
+
* Whether the answer to the request that left on `ticket` may change what
|
|
439
|
+
* the cache holds for `key`. The single gate in front of every cache write
|
|
440
|
+
* that follows a request - cached read or bypass, a new value, a `304`, a
|
|
441
|
+
* deletion or a retry pause alike. It keeps the cache from going back in time:
|
|
442
|
+
*
|
|
443
|
+
* - not when the cache was invalidated after the request left: the answer
|
|
444
|
+
* may show the content from before (a write, `clearCache`);
|
|
445
|
+
* - not when the cache already holds the answer to a request that left
|
|
446
|
+
* later: that one is newer, whichever of the two came back first.
|
|
447
|
+
*/
|
|
448
|
+
private storable;
|
|
449
|
+
/**
|
|
450
|
+
* Settles the cache with the studio's own definitive "no" for `key` (deleted,
|
|
451
|
+
* forbidden), in answer to the request that left on `ticket`: the entry goes.
|
|
452
|
+
* The one place this happens, for a cached read and a bypass alike.
|
|
453
|
+
*
|
|
454
|
+
* The deletion is noted like any other answer. Without that, a request that
|
|
455
|
+
* left earlier and comes back later would pass the gate and put the deleted
|
|
456
|
+
* content straight back.
|
|
457
|
+
*/
|
|
458
|
+
private gone;
|
|
459
|
+
/**
|
|
460
|
+
* Notes that the cache now reflects, for `key`, the answer to the request
|
|
461
|
+
* that left on `ticket` - a value, a `304`, or the studio's own "this does
|
|
462
|
+
* not exist", after which the cache holds nothing for it. A refresh of that
|
|
463
|
+
* key that left earlier and is still under way is retired: a read arriving
|
|
464
|
+
* now must not join it for an older answer than the cache already reflects.
|
|
465
|
+
*/
|
|
466
|
+
private stored;
|
|
340
467
|
/**
|
|
341
468
|
* The read path, with conditional revalidation.
|
|
342
469
|
*
|
|
343
470
|
* Steady state (project stamp unchanged since we last confirmed this entry):
|
|
344
471
|
* served straight from cache, zero network. After a content change the stamp
|
|
345
472
|
* moves, so the entry is revalidated once with `If-None-Match` - a `304` keeps
|
|
346
|
-
* the cached value, a `200` replaces it - and then it is fast again.
|
|
347
|
-
*
|
|
348
|
-
*
|
|
473
|
+
* the cached value, a `200` replaces it - and then it is fast again.
|
|
474
|
+
*
|
|
475
|
+
* Five rules hold for every read, whatever the studio does. Each is enforced
|
|
476
|
+
* in exactly one place, named here, so that it cannot hold for one kind of
|
|
477
|
+
* read and not for another:
|
|
478
|
+
*
|
|
479
|
+
* 1. A failure that is not the studio's own definitive answer never costs a
|
|
480
|
+
* cached value: it is served instead, and stays cached (`refresh`).
|
|
481
|
+
* 2. Only what is recognisably this endpoint's answer is ever stored
|
|
482
|
+
* (`conditionalGet`).
|
|
483
|
+
* 3. The cache never goes back in time: an answer is not stored when the
|
|
484
|
+
* cache was invalidated, or filled with a newer answer, after its request
|
|
485
|
+
* left (`storable`, in front of every write to the cache).
|
|
486
|
+
* 4. A read that already holds a value waits on the studio for `waitForFresh`
|
|
487
|
+
* at most, stamp check and refresh together (here); the refresh then
|
|
488
|
+
* finishes on the side.
|
|
489
|
+
* 5. One request per key at a time (`refresh`).
|
|
349
490
|
*/
|
|
350
491
|
private cachedGet;
|
|
492
|
+
/**
|
|
493
|
+
* Fetches `cacheKey` from the studio and settles the cache with the answer.
|
|
494
|
+
* One per key at a time: reads that arrive while it runs get the same promise,
|
|
495
|
+
* so a slow studio sees one request for a key and not one per visitor.
|
|
496
|
+
*
|
|
497
|
+
* Resolves with the value to serve: the fresh one, or a cached one when the
|
|
498
|
+
* studio failed transiently. Rejects on a definitive answer, or when there is
|
|
499
|
+
* nothing cached to serve in its place.
|
|
500
|
+
*/
|
|
501
|
+
private refresh;
|
|
502
|
+
/**
|
|
503
|
+
* Hands an absorbed failure to `onError`, or without one warns once per
|
|
504
|
+
* outage. Never throws and never leaves a rejection behind: a broken hook
|
|
505
|
+
* must not turn a served read into an error, and an `async` hook whose own
|
|
506
|
+
* work fails (an alert that cannot be delivered, during the very outage it
|
|
507
|
+
* reports) must not become the unhandled rejection that stops the process.
|
|
508
|
+
*/
|
|
509
|
+
private report;
|
|
351
510
|
/**
|
|
352
511
|
* A GET that understands conditional requests: sends `If-None-Match` when an
|
|
353
512
|
* etag is supplied, and reports a `304` as `notModified` rather than a value.
|
|
513
|
+
*
|
|
514
|
+
* The one place that decides what counts as an answer, for every read:
|
|
515
|
+
*
|
|
516
|
+
* - A `2xx` whose body fails `isExpected` is rejected, not returned. A
|
|
517
|
+
* maintenance page or a captive portal can answer `200` with JSON of its
|
|
518
|
+
* own; storing that would replace the last good value with nothing, at the
|
|
519
|
+
* very moment the studio is away.
|
|
520
|
+
* - A `304` is only an answer to a request that sent an etag. Unasked for,
|
|
521
|
+
* it says "keep what you have" to a caller that has nothing, and there is
|
|
522
|
+
* no value to return or store.
|
|
354
523
|
*/
|
|
355
524
|
private conditionalGet;
|
|
356
525
|
private request;
|
|
@@ -367,6 +536,8 @@ declare class DatasetHandle<T = Record<string, unknown>> {
|
|
|
367
536
|
get(recordUid: string, opts?: ReadOptions): Promise<ContentRecord<T>>;
|
|
368
537
|
create(data: Record<string, unknown>): Promise<ContentRecord<T>>;
|
|
369
538
|
update(recordUid: string, data: Record<string, unknown>): Promise<ContentRecord<T>>;
|
|
539
|
+
/** Upload a file for one of this dataset's file fields; see `HavenClient.uploadFile`. */
|
|
540
|
+
uploadFile(fieldSlug: string, file: Blob, opts?: UploadOptions): Promise<UploadedFile>;
|
|
370
541
|
/** Clear cached reads for this dataset. */
|
|
371
542
|
clearCache(): void;
|
|
372
543
|
}
|
|
@@ -597,5 +768,5 @@ interface ForecastSeries {
|
|
|
597
768
|
/** Convenience factory: `createClient({ apiKey, baseUrl })`. */
|
|
598
769
|
declare function createClient(options: HavenClientOptions): HavenClient;
|
|
599
770
|
|
|
600
|
-
export { CHART_FENCE_LANG, CHART_FENCE_LIMITS,
|
|
601
|
-
export type {
|
|
771
|
+
export { CHART_FENCE_LANG, CHART_FENCE_LIMITS, CacheOptions, ChartData, DEFAULT_BASE_URL, DatasetHandle, HavenClient, HavenError, HavenClient as JustmadeClient, HavenError as JustmadeError, PREVIEW_PARAM, chartFenceHeight, createClient, isChartFence, isPreviewRequest, parseChartFence };
|
|
772
|
+
export type { ChartFence, ChartFenceResult, ContentLocale, ContentRecord, ContentSchema, FieldKind, FieldType, ForecastSeries, HavenClientOptions, HavenClientOptions as JustmadeClientOptions, PreviewInput, QueryResult, QueryShape, ReadOptions, RoadmapData, RoadmapEvent, RoadmapHappening, RoadmapLane, RoadmapMilestone, RoadmapPhase, RoadmapPhaseState, RoadmapRepeat, RoadmapTask, RoadmapTip, RoadmapTone, RoadmapWaiting, RoadmapWorkState, SchemaDataset, SchemaField, SchemaNode, StudioFailure, UploadOptions, UploadedFile };
|