peerpigeon 0.0.7 → 0.0.8

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.
@@ -0,0 +1,1334 @@
1
+ import { EventEmitter } from './EventEmitter.js';
2
+
3
+ // Dynamic import for unsea to handle both Node.js and browser environments
4
+ let unsea = null;
5
+
6
+ async function initializeUnsea() {
7
+ if (unsea) return unsea;
8
+
9
+ try {
10
+ // Detect environment - prioritize Node.js detection for tests
11
+ const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
12
+ const isBrowser = !isNode && typeof window !== 'undefined' && typeof document !== 'undefined';
13
+
14
+ if (isNode) {
15
+ // For Node.js environments (including tests), use npm package
16
+ unsea = await import('unsea');
17
+ console.log('✅ Loaded unsea from npm package (Node.js) for storage');
18
+ } else if (isBrowser) {
19
+ // For browser environments, try CDN sources
20
+ try {
21
+ unsea = await import('https://cdn.jsdelivr.net/npm/unsea@latest/+esm');
22
+ console.log('✅ Loaded unsea from jsDelivr CDN for storage');
23
+ } catch (jsDelivrError) {
24
+ console.warn('Failed to load from jsDelivr, trying unpkg:', jsDelivrError);
25
+ try {
26
+ unsea = await import('https://unpkg.com/unsea@latest/dist/unsea.esm.js');
27
+ console.log('✅ Loaded unsea from unpkg CDN for storage');
28
+ } catch (unpkgError) {
29
+ console.warn('Failed to load from unpkg, trying Skypack:', unpkgError);
30
+ unsea = await import('https://cdn.skypack.dev/unsea');
31
+ console.log('✅ Loaded unsea from Skypack CDN for storage');
32
+ }
33
+ }
34
+ } else {
35
+ throw new Error('Unknown environment - cannot load unsea');
36
+ }
37
+
38
+ if (!unsea) {
39
+ throw new Error('Unsea not found after loading');
40
+ }
41
+
42
+ return unsea;
43
+ } catch (error) {
44
+ console.error('Failed to load unsea for storage:', error);
45
+ throw error;
46
+ }
47
+ }
48
+
49
+ /**
50
+ * DistributedStorageManager - Optional storage layer for PeerPigeon mesh
51
+ *
52
+ * Features:
53
+ * - Encrypted storage using unsea directly
54
+ * - Optional public/private data visibility
55
+ * - Optional immutability with CRDT support for collaborative editing
56
+ * - Mutable layer for data originators
57
+ * - Integration with WebDHT for distributed storage
58
+ */
59
+ export class DistributedStorageManager extends EventEmitter {
60
+ constructor(mesh) {
61
+ super();
62
+ this.mesh = mesh;
63
+ this.webDHT = mesh.webDHT;
64
+ this.cryptoManager = mesh.cryptoManager;
65
+
66
+ // Initialize unsea for encryption
67
+ this.unsea = null;
68
+ this.storageKeypair = null;
69
+ this.initializeCrypto();
70
+
71
+ // Storage configuration
72
+ this.config = {
73
+ encryptionEnabled: true,
74
+ defaultTTL: null, // No expiration by default
75
+ maxValueSize: 1024 * 1024, // 1MB default max size
76
+ enableCRDT: true,
77
+ conflictResolution: 'last-write-wins' // or 'crdt-merge'
78
+ };
79
+
80
+ // Local storage for metadata and permissions
81
+ this.storageMetadata = new Map(); // keyId -> metadata
82
+ this.accessControl = new Map(); // keyId -> access control info
83
+ this.crdtStates = new Map(); // keyId -> CRDT state for collaborative data
84
+
85
+ // Track data ownership
86
+ this.ownedKeys = new Set(); // Keys owned by this peer
87
+
88
+ // Track enabled state
89
+ this.enabled = true; // Enable by default
90
+
91
+ console.log(`DistributedStorageManager initialized for peer ${this.mesh.peerId?.substring(0, 8)}...`);
92
+ }
93
+
94
+ /**
95
+ * Initialize unsea encryption for storage
96
+ * @private
97
+ */
98
+ async initializeCrypto() {
99
+ try {
100
+ this.unsea = await initializeUnsea();
101
+
102
+ // Generate a keypair for storage encryption
103
+ // Use the mesh's keypair if available, otherwise generate one
104
+ if (this.cryptoManager && this.cryptoManager.keypair) {
105
+ this.storageKeypair = this.cryptoManager.keypair;
106
+ } else {
107
+ this.storageKeypair = await this.unsea.generateRandomPair();
108
+ }
109
+
110
+ console.log('📦 Storage encryption initialized with unsea');
111
+ } catch (error) {
112
+ console.warn('📦 Failed to initialize storage encryption:', error);
113
+ // Disable encryption if unsea fails to load
114
+ this.config.encryptionEnabled = false;
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Wait for crypto initialization to complete
120
+ * @returns {Promise<void>}
121
+ */
122
+ async waitForCrypto() {
123
+ if (this.unsea && this.storageKeypair) {
124
+ return;
125
+ }
126
+
127
+ // Wait up to 5 seconds for crypto to initialize
128
+ const timeout = 5000;
129
+ const start = Date.now();
130
+
131
+ while (!this.unsea || !this.storageKeypair) {
132
+ if (Date.now() - start > timeout) {
133
+ console.warn('📦 Crypto initialization timeout - proceeding without encryption');
134
+ this.config.encryptionEnabled = false;
135
+ break;
136
+ }
137
+ await new Promise(resolve => setTimeout(resolve, 100));
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Store data with encryption and access control
143
+ * @param {string} key - The storage key
144
+ * @param {any} value - The value to store
145
+ * @param {Object} options - Storage options
146
+ * @param {boolean} options.isPublic - Whether data is publicly readable (default: false - private and encrypted)
147
+ * @param {boolean} options.isImmutable - Whether data is immutable for other peers (default: false)
148
+ * @param {boolean} options.enableCRDT - Whether to enable CRDT for collaborative editing (default: false)
149
+ * @param {number} options.ttl - Time to live in milliseconds
150
+ * @param {Array<string>} options.allowedPeers - Specific peers allowed to read private data
151
+ * @returns {Promise<boolean>} Success status
152
+ */
153
+ async store(key, value, options = {}) {
154
+ if (!this.webDHT) {
155
+ throw new Error('WebDHT not available - ensure it is enabled in mesh configuration');
156
+ }
157
+
158
+ // Wait for crypto initialization if encryption is enabled
159
+ if (this.config.encryptionEnabled) {
160
+ await this.waitForCrypto();
161
+ }
162
+
163
+ const keyId = await this.webDHT.generateKeyId(key);
164
+ const timestamp = Date.now();
165
+ console.log(`📦 Storing key: ${key}, keyId: ${keyId.substring(0, 8)}...`);
166
+
167
+ // Validate value size
168
+ const serializedValue = JSON.stringify(value);
169
+ if (serializedValue.length > this.config.maxValueSize) {
170
+ throw new Error(`Value size exceeds maximum allowed size of ${this.config.maxValueSize} bytes`);
171
+ }
172
+
173
+ // Create storage metadata - default to private for security
174
+ const metadata = {
175
+ key,
176
+ keyId,
177
+ owner: this.mesh.peerId,
178
+ isPublic: options.isPublic !== undefined ? options.isPublic : false, // Default to private
179
+ isImmutable: options.isImmutable || false,
180
+ enableCRDT: options.enableCRDT || false,
181
+ allowedPeers: options.allowedPeers || [],
182
+ createdAt: timestamp,
183
+ updatedAt: timestamp,
184
+ version: 1,
185
+ ttl: options.ttl || this.config.defaultTTL
186
+ };
187
+
188
+ // Store metadata locally
189
+ this.storageMetadata.set(keyId, metadata);
190
+ this.ownedKeys.add(keyId);
191
+
192
+ // Set up access control
193
+ this.accessControl.set(keyId, {
194
+ isPublic: metadata.isPublic,
195
+ owner: metadata.owner,
196
+ allowedPeers: new Set(metadata.allowedPeers),
197
+ isImmutable: metadata.isImmutable
198
+ });
199
+
200
+ // Initialize CRDT state if enabled
201
+ if (metadata.enableCRDT) {
202
+ this.crdtStates.set(keyId, {
203
+ vectorClock: { [this.mesh.peerId]: 1 },
204
+ operations: [],
205
+ lastMerged: timestamp
206
+ });
207
+ }
208
+
209
+ // Prepare the storage payload
210
+ let storagePayload = {
211
+ value,
212
+ metadata,
213
+ encrypted: false
214
+ };
215
+
216
+ // Only encrypt private data (isPublic: false) to ensure owner-only access
217
+ if (!metadata.isPublic && this.config.encryptionEnabled && this.unsea && this.storageKeypair) {
218
+ try {
219
+ // For private data, encrypt with the owner's keypair so only the owner can decrypt
220
+ const encryptedValue = await this.unsea.encryptMessageWithMeta(serializedValue, this.storageKeypair);
221
+ storagePayload = {
222
+ value: encryptedValue,
223
+ metadata,
224
+ encrypted: true,
225
+ encryptedBy: this.mesh.peerId // Track who encrypted it
226
+ };
227
+ console.log(`📦 Encrypted private storage data for key: ${key} (owner-only access)`);
228
+ } catch (error) {
229
+ console.warn(`Failed to encrypt private storage data for key ${key}:`, error);
230
+ // Fall back to unencrypted storage if encryption fails
231
+ }
232
+ } else {
233
+ console.log(`📦 Storing ${metadata.isPublic ? 'public' : 'unencrypted'} data for key: ${key}`);
234
+ }
235
+
236
+ // Store in WebDHT
237
+ try {
238
+ console.log(`📦 Storing payload for key ${key}:`, {
239
+ hasValue: !!storagePayload.value,
240
+ hasMetadata: !!storagePayload.metadata,
241
+ encrypted: storagePayload.encrypted
242
+ });
243
+
244
+ await this.webDHT.put(`storage:${key}`, storagePayload, {
245
+ ttl: metadata.ttl
246
+ });
247
+
248
+ console.log(`📦 Stored ${metadata.isPublic ? 'public' : 'private'} data for key: ${key}`);
249
+
250
+ // Emit storage event
251
+ this.emit('dataStored', {
252
+ key,
253
+ keyId,
254
+ isPublic: metadata.isPublic,
255
+ isImmutable: metadata.isImmutable,
256
+ enableCRDT: metadata.enableCRDT
257
+ });
258
+
259
+ return true;
260
+ } catch (error) {
261
+ console.error(`Failed to store data for key ${key}:`, error);
262
+ // Clean up local metadata on failure
263
+ this.storageMetadata.delete(keyId);
264
+ this.ownedKeys.delete(keyId);
265
+ this.accessControl.delete(keyId);
266
+ this.crdtStates.delete(keyId);
267
+ throw error;
268
+ }
269
+ }
270
+
271
+ /**
272
+ * Retrieve data with access control and decryption
273
+ * @param {string} key - The storage key
274
+ * @param {Object} options - Retrieval options
275
+ * @param {boolean} options.forceRefresh - Force refresh from network
276
+ * @returns {Promise<any>} The stored value or null if not accessible
277
+ */
278
+ async retrieve(key, options = {}) {
279
+ if (!this.webDHT) {
280
+ throw new Error('WebDHT not available - ensure it is enabled in mesh configuration');
281
+ }
282
+
283
+ // Wait for crypto initialization if encryption is enabled
284
+ if (this.config.encryptionEnabled) {
285
+ await this.waitForCrypto();
286
+ }
287
+
288
+ const keyId = await this.webDHT.generateKeyId(key);
289
+ console.log(`📦 Retrieving key: ${key}, keyId: ${keyId.substring(0, 8)}...`);
290
+
291
+ try {
292
+ // Get data from WebDHT
293
+ console.log(`📦 Attempting to retrieve data for key: ${key}`);
294
+ const storagePayload = await this.webDHT.get(`storage:${key}`, {
295
+ forceRefresh: options.forceRefresh
296
+ });
297
+
298
+ console.log(`📦 Retrieved payload for key ${key}:`, {
299
+ payloadExists: !!storagePayload,
300
+ payloadType: typeof storagePayload,
301
+ payloadValue: storagePayload,
302
+ keys: storagePayload ? Object.keys(storagePayload) : 'none'
303
+ });
304
+
305
+ if (!storagePayload || typeof storagePayload !== 'object') {
306
+ console.log(`📦 No data found for key: ${key}`);
307
+ return null;
308
+ }
309
+
310
+ // Debug: Log the raw payload structure
311
+ console.log('📦 Raw payload structure:', JSON.stringify(storagePayload, null, 2));
312
+
313
+ // Safely destructure the payload with detailed logging
314
+ const value = storagePayload.value;
315
+ const metadata = storagePayload.metadata;
316
+ const encrypted = storagePayload.encrypted || false;
317
+ const encryptedBy = storagePayload.encryptedBy;
318
+
319
+ console.log('📦 Payload components:', {
320
+ hasValue: value !== undefined,
321
+ valueType: typeof value,
322
+ hasMetadata: metadata !== undefined,
323
+ metadataType: typeof metadata,
324
+ encrypted,
325
+ encryptedBy: encryptedBy?.substring(0, 8) + '...' || 'unknown'
326
+ });
327
+
328
+ if (!metadata) {
329
+ console.warn(`📦 Invalid storage payload for key ${key}: missing metadata`);
330
+ return null;
331
+ }
332
+
333
+ // Check access permissions
334
+ if (!this.hasReadAccess(keyId, metadata)) {
335
+ console.warn(`Access denied for key: ${key}`);
336
+ return null;
337
+ }
338
+
339
+ // Decrypt if necessary
340
+ let finalValue = value;
341
+ if (encrypted && this.unsea && this.storageKeypair) {
342
+ // Only allow decryption if this peer is the owner (encrypted the data)
343
+ if (encryptedBy && encryptedBy !== this.mesh.peerId) {
344
+ console.warn(`📦 Cannot decrypt data for key ${key}: encrypted by different peer (${encryptedBy.substring(0, 8)}...), current peer: ${this.mesh.peerId.substring(0, 8)}...`);
345
+ // For private data encrypted by another peer, deny access
346
+ if (!metadata.isPublic) {
347
+ console.warn(`Access denied for key: ${key} - private data encrypted by different peer`);
348
+ return null;
349
+ }
350
+ }
351
+
352
+ try {
353
+ const decryptedValue = await this.unsea.decryptMessageWithMeta(value, this.storageKeypair.epriv);
354
+ finalValue = JSON.parse(decryptedValue);
355
+ console.log(`🔓 Decrypted storage data for key: ${key}`);
356
+ } catch (error) {
357
+ console.error(`Failed to decrypt data for key ${key}:`, error);
358
+ // If this is private data and decryption fails, deny access
359
+ if (!metadata.isPublic) {
360
+ return null;
361
+ }
362
+ // For public data, if decryption fails, try to use the raw value
363
+ console.warn(`📦 Using raw value for public key ${key} due to decryption failure`);
364
+ finalValue = value;
365
+ }
366
+ }
367
+
368
+ // Update local metadata if we don't have it
369
+ if (!this.storageMetadata.has(keyId)) {
370
+ this.storageMetadata.set(keyId, metadata);
371
+ this.accessControl.set(keyId, {
372
+ isPublic: metadata.isPublic,
373
+ owner: metadata.owner,
374
+ allowedPeers: new Set(metadata.allowedPeers),
375
+ isImmutable: metadata.isImmutable
376
+ });
377
+ }
378
+
379
+ console.log(`📦 Retrieved ${metadata.isPublic ? 'public' : 'private'} data for key: ${key}`);
380
+
381
+ // Emit retrieval event
382
+ this.emit('dataRetrieved', {
383
+ key,
384
+ keyId,
385
+ isPublic: metadata.isPublic,
386
+ owner: metadata.owner
387
+ });
388
+
389
+ return finalValue;
390
+ } catch (error) {
391
+ console.error(`Failed to retrieve data for key ${key}:`, error);
392
+ return null;
393
+ }
394
+ }
395
+
396
+ /**
397
+ * Update existing data (only allowed for owners or if mutable)
398
+ * @param {string} key - The storage key
399
+ * @param {any} newValue - The new value
400
+ * @param {Object} options - Update options
401
+ * @param {boolean} options.forceCRDTMerge - Force CRDT merge even if not owner
402
+ * @returns {Promise<boolean>} Success status
403
+ */
404
+ async update(key, newValue, options = {}) {
405
+ if (!this.webDHT) {
406
+ throw new Error('WebDHT not available - ensure it is enabled in mesh configuration');
407
+ }
408
+
409
+ const keyId = await this.webDHT.generateKeyId(key);
410
+ const timestamp = Date.now();
411
+
412
+ // Check if we have access to update
413
+ const accessControl = this.accessControl.get(keyId);
414
+ const metadata = this.storageMetadata.get(keyId);
415
+
416
+ if (!accessControl || !metadata) {
417
+ // Try to retrieve metadata first
418
+ const existingData = await this.retrieve(key);
419
+ if (!existingData) {
420
+ throw new Error(`Key ${key} does not exist or is not accessible`);
421
+ }
422
+ // Retry after retrieving metadata
423
+ return this.update(key, newValue, options);
424
+ }
425
+
426
+ const isOwner = accessControl.owner === this.mesh.peerId;
427
+ const canUpdate = isOwner || (!accessControl.isImmutable) || (metadata.enableCRDT && options.forceCRDTMerge);
428
+
429
+ if (!canUpdate) {
430
+ throw new Error(`Update not allowed for key ${key}: immutable data and not owner`);
431
+ }
432
+
433
+ // Handle CRDT merge if enabled
434
+ if (metadata.enableCRDT && !isOwner) {
435
+ return this.applyCRDTUpdate(key, keyId, newValue, options);
436
+ }
437
+
438
+ // Update metadata
439
+ metadata.updatedAt = timestamp;
440
+ metadata.version += 1;
441
+
442
+ // Prepare storage payload
443
+ let storagePayload = {
444
+ value: newValue,
445
+ metadata,
446
+ encrypted: false
447
+ };
448
+
449
+ // Encrypt if not public and crypto is available
450
+ if (!metadata.isPublic && this.config.encryptionEnabled && this.unsea && this.storageKeypair) {
451
+ try {
452
+ const serializedValue = JSON.stringify(newValue);
453
+ const encryptedValue = await this.unsea.encryptMessageWithMeta(serializedValue, this.storageKeypair);
454
+ storagePayload = {
455
+ value: encryptedValue,
456
+ metadata,
457
+ encrypted: true,
458
+ encryptedBy: this.mesh.peerId // Track who encrypted it
459
+ };
460
+ console.log(`📦 Encrypted updated storage data for key: ${key}`);
461
+ } catch (error) {
462
+ console.warn(`Failed to encrypt updated storage data for key ${key}:`, error);
463
+ }
464
+ }
465
+
466
+ try {
467
+ // Use WebDHT update to notify all replicas and subscribers
468
+ await this.webDHT.update(`storage:${key}`, storagePayload, {
469
+ ttl: metadata.ttl
470
+ });
471
+
472
+ // Update local metadata
473
+ this.storageMetadata.set(keyId, metadata);
474
+
475
+ console.log(`📦 Updated ${metadata.isPublic ? 'public' : 'private'} data for key: ${key}`);
476
+
477
+ // Emit update event
478
+ this.emit('dataUpdated', {
479
+ key,
480
+ keyId,
481
+ isPublic: metadata.isPublic,
482
+ version: metadata.version,
483
+ isOwner
484
+ });
485
+
486
+ return true;
487
+ } catch (error) {
488
+ console.error(`Failed to update data for key ${key}:`, error);
489
+ throw error;
490
+ }
491
+ }
492
+
493
+ /**
494
+ * Apply CRDT-based update for collaborative editing
495
+ * @private
496
+ */
497
+ async applyCRDTUpdate(key, keyId, operation, options = {}) {
498
+ const crdtState = this.crdtStates.get(keyId);
499
+ const metadata = this.storageMetadata.get(keyId);
500
+
501
+ if (!crdtState || !metadata) {
502
+ throw new Error(`CRDT state not found for key ${key}`);
503
+ }
504
+
505
+ // Increment vector clock for this peer
506
+ const currentClock = crdtState.vectorClock[this.mesh.peerId] || 0;
507
+ crdtState.vectorClock[this.mesh.peerId] = currentClock + 1;
508
+
509
+ // Add operation to CRDT state
510
+ const crdtOperation = {
511
+ peerId: this.mesh.peerId,
512
+ timestamp: Date.now(),
513
+ vectorClock: { ...crdtState.vectorClock },
514
+ operation,
515
+ type: options.operationType || 'replace'
516
+ };
517
+
518
+ crdtState.operations.push(crdtOperation);
519
+ crdtState.lastMerged = Date.now();
520
+
521
+ // Apply CRDT merge logic (simplified last-write-wins for now)
522
+ const mergedValue = this.mergeCRDTOperations(crdtState.operations);
523
+
524
+ // Update the stored value
525
+ return this.update(key, mergedValue, { ...options, forceCRDTMerge: false });
526
+ }
527
+
528
+ /**
529
+ * Simple CRDT merge implementation (can be extended for more sophisticated CRDTs)
530
+ * @private
531
+ */
532
+ mergeCRDTOperations(operations) {
533
+ // Sort operations by timestamp and apply in order
534
+ const sortedOps = operations.sort((a, b) => a.timestamp - b.timestamp);
535
+
536
+ let result = null;
537
+ for (const op of sortedOps) {
538
+ switch (op.type) {
539
+ case 'replace':
540
+ result = op.operation;
541
+ break;
542
+ case 'merge':
543
+ if (result && typeof result === 'object' && typeof op.operation === 'object') {
544
+ result = { ...result, ...op.operation };
545
+ } else {
546
+ result = op.operation;
547
+ }
548
+ break;
549
+ default:
550
+ result = op.operation;
551
+ }
552
+ }
553
+
554
+ return result;
555
+ }
556
+
557
+ /**
558
+ * Delete data (only allowed for owners)
559
+ * @param {string} key - The storage key
560
+ * @returns {Promise<boolean>} Success status
561
+ */
562
+ async delete(key) {
563
+ if (!this.webDHT) {
564
+ throw new Error('WebDHT not available - ensure it is enabled in mesh configuration');
565
+ }
566
+
567
+ const keyId = await this.webDHT.generateKeyId(key);
568
+ const accessControl = this.accessControl.get(keyId);
569
+
570
+ if (!accessControl || accessControl.owner !== this.mesh.peerId) {
571
+ throw new Error(`Delete not allowed for key ${key}: not owner`);
572
+ }
573
+
574
+ try {
575
+ // Create a tombstone entry to mark as deleted
576
+ const tombstone = {
577
+ deleted: true,
578
+ deletedAt: Date.now(),
579
+ deletedBy: this.mesh.peerId
580
+ };
581
+
582
+ await this.webDHT.update(`storage:${key}`, tombstone);
583
+
584
+ // Clean up local state
585
+ this.storageMetadata.delete(keyId);
586
+ this.ownedKeys.delete(keyId);
587
+ this.accessControl.delete(keyId);
588
+ this.crdtStates.delete(keyId);
589
+
590
+ console.log(`📦 Deleted data for key: ${key}`);
591
+
592
+ // Emit deletion event
593
+ this.emit('dataDeleted', { key, keyId });
594
+
595
+ return true;
596
+ } catch (error) {
597
+ console.error(`Failed to delete data for key ${key}:`, error);
598
+ throw error;
599
+ }
600
+ }
601
+
602
+ /**
603
+ * Subscribe to changes for a storage key
604
+ * @param {string} key - The storage key
605
+ * @returns {Promise<any>} Current value or null
606
+ */
607
+ async subscribe(key) {
608
+ if (!this.webDHT) {
609
+ throw new Error('WebDHT not available - ensure it is enabled in mesh configuration');
610
+ }
611
+
612
+ // Subscribe to the WebDHT key
613
+ const currentValue = await this.webDHT.subscribe(`storage:${key}`);
614
+
615
+ console.log(`📦 Subscribed to storage key: ${key}`);
616
+
617
+ return currentValue;
618
+ }
619
+
620
+ /**
621
+ * Unsubscribe from changes for a storage key
622
+ * @param {string} key - The storage key
623
+ */
624
+ async unsubscribe(key) {
625
+ if (!this.webDHT) {
626
+ return;
627
+ }
628
+
629
+ await this.webDHT.unsubscribe(`storage:${key}`);
630
+ console.log(`📦 Unsubscribed from storage key: ${key}`);
631
+ }
632
+
633
+ /**
634
+ * Check if the current peer has read access to a key
635
+ * @private
636
+ */
637
+ hasReadAccess(keyId, metadata) {
638
+ const accessControl = this.accessControl.get(keyId);
639
+
640
+ if (!accessControl && metadata) {
641
+ // Create access control from metadata
642
+ this.accessControl.set(keyId, {
643
+ isPublic: metadata.isPublic,
644
+ owner: metadata.owner,
645
+ allowedPeers: new Set(metadata.allowedPeers || []),
646
+ isImmutable: metadata.isImmutable
647
+ });
648
+ return this.hasReadAccess(keyId, metadata);
649
+ }
650
+
651
+ if (!accessControl) {
652
+ return false;
653
+ }
654
+
655
+ // Owner always has access
656
+ if (accessControl.owner === this.mesh.peerId) {
657
+ return true;
658
+ }
659
+
660
+ // Public data is accessible to everyone
661
+ if (accessControl.isPublic) {
662
+ return true;
663
+ }
664
+
665
+ // Check if peer is in allowed list
666
+ return accessControl.allowedPeers.has(this.mesh.peerId);
667
+ }
668
+
669
+ /**
670
+ * Grant access to a peer for a private key (only owner can do this)
671
+ * @param {string} key - The storage key
672
+ * @param {string} peerId - The peer to grant access to
673
+ * @returns {Promise<boolean>} Success status
674
+ */
675
+ async grantAccess(key, peerId) {
676
+ const keyId = await this.webDHT.generateKeyId(key);
677
+ const accessControl = this.accessControl.get(keyId);
678
+ const metadata = this.storageMetadata.get(keyId);
679
+
680
+ if (!accessControl || !metadata || accessControl.owner !== this.mesh.peerId) {
681
+ throw new Error(`Cannot grant access for key ${key}: not owner`);
682
+ }
683
+
684
+ if (accessControl.isPublic) {
685
+ throw new Error(`Cannot grant access for key ${key}: already public`);
686
+ }
687
+
688
+ // Add peer to allowed list
689
+ accessControl.allowedPeers.add(peerId);
690
+ metadata.allowedPeers.push(peerId);
691
+ metadata.updatedAt = Date.now();
692
+
693
+ // Update the stored metadata
694
+ try {
695
+ const currentPayload = await this.webDHT.get(`storage:${key}`);
696
+ if (currentPayload) {
697
+ currentPayload.metadata = metadata;
698
+ await this.webDHT.update(`storage:${key}`, currentPayload);
699
+ }
700
+
701
+ console.log(`📦 Granted access to peer ${peerId.substring(0, 8)}... for key: ${key}`);
702
+
703
+ // Emit access granted event
704
+ this.emit('accessGranted', { key, keyId, peerId });
705
+
706
+ return true;
707
+ } catch (error) {
708
+ // Rollback changes
709
+ accessControl.allowedPeers.delete(peerId);
710
+ metadata.allowedPeers = metadata.allowedPeers.filter(p => p !== peerId);
711
+ throw error;
712
+ }
713
+ }
714
+
715
+ /**
716
+ * Revoke access from a peer for a private key (only owner can do this)
717
+ * @param {string} key - The storage key
718
+ * @param {string} peerId - The peer to revoke access from
719
+ * @returns {Promise<boolean>} Success status
720
+ */
721
+ async revokeAccess(key, peerId) {
722
+ const keyId = await this.webDHT.generateKeyId(key);
723
+ const accessControl = this.accessControl.get(keyId);
724
+ const metadata = this.storageMetadata.get(keyId);
725
+
726
+ if (!accessControl || !metadata || accessControl.owner !== this.mesh.peerId) {
727
+ throw new Error(`Cannot revoke access for key ${key}: not owner`);
728
+ }
729
+
730
+ // Remove peer from allowed list
731
+ accessControl.allowedPeers.delete(peerId);
732
+ metadata.allowedPeers = metadata.allowedPeers.filter(p => p !== peerId);
733
+ metadata.updatedAt = Date.now();
734
+
735
+ // Update the stored metadata
736
+ try {
737
+ const currentPayload = await this.webDHT.get(`storage:${key}`);
738
+ if (currentPayload) {
739
+ currentPayload.metadata = metadata;
740
+ await this.webDHT.update(`storage:${key}`, currentPayload);
741
+ }
742
+
743
+ console.log(`📦 Revoked access from peer ${peerId.substring(0, 8)}... for key: ${key}`);
744
+
745
+ // Emit access revoked event
746
+ this.emit('accessRevoked', { key, keyId, peerId });
747
+
748
+ return true;
749
+ } catch (error) {
750
+ // Rollback changes
751
+ accessControl.allowedPeers.add(peerId);
752
+ metadata.allowedPeers.push(peerId);
753
+ throw error;
754
+ }
755
+ }
756
+
757
+ /**
758
+ * List all keys owned by this peer
759
+ * @returns {Array<Object>} Array of owned key metadata
760
+ */
761
+ getOwnedKeys() {
762
+ const ownedKeys = [];
763
+ for (const keyId of this.ownedKeys) {
764
+ const metadata = this.storageMetadata.get(keyId);
765
+ if (metadata) {
766
+ ownedKeys.push({
767
+ key: metadata.key,
768
+ keyId,
769
+ isPublic: metadata.isPublic,
770
+ isImmutable: metadata.isImmutable,
771
+ enableCRDT: metadata.enableCRDT,
772
+ createdAt: metadata.createdAt,
773
+ updatedAt: metadata.updatedAt,
774
+ version: metadata.version
775
+ });
776
+ }
777
+ }
778
+ return ownedKeys;
779
+ }
780
+
781
+ /**
782
+ * Get statistics about the storage manager
783
+ * @returns {Promise<Object>} Storage statistics
784
+ */
785
+ async getStats() {
786
+ let totalSize = 0;
787
+ let itemCount = 0;
788
+
789
+ // Calculate sizes for owned keys
790
+ for (const keyId of this.ownedKeys) {
791
+ const metadata = this.storageMetadata.get(keyId);
792
+ if (metadata) {
793
+ try {
794
+ const value = await this.retrieve(metadata.key);
795
+ if (value !== null) {
796
+ const serializedSize = JSON.stringify(value).length;
797
+ totalSize += serializedSize;
798
+ itemCount++;
799
+ }
800
+ } catch (error) {
801
+ // Skip keys that can't be retrieved
802
+ }
803
+ }
804
+ }
805
+
806
+ return {
807
+ enabled: this.enabled,
808
+ itemCount,
809
+ totalSize,
810
+ ownedKeys: this.ownedKeys.size,
811
+ totalKeys: this.storageMetadata.size,
812
+ crdtKeys: this.crdtStates.size,
813
+ encryptionEnabled: this.config.encryptionEnabled && !!this.unsea && !!this.storageKeypair,
814
+ maxValueSize: this.config.maxValueSize
815
+ };
816
+ }
817
+
818
+ /**
819
+ * Clean up expired data and old CRDT operations
820
+ */
821
+ cleanup() {
822
+ const now = Date.now();
823
+
824
+ // Clean up expired metadata
825
+ for (const [keyId, metadata] of this.storageMetadata.entries()) {
826
+ if (metadata.ttl && (metadata.createdAt + metadata.ttl) < now) {
827
+ this.storageMetadata.delete(keyId);
828
+ this.accessControl.delete(keyId);
829
+ this.ownedKeys.delete(keyId);
830
+ console.log(`📦 Cleaned up expired metadata for key: ${metadata.key}`);
831
+ }
832
+ }
833
+
834
+ // Clean up old CRDT operations (keep last 100 operations per key)
835
+ for (const [keyId, crdtState] of this.crdtStates.entries()) {
836
+ if (crdtState.operations.length > 100) {
837
+ crdtState.operations = crdtState.operations.slice(-100);
838
+ console.log(`📦 Cleaned up old CRDT operations for key: ${keyId.substring(0, 8)}...`);
839
+ }
840
+ }
841
+ }
842
+
843
+ /**
844
+ * Backup all owned data to a serializable format
845
+ * @returns {Object} Backup data that can be restored later
846
+ */
847
+ async backup() {
848
+ const backupData = {
849
+ version: '1.0.0',
850
+ timestamp: Date.now(),
851
+ peerId: this.mesh.peerId,
852
+ keys: []
853
+ };
854
+
855
+ for (const keyId of this.ownedKeys) {
856
+ const metadata = this.storageMetadata.get(keyId);
857
+ if (metadata) {
858
+ try {
859
+ // Get the current value from storage
860
+ const value = await this.retrieve(metadata.key);
861
+ if (value !== null) {
862
+ backupData.keys.push({
863
+ key: metadata.key,
864
+ value,
865
+ metadata: {
866
+ isPublic: metadata.isPublic,
867
+ isImmutable: metadata.isImmutable,
868
+ enableCRDT: metadata.enableCRDT,
869
+ allowedPeers: metadata.allowedPeers,
870
+ ttl: metadata.ttl
871
+ }
872
+ });
873
+ }
874
+ } catch (error) {
875
+ console.warn(`Failed to backup key ${metadata.key}:`, error);
876
+ }
877
+ }
878
+ }
879
+
880
+ console.log(`📦 Created backup with ${backupData.keys.length} keys`);
881
+ return backupData;
882
+ }
883
+
884
+ /**
885
+ * Restore data from a backup
886
+ * @param {Object} backupData - Backup data created by backup()
887
+ * @param {Object} options - Restore options
888
+ * @param {boolean} options.overwrite - Whether to overwrite existing keys (default: false)
889
+ * @returns {Promise<Object>} Restore results
890
+ */
891
+ async restore(backupData, options = {}) {
892
+ if (!backupData || !backupData.keys || !Array.isArray(backupData.keys)) {
893
+ throw new Error('Invalid backup data format');
894
+ }
895
+
896
+ const results = {
897
+ restored: 0,
898
+ skipped: 0,
899
+ failed: 0,
900
+ errors: []
901
+ };
902
+
903
+ for (const keyData of backupData.keys) {
904
+ try {
905
+ const { key, value, metadata } = keyData;
906
+
907
+ // Check if key already exists
908
+ const existing = await this.retrieve(key);
909
+ if (existing !== null && !options.overwrite) {
910
+ results.skipped++;
911
+ continue;
912
+ }
913
+
914
+ // Restore the key
915
+ await this.store(key, value, metadata);
916
+ results.restored++;
917
+ } catch (error) {
918
+ results.failed++;
919
+ results.errors.push({
920
+ key: keyData.key,
921
+ error: error.message
922
+ });
923
+ console.warn(`Failed to restore key ${keyData.key}:`, error);
924
+ }
925
+ }
926
+
927
+ console.log(`📦 Restore complete: ${results.restored} restored, ${results.skipped} skipped, ${results.failed} failed`);
928
+ return results;
929
+ }
930
+
931
+ /**
932
+ * List all accessible keys (owned + granted access)
933
+ * @returns {Array<Object>} Array of accessible key metadata
934
+ */
935
+ async listAccessibleKeys() {
936
+ const accessibleKeys = [];
937
+
938
+ // Add owned keys
939
+ for (const keyId of this.ownedKeys) {
940
+ const metadata = this.storageMetadata.get(keyId);
941
+ if (metadata) {
942
+ accessibleKeys.push({
943
+ key: metadata.key,
944
+ keyId,
945
+ isPublic: metadata.isPublic,
946
+ isImmutable: metadata.isImmutable,
947
+ enableCRDT: metadata.enableCRDT,
948
+ createdAt: metadata.createdAt,
949
+ updatedAt: metadata.updatedAt,
950
+ version: metadata.version,
951
+ owned: true,
952
+ accessible: true
953
+ });
954
+ }
955
+ }
956
+
957
+ // Add keys we have access to but don't own
958
+ for (const [keyId] of this.accessControl.entries()) {
959
+ if (!this.ownedKeys.has(keyId) && this.hasReadAccess(keyId)) {
960
+ const metadata = this.storageMetadata.get(keyId);
961
+ if (metadata) {
962
+ accessibleKeys.push({
963
+ key: metadata.key,
964
+ keyId,
965
+ isPublic: metadata.isPublic,
966
+ isImmutable: metadata.isImmutable,
967
+ enableCRDT: metadata.enableCRDT,
968
+ createdAt: metadata.createdAt,
969
+ updatedAt: metadata.updatedAt,
970
+ version: metadata.version,
971
+ owned: false,
972
+ accessible: true
973
+ });
974
+ }
975
+ }
976
+ }
977
+
978
+ return accessibleKeys;
979
+ }
980
+
981
+ /**
982
+ * Bulk store multiple key-value pairs
983
+ * @param {Array<Object>} items - Array of {key, value, options} objects
984
+ * @param {Object} globalOptions - Options to apply to all items
985
+ * @returns {Promise<Object>} Results summary
986
+ */
987
+ async bulkStore(items, globalOptions = {}) {
988
+ const results = {
989
+ stored: 0,
990
+ failed: 0,
991
+ errors: []
992
+ };
993
+
994
+ const storePromises = items.map(async (item) => {
995
+ try {
996
+ const options = { ...globalOptions, ...item.options };
997
+ await this.store(item.key, item.value, options);
998
+ results.stored++;
999
+ } catch (error) {
1000
+ results.failed++;
1001
+ results.errors.push({
1002
+ key: item.key,
1003
+ error: error.message
1004
+ });
1005
+ }
1006
+ });
1007
+
1008
+ await Promise.allSettled(storePromises);
1009
+ console.log(`📦 Bulk store complete: ${results.stored} stored, ${results.failed} failed`);
1010
+ return results;
1011
+ }
1012
+
1013
+ /**
1014
+ * Bulk retrieve multiple keys
1015
+ * @param {Array<string>} keys - Array of keys to retrieve
1016
+ * @param {Object} options - Retrieval options
1017
+ * @returns {Promise<Object>} Map of key -> value (null for inaccessible keys)
1018
+ */
1019
+ async bulkRetrieve(keys, options = {}) {
1020
+ const results = {};
1021
+
1022
+ const retrievePromises = keys.map(async (key) => {
1023
+ try {
1024
+ const value = await this.retrieve(key, options);
1025
+ results[key] = value;
1026
+ } catch (error) {
1027
+ console.warn(`Failed to retrieve key ${key}:`, error);
1028
+ results[key] = null;
1029
+ }
1030
+ });
1031
+
1032
+ await Promise.allSettled(retrievePromises);
1033
+ return results;
1034
+ }
1035
+
1036
+ /**
1037
+ * Search for keys by pattern or metadata
1038
+ * @param {Object} criteria - Search criteria
1039
+ * @param {string} criteria.keyPattern - Regex pattern to match keys
1040
+ * @param {boolean} criteria.isPublic - Filter by public/private
1041
+ * @param {boolean} criteria.owned - Filter by ownership
1042
+ * @param {string} criteria.owner - Filter by specific owner
1043
+ * @returns {Array<Object>} Matching keys
1044
+ */
1045
+ searchKeys(criteria = {}) {
1046
+ const matches = [];
1047
+
1048
+ for (const [keyId, metadata] of this.storageMetadata.entries()) {
1049
+ let match = true;
1050
+
1051
+ // Check key pattern
1052
+ if (criteria.keyPattern) {
1053
+ const regex = new RegExp(criteria.keyPattern);
1054
+ if (!regex.test(metadata.key)) {
1055
+ match = false;
1056
+ }
1057
+ }
1058
+
1059
+ // Check public/private
1060
+ if (criteria.isPublic !== undefined && metadata.isPublic !== criteria.isPublic) {
1061
+ match = false;
1062
+ }
1063
+
1064
+ // Check ownership
1065
+ if (criteria.owned !== undefined) {
1066
+ const isOwned = this.ownedKeys.has(keyId);
1067
+ if (isOwned !== criteria.owned) {
1068
+ match = false;
1069
+ }
1070
+ }
1071
+
1072
+ // Check specific owner
1073
+ if (criteria.owner && metadata.owner !== criteria.owner) {
1074
+ match = false;
1075
+ }
1076
+
1077
+ if (match) {
1078
+ matches.push({
1079
+ key: metadata.key,
1080
+ keyId,
1081
+ isPublic: metadata.isPublic,
1082
+ isImmutable: metadata.isImmutable,
1083
+ enableCRDT: metadata.enableCRDT,
1084
+ owner: metadata.owner,
1085
+ createdAt: metadata.createdAt,
1086
+ updatedAt: metadata.updatedAt,
1087
+ version: metadata.version,
1088
+ owned: this.ownedKeys.has(keyId)
1089
+ });
1090
+ }
1091
+ }
1092
+
1093
+ return matches;
1094
+ }
1095
+
1096
+ /**
1097
+ * Watch for changes to multiple keys
1098
+ * @param {Array<string>} keys - Keys to watch
1099
+ * @param {Function} callback - Callback function for changes
1100
+ * @returns {Function} Unwatch function
1101
+ */
1102
+ async watchKeys(keys, callback) {
1103
+ const subscriptions = new Set();
1104
+
1105
+ // Subscribe to each key
1106
+ for (const key of keys) {
1107
+ try {
1108
+ await this.subscribe(key);
1109
+ subscriptions.add(key);
1110
+ } catch (error) {
1111
+ console.warn(`Failed to subscribe to key ${key}:`, error);
1112
+ }
1113
+ }
1114
+
1115
+ // Set up event listener
1116
+ const eventHandler = (event) => {
1117
+ if (keys.includes(event.key)) {
1118
+ callback(event);
1119
+ }
1120
+ };
1121
+
1122
+ this.addEventListener('dataUpdated', eventHandler);
1123
+ this.addEventListener('dataDeleted', eventHandler);
1124
+
1125
+ // Return unwatch function
1126
+ return async () => {
1127
+ // Unsubscribe from keys
1128
+ for (const key of subscriptions) {
1129
+ try {
1130
+ await this.unsubscribe(key);
1131
+ } catch (error) {
1132
+ console.warn(`Failed to unsubscribe from key ${key}:`, error);
1133
+ }
1134
+ }
1135
+
1136
+ // Remove event listeners
1137
+ this.removeEventListener('dataUpdated', eventHandler);
1138
+ this.removeEventListener('dataDeleted', eventHandler);
1139
+ };
1140
+ }
1141
+
1142
+ /**
1143
+ * Get detailed information about a key including access control
1144
+ * @param {string} key - The storage key
1145
+ * @returns {Promise<Object|null>} Key information or null if not found
1146
+ */
1147
+ async getKeyInfo(key) {
1148
+ const keyId = await this.webDHT.generateKeyId(key);
1149
+ const metadata = this.storageMetadata.get(keyId);
1150
+
1151
+ if (!metadata) {
1152
+ return null;
1153
+ }
1154
+
1155
+ return {
1156
+ key: metadata.key,
1157
+ keyId,
1158
+ owner: metadata.owner,
1159
+ isPublic: metadata.isPublic,
1160
+ isImmutable: metadata.isImmutable,
1161
+ enableCRDT: metadata.enableCRDT,
1162
+ allowedPeers: metadata.allowedPeers,
1163
+ createdAt: metadata.createdAt,
1164
+ updatedAt: metadata.updatedAt,
1165
+ version: metadata.version,
1166
+ ttl: metadata.ttl,
1167
+ owned: this.ownedKeys.has(keyId),
1168
+ accessible: this.hasReadAccess(keyId, metadata),
1169
+ crdtEnabled: this.crdtStates.has(keyId)
1170
+ };
1171
+ }
1172
+
1173
+ /**
1174
+ * Enable the distributed storage layer
1175
+ * @returns {Promise<void>}
1176
+ */
1177
+ async enable() {
1178
+ if (!this.webDHT) {
1179
+ throw new Error('WebDHT not available - ensure it is enabled in mesh configuration');
1180
+ }
1181
+
1182
+ this.enabled = true;
1183
+ console.log('📦 Distributed storage enabled');
1184
+ this.emit('storageEnabled');
1185
+ }
1186
+
1187
+ /**
1188
+ * Disable the distributed storage layer
1189
+ * @returns {Promise<void>}
1190
+ */
1191
+ async disable() {
1192
+ this.enabled = false;
1193
+ console.log('📦 Distributed storage disabled');
1194
+ this.emit('storageDisabled');
1195
+ }
1196
+
1197
+ /**
1198
+ * Check if the distributed storage layer is enabled
1199
+ * @returns {boolean} Whether storage is enabled
1200
+ */
1201
+ isEnabled() {
1202
+ return this.enabled;
1203
+ }
1204
+
1205
+ /**
1206
+ * Clear all stored data owned by this peer
1207
+ * @returns {Promise<void>}
1208
+ */
1209
+ async clear() {
1210
+ const ownedKeys = Array.from(this.ownedKeys);
1211
+
1212
+ for (const keyId of ownedKeys) {
1213
+ const metadata = this.storageMetadata.get(keyId);
1214
+ if (metadata) {
1215
+ try {
1216
+ await this.delete(metadata.key);
1217
+ } catch (error) {
1218
+ console.warn(`Failed to delete key ${metadata.key} during clear:`, error);
1219
+ }
1220
+ }
1221
+ }
1222
+
1223
+ // Clear local state
1224
+ this.storageMetadata.clear();
1225
+ this.accessControl.clear();
1226
+ this.crdtStates.clear();
1227
+ this.ownedKeys.clear();
1228
+
1229
+ console.log('📦 All stored data cleared');
1230
+ this.emit('storageCleared');
1231
+ }
1232
+
1233
+ /**
1234
+ * List keys with optional prefix filter
1235
+ * @param {string} prefix - Optional prefix to filter keys
1236
+ * @returns {Promise<Array<string>>} Array of matching keys
1237
+ */
1238
+ async listKeys(prefix = '') {
1239
+ const keys = [];
1240
+
1241
+ for (const keyId of this.ownedKeys) {
1242
+ const metadata = this.storageMetadata.get(keyId);
1243
+ if (metadata && metadata.key.startsWith(prefix)) {
1244
+ keys.push(metadata.key);
1245
+ }
1246
+ }
1247
+
1248
+ return keys.sort();
1249
+ }
1250
+
1251
+ /**
1252
+ * Bulk delete keys with a given prefix
1253
+ * @param {string} prefix - Prefix of keys to delete
1254
+ * @returns {Promise<number>} Number of keys deleted
1255
+ */
1256
+ async bulkDelete(prefix) {
1257
+ const keysToDelete = await this.listKeys(prefix);
1258
+ let deletedCount = 0;
1259
+
1260
+ for (const key of keysToDelete) {
1261
+ try {
1262
+ await this.delete(key);
1263
+ deletedCount++;
1264
+ } catch (error) {
1265
+ console.warn(`Failed to delete key ${key} during bulk delete:`, error);
1266
+ }
1267
+ }
1268
+
1269
+ return deletedCount;
1270
+ }
1271
+
1272
+ /**
1273
+ * Search for data by key, value, or metadata
1274
+ * @param {string} query - Search query
1275
+ * @param {string} type - Search type: 'key', 'value', or 'metadata'
1276
+ * @returns {Promise<Array<Object>>} Search results
1277
+ */
1278
+ async search(query, type = 'key') {
1279
+ const results = [];
1280
+ const searchRegex = new RegExp(query, 'i'); // Case-insensitive search
1281
+
1282
+ for (const keyId of this.ownedKeys) {
1283
+ const metadata = this.storageMetadata.get(keyId);
1284
+ if (!metadata) continue;
1285
+
1286
+ let match = false;
1287
+
1288
+ if (type === 'key' && searchRegex.test(metadata.key)) {
1289
+ match = true;
1290
+ } else if (type === 'value') {
1291
+ try {
1292
+ const value = await this.retrieve(metadata.key);
1293
+ if (value && searchRegex.test(JSON.stringify(value))) {
1294
+ match = true;
1295
+ }
1296
+ } catch (error) {
1297
+ // Skip values that can't be retrieved
1298
+ }
1299
+ } else if (type === 'metadata') {
1300
+ const metadataStr = JSON.stringify({
1301
+ owner: metadata.owner,
1302
+ isPublic: metadata.isPublic,
1303
+ isImmutable: metadata.isImmutable,
1304
+ enableCRDT: metadata.enableCRDT
1305
+ });
1306
+ if (searchRegex.test(metadataStr)) {
1307
+ match = true;
1308
+ }
1309
+ }
1310
+
1311
+ if (match) {
1312
+ try {
1313
+ const value = await this.retrieve(metadata.key);
1314
+ results.push({
1315
+ key: metadata.key,
1316
+ value,
1317
+ metadata: {
1318
+ owner: metadata.owner,
1319
+ isPublic: metadata.isPublic,
1320
+ isImmutable: metadata.isImmutable,
1321
+ enableCRDT: metadata.enableCRDT,
1322
+ createdAt: metadata.createdAt,
1323
+ updatedAt: metadata.updatedAt
1324
+ }
1325
+ });
1326
+ } catch (error) {
1327
+ // Skip keys that can't be retrieved
1328
+ }
1329
+ }
1330
+ }
1331
+
1332
+ return results;
1333
+ }
1334
+ }