deno.land / x / ultra@v2.3.8 / lib / server.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
import { ULTRA_COMPILER_PATH } from "./constants.ts";import { assert, dotenv, Hono, resolve, toFileUrl } from "./deps.ts";import { ensureMinDenoVersion } from "./dev/ensureMinDenoVersion.ts";import { log } from "./logger.ts";import { serveStatic } from "./middleware/serveStatic.ts";import { CreateServerOptions, Env, Mode } from "./types.ts";import { UltraServer } from "./ultra.ts";import { resolveImportMapPath } from "./utils/import-map.ts";
/** * Dotenv */await dotenv({ export: true });
/** * Check if we are running on Deno Deploy and set the mode to production * if the mode hasn't been specified via the environment. */const modeFromEnv = Deno.env.get("ULTRA_MODE") || (Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined ? "production" : undefined);
const defaultOptions = { mode: (modeFromEnv || "development") as Mode, enableEsModuleShims: true, esModuleShimsPath: "https://ga.jspm.io/npm:es-module-shims@1.8.0/dist/es-module-shims.js",};
export async function createServer< // deno-lint-ignore no-explicit-any E extends Env = any, // deno-lint-ignore ban-types S = {}, BasePath extends string = "/",>( options: CreateServerOptions = {},) { const resolvedOptions = { ...defaultOptions, ...options, };
assertServerOptions(resolvedOptions);
const { mode = "development", browserEntrypoint, enableEsModuleShims, esModuleShimsPath, } = resolvedOptions;
const root = Deno.cwd(); const assetManifestPath = toFileUrl(resolve(root, "asset-manifest.json"));
const server = new UltraServer<E, S, BasePath>(root, { mode, entrypoint: browserEntrypoint, importMapPath: options.importMapPath ? resolveImportMapPath(mode, root, options.importMapPath) : undefined, assetManifestPath: String(assetManifestPath), enableEsModuleShims, esModuleShimsPath, });
await server.init();
// We always try to serve public assets before anything else. // deno-fmt-ignore server.get("*", serveStatic({ root: resolve(root, "./public"), cache: mode !== "development", }));
// Serve anything else static at "/" // deno-fmt-ignore server.get("/ultra/*", (context, next) => { const path = new URL(context.req.url).pathname.slice(`/ultra`.length) return serveStatic({ root: server.ultraDir, path: path, cache: mode !== "development", })(context, next); });
// Serve anything else static at "/" // deno-fmt-ignore server.get("*", serveStatic({ root: resolve(root, "./"), cache: mode !== "development", }));
if (mode === "development") { log.info("Loading compiler"); const { compiler } = await import("./middleware/compiler.ts");
// deno-fmt-ignore server.get(`${ULTRA_COMPILER_PATH}/*`, compiler({ root, ...options.compilerOptions, })); }
return server;}
export function createRouter< // deno-lint-ignore no-explicit-any E extends Env = any, // deno-lint-ignore ban-types S = {}, BasePath extends string = "/",>() { return new Hono<E, S, BasePath>();}
export function assertServerOptions( options: CreateServerOptions,): options is Required<CreateServerOptions> { try { /** * Ensure we are running a supported Deno version */ options.mode === "development" && ensureMinDenoVersion();
/** * Assert that we are provided a valid "mode" */ assert( ["development", "production"].includes(options.mode!), `Invalid value supplied for "mode", expected either "production" or "development" received "${options.mode}"`, );
/** * Assert that we are provided an importMapPath if a browserEntrypoint is provided */ if (options.browserEntrypoint) { assert( options.importMapPath, "No importMapPath was supplied, yet a browserEntrypoint has been set.", ); }
return true; } catch (error) { throw new Error(error.message); }}
ultra

Version Info

Tagged at
8 months ago