@ibgib/node-gib 0.0.2

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 (127) hide show
  1. package/.ibgibignore +30 -0
  2. package/CHANGELOG.md +18 -0
  3. package/README.md +16 -0
  4. package/dist/assumptions.respec.d.mts +2 -0
  5. package/dist/assumptions.respec.d.mts.map +1 -0
  6. package/dist/assumptions.respec.mjs +41 -0
  7. package/dist/assumptions.respec.mjs.map +1 -0
  8. package/dist/common/prompt-functions.d.mts +89 -0
  9. package/dist/common/prompt-functions.d.mts.map +1 -0
  10. package/dist/common/prompt-functions.mjs +418 -0
  11. package/dist/common/prompt-functions.mjs.map +1 -0
  12. package/dist/constants.d.mts +3 -0
  13. package/dist/constants.d.mts.map +1 -0
  14. package/dist/constants.mjs +3 -0
  15. package/dist/constants.mjs.map +1 -0
  16. package/dist/foo.d.mts +2 -0
  17. package/dist/foo.d.mts.map +1 -0
  18. package/dist/foo.mjs +19 -0
  19. package/dist/foo.mjs.map +1 -0
  20. package/dist/helpers.d.mts +2 -0
  21. package/dist/helpers.d.mts.map +1 -0
  22. package/dist/helpers.mjs +3 -0
  23. package/dist/helpers.mjs.map +1 -0
  24. package/dist/respec-gib.node.d.mts +2 -0
  25. package/dist/respec-gib.node.d.mts.map +1 -0
  26. package/dist/respec-gib.node.mjs +213 -0
  27. package/dist/respec-gib.node.mjs.map +1 -0
  28. package/dist/spec-helper.node.respec.d.mts +13 -0
  29. package/dist/spec-helper.node.respec.d.mts.map +1 -0
  30. package/dist/spec-helper.node.respec.mjs +50 -0
  31. package/dist/spec-helper.node.respec.mjs.map +1 -0
  32. package/dist/types.d.mts +2 -0
  33. package/dist/types.d.mts.map +1 -0
  34. package/dist/types.mjs +2 -0
  35. package/dist/types.mjs.map +1 -0
  36. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.d.mts +35 -0
  37. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.d.mts.map +1 -0
  38. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.mjs +267 -0
  39. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.mjs.map +1 -0
  40. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.d.mts +98 -0
  41. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.d.mts.map +1 -0
  42. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.mjs +160 -0
  43. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.mjs.map +1 -0
  44. package/dist/witness/space/node-filesystem-space/node-filesystem-space-helper.d.mts +42 -0
  45. package/dist/witness/space/node-filesystem-space/node-filesystem-space-helper.d.mts.map +1 -0
  46. package/dist/witness/space/node-filesystem-space/node-filesystem-space-helper.mjs +312 -0
  47. package/dist/witness/space/node-filesystem-space/node-filesystem-space-helper.mjs.map +1 -0
  48. package/dist/witness/space/node-filesystem-space/node-filesystem-space-types.d.mts +84 -0
  49. package/dist/witness/space/node-filesystem-space/node-filesystem-space-types.d.mts.map +1 -0
  50. package/dist/witness/space/node-filesystem-space/node-filesystem-space-types.mjs +65 -0
  51. package/dist/witness/space/node-filesystem-space/node-filesystem-space-types.mjs.map +1 -0
  52. package/dist/witness/space/node-filesystem-space/node-filesystem-space-v1.d.mts +77 -0
  53. package/dist/witness/space/node-filesystem-space/node-filesystem-space-v1.d.mts.map +1 -0
  54. package/dist/witness/space/node-filesystem-space/node-filesystem-space-v1.mjs +672 -0
  55. package/dist/witness/space/node-filesystem-space/node-filesystem-space-v1.mjs.map +1 -0
  56. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.d.mts +5 -0
  57. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.d.mts.map +1 -0
  58. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.mjs +5 -0
  59. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.mjs.map +1 -0
  60. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.d.mts +30 -0
  61. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.d.mts.map +1 -0
  62. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.mjs +38 -0
  63. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.mjs.map +1 -0
  64. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.d.mts +64 -0
  65. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.d.mts.map +1 -0
  66. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.mjs +353 -0
  67. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.mjs.map +1 -0
  68. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.d.mts +2 -0
  69. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.d.mts.map +1 -0
  70. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.mjs +52 -0
  71. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.mjs.map +1 -0
  72. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.d.mts +2 -0
  73. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.d.mts.map +1 -0
  74. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.mjs +53 -0
  75. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.mjs.map +1 -0
  76. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.d.mts +2 -0
  77. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.d.mts.map +1 -0
  78. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.mjs +59 -0
  79. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.mjs.map +1 -0
  80. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.d.mts +2 -0
  81. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.d.mts.map +1 -0
  82. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.mjs +55 -0
  83. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.mjs.map +1 -0
  84. package/dist/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.d.mts +2 -0
  85. package/dist/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.d.mts.map +1 -0
  86. package/dist/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.mjs +67 -0
  87. package/dist/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.mjs.map +1 -0
  88. package/dist/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.d.mts +2 -0
  89. package/dist/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.d.mts.map +1 -0
  90. package/dist/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.mjs +67 -0
  91. package/dist/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.mjs.map +1 -0
  92. package/dist/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.d.mts +2 -0
  93. package/dist/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.d.mts.map +1 -0
  94. package/dist/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.mjs +68 -0
  95. package/dist/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.mjs.map +1 -0
  96. package/dist/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.d.mts +2 -0
  97. package/dist/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.d.mts.map +1 -0
  98. package/dist/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.mjs +69 -0
  99. package/dist/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.mjs.map +1 -0
  100. package/generate-version-file.js +29 -0
  101. package/package.json +41 -0
  102. package/src/assumptions.respec.mts +51 -0
  103. package/src/common/prompt-functions.mts +429 -0
  104. package/src/constants.mts +2 -0
  105. package/src/foo.mts +13 -0
  106. package/src/helpers.mts +5 -0
  107. package/src/respec-gib.node.mts +201 -0
  108. package/src/spec-helper.node.respec.mts +50 -0
  109. package/src/types.mts +0 -0
  110. package/src/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.mts +290 -0
  111. package/src/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.mts +166 -0
  112. package/src/witness/space/node-filesystem-space/node-filesystem-space-helper.mts +298 -0
  113. package/src/witness/space/node-filesystem-space/node-filesystem-space-types.mts +150 -0
  114. package/src/witness/space/node-filesystem-space/node-filesystem-space-v1.mts +672 -0
  115. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.mts +6 -0
  116. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.mts +68 -0
  117. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.mts +403 -0
  118. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.mts +68 -0
  119. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.mts +70 -0
  120. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.mts +72 -0
  121. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.mts +72 -0
  122. package/src/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.mts +87 -0
  123. package/src/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.mts +88 -0
  124. package/src/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.mts +88 -0
  125. package/src/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.mts +90 -0
  126. package/tsconfig.json +17 -0
  127. package/tsconfig.test.json +10 -0
@@ -0,0 +1,166 @@
1
+ /**
2
+ * @module node-metaspace driven by a node+fs storage substrate
3
+ *
4
+ * a metaspace is a space of spaces. it adds/removes spaces and does
5
+ * inter-spatial composition related things. it's similar to what people think
6
+ * of in terms of a node in a p2p network.
7
+ */
8
+
9
+ import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
10
+ import { IbGibSpaceAny } from '@ibgib/core-gib/dist/witness/space/space-base-v1.mjs';
11
+ import { IbGibCacheService } from '@ibgib/core-gib/dist/common/cache/cache-types.mjs';
12
+ import { MetaspaceBase } from '@ibgib/core-gib/dist/witness/space/metaspace/metaspace-base.mjs';
13
+ import { MetaspaceFactory } from '@ibgib/core-gib/dist/witness/space/metaspace/metaspace-types.mjs';
14
+
15
+ import { GLOBAL_LOG_A_LOT, GLOBAL_TIMER_NAME } from '../../../../constants.mjs';
16
+ import { fnCreateNewLocalSpace, fnDtoToSpace_nodeFilesystem } from './metaspace-nodespace-helper.mjs';
17
+ import { NodeFilesystemSpace_V1 } from '../../node-filesystem-space/node-filesystem-space-v1.mjs';
18
+
19
+ const logalot = GLOBAL_LOG_A_LOT;
20
+
21
+ /**
22
+ * All-purpose mega service (todo: which we'll need to break up!) to interact
23
+ * with ibgibs at the app/device level.
24
+ *
25
+ * This works with the local app user space.
26
+ *
27
+ * ## regarding special ibgibs
28
+ *
29
+ * Special ibgibs' behaviors are what hold in other apps configuration data.
30
+ * Of course the difference is that most special ibgibs can leverage the "on-chain"
31
+ * functionality of "regular" ibgibs.
32
+ *
33
+ * There are a couple meta ibgibs (which I also call "special"):
34
+ * * roots^gib
35
+ * * tracks other special root^gib ibgibs, which are like local app-level indexes.
36
+ * * tags^gib
37
+ * * tracks other special tag^gib ibgibs, which you can apply to any ibgib
38
+ * * latest^gib
39
+ * * tracks mappings between tjp -> latest ib^gib address
40
+ * * ephemeral (deletes past rel8ns and past ibGib frames)
41
+ * * ...
42
+ *
43
+ * ## regarding latest ibgibs
44
+ *
45
+ * The tjp (temporal junction point) defines atow the beginning of an ibGib
46
+ * timeline. it's like the birthday for an ibGib. (Or you can think if it as
47
+ * the id for the stream of ibgib frames in a given timeline.)
48
+ *
49
+ * The latest ibGib in that timeline is also special, because it's often what
50
+ * you want to work with.
51
+ *
52
+ * So ideally, when an ibgib, A, has a tjp A1, and it is updated to A2, A3, An
53
+ * via `mut8` and/or `rel8` transforms, that ibgib creates a single timeline.
54
+ * This service attempts to track the relationship between that starting tjp
55
+ * address and its corresponding latest frame in that timeline, i.e., A1 -> An.
56
+ *
57
+ * ### mapping persistence implementation details
58
+ *
59
+ * The latest ibGib service is backed by a special ibgib that maintains the
60
+ * mapping index. It does this by rel8-ing that special backing ibgib via the
61
+ * tjp pointer, e.g. [special latest ibgib^XXX000].rel8ns[A^TJP123] ===
62
+ * [A^N12345] . It does this via the ib^gib content address pointer, so this
63
+ * becomes a mapping from A^TJP123 to A^N12345.
64
+ *
65
+ * This backing ibGib is special (even for special ibGibs) in that:
66
+ * * it does not relate itself with the current root of the application
67
+ * * it does not maintain references to its past (i.e. rel8ns['past'] === [])
68
+ * * it DELETES its previous incarnation from the files service
69
+ *
70
+ * In other words, this service is meant to be as ephemeral as possible. I am
71
+ * keeping it as an ibGib and not some other data format (like straight in
72
+ * storage/some other db) because I've found this is often useful and what I end
73
+ * up doing anyway to leverage other ibgib behavior. For example, in the future
74
+ * it may be good to take snapshots, which is a simple copy operation of the
75
+ * file persistence.
76
+ *
77
+ * ### current naive implementation notes
78
+ *
79
+ * questions:
80
+ * * What do we want to do if we can't locate an ibGib record?
81
+ * * How/when do we want to alert the user/our own code that we've found
82
+ * multiple timelines for an ibGib with a tjp (usually a thing we want to
83
+ * avoid)?
84
+ * * Who do we want to notify when new ibGibs arrive?
85
+ * * How often do we want to check external sources for latest?
86
+ * * When do we get to merging ibGib timelines?
87
+ *
88
+ * This is behavior that is somewhat taken care of, e.g. in git, with the HEAD
89
+ * pointer for a repo. But we're talking about here basically as a metarepo or
90
+ * "repo of repos", and unlike git, we don't want our HEAD metadata living "off
91
+ * chain" (outside of the DLT itself that it's modifying). So eventually, what
92
+ * we want is just like what we want with ALL ibGibs: perspective. From "the
93
+ * app"'s perspective, the latest is mapped. But really, apps can't view slices
94
+ * of ibGib graphs in all sorts of interesting ways and still be productive &
95
+ * beneficial to the ecosystem as a whole.
96
+ */
97
+ export class Metaspace_Nodespace extends MetaspaceBase {
98
+
99
+ // we won't get an object back, only a DTO ibGib essentially
100
+ protected lc: string = `[${Metaspace_Nodespace.name}]`;
101
+
102
+ get zeroSpace(): IbGibSpaceAny {
103
+ const lc = `[${this.lc}][get zeroSpace]`;
104
+ if (this.metaspaceFactory.fnZeroSpaceFactory) {
105
+ return this.metaspaceFactory.fnZeroSpaceFactory();
106
+ } else {
107
+ throw new Error(`${lc} (UNEXPECTED) this.metaspaceFactory.fnZeroSpaceFactory falsy. not initialized? (E: 9e6c6954dacd868b18c9f34a71e63523)`);
108
+ }
109
+ }
110
+
111
+ constructor(
112
+ // public modalController: ModalController,
113
+ // public alertController: AlertController,
114
+ protected cacheSvc: IbGibCacheService | undefined,
115
+ // private latestCacheSvc: IonicStorageLatestIbgibCacheService,
116
+ ) {
117
+ super(cacheSvc);
118
+ const lc = `${this.lc}[ctor]`;
119
+ if (logalot) {
120
+ console.log(`${lc}${GLOBAL_TIMER_NAME}`);
121
+ console.timeLog(GLOBAL_TIMER_NAME);
122
+ console.log(`${lc} created. (I: 5690dc2ebf774df3a442bd463dee7455)`);
123
+ }
124
+
125
+ }
126
+
127
+ protected async initializeMetaspaceFactory({ metaspaceFactory }: {
128
+ metaspaceFactory: MetaspaceFactory,
129
+ }): Promise<void> {
130
+ const lc = `[${this.initializeMetaspaceFactory.name}]`;
131
+ try {
132
+ if (logalot) { console.log(`${lc} starting... (I: 5a29670439ac47634dcdfe25225c8223)`); }
133
+
134
+ await super.initializeMetaspaceFactory({ metaspaceFactory });
135
+
136
+ metaspaceFactory.fnDefaultLocalSpaceFactory ??= (opts) => fnCreateNewLocalSpace(opts);
137
+ metaspaceFactory.fnDtoToSpace ??= (spaceDto) => fnDtoToSpace_nodeFilesystem(spaceDto);
138
+
139
+ this.metaspaceFactory.fnZeroSpaceFactory ??= () => {
140
+ // default to node space
141
+ const zeroSpace = new NodeFilesystemSpace_V1(/*initialData*/ undefined, /*initialRel8ns*/ undefined);
142
+ zeroSpace.gib = 'gib';
143
+ return zeroSpace;
144
+ }
145
+ } catch (error) {
146
+ console.error(`${lc} ${extractErrorMsg(error)}`);
147
+ throw error;
148
+ } finally {
149
+ if (logalot) { console.log(`${lc} complete.`); }
150
+ }
151
+ }
152
+
153
+ async initializeZeroSpace(): Promise<void> {
154
+ const lc = `[${this.initializeZeroSpace.name}]`;
155
+ try {
156
+ if (logalot) { console.log(`${lc} starting... (I: a3364e9601c70c8163e67868e5d67723)`); }
157
+
158
+ } catch (error) {
159
+ console.error(`${lc} ${extractErrorMsg(error)}`);
160
+ throw error;
161
+ } finally {
162
+ if (logalot) { console.log(`${lc} complete.`); }
163
+ }
164
+ }
165
+
166
+ }
@@ -0,0 +1,298 @@
1
+ /**
2
+ * @module node-filesystem-space-helper
3
+ *
4
+ */
5
+
6
+
7
+ import { readFile, } from 'node:fs/promises';
8
+ import { default as pathUtils } from 'path';
9
+ import { cwd } from 'node:process';
10
+
11
+ import { extractErrorMsg, unique, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
12
+ import { IbGibAddr } from '@ibgib/ts-gib/dist/types.mjs';
13
+ import { getIbAndGib, } from '@ibgib/ts-gib/dist/helper.mjs';
14
+ import { GIB, } from '@ibgib/ts-gib/dist/V1/index.mjs';
15
+ import { validateIbGibIntrinsically } from '@ibgib/ts-gib/dist/V1/validate-helper.mjs';
16
+
17
+ import { GLOBAL_LOG_A_LOT } from '@ibgib/core-gib/dist/core-constants.mjs';
18
+ import { parseSpaceIb, } from '@ibgib/core-gib/dist/witness/space/space-helper.mjs';
19
+ import { NodeFilesystemSpace_V1 } from './node-filesystem-space-v1.mjs';
20
+ import { B2TFS_DEFAULT_ENCODINGS_TO_TRY, FILE_ENCODINGS, FileDataInfo, FileEncoding } from './node-filesystem-space-types.mjs';
21
+
22
+
23
+ const logalot = GLOBAL_LOG_A_LOT;
24
+
25
+ /**
26
+ * tries to read the given path. if fails **FOR ANY REASON**, @returns `null`.
27
+ *
28
+ * does not throw
29
+ *
30
+ * @returns file contents as string or `null`
31
+ */
32
+ export async function tryRead({
33
+ path,
34
+ directory,
35
+ encoding,
36
+ }: {
37
+ path: string,
38
+ directory: string,
39
+ encoding: string,
40
+ }): Promise<string | Uint8Array | null> {
41
+ const lc = `[${tryRead.name}]`;
42
+ try {
43
+ if (logalot) {
44
+ console.log(`${lc} starting...`);
45
+ console.log(`${lc} cwd: ${cwd()}`);
46
+ if (path.includes('bootstrap^gib')) { console.log(`${lc} trying bootstrap^gib... (I: dacfdbe5a2d640ab947a7c17e3c56f78)`); }
47
+ }
48
+ const fullPath = pathUtils.join(directory, path);
49
+ if (logalot) { console.log(`${lc} fullPath: ${fullPath} (I: 459b416ac32c711bfbab54177839b323)`); }
50
+ encoding ||= 'utf8';
51
+ let resRead: string | undefined = undefined;
52
+ resRead = await readFile(fullPath, { encoding: encoding as BufferEncoding });
53
+
54
+ if (logalot) {
55
+ console.log(`${lc} record found. data length: ${resRead?.length ?? 0}. fullPath: ${fullPath}. console.dir(resRead)... (I: 82e3ad9821bd4bf8a54c8facc61dbad0)`);
56
+ console.dir(resRead)
57
+ }
58
+ return resRead;
59
+ } catch (error) {
60
+ if (logalot) {
61
+ console.log(`${lc} fullPath not found from directory (${directory}) and path (${path})\nerror:\n${extractErrorMsg(error)} (I: 1fde689d29aa47fcb589b3e7dac8929b)`);
62
+ }
63
+ return null;
64
+ } finally {
65
+ if (logalot) { console.log(`${lc} complete. (I: e2615c944a464cd48a5635fe401562d9)`); }
66
+ }
67
+ }
68
+
69
+ export async function tryRead_bin({
70
+ path,
71
+ directory,
72
+ encoding,
73
+ }: {
74
+ path: string,
75
+ directory: string,
76
+ encoding: string,
77
+ }): Promise<FileDataInfo | null> {
78
+ const lc = `[${tryRead_bin.name}]`;
79
+ try {
80
+ if (logalot) {
81
+ console.log(`${lc} starting...`);
82
+ console.log(`${lc} cwd: ${cwd()}`);
83
+ if (path.includes('bootstrap^gib')) { console.log(`${lc} trying bootstrap^gib... (I: 5eb010444b814addb1a6bbe18ca3dbfa)`); }
84
+ }
85
+ const fullPath = pathUtils.join(directory, path);
86
+ if (logalot) { console.log(`${lc} fullPath: ${fullPath} (I: c342bf56df354e9ab2653855a458b99d)`); }
87
+ encoding ||= 'utf8';
88
+ let resRead: string | Uint8Array | undefined = undefined;
89
+ let encodingsToTry: FileEncoding[] = unique<FileEncoding>([
90
+ encoding as FileEncoding,
91
+ ...FILE_ENCODINGS,
92
+ ]);
93
+ let { dataBuffer, dataString, inputPath, detectedEncoding } = await getFileDataAndEncoding({
94
+ inputPath: path,
95
+ encodingsToTry,
96
+ });
97
+ if (!dataBuffer) { throw new Error(`getFileDataAndEncoding yielded falsy dataBuffer (E: f7aac299212a1ae41d5bbef6079c4d24)`); }
98
+ resRead = dataBuffer;
99
+
100
+ if (logalot) {
101
+ console.log(`${lc} record found. data length: ${resRead?.length ?? 0}. fullPath: ${fullPath}. console.dir(resRead)... (I: 438189ba2fd345d782f928ecc7dc6e58)`);
102
+ console.dir(resRead)
103
+ }
104
+
105
+ return {
106
+ dataBuffer, dataString, inputPath, detectedEncoding
107
+ };
108
+ } catch (error) {
109
+ if (logalot) {
110
+ console.log(`${lc} fullPath not found from directory (${directory}) and path (${path})\nerror:\n${extractErrorMsg(error)} (I: 882e54f079024c94864bdaa74979b397)`);
111
+ }
112
+ return null;
113
+ } finally {
114
+ if (logalot) { console.log(`${lc} complete. (I: 500e391835aa4d959adf4720fdc38c4b)`); }
115
+ }
116
+ }
117
+
118
+ /**
119
+ * basic validation for a node fs space ibgib
120
+ */
121
+ export async function validateNodeFilesystemSpace_V1Intrinsically({ space }: { space: NodeFilesystemSpace_V1<any, any> }): Promise<string[] | null> {
122
+ const lc = `[${validateNodeFilesystemSpace_V1Intrinsically.name}]`;
123
+ try {
124
+ if (logalot) { console.log(`${lc} starting... (I: fcfe57145426ab4a5a0e961e87001922)`); }
125
+ const errors: string[] = (await validateIbGibIntrinsically({ ibGib: space })) ?? [];
126
+
127
+ const { ib, gib, data, rel8ns } = space;
128
+
129
+ if (!ib) { errors.push('ib required. (E: e69ddadd947d4534ae60af9eda1bda47)'); }
130
+ if (!gib) { errors.push('gib required. (E: 9794fb92dd79473b9139c001f0634ec2)'); }
131
+ if (gib === GIB) { errors.push('gib cannot be primitive. (E: 651c12f1303d45aba54aa228eca1e575)'); }
132
+
133
+ if (!data) {
134
+ errors.push('data required. (E: 264d13ce6b47400e8943bf046c57e8bc)');
135
+ return errors;
136
+ }
137
+
138
+ if (!data.name) { errors.push('space name required. (E: 4845041c9f2e44098e1e7bb30fe3c13d)') }
139
+ // if (!data.classname) { errors.push('classname required. (E: cb645bebf8fc4baabfbf74be39563008)') }
140
+ if (data.classname && data.classname !== NodeFilesystemSpace_V1.name) {
141
+ errors.push(`unknown classname (${data.classname}). data.classname !== FilesystemSpace_V1.name (E: e159eb46ca5e4c2b850998af88cb5840)`);
142
+ }
143
+ if (!data.baseDir) { errors.push(`data.baseDir required (E: 373544741a974ab9bc0001d3d63c2601).`) }
144
+ if (!data.baseSubPath) { errors.push(`data.baseSubPath required. (E: 7336f8eb7c7846d48993e0db893fb7cb)`) }
145
+ if (!data.binSubPath) { errors.push(`data.binSubPath required. (E: 3544a80a3cec4fdfb35d3e6ccb36dbd6)`) }
146
+ if (!data.dnaSubPath) { errors.push(`data.dnaSubPath required. (E: 4d6e529cdd0f47a492545c31e1dda865)`) }
147
+ if (!data.ibgibsSubPath) { errors.push(`data.ibgibsSubPath required. (E: 11b90fdc1b1c41b492c26a61da3409b8)`) }
148
+ if (!data.metaSubPath) { errors.push(`data.metaSubPath required. (E: 259113f4f0bc488785f9019160c8a226)`) }
149
+ if (data.n && typeof data.n !== 'number') { errors.push(`data.n must be a number. (E: e7c76f44b1114dba949d06146a8c169b)`) }
150
+ if (data.n === undefined) {
151
+ // the very first space record (tjp) has an undefined n and no rel8ns
152
+ if (rel8ns) { errors.push(`rel8ns not expected when data.n is falsy (custom temporal junction point indicator I suppose...) (E: 08604c8b33524009886ac2c63eb34c50)`); }
153
+ }
154
+ if (!data.spaceSubPath) { errors.push(`data.spaceSubPath required. (E: 1688a59dfc6447b3bccb7690b233e4fa)`) }
155
+ if (!data.uuid) { errors.push(`data.uuid required. (E: dd984a04d0d14d16a4faa300ec125960)`) }
156
+ if (!data.encoding) { errors.push(`data.encoding required. (E: e75958932f614364a1765a875acad2ce)`) }
157
+ /** should probably get this from Capacitor... */
158
+ const validEncodings = ["utf8", "ascii", "utf16"];
159
+ if (!validEncodings.includes(data.encoding)) {
160
+ errors.push(`invalid encoding: ${data.encoding}. validEncodings: ${validEncodings.join(', ')} (E: b88b909521db42cbae684c2937c77079)`);
161
+ }
162
+
163
+ // ensure ib matches up with internal data
164
+ const { spaceClassname, spaceId, spaceName } = parseSpaceIb({ spaceIb: ib });
165
+ if (data.classname && (spaceClassname !== data.classname)) {
166
+ errors.push(`ib's spaceClassname (${spaceClassname}) must match data.classname (${data.classname}) (E: 913b8b98717647fea832e4cca80d8ee6)`);
167
+ }
168
+ if (spaceId !== data.uuid) {
169
+ errors.push(`ib's spaceId (${spaceId}) must match data.uuid (${data.uuid}) (E: b20f36f32bc244008b4df856f892a62a)`);
170
+ }
171
+ if (spaceName !== data.name) {
172
+ errors.push(`ib's spaceName (${spaceName}) must match data.name (${data.name}) (E: cf599a9bf875464d9ec99c79cdee0452)`);
173
+ }
174
+
175
+ // ensure rel8ns make sense
176
+ if (data.n === undefined && (rel8ns?.past ?? []).length > 0) {
177
+ errors.push(`"past" rel8n not expected when data.n is falsy (E: 77bb7a90b40a4d989a9e53053776be86)`);
178
+ }
179
+ if (data.n && (rel8ns?.past ?? []).length === 0) {
180
+ errors.push(`"past" rel8n required when data.n is truthy (E: 735e07bac58b44deb2523e3d349d5479)`);
181
+ }
182
+ if (data.n === 0 && (rel8ns?.past ?? []).length !== 1) {
183
+ errors.push(`"past" rel8n expected to have a single record when data.n === 0 (E: 8915bb8084414d3b810cff9bccfc3ec6)`);
184
+ }
185
+ if (rel8ns && (rel8ns.past?.length ?? -1) > 0) {
186
+ const pastAddrs = rel8ns.past as IbGibAddr[];
187
+ pastAddrs.forEach(x => {
188
+ const { ib: pastIb } = getIbAndGib({ ibGibAddr: x });
189
+ const pastIbInfo = parseSpaceIb({ spaceIb: pastIb });
190
+ if (pastIbInfo.spaceClassname && (pastIbInfo.spaceClassname !== spaceClassname)) {
191
+ errors.push(`rel8ns.past address classname (${pastIbInfo.spaceClassname}) must match current spaceClassname (${spaceClassname}) (E: 10b2f05597574558ad2f20db8e471a44)`);
192
+ }
193
+ if (pastIbInfo.spaceId !== spaceId) {
194
+ errors.push(`rel8ns.past address spaceId (${pastIbInfo.spaceId}) must match current spaceId (${spaceId}) (E: 78b665a31fc34c68be3884d4cd4da35a)`);
195
+ }
196
+ // i want to allow this, but for now we're going to require not changing the name...
197
+ if (pastIbInfo.spaceName !== spaceName) {
198
+ errors.push(`rel8ns.past address spaceName (${pastIbInfo.spaceName}) must match current spaceName (${spaceName}) (E: 8a6621cbec9e48e7b6d880375c444bfe)`);
199
+ }
200
+ });
201
+ }
202
+
203
+ return errors;
204
+ } catch (error) {
205
+ console.error(`${lc} ${error.message}`);
206
+ throw error;
207
+ } finally {
208
+ if (logalot) { console.log(`${lc} complete.`); }
209
+ }
210
+ }
211
+
212
+ /**
213
+ * gets the data and encoding of a given {@link inputPath} if that file exists.
214
+ * if it doesn't exist, this will throw an ENOENT error, but this is not logged
215
+ * as an error.
216
+ *
217
+ * this is because atow (03/2024) this is only used in a tryRead_bin function
218
+ * where the file often does not exist.
219
+ */
220
+ export async function getFileDataAndEncoding({
221
+ inputPath,
222
+ encodingsToTry = B2TFS_DEFAULT_ENCODINGS_TO_TRY,
223
+ }: {
224
+ inputPath: string,
225
+ encodingsToTry?: (FileEncoding | undefined)[],
226
+ }): Promise<FileDataInfo> {
227
+ const lc = `[${getFileDataAndEncoding.name}]`;
228
+ try {
229
+ if (logalot) { console.log(`${lc} starting... (I: b4688e6e5f28efa9ed95729f10a6df24)`); }
230
+
231
+ let resDataAndEncoding: FileDataInfo | undefined = undefined;
232
+ const dataBuffer = await readFile(inputPath);
233
+
234
+ if (dataBuffer.length === 0) {
235
+ console.warn(`${lc} file at inputPath (${inputPath}) has dataBuffer.length === 0, i.e., no data content. we are treating this as utf8. (W: 2ea55367538146d695482cfb6ec1b79a)`)
236
+ return {
237
+ inputPath,
238
+ dataBuffer,
239
+ detectedEncoding: 'utf8',
240
+ dataString: '',
241
+ }; /* <<<< returns early */
242
+ }
243
+
244
+ encodingsToTry ??= B2TFS_DEFAULT_ENCODINGS_TO_TRY;
245
+
246
+ const tryEncoding = async (encoding: FileEncoding | undefined) => {
247
+ let res: [boolean, string | undefined];
248
+ try {
249
+ const str = dataBuffer.toString(encoding ?? undefined);
250
+ /**
251
+ * doesn't include the node.js replacement unicode char
252
+ */
253
+ const isProbablyCorrect = !str.includes('\ufffd');
254
+ res = [isProbablyCorrect, str];
255
+ return res;
256
+ } catch (error) {
257
+ debugger;
258
+ res = [false, undefined];
259
+ }
260
+ return res;
261
+ }
262
+
263
+ for (let i = 0; i < encodingsToTry.length; i++) {
264
+ const maybeEncoding = encodingsToTry[i];
265
+ const [isProbablyCorrectEncoding, dataString] = await tryEncoding(maybeEncoding);
266
+ if (isProbablyCorrectEncoding) {
267
+ resDataAndEncoding = {
268
+ inputPath,
269
+ dataString,
270
+ detectedEncoding: maybeEncoding,
271
+ dataBuffer,
272
+ };
273
+ break;
274
+ }
275
+ }
276
+
277
+ if (!resDataAndEncoding) {
278
+ debugger; // !resDataAndEncoding -> want to see why resDataAndEncoding is still undefined
279
+ throw new Error(`couldn't turn dataBuffer (length: ${dataBuffer.length}) into dataString with encodingsToTry: (${encodingsToTry}). no idea why. (E: 557c7722f2df7703429778662d3ea224)`);
280
+ // resDataAndEncoding = {
281
+ // inputPath,
282
+ // detectedEncoding: undefined,
283
+ // dataString: undefined,
284
+ // dataBuffer,
285
+ // }
286
+ }
287
+
288
+ return resDataAndEncoding;
289
+ } catch (error) {
290
+ // i'm just suppressing ENOENT at this time because in context, this is
291
+ // often the case. we do, however, rethrow the error for proper flow.
292
+ const emsg = extractErrorMsg(error);
293
+ if (!emsg.startsWith('ENOENT:')) { console.error(`${lc} ${emsg}`); }
294
+ throw error;
295
+ } finally {
296
+ if (logalot) { console.log(`${lc} complete.`); }
297
+ }
298
+ }
@@ -0,0 +1,150 @@
1
+ /**
2
+ * @module node-filesystem-space-types
3
+ *
4
+ * mostly interfaces, some constants
5
+ */
6
+
7
+ import { IbGibRel8ns_V1, } from '@ibgib/ts-gib/dist/V1/index.mjs';
8
+
9
+ import { DEFAULT_LOCAL_SPACE_POLLING_INTERVAL_MS, IBGIB_SPACE_NAME_DEFAULT, ZERO_SPACE_ID } from '@ibgib/core-gib/dist/witness/space/space-constants.mjs';
10
+ import {
11
+ IBGIB_BASE_SUBPATH, IBGIB_SPACE_SUBPATH_DEFAULT, IBGIB_BASE_DIR,
12
+ IBGIB_ENCODING, IBGIB_IBGIBS_SUBPATH, IBGIB_META_SUBPATH,
13
+ IBGIB_BIN_SUBPATH, IBGIB_DNA_SUBPATH,
14
+ IBGIB_LONG_SUBPATH,
15
+ DEFAULT_LONG_PATH_LENGTH,
16
+ DEFAULT_FILESYSTEM_SPACE_DESCRIPTION,
17
+ } from '@ibgib/core-gib/dist/witness/space/filesystem-space/filesystem-constants.mjs';
18
+ import {
19
+ FilesystemSpaceData_V1,
20
+ FilesystemSpaceOptionsData, FilesystemSpaceOptionsRel8ns, FilesystemSpaceOptionsIbGib,
21
+ FilesystemSpaceResultData, FilesystemSpaceResultRel8ns, FilesystemSpaceResultIbGib,
22
+ } from '@ibgib/core-gib/dist/witness/space/filesystem-space/filesystem-space-v1.mjs';
23
+
24
+
25
+ /**
26
+ * This is the shape of data about this space itself (not the contained ibgibs' spaces).
27
+ */
28
+ export interface NodeFilesystemSpaceData_V1 extends FilesystemSpaceData_V1 {
29
+ }
30
+
31
+ /**
32
+ * Used in bootstrapping.
33
+ *
34
+ * If you change this, please bump the version
35
+ *
36
+ * (but of course won't be the end of the world when this doesn't happen).
37
+ */
38
+ export const DEFAULT_NODE_FILESYSTEM_SPACE_DATA_V1: NodeFilesystemSpaceData_V1 = {
39
+ version: '2',
40
+ uuid: ZERO_SPACE_ID,
41
+ name: IBGIB_SPACE_NAME_DEFAULT,
42
+ classname: 'NodeFilesystemSpace_V1',
43
+ baseDir: IBGIB_BASE_DIR,
44
+ encoding: IBGIB_ENCODING,
45
+ baseSubPath: IBGIB_BASE_SUBPATH,
46
+ spaceSubPath: IBGIB_SPACE_SUBPATH_DEFAULT,
47
+ ibgibsSubPath: IBGIB_IBGIBS_SUBPATH,
48
+ metaSubPath: IBGIB_META_SUBPATH,
49
+ binSubPath: IBGIB_BIN_SUBPATH,
50
+ dnaSubPath: IBGIB_DNA_SUBPATH,
51
+ longSubPath: IBGIB_LONG_SUBPATH,
52
+ mitigateLongPaths: true,
53
+ longPathLength: DEFAULT_LONG_PATH_LENGTH,
54
+ persistOptsAndResultIbGibs: false,
55
+ validateIbGibAddrsMatchIbGibs: false,
56
+ longPollingIntervalMs: DEFAULT_LOCAL_SPACE_POLLING_INTERVAL_MS,
57
+ allowPrimitiveArgs: false,
58
+ catchAllErrors: true,
59
+ description: DEFAULT_FILESYSTEM_SPACE_DESCRIPTION,
60
+ trace: false,
61
+ }
62
+
63
+ /** Marker interface atm */
64
+ export interface NodeFilesystemSpaceRel8ns_V1 extends IbGibRel8ns_V1 { }
65
+
66
+ /**
67
+ * Space options involve whether we're getting/putting ibgibs categorized as
68
+ * meta, bin, dna.
69
+ *
70
+ * We'll leverage the fact that we don't need to get dna very often, and that
71
+ * meta ibgibs act differently and are recorded differently.
72
+ *
73
+ * For example, we don't necessarily want to keep the past of certain meta
74
+ * objects, because it may change (and thus grow) too quickly.
75
+ */
76
+ export interface NodeFilesystemSpaceOptionsData extends FilesystemSpaceOptionsData {
77
+ }
78
+
79
+ export interface NodeFilesystemSpaceOptionsRel8ns extends FilesystemSpaceOptionsRel8ns {
80
+ }
81
+
82
+ /** Marker interface atm */
83
+ export interface NodeFilesystemSpaceOptionsIbGib
84
+ extends FilesystemSpaceOptionsIbGib<NodeFilesystemSpaceOptionsData, NodeFilesystemSpaceOptionsRel8ns> {
85
+ }
86
+
87
+ /** Marker interface atm */
88
+ export interface NodeFilesystemSpaceResultData extends FilesystemSpaceResultData {
89
+ }
90
+
91
+ export interface NodeFilesystemSpaceResultRel8ns extends FilesystemSpaceResultRel8ns {
92
+ }
93
+
94
+ export interface NodeFilesystemSpaceResultIbGib
95
+ extends FilesystemSpaceResultIbGib<NodeFilesystemSpaceResultData, NodeFilesystemSpaceResultRel8ns> {
96
+ }
97
+ /**
98
+ * currently i'm just taking this from the definition of node's `Buffer` type.
99
+ *
100
+ * ## notes
101
+ *
102
+ * * using this with handle-reify-file.mts when reading the file.
103
+ * * 'binary' may resolve to 'latin1' per SO
104
+ * * https://stackoverflow.com/questions/46441667/reading-binary-data-in-node-js
105
+ */
106
+ export type FileEncoding =
107
+ | 'ascii'
108
+ | 'utf8'
109
+ | 'utf-8'
110
+ | 'utf16le'
111
+ | 'ucs2'
112
+ | 'ucs-2'
113
+ | 'base64'
114
+ | 'base64url'
115
+ | 'latin1'
116
+ | 'binary'
117
+ | 'hex';
118
+ export const FileEncoding = {
119
+ ascii: 'ascii' as FileEncoding,
120
+ utf8: 'utf8' as FileEncoding,
121
+ utf_8: 'utf-8' as FileEncoding,
122
+ ['utf-8']: 'utf-8' as FileEncoding,
123
+ utf16le: 'utf16le' as FileEncoding,
124
+ ucs2: 'ucs2' as FileEncoding,
125
+ ucs_2: 'ucs-2' as FileEncoding,
126
+ ['ucs-2']: 'ucs-2' as FileEncoding,
127
+ base64: 'base64' as FileEncoding,
128
+ base64url: 'base64url' as FileEncoding,
129
+ latin1: 'latin1' as FileEncoding,
130
+ binary: 'binary' as FileEncoding,
131
+ hex: 'hex' as FileEncoding,
132
+ };
133
+ /**
134
+ * valid file encoding values per {@link FileEncoding}
135
+ */
136
+ export const FILE_ENCODINGS = Object.values(FileEncoding);
137
+
138
+ export interface FileDataInfo {
139
+ inputPath: string;
140
+ detectedEncoding?: FileEncoding | undefined;
141
+ dataString?: string | undefined;
142
+ dataBuffer?: Uint8Array | undefined;
143
+ }
144
+ export const B2TFS_DEFAULT_ENCODINGS_TO_TRY: (FileEncoding | undefined)[] = [
145
+ FileEncoding.utf8,
146
+ FileEncoding.utf16le,
147
+ FileEncoding.base64,
148
+ FileEncoding.binary,
149
+ undefined,
150
+ ];