@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,672 @@
1
+ import { existsSync, mkdirSync } from 'node:fs';
2
+ import { writeFile, rm, readdir } from 'node:fs/promises';
3
+ import { default as pathUtils } from 'path';
4
+ import { clone, extractErrorMsg, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
5
+ import { getIbAndGib, getIbGibAddr, } from '@ibgib/ts-gib/dist/helper.mjs';
6
+ import { getGib, getGibInfo, } from '@ibgib/ts-gib/dist/V1/transforms/transform-helper.mjs';
7
+ import { validateIbGibAddr } from '@ibgib/ts-gib/dist/V1/validate-helper.mjs';
8
+ import { IBGIB_DELIMITER } from '@ibgib/ts-gib/dist/V1/constants.mjs';
9
+ import { GLOBAL_LOG_A_LOT } from '@ibgib/core-gib/dist/core-constants.mjs';
10
+ import { isBinary, parseBinIb, toDto, } from '@ibgib/core-gib/dist/common/other/ibgib-helper.mjs';
11
+ import { getSpaceIb, } from '@ibgib/core-gib/dist/witness/space/space-helper.mjs';
12
+ import { IBGIB_BASE_SUBPATH, IBGIB_SPACE_SUBPATH_DEFAULT, IBGIB_BASE_DIR, IBGIB_ENCODING, IBGIB_IBGIBS_SUBPATH, IBGIB_META_SUBPATH, IBGIB_BIN_SUBPATH, IBGIB_DNA_SUBPATH, DEFAULT_LONG_PATH_LENGTH, IBGIB_LONG_SUBPATH, } from '@ibgib/core-gib/dist/witness/space/filesystem-space/filesystem-constants.mjs';
13
+ import { FilesystemSpace_V1, } from '@ibgib/core-gib/dist/witness/space/filesystem-space/filesystem-space-v1.mjs';
14
+ import { tryRead, tryRead_bin } from './node-filesystem-space-helper.mjs';
15
+ import { DEFAULT_NODE_FILESYSTEM_SPACE_DATA_V1, } from './node-filesystem-space-types.mjs';
16
+ import { META_STONE_ATOM } from '@ibgib/core-gib/dist/common/meta-stone/meta-stone-constants.mjs';
17
+ import { isMetaStone, validateCommonMetaStoneIb } from '@ibgib/core-gib/dist/common/meta-stone/meta-stone-helper.mjs';
18
+ const logalot = GLOBAL_LOG_A_LOT;
19
+ /**
20
+ * Base class convenience for a local space with V1 ibgibs working with the node
21
+ * filesystem.
22
+ *
23
+ * Unfortunately, file systems have short file name requirements, where 255 is
24
+ * often the max length of a filename. So this cannot store ibgibs directly by
25
+ * their address.
26
+ *
27
+ * This naively caches ibGibs in memory. When not found there, will looks in
28
+ * files using Ionic `FileSystem`.
29
+ */
30
+ export class NodeFilesystemSpace_V1 extends FilesystemSpace_V1 {
31
+ /**
32
+ * Log context for convenience with logging. (Ignore if you don't want to use this.)
33
+ */
34
+ lc = `[${NodeFilesystemSpace_V1.name}]`;
35
+ /**
36
+ * in case the caller tries to override the classname. This should be overridden in any descendant classes
37
+ */
38
+ get classname() { return NodeFilesystemSpace_V1.name; }
39
+ constructor(
40
+ // /**
41
+ // * Default predicate value when putting an unknown ibGib.
42
+ // *
43
+ // * ## notes
44
+ // *
45
+ // * So when a repo witnesses another ibGib, it either defaults to
46
+ // * storing that ibGib or not storing that ibGib. This is what that
47
+ // * is referring to. If it's optimistic, then it stores any ibGib by
48
+ // * default and it passes its put predicate.
49
+ // */
50
+ // public optimisticPut: boolean = true,
51
+ initialData, initialRel8ns) {
52
+ super(initialData ?? clone(DEFAULT_NODE_FILESYSTEM_SPACE_DATA_V1), initialRel8ns);
53
+ const lc = `${this.lc}[ctor]`;
54
+ // try {
55
+ // if (logalot) { console.log(`${lc} starting...`); }
56
+ // this.initialize();
57
+ // } catch (error) {
58
+ // console.error(`${lc} ${extractErrorMsg(error)}`);
59
+ // throw error;
60
+ // } finally {
61
+ // if (logalot) { console.log(`${lc} complete.`); }
62
+ // }
63
+ }
64
+ /**
65
+ * Factory static method to create the space with the given
66
+ * `dto` param's ibGib properties.
67
+ *
68
+ * We do this because when we persist this space (and its settings
69
+ * located in `data`), we do not save the actual class instantiation
70
+ * but just the ibgib properties. Use this factory method to
71
+ * create a new space instance and rehydrate from that saved dto.
72
+ *
73
+ * ## notes
74
+ *
75
+ * * DTO stands for data transfer object.
76
+ *
77
+ * @param dto space ibGib dto that we're going to load from
78
+ * @returns newly created space built upon `dto`
79
+ */
80
+ static async createFromDto(dto) {
81
+ const lc = `[${FilesystemSpace_V1.name}][${this.createFromDto.name}]`;
82
+ if (logalot) {
83
+ console.log(`${lc}`);
84
+ }
85
+ const space = new NodeFilesystemSpace_V1();
86
+ await space.initialized;
87
+ await space.loadIbGibDto(dto);
88
+ return space;
89
+ }
90
+ async validateWitnessArg(arg) {
91
+ const lc = `${this.lc}[${this.validateWitnessArg.name}]`;
92
+ let errors = [];
93
+ try {
94
+ errors = (await super.validateWitnessArg(arg)) || [];
95
+ if (arg.data?.cmd === 'put' && (arg.ibGibs || []).length === 0) {
96
+ errors.push(`when "put" cmd is called, ibGibs required.`);
97
+ }
98
+ if (arg.data?.cmd === 'get' && (arg.data?.ibGibAddrs || []).length === 0) {
99
+ errors.push(`when "get" cmd is called, ibGibAddrs required.`);
100
+ }
101
+ }
102
+ catch (error) {
103
+ console.error(`${lc} ${extractErrorMsg(error)}`);
104
+ throw error;
105
+ }
106
+ finally {
107
+ if (errors?.length > 0) {
108
+ console.error(`${lc} errors: ${errors}`);
109
+ }
110
+ }
111
+ return errors;
112
+ }
113
+ /**
114
+ * Initializes to default space values.
115
+ */
116
+ async initialize() {
117
+ const lc = `${this.lc}[${this.initialize.name}]`;
118
+ try {
119
+ if (logalot) {
120
+ console.log(`${lc} starting...`);
121
+ }
122
+ if (!this.data) {
123
+ this.data = clone(DEFAULT_NODE_FILESYSTEM_SPACE_DATA_V1);
124
+ this.data = this.data; // why does ts compiler need this?
125
+ }
126
+ // if (!this.data?.classname) { this.data!.classname = NodeFilesystemSpace_V1.name }
127
+ if (this.data.classname !== this.classname) {
128
+ if (this.data.classname) {
129
+ // only warn if the consumer has explicitly set a different classname
130
+ debugger; // how are we hitting this in space-gib server init
131
+ console.warn(`${lc} initializer tried to init this data with this.data.classname (${this.data.classname}) !== ${this.classname}. overriding this to be ${this.classname}. (W: 0f53162c43a440c2a790c19dd223c6b6)`);
132
+ }
133
+ this.data.classname = this.classname; // always set?
134
+ }
135
+ if (!this.data.baseDir) {
136
+ this.data.baseDir = IBGIB_BASE_DIR;
137
+ }
138
+ if (!this.data.encoding) {
139
+ this.data.encoding = IBGIB_ENCODING;
140
+ }
141
+ if (!this.data.baseSubPath) {
142
+ this.data.baseSubPath = IBGIB_BASE_SUBPATH;
143
+ }
144
+ if (!this.data.spaceSubPath) {
145
+ this.data.spaceSubPath = IBGIB_SPACE_SUBPATH_DEFAULT;
146
+ }
147
+ if (!this.data.ibgibsSubPath) {
148
+ this.data.ibgibsSubPath = IBGIB_IBGIBS_SUBPATH;
149
+ }
150
+ if (!this.data.metaSubPath) {
151
+ this.data.metaSubPath = IBGIB_META_SUBPATH;
152
+ }
153
+ if (!this.data.binSubPath) {
154
+ this.data.binSubPath = IBGIB_BIN_SUBPATH;
155
+ }
156
+ if (!this.data.dnaSubPath) {
157
+ this.data.dnaSubPath = IBGIB_DNA_SUBPATH;
158
+ }
159
+ // do nothing, allow falsy
160
+ if (this.data.longSubPath === undefined) {
161
+ this.data.longSubPath = IBGIB_LONG_SUBPATH;
162
+ }
163
+ if (this.data.longPathLength === undefined) {
164
+ this.data.longPathLength = DEFAULT_LONG_PATH_LENGTH;
165
+ }
166
+ if (this.data.mitigateLongPaths === undefined) {
167
+ this.data.mitigateLongPaths = true;
168
+ }
169
+ this.ib = getSpaceIb({ space: this, classname: this.data.classname });
170
+ this.gib = await getGib({ ibGib: this });
171
+ }
172
+ catch (error) {
173
+ console.error(`${lc} ${extractErrorMsg(error)}`);
174
+ }
175
+ finally {
176
+ if (logalot) {
177
+ console.log(`${lc} complete.`);
178
+ }
179
+ }
180
+ }
181
+ async putFile({ ibGib, isDna, }) {
182
+ const lc = `${this.lc}[${this.putFile.name}]`;
183
+ let result = {};
184
+ try {
185
+ if (!ibGib) {
186
+ throw new Error(`ibGib required. (E: b019eedc22094687a83dca8f7a98ba35)`);
187
+ }
188
+ ;
189
+ const thisData = this.data;
190
+ // await this.ensureAllDirsExist();
191
+ let path = "";
192
+ let data = "";
193
+ const addr = getIbGibAddr({ ibGib });
194
+ const isBin = isBinary({ addr });
195
+ path = await this.buildPath({
196
+ addr, isDna: isDna ?? false, isBin, ensureMetaStonePaths: true,
197
+ });
198
+ await this.ensureDirsImpl([pathUtils.dirname(path)]);
199
+ let fullPath = pathUtils.join(thisData.baseDir, path);
200
+ if (logalot) {
201
+ console.log(`${lc} fullPath: ${fullPath} (I: 2441a896f7e6418db57c0341d5c65ef2)`);
202
+ }
203
+ if (logalot) {
204
+ console.log(`${lc} path: ${path}, directory: ${thisData.baseDir}, thisData.encoding: ${thisData.encoding} (I: d4440ececbe6c859f8fcf6d7ece12522)`);
205
+ }
206
+ let encoding;
207
+ if (!isBin) {
208
+ // not binary - most ibgibs will not be bins.
209
+ // we only want to persist the ibGib protocol properties (not
210
+ // any functions or other properties that might exist on the
211
+ // incoming ibGib arg)
212
+ const bareIbGib = toDto({ ibGib });
213
+ data = JSON.stringify(bareIbGib);
214
+ encoding = thisData.encoding || 'utf8';
215
+ await writeFile(fullPath, data, encoding);
216
+ }
217
+ else {
218
+ // binary
219
+ const binIbGib = ibGib;
220
+ data = binIbGib.data;
221
+ if (!data) {
222
+ throw new Error(`(UNEXPECTED) binIbGib.data falsy? I'm trying to have data always populated for bin ibgibs even if it is an empty Uint8Array. (E: 8c9c3650ec97792b1913754ed0877824)`);
223
+ }
224
+ // if (!Buffer.isBuffer(data)) {
225
+ // debugger; // checking to see if !Buffer.isBuffer(data) fails for Uint8Array
226
+ // throw new Error(`(UNEXPECTED) !Buffer.isBuffer(data)? I'm trying to have binIbGib.data always populated and it should be a Buffer. (E: 179c97da10ac082b77376c0db9077d24)`);
227
+ // }
228
+ // encoding = parseBinIb({ addr }).binEncoding ?? undefined
229
+ // write bins directly as buffers
230
+ // const dataBuffer = Buffer.from(data);
231
+ await writeFile(fullPath, data);
232
+ }
233
+ result.success = true;
234
+ }
235
+ catch (error) {
236
+ const errorMsg = `${lc} ${extractErrorMsg(error)}`;
237
+ console.error(errorMsg);
238
+ result.errorMsg = errorMsg;
239
+ }
240
+ return result;
241
+ }
242
+ async deleteFile({ addr, isDna, }) {
243
+ const lc = `${this.lc}[${this.deleteFile.name}]`;
244
+ const result = {};
245
+ try {
246
+ if (!addr) {
247
+ throw new Error(`addr required. (E: 98ff4fb4067a4a8281e21756aca4bf82)`);
248
+ }
249
+ ;
250
+ if (!this.data) {
251
+ throw new Error(`this.data required (E: 27e1f5206d96d635e617f5ba69141423)`);
252
+ }
253
+ const data = this.data;
254
+ let path = "";
255
+ if (!isBinary({ addr })) {
256
+ // regular ibGib
257
+ path = await this.buildPath({
258
+ addr, isDna: isDna ?? false, ensureMetaStonePaths: true,
259
+ });
260
+ }
261
+ else {
262
+ path = await this.buildPath({
263
+ addr, isDna: false, isBin: true, ensureMetaStonePaths: false,
264
+ });
265
+ }
266
+ if (logalot) {
267
+ console.log(`${lc} path: ${path}, directory: ${data.baseDir}`);
268
+ }
269
+ const fullPath = pathUtils.join(this.data.baseDir, path);
270
+ await rm(fullPath, { force: true });
271
+ if (logalot) {
272
+ console.log(`${lc} deleted. path: ${path}`);
273
+ }
274
+ result.success = true;
275
+ }
276
+ catch (error) {
277
+ const errorMsg = `${lc} ${extractErrorMsg(error)}`;
278
+ if (!errorMsg.includes('File does not exist')) {
279
+ console.error(errorMsg);
280
+ }
281
+ else {
282
+ if (logalot) {
283
+ console.log(`${lc} attempted to delete non-existent file. ${errorMsg} (I: 8953b51a5f14960e5ea2e86f6c6a7622)`);
284
+ }
285
+ }
286
+ result.errorMsg = errorMsg;
287
+ }
288
+ return result;
289
+ }
290
+ async getFile({ addr, isDna, }) {
291
+ let lc = `${this.lc}[${this.getFile.name}(${addr})]`;
292
+ const result = {};
293
+ try {
294
+ if (logalot) {
295
+ console.log(`${lc} starting... (I: f69b58063ff7cafaaab341e633002a23)`);
296
+ }
297
+ if (!addr) {
298
+ throw new Error(`addr required`);
299
+ }
300
+ ;
301
+ const data = this.data;
302
+ const { ib } = getIbAndGib({ ibGibAddr: addr });
303
+ const addrIsBin = isBinary({ addr });
304
+ const knownTransformIbs = ['fork', 'mut8', 'rel8', 'plan']; // hack/kluge here
305
+ const addrMightBeDna = knownTransformIbs.some(x => x === ib || x.startsWith(ib + ' '));
306
+ const pathsToLook = [];
307
+ if (isDna || addrMightBeDna) {
308
+ pathsToLook.push(await this.buildPath({
309
+ addr, isDna: true, ensureMetaStonePaths: false
310
+ }));
311
+ }
312
+ else if (addrIsBin) {
313
+ pathsToLook.push(await this.buildPath({
314
+ addr, isDna: false, isBin: true, ensureMetaStonePaths: false
315
+ }));
316
+ }
317
+ // always look in the main location as a last resort
318
+ pathsToLook.push(await this.buildPath({
319
+ addr, isDna: false, ensureMetaStonePaths: false
320
+ }));
321
+ // ...(pretend shortened as well as an absolute last last resort)
322
+ if (!isMetaStone({ addr })) {
323
+ pathsToLook.push(await this.buildPath({
324
+ addr, isDna: false, ensureMetaStonePaths: false,
325
+ pretendItsALongPath: true,
326
+ }));
327
+ }
328
+ // this is ugly atow (02/2024) because i'm struggling a bit of
329
+ // changing bin ibgibs to actually support binaries. i apologize.
330
+ if (!addrIsBin) {
331
+ // non-bin ibGib(s) retrieved (most ibgibs are non-bin)
332
+ let resRead = null;
333
+ for (const tryPath of pathsToLook) {
334
+ const x = await tryRead({ path: tryPath, directory: data.baseDir, encoding: data.encoding });
335
+ if (x) {
336
+ resRead = x;
337
+ break;
338
+ }
339
+ }
340
+ if (resRead) {
341
+ result.ibGib = JSON.parse(resRead);
342
+ }
343
+ else {
344
+ if (logalot) {
345
+ console.log(`${lc} paths not found: ${JSON.stringify(pathsToLook)} (I: 7fc6c550b1a047d8a14392185de1b06e)`);
346
+ }
347
+ // will return success since it's not really an error, but ibgib
348
+ // will not be populated, indicating the addr was not found.
349
+ }
350
+ }
351
+ else {
352
+ // bin
353
+ let resRead = null;
354
+ let { binEncoding, binHash } = parseBinIb({ addr });
355
+ if (binHash === '0') {
356
+ // special case: if the binHash is 0, then it is an empty file.
357
+ const { ib, gib } = getIbAndGib({ ibGibAddr: addr });
358
+ result.ibGib = {
359
+ ib,
360
+ gib,
361
+ data: new Uint8Array(0),
362
+ // data: '',
363
+ // rawData: Buffer.from([]),
364
+ };
365
+ }
366
+ else {
367
+ for (const tryPath of pathsToLook) {
368
+ const x = await tryRead_bin({
369
+ path: tryPath, directory: data.baseDir, encoding: binEncoding ?? data.encoding,
370
+ });
371
+ if (x) {
372
+ resRead = x;
373
+ break;
374
+ }
375
+ }
376
+ if (resRead) {
377
+ // all bins are buffers
378
+ const { ib, gib } = getIbAndGib({ ibGibAddr: addr });
379
+ const binIbGib = {
380
+ ib, gib, data: resRead.dataBuffer,
381
+ // rawData: resRead.dataBuffer,
382
+ };
383
+ result.ibGib = binIbGib;
384
+ }
385
+ else {
386
+ if (logalot) {
387
+ console.log(`${lc} paths not found: ${JSON.stringify(pathsToLook)} (I: 6a3bd3b125619da7a944200b14e7e922)`);
388
+ }
389
+ // will return success since it's not really an error, but ibgib
390
+ // will not be populated, indicating the addr was not found.
391
+ }
392
+ }
393
+ }
394
+ result.success = true;
395
+ }
396
+ catch (error) {
397
+ const errorMsg = `${lc} ${extractErrorMsg(error)}`;
398
+ console.error(errorMsg);
399
+ result.errorMsg = errorMsg;
400
+ }
401
+ finally {
402
+ if (logalot) {
403
+ console.log(`${lc} complete. (I: 413374e48f0a3cb8fd25de0d82134123)`);
404
+ }
405
+ }
406
+ return result;
407
+ }
408
+ async ensurePermissions() {
409
+ const lc = `${this.lc}[${this.ensurePermissions.name}]`;
410
+ if (logalot) {
411
+ console.log(`${lc} always returns true in base class (I: 0a036a097093c622167e40f81e03d923)`);
412
+ }
413
+ return true;
414
+ }
415
+ async ensureAllDirsExist() {
416
+ const lc = `${this.lc}[${this.ensureAllDirsExist.name}]`;
417
+ try {
418
+ if (logalot) {
419
+ console.log(`${lc} starting... (I: f9f1bc18215fd591cf9dcaea4deaa323)`);
420
+ }
421
+ if (!this.data) {
422
+ throw new Error(`this.data required (E: ccf51541c1d5714ddafb67a8c3289823)`);
423
+ }
424
+ if (!this.data.uuid) {
425
+ throw new Error(`this.data.uuid required (E: 33691c4c97e2033bf13d2c2d26a8f823)`);
426
+ }
427
+ const data = this.data;
428
+ /** these are the paths we're ensuring exist. all ibgibs are stored here. */
429
+ const paths = [
430
+ data.baseSubPath, // = 'ibgib';
431
+ data.baseSubPath + '/' + data.spaceSubPath,
432
+ data.baseSubPath + '/' + data.spaceSubPath + '/' + data.ibgibsSubPath,
433
+ data.baseSubPath + '/' + data.spaceSubPath + '/' + data.metaSubPath,
434
+ data.baseSubPath + '/' + data.spaceSubPath + '/' + data.binSubPath,
435
+ data.baseSubPath + '/' + data.spaceSubPath + '/' + data.dnaSubPath,
436
+ ];
437
+ if (data.mitigateLongPaths) {
438
+ paths.push(data.baseSubPath + '/' + data.spaceSubPath + '/' + data.longSubPath);
439
+ }
440
+ await this.ensureDirsImpl(paths);
441
+ }
442
+ catch (error) {
443
+ console.error(`${lc} ${extractErrorMsg(error)}`);
444
+ throw error;
445
+ }
446
+ finally {
447
+ if (logalot) {
448
+ console.log(`${lc} complete.`);
449
+ }
450
+ }
451
+ }
452
+ /**
453
+ * actually executes the ensure functionality for given `paths`. If they
454
+ * don't exist, then this tries to make the dirs.
455
+ * @param paths to ensure exist.
456
+ */
457
+ async ensureDirsImpl(paths) {
458
+ const lc = `${this.lc}[${this.ensureDirsImpl.name}]`;
459
+ try {
460
+ if (logalot) {
461
+ console.log(`${lc} starting... (I: ad355a80dafa5873ac8ef31bf1f27923)`);
462
+ }
463
+ if (paths.length === 0) {
464
+ console.warn(`${lc} paths empty? returning early. (W: a089ab1c9c0a4275a49341fb9f4c01c1)`);
465
+ return; /* <<<< returns early */
466
+ }
467
+ const directory = this.data.baseDir;
468
+ const getPathKey = (p) => { return directory.toString() + '/' + p; };
469
+ let allExist = paths.every(p => this.pathExistsMap[getPathKey(p)]);
470
+ if (allExist) {
471
+ if (logalot) {
472
+ console.log(`${lc} allExist (I: f14ad6db1d29e368c37c3117fee1cb22)`);
473
+ }
474
+ return; /* <<<< returns early */
475
+ }
476
+ const permitted = await this.ensurePermissions();
477
+ if (!permitted) {
478
+ console.error(`${lc} permission not granted.`);
479
+ return; /* <<<< returns early */
480
+ }
481
+ for (let i = 0; i < paths.length; i++) {
482
+ const path = paths[i];
483
+ const lc2 = `${lc}[(path: ${path}, directory: ${directory})]`;
484
+ const fullPath = pathUtils.join(directory, path);
485
+ // check if we've already ensured for this path
486
+ const pathExistsKey = getPathKey(path);
487
+ let exists = this.pathExistsMap[pathExistsKey] || false;
488
+ if (!exists) {
489
+ // we've not checked this path (or it didn't exist)
490
+ try {
491
+ exists = existsSync(fullPath);
492
+ this.pathExistsMap[pathExistsKey] = exists;
493
+ }
494
+ catch (error) {
495
+ if (logalot) {
496
+ console.log(`${lc2} Did not exist`);
497
+ }
498
+ }
499
+ }
500
+ if (!exists) {
501
+ // try full path
502
+ if (logalot) {
503
+ console.log(`${lc2} creating...`);
504
+ }
505
+ try {
506
+ mkdirSync(fullPath, { recursive: true });
507
+ exists = existsSync(fullPath);
508
+ if (logalot) {
509
+ console.log(`${lc} exists: ${exists}`);
510
+ }
511
+ this.pathExistsMap[pathExistsKey] = exists;
512
+ }
513
+ catch (error) {
514
+ if (logalot) {
515
+ console.log(`${lc2} Error creating. Trying next`);
516
+ }
517
+ }
518
+ finally {
519
+ if (logalot) {
520
+ console.log(`${lc2} complete.`);
521
+ }
522
+ }
523
+ }
524
+ }
525
+ }
526
+ catch (error) {
527
+ console.error(`${lc} ${extractErrorMsg(error)}`);
528
+ throw error;
529
+ }
530
+ finally {
531
+ if (logalot) {
532
+ console.log(`${lc} complete.`);
533
+ }
534
+ }
535
+ }
536
+ /** */
537
+ async getMetaStoneAddrs({ ibGibAddr, tjpGib, fnFilterIb, }) {
538
+ const lc = `${this.lc}[${this.getMetaStoneAddrs.name}]`;
539
+ try {
540
+ if (logalot) {
541
+ console.log(`${lc} starting... (I: 510ba658195aa0aed9b8836f616d7823)`);
542
+ }
543
+ // first build the path with the given tjpGib
544
+ // debugger;
545
+ let pathSansBaseDir = await this.buildPath({
546
+ addr: ibGibAddr, ensureMetaStonePaths: false, isDna: false,
547
+ addrIsForAMetaStone: true,
548
+ });
549
+ let fullPath = pathUtils.join(this.data.baseDir, pathSansBaseDir);
550
+ if (logalot) {
551
+ console.log(`${lc} fullPath: ${fullPath} (I: 9fc7113d6c9649f9b25f227ddad57f69)`);
552
+ }
553
+ // the given ibgib's containing dir is the one that should contain
554
+ // the metastones.
555
+ let gibInfo = getGibInfo({ ibGibAddr });
556
+ let containingDir = !!gibInfo.tjpGib ?
557
+ pathUtils.dirname(pathUtils.join(fullPath, '..')) :
558
+ pathUtils.dirname(fullPath);
559
+ if (logalot) {
560
+ console.log(`${lc} containingDir: ${containingDir} (I: 2780437a6778a4633f49d81ac26ab723)`);
561
+ }
562
+ // iterate through the files for metastones.
563
+ const fnFilter = fnFilterIb ?
564
+ (s) => s.startsWith(META_STONE_ATOM) && fnFilterIb(s) :
565
+ (s) => s.startsWith(META_STONE_ATOM);
566
+ let filenames = [];
567
+ try {
568
+ if (logalot) {
569
+ console.log(`${lc} calling readdir(containingDir) (I: 682d129a44d1101a0a7c2be999921923)`);
570
+ }
571
+ filenames = await readdir(containingDir, {
572
+ recursive: false,
573
+ withFileTypes: false,
574
+ });
575
+ }
576
+ catch (error) {
577
+ let emsg = extractErrorMsg(error);
578
+ if (!emsg.includes('ENOENT')) { // no such file or directory
579
+ console.warn(`${lc} readdir error did not contain ENOENT...not sure what this is...maybe a different OS no dir exists emsg? Treating this as if dir does not exist. (W: cde1cab3f64c47559782da8da55ed48a)`);
580
+ }
581
+ else {
582
+ if (logalot) {
583
+ console.log(`${lc} readdir errors: ${extractErrorMsg(error)} (I: 62bd4ec398b86ca84f7b3866dd83c123)`);
584
+ }
585
+ }
586
+ filenames = [];
587
+ }
588
+ if (filenames.length === 0) {
589
+ if (logalot) {
590
+ console.log(`${lc} filenames: [empty]. no metastones found. (I: da9fef3f2edde037ca1c340bc38b7723)`);
591
+ }
592
+ return []; /* <<<< returns early */
593
+ }
594
+ if (logalot) {
595
+ console.log(`${lc} filenames: ${filenames} (I: cb2948540a031522d184b1611f914323)`);
596
+ }
597
+ /**
598
+ * atow (11/2023) extensions are hard-coded to ".json",
599
+ */
600
+ const dotExt = '.json';
601
+ const dotExtLength = 5;
602
+ const metaStoneAddrs = filenames
603
+ .filter(x => {
604
+ // some filenames may be adjusted and not the full ibs.
605
+ // however, the metastones are never shortened (atow
606
+ // 11/2023), so those that don't have the delimiter will be
607
+ // stored in ibIsh but they should get filtered out.
608
+ const [ibIsh, gibPlusExt] = x.split(IBGIB_DELIMITER);
609
+ return !!gibPlusExt?.endsWith(dotExt) && fnFilter(ibIsh);
610
+ }).map(addrPlusExt => {
611
+ // strip the extension
612
+ return addrPlusExt.substring(0, addrPlusExt.length - dotExtLength);
613
+ }).filter(metaStoneAddr => {
614
+ // atow (11/2023) I'm not sure what other metastones are
615
+ // going to be in this dir, so I'm saying it has to
616
+ // explicitly contain the tjpGib.
617
+ return isMetaStone({ addr: metaStoneAddr }) && metaStoneAddr.includes(tjpGib);
618
+ });
619
+ if (logalot) {
620
+ console.log(`${lc} filtered/mapped filenames -> metaStoneAddrs: ${metaStoneAddrs} (I: ed062833a65730c04c7f9523e31a5a23)`);
621
+ }
622
+ // do some basic validation
623
+ // throw (which could cripple in the future...), or silent fail
624
+ // (which could lead to corrupt data)... hmm...going to throw for
625
+ // now.
626
+ for (let i = 0; i < metaStoneAddrs.length; i++) {
627
+ const metaStoneAddr = metaStoneAddrs[i];
628
+ let basicErrors = validateIbGibAddr({ addr: metaStoneAddr });
629
+ if ((basicErrors ?? []).length > 0) {
630
+ throw new Error(`(UNEXPECTED) invalid metastone found? metaStoneAddr (${metaStoneAddr}) had basic validation errors: ${basicErrors.join('|')} (E: 318c164fc2014c036c91b179c8d7f623)`);
631
+ }
632
+ const { ib } = getIbAndGib({ ibGibAddr: metaStoneAddr });
633
+ let metaStoneErrors = validateCommonMetaStoneIb({ ib });
634
+ if (metaStoneErrors.length > 0) {
635
+ throw new Error(`(UNEXPECTED) metaStoneIb had validation errors? metaStoneErrors: ${metaStoneErrors.join('|')} (E: 5a07b4dcd817f72a1a70e4b2d18f7323)`);
636
+ }
637
+ }
638
+ // all good, return 'em!
639
+ if (logalot) {
640
+ console.log(`${lc} returning metaStoneAddrs: ${metaStoneAddrs} (I: c235e1005d02a53219e032f8818e8223)`);
641
+ }
642
+ return metaStoneAddrs;
643
+ }
644
+ catch (error) {
645
+ console.error(`${lc} ${extractErrorMsg(error)}`);
646
+ throw error;
647
+ }
648
+ finally {
649
+ if (logalot) {
650
+ console.log(`${lc} complete.`);
651
+ }
652
+ }
653
+ }
654
+ }
655
+ export async function convertData({ data, encoding, }) {
656
+ const lc = `[${convertData.name}]`;
657
+ try {
658
+ if (logalot) {
659
+ console.log(`${lc} starting... (I: 77ef782249fdc9588c5b5b810126cc24)`);
660
+ }
661
+ }
662
+ catch (error) {
663
+ console.error(`${lc} ${extractErrorMsg(error)}`);
664
+ throw error;
665
+ }
666
+ finally {
667
+ if (logalot) {
668
+ console.log(`${lc} complete.`);
669
+ }
670
+ }
671
+ }
672
+ //# sourceMappingURL=node-filesystem-space-v1.mjs.map