@ibgib/core-gib 0.0.105 → 0.0.107

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 (39) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/common/import-export/import-export-helper.web.d.mts +49 -0
  3. package/dist/common/import-export/import-export-helper.web.d.mts.map +1 -0
  4. package/dist/common/import-export/import-export-helper.web.mjs +184 -0
  5. package/dist/common/import-export/import-export-helper.web.mjs.map +1 -0
  6. package/dist/common/import-export/import-export-types.d.mts +9 -0
  7. package/dist/common/import-export/import-export-types.d.mts.map +1 -1
  8. package/dist/common/other/graph-helper.d.mts +2 -3
  9. package/dist/common/other/graph-helper.d.mts.map +1 -1
  10. package/dist/common/other/graph-helper.mjs.map +1 -1
  11. package/dist/common/other/graph-types.d.mts +8 -0
  12. package/dist/common/other/graph-types.d.mts.map +1 -0
  13. package/dist/common/other/graph-types.mjs +2 -0
  14. package/dist/common/other/graph-types.mjs.map +1 -0
  15. package/dist/common/other/ibgib-helper.respec.mjs.map +1 -1
  16. package/dist/common/other/other-helper.respec.d.mts +2 -0
  17. package/dist/common/other/other-helper.respec.d.mts.map +1 -0
  18. package/dist/common/other/other-helper.respec.mjs +85 -0
  19. package/dist/common/other/other-helper.respec.mjs.map +1 -0
  20. package/dist/common/other/other-helper.web.d.mts +30 -0
  21. package/dist/common/other/other-helper.web.d.mts.map +1 -0
  22. package/dist/common/other/other-helper.web.mjs +161 -0
  23. package/dist/common/other/other-helper.web.mjs.map +1 -0
  24. package/dist/common/other/other-types.d.mts +7 -0
  25. package/dist/common/other/other-types.d.mts.map +1 -1
  26. package/package.json +1 -1
  27. package/src/common/import-export/import-export-helper.web.mts +222 -0
  28. package/src/common/import-export/import-export-types.mts +9 -0
  29. package/src/common/other/graph-helper.mts +2 -2
  30. package/src/common/other/graph-types.mts +6 -0
  31. package/src/common/other/ibgib-helper.respec.mts +1 -1
  32. package/src/common/other/other-helper.respec.mts +100 -0
  33. package/src/common/other/other-helper.web.mts +184 -0
  34. package/src/common/other/other-types.mts +8 -0
  35. package/dist/common/import-export/import-export-helper.d.mts +0 -18
  36. package/dist/common/import-export/import-export-helper.d.mts.map +0 -1
  37. package/dist/common/import-export/import-export-helper.mjs +0 -65
  38. package/dist/common/import-export/import-export-helper.mjs.map +0 -1
  39. package/src/common/import-export/import-export-helper.mts +0 -62
@@ -0,0 +1,184 @@
1
+ import { extractErrorMsg } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
2
+
3
+ import { GLOBAL_LOG_A_LOT } from "../../core-constants.mjs";
4
+ import { FlatIbGibGraph } from "./graph-types.mjs";
5
+ import { SerializedUint8Array } from "./other-types.mjs";
6
+
7
+ const logalot = GLOBAL_LOG_A_LOT;
8
+
9
+ /**
10
+ * @internal Helper to convert a Uint8Array to a Base64 string
11
+ * @param uint8Array @
12
+ * @returns Base64 string
13
+ */
14
+ function uint8ArrayToBase64(uint8Array: Uint8Array): Promise<string> {
15
+ return new Promise((resolve, reject) => {
16
+ const blob = new Blob([uint8Array], { type: 'application/octet-stream' });
17
+ const reader = new FileReader();
18
+ reader.onload = () => {
19
+ const dataUrl = reader.result as string;
20
+ // The result is a data URL: "data:application/octet-stream;base64,..."
21
+ // We only want the Base64 part.
22
+ const base64String = dataUrl.split(',')[1];
23
+ resolve(base64String);
24
+ };
25
+ reader.onerror = (error) => reject(error);
26
+ reader.readAsDataURL(blob);
27
+ });
28
+ }
29
+
30
+ /**
31
+ * @internal Helper to convert a Base64 string back to a Uint8Array
32
+ * @param base64String to convert back to Uint8Array
33
+ * @returns Uint8Array
34
+ */
35
+ async function base64ToUint8Array(base64String: string): Promise<Uint8Array> {
36
+ // Use the fetch API to decode the Base64 string.
37
+ // This is a modern and robust way to handle Base64 in the browser.
38
+ const dataUrl = `data:application/octet-stream;base64,${base64String}`;
39
+ const response = await fetch(dataUrl);
40
+ const blob = await response.blob();
41
+ const buffer = await blob.arrayBuffer();
42
+ return new Uint8Array(buffer);
43
+ }
44
+
45
+ /**
46
+ * Compresses an FlatIbGibGraph object into a Blob.
47
+ * @param graph The FlatIbGibGraph object to compress.
48
+ * @returns A Promise that resolves with the compressed Blob.
49
+ */
50
+ export async function compressIbGib(graph: FlatIbGibGraph): Promise<Blob> {
51
+ // 1. Serialize: Create a JSON-safe representation of the graph
52
+ // We use a custom replacer to handle Uint8Array
53
+ // const replacer = (key: string, value: any) => {
54
+ // if (value instanceof Uint8Array) {
55
+ // // This part is async, so we can't use it directly in JSON.stringify.
56
+ // // We must pre-process the object first.
57
+ // throw new Error("Cannot use async replacer. Pre-process the object first.");
58
+ // }
59
+ // return value;
60
+ // };
61
+
62
+ // Pre-process the graph to handle async Base64 conversion
63
+ const serializableGraph = JSON.parse(JSON.stringify(graph)); // Deep clone to avoid mutation
64
+ for (const key in serializableGraph) {
65
+ if (graph[key].data instanceof Uint8Array) {
66
+ serializableGraph[key].data = {
67
+ _dataType: 'Uint8Array_Base64',
68
+ value: await uint8ArrayToBase64(graph[key].data as Uint8Array)
69
+ } as SerializedUint8Array;
70
+ }
71
+ }
72
+
73
+ // 2. Stringify: Convert the serializable object to a JSON string
74
+ const jsonString = JSON.stringify(serializableGraph);
75
+
76
+ // 3. Encode: Convert the string to a Uint8Array (UTF-8)
77
+ const dataToCompress = new TextEncoder().encode(jsonString);
78
+
79
+ // 4. Compress: Use the Compression Streams API
80
+ const stream = new Response(dataToCompress).body!;
81
+ const compressedStream = stream.pipeThrough(new CompressionStream('gzip'));
82
+
83
+ // Return the compressed data as a Blob
84
+ return await new Response(compressedStream).blob();
85
+ }
86
+
87
+ /**
88
+ * Decompresses a Blob back into an FlatIbGibGraph object.
89
+ * @param compressedBlob The Blob containing the compressed graph data.
90
+ * @returns A Promise that resolves with the original FlatIbGibGraph object.
91
+ */
92
+ export async function decompressIbGib(compressedBlob: Blob): Promise<FlatIbGibGraph> {
93
+ // 1. Decompress: Use the Decompression Streams API
94
+ const decompressedStream = compressedBlob.stream().pipeThrough(
95
+ new DecompressionStream('gzip')
96
+ );
97
+
98
+ // 2. Decode: Convert the decompressed stream back to a string
99
+ const jsonString = await new Response(decompressedStream).text();
100
+
101
+ // 3. Deserialize: Parse the JSON with a reviver to restore Uint8Arrays
102
+ const reviver = (key: string, value: any) => {
103
+ if (value && value._dataType === 'Uint8Array_Base64') {
104
+ // This part is async, so we can't use it in JSON.parse.
105
+ // We must post-process the object.
106
+ return value; // Return the placeholder for now
107
+ }
108
+ return value;
109
+ };
110
+
111
+ const parsedGraph = JSON.parse(jsonString, reviver);
112
+
113
+ // Post-process to convert Base64 back to Uint8Array
114
+ for (const key in parsedGraph) {
115
+ const data = parsedGraph[key].data;
116
+ if (data && data._dataType === 'Uint8Array_Base64') {
117
+ parsedGraph[key].data = await base64ToUint8Array(data.value);
118
+ }
119
+ }
120
+
121
+ return parsedGraph as FlatIbGibGraph;
122
+ }
123
+
124
+ /**
125
+ * Compresses an FlatIbGibGraph object and returns a Base64 string representation.
126
+ * @param graph The FlatIbGibGraph object to compress.
127
+ * @returns A Promise that resolves with the compressed data as a Base64 string.
128
+ */
129
+ export async function compressIbGibGraphToString({
130
+ graph
131
+ }: {
132
+ graph: FlatIbGibGraph
133
+ }): Promise<string> {
134
+ const lc = `[${compressIbGibGraphToString.name}]`;
135
+ try {
136
+ if (logalot) { console.log(`${lc} starting... (I: 1352f8540c08f0b725952df8f18c4825)`); }
137
+
138
+ // 1. Get the compressed data as a Blob first.
139
+ const compressedBlob = await compressIbGib(graph);
140
+
141
+ // 2. Convert the Blob's binary data to a Uint8Array.
142
+ const buffer = await compressedBlob.arrayBuffer();
143
+ const uint8Array = new Uint8Array(buffer);
144
+
145
+ // 3. Convert that Uint8Array to a Base64 string.
146
+ return await uint8ArrayToBase64(uint8Array);
147
+ } catch (error) {
148
+ console.error(`${lc} ${extractErrorMsg(error)}`);
149
+ throw error;
150
+ } finally {
151
+ if (logalot) { console.log(`${lc} complete.`); }
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Decompresses a Base64 string back into an FlatIbGibGraph object.
157
+ * @param compressedBase64 The Base64 string containing the compressed graph data.
158
+ * @returns A Promise that resolves with the original FlatIbGibGraph object.
159
+ */
160
+ export async function decompressIbGibGraphFromString({
161
+ compressedBase64,
162
+ }: {
163
+ compressedBase64: string,
164
+ }): Promise<FlatIbGibGraph> {
165
+ const lc = `[${decompressIbGibGraphFromString.name}]`;
166
+ try {
167
+ if (logalot) { console.log(`${lc} starting... (I: 5f3fa940a2c8f8d7b877978ae7f87825)`); }
168
+
169
+ // 1. Convert the Base64 string back to a Uint8Array.
170
+ const compressedUint8Array = await base64ToUint8Array(compressedBase64);
171
+
172
+ // 2. Create a Blob from the Uint8Array.
173
+ // The MIME type isn't strictly necessary for DecompressionStream but is good practice.
174
+ const compressedBlob = new Blob([compressedUint8Array], { type: 'application/gzip' });
175
+
176
+ // 3. Use our existing function to decompress the Blob.
177
+ return await decompressIbGib(compressedBlob);
178
+ } catch (error) {
179
+ console.error(`${lc} ${extractErrorMsg(error)}`);
180
+ throw error;
181
+ } finally {
182
+ if (logalot) { console.log(`${lc} complete.`); }
183
+ }
184
+ }
@@ -185,3 +185,11 @@ export interface IbGibTimelineUpdateInfo extends IbGib_V1 {
185
185
  latestAddr: IbGibAddr;
186
186
  latestIbGib?: IbGib_V1<any>;
187
187
  }
188
+
189
+ /**
190
+ * Serializing helper type for compressing dependency graphs.
191
+ */
192
+ export type SerializedUint8Array = {
193
+ _dataType: 'Uint8Array_Base64';
194
+ value: string;
195
+ };
@@ -1,18 +0,0 @@
1
- import { Ib } from "@ibgib/ts-gib/dist/types.mjs";
2
- import { RawExportData_V1, RawExportIbInfo } from "./import-export-types.mjs";
3
- /**
4
- * builds the ib for a {@link RawExportIbGib_V1}
5
- */
6
- export declare function getRawExportIb({ data, }: {
7
- data: RawExportData_V1;
8
- }): Ib;
9
- /**
10
- * space-delimited info contained in ib.
11
- *
12
- * atow (02/2024)
13
- * `${RAW_EXPORT_ATOM} ${timestampInTicks} ${graphSize} ${graphStringLength}`
14
- */
15
- export declare function parseRawExportIb({ ib, }: {
16
- ib: Ib;
17
- }): RawExportIbInfo;
18
- //# sourceMappingURL=import-export-helper.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"import-export-helper.d.mts","sourceRoot":"","sources":["../../../src/common/import-export/import-export-helper.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,8BAA8B,CAAC;AAGlD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAK9E;;GAEG;AACH,wBAAgB,cAAc,CAAC,EAC3B,IAAI,GACP,EAAE;IACC,IAAI,EAAE,gBAAgB,CAAC;CAC1B,GAAG,EAAE,CAcL;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,EAC7B,EAAE,GACL,EAAE;IACC,EAAE,EAAE,EAAE,CAAC;CACV,GAAG,eAAe,CAmBlB"}
@@ -1,65 +0,0 @@
1
- import { extractErrorMsg, getTimestampInTicks } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
2
- import { GLOBAL_LOG_A_LOT } from "../../core-constants.mjs";
3
- import { RAW_EXPORT_ATOM } from "./import-export-constants.mjs";
4
- const logalot = GLOBAL_LOG_A_LOT;
5
- /**
6
- * builds the ib for a {@link RawExportIbGib_V1}
7
- */
8
- export function getRawExportIb({ data, }) {
9
- const lc = `[${getRawExportIb.name}]`;
10
- try {
11
- if (logalot) {
12
- console.log(`${lc} starting... (I: 158dfaeb9613a1531be71a9735184624)`);
13
- }
14
- const { graphSize, dependencyGraphAsString, timestamp } = data;
15
- const graphStringLength = dependencyGraphAsString.length;
16
- const timestampInTicks = getTimestampInTicks(timestamp);
17
- return `${RAW_EXPORT_ATOM} ${timestampInTicks} ${graphSize} ${graphStringLength}`;
18
- }
19
- catch (error) {
20
- console.error(`${lc} ${extractErrorMsg(error)}`);
21
- throw error;
22
- }
23
- finally {
24
- if (logalot) {
25
- console.log(`${lc} complete.`);
26
- }
27
- }
28
- }
29
- /**
30
- * space-delimited info contained in ib.
31
- *
32
- * atow (02/2024)
33
- * `${RAW_EXPORT_ATOM} ${timestampInTicks} ${graphSize} ${graphStringLength}`
34
- */
35
- export function parseRawExportIb({ ib, }) {
36
- const lc = `[${parseRawExportIb.name}]`;
37
- try {
38
- if (logalot) {
39
- console.log(`${lc} starting... (I: 3297e51184f87a45d343d8654b165324)`);
40
- }
41
- const [atom, timestampInTicks, graphSizeStr, graphStringLengthStr] = ib.split(' ');
42
- if (atom !== RAW_EXPORT_ATOM) {
43
- throw new Error(`atom (${atom}) !== ${RAW_EXPORT_ATOM} (E: ad7545da5b8c4baeb15d86e92d657524)`);
44
- }
45
- const graphSize = Number.parseInt(graphSizeStr);
46
- if (Number.isNaN(graphSize) || graphSize < 0) {
47
- throw new Error(`graphSize (${graphSizeStr}) is not a positive integer. (E: 2b43d9d3462f6e16547b121bccef9b24)`);
48
- }
49
- const graphStringLength = Number.parseInt(graphStringLengthStr);
50
- if (Number.isNaN(graphStringLength) || graphStringLength < 0) {
51
- throw new Error(`graphStringLength (${graphStringLengthStr}) is not a positive integer. (E: 112ceccee1a54b19ab4f4662b6694ba0)`);
52
- }
53
- return { atom, timestampInTicks, graphSize, graphStringLength };
54
- }
55
- catch (error) {
56
- console.error(`${lc} ${extractErrorMsg(error)}`);
57
- throw error;
58
- }
59
- finally {
60
- if (logalot) {
61
- console.log(`${lc} complete.`);
62
- }
63
- }
64
- }
65
- //# sourceMappingURL=import-export-helper.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"import-export-helper.mjs","sourceRoot":"","sources":["../../../src/common/import-export/import-export-helper.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,iDAAiD,CAAC;AAGvG,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAEhE,MAAM,OAAO,GAAG,gBAA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,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;QAChD,IAAI,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,SAAS,GAAG,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,cAAc,YAAY,oEAAoE,CAAC,CAAC;SAAE;QAElK,MAAM,iBAAiB,GAAG,MAAM,CAAC,QAAQ,CAAC,oBAAoB,CAAC,CAAC;QAChE,IAAI,MAAM,CAAC,KAAK,CAAC,iBAAiB,CAAC,IAAI,iBAAiB,GAAG,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,sBAAsB,oBAAoB,oEAAoE,CAAC,CAAC;SAAE;QAElM,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,SAAS,EAAE,iBAAiB,EAAE,CAAC;KACnE;IAAC,OAAO,KAAK,EAAE;QACZ,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACjD,MAAM,KAAK,CAAC;KACf;YAAS;QACN,IAAI,OAAO,EAAE;YAAE,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;SAAE;KACnD;AACL,CAAC"}
@@ -1,62 +0,0 @@
1
- import { extractErrorMsg, getTimestampInTicks } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
2
- import { Ib } from "@ibgib/ts-gib/dist/types.mjs";
3
-
4
- import { GLOBAL_LOG_A_LOT } from "../../core-constants.mjs";
5
- import { RawExportData_V1, RawExportIbInfo } from "./import-export-types.mjs";
6
- import { RAW_EXPORT_ATOM } from "./import-export-constants.mjs";
7
-
8
- const logalot = GLOBAL_LOG_A_LOT;
9
-
10
- /**
11
- * builds the ib for a {@link RawExportIbGib_V1}
12
- */
13
- export function getRawExportIb({
14
- data,
15
- }: {
16
- data: RawExportData_V1,
17
- }): Ib {
18
- const lc = `[${getRawExportIb.name}]`;
19
- try {
20
- if (logalot) { console.log(`${lc} starting... (I: 158dfaeb9613a1531be71a9735184624)`); }
21
- const { graphSize, dependencyGraphAsString, timestamp } = data;
22
- const graphStringLength = dependencyGraphAsString.length;
23
- const timestampInTicks = getTimestampInTicks(timestamp);
24
- return `${RAW_EXPORT_ATOM} ${timestampInTicks} ${graphSize} ${graphStringLength}`;
25
- } catch (error) {
26
- console.error(`${lc} ${extractErrorMsg(error)}`);
27
- throw error;
28
- } finally {
29
- if (logalot) { console.log(`${lc} complete.`); }
30
- }
31
- }
32
-
33
- /**
34
- * space-delimited info contained in ib.
35
- *
36
- * atow (02/2024)
37
- * `${RAW_EXPORT_ATOM} ${timestampInTicks} ${graphSize} ${graphStringLength}`
38
- */
39
- export function parseRawExportIb({
40
- ib,
41
- }: {
42
- ib: Ib,
43
- }): RawExportIbInfo {
44
- const lc = `[${parseRawExportIb.name}]`;
45
- try {
46
- if (logalot) { console.log(`${lc} starting... (I: 3297e51184f87a45d343d8654b165324)`); }
47
- const [atom, timestampInTicks, graphSizeStr, graphStringLengthStr] = ib.split(' ');
48
- if (atom !== RAW_EXPORT_ATOM) { throw new Error(`atom (${atom}) !== ${RAW_EXPORT_ATOM} (E: ad7545da5b8c4baeb15d86e92d657524)`); }
49
- const graphSize = Number.parseInt(graphSizeStr);
50
- if (Number.isNaN(graphSize) || graphSize < 0) { throw new Error(`graphSize (${graphSizeStr}) is not a positive integer. (E: 2b43d9d3462f6e16547b121bccef9b24)`); }
51
-
52
- const graphStringLength = Number.parseInt(graphStringLengthStr);
53
- if (Number.isNaN(graphStringLength) || graphStringLength < 0) { throw new Error(`graphStringLength (${graphStringLengthStr}) is not a positive integer. (E: 112ceccee1a54b19ab4f4662b6694ba0)`); }
54
-
55
- return { atom, timestampInTicks, graphSize, graphStringLength };
56
- } catch (error) {
57
- console.error(`${lc} ${extractErrorMsg(error)}`);
58
- throw error;
59
- } finally {
60
- if (logalot) { console.log(`${lc} complete.`); }
61
- }
62
- }