@milaboratories/pl-client 3.16.2 → 3.17.0
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/dist/core/capabilities.cjs.map +1 -1
- package/dist/core/capabilities.d.ts +1 -1
- package/dist/core/capabilities.d.ts.map +1 -1
- package/dist/core/capabilities.js.map +1 -1
- package/dist/core/transaction.cjs +37 -5
- package/dist/core/transaction.cjs.map +1 -1
- package/dist/core/transaction.d.ts +32 -3
- package/dist/core/transaction.d.ts.map +1 -1
- package/dist/core/transaction.js +37 -5
- package/dist/core/transaction.js.map +1 -1
- package/dist/core/tree_filter.cjs +2 -1
- package/dist/core/tree_filter.cjs.map +1 -1
- package/dist/core/tree_filter.d.ts +2 -1
- package/dist/core/tree_filter.d.ts.map +1 -1
- package/dist/core/tree_filter.js +2 -1
- package/dist/core/tree_filter.js.map +1 -1
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.cjs +33 -11
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.cjs.map +1 -1
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts +129 -37
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts.map +1 -1
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.js +33 -11
- package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.js.map +1 -1
- package/package.json +3 -3
- package/src/core/capabilities.ts +4 -0
- package/src/core/ll_transaction.test.ts +6 -0
- package/src/core/transaction.ts +53 -8
- package/src/core/tree_filter.ts +2 -1
- package/src/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.ts +158 -51
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capabilities.cjs","names":[],"sources":["../../src/core/capabilities.ts"],"sourcesContent":["/**\n * Backend capability tokens advertised by pl in\n * `MaintenanceAPI.Ping.Response.capabilities`.\n * Capabilities affect behaviour of inner ML mechanisms and allow\n * to detect incompatibilities between blocks and backend via\n * via `TemplateDataV3.requiredCapabilities`.\n *\n * Keep tokens in sync with the backend's advertised list. Format is\n * `<feature>:<version>`; bump the version when wire semantics change.\n *\n * Shared across:\n * - `@milaboratories/pl-client` — client-side capability checks\n * - `@platforma-sdk/tengo-builder` — populates `requiredCapabilities` at compile time\n * - `@platforma-sdk/block-tools` — copies it onto the published manifest\n * - `@milaboratories/pl-middle-layer` — checks it at install time\n */\nexport type BackendCapability =\n | \"auth:v2\"\n | \"treeFilter:v2\"\n | \"wasm:v1\"\n // Advertised only where the deployment actually has scratch storage, not merely where the\n // build understands a 'scratchFreeSpace' request: a client that sees this stops arranging\n // its own temporary storage. Off unless pl was started with --has-scratch-space.\n | \"scratchSpace:v1\"\n // Project-sharing capability tokens.\n | \"crossTreeRefs:v1\" // cross-color field attach (accept a foreign-colored shared envelope)\n | \"userListing:v1\" // list users for the recipient picker\n | \"publicGrants:v1\" // public (everyone) grants allowed for any role\n | \"txListGrants:v1\"; // list grants inside a transaction (batched recipient reads)\n\n/** True iff `capabilities` advertises the requested token. */\nexport function hasCapability(\n capabilities: readonly string[] | undefined,\n capability: BackendCapability,\n): boolean {\n return capabilities?.includes(capability) ?? false;\n}\n"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"capabilities.cjs","names":[],"sources":["../../src/core/capabilities.ts"],"sourcesContent":["/**\n * Backend capability tokens advertised by pl in\n * `MaintenanceAPI.Ping.Response.capabilities`.\n * Capabilities affect behaviour of inner ML mechanisms and allow\n * to detect incompatibilities between blocks and backend via\n * via `TemplateDataV3.requiredCapabilities`.\n *\n * Keep tokens in sync with the backend's advertised list. Format is\n * `<feature>:<version>`; bump the version when wire semantics change.\n *\n * Shared across:\n * - `@milaboratories/pl-client` — client-side capability checks\n * - `@platforma-sdk/tengo-builder` — populates `requiredCapabilities` at compile time\n * - `@platforma-sdk/block-tools` — copies it onto the published manifest\n * - `@milaboratories/pl-middle-layer` — checks it at install time\n */\nexport type BackendCapability =\n | \"auth:v2\"\n | \"treeFilter:v2\"\n // Delta tree reads: Tree, ResourceAPI.Get and ResourceKVAPI.List accept a\n // changed_since_token and answer with only what changed since it. Narrower in name\n // than in reach, since it gates all three.\n | \"treeChangedSince:v1\"\n | \"wasm:v1\"\n // Advertised only where the deployment actually has scratch storage, not merely where the\n // build understands a 'scratchFreeSpace' request: a client that sees this stops arranging\n // its own temporary storage. Off unless pl was started with --has-scratch-space.\n | \"scratchSpace:v1\"\n // Project-sharing capability tokens.\n | \"crossTreeRefs:v1\" // cross-color field attach (accept a foreign-colored shared envelope)\n | \"userListing:v1\" // list users for the recipient picker\n | \"publicGrants:v1\" // public (everyone) grants allowed for any role\n | \"txListGrants:v1\"; // list grants inside a transaction (batched recipient reads)\n\n/** True iff `capabilities` advertises the requested token. */\nexport function hasCapability(\n capabilities: readonly string[] | undefined,\n capability: BackendCapability,\n): boolean {\n return capabilities?.includes(capability) ?? false;\n}\n"],"mappings":";;AAmCA,SAAgB,cACd,cACA,YACS;CACT,OAAO,cAAc,SAAS,UAAU,KAAK;AAC/C"}
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* - `@platforma-sdk/block-tools` — copies it onto the published manifest
|
|
16
16
|
* - `@milaboratories/pl-middle-layer` — checks it at install time
|
|
17
17
|
*/
|
|
18
|
-
export type BackendCapability = "auth:v2" | "treeFilter:v2" | "wasm:v1" | "scratchSpace:v1" | "crossTreeRefs:v1" | "userListing:v1" | "publicGrants:v1" | "txListGrants:v1";
|
|
18
|
+
export type BackendCapability = "auth:v2" | "treeFilter:v2" | "treeChangedSince:v1" | "wasm:v1" | "scratchSpace:v1" | "crossTreeRefs:v1" | "userListing:v1" | "publicGrants:v1" | "txListGrants:v1";
|
|
19
19
|
/** True iff `capabilities` advertises the requested token. */
|
|
20
20
|
export declare function hasCapability(capabilities: readonly string[] | undefined, capability: BackendCapability): boolean;
|
|
21
21
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capabilities.d.ts","names":[],"sources":["../../src/core/capabilities.ts"],"mappings":";;;;;;;;;;;;;;;;;YAgBY;;
|
|
1
|
+
{"version":3,"file":"capabilities.d.ts","names":[],"sources":["../../src/core/capabilities.ts"],"mappings":";;;;;;;;;;;;;;;;;YAgBY;;wBAmBI,cACd,6CACA,YAAY"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capabilities.js","names":[],"sources":["../../src/core/capabilities.ts"],"sourcesContent":["/**\n * Backend capability tokens advertised by pl in\n * `MaintenanceAPI.Ping.Response.capabilities`.\n * Capabilities affect behaviour of inner ML mechanisms and allow\n * to detect incompatibilities between blocks and backend via\n * via `TemplateDataV3.requiredCapabilities`.\n *\n * Keep tokens in sync with the backend's advertised list. Format is\n * `<feature>:<version>`; bump the version when wire semantics change.\n *\n * Shared across:\n * - `@milaboratories/pl-client` — client-side capability checks\n * - `@platforma-sdk/tengo-builder` — populates `requiredCapabilities` at compile time\n * - `@platforma-sdk/block-tools` — copies it onto the published manifest\n * - `@milaboratories/pl-middle-layer` — checks it at install time\n */\nexport type BackendCapability =\n | \"auth:v2\"\n | \"treeFilter:v2\"\n | \"wasm:v1\"\n // Advertised only where the deployment actually has scratch storage, not merely where the\n // build understands a 'scratchFreeSpace' request: a client that sees this stops arranging\n // its own temporary storage. Off unless pl was started with --has-scratch-space.\n | \"scratchSpace:v1\"\n // Project-sharing capability tokens.\n | \"crossTreeRefs:v1\" // cross-color field attach (accept a foreign-colored shared envelope)\n | \"userListing:v1\" // list users for the recipient picker\n | \"publicGrants:v1\" // public (everyone) grants allowed for any role\n | \"txListGrants:v1\"; // list grants inside a transaction (batched recipient reads)\n\n/** True iff `capabilities` advertises the requested token. */\nexport function hasCapability(\n capabilities: readonly string[] | undefined,\n capability: BackendCapability,\n): boolean {\n return capabilities?.includes(capability) ?? false;\n}\n"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"capabilities.js","names":[],"sources":["../../src/core/capabilities.ts"],"sourcesContent":["/**\n * Backend capability tokens advertised by pl in\n * `MaintenanceAPI.Ping.Response.capabilities`.\n * Capabilities affect behaviour of inner ML mechanisms and allow\n * to detect incompatibilities between blocks and backend via\n * via `TemplateDataV3.requiredCapabilities`.\n *\n * Keep tokens in sync with the backend's advertised list. Format is\n * `<feature>:<version>`; bump the version when wire semantics change.\n *\n * Shared across:\n * - `@milaboratories/pl-client` — client-side capability checks\n * - `@platforma-sdk/tengo-builder` — populates `requiredCapabilities` at compile time\n * - `@platforma-sdk/block-tools` — copies it onto the published manifest\n * - `@milaboratories/pl-middle-layer` — checks it at install time\n */\nexport type BackendCapability =\n | \"auth:v2\"\n | \"treeFilter:v2\"\n // Delta tree reads: Tree, ResourceAPI.Get and ResourceKVAPI.List accept a\n // changed_since_token and answer with only what changed since it. Narrower in name\n // than in reach, since it gates all three.\n | \"treeChangedSince:v1\"\n | \"wasm:v1\"\n // Advertised only where the deployment actually has scratch storage, not merely where the\n // build understands a 'scratchFreeSpace' request: a client that sees this stops arranging\n // its own temporary storage. Off unless pl was started with --has-scratch-space.\n | \"scratchSpace:v1\"\n // Project-sharing capability tokens.\n | \"crossTreeRefs:v1\" // cross-color field attach (accept a foreign-colored shared envelope)\n | \"userListing:v1\" // list users for the recipient picker\n | \"publicGrants:v1\" // public (everyone) grants allowed for any role\n | \"txListGrants:v1\"; // list grants inside a transaction (batched recipient reads)\n\n/** True iff `capabilities` advertises the requested token. */\nexport function hasCapability(\n capabilities: readonly string[] | undefined,\n capability: BackendCapability,\n): boolean {\n return capabilities?.includes(capability) ?? false;\n}\n"],"mappings":";;AAmCA,SAAgB,cACd,cACA,YACS;CACT,OAAO,cAAc,SAAS,UAAU,KAAK;AAC/C"}
|
|
@@ -122,6 +122,7 @@ var PlTransaction = class PlTransaction {
|
|
|
122
122
|
finalPredicate;
|
|
123
123
|
sharedResourceDataCache;
|
|
124
124
|
enableFormattedErrors;
|
|
125
|
+
txOpen;
|
|
125
126
|
globalTxId;
|
|
126
127
|
localTxId = PlTransaction.nextLocalTxId();
|
|
127
128
|
/** Used in caching */
|
|
@@ -149,14 +150,18 @@ var PlTransaction = class PlTransaction {
|
|
|
149
150
|
this.finalPredicate = finalPredicate;
|
|
150
151
|
this.sharedResourceDataCache = sharedResourceDataCache;
|
|
151
152
|
this.enableFormattedErrors = enableFormattedErrors;
|
|
152
|
-
this.
|
|
153
|
+
this.txOpen = this.sendSingleAndParse({
|
|
153
154
|
oneofKind: "txOpen",
|
|
154
155
|
txOpen: {
|
|
155
156
|
name,
|
|
156
157
|
enableFormattedErrors,
|
|
157
158
|
writable: writable ? 1 : 2
|
|
158
159
|
}
|
|
159
|
-
}, (r) => (
|
|
160
|
+
}, (r) => ({
|
|
161
|
+
id: (0, _milaboratories_ts_helpers.notEmpty)(r.txOpen.tx?.id),
|
|
162
|
+
sinceToken: r.txOpen.nextSinceToken.length > 0 ? r.txOpen.nextSinceToken : void 0
|
|
163
|
+
}));
|
|
164
|
+
this.globalTxId = this.txOpen.then((r) => r.id);
|
|
160
165
|
this.track(this.globalTxId);
|
|
161
166
|
this.globalTxId.catch((err) => {
|
|
162
167
|
if (!this.globalTxIdWasAwaited) console.warn(err);
|
|
@@ -424,7 +429,8 @@ var PlTransaction = class PlTransaction {
|
|
|
424
429
|
resourceGet: {
|
|
425
430
|
...this.toSignedResourceId(rId),
|
|
426
431
|
loadFields,
|
|
427
|
-
showSoftDeletes: false
|
|
432
|
+
showSoftDeletes: false,
|
|
433
|
+
changedSinceToken: /* @__PURE__ */ new Uint8Array(0)
|
|
428
434
|
}
|
|
429
435
|
}, (r) => require_type_conversion.protoToResource((0, _milaboratories_ts_helpers.notEmpty)(r.resourceGet.resource)));
|
|
430
436
|
this._stat.rGetDataNetRequests++;
|
|
@@ -630,7 +636,8 @@ var PlTransaction = class PlTransaction {
|
|
|
630
636
|
resourceKeyValueList: {
|
|
631
637
|
...this.toSignedResourceId(rId),
|
|
632
638
|
startFrom: "",
|
|
633
|
-
limit: 0
|
|
639
|
+
limit: 0,
|
|
640
|
+
changedSinceToken: /* @__PURE__ */ new Uint8Array(0)
|
|
634
641
|
}
|
|
635
642
|
}, (r) => r.map((e) => e.resourceKeyValueList.record));
|
|
636
643
|
this._stat.kvListRequests++;
|
|
@@ -742,6 +749,20 @@ var PlTransaction = class PlTransaction {
|
|
|
742
749
|
this.globalTxIdWasAwaited = true;
|
|
743
750
|
return await this.globalTxId;
|
|
744
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* The change token this transaction was opened at, to hand back as
|
|
754
|
+
* {@link resourceTree}'s `changedSinceToken` on a later poll. `undefined` when the server
|
|
755
|
+
* does not advertise `treeChangedSince:v1`, and on a writable transaction, which is served
|
|
756
|
+
* none.
|
|
757
|
+
*
|
|
758
|
+
* Store it only after a whole response has been applied - kept after a partial apply, the
|
|
759
|
+
* dropped resources are never sent again - and discard it when the traversal shape changes,
|
|
760
|
+
* which the token cannot express itself. Opaque: never parse one, and never compare across
|
|
761
|
+
* instances.
|
|
762
|
+
*/
|
|
763
|
+
async getNextSinceToken() {
|
|
764
|
+
return (await this.txOpen).sinceToken;
|
|
765
|
+
}
|
|
745
766
|
/** Closes output event stream */
|
|
746
767
|
async complete() {
|
|
747
768
|
if (this._completed) return;
|
|
@@ -773,8 +794,18 @@ var PlTransaction = class PlTransaction {
|
|
|
773
794
|
* with `traverseWasStopped = true` and their children are not visited.
|
|
774
795
|
* This wire field accepts only one filter. Compose multiple rules via
|
|
775
796
|
* `treeFilter.and(...)` / `treeFilter.or(...)`.
|
|
797
|
+
* Deprecated, and ignored under `changedSinceToken`: a stop rule prunes on mutable state,
|
|
798
|
+
* so on a delta walk it would hide a changed subtree behind a resource that merely
|
|
799
|
+
* flipped a gate.
|
|
776
800
|
* @param opts.includeKv - When true, each yielded item includes KV entries.
|
|
777
801
|
* @param opts.maxDepth - Optional depth cap.
|
|
802
|
+
* @param opts.changedSinceToken - A token from {@link getNextSinceToken}. Yields only
|
|
803
|
+
* resources newer than it, and the walk ENDS at an unchanged one, so a change beneath one
|
|
804
|
+
* is not reached. A response may reference resources it does not carry; read those back
|
|
805
|
+
* with `unconditionalDepth`. An unusable token is answered with the full tree, not an error.
|
|
806
|
+
* @param opts.unconditionalDepth - Emit every resource at or below this depth whatever its
|
|
807
|
+
* change token says, the seed being depth 0. Ignored without `changedSinceToken`. Absent
|
|
808
|
+
* differs from 0, which takes the seeds alone.
|
|
778
809
|
*/
|
|
779
810
|
resourceTree(seeds, opts) {
|
|
780
811
|
if (seeds.length === 0) throw new Error("resourceTree: at least one seed must be provided");
|
|
@@ -797,7 +828,8 @@ var PlTransaction = class PlTransaction {
|
|
|
797
828
|
includeKv: opts?.includeKv ?? false,
|
|
798
829
|
maxDepth: opts?.maxDepth,
|
|
799
830
|
showSoftDeletes: false,
|
|
800
|
-
changedSinceToken: /* @__PURE__ */ new Uint8Array(0)
|
|
831
|
+
changedSinceToken: opts?.changedSinceToken ?? /* @__PURE__ */ new Uint8Array(0),
|
|
832
|
+
unconditionalDepth: opts?.unconditionalDepth
|
|
801
833
|
}
|
|
802
834
|
});
|
|
803
835
|
return { [Symbol.asyncIterator]() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction.cjs","names":["isSignedResourceId","AuthAPI_GrantAccess_GrantType","AuthAPI_Role","toResourceSignature","parseSignedResourceId","isLocalResourceId","createSignedResourceId","isNotFoundError","PromiseTracker","initialTxStatWithoutTime","ensureSignedResourceIdNotNull","protoToResource","JsonObject","JsonGzObject","ErrorResourceType","extractBasicResourceData","fieldTypeToProto","protoToField","toBytes","resourceIsDeleted","createLocalResourceId"],"sources":["../../src/core/transaction.ts"],"sourcesContent":["import type {\n ColorProof,\n LocalResourceId,\n OptionalSignedResourceId,\n BasicResourceData,\n FieldData,\n FieldType,\n ResourceData,\n SignedResourceId,\n ResourceSignature,\n ResourceType,\n FutureFieldType,\n} from \"./types\";\nimport {\n createSignedResourceId,\n createLocalResourceId,\n isLocalResourceId,\n ensureSignedResourceIdNotNull,\n MaxTxId,\n extractBasicResourceData,\n parseSignedResourceId,\n toResourceSignature,\n isSignedResourceId,\n} from \"./types\";\nimport type {\n ClientMessageRequest,\n LLPlTransaction,\n OneOfKind,\n ServerMessageResponse,\n} from \"./ll_transaction\";\nimport type {\n AuthAPI_Grant,\n ResourceAPI_Tree_Filter,\n ResourceAPI_Tree_SeedResource,\n} from \"../proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api\";\nimport {\n TxAPI_Open_Request_WritableTx,\n AuthAPI_GrantAccess_GrantType,\n AuthAPI_Role,\n} from \"../proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api\";\nimport type { NonUndefined } from \"utility-types\";\nimport { toBytes } from \"../util/util\";\nimport {\n fieldTypeToProto,\n protoToField,\n protoToResource,\n resourceIsDeleted,\n} from \"./type_conversion\";\nimport {\n canonicalJsonBytes,\n canonicalJsonGzBytes,\n deepFreeze,\n notEmpty,\n} from \"@milaboratories/ts-helpers\";\nimport { isNotFoundError } from \"./errors\";\nimport type { FinalResourceDataPredicate } from \"./final\";\nimport type { LRUCache } from \"lru-cache\";\nimport type { ResourceDataCacheRecord } from \"./cache\";\nimport type { TxStat } from \"./stat\";\nimport { initialTxStatWithoutTime } from \"./stat\";\nimport type { ErrorResourceData } from \"./error_resource\";\nimport { ErrorResourceType } from \"./error_resource\";\nimport { JsonGzObject, JsonObject } from \"../helpers/pl\";\nimport { PromiseTracker } from \"./PromiseTracker\";\n\n/** Reference to resource, used only within transaction */\nexport interface ResourceRef {\n /** Global resource id of newly created resources, become available only\n * after response for the corresponding creation request is received. */\n readonly globalId: Promise<SignedResourceId>;\n\n /** Transaction-local resource id is assigned right after resource creation\n * request is sent, and can be used right away */\n readonly localId: LocalResourceId;\n}\n\n/** Key-Value pair from resource-attached KV storage */\nexport interface KeyValue {\n key: string;\n value: Uint8Array;\n}\n\n/** Key-Value pair from resource-attached KV storage */\nexport interface KeyValueString {\n key: string;\n value: string;\n}\n\nexport type ResourceTreeItem = ResourceData & { kv: KeyValue[]; traverseWasStopped: boolean };\n\n/** A single frame from the resourceTree() stream.\n *\n * When the server advertises `treeStopMarker:v1`, a stop-matched node is emitted\n * as a lightweight marker instead of a full resource payload. Callers must\n * narrow on `frameKind` before accessing type-specific fields.\n * Note: `frameKind` is distinct from `ResourceData.kind` (which is the resource kind).\n */\nexport type ResourceTreeFrame =\n | (ResourceTreeItem & { frameKind: \"resource\" })\n | { frameKind: \"stopMarker\"; id: SignedResourceId; traverseWasStopped: true };\n\ninterface _FieldId<RId> {\n /** Parent resource id */\n resourceId: RId;\n /** Field name */\n fieldName: string;\n}\n\nexport type FieldId = _FieldId<SignedResourceId>;\nexport type FieldRef = _FieldId<ResourceRef>;\nexport type LocalFieldId = _FieldId<LocalResourceId>;\nexport type AnyFieldId = FieldId | LocalFieldId;\n\nexport type AnyResourceRef = ResourceRef | LocalResourceId | SignedResourceId;\nexport type AnyFieldRef = _FieldId<AnyResourceRef>; // FieldRef | FieldId\nexport type AnyRef = AnyResourceRef | AnyFieldRef;\n\nexport function isField(ref: AnyRef): ref is AnyFieldRef {\n return ref.hasOwnProperty(\"resourceId\") && ref.hasOwnProperty(\"fieldName\");\n}\n\nexport function isResource(ref: AnyRef): ref is AnyResourceRef {\n if (typeof ref === \"bigint\") return true; // LocalResourceId\n if (typeof ref === \"string\") return true; // SignedResourceId\n return isResourceRef(ref);\n}\n\nexport function isResourceId(ref: AnyRef): ref is SignedResourceId {\n return typeof ref === \"string\" && isSignedResourceId(ref);\n}\n\nexport function isFieldRef(ref: AnyFieldRef): ref is FieldRef {\n return isResourceRef(ref.resourceId);\n}\n\nexport function isResourceRef(ref: AnyRef): ref is ResourceRef {\n return (\n typeof ref === \"object\" &&\n ref !== null &&\n ref.hasOwnProperty(\"globalId\") &&\n ref.hasOwnProperty(\"localId\")\n );\n}\n\nexport function toFieldId(ref: AnyFieldRef): AnyFieldId {\n if (isFieldRef(ref)) return { resourceId: ref.resourceId.localId, fieldName: ref.fieldName };\n else return ref as FieldId;\n}\n\nexport async function toGlobalFieldId(ref: FieldRef | FieldId): Promise<FieldId> {\n if (isFieldRef(ref))\n return { resourceId: await ref.resourceId.globalId, fieldName: ref.fieldName };\n return ref;\n}\n\nexport interface ResourceIdWithSignature {\n resourceId: bigint;\n resourceSignature: ResourceSignature;\n}\n\n/** Access level handed to a grant recipient (see {@link PlTransaction.grantAccess}). */\nexport interface GrantPermissions {\n /** Write access = ability to modify the resource subtree + re-share it. */\n writable: boolean;\n}\n\n/**\n * Distinguishes a regular user-to-user grant from a system-level public grant.\n * Re-export of the wire enum so callers need not reach into the generated proto.\n */\nexport const GrantType = AuthAPI_GrantAccess_GrantType;\nexport type GrantType = AuthAPI_GrantAccess_GrantType;\n\n/**\n * Effective role of an authenticated session (controller / workflow / user / admin).\n * Re-export of the wire enum so callers need not reach into the generated proto.\n * Returned by {@link LLPlClient.getSessionInfo} and surfaced as\n * {@link PlClient.currentUserRole}.\n */\nexport const Role = AuthAPI_Role;\nexport type Role = AuthAPI_Role;\n\n/**\n * Recognises the \"all users on the server\" sentinel login. A public\n * ({@link GrantType.ANY_AUTHORISED}) grant is recorded against a reserved login, and\n * recipients of an everyone-grant surface in `ListGrants` with that value — callers\n * detect it here and map it to \"*\".\n *\n * Matched by exact token equality. The long random token guarantees no real user login\n * collides with it.\n */\nconst EveryoneUser = \"everyone-Po9ahwahxai7Aejingaiyiequuecu3ei4moaNge4xahTh0Co7XeeLeiph6ahy3As\";\nexport function isEveryoneUserLogin(login: string): boolean {\n return login === EveryoneUser;\n}\n\nconst emptySignature = toResourceSignature(new Uint8Array(0));\n\nfunction toResourceIdAndSignature(ref: AnyResourceRef): ResourceIdWithSignature {\n if (isResourceRef(ref)) {\n // Local ID — no signature yet\n return { resourceId: ref.localId, resourceSignature: emptySignature };\n }\n if (isResourceId(ref)) {\n // SignedResourceId — decompose\n const { globalId, signature } = parseSignedResourceId(ref);\n return { resourceId: globalId as bigint, resourceSignature: signature };\n }\n // Raw bigint (LocalResourceId)\n return { resourceId: ref as bigint, resourceSignature: emptySignature };\n}\n\nexport async function toGlobalResourceId(ref: AnyResourceRef): Promise<SignedResourceId> {\n if (isResourceRef(ref)) return await ref.globalId;\n if (isLocalResourceId(ref)) return createSignedResourceId(ref); // legacy path: loose security mode in backend\n return ref;\n}\n\nexport function field(resourceId: AnyResourceRef, fieldName: string): AnyFieldRef {\n return { resourceId, fieldName };\n}\n\n/** If transaction commit failed due to write conflicts */\nexport class TxCommitConflict extends Error {\n name = \"TxCommitConflict\";\n}\n\nasync function notFoundToUndefined<T>(cb: () => Promise<T>): Promise<T | undefined> {\n try {\n return await cb();\n } catch (e) {\n if (isNotFoundError(e)) return undefined;\n throw e;\n }\n}\n\n/**\n * Each platform transaction has 3 stages:\n * - initialization (txOpen message -> txInfo response)\n * - communication (create resources, fields, references and so on)\n * - finalization (txCommit or txDiscard message)\n *\n * This class encapsulates finalization stage and provides ready-to-communication transaction object.\n * */\nexport class PlTransaction {\n private readonly globalTxId: Promise<bigint>;\n private readonly localTxId: number = PlTransaction.nextLocalTxId();\n\n /** Used in caching */\n private readonly txOpenTimestamp = Date.now();\n\n private localResourceIdCounter = 0;\n\n /** Store logical tx open / closed state to prevent invalid sequence of requests.\n * True means output stream was completed.\n * Contract: there must be no async operations between setting this field to true and sending complete signal to stream. */\n private _completed = false;\n\n private globalTxIdWasAwaited: boolean = false;\n\n public readonly pending = new PromiseTracker();\n\n private readonly _startTime = Date.now();\n private readonly _stat = initialTxStatWithoutTime();\n public get stat(): TxStat {\n return {\n ...this._stat,\n timeMs: Date.now() - this._startTime,\n };\n }\n\n constructor(\n private readonly ll: LLPlTransaction,\n public readonly name: string,\n public readonly writable: boolean,\n private readonly _clientRoot: OptionalSignedResourceId,\n private readonly finalPredicate: FinalResourceDataPredicate,\n private readonly sharedResourceDataCache: LRUCache<SignedResourceId, ResourceDataCacheRecord>,\n private readonly enableFormattedErrors: boolean = false,\n ) {\n // initiating transaction\n this.globalTxId = this.sendSingleAndParse(\n {\n oneofKind: \"txOpen\",\n txOpen: {\n name,\n enableFormattedErrors,\n writable: writable\n ? TxAPI_Open_Request_WritableTx.WRITABLE\n : TxAPI_Open_Request_WritableTx.NOT_WRITABLE,\n },\n },\n (r) => notEmpty(r.txOpen.tx?.id),\n );\n\n void this.track(this.globalTxId);\n\n // To avoid floating promise\n this.globalTxId.catch((err) => {\n if (!this.globalTxIdWasAwaited) {\n console.warn(err);\n }\n });\n\n // Adding stats\n this._stat.txCount++;\n }\n\n /**\n * Collect all pending promises for the transaction finalization.\n */\n public track<T>(promiseOrCallback: Promise<T> | (() => Promise<T>)): Promise<T> {\n return this.pending.track(promiseOrCallback);\n }\n\n private async drainAndAwaitPendingOps(): Promise<void> {\n // awaiting these pending operations first, to catch any errors\n await this.pending.awaitAll();\n }\n\n private sendSingleAndParse<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>, T>(\n r: OneOfKind<ClientMessageRequest, Kind>,\n parser: (resp: OneOfKind<ServerMessageResponse, Kind>) => T,\n ): Promise<T> {\n return this.pending.track(async () => {\n const rawResponsePromise = this.ll.send(r, false);\n\n void this.pending.track(rawResponsePromise);\n\n await this.drainAndAwaitPendingOps();\n\n // awaiting our result, and parsing the response\n return parser(await rawResponsePromise);\n });\n }\n\n private sendMultiAndParse<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>, T>(\n r: OneOfKind<ClientMessageRequest, Kind>,\n parser: (resp: OneOfKind<ServerMessageResponse, Kind>[]) => T,\n ): Promise<T> {\n return this.pending.track(async () => {\n const rawResponsePromise = this.ll.send(r, true);\n\n void this.pending.track(rawResponsePromise);\n\n await this.drainAndAwaitPendingOps();\n\n // awaiting our result, and parsing the response\n return parser(await rawResponsePromise);\n });\n }\n\n private async sendVoidSync<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>>(\n r: OneOfKind<ClientMessageRequest, Kind>,\n ): Promise<void> {\n await this.ll.send(r, false);\n }\n\n /** Requests sent with this method should never produce recoverable errors */\n private sendVoidAsync<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>>(\n r: OneOfKind<ClientMessageRequest, Kind>,\n ): void {\n void this.track(this.sendVoidSync(r));\n }\n\n /** Set default color proof for subsequent resource creation requests */\n public setDefaultColor(colorProof: ColorProof): void {\n this.sendVoidAsync({\n oneofKind: \"setDefaultColor\",\n setDefaultColor: { colorProof },\n });\n }\n\n private toSignedResourceId(rId: AnyResourceRef): ResourceIdWithSignature {\n return toResourceIdAndSignature(rId);\n }\n\n private toSignedFieldId(fId: AnyFieldRef): {\n resourceId: bigint;\n resourceSignature: ResourceSignature;\n fieldName: string;\n } {\n const base = toFieldId(fId);\n const signed = toResourceIdAndSignature(base.resourceId);\n return {\n resourceId: signed.resourceId,\n fieldName: base.fieldName,\n resourceSignature: signed.resourceSignature,\n };\n }\n\n private toSignedErrorRef(rId: AnyResourceRef): {\n errorResourceId: bigint;\n errorResourceSignature: ResourceSignature;\n } {\n const { resourceId, resourceSignature } = this.toSignedResourceId(rId);\n return { errorResourceId: resourceId, errorResourceSignature: resourceSignature };\n }\n\n private checkTxOpen() {\n if (this._completed) throw new Error(\"Transaction already closed\");\n }\n\n public get completed() {\n return this._completed;\n }\n\n /** Commit & closes transaction. {@link TxCommitConflict} is thrown on\n * commit conflicts. */\n public async commit() {\n this.checkTxOpen();\n\n // tx will accept no requests after this one\n this._completed = true;\n\n if (!this.writable) {\n // no need to explicitly commit or reject read-only tx\n const completeResult = this.track(this.ll.complete());\n await this.drainAndAwaitPendingOps();\n await completeResult;\n await this.ll.await();\n } else {\n const commitResponse = this.track(\n this.sendSingleAndParse({ oneofKind: \"txCommit\", txCommit: {} }, (r) => r.txCommit.success),\n );\n\n // send closing frame right after commit to save some time on round-trips\n const completeResult = this.track(this.ll.complete());\n\n // now when we pushed all packets into the stream, we should wait for any\n // pending void operations from before, to catch any errors\n await this.drainAndAwaitPendingOps();\n\n if (!(await commitResponse)) throw new TxCommitConflict();\n\n await completeResult;\n\n // await event-loop completion\n await this.ll.await();\n }\n }\n\n public async discard() {\n this.checkTxOpen();\n\n // tx will accept no requests after this one\n this._completed = true;\n\n const discardResponse = this.sendVoidSync({ oneofKind: \"txDiscard\", txDiscard: {} });\n void this.track(discardResponse);\n // send closing frame right after commit to save some time on round-trips\n const completeResult = this.track(this.ll.complete());\n\n // now when we pushed all packets into the stream, we should wait for any\n // pending void operations from before, to catch any errors\n await this.drainAndAwaitPendingOps();\n\n await discardResponse;\n await completeResult;\n await this.ll.await();\n }\n\n //\n // Main tx methods\n //\n\n public get clientRoot(): SignedResourceId {\n return ensureSignedResourceIdNotNull(this._clientRoot);\n }\n\n //\n // Resources\n //\n\n public createSingleton(\n name: string,\n type: ResourceType,\n errorIfExists: boolean = false,\n color?: ColorProof,\n ): ResourceRef {\n return this.createResource(\n false,\n (localId) => ({\n oneofKind: \"resourceCreateSingleton\",\n resourceCreateSingleton: {\n type,\n id: localId,\n data: Buffer.from(name),\n errorIfExists,\n colorProof: color ?? emptySignature,\n },\n }),\n (r) => r.resourceCreateSingleton.resourceId,\n (r) => r.resourceCreateSingleton.resourceSignature,\n );\n }\n\n public getSingleton(name: string, loadFields: true): Promise<ResourceData>;\n public getSingleton(name: string, loadFields: false): Promise<BasicResourceData>;\n public getSingleton(\n name: string,\n loadFields: boolean = true,\n ): Promise<BasicResourceData | ResourceData> {\n return this.sendSingleAndParse(\n {\n oneofKind: \"resourceGetSingleton\",\n resourceGetSingleton: {\n data: Buffer.from(name),\n loadFields,\n },\n },\n (r) => protoToResource(notEmpty(r.resourceGetSingleton.resource)),\n );\n }\n\n private createResource<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>>(\n root: boolean,\n req: (localId: LocalResourceId) => OneOfKind<ClientMessageRequest, Kind>,\n parser: (resp: OneOfKind<ServerMessageResponse, Kind>) => bigint,\n sigExtractor: (resp: OneOfKind<ServerMessageResponse, Kind>) => Uint8Array | undefined,\n ): ResourceRef {\n const localId = this.nextLocalResourceId(root);\n\n const globalId = this.sendSingleAndParse(req(localId), (r) => {\n const rawId = parser(r);\n const sig = sigExtractor ? toResourceSignature(sigExtractor(r)) : undefined;\n return createSignedResourceId(rawId, sig);\n });\n\n void this.track(globalId);\n\n return { globalId, localId };\n }\n\n public createRoot(type: ResourceType): ResourceRef {\n this._stat.rootsCreated++;\n return this.createResource(\n true,\n (localId) => ({ oneofKind: \"resourceCreateRoot\", resourceCreateRoot: { type, id: localId } }),\n (r) => r.resourceCreateRoot.resourceId,\n (r) => r.resourceCreateRoot.resourceSignature,\n );\n }\n\n public createStruct(\n type: ResourceType,\n data?: Uint8Array | string,\n color?: ColorProof,\n ): ResourceRef {\n this._stat.structsCreated++;\n this._stat.structsCreatedDataBytes += data?.length ?? 0;\n return this.createResource(\n false,\n (localId) => ({\n oneofKind: \"resourceCreateStruct\",\n resourceCreateStruct: {\n type,\n id: localId,\n data:\n data === undefined ? undefined : typeof data === \"string\" ? Buffer.from(data) : data,\n colorProof: color ?? emptySignature,\n },\n }),\n (r) => r.resourceCreateStruct.resourceId,\n (r) => r.resourceCreateStruct.resourceSignature,\n );\n }\n\n public createEphemeral(\n type: ResourceType,\n data?: Uint8Array | string,\n color?: ColorProof,\n ): ResourceRef {\n this._stat.ephemeralsCreated++;\n this._stat.ephemeralsCreatedDataBytes += data?.length ?? 0;\n return this.createResource(\n false,\n (localId) => ({\n oneofKind: \"resourceCreateEphemeral\",\n resourceCreateEphemeral: {\n type,\n id: localId,\n data:\n data === undefined ? undefined : typeof data === \"string\" ? Buffer.from(data) : data,\n colorProof: color ?? emptySignature,\n },\n }),\n (r) => r.resourceCreateEphemeral.resourceId,\n (r) => r.resourceCreateEphemeral.resourceSignature,\n );\n }\n\n public createValue(\n type: ResourceType,\n data: Uint8Array | string,\n errorIfExists: boolean = false,\n color?: ColorProof,\n ): ResourceRef {\n this._stat.valuesCreated++;\n this._stat.valuesCreatedDataBytes += data?.length ?? 0;\n return this.createResource(\n false,\n (localId) => ({\n oneofKind: \"resourceCreateValue\",\n resourceCreateValue: {\n type,\n id: localId,\n data: typeof data === \"string\" ? Buffer.from(data) : data,\n errorIfExists,\n colorProof: color ?? emptySignature,\n },\n }),\n (r) => r.resourceCreateValue.resourceId,\n (r) => r.resourceCreateValue.resourceSignature,\n );\n }\n\n public createJsonValue(data: unknown, color?: ColorProof): ResourceRef {\n const jsonData = canonicalJsonBytes(data);\n return this.createValue(JsonObject, jsonData, false, color);\n }\n\n public createJsonGzValue(\n data: unknown,\n minSizeToGzip: number | undefined = 16_384,\n color?: ColorProof,\n ): ResourceRef {\n const { data: jsonData, isGzipped } = canonicalJsonGzBytes(data, minSizeToGzip);\n return this.createValue(isGzipped ? JsonGzObject : JsonObject, jsonData, false, color);\n }\n\n public createError(message: string, color?: ColorProof): ResourceRef {\n return this.createValue(\n ErrorResourceType,\n JSON.stringify({ message } satisfies ErrorResourceData),\n false,\n color,\n );\n }\n\n public setResourceName(name: string, rId: AnyResourceRef): void {\n this.sendVoidAsync({\n oneofKind: \"resourceNameSet\",\n resourceNameSet: { ...this.toSignedResourceId(rId), name },\n });\n }\n\n public deleteResourceName(name: string): void {\n this.sendVoidAsync({ oneofKind: \"resourceNameDelete\", resourceNameDelete: { name } });\n }\n\n public getResourceByName(name: string): Promise<SignedResourceId> {\n return this.sendSingleAndParse(\n { oneofKind: \"resourceNameGet\", resourceNameGet: { name } },\n (r) => {\n const rawId = r.resourceNameGet.resourceId;\n if (rawId === 0n) throw new Error(\"null resource id from getResourceByName\");\n const sig = toResourceSignature(r.resourceNameGet.resourceSignature);\n return createSignedResourceId(rawId, sig);\n },\n );\n }\n\n public checkResourceNameExists(name: string): Promise<boolean> {\n return this.sendSingleAndParse(\n { oneofKind: \"resourceNameExists\", resourceNameExists: { name } },\n (r) => r.resourceNameExists.exists,\n );\n }\n\n public removeResource(rId: SignedResourceId): void {\n this.sendVoidAsync({\n oneofKind: \"resourceRemove\",\n resourceRemove: this.toSignedResourceId(rId),\n });\n }\n\n public resourceExists(rId: SignedResourceId): Promise<boolean> {\n return this.sendSingleAndParse(\n {\n oneofKind: \"resourceExists\",\n resourceExists: this.toSignedResourceId(rId),\n },\n (r) => r.resourceExists.exists,\n );\n }\n\n /** This method may return stale resource state from cache if resource was removed */\n public async getResourceData(rId: AnyResourceRef, loadFields: true): Promise<ResourceData>;\n /** This method may return stale resource state from cache if resource was removed */\n public async getResourceData(rId: AnyResourceRef, loadFields: false): Promise<BasicResourceData>;\n /** This method may return stale resource state from cache if resource was removed */\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: boolean,\n ): Promise<BasicResourceData | ResourceData>;\n /** This method may return stale resource state from cache if ignoreCache == false if resource was removed */\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: true,\n ignoreCache: boolean,\n ): Promise<ResourceData>;\n /** This method may return stale resource state from cache if ignoreCache == false if resource was removed */\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: false,\n ignoreCache: boolean,\n ): Promise<BasicResourceData>;\n /** This method may return stale resource state from cache if ignoreCache == false if resource was removed */\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: boolean,\n ignoreCache: boolean,\n ): Promise<BasicResourceData | ResourceData>;\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: boolean = true,\n ignoreCache: boolean = false,\n ): Promise<BasicResourceData | ResourceData> {\n return this.track(async () => {\n if (!ignoreCache && isResourceId(rId)) {\n // checking if we can return result from cache\n const fromCache = this.sharedResourceDataCache.get(rId);\n if (fromCache && fromCache.cacheTxOpenTimestamp < this.txOpenTimestamp) {\n if (!loadFields) {\n this._stat.rGetDataCacheHits++;\n this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;\n return fromCache.basicData;\n } else if (fromCache.data) {\n this._stat.rGetDataCacheHits++;\n this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;\n this._stat.rGetDataCacheFields += fromCache.data.fields.length;\n return fromCache.data;\n }\n }\n }\n\n const result = await this.sendSingleAndParse(\n {\n oneofKind: \"resourceGet\",\n resourceGet: {\n ...this.toSignedResourceId(rId),\n loadFields: loadFields,\n showSoftDeletes: false,\n },\n },\n (r) => protoToResource(notEmpty(r.resourceGet.resource)),\n );\n\n this._stat.rGetDataNetRequests++;\n this._stat.rGetDataNetBytes += result.data?.length ?? 0;\n this._stat.rGetDataNetFields += result.fields.length;\n\n // we will cache only final resource data states\n // caching result even if we were ignore the cache\n if (isResourceId(rId) && this.finalPredicate(result)) {\n deepFreeze(result);\n const fromCache = this.sharedResourceDataCache.get(rId);\n if (fromCache) {\n if (loadFields && !fromCache.data) {\n fromCache.data = result;\n // updating timestamp because we updated the record\n fromCache.cacheTxOpenTimestamp = this.txOpenTimestamp;\n }\n } else {\n const basicData = extractBasicResourceData(result);\n deepFreeze(basicData);\n if (loadFields)\n this.sharedResourceDataCache.set(rId, {\n basicData,\n data: result,\n cacheTxOpenTimestamp: this.txOpenTimestamp,\n });\n else\n this.sharedResourceDataCache.set(rId, {\n basicData,\n data: undefined,\n cacheTxOpenTimestamp: this.txOpenTimestamp,\n });\n }\n }\n\n return result;\n });\n }\n\n public async getResourceDataIfExists(\n rId: AnyResourceRef,\n loadFields: true,\n ): Promise<ResourceData | undefined>;\n public async getResourceDataIfExists(\n rId: AnyResourceRef,\n loadFields: false,\n ): Promise<BasicResourceData | undefined>;\n public async getResourceDataIfExists(\n rId: AnyResourceRef,\n loadFields: boolean,\n ): Promise<BasicResourceData | ResourceData | undefined>;\n public async getResourceDataIfExists(\n rId: AnyResourceRef,\n loadFields: boolean = true,\n ): Promise<BasicResourceData | ResourceData | undefined> {\n return this.track(async () => {\n // calling this method will ignore cache, because user intention is to detect resource absence\n // which cache will prevent\n const result = await notFoundToUndefined(\n async () => await this.getResourceData(rId, loadFields, true),\n );\n\n // cleaning cache record if resource was removed from the db\n if (result === undefined && isResourceId(rId)) this.sharedResourceDataCache.delete(rId);\n\n return result;\n });\n }\n\n /**\n * Inform platform that resource will not get any new input fields.\n * This is required, when client creates resource without schema and wants\n * controller to start calculations.\n * Most controllers will not start calculations even when all inputs\n * have their values, if inputs list is not locked.\n */\n public lockInputs(rId: AnyResourceRef): void {\n this._stat.inputsLocked++;\n this.sendVoidAsync({\n oneofKind: \"resourceLockInputs\",\n resourceLockInputs: this.toSignedResourceId(rId),\n });\n }\n\n /**\n * Inform platform that resource will not get any new output fields.\n * This is required for resource to pass deduplication.\n */\n public lockOutputs(rId: AnyResourceRef): void {\n this._stat.outputsLocked++;\n this.sendVoidAsync({\n oneofKind: \"resourceLockOutputs\",\n resourceLockOutputs: this.toSignedResourceId(rId),\n });\n }\n\n public lock(rID: AnyResourceRef): void {\n this.lockInputs(rID);\n this.lockOutputs(rID);\n }\n\n public setResourceError(rId: AnyResourceRef, ref: AnyResourceRef): void {\n this.sendVoidAsync({\n oneofKind: \"resourceSetError\",\n resourceSetError: {\n ...this.toSignedResourceId(rId),\n ...this.toSignedErrorRef(ref),\n },\n });\n }\n\n /**\n * Grant another user access to a resource (and its subtree). Used by the\n * project-sharing donor flow to hand a recipient the shared envelope.\n *\n * `grantType` defaults to a regular user-to-user grant\n * ({@link GrantType.SINGLE_USER}); pass {@link GrantType.ANY_AUTHORISED}\n * to make the resource public to everyone (requires backend `publicGrants:v1`\n * and a role allowed to grant to everyone — `target` is ignored / rewritten\n * to the everyone-user by the backend).\n */\n public grantAccess(\n rId: AnyResourceRef,\n target: string,\n permissions: GrantPermissions,\n grantType: GrantType = AuthAPI_GrantAccess_GrantType.SINGLE_USER,\n ): void {\n this.sendVoidAsync({\n oneofKind: \"grantAccess\",\n grantAccess: {\n ...this.toSignedResourceId(rId),\n targetUser: target,\n permissions,\n grantType,\n },\n });\n }\n\n /**\n * Revoke a single user's grant on a resource — the inverse of {@link grantAccess}.\n * Used by the sharing donor flow to pull one recipient out of a multi-recipient\n * envelope without tearing the whole share down. A no-op on the backend if the user\n * has no grant.\n */\n public revokeAccess(rId: AnyResourceRef, target: string): void {\n this.sendVoidAsync({\n oneofKind: \"revokeAccess\",\n revokeAccess: {\n ...this.toSignedResourceId(rId),\n targetUser: target,\n },\n });\n }\n\n /**\n * Enumerate the grants of a resource — the donor-side \"who did I share with\" view.\n * The backend gates this on the signed, writable resource handle, so only the\n * resource's owner can read its grants. Recipients of a public\n * ({@link GrantType.ANY_AUTHORISED}) grant surface with `user` matching the\n * everyone-sentinel — test with {@link isEveryoneUserLogin}.\n *\n * Transactional unary op (single response with all grants), so it rides the tx\n * channel and works on every wire protocol — unlike the standalone server-streaming\n * `ListGrants` RPC, which has no REST binding.\n */\n public listGrants(rId: AnyResourceRef): Promise<AuthAPI_Grant[]> {\n return this.sendSingleAndParse(\n { oneofKind: \"listGrants\", listGrants: this.toSignedResourceId(rId) },\n (r) => r.listGrants.grants,\n );\n }\n\n //\n // Fields\n //\n\n public createField(fId: AnyFieldRef, fieldType: FieldType, value?: AnyRef): void {\n this._stat.fieldsCreated++;\n this.sendVoidAsync({\n oneofKind: \"fieldCreate\",\n fieldCreate: { type: fieldTypeToProto(fieldType), id: this.toSignedFieldId(fId) },\n });\n if (value !== undefined) this.setField(fId, value);\n }\n\n public fieldExists(fId: AnyFieldRef): Promise<boolean> {\n return this.sendSingleAndParse(\n {\n oneofKind: \"fieldExists\",\n fieldExists: { field: this.toSignedFieldId(fId) },\n },\n (r) => r.fieldExists.exists,\n );\n }\n\n public setField(fId: AnyFieldRef, ref: AnyRef): void {\n this._stat.fieldsSet++;\n if (isResource(ref)) {\n this.sendVoidAsync({\n oneofKind: \"fieldSet\",\n fieldSet: {\n field: this.toSignedFieldId(fId),\n value: {\n ...this.toSignedResourceId(ref),\n fieldName: \"\", // default value, read as undefined\n },\n },\n });\n } else {\n this.sendVoidAsync({\n oneofKind: \"fieldSet\",\n fieldSet: {\n field: this.toSignedFieldId(fId),\n value: this.toSignedFieldId(ref),\n },\n });\n }\n }\n\n public setFieldError(fId: AnyFieldRef, ref: AnyResourceRef): void {\n this._stat.fieldsSet++;\n this.sendVoidAsync({\n oneofKind: \"fieldSetError\",\n fieldSetError: {\n field: this.toSignedFieldId(fId),\n ...this.toSignedErrorRef(ref),\n },\n });\n }\n\n public getField(fId: AnyFieldRef): Promise<FieldData> {\n this._stat.fieldsGet++;\n return this.sendSingleAndParse(\n { oneofKind: \"fieldGet\", fieldGet: { field: this.toSignedFieldId(fId) } },\n (r) => protoToField(notEmpty(r.fieldGet.field)),\n );\n }\n\n public async getFieldIfExists(fId: AnyFieldRef): Promise<FieldData | undefined> {\n return this.track(notFoundToUndefined(async () => await this.getField(fId)));\n }\n\n public resetField(fId: AnyFieldRef): void {\n this.sendVoidAsync({\n oneofKind: \"fieldReset\",\n fieldReset: { field: this.toSignedFieldId(fId) },\n });\n }\n\n public removeField(fId: AnyFieldRef): void {\n this.sendVoidAsync({\n oneofKind: \"fieldRemove\",\n fieldRemove: { field: this.toSignedFieldId(fId) },\n });\n }\n\n //\n // KV\n //\n\n public async listKeyValues(rId: AnyResourceRef): Promise<KeyValue[]> {\n return this.track(async () => {\n const result = await this.sendMultiAndParse(\n {\n oneofKind: \"resourceKeyValueList\",\n resourceKeyValueList: {\n ...this.toSignedResourceId(rId),\n startFrom: \"\",\n limit: 0,\n },\n },\n (r) => r.map((e) => e.resourceKeyValueList.record!),\n );\n\n this._stat.kvListRequests++;\n this._stat.kvListEntries += result.length;\n for (const kv of result) this._stat.kvListBytes += kv.key.length + kv.value.length;\n\n return result;\n });\n }\n\n public async listKeyValuesString(rId: AnyResourceRef): Promise<KeyValueString[]> {\n return this.track(async () =>\n (await this.listKeyValues(rId)).map(({ key, value }) => ({\n key,\n value: Buffer.from(value).toString(),\n })),\n );\n }\n\n public async listKeyValuesIfResourceExists(rId: AnyResourceRef): Promise<KeyValue[] | undefined> {\n return this.track(notFoundToUndefined(async () => await this.listKeyValues(rId)));\n }\n\n public async listKeyValuesStringIfResourceExists(\n rId: AnyResourceRef,\n ): Promise<KeyValueString[] | undefined> {\n return this.track(notFoundToUndefined(async () => await this.listKeyValuesString(rId)));\n }\n\n public setKValue(rId: AnyResourceRef, key: string, value: Uint8Array | string): void {\n this._stat.kvSetRequests++;\n this._stat.kvSetBytes++;\n this.sendVoidAsync({\n oneofKind: \"resourceKeyValueSet\",\n resourceKeyValueSet: {\n ...this.toSignedResourceId(rId),\n key,\n value: toBytes(value),\n },\n });\n }\n\n public deleteKValue(rId: AnyResourceRef, key: string): void {\n this.sendVoidAsync({\n oneofKind: \"resourceKeyValueDelete\",\n resourceKeyValueDelete: {\n ...this.toSignedResourceId(rId),\n key,\n },\n });\n }\n\n public async getKValue(rId: AnyResourceRef, key: string): Promise<Uint8Array> {\n return this.track(async () => {\n const result = await this.sendSingleAndParse(\n {\n oneofKind: \"resourceKeyValueGet\",\n resourceKeyValueGet: {\n ...this.toSignedResourceId(rId),\n key,\n },\n },\n (r) => r.resourceKeyValueGet.value,\n );\n\n this._stat.kvGetRequests++;\n this._stat.kvGetBytes += result.length;\n\n return result;\n });\n }\n\n public async getKValueString(rId: AnyResourceRef, key: string): Promise<string> {\n return this.track(async () => Buffer.from(await this.getKValue(rId, key)).toString());\n }\n\n public async getKValueJson<T>(rId: AnyResourceRef, key: string): Promise<T> {\n return this.track(async () => JSON.parse(await this.getKValueString(rId, key)) as T);\n }\n\n public async getKValueIfExists(\n rId: AnyResourceRef,\n key: string,\n ): Promise<Uint8Array | undefined> {\n return this.track(async () => {\n const result = await this.sendSingleAndParse(\n {\n oneofKind: \"resourceKeyValueGetIfExists\",\n resourceKeyValueGetIfExists: {\n ...this.toSignedResourceId(rId),\n key,\n },\n },\n (r) =>\n r.resourceKeyValueGetIfExists.exists ? r.resourceKeyValueGetIfExists.value : undefined,\n );\n\n this._stat.kvGetRequests++;\n this._stat.kvGetBytes += result?.length ?? 0;\n\n return result;\n });\n }\n\n public async getKValueStringIfExists(\n rId: AnyResourceRef,\n key: string,\n ): Promise<string | undefined> {\n return this.track(async () => {\n const data = await this.getKValueIfExists(rId, key);\n return data === undefined ? undefined : Buffer.from(data).toString();\n });\n }\n\n public async getKValueJsonIfExists<T>(rId: AnyResourceRef, key: string): Promise<T | undefined> {\n return this.track(async () => {\n const str = await this.getKValueStringIfExists(rId, key);\n if (str === undefined) return undefined;\n return JSON.parse(str) as T;\n });\n }\n\n //\n // Cache\n //\n // TODO\n\n //\n // High level ops\n //\n\n /** Resolves existing or create first level resource from */\n public getFutureFieldValue(rId: AnyRef, fieldName: string, fieldType: FutureFieldType): FieldRef {\n const data = Buffer.from(JSON.stringify({ fieldName, fieldType }));\n const getFieldResource = this.createEphemeral({ name: \"json/getField\", version: \"1\" }, data);\n this.setField({ resourceId: getFieldResource, fieldName: \"resource\" }, rId);\n return { resourceId: getFieldResource, fieldName: \"result\" };\n }\n\n //\n // Technical\n //\n\n public async getGlobalTxId() {\n this.globalTxIdWasAwaited = true;\n return await this.globalTxId;\n }\n\n /** Closes output event stream */\n public async complete() {\n if (this._completed) return;\n this._completed = true;\n const completeResult = this.track(this.ll.complete());\n await this.drainAndAwaitPendingOps();\n await completeResult;\n }\n\n /** Await incoming message loop termination and throw\n * any leftover errors if it was unsuccessful */\n public async await() {\n await this.ll.await();\n }\n\n //\n // Tree streaming\n //\n\n /**\n * Perform a single `ResourceTree` walk and yield each visited resource.\n *\n * Items are yielded incrementally as stream frames arrive from server.\n *\n * The low-level transaction stream is still ordered, so each active request\n * must be consumed to completion (or closed via iterator `return()`/`throw()`)\n * before later responses can be dispatched.\n *\n * @param seeds - Roots for the traversal. May include both {@link ResourceData}\n * and {@link SignedResourceId} values in the same array.\n * @param opts.fieldFilter - Per-field predicate; matching field edges are not descended.\n * This wire field accepts only one filter. Compose multiple rules via\n * `treeFilter.and(...)` / `treeFilter.or(...)`.\n * @param opts.traverseStopRules - Per-resource predicate; matching resources are returned\n * with `traverseWasStopped = true` and their children are not visited.\n * This wire field accepts only one filter. Compose multiple rules via\n * `treeFilter.and(...)` / `treeFilter.or(...)`.\n * @param opts.includeKv - When true, each yielded item includes KV entries.\n * @param opts.maxDepth - Optional depth cap.\n */\n public resourceTree(\n seeds: (ResourceData | SignedResourceId)[],\n opts?: {\n fieldFilter?: ResourceAPI_Tree_Filter;\n traverseStopRules?: ResourceAPI_Tree_Filter;\n includeKv?: boolean;\n maxDepth?: number;\n },\n ): AsyncIterable<ResourceTreeFrame> {\n if (seeds.length === 0) {\n throw new Error(\"resourceTree: at least one seed must be provided\");\n }\n\n const seedProtos = seeds.map((seed): ResourceAPI_Tree_SeedResource => {\n const signedId = typeof seed === \"string\" ? seed : seed.id;\n const { globalId, signature } = parseSignedResourceId(signedId);\n return { resourceId: globalId, resourceSignature: signature };\n });\n\n const firstSeed = seedProtos[0];\n\n const responseStream = this.ll.sendStream({\n oneofKind: \"resourceTree\",\n resourceTree: {\n // Keep compatibility with request shape that requires non-optional fields.\n resourceId: firstSeed?.resourceId ?? 0n,\n resourceSignature: firstSeed?.resourceSignature ?? new Uint8Array(0),\n seeds: seedProtos,\n fieldFilter: opts?.fieldFilter,\n traverseStopRules: opts?.traverseStopRules,\n includeKv: opts?.includeKv ?? false,\n maxDepth: opts?.maxDepth,\n showSoftDeletes: false,\n // Empty token requests the full tree — this call has no continuation to resume.\n changedSinceToken: new Uint8Array(0),\n },\n });\n\n return {\n [Symbol.asyncIterator](): AsyncIterator<ResourceTreeFrame> {\n const iterator = responseStream[Symbol.asyncIterator]();\n\n return {\n async next(): Promise<IteratorResult<ResourceTreeFrame>> {\n while (true) {\n const item = await iterator.next();\n if (item.done) return { value: undefined, done: true };\n\n const frame = item.value.resourceTree;\n if (frame === undefined) continue;\n\n if (frame.resource && resourceIsDeleted(frame.resource)) {\n continue;\n }\n\n if (!frame.resource) {\n // Stop-marker frame: no resource body; server advertised treeStopMarker:v1.\n const id = createSignedResourceId(\n frame.resourceId,\n toResourceSignature(frame.resourceSignature),\n );\n return {\n value: { frameKind: \"stopMarker\", id, traverseWasStopped: true },\n done: false,\n };\n }\n\n return {\n value: {\n frameKind: \"resource\",\n ...protoToResource(frame.resource),\n kv: frame.kv,\n traverseWasStopped: frame.traverseWasStopped,\n },\n done: false,\n };\n }\n },\n async return(): Promise<IteratorResult<ResourceTreeFrame>> {\n if (iterator.return) await iterator.return();\n return { value: undefined, done: true };\n },\n async throw(error?: unknown): Promise<IteratorResult<ResourceTreeFrame>> {\n if (iterator.throw) await iterator.throw(error);\n throw error;\n },\n };\n },\n };\n }\n\n //\n // Helpers\n //\n\n private nextLocalResourceId(root: boolean): LocalResourceId {\n return createLocalResourceId(root, ++this.localResourceIdCounter, this.localTxId);\n }\n\n private static localTxIdCounter = 0;\n\n private static nextLocalTxId() {\n PlTransaction.localTxIdCounter++;\n if (PlTransaction.localTxIdCounter === MaxTxId) PlTransaction.localTxIdCounter = 1;\n return PlTransaction.localTxIdCounter;\n }\n}\n"],"mappings":";;;;;;;;;;;AAqHA,SAAgB,QAAQ,KAAiC;CACvD,OAAO,IAAI,eAAe,YAAY,KAAK,IAAI,eAAe,WAAW;AAC3E;AAEA,SAAgB,WAAW,KAAoC;CAC7D,IAAI,OAAO,QAAQ,UAAU,OAAO;CACpC,IAAI,OAAO,QAAQ,UAAU,OAAO;CACpC,OAAO,cAAc,GAAG;AAC1B;AAEA,SAAgB,aAAa,KAAsC;CACjE,OAAO,OAAO,QAAQ,YAAYA,cAAAA,mBAAmB,GAAG;AAC1D;AAEA,SAAgB,WAAW,KAAmC;CAC5D,OAAO,cAAc,IAAI,UAAU;AACrC;AAEA,SAAgB,cAAc,KAAiC;CAC7D,OACE,OAAO,QAAQ,YACf,QAAQ,QACR,IAAI,eAAe,UAAU,KAC7B,IAAI,eAAe,SAAS;AAEhC;AAEA,SAAgB,UAAU,KAA8B;CACtD,IAAI,WAAW,GAAG,GAAG,OAAO;EAAE,YAAY,IAAI,WAAW;EAAS,WAAW,IAAI;CAAU;MACtF,OAAO;AACd;AAEA,eAAsB,gBAAgB,KAA2C;CAC/E,IAAI,WAAW,GAAG,GAChB,OAAO;EAAE,YAAY,MAAM,IAAI,WAAW;EAAU,WAAW,IAAI;CAAU;CAC/E,OAAO;AACT;;;;;AAiBA,MAAa,YAAYC,YAAAA;;;;;;;AASzB,MAAa,O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names":["isSignedResourceId","AuthAPI_GrantAccess_GrantType","AuthAPI_Role","toResourceSignature","parseSignedResourceId","isLocalResourceId","createSignedResourceId","isNotFoundError","PromiseTracker","initialTxStatWithoutTime","ensureSignedResourceIdNotNull","protoToResource","JsonObject","JsonGzObject","ErrorResourceType","extractBasicResourceData","fieldTypeToProto","protoToField","toBytes","resourceIsDeleted","createLocalResourceId"],"sources":["../../src/core/transaction.ts"],"sourcesContent":["import type {\n ColorProof,\n LocalResourceId,\n OptionalSignedResourceId,\n BasicResourceData,\n FieldData,\n FieldType,\n ResourceData,\n SignedResourceId,\n ResourceSignature,\n ResourceType,\n FutureFieldType,\n} from \"./types\";\nimport {\n createSignedResourceId,\n createLocalResourceId,\n isLocalResourceId,\n ensureSignedResourceIdNotNull,\n MaxTxId,\n extractBasicResourceData,\n parseSignedResourceId,\n toResourceSignature,\n isSignedResourceId,\n} from \"./types\";\nimport type {\n ClientMessageRequest,\n LLPlTransaction,\n OneOfKind,\n ServerMessageResponse,\n} from \"./ll_transaction\";\nimport type {\n AuthAPI_Grant,\n ResourceAPI_Tree_Filter,\n ResourceAPI_Tree_SeedResource,\n} from \"../proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api\";\nimport {\n TxAPI_Open_Request_WritableTx,\n AuthAPI_GrantAccess_GrantType,\n AuthAPI_Role,\n} from \"../proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api\";\nimport type { NonUndefined } from \"utility-types\";\nimport { toBytes } from \"../util/util\";\nimport {\n fieldTypeToProto,\n protoToField,\n protoToResource,\n resourceIsDeleted,\n} from \"./type_conversion\";\nimport {\n canonicalJsonBytes,\n canonicalJsonGzBytes,\n deepFreeze,\n notEmpty,\n} from \"@milaboratories/ts-helpers\";\nimport { isNotFoundError } from \"./errors\";\nimport type { FinalResourceDataPredicate } from \"./final\";\nimport type { LRUCache } from \"lru-cache\";\nimport type { ResourceDataCacheRecord } from \"./cache\";\nimport type { TxStat } from \"./stat\";\nimport { initialTxStatWithoutTime } from \"./stat\";\nimport type { ErrorResourceData } from \"./error_resource\";\nimport { ErrorResourceType } from \"./error_resource\";\nimport { JsonGzObject, JsonObject } from \"../helpers/pl\";\nimport { PromiseTracker } from \"./PromiseTracker\";\n\n/** Reference to resource, used only within transaction */\nexport interface ResourceRef {\n /** Global resource id of newly created resources, become available only\n * after response for the corresponding creation request is received. */\n readonly globalId: Promise<SignedResourceId>;\n\n /** Transaction-local resource id is assigned right after resource creation\n * request is sent, and can be used right away */\n readonly localId: LocalResourceId;\n}\n\n/** Key-Value pair from resource-attached KV storage */\nexport interface KeyValue {\n key: string;\n value: Uint8Array;\n}\n\n/** Key-Value pair from resource-attached KV storage */\nexport interface KeyValueString {\n key: string;\n value: string;\n}\n\nexport type ResourceTreeItem = ResourceData & { kv: KeyValue[]; traverseWasStopped: boolean };\n\n/** A single frame from the resourceTree() stream.\n *\n * A node the request's `traverseStopRules` matched is emitted as a lightweight marker\n * instead of a full resource payload. Callers must narrow on `frameKind` before accessing\n * type-specific fields.\n * Note: `frameKind` is distinct from `ResourceData.kind` (which is the resource kind).\n *\n * Under `changedSinceToken` there are no markers: stop rules do not apply, and an unchanged\n * resource produces no frame at all.\n */\nexport type ResourceTreeFrame =\n | (ResourceTreeItem & { frameKind: \"resource\" })\n | { frameKind: \"stopMarker\"; id: SignedResourceId; traverseWasStopped: true };\n\ninterface _FieldId<RId> {\n /** Parent resource id */\n resourceId: RId;\n /** Field name */\n fieldName: string;\n}\n\nexport type FieldId = _FieldId<SignedResourceId>;\nexport type FieldRef = _FieldId<ResourceRef>;\nexport type LocalFieldId = _FieldId<LocalResourceId>;\nexport type AnyFieldId = FieldId | LocalFieldId;\n\nexport type AnyResourceRef = ResourceRef | LocalResourceId | SignedResourceId;\nexport type AnyFieldRef = _FieldId<AnyResourceRef>; // FieldRef | FieldId\nexport type AnyRef = AnyResourceRef | AnyFieldRef;\n\nexport function isField(ref: AnyRef): ref is AnyFieldRef {\n return ref.hasOwnProperty(\"resourceId\") && ref.hasOwnProperty(\"fieldName\");\n}\n\nexport function isResource(ref: AnyRef): ref is AnyResourceRef {\n if (typeof ref === \"bigint\") return true; // LocalResourceId\n if (typeof ref === \"string\") return true; // SignedResourceId\n return isResourceRef(ref);\n}\n\nexport function isResourceId(ref: AnyRef): ref is SignedResourceId {\n return typeof ref === \"string\" && isSignedResourceId(ref);\n}\n\nexport function isFieldRef(ref: AnyFieldRef): ref is FieldRef {\n return isResourceRef(ref.resourceId);\n}\n\nexport function isResourceRef(ref: AnyRef): ref is ResourceRef {\n return (\n typeof ref === \"object\" &&\n ref !== null &&\n ref.hasOwnProperty(\"globalId\") &&\n ref.hasOwnProperty(\"localId\")\n );\n}\n\nexport function toFieldId(ref: AnyFieldRef): AnyFieldId {\n if (isFieldRef(ref)) return { resourceId: ref.resourceId.localId, fieldName: ref.fieldName };\n else return ref as FieldId;\n}\n\nexport async function toGlobalFieldId(ref: FieldRef | FieldId): Promise<FieldId> {\n if (isFieldRef(ref))\n return { resourceId: await ref.resourceId.globalId, fieldName: ref.fieldName };\n return ref;\n}\n\nexport interface ResourceIdWithSignature {\n resourceId: bigint;\n resourceSignature: ResourceSignature;\n}\n\n/** Access level handed to a grant recipient (see {@link PlTransaction.grantAccess}). */\nexport interface GrantPermissions {\n /** Write access = ability to modify the resource subtree + re-share it. */\n writable: boolean;\n}\n\n/**\n * Distinguishes a regular user-to-user grant from a system-level public grant.\n * Re-export of the wire enum so callers need not reach into the generated proto.\n */\nexport const GrantType = AuthAPI_GrantAccess_GrantType;\nexport type GrantType = AuthAPI_GrantAccess_GrantType;\n\n/**\n * Effective role of an authenticated session (controller / workflow / user / admin).\n * Re-export of the wire enum so callers need not reach into the generated proto.\n * Returned by {@link LLPlClient.getSessionInfo} and surfaced as\n * {@link PlClient.currentUserRole}.\n */\nexport const Role = AuthAPI_Role;\nexport type Role = AuthAPI_Role;\n\n/**\n * Recognises the \"all users on the server\" sentinel login. A public\n * ({@link GrantType.ANY_AUTHORISED}) grant is recorded against a reserved login, and\n * recipients of an everyone-grant surface in `ListGrants` with that value — callers\n * detect it here and map it to \"*\".\n *\n * Matched by exact token equality. The long random token guarantees no real user login\n * collides with it.\n */\nconst EveryoneUser = \"everyone-Po9ahwahxai7Aejingaiyiequuecu3ei4moaNge4xahTh0Co7XeeLeiph6ahy3As\";\nexport function isEveryoneUserLogin(login: string): boolean {\n return login === EveryoneUser;\n}\n\nconst emptySignature = toResourceSignature(new Uint8Array(0));\n\nfunction toResourceIdAndSignature(ref: AnyResourceRef): ResourceIdWithSignature {\n if (isResourceRef(ref)) {\n // Local ID — no signature yet\n return { resourceId: ref.localId, resourceSignature: emptySignature };\n }\n if (isResourceId(ref)) {\n // SignedResourceId — decompose\n const { globalId, signature } = parseSignedResourceId(ref);\n return { resourceId: globalId as bigint, resourceSignature: signature };\n }\n // Raw bigint (LocalResourceId)\n return { resourceId: ref as bigint, resourceSignature: emptySignature };\n}\n\nexport async function toGlobalResourceId(ref: AnyResourceRef): Promise<SignedResourceId> {\n if (isResourceRef(ref)) return await ref.globalId;\n if (isLocalResourceId(ref)) return createSignedResourceId(ref); // legacy path: loose security mode in backend\n return ref;\n}\n\nexport function field(resourceId: AnyResourceRef, fieldName: string): AnyFieldRef {\n return { resourceId, fieldName };\n}\n\n/** If transaction commit failed due to write conflicts */\nexport class TxCommitConflict extends Error {\n name = \"TxCommitConflict\";\n}\n\nasync function notFoundToUndefined<T>(cb: () => Promise<T>): Promise<T | undefined> {\n try {\n return await cb();\n } catch (e) {\n if (isNotFoundError(e)) return undefined;\n throw e;\n }\n}\n\n/**\n * Each platform transaction has 3 stages:\n * - initialization (txOpen message -> txInfo response)\n * - communication (create resources, fields, references and so on)\n * - finalization (txCommit or txDiscard message)\n *\n * This class encapsulates finalization stage and provides ready-to-communication transaction object.\n * */\nexport class PlTransaction {\n private readonly txOpen: Promise<{ id: bigint; sinceToken?: Uint8Array }>;\n private readonly globalTxId: Promise<bigint>;\n private readonly localTxId: number = PlTransaction.nextLocalTxId();\n\n /** Used in caching */\n private readonly txOpenTimestamp = Date.now();\n\n private localResourceIdCounter = 0;\n\n /** Store logical tx open / closed state to prevent invalid sequence of requests.\n * True means output stream was completed.\n * Contract: there must be no async operations between setting this field to true and sending complete signal to stream. */\n private _completed = false;\n\n private globalTxIdWasAwaited: boolean = false;\n\n public readonly pending = new PromiseTracker();\n\n private readonly _startTime = Date.now();\n private readonly _stat = initialTxStatWithoutTime();\n public get stat(): TxStat {\n return {\n ...this._stat,\n timeMs: Date.now() - this._startTime,\n };\n }\n\n constructor(\n private readonly ll: LLPlTransaction,\n public readonly name: string,\n public readonly writable: boolean,\n private readonly _clientRoot: OptionalSignedResourceId,\n private readonly finalPredicate: FinalResourceDataPredicate,\n private readonly sharedResourceDataCache: LRUCache<SignedResourceId, ResourceDataCacheRecord>,\n private readonly enableFormattedErrors: boolean = false,\n ) {\n // initiating transaction\n this.txOpen = this.sendSingleAndParse(\n {\n oneofKind: \"txOpen\",\n txOpen: {\n name,\n enableFormattedErrors,\n writable: writable\n ? TxAPI_Open_Request_WritableTx.WRITABLE\n : TxAPI_Open_Request_WritableTx.NOT_WRITABLE,\n },\n },\n (r) => ({\n id: notEmpty(r.txOpen.tx?.id),\n // Empty on a server without treeChangedSince:v1, and on a writable transaction,\n // which is served none: its reads see this transaction's own uncommitted writes,\n // so a token earned here could pin state that never commits.\n sinceToken: r.txOpen.nextSinceToken.length > 0 ? r.txOpen.nextSinceToken : undefined,\n }),\n );\n this.globalTxId = this.txOpen.then((r) => r.id);\n\n void this.track(this.globalTxId);\n\n // To avoid floating promise\n this.globalTxId.catch((err) => {\n if (!this.globalTxIdWasAwaited) {\n console.warn(err);\n }\n });\n\n // Adding stats\n this._stat.txCount++;\n }\n\n /**\n * Collect all pending promises for the transaction finalization.\n */\n public track<T>(promiseOrCallback: Promise<T> | (() => Promise<T>)): Promise<T> {\n return this.pending.track(promiseOrCallback);\n }\n\n private async drainAndAwaitPendingOps(): Promise<void> {\n // awaiting these pending operations first, to catch any errors\n await this.pending.awaitAll();\n }\n\n private sendSingleAndParse<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>, T>(\n r: OneOfKind<ClientMessageRequest, Kind>,\n parser: (resp: OneOfKind<ServerMessageResponse, Kind>) => T,\n ): Promise<T> {\n return this.pending.track(async () => {\n const rawResponsePromise = this.ll.send(r, false);\n\n void this.pending.track(rawResponsePromise);\n\n await this.drainAndAwaitPendingOps();\n\n // awaiting our result, and parsing the response\n return parser(await rawResponsePromise);\n });\n }\n\n private sendMultiAndParse<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>, T>(\n r: OneOfKind<ClientMessageRequest, Kind>,\n parser: (resp: OneOfKind<ServerMessageResponse, Kind>[]) => T,\n ): Promise<T> {\n return this.pending.track(async () => {\n const rawResponsePromise = this.ll.send(r, true);\n\n void this.pending.track(rawResponsePromise);\n\n await this.drainAndAwaitPendingOps();\n\n // awaiting our result, and parsing the response\n return parser(await rawResponsePromise);\n });\n }\n\n private async sendVoidSync<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>>(\n r: OneOfKind<ClientMessageRequest, Kind>,\n ): Promise<void> {\n await this.ll.send(r, false);\n }\n\n /** Requests sent with this method should never produce recoverable errors */\n private sendVoidAsync<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>>(\n r: OneOfKind<ClientMessageRequest, Kind>,\n ): void {\n void this.track(this.sendVoidSync(r));\n }\n\n /** Set default color proof for subsequent resource creation requests */\n public setDefaultColor(colorProof: ColorProof): void {\n this.sendVoidAsync({\n oneofKind: \"setDefaultColor\",\n setDefaultColor: { colorProof },\n });\n }\n\n private toSignedResourceId(rId: AnyResourceRef): ResourceIdWithSignature {\n return toResourceIdAndSignature(rId);\n }\n\n private toSignedFieldId(fId: AnyFieldRef): {\n resourceId: bigint;\n resourceSignature: ResourceSignature;\n fieldName: string;\n } {\n const base = toFieldId(fId);\n const signed = toResourceIdAndSignature(base.resourceId);\n return {\n resourceId: signed.resourceId,\n fieldName: base.fieldName,\n resourceSignature: signed.resourceSignature,\n };\n }\n\n private toSignedErrorRef(rId: AnyResourceRef): {\n errorResourceId: bigint;\n errorResourceSignature: ResourceSignature;\n } {\n const { resourceId, resourceSignature } = this.toSignedResourceId(rId);\n return { errorResourceId: resourceId, errorResourceSignature: resourceSignature };\n }\n\n private checkTxOpen() {\n if (this._completed) throw new Error(\"Transaction already closed\");\n }\n\n public get completed() {\n return this._completed;\n }\n\n /** Commit & closes transaction. {@link TxCommitConflict} is thrown on\n * commit conflicts. */\n public async commit() {\n this.checkTxOpen();\n\n // tx will accept no requests after this one\n this._completed = true;\n\n if (!this.writable) {\n // no need to explicitly commit or reject read-only tx\n const completeResult = this.track(this.ll.complete());\n await this.drainAndAwaitPendingOps();\n await completeResult;\n await this.ll.await();\n } else {\n const commitResponse = this.track(\n this.sendSingleAndParse({ oneofKind: \"txCommit\", txCommit: {} }, (r) => r.txCommit.success),\n );\n\n // send closing frame right after commit to save some time on round-trips\n const completeResult = this.track(this.ll.complete());\n\n // now when we pushed all packets into the stream, we should wait for any\n // pending void operations from before, to catch any errors\n await this.drainAndAwaitPendingOps();\n\n if (!(await commitResponse)) throw new TxCommitConflict();\n\n await completeResult;\n\n // await event-loop completion\n await this.ll.await();\n }\n }\n\n public async discard() {\n this.checkTxOpen();\n\n // tx will accept no requests after this one\n this._completed = true;\n\n const discardResponse = this.sendVoidSync({ oneofKind: \"txDiscard\", txDiscard: {} });\n void this.track(discardResponse);\n // send closing frame right after commit to save some time on round-trips\n const completeResult = this.track(this.ll.complete());\n\n // now when we pushed all packets into the stream, we should wait for any\n // pending void operations from before, to catch any errors\n await this.drainAndAwaitPendingOps();\n\n await discardResponse;\n await completeResult;\n await this.ll.await();\n }\n\n //\n // Main tx methods\n //\n\n public get clientRoot(): SignedResourceId {\n return ensureSignedResourceIdNotNull(this._clientRoot);\n }\n\n //\n // Resources\n //\n\n public createSingleton(\n name: string,\n type: ResourceType,\n errorIfExists: boolean = false,\n color?: ColorProof,\n ): ResourceRef {\n return this.createResource(\n false,\n (localId) => ({\n oneofKind: \"resourceCreateSingleton\",\n resourceCreateSingleton: {\n type,\n id: localId,\n data: Buffer.from(name),\n errorIfExists,\n colorProof: color ?? emptySignature,\n },\n }),\n (r) => r.resourceCreateSingleton.resourceId,\n (r) => r.resourceCreateSingleton.resourceSignature,\n );\n }\n\n public getSingleton(name: string, loadFields: true): Promise<ResourceData>;\n public getSingleton(name: string, loadFields: false): Promise<BasicResourceData>;\n public getSingleton(\n name: string,\n loadFields: boolean = true,\n ): Promise<BasicResourceData | ResourceData> {\n return this.sendSingleAndParse(\n {\n oneofKind: \"resourceGetSingleton\",\n resourceGetSingleton: {\n data: Buffer.from(name),\n loadFields,\n },\n },\n (r) => protoToResource(notEmpty(r.resourceGetSingleton.resource)),\n );\n }\n\n private createResource<Kind extends NonUndefined<ClientMessageRequest[\"oneofKind\"]>>(\n root: boolean,\n req: (localId: LocalResourceId) => OneOfKind<ClientMessageRequest, Kind>,\n parser: (resp: OneOfKind<ServerMessageResponse, Kind>) => bigint,\n sigExtractor: (resp: OneOfKind<ServerMessageResponse, Kind>) => Uint8Array | undefined,\n ): ResourceRef {\n const localId = this.nextLocalResourceId(root);\n\n const globalId = this.sendSingleAndParse(req(localId), (r) => {\n const rawId = parser(r);\n const sig = sigExtractor ? toResourceSignature(sigExtractor(r)) : undefined;\n return createSignedResourceId(rawId, sig);\n });\n\n void this.track(globalId);\n\n return { globalId, localId };\n }\n\n public createRoot(type: ResourceType): ResourceRef {\n this._stat.rootsCreated++;\n return this.createResource(\n true,\n (localId) => ({ oneofKind: \"resourceCreateRoot\", resourceCreateRoot: { type, id: localId } }),\n (r) => r.resourceCreateRoot.resourceId,\n (r) => r.resourceCreateRoot.resourceSignature,\n );\n }\n\n public createStruct(\n type: ResourceType,\n data?: Uint8Array | string,\n color?: ColorProof,\n ): ResourceRef {\n this._stat.structsCreated++;\n this._stat.structsCreatedDataBytes += data?.length ?? 0;\n return this.createResource(\n false,\n (localId) => ({\n oneofKind: \"resourceCreateStruct\",\n resourceCreateStruct: {\n type,\n id: localId,\n data:\n data === undefined ? undefined : typeof data === \"string\" ? Buffer.from(data) : data,\n colorProof: color ?? emptySignature,\n },\n }),\n (r) => r.resourceCreateStruct.resourceId,\n (r) => r.resourceCreateStruct.resourceSignature,\n );\n }\n\n public createEphemeral(\n type: ResourceType,\n data?: Uint8Array | string,\n color?: ColorProof,\n ): ResourceRef {\n this._stat.ephemeralsCreated++;\n this._stat.ephemeralsCreatedDataBytes += data?.length ?? 0;\n return this.createResource(\n false,\n (localId) => ({\n oneofKind: \"resourceCreateEphemeral\",\n resourceCreateEphemeral: {\n type,\n id: localId,\n data:\n data === undefined ? undefined : typeof data === \"string\" ? Buffer.from(data) : data,\n colorProof: color ?? emptySignature,\n },\n }),\n (r) => r.resourceCreateEphemeral.resourceId,\n (r) => r.resourceCreateEphemeral.resourceSignature,\n );\n }\n\n public createValue(\n type: ResourceType,\n data: Uint8Array | string,\n errorIfExists: boolean = false,\n color?: ColorProof,\n ): ResourceRef {\n this._stat.valuesCreated++;\n this._stat.valuesCreatedDataBytes += data?.length ?? 0;\n return this.createResource(\n false,\n (localId) => ({\n oneofKind: \"resourceCreateValue\",\n resourceCreateValue: {\n type,\n id: localId,\n data: typeof data === \"string\" ? Buffer.from(data) : data,\n errorIfExists,\n colorProof: color ?? emptySignature,\n },\n }),\n (r) => r.resourceCreateValue.resourceId,\n (r) => r.resourceCreateValue.resourceSignature,\n );\n }\n\n public createJsonValue(data: unknown, color?: ColorProof): ResourceRef {\n const jsonData = canonicalJsonBytes(data);\n return this.createValue(JsonObject, jsonData, false, color);\n }\n\n public createJsonGzValue(\n data: unknown,\n minSizeToGzip: number | undefined = 16_384,\n color?: ColorProof,\n ): ResourceRef {\n const { data: jsonData, isGzipped } = canonicalJsonGzBytes(data, minSizeToGzip);\n return this.createValue(isGzipped ? JsonGzObject : JsonObject, jsonData, false, color);\n }\n\n public createError(message: string, color?: ColorProof): ResourceRef {\n return this.createValue(\n ErrorResourceType,\n JSON.stringify({ message } satisfies ErrorResourceData),\n false,\n color,\n );\n }\n\n public setResourceName(name: string, rId: AnyResourceRef): void {\n this.sendVoidAsync({\n oneofKind: \"resourceNameSet\",\n resourceNameSet: { ...this.toSignedResourceId(rId), name },\n });\n }\n\n public deleteResourceName(name: string): void {\n this.sendVoidAsync({ oneofKind: \"resourceNameDelete\", resourceNameDelete: { name } });\n }\n\n public getResourceByName(name: string): Promise<SignedResourceId> {\n return this.sendSingleAndParse(\n { oneofKind: \"resourceNameGet\", resourceNameGet: { name } },\n (r) => {\n const rawId = r.resourceNameGet.resourceId;\n if (rawId === 0n) throw new Error(\"null resource id from getResourceByName\");\n const sig = toResourceSignature(r.resourceNameGet.resourceSignature);\n return createSignedResourceId(rawId, sig);\n },\n );\n }\n\n public checkResourceNameExists(name: string): Promise<boolean> {\n return this.sendSingleAndParse(\n { oneofKind: \"resourceNameExists\", resourceNameExists: { name } },\n (r) => r.resourceNameExists.exists,\n );\n }\n\n public removeResource(rId: SignedResourceId): void {\n this.sendVoidAsync({\n oneofKind: \"resourceRemove\",\n resourceRemove: this.toSignedResourceId(rId),\n });\n }\n\n public resourceExists(rId: SignedResourceId): Promise<boolean> {\n return this.sendSingleAndParse(\n {\n oneofKind: \"resourceExists\",\n resourceExists: this.toSignedResourceId(rId),\n },\n (r) => r.resourceExists.exists,\n );\n }\n\n /** This method may return stale resource state from cache if resource was removed */\n public async getResourceData(rId: AnyResourceRef, loadFields: true): Promise<ResourceData>;\n /** This method may return stale resource state from cache if resource was removed */\n public async getResourceData(rId: AnyResourceRef, loadFields: false): Promise<BasicResourceData>;\n /** This method may return stale resource state from cache if resource was removed */\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: boolean,\n ): Promise<BasicResourceData | ResourceData>;\n /** This method may return stale resource state from cache if ignoreCache == false if resource was removed */\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: true,\n ignoreCache: boolean,\n ): Promise<ResourceData>;\n /** This method may return stale resource state from cache if ignoreCache == false if resource was removed */\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: false,\n ignoreCache: boolean,\n ): Promise<BasicResourceData>;\n /** This method may return stale resource state from cache if ignoreCache == false if resource was removed */\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: boolean,\n ignoreCache: boolean,\n ): Promise<BasicResourceData | ResourceData>;\n public async getResourceData(\n rId: AnyResourceRef,\n loadFields: boolean = true,\n ignoreCache: boolean = false,\n ): Promise<BasicResourceData | ResourceData> {\n return this.track(async () => {\n if (!ignoreCache && isResourceId(rId)) {\n // checking if we can return result from cache\n const fromCache = this.sharedResourceDataCache.get(rId);\n if (fromCache && fromCache.cacheTxOpenTimestamp < this.txOpenTimestamp) {\n if (!loadFields) {\n this._stat.rGetDataCacheHits++;\n this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;\n return fromCache.basicData;\n } else if (fromCache.data) {\n this._stat.rGetDataCacheHits++;\n this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;\n this._stat.rGetDataCacheFields += fromCache.data.fields.length;\n return fromCache.data;\n }\n }\n }\n\n const result = await this.sendSingleAndParse(\n {\n oneofKind: \"resourceGet\",\n resourceGet: {\n ...this.toSignedResourceId(rId),\n loadFields: loadFields,\n showSoftDeletes: false,\n // Always empty: an on-demand read asks for what the caller does not hold, so a\n // token here could only suppress the one body it came for.\n changedSinceToken: new Uint8Array(0),\n },\n },\n (r) => protoToResource(notEmpty(r.resourceGet.resource)),\n );\n\n this._stat.rGetDataNetRequests++;\n this._stat.rGetDataNetBytes += result.data?.length ?? 0;\n this._stat.rGetDataNetFields += result.fields.length;\n\n // we will cache only final resource data states\n // caching result even if we were ignore the cache\n if (isResourceId(rId) && this.finalPredicate(result)) {\n deepFreeze(result);\n const fromCache = this.sharedResourceDataCache.get(rId);\n if (fromCache) {\n if (loadFields && !fromCache.data) {\n fromCache.data = result;\n // updating timestamp because we updated the record\n fromCache.cacheTxOpenTimestamp = this.txOpenTimestamp;\n }\n } else {\n const basicData = extractBasicResourceData(result);\n deepFreeze(basicData);\n if (loadFields)\n this.sharedResourceDataCache.set(rId, {\n basicData,\n data: result,\n cacheTxOpenTimestamp: this.txOpenTimestamp,\n });\n else\n this.sharedResourceDataCache.set(rId, {\n basicData,\n data: undefined,\n cacheTxOpenTimestamp: this.txOpenTimestamp,\n });\n }\n }\n\n return result;\n });\n }\n\n public async getResourceDataIfExists(\n rId: AnyResourceRef,\n loadFields: true,\n ): Promise<ResourceData | undefined>;\n public async getResourceDataIfExists(\n rId: AnyResourceRef,\n loadFields: false,\n ): Promise<BasicResourceData | undefined>;\n public async getResourceDataIfExists(\n rId: AnyResourceRef,\n loadFields: boolean,\n ): Promise<BasicResourceData | ResourceData | undefined>;\n public async getResourceDataIfExists(\n rId: AnyResourceRef,\n loadFields: boolean = true,\n ): Promise<BasicResourceData | ResourceData | undefined> {\n return this.track(async () => {\n // calling this method will ignore cache, because user intention is to detect resource absence\n // which cache will prevent\n const result = await notFoundToUndefined(\n async () => await this.getResourceData(rId, loadFields, true),\n );\n\n // cleaning cache record if resource was removed from the db\n if (result === undefined && isResourceId(rId)) this.sharedResourceDataCache.delete(rId);\n\n return result;\n });\n }\n\n /**\n * Inform platform that resource will not get any new input fields.\n * This is required, when client creates resource without schema and wants\n * controller to start calculations.\n * Most controllers will not start calculations even when all inputs\n * have their values, if inputs list is not locked.\n */\n public lockInputs(rId: AnyResourceRef): void {\n this._stat.inputsLocked++;\n this.sendVoidAsync({\n oneofKind: \"resourceLockInputs\",\n resourceLockInputs: this.toSignedResourceId(rId),\n });\n }\n\n /**\n * Inform platform that resource will not get any new output fields.\n * This is required for resource to pass deduplication.\n */\n public lockOutputs(rId: AnyResourceRef): void {\n this._stat.outputsLocked++;\n this.sendVoidAsync({\n oneofKind: \"resourceLockOutputs\",\n resourceLockOutputs: this.toSignedResourceId(rId),\n });\n }\n\n public lock(rID: AnyResourceRef): void {\n this.lockInputs(rID);\n this.lockOutputs(rID);\n }\n\n public setResourceError(rId: AnyResourceRef, ref: AnyResourceRef): void {\n this.sendVoidAsync({\n oneofKind: \"resourceSetError\",\n resourceSetError: {\n ...this.toSignedResourceId(rId),\n ...this.toSignedErrorRef(ref),\n },\n });\n }\n\n /**\n * Grant another user access to a resource (and its subtree). Used by the\n * project-sharing donor flow to hand a recipient the shared envelope.\n *\n * `grantType` defaults to a regular user-to-user grant\n * ({@link GrantType.SINGLE_USER}); pass {@link GrantType.ANY_AUTHORISED}\n * to make the resource public to everyone (requires backend `publicGrants:v1`\n * and a role allowed to grant to everyone — `target` is ignored / rewritten\n * to the everyone-user by the backend).\n */\n public grantAccess(\n rId: AnyResourceRef,\n target: string,\n permissions: GrantPermissions,\n grantType: GrantType = AuthAPI_GrantAccess_GrantType.SINGLE_USER,\n ): void {\n this.sendVoidAsync({\n oneofKind: \"grantAccess\",\n grantAccess: {\n ...this.toSignedResourceId(rId),\n targetUser: target,\n permissions,\n grantType,\n },\n });\n }\n\n /**\n * Revoke a single user's grant on a resource — the inverse of {@link grantAccess}.\n * Used by the sharing donor flow to pull one recipient out of a multi-recipient\n * envelope without tearing the whole share down. A no-op on the backend if the user\n * has no grant.\n */\n public revokeAccess(rId: AnyResourceRef, target: string): void {\n this.sendVoidAsync({\n oneofKind: \"revokeAccess\",\n revokeAccess: {\n ...this.toSignedResourceId(rId),\n targetUser: target,\n },\n });\n }\n\n /**\n * Enumerate the grants of a resource — the donor-side \"who did I share with\" view.\n * The backend gates this on the signed, writable resource handle, so only the\n * resource's owner can read its grants. Recipients of a public\n * ({@link GrantType.ANY_AUTHORISED}) grant surface with `user` matching the\n * everyone-sentinel — test with {@link isEveryoneUserLogin}.\n *\n * Transactional unary op (single response with all grants), so it rides the tx\n * channel and works on every wire protocol — unlike the standalone server-streaming\n * `ListGrants` RPC, which has no REST binding.\n */\n public listGrants(rId: AnyResourceRef): Promise<AuthAPI_Grant[]> {\n return this.sendSingleAndParse(\n { oneofKind: \"listGrants\", listGrants: this.toSignedResourceId(rId) },\n (r) => r.listGrants.grants,\n );\n }\n\n //\n // Fields\n //\n\n public createField(fId: AnyFieldRef, fieldType: FieldType, value?: AnyRef): void {\n this._stat.fieldsCreated++;\n this.sendVoidAsync({\n oneofKind: \"fieldCreate\",\n fieldCreate: { type: fieldTypeToProto(fieldType), id: this.toSignedFieldId(fId) },\n });\n if (value !== undefined) this.setField(fId, value);\n }\n\n public fieldExists(fId: AnyFieldRef): Promise<boolean> {\n return this.sendSingleAndParse(\n {\n oneofKind: \"fieldExists\",\n fieldExists: { field: this.toSignedFieldId(fId) },\n },\n (r) => r.fieldExists.exists,\n );\n }\n\n public setField(fId: AnyFieldRef, ref: AnyRef): void {\n this._stat.fieldsSet++;\n if (isResource(ref)) {\n this.sendVoidAsync({\n oneofKind: \"fieldSet\",\n fieldSet: {\n field: this.toSignedFieldId(fId),\n value: {\n ...this.toSignedResourceId(ref),\n fieldName: \"\", // default value, read as undefined\n },\n },\n });\n } else {\n this.sendVoidAsync({\n oneofKind: \"fieldSet\",\n fieldSet: {\n field: this.toSignedFieldId(fId),\n value: this.toSignedFieldId(ref),\n },\n });\n }\n }\n\n public setFieldError(fId: AnyFieldRef, ref: AnyResourceRef): void {\n this._stat.fieldsSet++;\n this.sendVoidAsync({\n oneofKind: \"fieldSetError\",\n fieldSetError: {\n field: this.toSignedFieldId(fId),\n ...this.toSignedErrorRef(ref),\n },\n });\n }\n\n public getField(fId: AnyFieldRef): Promise<FieldData> {\n this._stat.fieldsGet++;\n return this.sendSingleAndParse(\n { oneofKind: \"fieldGet\", fieldGet: { field: this.toSignedFieldId(fId) } },\n (r) => protoToField(notEmpty(r.fieldGet.field)),\n );\n }\n\n public async getFieldIfExists(fId: AnyFieldRef): Promise<FieldData | undefined> {\n return this.track(notFoundToUndefined(async () => await this.getField(fId)));\n }\n\n public resetField(fId: AnyFieldRef): void {\n this.sendVoidAsync({\n oneofKind: \"fieldReset\",\n fieldReset: { field: this.toSignedFieldId(fId) },\n });\n }\n\n public removeField(fId: AnyFieldRef): void {\n this.sendVoidAsync({\n oneofKind: \"fieldRemove\",\n fieldRemove: { field: this.toSignedFieldId(fId) },\n });\n }\n\n //\n // KV\n //\n\n public async listKeyValues(rId: AnyResourceRef): Promise<KeyValue[]> {\n return this.track(async () => {\n const result = await this.sendMultiAndParse(\n {\n oneofKind: \"resourceKeyValueList\",\n resourceKeyValueList: {\n ...this.toSignedResourceId(rId),\n startFrom: \"\",\n limit: 0,\n // Always empty, as for the single resource read above.\n changedSinceToken: new Uint8Array(0),\n },\n },\n (r) => r.map((e) => e.resourceKeyValueList.record!),\n );\n\n this._stat.kvListRequests++;\n this._stat.kvListEntries += result.length;\n for (const kv of result) this._stat.kvListBytes += kv.key.length + kv.value.length;\n\n return result;\n });\n }\n\n public async listKeyValuesString(rId: AnyResourceRef): Promise<KeyValueString[]> {\n return this.track(async () =>\n (await this.listKeyValues(rId)).map(({ key, value }) => ({\n key,\n value: Buffer.from(value).toString(),\n })),\n );\n }\n\n public async listKeyValuesIfResourceExists(rId: AnyResourceRef): Promise<KeyValue[] | undefined> {\n return this.track(notFoundToUndefined(async () => await this.listKeyValues(rId)));\n }\n\n public async listKeyValuesStringIfResourceExists(\n rId: AnyResourceRef,\n ): Promise<KeyValueString[] | undefined> {\n return this.track(notFoundToUndefined(async () => await this.listKeyValuesString(rId)));\n }\n\n public setKValue(rId: AnyResourceRef, key: string, value: Uint8Array | string): void {\n this._stat.kvSetRequests++;\n this._stat.kvSetBytes++;\n this.sendVoidAsync({\n oneofKind: \"resourceKeyValueSet\",\n resourceKeyValueSet: {\n ...this.toSignedResourceId(rId),\n key,\n value: toBytes(value),\n },\n });\n }\n\n public deleteKValue(rId: AnyResourceRef, key: string): void {\n this.sendVoidAsync({\n oneofKind: \"resourceKeyValueDelete\",\n resourceKeyValueDelete: {\n ...this.toSignedResourceId(rId),\n key,\n },\n });\n }\n\n public async getKValue(rId: AnyResourceRef, key: string): Promise<Uint8Array> {\n return this.track(async () => {\n const result = await this.sendSingleAndParse(\n {\n oneofKind: \"resourceKeyValueGet\",\n resourceKeyValueGet: {\n ...this.toSignedResourceId(rId),\n key,\n },\n },\n (r) => r.resourceKeyValueGet.value,\n );\n\n this._stat.kvGetRequests++;\n this._stat.kvGetBytes += result.length;\n\n return result;\n });\n }\n\n public async getKValueString(rId: AnyResourceRef, key: string): Promise<string> {\n return this.track(async () => Buffer.from(await this.getKValue(rId, key)).toString());\n }\n\n public async getKValueJson<T>(rId: AnyResourceRef, key: string): Promise<T> {\n return this.track(async () => JSON.parse(await this.getKValueString(rId, key)) as T);\n }\n\n public async getKValueIfExists(\n rId: AnyResourceRef,\n key: string,\n ): Promise<Uint8Array | undefined> {\n return this.track(async () => {\n const result = await this.sendSingleAndParse(\n {\n oneofKind: \"resourceKeyValueGetIfExists\",\n resourceKeyValueGetIfExists: {\n ...this.toSignedResourceId(rId),\n key,\n },\n },\n (r) =>\n r.resourceKeyValueGetIfExists.exists ? r.resourceKeyValueGetIfExists.value : undefined,\n );\n\n this._stat.kvGetRequests++;\n this._stat.kvGetBytes += result?.length ?? 0;\n\n return result;\n });\n }\n\n public async getKValueStringIfExists(\n rId: AnyResourceRef,\n key: string,\n ): Promise<string | undefined> {\n return this.track(async () => {\n const data = await this.getKValueIfExists(rId, key);\n return data === undefined ? undefined : Buffer.from(data).toString();\n });\n }\n\n public async getKValueJsonIfExists<T>(rId: AnyResourceRef, key: string): Promise<T | undefined> {\n return this.track(async () => {\n const str = await this.getKValueStringIfExists(rId, key);\n if (str === undefined) return undefined;\n return JSON.parse(str) as T;\n });\n }\n\n //\n // Cache\n //\n // TODO\n\n //\n // High level ops\n //\n\n /** Resolves existing or create first level resource from */\n public getFutureFieldValue(rId: AnyRef, fieldName: string, fieldType: FutureFieldType): FieldRef {\n const data = Buffer.from(JSON.stringify({ fieldName, fieldType }));\n const getFieldResource = this.createEphemeral({ name: \"json/getField\", version: \"1\" }, data);\n this.setField({ resourceId: getFieldResource, fieldName: \"resource\" }, rId);\n return { resourceId: getFieldResource, fieldName: \"result\" };\n }\n\n //\n // Technical\n //\n\n public async getGlobalTxId() {\n this.globalTxIdWasAwaited = true;\n return await this.globalTxId;\n }\n\n /**\n * The change token this transaction was opened at, to hand back as\n * {@link resourceTree}'s `changedSinceToken` on a later poll. `undefined` when the server\n * does not advertise `treeChangedSince:v1`, and on a writable transaction, which is served\n * none.\n *\n * Store it only after a whole response has been applied - kept after a partial apply, the\n * dropped resources are never sent again - and discard it when the traversal shape changes,\n * which the token cannot express itself. Opaque: never parse one, and never compare across\n * instances.\n */\n public async getNextSinceToken(): Promise<Uint8Array | undefined> {\n return (await this.txOpen).sinceToken;\n }\n\n /** Closes output event stream */\n public async complete() {\n if (this._completed) return;\n this._completed = true;\n const completeResult = this.track(this.ll.complete());\n await this.drainAndAwaitPendingOps();\n await completeResult;\n }\n\n /** Await incoming message loop termination and throw\n * any leftover errors if it was unsuccessful */\n public async await() {\n await this.ll.await();\n }\n\n //\n // Tree streaming\n //\n\n /**\n * Perform a single `ResourceTree` walk and yield each visited resource.\n *\n * Items are yielded incrementally as stream frames arrive from server.\n *\n * The low-level transaction stream is still ordered, so each active request\n * must be consumed to completion (or closed via iterator `return()`/`throw()`)\n * before later responses can be dispatched.\n *\n * @param seeds - Roots for the traversal. May include both {@link ResourceData}\n * and {@link SignedResourceId} values in the same array.\n * @param opts.fieldFilter - Per-field predicate; matching field edges are not descended.\n * This wire field accepts only one filter. Compose multiple rules via\n * `treeFilter.and(...)` / `treeFilter.or(...)`.\n * @param opts.traverseStopRules - Per-resource predicate; matching resources are returned\n * with `traverseWasStopped = true` and their children are not visited.\n * This wire field accepts only one filter. Compose multiple rules via\n * `treeFilter.and(...)` / `treeFilter.or(...)`.\n * Deprecated, and ignored under `changedSinceToken`: a stop rule prunes on mutable state,\n * so on a delta walk it would hide a changed subtree behind a resource that merely\n * flipped a gate.\n * @param opts.includeKv - When true, each yielded item includes KV entries.\n * @param opts.maxDepth - Optional depth cap.\n * @param opts.changedSinceToken - A token from {@link getNextSinceToken}. Yields only\n * resources newer than it, and the walk ENDS at an unchanged one, so a change beneath one\n * is not reached. A response may reference resources it does not carry; read those back\n * with `unconditionalDepth`. An unusable token is answered with the full tree, not an error.\n * @param opts.unconditionalDepth - Emit every resource at or below this depth whatever its\n * change token says, the seed being depth 0. Ignored without `changedSinceToken`. Absent\n * differs from 0, which takes the seeds alone.\n */\n public resourceTree(\n seeds: (ResourceData | SignedResourceId)[],\n opts?: {\n fieldFilter?: ResourceAPI_Tree_Filter;\n /** @deprecated prune with `changedSinceToken` instead; ignored under one. */\n traverseStopRules?: ResourceAPI_Tree_Filter;\n includeKv?: boolean;\n maxDepth?: number;\n changedSinceToken?: Uint8Array;\n unconditionalDepth?: number;\n },\n ): AsyncIterable<ResourceTreeFrame> {\n if (seeds.length === 0) {\n throw new Error(\"resourceTree: at least one seed must be provided\");\n }\n\n const seedProtos = seeds.map((seed): ResourceAPI_Tree_SeedResource => {\n const signedId = typeof seed === \"string\" ? seed : seed.id;\n const { globalId, signature } = parseSignedResourceId(signedId);\n return { resourceId: globalId, resourceSignature: signature };\n });\n\n const firstSeed = seedProtos[0];\n\n const responseStream = this.ll.sendStream({\n oneofKind: \"resourceTree\",\n resourceTree: {\n // Keep compatibility with request shape that requires non-optional fields.\n resourceId: firstSeed?.resourceId ?? 0n,\n resourceSignature: firstSeed?.resourceSignature ?? new Uint8Array(0),\n seeds: seedProtos,\n fieldFilter: opts?.fieldFilter,\n traverseStopRules: opts?.traverseStopRules,\n includeKv: opts?.includeKv ?? false,\n maxDepth: opts?.maxDepth,\n showSoftDeletes: false,\n // Empty requests the full tree.\n changedSinceToken: opts?.changedSinceToken ?? new Uint8Array(0),\n unconditionalDepth: opts?.unconditionalDepth,\n },\n });\n\n return {\n [Symbol.asyncIterator](): AsyncIterator<ResourceTreeFrame> {\n const iterator = responseStream[Symbol.asyncIterator]();\n\n return {\n async next(): Promise<IteratorResult<ResourceTreeFrame>> {\n while (true) {\n const item = await iterator.next();\n if (item.done) return { value: undefined, done: true };\n\n const frame = item.value.resourceTree;\n if (frame === undefined) continue;\n\n if (frame.resource && resourceIsDeleted(frame.resource)) {\n continue;\n }\n\n if (!frame.resource) {\n // A traverseStopRules match, sent without a body.\n const id = createSignedResourceId(\n frame.resourceId,\n toResourceSignature(frame.resourceSignature),\n );\n return {\n value: { frameKind: \"stopMarker\", id, traverseWasStopped: true },\n done: false,\n };\n }\n\n return {\n value: {\n frameKind: \"resource\",\n ...protoToResource(frame.resource),\n kv: frame.kv,\n traverseWasStopped: frame.traverseWasStopped,\n },\n done: false,\n };\n }\n },\n async return(): Promise<IteratorResult<ResourceTreeFrame>> {\n if (iterator.return) await iterator.return();\n return { value: undefined, done: true };\n },\n async throw(error?: unknown): Promise<IteratorResult<ResourceTreeFrame>> {\n if (iterator.throw) await iterator.throw(error);\n throw error;\n },\n };\n },\n };\n }\n\n //\n // Helpers\n //\n\n private nextLocalResourceId(root: boolean): LocalResourceId {\n return createLocalResourceId(root, ++this.localResourceIdCounter, this.localTxId);\n }\n\n private static localTxIdCounter = 0;\n\n private static nextLocalTxId() {\n PlTransaction.localTxIdCounter++;\n if (PlTransaction.localTxIdCounter === MaxTxId) PlTransaction.localTxIdCounter = 1;\n return PlTransaction.localTxIdCounter;\n }\n}\n"],"mappings":";;;;;;;;;;;AAwHA,SAAgB,QAAQ,KAAiC;CACvD,OAAO,IAAI,eAAe,YAAY,KAAK,IAAI,eAAe,WAAW;AAC3E;AAEA,SAAgB,WAAW,KAAoC;CAC7D,IAAI,OAAO,QAAQ,UAAU,OAAO;CACpC,IAAI,OAAO,QAAQ,UAAU,OAAO;CACpC,OAAO,cAAc,GAAG;AAC1B;AAEA,SAAgB,aAAa,KAAsC;CACjE,OAAO,OAAO,QAAQ,YAAYA,cAAAA,mBAAmB,GAAG;AAC1D;AAEA,SAAgB,WAAW,KAAmC;CAC5D,OAAO,cAAc,IAAI,UAAU;AACrC;AAEA,SAAgB,cAAc,KAAiC;CAC7D,OACE,OAAO,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cAAAA,uBACT,MAAM,YACNH,cAAAA,oBAAoB,MAAM,iBAAiB,CAGR;QAAG,oBAAoB;OAAK;OAC/D,MAAM;MACR;MAGF,OAAO;OACL,OAAO;QACL,WAAW;QACX,GAAGQ,wBAAAA,gBAAgB,MAAM,QAAQ;QACjC,IAAI,MAAM;QACV,oBAAoB,MAAM;OAC5B;OACA,MAAM;MACR;KACF;IACF;IACA,MAAM,SAAqD;KACzD,IAAI,SAAS,QAAQ,MAAM,SAAS,OAAO;KAC3C,OAAO;MAAE,OAAO,KAAA;MAAW,MAAM;KAAK;IACxC;IACA,MAAM,MAAM,OAA6D;KACvE,IAAI,SAAS,OAAO,MAAM,SAAS,MAAM,KAAK;KAC9C,MAAM;IACR;GACF;EACF,EACF;CACF;CAMA,oBAA4B,MAAgC;EAC1D,OAAOS,cAAAA,sBAAsB,MAAM,EAAE,KAAK,wBAAwB,KAAK,SAAS;CAClF;CAEA,OAAe,mBAAmB;CAElC,OAAe,gBAAgB;EAC7B,cAAc;EACd,IAAI,cAAc,qBAAA,YAA8B,cAAc,mBAAmB;EACjF,OAAO,cAAc;CACvB;AACF"}
|
|
@@ -32,10 +32,13 @@ export type ResourceTreeItem = ResourceData & {
|
|
|
32
32
|
};
|
|
33
33
|
/** A single frame from the resourceTree() stream.
|
|
34
34
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
35
|
+
* A node the request's `traverseStopRules` matched is emitted as a lightweight marker
|
|
36
|
+
* instead of a full resource payload. Callers must narrow on `frameKind` before accessing
|
|
37
|
+
* type-specific fields.
|
|
38
38
|
* Note: `frameKind` is distinct from `ResourceData.kind` (which is the resource kind).
|
|
39
|
+
*
|
|
40
|
+
* Under `changedSinceToken` there are no markers: stop rules do not apply, and an unchanged
|
|
41
|
+
* resource produces no frame at all.
|
|
39
42
|
*/
|
|
40
43
|
export type ResourceTreeFrame = (ResourceTreeItem & {
|
|
41
44
|
frameKind: "resource";
|
|
@@ -110,6 +113,7 @@ export declare class PlTransaction {
|
|
|
110
113
|
private readonly finalPredicate;
|
|
111
114
|
private readonly sharedResourceDataCache;
|
|
112
115
|
private readonly enableFormattedErrors;
|
|
116
|
+
private readonly txOpen;
|
|
113
117
|
private readonly globalTxId;
|
|
114
118
|
private readonly localTxId;
|
|
115
119
|
/** Used in caching */
|
|
@@ -247,6 +251,18 @@ export declare class PlTransaction {
|
|
|
247
251
|
/** Resolves existing or create first level resource from */
|
|
248
252
|
getFutureFieldValue(rId: AnyRef, fieldName: string, fieldType: FutureFieldType): FieldRef;
|
|
249
253
|
getGlobalTxId(): Promise<bigint>;
|
|
254
|
+
/**
|
|
255
|
+
* The change token this transaction was opened at, to hand back as
|
|
256
|
+
* {@link resourceTree}'s `changedSinceToken` on a later poll. `undefined` when the server
|
|
257
|
+
* does not advertise `treeChangedSince:v1`, and on a writable transaction, which is served
|
|
258
|
+
* none.
|
|
259
|
+
*
|
|
260
|
+
* Store it only after a whole response has been applied - kept after a partial apply, the
|
|
261
|
+
* dropped resources are never sent again - and discard it when the traversal shape changes,
|
|
262
|
+
* which the token cannot express itself. Opaque: never parse one, and never compare across
|
|
263
|
+
* instances.
|
|
264
|
+
*/
|
|
265
|
+
getNextSinceToken(): Promise<Uint8Array | undefined>;
|
|
250
266
|
/** Closes output event stream */
|
|
251
267
|
complete(): Promise<void>;
|
|
252
268
|
/** Await incoming message loop termination and throw
|
|
@@ -270,14 +286,27 @@ export declare class PlTransaction {
|
|
|
270
286
|
* with `traverseWasStopped = true` and their children are not visited.
|
|
271
287
|
* This wire field accepts only one filter. Compose multiple rules via
|
|
272
288
|
* `treeFilter.and(...)` / `treeFilter.or(...)`.
|
|
289
|
+
* Deprecated, and ignored under `changedSinceToken`: a stop rule prunes on mutable state,
|
|
290
|
+
* so on a delta walk it would hide a changed subtree behind a resource that merely
|
|
291
|
+
* flipped a gate.
|
|
273
292
|
* @param opts.includeKv - When true, each yielded item includes KV entries.
|
|
274
293
|
* @param opts.maxDepth - Optional depth cap.
|
|
294
|
+
* @param opts.changedSinceToken - A token from {@link getNextSinceToken}. Yields only
|
|
295
|
+
* resources newer than it, and the walk ENDS at an unchanged one, so a change beneath one
|
|
296
|
+
* is not reached. A response may reference resources it does not carry; read those back
|
|
297
|
+
* with `unconditionalDepth`. An unusable token is answered with the full tree, not an error.
|
|
298
|
+
* @param opts.unconditionalDepth - Emit every resource at or below this depth whatever its
|
|
299
|
+
* change token says, the seed being depth 0. Ignored without `changedSinceToken`. Absent
|
|
300
|
+
* differs from 0, which takes the seeds alone.
|
|
275
301
|
*/
|
|
276
302
|
resourceTree(seeds: (ResourceData | SignedResourceId)[], opts?: {
|
|
277
303
|
fieldFilter?: ResourceAPI_Tree_Filter;
|
|
304
|
+
/** @deprecated prune with `changedSinceToken` instead; ignored under one. */
|
|
278
305
|
traverseStopRules?: ResourceAPI_Tree_Filter;
|
|
279
306
|
includeKv?: boolean;
|
|
280
307
|
maxDepth?: number;
|
|
308
|
+
changedSinceToken?: Uint8Array;
|
|
309
|
+
unconditionalDepth?: number;
|
|
281
310
|
}): AsyncIterable<ResourceTreeFrame>;
|
|
282
311
|
private nextLocalResourceId;
|
|
283
312
|
private static localTxIdCounter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction.d.ts","names":[],"sources":["../../src/core/transaction.ts"],"mappings":";;;;;;;;;;iBAkEiB;;;WAGN,UAAU,QAAQ;;;WAIlB,SAAS;;;iBAIH;EACf;EACA,OAAO;;;iBAIQ;EACf;EACA;;YAGU,mBAAmB;EAAiB,IAAI;EAAY
|
|
1
|
+
{"version":3,"file":"transaction.d.ts","names":[],"sources":["../../src/core/transaction.ts"],"mappings":";;;;;;;;;;iBAkEiB;;;WAGN,UAAU,QAAQ;;;WAIlB,SAAS;;;iBAIH;EACf;EACA,OAAO;;;iBAIQ;EACf;EACA;;YAGU,mBAAmB;EAAiB,IAAI;EAAY;;;;;;;;;;;;YAYpD,qBACP;EAAqB;;EACpB;EAAyB,IAAI;EAAkB;;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Q;EAIlD,WAAW,KAAK;EAOhB,YAAY,KAAK;EAWX,cAAc,KAAK,iBAAiB,QAAQ;EAwB5C,oBAAoB,KAAK,iBAAiB,QAAQ;EASlD,8BAA8B,KAAK,iBAAiB,QAAQ;EAI5D,oCACX,KAAK,iBACJ,QAAQ;EAIJ,UAAU,KAAK,gBAAgB,aAAa,OAAO;EAanD,aAAa,KAAK,gBAAgB;EAU5B,UAAU,KAAK,gBAAgB,cAAc,QAAQ;EAoBrD,gBAAgB,KAAK,gBAAgB,cAAc;EAInD,cAAc,GAAG,KAAK,gBAAgB,cAAc,QAAQ;EAI5D,kBACX,KAAK,gBACL,cACC,QAAQ;EAqBE,wBACX,KAAK,gBACL,cACC;EAOU,sBAAsB,GAAG,KAAK,gBAAgB,cAAc,QAAQ;;EAkB1E,oBAAoB,KAAK,QAAQ,mBAAmB,WAAW,kBAAkB;EAW3E,iBAAa;;;;;;;;;;;;EAgBb,qBAAqB,QAAQ;;EAK7B,YAAQ;;;EAUR,SAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuCX,aACL,QAAQ,eAAe,qBACvB;IACE,cAAc;;IAEd,oBAAoB;IACpB;IACA;IACA,oBAAoB;IACpB;MAED,cAAc;UAwFT;iBAIO;iBAEA"}
|
package/dist/core/transaction.js
CHANGED
|
@@ -122,6 +122,7 @@ var PlTransaction = class PlTransaction {
|
|
|
122
122
|
finalPredicate;
|
|
123
123
|
sharedResourceDataCache;
|
|
124
124
|
enableFormattedErrors;
|
|
125
|
+
txOpen;
|
|
125
126
|
globalTxId;
|
|
126
127
|
localTxId = PlTransaction.nextLocalTxId();
|
|
127
128
|
/** Used in caching */
|
|
@@ -149,14 +150,18 @@ var PlTransaction = class PlTransaction {
|
|
|
149
150
|
this.finalPredicate = finalPredicate;
|
|
150
151
|
this.sharedResourceDataCache = sharedResourceDataCache;
|
|
151
152
|
this.enableFormattedErrors = enableFormattedErrors;
|
|
152
|
-
this.
|
|
153
|
+
this.txOpen = this.sendSingleAndParse({
|
|
153
154
|
oneofKind: "txOpen",
|
|
154
155
|
txOpen: {
|
|
155
156
|
name,
|
|
156
157
|
enableFormattedErrors,
|
|
157
158
|
writable: writable ? 1 : 2
|
|
158
159
|
}
|
|
159
|
-
}, (r) =>
|
|
160
|
+
}, (r) => ({
|
|
161
|
+
id: notEmpty(r.txOpen.tx?.id),
|
|
162
|
+
sinceToken: r.txOpen.nextSinceToken.length > 0 ? r.txOpen.nextSinceToken : void 0
|
|
163
|
+
}));
|
|
164
|
+
this.globalTxId = this.txOpen.then((r) => r.id);
|
|
160
165
|
this.track(this.globalTxId);
|
|
161
166
|
this.globalTxId.catch((err) => {
|
|
162
167
|
if (!this.globalTxIdWasAwaited) console.warn(err);
|
|
@@ -424,7 +429,8 @@ var PlTransaction = class PlTransaction {
|
|
|
424
429
|
resourceGet: {
|
|
425
430
|
...this.toSignedResourceId(rId),
|
|
426
431
|
loadFields,
|
|
427
|
-
showSoftDeletes: false
|
|
432
|
+
showSoftDeletes: false,
|
|
433
|
+
changedSinceToken: /* @__PURE__ */ new Uint8Array(0)
|
|
428
434
|
}
|
|
429
435
|
}, (r) => protoToResource(notEmpty(r.resourceGet.resource)));
|
|
430
436
|
this._stat.rGetDataNetRequests++;
|
|
@@ -630,7 +636,8 @@ var PlTransaction = class PlTransaction {
|
|
|
630
636
|
resourceKeyValueList: {
|
|
631
637
|
...this.toSignedResourceId(rId),
|
|
632
638
|
startFrom: "",
|
|
633
|
-
limit: 0
|
|
639
|
+
limit: 0,
|
|
640
|
+
changedSinceToken: /* @__PURE__ */ new Uint8Array(0)
|
|
634
641
|
}
|
|
635
642
|
}, (r) => r.map((e) => e.resourceKeyValueList.record));
|
|
636
643
|
this._stat.kvListRequests++;
|
|
@@ -742,6 +749,20 @@ var PlTransaction = class PlTransaction {
|
|
|
742
749
|
this.globalTxIdWasAwaited = true;
|
|
743
750
|
return await this.globalTxId;
|
|
744
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* The change token this transaction was opened at, to hand back as
|
|
754
|
+
* {@link resourceTree}'s `changedSinceToken` on a later poll. `undefined` when the server
|
|
755
|
+
* does not advertise `treeChangedSince:v1`, and on a writable transaction, which is served
|
|
756
|
+
* none.
|
|
757
|
+
*
|
|
758
|
+
* Store it only after a whole response has been applied - kept after a partial apply, the
|
|
759
|
+
* dropped resources are never sent again - and discard it when the traversal shape changes,
|
|
760
|
+
* which the token cannot express itself. Opaque: never parse one, and never compare across
|
|
761
|
+
* instances.
|
|
762
|
+
*/
|
|
763
|
+
async getNextSinceToken() {
|
|
764
|
+
return (await this.txOpen).sinceToken;
|
|
765
|
+
}
|
|
745
766
|
/** Closes output event stream */
|
|
746
767
|
async complete() {
|
|
747
768
|
if (this._completed) return;
|
|
@@ -773,8 +794,18 @@ var PlTransaction = class PlTransaction {
|
|
|
773
794
|
* with `traverseWasStopped = true` and their children are not visited.
|
|
774
795
|
* This wire field accepts only one filter. Compose multiple rules via
|
|
775
796
|
* `treeFilter.and(...)` / `treeFilter.or(...)`.
|
|
797
|
+
* Deprecated, and ignored under `changedSinceToken`: a stop rule prunes on mutable state,
|
|
798
|
+
* so on a delta walk it would hide a changed subtree behind a resource that merely
|
|
799
|
+
* flipped a gate.
|
|
776
800
|
* @param opts.includeKv - When true, each yielded item includes KV entries.
|
|
777
801
|
* @param opts.maxDepth - Optional depth cap.
|
|
802
|
+
* @param opts.changedSinceToken - A token from {@link getNextSinceToken}. Yields only
|
|
803
|
+
* resources newer than it, and the walk ENDS at an unchanged one, so a change beneath one
|
|
804
|
+
* is not reached. A response may reference resources it does not carry; read those back
|
|
805
|
+
* with `unconditionalDepth`. An unusable token is answered with the full tree, not an error.
|
|
806
|
+
* @param opts.unconditionalDepth - Emit every resource at or below this depth whatever its
|
|
807
|
+
* change token says, the seed being depth 0. Ignored without `changedSinceToken`. Absent
|
|
808
|
+
* differs from 0, which takes the seeds alone.
|
|
778
809
|
*/
|
|
779
810
|
resourceTree(seeds, opts) {
|
|
780
811
|
if (seeds.length === 0) throw new Error("resourceTree: at least one seed must be provided");
|
|
@@ -797,7 +828,8 @@ var PlTransaction = class PlTransaction {
|
|
|
797
828
|
includeKv: opts?.includeKv ?? false,
|
|
798
829
|
maxDepth: opts?.maxDepth,
|
|
799
830
|
showSoftDeletes: false,
|
|
800
|
-
changedSinceToken: /* @__PURE__ */ new Uint8Array(0)
|
|
831
|
+
changedSinceToken: opts?.changedSinceToken ?? /* @__PURE__ */ new Uint8Array(0),
|
|
832
|
+
unconditionalDepth: opts?.unconditionalDepth
|
|
801
833
|
}
|
|
802
834
|
});
|
|
803
835
|
return { [Symbol.asyncIterator]() {
|