@bjornpagen/bumbledb-log 1.2.1 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +152 -63
  2. package/dist/admin.d.ts +1 -1
  3. package/dist/bin.d.ts.map +1 -0
  4. package/dist/bin.js +13 -0
  5. package/dist/bin.js.map +1 -0
  6. package/dist/codes.d.ts +1 -1
  7. package/dist/codes.d.ts.map +1 -1
  8. package/dist/codes.js +1 -8
  9. package/dist/codes.js.map +1 -1
  10. package/dist/errors.d.ts +7 -21
  11. package/dist/errors.d.ts.map +1 -1
  12. package/dist/errors.js +11 -45
  13. package/dist/errors.js.map +1 -1
  14. package/dist/identity.d.ts +0 -8
  15. package/dist/identity.d.ts.map +1 -1
  16. package/dist/identity.js +0 -5
  17. package/dist/identity.js.map +1 -1
  18. package/dist/index.d.ts +5 -3
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +2 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/machine.d.ts +4 -28
  23. package/dist/machine.d.ts.map +1 -1
  24. package/dist/machine.js +183 -246
  25. package/dist/machine.js.map +1 -1
  26. package/dist/native.d.ts +59 -154
  27. package/dist/native.d.ts.map +1 -1
  28. package/dist/native.js.map +1 -1
  29. package/dist/options.d.ts +2 -11
  30. package/dist/options.d.ts.map +1 -1
  31. package/dist/outcome.d.ts +1 -85
  32. package/dist/outcome.d.ts.map +1 -1
  33. package/dist/schema-files.d.ts +16 -0
  34. package/dist/schema-files.d.ts.map +1 -0
  35. package/dist/schema-files.js +77 -0
  36. package/dist/schema-files.js.map +1 -0
  37. package/dist/schema.d.ts +6 -9
  38. package/dist/schema.d.ts.map +1 -1
  39. package/dist/schema.js +8 -8
  40. package/dist/schema.js.map +1 -1
  41. package/dist/transition.d.ts +75 -0
  42. package/dist/transition.d.ts.map +1 -0
  43. package/dist/transition.js +3 -0
  44. package/dist/transition.js.map +1 -0
  45. package/pack-provenance.json +4 -4
  46. package/package.json +4 -8
  47. package/dist/migration-ops.d.ts +0 -20
  48. package/dist/migration-ops.d.ts.map +0 -1
  49. package/dist/migration-ops.js +0 -7
  50. package/dist/migration-ops.js.map +0 -1
  51. package/dist/migrations/bin.d.ts.map +0 -1
  52. package/dist/migrations/bin.js +0 -30
  53. package/dist/migrations/bin.js.map +0 -1
  54. package/dist/migrations/canonical.d.ts +0 -40
  55. package/dist/migrations/canonical.d.ts.map +0 -1
  56. package/dist/migrations/canonical.js +0 -156
  57. package/dist/migrations/canonical.js.map +0 -1
  58. package/dist/migrations/cli.d.ts +0 -29
  59. package/dist/migrations/cli.d.ts.map +0 -1
  60. package/dist/migrations/cli.js +0 -124
  61. package/dist/migrations/cli.js.map +0 -1
  62. package/dist/migrations/codec.d.ts +0 -74
  63. package/dist/migrations/codec.d.ts.map +0 -1
  64. package/dist/migrations/codec.js +0 -2
  65. package/dist/migrations/codec.js.map +0 -1
  66. package/dist/migrations/decode.d.ts +0 -16
  67. package/dist/migrations/decode.d.ts.map +0 -1
  68. package/dist/migrations/decode.js +0 -302
  69. package/dist/migrations/decode.js.map +0 -1
  70. package/dist/migrations/diff.d.ts +0 -39
  71. package/dist/migrations/diff.d.ts.map +0 -1
  72. package/dist/migrations/diff.js +0 -385
  73. package/dist/migrations/diff.js.map +0 -1
  74. package/dist/migrations/expr.d.ts +0 -22
  75. package/dist/migrations/expr.d.ts.map +0 -1
  76. package/dist/migrations/expr.js +0 -219
  77. package/dist/migrations/expr.js.map +0 -1
  78. package/dist/migrations/fail.d.ts +0 -22
  79. package/dist/migrations/fail.d.ts.map +0 -1
  80. package/dist/migrations/fail.js +0 -52
  81. package/dist/migrations/fail.js.map +0 -1
  82. package/dist/migrations/fsops.d.ts +0 -37
  83. package/dist/migrations/fsops.d.ts.map +0 -1
  84. package/dist/migrations/fsops.js +0 -222
  85. package/dist/migrations/fsops.js.map +0 -1
  86. package/dist/migrations/generate.d.ts +0 -11
  87. package/dist/migrations/generate.d.ts.map +0 -1
  88. package/dist/migrations/generate.js +0 -455
  89. package/dist/migrations/generate.js.map +0 -1
  90. package/dist/migrations/index.d.ts +0 -23
  91. package/dist/migrations/index.d.ts.map +0 -1
  92. package/dist/migrations/index.js +0 -6
  93. package/dist/migrations/index.js.map +0 -1
  94. package/dist/migrations/intent.d.ts +0 -86
  95. package/dist/migrations/intent.d.ts.map +0 -1
  96. package/dist/migrations/intent.js +0 -97
  97. package/dist/migrations/intent.js.map +0 -1
  98. package/dist/migrations/lock.d.ts +0 -27
  99. package/dist/migrations/lock.d.ts.map +0 -1
  100. package/dist/migrations/lock.js +0 -14
  101. package/dist/migrations/lock.js.map +0 -1
  102. package/dist/migrations/native.d.ts +0 -26
  103. package/dist/migrations/native.d.ts.map +0 -1
  104. package/dist/migrations/native.js +0 -124
  105. package/dist/migrations/native.js.map +0 -1
  106. package/dist/migrations/repo.d.ts +0 -38
  107. package/dist/migrations/repo.d.ts.map +0 -1
  108. package/dist/migrations/repo.js +0 -191
  109. package/dist/migrations/repo.js.map +0 -1
  110. package/dist/migrations/theory.d.ts +0 -21
  111. package/dist/migrations/theory.d.ts.map +0 -1
  112. package/dist/migrations/theory.js +0 -107
  113. package/dist/migrations/theory.js.map +0 -1
  114. package/dist/migrations/types.d.ts +0 -242
  115. package/dist/migrations/types.d.ts.map +0 -1
  116. package/dist/migrations/types.js +0 -2
  117. package/dist/migrations/types.js.map +0 -1
  118. package/dist/migrations/workflow.d.ts +0 -11
  119. package/dist/migrations/workflow.d.ts.map +0 -1
  120. package/dist/migrations/workflow.js +0 -14
  121. package/dist/migrations/workflow.js.map +0 -1
  122. /package/dist/{migrations/bin.d.ts → bin.d.ts} +0 -0
package/dist/machine.js CHANGED
@@ -78,17 +78,6 @@ function healthOf(operation, wire) {
78
78
  }
79
79
  return { kind: "unavailable", error: errorOf(operation, wire.error) };
80
80
  }
81
- function phaseOf(operation, wire) {
82
- switch (wire) {
83
- case "prepared":
84
- case "dispatchedUnresolved":
85
- case "confirmed":
86
- case "provedNonpublication":
87
- return wire;
88
- default:
89
- throw invalidInput(operation);
90
- }
91
- }
92
81
  function inspectionOf(wire) {
93
82
  return {
94
83
  identity: identityOf(wire.identity),
@@ -277,40 +266,10 @@ function creationWire(operation, creation) {
277
266
  artifact: creation.artifact
278
267
  };
279
268
  }
280
- function snapshotsField(operation, plans) {
281
- // Base schema snapshot first, then each entry's target: entries + 1 rows.
282
- // Empty source still supplies the empty-schema render — never optional.
283
- if (!Array.isArray(plans.snapshots) || plans.snapshots.length !== plans.manifest.entries.length + 1) {
284
- throw invalidInput(operation);
285
- }
286
- return { snapshots: plans.snapshots.map((snapshot) => checkedString(operation, snapshot, 4 << 20)) };
287
- }
288
- function plansWire(operation, plans) {
289
- if (plans.manifest.entries.length !== plans.plans.length || plans.manifest.entries.length > 4096) {
290
- throw invalidInput(operation);
291
- }
292
- return {
293
- ...snapshotsField(operation, plans),
294
- manifestVersion: checkedNonNegative(operation, plans.manifest.manifestVersion, 0xffff),
295
- planVersion: checkedNonNegative(operation, plans.manifest.planVersion, 0xffff),
296
- baseSchemaId: checkedString(operation, plans.manifest.baseSchemaId, 64),
297
- basePrefixDigest: checkedString(operation, plans.manifest.basePrefixDigest, 64),
298
- entries: plans.manifest.entries.map((entry) => ({
299
- sequence: checkedString(operation, entry.sequence, 20),
300
- id: checkedString(operation, entry.id, 64),
301
- fromSchemaId: checkedString(operation, entry.fromSchemaId, 64),
302
- toSchemaId: checkedString(operation, entry.toSchemaId, 64),
303
- planDigest: checkedString(operation, entry.planDigest, 64),
304
- prefixDigest: checkedString(operation, entry.prefixDigest, 64)
305
- })),
306
- // Inert canonical transport of the generated plan data; the native
307
- // migration codec is the one canonicalization/digest authority.
308
- plans: plans.plans.map((plan) => JSON.stringify(plan))
309
- };
310
- }
311
269
  export function makeLogMachine(wire, core) {
312
270
  const cancel = (operation, callback) => wire.runtimeCancel(operation, callback);
313
271
  const commandEntries = new WeakMap();
272
+ const historyEntries = new WeakMap();
314
273
  function makeCommand(cw) {
315
274
  const state = { closed: false };
316
275
  const command = {
@@ -346,22 +305,19 @@ export function makeLogMachine(wire, core) {
346
305
  return {
347
306
  kind: "decided",
348
307
  receipt: receiptOf(outcome.receipt),
349
- localHealth: healthOf(operation, outcome.localHealth),
350
- phase: phaseOf(operation, outcome.publicationPhase)
308
+ localHealth: healthOf(operation, outcome.localHealth)
351
309
  };
352
310
  case "not-submitted":
353
311
  return {
354
312
  kind: "not-submitted",
355
313
  command: ref,
356
- error: errorOf(operation, outcome.error),
357
- phase: phaseOf(operation, outcome.publicationPhase)
314
+ error: errorOf(operation, outcome.error)
358
315
  };
359
316
  case "outcome-unknown":
360
317
  return {
361
318
  kind: "outcome-unknown",
362
319
  command: ref,
363
- error: errorOf(operation, outcome.error),
364
- phase: phaseOf(operation, outcome.publicationPhase)
320
+ error: errorOf(operation, outcome.error)
365
321
  };
366
322
  }
367
323
  }
@@ -430,8 +386,7 @@ export function makeLogMachine(wire, core) {
430
386
  return Effect.succeed({
431
387
  kind: "not-submitted",
432
388
  command: ref,
433
- error: closedHandle(operation),
434
- phase: "prepared"
389
+ error: closedHandle(operation)
435
390
  });
436
391
  }
437
392
  return certaintyOperation(operation, cancel, (callback) => wire.logHistoryCall(capability, {
@@ -443,13 +398,11 @@ export function makeLogMachine(wire, core) {
443
398
  }, callback), wire.logHistoryResult, (result) => decodeSubmit(operation, ref, result), (error) => ({
444
399
  kind: "not-submitted",
445
400
  command: ref,
446
- error,
447
- phase: "prepared"
401
+ error
448
402
  }), (error) => ({
449
403
  kind: "outcome-unknown",
450
404
  command: ref,
451
- error,
452
- phase: "dispatchedUnresolved"
405
+ error
453
406
  }));
454
407
  });
455
408
  },
@@ -480,24 +433,28 @@ export function makeLogMachine(wire, core) {
480
433
  function makeHistory(schema, handle) {
481
434
  const state = { closed: false };
482
435
  const members = makeFacadeMembers(schema, handle.history, handle.meta, state);
483
- return {
436
+ const history = {
484
437
  ...members,
485
438
  close: () => Effect.suspend(() => {
486
439
  state.closed = true;
487
440
  return drainClose("History.close", (callback) => wire.logHistoryClose(handle.history, callback));
488
441
  })
489
442
  };
443
+ historyEntries.set(history, { capability: handle.history, schema });
444
+ return history;
490
445
  }
491
446
  function makeBorrow(schema, handle) {
492
447
  const state = { closed: false };
493
448
  const members = makeFacadeMembers(schema, handle.history, handle.meta, state);
494
- return {
449
+ const history = {
495
450
  ...members,
496
451
  release: () => Effect.suspend(() => {
497
452
  state.closed = true;
498
453
  return drainClose("HistoryBorrow.release", (callback) => wire.logBorrowRelease(handle.history, callback));
499
454
  })
500
455
  };
456
+ historyEntries.set(history, { capability: handle.history, schema });
457
+ return history;
501
458
  }
502
459
  function openHistory(operation, kind, mode, binding, schema, options) {
503
460
  return Effect.gen(function* () {
@@ -534,15 +491,12 @@ export function makeLogMachine(wire, core) {
534
491
  const acquire = Effect.suspend(() => {
535
492
  // The exact core registry accessor: a foreign dynamic object
536
493
  // refuses before any native dispatch (the Db.apply pattern);
537
- // a spent/closed ChangeSet refuses as ClosedHandle; a scope/
538
- // change schema mismatch refuses before native work.
494
+ // a scope/change schema mismatch refuses before native work.
495
+ // Native admission owns the ChangeSet's lifetime.
539
496
  const internal = core.changes(input.changes);
540
497
  if (internal === undefined) {
541
498
  return Effect.fail(invalidInput(operation));
542
499
  }
543
- if (internal.closed) {
544
- return Effect.fail(closedHandle(operation));
545
- }
546
500
  let request;
547
501
  try {
548
502
  if (String(internal.schemaId) !== String(input.scope.schemaId)) {
@@ -600,12 +554,6 @@ export function makeLogMachine(wire, core) {
600
554
  try {
601
555
  request = {
602
556
  maxOpen: checkedCount(operation, options.maxOpen, 0xffffffff),
603
- expected: options.expected === undefined
604
- ? null
605
- : {
606
- schemaId: checkedString(operation, options.expected.schemaId, 64),
607
- appliedPrefixDigest: checkedString(operation, options.expected.appliedPrefixDigest, 64)
608
- },
609
557
  schema: core.schemaSpec(schema)
610
558
  };
611
559
  }
@@ -676,22 +624,19 @@ export function makeLogMachine(wire, core) {
676
624
  return {
677
625
  kind: "completed",
678
626
  ref,
679
- value: decode(result.value),
680
- phase: phaseOf(operation, result.publicationPhase)
627
+ value: decode(result.value)
681
628
  };
682
629
  case "not-started":
683
630
  return {
684
631
  kind: "not-started",
685
632
  ref,
686
- error: errorOf(operation, result.error),
687
- phase: phaseOf(operation, result.publicationPhase)
633
+ error: errorOf(operation, result.error)
688
634
  };
689
635
  case "outcome-unknown":
690
636
  return {
691
637
  kind: "outcome-unknown",
692
638
  ref,
693
- error: errorOf(operation, result.error),
694
- phase: phaseOf(operation, result.publicationPhase)
639
+ error: errorOf(operation, result.error)
695
640
  };
696
641
  case "report":
697
642
  // A read-only certainty from a mutating verb is a wire defect.
@@ -705,18 +650,16 @@ export function makeLogMachine(wire, core) {
705
650
  return {
706
651
  kind: "not-started",
707
652
  ref,
708
- error: runtime.failure,
709
- phase: "prepared"
653
+ error: runtime.failure
710
654
  };
711
655
  }
712
656
  // `ref` is the caller-supplied operationId, fixed before dispatch.
713
657
  // Interrupt after the native lease is outcome-unknown under this
714
658
  // same ref; retry status/the same operationId, never a new mint.
715
- return yield* certaintyOperation(operation, cancel, (callback) => wire.logAdmin(runtime.success, request(), callback), wire.logAdminTake, (result) => decodeAdmin(operation, ref, result, decode), (error) => ({ kind: "not-started", ref, error, phase: "prepared" }), (error) => ({
659
+ return yield* certaintyOperation(operation, cancel, (callback) => wire.logAdmin(runtime.success, request(), callback), wire.logAdminTake, (result) => decodeAdmin(operation, ref, result, decode), (error) => ({ kind: "not-started", ref, error }), (error) => ({
716
660
  kind: "outcome-unknown",
717
661
  ref,
718
- error,
719
- phase: "dispatchedUnresolved"
662
+ error
720
663
  }));
721
664
  });
722
665
  }
@@ -742,13 +685,6 @@ export function makeLogMachine(wire, core) {
742
685
  function backupField(operation, backup) {
743
686
  return backup === undefined ? {} : { backup: checkedUuid(operation, backup) };
744
687
  }
745
- /** Optional source binding + target schema for activate/abort. */
746
- function targetFields(operation, options) {
747
- return {
748
- ...(options.binding === undefined ? {} : { binding: bindingWire(operation, options.binding) }),
749
- ...schemaField(options.schema)
750
- };
751
- }
752
688
  function destinationWire(operation, destination) {
753
689
  if (destination.kind === "filesystem") {
754
690
  return { kind: "filesystem", directory: checkedString(operation, destination.directory, 4096) };
@@ -772,104 +708,6 @@ export function makeLogMachine(wire, core) {
772
708
  return value;
773
709
  };
774
710
  }
775
- function sourceAccessOf(wire) {
776
- return {
777
- access: wire.access,
778
- operation: wire.operationId === null ? null : wire.operationId
779
- };
780
- }
781
- // `ActivationRef` is declared in #migrations/types.ts:
782
- // { operation: OperationId, planSetDigest, target, targetGenesis }.
783
- function activationRefOf(wire) {
784
- return {
785
- operation: wire.operationId,
786
- planSetDigest: wire.planSetDigest,
787
- target: identityOf(wire.target),
788
- targetGenesis: wire.targetGenesis
789
- };
790
- }
791
- function migrationRefOf(wire) {
792
- return {
793
- operation: { identity: identityOf(wire.identity), operation: wire.operationId },
794
- planSetDigest: wire.planSetDigest,
795
- target: identityOf(wire.target)
796
- };
797
- }
798
- function activationRefWire(operation, ref) {
799
- return {
800
- operationId: checkedUuid(operation, ref.operation),
801
- planSetDigest: checkedString(operation, ref.planSetDigest, 64),
802
- target: identityWire(operation, ref.target),
803
- targetGenesis: checkedString(operation, ref.targetGenesis, 64)
804
- };
805
- }
806
- function migrationRefWire(operation, ref) {
807
- return {
808
- identity: identityWire(operation, ref.operation.identity),
809
- operationId: checkedUuid(operation, ref.operation.operation),
810
- planSetDigest: checkedString(operation, ref.planSetDigest, 64),
811
- target: identityWire(operation, ref.target)
812
- };
813
- }
814
- function migrateValueOf(operation, wire) {
815
- switch (wire.kind) {
816
- case "up-to-date":
817
- return { kind: "up-to-date", binding: bindingOf(wire.binding) };
818
- case "ready-to-switch":
819
- return {
820
- kind: "ready-to-switch",
821
- deploymentBinding: bindingOf(wire.deploymentBinding),
822
- activation: activationRefOf(wire.activation)
823
- };
824
- case "paused":
825
- return {
826
- kind: "paused",
827
- error: errorOf(operation, wire.error),
828
- sourceState: sourceAccessOf(wire.sourceState)
829
- };
830
- }
831
- }
832
- function migrationStatusOf(operation, wire) {
833
- switch (wire.kind) {
834
- case "up-to-date":
835
- return { kind: "up-to-date", appliedPrefixDigest: wire.appliedPrefixDigest };
836
- case "pending":
837
- return { kind: "pending", pending: wire.pending };
838
- case "in-progress":
839
- return { kind: "in-progress", operation: migrationRefOf(wire.operationRef).operation };
840
- case "paused":
841
- return {
842
- kind: "paused",
843
- operation: migrationRefOf(wire.operationRef).operation,
844
- error: errorOf(operation, wire.error),
845
- sourceState: sourceAccessOf(wire.sourceState)
846
- };
847
- case "ready-to-switch":
848
- return {
849
- kind: "ready-to-switch",
850
- operation: migrationRefOf(wire.operationRef).operation,
851
- activation: activationRefOf(wire.activation)
852
- };
853
- case "activated":
854
- return {
855
- kind: "activated",
856
- operation: migrationRefOf(wire.operationRef).operation,
857
- target: identityOf(wire.target)
858
- };
859
- case "aborted":
860
- return { kind: "aborted", operation: migrationRefOf(wire.operationRef).operation };
861
- case "outcome-unknown":
862
- return {
863
- kind: "outcome-unknown",
864
- operation: migrationRefOf(wire.operationRef).operation,
865
- error: errorOf(operation, wire.error)
866
- };
867
- case "drift":
868
- return { kind: "drift", detail: wire.detail };
869
- case "database-ahead":
870
- return { kind: "database-ahead", detail: wire.detail };
871
- }
872
- }
873
711
  const admin = {
874
712
  checkpoint(binding, options) {
875
713
  const operation = "admin.checkpoint";
@@ -1038,75 +876,174 @@ export function makeLogMachine(wire, core) {
1038
876
  });
1039
877
  }
1040
878
  };
1041
- const migrations = {
1042
- migrationStatus(binding, plans, options) {
1043
- const operation = "migrations.status";
1044
- return adminQuery(operation, () => ({
1045
- verb: "migration-status",
1046
- binding: bindingWire(operation, binding),
1047
- ...schemaField(options.schema),
1048
- plans: plansWire(operation, plans)
1049
- }), (value) => {
1050
- const report = expectVerb(operation, "migration-status")(value);
1051
- return migrationStatusOf(operation, report.status);
879
+ function contractWire(operation, contract) {
880
+ return {
881
+ operationId: checkedUuid(operation, contract.operation),
882
+ source: identityWire(operation, contract.source),
883
+ target: identityWire(operation, contract.target),
884
+ commitment: checkedString(operation, contract.commitment, 64)
885
+ };
886
+ }
887
+ function contractOf(contract) {
888
+ return {
889
+ operation: contract.operationId,
890
+ source: identityOf(contract.source),
891
+ target: identityOf(contract.target),
892
+ commitment: contract.commitment
893
+ };
894
+ }
895
+ function captureOf(captured) {
896
+ return {
897
+ contract: contractOf(captured.contract),
898
+ decision: stampOf(captured.decision),
899
+ state: stateOf(captured.state)
900
+ };
901
+ }
902
+ function installedOf(installed) {
903
+ return {
904
+ captured: captureOf(installed.captured),
905
+ applicationDigest: installed.applicationDigest,
906
+ bytes: installed.bytes
907
+ };
908
+ }
909
+ function readyOf(result) {
910
+ return {
911
+ kind: "ready",
912
+ installed: installedOf(result.installed),
913
+ binding: {
914
+ kind: "local",
915
+ directory: result.directory,
916
+ identity: identityOf(result.installed.captured.contract.target)
917
+ }
918
+ };
919
+ }
920
+ function resolutionOf(operation, result) {
921
+ switch (result.kind) {
922
+ case "ready":
923
+ return readyOf(result);
924
+ case "activated":
925
+ return {
926
+ kind: "activated",
927
+ contract: contractOf(result.contract),
928
+ genesis: result.genesis,
929
+ binding: { kind: "local", directory: result.directory, identity: identityOf(result.contract.target) }
930
+ };
931
+ case "uninstalled":
932
+ return { kind: "uninstalled" };
933
+ case "aborted":
934
+ return { kind: "aborted" };
935
+ default:
936
+ throw invalidInput(operation);
937
+ }
938
+ }
939
+ function transitionCall(operation, history, request, accept) {
940
+ return Effect.suspend(() => {
941
+ const entry = historyEntries.get(history);
942
+ if (entry === undefined)
943
+ return Effect.fail(invalidInput(operation));
944
+ let input;
945
+ try {
946
+ input = request();
947
+ }
948
+ catch (cause) {
949
+ return Effect.fail(logFailure(operation, cause));
950
+ }
951
+ return logOperation(operation, cancel, (callback) => wire.logTransitionCall(entry.capability, input, callback), wire.logTransitionResult, accept);
952
+ });
953
+ }
954
+ function makePopulation(schema, handle) {
955
+ return {
956
+ schema,
957
+ apply(changes) {
958
+ const operation = "Population.apply";
959
+ return Effect.suspend(() => {
960
+ const entry = core.changes(changes);
961
+ if (entry === undefined)
962
+ return Effect.fail(invalidInput(operation));
963
+ return logOperation(operation, cancel, (callback) => wire.logPopulationApply(handle, entry.handle, callback), wire.logTransitionResult, (result) => {
964
+ if (result.kind !== "applied")
965
+ throw invalidInput(operation);
966
+ });
967
+ });
968
+ },
969
+ finish() {
970
+ const operation = "Population.finish";
971
+ return logOperation(operation, cancel, (callback) => wire.logPopulationFinish(handle, callback), wire.logTransitionResult, (result) => {
972
+ if (result.kind !== "ready")
973
+ throw invalidInput(operation);
974
+ return readyOf(result);
975
+ });
976
+ },
977
+ close: () => drainClose("Population.close", (callback) => wire.logPopulationClose(handle, callback))
978
+ };
979
+ }
980
+ const TransitionNamespace = {
981
+ begin(source, target, contract) {
982
+ const operation = "Transition.begin";
983
+ const acquire = transitionCall(operation, source, () => ({ verb: "begin", contract: contractWire(operation, contract), schema: core.schemaSpec(target) }), (result) => {
984
+ if (result.kind === "activated")
985
+ return resolutionOf(operation, result);
986
+ if (result.kind === "aborted")
987
+ return { kind: "aborted" };
988
+ if (result.kind !== "ready" && result.kind !== "populating")
989
+ throw invalidInput(operation);
990
+ const entry = historyEntries.get(source);
991
+ if (entry === undefined || result.source === undefined)
992
+ throw invalidInput(operation);
993
+ const reader = makeSnapshot(entry.schema, result.source.snapshot, result.source.provenance);
994
+ if (result.kind === "ready")
995
+ return { ...readyOf(result), source: reader };
996
+ return {
997
+ kind: "populating",
998
+ captured: captureOf(result.captured),
999
+ source: reader,
1000
+ population: makePopulation(target, result.population),
1001
+ binding: {
1002
+ kind: "local",
1003
+ directory: result.directory,
1004
+ identity: identityOf(result.captured.contract.target)
1005
+ }
1006
+ };
1052
1007
  });
1008
+ return scopedResource(operation, acquire, (value) => Effect.gen(function* () {
1009
+ if (value.kind === "populating") {
1010
+ const population = yield* value.population.close();
1011
+ const source = yield* value.source.close();
1012
+ return population.kind === "closed" ? source : population;
1013
+ }
1014
+ if (value.kind === "ready")
1015
+ return yield* value.source.close();
1016
+ return { kind: "closed" };
1017
+ }));
1053
1018
  },
1054
- initialize(binding, plans, options) {
1055
- const operation = "migrations.initialize";
1056
- return adminMutation(operation, operationRef(binding, options.operationId), () => ({
1057
- verb: "migration-initialize",
1058
- binding: bindingWire(operation, binding),
1059
- ...schemaField(options.schema),
1060
- operationId: checkedUuid(operation, options.operationId),
1061
- plans: plansWire(operation, plans)
1062
- }), (value) => {
1063
- const report = expectVerb(operation, "migration-initialize")(value);
1064
- return { binding: bindingOf(report.binding), genesis: report.genesis };
1065
- });
1019
+ resolve(source, target, contract) {
1020
+ const operation = "Transition.resolve";
1021
+ return transitionCall(operation, source, () => ({ verb: "resolve", contract: contractWire(operation, contract), schema: core.schemaSpec(target) }), (result) => resolutionOf(operation, result));
1066
1022
  },
1067
- migrate(binding, plans, options) {
1068
- const operation = "migrations.migrate";
1069
- return adminMutation(operation, operationRef(binding, options.operationId), () => ({
1070
- verb: "migration-migrate",
1071
- binding: bindingWire(operation, binding),
1072
- ...schemaField(options.schema),
1073
- operationId: checkedUuid(operation, options.operationId),
1074
- plans: plansWire(operation, plans),
1075
- to: options.to === undefined ? null : checkedString(operation, options.to, 256)
1076
- }), (value) => {
1077
- const report = expectVerb(operation, "migration-migrate")(value);
1078
- return migrateValueOf(operation, report.value);
1023
+ inspect(target, installed) {
1024
+ const operation = "Transition.inspect";
1025
+ const acquire = transitionCall(operation, target, () => ({ verb: "inspect", evidence: installed.bytes }), (result) => {
1026
+ const entry = historyEntries.get(target);
1027
+ if (entry === undefined || result.kind !== "ready" || result.source === undefined)
1028
+ throw invalidInput(operation);
1029
+ return makeSnapshot(entry.schema, result.source.snapshot, result.source.provenance);
1079
1030
  });
1031
+ return scopedResource(operation, acquire, (reader) => reader.close());
1080
1032
  },
1081
- activateMigration(ref, options) {
1082
- const operation = "migrations.activate";
1083
- return adminMutation(operation, { identity: ref.target, operation: ref.operation }, () => ({
1084
- verb: "migration-activate",
1085
- ref: activationRefWire(operation, ref),
1086
- ...targetFields(operation, options)
1087
- }), (value) => {
1088
- const report = expectVerb(operation, "migration-activate")(value);
1089
- return {
1090
- target: identityOf(report.target),
1091
- accessMode: report.accessMode,
1092
- operation: report.operationId,
1093
- activatedNow: report.activatedNow
1094
- };
1033
+ activate(source, target, installed) {
1034
+ const operation = "Transition.activate";
1035
+ return transitionCall(operation, source, () => ({ verb: "activate", evidence: installed.bytes, schema: core.schemaSpec(target) }), (result) => {
1036
+ const value = resolutionOf(operation, result);
1037
+ if (value.kind !== "activated")
1038
+ throw invalidInput(operation);
1039
+ return value;
1095
1040
  });
1096
1041
  },
1097
- abortMigration(ref, options) {
1098
- const operation = "migrations.abort";
1099
- return adminMutation(operation, ref.operation, () => ({
1100
- verb: "migration-abort",
1101
- ref: migrationRefWire(operation, ref),
1102
- ...targetFields(operation, options)
1103
- }), (value) => {
1104
- const report = expectVerb(operation, "migration-abort")(value);
1105
- return {
1106
- target: identityOf(report.target),
1107
- targetFenced: report.targetFenced,
1108
- sourceAccess: report.sourceAccess
1109
- };
1042
+ abort(source, target, contract) {
1043
+ const operation = "Transition.abort";
1044
+ return transitionCall(operation, source, () => ({ verb: "abort", contract: contractWire(operation, contract), schema: core.schemaSpec(target) }), (result) => {
1045
+ if (result.kind !== "aborted")
1046
+ throw invalidInput(operation);
1110
1047
  });
1111
1048
  }
1112
1049
  };
@@ -1116,7 +1053,7 @@ export function makeLogMachine(wire, core) {
1116
1053
  Command: CommandNamespace,
1117
1054
  TenantCache: TenantCacheNamespace,
1118
1055
  admin,
1119
- migrations
1056
+ Transition: TransitionNamespace
1120
1057
  };
1121
1058
  }
1122
1059
  //# sourceMappingURL=machine.js.map