@rsc-kit/mcp 0.40.0 → 0.42.0
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/dist/recipes.js +37 -20
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +206 -6
- package/guides/api-routes.md +159 -215
- package/guides/authorization.md +103 -114
- package/guides/backend-answered-pages.md +301 -250
- package/guides/bap-vs-inertia.md +67 -25
- package/guides/blueprints.md +307 -174
- package/guides/bun.md +42 -25
- package/guides/caching.md +46 -49
- package/guides/coming-from-inertia.md +5 -5
- package/guides/commands.md +251 -200
- package/guides/connection.md +50 -54
- package/guides/content-security-policy.md +133 -84
- package/guides/domains.md +190 -149
- package/guides/edge-caching.md +242 -268
- package/guides/emails.md +96 -45
- package/guides/errors.md +116 -137
- package/guides/feature-flags.md +100 -30
- package/guides/file-uploads.md +101 -26
- package/guides/fonts.md +139 -110
- package/guides/forms.md +520 -538
- package/guides/images.md +87 -28
- package/guides/index.json +13 -33
- package/guides/instrumentation.md +134 -95
- package/guides/isr.md +3 -3
- package/guides/live-data-in-production.md +695 -0
- package/guides/live-data.md +170 -839
- package/guides/mcp.md +83 -59
- package/guides/metadata.md +124 -179
- package/guides/navigation.md +118 -147
- package/guides/no-javascript.md +49 -57
- package/guides/offline.md +118 -153
- package/guides/openapi.md +100 -35
- package/guides/pages.md +160 -50
- package/guides/ppr.md +178 -256
- package/guides/pwa.md +251 -218
- package/guides/queries.md +171 -300
- package/guides/react-compiler.md +90 -50
- package/guides/redirects.md +141 -177
- package/guides/response-headers.md +76 -74
- package/guides/route-interception.md +112 -111
- package/guides/routing.md +216 -322
- package/guides/sections.md +63 -66
- package/guides/seo-files.md +127 -65
- package/guides/server-actions.md +285 -357
- package/guides/static-generation.md +131 -199
- package/guides/testing.md +272 -150
- package/guides/third-party-scripts.md +94 -47
- package/guides/typed-routes.md +115 -113
- package/guides/url-validation.md +113 -61
- package/guides/validation.md +53 -62
- package/guides/view-transitions.md +159 -132
- package/guides/where-it-runs.md +226 -162
- package/package.json +2 -2
- package/guides/admin.md +0 -520
- package/guides/deployment.md +0 -598
- package/guides/go.md +0 -538
- package/guides/laravel.md +0 -786
- package/guides/your-own-backend.md +0 -587
package/guides/commands.md
CHANGED
|
@@ -2,10 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
> A release step or a worker is the same app started differently — ./app migrate.
|
|
4
4
|
|
|
5
|
-
A
|
|
6
|
-
|
|
7
|
-
Those are your code, with your dependencies, started differently. A file in
|
|
8
|
-
`src/commands` is one of them:
|
|
5
|
+
A command is your app's code started without the server: a migration before a
|
|
6
|
+
deploy, a queue worker, a nightly cleanup. Put a file in `src/commands`:
|
|
9
7
|
|
|
10
8
|
```ts title="src/commands/migrate.ts"
|
|
11
9
|
import { db } from '@repo/database'
|
|
@@ -21,21 +19,45 @@ export default async function (args: string[]) {
|
|
|
21
19
|
```
|
|
22
20
|
|
|
23
21
|
```sh
|
|
24
|
-
./app migrate --dry-run
|
|
22
|
+
./app migrate --dry-run # the compiled binary
|
|
25
23
|
node .output/server/commands.mjs migrate --dry-run # a plain build, node or bun
|
|
26
24
|
```
|
|
27
25
|
|
|
28
|
-
`./app` with no arguments starts the server
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
`./app` with no arguments starts the server. Anything else runs a command, and
|
|
27
|
+
the server is not started, so a release step beside the running version doesn't
|
|
28
|
+
try to bind its port.
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
Commands are built into the same bundle as the pages. `@repo/database` and the
|
|
31
|
+
logger are the same packages the pages use, and a binary carries the commands
|
|
32
|
+
inside it.
|
|
35
33
|
|
|
36
|
-
|
|
34
|
+
A Laravel app's commands are artisan's. See [Laravel commands](/hosts/laravel/commands).
|
|
37
35
|
|
|
38
|
-
|
|
36
|
+
## Writing a command
|
|
37
|
+
|
|
38
|
+
A command is a file directly in `src/commands` with a default export. Wrap it in
|
|
39
|
+
`defineCommand` to get the types without writing them:
|
|
40
|
+
|
|
41
|
+
```ts title="src/commands/seed.ts"
|
|
42
|
+
import { parseArgs } from 'node:util'
|
|
43
|
+
import { defineCommand } from '@rsc-kit/core/commands'
|
|
44
|
+
import { db } from '@repo/database'
|
|
45
|
+
|
|
46
|
+
export const description = 'Fill the database with sample data'
|
|
47
|
+
export const register = true
|
|
48
|
+
|
|
49
|
+
export default defineCommand(async (args) => {
|
|
50
|
+
const { values } = parseArgs({ args, options: { count: { type: 'string', default: '10' } } })
|
|
51
|
+
|
|
52
|
+
await db.seed({ count: Number(values.count) })
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
./app seed --count 50
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The full signature, if you prefer to annotate it yourself:
|
|
39
61
|
|
|
40
62
|
```ts
|
|
41
63
|
import type { CommandContext } from '@rsc-kit/core/commands'
|
|
@@ -43,30 +65,43 @@ import type { CommandContext } from '@rsc-kit/core/commands'
|
|
|
43
65
|
export default async function (args: string[], context: CommandContext): Promise<number | void>
|
|
44
66
|
```
|
|
45
67
|
|
|
46
|
-
`defineCommand` types a command without writing the annotations, and returns
|
|
47
|
-
what it is given: `export default defineCommand(async (args, context) => {})`.
|
|
48
|
-
Both come from `@rsc-kit/core/commands`.
|
|
49
|
-
|
|
50
68
|
- **`args`** is everything after the name, untouched. `./app migrate --help`
|
|
51
|
-
gives `migrate` the `--help
|
|
52
|
-
-
|
|
53
|
-
`255` is itself. A command that throws exits `1` with the error and every
|
|
54
|
-
`cause` under it, each on a `[cause]:` line - a library that wraps an error,
|
|
55
|
-
as Drizzle does a failed query, keeps the reason there. One that returns
|
|
56
|
-
anything else (a string, `256`) exits `1` and says what it returned, rather
|
|
57
|
-
than reporting success for a mistake.
|
|
58
|
-
- **`context`** is `{ signal, deadline }`: `signal` is aborted by `SIGTERM` and
|
|
59
|
-
`SIGINT`, and `deadline` says when the command has to be done. See
|
|
69
|
+
gives `migrate` the `--help`. Parsing is yours.
|
|
70
|
+
- **`context`** is `{ signal, deadline }`, for knowing when to stop. See
|
|
60
71
|
[a worker](#a-worker).
|
|
61
|
-
- **`description`**, an optional exported string, is what `./app --help`
|
|
62
|
-
beside the name. It
|
|
63
|
-
listing never loads a command.
|
|
72
|
+
- **`description`**, an optional exported string, is what `./app --help` shows
|
|
73
|
+
beside the name. It must be a plain string literal: the build reads it from
|
|
74
|
+
the source, so listing never loads a command.
|
|
75
|
+
|
|
76
|
+
The name is the file name without its extension. These are not commands:
|
|
77
|
+
|
|
78
|
+
- declaration files and tests (`*.test.ts`, `*.spec.ts`);
|
|
79
|
+
- anything starting with `_` or `.`, so `_shared.ts` is how two commands share
|
|
80
|
+
a helper;
|
|
81
|
+
- a file in a folder. `src/commands/db/seed.ts` is not `db:seed`, and the build
|
|
82
|
+
says so rather than leaving it out silently.
|
|
83
|
+
|
|
84
|
+
## Exit codes
|
|
85
|
+
|
|
86
|
+
The return value is the exit code:
|
|
87
|
+
|
|
88
|
+
```ts title="src/commands/check-links.ts"
|
|
89
|
+
export default async function () {
|
|
90
|
+
const broken = await findBrokenLinks()
|
|
64
91
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
92
|
+
for (const link of broken) console.error(link)
|
|
93
|
+
|
|
94
|
+
return broken.length > 0 ? 1 : 0
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
- Returning nothing exits `0`. A number from `0` to `255` exits with itself.
|
|
99
|
+
- A command that throws exits `1`, printing the error and every `cause` under
|
|
100
|
+
it, each on a `[cause]:` line. A library that wraps an error, as Drizzle does
|
|
101
|
+
a failed query, keeps the reason there.
|
|
102
|
+
- Returning anything else (a string, `256`) exits `1` and says what was
|
|
103
|
+
returned, rather than reporting success for a mistake.
|
|
104
|
+
- An unknown name exits `2`, as any misused tool does.
|
|
70
105
|
|
|
71
106
|
```
|
|
72
107
|
$ ./app --help
|
|
@@ -83,35 +118,74 @@ $ echo $?
|
|
|
83
118
|
2
|
|
84
119
|
```
|
|
85
120
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
121
|
+
A deploy that runs `./app migrate` and stops on a non-zero exit is right in
|
|
122
|
+
both failure cases.
|
|
123
|
+
|
|
124
|
+
## Running one
|
|
125
|
+
|
|
126
|
+
Anywhere that can run the binary or the build. One image serves the server and
|
|
127
|
+
every command:
|
|
128
|
+
|
|
129
|
+
```dockerfile
|
|
130
|
+
FROM oven/bun:1
|
|
131
|
+
COPY dist/app /app
|
|
132
|
+
ENTRYPOINT ["/app"]
|
|
133
|
+
CMD []
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```sh
|
|
137
|
+
docker run --rm myapp # the server
|
|
138
|
+
docker run --rm myapp migrate # a release step
|
|
139
|
+
docker run --rm myapp work # a worker
|
|
140
|
+
```
|
|
89
141
|
|
|
90
|
-
|
|
142
|
+
The same shape is a Kubernetes Job's `args: ["migrate"]`, a platform's release
|
|
143
|
+
command, or a process type. Nothing in `src/commands` names a platform.
|
|
144
|
+
|
|
145
|
+
From a plain build, run the commands entry:
|
|
146
|
+
|
|
147
|
+
```sh
|
|
148
|
+
node .output/server/commands.mjs migrate
|
|
149
|
+
bun .output/server/commands.mjs migrate
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
<Aside type="caution" title="Not .output/server/index.mjs">
|
|
153
|
+
`index.mjs` is the server, and it does not read its arguments:
|
|
154
|
+
`node .output/server/index.mjs migrate` starts the server and migrates
|
|
155
|
+
nothing. `commands.mjs` is the entry for a command. The binary has both
|
|
156
|
+
behind one file because it knows from `argv` which was meant.
|
|
157
|
+
</Aside>
|
|
158
|
+
|
|
159
|
+
The build lists the commands in `.output/rsc-kit.json` as `commands`, for a
|
|
160
|
+
deploy that wants to check before running one.
|
|
161
|
+
|
|
162
|
+
## Using the server's setup
|
|
91
163
|
|
|
92
164
|
`src/instrumentation.ts` [validates the environment at import and connects in
|
|
93
|
-
`register()`](/guides/instrumentation/).
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
that
|
|
165
|
+
`register()`](/guides/instrumentation/). A command doesn't load it unless it
|
|
166
|
+
asks:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
export const register = true
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
- **By default it is not imported.** The command runs from a bundle of its own,
|
|
173
|
+
without instrumentation.ts, so a queue drain doesn't fail on the web's Stripe key.
|
|
174
|
+
What the command imports still runs: one that imports `@repo/env/worker`
|
|
175
|
+
validates the worker's schema, and only that one.
|
|
176
|
+
- **With `export const register = true`**, the command runs from the server's bundle. instrumentation.ts is
|
|
177
|
+
imported, so its environment is validated, and `register()` runs before the
|
|
178
|
+
command. It has to be the literal `true`: the build reads it from the source
|
|
179
|
+
to choose the bundle, and refuses a computed one that is true.
|
|
180
|
+
|
|
181
|
+
A command that opted in gets `register()` first and `shutdown()` after it
|
|
182
|
+
returns, or after it throws, so the pool `register()` opened is closed. Both are
|
|
183
|
+
bounded by `RSC_SHUTDOWN_TIMEOUT` (10 seconds). A `shutdown()` that fails is
|
|
184
|
+
reported without changing the command's exit code.
|
|
110
185
|
|
|
111
186
|
## A worker
|
|
112
187
|
|
|
113
|
-
A command that
|
|
114
|
-
how it is told:
|
|
188
|
+
A command that runs until it is told to stop is a worker. `signal` tells it:
|
|
115
189
|
|
|
116
190
|
```ts title="src/commands/work.ts"
|
|
117
191
|
import { defineCommand } from '@rsc-kit/core/commands'
|
|
@@ -125,29 +199,21 @@ export default defineCommand(async (_args, { signal }) => {
|
|
|
125
199
|
```
|
|
126
200
|
|
|
127
201
|
The first `SIGTERM` or `SIGINT` aborts `signal` and gives the command
|
|
128
|
-
`RSC_SHUTDOWN_TIMEOUT` seconds to return
|
|
129
|
-
`shutdown()` and exits with the command's code, `0` for a clean stop. A
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
`abort` event fires while the command is running.
|
|
144
|
-
|
|
145
|
-
So check `signal.aborted` as the command starts, and listen for `abort` for the
|
|
146
|
-
rest, or pass `signal` to something that does (`sleep(ms, undefined, { signal })`
|
|
147
|
-
rejects at once). Checking alone misses a stop that arrives during a long
|
|
148
|
-
`await`; listening alone waits for ever on a stop that came first. The worker
|
|
149
|
-
above does both: the loop tests `signal.aborted`, and `queue.processNext({ signal })`
|
|
150
|
-
returns when the signal aborts. A command that only waits does it by hand:
|
|
202
|
+
`RSC_SHUTDOWN_TIMEOUT` seconds to return. If it does, the process runs
|
|
203
|
+
`shutdown()` and exits with the command's code, `0` for a clean stop. A command
|
|
204
|
+
that doesn't stop in time exits `1` and says so. A second signal doesn't wait.
|
|
205
|
+
|
|
206
|
+
### Catching a stop that came early
|
|
207
|
+
|
|
208
|
+
A stop can arrive before the command starts, while the app loads or while
|
|
209
|
+
`register()` runs. Then `signal` is already aborted, and the `abort` event fired
|
|
210
|
+
before the command could listen. So a command that waits needs both: it checks
|
|
211
|
+
`signal.aborted` as it starts, and listens for `abort` after that, or passes
|
|
212
|
+
`signal` to something that does.
|
|
213
|
+
|
|
214
|
+
The worker above does both: the loop tests `signal.aborted`, and
|
|
215
|
+
`queue.processNext({ signal })` returns when the signal aborts. A command that
|
|
216
|
+
only waits does it by hand:
|
|
151
217
|
|
|
152
218
|
```ts
|
|
153
219
|
export default defineCommand(async (_args, { signal }) => {
|
|
@@ -156,10 +222,26 @@ export default defineCommand(async (_args, { signal }) => {
|
|
|
156
222
|
})
|
|
157
223
|
```
|
|
158
224
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
225
|
+
Checking alone misses a stop during a long `await`. Listening alone waits for
|
|
226
|
+
ever on a stop that came first. The runner gives the event loop one turn before
|
|
227
|
+
it calls the command, so a waiting stop is delivered first, on Node, on Bun and
|
|
228
|
+
in a binary.
|
|
229
|
+
|
|
230
|
+
### Finishing the current job
|
|
231
|
+
|
|
232
|
+
`deadline` is when the runner stops waiting, as a `Date.now()` time. It is
|
|
233
|
+
`undefined` until the command has been told to stop:
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
export default defineCommand(async (_args, context) => {
|
|
237
|
+
while (!context.signal.aborted) {
|
|
238
|
+
const job = await queue.next()
|
|
239
|
+
const budget = context.deadline ? context.deadline - Date.now() : Infinity
|
|
240
|
+
|
|
241
|
+
await job.run({ timeout: budget })
|
|
242
|
+
}
|
|
243
|
+
})
|
|
244
|
+
```
|
|
163
245
|
|
|
164
246
|
<Aside type="caution" title="Do not destructure deadline">
|
|
165
247
|
`deadline` is a getter, because what it answers changes. Destructuring it,
|
|
@@ -168,152 +250,121 @@ current job reads `context.deadline - Date.now()` instead of working out
|
|
|
168
250
|
moment you need it. `signal` is a plain property and can be destructured.
|
|
169
251
|
</Aside>
|
|
170
252
|
|
|
171
|
-
The process
|
|
172
|
-
|
|
173
|
-
Bun.
|
|
253
|
+
The process stays alive for as long as the command runs, whether or not it holds
|
|
254
|
+
a socket, on Node as on Bun.
|
|
174
255
|
|
|
175
|
-
##
|
|
256
|
+
## Running on a schedule
|
|
176
257
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
a stop is the operating system's, and ends the process with `143`.
|
|
258
|
+
A scheduler is a worker that runs whatever is due every minute, the way
|
|
259
|
+
Laravel's `schedule:work` does:
|
|
180
260
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
first line of any, so a stop that arrives while a large app is being parsed
|
|
185
|
-
(a production app measured about 170 ms, for 580 modules and 119 MB) ends the
|
|
186
|
-
process.
|
|
187
|
-
|
|
188
|
-
`--splitting` makes the entry its own chunk, which runs first and loads the rest
|
|
189
|
-
after it. The scaffold's `compile` script has it:
|
|
190
|
-
|
|
191
|
-
```sh
|
|
192
|
-
bun build --compile --splitting .output/server/compile.mjs --outfile dist/app
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
An app made before it was added keeps its old script until you add the flag. An
|
|
196
|
-
app with no `src/commands` has nothing to split, so the flag changes nothing
|
|
197
|
-
there until it gets one. `dist/app` is still the only file the binary needs. A Bun that does not accept the pair fails the compile
|
|
198
|
-
loudly: 1.2 says `cannot use --compile with --splitting`.
|
|
199
|
-
|
|
200
|
-
<Aside type="note" title="--sourcemap writes files beside the binary">
|
|
201
|
-
With `--compile`, `--sourcemap` embeds the map in the binary, and Bun also
|
|
202
|
-
writes `.map` files next to the output: one for a plain compile, and one per
|
|
203
|
-
chunk with `--splitting` (46 for the example, about 224 for a large app, and a
|
|
204
|
-
`dist` that grew to 201 MB). The binary does not read them. A stack trace is
|
|
205
|
-
the same with them deleted, so ship the binary alone and remove them
|
|
206
|
-
(`rm -f dist/*.map`), or leave `--sourcemap` off, which is what the scaffold
|
|
207
|
-
does. `--sourcemap=inline` does not prevent them under `--compile`. What
|
|
208
|
-
a trace shows either way is in [reading a stack trace](/hosts/where-it-runs/#reading-a-stack-trace).
|
|
209
|
-
</Aside>
|
|
210
|
-
|
|
211
|
-
The same stop, 250 ms into starting a binary of 80 MB, exits `143` without it
|
|
212
|
-
and `0` with it, and `./app --help` answers without loading the app.
|
|
213
|
-
Nothing else about the binary changes: the server, the frozen pages and the
|
|
214
|
-
commands run from it as they do without.
|
|
215
|
-
|
|
216
|
-
## Asking needs no environment
|
|
217
|
-
|
|
218
|
-
`./app --help` and a name that is not a command are answered before the app is
|
|
219
|
-
loaded, from a list the build wrote into the entry. A build machine, or a CI step
|
|
220
|
-
that has no database url, can list the commands. A real command loads the app,
|
|
221
|
-
and so validates the environment, and fails there with the schema's own message.
|
|
261
|
+
```ts title="src/commands/schedule.ts"
|
|
262
|
+
import { setTimeout as sleep } from 'node:timers/promises'
|
|
263
|
+
import { defineCommand } from '@rsc-kit/core/commands'
|
|
222
264
|
|
|
223
|
-
|
|
265
|
+
export const register = true
|
|
224
266
|
|
|
225
|
-
|
|
267
|
+
export default defineCommand(async (_args, { signal }) => {
|
|
268
|
+
while (!signal.aborted) {
|
|
269
|
+
await runDueJobs()
|
|
226
270
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
CMD []
|
|
271
|
+
// Wakes at once when told to stop, instead of finishing the minute.
|
|
272
|
+
await sleep(60_000, undefined, { signal }).catch(() => {})
|
|
273
|
+
}
|
|
274
|
+
})
|
|
232
275
|
```
|
|
233
276
|
|
|
234
277
|
```sh
|
|
235
|
-
|
|
236
|
-
docker run --rm myapp migrate # a release step
|
|
237
|
-
docker run --rm myapp work # a worker
|
|
278
|
+
./app schedule
|
|
238
279
|
```
|
|
239
280
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
281
|
+
Run **one** of it. Two would run every job twice, which is also why the web
|
|
282
|
+
server is not the place for a schedule: it runs as many copies as you scale it
|
|
283
|
+
to.
|
|
284
|
+
|
|
285
|
+
A job that runs once a day doesn't need a process waiting for it. Let the
|
|
286
|
+
platform's cron run an ordinary command, such as `./app cleanup`, and let it
|
|
287
|
+
exit.
|
|
244
288
|
|
|
245
|
-
|
|
289
|
+
## While developing
|
|
290
|
+
|
|
291
|
+
`vite dev` has no build to run a command from. Run the file instead:
|
|
246
292
|
|
|
247
293
|
```sh
|
|
248
|
-
|
|
249
|
-
bun .output/server/commands.mjs migrate
|
|
294
|
+
bun src/commands/migrate.ts
|
|
250
295
|
```
|
|
251
296
|
|
|
252
|
-
|
|
253
|
-
`index.mjs` is the server, and it does not read its arguments:
|
|
254
|
-
`node .output/server/index.mjs migrate` starts the server and migrates
|
|
255
|
-
nothing. `commands.mjs` is the entry for a command. The binary has both
|
|
256
|
-
behind one file because it knows from `argv` which was meant.
|
|
257
|
-
</Aside>
|
|
297
|
+
Bun runs TypeScript and follows the app's `tsconfig` paths.
|
|
258
298
|
|
|
259
|
-
|
|
260
|
-
wants to know before running one.
|
|
299
|
+
## Where commands aren't built
|
|
261
300
|
|
|
262
|
-
|
|
301
|
+
Commands are built for the `bun` and `node` presets, whose output is a process.
|
|
263
302
|
|
|
264
|
-
- **`vite dev`.** There is no build to run one from. While developing, run the
|
|
265
|
-
file: `bun src/commands/migrate.ts` (Bun runs TypeScript, and the app's
|
|
266
|
-
`tsconfig` paths).
|
|
267
303
|
- **A Worker.** The bundle targets workerd, where a database driver or
|
|
268
|
-
`node:child_process` is a different build of the package or not there at all
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
`bun` and `node` presets, whose output is a process. Anywhere else the build
|
|
274
|
-
says so and leaves `src/commands` out; run the file from CI.
|
|
304
|
+
`node:child_process` is a different build of the package or not there at all.
|
|
305
|
+
The build leaves `src/commands` out and says so. Run the file in CI before the
|
|
306
|
+
deploy, or use `wrangler` for D1.
|
|
307
|
+
- **Any other preset** (Vercel, Netlify, Deno): the build says so and leaves
|
|
308
|
+
`src/commands` out. Run the file from CI.
|
|
275
309
|
|
|
276
|
-
##
|
|
310
|
+
## Common mistakes
|
|
277
311
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
stops when it is told to:
|
|
312
|
+
**`process.exit()` inside a command skips `shutdown()`.** Return the code
|
|
313
|
+
instead. The runner exits for you, after closing what `register()` opened.
|
|
281
314
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
315
|
+
**A package with an import side effect runs it for the server too, if a page
|
|
316
|
+
imports it.** The command's own module runs only when it is named. What it
|
|
317
|
+
imports is the bundle's, shared with the pages.
|
|
285
318
|
|
|
286
|
-
|
|
319
|
+
**A command is not a route.** It isn't prerendered and has no request, so
|
|
320
|
+
`headers()` and `cookies()` have nothing to read.
|
|
287
321
|
|
|
288
|
-
|
|
289
|
-
while (!signal.aborted) {
|
|
290
|
-
await runDueJobs()
|
|
322
|
+
## How it works
|
|
291
323
|
|
|
292
|
-
|
|
293
|
-
await sleep(60_000, undefined, { signal }).catch(() => {})
|
|
294
|
-
}
|
|
295
|
-
})
|
|
296
|
-
```
|
|
324
|
+
### Listing needs no environment
|
|
297
325
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
326
|
+
`./app --help` and a name that isn't a command are answered before the app
|
|
327
|
+
loads, from a list the build wrote into the entry. A build machine or a CI step
|
|
328
|
+
with no database URL can list the commands. A real command loads the app,
|
|
329
|
+
validates the environment, and fails there with the schema's own message.
|
|
330
|
+
|
|
331
|
+
### Where a stop is held from
|
|
332
|
+
|
|
333
|
+
The entry holds `SIGTERM` and `SIGINT` from its first line, however long the app
|
|
334
|
+
takes to load, and hands them to the command. Before that line, a stop is the
|
|
335
|
+
operating system's and ends the process with `143`.
|
|
301
336
|
|
|
302
|
-
|
|
303
|
-
|
|
337
|
+
In a plain build that window is tiny: `commands.mjs` is a few lines and loads
|
|
338
|
+
the app after it holds the signals. A compiled binary has one more step. Bun
|
|
339
|
+
parses every module in the file before running the first line of any, so a stop
|
|
340
|
+
while a large app is parsed (about 170 ms for a production app of 580 modules
|
|
341
|
+
and 119 MB) ends the process.
|
|
304
342
|
|
|
305
|
-
|
|
306
|
-
|
|
343
|
+
`--splitting` makes the entry its own chunk, which runs first and loads the rest
|
|
344
|
+
after. The scaffold's `compile` script has it:
|
|
307
345
|
|
|
308
|
-
|
|
346
|
+
```sh
|
|
347
|
+
bun build --compile --splitting .output/server/compile.mjs --outfile dist/app
|
|
348
|
+
```
|
|
309
349
|
|
|
310
|
-
|
|
311
|
-
|
|
350
|
+
- An app made before the flag was added keeps its old script until you add it.
|
|
351
|
+
- An app with no `src/commands` has nothing to split, so the flag changes
|
|
352
|
+
nothing there.
|
|
353
|
+
- `dist/app` is still the only file the binary needs.
|
|
354
|
+
- A Bun that doesn't accept the pair fails the compile loudly. 1.2 says
|
|
355
|
+
`cannot use --compile with --splitting`.
|
|
312
356
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
the bundle's, shared with the pages.
|
|
357
|
+
The same stop, 250 ms into starting an 80 MB binary, exits `143` without the
|
|
358
|
+
flag and `0` with it, and `./app --help` answers without loading the app.
|
|
359
|
+
Nothing else about the binary changes.
|
|
317
360
|
|
|
318
|
-
|
|
319
|
-
|
|
361
|
+
<Aside type="note" title="--sourcemap writes files beside the binary">
|
|
362
|
+
With `--compile`, `--sourcemap` embeds the map in the binary, and Bun also
|
|
363
|
+
writes `.map` files next to the output: one for a plain compile, and one per
|
|
364
|
+
chunk with `--splitting` (46 for the example, about 224 for a large app, and a
|
|
365
|
+
`dist` that grew to 201 MB). The binary does not read them. A stack trace is
|
|
366
|
+
the same with them deleted, so ship the binary alone and remove them
|
|
367
|
+
(`rm -f dist/*.map`), or leave `--sourcemap` off, which is what the scaffold
|
|
368
|
+
does. `--sourcemap=inline` does not prevent them under `--compile`. What
|
|
369
|
+
a trace shows either way is in [reading a stack trace](/hosts/where-it-runs/#reading-a-stack-trace).
|
|
370
|
+
</Aside>
|