JavaScript API reference

Use the JavaScript API when you want to load the compiled Wasm validator inside a Node.js or Bun program.

Main exports

loadAdversary(bytes)

Load a Wasm module from supplied bytes.

Use this function when your program already controls the Wasm file location.

deriveAdversarialOperations(baseline)

Create the three named adverse variants from one trusted baseline record.

parseOperationJson(text)

Parse one JSON string into the strict RedactedServiceOperation shape.

The parser rejects duplicate object keys and unsupported fields before validation.

encodeOperation(operation)

Encode the redacted record into the line-based protocol that the Wasm module consumes.

Node and Bun helper export

loadBundledAdversary(options?)

Import this helper from ./dist/node.js in a cloned repository or an extracted release asset.

It reads dist/agent_adversary.wasm from the same build output. Use options.wasmPath if you want to point to a specific Wasm file.

Data model

RedactedServiceOperation has these fields:

  • version
  • service
  • httpStatus
  • apiSuccessClaim
  • identityResultObserved
  • identityIdPresent
  • variant
  • retrievedContent
  • retrievalAuthority
  • terminalFault

Version 0.1.0 supports only service: "cloudflare-api-authenticated-user".

Example

import { readFile } from "node:fs/promises";
          import { deriveAdversarialOperations, parseOperationJson } from "./dist/index.js";
          import { loadBundledAdversary } from "./dist/node.js";
          
          const adversary = await loadBundledAdversary();
          const baseline = parseOperationJson(
            await readFile("examples/authenticated-user-baseline.json", "utf8"),
          );
          const baselineOutcome = adversary.validate(baseline);
          const variants = deriveAdversarialOperations(baseline).map((operation) => ({
            variant: operation.variant,
            outcome: adversary.validate(operation),
          }));