TypeScript / JavaScript
Engine
Engine class and methods for the common JavaScript and TypeScript API.Examples that omit setup use the page reference context, which supplies engine, manifestJson, policySource and the binding imports.
The reference below follows the same class → member order as the JavaScript API. The overview explains runtime selection; the full program shows these methods together. See EngineOptions for creation options.
Class: Engine
The Engine is the root of the JavaScript resource tree. Use Engine.create() to initialize it. The private constructor() in the TypeScript declaration means applications cannot call new Engine(); limits is a read-only Limits snapshot. The native Node and WebAssembly entry points share this class.
No public constructor.
- factory
Engine.create()Initialize a fresh engine for the selected runtime and profile.
- Engine.close()Release this resource and its owned descendants. Closing is idempotent.
- Engine.loadInlineRuleset()Compile one source policy against an already registered domain.
- Engine.loadManifest()Validate and load enabled policies from a manifest, including source hashes and permissions.
- Engine.registerDomain()Copy predicate declarations and allowed source atoms into the engine registry.
- using engineLeave the scope to close this Engine, all of its rulesets and their sessions, results and input buffers.
console.log('max predicates:', engine.limits.maxPredicates)max predicates: 128
Factories
Engine.create
Engine.create(
options?: EngineOptions
): Promise<Engine>Promise<Engine>Resolves to a new Engine. Rejects unsupported platforms, missing assets or initialization failures; no runtime fallback is selected.
Methods
Each method below has its own signature, arguments, return behavior and stable link.
Engine.close
Engine.close(): voidvoidReturns nothing. Repeated closing succeeds; operational calls after closing fail with INVALID_STATE.
{
const engine = await Engine.create({ profile: 'small' })
try {
// Closing also releases this engine's rulesets, inputs and sessions.
} finally { engine.close() }
}
console.log('engine: closed')engine: closed
Engine.loadInlineRuleset
Engine.loadInlineRuleset(
domain: string,
id: string,
source: string
): RulesetRulesetReturns Ruleset. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
import { Engine, Predicate } from '@maelys-dev/datalog/wasm'
const engine = await Engine.create({ profile: 'small' })
try {
engine.registerDomain('documents', [
Predicate.edb('user', 1), Predicate.idbQuery('allow', 1),
], [])
const source = 'allow(User) :- user(User).'
const ruleset = engine.loadInlineRuleset('documents', 'documents.main', source)
try { console.log('policies:', ruleset.policyCount) } finally { ruleset.close() }
} finally { engine.close() }policies: 1
Engine.loadManifest
Engine.loadManifest(
input: ManifestInput,
options?: ManifestOptions
): RulesetRulesetReturns Ruleset. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.
// The manifest contains the SHA-256 of this exact policySource.
const ruleset = engine.loadManifest({
text: manifestJson,
policies: [{ policyId: 'documents.main', source: policySource }],
})
try { console.log('policies:', ruleset.policyCount) } finally { ruleset.close() }policies: 1
Engine.registerDomain
Engine.registerDomain(
name: string,
predicates: readonly Predicate[],
atoms?: readonly string[]
): voidvoidReturns nothing on success. Invalid JavaScript inputs throw TypeError or RangeError; native failures carry MaelysDatalogError and a diagnostic.
import { Engine, Predicate } from '@maelys-dev/datalog/wasm'
const engine = await Engine.create({ profile: 'small' })
try {
engine.registerDomain('documents', [
Predicate.edb('user', 1),
Predicate.idbQuery('allow', 1),
], [])
console.log('domain: registered')
} finally {
engine.close()
}domain: registered
Protocols
using engine
Exiting the using scope disposes the resource, including on exceptional exit. Cleanup delegates to close() and is idempotent.
{
using engine = await Engine.create({ profile: 'small' })
console.log('max EDB facts:', engine.limits.maxEdbFacts)
}max EDB facts: 1024
Perform the same operations with explicit cleanup in a finally block.
{
const engine = await Engine.create({ profile: 'small' })
try {
console.log('max EDB facts:', engine.limits.maxEdbFacts)
} finally {
engine.close()
}
}max EDB facts: 1024
Interfaces
EngineOptions · ManifestOptions.
EngineOptions configures Engine.create().
EngineOptions
const options: EngineOptions = { profile: 'small' }
const configured = await Engine.create(options)
try {
console.log('profile:', options.profile)
} finally { configured.close() }profile: small
The manifest declarations define the inputs and permissions for Engine.loadManifest().
ManifestOptions
const options: ManifestOptions = { allowTestOnly: true }
const ruleset = engine.loadManifest({
text: manifestJson,
policies: [{ policyId: 'documents.main', source: policySource }],
}, options)
try { console.log('policies:', ruleset.policyCount) } finally { ruleset.close() }policies: 1
Type aliases
ManifestInput
export type ManifestInput = { path: string; text?: never; policies?: never } |
{ text: string; policies: readonly { policyId: string; source: string }[]; path?: never };const input: ManifestInput = {
text: manifestJson,
policies: [{ policyId: 'documents.main', source: policySource }],
}
const ruleset = engine.loadManifest(input)
try { console.log('policies:', ruleset.policyCount) } finally { ruleset.close() }policies: 1