<!--{"title": "service-compris", "pin": "top", "order": 0, "headTitle": "service-compris: a host from a JSON manifest", "description": "A JSON manifest becomes collections, schemas and access rules on a running host: Firebase, or one Bun process on SQLite."}-->

# <img src="https://raw.githubusercontent.com/tonioloewald/tosijs-platform/main/assets/service-compris.png" width="48" height="48" alt="" align="top"> service-compris

*Service compris* — service included. Also, **understood**, which is the reading that matters: this
is the part of a backend that **decides** things, small enough to read and pure enough to test
without a cloud.

```
npm install service-compris
```

> **0.4.0.** Two things live here. The **npm package**'s root import is the pure decision kernel:
> RBAC, a write pipeline, role resolution and file-storage decisions, with no I/O and no vendor
> (`service-compris/assets`, the asset manager, is the one part with a DOM). The **repository** is a
> host you can deploy onto a blank Firebase project, claim without anyone handing you a secret,
> install a library onto from a JSON manifest, store files in, and give an agent its own scoped identity. Two
> production hosts run it: tosijs-virta's and loewald.com. Start at **[BETA.md](BETA.md)** (the
> host walkthrough, including what is deliberately not ready yet). Upgrading a host from 0.3.0:
> see "Upgrading a host" in the [CHANGELOG](CHANGELOG.md)'s 0.4.0 entry. Orientation for agents:
> [llms.txt](llms.txt).

## What the npm package actually is

**The decision kernel of a backend, not a server.** It does not talk to a database, serve HTTP, or
authenticate anyone. Given a principal, a collection config, and a proposed write, it tells you what
should happen — and nothing else.

It is pure, dependency-injected, and has no vendor in it: the same code runs in a unit test, in a
Firebase function, or in front of Postgres, because it does no I/O at all. This repository's own
host runs its writes and access decisions through exactly this package.

```ts
import { runWritePipeline, getMethodAccess, ROLES, ALL, type CollectionMap } from 'service-compris'

const collections: CollectionMap = {
  note: {
    access: {
      [ROLES.public]: { read: ALL },
      [ROLES.author]: { write: ALL },
    },
  },
}

const author = { name: 'ada', contacts: [], roles: [ROLES.author], userIds: ['u1'] }

// An authorization decision — no I/O.
getMethodAccess(collections, 'note', 'PUT', author, false) // → ALL

// A write decision — no database, no ambient clock.
const outcome = await runWritePipeline(
  { method: 'POST', body: { title: 'hello' }, existing: null, config: collections.note, userRoles: author },
  { now: () => new Date().toISOString(), isUnique: async (field, value) => true }
)
// → { status: 'write', data: { title: 'hello', _created: …, _modified: …, _by: { uid: 'u1', name: 'ada' } } }
```

`outcome.status` is `'write'`, `'noop'`, or `'rejected'`. **The caller commits** — this library never
writes anything. Provenance (`_by`) is stamped from `userRoles`, never taken from the body.

## What it gives you

- **A write pipeline** — existence guards, `PUT`-replaces vs `PATCH`-merges, provenance stamping
  through an **injected clock**, envelope stripping, a no-op check (an unchanged body neither writes
  nor re-stamps), and uniqueness through an **injected privileged read**.
- **An access model** — grants across a collection's access map **joined as a lattice** (holding
  more roles never grants less, and key order is irrelevant), field-map straining, capability-token
  caveats that narrow every grant (methods use REST names, with `LIST` distinct from `GET`), and
  opaque denials so a protected resource does not confirm its own existence.
- **Roles** — the role vocabulary and the `UserRoles` shape.

Every dependency is injected, which is the whole point: the decisions are the part you want to be
able to reason about, and reasoning about them should not require standing anything up.

## Extensions

Content management is part of almost any web app, so its pieces ship from here as **installable
extensions** (D28): a manifest a host installs, plus components for the browser. They are separate
imports, so the root import stays pure and DOM-free.

- **`service-compris/assets`** — the asset manager. `<asset-manager>` lists, uploads, renames and
  deletes files and inserts them into an editor; `blobFiles()` connects it to a host's storage areas;
  `service-compris/assets/manifest.json` installs two of them (`assets:public`, `assets:private`).
  Needs `tosijs` and `tosijs-ui` (optional peer dependencies).

```ts
import { assetManager, blobFiles } from 'service-compris/assets'

document.body.append(
  assetManager({
    files: blobFiles({ serviceUrl: 'https://example.com/', getToken: (refresh) => user?.getIdToken(refresh) }),
    folders: ['assets:public', 'assets:private'],
  })
)
```

The blog follows the same way.

## What it does *not* give you

The root import has no storage, no HTTP, no auth, no realtime. Those belong to the deployable platform in this
repository (not published to npm): clone it and follow [BETA.md](BETA.md) to stand up a host. The
npm package is the piece of that backend that can be audited on its own.

## Stability

**0.2.x.** Settled, and changed within 0.2.x only to fix bugs: the exports, the `WriteOutcome`
shape and its rejection reasons, the lattice-join semantics of `getMethodAccess`, and the role
vocabulary. A new rejection reason ships in a minor release and is announced, because it breaks an
exhaustive `switch`. Still provisional, and each will change the API when it lands: `isWriteAllowed`
(the monotonicity property depends on it) and schema-valued field permissions.

**Upgrading from 0.1.0** changes authorization results: the access map is now a lattice join (0.1.0
let the last matching role win), token caveats deny, and `WriteOutcome` has new reasons. 0.1.0 also
could not be imported under Node ESM at all. The CHANGELOG's 0.2.0 entry lists every change.

One current limitation worth knowing: a `write` permission that is not `ALL` is **denied**, not
strained. The write path cannot yet enforce a field-level write restriction, and refusing is the
honest answer — silently dropping fields an author submitted is data loss wearing a permission's
clothes. Schema-valued write permissions replace this.

## Design

The reasoning behind this is written down rather than folklore — see [`DECISIONS.md`](DECISIONS.md),
a dated, append-only ledger, including the mistakes and retractions. [`PLATFORM.md`](PLATFORM.md) is
the wider vision this is the first piece of, and [`SOVEREIGN.md`](SOVEREIGN.md) is what it would take
to run the whole thing on a commodity server.

This repository also runs [loewald.com](https://loewald.com), which is where the code gets its
bruises.

## Licence

Code is [Apache-2.0](LICENSE). Documentation, images, icons and other content, including the code
samples in the docs, are [CC0 1.0](LICENSE-CONTENT): public domain, no attribution needed.
The exception is the logos (inconsequence, service-compris, tosijs): they are
[CC BY 4.0](LICENSE-LOGOS), so use them freely, but credit them. No licence here covers the names
or marks, so don't use them to pass something off as ours. Third-party material keeps its own
licence. What you make with it is yours.

Releases up to and including 0.3.0 were published under MIT and stay under it.
