The book’s style guide
A sample chapter on startup and the core queue, set in the book’s reading layout.
You start Pipal from its repository. Before the browser can open a chat, the host finds the built web files, opens the runtime, and starts listening for requests. The order gives you a way to read the startup code one piece at a time.
In this chapter
- Where to look for the browser build
- How the host connects the runtime to Hono
- What keeps two operations from taking the same turn in the core
From the build to the browser
The Hono host starts from the web buildThe static files that Hono serves to the browser, produced by building the Svelte client. in packages/web/build/. It reads pipal-base-path.txt there to learn the path at which to mount the client. When the runtime is ready, createApp receives both the runtime and the web root. main.ts
- Node host
- Core runtime
- Pi Durable
- Hono
Node host → Node host · Find the client
The host locates the built web files
Startup resolves
packages/web/build/and readspipal-base-path.txt.Node host → Core runtime · createRuntime
The host opens the core runtime
It passes the resolved data directory as
dataDir.Core runtime → Core runtime · Acquire store lock
The runtime takes the store lock
The runtime acquires the lock in
owner.sqlitebefore invoking the host’s initialization callback.Core runtime → Node host · Initialize
The host loads the profiles and workspace locations
The callback loads the profiles, assembles enabled extensions, and probes enabled workspace locations before the harness opens.
Core runtime → Pi Durable · Harness.open
The runtime opens Pi Durable
The SQLite storage passed to
Harness.openusesdurable.sqliteinsidedataDir.Node host → Hono · createApp
The host builds the Hono app
The app receives the runtime, web root, and base path.
Node host → Node host · server.listen
The server begins listening
Node’s request listener hands HTTP requests to
app.fetch.
Node host → Node host
The host locates the built web files
Startup resolves packages/web/build/ and reads pipal-base-path.txt.
Three places to look
packages/web/- Generated web assets
build/- The client’s mount path
pipal-base-path.txt
server/src/- The host startup entry point
main.ts
core/src/- The runtime opens Pi Durable here
index.ts
- Generated web assets
- The client’s mount path
pipal-base-path.txt
- The host startup entry point
main.ts
- The runtime opens Pi Durable here
index.ts
| Place | What you can find there |
|---|---|
packages/web/build/ |
The web files and their base-path record |
packages/server/src/main.ts |
The order in which the host starts |
packages/core/src/index.ts |
Runtime startup, from taking the lock to opening durable.sqlite |
One turn at a time
The core keeps a promise named line. When an operation takes a turn through exclusive, it joins the end of that line. index.ts
Deep diveHow the next turn starts after a failed operation
The caller receives result, which can still reject. The stored line uses a separate promise with a rejection handler. The next call can therefore run even if the caller before it received an error. index.ts
function exclusive<T>(operation: () => Promise<T>) { const result = line.then(operation); line = result.catch(() => {}); return result;}first operation → result for the first caller ↓second operation → result for the second callerWeb build
The web build is the set of static files Hono serves to the browser. The app points a chat URL at index.html and serves other web assets from the same directory. app.ts
Keep the directory for private data outside the directory the server publishes.
Follow the host’s startup in the snapshot.
- Find
webRootand read through the base-path check. - Follow
runtimeintocreateApp. - Stop at
server.listen.
The host’s reading list
- Start in
main.ts. - Follow the runtime into the core chapter.
- The serialized queue is one place to resume.
- Replace
the databasewith the name of the file you mean,durable.sqlite.
Open book search with ⌘K or Ctrl+K, or continue to the Hono server to follow the routes.
Recap
main.tslocates the web build before it opens the runtime.exclusivelets a caller see its own failure while the queue moves on.
- You start the host before building the Svelte client. What happens first?
- The host stops while resolving the web build.
- The runtime starts and serves an empty page.
- Hono builds the client on the first request.
Answer and explanation
The host stops while resolving the web build.
The host resolves the built web directory before it opens the runtime.
- One call to exclusive fails. What can the next call do?
- Wait for a server restart.
- Take its turn after the rejected call settles.
- Take over the first call’s promise.
Answer and explanation
Take its turn after the rejected call settles.
The queue keeps a promise with a rejection handler, so the next operation can run.
- A chat URL needs to load the client. Which file is served there?
- pipal-base-path.txt
- durable.sqlite
- index.html
Answer and explanation
index.html
Hono serves the built index.html for a chat URL. The Svelte client runs in that page.
You start the host before building the Svelte client. What happens first?
One call to exclusive fails. What can the next call do?
A chat URL needs to load the client. Which file is served there?