Skip to main content

Dependencies

A document can depend on other documents, packages, and data, wherever they live. It says so with one pinned line, and the host loads it after parsing, the same way in every host, the browser included:

#[load] fn intent() { await import('https://github.com/formata/intent/releases/download/v1.4.0/intent.pkg', '9f2c...', '@formata/intent@1.4.0'); }
#[load] fn rates() { await import('https://example.com/rates.json', '4b1e...', 'root.deps.rates'); }

Parsing never touches the network. Loading does: the host calls load(), which runs the document's #[load] functions.

Loading​

HostCall
JavaScriptawait doc.load() (or doc.load('deps') for one object)
Pythondoc.load()
Rustgraph.load(None)
Stofawait load(), or await load(self.deps)
CLIstof run and stof test load before they run anything (--no-load skips it)

Each returns how many #[load] functions ran. Loading again is safe: what's already imported is skipped without fetching. A failing #[load] function fails the load, and runs again on the next one.

import(url, sha256, into)​

import fetches a file, checks it, and parses it into the document. It's async like Http.fetch, so await it in a #[load] function (an import that isn't awaited fails on its own, not the load):

await import(url, sha256, into, format = null, profile = 'prod', headers = null) // true, or false if it's there already
  • Pinned: the bytes must have the sha256 you give (the CLI's stof hash <url> prints it), or nothing is parsed. Pin a URL that never changes, like a release's /releases/download/v1.4.0/..., never /releases/latest/....
  • The host's network rules: it uses the document's own Http.fetch, so it reaches only what the host allows, and in JavaScript it's the host's fetch. GitHub release files redirect to release-assets.githubusercontent.com, so a host list that allows github.com needs that host too.
  • Once: the import is recorded on the object it went into (imported() lists them), so the same file there is never fetched twice, and the record travels with the document.
  • into: a path (created if it's missing) or an object.
  • Private files: headers go with the request, Ex. headers = { 'Authorization': 'Bearer ' + env('GITHUB_TOKEN') }.

Any File​

A dependency can be any file: Stof, a package (.pkg) or binary Stof (.bstf), JSON, YAML, TOML, CSV, text, an image, a PDF. The format is format when you give one; else the URL's extension; else the response's content type; else text, or bytes when it isn't text; else Stof (no extension). It lands like an import statement's file: a CSV in a text field, an image in an image field.

Packages and Versions​

An into starting with @ is a package: it's parsed into the root, so its functions and types are there for everything. With file access (the CLI), it's also written to stof/, so the next run reads it from there.

#[load] fn intent() { await import('https://github.com/formata/intent/releases/download/v1.4.0/intent.pkg', '9f2c...', '@formata/intent@1.4.0'); }
  • @formata/intent@1.4.0 means 1.4.0 or a newer compatible version: the same major version (for 0.x, the same minor version).
  • One version per document: when two packages need different versions of a third, the highest one loads, whatever order they run in. Versions that can't share (Ex. 1.x and 2.x) are an error that names both.
  • A package brings its own dependencies: the #[load] functions in its pkg.stof come with it, and run when it loads.
  • A .pkg must be the version its pkg.stof says.

With the CLI​

The stof CLI writes and manages these lines in your package's pkg.stof:

stof init # a package: pkg.stof and main.stof
stof add https://github.com/formata/intent/releases/download/v1.4.0/intent.pkg
stof run # installs and loads, then runs #[main]
stof outdated # newer releases of your dependencies
stof update # move to the newest compatible ones

stof install follows the dependencies of your dependencies (without running any of their code), pins them in pkg.stof marked // indirect, and writes everything to stof/. Fetched files are cached, so runs work offline once they've run. stof add --embed puts the line in the document instead, so it travels with the document to wherever it's loaded.

Trust​

Untrusted source can't have #[load] functions (like #[init]), and code in a sealed part runs only its own part's. A dependency's code is code you trust: you pinned it.