holosphere 1.3.0-alpha0 → 1.3.0-alpha4

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.
package/global.js CHANGED
@@ -65,11 +65,15 @@ export async function putGlobal(holoInstance, tableName, data, password = null)
65
65
 
66
66
  return new Promise((resolve, reject) => {
67
67
  try {
68
- // Create a copy of data without the _meta field if it exists
68
+ // Create a copy of data, stripping read-side envelopes that
69
+ // must never be persisted (they're attached at resolution time).
69
70
  let dataToStore = { ...data };
70
71
  if (dataToStore._meta !== undefined) {
71
72
  delete dataToStore._meta;
72
73
  }
74
+ if (dataToStore._hologram !== undefined) {
75
+ delete dataToStore._hologram;
76
+ }
73
77
  const payload = JSON.stringify(dataToStore);
74
78
 
75
79
  // Check if the data being stored is a hologram
@@ -327,8 +331,12 @@ export async function getAllGlobal(holoInstance, tableName, password = null) {
327
331
  user.get('private').get(tableName) :
328
332
  holoInstance.gun.get(holoInstance.appname).get(tableName);
329
333
 
330
- // PASS 1: Get shallow node to determine expected item count
331
- dataPath.once((data) => {
334
+ // PASS 1: Get shallow node to determine expected item count.
335
+ // Retry once if empty — Gun's .once() reads from local cache, which
336
+ // may be cold immediately after startup before peers have synced.
337
+ const shallowOnce = () => new Promise((res) => dataPath.once((d) => res(d)));
338
+
339
+ const processShallow = (data) => {
332
340
  if (!data) {
333
341
  resolve([]);
334
342
  return;
@@ -350,60 +358,63 @@ export async function getAllGlobal(holoInstance, tableName, password = null) {
350
358
  return;
351
359
  }
352
360
 
353
- // PASS 2: Use map().once() to iterate and get full item data
354
- let receivedCount = 0;
355
-
356
- dataPath.map().once(async (itemData, key) => {
357
- if (!itemData || key === '_') {
358
- receivedCount++;
359
- if (receivedCount >= expectedCount) {
360
- await Promise.all(pendingProcessing);
361
- resolve(output);
362
- }
363
- return;
364
- }
365
-
366
- const processingPromise = (async () => {
367
- try {
368
- const parsed = await holoInstance.parse(itemData);
369
- if (!parsed) return;
361
+ // PASS 2: iterate explicitly over the filtered keys.
362
+ // Avoid dataPath.map().once() — it fires for null tombstone
363
+ // siblings and can resolve before real items are processed.
364
+ const processItem = async (itemData, key) => {
365
+ if (!itemData) return;
366
+ try {
367
+ const parsed = await holoInstance.parse(itemData);
368
+ if (!parsed) return;
370
369
 
371
- if (holoInstance.isHologram(parsed)) {
372
- const resolved = await holoInstance.resolveHologram(parsed, {
373
- followHolograms: true
374
- });
370
+ if (holoInstance.isHologram(parsed)) {
371
+ const resolved = await holoInstance.resolveHologram(parsed, {
372
+ followHolograms: true
373
+ });
375
374
 
376
- if (resolved === null) {
377
- try {
378
- await holoInstance.deleteGlobal(tableName, key, password);
379
- } catch (deleteError) {
380
- console.error(`Failed to delete invalid global hologram at ${tableName}/${key}:`, deleteError);
381
- }
382
- return;
375
+ if (resolved === null) {
376
+ try {
377
+ await holoInstance.deleteGlobal(tableName, key, password);
378
+ } catch (deleteError) {
379
+ console.error(`Failed to delete invalid global hologram at ${tableName}/${key}:`, deleteError);
383
380
  }
381
+ return;
382
+ }
384
383
 
385
- if (resolved !== parsed) {
386
- output.push(resolved);
387
- } else {
388
- output.push(parsed);
389
- }
384
+ if (resolved !== parsed) {
385
+ output.push(resolved);
390
386
  } else {
391
387
  output.push(parsed);
392
388
  }
393
- } catch (error) {
394
- console.error('Error parsing data:', error);
389
+ } else {
390
+ output.push(parsed);
395
391
  }
396
- })();
397
-
398
- pendingProcessing.push(processingPromise);
399
- receivedCount++;
392
+ } catch (error) {
393
+ console.error('Error parsing data:', error);
394
+ }
395
+ };
400
396
 
401
- if (receivedCount >= expectedCount) {
402
- await Promise.all(pendingProcessing);
403
- resolve(output);
397
+ Promise.all(keys.map((key) => {
398
+ const inline = data[key];
399
+ if (typeof inline !== 'object' || inline === null) {
400
+ return processItem(inline, key);
404
401
  }
405
- });
406
- });
402
+ return new Promise((resolveItem) => {
403
+ dataPath.get(key).once((itemData) => {
404
+ processItem(itemData, key).then(resolveItem, resolveItem);
405
+ });
406
+ });
407
+ })).then(() => resolve(output));
408
+ };
409
+
410
+ (async () => {
411
+ let data = await shallowOnce();
412
+ if (!data) {
413
+ await new Promise(r => setTimeout(r, 1500));
414
+ data = await shallowOnce();
415
+ }
416
+ processShallow(data);
417
+ })();
407
418
  });
408
419
  } catch (error) {
409
420
  console.error('Error in getAllGlobal:', error);
package/hologram.js CHANGED
@@ -78,7 +78,37 @@ export function isHologram(data) {
78
78
  }
79
79
 
80
80
  /**
81
- * Resolves a hologram to its actual data
81
+ * Attaches the canonical `_hologram` envelope to data resolved from a soul reference.
82
+ *
83
+ * This is the SINGLE source of truth for resolved-hologram metadata in HoloSphere.
84
+ * Every code path that returns "data resolved from a hologram reference" must go
85
+ * through this helper so consumers can rely on one shape.
86
+ *
87
+ * @param {object} originalData - The data fetched from the source holon/lens/key.
88
+ * @param {string} hologramSoul - The soul path the hologram pointed at.
89
+ * @returns {object} - originalData merged with `_hologram: { isHologram, soul, sourceHolon, sourceLens, sourceKey, resolvedAt }`.
90
+ */
91
+ export function attachHologramMeta(originalData, hologramSoul) {
92
+ const parts = parseSoulPath(hologramSoul);
93
+ return {
94
+ ...originalData,
95
+ _hologram: {
96
+ isHologram: true,
97
+ soul: hologramSoul,
98
+ sourceHolon: parts?.holon ?? null,
99
+ sourceLens: parts?.lens ?? null,
100
+ sourceKey: parts?.key ?? null,
101
+ resolvedAt: Date.now(),
102
+ },
103
+ };
104
+ }
105
+
106
+ /**
107
+ * Resolves a hologram to its actual data.
108
+ *
109
+ * On success, the returned object spreads `originalData` and attaches the
110
+ * canonical `_hologram` envelope (see {@link attachHologramMeta}).
111
+ *
82
112
  * @param {HoloSphere} holoInstance - The HoloSphere instance.
83
113
  * @param {object} hologram - The hologram to resolve
84
114
  * @param {object} [options] - Optional parameters
@@ -86,7 +116,7 @@ export function isHologram(data) {
86
116
  * @param {Set<string>} [options.visited] - Internal use: Tracks visited souls to prevent loops
87
117
  * @param {number} [options.maxDepth=10] - Maximum resolution depth to prevent infinite loops
88
118
  * @param {number} [options.currentDepth=0] - Current resolution depth
89
- * @returns {Promise<object|null>} - The resolved data, null if resolution failed due to target not found, or the original hologram for circular/invalid cases.
119
+ * @returns {Promise<object|null>} - The resolved data with `_hologram` attached, null if resolution failed due to target not found, or the original hologram for circular/invalid cases.
90
120
  */
91
121
  export async function resolveHologram(holoInstance, hologram, options = {}) {
92
122
  if (!isHologram(hologram)) {
@@ -143,16 +173,9 @@ export async function resolveHologram(holoInstance, hologram, options = {}) {
143
173
  );
144
174
 
145
175
  if (originalData && !originalData._invalidHologram) {
146
- // Structure for the returned object - isHologram (top-level) is removed
147
- return {
148
- ...originalData,
149
- _meta: {
150
- ...(originalData._meta || {}), // Preserve original _meta
151
- resolvedFromHologram: true, // This is now the primary indicator
152
- hologramSoul: hologram.soul, // Clarified meta field
153
- resolutionDepth: currentDepth
154
- }
155
- };
176
+ // Attach the canonical `_hologram` envelope. This is the only
177
+ // resolved-hologram indicator HoloSphere emits.
178
+ return attachHologramMeta(originalData, hologram.soul);
156
179
  } else {
157
180
  console.warn(`!!! Original data NOT FOUND for soul: ${hologram.soul}. Removing broken hologram.`);
158
181
 
@@ -179,5 +202,6 @@ export default {
179
202
  createHologram,
180
203
  parseSoulPath,
181
204
  isHologram,
182
- resolveHologram
183
- };
205
+ resolveHologram,
206
+ attachHologramMeta
207
+ };