@hamur/adapter-svelte 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.
- package/LICENSE +22 -0
- package/README.md +31 -2
- package/dist/chunks/browser-index-324qgh1c.js +190 -0
- package/dist/chunks/browser-index-evqp5a71.js +64 -0
- package/dist/chunks/browser-index-xc6081e3.js +9 -0
- package/dist/src/adapter.js +480 -0
- package/dist/src/index.js +1161 -0
- package/dist/src/runtime/client.js +65 -0
- package/dist/src/runtime/island.js +72 -0
- package/dist/src/runtime/server.js +142 -0
- package/dist/types/src/activation.d.ts +15 -0
- package/dist/types/src/adapter.d.ts +2 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/boundary-error.d.ts +9 -0
- package/dist/types/src/client-only.d.ts +6 -0
- package/dist/types/src/context.d.ts +87 -0
- package/dist/types/src/deferred.svelte.d.ts +6 -0
- package/dist/types/src/effect.svelte.d.ts +2 -0
- package/dist/types/src/hydrating.d.ts +4 -0
- package/dist/types/src/index.d.ts +270 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/keepalive.d.ts +11 -0
- package/dist/types/src/lazy-hydration.d.ts +25 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/plugin.d.ts +52 -0
- package/dist/types/src/runtime/client.d.ts +12 -0
- package/dist/types/src/runtime/hmr.d.ts +28 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +8 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/server.d.ts +12 -0
- package/dist/types/src/server-component.d.ts +3 -0
- package/dist/types/src/store.d.ts +7 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +43 -4
- package/src/Announcer.svelte +26 -0
- package/src/Await.svelte +43 -0
- package/src/AwaitAsync.svelte +42 -0
- package/src/BodyAttrs.svelte +7 -0
- package/src/Caught.svelte +41 -0
- package/src/ClientComponent.svelte +35 -0
- package/src/ClientFallback.svelte +46 -0
- package/src/ClientOnly.svelte +22 -0
- package/src/DefaultErrorPage.svelte +15 -0
- package/src/DevOnly.svelte +12 -0
- package/src/ErrorBoundary.svelte +14 -0
- package/src/FallbackEffect.svelte +9 -0
- package/src/Form.svelte +96 -0
- package/src/HeadLink.svelte +7 -0
- package/src/HeadMeta.svelte +7 -0
- package/src/HeadScript.svelte +7 -0
- package/src/HeadStyle.svelte +7 -0
- package/src/HeadTitle.svelte +6 -0
- package/src/HtmlAttrs.svelte +7 -0
- package/src/HttpHeader.svelte +7 -0
- package/src/HttpStatusCode.svelte +7 -0
- package/src/Island.svelte +59 -0
- package/src/IslandChildren.svelte +7 -0
- package/src/KeptOutlet.svelte +46 -0
- package/src/KeptPage.svelte +55 -0
- package/src/LazyHydration.svelte +54 -0
- package/src/Link.svelte +209 -0
- package/src/LoadingIndicator.svelte +18 -0
- package/src/MarkdownNodes.svelte +36 -0
- package/src/MarkdownRenderer.svelte +20 -0
- package/src/MarkdownSlots.svelte +33 -0
- package/src/Navigate.svelte +10 -0
- package/src/Nest.svelte +83 -0
- package/src/Outlet.svelte +31 -0
- package/src/Rethrow.svelte +7 -0
- package/src/Root.svelte +29 -0
- package/src/RouteAnnouncer.svelte +47 -0
- package/src/Scoped.svelte +11 -0
- package/src/ServerComponent.svelte +19 -0
- package/src/ServerComponentClient.svelte +64 -0
- package/src/ServerIsland.svelte +26 -0
- package/src/Slotted.svelte +11 -0
- package/src/Thrown.svelte +9 -0
- package/src/Time.svelte +23 -0
- package/src/activation.ts +51 -0
- package/src/adapter.ts +24 -0
- package/src/async-data.ts +105 -0
- package/src/boundary-error.ts +77 -0
- package/src/client-only.ts +8 -0
- package/src/context.ts +227 -0
- package/src/deferred.svelte.ts +33 -0
- package/src/effect.svelte.ts +7 -0
- package/src/env.d.ts +3 -0
- package/src/hydrating.ts +18 -0
- package/src/index.ts +1337 -0
- package/src/island-html.ts +58 -0
- package/src/keepalive.ts +12 -0
- package/src/lazy-hydration.ts +89 -0
- package/src/markdown-page.ts +17 -0
- package/src/plugin.ts +558 -0
- package/src/runtime/client.ts +61 -0
- package/src/runtime/hmr.ts +177 -0
- package/src/runtime/hydration-dev.ts +22 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +94 -0
- package/src/runtime/server.ts +87 -0
- package/src/server-component.ts +13 -0
- package/src/store.ts +31 -0
- package/src/svelte.d.ts +5 -0
- package/src/svg.ts +30 -0
- package/src/webmcp.d.ts +18 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { OVERLAY_HOOK, type OverlayHook } from "hamur/adapter/dev"
|
|
2
|
+
import { getContext, onDestroy, setContext, untrack } from "svelte"
|
|
3
|
+
|
|
4
|
+
/** What the runtime calls outside itself; the unit tests replace it, as there is no component context without a DOM. */
|
|
5
|
+
export const host = {
|
|
6
|
+
getContext,
|
|
7
|
+
setContext,
|
|
8
|
+
onDestroy,
|
|
9
|
+
untrack,
|
|
10
|
+
reload: () => location.reload(),
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HotInstance {
|
|
14
|
+
/** Value `i` of the instance this one replaces, when there is one and its type matches `init`; else `init`. */
|
|
15
|
+
take<T>(i: number, init: T): T
|
|
16
|
+
read(values: () => unknown[]): void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface HotModule {
|
|
20
|
+
readonly id: string
|
|
21
|
+
readonly signature: string
|
|
22
|
+
readonly hash: string
|
|
23
|
+
readonly deps: readonly object[]
|
|
24
|
+
/** The default export of the module's first version: Svelte's `update` makes it render the newest function. */
|
|
25
|
+
first?: Function
|
|
26
|
+
enter(): HotInstance
|
|
27
|
+
component(component: Function): void
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Parent {
|
|
31
|
+
key: string
|
|
32
|
+
/** Next free index per module id, and the module ids that ever held two live instances under this parent. */
|
|
33
|
+
used: Map<string, Set<number>>
|
|
34
|
+
shared: Set<string>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Instance extends Parent {
|
|
38
|
+
parent: Parent
|
|
39
|
+
id: string
|
|
40
|
+
read?: () => unknown[]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface Span {
|
|
44
|
+
saved: Map<Instance, unknown[]>
|
|
45
|
+
byKey: Map<string, Instance>
|
|
46
|
+
reset: Set<string>
|
|
47
|
+
/** Module ids that ran again in this window: one that runs a second time is a later update, whose saved state is newer. */
|
|
48
|
+
updated: Set<string>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const KEY = Symbol()
|
|
52
|
+
const top: Parent = { key: "", used: new Map(), shared: new Set() }
|
|
53
|
+
const live = new Set<Instance>()
|
|
54
|
+
const latest = new Map<string, HotModule>()
|
|
55
|
+
const modules = new WeakMap<Function, HotModule>()
|
|
56
|
+
let forwarding = true
|
|
57
|
+
let destroyed: Instance | undefined
|
|
58
|
+
let open: Span | undefined
|
|
59
|
+
|
|
60
|
+
/** True when this instance or an ancestor sits in a shared (parent, module) pair: matching it by position could pick a sibling's state. */
|
|
61
|
+
function excluded(instance: Instance): boolean {
|
|
62
|
+
for (let node: Parent | Instance = instance; "parent" in node; node = node.parent)
|
|
63
|
+
if (node.parent.shared.has(node.id)) return true
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function openWindow(): Span {
|
|
68
|
+
const span: Span = { saved: new Map(), byKey: new Map(), reset: new Set(), updated: new Set() }
|
|
69
|
+
for (const instance of live) {
|
|
70
|
+
if (!instance.read) continue
|
|
71
|
+
try {
|
|
72
|
+
span.saved.set(instance, host.untrack(instance.read))
|
|
73
|
+
} catch {
|
|
74
|
+
continue
|
|
75
|
+
}
|
|
76
|
+
if (!excluded(instance)) span.byKey.set(instance.key, instance)
|
|
77
|
+
}
|
|
78
|
+
open = span
|
|
79
|
+
// The overlay hook's `error` listener runs first and stops the event, so its `failed` flag is what sees an error.
|
|
80
|
+
const hook = (globalThis as Record<string, unknown>)[OVERLAY_HOOK] as OverlayHook | undefined
|
|
81
|
+
const failed = hook?.failed
|
|
82
|
+
setTimeout(() => {
|
|
83
|
+
if (open === span) open = undefined
|
|
84
|
+
if (hook?.failed && !failed) host.reload()
|
|
85
|
+
}, 0)
|
|
86
|
+
return span
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The first version of a hot-updated component, or `component` itself when it is unknown. */
|
|
90
|
+
export function stable(component: unknown): unknown {
|
|
91
|
+
return (forwarding && typeof component === "function" && modules.get(component)?.first) || component
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
;(globalThis as Record<symbol, unknown>)[Symbol.for("hamur.svelte.hot")] = stable
|
|
95
|
+
|
|
96
|
+
function enter(module: HotModule): HotInstance {
|
|
97
|
+
const { id } = module
|
|
98
|
+
if (latest.get(id) !== module && forwarding) {
|
|
99
|
+
forwarding = false
|
|
100
|
+
console.warn(`[hamur] hot: ${id} rendered an older version; reloading`)
|
|
101
|
+
host.reload()
|
|
102
|
+
}
|
|
103
|
+
const parent = host.getContext<Parent | undefined>(KEY) ?? top
|
|
104
|
+
let used = parent.used.get(id)
|
|
105
|
+
if (!used) parent.used.set(id, (used = new Set()))
|
|
106
|
+
let n = 0
|
|
107
|
+
while (used.has(n)) n++
|
|
108
|
+
used.add(n)
|
|
109
|
+
if (used.size > 1) parent.shared.add(id)
|
|
110
|
+
const instance: Instance = { key: `${parent.key}/${id}#${n}`, parent, id, used: new Map(), shared: new Set() }
|
|
111
|
+
let values: unknown[] | undefined
|
|
112
|
+
if (open && !open.reset.has(id)) {
|
|
113
|
+
const from = destroyed?.id === id && open.saved.has(destroyed) ? destroyed : open.byKey.get(instance.key)
|
|
114
|
+
if (from) {
|
|
115
|
+
values = open.saved.get(from)
|
|
116
|
+
open.saved.delete(from)
|
|
117
|
+
open.byKey.delete(from.key)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
destroyed = undefined
|
|
121
|
+
host.setContext(KEY, instance)
|
|
122
|
+
live.add(instance)
|
|
123
|
+
const slots = used
|
|
124
|
+
host.onDestroy(() => {
|
|
125
|
+
live.delete(instance)
|
|
126
|
+
slots.delete(n)
|
|
127
|
+
destroyed = instance
|
|
128
|
+
})
|
|
129
|
+
return {
|
|
130
|
+
take(i, init) {
|
|
131
|
+
if (!values || i >= values.length) return init
|
|
132
|
+
const value = values[i]
|
|
133
|
+
return (value == null || init == null || typeof value === typeof init ? value : init) as typeof init
|
|
134
|
+
},
|
|
135
|
+
read(read) {
|
|
136
|
+
instance.read = read
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** One per module version, `previous` is what the module's last version returned. */
|
|
142
|
+
export function hotModule(
|
|
143
|
+
previous: HotModule | undefined,
|
|
144
|
+
id: string,
|
|
145
|
+
signature: string,
|
|
146
|
+
hash: string,
|
|
147
|
+
deps: readonly object[],
|
|
148
|
+
): HotModule {
|
|
149
|
+
const module: HotModule = {
|
|
150
|
+
id,
|
|
151
|
+
signature,
|
|
152
|
+
hash,
|
|
153
|
+
deps,
|
|
154
|
+
first: previous?.first,
|
|
155
|
+
enter: () => enter(module),
|
|
156
|
+
component(component) {
|
|
157
|
+
module.first ??= component
|
|
158
|
+
modules.set(component, module)
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
latest.set(id, module)
|
|
162
|
+
if (previous) {
|
|
163
|
+
const span = open && !open.updated.has(id) ? open : openWindow()
|
|
164
|
+
span.updated.add(id)
|
|
165
|
+
const reason =
|
|
166
|
+
signature !== previous.signature
|
|
167
|
+
? "signature changed"
|
|
168
|
+
: hash === previous.hash && deps.some((dep, index) => dep !== previous.deps[index])
|
|
169
|
+
? "a dependency changed"
|
|
170
|
+
: undefined
|
|
171
|
+
if (reason && !span.reset.has(id)) {
|
|
172
|
+
span.reset.add(id)
|
|
173
|
+
console.info(`[hamur] hot: ${id} state reset (${reason})`)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return module
|
|
177
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { snapshotHydration } from "hamur/adapter/dev"
|
|
2
|
+
import { devHydration } from "./hydration.ts"
|
|
3
|
+
|
|
4
|
+
// svelte 5.57's `hydration_mismatch` warning (internal/client/warnings.js), printed when it gives up and mounts the root anew.
|
|
5
|
+
const MISMATCH = "hydration_mismatch"
|
|
6
|
+
|
|
7
|
+
/** Svelte rewrites a text that differs without a warning, so the DOM after is compared either way; the warning marks a failed root. */
|
|
8
|
+
devHydration.watch = (root) => {
|
|
9
|
+
const snapshot = snapshotHydration(root)
|
|
10
|
+
const warn = console.warn
|
|
11
|
+
let signalled = false
|
|
12
|
+
console.warn = (...args: unknown[]) => {
|
|
13
|
+
if (typeof args[0] === "string" && args[0].includes(MISMATCH)) signalled = true
|
|
14
|
+
warn.apply(console, args)
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
done() {
|
|
18
|
+
console.warn = warn
|
|
19
|
+
snapshot.compare(undefined, undefined, signalled)
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** What `hydration-dev.ts` hands the client runtime in `hamur dev`; a production client never sets it. */
|
|
2
|
+
export interface HydrationWatch {
|
|
3
|
+
/** `hydrate` returned: its DOM is compared with the server markup saved before. */
|
|
4
|
+
done(): void
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const devHydration: { watch?: (root: Element) => HydrationWatch } = {}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { flushSync, hydrate, mount, unmount } from "svelte"
|
|
2
|
+
import { ISLAND_ELEMENT, SLOT_ELEMENT } from "hamur/adapter"
|
|
3
|
+
import type { IslandContext } from "hamur/adapter"
|
|
4
|
+
import { islandSource } from "../context.ts"
|
|
5
|
+
import type { AppState } from "../context.ts"
|
|
6
|
+
import type { Source } from "../store.ts"
|
|
7
|
+
import { whileHydratingPage } from "../hydrating.ts"
|
|
8
|
+
import Root from "../Root.svelte"
|
|
9
|
+
|
|
10
|
+
const instances = new WeakMap<Element, Record<string, unknown>>()
|
|
11
|
+
const sources = new WeakMap<
|
|
12
|
+
Element,
|
|
13
|
+
{
|
|
14
|
+
source: Source<AppState>
|
|
15
|
+
component: unknown
|
|
16
|
+
ctx: IslandContext
|
|
17
|
+
kept: NonNullable<Parameters<typeof islandSource>[3]>
|
|
18
|
+
}
|
|
19
|
+
>()
|
|
20
|
+
|
|
21
|
+
const options = (el: Element, component: unknown, props: Record<string, unknown>, ctx: IslandContext) => {
|
|
22
|
+
const kept = {}
|
|
23
|
+
const source = islandSource(component, props, ctx, kept)
|
|
24
|
+
sources.set(el, { source, component, ctx, kept })
|
|
25
|
+
return { target: el, props: { source, idPrefix: `${ctx.idPrefix}-` }, idPrefix: ctx.idPrefix }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Svelte hydrates an `{@html}` as the nodes up to the next empty comment, and an inner island's own HTML holds such comments:
|
|
30
|
+
* the nodes between the inner island's opening and closing comments are left out while the outer island hydrates. An island
|
|
31
|
+
* inside the outer one's slot stays, as Svelte adopts the slot element whole.
|
|
32
|
+
*/
|
|
33
|
+
function setInnerIslandsAside(el: Element): () => void {
|
|
34
|
+
const aside: Array<[ChildNode, ChildNode[]]> = []
|
|
35
|
+
for (const inner of el.querySelectorAll(ISLAND_ELEMENT)) {
|
|
36
|
+
if (
|
|
37
|
+
inner.parentElement?.closest(`${ISLAND_ELEMENT}, ${SLOT_ELEMENT}`) !== el ||
|
|
38
|
+
inner.getAttribute("on") === "only"
|
|
39
|
+
)
|
|
40
|
+
continue
|
|
41
|
+
const open = inner.firstChild
|
|
42
|
+
// `<svelte:element>` ends its children with an empty comment of its own, after the `{@html}` closing one.
|
|
43
|
+
const close = inner.lastChild?.previousSibling
|
|
44
|
+
if (!open || !close || open === close) continue
|
|
45
|
+
const nodes: ChildNode[] = []
|
|
46
|
+
for (let node = open.nextSibling; node && node !== close; node = node.nextSibling) nodes.push(node)
|
|
47
|
+
for (const node of nodes) node.remove()
|
|
48
|
+
aside.push([close, nodes])
|
|
49
|
+
}
|
|
50
|
+
return () => {
|
|
51
|
+
for (const [close, nodes] of aside) close.before(...nodes)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Hydrates the island's server HTML in `el` as a root of its own, its ids prefixed as the server prefixed them. */
|
|
56
|
+
export function hydrateIsland(
|
|
57
|
+
el: Element,
|
|
58
|
+
component: unknown,
|
|
59
|
+
props: Record<string, unknown>,
|
|
60
|
+
ctx: IslandContext,
|
|
61
|
+
): void {
|
|
62
|
+
const restore = setInnerIslandsAside(el)
|
|
63
|
+
try {
|
|
64
|
+
instances.set(
|
|
65
|
+
el,
|
|
66
|
+
whileHydratingPage(() => hydrate(Root, options(el, component, props, ctx))),
|
|
67
|
+
)
|
|
68
|
+
flushSync()
|
|
69
|
+
} finally {
|
|
70
|
+
restore()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Mounts the island into `el` in place of the fallback it holds. */
|
|
75
|
+
export function mountIsland(el: Element, component: unknown, props: Record<string, unknown>, ctx: IslandContext): void {
|
|
76
|
+
el.replaceChildren()
|
|
77
|
+
instances.set(el, mount(Root, options(el, component, props, ctx)))
|
|
78
|
+
flushSync()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function unmountIsland(el: Element): void {
|
|
82
|
+
const instance = instances.get(el)
|
|
83
|
+
instances.delete(el)
|
|
84
|
+
sources.delete(el)
|
|
85
|
+
if (instance) void unmount(instance)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Hands the island's root new props through its source, which keeps its state. */
|
|
89
|
+
export function updateIsland(el: Element, props: Record<string, unknown>): void {
|
|
90
|
+
const own = sources.get(el)
|
|
91
|
+
if (!own) return
|
|
92
|
+
own.source.set(islandSource(own.component, props, own.ctx, own.kept).get())
|
|
93
|
+
flushSync()
|
|
94
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { render as renderSvelte } from "svelte/server"
|
|
2
|
+
import { publicError } from "hamur/adapter"
|
|
3
|
+
import { isRedirect } from "hamur/app"
|
|
4
|
+
import { renderInPasses, setAdapterInstance } from "hamur/adapter"
|
|
5
|
+
import type { ComponentRenderContext, RenderContext, RenderResult } from "hamur/adapter"
|
|
6
|
+
import { PENDING_ISLANDS_KEY, appSource, installKeepAlive, islandSource } from "../context.ts"
|
|
7
|
+
import * as keptOutlet from "../KeptOutlet.svelte"
|
|
8
|
+
import { islandsSettled, renderSettled } from "../island-html.ts"
|
|
9
|
+
import { rememberOriginal, serializeError } from "../boundary-error.ts"
|
|
10
|
+
import Root from "../Root.svelte"
|
|
11
|
+
import { lazyModules, setLazyRenderer } from "../lazy-hydration.ts"
|
|
12
|
+
import ServerComponent from "../ServerComponent.svelte"
|
|
13
|
+
import { withServer } from "../server-component.ts"
|
|
14
|
+
import ClientComponent from "../ClientComponent.svelte"
|
|
15
|
+
import type { Component } from "svelte"
|
|
16
|
+
|
|
17
|
+
installKeepAlive(keptOutlet)
|
|
18
|
+
|
|
19
|
+
setLazyRenderer((component, props, context) => renderSvelte(component, { props, context }).body)
|
|
20
|
+
|
|
21
|
+
export async function render(ctx: RenderContext): Promise<RenderResult> {
|
|
22
|
+
await setAdapterInstance(ctx.app, null)
|
|
23
|
+
const csp = ctx.scriptHashes ? { csp: { hash: true } } : ctx.nonce ? { csp: { nonce: ctx.nonce } } : {}
|
|
24
|
+
const { hashes, ...result } = await renderInPasses<RenderResult & { hashes: readonly string[] }>({
|
|
25
|
+
app: ctx.app,
|
|
26
|
+
head: ctx.head,
|
|
27
|
+
async render(headOfPass) {
|
|
28
|
+
await lazyModules()
|
|
29
|
+
const pending: Promise<void>[] = []
|
|
30
|
+
const { body, head, hashes } = await renderSvelte(Root, {
|
|
31
|
+
props: { source: appSource({ ...ctx, head: headOfPass }) },
|
|
32
|
+
context: new Map([[PENDING_ISLANDS_KEY, pending]]),
|
|
33
|
+
transformError: (error) =>
|
|
34
|
+
isRedirect(error) ? error : rememberOriginal(serializeError(publicError(ctx.app, error)), error),
|
|
35
|
+
...csp,
|
|
36
|
+
})
|
|
37
|
+
await islandsSettled(ctx.app, pending)
|
|
38
|
+
return { ...(head ? { body, head } : { body }), hashes: hashes.script }
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
for (const hash of hashes) ctx.scriptHashes?.push(`'${hash}'`)
|
|
42
|
+
return ctx.stream ? { ...result, body: oneChunk(result.body as string) } : result
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** `component` as a root of its own, its ids prefixed with `ctx.idPrefix` as `hydrateIsland` prefixes them, with each island inside it rendered in place. */
|
|
46
|
+
export async function renderComponent(
|
|
47
|
+
component: unknown,
|
|
48
|
+
props: Record<string, unknown>,
|
|
49
|
+
ctx: ComponentRenderContext,
|
|
50
|
+
): Promise<{ html: string; head?: string }> {
|
|
51
|
+
// `renderIsland` calls this inside the page's synchronous render, whose renderer state a nested render would overwrite.
|
|
52
|
+
await Promise.resolve()
|
|
53
|
+
await lazyModules()
|
|
54
|
+
const { body: html, head } = await renderSettled(async (pending) =>
|
|
55
|
+
renderSvelte(Root, {
|
|
56
|
+
props: { source: islandSource(component, props, ctx), idPrefix: `${ctx.idPrefix}-` },
|
|
57
|
+
context: new Map([[PENDING_ISLANDS_KEY, pending]]),
|
|
58
|
+
idPrefix: ctx.idPrefix,
|
|
59
|
+
transformError: (error) =>
|
|
60
|
+
isRedirect(error) ? error : rememberOriginal(serializeError(publicError(ctx.app, error)), error),
|
|
61
|
+
...(ctx.nonce ? { csp: { nonce: ctx.nonce } } : {}),
|
|
62
|
+
}),
|
|
63
|
+
)
|
|
64
|
+
return head ? { html, head } : { html }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function oneChunk(text: string): ReadableStream<Uint8Array> {
|
|
68
|
+
return new ReadableStream({
|
|
69
|
+
start(controller) {
|
|
70
|
+
controller.enqueue(new TextEncoder().encode(text))
|
|
71
|
+
controller.close()
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The server side of a client-only component renders the wrapper with no loader, so its markers match the client's hydration render. */
|
|
77
|
+
export function clientComponent(): Component<Record<string, unknown>> {
|
|
78
|
+
return withServer(ClientComponent, {})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The server side of a server-only component: the component itself inside `hamur-component`, its props checked as the client sends them. */
|
|
82
|
+
export function serverComponent(
|
|
83
|
+
name: string,
|
|
84
|
+
component: Component<Record<string, unknown>>,
|
|
85
|
+
): Component<Record<string, unknown>> {
|
|
86
|
+
return withServer(ServerComponent, { name, component })
|
|
87
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Component } from "svelte"
|
|
2
|
+
|
|
3
|
+
/** Calls the compiled `wrapper` with the props it was given and a `server` prop that reads the wrapper's own settings, as lazy hydration reads `lazy`. */
|
|
4
|
+
export function withServer(wrapper: unknown, server: object): Component<Record<string, unknown>> {
|
|
5
|
+
const call = wrapper as (anchor: unknown, props: object) => unknown
|
|
6
|
+
return ((anchor: unknown, props: object) =>
|
|
7
|
+
call(
|
|
8
|
+
anchor,
|
|
9
|
+
new Proxy(props, {
|
|
10
|
+
get: (target, key, receiver) => (key === "server" ? server : Reflect.get(target, key, receiver)),
|
|
11
|
+
}),
|
|
12
|
+
)) as never
|
|
13
|
+
}
|
package/src/store.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createSubscriber } from "svelte/reactivity"
|
|
2
|
+
|
|
3
|
+
export interface Source<T> {
|
|
4
|
+
get(): T
|
|
5
|
+
set(value: T): void
|
|
6
|
+
subscribe(listener: () => void): () => void
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function createSource<T>(initial: T): Source<T> {
|
|
10
|
+
let value = initial
|
|
11
|
+
const listeners = new Set<() => void>()
|
|
12
|
+
return {
|
|
13
|
+
get: () => value,
|
|
14
|
+
set(next) {
|
|
15
|
+
value = next
|
|
16
|
+
for (const listener of [...listeners]) listener()
|
|
17
|
+
},
|
|
18
|
+
subscribe(listener) {
|
|
19
|
+
listeners.add(listener)
|
|
20
|
+
return () => listeners.delete(listener)
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function track<T>(source: Source<T>): () => T {
|
|
26
|
+
const subscribe = createSubscriber((update) => source.subscribe(update))
|
|
27
|
+
return () => {
|
|
28
|
+
subscribe()
|
|
29
|
+
return source.get()
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/svelte.d.ts
ADDED
package/src/svg.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { svgCode, type SvgCode, type SvgNode, type SvgSource, type SvgValue } from "hamur/adapter"
|
|
2
|
+
|
|
3
|
+
const attributesCode = (code: SvgCode, attrs: Record<string, SvgValue>): string =>
|
|
4
|
+
Object.entries(attrs)
|
|
5
|
+
.map(([name, value]) => ` ${name}={${code.value(value)}}`)
|
|
6
|
+
.join("")
|
|
7
|
+
|
|
8
|
+
function nodeCode(code: SvgCode, node: SvgNode): string {
|
|
9
|
+
if (typeof node === "string" || Array.isArray(node)) return `{${code.value(node)}}`
|
|
10
|
+
return `<${node.tag}${attributesCode(code, code.attrs(node))}>${node.children.map((child) => nodeCode(code, child)).join("")}</${node.tag}>`
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function module(svg: SvgSource): { contents: string; extension: string } {
|
|
14
|
+
const code = svgCode(svg, { title: "title", props: "rest" })
|
|
15
|
+
const hidden = code.hidden ? `\n const hidden = $derived(${code.hidden} ? "true" : undefined);` : ""
|
|
16
|
+
const contents = `<script>
|
|
17
|
+
let { title, ...rest } = $props();
|
|
18
|
+
const id = $props.id();
|
|
19
|
+
const uid = id.replace(/[^\\w-]/g, "_");${hidden}
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<svg${attributesCode(code, svg.attrs)}${code.hidden ? " aria-hidden={hidden}" : ""} {...rest}>{#if title != null}<title>{title}</title>{/if}${svg.children.map((child) => nodeCode(code, child)).join("")}</svg>
|
|
23
|
+
`
|
|
24
|
+
return { contents, extension: ".svelte" }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const svgComponent = {
|
|
28
|
+
types: `import("svelte").Component<import("svelte/elements").SVGAttributes<SVGSVGElement> & { title?: string }>`,
|
|
29
|
+
module,
|
|
30
|
+
}
|
package/src/webmcp.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import "svelte/elements"
|
|
2
|
+
|
|
3
|
+
declare module "svelte/elements" {
|
|
4
|
+
interface HTMLFormAttributes {
|
|
5
|
+
toolname?: string | undefined | null
|
|
6
|
+
tooldescription?: string | undefined | null
|
|
7
|
+
toolautosubmit?: boolean | undefined | null
|
|
8
|
+
}
|
|
9
|
+
interface HTMLInputAttributes {
|
|
10
|
+
toolparamdescription?: string | undefined | null
|
|
11
|
+
}
|
|
12
|
+
interface HTMLSelectAttributes {
|
|
13
|
+
toolparamdescription?: string | undefined | null
|
|
14
|
+
}
|
|
15
|
+
interface HTMLTextareaAttributes {
|
|
16
|
+
toolparamdescription?: string | undefined | null
|
|
17
|
+
}
|
|
18
|
+
}
|