@teyik0/furin 0.2.0-alpha.4 → 0.2.0-alpha.5

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 (67) hide show
  1. package/dist/adapter/bun.d.ts +17 -1
  2. package/dist/adapter/package.d.ts +26 -0
  3. package/dist/build/client.d.ts +1 -1
  4. package/dist/build/compile-entry.d.ts +3 -3
  5. package/dist/build/compile-entry.ts +88 -83
  6. package/dist/build/entry-template.d.ts +35 -9
  7. package/dist/build/entry-template.ts +106 -56
  8. package/dist/build/hydrate.d.ts +1 -1
  9. package/dist/build/index.js +643 -247
  10. package/dist/build/scan-server.d.ts +10 -4
  11. package/dist/build/server-routes-entry.d.ts +3 -3
  12. package/dist/build/server-routes-entry.ts +5 -11
  13. package/dist/build/ssg-cache.d.ts +1 -1
  14. package/dist/build/types.d.ts +36 -1
  15. package/dist/cli/config.d.ts +6 -1
  16. package/dist/cli/index.js +662 -260
  17. package/dist/config.d.ts +11 -2
  18. package/dist/config.js +5 -1
  19. package/dist/furin.d.ts +36 -18
  20. package/dist/furin.js +630 -310
  21. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  22. package/dist/server/cache/dev-loader.d.ts +3 -2
  23. package/dist/server/cache/invalidation.d.ts +19 -0
  24. package/dist/server/cache/isr.d.ts +6 -2
  25. package/dist/server/cache/registry.d.ts +3 -3
  26. package/dist/server/cache/ssg.d.ts +6 -2
  27. package/dist/server/cache/store-view.d.ts +11 -0
  28. package/dist/server/instance.d.ts +86 -0
  29. package/dist/server/internal.d.ts +16 -1
  30. package/dist/server/internal.js +72 -5
  31. package/dist/server/render/template.d.ts +3 -2
  32. package/dist/server/sync/config.d.ts +11 -0
  33. package/dist/server/sync/stream.d.ts +8 -1
  34. package/dist/shared/prefix.d.ts +15 -0
  35. package/package.json +6 -1
  36. package/src/adapter/bun.ts +113 -50
  37. package/src/adapter/package.ts +206 -0
  38. package/src/build/client.ts +9 -3
  39. package/src/build/compile-entry.ts +88 -83
  40. package/src/build/entry-template.ts +106 -56
  41. package/src/build/hydrate.ts +4 -2
  42. package/src/build/index.ts +99 -28
  43. package/src/build/scan-server.ts +23 -7
  44. package/src/build/server-routes-entry.ts +5 -11
  45. package/src/build/ssg-cache.ts +6 -2
  46. package/src/build/types.ts +37 -1
  47. package/src/cli/config.ts +9 -3
  48. package/src/cli/index.ts +21 -3
  49. package/src/config.ts +14 -1
  50. package/src/furin.ts +297 -143
  51. package/src/server/auto-invalidate/registry.ts +23 -1
  52. package/src/server/auto-invalidate/runtime.ts +29 -4
  53. package/src/server/cache/dev-loader.ts +77 -38
  54. package/src/server/cache/invalidation.ts +71 -30
  55. package/src/server/cache/isr.ts +20 -5
  56. package/src/server/cache/registry.ts +17 -20
  57. package/src/server/cache/ssg.ts +19 -4
  58. package/src/server/cache/store-view.ts +30 -0
  59. package/src/server/instance.ts +252 -0
  60. package/src/server/internal.ts +100 -5
  61. package/src/server/render/not-found.ts +9 -3
  62. package/src/server/render/ssr.ts +16 -4
  63. package/src/server/render/template.ts +57 -24
  64. package/src/server/sync/config.ts +32 -1
  65. package/src/server/sync/plugin.ts +4 -5
  66. package/src/server/sync/stream.ts +19 -9
  67. package/src/shared/prefix.ts +38 -0
@@ -1,6 +1,6 @@
1
1
  import { Elysia } from "elysia";
2
2
  import type { SyncChange } from "./adapter.ts";
3
- import { getSyncStreamPath } from "./config.ts";
3
+ import { syncPublishChannels } from "./config.ts";
4
4
  import { memorySyncAdapter } from "./memory-adapter.ts";
5
5
 
6
6
  export type { ChangePage as SyncChangePage, SyncChange } from "./adapter.ts";
@@ -72,25 +72,35 @@ export function publishSyncInvalidation(invalidations: readonly string[]): void
72
72
  if (invalidations.length === 0) {
73
73
  return;
74
74
  }
75
- memorySyncAdapter.appendChanges({
76
- invalidations,
77
- path: getSyncStreamPath() ?? defaultStreamPath,
78
- });
75
+ for (const channel of syncPublishChannels()) {
76
+ memorySyncAdapter.appendChanges({
77
+ invalidations,
78
+ path: channel,
79
+ });
80
+ }
79
81
  }
80
82
 
81
- export function createSyncStreamPlugin(path?: string) {
83
+ /**
84
+ * Builds the SSE plugin serving one instance's sync stream. Routes are
85
+ * registered at the logical `path` (the parent instance's Elysia prefix makes
86
+ * them physical); `channel` is the adapter key the stream reads from — pass
87
+ * the PHYSICAL path (`prefix + path`) so two mounted apps never share a
88
+ * cursor sequence.
89
+ */
90
+ export function createSyncStreamPlugin(path?: string, channel?: string) {
82
91
  const streamPath = path ?? defaultStreamPath;
83
- return new Elysia({ name: `furin-sync-stream-${streamPath}` })
92
+ const channelKey = channel ?? streamPath;
93
+ return new Elysia({ name: `furin-sync-stream-${channelKey}` })
84
94
  .get(`${streamPath}/changes`, ({ request, set }) => {
85
95
  set.headers["cache-control"] = "no-store";
86
96
  const query = parseChangeQuery(request);
87
97
  if ("error" in query) {
88
98
  return query.error;
89
99
  }
90
- return memorySyncAdapter.readChanges({ ...query, path: streamPath });
100
+ return memorySyncAdapter.readChanges({ ...query, path: channelKey });
91
101
  })
92
102
  .get(streamPath, () => {
93
- const state = getStreamState(streamPath);
103
+ const state = getStreamState(channelKey);
94
104
  let controllerRef: ReadableStreamDefaultController<Uint8Array> | undefined;
95
105
  let heartbeat: ReturnType<typeof setInterval> | undefined;
96
106
  const stream = new ReadableStream<Uint8Array>({
@@ -0,0 +1,38 @@
1
+ // Naming helpers shared between the runtime (furin.ts) and the build
2
+ // pipeline (adapter/bun.ts) — both must agree on where a mounted app's
3
+ // client assets live on disk.
4
+
5
+ /**
6
+ * On-disk client dir name for a mounted app: the root instance keeps the
7
+ * historical `client/`, prefixed instances get `client-<slug>/` next to it.
8
+ */
9
+ export function clientDirNameForPrefix(prefix: string): string {
10
+ return prefix === "" ? "client" : `client-${prefixSlug(prefix)}`;
11
+ }
12
+
13
+ /** Filesystem-safe slug for a mount prefix (`/admin/v2` → `admin-v2`). */
14
+ export function prefixSlug(prefix: string): string {
15
+ return prefix.slice(1).replaceAll("/", "-");
16
+ }
17
+
18
+ /**
19
+ * `prefixSlug` is NOT injective: `/a-b` and `/a/b` both slug to `a-b`, so two
20
+ * distinct prefixes can claim the same client dir and silently overwrite each
21
+ * other's build output. A readable injective encoding is ambiguous anyway
22
+ * (escaping `-` as `--` still confuses `/a-/b` with `/a/-b`), so we detect the
23
+ * collision and fail fast instead.
24
+ */
25
+ export function assertNoPrefixSlugCollisions(prefixes: string[]): void {
26
+ const byDirName = new Map<string, string>();
27
+ for (const prefix of prefixes) {
28
+ const dirName = clientDirNameForPrefix(prefix);
29
+ const existing = byDirName.get(dirName);
30
+ if (existing !== undefined && existing !== prefix) {
31
+ throw new Error(
32
+ `[furin] prefixes "${existing}" and "${prefix}" both map to the client directory ` +
33
+ `"${dirName}" — rename one of them so their slugs (\`/\` → \`-\`) no longer collide.`
34
+ );
35
+ }
36
+ byDirName.set(dirName, prefix);
37
+ }
38
+ }