@decocms/blocks 8.1.0-next.7 → 8.1.1

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.
Files changed (55) hide show
  1. package/dist/v8/cli/check/index.d.ts +6 -2
  2. package/dist/v8/cli/check/index.d.ts.map +1 -1
  3. package/dist/v8/cli/check/index.js +34 -7
  4. package/dist/v8/cli/check/index.js.map +1 -1
  5. package/dist/v8/cli/color.d.ts +14 -0
  6. package/dist/v8/cli/color.d.ts.map +1 -0
  7. package/dist/v8/cli/color.js +14 -0
  8. package/dist/v8/cli/color.js.map +1 -0
  9. package/dist/v8/cli/log.d.ts +4 -0
  10. package/dist/v8/cli/log.d.ts.map +1 -1
  11. package/dist/v8/cli/log.js +9 -0
  12. package/dist/v8/cli/log.js.map +1 -1
  13. package/dist/v8/cli/run.d.ts +1 -0
  14. package/dist/v8/cli/run.d.ts.map +1 -1
  15. package/dist/v8/cli/run.js +17 -3
  16. package/dist/v8/cli/run.js.map +1 -1
  17. package/dist/v8/cli/schema/index.d.ts.map +1 -1
  18. package/dist/v8/cli/schema/index.js +5 -3
  19. package/dist/v8/cli/schema/index.js.map +1 -1
  20. package/dist/v8/cli/schema/tsProgram.d.ts +9 -0
  21. package/dist/v8/cli/schema/tsProgram.d.ts.map +1 -1
  22. package/dist/v8/cli/schema/tsProgram.js +12 -0
  23. package/dist/v8/cli/schema/tsProgram.js.map +1 -1
  24. package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -1
  25. package/dist/v8/cli/schema/typeToSchema.js +54 -7
  26. package/dist/v8/cli/schema/typeToSchema.js.map +1 -1
  27. package/dist/v8/cms.js +1 -1
  28. package/dist/v8/cms.js.map +1 -1
  29. package/dist/v8/matchRoute.d.ts +12 -10
  30. package/dist/v8/matchRoute.d.ts.map +1 -1
  31. package/dist/v8/matchRoute.js +60 -86
  32. package/dist/v8/matchRoute.js.map +1 -1
  33. package/dist/v8/remoteLoader.d.ts +2 -0
  34. package/dist/v8/remoteLoader.d.ts.map +1 -1
  35. package/dist/v8/remoteLoader.js +7 -16
  36. package/dist/v8/remoteLoader.js.map +1 -1
  37. package/dist/v8/routeTrie.d.ts +43 -0
  38. package/dist/v8/routeTrie.d.ts.map +1 -0
  39. package/dist/v8/routeTrie.js +88 -0
  40. package/dist/v8/routeTrie.js.map +1 -0
  41. package/dist/v8/types.d.ts +12 -1
  42. package/dist/v8/types.d.ts.map +1 -1
  43. package/package.json +1 -1
  44. package/src/v8/cli/check/index.ts +32 -7
  45. package/src/v8/cli/color.ts +29 -0
  46. package/src/v8/cli/log.ts +12 -0
  47. package/src/v8/cli/run.ts +18 -3
  48. package/src/v8/cli/schema/index.ts +5 -3
  49. package/src/v8/cli/schema/tsProgram.ts +14 -1
  50. package/src/v8/cli/schema/typeToSchema.ts +50 -7
  51. package/src/v8/cms.ts +1 -1
  52. package/src/v8/matchRoute.ts +70 -106
  53. package/src/v8/remoteLoader.ts +13 -17
  54. package/src/v8/routeTrie.ts +103 -0
  55. package/src/v8/types.ts +12 -1
@@ -25,7 +25,9 @@
25
25
  * a publish still loses to it.
26
26
  * Pointers aren't ordered among themselves, and the fallback's content is
27
27
  * never compared: only the two timestamps are.
28
- * - In development (`NODE_ENV=development`), it never swaps, so local files win.
28
+ * - With `dev: true` (`createCMS({ dev })`), it never swaps, so local files win.
29
+ * The SDK reads no environment variable to decide this: the site passes it
30
+ * (`import.meta.env.DEV`, `process.env.NODE_ENV === "development"`).
29
31
  * - A release larger than `MAX_SNAPSHOT_BYTES` is refused while it downloads,
30
32
  * before it's buffered whole (a Worker isolate has 128 MB).
31
33
  * - Without `site` it's a plain loader over the fallback.
@@ -55,6 +57,8 @@ interface RemoteLoaderOptions {
55
57
  site?: string;
56
58
  /** ms between release checks, used when `createCMS` has no `interval` of its own. */
57
59
  interval?: number;
60
+ /** Local development: never swap in a release, so local files win. */
61
+ dev?: boolean;
58
62
  }
59
63
 
60
64
  /** `sites/<site>/latest.json`: the release a site serves. */
@@ -73,11 +77,16 @@ class RemoteLoader implements Loader {
73
77
  #fallbackSnapshot: Snapshot | undefined;
74
78
  /** The release this process last swapped in; `undefined` serves the fallback. */
75
79
  #current: Snapshot | undefined;
80
+ readonly #dev: boolean;
76
81
 
77
- constructor(fallback: Snapshot | Loader, options: { site: string; interval?: number }) {
82
+ constructor(
83
+ fallback: Snapshot | Loader,
84
+ options: { site: string; interval?: number; dev?: boolean },
85
+ ) {
78
86
  this.#fallback = fallback;
79
87
  this.#site = options.site;
80
88
  this.interval = options.interval;
89
+ this.#dev = options.dev === true;
81
90
  }
82
91
 
83
92
  /** A hot reload hands the same instance new fallback content. */
@@ -98,7 +107,7 @@ class RemoteLoader implements Loader {
98
107
  }
99
108
 
100
109
  async update(): Promise<{ updated: boolean }> {
101
- if (isDevelopment()) return { updated: false };
110
+ if (this.#dev) return { updated: false };
102
111
  const fallback = this.#fallbackSnapshot ?? (await this.#loadFallback());
103
112
  if (fallback.schemaHash === undefined) return { updated: false };
104
113
  const site = encodeURIComponent(this.#site);
@@ -211,7 +220,7 @@ export function remoteLoader(fallback: Snapshot | Loader, options: RemoteLoaderO
211
220
  return existing;
212
221
  }
213
222
  const instance = hosted
214
- ? new RemoteLoader(fallback, { site: site!, interval: options.interval })
223
+ ? new RemoteLoader(fallback, { site: site!, interval: options.interval, dev: options.dev })
215
224
  : new LocalLoader(fallback);
216
225
  store[key] = instance;
217
226
  return instance;
@@ -241,16 +250,3 @@ function deliveryOrigin(): string {
241
250
  const override = (globalThis as Record<symbol, unknown>)[TEST_DELIVERY_ORIGIN];
242
251
  return typeof override === "string" ? override : HOSTED_DELIVERY_ORIGIN;
243
252
  }
244
-
245
- /**
246
- * The SDK's only environment read: local development never swaps in hosted
247
- * releases, so local files win.
248
- */
249
- function isDevelopment(): boolean {
250
- try {
251
- // Written out so bundlers that define process.env.NODE_ENV replace it.
252
- return process.env.NODE_ENV === "development";
253
- } catch {
254
- return false;
255
- }
256
- }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * The segment trie behind `matchRoute` (see /next/router-internals): paths
3
+ * compile into nodes keyed by segment, and a lookup walks the URL's segments,
4
+ * trying the literal child, then the parameter child, then a trailing `*` (a
5
+ * splat: one or more remaining segments). A lookup costs the URL's depth, not
6
+ * the number of entries. No regex, no sorting.
7
+ */
8
+
9
+ /** A leaf holds the entry and its parameter names, in path order; `splat` is a trailing `*`'s leaf. */
10
+ type Leaf<T> = { value: T; names: string[] };
11
+ export type TrieNode<T> = {
12
+ literals: Map<string, TrieNode<T>>;
13
+ param?: TrieNode<T>;
14
+ leaf?: Leaf<T>;
15
+ splat?: Leaf<T>;
16
+ };
17
+ export type Hit<T> = { value: T; params: Record<string, string>; rest: string[] };
18
+
19
+ /** A trie holding `entries` (`[path, value]`); on a conflict the earlier keeps the leaf. */
20
+ export function buildTrie<T>(entries: Iterable<[string, T]>): TrieNode<T> {
21
+ const root: TrieNode<T> = { literals: new Map() };
22
+ for (const [path, value] of entries) insert(root, path, value);
23
+ return root;
24
+ }
25
+
26
+ /** Adds `path`; returns the value already holding its leaf (a conflict: the earlier keeps it). */
27
+ export function insert<T>(root: TrieNode<T>, path: string, value: T): T | undefined {
28
+ const names: string[] = [];
29
+ const segments = splitPath(path);
30
+ let node = root;
31
+ for (const [i, segment] of segments.entries()) {
32
+ if (segment === "*" && i === segments.length - 1) {
33
+ if (node.splat) return node.splat.value;
34
+ node.splat = { value, names };
35
+ return undefined;
36
+ }
37
+ if (segment.startsWith(":") && segment.length > 1) {
38
+ names.push(segment.slice(1));
39
+ node = node.param ??= { literals: new Map() };
40
+ continue;
41
+ }
42
+ const literal = safeDecode(segment);
43
+ const child = node.literals.get(literal) ?? { literals: new Map() };
44
+ node.literals.set(literal, child);
45
+ node = child;
46
+ }
47
+ if (node.leaf) return node.leaf.value;
48
+ node.leaf = { value, names };
49
+ return undefined;
50
+ }
51
+
52
+ /** Walks the URL's segments: the literal child first, then the parameter child, then the splat. */
53
+ export function lookup<T>(
54
+ node: TrieNode<T>,
55
+ segs: string[],
56
+ i = 0,
57
+ vals: string[] = [],
58
+ ): Hit<T> | null {
59
+ if (i === segs.length) return node.leaf ? hit(node.leaf, vals, []) : null;
60
+ const literal = node.literals.get(segs[i]!);
61
+ let found = literal ? lookup(literal, segs, i + 1, vals) : null;
62
+ // A parameter is one segment and never contains a slash, even an encoded one.
63
+ if (!found && node.param && !segs[i]!.includes("/")) {
64
+ found = lookup(node.param, segs, i + 1, [...vals, segs[i]!]);
65
+ }
66
+ // A splat takes the rest (at least one segment, as there is one here).
67
+ return found ?? (node.splat ? hit(node.splat, vals, segs.slice(i)) : null);
68
+ }
69
+
70
+ function hit<T>({ value, names }: Leaf<T>, values: string[], rest: string[]): Hit<T> {
71
+ const params: Record<string, string> = Object.fromEntries(names.map((n, i) => [n, values[i]!]));
72
+ if (rest.length > 0) params["*"] = rest.join("/");
73
+ return { value, params, rest };
74
+ }
75
+
76
+ /** `/a/b/`, `/a//b` and `/a/b?x` all name `["a", "b"]`; `/` names `[]`. */
77
+ export function splitPath(path: string): string[] {
78
+ return splitRef(path)
79
+ .path.split("/")
80
+ .filter((segment) => segment.length > 0);
81
+ }
82
+
83
+ /**
84
+ * A path or URL split at its first `?` and `#`: everything before, the query
85
+ * (without its `?`, `""` when there's none) and the fragment (with its `#`).
86
+ */
87
+ export function splitRef(ref: string): { path: string; query: string; hash: string } {
88
+ const hashAt = ref.indexOf("#") === -1 ? ref.length : ref.indexOf("#");
89
+ const queryAt = ref.indexOf("?") === -1 || ref.indexOf("?") > hashAt ? hashAt : ref.indexOf("?");
90
+ return {
91
+ path: ref.slice(0, queryAt),
92
+ query: ref.slice(queryAt + 1, hashAt),
93
+ hash: ref.slice(hashAt),
94
+ };
95
+ }
96
+
97
+ export function safeDecode(segment: string): string {
98
+ try {
99
+ return decodeURIComponent(segment);
100
+ } catch {
101
+ return segment;
102
+ }
103
+ }
package/src/v8/types.ts CHANGED
@@ -107,7 +107,12 @@ export interface Page extends Route {
107
107
 
108
108
  /** The built-in `redirect` block. */
109
109
  export interface Redirect {
110
- /** Literal path or template (`:name` segments, an optional trailing `/*`). */
110
+ /**
111
+ * Literal path or template (`:name` segments, an optional trailing `/*`),
112
+ * optionally scoped to a query (`/calcados?map=c,ft`): then it matches only
113
+ * a request whose query string is exactly that one, and wins over a
114
+ * path-only `from` for the same URL.
115
+ */
111
116
  from: string;
112
117
  to: string;
113
118
  /** `true` for a 301, `false` for a 302. */
@@ -250,6 +255,12 @@ export interface CMSConfig {
250
255
  content: ContentModule | Loader;
251
256
  /** ms between `update()` checks of a content source that has one; minimum 60 000. */
252
257
  interval?: number;
258
+ /**
259
+ * Local development (`import.meta.env.DEV`, `process.env.NODE_ENV === "development"`):
260
+ * hosted releases are never swapped in, so local files win. Drafts still load.
261
+ * The SDK reads no environment variable for it; default `false`.
262
+ */
263
+ dev?: boolean;
253
264
  /** Where telemetry goes; see /next/telemetry. */
254
265
  telemetry?: false | TelemetryConfig;
255
266
  preview?: {