Skip to main content
Version: dev

Configuration

Fory JavaScript is an xlang-only runtime. new Fory() writes xlang payloads and uses compatible schema evolution by default. There is no native-mode switch in the JavaScript API.

Basic Configuration

import Fory from "@apache-fory/core";

const fory = new Fory();

Create one Fory instance per application area and reuse it. Registration generates and caches serializer code for each schema.

Constructor Options

import Fory from "@apache-fory/core";
import hps from "@apache-fory/hps";

const fory = new Fory({
ref: true,
compatible: true,
maxDepth: 100,
maxBinarySize: 64 * 1024 * 1024,
maxCollectionSize: 1_000_000,
hps,
});
OptionDefaultDescription
reffalseEnable reference tracking for shared or circular object graphs
compatibletrueAllow field additions/removals without breaking existing messages
maxDepth50Maximum nesting depth. Must be >= 2. Increase for deeply nested structures
maxBinarySize64 MiBMaximum bytes accepted for any single binary field
maxCollectionSize1_000_000Maximum elements accepted in any list, set, or map
useSliceStringfalseOptional string-reading optimization for Node.js. Leave at default unless benchmarked
hpsunsetOptional fast string helper from @apache-fory/hps (Node.js 20+)
hooks.afterCodeGeneratedunsetCallback to inspect the generated serializer code, useful for debugging

Reference Tracking

Global reference tracking must be enabled before field-level reference metadata can take effect:

const fory = new Fory({ ref: true });

Then mark reference-tracked fields in the schema, for example with Type.struct("example.node").setTrackingRef(true). See References and Schema Metadata.

Compatible Schema Evolution

Compatible mode is the default:

const fory = new Fory();

Use this default for rolling upgrades, independently deployed services, and cross-language payloads. You can opt out for one stable struct with evolving: false; see Schema Evolution.

Optional HPS String Path

@apache-fory/hps provides an optional Node.js string fast path:

import hps from "@apache-fory/hps";

const fory = new Fory({ hps });

Leave this unset unless you run on Node.js 20+ and have benchmarked your workload.

Security

Security-related configuration:

  • Register only the expected schemas before deserializing untrusted payloads.
  • Set maxDepth, maxBinarySize, and maxCollectionSize for the maximum payload shape your service accepts.
  • Prefer explicit Type.struct(...) schemas over Type.any() for untrusted input.
  • Pass hps only from the official package version you deploy with the runtime.