@ibgib/core-gib 0.1.12 → 0.1.13

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.
@@ -0,0 +1,125 @@
1
+ import { extractErrorMsg } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
2
+ import { getIbGibAddr } from "@ibgib/ts-gib/dist/helper.mjs";
3
+ import {
4
+ IbGibAddr, TransformOpts, TransformOpts_Mut8, TransformOpts_Rel8,
5
+ TransformResult,
6
+ } from "@ibgib/ts-gib/dist/types.mjs";
7
+ import { mut8 } from "@ibgib/ts-gib/dist/V1/transforms/mut8.mjs";
8
+ import { rel8 } from "@ibgib/ts-gib/dist/V1/transforms/rel8.mjs";
9
+ import { IbGib_V1 } from "@ibgib/ts-gib/dist/V1/types.mjs";
10
+
11
+ /**
12
+ * iteratively (recursively) apply transforms in
13
+ * `dnaAddrsToApplyToStoreVersion` to the store's version of the ibgib,
14
+ * keeping track of all dependencies used/created. ibgibs that we create
15
+ * along the way that we'll set to createdIbGibs to include dna,
16
+ * intermediate ibGibs, and newer versions of the source.
17
+ *
18
+ * populates the given `createdibGibs_Running` array, INCLUDING THE MOST
19
+ * RECENT IBGIB THAT WILL BE RETURNED BY THE FUNCTION.
20
+ *
21
+ * @returns the final ibgib produced from the transform applications.
22
+ */
23
+ export async function applyTransforms({
24
+ src,
25
+ createdIbGibs_Running,
26
+ dnaAddrsToApplyToStoreVersion,
27
+ allLocalIbGibs,
28
+ }: {
29
+ /**
30
+ * Most recent src to which we will apply the given
31
+ * `dnaAddrsToApplyToStoreVersion`.
32
+ */
33
+ src: IbGib_V1,
34
+ /**
35
+ * This will be populated through recursive iterations while applying
36
+ * dnas. Everything, including new dnas, new intermediate ibgibs, and
37
+ * new "src" ibgibs will be added to this before the final recursive
38
+ * call returns the final produced (out) ibgib.
39
+ */
40
+ createdIbGibs_Running: IbGib_V1[],
41
+ /**
42
+ * Transform addresses whose ibgib will be applied to the given `src`.
43
+ * This will be reduced by one per each recursive call to this function
44
+ * that is made.
45
+ */
46
+ dnaAddrsToApplyToStoreVersion: IbGibAddr[],
47
+ /**
48
+ * Should contain the dna ibGibs that are given in
49
+ * `dnaAddrsToApplyToStoreVersion`
50
+ */
51
+ allLocalIbGibs: IbGib_V1[],
52
+ }): Promise<IbGib_V1> {
53
+ const lc = `[${applyTransforms.name}]`;
54
+ try {
55
+ if (dnaAddrsToApplyToStoreVersion.length > 0) {
56
+ const dnaAddrToApply = dnaAddrsToApplyToStoreVersion.splice(0, 1)[0];
57
+ // we expect this dna ibgib to supplied to us, but need to check
58
+ const dnaIbGib_IntermediateArray =
59
+ allLocalIbGibs.filter(x => getIbGibAddr({ ibGib: x }) === dnaAddrToApply);
60
+ let dnaIbGib: IbGib_V1;
61
+ if (dnaIbGib_IntermediateArray.length === 1) {
62
+ dnaIbGib = dnaIbGib_IntermediateArray[0];
63
+ } else if (dnaIbGib_IntermediateArray.length === 0) {
64
+ throw new Error(`dna ibGib not found in supplied allLocalIbGibs. dnaAddr: ${dnaAddrToApply}. (E: 7f56826852cf48a79ab8af16bf27e284)`);
65
+ } else {
66
+ // more than one ibgib with the dna address?
67
+ throw new Error(`More than one ibGib in allLocalIbGibs with the dna address of ${dnaAddrToApply}? (E: a726134f4cc14a4fb2ed2d39d22af17c)(UNEXPECTED)`);
68
+ }
69
+
70
+ // we have our dna to apply, so do so against the incoming src
71
+ // any transform options is actually the dna.data plus src info.
72
+ let argTransform = dnaIbGib.data as TransformOpts<IbGib_V1>;
73
+ argTransform.src = src;
74
+ argTransform.srcAddr = getIbGibAddr({ ibGib: src });
75
+ let resTransform: TransformResult<IbGib_V1>;
76
+ switch (argTransform.type) {
77
+ case 'mut8':
78
+ resTransform = await mut8(argTransform as TransformOpts_Mut8<IbGib_V1, any>);
79
+ break;
80
+ case 'rel8':
81
+ resTransform = await rel8(argTransform as TransformOpts_Rel8<IbGib_V1>);
82
+ break;
83
+ case 'fork':
84
+ throw new Error(`fork transform not expected. atow only a single fork is expected at the beginning of the lifetime of a tjp ibgib. This fork defines the uniqueness of the tjp ibgib. When merging another tjp ibgib, we would only expect update transforms to be mut8 or rel8. (E: f8ad6996ac5545edad2d58a293d37d94)`);
85
+ default:
86
+ throw new Error(`unknown dna argTransform.type (${argTransform.type}) for dna address: ${dnaAddrToApply}. Expecting either mut8 or rel8. (fork transform is known but not expected either.) (E: 52a98db56e934e4cb42c64e2f45fa552)`);
87
+ }
88
+
89
+ // add intermediate ibgibs (including dna) to createdIbGibs_Running
90
+ (resTransform.intermediateIbGibs ?? []).forEach(x => createdIbGibs_Running.push(x));
91
+ (resTransform.dnas ?? []).forEach(x => createdIbGibs_Running.push(x));
92
+ createdIbGibs_Running.push(resTransform.newIbGib);
93
+
94
+ // not 100% but I believe the created dna should deep equal our
95
+ // incoming dna that we applied. so we'll warn if it doesn't.
96
+ // (ibgib addresses matching necessitates deep equal)
97
+ if ((resTransform.dnas ?? []).length > 0) {
98
+ if (!resTransform.dnas!.some(x => getIbGibAddr(x) === dnaAddrToApply)) {
99
+ console.warn(`${lc}(UNEXPECTED) Expected to generate exact dna that we applied in transform. dnaAddr: ${dnaAddrToApply} (W: 4a364c0b5d8d46c8af6bd540915fd973)`);
100
+ }
101
+ } else {
102
+ console.warn(`${lc}(UNEXPECTED) expected resTransform to include generated dna. dnaAddr: ${dnaAddrToApply}(W: 9648e2d5c40d42b7b9fb7efb09c5b13a)`)
103
+ }
104
+
105
+
106
+ // call recursively
107
+ return await applyTransforms({
108
+ src: resTransform.newIbGib,
109
+ createdIbGibs_Running,
110
+ dnaAddrsToApplyToStoreVersion,
111
+ allLocalIbGibs,
112
+ });
113
+ } else {
114
+ // no more dna to apply.
115
+ // createdIbGibs_Running should be populated
116
+ if (createdIbGibs_Running.length === 0) {
117
+ console.warn(`${lc} no dna transforms to apply but createdIbGibs_Running is empty. Dna is expected to start with at least one transform, otherwise don't call this function. (W: 52c7f2f6bd7e4bd0b31f53611b90268b)(UNEXPECTED)`)
118
+ }
119
+ return src;
120
+ }
121
+ } catch (error) {
122
+ console.error(`${lc} ${extractErrorMsg(error)}`);
123
+ throw error;
124
+ }
125
+ }