44 */
55
66import { DatedError , MigrationError } from "./Errors.ts" ;
7- import type { DataStoreEngine } from "./DataStoreEngine.ts" ;
7+ import type { DataStoreEngine , DataStoreEngineDSOptions } from "./DataStoreEngine.ts" ;
88import type { LooseUnion , Prettify , SerializableVal } from "./types.ts" ;
99import { compress , decompress } from "./crypto.ts" ;
1010
@@ -180,20 +180,11 @@ export class DataStore<TData extends DataStoreData> {
180180 this . migrations = opts . migrations ;
181181 if ( opts . migrateIds )
182182 this . migrateIds = Array . isArray ( opts . migrateIds ) ? opts . migrateIds : [ opts . migrateIds ] ;
183- this . encodeData = opts . encodeData ;
184- this . decodeData = opts . decodeData ;
185183 this . engine = typeof opts . engine === "function" ? opts . engine ( ) : opts . engine ;
186184
187185 this . options = opts ;
188186
189- if ( typeof opts . compressionFormat === "undefined" )
190- this . compressionFormat = opts . compressionFormat = opts . encodeData ?. [ 0 ] as CompressionFormat | undefined ?? "deflate-raw" ;
191-
192- if ( typeof opts . compressionFormat === "string" ) {
193- this . encodeData = [ opts . compressionFormat , async ( data : string ) => await compress ( data , opts . compressionFormat ! , "string" ) ] ;
194- this . decodeData = [ opts . compressionFormat , async ( data : string ) => await decompress ( data , opts . compressionFormat ! , "string" ) ] ;
195- }
196- else if ( "encodeData" in opts && "decodeData" in opts && Array . isArray ( opts . encodeData ) && Array . isArray ( opts . decodeData ) ) {
187+ if ( "encodeData" in opts && "decodeData" in opts && Array . isArray ( opts . encodeData ) && Array . isArray ( opts . decodeData ) ) {
197188 this . encodeData = [ opts . encodeData ! [ 0 ] , opts . encodeData ! [ 1 ] ] ;
198189 this . decodeData = [ opts . decodeData ! [ 0 ] , opts . decodeData ! [ 1 ] ] ;
199190 this . compressionFormat = opts . encodeData [ 0 ] as CompressionFormat ?? null ;
@@ -203,10 +194,18 @@ export class DataStore<TData extends DataStoreData> {
203194 this . decodeData = undefined ;
204195 this . compressionFormat = null ;
205196 }
206- else
207- throw new TypeError ( "Either `compressionFormat` or `encodeData` and `decodeData` have to be set and valid, but not all three at a time. Please refer to the documentation for more info." ) ;
197+ else {
198+ const fmt = typeof opts . compressionFormat === "string" ? opts . compressionFormat : "deflate-raw" ;
199+ this . compressionFormat = fmt ;
200+ this . encodeData = [ fmt , async ( data : string ) => await compress ( data , fmt , "string" ) ] ;
201+ this . decodeData = [ fmt , async ( data : string ) => await decompress ( data , fmt , "string" ) ] ;
202+ }
208203
209- this . engine . setDataStoreOptions ( opts ) ;
204+ this . engine . setDataStoreOptions ( {
205+ id : this . id ,
206+ encodeData : this . encodeData ,
207+ decodeData : this . decodeData ,
208+ } as DataStoreEngineDSOptions < TData > ) ;
210209 }
211210
212211 //#region loadData
@@ -278,7 +277,7 @@ export class DataStore<TData extends DataStoreData> {
278277
279278 // check if the data is encoded
280279 const encodingFmt = String ( await this . engine . getValue ( `__ds-${ this . id } -enf` , null ) ) ;
281- const isEncoded = encodingFmt !== "null" && encodingFmt !== "false" ;
280+ const isEncoded = encodingFmt !== "null" && encodingFmt !== "false" && encodingFmt !== "0" && encodingFmt !== "" && encodingFmt !== null ;
282281
283282 // if no format version is found, save the current one
284283 let saveData = false ;
@@ -416,7 +415,7 @@ export class DataStore<TData extends DataStoreData> {
416415 }
417416
418417 await Promise . allSettled ( [
419- this . engine . setValue ( `__ds-${ this . id } -dat` , await this . engine . serializeData ( newData as TData ) ) ,
418+ this . engine . setValue ( `__ds-${ this . id } -dat` , await this . engine . serializeData ( newData as TData , this . encodingEnabled ( ) ) ) ,
420419 this . engine . setValue ( `__ds-${ this . id } -ver` , lastFmtVer ) ,
421420 this . engine . setValue ( `__ds-${ this . id } -enf` , this . compressionFormat ) ,
422421 ] ) ;
@@ -450,7 +449,7 @@ export class DataStore<TData extends DataStoreData> {
450449
451450 const parsed = await this . engine . deserializeData ( data , isEncoded ) as TData ;
452451 await Promise . allSettled ( [
453- this . engine . setValue ( `__ds-${ this . id } -dat` , await this . engine . serializeData ( parsed ) ) ,
452+ this . engine . setValue ( `__ds-${ this . id } -dat` , await this . engine . serializeData ( parsed , this . encodingEnabled ( ) ) ) ,
454453 this . engine . setValue ( `__ds-${ this . id } -ver` , fmtVer ) ,
455454 this . engine . setValue ( `__ds-${ this . id } -enf` , this . compressionFormat ) ,
456455 this . engine . deleteValue ( `__ds-${ id } -dat` ) ,
0 commit comments