@@ -3,7 +3,6 @@ import { base64ToBytes, getRandomSeed } from '../client/seed';
33import { decompress } from '../compression/bz2_decompress' ;
44import { bloomLookup } from '../data/bloom' ;
55import {
6- DataWithMetadata ,
76 concatBytes ,
87 deserialize ,
98 deserializeChunks ,
@@ -40,7 +39,7 @@ export class Bucket {
4039 readonly api : Api ;
4140
4241 /** The name of this bucket. */
43- readonly name : string ;
42+ name : string ;
4443
4544 /**
4645 * The secret seed for this instance of the client, which can be saved and
@@ -110,7 +109,7 @@ export class Bucket {
110109 try {
111110 decompressedResult = decompress ( decryptedResult ) ;
112111 } catch ( e ) {
113- console . error ( 'decompress error' , e ) ;
112+ console . error ( `key ${ key } not found (decompression failed)` ) ;
114113 }
115114 if ( decompressedResult === null ) {
116115 return null ;
@@ -120,7 +119,7 @@ export class Bucket {
120119 try {
121120 extractedResult = this . lib . extractResult ( key , decompressedResult ) ;
122121 } catch ( e ) {
123- console . error ( 'extraction error' , e ) ;
122+ console . error ( `key ${ key } not found (extraction failed)` ) ;
124123 }
125124 if ( extractedResult === null ) {
126125 return null ;
@@ -151,7 +150,7 @@ export class Bucket {
151150
152151 private async performPrivateReads (
153152 keys : string [ ]
154- ) : Promise < DataWithMetadata [ ] > {
153+ ) : Promise < any [ ] > {
155154 if ( ! this . uuid || ! this . check ( this . uuid ) ) {
156155 await this . setup ( ) ;
157156 }
@@ -185,7 +184,7 @@ export class Bucket {
185184 return endResults ;
186185 }
187186
188- private async performPrivateRead ( key : string ) : Promise < DataWithMetadata > {
187+ private async performPrivateRead ( key : string ) : Promise < any > {
189188 return ( await this . performPrivateReads ( [ key ] ) ) [ 0 ] ;
190189 }
191190
@@ -320,6 +319,15 @@ export class Bucket {
320319 return await this . api . meta ( this . name ) ;
321320 }
322321
322+ /** Renames this bucket, leaving all data and other bucket settings intact. */
323+ async rename ( newBucketName : string ) : Promise < BucketMetadata > {
324+ const bucketCreateReq = {
325+ name : newBucketName
326+ } ;
327+ await this . api . modify ( this . name , JSON . stringify ( bucketCreateReq ) ) ;
328+ this . name = newBucketName ;
329+ }
330+
323331 /** Gets info on all keys in this bucket. */
324332 async listKeys ( ) : Promise < KeyInfo [ ] > {
325333 this . ensureSpiral ( ) ;
@@ -333,32 +341,28 @@ export class Bucket {
333341 * key-value pairs to write. Keys must be strings, and values may be any
334342 * JSON-serializable value or a Uint8Array. The maximum size of a key is
335343 * 1024 UTF-8 bytes.
336- * @param {{ [key: string]: any } } [metadata] - An optional object containing
337- * metadata. Each key of this object should also be a key of
338- * `keyValuePairs`, and the value should be some metadata object to store
339- * with the values being written.
340344 */
341345 async write (
342- keyValuePairs : { [ key : string ] : any } ,
343- metadata ?: { [ key : string ] : any }
346+ keyValuePairs : { [ key : string ] : any }
344347 ) {
345348 this . ensureSpiral ( ) ;
346349
347350 const data = [ ] ;
348351 for ( const key in keyValuePairs ) {
349352 if ( Object . prototype . hasOwnProperty . call ( keyValuePairs , key ) ) {
350353 const value = keyValuePairs [ key ] ;
351- let valueMetadata = undefined ;
352- if ( metadata && Object . prototype . hasOwnProperty . call ( metadata , key ) ) {
353- valueMetadata = metadata [ key ] ;
354- }
355- const valueBytes = serialize ( value , valueMetadata ) ;
354+ const valueBytes = serialize ( value ) ;
356355 const keyBytes = new TextEncoder ( ) . encode ( key ) ;
357356 const serializedKeyValue = wrapKeyValue ( keyBytes , valueBytes ) ;
358357 data . push ( serializedKeyValue ) ;
358+ // const kv = {
359+ // key: key,
360+ // value: Buffer.from(valueBytes).toString('base64')
361+ // }
359362 }
360363 }
361364 const concatenatedData = concatBytes ( data ) ;
365+ // const concatenatedData = serialize(data);
362366 await this . api . write ( this . name , concatenatedData ) ;
363367 }
364368
@@ -385,6 +389,14 @@ export class Bucket {
385389 await this . api . destroy ( this . name ) ;
386390 }
387391
392+ /**
393+ * Clears the contents of the entire bucket, and all data inside of it. This action is
394+ * permanent and irreversible.
395+ */
396+ async clearEntireBucket ( ) {
397+ await this . api . clear ( this . name ) ;
398+ }
399+
388400 /**
389401 * Privately reads the supplied key from the bucket, returning the value
390402 * corresponding to the key.
@@ -398,28 +410,13 @@ export class Bucket {
398410 this . ensureSpiral ( ) ;
399411
400412 if ( Array . isArray ( key ) ) {
401- return ( await this . performPrivateReads ( key ) ) . map ( r => r . data ) ;
413+ return ( await this . performPrivateReads ( key ) ) ;
402414 } else {
403415 const result = await this . performPrivateRead ( key ) ;
404- return result ? result . data : null ;
416+ return result ? result : null ;
405417 }
406418 }
407419
408- /**
409- * Privately reads the supplied key from the bucket, returning the value and
410- * metadata corresponding to the key.
411- *
412- * No entity, including the Blyss service, should be able to determine which
413- * key this method was called for.
414- *
415- * @param {string } key - The key to _privately_ retrieve the value of.
416- */
417- async privateReadWithMetadata ( key : string ) : Promise < DataWithMetadata > {
418- this . ensureSpiral ( ) ;
419-
420- return await this . performPrivateRead ( key ) ;
421- }
422-
423420 /**
424421 * Privately intersects the given set of keys with the keys in this bucket,
425422 * returning the keys that intersected and their values. This is generally
@@ -437,7 +434,7 @@ export class Bucket {
437434 this . ensureSpiral ( ) ;
438435
439436 if ( keys . length < BLOOM_CUTOFF ) {
440- return ( await this . performPrivateReads ( keys ) ) . map ( x => x . data ) ;
437+ return ( await this . performPrivateReads ( keys ) ) ;
441438 }
442439
443440 const bloomFilter = await this . api . bloom ( this . name ) ;
@@ -451,7 +448,7 @@ export class Bucket {
451448 if ( ! retrieveValues ) {
452449 return matches ;
453450 }
454- return ( await this . performPrivateReads ( matches ) ) . map ( x => x . data ) ;
451+ return ( await this . performPrivateReads ( matches ) ) ;
455452 }
456453
457454 /**
0 commit comments