JavaScript
Install the npm package, write conditions, overrides and invariants as JavaScript functions, generate permission types for the frontend and keep grants in a JSON file.
The npm package @the-hades-organization/strig runs the same engine on Node, Bun and in the browser. This page covers what is specific to JavaScript. Policies, decisions and governance work as they do in the other pages, with camelCase names. Every signature is in the reference.
import { load } from '@the-hades-organization/strig';
const { Engine, Context } = await load(); // native on Node and Bun, wasm in the browser
const engine = new Engine(spec); // the same EngineSpec JSON as in Rust
const ctx = new Context({ grants });
engine.check(ctx, { subject, permission: 'document.view', resource });
engine.apply({ actor, current, mutation: { add: grant }, invariants, audit: { level: 'standard' } });
Entry points#
Each subpath is its own import, so a browser bundle takes only what it uses.
| Import | For | Page |
|---|---|---|
@the-hades-organization/strig | the engine, Context, compile, diff, lint, the audit and Merkle verifiers | this page |
.../client, .../react, .../angular | a client that decides locally for the interface | Frontend |
.../server, .../remote | a hub that pushes changes, and the client of a decision server | Server |
.../express, .../fastify, .../nestjs | route guards | Web frameworks |
.../prisma, .../drizzle, .../kysely | a list of allowed rows as a where | ORMs |
.../approvals | approvals, elevation, recertification | Approvals |
.../store | grants and roles in a JSON file | below |
.../testing | check that every route has a rule | Web frameworks |
.../types | permission ids as a TypeScript union | below |
Every signature is in the reference.
What the engine answers beyond a verdict#
engine.allowedFields(ctx, { subject, permission: 'document.edit', resource });
// { kind: 'only', fields: ['title', 'body'] }
engine.whyNot(ctx, { subject, permission: 'document.edit', resource });
// the cause of the denial and what would change it, or null when it is allowed
engine.impact({ actor, current: grants, mutation: { add: grant }, invariants });
// who gains and who loses access if this change were applied; nothing is applied
- Field permissions for
allowedFieldsandexplainFields. - Why not for
whyNot, Impact forimpactandreviewImpact. engine.elevate(input)and theElevationsandRecertificationsclasses of.../approvalsare in Elevation and Recertification.
Two functions work on policies, not on decisions. strig.diff(before, after) lists the decisions that change between two versions of a policy, and strig.lint(policy) reports findings such as a set already violated. Diff and lint has the options and the CI use.
After a write, hold the client back until it has seen the change:
client.requireRevision('tenant@42'); // until the capabilities reach it, every decision denies
client.behind(); // true while it is behind
The audit verifiers verifyMerkleInclusion, verifyMerkleConsistency and verifyTimestamp run offline, on both backends: see Audit.
Install by platform#
Changed in 0.7.0On Node and Bun, load() tries the native module and falls back to wasm. Set STRIG_BACKEND=native or STRIG_BACKEND=wasm to force one.
npm install @the-hades-organization/strig
The native module no longer ships inside the main package, which had about 24 MB unpacked. Each platform has its own package, installed by npm as an optional dependency. A typical install is about 2 MB to download and 8 MB on disk.
| Platform | Package |
|---|---|
| Linux x64 and arm64 (glibc) | strig-linux-x64-gnu, strig-linux-arm64-gnu |
| macOS Intel and Apple silicon | strig-darwin-x64, strig-darwin-arm64 |
| Windows x64 | strig-win32-x64-msvc |
Each is published as @the-hades-organization/<name>. The loader resolves the platform package, then a local build, then falls back to wasm.
- Linux with musl (Alpine, for example) has no native module and runs on the wasm backend, which is bundled in the main package. The benchmarks show what that costs.
--omit=optionalor--no-optionalleaves the platform package out, soload()falls back to wasm. WithSTRIG_BACKEND=nativeit throws an error that names the package to install.- If you vendored
node_modulesby platform, or copied files out of the package, point them at the platform package.
Verify what you installed#
Every npm package is published with provenance, and each release carries a CycloneDX SBOM. npm audit signatures checks both of what is in your node_modules. Security has the commands.
Rules written in JavaScript#
Added in 0.7.0The function of a call condition, your own override and your own invariant are synchronous callbacks passed to the Engine constructor. The engine calls them in the middle of a decision or of apply, and they show up like the built-in ones: the explain trace names the override, a refusal carries your violation, and the audit records it as it records the built-in invariants.
const engine = new Engine(spec, {
// `call` conditions: { op: 'call', func: 'same_dept', args: [{ attr: 'subject.dept' }, { attr: 'resource.dept' }] }
conditions: { same_dept: ([a, b]) => a === b },
// Decides before the grants: 'allowed', 'denied' or 'abstained'. `except` skips permissions.
overrides: [{ name: 'frozen', except: ['project.view'], evaluate: ({ permission }) => (frozen ? 'denied' : undefined) }],
invariants: {
// A role in use is not deleted.
role_in_use({ mutation, before }) {
if (!('remove' in mutation)) return;
const grant = before.find((g) => g.id === mutation.remove.id);
if (grant?.subject.kind === 'role' && inUse(grant.subject.id)) return `role ${grant.subject.id} is in use`;
},
},
});
engine.apply({ actor, current, mutation, invariants: [{ kind: 'custom', name: 'role_in_use' }] });
An invariant runs only when invariants lists { kind: 'custom', name }, and an unknown name is UNKNOWN_INVARIANT.
| Callback | Gets | Answers |
|---|---|---|
conditions[func] | The operands of the call, already resolved: { attr: 'subject.dept' } arrives as the value of the attribute. | A boolean. |
overrides[i].evaluate | { subject, permission, resource, env, now }. | 'allowed', 'denied', 'abstained', or nothing to abstain. |
invariants[name] | { actor, mutation, before, after, now }; mutation has the shape apply takes. | Nothing, null or true to accept; a text, or { message, suggestion? }, to refuse. |
A host override runs after the overrides of the spec, in list order, and except lists permissions it is never consulted for. A suggestion is a change: the governance checks it against every invariant before it surfaces in the violation.
- A throw denies, never crashes. A condition that throws, or does not return a boolean, is a condition error: an
allowgrant does not apply, adenygrant still does, and the trace carries the message. An override that throws answersdenied. An invariant that throws refuses with the message of the error. - Callbacks are synchronous. A promise is not awaited. It is a wrong answer and denies like a throw.
- No re-entry. A callback that calls the engine or changes a
ContextgetsREENTRANT_CALL. Changing the veryContextunder decision is refused by the runtime with an error of its own. Read what you need before the call and keep it in the closure. - Listings treat an override as opaque.
listcannot know what a function of yours would decide, so the resources it might decide on stay as residue, never as allowed.
The audit sink, its policy and the redactor go in the same second argument. They are on Audit.
Catalog types for the frontend#
Added in 0.7.0The frontend should not hold permission ids as loose strings. strig-types turns an exported catalog into one .ts file, so a typo is a compile error.
A permission can carry its display name and description per locale, labels and descriptions, next to label and description. They are optional, they are not declared in the policy language, only in the catalog export (JSON) or the builder, and in Rust PermissionDef::localized_label("pt-BR", "Editar") sets one.
npx strig-types catalog.json -o src/permissions.generated.ts
The file has the PermissionId union, a PermissionLocale union, a PERMISSIONS constant with the metadata, PERMISSION_IDS, and three helpers: isPermissionId, permissionLabel and permissionDescription. The helpers take a locale and fall back to the default text. The output is sorted, so it changes only when the catalog does. Without -o it goes to the standard output.
generateTypes from @the-hades-organization/strig/types does the same as a function.
import { PermissionClient } from '@the-hades-organization/strig/client';
import { permissionLabel, isPermissionId, type PermissionId } from './permissions.generated';
// With the parameter, check, decide, explain, list, whoCan, whoCanOn and checkFor accept only these ids.
// Without it they accept any string, as before.
const engine = new strig.Engine<PermissionId>(spec);
const client = new PermissionClient<PermissionId>(/* ... */);
client.can('users:read', resource); // only ids of the union compile
permissionLabel('users:read', 'pt-BR'); // the label in that locale, or the default one
function guard(id: string) {
if (isPermissionId(id)) client.can(id, resource); // narrow a plain string first
}
Grants and roles in a JSON file#
Added in 0.7.0For a system with neither Postgres nor Redis, @the-hades-organization/strig/store keeps the grants and the role assignments in one JSON file. It runs on Node and Bun, because it uses node:fs.
import { openJsonStore } from '@the-hades-organization/strig/store';
const store = await openJsonStore({ path: './strig.json', createIfMissing: true });
const provider = engineProvider({ engine, Context, loadGrants: store.loadGrants }); // the grant source
const subject = store.subject({ kind: 'user', id: 'ana' }); // plus the roles in the file
const result = await store.apply(engine, { actor, mutation: { assign_role: { subject, role: { role: 'admin' } } }, invariants });
// accepted: the new grants and roles are written; refused: nothing is written
The file is { "version": 1, "grants": [...], "roles": { "<subject id>": [<assignment>, ...] } }. The roles themselves, the hierarchy, stay in the engine spec. The file holds who has which. It can also hold a pending list: see Approvals.
- It fails closed. A missing file, invalid JSON, a repeated grant id or a
versionother than 1 throwsJsonStoreError(STORE_NOT_FOUND,STORE_CORRUPT,STORE_VERSION) on load. The store never starts empty on its own.createIfMissingis the explicit way to start one. - Writes are atomic. The store writes a temp file in the same directory, calls
fsync, renames it over the file and then syncs the directory. A crash leaves the old file or the new one, never half of one. - Writes are serialised. Calls in one process queue up. Between processes, a
<file>.lockcreated withO_EXCLis held. A lock whose owner died on this host, or that is older thanlockStaleMs(30 s), is taken over. Each change re-reads the file under the lock, so processes do not overwrite each other. AfterlockTimeoutMs(5 s) a write fails withSTORE_LOCK_TIMEOUT. - Changes are
addGrant,updateGrant,removeGrant,assignRole,revokeRole,setRoles, orapply(engine, input), which runs the governance over the grants the file holds right now and persists what it accepts. A refusal writes nothing. - Reload.
reload()picks up an outside edit, andwatch()does it on its own. It watches the directory, because the rename replaces the file. A bad file on reload keeps the last good snapshot, throws fromreload()and goes toonErrorfromwatch().onChangefires after every reload and every write.
Not here: audit records, which go to your own sink (Audit).
Web frameworksOne route table for Express, Fastify and NestJS, deny by default.