deno.land / x / ultra@v2.3.8 / lib / ultra.ts

نووسراو ببینە
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import type { ReactElement } from "react";import { ULTRA_COMPILER_PATH } from "./constants.ts";import { fromFileUrl, Hono, logger, relative, resolve, sprintf,} from "./deps.ts";import { log } from "./logger.ts";import { renderToStream } from "./render.ts";import { Context, Env, ImportMap, Mode } from "./types.ts";import { toUltraUrl } from "./utils/url.ts";
type UltraServerRenderOptions = { entrypoint?: string; generateStaticHTML?: boolean; enableEsModuleShims?: boolean; disableHydration?: boolean; flushEffectsToHead?: boolean;};
type UltraServerOptions = { mode: Mode; importMapPath?: string; assetManifestPath: string; enableEsModuleShims?: boolean; esModuleShimsPath?: string; entrypoint?: string;};
export class UltraServer< E extends Env = Env, // deno-lint-ignore ban-types S = {}, BasePath extends string = "/",> extends Hono<E, S, BasePath> { public importMap: ImportMap | undefined; public assetManifest: Map<string, string> | undefined;
public mode: Mode; public baseUrl: string; public importMapPath?: string; public assetManifestPath: string; public enableEsModuleShims?: boolean; public esModuleShimsPath?: string; public entrypoint?: string; public ultraDir?: string;
constructor( public root: string, options: UltraServerOptions, ) { super();
this.mode = options.mode; this.importMapPath = options.importMapPath; this.assetManifestPath = options.assetManifestPath; this.enableEsModuleShims = options.enableEsModuleShims; this.esModuleShimsPath = options.esModuleShimsPath; this.entrypoint = options.entrypoint; this.baseUrl = this.mode === "development" ? `${ULTRA_COMPILER_PATH}/` : "/";
const logFn = options.mode === "development" ? (message: string) => log.info(message) : (message: string) => console.info(message);
this.use("*", logger(logFn)); }
async init() { log.debug("Initialising server");
/** * Parse the importMap if provided */ this.importMap = this.importMapPath ? await this.#parseJsonFile(this.importMapPath) : undefined;
/** * Parse the provided asset manifest if we have an entrypoint */ const assetManifest: [string, string][] | undefined = this.mode === "production" ? await this.#parseJsonFile(this.assetManifestPath) : undefined;
this.assetManifest = assetManifest ? new Map(assetManifest) : undefined;
/** * Prepare the entrypoint if provided an importMap */ if (this.importMap) { this.#importMapHandler(this.importMap); this.entrypoint = this.#prepareEntrypoint( this.importMap, this.entrypoint, ); }
// Validate this.#valid(); }
render( Component: ReactElement, options?: UltraServerRenderOptions, ) { return this.renderWithContext(Component, undefined, options); }
renderWithContext( Component: ReactElement, context: Context | undefined, options?: UltraServerRenderOptions, ) { log.debug("Rendering component");
let bootstrapModules: string[] = [];
if (options?.entrypoint) { options.entrypoint = options?.entrypoint && this.importMap ? this.#prepareEntrypoint(this.importMap, options.entrypoint) : undefined; }
const entrypoint = options?.entrypoint || this.entrypoint;
if (entrypoint) { bootstrapModules = [ entrypoint.startsWith("file://") ? fromFileUrl(entrypoint) : entrypoint, ]; }
return renderToStream(Component, context, { mode: this.mode, baseUrl: this.baseUrl, assetManifest: this.assetManifest, importMap: this.importMap, enableEsModuleShims: this.enableEsModuleShims, esModuleShimsPath: this.esModuleShimsPath, bootstrapModules, ...options, }); }
async #parseJsonFile<T>(path: string): Promise<T> { try { log.debug(sprintf("Parsing JSON: %s", path));
const bytes = await fetch(path).then((response) => response.arrayBuffer() ); const content = new TextDecoder().decode(bytes);
const json = JSON.parse(content); log.debug(json);
return json; } catch (cause) { throw new Error(sprintf("Failed to parse JSON file at path: %s", path), { cause, }); } }
#importMapHandler(importMap: ImportMap | undefined) { if (importMap?.imports) { const ultraUrl = importMap.imports["ultra/"]; // Set importMap for ultra/ framework if (ultraUrl && !ultraUrl.startsWith("http")) { if (ultraUrl.startsWith("/")) { this.ultraDir = ultraUrl; } else { this.ultraDir = resolve(Deno.cwd(), ultraUrl); } importMap.imports["ultra/"] = "/ultra/"; } } }
#valid() { // Check we have an importMap if we we're provided an entrypoint if (!this.importMap && this.entrypoint) { throw new Error("Import map has not been parsed."); } }
#prepareEntrypoint(importMap: ImportMap, entrypoint?: string) { if (!entrypoint) { log.debug("No entrypoint provided, hydration disabled."); return entrypoint; }
log.debug(sprintf("Resolving entrypoint: %s", entrypoint));
let entrypointSpecifier = `./${ relative(this.root, fromFileUrl(entrypoint)) }`;
if (this.mode === "production") { for (const [specifier, resolved] of Object.entries(importMap.imports)) { if (specifier === entrypointSpecifier) { entrypointSpecifier = resolved.replace("./", "/"); } } } else { entrypointSpecifier = toUltraUrl(this.root, entrypoint, this.mode)!; }
log.debug(sprintf("Resolved entrypoint: %s", entrypointSpecifier));
return entrypointSpecifier; }}
ultra

Version Info

Tagged at
8 months ago