@decocms/blocks 8.5.2 → 8.7.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/dist/async/default.d.ts +10 -0
- package/dist/async/default.d.ts.map +1 -0
- package/dist/async/default.js +5 -0
- package/dist/async/default.js.map +1 -0
- package/dist/async/node.d.ts +3 -0
- package/dist/async/node.d.ts.map +1 -0
- package/dist/async/node.js +11 -0
- package/dist/async/node.js.map +1 -0
- package/dist/async/types.d.ts +6 -0
- package/dist/async/types.d.ts.map +1 -0
- package/dist/async/types.js +2 -0
- package/dist/async/types.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/v8/bucketing.d.ts +24 -0
- package/dist/v8/bucketing.d.ts.map +1 -0
- package/dist/v8/bucketing.js +78 -0
- package/dist/v8/bucketing.js.map +1 -0
- package/dist/v8/builtins/campaigns.d.ts +36 -0
- package/dist/v8/builtins/campaigns.d.ts.map +1 -0
- package/dist/v8/builtins/campaigns.js +107 -0
- package/dist/v8/builtins/campaigns.js.map +1 -0
- package/dist/v8/builtins/data.d.ts +2 -2
- package/dist/v8/builtins/data.js +2 -2
- package/dist/v8/builtins/experiments.d.ts +27 -0
- package/dist/v8/builtins/experiments.d.ts.map +1 -0
- package/dist/v8/builtins/experiments.js +62 -0
- package/dist/v8/builtins/experiments.js.map +1 -0
- package/dist/v8/builtins/index.d.ts.map +1 -1
- package/dist/v8/builtins/index.js +7 -2
- package/dist/v8/builtins/index.js.map +1 -1
- package/dist/v8/builtins/matchers.d.ts +0 -14
- package/dist/v8/builtins/matchers.d.ts.map +1 -1
- package/dist/v8/builtins/matchers.js +0 -19
- package/dist/v8/builtins/matchers.js.map +1 -1
- package/dist/v8/cli/builtins.d.ts +6 -0
- package/dist/v8/cli/builtins.d.ts.map +1 -1
- package/dist/v8/cli/builtins.js +19 -0
- package/dist/v8/cli/builtins.js.map +1 -1
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +231 -8
- package/dist/v8/cli/check/index.js.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.js +76 -21
- package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
- package/dist/v8/cli/schema/generate.d.ts.map +1 -1
- package/dist/v8/cli/schema/generate.js +48 -2
- package/dist/v8/cli/schema/generate.js.map +1 -1
- package/dist/v8/client.d.ts +25 -0
- package/dist/v8/client.d.ts.map +1 -1
- package/dist/v8/client.js +58 -10
- package/dist/v8/client.js.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +113 -44
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +80 -1
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +149 -4
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/current.d.ts +28 -0
- package/dist/v8/current.d.ts.map +1 -0
- package/dist/v8/current.js +77 -0
- package/dist/v8/current.js.map +1 -0
- package/dist/v8/draft.d.ts +40 -21
- package/dist/v8/draft.d.ts.map +1 -1
- package/dist/v8/draft.js +160 -25
- package/dist/v8/draft.js.map +1 -1
- package/dist/v8/experiments.d.ts +107 -0
- package/dist/v8/experiments.d.ts.map +1 -0
- package/dist/v8/experiments.js +292 -0
- package/dist/v8/experiments.js.map +1 -0
- package/dist/v8/hosts.d.ts +3 -2
- package/dist/v8/hosts.d.ts.map +1 -1
- package/dist/v8/hosts.js +3 -2
- package/dist/v8/hosts.js.map +1 -1
- package/dist/v8/index.d.ts +7 -5
- package/dist/v8/index.d.ts.map +1 -1
- package/dist/v8/index.js +6 -4
- package/dist/v8/index.js.map +1 -1
- package/dist/v8/preview/DraftBadge.d.ts +9 -1
- package/dist/v8/preview/DraftBadge.d.ts.map +1 -1
- package/dist/v8/preview/DraftBadge.js +42 -2
- package/dist/v8/preview/DraftBadge.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +169 -21
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/resolver.d.ts +4 -2
- package/dist/v8/resolver.d.ts.map +1 -1
- package/dist/v8/resolver.js +11 -3
- package/dist/v8/resolver.js.map +1 -1
- package/dist/v8/settings.d.ts +16 -4
- package/dist/v8/settings.d.ts.map +1 -1
- package/dist/v8/settings.js +24 -21
- package/dist/v8/settings.js.map +1 -1
- package/dist/v8/types.d.ts +162 -22
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +35 -1
- package/src/async/default.ts +13 -0
- package/src/async/node.ts +12 -0
- package/src/async/types.ts +5 -0
- package/src/index.ts +4 -0
- package/src/v8/bucketing.ts +88 -0
- package/src/v8/builtins/campaigns.ts +120 -0
- package/src/v8/builtins/data.ts +2 -2
- package/src/v8/builtins/experiments.ts +72 -0
- package/src/v8/builtins/index.ts +7 -2
- package/src/v8/builtins/matchers.ts +0 -26
- package/src/v8/cli/builtins.ts +23 -0
- package/src/v8/cli/check/index.ts +289 -8
- package/src/v8/cli/schema/builtinSchemas.ts +82 -22
- package/src/v8/cli/schema/generate.ts +48 -3
- package/src/v8/client.ts +76 -10
- package/src/v8/cms.ts +134 -45
- package/src/v8/content.ts +168 -4
- package/src/v8/current.ts +98 -0
- package/src/v8/draft.ts +170 -30
- package/src/v8/experiments.ts +340 -0
- package/src/v8/hosts.ts +3 -2
- package/src/v8/index.ts +8 -4
- package/src/v8/preview/DraftBadge.tsx +72 -2
- package/src/v8/remoteLoader.ts +193 -26
- package/src/v8/resolver.ts +14 -4
- package/src/v8/settings.ts +35 -29
- package/src/v8/types.ts +163 -20
package/dist/v8/types.d.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* (`/next/api-reference#types`) — every type here is documented there.
|
|
4
4
|
*/
|
|
5
5
|
import type { ReactNode } from "react";
|
|
6
|
+
import type { Assignments, Visitor } from "./experiments.ts";
|
|
6
7
|
/**
|
|
7
8
|
* A block as stored: the JSON in `.deco/blocks`, and what `client.list` and
|
|
8
9
|
* `{ run: false }` return. Props never use it: a field's type is the value the
|
|
@@ -36,6 +37,13 @@ export type Snapshot = {
|
|
|
36
37
|
* module, a build outside git), the bundled content counts as the oldest.
|
|
37
38
|
*/
|
|
38
39
|
committedAt?: string;
|
|
40
|
+
/**
|
|
41
|
+
* The hosts this release may be previewed on (host patterns), as Studio
|
|
42
|
+
* wrote them into the release metadata on delivery.decocms.com
|
|
43
|
+
* (`latest.json` and `revisions/<sha>.json`). Previews are checked against
|
|
44
|
+
* the current release's list; the bundled content has none.
|
|
45
|
+
*/
|
|
46
|
+
previewHosts?: string[];
|
|
39
47
|
};
|
|
40
48
|
/**
|
|
41
49
|
* The content module `deco content` writes: a snapshot plus `root`, the
|
|
@@ -87,10 +95,14 @@ export interface Seo {
|
|
|
87
95
|
title: string;
|
|
88
96
|
description: string;
|
|
89
97
|
}
|
|
90
|
-
/**
|
|
91
|
-
|
|
98
|
+
/**
|
|
99
|
+
* The built-in `page` block, resolved: without `seo`, the site's defaults
|
|
100
|
+
* apply. Its sections are whatever its section blocks return: React
|
|
101
|
+
* elements by default, or a site's descriptors (`Page<{ component, props }>`).
|
|
102
|
+
*/
|
|
103
|
+
export interface Page<Section = ReactNode> extends Route {
|
|
92
104
|
seo?: Seo;
|
|
93
|
-
sections:
|
|
105
|
+
sections: Section[];
|
|
94
106
|
}
|
|
95
107
|
/** The built-in `redirect` block. */
|
|
96
108
|
export interface Redirect {
|
|
@@ -141,16 +153,19 @@ export interface Variant<T> {
|
|
|
141
153
|
* caps (see /next/built-in-blocks#cms-settings).
|
|
142
154
|
*/
|
|
143
155
|
export interface CMSSettings {
|
|
144
|
-
/** Host patterns previews are allowed on, within `createCMS`'s `preview.hosts`. */
|
|
145
|
-
preview?: {
|
|
146
|
-
hosts?: string[];
|
|
147
|
-
};
|
|
148
156
|
telemetry?: Telemetry;
|
|
149
157
|
analytics?: Analytics;
|
|
150
158
|
}
|
|
151
159
|
/** What `cms.settings()` returns: every section, defaults filled in, caps applied. */
|
|
152
160
|
export interface EffectiveSettings {
|
|
153
|
-
/**
|
|
161
|
+
/**
|
|
162
|
+
* The hosts previews are allowed on, as known in memory: the current
|
|
163
|
+
* release's `previewHosts` (Studio writes them into the release metadata)
|
|
164
|
+
* plus `createCMS`'s `preview.hosts`. A site with no list published (no
|
|
165
|
+
* `latest.json` yet, or one without `previewHosts`) and content with no
|
|
166
|
+
* remote site and no `preview.hosts` give `["*"]`, every host; an empty
|
|
167
|
+
* list, none (as before this process has read the release metadata).
|
|
168
|
+
*/
|
|
154
169
|
preview: {
|
|
155
170
|
hosts: string[];
|
|
156
171
|
};
|
|
@@ -241,8 +256,12 @@ export interface CMSConfig {
|
|
|
241
256
|
telemetry?: false | TelemetryConfig;
|
|
242
257
|
preview?: {
|
|
243
258
|
/**
|
|
244
|
-
*
|
|
245
|
-
*
|
|
259
|
+
* Hosts previews are allowed on besides the release's own (Studio writes
|
|
260
|
+
* those into the release metadata), in the host pattern format
|
|
261
|
+
* (/next/api-reference#host-patterns). Without `site`, the only list:
|
|
262
|
+
* left out, previews are allowed on every host. With `site`, it adds to
|
|
263
|
+
* the release's list; a site with no list published previews on every
|
|
264
|
+
* host anyway.
|
|
246
265
|
*/
|
|
247
266
|
hosts?: string[];
|
|
248
267
|
/**
|
|
@@ -278,13 +297,14 @@ export interface ListOptions<T> {
|
|
|
278
297
|
run?: boolean;
|
|
279
298
|
}
|
|
280
299
|
/**
|
|
281
|
-
* Reads one revision for one request.
|
|
300
|
+
* Reads one revision for one request, for one visitor, at one moment.
|
|
282
301
|
*
|
|
283
302
|
* Results are read-only. A client runs each block function once per distinct
|
|
284
303
|
* input, so the same block at two places in a request hands both places the
|
|
285
304
|
* same object; mutating it in place changes the other occurrence too. Copy a
|
|
286
305
|
* result before changing it. (The memo belongs to one client, so nothing is
|
|
287
|
-
* shared across requests
|
|
306
|
+
* shared across requests; it is also why tests and campaigns are decided once
|
|
307
|
+
* per client, however many rules ask.)
|
|
288
308
|
*/
|
|
289
309
|
export interface Client {
|
|
290
310
|
resolve<T = unknown>(target: unknown, options?: ResolveOptions): Promise<Result<T>>;
|
|
@@ -295,17 +315,118 @@ export interface Client {
|
|
|
295
315
|
* can't load, since there is no revision to report.
|
|
296
316
|
*/
|
|
297
317
|
revision(): Promise<string>;
|
|
318
|
+
/** Who is visiting, as passed to `cms.client()` (a frozen copy); `null` for none. */
|
|
319
|
+
readonly visitor: Visitor | null;
|
|
320
|
+
/**
|
|
321
|
+
* The version it reads: the release's commit SHA, or the draft pointer, as
|
|
322
|
+
* passed to `cms.client()`. For the current release, the SHA of the release
|
|
323
|
+
* it loaded (`undefined` until its first `resolve`, `list` or `revision()`).
|
|
324
|
+
*/
|
|
325
|
+
readonly version: string | undefined;
|
|
326
|
+
/** The date campaigns run at: the one passed to `cms.client()`, otherwise when the client was created. */
|
|
327
|
+
readonly date: Date;
|
|
298
328
|
}
|
|
329
|
+
/**
|
|
330
|
+
* What one client is created with, all read by the app outside blocks and
|
|
331
|
+
* passed in. The visitor and the date decide; `experiments` and `campaigns`
|
|
332
|
+
* override what they decide, name by name. Given every test and campaign and
|
|
333
|
+
* no visitor, a client is fully decided in advance (a page rendering what a
|
|
334
|
+
* proxy in front of it decided).
|
|
335
|
+
*/
|
|
336
|
+
export interface ClientOptions {
|
|
337
|
+
/**
|
|
338
|
+
* What to read: `undefined` (or `null`) for the current release; a
|
|
339
|
+
* published release's commit SHA (40 hex characters), read from hosted
|
|
340
|
+
* releases; or a draft pointer, the draft's changes over the current
|
|
341
|
+
* release. Only a version: a Studio link's moment and switched tests are
|
|
342
|
+
* the options below (`cms.readVersion(request)` reads all of them).
|
|
343
|
+
*/
|
|
344
|
+
version?: string | null;
|
|
345
|
+
/**
|
|
346
|
+
* For a draft: the release check made for this request
|
|
347
|
+
* (`cms.checkRelease()`), so the draft waits for it instead of making a
|
|
348
|
+
* check of its own; one request, one read of the release metadata. An id
|
|
349
|
+
* this CMS didn't make (another process's) is a check of its own. Ignored
|
|
350
|
+
* for the release and a release's SHA.
|
|
351
|
+
*/
|
|
352
|
+
releaseCheck?: string | null;
|
|
353
|
+
/**
|
|
354
|
+
* Who is visiting (`readVisitor`, `parseVisitor` or `newVisitor` from
|
|
355
|
+
* `@decocms/blocks/experiments`): the saved block `experiments` decides
|
|
356
|
+
* its tests for them. Without one, every test not in `experiments` is off.
|
|
357
|
+
*/
|
|
358
|
+
visitor?: Visitor | null;
|
|
359
|
+
/**
|
|
360
|
+
* The moment the saved block `campaigns` checks its windows at
|
|
361
|
+
* (`client.date`). Default: when the client is created.
|
|
362
|
+
*/
|
|
363
|
+
date?: Date | null;
|
|
364
|
+
/**
|
|
365
|
+
* Tests on (`true`) or off (`false`) by name, whatever the visitor and the
|
|
366
|
+
* test's percentage; the others are decided for the visitor. A name the
|
|
367
|
+
* saved block `experiments` doesn't have stays out.
|
|
368
|
+
*/
|
|
369
|
+
experiments?: Partial<Assignments> | null;
|
|
370
|
+
/**
|
|
371
|
+
* Campaigns on (`true`) or off (`false`) by name, whatever their window;
|
|
372
|
+
* the others are checked at `date`. A name the saved block `campaigns`
|
|
373
|
+
* doesn't have stays out.
|
|
374
|
+
*/
|
|
375
|
+
campaigns?: Partial<Assignments> | null;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* What a Studio link asks for (`cms.readVersion`, `parseVersion`): the
|
|
379
|
+
* version, and the preview's moment and the tests and campaigns it switches,
|
|
380
|
+
* in the shape of `ClientOptions`, so it spreads into `cms.client()`.
|
|
381
|
+
*/
|
|
382
|
+
export interface RequestVersion {
|
|
383
|
+
/** A draft pointer, or a published release's commit SHA. */
|
|
384
|
+
version: string;
|
|
385
|
+
/** The moment the preview shows (`__at`). */
|
|
386
|
+
date?: Date;
|
|
387
|
+
/** The tests the preview switches on or off (`__test=<name>=on|off`). */
|
|
388
|
+
experiments?: Assignments;
|
|
389
|
+
/** The campaigns the preview switches on or off (`__campaign=<name>=on|off`). */
|
|
390
|
+
campaigns?: Assignments;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* `cms.readVersion`'s and `cms.forDraft`'s options: the release check made
|
|
394
|
+
* for the request (`cms.checkRelease()`), so they share it instead of
|
|
395
|
+
* checking again.
|
|
396
|
+
*/
|
|
397
|
+
export type ReleaseCheckOptions = {
|
|
398
|
+
releaseCheck?: string | null;
|
|
399
|
+
};
|
|
299
400
|
export interface CMS {
|
|
300
|
-
/**
|
|
401
|
+
/**
|
|
402
|
+
* A client: the version it reads and the visitor it decides tests for, both
|
|
403
|
+
* passed in. Create one per request.
|
|
404
|
+
*/
|
|
405
|
+
client(options?: ClientOptions): Client;
|
|
406
|
+
/** A client reading the current release: `cms.client()`. */
|
|
301
407
|
forRelease(): Client;
|
|
302
408
|
/**
|
|
303
|
-
* A client reading the draft a pointer names:
|
|
304
|
-
* draft host (`*.decocms.com` and loopback by
|
|
305
|
-
* `createCMS({ preview: { draftHosts } })` replaces the list)
|
|
306
|
-
* server's production content.
|
|
409
|
+
* A client reading the draft a pointer names: `cms.client({ version: pointer })`.
|
|
410
|
+
* Its changes are fetched from a draft host (`*.decocms.com` and loopback by
|
|
411
|
+
* default; `createCMS({ preview: { draftHosts } })` replaces the list) and
|
|
412
|
+
* laid over this server's production content.
|
|
413
|
+
*/
|
|
414
|
+
forDraft(pointer: string, options?: ReleaseCheckOptions): Client;
|
|
415
|
+
/**
|
|
416
|
+
* Checks for the latest release now, as a draft read does before it
|
|
417
|
+
* renders, and resolves with that check's id once it ends; never rejects.
|
|
418
|
+
* A request arriving while a check runs waits for the next one, shared by
|
|
419
|
+
* every request arriving meanwhile; there is no time-based throttle. Pass
|
|
420
|
+
* the id as `releaseCheck` to `readVersion`, `forDraft` and `client` for
|
|
421
|
+
* the same request, so its host check, its draft reads and its render
|
|
422
|
+
* share one check: one read of `latest.json` per preview request. A
|
|
423
|
+
* binding (`@decocms/tanstack`'s worker, `@decocms/nextjs`'s proxy) makes
|
|
424
|
+
* one per draft request and hands it down. Given the id of a check this
|
|
425
|
+
* CMS made (one the proxy in front made for the same request), it waits
|
|
426
|
+
* for that check and resolves with the same id; an id it didn't make
|
|
427
|
+
* (another process's) is ignored, and it checks.
|
|
307
428
|
*/
|
|
308
|
-
|
|
429
|
+
checkRelease(releaseCheck?: string | null): Promise<string>;
|
|
309
430
|
/** Ask the content source for newer content now; never throws. */
|
|
310
431
|
update(): Promise<{
|
|
311
432
|
updated: boolean;
|
|
@@ -317,11 +438,30 @@ export interface CMS {
|
|
|
317
438
|
*/
|
|
318
439
|
settings(): Promise<EffectiveSettings>;
|
|
319
440
|
/**
|
|
320
|
-
*
|
|
321
|
-
* cookie
|
|
322
|
-
*
|
|
323
|
-
* (
|
|
441
|
+
* What a Studio link asks for: `?__draft=` first, then the `__deco_draft`
|
|
442
|
+
* cookie, holding a draft pointer or a published release's commit SHA,
|
|
443
|
+
* with the preview's moment (`__at`) and switched tests (`__test`) and
|
|
444
|
+
* campaigns (`__campaign`) as `date`, `experiments` and `campaigns`.
|
|
445
|
+
* `null` when neither is present, for `?__draft=off`, on a host outside
|
|
446
|
+
* the preview hosts (the current release's `previewHosts`, which Studio
|
|
447
|
+
* writes into the release metadata, plus `createCMS`'s `preview.hosts`;
|
|
448
|
+
* every host while the site has published no list, or without `site` and
|
|
449
|
+
* `preview.hosts`; no host while the metadata can't be read, besides
|
|
450
|
+
* `preview.hosts`), and for a value that doesn't parse (ignored,
|
|
451
|
+
* as if absent): in each case the request gets the current release. A
|
|
452
|
+
* draft and an older release's SHA are both checked against the current
|
|
453
|
+
* release's list.
|
|
454
|
+
*
|
|
455
|
+
* ```ts
|
|
456
|
+
* const client = cms.client({ visitor, ...(await cms.readVersion(request)) });
|
|
457
|
+
* ```
|
|
458
|
+
*
|
|
459
|
+
* With `releaseCheck` (the id `checkRelease()` resolved with for this
|
|
460
|
+
* request), the host check uses the preview hosts that check read instead
|
|
461
|
+
* of reading the release metadata again.
|
|
324
462
|
*/
|
|
463
|
+
readVersion(request: RequestLike, options?: ReleaseCheckOptions): Promise<RequestVersion | null>;
|
|
464
|
+
/** The version alone (`readVersion(request)`'s `version`), under its earlier name. */
|
|
325
465
|
draftPointer(request: RequestLike): Promise<string | null>;
|
|
326
466
|
/**
|
|
327
467
|
* The `Set-Cookie` value that starts a preview (a valid `?__draft=` on an
|
package/dist/v8/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/v8/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/v8/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAM7D;;;;GAIG;AACH,MAAM,MAAM,KAAK,GAAG;IAAE,aAAa,EAAE,MAAM,CAAC;IAAC,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC;AAExE,mCAAmC;AACnC,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAExE,oEAAoE;AACpE,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AAEnD,2FAA2F;AAC3F,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,wEAAwE;IACxE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC;;;;OAIG;IAC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AG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;CAC3D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/blocks",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24"
|
|
@@ -13,6 +13,35 @@
|
|
|
13
13
|
},
|
|
14
14
|
"main": "./dist/index.js",
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
|
+
"imports": {
|
|
17
|
+
"#async-store": {
|
|
18
|
+
"node": {
|
|
19
|
+
"types": "./dist/async/node.d.ts",
|
|
20
|
+
"source": "./src/async/node.ts",
|
|
21
|
+
"default": "./dist/async/node.js"
|
|
22
|
+
},
|
|
23
|
+
"workerd": {
|
|
24
|
+
"types": "./dist/async/node.d.ts",
|
|
25
|
+
"source": "./src/async/node.ts",
|
|
26
|
+
"default": "./dist/async/node.js"
|
|
27
|
+
},
|
|
28
|
+
"bun": {
|
|
29
|
+
"types": "./dist/async/node.d.ts",
|
|
30
|
+
"source": "./src/async/node.ts",
|
|
31
|
+
"default": "./dist/async/node.js"
|
|
32
|
+
},
|
|
33
|
+
"deno": {
|
|
34
|
+
"types": "./dist/async/node.d.ts",
|
|
35
|
+
"source": "./src/async/node.ts",
|
|
36
|
+
"default": "./dist/async/node.js"
|
|
37
|
+
},
|
|
38
|
+
"default": {
|
|
39
|
+
"types": "./dist/async/default.d.ts",
|
|
40
|
+
"source": "./src/async/default.ts",
|
|
41
|
+
"default": "./dist/async/default.js"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
16
45
|
"files": [
|
|
17
46
|
"bin",
|
|
18
47
|
"dist",
|
|
@@ -43,6 +72,11 @@
|
|
|
43
72
|
"source": "./src/v8/cli/index.ts",
|
|
44
73
|
"default": "./dist/v8/cli/index.js"
|
|
45
74
|
},
|
|
75
|
+
"./experiments": {
|
|
76
|
+
"types": "./dist/v8/experiments.d.ts",
|
|
77
|
+
"source": "./src/v8/experiments.ts",
|
|
78
|
+
"default": "./dist/v8/experiments.js"
|
|
79
|
+
},
|
|
46
80
|
"./fetch": {
|
|
47
81
|
"types": "./dist/v8/fetch.d.ts",
|
|
48
82
|
"source": "./src/v8/fetch.ts",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The async store where `node:async_hooks` isn't there to import (browsers,
|
|
3
|
+
* React Native, edge runtimes). A runtime that exposes `AsyncLocalStorage` as
|
|
4
|
+
* a global (Vercel's edge runtime) still gets one; elsewhere there is none,
|
|
5
|
+
* and `currentClient()` works from the start of a block function, before its
|
|
6
|
+
* first `await`. Picked by the `#async-store` import's `default` condition.
|
|
7
|
+
*/
|
|
8
|
+
import type { AsyncStore } from "./types.ts";
|
|
9
|
+
|
|
10
|
+
export function createAsyncStore<T>(): AsyncStore<T> | undefined {
|
|
11
|
+
const Storage = (globalThis as { AsyncLocalStorage?: new () => AsyncStore<T> }).AsyncLocalStorage;
|
|
12
|
+
return typeof Storage === "function" ? new Storage() : undefined;
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The async store on servers with `AsyncLocalStorage` (Node, Bun, Deno, and
|
|
3
|
+
* Workers with `nodejs_compat`), so `currentClient()` survives `await`.
|
|
4
|
+
* Picked by the `#async-store` import's `node`, `workerd`, `bun` and `deno`
|
|
5
|
+
* conditions; the runtime core (`src/v8`) never imports a Node built-in.
|
|
6
|
+
*/
|
|
7
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
8
|
+
import type { AsyncStore } from "./types.ts";
|
|
9
|
+
|
|
10
|
+
export function createAsyncStore<T>(): AsyncStore<T> | undefined {
|
|
11
|
+
return new AsyncLocalStorage<T>();
|
|
12
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -6,10 +6,12 @@ export {
|
|
|
6
6
|
type BlockFunction,
|
|
7
7
|
type Blocks,
|
|
8
8
|
type Client,
|
|
9
|
+
type ClientOptions,
|
|
9
10
|
type CMS,
|
|
10
11
|
type CMSError,
|
|
11
12
|
type CMSSettings,
|
|
12
13
|
createCMS,
|
|
14
|
+
currentClient,
|
|
13
15
|
type DraftPointer,
|
|
14
16
|
type EffectiveSettings,
|
|
15
17
|
formatDraftPointer,
|
|
@@ -20,8 +22,10 @@ export {
|
|
|
20
22
|
matchRoute,
|
|
21
23
|
type Page,
|
|
22
24
|
parseDraftPointer,
|
|
25
|
+
parseVersion,
|
|
23
26
|
type Redirect,
|
|
24
27
|
type RequestLike,
|
|
28
|
+
type RequestVersion,
|
|
25
29
|
type Result,
|
|
26
30
|
type Route,
|
|
27
31
|
remoteLoader,
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic behind `@decocms/blocks/experiments`: the hash, the roll and
|
|
3
|
+
* the visitor id. Internal: not in the exports map, so the public surface is
|
|
4
|
+
* `assign` and the visitor helpers alone, and the tests import this by path.
|
|
5
|
+
*
|
|
6
|
+
* A hash bug here fails silently: every visitor still gets an answer, it is
|
|
7
|
+
* just the wrong one, and the test's result is quietly meaningless. That is
|
|
8
|
+
* why `experiments.test.ts` pins MurmurHash3's published vectors and a table
|
|
9
|
+
* of rolls, and why changing anything here moves visitors already allocated.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Fills `bytes` with random values. `newId` takes one so tests can pin the id. */
|
|
13
|
+
export type RandomBytes = (bytes: Uint8Array<ArrayBuffer>) => void;
|
|
14
|
+
|
|
15
|
+
const ID_BYTES = 16;
|
|
16
|
+
|
|
17
|
+
const encoder = new TextEncoder();
|
|
18
|
+
|
|
19
|
+
/** MurmurHash3 x86 32-bit, seed 0 by default, over `bytes`, as an unsigned 32-bit number. */
|
|
20
|
+
export function murmur3_32(bytes: Uint8Array, seed = 0): number {
|
|
21
|
+
const c1 = 0xcc9e2d51;
|
|
22
|
+
const c2 = 0x1b873593;
|
|
23
|
+
const len = bytes.length;
|
|
24
|
+
const blocks = len & ~3;
|
|
25
|
+
let h = seed >>> 0;
|
|
26
|
+
|
|
27
|
+
for (let i = 0; i < blocks; i += 4) {
|
|
28
|
+
let k =
|
|
29
|
+
((bytes[i] as number) |
|
|
30
|
+
((bytes[i + 1] as number) << 8) |
|
|
31
|
+
((bytes[i + 2] as number) << 16) |
|
|
32
|
+
((bytes[i + 3] as number) << 24)) >>>
|
|
33
|
+
0;
|
|
34
|
+
k = Math.imul(k, c1);
|
|
35
|
+
k = (k << 15) | (k >>> 17);
|
|
36
|
+
k = Math.imul(k, c2);
|
|
37
|
+
h ^= k;
|
|
38
|
+
h = (h << 13) | (h >>> 19);
|
|
39
|
+
h = (Math.imul(h, 5) + 0xe6546b64) | 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const tail = len & 3;
|
|
43
|
+
if (tail > 0) {
|
|
44
|
+
let k = 0;
|
|
45
|
+
if (tail === 3) k ^= (bytes[blocks + 2] as number) << 16;
|
|
46
|
+
if (tail >= 2) k ^= (bytes[blocks + 1] as number) << 8;
|
|
47
|
+
k ^= bytes[blocks] as number;
|
|
48
|
+
k = Math.imul(k, c1);
|
|
49
|
+
k = (k << 15) | (k >>> 17);
|
|
50
|
+
k = Math.imul(k, c2);
|
|
51
|
+
h ^= k;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
h ^= len;
|
|
55
|
+
h ^= h >>> 16;
|
|
56
|
+
h = Math.imul(h, 0x85ebca6b);
|
|
57
|
+
h ^= h >>> 13;
|
|
58
|
+
h = Math.imul(h, 0xc2b2ae35);
|
|
59
|
+
h ^= h >>> 16;
|
|
60
|
+
return h >>> 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* This visitor's number for this experiment: a fixed value in [0, 100) with a
|
|
65
|
+
* step of 0.01, the same on every runtime. The visitor is in when it is below
|
|
66
|
+
* the allocation, which is what makes a ramp only ever add people: the roll
|
|
67
|
+
* does not move, the threshold does.
|
|
68
|
+
*/
|
|
69
|
+
export function roll(name: string, id: string): number {
|
|
70
|
+
return (murmur3_32(encoder.encode(`${name}:${id}`)) % 10000) / 100;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function toBase64Url(bytes: Uint8Array): string {
|
|
74
|
+
let binary = "";
|
|
75
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
76
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const defaultRandom: RandomBytes = (bytes) => {
|
|
80
|
+
crypto.getRandomValues(bytes);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** A fresh visitor id: 128 bits as 22 base64url characters. */
|
|
84
|
+
export function newId(random: RandomBytes = defaultRandom): string {
|
|
85
|
+
const bytes = new Uint8Array(ID_BYTES);
|
|
86
|
+
random(bytes);
|
|
87
|
+
return toBase64Url(bytes);
|
|
88
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Campaign dates as content (see /next/built-in-blocks#calendar): the
|
|
3
|
+
* built-in type `calendar`, which the saved block `campaigns` has, and the
|
|
4
|
+
* built-in rule `campaign`.
|
|
5
|
+
*
|
|
6
|
+
* - `campaigns` maps each campaign's name to one window, `{ start?, end? }`,
|
|
7
|
+
* ISO date-times with an offset. Resolving it gives each name and whether
|
|
8
|
+
* the client's date is inside its window, never a date.
|
|
9
|
+
* - `campaign { name }` is `true` when that answer is `true`. A rule always
|
|
10
|
+
* names a campaign: there are no inline `{ start, end }` dates, so
|
|
11
|
+
* `calendar` is the only place date logic lives.
|
|
12
|
+
*
|
|
13
|
+
* The date is the client's (`client.date`: `cms.client({ date })`, or when
|
|
14
|
+
* the client was created), read through `currentClient()` before any
|
|
15
|
+
* `await`. A campaign the client is given (`cms.client({ campaigns })`) is
|
|
16
|
+
* on or off as given, whatever its window; a name the block doesn't have
|
|
17
|
+
* stays out.
|
|
18
|
+
*/
|
|
19
|
+
import { campaignOverrides, currentClient } from "../current.ts";
|
|
20
|
+
|
|
21
|
+
/** The saved block every `campaign` rule reads, and its built-in type. */
|
|
22
|
+
export const CAMPAIGNS_BLOCK = "campaigns";
|
|
23
|
+
export const CALENDAR_TYPE = "calendar";
|
|
24
|
+
|
|
25
|
+
/** The built-in rule that reads a campaign. */
|
|
26
|
+
export const CAMPAIGN_RULE = "campaign";
|
|
27
|
+
|
|
28
|
+
/** What resolving `campaigns` gives: each campaign and whether it is running. */
|
|
29
|
+
type Campaigns = Record<string, boolean>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `calendar`: each campaign and whether the current client's date is inside
|
|
33
|
+
* its window, or, for a campaign the client was given, what it was given.
|
|
34
|
+
*/
|
|
35
|
+
export const calendar = (windows: Record<string, unknown> = {}): Campaigns => {
|
|
36
|
+
const client = currentClient();
|
|
37
|
+
const given = campaignOverrides(client);
|
|
38
|
+
const at = client.date.getTime();
|
|
39
|
+
const out = Object.create(null) as Campaigns;
|
|
40
|
+
for (const name of Object.keys(windows)) {
|
|
41
|
+
out[name] =
|
|
42
|
+
given !== undefined && Object.hasOwn(given, name)
|
|
43
|
+
? given[name] === true
|
|
44
|
+
: inside(windows[name], at);
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Whether `at` (ms since the epoch) is inside `window`, `{ start?, end? }`:
|
|
51
|
+
* from `start` (inclusive) until `end` (exclusive); an empty or absent bound
|
|
52
|
+
* is open. A window that isn't an object, or a bound that isn't an ISO 8601
|
|
53
|
+
* date-time with an offset, is outside, so a typo can't switch a campaign
|
|
54
|
+
* on. See `campaignTime` for how a bound reads.
|
|
55
|
+
*/
|
|
56
|
+
function inside(window: unknown, at: number): boolean {
|
|
57
|
+
if (typeof window !== "object" || window === null || Array.isArray(window)) return false;
|
|
58
|
+
const { start, end } = window as Record<string, unknown>;
|
|
59
|
+
const from = campaignTime(start);
|
|
60
|
+
const until = campaignTime(end);
|
|
61
|
+
if (from === null || until === null) return false;
|
|
62
|
+
return (from === undefined || at >= from) && (until === undefined || at < until);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** An ISO 8601 date-time with its offset (`Z` or `±hh:mm`). */
|
|
66
|
+
const ISO_RE = /^(\d{4}-\d{2}-\d{2})T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A campaign bound's time in ms since the epoch: `undefined` when absent or
|
|
70
|
+
* empty (open), `null` when it can't be one. A bound is an ISO 8601
|
|
71
|
+
* date-time with an explicit offset (`Z` or `±hh:mm`), so it names the same
|
|
72
|
+
* moment on every runtime. A date without a time, a date-time without an
|
|
73
|
+
* offset (whose moment depends on where it is read), a day the calendar
|
|
74
|
+
* doesn't have and anything else, such as `"Nov 27 2026"`, can't be one,
|
|
75
|
+
* so that campaign never runs.
|
|
76
|
+
*/
|
|
77
|
+
export function campaignTime(value: unknown): number | undefined | null {
|
|
78
|
+
if (value === undefined || value === null || value === "") return undefined;
|
|
79
|
+
if (typeof value !== "string") return null;
|
|
80
|
+
const match = ISO_RE.exec(value);
|
|
81
|
+
if (match === null || !isCalendarDate(match[1] as string)) return null;
|
|
82
|
+
const time = Date.parse(value);
|
|
83
|
+
return Number.isNaN(time) ? null : time;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Whether `yyyy-mm-dd` is a day of the calendar (some runtimes roll February 30 into March, others refuse it). */
|
|
87
|
+
function isCalendarDate(date: string): boolean {
|
|
88
|
+
const [y, m, d] = date.split("-").map(Number) as [number, number, number];
|
|
89
|
+
const day = new Date(Date.UTC(y, m - 1, d));
|
|
90
|
+
return day.getUTCFullYear() === y && day.getUTCMonth() === m - 1 && day.getUTCDate() === d;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Inputs of the built-in `campaign` rule. */
|
|
94
|
+
interface CampaignProps {
|
|
95
|
+
/** A campaign in the saved block `campaigns`. */
|
|
96
|
+
name?: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* `campaign`: `true` while the campaign `name` of the saved block
|
|
101
|
+
* `campaigns` is running for the client. An unknown name, no `campaigns`
|
|
102
|
+
* block, or a rule without a name give `false`. The block is resolved once
|
|
103
|
+
* per client, however many rules ask.
|
|
104
|
+
*/
|
|
105
|
+
export const campaign = ({ name }: CampaignProps = {}): boolean | Promise<boolean> => {
|
|
106
|
+
const client = currentClient();
|
|
107
|
+
if (typeof name !== "string") return false;
|
|
108
|
+
return client
|
|
109
|
+
.resolve<Record<string, unknown>>(CAMPAIGNS_BLOCK)
|
|
110
|
+
.then(([campaigns]) => isTrueAt(campaigns, name));
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
function isTrueAt(record: unknown, name: string): boolean {
|
|
114
|
+
return (
|
|
115
|
+
typeof record === "object" &&
|
|
116
|
+
record !== null &&
|
|
117
|
+
Object.hasOwn(record, name) &&
|
|
118
|
+
(record as Record<string, unknown>)[name] === true
|
|
119
|
+
);
|
|
120
|
+
}
|
package/src/v8/builtins/data.ts
CHANGED
|
@@ -49,8 +49,8 @@ export function analyticsSection(section: unknown): Required<Analytics> {
|
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
51
|
* `cms-settings`: the type of the well-known saved block `CMS`. Returns its
|
|
52
|
-
* input with the telemetry and analytics defaults filled in
|
|
53
|
-
*
|
|
52
|
+
* input with the telemetry and analytics defaults filled in. Preview hosts
|
|
53
|
+
* aren't here: Studio writes them into the release metadata.
|
|
54
54
|
*/
|
|
55
55
|
export const cmsSettings = (
|
|
56
56
|
props: CMSSettings = {},
|