toiljs 0.0.15 → 0.0.19
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/.babelrc +13 -13
- package/.gitattributes +2 -2
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
- package/.github/ISSUE_TEMPLATE/bug_report.yml +90 -90
- package/.github/ISSUE_TEMPLATE/config.yml +8 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
- package/.github/PULL_REQUEST_TEMPLATE.md +43 -43
- package/.github/changelog-config.json +45 -45
- package/.github/dependabot.yml +27 -27
- package/.github/workflows/ci.yml +191 -191
- package/.prettierrc.json +11 -11
- package/.vscode/settings.json +9 -9
- package/CHANGELOG.md +116 -5
- package/LICENSE +187 -187
- package/README.md +524 -315
- package/as-pect.asconfig.json +34 -34
- package/as-pect.config.js +65 -65
- package/assets/logo.svg +36 -36
- package/build/backend/.tsbuildinfo +1 -1
- package/build/backend/index.d.ts +1 -0
- package/build/backend/index.js +20 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +1320 -696
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/dev/devtools.d.ts +6 -0
- package/build/client/dev/devtools.js +479 -0
- package/build/client/dev/error-overlay.d.ts +9 -0
- package/build/client/dev/error-overlay.js +19 -4
- package/build/client/errors.d.ts +1 -0
- package/build/client/errors.js +3 -0
- package/build/client/index.d.ts +2 -0
- package/build/client/index.js +2 -0
- package/build/client/navigation/prefetch.d.ts +1 -0
- package/build/client/navigation/prefetch.js +35 -0
- package/build/client/routing/Router.js +1 -1
- package/build/client/routing/hooks.js +6 -2
- package/build/client/routing/loader.d.ts +23 -0
- package/build/client/routing/loader.js +53 -7
- package/build/client/routing/mount.js +4 -3
- package/build/client/rpc.d.ts +1 -0
- package/build/client/rpc.js +37 -0
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +16 -0
- package/build/compiler/config.js +9 -0
- package/build/compiler/docs.js +78 -21
- package/build/compiler/generate.js +5 -4
- package/build/compiler/index.d.ts +3 -2
- package/build/compiler/index.js +2 -2
- package/build/compiler/plugin.js +228 -0
- package/build/compiler/prerender.d.ts +1 -0
- package/build/compiler/prerender.js +1 -1
- package/build/compiler/seo.d.ts +1 -1
- package/build/compiler/seo.js +20 -5
- package/build/compiler/ssg.js +39 -2
- package/build/compiler/vite.js +25 -0
- package/build/io/.tsbuildinfo +1 -1
- package/build/io/codec.d.ts +54 -0
- package/build/io/codec.js +143 -0
- package/build/io/index.d.ts +1 -2
- package/build/io/index.js +1 -2
- package/build/logger/.tsbuildinfo +1 -1
- package/build/shared/.tsbuildinfo +1 -1
- package/eslint.config.js +48 -48
- package/examples/basic/client/404.tsx +11 -11
- package/examples/basic/client/components/.gitkeep +1 -1
- package/examples/basic/client/global-error.tsx +13 -13
- package/examples/basic/client/layout.tsx +25 -25
- package/examples/basic/client/public/images/.gitkeep +1 -1
- package/examples/basic/client/public/images/logo.svg +36 -36
- package/examples/basic/client/public/robots.txt +2 -2
- package/examples/basic/client/routes/docs/[...slug].tsx +12 -12
- package/examples/basic/client/routes/features/error/error.tsx +16 -16
- package/examples/basic/client/routes/features/index.tsx +1 -1
- package/examples/basic/client/routes/features/template/b.tsx +14 -14
- package/examples/basic/client/routes/files/[[...slug]].tsx +21 -21
- package/examples/basic/client/routes/gallery/layout.tsx +13 -13
- package/examples/basic/client/routes/io.tsx +23 -24
- package/examples/basic/client/routes/loader-demo/loading.tsx +13 -13
- package/examples/basic/client/routes/rest.tsx +74 -0
- package/examples/basic/client/routes/rpc.tsx +43 -0
- package/examples/basic/client/routes/search.tsx +61 -61
- package/examples/basic/client/toil.tsx +5 -5
- package/package.json +167 -148
- package/presets/eslint.js +88 -88
- package/presets/no-uint8array-tostring.js +200 -200
- package/presets/prettier-plugin.js +51 -0
- package/presets/prettier.json +19 -18
- package/presets/tsconfig.json +37 -37
- package/server/runtime/README.md +97 -0
- package/server/runtime/abort/abort.ts +27 -0
- package/server/runtime/env/Server.ts +61 -0
- package/server/runtime/envelope.ts +191 -0
- package/server/runtime/exports/index.ts +52 -0
- package/server/runtime/handlers/ToilHandler.ts +34 -0
- package/server/runtime/index.ts +26 -0
- package/server/runtime/lang/Potential.ts +5 -0
- package/server/runtime/memory.ts +81 -0
- package/server/runtime/request.ts +55 -0
- package/server/runtime/response.ts +86 -0
- package/server/runtime/rest/Rest.ts +39 -0
- package/server/runtime/rest/RestHandler.ts +20 -0
- package/server/runtime/rest/RouteContext.ts +82 -0
- package/server/runtime/rest/match.ts +48 -0
- package/server/runtime/tsconfig.json +7 -0
- package/src/backend/index.ts +202 -160
- package/src/cli/create.ts +15 -5
- package/src/cli/diagnostics.ts +81 -0
- package/src/cli/doctor.ts +384 -7
- package/src/cli/index.ts +11 -2
- package/src/cli/proc.ts +50 -50
- package/src/cli/updates.ts +69 -69
- package/src/cli/validate.ts +31 -31
- package/src/client/channel/channel.ts +146 -146
- package/src/client/components/Form.tsx +65 -65
- package/src/client/components/Script.tsx +113 -113
- package/src/client/components/Slot.tsx +21 -21
- package/src/client/dev/devtools.tsx +1018 -0
- package/src/client/dev/error-overlay.tsx +30 -4
- package/src/client/errors.ts +11 -0
- package/src/client/head/head.ts +167 -167
- package/src/client/head/metadata.ts +112 -112
- package/src/client/index.ts +91 -89
- package/src/client/navigation/NavLink.tsx +86 -86
- package/src/client/navigation/navigation.ts +235 -235
- package/src/client/navigation/prefetch.ts +169 -130
- package/src/client/navigation/scroll.ts +53 -53
- package/src/client/routing/Router.tsx +8 -2
- package/src/client/routing/action.ts +122 -122
- package/src/client/routing/error-boundary.tsx +43 -43
- package/src/client/routing/hooks.ts +21 -6
- package/src/client/routing/loader.ts +325 -235
- package/src/client/routing/match.ts +47 -47
- package/src/client/routing/mount.tsx +54 -52
- package/src/client/routing/params-context.ts +10 -10
- package/src/client/routing/slot-context.ts +7 -7
- package/src/client/rpc.ts +64 -0
- package/src/client/search/search.ts +189 -189
- package/src/client/search/use-page-search.ts +73 -73
- package/src/client/types.ts +73 -73
- package/src/compiler/config.ts +221 -182
- package/src/compiler/docs.ts +285 -228
- package/src/compiler/generate.ts +395 -394
- package/src/compiler/index.ts +66 -57
- package/src/compiler/pages.ts +70 -70
- package/src/compiler/plugin.ts +258 -2
- package/src/compiler/prerender.ts +156 -156
- package/src/compiler/seo.ts +417 -390
- package/src/compiler/ssg.ts +171 -126
- package/src/compiler/vite.ts +34 -0
- package/src/io/FastMap.ts +151 -127
- package/src/io/FastSet.ts +15 -1
- package/src/io/codec.ts +217 -0
- package/src/io/index.ts +10 -11
- package/src/io/lengths.ts +14 -14
- package/src/io/types.ts +19 -18
- package/src/logger/index.ts +22 -22
- package/src/shared/index.ts +10 -10
- package/std/client/index.d.ts +15 -15
- package/std/client/package.json +3 -3
- package/test/assembly/example.spec.ts +17 -7
- package/test/channel.test.ts +21 -21
- package/test/doctor.test.ts +65 -0
- package/test/dom/Link.test.tsx +47 -47
- package/test/dom/NavLink.test.tsx +37 -37
- package/test/dom/error-overlay.test.tsx +44 -44
- package/test/dom/loader.test.tsx +121 -121
- package/test/dom/navigation.test.ts +59 -59
- package/test/dom/revalidate.test.tsx +38 -38
- package/test/dom/route-head.test.tsx +78 -78
- package/test/dom/router-loading.test.tsx +44 -44
- package/test/dom/scroll.test.ts +56 -56
- package/test/dom/use-metadata.test.tsx +58 -58
- package/test/errors.test.ts +21 -0
- package/test/io.test.ts +117 -93
- package/test/navlink.test.ts +28 -28
- package/test/placeholder.test.ts +9 -9
- package/test/prettier-plugin.test.ts +46 -0
- package/test/routes.test.ts +76 -76
- package/test/rpc.test.ts +50 -0
- package/test/seo.test.ts +175 -164
- package/test/slot-layouts.test.ts +69 -69
- package/test/ssg.test.ts +36 -36
- package/test/update.test.ts +44 -44
- package/test/validate.test.ts +42 -42
- package/tests/data-parity/generated-parity.ts +99 -0
- package/tests/data-parity/parity.ts +80 -0
- package/tests/data-parity/spec.ts +46 -0
- package/toil-routes.d.ts +7 -0
- package/tsconfig.backend.json +13 -13
- package/tsconfig.base.json +35 -35
- package/tsconfig.cli.json +13 -13
- package/tsconfig.client.json +14 -14
- package/tsconfig.compiler.json +13 -13
- package/tsconfig.io.json +12 -12
- package/tsconfig.json +22 -22
- package/tsconfig.logger.json +12 -12
- package/tsconfig.server.json +10 -10
- package/tsconfig.shared.json +12 -12
- package/vitest.config.ts +26 -26
- package/.idea/codeStyles/Project.xml +0 -54
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/prettier.xml +0 -7
- package/.idea/toiljs.iml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.toil/entry.tsx +0 -9
- package/.toil/index.html +0 -12
- package/.toil/routes.ts +0 -9
- package/build/cli/configure.d.ts +0 -16
- package/build/cli/configure.js +0 -272
- package/build/cli/create.d.ts +0 -16
- package/build/cli/create.js +0 -420
- package/build/cli/diagnostics.d.ts +0 -55
- package/build/cli/diagnostics.js +0 -333
- package/build/cli/doctor.d.ts +0 -6
- package/build/cli/doctor.js +0 -249
- package/build/cli/features.d.ts +0 -25
- package/build/cli/features.js +0 -107
- package/build/cli/index.d.ts +0 -2
- package/build/cli/proc.d.ts +0 -6
- package/build/cli/proc.js +0 -31
- package/build/cli/ui.d.ts +0 -9
- package/build/cli/ui.js +0 -75
- package/build/cli/update.d.ts +0 -7
- package/build/cli/update.js +0 -117
- package/build/cli/updates.d.ts +0 -10
- package/build/cli/updates.js +0 -45
- package/build/cli/validate.d.ts +0 -4
- package/build/cli/validate.js +0 -19
- package/build/client/Link.d.ts +0 -8
- package/build/client/Link.js +0 -44
- package/build/client/NavLink.d.ts +0 -14
- package/build/client/NavLink.js +0 -37
- package/build/client/Router.d.ts +0 -7
- package/build/client/Router.js +0 -55
- package/build/client/channel.d.ts +0 -23
- package/build/client/channel.js +0 -94
- package/build/client/error-boundary.d.ts +0 -16
- package/build/client/error-boundary.js +0 -19
- package/build/client/head.d.ts +0 -26
- package/build/client/head.js +0 -87
- package/build/client/hooks.d.ts +0 -17
- package/build/client/hooks.js +0 -48
- package/build/client/lazy.d.ts +0 -16
- package/build/client/lazy.js +0 -53
- package/build/client/match.d.ts +0 -2
- package/build/client/match.js +0 -32
- package/build/client/mount.d.ts +0 -2
- package/build/client/mount.js +0 -13
- package/build/client/navigation.d.ts +0 -13
- package/build/client/navigation.js +0 -97
- package/build/client/params-context.d.ts +0 -2
- package/build/client/params-context.js +0 -2
- package/build/client/prefetch.d.ts +0 -11
- package/build/client/prefetch.js +0 -100
- package/build/client/runtime.d.ts +0 -31
- package/build/client/runtime.js +0 -112
- package/build/client/scroll.d.ts +0 -8
- package/build/client/scroll.js +0 -36
- package/build/io/BinaryReader.d.ts +0 -44
- package/build/io/BinaryReader.js +0 -244
- package/build/io/BinaryWriter.d.ts +0 -44
- package/build/io/BinaryWriter.js +0 -297
- package/build/server/release.wasm +0 -0
- package/build/server/release.wat +0 -9
- package/src/io/BinaryReader.ts +0 -340
- package/src/io/BinaryWriter.ts +0 -385
- package/src/server/index.ts +0 -10
- package/src/server/main.ts +0 -13
- package/src/server/tsconfig.json +0 -4
- package/toil-env.d.ts +0 -16
- package/toilconfig.json +0 -30
package/src/compiler/index.ts
CHANGED
|
@@ -1,57 +1,66 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
import { build as viteBuild, createServer, type ViteDevServer } from 'vite';
|
|
5
|
-
import { startBackend, type RunningBackend } from 'toiljs/backend';
|
|
6
|
-
|
|
7
|
-
import { loadConfig } from './config.js';
|
|
8
|
-
import { generate } from './generate.js';
|
|
9
|
-
import { prerenderStaticParams } from './ssg.js';
|
|
10
|
-
import { createViteConfig } from './vite.js';
|
|
11
|
-
|
|
12
|
-
export interface ToilCommandOptions {
|
|
13
|
-
readonly root?: string;
|
|
14
|
-
readonly port?: number;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
server
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
await
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
export
|
|
53
|
-
export {
|
|
54
|
-
export {
|
|
55
|
-
export
|
|
56
|
-
export
|
|
57
|
-
export type {
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { build as viteBuild, createServer, type ViteDevServer } from 'vite';
|
|
5
|
+
import { startBackend, type RunningBackend } from 'toiljs/backend';
|
|
6
|
+
|
|
7
|
+
import { loadConfig } from './config.js';
|
|
8
|
+
import { generate } from './generate.js';
|
|
9
|
+
import { prerenderStaticParams } from './ssg.js';
|
|
10
|
+
import { createViteConfig } from './vite.js';
|
|
11
|
+
|
|
12
|
+
export interface ToilCommandOptions {
|
|
13
|
+
readonly root?: string;
|
|
14
|
+
readonly port?: number;
|
|
15
|
+
/** Bind host for `start`. Defaults to loopback (`127.0.0.1`); pass `0.0.0.0` to expose. */
|
|
16
|
+
readonly host?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Starts the Vite dev server (HMR + transforms) for the client app. Returns the running server. */
|
|
20
|
+
export async function dev(opts: ToilCommandOptions = {}): Promise<ViteDevServer> {
|
|
21
|
+
const cfg = await loadConfig(opts);
|
|
22
|
+
generate(cfg);
|
|
23
|
+
const server = await createServer(await createViteConfig(cfg));
|
|
24
|
+
await server.listen();
|
|
25
|
+
server.printUrls();
|
|
26
|
+
return server;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Produces an optimized production SPA bundle in the configured `outDir`. */
|
|
30
|
+
export async function build(opts: ToilCommandOptions = {}): Promise<void> {
|
|
31
|
+
const cfg = await loadConfig(opts);
|
|
32
|
+
generate(cfg);
|
|
33
|
+
await viteBuild(await createViteConfig(cfg));
|
|
34
|
+
// SSG: bake per-URL HTML + sitemap for dynamic routes that opt in via `generateStaticParams`.
|
|
35
|
+
await prerenderStaticParams(cfg);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Self-hosts the built client over the high-performance hyper-express backend (uWebSockets.js),
|
|
40
|
+
* serving the configured `outDir` with an SPA fallback plus a WebSocket channel. Requires a prior
|
|
41
|
+
* `build`. Returns the running backend.
|
|
42
|
+
*/
|
|
43
|
+
export async function start(opts: ToilCommandOptions = {}): Promise<RunningBackend> {
|
|
44
|
+
const cfg = await loadConfig(opts);
|
|
45
|
+
const outDir = path.resolve(cfg.root, cfg.outDir);
|
|
46
|
+
if (!fs.existsSync(path.join(outDir, 'index.html'))) {
|
|
47
|
+
throw new Error(`No build found in ${outDir}. Run \`toiljs build\` first.`);
|
|
48
|
+
}
|
|
49
|
+
return startBackend({ root: outDir, port: cfg.port, host: opts.host });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { defineConfig, loadConfig, AiProvider } from './config.js';
|
|
53
|
+
export { scanRoutes } from './routes.js';
|
|
54
|
+
export type { ScannedRoute } from './routes.js';
|
|
55
|
+
export { TOIL_ENV_DTS } from './generate.js';
|
|
56
|
+
export { AI_HELPERS, AI_HELPER_IDS, aiHelperFiles, TOIL_DOCS } from './docs.js';
|
|
57
|
+
export type { AiHelper } from './docs.js';
|
|
58
|
+
export type {
|
|
59
|
+
ToilConfig,
|
|
60
|
+
ResolvedToilConfig,
|
|
61
|
+
ClientConfig,
|
|
62
|
+
ServerConfig,
|
|
63
|
+
DevtoolsConfig,
|
|
64
|
+
DevtoolsAiConfig,
|
|
65
|
+
} from './config.js';
|
|
66
|
+
export type { RunningBackend, BackendOptions } from 'toiljs/backend';
|
package/src/compiler/pages.ts
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
import type * as TS from 'typescript';
|
|
5
|
-
|
|
6
|
-
import { extractStaticExports } from './prerender.js';
|
|
7
|
-
import type { ScannedRoute } from './routes.js';
|
|
8
|
-
|
|
9
|
-
type Ts = typeof TS;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* A page in the build-time search index: its URL pattern, whether it's dynamic, and the
|
|
13
|
-
* statically-extracted `metadata` literal (the searchable subset; dynamic `generateMetadata` and
|
|
14
|
-
* computed values can't be known at build, so they're absent). Serialized into the generated
|
|
15
|
-
* `routes` module and registered client-side for {@link searchPages}.
|
|
16
|
-
*/
|
|
17
|
-
export interface PageIndexEntry {
|
|
18
|
-
readonly path: string;
|
|
19
|
-
readonly dynamic: boolean;
|
|
20
|
-
readonly metadata: Record<string, unknown>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Loads the project's TypeScript synchronously (so {@link buildPageIndex} can run inside the sync
|
|
25
|
-
* `generate()`), or `null` if it isn't installed, in which case pages are indexed by path only.
|
|
26
|
-
*/
|
|
27
|
-
function loadTypeScriptSync(root: string): Ts | null {
|
|
28
|
-
try {
|
|
29
|
-
const require = createRequire(path.join(root, 'package.json'));
|
|
30
|
-
const mod = require('typescript') as { default?: Ts } & Ts;
|
|
31
|
-
return mod.default ?? mod;
|
|
32
|
-
} catch {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** True when a route pattern has dynamic (`:param` / `*catch-all`) segments. */
|
|
38
|
-
function isDynamic(pattern: string): boolean {
|
|
39
|
-
return /[:*]/.test(pattern);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Builds the searchable page index from the scanned routes: every main-tree page (slots and
|
|
44
|
-
* intercepting routes are excluded, they don't own a distinct URL) paired with its statically
|
|
45
|
-
* extracted `metadata`. A route may also `export const searchHints` (a static `title`/`description`/
|
|
46
|
-
* `keywords` object) to feed the index even when its real metadata is dynamic (`generateMetadata`);
|
|
47
|
-
* hints are merged over the static `metadata`, winning ties. Reads each route file once.
|
|
48
|
-
*/
|
|
49
|
-
export function buildPageIndex(root: string, routes: readonly ScannedRoute[]): PageIndexEntry[] {
|
|
50
|
-
const ts = loadTypeScriptSync(root);
|
|
51
|
-
const seen = new Set<string>();
|
|
52
|
-
const pages: PageIndexEntry[] = [];
|
|
53
|
-
for (const route of routes) {
|
|
54
|
-
if (route.slot !== undefined || route.intercept) continue;
|
|
55
|
-
if (seen.has(route.pattern)) continue;
|
|
56
|
-
seen.add(route.pattern);
|
|
57
|
-
const exports = ts ? extractStaticExports(ts, route.file, ['metadata', 'searchHints']) : {};
|
|
58
|
-
const metadata = { ...exports.metadata, ...exports.searchHints };
|
|
59
|
-
pages.push({ path: route.pattern, dynamic: isDynamic(route.pattern), metadata });
|
|
60
|
-
}
|
|
61
|
-
// Stable order (by path) so the generated module is deterministic across runs.
|
|
62
|
-
pages.sort((a, b) => a.path.localeCompare(b.path));
|
|
63
|
-
return pages;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Serializes the page index to the `export const pages` literal embedded in the routes module. */
|
|
67
|
-
export function pagesModuleSource(pages: readonly PageIndexEntry[]): string {
|
|
68
|
-
const body = pages.map((p) => ` ${JSON.stringify(p)},`).join('\n');
|
|
69
|
-
return `export const pages: PageMeta[] = [\n${body}\n];\n`;
|
|
70
|
-
}
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import type * as TS from 'typescript';
|
|
5
|
+
|
|
6
|
+
import { extractStaticExports } from './prerender.js';
|
|
7
|
+
import type { ScannedRoute } from './routes.js';
|
|
8
|
+
|
|
9
|
+
type Ts = typeof TS;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A page in the build-time search index: its URL pattern, whether it's dynamic, and the
|
|
13
|
+
* statically-extracted `metadata` literal (the searchable subset; dynamic `generateMetadata` and
|
|
14
|
+
* computed values can't be known at build, so they're absent). Serialized into the generated
|
|
15
|
+
* `routes` module and registered client-side for {@link searchPages}.
|
|
16
|
+
*/
|
|
17
|
+
export interface PageIndexEntry {
|
|
18
|
+
readonly path: string;
|
|
19
|
+
readonly dynamic: boolean;
|
|
20
|
+
readonly metadata: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Loads the project's TypeScript synchronously (so {@link buildPageIndex} can run inside the sync
|
|
25
|
+
* `generate()`), or `null` if it isn't installed, in which case pages are indexed by path only.
|
|
26
|
+
*/
|
|
27
|
+
function loadTypeScriptSync(root: string): Ts | null {
|
|
28
|
+
try {
|
|
29
|
+
const require = createRequire(path.join(root, 'package.json'));
|
|
30
|
+
const mod = require('typescript') as { default?: Ts } & Ts;
|
|
31
|
+
return mod.default ?? mod;
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** True when a route pattern has dynamic (`:param` / `*catch-all`) segments. */
|
|
38
|
+
function isDynamic(pattern: string): boolean {
|
|
39
|
+
return /[:*]/.test(pattern);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Builds the searchable page index from the scanned routes: every main-tree page (slots and
|
|
44
|
+
* intercepting routes are excluded, they don't own a distinct URL) paired with its statically
|
|
45
|
+
* extracted `metadata`. A route may also `export const searchHints` (a static `title`/`description`/
|
|
46
|
+
* `keywords` object) to feed the index even when its real metadata is dynamic (`generateMetadata`);
|
|
47
|
+
* hints are merged over the static `metadata`, winning ties. Reads each route file once.
|
|
48
|
+
*/
|
|
49
|
+
export function buildPageIndex(root: string, routes: readonly ScannedRoute[]): PageIndexEntry[] {
|
|
50
|
+
const ts = loadTypeScriptSync(root);
|
|
51
|
+
const seen = new Set<string>();
|
|
52
|
+
const pages: PageIndexEntry[] = [];
|
|
53
|
+
for (const route of routes) {
|
|
54
|
+
if (route.slot !== undefined || route.intercept) continue;
|
|
55
|
+
if (seen.has(route.pattern)) continue;
|
|
56
|
+
seen.add(route.pattern);
|
|
57
|
+
const exports = ts ? extractStaticExports(ts, route.file, ['metadata', 'searchHints']) : {};
|
|
58
|
+
const metadata = { ...exports.metadata, ...exports.searchHints };
|
|
59
|
+
pages.push({ path: route.pattern, dynamic: isDynamic(route.pattern), metadata });
|
|
60
|
+
}
|
|
61
|
+
// Stable order (by path) so the generated module is deterministic across runs.
|
|
62
|
+
pages.sort((a, b) => a.path.localeCompare(b.path));
|
|
63
|
+
return pages;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Serializes the page index to the `export const pages` literal embedded in the routes module. */
|
|
67
|
+
export function pagesModuleSource(pages: readonly PageIndexEntry[]): string {
|
|
68
|
+
const body = pages.map((p) => ` ${JSON.stringify(p)},`).join('\n');
|
|
69
|
+
return `export const pages: PageMeta[] = [\n${body}\n];\n`;
|
|
70
|
+
}
|
package/src/compiler/plugin.ts
CHANGED
|
@@ -1,7 +1,152 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { type IncomingMessage } from 'node:http';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
2
7
|
|
|
3
|
-
import { type
|
|
8
|
+
import { type Plugin, version as viteVersion } from 'vite';
|
|
9
|
+
|
|
10
|
+
import { AiProvider, type DevtoolsAiConfig, type ResolvedToilConfig } from './config.js';
|
|
4
11
|
import { generate } from './generate.js';
|
|
12
|
+
import { scanRoutes } from './routes.js';
|
|
13
|
+
|
|
14
|
+
/** Calls the configured AI provider (server-side, so the key never reaches the browser). */
|
|
15
|
+
async function aiComplete(ai: DevtoolsAiConfig, prompt: string): Promise<string> {
|
|
16
|
+
const key = ai.apiKeyEnv ? process.env[ai.apiKeyEnv] : undefined;
|
|
17
|
+
if (ai.endpoint) {
|
|
18
|
+
const r = await fetch(ai.endpoint, {
|
|
19
|
+
method: 'POST',
|
|
20
|
+
headers: { 'content-type': 'application/json' },
|
|
21
|
+
body: JSON.stringify({ prompt }),
|
|
22
|
+
});
|
|
23
|
+
const j = (await r.json()) as { text?: string };
|
|
24
|
+
return j.text ?? '';
|
|
25
|
+
}
|
|
26
|
+
if (ai.provider === AiProvider.OpenAI) {
|
|
27
|
+
if (!key) throw new Error(`missing API key (set env ${ai.apiKeyEnv ?? 'OPENAI_API_KEY'})`);
|
|
28
|
+
const r = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers: { 'content-type': 'application/json', authorization: `Bearer ${key}` },
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
model: ai.model ?? 'gpt-4o',
|
|
33
|
+
messages: [{ role: 'user', content: prompt }],
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
const j = (await r.json()) as { choices?: { message?: { content?: string } }[] };
|
|
37
|
+
return j.choices?.[0]?.message?.content ?? '';
|
|
38
|
+
}
|
|
39
|
+
// default: anthropic
|
|
40
|
+
if (!key) throw new Error(`missing API key (set env ${ai.apiKeyEnv ?? 'ANTHROPIC_API_KEY'})`);
|
|
41
|
+
const r = await fetch('https://api.anthropic.com/v1/messages', {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers: {
|
|
44
|
+
'content-type': 'application/json',
|
|
45
|
+
'x-api-key': key,
|
|
46
|
+
'anthropic-version': '2023-06-01',
|
|
47
|
+
},
|
|
48
|
+
body: JSON.stringify({
|
|
49
|
+
model: ai.model ?? 'claude-sonnet-4-6',
|
|
50
|
+
max_tokens: 1024,
|
|
51
|
+
messages: [{ role: 'user', content: prompt }],
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
const j = (await r.json()) as { content?: { text?: string }[] };
|
|
55
|
+
return (j.content ?? []).map((c) => c.text ?? '').join('');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Reads a package's version resolved from `<fromDir>`, or 'unknown'. */
|
|
59
|
+
function depVersion(fromDir: string, name: string): string {
|
|
60
|
+
try {
|
|
61
|
+
const pkgPath = createRequire(path.join(fromDir, 'package.json')).resolve(`${name}/package.json`);
|
|
62
|
+
const raw = JSON.parse(fs.readFileSync(pkgPath, 'utf8')) as { version?: string };
|
|
63
|
+
return raw.version ?? 'unknown';
|
|
64
|
+
} catch {
|
|
65
|
+
return 'unknown';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** toiljs's own version (package.json two levels up from build/compiler). */
|
|
70
|
+
function frameworkVersion(): string {
|
|
71
|
+
try {
|
|
72
|
+
const p = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'package.json');
|
|
73
|
+
const raw = JSON.parse(fs.readFileSync(p, 'utf8')) as { version?: string };
|
|
74
|
+
return raw.version ?? '0.0.0';
|
|
75
|
+
} catch {
|
|
76
|
+
return '0.0.0';
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Build/config snapshot served to the dev toolbar at `/__toil/devinfo`. */
|
|
81
|
+
function devInfo(cfg: ResolvedToilConfig, port: number): Record<string, unknown> {
|
|
82
|
+
const routes: Record<string, string> = {};
|
|
83
|
+
for (const r of scanRoutes(cfg.routesAbsDir)) {
|
|
84
|
+
if (r.slot === undefined && !r.intercept) routes[r.pattern] = r.file;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
toiljs: frameworkVersion(),
|
|
88
|
+
// vite is a dependency of the framework, not the app, so resolving it from the app root
|
|
89
|
+
// fails; read the running vite's own exported version instead.
|
|
90
|
+
vite: viteVersion,
|
|
91
|
+
react: depVersion(cfg.root, 'react'),
|
|
92
|
+
port,
|
|
93
|
+
enabled: cfg.devtools,
|
|
94
|
+
flags: {
|
|
95
|
+
images: cfg.images,
|
|
96
|
+
fonts: cfg.fonts,
|
|
97
|
+
viewTransitions: cfg.viewTransitions,
|
|
98
|
+
transitions: cfg.transitions,
|
|
99
|
+
seo: cfg.seo != null,
|
|
100
|
+
},
|
|
101
|
+
routes,
|
|
102
|
+
ai: cfg.devtoolsAi !== null,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Resolves a request's `file` param to an absolute path that is genuinely inside the project root,
|
|
108
|
+
* or null. Guards against `..` traversal, sibling-prefix escapes (`<root>-evil/secret` passes a bare
|
|
109
|
+
* `startsWith(root)`), and symlinks inside the project that point outside it (realpath re-check).
|
|
110
|
+
*/
|
|
111
|
+
function safeProjectPath(cfg: ResolvedToilConfig, file: string | null): string | null {
|
|
112
|
+
if (!file) return null;
|
|
113
|
+
const root = cfg.root;
|
|
114
|
+
const inside = (p: string): boolean => p === root || p.startsWith(root + path.sep);
|
|
115
|
+
const abs = path.resolve(file);
|
|
116
|
+
if (!inside(abs) || !fs.existsSync(abs)) return null;
|
|
117
|
+
try {
|
|
118
|
+
const real = fs.realpathSync(abs);
|
|
119
|
+
return inside(real) ? real : null;
|
|
120
|
+
} catch {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* True for requests that must NOT reach the dev endpoints (they open files, read source, and spend
|
|
127
|
+
* AI credits). Uses an allowlist on the browser-set `Sec-Fetch-Site`: only `same-origin` (the
|
|
128
|
+
* toolbar), `none` (user-initiated, e.g. the address bar), or an absent header (non-browser tooling
|
|
129
|
+
* like curl) are allowed. Everything else, `cross-site`, `same-site`, or any unexpected value, is
|
|
130
|
+
* rejected. This blocks CSRF (a malicious site's fetch/img) without breaking local dev tooling.
|
|
131
|
+
*/
|
|
132
|
+
function isCrossSiteRequest(headers: IncomingMessage['headers']): boolean {
|
|
133
|
+
const site = headers['sec-fetch-site'];
|
|
134
|
+
return site !== undefined && site !== 'same-origin' && site !== 'none';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Opens `file` in the user's editor (best-effort): `$EDITOR file`, else `code -g file`. */
|
|
138
|
+
function openInEditor(file: string): void {
|
|
139
|
+
try {
|
|
140
|
+
const editor = process.env.EDITOR ?? process.env.VISUAL;
|
|
141
|
+
const child = editor
|
|
142
|
+
? spawn(editor, [file], { stdio: 'ignore', detached: true })
|
|
143
|
+
: spawn('code', ['-g', file], { stdio: 'ignore', detached: true });
|
|
144
|
+
child.on('error', () => undefined);
|
|
145
|
+
child.unref();
|
|
146
|
+
} catch {
|
|
147
|
+
/* ignore */
|
|
148
|
+
}
|
|
149
|
+
}
|
|
5
150
|
|
|
6
151
|
/**
|
|
7
152
|
* Vite plugin that keeps the generated route table in sync during dev: when a route file is
|
|
@@ -35,6 +180,117 @@ export function toilPlugin(cfg: ResolvedToilConfig): Plugin {
|
|
|
35
180
|
return null;
|
|
36
181
|
},
|
|
37
182
|
configureServer(server) {
|
|
183
|
+
// Dev toolbar endpoints (dev only). `/__toil/devinfo` -> build/config snapshot;
|
|
184
|
+
// `/__toil/open?file=` -> open the file in the editor.
|
|
185
|
+
server.middlewares.use('/__toil/devinfo', (req, res) => {
|
|
186
|
+
if (isCrossSiteRequest(req.headers)) {
|
|
187
|
+
res.statusCode = 403;
|
|
188
|
+
res.end();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const port = server.config.server.port ?? cfg.port;
|
|
192
|
+
res.setHeader('content-type', 'application/json');
|
|
193
|
+
res.end(JSON.stringify(devInfo(cfg, port)));
|
|
194
|
+
});
|
|
195
|
+
// `/__toil/ai` -> server-side AI proxy. The key is read from the env here and never
|
|
196
|
+
// reaches the browser; 404 when AI isn't configured (the toolbar then only hands off).
|
|
197
|
+
server.middlewares.use('/__toil/ai', (req, res) => {
|
|
198
|
+
if (isCrossSiteRequest(req.headers)) {
|
|
199
|
+
res.statusCode = 403;
|
|
200
|
+
res.end();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (req.method !== 'POST') {
|
|
204
|
+
res.statusCode = 405;
|
|
205
|
+
res.end();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const ai = cfg.devtoolsAi;
|
|
209
|
+
if (!ai) {
|
|
210
|
+
res.statusCode = 404;
|
|
211
|
+
res.end();
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
let body = '';
|
|
215
|
+
let aborted = false;
|
|
216
|
+
req.on('data', (chunk) => {
|
|
217
|
+
if (aborted) return;
|
|
218
|
+
body += String(chunk);
|
|
219
|
+
if (body.length > 100_000) {
|
|
220
|
+
// Cap the request body so a runaway/malicious POST can't grow it unbounded.
|
|
221
|
+
aborted = true;
|
|
222
|
+
res.statusCode = 413;
|
|
223
|
+
res.end();
|
|
224
|
+
req.destroy();
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
req.on('end', () => {
|
|
228
|
+
if (aborted) return;
|
|
229
|
+
void (async () => {
|
|
230
|
+
try {
|
|
231
|
+
const parsed = JSON.parse(body || '{}') as { prompt?: string };
|
|
232
|
+
// Cap the prompt actually forwarded upstream (independent of the raw-body cap).
|
|
233
|
+
const prompt =
|
|
234
|
+
typeof parsed.prompt === 'string' ? parsed.prompt.slice(0, 16000) : '';
|
|
235
|
+
const text = await aiComplete(ai, prompt);
|
|
236
|
+
res.setHeader('content-type', 'application/json');
|
|
237
|
+
res.end(JSON.stringify({ text }));
|
|
238
|
+
} catch (e) {
|
|
239
|
+
// Log the detail to the dev's terminal; return a generic message to the
|
|
240
|
+
// client so upstream/provider error text is never reflected over HTTP.
|
|
241
|
+
process.stderr.write(
|
|
242
|
+
`toil: /__toil/ai failed: ${e instanceof Error ? e.message : String(e)}\n`,
|
|
243
|
+
);
|
|
244
|
+
res.statusCode = 500;
|
|
245
|
+
res.setHeader('content-type', 'application/json');
|
|
246
|
+
res.end(JSON.stringify({ error: 'AI request failed (see dev server logs).' }));
|
|
247
|
+
}
|
|
248
|
+
})();
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
server.middlewares.use('/__toil/open', (req, res) => {
|
|
252
|
+
try {
|
|
253
|
+
if (isCrossSiteRequest(req.headers)) {
|
|
254
|
+
res.statusCode = 403;
|
|
255
|
+
res.end();
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const url = new URL(req.url ?? '', 'http://localhost');
|
|
259
|
+
const abs = safeProjectPath(cfg, url.searchParams.get('file'));
|
|
260
|
+
if (abs) openInEditor(abs);
|
|
261
|
+
res.statusCode = 204;
|
|
262
|
+
res.end();
|
|
263
|
+
} catch {
|
|
264
|
+
res.statusCode = 400;
|
|
265
|
+
res.end();
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
// `/__toil/source?file=` -> the file's text, so the AI tab can include the page's code in
|
|
269
|
+
// its prompt. Same root-confinement (`safeProjectPath`) as `/__toil/open`; capped so a
|
|
270
|
+
// stray huge file can't bloat the response.
|
|
271
|
+
server.middlewares.use('/__toil/source', (req, res) => {
|
|
272
|
+
try {
|
|
273
|
+
if (isCrossSiteRequest(req.headers)) {
|
|
274
|
+
res.statusCode = 403;
|
|
275
|
+
res.end();
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const url = new URL(req.url ?? '', 'http://localhost');
|
|
279
|
+
const abs = safeProjectPath(cfg, url.searchParams.get('file'));
|
|
280
|
+
if (!abs) {
|
|
281
|
+
res.statusCode = 404;
|
|
282
|
+
res.end();
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const text = fs.readFileSync(abs, 'utf8').slice(0, 20000);
|
|
286
|
+
res.setHeader('content-type', 'text/plain; charset=utf-8');
|
|
287
|
+
res.end(text);
|
|
288
|
+
} catch {
|
|
289
|
+
res.statusCode = 400;
|
|
290
|
+
res.end();
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
|
|
38
294
|
// Trailing slash so a sibling like `routes-extra/` doesn't match the `routes/` prefix.
|
|
39
295
|
const routesPrefix = cfg.routesAbsDir.replace(/\\/g, '/').replace(/\/?$/, '/');
|
|
40
296
|
const onChange = (file: string): void => {
|