@hamur/adapter-solid 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 (72) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +35 -2
  3. package/dist/chunks/browser-index-8rhv2s58.js +14 -0
  4. package/dist/chunks/browser-index-b05qbzsh.js +165 -0
  5. package/dist/chunks/browser-index-n80jt19y.js +114 -0
  6. package/dist/chunks/browser-index-ncnkshd9.js +39 -0
  7. package/dist/chunks/browser-index-r4jwfgh7.js +407 -0
  8. package/dist/src/adapter.js +556 -0
  9. package/dist/src/error-page.js +466 -0
  10. package/dist/src/index.js +1535 -0
  11. package/dist/src/keepalive.js +6 -0
  12. package/dist/src/runtime/client.js +174 -0
  13. package/dist/src/runtime/island.js +58 -0
  14. package/dist/src/runtime/server.js +138 -0
  15. package/dist/types/src/activation.d.ts +15 -0
  16. package/dist/types/src/adapter.d.ts +4 -0
  17. package/dist/types/src/async-data.d.ts +21 -0
  18. package/dist/types/src/client-component.d.ts +10 -0
  19. package/dist/types/src/client-fallback.d.ts +2 -0
  20. package/dist/types/src/context.d.ts +89 -0
  21. package/dist/types/src/error-page.d.ts +1 -0
  22. package/dist/types/src/index.d.ts +392 -0
  23. package/dist/types/src/island-html.d.ts +13 -0
  24. package/dist/types/src/island.d.ts +20 -0
  25. package/dist/types/src/jsx-locator.d.ts +6 -0
  26. package/dist/types/src/keepalive.d.ts +30 -0
  27. package/dist/types/src/lazy-hydration.d.ts +13 -0
  28. package/dist/types/src/markdown-page.d.ts +9 -0
  29. package/dist/types/src/markdown.d.ts +13 -0
  30. package/dist/types/src/plugin.d.ts +44 -0
  31. package/dist/types/src/runtime/client.d.ts +13 -0
  32. package/dist/types/src/runtime/hmr-solid.d.ts +5 -0
  33. package/dist/types/src/runtime/hmr.d.ts +27 -0
  34. package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
  35. package/dist/types/src/runtime/hydration.d.ts +10 -0
  36. package/dist/types/src/runtime/island.d.ts +8 -0
  37. package/dist/types/src/runtime/portal.d.ts +13 -0
  38. package/dist/types/src/runtime/server.d.ts +14 -0
  39. package/dist/types/src/runtime/trusted.d.ts +2 -0
  40. package/dist/types/src/server-island.d.ts +10 -0
  41. package/dist/types/src/svg.d.ts +10 -0
  42. package/package.json +53 -4
  43. package/src/activation.ts +51 -0
  44. package/src/adapter.ts +42 -0
  45. package/src/async-data.ts +105 -0
  46. package/src/client-component.ts +40 -0
  47. package/src/client-fallback.ts +2 -0
  48. package/src/context.ts +495 -0
  49. package/src/env.d.ts +3 -0
  50. package/src/error-page.ts +1 -0
  51. package/src/index.ts +1900 -0
  52. package/src/island-html.ts +58 -0
  53. package/src/island.ts +55 -0
  54. package/src/jsx-locator.ts +199 -0
  55. package/src/keepalive.ts +147 -0
  56. package/src/lazy-hydration.ts +146 -0
  57. package/src/markdown-page.ts +18 -0
  58. package/src/markdown.ts +70 -0
  59. package/src/plugin.ts +310 -0
  60. package/src/runtime/client.ts +175 -0
  61. package/src/runtime/hmr-solid.ts +19 -0
  62. package/src/runtime/hmr.ts +231 -0
  63. package/src/runtime/hydration-dev.ts +41 -0
  64. package/src/runtime/hydration.ts +9 -0
  65. package/src/runtime/island.ts +66 -0
  66. package/src/runtime/portal.ts +64 -0
  67. package/src/runtime/server.ts +160 -0
  68. package/src/runtime/trusted.ts +16 -0
  69. package/src/server-island.ts +47 -0
  70. package/src/solid.d.ts +4 -0
  71. package/src/svg.ts +53 -0
  72. package/src/webmcp.d.ts +20 -0
@@ -0,0 +1,14 @@
1
+ import type { Accessor, Component } from "solid-js";
2
+ import type { ComponentRenderContext, RenderContext, RenderResult } from "hamur/adapter";
3
+ import type { AppState } from "../context.ts";
4
+ /** The client's root guard, which the server renders too so the hydration keys agree; here it lets every failure through. */
5
+ export declare function guardedRoot(state: Accessor<AppState>): import("solid-js").Element;
6
+ export declare function render(ctx: RenderContext): Promise<RenderResult>;
7
+ /** `component` as a root of its own, its hydration keys under `<idPrefix>-` as `hydrateIsland` reads them, with each island inside it rendered in place. */
8
+ export declare function renderComponent(component: unknown, props: Record<string, unknown>, ctx: ComponentRenderContext): Promise<{
9
+ html: string;
10
+ }>;
11
+ /** The server side of a client-only component renders nothing. */
12
+ export declare function clientComponent(): Component<Record<string, unknown>>;
13
+ /** The server side of a server-only component: the component itself inside `hamur-component`, its props checked as the client sends them. */
14
+ export declare function serverComponent(name: string, component: Component<Record<string, unknown>>): Component<Record<string, unknown>>;
@@ -0,0 +1,2 @@
1
+ /** Under `server.csp.trustedTypes`, Solid writes its compiled templates through `solid-trusted-html` (`Adapter.trustedTypesPolicies`); called before the first `render` or `hydrate`. */
2
+ export declare function trustTemplates(): void;
@@ -0,0 +1,10 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ export interface ServerIslandProps<P extends Record<string, unknown> = Record<string, unknown>> {
3
+ /** The name of a component under `app/server-islands/`. */
4
+ name: string;
5
+ props?: P;
6
+ /** Rendered with the page; replaced by the island's HTML once the browser has it. */
7
+ fallback?: JSX.Element;
8
+ }
9
+ /** A component rendered on the server in its own request; the page holds its fallback, the core swaps the answer in, and Solid never renders or hydrates it. */
10
+ export declare function ServerIsland<P extends Record<string, unknown> = Record<string, unknown>>(props: ServerIslandProps<P>): JSX.Element;
@@ -0,0 +1,10 @@
1
+ import { type SvgSource } from "hamur/adapter";
2
+ declare function module(svg: SvgSource): {
3
+ contents: string;
4
+ extension: string;
5
+ };
6
+ export declare const svgComponent: {
7
+ types: string;
8
+ module: typeof module;
9
+ };
10
+ export {};
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@hamur/adapter-solid",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/productdevbook/hamur.git",
9
+ "directory": "packages/adapter-solid"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/types/src/index.d.ts",
14
+ "default": "./dist/src/index.js"
15
+ },
16
+ "./adapter": {
17
+ "types": "./dist/types/src/adapter.d.ts",
18
+ "default": "./dist/src/adapter.js"
19
+ },
20
+ "./runtime/server": {
21
+ "types": "./dist/types/src/runtime/server.d.ts",
22
+ "default": "./dist/src/runtime/server.js"
23
+ },
24
+ "./runtime/client": {
25
+ "types": "./dist/types/src/runtime/client.d.ts",
26
+ "default": "./dist/src/runtime/client.js"
27
+ },
28
+ "./runtime/keepalive": {
29
+ "types": "./dist/types/src/keepalive.d.ts",
30
+ "default": "./dist/src/keepalive.js"
31
+ },
32
+ "./runtime/island": {
33
+ "types": "./dist/types/src/runtime/island.d.ts",
34
+ "default": "./dist/src/runtime/island.js"
35
+ },
36
+ "./error-page": {
37
+ "types": "./dist/types/src/error-page.d.ts",
38
+ "default": "./dist/src/error-page.js"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "files": [
43
+ "dist",
44
+ "src"
45
+ ],
46
+ "dependencies": {},
47
+ "peerDependencies": {
48
+ "solid-js": "^2.0.4",
49
+ "@solidjs/web": "^2.0.4",
50
+ "@solidjs/babel-plugin": "^2.0.3",
51
+ "@babel/core": "^8.1.4",
52
+ "@babel/preset-typescript": "^8.0.7",
53
+ "hamur": "^0.0.1"
54
+ }
55
+ }
@@ -0,0 +1,51 @@
1
+ /** The shown state of one keep-alive wrapper and the `onPageActivated`/`onPageDeactivated` functions under it. */
2
+ export interface Activation {
3
+ hidden: boolean
4
+ readonly parent: Activation | undefined
5
+ readonly children: Set<Activation>
6
+ readonly on: Set<() => void>
7
+ readonly off: Set<() => void>
8
+ }
9
+
10
+ export function activation(parent: Activation | undefined, hidden: boolean): Activation {
11
+ const own: Activation = { hidden, parent, children: new Set(), on: new Set(), off: new Set() }
12
+ parent?.children.add(own)
13
+ return own
14
+ }
15
+
16
+ export function detach(own: Activation): void {
17
+ own.parent?.children.delete(own)
18
+ }
19
+
20
+ /** Whether a page under `own` is on screen: no wrapper above it is hidden. */
21
+ export function shown(own: Activation | undefined): boolean {
22
+ return !own || (!own.hidden && shown(own.parent))
23
+ }
24
+
25
+ let queue: Array<{ shown: boolean; fn: () => void }> | undefined
26
+
27
+ /** Runs `fn` once the commit is done; within one commit every deactivation runs before any activation, so a hide comes before the show it makes room for. */
28
+ export function later(isShown: boolean, fn: () => void): void {
29
+ if (!queue) {
30
+ queue = []
31
+ queueMicrotask(() => {
32
+ const run = queue!
33
+ queue = undefined
34
+ for (const item of run) if (!item.shown) item.fn()
35
+ for (const item of run) if (item.shown) item.fn()
36
+ })
37
+ }
38
+ queue.push({ shown: isShown, fn })
39
+ }
40
+
41
+ function fire(own: Activation, isShown: boolean): void {
42
+ for (const fn of isShown ? own.on : own.off) later(isShown, fn)
43
+ for (const child of own.children) if (!child.hidden) fire(child, isShown)
44
+ }
45
+
46
+ export function setHidden(own: Activation, hidden: boolean): void {
47
+ if (own.hidden === hidden) return
48
+ const before = shown(own)
49
+ own.hidden = hidden
50
+ if (shown(own) !== before) fire(own, !before)
51
+ }
package/src/adapter.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { readFileSync } from "node:fs"
2
+ import { ADAPTER_CONTRACT, defineAdapter } from "hamur/adapter"
3
+ import { clientComponentModule, serverComponentModule } from "hamur/adapter/build"
4
+ import { solidDevBuild, solidHydrationPlugin, solidPlugin } from "./plugin.ts"
5
+ import { svgComponent } from "./svg.ts"
6
+ import { markdownComponent } from "./markdown-page.ts"
7
+
8
+ /** The core's peer check only asks that each peer resolves, so a `solid-js` 1 left in the app would fail late inside the compiler. */
9
+ export function assertSolid2(rootDir: string): void {
10
+ const { version } = JSON.parse(readFileSync(Bun.resolveSync("solid-js/package.json", rootDir), "utf8")) as {
11
+ version: string
12
+ }
13
+ if (version.split(".")[0] === "2") return
14
+ throw new Error(
15
+ `@hamur/adapter-solid supports Solid 2 only: solid-js ${version} is installed in ${rootDir}. Install it with: bun add solid-js@latest @solidjs/web@latest @solidjs/babel-plugin@latest`,
16
+ )
17
+ }
18
+
19
+ export default defineAdapter({
20
+ name: "solid",
21
+ contract: ADAPTER_CONTRACT,
22
+ extensions: [".tsx", ".jsx"],
23
+ clientEntry: "@hamur/adapter-solid/runtime/client",
24
+ serverEntry: "@hamur/adapter-solid/runtime/server",
25
+ keepAliveOutlet: "@hamur/adapter-solid/runtime/keepalive",
26
+ islandEntry: "@hamur/adapter-solid/runtime/island",
27
+ errorPage: "@hamur/adapter-solid/error-page",
28
+ plugins: (ctx) => {
29
+ assertSolid2(ctx.rootDir)
30
+ return []
31
+ },
32
+ transforms: (ctx) => [
33
+ solidPlugin(ctx),
34
+ ...(ctx.dev && ctx.target === "client" ? [solidDevBuild(ctx), solidHydrationPlugin()] : []),
35
+ ],
36
+ peers: ["solid-js", "@solidjs/web", "@solidjs/babel-plugin", "@babel/core", "@babel/preset-typescript"],
37
+ trustedTypesPolicies: ["solid-trusted-html"],
38
+ svgComponent,
39
+ markdownComponent,
40
+ serverComponent: { module: (component) => serverComponentModule(component, "@hamur/adapter-solid/runtime") },
41
+ clientComponent: { module: (component) => clientComponentModule(component, "@hamur/adapter-solid/runtime") },
42
+ })
@@ -0,0 +1,105 @@
1
+ import { asyncDataKey, fetchKey, isPreviewMode } from "hamur/adapter"
2
+ import { $fetch } from "hamur/app"
3
+ import type { $Fetch, AppContext, AsyncDataOptions, FetchOptions } from "hamur/app"
4
+ import type { AsyncDataHandler, FetchRequest, GetCachedData, GetCachedDataCause, HandlerReference } from "hamur/adapter"
5
+
6
+ export const valueOf = <T>(value: T | (() => T)): T => (typeof value === "function" ? (value as () => T)() : value)
7
+
8
+ // A function is never a body to send, so a function `query` or `body` is a getter.
9
+ export function requestOf({ query, body, ...rest }: FetchRequest): FetchOptions {
10
+ const request: FetchOptions = rest
11
+ const resolvedQuery = valueOf(query)
12
+ const resolvedBody = valueOf(body)
13
+ if (resolvedQuery !== undefined) request.query = resolvedQuery
14
+ if (resolvedBody !== undefined) request.body = resolvedBody
15
+ return request
16
+ }
17
+
18
+ /** A getter `useFetch` key; each key's handler fetches the url and request that key was made from. */
19
+ export function fetchSlots<R>(
20
+ url: string | (() => string),
21
+ options: FetchRequest,
22
+ fetcher: $Fetch = $fetch,
23
+ fixedKey?: string,
24
+ ): { keyOf: () => string; handlerFor: (key: string) => AsyncDataHandler<R> } {
25
+ const requests = new Map<string, { url: string; request: FetchOptions }>()
26
+ return {
27
+ keyOf() {
28
+ const target = valueOf(url)
29
+ const request = requestOf(options)
30
+ const key = fixedKey ?? fetchKey(target, request)
31
+ requests.set(key, { url: target, request })
32
+ return key
33
+ },
34
+ handlerFor(key) {
35
+ const { url: target, request } = requests.get(key)!
36
+ return ({ signal }) => fetcher<R>(target, { ...request, signal: signalOf(request.signal, signal) })
37
+ },
38
+ }
39
+ }
40
+
41
+ /** The key of `useAsyncData(fn, input)` for the input as it reads now; each key's handler calls `fn` with the input that key was made from. */
42
+ export function handlerSlots<R>(
43
+ app: AppContext,
44
+ fn: HandlerReference,
45
+ input: unknown,
46
+ ): { keyOf: () => string; handlerFor: (key: string) => AsyncDataHandler<R> } {
47
+ const inputs = new Map<string, unknown>()
48
+ return {
49
+ keyOf() {
50
+ const value = valueOf(input)
51
+ const key = asyncDataKey(fn, value)
52
+ inputs.set(key, value)
53
+ return key
54
+ },
55
+ handlerFor(key) {
56
+ const value = inputs.get(key)
57
+ return ({ signal }) =>
58
+ (fn as (input: unknown, init: { signal: AbortSignal; event: AppContext["event"] }) => Promise<R>)(value, {
59
+ signal,
60
+ event: app.event,
61
+ })
62
+ },
63
+ }
64
+ }
65
+
66
+ /** The restored payload entry's data while the app hydrates, else `undefined`. */
67
+ export function defaultGetCachedData<T>(key: string, app: AppContext): T | undefined {
68
+ if (!app.asyncData.hydrating || isPreviewMode(app)) return undefined
69
+ const entry = app.asyncData.get<T>(key)
70
+ return entry?.status === "success" ? entry.data : undefined
71
+ }
72
+
73
+ /** `refresh()`, `execute()` and a `watch` change: ask `getCachedData` with their cause, run `handler` when it has nothing. */
74
+ export function refreshCached<T, R>(
75
+ app: AppContext,
76
+ key: string,
77
+ handler: AsyncDataHandler<R>,
78
+ options: AsyncDataOptions<T, R>,
79
+ getCachedData: GetCachedData<T>,
80
+ cause: GetCachedDataCause,
81
+ ): Promise<void> {
82
+ const cached = getCachedData(key, app, { cause })
83
+ if (cached !== undefined) {
84
+ app.asyncData.fetch<T, R>(key, handler, { ...options, cached })
85
+ return Promise.resolve()
86
+ }
87
+ if (!app.asyncData.has(key)) app.asyncData.fetch<T, R>(key, handler, { ...options, immediate: false })
88
+ return app.asyncData.refresh(key, handler)
89
+ }
90
+
91
+ const kept = new WeakMap<object, Set<string>>()
92
+
93
+ // Nuxt purges no entry whose caller reads it back through its own getCachedData: the entry is that cache.
94
+ export function keepCached<T>(app: AppContext, key: string, getCachedData: GetCachedData<T>): void {
95
+ if (getCachedData === defaultGetCachedData) return
96
+ let keys = kept.get(app.asyncData)
97
+ if (!keys) kept.set(app.asyncData, (keys = new Set()))
98
+ if (keys.has(key)) return
99
+ keys.add(key)
100
+ app.asyncData.subscribe(key, () => {})
101
+ }
102
+
103
+ /** The run's signal, combined with a `signal` the caller gave to `useFetch`. */
104
+ export const signalOf = (own: AbortSignal | undefined, run: AbortSignal): AbortSignal =>
105
+ own ? AbortSignal.any([own, run]) : run
@@ -0,0 +1,40 @@
1
+ import {
2
+ Loading,
3
+ createComponent,
4
+ createContext,
5
+ createMemo,
6
+ createSignal,
7
+ lazy,
8
+ onSettled,
9
+ useContext,
10
+ } from "solid-js"
11
+ import type { Component } from "solid-js"
12
+ import type { JSX } from "@solidjs/web"
13
+
14
+ /** Set by `ClientOnly` around its children; a client-only component below shows its `fallback` while its chunk loads. */
15
+ export const ClientOnlyContext = createContext<{ readonly fallback: JSX.Element } | null>(null)
16
+
17
+ /** The client side of a client-only component: nothing in the hydration render, the file's chunk once mounted. */
18
+ export function clientComponent(
19
+ load: () => Promise<{ default: Component<Record<string, unknown>> }>,
20
+ ): Component<Record<string, unknown>> {
21
+ const Loaded = lazy(load)
22
+ return (props) => {
23
+ const scope = useContext(ClientOnlyContext)
24
+ const [hydrated, setHydrated] = createSignal(scope !== null)
25
+ onSettled(() => {
26
+ setHydrated(true)
27
+ })
28
+ return createMemo(() => {
29
+ if (!hydrated()) return null
30
+ return createComponent(Loading, {
31
+ get fallback() {
32
+ return scope?.fallback ?? null
33
+ },
34
+ get children() {
35
+ return createComponent(Loaded, props)
36
+ },
37
+ })
38
+ }) as unknown as JSX.Element
39
+ }
40
+ }
@@ -0,0 +1,2 @@
1
+ /** Client: the ids of the `ClientFallback` boundaries whose children the server could not render; they render them fresh once. */
2
+ export const clientFallbackIds = new Set<string>()