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