shtukbasehub

Live server

A small Bun server that makes a project's HTML interactive: lenses and your own pages read and change records (fields, text, phases along a contour), create records, run allowed actions and update themselves. Only on this computer, writing needs a session token.

Packagelivev0.1.020 files

Live server

The package makes a project's HTML live. shtuk run live serve starts a server on this computer: it serves the project folder like an ordinary static server and gives an API to read and change records, run allowed actions and learn about file changes. Lenses (the lenses package 0.3.1+) notice the server themselves and turn on editing; from a static server or from disk they, as before, only show.

Why "live": that is what we call something that changes: a live lens, a live page. The words "server" and "app" are too general, and "api" is only half of the package.

What lenses can do on the live server

  • In an open record: fields by the collection schema: a choice and a phase as a list, text, a number, a date, a checkbox as an input field; the "Edit text" button; "To trash" (a move to deleted/).
  • On a card: a click on a choice or phase chip opens a list of values.
  • A board by a choice or phase field: a card can be dragged to another column. The phase columns are all the phases of the contour in order.
  • A "Create record" button on a collection lens and on a collection page.
  • A schema or contour error (response 422) is shown at the field or on the card, in English.
  • A second open tab updates itself through /api/events.

API

The address is http://127.0.0.1:<port>. Responses are JSON. Reading needs no token. Writing, moving, deleting and running need a token: a cookie from the server's link or the header Authorization: Bearer <token>. The body of write requests is JSON with Content-Type: application/json.

MethodPathWhat it doesExample
GET/api/helloA mark of a live server: the version, whether writing is possible (write), the allow list (run)curl 127.0.0.1:4322/api/hello
GET/api/collectionsThe project's collections: path, title, record type, schema, how many recordscurl 127.0.0.1:4322/api/collections
GET/api/records?collection=Records of a collection: path, title, fields, etag; &body=1 adds the textcurl '127.0.0.1:4322/api/records?collection=/work/tasks'
GET/api/record?path=One record: fields, text, etagcurl '127.0.0.1:4322/api/record?path=/work/tasks/x.md'
GET/api/search?q=Search by title and text of records; &collection=, &limit=curl '127.0.0.1:4322/api/search?q=lenses'
GET/api/eventsAn SSE stream: a change event with paths after files change and lenses are rebuiltcurl -N 127.0.0.1:4322/api/events
POST/api/recordsCreate a record in a collection: {collection, fields, body?, name?}. The file name comes from the title; the phase is the contour entry. Response 201curl -X POST -H "Authorization: Bearer $T" -H 'Content-Type: application/json' -d '{"collection":"/work/tasks","fields":{"title":"New"}}' 127.0.0.1:4322/api/records
PATCH/api/recordChange fields and/or text: {path, set?: {field: value}, body?, etag?}; null removes a field; a mismatched etag gives 409curl -X PATCH … -d '{"path":"/work/tasks/x.md","set":{"status":"doing"}}' 127.0.0.1:4322/api/record
POST/api/moveMove a record to another collection: {path, to}curl -X POST … -d '{"path":"/work/tasks/x.md","to":"/work/owner-tasks"}' 127.0.0.1:4322/api/move
DELETE/api/record?path=Move a record to deleted/<the same path> (not for good)curl -X DELETE -H "Authorization: Bearer $T" '127.0.0.1:4322/api/record?path=/work/tasks/x.md'
POST/api/runRun an action from the allow list: {package, action, args?, stream?}. The response is {code, ok, steps, output}; stream: true gives NDJSON lines as it goes (start, out, end, exit)curl -X POST … -d '{"package":"shtuk","action":"check"}' 127.0.0.1:4322/api/run

Errors are {error} with a code: 400 means the request was not understood, 401 no token, 403 not allowed (a foreign page, not a collection record, an action not from the list), 404 no such thing, 409 the record already exists or changed, 422 schema or contour: {error, field, errors: [{field, message}]}.

How the server writes

  • Only collection records: .md in a collection folder. Settings, lenses, lib/ and .shtukbase/ are not changed through the API.
  • Atomically: into a temporary file next to it, then a rename.
  • It edits the header in place: it changes only the lines of changed fields; field order, quotes and comments stay. A new field goes at the end of the header. A value is written so that it reads back as the same; if not, 422.
  • It checks what changes against the collection schema (properties of its page) with the rules of the collections package: title, required fields and requiredWhen, choice options, a date, a number, a checkbox, a link to an existing record of the right collection.
  • A phase goes by the contour (the contours package): only a phase of the contour; a move is one of the next exits of the previous phase or a phase with fromAnywhere: true; an exit with needs requires filled fields. A new record starts at the contour entry (entry).

Your own interactive page

shtuk run live app [path.html] places the example templates/tasks-app.html (by default in work/tasks-app.html): a task board with the buttons "Take", "Done" and "Check project" (shtuk check from the allow list). It is ordinary HTML without a build: fetch('/api/records?collection=/work/tasks'), fetch('/api/record', {method: 'PATCH', …}), new EventSource('/api/events'). Copy and change it. The page works only from the live server: from disk it has no API.

Settings and norms

The settings are the header of the page /base/live.md in the project: port, run (the allow list, empty by default), host (only this computer). Three norms, each with an enforcer that is a package check and a test:

  • LIVE-R01: the server listens only on 127.0.0.1 (the host check).
  • LIVE-R02: writing only with a session token (the token check).
  • LIVE-R03: actions only from the allow list (the run-list check).

The test (shtuk run live selftest, the file test/live.test.ts) starts the server on a temporary project and checks all this live.

Where the boundary is

  • Here are the server, its API, the norms and an example page. Editing in lenses is in the kit of the lenses package.
  • The Shtukbase node (the application) writes records with its own methods; it does not need this server and does not depend on it: only Bun, no npm.
  • The server is not for a network and not for several people: one person, one computer.