@hamur/adapter-vue 0.0.0-stage → 0.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 (74) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +32 -2
  3. package/dist/chunks/browser-index-43pdsfp6.js +69 -0
  4. package/dist/chunks/browser-index-d1bxd40m.js +320 -0
  5. package/dist/chunks/browser-index-nhft0ewv.js +161 -0
  6. package/dist/src/adapter.js +402 -0
  7. package/dist/src/index.js +1348 -0
  8. package/dist/src/keepalive.js +6 -0
  9. package/dist/src/runtime/client.js +20 -0
  10. package/dist/src/runtime/island.js +45 -0
  11. package/dist/src/runtime/server.js +238 -0
  12. package/dist/types/src/adapter.d.ts +2 -0
  13. package/dist/types/src/async-data.d.ts +14 -0
  14. package/dist/types/src/client-component.d.ts +12 -0
  15. package/dist/types/src/client-fallback.d.ts +2 -0
  16. package/dist/types/src/components/AwaitValue.d.ts +15 -0
  17. package/dist/types/src/components/ClientFallback.server.d.ts +17 -0
  18. package/dist/types/src/components/ErrorBoundary.server.d.ts +21 -0
  19. package/dist/types/src/components/error-code.d.ts +1 -0
  20. package/dist/types/src/context.d.ts +65 -0
  21. package/dist/types/src/index.d.ts +329 -0
  22. package/dist/types/src/island.d.ts +68 -0
  23. package/dist/types/src/keepalive.d.ts +22 -0
  24. package/dist/types/src/lazy-hydration.d.ts +10 -0
  25. package/dist/types/src/markdown-page.d.ts +9 -0
  26. package/dist/types/src/markdown.d.ts +45 -0
  27. package/dist/types/src/plugin.d.ts +45 -0
  28. package/dist/types/src/route-boundary.d.ts +35 -0
  29. package/dist/types/src/route-boundary.server.d.ts +5 -0
  30. package/dist/types/src/runtime/client.d.ts +11 -0
  31. package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
  32. package/dist/types/src/runtime/hydration.d.ts +8 -0
  33. package/dist/types/src/runtime/island.d.ts +8 -0
  34. package/dist/types/src/runtime/server.d.ts +12 -0
  35. package/dist/types/src/server-island.d.ts +33 -0
  36. package/dist/types/src/svg.d.ts +10 -0
  37. package/package.json +46 -4
  38. package/src/adapter.ts +25 -0
  39. package/src/async-data.ts +77 -0
  40. package/src/client-component.ts +39 -0
  41. package/src/client-fallback.ts +2 -0
  42. package/src/components/Await.vue +30 -0
  43. package/src/components/AwaitValue.ts +41 -0
  44. package/src/components/ClientFallback.server.ts +41 -0
  45. package/src/components/ClientFallback.vue +31 -0
  46. package/src/components/ClientOnly.vue +16 -0
  47. package/src/components/DefaultErrorPage.vue +19 -0
  48. package/src/components/DevOnly.vue +8 -0
  49. package/src/components/ErrorBoundary.server.ts +69 -0
  50. package/src/components/ErrorBoundary.vue +32 -0
  51. package/src/components/Form.vue +85 -0
  52. package/src/components/Link.vue +200 -0
  53. package/src/components/Navigate.vue +16 -0
  54. package/src/components/Outlet.vue +28 -0
  55. package/src/components/error-code.ts +7 -0
  56. package/src/context.ts +391 -0
  57. package/src/index.ts +1528 -0
  58. package/src/island.ts +53 -0
  59. package/src/keepalive.ts +92 -0
  60. package/src/lazy-hydration.ts +53 -0
  61. package/src/markdown-page.ts +21 -0
  62. package/src/markdown.ts +80 -0
  63. package/src/plugin.ts +398 -0
  64. package/src/route-boundary.server.ts +29 -0
  65. package/src/route-boundary.ts +48 -0
  66. package/src/runtime/client.ts +137 -0
  67. package/src/runtime/hydration-dev.ts +38 -0
  68. package/src/runtime/hydration.ts +7 -0
  69. package/src/runtime/island.ts +60 -0
  70. package/src/runtime/server.ts +145 -0
  71. package/src/server-island.ts +38 -0
  72. package/src/svg.ts +28 -0
  73. package/src/vue.d.ts +5 -0
  74. package/src/webmcp.d.ts +18 -0
@@ -0,0 +1,60 @@
1
+ import { createApp, createSSRApp } from "vue"
2
+ import type { App } from "vue"
3
+ import { setAdapterInstance } from "hamur/adapter"
4
+ import type { IslandContext } from "hamur/adapter"
5
+ import { createIslandRoot } from "../context.ts"
6
+ import { rootErrorHandler } from "./client.ts"
7
+
8
+ const apps = new WeakMap<Element, App>()
9
+ const roots = new WeakMap<Element, ReturnType<typeof createIslandRoot>>()
10
+
11
+ async function start(
12
+ create: typeof createSSRApp,
13
+ el: Element,
14
+ component: unknown,
15
+ props: Record<string, unknown>,
16
+ ctx: IslandContext,
17
+ ): Promise<void> {
18
+ const root = createIslandRoot(component, props, ctx)
19
+ const app = create(root.component)
20
+ app.config.idPrefix = ctx.idPrefix
21
+ app.config.errorHandler = rootErrorHandler(ctx.app)
22
+ root.install(app)
23
+ apps.set(el, app)
24
+ roots.set(el, root)
25
+ await setAdapterInstance(ctx.app, app)
26
+ // An unmountIsland during the await found the app not mounted yet; mounting now would leave it live on a removed element.
27
+ if (apps.get(el) === app) app.mount(el)
28
+ }
29
+
30
+ /** Hydrates the island's server HTML in `el` as an app of its own. */
31
+ export function hydrateIsland(
32
+ el: Element,
33
+ component: unknown,
34
+ props: Record<string, unknown>,
35
+ ctx: IslandContext,
36
+ ): Promise<void> {
37
+ return start(createSSRApp, el, component, props, ctx)
38
+ }
39
+
40
+ /** Mounts the island into `el` in place of the fallback it holds. */
41
+ export function mountIsland(
42
+ el: Element,
43
+ component: unknown,
44
+ props: Record<string, unknown>,
45
+ ctx: IslandContext,
46
+ ): Promise<void> {
47
+ el.replaceChildren()
48
+ return start(createApp, el, component, props, ctx)
49
+ }
50
+
51
+ export function unmountIsland(el: Element): void {
52
+ apps.get(el)?.unmount()
53
+ apps.delete(el)
54
+ roots.delete(el)
55
+ }
56
+
57
+ /** Gives the island's app new props, which keeps its state. */
58
+ export function updateIsland(el: Element, props: Record<string, unknown>): Promise<void> | void {
59
+ return roots.get(el)?.setProps(props)
60
+ }
@@ -0,0 +1,145 @@
1
+ import { createSSRApp, defineComponent, h, useId } from "vue"
2
+ import type { Component } from "vue"
3
+ import { renderToString, renderToWebStream } from "vue/server-renderer"
4
+ import type { SSRContext } from "vue/server-renderer"
5
+ import {
6
+ publicError,
7
+ resetAnnouncerClaims,
8
+ serverComponentAttributes,
9
+ serverComponentProps,
10
+ setAdapterInstance,
11
+ } from "hamur/adapter"
12
+ import type { ComponentRenderContext, RenderContext, RenderResult } from "hamur/adapter"
13
+ import ClientFallback from "../components/ClientFallback.vue"
14
+ import { ssrRender as clientFallbackRender } from "../components/ClientFallback.server.ts"
15
+ import ErrorBoundary from "../components/ErrorBoundary.vue"
16
+ import { ssrRender } from "../components/ErrorBoundary.server.ts"
17
+ import { RouteBoundary } from "../route-boundary.ts"
18
+ import { ssrRender as routeBoundaryRender } from "../route-boundary.server.ts"
19
+ import { HeadPatchesKey, createIslandRoot, createRoot, installKeepAlive } from "../context.ts"
20
+ import * as keepAlive from "../keepalive.ts"
21
+
22
+ installKeepAlive(keepAlive)
23
+
24
+ ;(ErrorBoundary as { ssrRender?: unknown }).ssrRender = ssrRender
25
+ ;(ClientFallback as { ssrRender?: unknown }).ssrRender = clientFallbackRender
26
+ ;(RouteBoundary as { ssrRender?: unknown }).ssrRender = routeBoundaryRender
27
+
28
+ /** A throw no `ErrorBoundary` caught fails the render, so the handler answers the error page with its status. */
29
+ export async function render(ctx: RenderContext): Promise<RenderResult> {
30
+ resetAnnouncerClaims(ctx.app)
31
+ const root = createRoot(ctx)
32
+ const app = createSSRApp(root.component)
33
+ root.install(app)
34
+ const headPatches: Array<() => void> = []
35
+ app.provide(HeadPatchesKey, headPatches)
36
+ await setAdapterInstance(ctx.app, app)
37
+ let failed: { error: unknown } | undefined
38
+ const previous = app.config.errorHandler
39
+ let shellRead = false
40
+ app.config.errorHandler = (error, instance, info) => {
41
+ if (shellRead) void publicError(ctx.app, error)
42
+ else if (failed) console.error(error)
43
+ else failed = { error }
44
+ previous?.(error, instance, info)
45
+ }
46
+ const ssrContext: SSRContext = {}
47
+ if (ctx.stream) {
48
+ const body = await afterShell(renderToWebStream(app, ssrContext), (error) => void publicError(ctx.app, error))
49
+ shellRead = true
50
+ if (failed) {
51
+ void body.cancel()
52
+ throw failed.error
53
+ }
54
+ for (const patch of headPatches) patch()
55
+ return { body }
56
+ }
57
+ const body = await renderToString(app, ssrContext)
58
+ if (failed) throw failed.error
59
+ for (const patch of headPatches) patch()
60
+ return ssrContext.teleports && Object.keys(ssrContext.teleports).length
61
+ ? { body, teleports: ssrContext.teleports }
62
+ : { body }
63
+ }
64
+
65
+ /** The page's app instance while its islands render as apps of their own, so `app.adapter` is the page's again once they are done. */
66
+ const islandsRendering = new WeakMap<object, { page: unknown; count: number }>()
67
+
68
+ /** `component` as an app of its own, its ids prefixed with `ctx.idPrefix` as `hydrateIsland` prefixes them. */
69
+ export async function renderComponent(
70
+ component: unknown,
71
+ props: Record<string, unknown>,
72
+ ctx: ComponentRenderContext,
73
+ ): Promise<{ html: string }> {
74
+ // `renderIsland` calls this inside the page's render, whose current instance a nested app would take over.
75
+ await Promise.resolve()
76
+ const root = createIslandRoot(component, props, ctx)
77
+ const app = createSSRApp(root.component)
78
+ app.config.idPrefix = ctx.idPrefix
79
+ root.install(app)
80
+ const headPatches: Array<() => void> = []
81
+ app.provide(HeadPatchesKey, headPatches)
82
+ let rendering = islandsRendering.get(ctx.app)
83
+ if (!rendering) islandsRendering.set(ctx.app, (rendering = { page: ctx.app.adapter, count: 0 }))
84
+ rendering.count++
85
+ try {
86
+ await setAdapterInstance(ctx.app, app)
87
+ let failed: { error: unknown } | undefined
88
+ const previous = app.config.errorHandler
89
+ app.config.errorHandler = (error, instance, info) => {
90
+ failed ??= { error }
91
+ previous?.(error, instance, info)
92
+ }
93
+ const html = await renderToString(app, {})
94
+ if (failed) throw failed.error
95
+ for (const patch of headPatches) patch()
96
+ return { html }
97
+ } finally {
98
+ if (--rendering.count === 0) {
99
+ islandsRendering.delete(ctx.app)
100
+ ctx.app.adapter = rendering.page
101
+ }
102
+ }
103
+ }
104
+
105
+ /** Vue's web stream errors its controller on a late throw; the shell is read here so a throw before it rejects the render, and a later one ends the stream instead of rejecting. */
106
+ async function afterShell(
107
+ stream: ReadableStream<Uint8Array>,
108
+ late: (error: unknown) => void,
109
+ ): Promise<ReadableStream<Uint8Array>> {
110
+ const reader = stream.getReader()
111
+ const first = await reader.read()
112
+ if (first.done) return new ReadableStream({ start: (controller) => controller.close() })
113
+ return new ReadableStream<Uint8Array>({
114
+ start: (controller) => controller.enqueue(first.value),
115
+ async pull(controller) {
116
+ const chunk = await reader
117
+ .read()
118
+ .catch((error: unknown) => (late(error), { done: true as const, value: undefined }))
119
+ if (chunk.done) controller.close()
120
+ else controller.enqueue(chunk.value)
121
+ },
122
+ cancel: (reason) => reader.cancel(reason),
123
+ })
124
+ }
125
+
126
+ /** The server side of a client-only component renders nothing: the comment Vue writes for an empty render. */
127
+ export function clientComponent(): Component {
128
+ return defineComponent({ name: "ClientComponent", render: () => null })
129
+ }
130
+
131
+ /** The server side of a server-only component: the component itself inside `hamur-component`, its props checked as the client sends them. */
132
+ export function serverComponent(name: string, component: Component): Component {
133
+ return defineComponent({
134
+ name: `${name}.server`,
135
+ inheritAttrs: false,
136
+ setup(_, { attrs, slots }) {
137
+ const uid = useId()
138
+ return () => {
139
+ const props = { ...attrs }
140
+ serverComponentProps(name, props, slots.default !== undefined)
141
+ return h("hamur-component", { name, uid, ...serverComponentAttributes(name) }, [h(component, props)])
142
+ }
143
+ },
144
+ })
145
+ }
@@ -0,0 +1,38 @@
1
+ import { defineComponent, h, inject, useId } from "vue"
2
+ import type { PropType } from "vue"
3
+ import { SERVER_ISLAND_ELEMENT, renderServerIsland, serverIslandProps } from "hamur/adapter"
4
+ import { AppKey } from "./context.ts"
5
+
6
+ const isServer = typeof document === "undefined"
7
+
8
+ export interface ServerIslandProps {
9
+ /** The name of a component under `app/server-islands/`. */
10
+ name: string
11
+ props?: Record<string, unknown>
12
+ }
13
+
14
+ /**
15
+ * A component rendered on the server in its own request; the page holds the `fallback` slot, the core swaps the answer in,
16
+ * and Vue never renders or hydrates it.
17
+ */
18
+ export const ServerIsland = defineComponent({
19
+ name: "ServerIsland",
20
+ props: {
21
+ name: { type: String, required: true },
22
+ props: { type: Object as PropType<ServerIslandProps["props"]>, default: undefined },
23
+ },
24
+ setup(props, { slots }) {
25
+ const app = inject(AppKey, null)
26
+ if (!app) throw new Error("ServerIsland must be rendered inside a Hamur app")
27
+ const id = useId()
28
+ if (isServer)
29
+ return renderServerIsland(app, id, props.name, props.props).attributes.then(
30
+ (attributes) => () => h(SERVER_ISLAND_ELEMENT, attributes, slots.fallback?.()),
31
+ )
32
+ // While hydrating, the server's element keeps its attributes and nodes: Vue patches every prop of a custom element, so it has none, and no children to compare.
33
+ if (app.asyncData.hydrating) return () => h(SERVER_ISLAND_ELEMENT)
34
+ const given =
35
+ props.props && Object.keys(props.props).length ? { props: serverIslandProps(props.name, props.props) } : {}
36
+ return () => h(SERVER_ISLAND_ELEMENT, { name: props.name, ...given }, slots.fallback?.())
37
+ },
38
+ })
package/src/svg.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { svgCode, type SvgCode, type SvgNode, type SvgSource } from "hamur/adapter"
2
+
3
+ function nodeCode(code: SvgCode, node: SvgNode): string {
4
+ if (typeof node === "string" || Array.isArray(node)) return code.value(node)
5
+ return `h(${JSON.stringify(node.tag)}, ${code.props(code.attrs(node))}, [${node.children.map((child) => nodeCode(code, child)).join(", ")}])`
6
+ }
7
+
8
+ function module(svg: SvgSource): { contents: string; extension: string } {
9
+ const code = svgCode(svg, { title: "props.title", props: "attrs" })
10
+ const children = svg.children.map((child) => nodeCode(code, child))
11
+ const hidden = code.hidden ? `${code.hidden} ? { "aria-hidden": "true" } : {}` : "{}"
12
+ const contents = `import { defineComponent, h, useId } from "vue";
13
+
14
+ export default defineComponent({
15
+ name: "SvgComponent",
16
+ inheritAttrs: false,
17
+ props: { title: { type: String, default: undefined } },
18
+ setup(props, { attrs }) {
19
+ const uid = useId().replace(/[^\\w-]/g, "_");
20
+ return () =>
21
+ h("svg", { ...${code.props(svg.attrs)}, ...(${hidden}), ...attrs }, [...(props.title == null ? [] : [h("title", props.title)])${children.length ? `, ${children.join(", ")}` : ""}]);
22
+ },
23
+ });
24
+ `
25
+ return { contents, extension: ".js" }
26
+ }
27
+
28
+ export const svgComponent = { types: `import("vue").Component`, module }
package/src/vue.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ declare module "*.vue" {
2
+ import type { DefineComponent } from "vue"
3
+ const component: DefineComponent<{}, {}, any>
4
+ export default component
5
+ }
@@ -0,0 +1,18 @@
1
+ import "vue"
2
+
3
+ declare module "vue" {
4
+ interface FormHTMLAttributes {
5
+ toolname?: string | undefined
6
+ tooldescription?: string | undefined
7
+ toolautosubmit?: boolean | undefined
8
+ }
9
+ interface InputHTMLAttributes {
10
+ toolparamdescription?: string | undefined
11
+ }
12
+ interface SelectHTMLAttributes {
13
+ toolparamdescription?: string | undefined
14
+ }
15
+ interface TextareaHTMLAttributes {
16
+ toolparamdescription?: string | undefined
17
+ }
18
+ }