1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 |
x3
x3
x3
x3
x3
x3
x3
x6
x6
x6
x6
x6
x48
x6
x6
x6
x6
x6
x76
x76
x76
x19
x19
x6
x6
x6
x8
x8
x6
x7
x7
x6
x22
x88
x110
x22
x22
x22
x22
x220
x22
x6
x120
x15
x3
x3
|
I
I
|
import type { Arg, Directive, Promisable, RendererOptions, RendererRenderOptions } from "@mizu/internal/engine"
import type { CallbackSource, GlobSource, StringSource, URLSource } from "./generate.ts"
import type { Buffer } from "node:buffer"
import { Context, Renderer } from "@mizu/internal/engine"
import { Window } from "@mizu/internal/vdom"
import defaults from "./defaults.ts"
import { generate } from "./generate.ts"
import { mkdir, readdir, readFile as read, rm, stat, writeFile as write } from "node:fs/promises"
export type * from "@mizu/internal/engine"
export type { CallbackSource, GlobSource, StringSource, URLSource } from "./generate.ts"
export class Server {
static defaults = {
directives: defaults,
context: {},
generate: {
output: "./output",
clean: true,
fs: { stat, read, write, rm, readdir, mkdir },
},
warn: console.warn,
debug: undefined,
} as unknown as Required<ServerOptions>
constructor(options?: ServerOptions) {
this.#options = { ...Server.defaults, ...options }
this.#options.generate = { ...Server.defaults.generate, ...options?.generate }
this.#options.generate.fs = { ...Server.defaults.generate.fs, ...options?.generate?.fs }
this.#context = new Context<any>(this.#options.context)
}
readonly #options
#context
get context(): Record<PropertyKey, any> {
return this.#context.target
}
set context(context: Record<PropertyKey, unknown>) {
this.#context = new Context(context)
}
async render(content: string | Arg<Renderer["render"]>, options?: ServerRenderOptions & Pick<ServerOptions, "warn">): Promise<string> {
await using window = new Window(typeof content === "string" ? content : `<body>${content.outerHTML}</body>`)
const { directives, warn, debug, context: _context } = { ...this.#options, ...options }
const renderer = await new Renderer(window, { directives, warn, debug }).ready
let context = this.#context
if (_context) {
context = context.with(_context)
}
return await renderer.render(renderer.document.documentElement, { implicit: true, ...options, context, state: { $renderer: "server", ...options?.state }, stringify: true })
}
generate(sources: Array<StringSource | GlobSource | CallbackSource | URLSource>, options?: ServerGenerateOptions): Promise<void> {
return generate(this, sources, { ...this.#options.generate, ...options, fs: { ...this.#options.generate.fs, ...options?.fs } } as Arg<typeof generate, 2>)
}
static readonly default = new Server() as Server
}
export type ServerOptions = Pick<RendererOptions, "warn" | "debug"> & {
directives?: Array<Partial<Directive> | string>
context?: ConstructorParameters<typeof Context>[0]
generate?: ServerGenerateOptions
}
export type ServerRenderOptions = Pick<RendererRenderOptions, "implicit" | "select" | "throw"> & {
context?: Arg<Context["with"]>
state?: Arg<Renderer["render"], 1, true>["state"]
}
export type ServerGenerateOptions = {
output?: string
clean?: boolean
fs?: Partial<ServerGenerateFileSystemOptions>
}
export type ServerGenerateFileSystemOptions = {
stat: (path: string) => Promise<{ isFile: boolean | (() => boolean); isDirectory: boolean | (() => boolean) }>
read: (path: string) => Promise<Buffer>
write: (path: string, data: Buffer) => Promisable<void>
rm: (path: string, options?: { recursive?: boolean }) => Promisable<unknown>
mkdir: (path: string, options?: { recursive?: boolean }) => Promisable<unknown>
readdir: (path: string) => Promise<string[] | { name: string }[]>
}
|