@loro-dev/loro-repo-engine 0.1.0 → 0.2.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -347,6 +347,37 @@ Executes the embedded `loro-repo` CLI synchronously in the current process (`arg
347
347
  - `1`: runtime or I/O error (including `ENGINE_BUSY` if another daemon already locks `--data-dir`)
348
348
  - `2`: invalid CLI usage or bad flags
349
349
 
350
+ ### Host process helpers: `closeInheritedDescriptors()` and `hardenHostProcess()`
351
+
352
+ ```ts
353
+ export function closeInheritedDescriptors(): number;
354
+ export function hardenHostProcess(): HostHardening;
355
+ export interface HostHardening {
356
+ coreDumpsDisabled: boolean; // RLIMIT_CORE is 0 (soft and hard)
357
+ dumpableCleared: boolean; // Linux: PR_SET_DUMPABLE is 0
358
+ platform: string; // process.platform-style name
359
+ limitations: string[]; // what this platform does not cover
360
+ }
361
+ ```
362
+
363
+ A dedicated process that hosts the engine holds epoch keys and writer seeds in memory. Call
364
+ both first thing in that process, in this order:
365
+
366
+ 1. `closeInheritedDescriptors()` closes every descriptor above stderr that it inherited
367
+ without close-on-exec, so a short-lived launcher's pipes do not stay open. It returns how
368
+ many it closed; off Unix it closes nothing.
369
+ 2. `hardenHostProcess()` keeps key material out of crash artifacts:
370
+
371
+ | Platform | Applied | `limitations` |
372
+ | --- | --- | --- |
373
+ | Linux | `setrlimit(RLIMIT_CORE, 0)` and `prctl(PR_SET_DUMPABLE, 0)`: no core file, and same-user processes can neither `ptrace` it nor read its memory through `/proc` | none |
374
+ | macOS | `setrlimit(RLIMIT_CORE, 0)` | no `PR_SET_DUMPABLE` equivalent; same-user debugger attach is governed by SIP and code signing, not by this call; ReportCrash reports carry stacks and registers, not memory |
375
+ | Windows | nothing in-process | Windows Error Reporting may write a dump; disable WER `LocalDumps` for the host executable |
376
+
377
+ Each item is re-read after it is applied, so the result reports what is actually in
378
+ effect. The call is idempotent and never throws. Run it after
379
+ `closeInheritedDescriptors()`, which lists `/dev/fd`.
380
+
350
381
  ### Error Contract
351
382
 
352
383
  | Condition | Thrown Error / Prefix | Caller Action |
@@ -559,7 +590,7 @@ try {
559
590
 
560
591
  For cloud sync, register the protector first, then configure `{ remote_id: "cloud", base_url: yourStreamsUrl, encrypted: true }` with `client.configureRemote(namespace, remote)` and call `client.syncNamespace(namespace, "cloud")`. Configure bearer auth separately if the server requires it. A peer needs the same genesis and epoch keys plus `admitWriterPubkey(namespace, publicKey)` for every trusted sending device. Only admit keys authenticated by your application's verified ledger. This keyring does not fetch or verify that ledger or distribute keys.
561
592
 
562
- All genesis/seed/key/public-key arguments are exactly 32-byte `Uint8Array`s. Epochs are integers from `0` through `4294967295`. Namespace and writer names are nonempty and cannot contain NUL. `configureNamespace` rejects conflicting genesis; `installEpochKey` permits identical reinstalls and rejects conflicting material for an existing epoch. The highest installed epoch immediately becomes the seal epoch. Older installed epochs remain usable for opening history.
593
+ All genesis/seed/key/public-key arguments are exactly 32-byte `Uint8Array`s. Epochs are integers from `0` through `4294967295`. Namespace and writer names are nonempty and cannot contain NUL. `configureNamespace` rejects conflicting genesis; `installEpochKey` permits identical reinstalls and rejects conflicting material for an existing epoch. The highest installed epoch immediately becomes the seal epoch unless `setSealEpoch` fixed one; a later install does not move a fixed seal epoch. Older installed epochs remain usable for opening history. `holdSealing(ns, writer?)` refuses that writer's seals with `unauthorized` until `releaseSealing`, and `removeWriterSigner(ns, writer?)` drops its seed; both keep its public key admitted, so frames it wrote keep opening. `revokeWriterPubkey` is the only call that removes admission.
563
594
 
564
595
  `configureNamespace(namespace, genesis, seed?)` installs an optional default signer. `configureWriterSigner(namespace, writer, seed)` installs a concrete writer signer; a requested writer never falls back to the default. Both return the signer's public key and admit it locally. Replacing a signer does not revoke the previous public key: explicitly call `revokeWriterPubkey` when your authorization policy requires it. Revocation blocks both opening that writer's frames and sealing with its local signer. `clearNamespace` drops all namespace custody and admission; restore genesis, trusted writers, and required epochs before resuming sync.
565
596
 
@@ -587,3 +618,41 @@ pnpm --dir packages/client exec vitest run test/keyring.test.ts test/keyring-int
587
618
  ```
588
619
 
589
620
  `keyring-smoke.mjs` exchanges update and snapshot fixtures in both directions with a separate Rust executable, checks unknown epochs, unauthorized/revoked writers, tampering, AAD/room/offset mismatches, immutable key conflicts, and clear/restore/history behavior. Set `KEYRING_INTEROP_BIN` to override the fixture executable path. The interop test also exchanges frames with an independent TypeScript test implementation. The E2E tests require a built `loro-repo` daemon (`LORO_REPO_BIN` override supported) and use real IPC, HTTP, and native custody. They cover a held seal callback plus separate custody processes, lost and stale installation acknowledgements, crash/restart, restoration of historical keys, and epoch-2 writes after all live participants acknowledge. These checks exercise an application coordinator in a test harness; production key delivery and coordinator recovery remain application responsibilities.
621
+
622
+ ## Room Resource Lifecycle
623
+
624
+ | Operation | Releases | Retains |
625
+ | --- | --- | --- |
626
+ | `unsubscribe` | That subscription; after the last subscription the room becomes eligible for eviction and leaves remote interest | Other connections' subscriptions, outbox, persistent data |
627
+ | Connection close | All that connection's subscriptions and reverse RPC registrations | Other connections' resources and committed writes |
628
+ | Idle eviction | The in-memory room backend | Persistent data, checkpoints, outbox, room table entry |
629
+ | `deleteRoom` | The room's stored data and associated room state | This is a data operation, not a memory management tool |
630
+ | Engine stop | Directory lock and all engine memory | Persistent data |
631
+
632
+ The idle backend limit is **16 per namespace**, excluding rooms with active
633
+ subscriptions. Eviction skips locked rooms, so concurrent activity can temporarily
634
+ exceed that limit. Access reloads a backend from committed storage. Room table
635
+ entries remain until `deleteRoom`; they are not the loaded backend count.
636
+
637
+ A room without subscriptions does not receive remote real-time updates. Its
638
+ outbox still uploads when a configured remote is unpaused, including after
639
+ backend eviction or an engine restart. The directory lock belongs to the engine
640
+ lifetime and is unrelated to individual rooms. A write committed before a socket
641
+ closes stays committed even if its reply was lost; callers must reconcile an
642
+ unknown write result before retrying.
643
+
644
+ `engine.stats()` provides structured resource counters through the in-process
645
+ addon only (no wire operation): per namespace `roomEntries`, `loadedBackends`,
646
+ `docSubscriptions`, `flockSubscriptions`; globally `connections` and
647
+ `pendingOutboxNamespaces`. This is a memory-only observation: it never calls the
648
+ storage factory, queries SQLite, or creates/configures remotes. Only loaded
649
+ namespaces appear in `namespaces`. A busy room is included in `roomEntries`, but
650
+ skipped in the backend and subscription counts; those counts are lower bounds
651
+ while room operations hold locks.
652
+
653
+ `pendingOutboxNamespaces` is the background discovery index: namespaces already
654
+ discovered with unpaused upload work. It excludes paused and unprobed persisted
655
+ backlog and can lag live writes or draining. Use `getRemoteStatus` for current
656
+ storage-backed outbox counts. The result is not an atomic snapshot across
657
+ namespaces. Calling after stop throws. It includes no room IDs, keys, values or
658
+ credentials.
package/index.d.ts CHANGED
@@ -17,6 +17,13 @@ export interface EngineOptions {
17
17
  streamsUrl?: string;
18
18
  /** Maximum local IPC frame size in bytes (omitted uses `67_108_864` / `64 MiB`). */
19
19
  maxFrameBytes?: number;
20
+ /**
21
+ * Report only these capabilities, to stand in for an older build in
22
+ * compatibility tests. Serving does not change.
23
+ */
24
+ advertisedCapabilities?: string[];
25
+ /** Report this release instead, to stand in for an older release in compatibility tests. */
26
+ advertisedVersion?: string;
20
27
  }
21
28
 
22
29
  /**
@@ -35,10 +42,33 @@ export interface EngineEndpoint {
35
42
  pid: number;
36
43
  /** Wire protocol version implemented by this engine (currently `2`). */
37
44
  protocolVersion: number;
45
+ /** This release (SemVer), which orders builds for takeover. */
46
+ version: string;
47
+ /** This build's identity. */
48
+ build: string;
49
+ /** What this build serves beyond the protocol's baseline. */
50
+ capabilities: string[];
51
+ }
52
+
53
+ /** Resource counters; no room content or keys. Concurrent activity may change counts. */
54
+ export interface NamespaceStats {
55
+ namespace: string;
56
+ roomEntries: number;
57
+ loadedBackends: number;
58
+ docSubscriptions: number;
59
+ flockSubscriptions: number;
60
+ }
61
+ export interface EngineStats {
62
+ namespaces: NamespaceStats[];
63
+ connections: number;
64
+ /** Background discovery index: known unpaused backlog; excludes paused/unprobed work and can lag. */
65
+ pendingOutboxNamespaces: number;
38
66
  }
39
67
 
40
68
  /** Running in-process engine handle returned by {@link startEngine}. */
41
69
  export declare class Engine {
70
+ /** Memory-only counts. Busy rooms count only toward roomEntries; others are lower bounds while busy. Throws after stop. */
71
+ stats(): EngineStats;
42
72
  /** Published local IPC endpoint matching `<dataDir>/engine.json`. */
43
73
  get endpoint(): EngineEndpoint;
44
74
  /** `true` once the engine has exited (via `stop()`, idle timeout, or lock loss). */
@@ -65,6 +95,12 @@ export interface EngineVersion {
65
95
  package: string;
66
96
  /** Wire protocol version supported by this engine (`2`). */
67
97
  protocol: number;
98
+ /** This release (SemVer), which orders builds for takeover. */
99
+ version: string;
100
+ /** This build's identity, as `engine.json` and `engine_info` report it. */
101
+ build: string;
102
+ /** What this build serves beyond the protocol's baseline; never shrinks within a protocol. */
103
+ capabilities: string[];
68
104
  }
69
105
 
70
106
  /** Returns the engine's package version and wire protocol version before touching a `dataDir`. */
@@ -81,6 +117,36 @@ export declare function engineVersion(): EngineVersion;
81
117
  */
82
118
  export declare function runCli(args: string[]): number;
83
119
 
120
+ /**
121
+ * Closes the descriptors this process inherited from whoever launched it: every
122
+ * descriptor above stderr open without close-on-exec. A long-lived host started
123
+ * by a short-lived application otherwise keeps that application's sockets and
124
+ * pipes open after it exits. Call it first thing in a dedicated host process.
125
+ * Returns how many were closed; off Unix it closes nothing.
126
+ */
127
+ export declare function closeInheritedDescriptors(): number;
128
+
129
+ /** What `hardenHostProcess` put into effect, each item verified after it was applied. */
130
+ export interface HostHardening {
131
+ /** The core-file limit is 0 (soft and hard): a crash writes no core with key material. */
132
+ coreDumpsDisabled: boolean;
133
+ /** Linux only: not dumpable, so same-user processes cannot ptrace it or read its memory. */
134
+ dumpableCleared: boolean;
135
+ /** `process.platform`-style name of the platform the measures were applied on. */
136
+ platform: string;
137
+ /** What this platform does not cover. */
138
+ limitations: string[];
139
+ }
140
+
141
+ /**
142
+ * Hardens a process that holds key material, such as a dedicated engine host:
143
+ * `setrlimit(RLIMIT_CORE, 0)` on Unix and, on Linux, `prctl(PR_SET_DUMPABLE, 0)`.
144
+ * macOS gets the core limit only; Windows gets nothing in-process (see
145
+ * `limitations`). Call it once, early, after `closeInheritedDescriptors()`.
146
+ * Idempotent; never throws.
147
+ */
148
+ export declare function hardenHostProcess(): HostHardening;
149
+
84
150
  /** Engine-provided request; pass bytes through without re-encoding or wrapping LSCE. */
85
151
  export interface ContentSealRequest {
86
152
  namespace: string;
@@ -130,11 +196,23 @@ export declare class ContentKeyring {
130
196
  /** Remove admission (including local sealing permission); idempotent, does not erase signer custody. */
131
197
  revokeWriterPubkey(namespace: string, publicKey: Uint8Array): void;
132
198
  /**
133
- * Install an integer epoch in [0, 4294967295]. Highest installed epoch immediately seals new data.
199
+ * Install an integer epoch in [0, 4294967295]. Without {@link setSealEpoch}, the highest installed epoch immediately seals new data.
134
200
  * Identical reinstall is allowed; a conflicting key for an existing epoch is rejected.
135
201
  * Retain older epochs for historical ciphertext; install only after readers are ready.
136
202
  */
137
203
  installEpochKey(namespace: string, epoch: number, key: Uint8Array): void;
204
+ /**
205
+ * Seal new content at this installed epoch instead of the highest installed one; a later
206
+ * install does not move it. Opening still uses every installed epoch. Unknown epochs fail
207
+ * with `unknown_key`.
208
+ */
209
+ setSealEpoch(namespace: string, epoch: number): void;
210
+ /** Refuse seals for `writer` (omitted: the default signer) with `unauthorized` until released; admission is kept. */
211
+ holdSealing(namespace: string, writer?: string | null): void;
212
+ /** End {@link holdSealing}; idempotent. */
213
+ releaseSealing(namespace: string, writer?: string | null): void;
214
+ /** Drop the writer's signing seed and keep its public key admitted, so its frames keep opening. */
215
+ removeWriterSigner(namespace: string, writer?: string | null): void;
138
216
  /** Drop all namespace custody and admission; idempotent. Subsequent seal/open reports missing_provider. */
139
217
  clearNamespace(namespace: string): void;
140
218
  /** Synchronous native seal; explicit writer never falls back to the default signer. Roost streams unsupported. */
package/index.js CHANGED
@@ -34,5 +34,21 @@ function load() {
34
34
  return require(`@loro-dev/loro-repo-engine-${suffix}`);
35
35
  }
36
36
 
37
- const { Engine, ContentKeyring, startEngine, engineVersion, runCli } = load();
38
- module.exports = { Engine, ContentKeyring, startEngine, engineVersion, runCli };
37
+ const {
38
+ Engine,
39
+ ContentKeyring,
40
+ startEngine,
41
+ engineVersion,
42
+ runCli,
43
+ closeInheritedDescriptors,
44
+ hardenHostProcess,
45
+ } = load();
46
+ module.exports = {
47
+ Engine,
48
+ ContentKeyring,
49
+ startEngine,
50
+ engineVersion,
51
+ runCli,
52
+ closeInheritedDescriptors,
53
+ hardenHostProcess,
54
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loro-dev/loro-repo-engine",
3
- "version": "0.1.0",
3
+ "version": "0.2.0-next.1",
4
4
  "description": "The loro-repo engine, prebuilt as a Node-API addon. Source is not public (D3).",
5
5
  "license": "UNLICENSED",
6
6
  "publishConfig": {
@@ -32,12 +32,12 @@
32
32
  ]
33
33
  },
34
34
  "optionalDependencies": {
35
- "@loro-dev/loro-repo-engine-darwin-arm64": "0.1.0",
36
- "@loro-dev/loro-repo-engine-darwin-x64": "0.1.0",
37
- "@loro-dev/loro-repo-engine-linux-x64-gnu": "0.1.0",
38
- "@loro-dev/loro-repo-engine-linux-arm64-gnu": "0.1.0",
39
- "@loro-dev/loro-repo-engine-win32-x64-msvc": "0.1.0",
40
- "@loro-dev/loro-repo-engine-win32-arm64-msvc": "0.1.0"
35
+ "@loro-dev/loro-repo-engine-darwin-arm64": "0.2.0-next.1",
36
+ "@loro-dev/loro-repo-engine-darwin-x64": "0.2.0-next.1",
37
+ "@loro-dev/loro-repo-engine-linux-x64-gnu": "0.2.0-next.1",
38
+ "@loro-dev/loro-repo-engine-linux-arm64-gnu": "0.2.0-next.1",
39
+ "@loro-dev/loro-repo-engine-win32-x64-msvc": "0.2.0-next.1",
40
+ "@loro-dev/loro-repo-engine-win32-arm64-msvc": "0.2.0-next.1"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@napi-rs/cli": "^3.10.0"