JSOC
elements man jsocJSOC is the configuration language. It is JSON with comments, unquoted keys,
trailing commas, and one function, env(), which reads an environment variable
while the file compiles. A .jsoc file is a module: import it like any other
file and the compiler evaluates it at build time, type checks every read of it,
and emits plain JSON. config.jsoc at the project root is a .jsoc file, and
it is the file Elements reads its own settings from; elements man config
covers the settings. This page covers the JSOC language.
Syntax
Syntax{ // line comment /* block comment */ name: "my_app", // unquoted key "retry-count": 3, // quoted key when the name is not an identifier tags: ["a", "b",], // trailing commas anywhere debug: false, nothing: null, port: env<number>("PORT", 4000), }
A value is a string, a number, true, false, null, an object, an array, or
an env() call. Strings take single or double quotes. To compute a value,
import the file and compute it in TypeScript. JSOC holds only literal values
and env(), so every value is known when the file compiles and emits as plain
JSON.
A key that appears twice takes the last value, as JSON parsers do. The one
exception is the target rule below, where a server entry and a @browser entry
for the same key are two different values on purpose.
The env() Function
env() is the only call. It reads one named variable when the file is
evaluated and writes the value into the emitted JSON. Nothing reads the
environment at runtime.
env() formsenv("KEY") // string | undefined: unset leaves it empty env("KEY", "fallback") // string: the fallback when unset env!("KEY") // string: a build error when unset env<number>("PORT", 4000) // number: parsed, a build error when it does not parse env<boolean>!("LIVE") // boolean, required
The type argument is string, number, or boolean, and nothing else. A
default must have that type. Without a type argument, the value has the type of
the default. With no type argument and no default, the value is a string.
Numbers parse as decimals. Booleans read false, no, n, 0, and f as
false, in any case, and everything else as true, including an empty value, so
LIVE= in an env file turns env<boolean>("LIVE", false) on. A value that
does not parse as the requested type is a build error at the call, with the
variable name and the text it held.
env() reads a variable from the process environment, then from
config/env/<environment>.env, whose value overrides the process
environment's. elements man config covers the env files.
Types
An import of a .jsoc file is typed from its contents. Literals widen to their
primitive: "dark" is string, 3 is number, true is boolean. Arrays
are arrays of their element type, objects are object types with exactly the
keys written, and env() calls have the types listed above. Reading a key the
file does not have is a compile error, and so is using a value as the wrong
type.
app/shared/settings.jsoc{ theme: "dark", retries: env<number>("RETRIES", 3), label: env("LABEL"), }
Typed readsimport settings from "#app/shared/settings.jsoc"; let theme: string = settings.theme; let retries: number = settings.retries; let label: string = settings.label ?? "untitled"; // string | undefined without the ?? settings.colour; // error: no such key
config.jsoc is also checked against the keys and value types Elements
expects: an unknown
compiler option, a bad session.expires, or a deploy block with a missing field
is a build diagnostic on the line that has it.
Browser and Server
A .jsoc file is evaluated once for each target. Every key is server-only
unless it is tagged, so a secret written next to a public value is left out
of the browser's copy of the file. A /** @browser */ tag on a
property makes that property and everything under it visible to the browser as
well.
Browser tag{ /** @browser */ public: { appName: "Acme", features: { signups: true }, }, databaseUrl: env!("DATABASE_URL"), }
The server's copy of the file holds every key. The browser's copy holds
public and the keys under it, and has no databaseUrl. An object is in
the browser's copy when any key under it is tagged, and an untagged key
beside it is left out. The same key may appear twice, once plain and once
tagged; the server takes the plain entry and the browser takes the tagged one,
whichever order they are written in.
To read a value in the browser, tag it. The browser module holds only tagged
keys, so secrets stay on the server. Reading an untagged key in browser code is
a build error at the read, and the error names the key. The import itself is
fine everywhere, and server code reads every key: a route, a job, the body of
an @rpc function, and a helper that only @rpc bodies call, wherever the
helper lives. The read is an error only in code that runs in the browser: an
event handler, a template expression, or a function they call that is not
an @rpc function. Reading a container such as config.stripe in the browser is
fine when a key under it is tagged; its browser copy holds just those keys.
Importing
Importsimport config from "#config"; // config.jsoc, through the import map import settings from "#app/shared/settings.jsoc"; // any other .jsoc file
The .jsoc extension is written out; it is not searched for the way .ts is.
The same import works in a .ts file, in an .ehtml template, on the server, and
in the browser, and the server loads a module with every key while the browser
loads a module with only the tagged keys. The module holds the evaluated
values. There is no parsing at runtime and no environment
read; config.database.host is a string that was decided when the build ran.
Hot Reload
A .jsoc file is a source like any other. Save it, or save an env file whose
variable it reads, and the build server evaluates it again, emits the module
again, rebuilds everything that imports it, and hot reloads the running program
and the open browsers. A process.env read does not do this: a value read
through env() in a .jsoc file changes in the running app when you edit
the env file, and a value read from process.env is fixed when the process
starts. Keep
configuration in .jsoc; use process.env only for a value that has to
stay out of the build.
Related
config: whatconfig.jsocholds, the environments, and the env files.typescript: the compiler that checks.jsocalongside.tsand.ehtml.build: the build loop that evaluates and emits.jsocmodules on save.