@ibgib/core-gib 0.1.21 → 0.1.22

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 (113) hide show
  1. package/dist/sync/sync-conflict.respec.mjs +24 -22
  2. package/dist/sync/sync-conflict.respec.mjs.map +1 -1
  3. package/dist/sync/sync-constants.d.mts +8 -8
  4. package/dist/sync/sync-constants.d.mts.map +1 -1
  5. package/dist/sync/sync-constants.mjs +3 -3
  6. package/dist/sync/sync-constants.mjs.map +1 -1
  7. package/dist/sync/sync-helpers.d.mts +2 -0
  8. package/dist/sync/sync-helpers.d.mts.map +1 -1
  9. package/dist/sync/sync-helpers.mjs +11 -9
  10. package/dist/sync/sync-helpers.mjs.map +1 -1
  11. package/dist/sync/sync-innerspace-constants.respec.mjs +5 -1
  12. package/dist/sync/sync-innerspace-constants.respec.mjs.map +1 -1
  13. package/dist/sync/sync-innerspace-deep-updates.respec.mjs +5 -1
  14. package/dist/sync/sync-innerspace-deep-updates.respec.mjs.map +1 -1
  15. package/dist/sync/sync-innerspace-dest-ahead.respec.mjs +8 -4
  16. package/dist/sync/sync-innerspace-dest-ahead.respec.mjs.map +1 -1
  17. package/dist/sync/sync-innerspace-multiple-timelines.respec.mjs +5 -1
  18. package/dist/sync/sync-innerspace-multiple-timelines.respec.mjs.map +1 -1
  19. package/dist/sync/sync-innerspace-partial-update.respec.mjs +5 -1
  20. package/dist/sync/sync-innerspace-partial-update.respec.mjs.map +1 -1
  21. package/dist/sync/sync-innerspace.respec.mjs +5 -1
  22. package/dist/sync/sync-innerspace.respec.mjs.map +1 -1
  23. package/dist/sync/sync-peer/sync-peer-constants.d.mts +2 -0
  24. package/dist/sync/sync-peer/sync-peer-constants.d.mts.map +1 -0
  25. package/dist/sync/sync-peer/sync-peer-constants.mjs +2 -0
  26. package/dist/sync/sync-peer/sync-peer-constants.mjs.map +1 -0
  27. package/dist/sync/sync-peer/sync-peer-helpers.d.mts +2 -0
  28. package/dist/sync/sync-peer/sync-peer-helpers.d.mts.map +1 -0
  29. package/dist/sync/sync-peer/sync-peer-helpers.mjs +2 -0
  30. package/dist/sync/sync-peer/sync-peer-helpers.mjs.map +1 -0
  31. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.d.mts +8 -0
  32. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.d.mts.map +1 -0
  33. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs +8 -0
  34. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs.map +1 -0
  35. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.d.mts +18 -0
  36. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.d.mts.map +1 -0
  37. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mjs +54 -0
  38. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mjs.map +1 -0
  39. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.d.mts +80 -0
  40. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.d.mts.map +1 -0
  41. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mjs +5 -0
  42. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mjs.map +1 -0
  43. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts +43 -0
  44. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts.map +1 -0
  45. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs +229 -0
  46. package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs.map +1 -0
  47. package/dist/sync/sync-peer/sync-peer-types.d.mts +12 -0
  48. package/dist/sync/sync-peer/sync-peer-types.d.mts.map +1 -1
  49. package/dist/sync/sync-peer/sync-peer-v1.d.mts +15 -7
  50. package/dist/sync/sync-peer/sync-peer-v1.d.mts.map +1 -1
  51. package/dist/sync/sync-peer/sync-peer-v1.mjs +105 -24
  52. package/dist/sync/sync-peer/sync-peer-v1.mjs.map +1 -1
  53. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts +5 -8
  54. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts.map +1 -1
  55. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs +34 -18
  56. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs.map +1 -1
  57. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts +26 -22
  58. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts.map +1 -1
  59. package/dist/sync/sync-saga-context/sync-saga-context-types.mjs +1 -9
  60. package/dist/sync/sync-saga-context/sync-saga-context-types.mjs.map +1 -1
  61. package/dist/sync/sync-saga-coordinator.d.mts +23 -33
  62. package/dist/sync/sync-saga-coordinator.d.mts.map +1 -1
  63. package/dist/sync/sync-saga-coordinator.mjs +271 -163
  64. package/dist/sync/sync-saga-coordinator.mjs.map +1 -1
  65. package/dist/sync/sync-types.d.mts +15 -3
  66. package/dist/sync/sync-types.d.mts.map +1 -1
  67. package/dist/witness/light-witness-base-v1.d.mts.map +1 -1
  68. package/dist/witness/light-witness-base-v1.mjs +2 -0
  69. package/dist/witness/light-witness-base-v1.mjs.map +1 -1
  70. package/dist/witness/space/inner-space/inner-space-v1.mjs +1 -1
  71. package/dist/witness/space/inner-space/inner-space-v1.mjs.map +1 -1
  72. package/package.json +1 -1
  73. package/src/sync/README.md +31 -22
  74. package/src/sync/sync-conflict.respec.mts +23 -19
  75. package/src/sync/sync-constants.mts +4 -5
  76. package/src/sync/sync-helpers.mts +11 -7
  77. package/src/sync/sync-innerspace-constants.respec.mts +5 -1
  78. package/src/sync/sync-innerspace-deep-updates.respec.mts +5 -1
  79. package/src/sync/sync-innerspace-dest-ahead.respec.mts +7 -3
  80. package/src/sync/sync-innerspace-multiple-timelines.respec.mts +5 -1
  81. package/src/sync/sync-innerspace-partial-update.respec.mts +8 -2
  82. package/src/sync/sync-innerspace.respec.mts +5 -1
  83. package/src/sync/sync-peer/sync-peer-constants.mts +0 -0
  84. package/src/sync/sync-peer/sync-peer-helpers.mts +0 -0
  85. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mts +8 -0
  86. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-helpers.mts +72 -0
  87. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mts +87 -0
  88. package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mts +242 -0
  89. package/src/sync/sync-peer/sync-peer-types.mts +13 -1
  90. package/src/sync/sync-peer/sync-peer-v1.mts +93 -27
  91. package/src/sync/sync-saga-context/sync-saga-context-helpers.mts +47 -29
  92. package/src/sync/sync-saga-context/sync-saga-context-types.mts +29 -30
  93. package/src/sync/sync-saga-coordinator.mts +280 -170
  94. package/src/sync/sync-types.mts +17 -3
  95. package/src/witness/light-witness-base-v1.mts +2 -1
  96. package/src/witness/space/inner-space/inner-space-v1.mts +1 -1
  97. package/test_output.log +0 -0
  98. package/tmp.md +62 -44
  99. package/dist/sync/sync-local-spaces.respec.d.mts +0 -2
  100. package/dist/sync/sync-local-spaces.respec.d.mts.map +0 -1
  101. package/dist/sync/sync-local-spaces.respec.mjs +0 -159
  102. package/dist/sync/sync-local-spaces.respec.mjs.map +0 -1
  103. package/dist/sync/sync-peer/sync-peer-innerspace-v1.d.mts +0 -42
  104. package/dist/sync/sync-peer/sync-peer-innerspace-v1.d.mts.map +0 -1
  105. package/dist/sync/sync-peer/sync-peer-innerspace-v1.mjs +0 -194
  106. package/dist/sync/sync-peer/sync-peer-innerspace-v1.mjs.map +0 -1
  107. package/dist/sync/sync-saga-coordinator.respec.d.mts +0 -2
  108. package/dist/sync/sync-saga-coordinator.respec.d.mts.map +0 -1
  109. package/dist/sync/sync-saga-coordinator.respec.mjs +0 -40
  110. package/dist/sync/sync-saga-coordinator.respec.mjs.map +0 -1
  111. package/src/sync/sync-local-spaces.respec.mts +0 -200
  112. package/src/sync/sync-peer/sync-peer-innerspace-v1.mts +0 -240
  113. package/src/sync/sync-saga-coordinator.respec.mts +0 -52
@@ -0,0 +1,242 @@
1
+ /**
2
+ * @module SyncPeerInnerspace_V1
3
+ */
4
+
5
+ import { extractErrorMsg, unique } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
6
+ import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
7
+ import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
8
+
9
+ import { GLOBAL_LOG_A_LOT } from '../../../core-constants.mjs';
10
+ import { SYNC_ATOM, } from '../../sync-constants.mjs';
11
+ import { IbGibSpaceAny } from '../../../witness/space/space-base-v1.mjs';
12
+ import { SyncPeer_V1 } from '../sync-peer-v1.mjs';
13
+ import { getFromSpace, putInSpace } from '../../../witness/space/space-helper.mjs';
14
+ import { SyncSagaContextIbGib_V1, } from '../../sync-saga-context/sync-saga-context-types.mjs';
15
+ import { createSyncSagaContext } from '../../sync-saga-context/sync-saga-context-helpers.mjs';
16
+ import { SubjectWitness } from '../../../common/pubsub/subject/subject-types.mjs';
17
+ import { newupSubject } from '../../../common/pubsub/subject/subject-helper.mjs';
18
+ import { SyncPeerInnerspaceData_V1, SyncPeerInnerspaceIbGib_V1, SyncPeerInnerspaceOptions, SyncPeerInnerspaceRel8ns_V1 } from './sync-peer-innerspace-types.mjs';
19
+ import { SYNC_PEER_INNERSPACE_ATOM } from './sync-peer-innerspace-constants.mjs';
20
+ import { SyncPeerData_V1, SyncPeerRel8ns_V1 } from '../sync-peer-types.mjs';
21
+ import { MetaspaceService } from '../../../witness/space/metaspace/metaspace-types.mjs';
22
+ import { SyncSagaCoordinator } from '../../sync-saga-coordinator.mjs';
23
+ import { IbGibAddr } from '@ibgib/ts-gib/dist/types.mjs';
24
+ import { IbGibSpaceResultData } from '../../../witness/space/space-types.mjs';
25
+ import { getSyncSagaDependencyGraph } from '../../sync-helpers.mjs';
26
+
27
+
28
+ const logalot = GLOBAL_LOG_A_LOT || true;
29
+ const logalotControlDomain = true;
30
+ const lcControlDomain = '[ControlDomain]';
31
+
32
+ /**
33
+ * Concrete implementation of SyncPeer for local in-memory simulation (Innerspace to Innerspace).
34
+ *
35
+ * Acts as the "Network" layer, transferring data between two local spaces and
36
+ * executing the receiver's coordinator logic.
37
+ */
38
+ export class SyncPeerInnerspace_V1 extends SyncPeer_V1 implements SyncPeerInnerspaceIbGib_V1 {
39
+
40
+ protected lc: string = `[${SyncPeerInnerspace_V1.name}]`;
41
+
42
+ public payloadIbGibsDomainReceived$!: SubjectWitness<IbGib_V1>;
43
+
44
+ private senderSpace: IbGibSpaceAny;
45
+ private senderTempSpace: IbGibSpaceAny;
46
+ private receiverCoordinator: SyncSagaCoordinator;
47
+ private receiverMetaspace: MetaspaceService;
48
+ private receiverSpace: IbGibSpaceAny;
49
+ private receiverTempSpace: IbGibSpaceAny;
50
+
51
+ override get classname(): string {
52
+ return SyncPeerInnerspace_V1.name;
53
+ }
54
+
55
+ constructor(
56
+ opts: SyncPeerInnerspaceOptions,
57
+ initialData: SyncPeerInnerspaceData_V1,
58
+ initialRel8ns?: SyncPeerInnerspaceRel8ns_V1,
59
+ ) {
60
+ super(initialData, initialRel8ns);
61
+ const {
62
+ senderSpace, senderTempSpace, receiverCoordinator,
63
+ receiverMetaspace, receiverSpace, receiverTempSpace
64
+ } = opts;
65
+
66
+ this.senderSpace = senderSpace;
67
+ this.senderTempSpace = senderTempSpace;
68
+ this.receiverCoordinator = receiverCoordinator;
69
+ this.receiverMetaspace = receiverMetaspace;
70
+ this.receiverSpace = receiverSpace;
71
+ this.receiverTempSpace = receiverTempSpace;
72
+ }
73
+
74
+ async initialize(): Promise<void> {
75
+ this.payloadIbGibsDomainReceived$ = await newupSubject<IbGib_V1>();
76
+ }
77
+
78
+ private async ensureReceiverTempSpace(): Promise<IbGibSpaceAny> {
79
+ if (!this.receiverTempSpace) {
80
+ const { receiverMetaspace } = this;
81
+
82
+ const uuid = crypto.randomUUID ? crypto.randomUUID() : Math.random().toString(36);
83
+ const tempSpaceName = `tmp_sync_recv_${uuid.substring(0, 8)}`;
84
+ const receiverTempSpace = await this.receiverMetaspace.createNewLocalSpace({
85
+ opts: {
86
+ allowCancel: false,
87
+ spaceName: tempSpaceName,
88
+ getFnPrompt: this.receiverMetaspace.getFnPrompt!,
89
+ logalot
90
+ }
91
+ });
92
+ if (!receiverTempSpace) { throw new Error(`(UNEXPECTED) receiverTempSpace falsy? couldn't create a temp space? (E: a6b8f86b0f58384b884a33b82924b826)`); }
93
+ await receiverTempSpace.initialized;
94
+ this.receiverTempSpace = receiverTempSpace;
95
+ }
96
+ return this.receiverTempSpace;
97
+ }
98
+
99
+ protected async getLocalIbGib(addr: string): Promise<IbGib_V1 | undefined> {
100
+ const lc = `${this.lc}[${this.getLocalIbGib.name}]`;
101
+ try {
102
+ if (logalot) { console.log(`${lc} starting... (I: 27b248cb9801eeb2386b71485389a826)`); }
103
+
104
+ console.warn(`${lc} possibly a bottleneck here, getLocalIbGib only gets a single ibgib... (W: 2fd448a435480e6b128f6b8bcbef4826)`);
105
+
106
+ const resGet = await getFromSpace({ space: this.senderSpace, addr });
107
+
108
+ if (resGet.success && resGet.ibGibs && resGet.ibGibs.length === 1) {
109
+ const ibGib = resGet.ibGibs[0];
110
+ return ibGib;
111
+ } else {
112
+ throw new Error(`couldn't get addr (${addr}) from local space ${this.senderSpace}. reason: ${resGet.errorMsg ?? 'unknown error (E: 926ef8bf4fcc299ab89dba34ea691a26)'} (E: d8a89807e471d3f8b938ab21df44cb26)`);
113
+ }
114
+ } catch (error) {
115
+ console.error(`${lc} ${extractErrorMsg(error)}`);
116
+ throw error;
117
+ } finally {
118
+ if (logalot) { console.log(`${lc} complete.`); }
119
+ }
120
+ }
121
+
122
+ protected async push(addrs: IbGibAddr[]): Promise<void> {
123
+ const lc = `${this.lc}[${this.push.name}]`;
124
+ try {
125
+ if (logalot) { console.log(`${lc} starting... (I: 07bce80be18f3259b8a311ed9c4da926)`); }
126
+
127
+ // silly defensive checks because this has been a ridiculous development cycle
128
+ if (!this.senderSpace) { throw new Error(`(UNEXPECTED) this.senderSpace falsy? (E: 503e288168f5d297f4d4e028bc538826)`); }
129
+ if (!this.receiverSpace) { throw new Error(`(UNEXPECTED) this.receiverSpace falsy? (E: 0bd7c1a0ba4868ac7a260e15b504e826)`); }
130
+
131
+ await this.transfer({ addrs, from: this.senderSpace, to: this.receiverSpace });
132
+ } catch (error) {
133
+ console.error(`${lc} ${extractErrorMsg(error)}`);
134
+ throw error;
135
+ } finally {
136
+ if (logalot) { console.log(`${lc} complete.`); }
137
+ }
138
+ }
139
+
140
+ protected async pull(addrs: IbGibAddr[]): Promise<void> {
141
+ const lc = `${this.lc}[${this.pull.name}]`;
142
+ try {
143
+ if (logalot) { console.log(`${lc} starting... (I: b16a28ed0dc8f52f1838ee257d9b1126)`); }
144
+
145
+ // silly defensive checks because this has been a ridiculous development cycle
146
+ if (!this.receiverSpace) { throw new Error(`(UNEXPECTED) this.receiverSpace falsy? (E: bd2fdfa89ba4c89a185b68e9bb1f1c26)`); }
147
+ if (!this.senderTempSpace) { throw new Error(`(UNEXPECTED) this.senderTempSpace falsy? (E: b23fa55713f881493dac4a9835ac7326)`); }
148
+
149
+ await this.transfer({ addrs, from: this.receiverSpace, to: this.senderTempSpace });
150
+ } catch (error) {
151
+ console.error(`${lc} ${extractErrorMsg(error)}`);
152
+ throw error;
153
+ } finally {
154
+ if (logalot) { console.log(`${lc} complete.`); }
155
+ }
156
+ }
157
+
158
+ protected async sendContextRequest(context: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined> {
159
+ const lc = `${this.lc}[${this.sendContextRequest.name}]`;
160
+ const { receiverSpace, receiverCoordinator, receiverMetaspace } = this;
161
+
162
+ try {
163
+ if (logalot) { console.log(`${lc} starting...Context: ${getIbGibAddr({ ibGib: context })}`); }
164
+
165
+ // 0. Validate context (naive, not business logic validation)
166
+ if (!context.data) { throw new Error(`(UNEXPECTED) context.data falsy? (E: 1d2ac8a9b991d451e8900588057fe626)`); }
167
+ if (!context.rel8ns) { throw new Error(`(UNEXPECTED) context.rel8ns falsy? (E: 3508786f0d8880bcadd346282a807826)`); }
168
+ if (!context.rel8ns.sagaFrame) { throw new Error(`(UNEXPECTED) context.rel8ns.sagaFrame falsy? (E: ac06782ad538be406882605f0a83aa26)`); }
169
+ if (!context.sagaFrame) { throw new Error(`(UNEXPECTED) context.sagaFrame falsy? (E: 79cd98a6d9b88a591f4596c8bc58ff26)`); }
170
+
171
+ // validate the context, sagaFrame, and identity(s)
172
+ // persist the context, sagaframe, identity(s) ONLY in the receiver's durable space
173
+ // if sending domain ibgibs...
174
+ // verify the domain ibgibs intrinsically.
175
+ // persist the domain ibgibs in the receiver's temp space (push all addrs?)
176
+ // await until all domain ibgibs are in receiver's temp space
177
+ // process the context with the receiver's coordinator to get a new response context
178
+ // analyze the response context
179
+ // validate context intrinsically
180
+ // validate return identity if present
181
+ // persist the response context's dependency graph (context, saga frame and msg(s), identities)
182
+ // if response has payload domain ibgibs,
183
+ // spin off...
184
+ // pull domain ibgibs to local temp space
185
+ // publish domain ibgibs to observable
186
+ // when all domain ibgibs are pulled, publish complete to the observable
187
+ // return resulting context ibgib (which the caller should get BEFORE the domain ibgibs observable completes)
188
+
189
+
190
+ throw new Error(`not implemented (E: 72840872e252d9f7380f1998b5a0c826)`);
191
+ } catch (error) {
192
+ console.error(`${lc} ${extractErrorMsg(error)}`);
193
+ throw error;
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Helper to copy single ibgib from A to B.
199
+ */
200
+ protected async transfer({
201
+ addrs,
202
+ from,
203
+ to
204
+ }: {
205
+ addrs: IbGibAddr[],
206
+ from: IbGibSpaceAny,
207
+ to: IbGibSpaceAny
208
+ }): Promise<void> {
209
+ const lc = `${this.lc}[${this.transfer.name}]`;
210
+ try {
211
+ if (logalot) { console.log(`${lc} starting... (I: f1145dc75a461afc885efe984f08d426)`); }
212
+
213
+ console.error(`${lc} NAG ERROR (DOES NOT THROW): This needs to be adapted to be able to pull from multiple spaces. IOW, the 'from' param should be an array of spaces. This is because we need to be checking both durable and temp spaces when querying. (E: 823f3856b8b82ece080868e85ded0826)`)
214
+
215
+ // 1. Get from Source
216
+ const resGet = await getFromSpace({ space: from, addrs });
217
+ if (!resGet.success || !resGet.ibGibs || resGet.ibGibs.length !== addrs.length) {
218
+ if (!resGet.rawResultIbGib) { throw new Error(`(UNEXPECTED) resGet.rawResultIbGib falsy? (E: b829f827a378890bcf7dc3d41c908826)`); }
219
+ if (!resGet.rawResultIbGib.data) { throw new Error(`(UNEXPECTED) resGet.rawResultIbGib.data falsy? (E: 8d15341d8fa8fc96297425e3c6e39226)`); }
220
+ const data = resGet.rawResultIbGib.data as IbGibSpaceResultData;
221
+
222
+ throw new Error(`${lc} Failed to get all ${addrs} from source space ${from.ib}. resGet.errorMsg: ${resGet.errorMsg}. failed info: ${JSON.stringify(data)} (E: 4ed3383573382920c80054d7c1024626)`);
223
+ }
224
+ const ibGibs = resGet.ibGibs.concat();
225
+
226
+ // 2. Put in Dest
227
+ // Note: putInSpace handles existence check usually, or we can check.
228
+ const resPut = await putInSpace({ space: to, ibGibs });
229
+ if (resPut.success) {
230
+ console.error(`${lc} NAG ERROR: (SORT OF AN ERROR, DID NOT THROW): is the sync peer responsible for calling registerNewIbGib?? I'm not sure at this point if that is true, rather, I think that it probably is NOT responsible for this. But I'm putting this error msg as a nag message until this is resolved. (E: 00086833e294ee834800046801c47826) `);
231
+ } else {
232
+ throw new Error(`ERROR: successfully got addrs (${addrs}) from space (${from.ib}), BUT failed to put into space (${to.ib}). resPut.errorMsg: ${resPut.errorMsg}. timedOut: ${resPut.timedOut}. (E: c1044e14cc1db72d28a2240819dab826)`);
233
+ }
234
+ } catch (error) {
235
+ console.error(`${lc} ${extractErrorMsg(error)}`);
236
+ throw error;
237
+ } finally {
238
+ if (logalot) { console.log(`${lc} complete.`); }
239
+ }
240
+
241
+ }
242
+ }
@@ -3,6 +3,7 @@
3
3
  */
4
4
 
5
5
  import { IbGibData_V1, IbGibRel8ns_V1, IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
6
+ import { SubjectWitness } from '../../common/pubsub/subject/subject-types.mjs';
6
7
 
7
8
  import { SyncSagaContextData_V1, SyncSagaContextIbGib_V1, SyncSagaContextRel8ns_V1 } from '../sync-saga-context/sync-saga-context-types.mjs';
8
9
  import { Witness_V1 } from '../../witness/witness-types.mjs';
@@ -11,7 +12,10 @@ import { Witness_V1 } from '../../witness/witness-types.mjs';
11
12
  * Data for the SyncPeer witness.
12
13
  */
13
14
  export interface SyncPeerData_V1 extends IbGibData_V1 {
14
- // Add specific settings for Sync Peer if needed (e.g. timeout, retry policy)
15
+ /**
16
+ * Concrete peer instance
17
+ */
18
+ classname: string;
15
19
  }
16
20
 
17
21
  /**
@@ -40,4 +44,12 @@ export interface SyncPeerWitness extends Witness_V1<
40
44
  SyncPeerRel8ns_V1
41
45
  > {
42
46
  // witness(arg: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined>;
47
+
48
+ /**
49
+ * Observable for streaming large domain payloads asynchronously.
50
+ *
51
+ * Implementations should publish domain ibgibs to this observable as they are received.
52
+ * The handling code (e.g. SyncSagaCoordinator) will subscribe to this.
53
+ */
54
+ payloadIbGibsDomainReceived$: SubjectWitness<IbGib_V1>;
43
55
  }
@@ -5,14 +5,18 @@
5
5
  import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
6
6
  import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
7
7
  import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
8
+ import { SubjectWitness } from '../../common/pubsub/subject/subject-types.mjs';
8
9
 
9
10
  import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs';
10
- import { SYNC_MSG_REL8N_NAME, SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME } from '../sync-constants.mjs';
11
+ import { SYNC_MSG_REL8N_NAME, SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN } from '../sync-constants.mjs';
11
12
  import { SyncSagaContextIbGib_V1 } from '../sync-saga-context/sync-saga-context-types.mjs';
12
13
  import { SyncPeerData_V1, SyncPeerRel8ns_V1, SyncPeerWitness } from './sync-peer-types.mjs';
13
14
  import { LightWitnessBase_V1 } from '../../witness/light-witness-base-v1.mjs';
15
+ import { IbGibAddr } from '@ibgib/ts-gib/dist/types.mjs';
14
16
 
15
17
  const logalot = GLOBAL_LOG_A_LOT || true;
18
+ const logalotControlDomain = true;
19
+ const lcControlDomain = '[ControlDomain]';
16
20
 
17
21
  /**
18
22
  * Abstract witness for talking to a Sync Peer (e.g. Remote Node or Local Simulator).
@@ -29,6 +33,17 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
29
33
 
30
34
  protected lc: string = `[${SyncPeer_V1.name}]`;
31
35
 
36
+ abstract payloadIbGibsDomainReceived$: SubjectWitness<IbGib_V1>;
37
+
38
+ get classname(): string {
39
+ if (!this.data) { throw new Error(`(UNEXPECTED) this.data falsy? (E: 1ab1841e9338b54f3aa615fa37024826)`); }
40
+ if (!this.data.classname) { throw new Error(`invalid peer. this.data.classname is falsy (E: b0ee28a0abb84a06588d9de7afcef826)`); }
41
+ return this.data.classname;
42
+ }
43
+
44
+ constructor(initialData: SyncPeerData_V1, initialRel8ns?: SyncPeerRel8ns_V1) {
45
+ super(initialData, initialRel8ns);
46
+ }
32
47
  /**
33
48
  * Witness the synchronization context (Send/Receive).
34
49
  *
@@ -40,11 +55,13 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
40
55
  try {
41
56
  if (logalot) { console.log(`${lc} starting...`); }
42
57
 
58
+ if (logalot) { console.log(`${lc} starting...`); }
59
+
43
60
  // 1. PUSH: Ensure Request Data is available on Receiver
44
61
  await this.pushContextGraph({ context: arg });
45
62
 
46
63
  // 2. EXECUTE: Trigger Remote Processing
47
- const response = await this.sendRequest(arg);
64
+ const response = await this.sendContextRequest(arg);
48
65
 
49
66
  if (!response) {
50
67
  if (logalot) { console.log(`${lc} No response from peer.`); }
@@ -65,45 +82,58 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
65
82
  }
66
83
 
67
84
  /**
68
- * Primitive: Transmits the Context IbGib itself to the Peer.
85
+ * Transmits the Context IbGib itself to the Peer.
69
86
  */
70
- protected abstract sendRequest(context: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined>;
87
+ protected abstract sendContextRequest(context: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined>;
71
88
 
72
89
  /**
73
- * Primitive: Pushes a specific IbGib (by address) from Local to Remote.
74
- * Should handle identifying if the remote already has it (optimization) or just put it.
90
+ * Pushes specific IbGib(s) (by address) from Local to Remote.
91
+ *
92
+ * Should NOT have to handle identifying if the remote already has it
93
+ * (optimization), because the sync coordinator should be doing this in
94
+ * its diffing algorithm.
75
95
  */
76
- protected abstract push(addr: string): Promise<void>;
96
+ protected abstract push(addrs: IbGibAddr[]): Promise<void>;
77
97
 
78
98
  /**
79
- * Primitive: Pulls a specific IbGib (by address) from Remote to Local.
99
+ * Primitive: Pulls specific IbGib(s) (by address) from Remote to Local.
80
100
  */
81
- protected abstract pull(addr: string): Promise<void>;
101
+ protected abstract pull(addrs: IbGibAddr[]): Promise<void>;
82
102
 
83
103
  /**
84
104
  * Helper to orchestrate the PUSH of a Context's dependency graph.
85
105
  */
86
106
  protected async pushContextGraph({ context }: { context: SyncSagaContextIbGib_V1 }): Promise<void> {
107
+ const lc = `[${SyncPeer_V1.name}][pushContextGraph]`;
108
+
87
109
  // A. Push Context Envelope
88
110
  const contextAddr = getIbGibAddr({ ibGib: context });
89
- await this.push(contextAddr);
111
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH Context Envelope -> ${contextAddr} (I: 3f4e5a6b7c8d9e0f)`); }
112
+ await this.push([contextAddr]);
90
113
 
91
114
  // B. Push Saga Frame & Immediate Deps (Msg, Identity)
92
115
  const frameAddr = context.rel8ns?.sagaFrame?.[0];
93
116
  if (frameAddr) {
94
- await this.push(frameAddr);
117
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL: Saga Frame -> ${frameAddr} (I: 4a5b6c7d8e9f0a1b)`); }
118
+ await this.push([frameAddr]);
95
119
  // We inspect the local frame to find its immediate deps
96
120
  const frame = await this.getLocalIbGib(frameAddr);
97
121
  if (frame) {
98
122
  // Msg Stone
99
123
  const msgAddrs = frame.rel8ns?.[SYNC_MSG_REL8N_NAME];
100
124
  if (msgAddrs) {
101
- for (const addr of msgAddrs) await this.push(addr);
125
+ for (const addr of msgAddrs) {
126
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL: Msg Stone -> ${addr} (I: 5b6c7d8e9f0a1b2c)`); }
127
+ await this.push([addr]);
128
+ }
102
129
  }
103
130
  // Identity
104
131
  const idAddrs = frame.rel8ns?.identity;
105
132
  if (idAddrs) {
106
- for (const addr of idAddrs) await this.push(addr);
133
+ for (const addr of idAddrs) {
134
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL: Identity -> ${addr} (I: 6c7d8e9f0a1b2c3d)`); }
135
+ await this.push([addr]);
136
+ }
107
137
  }
108
138
  }
109
139
  }
@@ -111,7 +141,8 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
111
141
  // C. Push Session Identity
112
142
  const sessionKeystoneAddr = context.rel8ns?.sessionKeystone?.[0];
113
143
  if (sessionKeystoneAddr) {
114
- await this.push(sessionKeystoneAddr);
144
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL: Session Keystone -> ${sessionKeystoneAddr} (I: 7d8e9f0a1b2c3d4e)`); }
145
+ await this.push([sessionKeystoneAddr]);
115
146
  }
116
147
 
117
148
  // D. Push Payloads (Deep/Soft)
@@ -121,36 +152,60 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
121
152
  // Or do we need to be explicit?
122
153
  // User said: "payloads should be flat... sync coordinator is the one doing the heavy lifting".
123
154
  // So we just push the addrs listed.
124
- const payloadAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME];
125
- if (payloadAddrs) {
126
- for (const addr of payloadAddrs) {
127
- await this.push(addr); // Assumed shallow/flat per user instruction
155
+
156
+ // 1. Control Payloads
157
+ // const controlAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_CONTROL];
158
+ // if (controlAddrs && controlAddrs.length > 0) {
159
+ // if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH CONTROL PAYLOADS (${controlAddrs.length}): ${controlAddrs.join(', ')} (I: 8e9f0a1b2c3d4e5f)`); }
160
+ // for (const addr of controlAddrs) {
161
+ // await this.push(addr);
162
+ // }
163
+ // }
164
+
165
+ // 2. Domain Payloads
166
+ const domainAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN];
167
+ if (domainAddrs && domainAddrs.length > 0) {
168
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH DOMAIN PAYLOADS (${domainAddrs.length}): ${domainAddrs.join(', ')} (I: 9f0a1b2c3d4e5f6a)`); }
169
+ for (const addr of domainAddrs) {
170
+ await this.push([addr]);
128
171
  }
129
172
  }
173
+
174
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PUSH COMPLETE for context ${contextAddr} (I: a0b1c2d3e4f5a6b7)`); }
130
175
  }
131
176
 
132
177
  /**
133
178
  * Helper to orchestrate the PULL of a Context's dependency graph.
134
179
  */
135
180
  protected async pullContextGraph({ context }: { context: SyncSagaContextIbGib_V1 }): Promise<void> {
181
+ const lc = `[${SyncPeer_V1.name}][pullContextGraph]`;
182
+
136
183
  // A. Pull Context Envelope
137
184
  const contextAddr = getIbGibAddr({ ibGib: context });
138
- await this.pull(contextAddr);
185
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL Context Envelope <- ${contextAddr} (I: b1c2d3e4f5a6b7c8)`); }
186
+ await this.pull([contextAddr]);
139
187
 
140
188
  // B. Pull Saga Frame & Immediate Deps
141
189
  const frameAddr = context.rel8ns?.sagaFrame?.[0];
142
190
  if (frameAddr) {
143
- await this.pull(frameAddr);
191
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL: Saga Frame <- ${frameAddr} (I: c2d3e4f5a6b7c8d9)`); }
192
+ await this.pull([frameAddr]);
144
193
  // Inspect REMOTE frame? We need to have pulled it first.
145
194
  const frame = await this.getLocalIbGib(frameAddr); // Should be local now
146
195
  if (frame) {
147
196
  const msgAddrs = frame.rel8ns?.[SYNC_MSG_REL8N_NAME];
148
197
  if (msgAddrs) {
149
- for (const addr of msgAddrs) await this.pull(addr);
198
+ for (const addr of msgAddrs) {
199
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL: Msg Stone <- ${addr} (I: d3e4f5a6b7c8d9e0)`); }
200
+ await this.pull([addr]);
201
+ }
150
202
  }
151
203
  const idAddrs = frame.rel8ns?.identity;
152
204
  if (idAddrs) {
153
- for (const addr of idAddrs) await this.pull(addr);
205
+ for (const addr of idAddrs) {
206
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL: Identity <- ${addr} (I: e4f5a6b7c8d9e0f1)`); }
207
+ await this.pull([addr]);
208
+ }
154
209
  }
155
210
  }
156
211
  }
@@ -158,16 +213,27 @@ export abstract class SyncPeer_V1 extends LightWitnessBase_V1<SyncPeerData_V1, S
158
213
  // C. Pull Session Identity
159
214
  const sessionKeystoneAddr = context.rel8ns?.sessionKeystone?.[0];
160
215
  if (sessionKeystoneAddr) {
161
- await this.pull(sessionKeystoneAddr);
216
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL: Session Keystone <- ${sessionKeystoneAddr} (I: f5a6b7c8d9e0f1a2)`); }
217
+ await this.pull([sessionKeystoneAddr]);
162
218
  }
163
219
 
164
220
  // D. Pull Payloads
165
- const payloadAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME];
166
- if (payloadAddrs) {
167
- for (const addr of payloadAddrs) {
168
- await this.pull(addr);
221
+ // const controlAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_CONTROL];
222
+ // if (controlAddrs && controlAddrs.length > 0) {
223
+ // if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL CONTROL PAYLOADS (${controlAddrs.length}): ${controlAddrs.join(', ')} (I: a6b7c8d9e0f1a2b3)`); }
224
+ // for (const addr of controlAddrs) {
225
+ // await this.pull(addr);
226
+ // }
227
+ // }
228
+ const domainAddrs = context.data?.[SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN];
229
+ if (domainAddrs && domainAddrs.length > 0) {
230
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL DOMAIN PAYLOADS (${domainAddrs.length}): ${domainAddrs.join(', ')} (I: b7c8d9e0f1a2b3c4)`); }
231
+ for (const addr of domainAddrs) {
232
+ await this.pull([addr]);
169
233
  }
170
234
  }
235
+
236
+ if (logalotControlDomain) { console.log(`${lc}${lcControlDomain} PULL COMPLETE for context ${contextAddr} (I: c8d9e0f1a2b3c4d5)`); }
171
237
  }
172
238
 
173
239
  /**
@@ -2,27 +2,24 @@
2
2
  * @module sync saga context helpers
3
3
  */
4
4
 
5
- import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
5
+ import { extractErrorMsg, getTimestamp } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
6
6
  import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/factory.mjs';
7
7
  import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
8
8
  import { IbGib_V1, } from '@ibgib/ts-gib/dist/V1/types.mjs';
9
9
  import { Ib } from '@ibgib/ts-gib/dist/types.mjs';
10
10
 
11
11
  import { SYNC_SAGA_CONTEXT_ATOM } from './sync-saga-context-constants.mjs';
12
- import { SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME } from '../sync-constants.mjs';
12
+ import { SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN } from '../sync-constants.mjs';
13
13
  import {
14
- SyncSagaContextCmd, SyncSagaContextData_V1, SyncSagaContextIbGib_V1,
14
+ SyncSagaContextData_V1, SyncSagaContextIbGib_V1,
15
15
  SyncSagaContextIb_V1, SyncSagaContextRel8ns_V1
16
16
  } from './sync-saga-context-types.mjs';
17
+ import { getTimestampInfo } from '../../common/other/ibgib-helper.mjs';
17
18
 
18
19
  /**
19
20
  * Options for creating a SyncSagaContext ibgib.
20
21
  */
21
22
  export interface CreateSyncSagaContextOptions {
22
- /**
23
- * Optional command string (e.g. 'process').
24
- */
25
- cmd?: SyncSagaContextCmd | string;
26
23
  /**
27
24
  * The main saga frame (Init, Ack, etc.).
28
25
  */
@@ -32,9 +29,10 @@ export interface CreateSyncSagaContextOptions {
32
29
  */
33
30
  sessionKeystones?: IbGib_V1[];
34
31
  /**
35
- * Payload addresses (strings) for bulk data.
32
+ * Domain payload ibgibs when the sync saga frame includes actual domain
33
+ * payloads to send, e.g., in a Delta frame.
36
34
  */
37
- payloadAddrs?: string[];
35
+ payloadIbGibsDomain?: IbGib_V1[];
38
36
  }
39
37
 
40
38
  /**
@@ -48,11 +46,12 @@ export async function getSyncSagaContextIb({
48
46
  const lc = `[${getSyncSagaContextIb.name}]`;
49
47
  try {
50
48
  if (!data) { throw new Error(`data required (E: 839d82138c234b3e89582855594191fe)`); }
51
- const cmd = data.cmd || 'default';
49
+
50
+ const payloadCount = data['@payloadAddrsDomain'] ? data['@payloadAddrsDomain'].length : 0;
52
51
  // sync_sagacontext cmd
53
52
  const ib = [
54
53
  SYNC_SAGA_CONTEXT_ATOM,
55
- cmd
54
+ payloadCount,
56
55
  ].join(' ');
57
56
 
58
57
  return ib;
@@ -73,17 +72,25 @@ export async function parseSyncSagaContextIb({
73
72
  const lc = `[${parseSyncSagaContextIb.name}]`;
74
73
  try {
75
74
  const parts = ib.split(' ');
76
- if (parts.length < 2) {
77
- throw new Error(`Invalid sync saga context ib. Expected at least 2 parts [atom cmd]. Got ${parts.length}. (E: 8b06c88820c74f5fb9f21f148459422a)`);
75
+ if (parts.length !== 2) {
76
+ throw new Error(`Invalid sync saga context ib. Expected at least 2 parts [atom, payloadCount]. Got ${parts.length}. (E: 8b06c88820c74f5fb9f21f148459422a)`);
78
77
  }
79
- const [atom, ...rest] = parts;
80
- const cmd = rest.join(' '); // cmd might have spaces if it's not a strict enum? but usually single word key.
78
+ const [
79
+ atom,
80
+ payloadCountStr,
81
+ ] = parts;
81
82
 
82
83
  if (atom !== SYNC_SAGA_CONTEXT_ATOM) {
83
84
  throw new Error(`Atom mismatch. Expected ${SYNC_SAGA_CONTEXT_ATOM}. Got ${atom}. (E: 67d71fc0f8d04840b178652309f471f5)`);
84
85
  }
85
86
 
86
- return { atom, cmd };
87
+ if (!payloadCountStr) { throw new Error(`invalid ib. second piece should be payloadCount but is falsy (E: 0bb7580793d800ca02227828a7294826)`); }
88
+ const payloadCount = parseInt(payloadCountStr);
89
+ if (isNaN(payloadCount)) {
90
+ throw new Error(`payloadCountStr is not a number. (E: 49457814282a44069871630987588722)`);
91
+ }
92
+
93
+ return { atom, payloadCount };
87
94
  } catch (error) {
88
95
  console.error(`${lc} ${extractErrorMsg(error)}`);
89
96
  throw error;
@@ -96,26 +103,32 @@ export async function parseSyncSagaContextIb({
96
103
  * @returns The context ibGib.
97
104
  */
98
105
  export async function createSyncSagaContext({
99
- cmd,
100
106
  sagaFrame,
101
107
  sessionKeystones,
102
- payloadAddrs,
108
+ payloadIbGibsDomain,
103
109
  }: CreateSyncSagaContextOptions): Promise<SyncSagaContextIbGib_V1> {
104
110
 
111
+ const date = new Date();
112
+ const timestamp = getTimestamp(date);
113
+ const timestampMs = date.getMilliseconds();
114
+
105
115
  const data: SyncSagaContextData_V1 = {
106
- cmd,
116
+ timestamp,
117
+ timestampMs,
107
118
  };
108
- if (payloadAddrs && payloadAddrs.length > 0) {
109
- data[SYNC_SAGA_PAYLOAD_ADDRS_REL8N_NAME] = payloadAddrs;
110
- }
111
-
112
- const rel8ns: SyncSagaContextRel8ns_V1 = {};
113
119
 
114
- if (sagaFrame) {
115
- rel8ns.sagaFrame = [getIbGibAddr(sagaFrame)];
120
+ // Domain Payloads
121
+ const payloadAddrsDomain = payloadIbGibsDomain ? payloadIbGibsDomain?.map(x => getIbGibAddr({ ibGib: x })) : undefined;
122
+ if (payloadAddrsDomain && payloadAddrsDomain.length > 0) {
123
+ data[SYNC_SAGA_PAYLOAD_ADDRS_DOMAIN] = payloadAddrsDomain;
116
124
  }
125
+
126
+ // rel8ns should always have saga frame, sometimes have keystone
127
+ const rel8ns: SyncSagaContextRel8ns_V1 = {
128
+ sagaFrame: [getIbGibAddr({ ibGib: sagaFrame })],
129
+ };
117
130
  if (sessionKeystones && sessionKeystones.length > 0) {
118
- rel8ns.sessionKeystone = sessionKeystones.map(getIbGibAddr);
131
+ rel8ns.sessionKeystone = sessionKeystones.map(x => getIbGibAddr({ ibGib: x }));
119
132
  }
120
133
 
121
134
  // Generate standard ib
@@ -126,7 +139,12 @@ export async function createSyncSagaContext({
126
139
  ib,
127
140
  data,
128
141
  rel8ns,
129
- });
142
+ }) as SyncSagaContextIbGib_V1;
143
+
144
+ // Attach actual ibgibs for transport
145
+ if (payloadIbGibsDomain && payloadIbGibsDomain.length > 0) {
146
+ contextIbGib.payloadIbGibsDomain = payloadIbGibsDomain;
147
+ }
130
148
 
131
- return contextIbGib as SyncSagaContextIbGib_V1;
149
+ return contextIbGib;
132
150
  }