dinou 9.0.2 → 9.0.3

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 (37) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +503 -9883
  3. package/dinou/bun/build.js +38 -1
  4. package/{cli.js → dinou/cli.js} +8 -7
  5. package/dinou/cloudflare/build.js +37 -0
  6. package/dinou/core/build-static-pages.js +15 -7
  7. package/dinou/core/cache-revalidate.js +909 -151
  8. package/dinou/core/cache-slot.js +728 -97
  9. package/dinou/core/client-webpack.jsx +60 -9
  10. package/dinou/core/client.jsx +60 -9
  11. package/dinou/core/edge-ssr.js +33 -5
  12. package/dinou/core/generate-static.js +109 -47
  13. package/dinou/core/get-jsx.js +333 -329
  14. package/dinou/core/handler.js +2983 -2343
  15. package/dinou/core/layout-functions.js +102 -1
  16. package/dinou/core/ppr-context.js +256 -105
  17. package/dinou/core/ppr-runtime.js +540 -267
  18. package/dinou/core/render-jsx-to-client-jsx.js +131 -71
  19. package/dinou/core/slot.js +246 -33
  20. package/dinou/core/storage-adapter.js +117 -11
  21. package/dinou/deno/build.js +38 -1
  22. package/dinou/deno/seed-kv.js +43 -11
  23. package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +242 -225
  24. package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +253 -226
  25. package/dinou/esbuild/react-refresh/esm-hmr/client.js +209 -209
  26. package/dinou/node/build.js +2 -2
  27. package/dinou/node/bundle-dual-engine.js +2 -0
  28. package/dinou/node/dev.js +2853 -2704
  29. package/dinou/package.json +5 -2
  30. package/dinou/rollup/react-refresh/esm-hmr/client.js +220 -220
  31. package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +182 -160
  32. package/dinou/server.d.ts +25 -11
  33. package/dinou/server.js +5 -0
  34. package/dinou/webpack/loaders/server-functions-loader.js +101 -94
  35. package/dinou/webpack/webpack.config.js +9 -4
  36. package/eject.js +219 -223
  37. package/package.json +2 -2
@@ -6,6 +6,57 @@ import path from "node:path";
6
6
  import fs from "node:fs";
7
7
 
8
8
  class StorageAdapter {
9
+ constructor() {
10
+ this._deletedKeys = new Set();
11
+ this._invalidatedTags = new Map();
12
+ }
13
+
14
+ markTagInvalidated(tag) {
15
+ if (tag) {
16
+ this._invalidatedTags.set(String(tag), Date.now());
17
+ }
18
+ }
19
+
20
+ getTagInvalidatedAt(tag) {
21
+ return tag ? this._invalidatedTags.get(String(tag)) || 0 : 0;
22
+ }
23
+
24
+ _normalizeTrackKey(key) {
25
+ return String(key || "")
26
+ .replace(/^\/+/, "")
27
+ .replace(/\\/g, "/")
28
+ .replace(/\/+$/, "");
29
+ }
30
+
31
+ _markDeleted(key) {
32
+ const k = this._normalizeTrackKey(key);
33
+ this._deletedKeys.add(k);
34
+ if (k.endsWith("/index.html")) {
35
+ this._deletedKeys.add(k.slice(0, -11));
36
+ } else if (k === "index.html") {
37
+ this._deletedKeys.add("");
38
+ } else if (!k.includes(".")) {
39
+ this._deletedKeys.add(k ? `${k}/index.html` : "index.html");
40
+ }
41
+ }
42
+
43
+ _unmarkDeleted(key) {
44
+ const k = this._normalizeTrackKey(key);
45
+ this._deletedKeys.delete(k);
46
+ if (k.endsWith("/index.html")) {
47
+ this._deletedKeys.delete(k.slice(0, -11));
48
+ } else if (k === "index.html") {
49
+ this._deletedKeys.delete("");
50
+ } else if (!k.includes(".")) {
51
+ this._deletedKeys.delete(k ? `${k}/index.html` : "index.html");
52
+ }
53
+ }
54
+
55
+ isDeleted(key) {
56
+ const k = this._normalizeTrackKey(key);
57
+ return this._deletedKeys.has(k);
58
+ }
59
+
9
60
  async get(key) {
10
61
  throw new Error("StorageAdapter.get must be implemented");
11
62
  }
@@ -68,7 +119,8 @@ class FileSystemStorage extends StorageAdapter {
68
119
  // Check companion metadata if available
69
120
  let metadata = null;
70
121
  const isLayoutKey = cleanKey.includes("layout.rsc") || cleanKey.includes("layout.metadata.json");
71
- const isSlotKey = cleanKey.includes("slot");
122
+ const normSlotKey = cleanKey.replace(/^\/+/, "");
123
+ const isSlotKey = normSlotKey.startsWith("slots/") || normSlotKey.endsWith("/slot.json") || normSlotKey.endsWith("/slot.metadata.json");
72
124
  const metaFileName = isLayoutKey
73
125
  ? "layout.metadata.json"
74
126
  : isSlotKey
@@ -84,6 +136,7 @@ class FileSystemStorage extends StorageAdapter {
84
136
  }
85
137
 
86
138
  async set(key, content, metadata = null) {
139
+ this._unmarkDeleted(key);
87
140
  let targetPath = this._resolve(key);
88
141
  if (fs.existsSync(targetPath) && fs.statSync(targetPath).isDirectory()) {
89
142
  targetPath = path.join(targetPath, "index.html");
@@ -99,7 +152,8 @@ class FileSystemStorage extends StorageAdapter {
99
152
  const isMetaJson = cleanKey.endsWith("metadata.json") || path.basename(targetPath).endsWith("metadata.json");
100
153
  if (metadata && !isMetaJson) {
101
154
  const isLayoutKey = cleanKey.includes("layout.rsc") || cleanKey.includes("layout.metadata.json");
102
- const isSlotKey = cleanKey.includes("slot");
155
+ const normSlotKey = cleanKey.replace(/^\/+/, "");
156
+ const isSlotKey = normSlotKey.startsWith("slots/") || normSlotKey.endsWith("/slot.json") || normSlotKey.endsWith("/slot.metadata.json");
103
157
  const metaFileName = isLayoutKey
104
158
  ? "layout.metadata.json"
105
159
  : isSlotKey
@@ -119,11 +173,13 @@ class FileSystemStorage extends StorageAdapter {
119
173
  }
120
174
 
121
175
  async delete(key) {
176
+ this._markDeleted(key);
122
177
  let targetPath = this._resolve(key);
123
178
  if (fs.existsSync(targetPath)) {
124
179
  fs.rmSync(targetPath, { force: true, recursive: true });
125
180
  }
126
- const isSlotKey = String(key || "").includes("slot");
181
+ const normSlotKey = String(key || "").replace(/^\/+/, "").replace(/\\/g, "/");
182
+ const isSlotKey = normSlotKey.startsWith("slots/") || normSlotKey.endsWith("/slot.json") || normSlotKey.endsWith("/slot.metadata.json");
127
183
  if (isSlotKey) {
128
184
  const dir = path.dirname(targetPath);
129
185
  if (fs.existsSync(dir)) {
@@ -136,6 +192,7 @@ class FileSystemStorage extends StorageAdapter {
136
192
 
137
193
  async keys(prefix = "") {
138
194
  if (!fs.existsSync(this.baseDir)) return [];
195
+ const effectivePrefix = prefix === "dinou_cache" ? "" : prefix;
139
196
  const results = [];
140
197
  const walk = (dir, rel = "") => {
141
198
  const entries = fs.readdirSync(dir, { withFileTypes: true });
@@ -145,7 +202,7 @@ class FileSystemStorage extends StorageAdapter {
145
202
  walk(path.join(dir, entry.name), entryRel);
146
203
  } else {
147
204
  const cleanRel = entryRel.replace(/\\/g, "/");
148
- if (!prefix || cleanRel.startsWith(prefix)) {
205
+ if (!effectivePrefix || cleanRel.startsWith(effectivePrefix)) {
149
206
  results.push(cleanRel);
150
207
  }
151
208
  }
@@ -184,6 +241,7 @@ class CloudflareKVStorage extends StorageAdapter {
184
241
  }
185
242
 
186
243
  async set(key, content, metadata = null) {
244
+ this._unmarkDeleted(key);
187
245
  if (!this.kv) return;
188
246
  const cleanKey = this._cleanKey(key);
189
247
  const options = metadata ? { metadata } : undefined;
@@ -191,6 +249,8 @@ class CloudflareKVStorage extends StorageAdapter {
191
249
  if (cleanKey.endsWith("/index.html")) {
192
250
  const folderKey = cleanKey.slice(0, -11);
193
251
  await this.kv.put(folderKey, content, options);
252
+ } else if (cleanKey === "index.html") {
253
+ await this.kv.put("", content, options);
194
254
  }
195
255
  }
196
256
 
@@ -202,15 +262,29 @@ class CloudflareKVStorage extends StorageAdapter {
202
262
  }
203
263
 
204
264
  async delete(key) {
265
+ this._markDeleted(key);
205
266
  if (!this.kv) return;
206
267
  const cleanKey = this._cleanKey(key);
207
268
  await this.kv.delete(cleanKey);
269
+ if (cleanKey.endsWith("/index.html")) {
270
+ await this.kv.delete(cleanKey.slice(0, -11));
271
+ } else if (cleanKey === "index.html") {
272
+ await this.kv.delete("");
273
+ } else if (!cleanKey.includes(".")) {
274
+ await this.kv.delete(cleanKey ? `${cleanKey}/index.html` : "index.html");
275
+ }
276
+ if (cleanKey.endsWith("/slot.json")) {
277
+ await this.kv.delete(cleanKey.slice(0, -10) + "/slot.metadata.json");
278
+ } else if (cleanKey.endsWith("/slot.metadata.json")) {
279
+ await this.kv.delete(cleanKey.slice(0, -19) + "/slot.json");
280
+ }
208
281
  }
209
282
 
210
283
  async keys(prefix = "") {
211
284
  if (!this.kv || typeof this.kv.list !== "function") return [];
212
285
  try {
213
- const options = prefix ? { prefix } : undefined;
286
+ const effectivePrefix = prefix === "dinou_cache" ? "" : prefix;
287
+ const options = effectivePrefix ? { prefix: effectivePrefix } : undefined;
214
288
  const res = await this.kv.list(options);
215
289
  return res && res.keys ? res.keys.map((k) => k.name) : [];
216
290
  } catch (e) {
@@ -243,11 +317,14 @@ class MemoryStorage extends StorageAdapter {
243
317
  }
244
318
 
245
319
  async set(key, content, metadata = null) {
320
+ this._unmarkDeleted(key);
246
321
  const cleanKey = this._cleanKey(key);
247
322
  this.store.set(cleanKey, { content, metadata });
248
323
  if (cleanKey.endsWith("/index.html")) {
249
324
  const folderKey = cleanKey.slice(0, -11);
250
325
  this.store.set(folderKey, { content, metadata });
326
+ } else if (cleanKey === "index.html") {
327
+ this.store.set("", { content, metadata });
251
328
  }
252
329
  }
253
330
 
@@ -257,13 +334,27 @@ class MemoryStorage extends StorageAdapter {
257
334
  }
258
335
 
259
336
  async delete(key) {
337
+ this._markDeleted(key);
260
338
  const cleanKey = this._cleanKey(key);
261
339
  this.store.delete(cleanKey);
262
- this.store.delete(cleanKey ? `${cleanKey}/index.html` : "index.html");
340
+ if (cleanKey.endsWith("/index.html")) {
341
+ this.store.delete(cleanKey.slice(0, -11));
342
+ } else if (cleanKey === "index.html") {
343
+ this.store.delete("");
344
+ } else if (!cleanKey.includes(".")) {
345
+ this.store.delete(cleanKey ? `${cleanKey}/index.html` : "index.html");
346
+ }
347
+ if (cleanKey.endsWith("/slot.json")) {
348
+ this.store.delete(cleanKey.slice(0, -10) + "/slot.metadata.json");
349
+ } else if (cleanKey.endsWith("/slot.metadata.json")) {
350
+ this.store.delete(cleanKey.slice(0, -19) + "/slot.json");
351
+ }
263
352
  }
264
353
 
265
- keys() {
266
- return Array.from(this.store.keys());
354
+ keys(prefix = "") {
355
+ const all = Array.from(this.store.keys());
356
+ if (!prefix || prefix === "dinou_cache") return all;
357
+ return all.filter((k) => k.startsWith(prefix));
267
358
  }
268
359
  }
269
360
 
@@ -294,6 +385,7 @@ class RedisStorage extends StorageAdapter {
294
385
  }
295
386
 
296
387
  async set(key, content, metadata = null) {
388
+ this._unmarkDeleted(key);
297
389
  if (!this.redis) return;
298
390
  const value = JSON.stringify({ content, metadata });
299
391
  await this.redis.set(this._k(key), value);
@@ -305,6 +397,7 @@ class RedisStorage extends StorageAdapter {
305
397
  }
306
398
 
307
399
  async delete(key) {
400
+ this._markDeleted(key);
308
401
  if (!this.redis) return;
309
402
  await this.redis.del(this._k(key));
310
403
  }
@@ -377,6 +470,7 @@ class DenoKVStorage extends StorageAdapter {
377
470
  }
378
471
 
379
472
  async set(key, content, metadata = null) {
473
+ this._unmarkDeleted(key);
380
474
  const kv = await this._getKV();
381
475
  if (!kv) return;
382
476
  const cleanKey = this._cleanKey(key);
@@ -429,6 +523,7 @@ class DenoKVStorage extends StorageAdapter {
429
523
  }
430
524
 
431
525
  async delete(key) {
526
+ this._markDeleted(key);
432
527
  const kv = await this._getKV();
433
528
  if (!kv) return;
434
529
  const cleanKey = this._cleanKey(key);
@@ -454,18 +549,29 @@ class DenoKVStorage extends StorageAdapter {
454
549
  await delKey(folderKey);
455
550
  } else if (cleanKey === "index.html") {
456
551
  await delKey("");
552
+ } else if (!cleanKey.includes(".")) {
553
+ await delKey(cleanKey ? `${cleanKey}/index.html` : "index.html");
554
+ }
555
+
556
+ if (cleanKey.endsWith("/slot.json")) {
557
+ await delKey(cleanKey.slice(0, -10) + "/slot.metadata.json");
558
+ } else if (cleanKey.endsWith("/slot.metadata.json")) {
559
+ await delKey(cleanKey.slice(0, -19) + "/slot.json");
457
560
  }
458
561
  }
459
562
 
460
- async keys(prefix = "dinou_cache") {
563
+ async keys(prefix = "") {
461
564
  const kv = await this._getKV();
462
565
  if (!kv) return [];
463
566
  try {
464
- const entries = kv.list({ prefix: [prefix] });
567
+ const entries = kv.list({ prefix: ["dinou_cache"] });
465
568
  const result = [];
466
569
  for await (const entry of entries) {
467
570
  if (entry.key && entry.key.length >= 2 && typeof entry.key[1] === "string") {
468
- result.push(entry.key[1]);
571
+ const k = entry.key[1];
572
+ if (!prefix || prefix === "dinou_cache" || k.startsWith(prefix)) {
573
+ result.push(k);
574
+ }
469
575
  }
470
576
  }
471
577
  return result;
@@ -166,6 +166,7 @@ function findClientComponents() {
166
166
  const coreCandidates = [
167
167
  ...Array.from(candidateLinkPaths),
168
168
  ...Array.from(candidateRedirectPaths),
169
+ ...Array.from(candidateSlotPaths),
169
170
  ];
170
171
  for (const f of coreCandidates) {
171
172
  if (fs.existsSync(f)) {
@@ -432,6 +433,17 @@ if (!isWebpackBuild && Object.keys(parsedClientManifest).length > 0) {
432
433
  }
433
434
  }
434
435
  }
436
+ if (slotChunkId) {
437
+ imports["dinou/core/slot.js"] = slotChunkId;
438
+ imports["dinou/core/slot"] = slotChunkId;
439
+ imports["/dinou/core/slot.js"] = slotChunkId;
440
+ imports["./dinou/core/slot.js"] = slotChunkId;
441
+ for (const sp of candidateSlotPaths) {
442
+ for (const u of generateAllUrlVariants(sp)) {
443
+ imports[u] = slotChunkId;
444
+ }
445
+ }
446
+ }
435
447
 
436
448
  importMapHtml = `<script type="importmap">{"imports":${JSON.stringify(imports)}}</script>`;
437
449
  }
@@ -447,6 +459,8 @@ function findLayoutRoutes(dir, rel = "") {
447
459
  findLayoutRoutes(path.join(dir, entry.name), rel ? `${rel}/${entry.name}` : entry.name);
448
460
  } else if (/^layout\.[jt]sx?$/.test(entry.name)) {
449
461
  discoveredLayouts.push(rel);
462
+ } else if (entry.name === "no_layout") {
463
+ discoveredLayouts.push("!" + rel);
450
464
  }
451
465
  }
452
466
  }
@@ -583,6 +597,17 @@ if (redirectChunkId) {
583
597
  }
584
598
  }
585
599
  }
600
+ if (slotChunkId) {
601
+ const slotCompIndex = clientComponents.findIndex((c) => candidateSlotPaths.has(path.resolve(c)));
602
+ if (slotCompIndex !== -1) {
603
+ addClientModule(slotChunkId, `mod_${slotCompIndex}`);
604
+ for (const sp of candidateSlotPaths) {
605
+ for (const u of generateAllUrlVariants(sp)) {
606
+ addClientModule(u, `mod_${slotCompIndex}`);
607
+ }
608
+ }
609
+ }
610
+ }
586
611
 
587
612
  serverFunctionFiles.forEach((relPath, index) => {
588
613
  const normRel = relPath.replace(/\\/g, "/");
@@ -654,6 +679,18 @@ if (redirectChunkId) {
654
679
  }
655
680
  }
656
681
  }
682
+ if (slotChunkId) {
683
+ const slotCompIndex = clientComponents.findIndex((c) => candidateSlotPaths.has(path.resolve(c)));
684
+ if (slotCompIndex !== -1) {
685
+ const fileUrl = pathToFileURL(clientComponents[slotCompIndex]).href;
686
+ addModuleMap(slotChunkId, `{ "*": { id: ${JSON.stringify(fileUrl)}, chunks: [], name: "*" } }`);
687
+ for (const sp of candidateSlotPaths) {
688
+ for (const u of generateAllUrlVariants(sp)) {
689
+ addModuleMap(u, `{ "*": { id: ${JSON.stringify(fileUrl)}, chunks: [], name: "*" } }`);
690
+ }
691
+ }
692
+ }
693
+ }
657
694
  ssrManifestCode += ` },\n serverModuleMap: {\n`;
658
695
 
659
696
  const emittedServerModuleMap = new Set();
@@ -812,7 +849,7 @@ export async function fetch(req) {
812
849
 
813
850
  if (typeof Deno !== "undefined" && typeof Deno.serve === "function" && import.meta.main) {
814
851
  const port = Number(Deno.env.get("PORT") || 3000);
815
- Deno.serve({ port }, fetch);
852
+ Deno.serve(Deno.env.get("HOST") ? { port, hostname: Deno.env.get("HOST") } : { port }, fetch);
816
853
  }
817
854
 
818
855
  export default { fetch };
@@ -25,7 +25,7 @@ if (kvUrl) {
25
25
  if (fs.existsSync(kvPath)) fs.unlinkSync(kvPath);
26
26
  if (fs.existsSync(kvPath + "-wal")) fs.unlinkSync(kvPath + "-wal");
27
27
  if (fs.existsSync(kvPath + "-shm")) fs.unlinkSync(kvPath + "-shm");
28
- } catch (e) {}
28
+ } catch (e) { }
29
29
  console.log(`💾 [Deno KV Seed] Pre-seeding local Deno KV at ${kvPath}...`);
30
30
  kv = await Deno.openKv(kvPath);
31
31
  }
@@ -66,14 +66,14 @@ async function processDir(dir, relPrefix = "") {
66
66
 
67
67
  if (entry.isDirectory()) {
68
68
  await processDir(fullPath, cleanRelPath);
69
- } else if (entry.name === "index.html") {
69
+ } else if (entry.name === "index.html" || entry.name === "shell.html") {
70
70
  const html = fs.readFileSync(fullPath, "utf8");
71
71
  let metadata = null;
72
72
  const metaPath = path.join(dir, "metadata.json");
73
73
  if (fs.existsSync(metaPath)) {
74
74
  try {
75
75
  metadata = JSON.parse(fs.readFileSync(metaPath, "utf8"));
76
- } catch (e) {}
76
+ } catch (e) { }
77
77
  }
78
78
 
79
79
  await setKvCache(kv, cleanRelPath, html, metadata);
@@ -82,26 +82,58 @@ async function processDir(dir, relPrefix = "") {
82
82
  console.log(` ✅ Cached HTML: ${cleanRelPath}`);
83
83
  }
84
84
 
85
- if (cleanRelPath.endsWith("/index.html")) {
86
- const folderKey = cleanRelPath.slice(0, -11);
87
- await setKvCache(kv, folderKey, html, metadata);
88
- } else if (cleanRelPath === "index.html") {
89
- await setKvCache(kv, "", html, metadata);
85
+ if (entry.name === "index.html") {
86
+ if (cleanRelPath.endsWith("/index.html")) {
87
+ const folderKey = cleanRelPath.slice(0, -11);
88
+ await setKvCache(kv, folderKey, html, metadata);
89
+ } else if (cleanRelPath === "index.html") {
90
+ await setKvCache(kv, "", html, metadata);
91
+ }
90
92
  }
91
93
  } else if (entry.name.endsWith("metadata.json")) {
92
94
  const metaContent = fs.readFileSync(fullPath, "utf8");
93
95
  let metadata = null;
94
96
  try {
95
97
  metadata = JSON.parse(metaContent);
96
- } catch (e) {}
98
+ } catch (e) { }
97
99
  await setKvCache(kv, cleanRelPath, metaContent, metadata);
98
100
  seededCount++;
99
101
  if (process.env.DINOU_DEBUG) {
100
102
  console.log(` ✅ Cached Meta: ${cleanRelPath}`);
101
103
  }
102
- } else if (entry.name.endsWith(".rsc")) {
104
+ } else if (entry.name === "slot.json") {
105
+ const slotContent = fs.readFileSync(fullPath, "utf8");
106
+ let metadata = null;
107
+ const metaPath = path.join(dir, "slot.metadata.json");
108
+ if (fs.existsSync(metaPath)) {
109
+ try {
110
+ metadata = JSON.parse(fs.readFileSync(metaPath, "utf8"));
111
+ } catch (e) { }
112
+ }
113
+ await setKvCache(kv, cleanRelPath, slotContent, metadata);
114
+ seededCount++;
115
+ if (process.env.DINOU_DEBUG) {
116
+ console.log(` ✅ Cached Slot: ${cleanRelPath}`);
117
+ }
118
+ } else if (
119
+ entry.name.endsWith(".rsc") ||
120
+ entry.name === "shell.page.json" ||
121
+ entry.name === "shell.layout.json"
122
+ ) {
103
123
  const rsc = fs.readFileSync(fullPath, "utf8");
104
- await setKvCache(kv, cleanRelPath, rsc, null);
124
+ if (entry.name.endsWith(".rsc") && rsc.trim().length === 0) {
125
+ continue;
126
+ }
127
+ let metadata = null;
128
+ if (entry.name === "layout.rsc") {
129
+ const lMetaPath = path.join(dir, "layout.metadata.json");
130
+ if (fs.existsSync(lMetaPath)) {
131
+ try {
132
+ metadata = JSON.parse(fs.readFileSync(lMetaPath, "utf8"));
133
+ } catch (e) { }
134
+ }
135
+ }
136
+ await setKvCache(kv, cleanRelPath, rsc, metadata);
105
137
  seededCount++;
106
138
  if (process.env.DINOU_DEBUG) {
107
139
  console.log(` ✅ Cached RSC: ${cleanRelPath}`);