@mdbase-dev/sdk 0.1.0-beta.186 → 0.1.0-beta.188
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +189 -925
- package/dist/app-grant/client.js +7 -3
- package/dist/app-grant/client.js.map +1 -1
- package/dist/app-grant/setup.js +2 -2
- package/dist/app-grant/setup.js.map +1 -1
- package/dist/app-host/cloud-copy-host.js +1 -1
- package/dist/app-host/cloud-copy-host.js.map +1 -1
- package/dist/app-host/collection-readiness.js +10 -12
- package/dist/app-host/collection-readiness.js.map +1 -1
- package/dist/app-host/handover.js +1 -1
- package/dist/app-host/handover.js.map +1 -1
- package/dist/app-host/initial-sync.js +4 -4
- package/dist/app-host/initial-sync.js.map +1 -1
- package/dist/app-host/installation-sign-in.d.ts +3 -0
- package/dist/app-host/installation-sign-in.js +11 -2
- package/dist/app-host/installation-sign-in.js.map +1 -1
- package/dist/app-host/web-sign-in-session.d.ts +2 -0
- package/dist/app-host/web-sign-in-session.js +25 -3
- package/dist/app-host/web-sign-in-session.js.map +1 -1
- package/dist/application/asset-pins.json +4 -4
- package/dist/application/browser-capabilities.d.ts +1 -1
- package/dist/application/browser-capabilities.js +3 -2
- package/dist/application/browser-capabilities.js.map +1 -1
- package/dist/application/configuration.js +12 -12
- package/dist/application/configuration.js.map +1 -1
- package/dist/application/entry.d.ts +69 -108
- package/dist/application/entry.js +164 -91
- package/dist/application/entry.js.map +1 -1
- package/dist/application/front-door.d.ts +30 -12
- package/dist/application/front-door.js +2 -89
- package/dist/application/front-door.js.map +1 -1
- package/dist/application/packaged-assets.d.ts +5 -2
- package/dist/application/packaged-assets.js +9 -6
- package/dist/application/packaged-assets.js.map +1 -1
- package/dist/application/phase.d.ts +4 -1
- package/dist/application/phase.js +5 -4
- package/dist/application/phase.js.map +1 -1
- package/dist/application/private-collections.js +2 -1
- package/dist/application/private-collections.js.map +1 -1
- package/dist/application/registration-refusal.d.ts +1 -1
- package/dist/application/registration-refusal.js +1 -3
- package/dist/application/registration-refusal.js.map +1 -1
- package/dist/application/registration.js +1 -4
- package/dist/application/registration.js.map +1 -1
- package/dist/application/setup.d.ts +6 -9
- package/dist/application/setup.js +16 -0
- package/dist/application/setup.js.map +1 -1
- package/dist/application/tabs.d.ts +15 -2
- package/dist/application/tabs.js +35 -13
- package/dist/application/tabs.js.map +1 -1
- package/dist/application/unsynced-transfer.d.ts +1 -1
- package/dist/application/unsynced-transfer.js +3 -3
- package/dist/application/unsynced-transfer.js.map +1 -1
- package/dist/application/worker.js +969 -465
- package/dist/application/worker.js.map +1 -1
- package/dist/browser/mdbase-sdk.js +21319 -20764
- package/dist/browser/mdbase-sdk.min.js +65 -65
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +1 -0
- package/dist/browser.js.map +1 -1
- package/dist/client.d.ts +169 -81
- package/dist/client.js +236 -178
- package/dist/client.js.map +1 -1
- package/dist/codec.d.ts +15 -0
- package/dist/codec.js +44 -2
- package/dist/codec.js.map +1 -1
- package/dist/collection-api.d.ts +44 -0
- package/dist/collection-api.js +24 -0
- package/dist/collection-api.js.map +1 -0
- package/dist/collection-availability.js +1 -1
- package/dist/collection-availability.js.map +1 -1
- package/dist/contract-query.d.ts +22 -3
- package/dist/contract-query.js.map +1 -1
- package/dist/contract-records.d.ts +2 -1
- package/dist/contract-records.js.map +1 -1
- package/dist/describe-error.d.ts +1 -1
- package/dist/describe-error.js +7 -6
- package/dist/describe-error.js.map +1 -1
- package/dist/errors.d.ts +3 -5
- package/dist/errors.js +3 -8
- package/dist/errors.js.map +1 -1
- package/dist/files.d.ts +7 -5
- package/dist/files.js +16 -17
- package/dist/files.js.map +1 -1
- package/dist/holds.d.ts +2 -1
- package/dist/holds.js.map +1 -1
- package/dist/index.d.ts +7 -11
- package/dist/index.js +5 -7
- package/dist/index.js.map +1 -1
- package/dist/live.d.ts +2 -1
- package/dist/live.js.map +1 -1
- package/dist/manifest.d.ts +1 -1
- package/dist/manifest.js +1 -1
- package/dist/native/provenance.json +5 -5
- package/dist/native/runtime.wasm +0 -0
- package/dist/people.js +3 -7
- package/dist/people.js.map +1 -1
- package/dist/plain.d.ts +102 -0
- package/dist/plain.js +62 -0
- package/dist/plain.js.map +1 -0
- package/dist/presence.d.ts +2 -1
- package/dist/presence.js +3 -3
- package/dist/presence.js.map +1 -1
- package/dist/read-helpers.d.ts +8 -6
- package/dist/read-helpers.js +20 -13
- package/dist/read-helpers.js.map +1 -1
- package/dist/record-set.d.ts +98 -0
- package/dist/record-set.js +347 -0
- package/dist/record-set.js.map +1 -0
- package/dist/records/session.d.ts +2 -1
- package/dist/records/session.js +22 -20
- package/dist/records/session.js.map +1 -1
- package/dist/records/types.d.ts +5 -3
- package/dist/records/witness.d.ts +7 -4
- package/dist/records/witness.js +13 -8
- package/dist/records/witness.js.map +1 -1
- package/dist/records.js +5 -2
- package/dist/records.js.map +1 -1
- package/dist/saved-views.d.ts +12 -4
- package/dist/saved-views.js +51 -43
- package/dist/saved-views.js.map +1 -1
- package/dist/settle.d.ts +62 -0
- package/dist/settle.js +119 -0
- package/dist/settle.js.map +1 -0
- package/dist/status.d.ts +84 -28
- package/dist/status.js +54 -48
- package/dist/status.js.map +1 -1
- package/dist/testing/application.d.ts +16 -9
- package/dist/testing/application.js +48 -45
- package/dist/testing/application.js.map +1 -1
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/memory-contracts.d.ts +5 -0
- package/dist/testing/memory-contracts.js.map +1 -1
- package/dist/testing/memory-replica.d.ts +58 -5
- package/dist/testing/memory-replica.js +126 -19
- package/dist/testing/memory-replica.js.map +1 -1
- package/dist/testing/test-app.d.ts +9 -10
- package/dist/testing/test-app.js +8 -17
- package/dist/testing/test-app.js.map +1 -1
- package/dist/thin-client.d.ts +15 -38
- package/dist/thin-client.js +11 -20
- package/dist/thin-client.js.map +1 -1
- package/dist/transport/control-route.js +1 -1
- package/dist/transport/control-route.js.map +1 -1
- package/dist/transport/relay-close.d.ts +0 -1
- package/dist/transport/relay-close.js +4 -2
- package/dist/transport/relay-close.js.map +1 -1
- package/dist/transport/relay-pipe.js +2 -2
- package/dist/transport/relay-pipe.js.map +1 -1
- package/dist/type-packs.js +4 -4
- package/dist/type-packs.js.map +1 -1
- package/dist/validation.d.ts +1 -1
- package/dist/validation.js +2 -2
- package/dist/validation.js.map +1 -1
- package/dist/values.d.ts +4 -20
- package/dist/values.js +3 -49
- package/dist/values.js.map +1 -1
- package/dist/view-sources.d.ts +2 -1
- package/dist/view-sources.js +1 -1
- package/dist/view-sources.js.map +1 -1
- package/dist/wire.d.ts +7 -3
- package/dist/wire.js +4 -4
- package/dist/wire.js.map +1 -1
- package/dist/write-ledger.d.ts +2 -1
- package/dist/write-ledger.js +3 -6
- package/dist/write-ledger.js.map +1 -1
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -19,6 +19,7 @@ import { list, uint, uuid as uuidCodec, uuidFromBytes } from "./codec.js";
|
|
|
19
19
|
import { recordValidations, captureValidation, bindValidation } from "./validation.js";
|
|
20
20
|
import { isMdbaseError, mdbaseError, MdbaseError } from "./errors.js";
|
|
21
21
|
import { FilesApi } from "./files.js";
|
|
22
|
+
import { settleWrite } from "./settle.js";
|
|
22
23
|
import { readMany as readManyRecords, queryPages as readQueryPages } from "./read-helpers.js";
|
|
23
24
|
import { RecordsApi } from "./records.js";
|
|
24
25
|
import { executeSavedView, listSavedViews, readSavedViewSource } from "./saved-views.js";
|
|
@@ -26,6 +27,7 @@ import { createViewSource, updateViewSource, readViewSourceRecord } from "./view
|
|
|
26
27
|
import { contractRecordWire, contractWritePlan } from "./contract-records.js";
|
|
27
28
|
import { contractQueryResult } from "./contract-query.js";
|
|
28
29
|
import { LiveResult } from "./live.js";
|
|
30
|
+
import { RecordSet } from "./record-set.js";
|
|
29
31
|
import { PresenceApi } from "./presence.js";
|
|
30
32
|
import { rememberWitness, witnessesFor } from "./witness.js";
|
|
31
33
|
import { API_VERSIONS, problemCodec, schemaToError, Session, withMutation } from "./session.js";
|
|
@@ -35,7 +37,10 @@ import { diffEdits, revisionOf, toFmMap, toValue, uuidv7, valueEquals } from "./
|
|
|
35
37
|
import { pendingDevice, recoveryKeyCreated, recoveryKeyStatus, accountKeyStatus, } from "./private.js";
|
|
36
38
|
import { backlinksResult, resolveLinksResult, describeResult, newTypeFile, describeTypingResult, DESCRIBE_TYPING_MAX_PATHS, DESCRIBE_TYPING_MAX_TYPES, listPendingResult, listResourcesResult, resourceView, } from "./wire.js";
|
|
37
39
|
import { memoryWriteLedgerStore, WriteLedger } from "./write-ledger.js";
|
|
40
|
+
import { decodePlain, plainAnswer } from "./codec.js";
|
|
38
41
|
import { changesResult, conflictEntry, fenceApply, fenceReport, fenceResult, hold, holdResolution, include as includeCodec, queryResult, queryUpdate, receipt as receiptCodec, recordView, submitParams, submitResult, syncStatus, appliedPrefix as appliedPrefixCodec, appliedPrefixParams, } from "./wire.js";
|
|
42
|
+
/** Changes asked for at a time while a watch catches up from its cursor. */
|
|
43
|
+
const CHANGES_PAGE = 1000;
|
|
39
44
|
/** Snapshot plain query data without losing Float64 kind/signed zero, exact
|
|
40
45
|
* integers, ordered Maps or omitted fields (structuredClone loses prototypes). */
|
|
41
46
|
function snapshotQueryValue(value) {
|
|
@@ -125,6 +130,8 @@ export class Write {
|
|
|
125
130
|
* is a final non-publication, not success. Without files, settles at confirmation.
|
|
126
131
|
* Rejects rejected/unknown outcomes; not a native custody/durable ACK gate. */
|
|
127
132
|
published;
|
|
133
|
+
/** The record a single-record write targets (create, update, replace, rename, delete). */
|
|
134
|
+
recordId;
|
|
128
135
|
constructor(receipt) {
|
|
129
136
|
this.receipt = receipt;
|
|
130
137
|
this.confirmed = new Promise((resolve, reject) => (this.settle = { resolve, reject }));
|
|
@@ -227,8 +234,11 @@ export class LiveQuery {
|
|
|
227
234
|
fn(this, null);
|
|
228
235
|
return () => this.listeners.delete(fn);
|
|
229
236
|
}
|
|
230
|
-
/** Change the query (for example a larger `limit` window). Re-subscribes.
|
|
231
|
-
|
|
237
|
+
/** Change the query (for example a larger `limit` window). Re-subscribes.
|
|
238
|
+
* `o.include` replaces what each record carries; without it the current
|
|
239
|
+
* include is kept. */
|
|
240
|
+
async setQuery(query, o = {}) {
|
|
241
|
+
const { include = this.include } = o;
|
|
232
242
|
this.query = query;
|
|
233
243
|
this.include = include;
|
|
234
244
|
this.result.markStale();
|
|
@@ -538,9 +548,9 @@ export class MdbaseClient {
|
|
|
538
548
|
/** Recheck session identity after waiting for catch-up (SDK-internal use). */
|
|
539
549
|
async readyForRead(signal, immediate = false) {
|
|
540
550
|
if (!this.fenceReads)
|
|
541
|
-
return immediate ? this.currentSession() : this.ready(signal);
|
|
551
|
+
return immediate ? this.currentSession() : this.ready({ signal });
|
|
542
552
|
for (;;) {
|
|
543
|
-
const s = immediate ? this.currentSession() : await this.ready(signal);
|
|
553
|
+
const s = immediate ? this.currentSession() : await this.ready({ signal });
|
|
544
554
|
await this.readFence(signal);
|
|
545
555
|
if (s === this.session && !s.closed && this.readFencedAt === null)
|
|
546
556
|
return s;
|
|
@@ -574,8 +584,9 @@ export class MdbaseClient {
|
|
|
574
584
|
}
|
|
575
585
|
return this.session;
|
|
576
586
|
}
|
|
577
|
-
/** @internal Wait until a session is open (or the client closes). */
|
|
578
|
-
async ready(
|
|
587
|
+
/** @internal Wait until a session is open (or the client closes); `o.signal` cancels the wait. */
|
|
588
|
+
async ready(o = {}) {
|
|
589
|
+
const signal = o.signal;
|
|
579
590
|
for (;;) {
|
|
580
591
|
if (this.linkState === "closed")
|
|
581
592
|
throw this.linkError ?? mdbaseError("unavailable", "client closed");
|
|
@@ -589,19 +600,18 @@ export class MdbaseClient {
|
|
|
589
600
|
});
|
|
590
601
|
}
|
|
591
602
|
}
|
|
592
|
-
/**
|
|
593
|
-
* Advanced: run a request on the current session; on a dropped link, wait for the
|
|
594
|
-
* reconnect and retry when `retry` (idempotent requests only).
|
|
595
|
-
*/
|
|
596
603
|
async call(method, p, o = {}) {
|
|
597
604
|
// Pin the decoder before any route/request await. Observation must use the
|
|
598
605
|
// SAME codec passed to dispatch, not caller options mutated while pending.
|
|
599
606
|
const codec = o.codec;
|
|
600
607
|
const read = this.fenceReads && READ_METHODS.has(method);
|
|
601
608
|
for (;;) {
|
|
602
|
-
const s = read ? await this.readyForRead(o.signal, !o.retry) : o.retry ? await this.ready(o.signal) : this.currentSession();
|
|
609
|
+
const s = read ? await this.readyForRead(o.signal, !o.retry) : o.retry ? await this.ready({ signal: o.signal }) : this.currentSession();
|
|
603
610
|
try {
|
|
604
|
-
const r = await s.request(method, p, {
|
|
611
|
+
const r = await s.request(method, p, {
|
|
612
|
+
...(o.signal ? { signal: o.signal } : {}),
|
|
613
|
+
...(codec ? { codec: plainAnswer(codec) } : {}),
|
|
614
|
+
});
|
|
605
615
|
if (read && (s !== this.session || s.closed || this.closing)) {
|
|
606
616
|
if (o.retry && !this.closing)
|
|
607
617
|
continue;
|
|
@@ -653,7 +663,7 @@ export class MdbaseClient {
|
|
|
653
663
|
return;
|
|
654
664
|
let u;
|
|
655
665
|
try {
|
|
656
|
-
u = queryUpdate
|
|
666
|
+
u = decodePlain(queryUpdate, p);
|
|
657
667
|
}
|
|
658
668
|
catch {
|
|
659
669
|
return;
|
|
@@ -673,7 +683,7 @@ export class MdbaseClient {
|
|
|
673
683
|
});
|
|
674
684
|
s.onPush("receipt", (p) => {
|
|
675
685
|
try {
|
|
676
|
-
const r = receiptCodec
|
|
686
|
+
const r = decodePlain(receiptCodec, p);
|
|
677
687
|
if (this.session === s)
|
|
678
688
|
this.noteReceipt(r);
|
|
679
689
|
const w = this.writes.get(r.mutation);
|
|
@@ -714,7 +724,7 @@ export class MdbaseClient {
|
|
|
714
724
|
});
|
|
715
725
|
s.onPush("conflicts", (p) => {
|
|
716
726
|
try {
|
|
717
|
-
this.emitConflicts(list(conflictEntry)
|
|
727
|
+
this.emitConflicts(decodePlain(list(conflictEntry), p));
|
|
718
728
|
}
|
|
719
729
|
catch {
|
|
720
730
|
// ignore
|
|
@@ -877,11 +887,12 @@ export class MdbaseClient {
|
|
|
877
887
|
* revalidate the authenticated session and all readiness gates after any await.
|
|
878
888
|
* Missing chain or any error means remain hosted; never substitute asOf or the
|
|
879
889
|
* current head/generations. Decoding does not authenticate a witness or switch.
|
|
890
|
+
* `o.signal` cancels the read.
|
|
880
891
|
*/
|
|
881
|
-
async appliedPrefix(seq,
|
|
892
|
+
async appliedPrefix(seq, o = {}) {
|
|
882
893
|
const p = appliedPrefixParams.enc({ seq });
|
|
883
894
|
const r = await this.call("applied_prefix", p, {
|
|
884
|
-
codec: appliedPrefixCodec, retry: false, ...(signal ? { signal } : {}),
|
|
895
|
+
codec: appliedPrefixCodec, retry: false, ...(o.signal ? { signal: o.signal } : {}),
|
|
885
896
|
});
|
|
886
897
|
if (r.seq !== seq || (r.chain !== undefined && (seq === 0 || r.appliedThrough < seq))) {
|
|
887
898
|
throw mdbaseError("invalid_request", "applied_prefix: inconsistent historical proof", "prefix_response");
|
|
@@ -915,9 +926,9 @@ export class MdbaseClient {
|
|
|
915
926
|
* definition and schema and each contract's schemas, bounded by the replica
|
|
916
927
|
* (`too_large` when over budget; read them with `resources.get` instead).
|
|
917
928
|
* A producer that cannot describe definitions fails `upgrade_required`.
|
|
929
|
+
* `o.signal` cancels the read.
|
|
918
930
|
*/
|
|
919
|
-
async describe(
|
|
920
|
-
const o = options instanceof AbortSignal ? { signal: options } : options;
|
|
931
|
+
async describe(o = {}) {
|
|
921
932
|
const result = await this.call("describe", o.definitions ? params([[0, true]]) : null, {
|
|
922
933
|
codec: describeResult,
|
|
923
934
|
retry: true,
|
|
@@ -952,14 +963,15 @@ export class MdbaseClient {
|
|
|
952
963
|
* Core's temporal typing for top-level fields under a query's exact `types` OR list
|
|
953
964
|
* (`date` / `date_time` only when every type agrees; otherwise `none`). The answer is
|
|
954
965
|
* bound to a catalog generation; nested paths are `none`. The SDK never infers typing.
|
|
966
|
+
* `o.signal` cancels the read.
|
|
955
967
|
*/
|
|
956
|
-
describeTyping(types, paths,
|
|
968
|
+
describeTyping(types, paths, o = {}) {
|
|
957
969
|
const strings = (a, max) => Array.isArray(a) && a.length <= max && a.every((s) => typeof s === "string" && s.length > 0 && s.length <= 1024);
|
|
958
970
|
if (!strings(types, DESCRIBE_TYPING_MAX_TYPES) || !strings(paths, DESCRIBE_TYPING_MAX_PATHS)) {
|
|
959
971
|
return Promise.reject(mdbaseError("invalid_request", "describe_typing: at most 64 types and 256 non-empty paths", "typing_bounds"));
|
|
960
972
|
}
|
|
961
973
|
return this.call("describe_typing", params([[0, [...types]], [1, [...paths]]]), {
|
|
962
|
-
codec: describeTypingResult, retry: true, ...(signal ? { signal } : {}),
|
|
974
|
+
codec: describeTypingResult, retry: true, ...(o.signal ? { signal: o.signal } : {}),
|
|
963
975
|
}).then((r) => {
|
|
964
976
|
if (r.fields.length !== paths.length || r.fields.some((f, i) => f.path !== paths[i])) {
|
|
965
977
|
throw mdbaseError("internal", "describe_typing answered for different paths", "invalid_typing_response");
|
|
@@ -967,12 +979,13 @@ export class MdbaseClient {
|
|
|
967
979
|
return r;
|
|
968
980
|
});
|
|
969
981
|
}
|
|
970
|
-
/** One record by ID or path. Throws `not_found`.
|
|
971
|
-
|
|
982
|
+
/** One record by ID or path. Throws `not_found`. `o.include` adds the body
|
|
983
|
+
* and other parts to the record; `o.signal` cancels the read. */
|
|
984
|
+
get(ref, o = {}) {
|
|
972
985
|
return this.call("get", params([
|
|
973
986
|
[0, refValue(ref)],
|
|
974
|
-
[1, includeValue(include)],
|
|
975
|
-
]), { codec: recordView, retry: true, ...(signal ? { signal } : {}) });
|
|
987
|
+
[1, includeValue(o.include)],
|
|
988
|
+
]), { codec: recordView, retry: true, ...(o.signal ? { signal: o.signal } : {}) });
|
|
976
989
|
}
|
|
977
990
|
/**
|
|
978
991
|
* One record's valid view through a record contract, mapped by the replica's
|
|
@@ -981,11 +994,12 @@ export class MdbaseClient {
|
|
|
981
994
|
* `invalid_request`/`data_contract_ambiguous` (select `type`; details: the types),
|
|
982
995
|
* `invalid_record`/`data_contract_record_invalid` (with issues), and
|
|
983
996
|
* `upgrade_required`/`contract_records_unsupported` from an older replica.
|
|
997
|
+
* `o.signal` cancels the read.
|
|
984
998
|
*/
|
|
985
|
-
async getContract(ref, contract,
|
|
999
|
+
async getContract(ref, contract, o = {}) {
|
|
986
1000
|
const r = await this.contractCall("get_contract_v1", params([
|
|
987
1001
|
[0, 1], [1, refValue(ref)], [2, contract.id], [3, contract.version], [4, contract.type],
|
|
988
|
-
]), contractRecordWire, signal);
|
|
1002
|
+
]), (answer) => decodePlain(contractRecordWire, answer), o.signal);
|
|
989
1003
|
if (r.version !== 1)
|
|
990
1004
|
throw mdbaseError("upgrade_required", "unsupported contract record", "contract_records_unsupported");
|
|
991
1005
|
return {
|
|
@@ -999,28 +1013,41 @@ export class MdbaseClient {
|
|
|
999
1013
|
[0, 1], [1, contract.id], [2, contract.version], [3, contract.type],
|
|
1000
1014
|
[4, target === undefined ? undefined : refValue(target)], [5, fields], [6, unset?.length ? unset : undefined],
|
|
1001
1015
|
[7, promote ? true : undefined],
|
|
1002
|
-
|
|
1016
|
+
// Exact: the plan's patch and base go back in an op unchanged.
|
|
1017
|
+
]), contractWritePlan.dec, signal);
|
|
1003
1018
|
if (plan.version !== 1)
|
|
1004
1019
|
throw mdbaseError("upgrade_required", "unsupported contract write plan", "contract_records_unsupported");
|
|
1005
1020
|
return plan;
|
|
1006
1021
|
}
|
|
1007
|
-
contractCall(method, p,
|
|
1008
|
-
|
|
1022
|
+
async contractCall(method, p, decode, signal) {
|
|
1023
|
+
let answer;
|
|
1024
|
+
try {
|
|
1025
|
+
answer = await this.call(method, p, { retry: true, ...(signal ? { signal } : {}) });
|
|
1026
|
+
}
|
|
1027
|
+
catch (e) {
|
|
1009
1028
|
if (isMdbaseError(e, "invalid_request") && e.reason === "unknown_method") {
|
|
1010
1029
|
throw mdbaseError("upgrade_required", "this replica does not serve contract-scoped records", "contract_records_unsupported");
|
|
1011
1030
|
}
|
|
1012
1031
|
throw e;
|
|
1013
|
-
}
|
|
1032
|
+
}
|
|
1033
|
+
try {
|
|
1034
|
+
return decode(answer);
|
|
1035
|
+
}
|
|
1036
|
+
catch (e) {
|
|
1037
|
+
throw schemaToError(e, `${method} result`);
|
|
1038
|
+
}
|
|
1014
1039
|
}
|
|
1015
1040
|
/** Bounded ordered ordinary gets. Missing/refused reads fail the batch;
|
|
1016
|
-
* duplicate slots and the original signal are retained, not a snapshot.
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
|
|
1041
|
+
* duplicate slots and the original signal are retained, not a snapshot.
|
|
1042
|
+
* `o.include` and `o.signal` apply to every get; `o.concurrency` bounds the
|
|
1043
|
+
* gets in flight. */
|
|
1044
|
+
readMany(refs, o = {}) {
|
|
1045
|
+
return readManyRecords(this, refs, o);
|
|
1046
|
+
}
|
|
1047
|
+
/** One record, or `null` when there is none. `o` is as for {@link get}. */
|
|
1048
|
+
async find(ref, o = {}) {
|
|
1022
1049
|
try {
|
|
1023
|
-
return await this.get(ref,
|
|
1050
|
+
return await this.get(ref, o);
|
|
1024
1051
|
}
|
|
1025
1052
|
catch (e) {
|
|
1026
1053
|
if (isMdbaseError(e, "not_found"))
|
|
@@ -1031,16 +1058,18 @@ export class MdbaseClient {
|
|
|
1031
1058
|
/**
|
|
1032
1059
|
* One page of a query, evaluated by the replica (body predicates and links
|
|
1033
1060
|
* included, §4). Pass `result.cursor` back as `query.cursor` for the next.
|
|
1034
|
-
* `
|
|
1061
|
+
* `o.include` adds the body and other parts to each record; `o.signal`
|
|
1062
|
+
* cancels the read.
|
|
1063
|
+
* `o.contract` is not supported by the live replica dispatch. Supplying it
|
|
1035
1064
|
* returns `invalid_request` with reason `unknown_param`; no contract filter
|
|
1036
1065
|
* is applied. The option remains encoded unchanged for API compatibility.
|
|
1037
1066
|
*/
|
|
1038
|
-
query(query,
|
|
1067
|
+
query(query, o = {}) {
|
|
1039
1068
|
return this.call("query", params([
|
|
1040
1069
|
[0, toValue(query)],
|
|
1041
|
-
[1, includeValue(include)],
|
|
1042
|
-
[2,
|
|
1043
|
-
]), { codec: queryResult, retry: true, ...(signal ? { signal } : {}) });
|
|
1070
|
+
[1, includeValue(o.include)],
|
|
1071
|
+
[2, o.contract],
|
|
1072
|
+
]), { codec: queryResult, retry: true, ...(o.signal ? { signal: o.signal } : {}) });
|
|
1044
1073
|
}
|
|
1045
1074
|
/** Canonical contract projections with validation issues and ordinary cursor
|
|
1046
1075
|
* metadata. Positive page sizes above the producer's 128-record maximum clamp
|
|
@@ -1110,7 +1139,8 @@ export class MdbaseClient {
|
|
|
1110
1139
|
}
|
|
1111
1140
|
/** Definitions: `mdbase.yaml`, type files, contract files (§4.1). */
|
|
1112
1141
|
resources = {
|
|
1113
|
-
|
|
1142
|
+
/** One definition by path. `o.signal` cancels the read. */
|
|
1143
|
+
get: (path, o = {}) => this.call("get_resource", params([[0, path]]), { codec: resourceView, retry: true, ...(o.signal ? { signal: o.signal } : {}) }),
|
|
1114
1144
|
/** One bounded resource inventory page. Text defaults to false; limit defaults
|
|
1115
1145
|
* to 64 (maximum 128). Preserve folder/text/limit on continuation. Collect all
|
|
1116
1146
|
* pages before assessing a complete snapshot; stale/expired cursors require
|
|
@@ -1129,15 +1159,15 @@ export class MdbaseClient {
|
|
|
1129
1159
|
if (o.baseRevision && o.mustNotExist) {
|
|
1130
1160
|
return Promise.reject(mdbaseError("invalid_request", "baseRevision and mustNotExist are exclusive"));
|
|
1131
1161
|
}
|
|
1132
|
-
return this.
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1162
|
+
return this.submit([{
|
|
1163
|
+
kind: "resource_put",
|
|
1164
|
+
path,
|
|
1165
|
+
doc,
|
|
1166
|
+
...(o.baseRevision ? { baseRevision: o.baseRevision } : {}),
|
|
1167
|
+
...(o.mustNotExist ? { mustNotExist: true } : {}),
|
|
1168
|
+
}], o);
|
|
1139
1169
|
},
|
|
1140
|
-
delete: (path, o = {}) => this.
|
|
1170
|
+
delete: (path, o = {}) => this.submit([{ kind: "resource_delete", path, ...(o.baseRevision ? { baseRevision: o.baseRevision } : {}) }], o),
|
|
1141
1171
|
};
|
|
1142
1172
|
/** Type packs: `plan` (writes nothing), review, then `apply(plan)` as one
|
|
1143
1173
|
* guarded all-or-nothing mutation (needs `definitions.manage`). */
|
|
@@ -1155,19 +1185,24 @@ export class MdbaseClient {
|
|
|
1155
1185
|
updateSource: (seen, document, options) => updateViewSource(this, seen, document, options),
|
|
1156
1186
|
getSourceRecord: (source, options = {}) => readViewSourceRecord(this, source, options),
|
|
1157
1187
|
/** `mdbase.view` sources (spec 12 `list_views` shape), resolved over served
|
|
1158
|
-
* contract queries: the native runtime has no `list_views` RPC.
|
|
1159
|
-
|
|
1188
|
+
* contract queries: the native runtime has no `list_views` RPC. `o.folder`
|
|
1189
|
+
* keeps the sources under one folder; `o.signal` cancels the read. */
|
|
1190
|
+
list: (o = {}) => listSavedViews(this, o),
|
|
1160
1191
|
/** Spec 11 named-view resolution, executed as one ordinary native query. */
|
|
1161
1192
|
execute: (source, view, o = {}) => executeSavedView(this, source, view, o),
|
|
1162
|
-
|
|
1193
|
+
/** The current source document of one `mdbase.view` record. `o.signal`
|
|
1194
|
+
* cancels the read. */
|
|
1195
|
+
readSource: (source, o = {}) => readSavedViewSource(this, source, o),
|
|
1163
1196
|
};
|
|
1164
1197
|
/**
|
|
1165
1198
|
* Pending mutations of this session's owner (its grant, or the device), oldest
|
|
1166
1199
|
* first (§6): a new session after a reload sees writes made before it. Follow
|
|
1167
1200
|
* each with `awaitReceipt`, or keep the `mutationId` you chose and call
|
|
1168
|
-
* `receipt(id)` / `awaitReceipt(id)`. Pages through everything.
|
|
1201
|
+
* `receipt(id)` / `awaitReceipt(id)`. Pages through everything; `o.signal`
|
|
1202
|
+
* cancels the read.
|
|
1169
1203
|
*/
|
|
1170
|
-
async pendingWrites(
|
|
1204
|
+
async pendingWrites(o = {}) {
|
|
1205
|
+
const signal = o.signal;
|
|
1171
1206
|
const out = [];
|
|
1172
1207
|
let after;
|
|
1173
1208
|
for (;;) {
|
|
@@ -1190,31 +1225,34 @@ export class MdbaseClient {
|
|
|
1190
1225
|
return this.preflight({ kind: "delete", id: refId(target) });
|
|
1191
1226
|
}
|
|
1192
1227
|
async preflight(op) {
|
|
1193
|
-
const
|
|
1194
|
-
const r = w.receipt;
|
|
1228
|
+
const r = (await this.submit([op], { dryRun: true })).receipt;
|
|
1195
1229
|
if (r.state === "rejected")
|
|
1196
1230
|
throw new MdbaseError(r.problem);
|
|
1197
1231
|
return r.preflight ?? { rewrites: [], broken: [] };
|
|
1198
1232
|
}
|
|
1199
|
-
/** Every page of a query.
|
|
1200
|
-
*
|
|
1201
|
-
|
|
1233
|
+
/** Every page of a query. `o.include` and `o.signal` apply to every page.
|
|
1234
|
+
* Cursor resets require explicit caller clearing through `o.onReset`;
|
|
1235
|
+
* otherwise cursor_expired/cursor_stale is thrown unchanged. The query and
|
|
1236
|
+
* `o` are captured when iteration starts. */
|
|
1237
|
+
async *pages(query, o = {}) {
|
|
1238
|
+
const { include, signal, onReset } = o;
|
|
1202
1239
|
const original = snapshotQueryValue(query);
|
|
1203
1240
|
const originalInclude = include ? { ...include } : undefined;
|
|
1241
|
+
const read = { ...(originalInclude ? { include: originalInclude } : {}), ...(signal ? { signal } : {}) };
|
|
1204
1242
|
let cursor = original.cursor, restarted = false, pagesYielded = 0;
|
|
1205
1243
|
delete original.cursor;
|
|
1206
1244
|
for (;;) {
|
|
1207
1245
|
signal?.throwIfAborted();
|
|
1208
1246
|
let page;
|
|
1209
1247
|
try {
|
|
1210
|
-
page = await this.query(cursor !== undefined ? { ...original, cursor } : { ...original },
|
|
1248
|
+
page = await this.query(cursor !== undefined ? { ...original, cursor } : { ...original }, read);
|
|
1211
1249
|
}
|
|
1212
1250
|
catch (error) {
|
|
1213
|
-
if (!
|
|
1251
|
+
if (!onReset || restarted || !isMdbaseError(error, "invalid_request") || error.recovery !== "fix_request" ||
|
|
1214
1252
|
(error.reason !== "cursor_expired" && error.reason !== "cursor_stale"))
|
|
1215
1253
|
throw error;
|
|
1216
1254
|
signal?.throwIfAborted();
|
|
1217
|
-
await
|
|
1255
|
+
await onReset({ reason: error.reason, error, pagesYielded, ...(signal ? { signal } : {}) });
|
|
1218
1256
|
signal?.throwIfAborted();
|
|
1219
1257
|
restarted = true;
|
|
1220
1258
|
cursor = undefined;
|
|
@@ -1232,16 +1270,29 @@ export class MdbaseClient {
|
|
|
1232
1270
|
cursor = page.cursor;
|
|
1233
1271
|
}
|
|
1234
1272
|
}
|
|
1235
|
-
/** Shared alias of pages(): whole native pages, no implicit cursor reset.
|
|
1236
|
-
|
|
1237
|
-
|
|
1273
|
+
/** Shared alias of pages(): whole native pages, no implicit cursor reset.
|
|
1274
|
+
* `o.include` and `o.signal` apply to every page; `o.onReset` opts in to one
|
|
1275
|
+
* first-page restart. */
|
|
1276
|
+
queryPages(query, o = {}) {
|
|
1277
|
+
return readQueryPages(this, query, o);
|
|
1238
1278
|
}
|
|
1239
1279
|
/**
|
|
1240
|
-
*
|
|
1241
|
-
*
|
|
1280
|
+
* Every record of the chosen types (default: all), read completely and then
|
|
1281
|
+
* kept current: the set an app lists, searches and renders from. Close it
|
|
1282
|
+
* when done.
|
|
1242
1283
|
*/
|
|
1243
|
-
|
|
1244
|
-
|
|
1284
|
+
recordSet(options = {}) {
|
|
1285
|
+
return new RecordSet(this, options);
|
|
1286
|
+
}
|
|
1287
|
+
/**
|
|
1288
|
+
* A live query: one window of at most 1,000 records and 1 MiB, with the
|
|
1289
|
+
* replica's order and counts. Subscribe for pushes; keep lists cheap with a
|
|
1290
|
+
* `limit` window and no bodies, and widen the window with `setQuery`. For a
|
|
1291
|
+
* whole collection use `recordSet`. `o.include` adds the body and other
|
|
1292
|
+
* parts to each record.
|
|
1293
|
+
*/
|
|
1294
|
+
live(query, o = {}) {
|
|
1295
|
+
const l = new LiveQuery(this, query, o.include);
|
|
1245
1296
|
this.lives.add(l);
|
|
1246
1297
|
void this._resubscribeLive(l, true);
|
|
1247
1298
|
return l;
|
|
@@ -1296,28 +1347,31 @@ export class MdbaseClient {
|
|
|
1296
1347
|
void this.call("unsubscribe", params([[0, id]])).catch(() => { });
|
|
1297
1348
|
}
|
|
1298
1349
|
}
|
|
1299
|
-
/** One page of the change feed
|
|
1300
|
-
|
|
1350
|
+
/** One page of the change feed, from `o.cursor` (default: the start) and of
|
|
1351
|
+
* at most `o.limit` changes. A `reset: true` result means "re-read what you
|
|
1352
|
+
* cache". `o.signal` cancels the read. */
|
|
1353
|
+
changes(o = {}) {
|
|
1301
1354
|
return this.call("changes", params([
|
|
1302
|
-
[0, cursor ?? null],
|
|
1303
|
-
[1, limit],
|
|
1304
|
-
]), { codec: changesResult, retry: true, ...(signal ? { signal } : {}) });
|
|
1355
|
+
[0, o.cursor ?? null],
|
|
1356
|
+
[1, o.limit],
|
|
1357
|
+
]), { codec: changesResult, retry: true, ...(o.signal ? { signal: o.signal } : {}) });
|
|
1305
1358
|
}
|
|
1306
1359
|
/**
|
|
1307
|
-
* Follow the change feed from `cursor
|
|
1308
|
-
* means the app must re-read what it caches. Survives
|
|
1360
|
+
* Follow the change feed from `o.cursor` (default: the start). `onChanges` gets
|
|
1361
|
+
* every batch; `reset: true` means the app must re-read what it caches. Survives
|
|
1362
|
+
* reconnects.
|
|
1309
1363
|
* `ready` confirms the first watch ACK, not a complete/current collection cache.
|
|
1310
1364
|
* After reconnect, observe `state`; an already-resolved promise is not freshness.
|
|
1311
1365
|
*/
|
|
1312
|
-
watchChanges(
|
|
1313
|
-
let last = cursor, stopped = false, epoch = 0;
|
|
1366
|
+
watchChanges(onChanges, o = {}) {
|
|
1367
|
+
let last = o.cursor, stopped = false, epoch = 0;
|
|
1314
1368
|
let state = "starting", error = null;
|
|
1315
1369
|
let active = null, controller = null;
|
|
1316
1370
|
let offPush = null, offClose = null;
|
|
1317
1371
|
let clearEarly = () => { };
|
|
1318
1372
|
let resolveReady, rejectReady;
|
|
1319
1373
|
const ready = new Promise((resolve, reject) => { resolveReady = resolve; rejectReady = reject; });
|
|
1320
|
-
//
|
|
1374
|
+
// A caller that never awaits `ready` must not get an unhandled rejection.
|
|
1321
1375
|
ready.catch(() => { });
|
|
1322
1376
|
const listeners = new Set();
|
|
1323
1377
|
const notify = (listener) => {
|
|
@@ -1437,12 +1491,22 @@ export class MdbaseClient {
|
|
|
1437
1491
|
if (!current())
|
|
1438
1492
|
return;
|
|
1439
1493
|
try {
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1494
|
+
let page = await s.request("changes", params([[0, last ?? null], [1, CHANGES_PAGE], [2, true]]), { codec: changesResult, signal });
|
|
1495
|
+
for (;;) {
|
|
1496
|
+
if (!current())
|
|
1497
|
+
return;
|
|
1498
|
+
last = page.cursor;
|
|
1499
|
+
if (page.changes.length || page.reset)
|
|
1500
|
+
deliver(page);
|
|
1501
|
+
if (!current())
|
|
1502
|
+
return;
|
|
1503
|
+
if (page.changes.length < CHANGES_PAGE)
|
|
1504
|
+
break;
|
|
1505
|
+
// A full page may not be the last one. Every push held so far was sent
|
|
1506
|
+
// before this next request, so its reply (or a later page) repeats it.
|
|
1507
|
+
clearEarly();
|
|
1508
|
+
page = await s.request("changes", params([[0, last], [1, CHANGES_PAGE]]), { codec: changesResult, signal });
|
|
1509
|
+
}
|
|
1446
1510
|
while (early.length && current())
|
|
1447
1511
|
deliver(early.shift());
|
|
1448
1512
|
clearEarly();
|
|
@@ -1477,13 +1541,15 @@ export class MdbaseClient {
|
|
|
1477
1541
|
Object.defineProperties(watch, { state: { get: () => state }, error: { get: () => error } });
|
|
1478
1542
|
return Object.freeze(watch);
|
|
1479
1543
|
}
|
|
1480
|
-
/** Validate
|
|
1544
|
+
/** Validate `o.refs`, or every record without them. Every record is validated a
|
|
1481
1545
|
* page at a time when the replica grants `validate-page-v1`; each page is an
|
|
1482
1546
|
* independent READ, so records changed meanwhile may be missed or repeated.
|
|
1483
|
-
* Older replicas validate every record in one bounded request.
|
|
1484
|
-
|
|
1547
|
+
* Older replicas validate every record in one bounded request. `o.signal`
|
|
1548
|
+
* cancels the read. */
|
|
1549
|
+
async validate(o = {}) {
|
|
1550
|
+
const signal = o.signal;
|
|
1485
1551
|
signal?.throwIfAborted();
|
|
1486
|
-
const { targets, expected } = captureValidation(refs);
|
|
1552
|
+
const { targets, expected } = captureValidation(o.refs);
|
|
1487
1553
|
const call = (p) => this.call("validate", p, {
|
|
1488
1554
|
codec: recordValidations, retry: true, ...(signal ? { signal } : {}),
|
|
1489
1555
|
});
|
|
@@ -1504,8 +1570,10 @@ export class MdbaseClient {
|
|
|
1504
1570
|
}
|
|
1505
1571
|
}
|
|
1506
1572
|
/** Independent native app-host Bases READ, exact original held session.
|
|
1507
|
-
* Unsupported transports refuse: no generic execute_view/data fallback.
|
|
1508
|
-
|
|
1573
|
+
* Unsupported transports refuse: no generic execute_view/data fallback.
|
|
1574
|
+
* `o.signal` cancels the read. */
|
|
1575
|
+
async executeAppBases(request, o = {}) {
|
|
1576
|
+
const signal = o.signal;
|
|
1509
1577
|
const window = request.window === undefined ? null : { ...request.window };
|
|
1510
1578
|
const bytes = encodeAppBasesRequest({ ...request, ...(window === null ? {} : { window }) });
|
|
1511
1579
|
const session = await this.readyForRead(signal);
|
|
@@ -1523,11 +1591,13 @@ export class MdbaseClient {
|
|
|
1523
1591
|
* `executeViewPages`). Each page is an independent READ; `changedSinceFirst`
|
|
1524
1592
|
* marks pages taken after the collection changed. */
|
|
1525
1593
|
executeAppBasesPages(request, options = {}) {
|
|
1526
|
-
return appBasesPages((r, signal) => this.executeAppBases(r, signal), request, options);
|
|
1594
|
+
return appBasesPages((r, signal) => this.executeAppBases(r, signal ? { signal } : {}), request, options);
|
|
1527
1595
|
}
|
|
1528
1596
|
/** One bounded native discovery page. Empty views do not mean EOF when a
|
|
1529
|
-
* continuation is present. Supplied handles never silently restart/retry.
|
|
1530
|
-
|
|
1597
|
+
* continuation is present. Supplied handles never silently restart/retry.
|
|
1598
|
+
* `o.signal` cancels the read. */
|
|
1599
|
+
async listAppBasesViews(request, o = {}) {
|
|
1600
|
+
const signal = o.signal;
|
|
1531
1601
|
const limit = request.limit, bytes = encodeAppBasesDiscoveryRequest(request);
|
|
1532
1602
|
const session = await this.readyForRead(signal);
|
|
1533
1603
|
const reply = await session.readAppBasesDiscovery("list-views", bytes, signal);
|
|
@@ -1544,8 +1614,10 @@ export class MdbaseClient {
|
|
|
1544
1614
|
}
|
|
1545
1615
|
}
|
|
1546
1616
|
/** Exact current source for one native UUID/SHA/original ordinal, never path
|
|
1547
|
-
* aliases, inferred catalog entries, generic RPC or cached document fallback.
|
|
1548
|
-
|
|
1617
|
+
* aliases, inferred catalog entries, generic RPC or cached document fallback.
|
|
1618
|
+
* `o.signal` cancels the read. */
|
|
1619
|
+
async readAppBasesViewSource(request, o = {}) {
|
|
1620
|
+
const signal = o.signal;
|
|
1549
1621
|
const original = { ...request }, bytes = encodeAppBasesSourceRequest(original);
|
|
1550
1622
|
const session = await this.readyForRead(signal);
|
|
1551
1623
|
const reply = await session.readAppBasesDiscovery("read-view-source", bytes, signal);
|
|
@@ -1562,17 +1634,14 @@ export class MdbaseClient {
|
|
|
1562
1634
|
}
|
|
1563
1635
|
}
|
|
1564
1636
|
// ---------------------------------------------------------------- writes (§5, §6)
|
|
1565
|
-
/** Submit operations as one mutation
|
|
1637
|
+
/** Submit operations as one mutation: they apply together or not at all. */
|
|
1566
1638
|
async submit(ops, o = {}) {
|
|
1567
1639
|
if (this.hello.version.minor >= 3 && this.hello.grant.role === "viewer")
|
|
1568
1640
|
throw mdbaseError("forbidden", "You have view access, so changes can't be saved.", "read_only");
|
|
1569
1641
|
if (o.intent !== undefined && o.mutationId === undefined)
|
|
1570
1642
|
o = { ...o, mutationId: (await intentIds(this.collectionId, o.intent))[0] };
|
|
1571
|
-
const
|
|
1572
|
-
|
|
1573
|
-
p.allowPartial = true;
|
|
1574
|
-
p.mutationIds = ops.map((_, i) => (i === 0 ? p.mutationId : uuidv7()));
|
|
1575
|
-
}
|
|
1643
|
+
const mutationId = o.mutationId ?? uuidv7();
|
|
1644
|
+
const p = { ops, mutationId };
|
|
1576
1645
|
if (o.conflictMode)
|
|
1577
1646
|
p.conflictMode = o.conflictMode;
|
|
1578
1647
|
if (o.timezone)
|
|
@@ -1588,30 +1657,33 @@ export class MdbaseClient {
|
|
|
1588
1657
|
// The ledger holds the intent before the replica can capture it.
|
|
1589
1658
|
if (!o.dryRun) {
|
|
1590
1659
|
this.startLedger();
|
|
1591
|
-
await this.writeLedger.begin(
|
|
1660
|
+
await this.writeLedger.begin(mutationId, ops);
|
|
1592
1661
|
}
|
|
1593
1662
|
// Idempotent by mutation ID: safe to resubmit after a dropped link.
|
|
1594
|
-
let
|
|
1663
|
+
let receipt;
|
|
1595
1664
|
try {
|
|
1596
|
-
receipts = await this.call("submit", submitParams.enc(p), {
|
|
1665
|
+
const receipts = await this.call("submit", submitParams.enc(p), {
|
|
1597
1666
|
codec: submitResult,
|
|
1598
1667
|
retry: true,
|
|
1599
1668
|
...(o.signal ? { signal: o.signal } : {}),
|
|
1600
1669
|
});
|
|
1670
|
+
// One mutation has one receipt.
|
|
1671
|
+
if (receipts.length !== 1)
|
|
1672
|
+
throw mdbaseError("internal", "submit answered without exactly one receipt", "invalid_submit_response");
|
|
1673
|
+
receipt = receipts[0];
|
|
1601
1674
|
}
|
|
1602
1675
|
catch (e) {
|
|
1603
1676
|
// A dry run captures nothing. Otherwise the request may have been captured
|
|
1604
1677
|
// whatever the code says: report the original identity, never a new one.
|
|
1605
1678
|
if (o.dryRun)
|
|
1606
1679
|
throw e;
|
|
1607
|
-
void this.writeLedger.
|
|
1608
|
-
throw withMutation(e, "submit",
|
|
1680
|
+
void this.writeLedger.settle(mutationId);
|
|
1681
|
+
throw withMutation(e, "submit", { mutationId });
|
|
1609
1682
|
}
|
|
1610
|
-
return
|
|
1683
|
+
return o.dryRun ? new Write(receipt) : this._track(receipt);
|
|
1611
1684
|
}
|
|
1612
|
-
async
|
|
1613
|
-
|
|
1614
|
-
return w;
|
|
1685
|
+
async record(op, o) {
|
|
1686
|
+
return Object.assign(await this.submit([op], o), { recordId: op.id });
|
|
1615
1687
|
}
|
|
1616
1688
|
/** Create a record. The ID is minted here, so the app can use it before confirmation. */
|
|
1617
1689
|
create(input, o = {}) {
|
|
@@ -1630,7 +1702,7 @@ export class MdbaseClient {
|
|
|
1630
1702
|
op.body = input.body;
|
|
1631
1703
|
if (input.document !== undefined)
|
|
1632
1704
|
op.document = input.document;
|
|
1633
|
-
return this.
|
|
1705
|
+
return this.record(op, o);
|
|
1634
1706
|
}
|
|
1635
1707
|
async createThroughContract(input, contract, o) {
|
|
1636
1708
|
if (input.document !== undefined)
|
|
@@ -1645,7 +1717,7 @@ export class MdbaseClient {
|
|
|
1645
1717
|
op.path = input.path;
|
|
1646
1718
|
if (input.body !== undefined)
|
|
1647
1719
|
op.body = input.body;
|
|
1648
|
-
return
|
|
1720
|
+
return this.record(op, o);
|
|
1649
1721
|
}
|
|
1650
1722
|
/**
|
|
1651
1723
|
* Update a record by field-level intent. Pass the `RecordView` you edited from, and
|
|
@@ -1655,7 +1727,7 @@ export class MdbaseClient {
|
|
|
1655
1727
|
update(target, changes, o = {}) {
|
|
1656
1728
|
if (changes.contract)
|
|
1657
1729
|
return this.updateThroughContract(target, changes, changes.contract, o);
|
|
1658
|
-
return this.
|
|
1730
|
+
return this.record(this.updateOp(target, changes), o);
|
|
1659
1731
|
}
|
|
1660
1732
|
async updateThroughContract(target, c, contract, o) {
|
|
1661
1733
|
if (c.add || c.remove) {
|
|
@@ -1682,7 +1754,7 @@ export class MdbaseClient {
|
|
|
1682
1754
|
op.updateRefs = c.updateRefs;
|
|
1683
1755
|
if (c.ifRevision)
|
|
1684
1756
|
op.ifRevision = c.ifRevision;
|
|
1685
|
-
return this.
|
|
1757
|
+
return this.record(op, o);
|
|
1686
1758
|
}
|
|
1687
1759
|
/** Build an `update` op (exposed for batching with `submit`). */
|
|
1688
1760
|
updateOp(target, c) {
|
|
@@ -1728,8 +1800,10 @@ export class MdbaseClient {
|
|
|
1728
1800
|
const base = [];
|
|
1729
1801
|
const touched = [...(op.patch?.keys() ?? []), ...(op.unset ?? [])];
|
|
1730
1802
|
for (const k of new Set(touched)) {
|
|
1731
|
-
|
|
1732
|
-
|
|
1803
|
+
// Core compares `observed` by value (`1` is `1.0`, key order is free), so
|
|
1804
|
+
// the plain value read is the value seen.
|
|
1805
|
+
const v = Object.hasOwn(seen.frontmatter, k) ? seen.frontmatter[k] : undefined;
|
|
1806
|
+
base.push(v === undefined ? { key: k } : { key: k, observed: toValue(v) });
|
|
1733
1807
|
}
|
|
1734
1808
|
if (base.length)
|
|
1735
1809
|
op.base = base;
|
|
@@ -1738,8 +1812,8 @@ export class MdbaseClient {
|
|
|
1738
1812
|
// should not create conflicts.
|
|
1739
1813
|
if (seen && op.patch) {
|
|
1740
1814
|
for (const [k, v] of [...op.patch]) {
|
|
1741
|
-
const cur = seen.frontmatter.
|
|
1742
|
-
if (cur !== undefined && valueEquals(cur, v)) {
|
|
1815
|
+
const cur = Object.hasOwn(seen.frontmatter, k) ? seen.frontmatter[k] : undefined;
|
|
1816
|
+
if (cur !== undefined && valueEquals(toValue(cur), v)) {
|
|
1743
1817
|
op.patch.delete(k);
|
|
1744
1818
|
op.base = op.base?.filter((b) => b.key !== k);
|
|
1745
1819
|
}
|
|
@@ -1761,7 +1835,7 @@ export class MdbaseClient {
|
|
|
1761
1835
|
op.base = { path: seen.path, doc: seen.document };
|
|
1762
1836
|
if (o.ifRevision)
|
|
1763
1837
|
op.ifRevision = o.ifRevision;
|
|
1764
|
-
return this.
|
|
1838
|
+
return this.record(op, o);
|
|
1765
1839
|
}
|
|
1766
1840
|
/**
|
|
1767
1841
|
* Delete a record. With a RecordView, a concurrent change supersedes the delete.
|
|
@@ -1775,7 +1849,7 @@ export class MdbaseClient {
|
|
|
1775
1849
|
const op = { kind: "delete", id: plan.id, baseRevision: plan.revision };
|
|
1776
1850
|
if (rest.ifRevision)
|
|
1777
1851
|
op.ifRevision = rest.ifRevision;
|
|
1778
|
-
return this.
|
|
1852
|
+
return this.record(op, rest);
|
|
1779
1853
|
});
|
|
1780
1854
|
}
|
|
1781
1855
|
const op = { kind: "delete", id: refId(target) };
|
|
@@ -1783,7 +1857,7 @@ export class MdbaseClient {
|
|
|
1783
1857
|
op.baseRevision = target.revision;
|
|
1784
1858
|
if (o.ifRevision)
|
|
1785
1859
|
op.ifRevision = o.ifRevision;
|
|
1786
|
-
return this.
|
|
1860
|
+
return this.record(op, o);
|
|
1787
1861
|
}
|
|
1788
1862
|
/** Rename (move) a record; `updateRefs` rewrites links to it (default true).
|
|
1789
1863
|
* With `contract`, the record must implement it, and moves from its current path. */
|
|
@@ -1795,7 +1869,7 @@ export class MdbaseClient {
|
|
|
1795
1869
|
const op = { kind: "rename", id: target.id, from: target.path, to, updateRefs: o.updateRefs ?? true };
|
|
1796
1870
|
if (o.ifRevision)
|
|
1797
1871
|
op.ifRevision = o.ifRevision;
|
|
1798
|
-
return this.
|
|
1872
|
+
return this.record(op, o);
|
|
1799
1873
|
}
|
|
1800
1874
|
/** @internal Track a receipt the replica returned outside `submit`. */
|
|
1801
1875
|
_track(r) {
|
|
@@ -1825,59 +1899,40 @@ export class MdbaseClient {
|
|
|
1825
1899
|
return w;
|
|
1826
1900
|
}
|
|
1827
1901
|
/**
|
|
1828
|
-
* Settle a write for the
|
|
1829
|
-
*
|
|
1830
|
-
*
|
|
1831
|
-
*
|
|
1902
|
+
* Settle a write: wait for the sync service to confirm it (3 s by default),
|
|
1903
|
+
* and with `read`, read the record back as it now reads here.
|
|
1904
|
+
*
|
|
1905
|
+
* Pass the write, the promise of one, or the mutation ID of an earlier write
|
|
1906
|
+
* whose result was `unknown`. The answer is a {@link Settlement}; the only
|
|
1907
|
+
* rejections are cancellation and a call that cannot be settled as asked.
|
|
1908
|
+
* Nothing is ever submitted again.
|
|
1909
|
+
*
|
|
1910
|
+
* const saved = await db.settle(db.update(record, { patch }), { read: true });
|
|
1911
|
+
* if (saved.outcome === "rejected") show(describeError(saved.problem));
|
|
1832
1912
|
*/
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
let r = w.receipt;
|
|
1836
|
-
if (r.state === "pending") {
|
|
1837
|
-
try {
|
|
1838
|
-
r = await this.awaitReceipt(w.mutationId, o.timeoutMs ?? 3000, o.signal);
|
|
1839
|
-
w.update(r);
|
|
1840
|
-
}
|
|
1841
|
-
catch (e) {
|
|
1842
|
-
if (isMdbaseError(e, "cancelled"))
|
|
1843
|
-
throw e;
|
|
1844
|
-
// The wait failing says nothing about the write: it is on this device.
|
|
1845
|
-
r = w.receipt;
|
|
1846
|
-
}
|
|
1847
|
-
}
|
|
1848
|
-
const outcome = r.state === "confirmed" ? "confirmed"
|
|
1849
|
-
: r.state === "pending" ? "saved_on_device" : "rejected";
|
|
1850
|
-
const records = w.records;
|
|
1851
|
-
let record = records[0];
|
|
1852
|
-
if (o.readBack && outcome !== "rejected" && record) {
|
|
1853
|
-
record = (await this.find(record.id, undefined, o.signal).catch(() => null)) ?? record;
|
|
1854
|
-
}
|
|
1855
|
-
return Object.freeze({
|
|
1856
|
-
outcome,
|
|
1857
|
-
mutationId: w.mutationId,
|
|
1858
|
-
records,
|
|
1859
|
-
...(record ? { record } : {}),
|
|
1860
|
-
...(outcome === "rejected" && r.problem ? { problem: r.problem } : {}),
|
|
1861
|
-
});
|
|
1913
|
+
settle(write, options = {}) {
|
|
1914
|
+
return settleWrite(this, write, options);
|
|
1862
1915
|
}
|
|
1863
|
-
/** The current receipt of a mutation. Failures carry `mutationId`. */
|
|
1864
|
-
receipt(mutation,
|
|
1916
|
+
/** The current receipt of a mutation. Failures carry `mutationId`. `o.signal` cancels the read. */
|
|
1917
|
+
receipt(mutation, o = {}) {
|
|
1865
1918
|
return this.call("receipt", params([[0, uuidCodec.enc(mutation)]]), {
|
|
1866
1919
|
codec: receiptCodec,
|
|
1867
1920
|
retry: true,
|
|
1868
|
-
...(signal ? { signal } : {}),
|
|
1921
|
+
...(o.signal ? { signal: o.signal } : {}),
|
|
1869
1922
|
}).catch((e) => { throw withMutation(e, "receipt", { mutationId: mutation }); });
|
|
1870
1923
|
}
|
|
1871
|
-
/** Resolves when the receipt leaves `pending`, or at `timeoutMs` with the
|
|
1872
|
-
|
|
1924
|
+
/** Resolves when the receipt leaves `pending`, or at `o.timeoutMs` with the
|
|
1925
|
+
* pending receipt. `o.signal` cancels the wait. */
|
|
1926
|
+
awaitReceipt(mutation, o = {}) {
|
|
1873
1927
|
return this.call("await", params([
|
|
1874
1928
|
[0, uuidCodec.enc(mutation)],
|
|
1875
|
-
[1, timeoutMs],
|
|
1876
|
-
]), { codec: receiptCodec, retry: true, ...(signal ? { signal } : {}) }).catch((e) => { throw withMutation(e, "await", { mutationId: mutation }); });
|
|
1929
|
+
[1, o.timeoutMs],
|
|
1930
|
+
]), { codec: receiptCodec, retry: true, ...(o.signal ? { signal: o.signal } : {}) }).catch((e) => { throw withMutation(e, "await", { mutationId: mutation }); });
|
|
1877
1931
|
}
|
|
1878
1932
|
// ---------------------------------------------------------------- holds and conflicts (§8)
|
|
1879
|
-
|
|
1880
|
-
|
|
1933
|
+
/** The current holds. `o.signal` cancels the read. */
|
|
1934
|
+
listHolds(o = {}) {
|
|
1935
|
+
return this.call("list_holds", null, { codec: list(hold), retry: true, ...(o.signal ? { signal: o.signal } : {}) });
|
|
1881
1936
|
}
|
|
1882
1937
|
/** Call `fn` with the holds now and on every change. */
|
|
1883
1938
|
onHolds(fn) {
|
|
@@ -1908,11 +1963,12 @@ export class MdbaseClient {
|
|
|
1908
1963
|
]), { codec: receiptCodec });
|
|
1909
1964
|
return this._track(r);
|
|
1910
1965
|
}
|
|
1911
|
-
|
|
1912
|
-
|
|
1966
|
+
/** The current conflicts, of `o.record` only when given. `o.signal` cancels the read. */
|
|
1967
|
+
listConflicts(o = {}) {
|
|
1968
|
+
return this.call("list_conflicts", o.record ? params([[0, uuidCodec.enc(o.record)]]) : null, {
|
|
1913
1969
|
codec: list(conflictEntry),
|
|
1914
1970
|
retry: true,
|
|
1915
|
-
...(signal ? { signal } : {}),
|
|
1971
|
+
...(o.signal ? { signal: o.signal } : {}),
|
|
1916
1972
|
});
|
|
1917
1973
|
}
|
|
1918
1974
|
onConflicts(fn) {
|
|
@@ -1936,15 +1992,14 @@ export class MdbaseClient {
|
|
|
1936
1992
|
}
|
|
1937
1993
|
/** Accept what the record kept. */
|
|
1938
1994
|
dismissConflict(entry, o = {}) {
|
|
1939
|
-
return this.
|
|
1995
|
+
return this.submit([{ kind: "conflict_dismiss", mutation: entry.mutation, record: entry.conflict.id }], o);
|
|
1940
1996
|
}
|
|
1941
1997
|
/** Resolve a field conflict with a chosen value, dismissing it in the same mutation. */
|
|
1942
|
-
|
|
1943
|
-
|
|
1998
|
+
resolveConflict(entry, field, chosen, o = {}) {
|
|
1999
|
+
return this.submit([
|
|
1944
2000
|
{ kind: "update", id: entry.conflict.id, patch: new Map([[field, toValue(chosen)]]) },
|
|
1945
2001
|
{ kind: "conflict_dismiss", mutation: entry.mutation, record: entry.conflict.id },
|
|
1946
2002
|
], o);
|
|
1947
|
-
return w;
|
|
1948
2003
|
}
|
|
1949
2004
|
// ---------------------------------------------------------------- private collections (§8.3)
|
|
1950
2005
|
/**
|
|
@@ -1952,9 +2007,10 @@ export class MdbaseClient {
|
|
|
1952
2007
|
* hosting the replica (no grant). `exchangeReady` is challenge/reveal readiness,
|
|
1953
2008
|
* NOT approval/key delivery. Once ready, `sas` is the host-only comparison code
|
|
1954
2009
|
* to display beside Allow; the user must check it matches on the other computer.
|
|
2010
|
+
* `o.signal` cancels the read.
|
|
1955
2011
|
*/
|
|
1956
|
-
pendingDevices(
|
|
1957
|
-
return this.call("pending_devices", null, { codec: list(pendingDevice), retry: true, ...(signal ? { signal } : {}) });
|
|
2012
|
+
pendingDevices(o = {}) {
|
|
2013
|
+
return this.call("pending_devices", null, { codec: list(pendingDevice), retry: true, ...(o.signal ? { signal: o.signal } : {}) });
|
|
1958
2014
|
}
|
|
1959
2015
|
/**
|
|
1960
2016
|
* Confirm with the six digits displayed for the current host exchange, only
|
|
@@ -1992,7 +2048,9 @@ export class MdbaseClient {
|
|
|
1992
2048
|
accountSecret(secret);
|
|
1993
2049
|
await this.call("account_key_unlock", params([[0, secret]]));
|
|
1994
2050
|
},
|
|
1995
|
-
|
|
2051
|
+
/** Where an account-key unlock stands on this replica. `o.signal` cancels
|
|
2052
|
+
* the read. */
|
|
2053
|
+
status: (o = {}) => this.call("account_key_status", null, { codec: accountKeyStatus, retry: true, ...(o.signal ? { signal: o.signal } : {}) }),
|
|
1996
2054
|
};
|
|
1997
2055
|
/** Recovery key for a private collection (hosting app only). */
|
|
1998
2056
|
recoveryKey = {
|