toiljs 0.0.85 → 0.0.87
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 +5 -0
- package/README.md +2 -2
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +303 -293
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +2 -0
- package/build/compiler/config.js +1 -0
- package/build/compiler/docs.js +8 -24
- package/build/compiler/generate.js +4 -2
- package/build/compiler/index.d.ts +1 -1
- package/build/compiler/index.js +69 -6
- package/build/compiler/toil-docs.generated.js +64 -22
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/analytics/index.js +7 -3
- package/build/devserver/db/database.d.ts +4 -0
- package/build/devserver/db/database.js +43 -1
- package/build/devserver/db/index.d.ts +1 -1
- package/build/devserver/db/index.js +1 -1
- package/build/devserver/db/types.d.ts +3 -0
- package/build/devserver/db/types.js +2 -0
- package/build/devserver/runtime/module.js +4 -1
- package/docs/README.md +104 -65
- package/docs/auth/README.md +102 -0
- package/docs/auth/configuration.md +94 -0
- package/docs/auth/extending.md +202 -0
- package/docs/auth/how-it-works.md +138 -0
- package/docs/auth/usage.md +188 -0
- package/docs/backend/README.md +143 -0
- package/docs/backend/data.md +351 -0
- package/docs/backend/rest.md +402 -0
- package/docs/backend/rpc.md +226 -0
- package/docs/background/README.md +114 -0
- package/docs/background/daemons.md +230 -0
- package/docs/background/derive.md +179 -0
- package/docs/cli/README.md +377 -0
- package/docs/concepts/config.md +416 -0
- package/docs/concepts/decorators.md +127 -0
- package/docs/concepts/security.md +108 -0
- package/docs/concepts/tiers.md +166 -0
- package/docs/concepts/types.md +216 -0
- package/docs/database/README.md +143 -0
- package/docs/database/capacity.md +350 -0
- package/docs/database/counters.md +174 -0
- package/docs/database/documents.md +255 -0
- package/docs/database/events.md +307 -0
- package/docs/database/membership.md +246 -0
- package/docs/database/setup.md +155 -0
- package/docs/database/unique.md +216 -0
- package/docs/database/views.md +246 -0
- package/docs/frontend/README.md +101 -0
- package/docs/frontend/data-fetching.md +243 -0
- package/docs/frontend/images.md +148 -0
- package/docs/frontend/metadata.md +344 -0
- package/docs/frontend/rendering.md +236 -0
- package/docs/frontend/routing.md +344 -0
- package/docs/frontend/scripts.md +118 -0
- package/docs/frontend/search.md +191 -0
- package/docs/frontend/styling.md +147 -0
- package/docs/getting-started/README.md +81 -0
- package/docs/getting-started/create-project.md +131 -0
- package/docs/getting-started/deploy.md +101 -0
- package/docs/getting-started/first-app.md +215 -0
- package/docs/getting-started/installation.md +106 -0
- package/docs/getting-started/migrating.md +125 -0
- package/docs/getting-started/project-structure.md +163 -0
- package/docs/introduction/README.md +41 -0
- package/docs/introduction/design-principles.md +55 -0
- package/docs/introduction/distributed.md +74 -0
- package/docs/introduction/how-it-works.md +74 -0
- package/docs/introduction/hyperscale.md +51 -0
- package/docs/introduction/modern-stack.md +36 -0
- package/docs/introduction/vs-other-frameworks.md +48 -0
- package/docs/introduction/why-toil.md +93 -0
- package/docs/llms.txt +90 -0
- package/docs/realtime/README.md +102 -0
- package/docs/realtime/channels.md +211 -0
- package/docs/realtime/streams.md +369 -0
- package/docs/services/README.md +60 -0
- package/docs/services/analytics.md +268 -0
- package/docs/services/caching.md +175 -0
- package/docs/services/cookies.md +235 -0
- package/docs/services/crypto.md +209 -0
- package/docs/services/email.md +289 -0
- package/docs/services/environment.md +141 -0
- package/docs/services/ratelimit.md +174 -0
- package/docs/services/time.md +85 -0
- package/examples/basic/client/routes/analytics.tsx +13 -12
- package/examples/basic/server/models/SiteAnalytics.ts +29 -17
- package/examples/basic/server/routes/Analytics.ts +15 -17
- package/examples/basic/server/routes/UserId.ts +22 -0
- package/package.json +15 -11
- package/scripts/gen-toil-docs.mjs +24 -35
- package/server/auth/AuthController.ts +336 -0
- package/server/auth/AuthUser.ts +23 -0
- package/server/auth/index.ts +16 -0
- package/server/globals/auth.ts +31 -0
- package/server/globals/userid.ts +107 -0
- package/src/compiler/config.ts +13 -0
- package/src/compiler/docs.ts +16 -33
- package/src/compiler/generate.ts +6 -2
- package/src/compiler/index.ts +114 -6
- package/src/compiler/toil-docs.generated.ts +64 -22
- package/src/devserver/analytics/index.ts +10 -4
- package/src/devserver/db/database.ts +67 -1
- package/src/devserver/db/index.ts +1 -0
- package/src/devserver/db/types.ts +13 -0
- package/src/devserver/runtime/module.ts +7 -0
- package/test/analytics-dev.test.ts +2 -1
- package/test/devserver-database.test.ts +113 -0
- package/docs/auth-todo.md +0 -149
- package/docs/auth.md +0 -322
- package/docs/caching.md +0 -115
- package/docs/cli.md +0 -17
- package/docs/client.md +0 -39
- package/docs/cookies.md +0 -457
- package/docs/crypto.md +0 -130
- package/docs/daemon.md +0 -123
- package/docs/data.md +0 -131
- package/docs/derive.md +0 -159
- package/docs/email.md +0 -326
- package/docs/environment.md +0 -97
- package/docs/getting-started.md +0 -128
- package/docs/index.md +0 -30
- package/docs/ratelimit.md +0 -95
- package/docs/routing.md +0 -259
- package/docs/rpc.md +0 -149
- package/docs/server.md +0 -61
- package/docs/ssr.md +0 -632
- package/docs/streams.md +0 -178
- package/docs/styling.md +0 -22
- package/docs/tiers.md +0 -133
- package/docs/time.md +0 -43
package/build/compiler/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import fs from 'node:fs';
|
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
4
|
import net from 'node:net';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
6
7
|
import pc from 'picocolors';
|
|
7
8
|
import { build as viteBuild, createServer, mergeConfig } from 'vite';
|
|
8
9
|
import { loadConfig } from './config.js';
|
|
@@ -76,7 +77,48 @@ function serverEntryFiles(root) {
|
|
|
76
77
|
visit(dir, 0);
|
|
77
78
|
return [...result].sort();
|
|
78
79
|
}
|
|
79
|
-
|
|
80
|
+
function authEntryFiles(root, appHasUser) {
|
|
81
|
+
const names = appHasUser ? ['AuthController.ts'] : ['AuthUser.ts', 'AuthController.ts'];
|
|
82
|
+
const primary = names.map((n) => path.posix.join('node_modules/toiljs/server/auth', n));
|
|
83
|
+
if (primary.every((rel) => fs.existsSync(path.join(root, rel))))
|
|
84
|
+
return primary;
|
|
85
|
+
try {
|
|
86
|
+
const pkgDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
|
87
|
+
const abs = names.map((n) => path.join(pkgDir, 'server', 'auth', n));
|
|
88
|
+
if (abs.every((p) => fs.existsSync(p)))
|
|
89
|
+
return abs.map((p) => path.relative(root, p).replace(/\\/g, '/'));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
}
|
|
93
|
+
throw new Error('toiljs: server.auth is enabled but the built-in auth controller ' +
|
|
94
|
+
'(server/auth/AuthController.ts) was not found under node_modules/toiljs. ' +
|
|
95
|
+
'Reinstall toiljs (npm i toiljs), or remove server.auth from toil.config.ts.');
|
|
96
|
+
}
|
|
97
|
+
function serverDeclaresUser(root, files) {
|
|
98
|
+
const re = /(^|[^.\w])@user\b/m;
|
|
99
|
+
for (const rel of files) {
|
|
100
|
+
try {
|
|
101
|
+
if (re.test(fs.readFileSync(path.join(root, rel), 'utf8')))
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
function serverImportsAuth(root, files) {
|
|
110
|
+
const re = /(^|[^.\w])import\s+['"]toiljs\/server\/auth['"]/m;
|
|
111
|
+
for (const rel of files) {
|
|
112
|
+
try {
|
|
113
|
+
if (re.test(fs.readFileSync(path.join(root, rel), 'utf8')))
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
export async function buildServer(root, auth = false) {
|
|
80
122
|
if (!fs.existsSync(path.join(root, 'toilconfig.json')))
|
|
81
123
|
return;
|
|
82
124
|
for (const dir of serverDirs(root)) {
|
|
@@ -89,6 +131,17 @@ export async function buildServer(root) {
|
|
|
89
131
|
}
|
|
90
132
|
const binJs = resolveToilscriptBin(root);
|
|
91
133
|
const files = serverEntryFiles(root);
|
|
134
|
+
const authOn = auth || serverImportsAuth(root, files);
|
|
135
|
+
if (authOn) {
|
|
136
|
+
const appHasUser = serverDeclaresUser(root, files);
|
|
137
|
+
const authFiles = authEntryFiles(root, appHasUser);
|
|
138
|
+
const authSet = new Set(authFiles);
|
|
139
|
+
files.unshift(...authFiles);
|
|
140
|
+
for (let i = files.length - 1; i >= authFiles.length; i--) {
|
|
141
|
+
if (authSet.has(files[i]))
|
|
142
|
+
files.splice(i, 1);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
92
145
|
const split = splitSurfaceFiles(root, files);
|
|
93
146
|
assertNoStreamInRequestTier(root, split);
|
|
94
147
|
if (split.hasDaemon || split.hasStream) {
|
|
@@ -98,12 +151,14 @@ export async function buildServer(root) {
|
|
|
98
151
|
mode: 'cold',
|
|
99
152
|
outFile: artifacts.cold,
|
|
100
153
|
withRpc: false,
|
|
154
|
+
authUser: authOn,
|
|
101
155
|
});
|
|
102
156
|
if (split.hasStream && split.stream.length > 0)
|
|
103
157
|
await runToilscriptPass(root, binJs, split.stream, {
|
|
104
158
|
mode: 'hot',
|
|
105
159
|
outFile: artifacts.stream,
|
|
106
160
|
withRpc: false,
|
|
161
|
+
authUser: authOn,
|
|
107
162
|
});
|
|
108
163
|
if (split.request.length > 0)
|
|
109
164
|
await runToilscriptPass(root, binJs, split.request, {
|
|
@@ -111,12 +166,18 @@ export async function buildServer(root) {
|
|
|
111
166
|
outFile: serverWasmFile(root),
|
|
112
167
|
withRpc: true,
|
|
113
168
|
rpcSurfaceFiles: split.hasStream ? split.stream : undefined,
|
|
169
|
+
authUser: authOn,
|
|
114
170
|
});
|
|
115
171
|
if (split.hasStream && split.stream.length > 0)
|
|
116
172
|
emitStreamClientSurface(root, artifacts.stream, split.stream);
|
|
117
173
|
return;
|
|
118
174
|
}
|
|
119
|
-
await runToilscriptPass(root, binJs, files, {
|
|
175
|
+
await runToilscriptPass(root, binJs, files, {
|
|
176
|
+
mode: null,
|
|
177
|
+
outFile: null,
|
|
178
|
+
withRpc: true,
|
|
179
|
+
authUser: authOn,
|
|
180
|
+
});
|
|
120
181
|
}
|
|
121
182
|
function resolveToilscriptBin(root) {
|
|
122
183
|
const require = createRequire(path.join(root, 'package.json'));
|
|
@@ -272,6 +333,8 @@ function runToilscriptPass(root, binJs, files, opts) {
|
|
|
272
333
|
if (opts.rpcSurfaceFiles)
|
|
273
334
|
for (const surfaceFile of opts.rpcSurfaceFiles)
|
|
274
335
|
args.push('--rpcSurfaceFiles', surfaceFile);
|
|
336
|
+
if (opts.authUser)
|
|
337
|
+
args.push('--authUser');
|
|
275
338
|
args.push('--noConfigEntries');
|
|
276
339
|
args.push('--disableWarning', '235');
|
|
277
340
|
return new Promise((resolve, reject) => {
|
|
@@ -298,7 +361,7 @@ function watchServer(cfg, watcher) {
|
|
|
298
361
|
building = true;
|
|
299
362
|
process.stdout.write(pc.dim(' server changed, rebuilding…') + '\n');
|
|
300
363
|
renderEmails(cfg)
|
|
301
|
-
.then(() => buildServer(root))
|
|
364
|
+
.then(() => buildServer(root, cfg.auth))
|
|
302
365
|
.then(() => process.stdout.write(pc.green(' ✓ ') + pc.dim('server rebuilt') + '\n'))
|
|
303
366
|
.catch((e) => process.stdout.write(pc.red(` ✗ server rebuild failed: ${String(e)}`) + '\n'))
|
|
304
367
|
.finally(() => {
|
|
@@ -486,7 +549,7 @@ export async function dev(opts = {}) {
|
|
|
486
549
|
generate(cfg);
|
|
487
550
|
if (hasServer)
|
|
488
551
|
await extractServerSlots(cfg);
|
|
489
|
-
await buildServer(cfg.root);
|
|
552
|
+
await buildServer(cfg.root, cfg.auth);
|
|
490
553
|
if (hasServer)
|
|
491
554
|
process.stdout.write(pc.green(' ✓ ') + pc.dim('server built') + '\n');
|
|
492
555
|
if (!hasServer) {
|
|
@@ -564,7 +627,7 @@ export async function build(opts = {}) {
|
|
|
564
627
|
await renderEmails(cfg);
|
|
565
628
|
generate(cfg);
|
|
566
629
|
const priorServerSlots = hasServer ? await extractServerSlots(cfg) : new Map();
|
|
567
|
-
await buildServer(cfg.root);
|
|
630
|
+
await buildServer(cfg.root, cfg.auth);
|
|
568
631
|
if (opts.serverOnly)
|
|
569
632
|
return;
|
|
570
633
|
if (hasServer)
|
|
@@ -584,7 +647,7 @@ export async function build(opts = {}) {
|
|
|
584
647
|
const ssr = await extractTemplates(cfg, 'edge', priorServerSlots);
|
|
585
648
|
if (ssr.serverSlotsChanged) {
|
|
586
649
|
process.stdout.write(pc.dim(' SSR template changed; recompiling the server with the new hash…') + '\n');
|
|
587
|
-
await buildServer(cfg.root);
|
|
650
|
+
await buildServer(cfg.root, cfg.auth);
|
|
588
651
|
}
|
|
589
652
|
}
|
|
590
653
|
export async function start(opts = {}) {
|