Writer’s reference

The book’s style guide

A sample chapter on startup and the core queue, set in the book’s reading layout.

3 min read

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

Svelte tabHono hostSQLite
The browser sends HTTP requests to the host. The host uses the runtime to work with the durable store.

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

Starting the host
  • Node host
  • Core runtime
  • Pi Durable
  • Hono
  1. Node host → Node host · Find the client

    The host locates the built web files

    Startup resolves packages/web/build/ and reads pipal-base-path.txt.

  2. Node host → Core runtime · createRuntime

    The host opens the core runtime

    It passes the resolved data directory as dataDir.

  3. Core runtime → Core runtime · Acquire store lock

    The runtime takes the store lock

    The runtime acquires the lock in owner.sqlite before invoking the host’s initialization callback.

  4. 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.

  5. Core runtime → Pi Durable · Harness.open

    The runtime opens Pi Durable

    The SQLite storage passed to Harness.open uses durable.sqlite inside dataDir.

  6. Node host → Hono · createApp

    The host builds the Hono app

    The app receives the runtime, web root, and base path.

  7. Node host → Node host · server.listen

    The server begins listening

    Node’s request listener hands HTTP requests to app.fetch.

Node host
Core runtime
Pi Durable
Hono

Node host → Node host

The host locates the built web files

Startup resolves packages/web/build/ and reads pipal-base-path.txt.

The host starts accepting HTTP requests after it opens the runtime and builds the Hono app.

Three places to look

Startup files in the checkout
  • packages/
    • web/
      • build/
        Generated web assets
        • pipal-base-path.txt
          The client’s mount path
    • server/
      • src/
        • main.ts
          The host startup entry point
    • core/
      • src/
        • index.ts
          The runtime opens Pi Durable here
      • Generated web assets
Follow main.ts for startup order. Open build/ to find the generated base-path record.
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
index.ts

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

packages/core/src/index.ts
function exclusive<T>(operation: () => Promise<T>) {
const result = line.then(operation);
line = result.catch(() => {});
return result;
}
Illustration: two calls to exclusive
first operation → result for the first caller
↓
second operation → result for the second caller
Each call hands its own result back to the caller and leaves a promise for the next call.

Web 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.

  1. Find webRoot and read through the base-path check.
  2. Follow runtime into createApp.
  3. Stop at server.listen.

The host’s reading list

The Pipal tree mark

  • Start in main.ts.
  • Follow the runtime into the core chapter.
    • The serialized queue is one place to resume.
  • Replace the database with 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.ts locates the web build before it opens the runtime.
  • exclusive lets a caller see its own failure while the queue moves on.
Check your reading
  1. 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.

  2. 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.

  3. 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.

  1. You start the host before building the Svelte client. What happens first?

  2. One call to exclusive fails. What can the next call do?

  3. A chat URL needs to load the client. Which file is served there?

Book contents

Search the book