dinou 9.3.0 → 10.0.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 (166) hide show
  1. package/CHANGELOG.md +196 -0
  2. package/README.md +157 -132
  3. package/dinou/adapters/bun.js +34 -5
  4. package/dinou/adapters/cloudflare.js +110 -17
  5. package/dinou/adapters/deno.js +46 -8
  6. package/dinou/adapters/netlify.js +86 -50
  7. package/dinou/adapters/node.js +8 -0
  8. package/dinou/assets.d.ts +32 -5
  9. package/dinou/bun/build.js +190 -106
  10. package/dinou/cli.js +992 -532
  11. package/dinou/cloudflare/build.js +187 -106
  12. package/dinou/compiled/autoprefixer/index.js +509 -517
  13. package/dinou/compiled/babel-core/index.js +738 -716
  14. package/dinou/compiled/babel-generator/index.js +341 -299
  15. package/dinou/compiled/babel-loader/index.js +766 -744
  16. package/dinou/compiled/babel-parser/index.js +2 -2
  17. package/dinou/compiled/babel-plugin-react-compiler/index.js +178 -178
  18. package/dinou/compiled/babel-plugin-syntax-import-meta/index.js +4 -4
  19. package/dinou/compiled/babel-preset-react/index.js +752 -730
  20. package/dinou/compiled/babel-preset-typescript/index.js +738 -716
  21. package/dinou/compiled/babel-traverse/index.js +463 -433
  22. package/dinou/compiled/babel-types/index.js +176 -176
  23. package/dinou/compiled/chokidar/index.js +4 -4
  24. package/dinou/compiled/commander/index.js +15 -15
  25. package/dinou/compiled/copy-webpack-plugin/index.js +433 -425
  26. package/dinou/compiled/css-loader/index.js +289 -289
  27. package/dinou/compiled/css-loader/runtime/api.js +85 -0
  28. package/dinou/compiled/css-loader/runtime/getUrl.js +26 -0
  29. package/dinou/compiled/css-loader/runtime/noSourceMaps.js +5 -0
  30. package/dinou/compiled/css-loader/runtime/package.json +3 -0
  31. package/dinou/compiled/css-loader/runtime/sourceMaps.js +16 -0
  32. package/dinou/compiled/fast-glob/index.js +5810 -0
  33. package/dinou/compiled/generic-names/index.js +16 -16
  34. package/dinou/compiled/mini-css-extract-plugin/{chunk-YYEJWYXE.js → chunk-R6DP5ZYM.js} +36 -36
  35. package/dinou/compiled/mini-css-extract-plugin/hmr/hotModuleReplacement.js +253 -0
  36. package/dinou/compiled/mini-css-extract-plugin/hmr/normalize-url.js +39 -0
  37. package/dinou/compiled/mini-css-extract-plugin/hmr/package.json +3 -0
  38. package/dinou/compiled/mini-css-extract-plugin/index.js +1 -1
  39. package/dinou/compiled/mini-css-extract-plugin/loader.js +5 -5
  40. package/dinou/compiled/postcss/index.js +83 -83
  41. package/dinou/compiled/postcss-import/index.js +147 -117
  42. package/dinou/compiled/postcss-modules/index.js +216 -216
  43. package/dinou/compiled/resolve/index.js +1224 -0
  44. package/dinou/compiled/rollup-plugin-babel/index.js +753 -731
  45. package/dinou/compiled/rollup-plugin-commonjs/index.js +43 -38
  46. package/dinou/compiled/rollup-plugin-delete/index.js +615 -253
  47. package/dinou/compiled/rollup-plugin-json/index.js +22 -18
  48. package/dinou/compiled/rollup-plugin-node-resolve/index.js +130 -100
  49. package/dinou/compiled/rollup-plugin-postcss/index.js +3299 -3277
  50. package/dinou/compiled/rollup-plugin-replace/index.js +34 -29
  51. package/dinou/compiled/tailwindcss-postcss/index.js +214 -211
  52. package/dinou/compiled/vendors-manifest.json +59 -83
  53. package/dinou/core/asset-extensions.js +482 -33
  54. package/dinou/core/bailout-proxy.js +21 -2
  55. package/dinou/core/build-static-pages.js +523 -371
  56. package/dinou/core/builtin-asset-extensions.js +63 -0
  57. package/dinou/core/cache-revalidate.js +449 -256
  58. package/dinou/core/cache-slot.js +289 -79
  59. package/dinou/core/client-webpack.jsx +470 -39
  60. package/dinou/core/client.jsx +402 -39
  61. package/dinou/core/config.js +3 -0
  62. package/dinou/core/copy-static-dirs.js +98 -0
  63. package/dinou/core/createScopedName.js +22 -4
  64. package/dinou/core/dinou-paths.js +1 -1
  65. package/dinou/core/discover-layouts.js +200 -0
  66. package/dinou/core/edge-ssr.js +1 -1
  67. package/dinou/core/ensure-vendor-mode.js +78 -19
  68. package/dinou/core/env.js +544 -0
  69. package/dinou/core/export-static.js +41 -9
  70. package/dinou/core/generate-route-types.js +989 -95
  71. package/dinou/core/generate-static-rsc.js +5 -6
  72. package/dinou/core/generate-static.js +45 -6
  73. package/dinou/core/get-abs-path-with-ext.js +217 -75
  74. package/dinou/core/get-asset-from-manifest.js +19 -8
  75. package/dinou/core/get-error-jsx.js +4 -5
  76. package/dinou/core/get-file-path-and-dynamic-params.js +561 -338
  77. package/dinou/core/get-jsx.js +220 -53
  78. package/dinou/core/handler.js +1647 -466
  79. package/dinou/core/import-module.js +7 -0
  80. package/dinou/core/layout-functions.js +72 -13
  81. package/dinou/core/link.jsx +34 -6
  82. package/dinou/core/load-dinou-config.js +520 -89
  83. package/dinou/core/manifest-provider.js +4 -14
  84. package/dinou/core/navigation-utils.js +141 -3
  85. package/dinou/core/navigation.js +134 -50
  86. package/dinou/core/node-middleware-runner.js +165 -0
  87. package/dinou/core/postcss-pipeline.js +236 -0
  88. package/dinou/core/ppr-context.js +114 -22
  89. package/dinou/core/ppr-runtime.js +391 -127
  90. package/dinou/core/redirect.jsx +1 -1
  91. package/dinou/core/render-jsx-to-client-jsx.js +216 -48
  92. package/dinou/core/request-context.js +59 -3
  93. package/dinou/core/resolve-contextual-dinou.js +523 -0
  94. package/dinou/core/resolve-setup-files.js +85 -27
  95. package/dinou/core/revalidating.js +10 -2
  96. package/dinou/core/route-generator.js +6 -2
  97. package/dinou/core/rsc-renderer.js +4 -1
  98. package/dinou/core/run-ssg.js +6 -0
  99. package/dinou/core/runtime-env.js +265 -0
  100. package/dinou/core/server-css-modules-plugin.js +155 -0
  101. package/dinou/core/server-function-proxy-webpack.js +27 -2
  102. package/dinou/core/server-function-proxy.js +27 -2
  103. package/dinou/core/slot.js +47 -2
  104. package/dinou/core/static-assets.js +503 -0
  105. package/dinou/core/storage-adapter.js +452 -24
  106. package/dinou/core/transform-server-functions.js +272 -24
  107. package/dinou/core/tsconfig-aliases.js +964 -0
  108. package/dinou/core/uneject.js +640 -0
  109. package/dinou/core/url-resolver.js +1 -1
  110. package/dinou/core/vfs.js +11 -2
  111. package/dinou/css.d.ts +4 -1
  112. package/dinou/deno/build.js +205 -153
  113. package/dinou/deno/seed-kv.js +4 -0
  114. package/dinou/env.d.ts +22 -2
  115. package/dinou/esbuild/build.js +40 -50
  116. package/dinou/esbuild/dev.js +33 -7
  117. package/dinou/esbuild/helpers-esbuild/get-config-esbuild-prod.js +41 -16
  118. package/dinou/esbuild/helpers-esbuild/get-config-esbuild.js +51 -49
  119. package/dinou/esbuild/helpers-esbuild/get-esbuild-entries.js +73 -60
  120. package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +35 -16
  121. package/dinou/esbuild/helpers-esbuild/write.js +13 -3
  122. package/dinou/esbuild/plugins-esbuild/assets-plugin.js +100 -238
  123. package/dinou/esbuild/plugins-esbuild/babel-react-compiler-plugin.js +73 -29
  124. package/dinou/esbuild/plugins-esbuild/css-processor-plugin.js +283 -91
  125. package/dinou/esbuild/plugins-esbuild/manifest-generator-plugin.js +3 -5
  126. package/dinou/esbuild/plugins-esbuild/react-client-manifest-plugin.js +9 -3
  127. package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +4 -3
  128. package/dinou/esbuild/plugins-esbuild/stable-chunk-names-and-maps-plugin.js +10 -1
  129. package/dinou/esbuild/plugins-postcss/postcss-extract-plugin.js +84 -21
  130. package/dinou/esbuild/react-refresh/esm-hmr/client.js +31 -10
  131. package/dinou/esbuild/react-refresh/esm-hmr-plugin.js +48 -18
  132. package/dinou/esbuild/react-refresh/is-react-refresh-boundary.js +9 -1
  133. package/dinou/esbuild/react-refresh/react-refresh-runtime.js +13 -7
  134. package/dinou/index.d.ts +1028 -88
  135. package/dinou/node/build.js +138 -74
  136. package/dinou/node/bundle-dual-engine.js +393 -131
  137. package/dinou/node/dev.js +1005 -275
  138. package/dinou/package.json +11 -34
  139. package/dinou/rollup/postcss.config.js +3 -4
  140. package/dinou/rollup/react-refresh/esm-hmr/client.js +31 -10
  141. package/dinou/rollup/react-refresh/react-refresh-wrap-modules.js +16 -13
  142. package/dinou/rollup/react-refresh/rollup-plugin-esm-hmr.js +53 -7
  143. package/dinou/rollup/rollup-plugins/dinou-asset-plugin.js +58 -20
  144. package/dinou/rollup/rollup-plugins/manifest-generator-plugin.js +16 -7
  145. package/dinou/rollup/rollup-plugins/rollup-plugin-react-client-manifest.js +118 -42
  146. package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +48 -36
  147. package/dinou/rollup/rollup.config.js +174 -101
  148. package/dinou/scripts/build-vendors.js +123 -33
  149. package/dinou/server.d.ts +126 -15
  150. package/dinou/webpack/helpers/get-webpack-entries.js +87 -41
  151. package/dinou/webpack/helpers/patch-webpack-internals.js +93 -1
  152. package/dinou/webpack/loaders/server-functions-loader.js +1 -1
  153. package/dinou/webpack/plugins/manifest-generator-plugin.js +27 -7
  154. package/dinou/webpack/plugins/server-functions-plugin.js +39 -12
  155. package/dinou/webpack/plugins/webpack-memory-plugin.js +25 -12
  156. package/dinou/webpack/postcss.config.js +3 -4
  157. package/dinou/webpack/webpack.config.js +408 -132
  158. package/eject.js +6 -224
  159. package/package.json +7 -9
  160. package/dinou/compiled/cross-env/index.js +0 -661
  161. package/dinou/compiled/dotenv/index.js +0 -375
  162. package/dinou/compiled/esbuild-copy-static-files/index.js +0 -63
  163. package/dinou/compiled/esbuild-tsconfig-paths/index.js +0 -1314
  164. package/dinou/compiled/rollup-plugin-copy/index.js +0 -12066
  165. package/dinou/compiled/rollup-plugin-tsconfig-paths/index.js +0 -569
  166. package/dinou/compiled/tsconfig-paths-webpack-plugin/index.js +0 -3956
@@ -9,6 +9,7 @@ class StorageAdapter {
9
9
  constructor() {
10
10
  this._deletedKeys = new Set();
11
11
  this._invalidatedTags = new Map();
12
+ this.hydrateFromDisk = true;
12
13
  }
13
14
 
14
15
  markTagInvalidated(tag) {
@@ -17,14 +18,22 @@ class StorageAdapter {
17
18
  }
18
19
  }
19
20
 
21
+ async markTagInvalidatedAsync(tag) {
22
+ this.markTagInvalidated(tag);
23
+ }
24
+
20
25
  getTagInvalidatedAt(tag) {
21
26
  return tag ? this._invalidatedTags.get(String(tag)) || 0 : 0;
22
27
  }
23
28
 
29
+ async getTagInvalidatedAtAsync(tag) {
30
+ return this.getTagInvalidatedAt(tag);
31
+ }
32
+
24
33
  _normalizeTrackKey(key) {
25
34
  return String(key || "")
26
- .replace(/^\/+/, "")
27
35
  .replace(/\\/g, "/")
36
+ .replace(/^\/+/, "")
28
37
  .replace(/\/+$/, "");
29
38
  }
30
39
 
@@ -38,6 +47,11 @@ class StorageAdapter {
38
47
  } else if (!k.includes(".")) {
39
48
  this._deletedKeys.add(k ? `${k}/index.html` : "index.html");
40
49
  }
50
+ if (k.endsWith("/slot.json")) {
51
+ this._deletedKeys.add(k.slice(0, -10) + "/slot.metadata.json");
52
+ } else if (k.endsWith("/slot.metadata.json")) {
53
+ this._deletedKeys.add(k.slice(0, -19) + "/slot.json");
54
+ }
41
55
  }
42
56
 
43
57
  _unmarkDeleted(key) {
@@ -50,6 +64,11 @@ class StorageAdapter {
50
64
  } else if (!k.includes(".")) {
51
65
  this._deletedKeys.delete(k ? `${k}/index.html` : "index.html");
52
66
  }
67
+ if (k.endsWith("/slot.json")) {
68
+ this._deletedKeys.delete(k.slice(0, -10) + "/slot.metadata.json");
69
+ } else if (k.endsWith("/slot.metadata.json")) {
70
+ this._deletedKeys.delete(k.slice(0, -19) + "/slot.json");
71
+ }
53
72
  }
54
73
 
55
74
  isDeleted(key) {
@@ -57,6 +76,10 @@ class StorageAdapter {
57
76
  return this._deletedKeys.has(k);
58
77
  }
59
78
 
79
+ async isKeyDeleted(key) {
80
+ return this.isDeleted(key);
81
+ }
82
+
60
83
  async get(key) {
61
84
  throw new Error("StorageAdapter.get must be implemented");
62
85
  }
@@ -83,8 +106,8 @@ class FileSystemStorage extends StorageAdapter {
83
106
 
84
107
  _resolve(key) {
85
108
  let cleanKey = String(key || "")
86
- .replace(/^\/+/, "")
87
109
  .replace(/\\/g, "/")
110
+ .replace(/^\/+/, "")
88
111
  .replace(/\/+$/, "");
89
112
  if (!cleanKey) {
90
113
  return path.join(this.baseDir, "index.html");
@@ -118,7 +141,10 @@ class FileSystemStorage extends StorageAdapter {
118
141
 
119
142
  // Check companion metadata if available
120
143
  let metadata = null;
121
- const isLayoutKey = cleanKey.includes("layout.rsc") || cleanKey.includes("layout.metadata.json");
144
+ const isLayoutKey =
145
+ cleanKey.includes("layout.rsc") ||
146
+ cleanKey.includes("layout.metadata.json") ||
147
+ cleanKey.includes("shell.layout.json");
122
148
  const normSlotKey = cleanKey.replace(/^\/+/, "");
123
149
  const isSlotKey = normSlotKey.startsWith("slots/") || normSlotKey.endsWith("/slot.json") || normSlotKey.endsWith("/slot.metadata.json");
124
150
  const metaFileName = isLayoutKey
@@ -151,7 +177,10 @@ class FileSystemStorage extends StorageAdapter {
151
177
  const cleanKey = String(key || "").replace(/\\/g, "/");
152
178
  const isMetaJson = cleanKey.endsWith("metadata.json") || path.basename(targetPath).endsWith("metadata.json");
153
179
  if (metadata && !isMetaJson) {
154
- const isLayoutKey = cleanKey.includes("layout.rsc") || cleanKey.includes("layout.metadata.json");
180
+ const isLayoutKey =
181
+ cleanKey.includes("layout.rsc") ||
182
+ cleanKey.includes("layout.metadata.json") ||
183
+ cleanKey.includes("shell.layout.json");
155
184
  const normSlotKey = cleanKey.replace(/^\/+/, "");
156
185
  const isSlotKey = normSlotKey.startsWith("slots/") || normSlotKey.endsWith("/slot.json") || normSlotKey.endsWith("/slot.metadata.json");
157
186
  const metaFileName = isLayoutKey
@@ -195,7 +224,9 @@ class FileSystemStorage extends StorageAdapter {
195
224
  const effectivePrefix = prefix === "dinou_cache" ? "" : prefix;
196
225
  const results = [];
197
226
  const walk = (dir, rel = "") => {
198
- const entries = fs.readdirSync(dir, { withFileTypes: true });
227
+ const entries = fs
228
+ .readdirSync(dir, { withFileTypes: true })
229
+ .sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
199
230
  for (const entry of entries) {
200
231
  const entryRel = rel ? `${rel}/${entry.name}` : entry.name;
201
232
  if (entry.isDirectory()) {
@@ -223,7 +254,7 @@ class CloudflareKVStorage extends StorageAdapter {
223
254
  }
224
255
 
225
256
  _cleanKey(key) {
226
- return String(key).replace(/^\/+/, "").replace(/\\/g, "/").replace(/\/+$/, "");
257
+ return String(key).replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+$/, "");
227
258
  }
228
259
 
229
260
  async get(key) {
@@ -258,7 +289,14 @@ class CloudflareKVStorage extends StorageAdapter {
258
289
  if (!this.kv) return false;
259
290
  const cleanKey = this._cleanKey(key);
260
291
  const val = await this.kv.get(cleanKey);
261
- return val !== null;
292
+ if (val !== null) return true;
293
+ if (!cleanKey.includes(".")) {
294
+ const fallbackVal = await this.kv.get(
295
+ cleanKey ? `${cleanKey}/index.html` : "index.html"
296
+ );
297
+ return fallbackVal !== null;
298
+ }
299
+ return false;
262
300
  }
263
301
 
264
302
  async delete(key) {
@@ -284,9 +322,28 @@ class CloudflareKVStorage extends StorageAdapter {
284
322
  if (!this.kv || typeof this.kv.list !== "function") return [];
285
323
  try {
286
324
  const effectivePrefix = prefix === "dinou_cache" ? "" : prefix;
287
- const options = effectivePrefix ? { prefix: effectivePrefix } : undefined;
288
- const res = await this.kv.list(options);
289
- return res && res.keys ? res.keys.map((k) => k.name) : [];
325
+ const results = [];
326
+ let cursor = undefined;
327
+ while (true) {
328
+ const options = {};
329
+ if (effectivePrefix) options.prefix = effectivePrefix;
330
+ if (cursor) options.cursor = cursor;
331
+ const res = await this.kv.list(
332
+ Object.keys(options).length > 0 ? options : undefined
333
+ );
334
+ if (res && Array.isArray(res.keys)) {
335
+ for (const k of res.keys) {
336
+ if (k && typeof k.name === "string") {
337
+ results.push(k.name);
338
+ }
339
+ }
340
+ }
341
+ if (!res || res.list_complete !== false || !res.cursor) {
342
+ break;
343
+ }
344
+ cursor = res.cursor;
345
+ }
346
+ return results;
290
347
  } catch (e) {
291
348
  return [];
292
349
  }
@@ -297,13 +354,14 @@ class CloudflareKVStorage extends StorageAdapter {
297
354
  * In-memory storage adapter.
298
355
  */
299
356
  class MemoryStorage extends StorageAdapter {
300
- constructor() {
357
+ constructor(options = {}) {
301
358
  super();
302
359
  this.store = new Map();
360
+ this.hydrateFromDisk = Boolean(options && options.hydrateFromDisk);
303
361
  }
304
362
 
305
363
  _cleanKey(key) {
306
- return String(key).replace(/^\/+/, "").replace(/\\/g, "/").replace(/\/+$/, "");
364
+ return String(key || "").replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+$/, "");
307
365
  }
308
366
 
309
367
  async get(key) {
@@ -360,22 +418,114 @@ class MemoryStorage extends StorageAdapter {
360
418
 
361
419
  /**
362
420
  * Redis storage adapter for multi-server / clustered deployments.
363
- * Shares ISR / ISG cache across multiple Node instances.
421
+ * Shares ISR / ISG cache and invalidation tombstones across multiple Node/Bun instances.
364
422
  */
365
423
  class RedisStorage extends StorageAdapter {
366
- constructor(redisClient, prefix = "dinou:cache:") {
424
+ constructor(redisClient, prefixOrOptions = "dinou:cache:") {
367
425
  super();
368
426
  this.redis = redisClient;
369
- this.prefix = prefix;
427
+ this.prefix =
428
+ typeof prefixOrOptions === "string"
429
+ ? prefixOrOptions
430
+ : prefixOrOptions && typeof prefixOrOptions.prefix === "string"
431
+ ? prefixOrOptions.prefix
432
+ : "dinou:cache:";
433
+ if (
434
+ prefixOrOptions &&
435
+ typeof prefixOrOptions === "object" &&
436
+ typeof prefixOrOptions.hydrateFromDisk === "boolean"
437
+ ) {
438
+ this.hydrateFromDisk = prefixOrOptions.hydrateFromDisk;
439
+ }
370
440
  }
371
441
 
372
- _k(key) {
373
- return this.prefix + key.replace(/^\/+/, "").replace(/\\/g, "/");
442
+ _cleanKey(key) {
443
+ return String(key || "")
444
+ .replace(/\\/g, "/")
445
+ .replace(/^\/+/, "")
446
+ .replace(/\/+$/, "");
374
447
  }
375
448
 
376
- async get(key) {
377
- if (!this.redis) return null;
378
- const raw = await this.redis.get(this._k(key));
449
+ _k(cleanKey) {
450
+ return this.prefix + cleanKey;
451
+ }
452
+
453
+ _tombstoneKey(cleanKey) {
454
+ return `${this.prefix}__tombstone__:${cleanKey}`;
455
+ }
456
+
457
+ _tagInvKey(tag) {
458
+ return `${this.prefix}__tag_inv__:${String(tag)}`;
459
+ }
460
+
461
+ _companionKeys(cleanKey) {
462
+ const companions = [];
463
+ if (cleanKey.endsWith("/index.html")) {
464
+ companions.push(cleanKey.slice(0, -11));
465
+ } else if (cleanKey === "index.html") {
466
+ companions.push("");
467
+ } else if (!cleanKey.includes(".")) {
468
+ companions.push(cleanKey ? `${cleanKey}/index.html` : "index.html");
469
+ }
470
+ if (cleanKey.endsWith("/slot.json")) {
471
+ companions.push(cleanKey.slice(0, -10) + "/slot.metadata.json");
472
+ } else if (cleanKey.endsWith("/slot.metadata.json")) {
473
+ companions.push(cleanKey.slice(0, -19) + "/slot.json");
474
+ }
475
+ return companions;
476
+ }
477
+
478
+ markTagInvalidated(tag) {
479
+ super.markTagInvalidated(tag);
480
+ if (tag && this.redis && typeof this.redis.set === "function") {
481
+ const ts = String(this.getTagInvalidatedAt(tag) || Date.now());
482
+ Promise.resolve(this.redis.set(this._tagInvKey(tag), ts)).catch(() => {});
483
+ }
484
+ }
485
+
486
+ async markTagInvalidatedAsync(tag) {
487
+ super.markTagInvalidated(tag);
488
+ if (tag && this.redis && typeof this.redis.set === "function") {
489
+ const ts = String(this.getTagInvalidatedAt(tag) || Date.now());
490
+ try {
491
+ await this.redis.set(this._tagInvKey(tag), ts);
492
+ } catch (e) {}
493
+ }
494
+ }
495
+
496
+ async getTagInvalidatedAtAsync(tag) {
497
+ const localTs = this.getTagInvalidatedAt(tag);
498
+ if (!tag || !this.redis || typeof this.redis.get !== "function") {
499
+ return localTs;
500
+ }
501
+ try {
502
+ const remoteRaw = await this.redis.get(this._tagInvKey(tag));
503
+ const remoteTs = remoteRaw ? Number(remoteRaw) || 0 : 0;
504
+ return Math.max(localTs, remoteTs);
505
+ } catch (e) {
506
+ return localTs;
507
+ }
508
+ }
509
+
510
+ async isKeyDeleted(key) {
511
+ if (this.isDeleted(key)) return true;
512
+ if (!this.redis) return false;
513
+ const cleanKey = this._cleanKey(key);
514
+ try {
515
+ if (typeof this.redis.exists === "function") {
516
+ const ex = await this.redis.exists(this._tombstoneKey(cleanKey));
517
+ return Boolean(ex && Number(ex) > 0);
518
+ }
519
+ if (typeof this.redis.get === "function") {
520
+ const val = await this.redis.get(this._tombstoneKey(cleanKey));
521
+ return val !== null && val !== undefined;
522
+ }
523
+ } catch (e) {}
524
+ return false;
525
+ }
526
+
527
+ async _readRaw(cleanKey) {
528
+ const raw = await this.redis.get(this._k(cleanKey));
379
529
  if (!raw) return null;
380
530
  try {
381
531
  return JSON.parse(raw);
@@ -384,22 +534,150 @@ class RedisStorage extends StorageAdapter {
384
534
  }
385
535
  }
386
536
 
537
+ async get(key) {
538
+ if (!this.redis) return null;
539
+ const cleanKey = this._cleanKey(key);
540
+ let item = await this._readRaw(cleanKey);
541
+ if (!item && !cleanKey.includes(".")) {
542
+ item = await this._readRaw(cleanKey ? `${cleanKey}/index.html` : "index.html");
543
+ }
544
+ return item;
545
+ }
546
+
387
547
  async set(key, content, metadata = null) {
388
548
  this._unmarkDeleted(key);
389
549
  if (!this.redis) return;
550
+ const cleanKey = this._cleanKey(key);
390
551
  const value = JSON.stringify({ content, metadata });
391
- await this.redis.set(this._k(key), value);
552
+ await this.redis.set(this._k(cleanKey), value);
553
+ const tombstonesToClear = [this._tombstoneKey(cleanKey)];
554
+ if (cleanKey.endsWith("/index.html")) {
555
+ const folderKey = cleanKey.slice(0, -11);
556
+ await this.redis.set(this._k(folderKey), value);
557
+ tombstonesToClear.push(this._tombstoneKey(folderKey));
558
+ } else if (cleanKey === "index.html") {
559
+ await this.redis.set(this._k(""), value);
560
+ tombstonesToClear.push(this._tombstoneKey(""));
561
+ } else if (!cleanKey.includes(".")) {
562
+ tombstonesToClear.push(
563
+ this._tombstoneKey(cleanKey ? `${cleanKey}/index.html` : "index.html")
564
+ );
565
+ }
566
+ if (cleanKey.endsWith("/slot.json")) {
567
+ tombstonesToClear.push(this._tombstoneKey(cleanKey.slice(0, -10) + "/slot.metadata.json"));
568
+ } else if (cleanKey.endsWith("/slot.metadata.json")) {
569
+ tombstonesToClear.push(this._tombstoneKey(cleanKey.slice(0, -19) + "/slot.json"));
570
+ }
571
+ if (typeof this.redis.del === "function") {
572
+ await Promise.all(
573
+ tombstonesToClear.map((tk) => Promise.resolve(this.redis.del(tk)).catch(() => {}))
574
+ );
575
+ }
392
576
  }
393
577
 
394
578
  async has(key) {
395
579
  if (!this.redis) return false;
396
- return (await this.redis.exists(this._k(key))) === 1;
580
+ const cleanKey = this._cleanKey(key);
581
+ const existsDirect = await this.redis.exists(this._k(cleanKey));
582
+ if (existsDirect && Number(existsDirect) > 0) return true;
583
+ if (!cleanKey.includes(".")) {
584
+ const fallback = cleanKey ? `${cleanKey}/index.html` : "index.html";
585
+ const existsFallback = await this.redis.exists(this._k(fallback));
586
+ return Boolean(existsFallback && Number(existsFallback) > 0);
587
+ }
588
+ return false;
397
589
  }
398
590
 
399
591
  async delete(key) {
400
592
  this._markDeleted(key);
401
593
  if (!this.redis) return;
402
- await this.redis.del(this._k(key));
594
+ const cleanKey = this._cleanKey(key);
595
+ const targets = [cleanKey, ...this._companionKeys(cleanKey)];
596
+ await Promise.all(
597
+ targets.map(async (tk) => {
598
+ try {
599
+ await this.redis.del(this._k(tk));
600
+ } catch (e) {}
601
+ try {
602
+ if (typeof this.redis.set === "function") {
603
+ await this.redis.set(this._tombstoneKey(tk), String(Date.now()));
604
+ }
605
+ } catch (e) {}
606
+ })
607
+ );
608
+ }
609
+
610
+ async keys(prefix = "") {
611
+ if (!this.redis) return [];
612
+ const normPrefix =
613
+ !prefix || prefix === "dinou_cache"
614
+ ? ""
615
+ : String(prefix).replace(/\\/g, "/").replace(/^\/+/, "");
616
+ const matchPattern = `${this.prefix}${normPrefix}*`;
617
+ const rawKeys = [];
618
+
619
+ try {
620
+ if (typeof this.redis.scanIterator === "function") {
621
+ for await (const item of this.redis.scanIterator({
622
+ MATCH: matchPattern,
623
+ COUNT: 100,
624
+ })) {
625
+ if (Array.isArray(item)) rawKeys.push(...item);
626
+ else if (typeof item === "string") rawKeys.push(item);
627
+ }
628
+ } else if (typeof this.redis.scanStream === "function") {
629
+ for await (const batch of this.redis.scanStream({
630
+ match: matchPattern,
631
+ count: 100,
632
+ })) {
633
+ if (Array.isArray(batch)) rawKeys.push(...batch);
634
+ else if (typeof batch === "string") rawKeys.push(batch);
635
+ }
636
+ } else if (typeof this.redis.keys === "function") {
637
+ const list = await this.redis.keys(matchPattern);
638
+ if (Array.isArray(list)) rawKeys.push(...list);
639
+ } else if (typeof this.redis.scan === "function") {
640
+ let cursor = "0";
641
+ do {
642
+ let scanRes;
643
+ try {
644
+ scanRes = await this.redis.scan(cursor, "MATCH", matchPattern, "COUNT", 100);
645
+ } catch (e) {
646
+ scanRes = await this.redis.scan(Number(cursor) || 0, {
647
+ MATCH: matchPattern,
648
+ COUNT: 100,
649
+ });
650
+ }
651
+ if (Array.isArray(scanRes)) {
652
+ cursor = String(scanRes[0]);
653
+ if (Array.isArray(scanRes[1])) rawKeys.push(...scanRes[1]);
654
+ } else if (scanRes && typeof scanRes === "object") {
655
+ cursor = String(scanRes.cursor);
656
+ if (Array.isArray(scanRes.keys)) rawKeys.push(...scanRes.keys);
657
+ } else {
658
+ break;
659
+ }
660
+ } while (cursor !== "0");
661
+ }
662
+ } catch (e) {
663
+ return [];
664
+ }
665
+
666
+ const result = new Set();
667
+ for (const fullKey of rawKeys) {
668
+ if (typeof fullKey !== "string" || !fullKey.startsWith(this.prefix)) continue;
669
+ const stripped = fullKey.slice(this.prefix.length);
670
+ if (
671
+ stripped.startsWith("__tombstone__:") ||
672
+ stripped.startsWith("__tag_inv__:")
673
+ ) {
674
+ continue;
675
+ }
676
+ if (!normPrefix || stripped.startsWith(normPrefix)) {
677
+ result.add(stripped);
678
+ }
679
+ }
680
+ return Array.from(result);
403
681
  }
404
682
  }
405
683
 
@@ -431,7 +709,10 @@ class DenoKVStorage extends StorageAdapter {
431
709
  }
432
710
 
433
711
  _cleanKey(key) {
434
- return String(key).replace(/^\/+/, "").replace(/\\/g, "/");
712
+ return String(key || "")
713
+ .replace(/\\/g, "/")
714
+ .replace(/^\/+/, "")
715
+ .replace(/\/+$/, "");
435
716
  }
436
717
 
437
718
  async get(key) {
@@ -616,6 +897,149 @@ function setStorageAdapter(adapter) {
616
897
  globalThis[DINOU_STORAGE_KEY] = adapter;
617
898
  }
618
899
 
900
+ function hasExplicitStorageAdapter() {
901
+ return Boolean(globalThis[DINOU_STORAGE_KEY]);
902
+ }
903
+
904
+ /**
905
+ * Hydrates a missing build-time static artifact from local `.dinou/dist2` into
906
+ * non-FileSystem distributed/KV adapters (such as RedisStorage or DenoKVStorage)
907
+ * when `storage.hydrateFromDisk !== false` and the key has not been deleted/invalidated.
908
+ */
909
+ async function readDist2Fallback(storage, key, knownTags = []) {
910
+ if (!storage || !key || storage.baseDir || storage.hydrateFromDisk === false) {
911
+ return null;
912
+ }
913
+ try {
914
+ const isDeleted =
915
+ typeof storage.isKeyDeleted === "function"
916
+ ? await storage.isKeyDeleted(key)
917
+ : typeof storage.isDeleted === "function" && storage.isDeleted(key);
918
+ if (isDeleted) {
919
+ return null;
920
+ }
921
+
922
+ const cwd =
923
+ typeof process !== "undefined" && typeof process.cwd === "function"
924
+ ? process.cwd()
925
+ : typeof Deno !== "undefined" && typeof Deno.cwd === "function"
926
+ ? Deno.cwd()
927
+ : null;
928
+ if (!cwd) return null;
929
+
930
+ const dist2Dir = path.resolve(cwd, ".dinou/dist2");
931
+ if (!fs.existsSync(dist2Dir)) return null;
932
+
933
+ const dist2Fs = new FileSystemStorage(dist2Dir);
934
+ const resolvedPath = path.resolve(dist2Fs._resolve(key));
935
+ if (
936
+ resolvedPath !== dist2Dir &&
937
+ !resolvedPath.startsWith(dist2Dir + path.sep)
938
+ ) {
939
+ return null;
940
+ }
941
+
942
+ const item = await dist2Fs.get(key);
943
+ if (!item || !item.content) return null;
944
+
945
+ const normKey = String(key || "")
946
+ .replace(/^\/+/, "")
947
+ .replace(/\\/g, "/")
948
+ .replace(/\/+$/, "");
949
+ const writeKey = normKey || "index.html";
950
+ const isMetaFile = writeKey.endsWith("metadata.json");
951
+ const isShellJson =
952
+ writeKey.endsWith("shell.page.json") ||
953
+ writeKey.endsWith("shell.layout.json");
954
+
955
+ if (!isMetaFile) {
956
+ const generatedAt = Number(
957
+ (writeKey.endsWith("shell.page.json") && item.metadata?.pageGeneratedAt) ||
958
+ (writeKey.endsWith("shell.layout.json") && item.metadata?.layoutGeneratedAt) ||
959
+ item.metadata?.generatedAt ||
960
+ 0
961
+ );
962
+ const slotIdFromKey = writeKey.match(/^slots\/([^/]+)\//)?.[1];
963
+ const combinedTags = new Set([
964
+ ...(Array.isArray(item.metadata?.tags) ? item.metadata.tags : []),
965
+ ...(!isShellJson && Array.isArray(item.metadata?.slotTags)
966
+ ? item.metadata.slotTags
967
+ : []),
968
+ ...(item.metadata?.slotId ? [item.metadata.slotId] : []),
969
+ ...(slotIdFromKey ? [slotIdFromKey] : []),
970
+ ...(Array.isArray(knownTags) ? knownTags : []),
971
+ ]);
972
+ if (combinedTags.size > 0) {
973
+ for (const t of combinedTags) {
974
+ if (!t) continue;
975
+ const invAt =
976
+ typeof storage.getTagInvalidatedAtAsync === "function"
977
+ ? await storage.getTagInvalidatedAtAsync(t)
978
+ : typeof storage.getTagInvalidatedAt === "function"
979
+ ? storage.getTagInvalidatedAt(t)
980
+ : 0;
981
+ if (invAt > 0 && invAt >= generatedAt) {
982
+ return null;
983
+ }
984
+ }
985
+ }
986
+ }
987
+
988
+ try {
989
+ const shouldPassMetadata =
990
+ !isMetaFile &&
991
+ (writeKey === "index.html" ||
992
+ writeKey.endsWith("/index.html") ||
993
+ writeKey.endsWith("shell.html") ||
994
+ writeKey.endsWith("layout.rsc") ||
995
+ writeKey.endsWith("/slot.json"));
996
+ await storage.set(
997
+ writeKey,
998
+ item.content,
999
+ shouldPassMetadata ? item.metadata || null : null
1000
+ );
1001
+ if (
1002
+ item.metadata &&
1003
+ typeof item.metadata === "object" &&
1004
+ !isMetaFile
1005
+ ) {
1006
+ const isLayoutKey =
1007
+ writeKey.endsWith("layout.rsc") ||
1008
+ writeKey.endsWith("shell.layout.json");
1009
+ const isSlotKey =
1010
+ writeKey.startsWith("slots/") || writeKey.endsWith("/slot.json");
1011
+ const dirPrefix = writeKey.includes("/")
1012
+ ? writeKey.slice(0, writeKey.lastIndexOf("/"))
1013
+ : "";
1014
+ const metaFileName = isLayoutKey
1015
+ ? "layout.metadata.json"
1016
+ : isSlotKey
1017
+ ? "slot.metadata.json"
1018
+ : "metadata.json";
1019
+ const companionMetaKey = dirPrefix
1020
+ ? `${dirPrefix}/${metaFileName}`
1021
+ : metaFileName;
1022
+ const metaDeleted =
1023
+ typeof storage.isKeyDeleted === "function"
1024
+ ? await storage.isKeyDeleted(companionMetaKey)
1025
+ : typeof storage.isDeleted === "function" &&
1026
+ storage.isDeleted(companionMetaKey);
1027
+ const alreadyHasMeta =
1028
+ typeof storage.has === "function"
1029
+ ? await storage.has(companionMetaKey)
1030
+ : false;
1031
+ if (!metaDeleted && !alreadyHasMeta) {
1032
+ await storage.set(companionMetaKey, JSON.stringify(item.metadata));
1033
+ }
1034
+ }
1035
+ } catch (e) {}
1036
+
1037
+ return item;
1038
+ } catch (e) {
1039
+ return null;
1040
+ }
1041
+ }
1042
+
619
1043
  export {
620
1044
  StorageAdapter,
621
1045
  FileSystemStorage,
@@ -625,6 +1049,8 @@ export {
625
1049
  RedisStorage,
626
1050
  getStorageAdapter,
627
1051
  setStorageAdapter,
1052
+ hasExplicitStorageAdapter,
1053
+ readDist2Fallback,
628
1054
  };
629
1055
  export default {
630
1056
  StorageAdapter,
@@ -635,4 +1061,6 @@ export default {
635
1061
  RedisStorage,
636
1062
  getStorageAdapter,
637
1063
  setStorageAdapter,
1064
+ hasExplicitStorageAdapter,
1065
+ readDist2Fallback,
638
1066
  };