toiljs 0.0.115 → 0.0.117
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/CHANGELOG.md +10 -0
- package/README.md +16 -7
- package/TYPESCRIPT_LAW.md +1946 -2073
- package/build/backend/.tsbuildinfo +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2375 -258
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/dev/devtools.js +1 -1
- package/build/client/dev/error-overlay.js +1 -1
- package/build/client/routing/mount.js +1 -1
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/auth-emails.js +7 -3
- package/build/compiler/emails.js +1 -1
- package/build/compiler/index.d.ts +1 -0
- package/build/compiler/index.js +25 -4
- package/build/compiler/pages.js +2 -3
- package/build/compiler/prerender.d.ts +3 -8
- package/build/compiler/prerender.js +88 -127
- package/build/compiler/ssg.js +3 -4
- package/build/compiler/template-build.js +3 -4
- package/build/compiler/toil-docs.generated.js +6 -5
- package/build/compiler/vite.js +3 -6
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/db/database.js +7 -3
- package/build/devserver/db/types.js +1 -1
- package/build/devserver/production.js +4 -3
- package/build/devserver/runtime/crypto.d.ts +1 -1
- package/build/io/.tsbuildinfo +1 -1
- package/build/logger/.tsbuildinfo +1 -1
- package/build/shared/.tsbuildinfo +1 -1
- package/examples/basic/client/routes/analytics.tsx +7 -5
- package/examples/basic/server/tsconfig.json +1 -3
- package/package.json +45 -44
- package/presets/no-uint8array-tostring.js +120 -153
- package/presets/oxlint.d.ts +5 -0
- package/presets/oxlint.js +224 -0
- package/server/runtime/tsconfig.json +1 -3
- package/tsconfig.json +2 -11
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
- package/.github/changelog-config.json +0 -45
- package/.github/dependabot.yml +0 -27
- package/.github/workflows/ci.yml +0 -222
- package/.vscode/settings.json +0 -10
- package/docs/README.md +0 -107
- package/docs/auth/README.md +0 -104
- package/docs/auth/configuration.md +0 -105
- package/docs/auth/emails.md +0 -92
- package/docs/auth/extending.md +0 -202
- package/docs/auth/how-it-works.md +0 -138
- package/docs/auth/usage.md +0 -188
- package/docs/backend/README.md +0 -143
- package/docs/backend/data.md +0 -351
- package/docs/backend/rest.md +0 -402
- package/docs/backend/rpc.md +0 -226
- package/docs/background/README.md +0 -114
- package/docs/background/daemons.md +0 -276
- package/docs/background/derive.md +0 -179
- package/docs/cli/README.md +0 -379
- package/docs/concepts/ai-guide.md +0 -132
- package/docs/concepts/config.md +0 -416
- package/docs/concepts/decorators.md +0 -127
- package/docs/concepts/security.md +0 -108
- package/docs/concepts/tiers.md +0 -166
- package/docs/concepts/types.md +0 -216
- package/docs/database/README.md +0 -143
- package/docs/database/capacity.md +0 -350
- package/docs/database/counters.md +0 -174
- package/docs/database/documents.md +0 -277
- package/docs/database/events.md +0 -307
- package/docs/database/membership.md +0 -246
- package/docs/database/setup.md +0 -155
- package/docs/database/unique.md +0 -216
- package/docs/database/views.md +0 -246
- package/docs/frontend/README.md +0 -104
- package/docs/frontend/components.md +0 -286
- package/docs/frontend/data-fetching.md +0 -260
- package/docs/frontend/images.md +0 -148
- package/docs/frontend/metadata.md +0 -344
- package/docs/frontend/navigation.md +0 -296
- package/docs/frontend/rendering.md +0 -236
- package/docs/frontend/routing.md +0 -346
- package/docs/frontend/scripts.md +0 -118
- package/docs/frontend/search.md +0 -191
- package/docs/frontend/styling.md +0 -147
- package/docs/frontend/toil-global.md +0 -216
- package/docs/getting-started/README.md +0 -81
- package/docs/getting-started/create-project.md +0 -131
- package/docs/getting-started/deploy.md +0 -101
- package/docs/getting-started/first-app.md +0 -215
- package/docs/getting-started/installation.md +0 -124
- package/docs/getting-started/migrating.md +0 -125
- package/docs/getting-started/project-structure.md +0 -163
- package/docs/introduction/README.md +0 -66
- package/docs/introduction/design-principles.md +0 -81
- package/docs/introduction/distributed.md +0 -91
- package/docs/introduction/how-it-works.md +0 -121
- package/docs/introduction/hyperscale.md +0 -70
- package/docs/introduction/modern-stack.md +0 -85
- package/docs/introduction/vs-other-frameworks.md +0 -64
- package/docs/introduction/why-toil.md +0 -99
- package/docs/llms.txt +0 -95
- package/docs/realtime/README.md +0 -102
- package/docs/realtime/channels.md +0 -211
- package/docs/realtime/streams.md +0 -369
- package/docs/services/README.md +0 -60
- package/docs/services/analytics.md +0 -279
- package/docs/services/caching.md +0 -175
- package/docs/services/cookies.md +0 -235
- package/docs/services/crypto.md +0 -209
- package/docs/services/email.md +0 -289
- package/docs/services/environment.md +0 -141
- package/docs/services/ratelimit.md +0 -174
- package/docs/services/time.md +0 -85
- package/eslint.config.js +0 -57
- package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
- package/presets/eslint.js +0 -88
- package/src/backend/index.ts +0 -204
- package/src/cli/configure.ts +0 -363
- package/src/cli/create.ts +0 -924
- package/src/cli/db.ts +0 -158
- package/src/cli/diagnostics.ts +0 -665
- package/src/cli/doctor.ts +0 -982
- package/src/cli/features.ts +0 -170
- package/src/cli/index.ts +0 -326
- package/src/cli/notify.ts +0 -120
- package/src/cli/proc.ts +0 -56
- package/src/cli/ui.ts +0 -169
- package/src/cli/update.ts +0 -248
- package/src/cli/updates.ts +0 -92
- package/src/cli/validate.ts +0 -31
- package/src/cli/version-check.ts +0 -125
- package/src/client/auth.ts +0 -869
- package/src/client/channel/channel.ts +0 -146
- package/src/client/components/Form.tsx +0 -65
- package/src/client/components/Image.tsx +0 -160
- package/src/client/components/Script.tsx +0 -113
- package/src/client/components/Slot.tsx +0 -21
- package/src/client/dev/devtools.tsx +0 -1089
- package/src/client/dev/error-overlay.tsx +0 -225
- package/src/client/errors.ts +0 -11
- package/src/client/head/head.ts +0 -182
- package/src/client/head/metadata.ts +0 -110
- package/src/client/index.ts +0 -130
- package/src/client/navigation/Link.tsx +0 -108
- package/src/client/navigation/NavLink.tsx +0 -86
- package/src/client/navigation/navigation.ts +0 -235
- package/src/client/navigation/prefetch.ts +0 -169
- package/src/client/navigation/scroll.ts +0 -53
- package/src/client/routing/Router.tsx +0 -216
- package/src/client/routing/action.ts +0 -122
- package/src/client/routing/error-boundary.tsx +0 -43
- package/src/client/routing/hooks.ts +0 -140
- package/src/client/routing/lazy.ts +0 -77
- package/src/client/routing/loader.ts +0 -381
- package/src/client/routing/match.ts +0 -47
- package/src/client/routing/mount.tsx +0 -90
- package/src/client/routing/params-context.ts +0 -10
- package/src/client/routing/slot-context.ts +0 -7
- package/src/client/rpc.ts +0 -97
- package/src/client/search/search.ts +0 -189
- package/src/client/search/use-page-search.ts +0 -73
- package/src/client/ssr/markers.tsx +0 -172
- package/src/client/stream/client.ts +0 -237
- package/src/client/types.ts +0 -73
- package/src/compiler/auth-emails.ts +0 -312
- package/src/compiler/config.ts +0 -369
- package/src/compiler/docs.ts +0 -127
- package/src/compiler/email-preview.ts +0 -314
- package/src/compiler/emails.ts +0 -404
- package/src/compiler/fonts.ts +0 -89
- package/src/compiler/generate.ts +0 -471
- package/src/compiler/image-blur.ts +0 -42
- package/src/compiler/image-report.ts +0 -88
- package/src/compiler/index.ts +0 -1362
- package/src/compiler/pages.ts +0 -49
- package/src/compiler/plugin.ts +0 -391
- package/src/compiler/prerender.ts +0 -263
- package/src/compiler/routes.ts +0 -170
- package/src/compiler/seo.ts +0 -415
- package/src/compiler/sri.ts +0 -144
- package/src/compiler/ssg.ts +0 -177
- package/src/compiler/ssr-codegen.ts +0 -85
- package/src/compiler/template-build.ts +0 -676
- package/src/compiler/template.ts +0 -262
- package/src/compiler/toil-docs.generated.ts +0 -77
- package/src/compiler/vite.ts +0 -245
- package/src/devserver/analytics/index.ts +0 -317
- package/src/devserver/config/dotenv.ts +0 -102
- package/src/devserver/config/env.ts +0 -30
- package/src/devserver/config/ratelimit.ts +0 -123
- package/src/devserver/daemon/catalog.ts +0 -120
- package/src/devserver/daemon/cron.ts +0 -87
- package/src/devserver/daemon/host.ts +0 -134
- package/src/devserver/daemon/index.ts +0 -369
- package/src/devserver/daemon/runtime.ts +0 -48
- package/src/devserver/db/catalog.ts +0 -109
- package/src/devserver/db/database.ts +0 -1834
- package/src/devserver/db/derives.ts +0 -121
- package/src/devserver/db/index.ts +0 -20
- package/src/devserver/db/routeKinds.ts +0 -191
- package/src/devserver/db/types.ts +0 -168
- package/src/devserver/email/caps.ts +0 -0
- package/src/devserver/email/config.ts +0 -141
- package/src/devserver/email/console.ts +0 -348
- package/src/devserver/email/index.ts +0 -155
- package/src/devserver/email/providers.ts +0 -131
- package/src/devserver/email/status.ts +0 -23
- package/src/devserver/email/validate.ts +0 -37
- package/src/devserver/email/wire.ts +0 -55
- package/src/devserver/http/cache.ts +0 -0
- package/src/devserver/http/envelope.ts +0 -157
- package/src/devserver/http/proxy.ts +0 -172
- package/src/devserver/http/runtime.ts +0 -387
- package/src/devserver/index.ts +0 -43
- package/src/devserver/production-ipc.ts +0 -63
- package/src/devserver/production-worker.ts +0 -83
- package/src/devserver/production.ts +0 -718
- package/src/devserver/runtime/crypto.ts +0 -587
- package/src/devserver/runtime/host.ts +0 -427
- package/src/devserver/runtime/module.ts +0 -442
- package/src/devserver/server.ts +0 -251
- package/src/devserver/ssr.ts +0 -208
- package/src/devserver/stream/catalog.ts +0 -106
- package/src/devserver/stream/host.ts +0 -42
- package/src/devserver/stream/index.ts +0 -301
- package/src/devserver/stream/manager.ts +0 -167
- package/src/devserver/stream/router.ts +0 -101
- package/src/devserver/stream/wire.ts +0 -58
- package/src/devserver/stream/ws.ts +0 -78
- package/src/devserver/wasm/sections.ts +0 -59
- package/src/devserver/wasm/surface.ts +0 -115
- package/src/io/FastMap.ts +0 -151
- package/src/io/FastSet.ts +0 -113
- package/src/io/codec.ts +0 -362
- package/src/io/index.ts +0 -10
- package/src/io/lengths.ts +0 -14
- package/src/io/types.ts +0 -19
- package/src/logger/index.ts +0 -22
- package/src/shared/index.ts +0 -46
- package/test/analytics-dev.test.ts +0 -113
- package/test/assembly/aspect-shim.ts +0 -33
- package/test/assembly/cookie.spec.ts +0 -303
- package/test/assembly/example.spec.ts +0 -22
- package/test/assembly/ssr.spec.ts +0 -95
- package/test/assembly.test.ts +0 -59
- package/test/auth-emails.test.ts +0 -118
- package/test/built-ssr.test.ts +0 -148
- package/test/channel.test.ts +0 -21
- package/test/config-host-port.test.ts +0 -59
- package/test/configure.test.ts +0 -94
- package/test/daemon-build.test.ts +0 -278
- package/test/daemon-catalog.test.ts +0 -349
- package/test/daemon-emulation.test.ts +0 -256
- package/test/db.test.ts +0 -0
- package/test/devserver-database.test.ts +0 -1102
- package/test/devserver-email.test.ts +0 -306
- package/test/devserver-pqauth.test.ts +0 -93
- package/test/devserver-secrets.test.ts +0 -63
- package/test/devserver.test.ts +0 -259
- package/test/doctor.test.ts +0 -298
- package/test/dom/Image.test.tsx +0 -144
- package/test/dom/Link.test.tsx +0 -67
- package/test/dom/NavLink.test.tsx +0 -37
- package/test/dom/Script.test.tsx +0 -48
- package/test/dom/action.test.tsx +0 -146
- package/test/dom/error-overlay.test.tsx +0 -44
- package/test/dom/loader.test.tsx +0 -121
- package/test/dom/navigation.test.ts +0 -59
- package/test/dom/revalidate.test.tsx +0 -38
- package/test/dom/route-head.test.tsx +0 -56
- package/test/dom/router-loading.test.tsx +0 -44
- package/test/dom/scroll.test.ts +0 -56
- package/test/dom/slot.test.tsx +0 -131
- package/test/dom/use-metadata.test.tsx +0 -58
- package/test/dom/view-transitions.test.tsx +0 -53
- package/test/dynamic-routing.test.ts +0 -132
- package/test/email-console.test.ts +0 -86
- package/test/email-preview.test.ts +0 -73
- package/test/emails.test.ts +0 -58
- package/test/errors.test.ts +0 -21
- package/test/example-guestbook.test.ts +0 -123
- package/test/features.test.ts +0 -149
- package/test/fixtures/bignum-wire/spec.ts +0 -25
- package/test/fixtures/daemon-app.ts +0 -65
- package/test/fixtures/stream-echo.ts +0 -26
- package/test/fixtures/stream-gate.ts +0 -28
- package/test/fixtures/stream-trap.ts +0 -18
- package/test/fixtures/stream-typed.ts +0 -41
- package/test/fonts.test.ts +0 -28
- package/test/global-setup.ts +0 -17
- package/test/head.test.ts +0 -42
- package/test/image-blur.test.ts +0 -50
- package/test/io.test.ts +0 -117
- package/test/metadata.test.ts +0 -40
- package/test/navlink.test.ts +0 -28
- package/test/pages.test.ts +0 -105
- package/test/placeholder.test.ts +0 -9
- package/test/pqauth-e2e.test.ts +0 -216
- package/test/pqauth-email.test.ts +0 -801
- package/test/prerender.test.ts +0 -116
- package/test/prettier-plugin.test.ts +0 -46
- package/test/routes.test.ts +0 -76
- package/test/rpc-bignum-wire.test.ts +0 -164
- package/test/rpc-dispatch.test.ts +0 -132
- package/test/rpc-kinds.test.ts +0 -18
- package/test/rpc.test.ts +0 -66
- package/test/search.test.ts +0 -114
- package/test/seo.test.ts +0 -175
- package/test/slot-layouts.test.ts +0 -69
- package/test/sri.test.ts +0 -129
- package/test/ssg.test.ts +0 -36
- package/test/ssr-hydration.test.tsx +0 -122
- package/test/ssr-render.test.ts +0 -197
- package/test/ssr-template.test.tsx +0 -482
- package/test/stream-emulation.test.ts +0 -479
- package/test/ui.test.ts +0 -40
- package/test/update.test.ts +0 -58
- package/test/validate.test.ts +0 -42
- package/test/version-check.test.ts +0 -130
- package/tests/data-parity/generated-parity.ts +0 -99
- package/tests/data-parity/parity.ts +0 -80
- package/tests/data-parity/spec.ts +0 -46
package/src/compiler/pages.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { extractStaticExports, loadTypeScriptSync } from './prerender.js';
|
|
2
|
-
import type { ScannedRoute } from './routes.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A page in the build-time search index: its URL pattern, whether it's dynamic, and the
|
|
6
|
-
* statically-extracted `metadata` literal (the searchable subset; dynamic `generateMetadata` and
|
|
7
|
-
* computed values can't be known at build, so they're absent). Serialized into the generated
|
|
8
|
-
* `routes` module and registered client-side for {@link searchPages}.
|
|
9
|
-
*/
|
|
10
|
-
export interface PageIndexEntry {
|
|
11
|
-
readonly path: string;
|
|
12
|
-
readonly dynamic: boolean;
|
|
13
|
-
readonly metadata: Record<string, unknown>;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** True when a route pattern has dynamic (`:param` / `*catch-all`) segments. */
|
|
17
|
-
function isDynamic(pattern: string): boolean {
|
|
18
|
-
return /[:*]/.test(pattern);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Builds the searchable page index from the scanned routes: every main-tree page (slots and
|
|
23
|
-
* intercepting routes are excluded, they don't own a distinct URL) paired with its statically
|
|
24
|
-
* extracted `metadata`. A route may also `export const searchHints` (a static `title`/`description`/
|
|
25
|
-
* `keywords` object) to feed the index even when its real metadata is dynamic (`generateMetadata`);
|
|
26
|
-
* hints are merged over the static `metadata`, winning ties. Reads each route file once.
|
|
27
|
-
*/
|
|
28
|
-
export function buildPageIndex(root: string, routes: readonly ScannedRoute[]): PageIndexEntry[] {
|
|
29
|
-
const ts = loadTypeScriptSync(root);
|
|
30
|
-
const seen = new Set<string>();
|
|
31
|
-
const pages: PageIndexEntry[] = [];
|
|
32
|
-
for (const route of routes) {
|
|
33
|
-
if (route.slot !== undefined || route.intercept) continue;
|
|
34
|
-
if (seen.has(route.pattern)) continue;
|
|
35
|
-
seen.add(route.pattern);
|
|
36
|
-
const exports = ts ? extractStaticExports(ts, route.file, ['metadata', 'searchHints']) : {};
|
|
37
|
-
const metadata = { ...exports.metadata, ...exports.searchHints };
|
|
38
|
-
pages.push({ path: route.pattern, dynamic: isDynamic(route.pattern), metadata });
|
|
39
|
-
}
|
|
40
|
-
// Stable order (by path) so the generated module is deterministic across runs.
|
|
41
|
-
pages.sort((a, b) => a.path.localeCompare(b.path));
|
|
42
|
-
return pages;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Serializes the page index to the `export const pages` literal embedded in the routes module. */
|
|
46
|
-
export function pagesModuleSource(pages: readonly PageIndexEntry[]): string {
|
|
47
|
-
const body = pages.map((p) => ` ${JSON.stringify(p)},`).join('\n');
|
|
48
|
-
return `export const pages: PageMeta[] = [\n${body}\n];\n`;
|
|
49
|
-
}
|
package/src/compiler/plugin.ts
DELETED
|
@@ -1,391 +0,0 @@
|
|
|
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';
|
|
7
|
-
|
|
8
|
-
import { type Plugin, version as viteVersion } from 'vite';
|
|
9
|
-
|
|
10
|
-
import { AiProvider, type DevtoolsAiConfig, type ResolvedToilConfig } from './config.js';
|
|
11
|
-
import { emailsVersion, listEmails, previewShellHtml, renderEmailByName } from './email-preview.js';
|
|
12
|
-
import { generate } from './generate.js';
|
|
13
|
-
import { scanRoutes } from './routes.js';
|
|
14
|
-
|
|
15
|
-
/** Calls the configured AI provider (server-side, so the key never reaches the browser). */
|
|
16
|
-
async function aiComplete(ai: DevtoolsAiConfig, prompt: string): Promise<string> {
|
|
17
|
-
const key = ai.apiKeyEnv ? process.env[ai.apiKeyEnv] : undefined;
|
|
18
|
-
if (ai.endpoint) {
|
|
19
|
-
const r = await fetch(ai.endpoint, {
|
|
20
|
-
method: 'POST',
|
|
21
|
-
headers: { 'content-type': 'application/json' },
|
|
22
|
-
body: JSON.stringify({ prompt }),
|
|
23
|
-
});
|
|
24
|
-
const j = (await r.json()) as { text?: string };
|
|
25
|
-
return j.text ?? '';
|
|
26
|
-
}
|
|
27
|
-
if (ai.provider === AiProvider.OpenAI) {
|
|
28
|
-
if (!key) throw new Error(`missing API key (set env ${ai.apiKeyEnv ?? 'OPENAI_API_KEY'})`);
|
|
29
|
-
const r = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
30
|
-
method: 'POST',
|
|
31
|
-
headers: { 'content-type': 'application/json', authorization: `Bearer ${key}` },
|
|
32
|
-
body: JSON.stringify({
|
|
33
|
-
model: ai.model ?? 'gpt-4o',
|
|
34
|
-
messages: [{ role: 'user', content: prompt }],
|
|
35
|
-
}),
|
|
36
|
-
});
|
|
37
|
-
const j = (await r.json()) as { choices?: { message?: { content?: string } }[] };
|
|
38
|
-
return j.choices?.[0]?.message?.content ?? '';
|
|
39
|
-
}
|
|
40
|
-
// default: anthropic
|
|
41
|
-
if (!key) throw new Error(`missing API key (set env ${ai.apiKeyEnv ?? 'ANTHROPIC_API_KEY'})`);
|
|
42
|
-
const r = await fetch('https://api.anthropic.com/v1/messages', {
|
|
43
|
-
method: 'POST',
|
|
44
|
-
headers: {
|
|
45
|
-
'content-type': 'application/json',
|
|
46
|
-
'x-api-key': key,
|
|
47
|
-
'anthropic-version': '2023-06-01',
|
|
48
|
-
},
|
|
49
|
-
body: JSON.stringify({
|
|
50
|
-
model: ai.model ?? 'claude-sonnet-4-6',
|
|
51
|
-
max_tokens: 1024,
|
|
52
|
-
messages: [{ role: 'user', content: prompt }],
|
|
53
|
-
}),
|
|
54
|
-
});
|
|
55
|
-
const j = (await r.json()) as { content?: { text?: string }[] };
|
|
56
|
-
return (j.content ?? []).map((c) => c.text ?? '').join('');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Reads a package's version resolved from `<fromDir>`, or 'unknown'. */
|
|
60
|
-
function depVersion(fromDir: string, name: string): string {
|
|
61
|
-
try {
|
|
62
|
-
const pkgPath = createRequire(path.join(fromDir, 'package.json')).resolve(
|
|
63
|
-
`${name}/package.json`,
|
|
64
|
-
);
|
|
65
|
-
const raw = JSON.parse(fs.readFileSync(pkgPath, 'utf8')) as { version?: string };
|
|
66
|
-
return raw.version ?? 'unknown';
|
|
67
|
-
} catch {
|
|
68
|
-
return 'unknown';
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** toiljs's own version (package.json two levels up from build/compiler). */
|
|
73
|
-
function frameworkVersion(): string {
|
|
74
|
-
try {
|
|
75
|
-
const p = path.resolve(
|
|
76
|
-
path.dirname(fileURLToPath(import.meta.url)),
|
|
77
|
-
'..',
|
|
78
|
-
'..',
|
|
79
|
-
'package.json',
|
|
80
|
-
);
|
|
81
|
-
const raw = JSON.parse(fs.readFileSync(p, 'utf8')) as { version?: string };
|
|
82
|
-
return raw.version ?? '0.0.0';
|
|
83
|
-
} catch {
|
|
84
|
-
return '0.0.0';
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** Build/config snapshot served to the dev toolbar at `/__toil/devinfo`. */
|
|
89
|
-
function devInfo(cfg: ResolvedToilConfig, port: number): Record<string, unknown> {
|
|
90
|
-
const routes: Record<string, string> = {};
|
|
91
|
-
for (const r of scanRoutes(cfg.routesAbsDir)) {
|
|
92
|
-
if (r.slot === undefined && !r.intercept) routes[r.pattern] = r.file;
|
|
93
|
-
}
|
|
94
|
-
return {
|
|
95
|
-
toiljs: frameworkVersion(),
|
|
96
|
-
// vite is a dependency of the framework, not the app, so resolving it from the app root
|
|
97
|
-
// fails; read the running vite's own exported version instead.
|
|
98
|
-
vite: viteVersion,
|
|
99
|
-
react: depVersion(cfg.root, 'react'),
|
|
100
|
-
port,
|
|
101
|
-
enabled: cfg.devtools,
|
|
102
|
-
flags: {
|
|
103
|
-
images: cfg.images,
|
|
104
|
-
fonts: cfg.fonts,
|
|
105
|
-
viewTransitions: cfg.viewTransitions,
|
|
106
|
-
transitions: cfg.transitions,
|
|
107
|
-
seo: cfg.seo != null,
|
|
108
|
-
},
|
|
109
|
-
routes,
|
|
110
|
-
ai: cfg.devtoolsAi !== null,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Resolves a request's `file` param to an absolute path that is genuinely inside the project root,
|
|
116
|
-
* or null. Guards against `..` traversal, sibling-prefix escapes (`<root>-evil/secret` passes a bare
|
|
117
|
-
* `startsWith(root)`), and symlinks inside the project that point outside it (realpath re-check).
|
|
118
|
-
*/
|
|
119
|
-
function safeProjectPath(cfg: ResolvedToilConfig, file: string | null): string | null {
|
|
120
|
-
if (!file) return null;
|
|
121
|
-
const root = cfg.root;
|
|
122
|
-
const inside = (p: string): boolean => p === root || p.startsWith(root + path.sep);
|
|
123
|
-
const abs = path.resolve(file);
|
|
124
|
-
if (!inside(abs) || !fs.existsSync(abs)) return null;
|
|
125
|
-
try {
|
|
126
|
-
const real = fs.realpathSync(abs);
|
|
127
|
-
return inside(real) ? real : null;
|
|
128
|
-
} catch {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* True for requests that must NOT reach the dev endpoints (they open files, read source, and spend
|
|
135
|
-
* AI credits). Uses an allowlist on the browser-set `Sec-Fetch-Site`: only `same-origin` (the
|
|
136
|
-
* toolbar), `none` (user-initiated, e.g. the address bar), or an absent header (non-browser tooling
|
|
137
|
-
* like curl) are allowed. Everything else, `cross-site`, `same-site`, or any unexpected value, is
|
|
138
|
-
* rejected. This blocks CSRF (a malicious site's fetch/img) without breaking local dev tooling.
|
|
139
|
-
*/
|
|
140
|
-
function isCrossSiteRequest(headers: IncomingMessage['headers']): boolean {
|
|
141
|
-
const site = headers['sec-fetch-site'];
|
|
142
|
-
return site !== undefined && site !== 'same-origin' && site !== 'none';
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** Opens `file` in the user's editor (best-effort): `$EDITOR file`, else `code -g file`. */
|
|
146
|
-
function openInEditor(file: string): void {
|
|
147
|
-
try {
|
|
148
|
-
const editor = process.env.EDITOR ?? process.env.VISUAL;
|
|
149
|
-
const child = editor
|
|
150
|
-
? spawn(editor, [file], { stdio: 'ignore', detached: true })
|
|
151
|
-
: spawn('code', ['-g', file], { stdio: 'ignore', detached: true });
|
|
152
|
-
child.on('error', () => undefined);
|
|
153
|
-
child.unref();
|
|
154
|
-
} catch {
|
|
155
|
-
/* ignore */
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Vite plugin that keeps the generated route table in sync during dev: when a route file is
|
|
161
|
-
* added or removed, it regenerates `.toil/routes.ts` and triggers a full reload. Editing a
|
|
162
|
-
* route file's contents hot-reloads through `@vitejs/plugin-react` as usual.
|
|
163
|
-
*/
|
|
164
|
-
export function toilPlugin(cfg: ResolvedToilConfig): Plugin {
|
|
165
|
-
return {
|
|
166
|
-
name: 'toil',
|
|
167
|
-
// Catch empty import specifiers in source and report the file, rolldown otherwise fails
|
|
168
|
-
// resolution with a cryptic "The specifiers must be a non-empty string. Received ''".
|
|
169
|
-
transform(code, id) {
|
|
170
|
-
const file = id.split('?')[0];
|
|
171
|
-
if (
|
|
172
|
-
id.includes('\0') ||
|
|
173
|
-
file.includes('/node_modules/') ||
|
|
174
|
-
!/\.[mc]?[jt]sx?$/.test(file)
|
|
175
|
-
) {
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
const empty =
|
|
179
|
-
/^[ \t]*import\s+(['"])\1\s*;?[ \t]*$/m.test(code) ||
|
|
180
|
-
/^[ \t]*import\b[^'"\n]*\bfrom\s+(['"])\1/m.test(code) ||
|
|
181
|
-
/^[ \t]*export\b[^'"\n]*\bfrom\s+(['"])\1/m.test(code) ||
|
|
182
|
-
/\bimport\s*\(\s*(['"])\1\s*\)/.test(code);
|
|
183
|
-
if (empty) {
|
|
184
|
-
throw new Error(
|
|
185
|
-
`toil: empty import specifier (e.g. \`import '';\`) in ${file}, remove or complete the import.`,
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
return null;
|
|
189
|
-
},
|
|
190
|
-
configureServer(server) {
|
|
191
|
-
// Dev toolbar endpoints (dev only). `/__toil/devinfo` -> build/config snapshot;
|
|
192
|
-
// `/__toil/open?file=` -> open the file in the editor.
|
|
193
|
-
server.middlewares.use('/__toil/devinfo', (req, res) => {
|
|
194
|
-
if (isCrossSiteRequest(req.headers)) {
|
|
195
|
-
res.statusCode = 403;
|
|
196
|
-
res.end();
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
const port = server.config.server.port ?? cfg.port;
|
|
200
|
-
res.setHeader('content-type', 'application/json');
|
|
201
|
-
res.end(JSON.stringify(devInfo(cfg, port)));
|
|
202
|
-
});
|
|
203
|
-
// `/__toil/ai` -> server-side AI proxy. The key is read from the env here and never
|
|
204
|
-
// reaches the browser; 404 when AI isn't configured (the toolbar then only hands off).
|
|
205
|
-
server.middlewares.use('/__toil/ai', (req, res) => {
|
|
206
|
-
if (isCrossSiteRequest(req.headers)) {
|
|
207
|
-
res.statusCode = 403;
|
|
208
|
-
res.end();
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
if (req.method !== 'POST') {
|
|
212
|
-
res.statusCode = 405;
|
|
213
|
-
res.end();
|
|
214
|
-
return;
|
|
215
|
-
}
|
|
216
|
-
const ai = cfg.devtoolsAi;
|
|
217
|
-
if (!ai) {
|
|
218
|
-
res.statusCode = 404;
|
|
219
|
-
res.end();
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
let body = '';
|
|
223
|
-
let aborted = false;
|
|
224
|
-
req.on('data', (chunk) => {
|
|
225
|
-
if (aborted) return;
|
|
226
|
-
body += String(chunk);
|
|
227
|
-
if (body.length > 100_000) {
|
|
228
|
-
// Cap the request body so a runaway/malicious POST can't grow it unbounded.
|
|
229
|
-
aborted = true;
|
|
230
|
-
res.statusCode = 413;
|
|
231
|
-
res.end();
|
|
232
|
-
req.destroy();
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
req.on('end', () => {
|
|
236
|
-
if (aborted) return;
|
|
237
|
-
void (async () => {
|
|
238
|
-
try {
|
|
239
|
-
const parsed = JSON.parse(body || '{}') as { prompt?: string };
|
|
240
|
-
// Cap the prompt actually forwarded upstream (independent of the raw-body cap).
|
|
241
|
-
const prompt =
|
|
242
|
-
typeof parsed.prompt === 'string'
|
|
243
|
-
? parsed.prompt.slice(0, 16000)
|
|
244
|
-
: '';
|
|
245
|
-
const text = await aiComplete(ai, prompt);
|
|
246
|
-
res.setHeader('content-type', 'application/json');
|
|
247
|
-
res.end(JSON.stringify({ text }));
|
|
248
|
-
} catch (e) {
|
|
249
|
-
// Log the detail to the dev's terminal; return a generic message to the
|
|
250
|
-
// client so upstream/provider error text is never reflected over HTTP.
|
|
251
|
-
process.stderr.write(
|
|
252
|
-
`toil: /__toil/ai failed: ${e instanceof Error ? e.message : String(e)}\n`,
|
|
253
|
-
);
|
|
254
|
-
res.statusCode = 500;
|
|
255
|
-
res.setHeader('content-type', 'application/json');
|
|
256
|
-
res.end(
|
|
257
|
-
JSON.stringify({
|
|
258
|
-
error: 'AI request failed (see dev server logs).',
|
|
259
|
-
}),
|
|
260
|
-
);
|
|
261
|
-
}
|
|
262
|
-
})();
|
|
263
|
-
});
|
|
264
|
-
});
|
|
265
|
-
server.middlewares.use('/__toil/open', (req, res) => {
|
|
266
|
-
try {
|
|
267
|
-
if (isCrossSiteRequest(req.headers)) {
|
|
268
|
-
res.statusCode = 403;
|
|
269
|
-
res.end();
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
const url = new URL(req.url ?? '', 'http://localhost');
|
|
273
|
-
const abs = safeProjectPath(cfg, url.searchParams.get('file'));
|
|
274
|
-
if (abs) openInEditor(abs);
|
|
275
|
-
res.statusCode = 204;
|
|
276
|
-
res.end();
|
|
277
|
-
} catch {
|
|
278
|
-
res.statusCode = 400;
|
|
279
|
-
res.end();
|
|
280
|
-
}
|
|
281
|
-
});
|
|
282
|
-
// `/__toil/source?file=` -> the file's text, so the AI tab can include the page's code in
|
|
283
|
-
// its prompt. Same root-confinement (`safeProjectPath`) as `/__toil/open`; capped so a
|
|
284
|
-
// stray huge file can't bloat the response.
|
|
285
|
-
server.middlewares.use('/__toil/source', (req, res) => {
|
|
286
|
-
try {
|
|
287
|
-
if (isCrossSiteRequest(req.headers)) {
|
|
288
|
-
res.statusCode = 403;
|
|
289
|
-
res.end();
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
const url = new URL(req.url ?? '', 'http://localhost');
|
|
293
|
-
const abs = safeProjectPath(cfg, url.searchParams.get('file'));
|
|
294
|
-
if (!abs) {
|
|
295
|
-
res.statusCode = 404;
|
|
296
|
-
res.end();
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
const text = fs.readFileSync(abs, 'utf8').slice(0, 20000);
|
|
300
|
-
res.setHeader('content-type', 'text/plain; charset=utf-8');
|
|
301
|
-
res.end(text);
|
|
302
|
-
} catch {
|
|
303
|
-
res.statusCode = 400;
|
|
304
|
-
res.end();
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
// Email preview tool (dev only). `/__toil/emails` -> a standalone page that lists
|
|
309
|
-
// `emails/*.tsx`, renders the selected one through the live SSR server (so edits and
|
|
310
|
-
// imported `client/*` CSS show), and live-refreshes by polling `/version`.
|
|
311
|
-
// Sub-paths are registered before the page so connect's prefix match resolves them first.
|
|
312
|
-
server.middlewares.use('/__toil/emails/list', (req, res) => {
|
|
313
|
-
if (isCrossSiteRequest(req.headers)) {
|
|
314
|
-
res.statusCode = 403;
|
|
315
|
-
res.end();
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
res.setHeader('content-type', 'application/json');
|
|
319
|
-
res.end(JSON.stringify(listEmails(cfg)));
|
|
320
|
-
});
|
|
321
|
-
server.middlewares.use('/__toil/emails/render', (req, res) => {
|
|
322
|
-
if (isCrossSiteRequest(req.headers)) {
|
|
323
|
-
res.statusCode = 403;
|
|
324
|
-
res.end();
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
void (async () => {
|
|
328
|
-
try {
|
|
329
|
-
const name = new URL(req.url ?? '', 'http://localhost').searchParams.get(
|
|
330
|
-
'name',
|
|
331
|
-
);
|
|
332
|
-
if (!name) {
|
|
333
|
-
res.statusCode = 400;
|
|
334
|
-
res.end();
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
const rendered = await renderEmailByName(server, cfg, name);
|
|
338
|
-
if (!rendered) {
|
|
339
|
-
res.statusCode = 404;
|
|
340
|
-
res.end();
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
|
-
res.setHeader('content-type', 'application/json');
|
|
344
|
-
res.end(JSON.stringify(rendered));
|
|
345
|
-
} catch (e) {
|
|
346
|
-
// Log the detail to the dev's terminal; the page shows a generic message.
|
|
347
|
-
process.stderr.write(
|
|
348
|
-
`toil: /__toil/emails/render failed: ${e instanceof Error ? e.message : String(e)}\n`,
|
|
349
|
-
);
|
|
350
|
-
res.statusCode = 500;
|
|
351
|
-
res.end();
|
|
352
|
-
}
|
|
353
|
-
})();
|
|
354
|
-
});
|
|
355
|
-
// A tiny mtime fingerprint of `emails/*` + client CSS the page polls (~1s) to detect
|
|
356
|
-
// edits. Polling (not SSE) because the wasm dev server proxies `/__toil/*` to Vite by
|
|
357
|
-
// buffering the whole response, so a long-lived stream would hang; a short poll works
|
|
358
|
-
// in every mode.
|
|
359
|
-
server.middlewares.use('/__toil/emails/version', (req, res) => {
|
|
360
|
-
if (isCrossSiteRequest(req.headers)) {
|
|
361
|
-
res.statusCode = 403;
|
|
362
|
-
res.end();
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
res.setHeader('content-type', 'text/plain; charset=utf-8');
|
|
366
|
-
res.end(emailsVersion(cfg));
|
|
367
|
-
});
|
|
368
|
-
server.middlewares.use('/__toil/emails', (req, res) => {
|
|
369
|
-
if (isCrossSiteRequest(req.headers)) {
|
|
370
|
-
res.statusCode = 403;
|
|
371
|
-
res.end();
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
res.setHeader('content-type', 'text/html; charset=utf-8');
|
|
375
|
-
res.end(previewShellHtml());
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
// Trailing slash so a sibling like `routes-extra/` doesn't match the `routes/` prefix.
|
|
379
|
-
const routesPrefix = cfg.routesAbsDir.replace(/\\/g, '/').replace(/\/?$/, '/');
|
|
380
|
-
const onChange = (file: string): void => {
|
|
381
|
-
if (file.replace(/\\/g, '/').startsWith(routesPrefix)) {
|
|
382
|
-
generate(cfg);
|
|
383
|
-
server.ws.send({ type: 'full-reload' });
|
|
384
|
-
}
|
|
385
|
-
};
|
|
386
|
-
server.watcher.add(cfg.routesAbsDir);
|
|
387
|
-
server.watcher.on('add', onChange);
|
|
388
|
-
server.watcher.on('unlink', onChange);
|
|
389
|
-
},
|
|
390
|
-
};
|
|
391
|
-
}
|