Concepts
Changing a site
A change is a request: a list of operations, applied to a known revision of a site. Nothing here changes a document in place. Every operation takes a document and returns a new one, or a refusal.
Operations
There are 37 operations. Each has typed arguments, and catalogue lists them with an example of each.
Group
Operations
Group
Nodes (12)
Operations
add, remove, move, setProps, setText, setStyle, setHidden, setSlot, setRequired, bindData, setStates, setMotion
Group
Blocks (3)
Operations
insertBlock, setBlockSetting, updateBlock
Group
Pieces (4)
Operations
makeComponent, setInstance, detach, removeComponent
Group
Site (8)
Operations
addPage, removePage, setPage, movePage, setSite, setLook, setSiteStyles, setFonts
Group
Effects (1)
Operations
addCustomEffect
Group
Live (6)
Operations
setBackends, setOrigins, setValues, setLiveSources, setLive, setGrants
Group
Tools (3)
Operations
setAgents, setTool, setActs (app/actions.md)
A node is addressed by a tree and an id: { tree: 'header' | 'footer' | { page: id } | { component: id }, id }. A component's tree has its own ids, apart from the pages. add fills in an id (the lowest unused n id), a slot and a name when the node leaves them out. detach turns an instance back into its own nodes: the component's own parts get new ids, while the root and the content a person placed in the instance's slots keep theirs. It is refused with RESOURCE_LIMIT_EXCEEDED when the instance, filled in, holds more than 2,000 elements, because the copy would be cut. updateBlock brings an inserted block to the registered version of its block and lists, as a note, the parts a person had changed. The block's root keeps its slot, placement, hiding, show conditions, lock and name.
A refusal is a list of problems. Each has a stable code, a message, and where they apply the nodeId, the path of the field, the allowed values and the rule. A problem from a batch also has op, the index of the operation that raised it. For a problem that a validation found at the end, op is the last operation that changed that node, and a problem that names no node, or names a node that no operation changed (a required text hidden by an ancestor that the batch hid, for example), has no op. A refusal about a page has the path page and no nodeId, because a page id is not a node id. A refusal about a node inside the node an add sends has the path to it, such as node.children.2.id. INVALID_OPERATION means the operation itself is wrong (an unknown id, a value of the wrong type, an unsafe key, an unknown operation name, with the allowed names listed). UNKNOWN_BLOCK, POLICY_VIOLATION and the other codes of validation mean the same as in validation.
The change contract
{ site: 'main', baseRevision: 0, key: 'first-change', ops: [ /* up to 200 operations */ ] }baseRevision is the revision the request was made from. If the site has moved on, the request is refused with REVISION_CONFLICT and nothing is written.
key is the request key, 1 to 200 characters. The same key with the same content returns the first result and applies once. The same key with other content is refused with KEY_CONFLICT. A refused batch does not use its key up: it is planned again if sent again.
The batch is all or nothing. The operations run in order on a copy, and a later one can use what an earlier one created. The first refusal stops the batch.
The document is validated once, after the last operation. A batch whose result has any problem is refused whole, and the problems come back.
A request holds at most 200 operations and 1,000,000 characters of canonical JSON. Past either, it is refused with RESOURCE_LIMIT_EXCEEDED.
plan(doc, registry, request, allow?) runs the batch on a document and returns the result without a store. Before it checks, an alias pass rewrites align on a text to textAlign, a layer's display to hidden, and a rect shape's style radius to geometry.radius. apply(store, registry, request, allow?) does the same against a store, in four steps: replay the key, check the revision, plan, commit. Both return problems and never throw for a bad request. A store that throws makes apply reject with that error.
A successful result has created and changed (node references, with no repeats), pages (the ids of the pages affected), notes, and from apply the new revision. created and changed are what diffSites reports between the document before and after the batch: created is every id the diff lists as added, changed every id it lists as changed or moved that is not created. A node that was removed is in neither list (the diff lists it as removed), and a parent whose children changed is not changed. A page's own settings are in pages, not in changed. A note has a kind (reset, expanded or info), the node ids it is about and a text. One note says which links now lead to no page after a page was removed, switched off or given another slug. Culm does not rewrite links.
allow is a function a product passes to refuse an operation for its own reasons. It is called once for each operation, with the operation and the working document, and returns a problem or nothing. The result is a POLICY_VIOLATION with the message, rule, nodeId and path it gave when they are text (and allowed when it is a list of texts), and nothing else of what it returned; with the message "Not allowed." if it gave none, and with the rule allow if the function threw.
The store
A store is the interface the contract writes through:
Method
Does
Method
head(site)
Does
The latest { revision, doc }, or problems if there is no such site
Method
get(site, revision)
Does
The document of one revision, or undefined
Method
replay(site, key)
Does
The request and result recorded under a key, or undefined
Method
commit(site, { expected, doc, key, request, result })
Does
Writes revision expected + 1 if the head is expected, as one step. Otherwise it returns REVISION_CONFLICT, KEY_CONFLICT or SOURCE_CONFLICT
memoryStore({ main: doc }) is a store in memory. It keeps every revision with its key, request and result, and copies in and out, so a document it returned cannot be used to change it. A store for files or a database follows the same four methods. The folder store is part of checkpoint 2b.
Reads
An agent reads a site without receiving the whole document. The four reads take a head (what store.head returns), never change it, and never throw.
Read
Returns
Read
siteOutline(head)
Returns
The site name, languages, text range, counts, style and font names, then one row per page and component
Read
pageOutline(head, tree, { depth, from })
Returns
One row per node: id, kind, parent, index, layouts it is hidden on, block of origin, required, a preview of its text
Read
readNode(head, tree, id, options?, registry?)
Returns
One node without its children, the ids of the children and, for a block root, the current settings (only when the registry is passed)
Read
search(head, { text, kind, block })
Returns
The nodes that match every condition given, with a preview
A reply is at most READ_CAP (16,000) characters of JSON. A long string is cut and its path listed, and a node too large to show whole is shown with its largest fields replaced by their length. When a reply cannot hold every row it has cut: true and a cursor such as "3:120:1k9x" (the revision, the offset and a short tag of the read and the options that select its rows); pass it back in options.cursor (or query.cursor) to get the next rows. readNode adds the run and the character, and the node's tree and id to its tag. A cursor made for another revision is refused with REVISION_CONFLICT, so a page never mixes two versions of a site. A cursor made by another read, or by the same read with other options (another depth, from, tree, query, what or id), is refused with INVALID_OPERATION at cursor. The tag guards against mix-ups; it is not a proof, and for diffSites it is made from a glance at each document (its schema version, page count and node count), which tells an obviously different pair apart.
The catalogue
catalogue(registry, { what: 'kinds' }) // compact rows, paged
catalogue(registry, { what: 'kinds', id: 'text' }) // one entry in fullwhat is guide, kinds, operations, recipes, blocks, sources or effects. It is generated from the registry, so a plug-in's kinds, blocks, sources and effects appear with the core ones. The list views are terse: guide is rows of plain sentences on composing a section; kinds is one string per kind (kind(role): setting, setting); operations is one string per operation with a legend for the long types; recipes is rows { recipe, name, guide, ops }; a block is { block, name, category, looks, guide }; a source has its fields; an effect has its name and minimum tier. A view with an id returns the one entry in full: a kind's settings with their hint (the grammar of a shape's geometry, fill and stroke), an operation's fields and example, a recipe's batch (its first operation aimed at the first page of the site, which plan accepts as it stands), a block's settings with a ready insert (pass site to name the first page of the site), a source's first sample row or an effect's numeric settings. Effect detail includes a setMotion example for a compatible site node, or describes the required target. An unknown
id is refused with the code of its kind and the allowed names; an unknown recipe or effect is INVALID_OPERATION at id.
GUIDE and RECIPES are core data, like OPERATIONS: six recipes (hero, split, cta-band, buttons, divider, card) that use only site colours and leave node ids out so one can be applied twice. The five list replies guide, kinds, operations, recipes and blocks stay within 11,500 characters on the tests' fixture registry (9,273). Last measured on the former starter registry, and not again since: 11,007 for a site that declares tools, which also lists the three tool operations; 11,449 with live data, and 11,971 with both, which is over the figure. The live operations and the island kind are listed only where a site or registry uses them.
Diff and restore
diffSites(a, b)diffSites compares two documents and returns, for each tree with a difference, the ids that were added, removed and moved, and the changed ones with the paths of their changed fields. site lists the site-level keys that differ (pages.<id>.inMenu, components.<id>, pages for page order and so on), and pages lists the affected pages: the page whose own tree changed, every page that is on for a changed header or footer, every page for a changed look or style, and the pages that use a changed component, also through a nested one. Its replies are paged like the reads. It never throws: a malformed document or options of the wrong shape give a refusal or an empty diff, and a tree nested past 64 levels is compared level by level down to that depth, and below it as one value.
restore(store, registry, { site: 'main', to: 0, baseRevision: 3, key: 'undo-1' }, allow?)restore commits the document of revision to as a new revision. The history is kept, and the new document equals the old one. It follows the same contract as apply (a key of 1 to 200 characters, the revision), migrates an older schema, validates the document against the current registry, and calls allow once with { op: 'restore', to }. A revision the store does not hold, or one above baseRevision, is refused with UNSUPPORTED_CAPABILITY. Restoring the head is allowed.
The build identity
buildIdentity(doc, data, registry, files?) returns one SHA-256 as 64 hex characters, over the document, the rows and values given to the compiler, the framework version, the name and version of every registered plug-in in name order, and, when given, the SHA-256 of each font or asset file, sorted by path and then by hash (so the same set of files in any order gives the same identity). What is hashed is the JSON form of the rows and values, so a value JSON cannot represent (a function, a Map, a Date against its ISO text, values: null against no values) does not tell two builds apart. The same inputs give the same identity, and a change to any one of them changes it. It is asynchronous, and it rejects with an Error if the rows or the document cannot be written as JSON. FRAMEWORK_VERSION is the version it uses.