TypeScript / JavaScript

Manifest loading guide

Common JavaScript and TypeScript API for native Node and WebAssembly.

Usage and examples

Choose a loading path

A manifest describes a policy bundle: which registered domain each policy uses, which source bytes are expected, and which queries callers may inspect. engine.loadManifest() accepts the same manifest checks through two input forms:

Where the files areCallWho reads the source?
Native Node filesystemengine.loadManifest({ path: '/policies/manifest.json' })The native loader reads the JSON and policy files named by it.
Browser or Node memoryengine.loadManifest({ text: manifestJson, policies: [{ policyId, source }] })Your application supplies the JSON and each enabled policy's exact source text.

WASM has no filesystem loading: { path } fails with UNSUPPORTED. It accepts the memory form. Both forms still validate the manifest, compare every enabled policy source with its declared SHA-256, and enforce the manifest's public query list. For memory input, policyId must match the manifest's policy_id; the manifest's file field does not trigger a fetch.

Register the domain before either call. The manifest selects an existing domain; it does not declare predicates. If you only have one source and do not need a manifest, use engine.loadInlineRuleset(). Inline loading has no manifest metadata, SHA-256 check or public query whitelist.

A small policy bundle

Both examples below use these two files. Save the source as /policies/documents.dl, including its final newline:

DATALOG
allow(User) :- user(User).

Save this JSON as /policies/manifest.json. Its SHA-256 matches the exact source bytes shown above. The relative file path is used by Node filesystem loading; memory loading matches the supplied source by policyId instead.

CODE
{
  "policy_set_id": "documents",
  "policy_set_version": "1.0.0",
  "manifest_version": "1",
  "default_profile": "enforce",
  "created_for": "production",
  "strict_loading": true,
  "fail_closed": true,
  "capabilities": [],
  "policies": [
    {
      "policy_id": "documents.main",
      "domain": "documents",
      "file": "documents.dl",
      "sha256": "6aac7702b1982dd6017f8becf5f78a431628b741ceda8b9bf58c1f010d808138",
      "mode": "enforce",
      "enabled": true,
      "description": "Allow registered users",
      "queries": [{ "name": "allow", "arity": 1 }]
    }
  ]
}

The queries list exposes allow/1 because the domain below declares it as queryable. Changes to the source, including a missing final newline, require a new SHA-256 in the manifest. See manifest concepts for the field model.

Load files in native Node

The native entry point reads the manifest and its relative policy file. The application first registers the domain named by the manifest:

CODE
import { Engine, Predicate } from '@maelys-dev/datalog/node'

const engine = await Engine.create({ profile: 'small' })
try {
  engine.registerDomain('documents', [
    Predicate.edb('user', 1),
    Predicate.idbQuery('allow', 1),
  ])
  const rules = engine.loadManifest({ path: '/policies/manifest.json' })
  try {
    console.log(rules.policyCount) // 1 enabled policy
  } finally {
    rules.close()
  }
} finally {
  engine.close()
}

Load the same bundle in a browser

With a browser bundler, import the WASM entry point. Your application fetches both files; the engine does not fetch the path in the manifest:

CODE
import { Engine, Predicate } from '@maelys-dev/datalog/wasm'

async function readText(url: string): Promise<string> {
  const response = await fetch(url)
  if (!response.ok) throw new Error(`Unable to load ${url}: ${response.status}`)
  return response.text()
}

const engine = await Engine.create({ profile: 'small' })
try {
  engine.registerDomain('documents', [
    Predicate.edb('user', 1),
    Predicate.idbQuery('allow', 1),
  ])
  const [manifestJson, source] = await Promise.all([
    readText('/policies/manifest.json'),
    readText('/policies/documents.dl'),
  ])
  const rules = engine.loadManifest({
    text: manifestJson,
    policies: [{ policyId: 'documents.main', source }],
  })
  try {
    console.log(rules.policyCount) // 1 enabled policy
  } finally {
    rules.close()
  }
} finally {
  engine.close()
}

For a manifest with several enabled policies, provide one matching { policyId, source } entry for each. Both optional loading permissions default to false; enabling a test-only policy or undeclared source atoms does not bypass SHA-256 verification. A successful load returns a Ruleset; keep it open while its EDB, sessions and results are in use. A failed load does not replace an existing ruleset.