-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathobject-storage.mjs
1616 lines (1446 loc) · 51.1 KB
/
object-storage.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let currentStorage = null;
//const sack = require( "sack.vfs" );
//console.log( "sack?", sack );
export default function( sack ) {
const _debug = false;
const _debug_dangling = false;
const _debug_output = _debug || false;
const _debug_object_convert = _debug || false;
const _debug_ll = false; // remote receive message logging.
const _debug_map = false;
const _debug_replace = false;
if( "undefined" === typeof log )
{
const os = require( "os" );
//const os = require( "os" );
sack.SaltyRNG.setSigningThreads( os.cpus().length );
}
// save original object.
const _objectStorage = sack.ObjectStorage;
const nativeVol = sack.Volume();
const path = import.meta?.url?.replace(/file\:\/\/\//, '' ).split("/") || ".";
//console.log( "path?", path );
//const path = import.meta.url.replace(/file\:\/\/\//, '' ).split("/");
const root = path;// __dirname;//(path.splice(path.length-1,1),path.join('/')+"/");
const remoteExtensionsSrc = nativeVol.read( root+"/object-storage-remote.js" );
//const remoteExtensionsSrc = nativeVol.read( __dirname+"/object-storage-remote.js" );
const remoteExtensions = remoteExtensionsSrc?remoteExtensionsSrc.toString():"// No COde Found";
const jsonRemoteExtensions = JSON.stringify( remoteExtensions );
function shortId( s ) {
return sack.Id( s );
}
let allDangling = new Map();
let dangling = [];
let objectRefs = 0;
let currentContainer = null;
let preloadStorage = null; // storage thrown from main thread
const updatedPrototypes = new WeakMap();
// ---- This block of variables is for parsing JSOX object and foriegn reference revivals during map
const fullObjectMaps = new WeakMap();
let rootContainer = false;
let rootObjectSet = false;
let rootObjectContainer = null;
//let mapFullObject = false;
// ------ end foreign reference tracking ^^^
let currentReadId ;
let loading = null; // mulitple requests for getRoot stack here until the first is resolved....
// these are used during store events because they are delay-flushed the same object may get re-written.
const pendingStore = [];
let lastStore = 0;
const storedPromise = Promise.resolve(undefined);
let mapping = false;
// default options used when map is not passed options.
const defaultMapOpts = {depth:0};
class DbStorage {
// this is a super simple key value store in a (postgresql) database.
// some support to fall back to sqlite - but will require manually specifying options
// need to update personality detection in C library.
#db = null;
#psql = false;
#maria = false;
#mysql = false;
#sqlite = false;
constructor(db, opts ) {
console.trace( "Options:", db, db.provider, opts );
this.#db = db;
if( db.provider === 3 )
this.#psql = true;
else if( db.provider === 2 )
this.#mysql = true;
else if( db.provider === 5 )
this.#maria = true; // really is mariadb
if( db.provider === 1 )
this.#sqlite = true;
{
if( this.#psql ) {
const table1 = "create table if not exists os (id char(45) primary key,value varchar(4096),updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)";
db.makeTable( table1 );
} if( this.#maria || this.#mysql ) {
const table1 = "create table if not exists os (id char(45) primary key,value LONGTEXT,updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)";
db.makeTable( table1 );
} else if( !this.#sqlite ) {
db.makeTable( "create table os (id char(45) primary key,value char,updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)" );
}
}
}
read( obj, ...args ) {
let version = 0;
let parser = null;
let cb = null;
//console.log( "Read in object storage interface? should it have been a file?", obj );
for( let arg of args ) {
if( "function" === typeof arg ) {
cb = arg;
} else if( "object" === typeof arg ) {
parser = arg;
} else if( "number" === typeof arg ) {
version = arg;
}
}
const records = this.#db.do( "select value from os where id=?", obj );
//console.log( "Thing?", obj, records );
if( records.length ) {
const o = parser.parse( records[0].value );
cb( o );
} else cb( undefined );
}
writeRaw( opts, obj ) {
//console.log( "Write Raw:", opts, obj );
if( this.#psql )
this.#db.do( "insert into os (id,value)values(?,?) ON CONFLICT (id) DO UPDATE SET value=?", opts.id, obj,obj );
else if( this.#mysql ) {
this.#db.do( "insert into os (id,value)values(?,?) ON CONFLICT (id) DO UPDATE SET value=?", opts.id, obj,obj );
}
else if( this.#maria ) {
try {
this.#db.do( "insert into os (id,value)values(?,?) ON DUPLICATE KEY UPDATE value=?", opts.id, obj, obj );
//this.#db.do( "insert into os (id,value)values(?,?) ON CONFLICT (id) DO UPDATE SET value=?", opts.id, obj, obj );
} catch( err ) {
console.log( "Insert error:", err );
}
} else
this.#db.do( "insert into os (id,value)values(?,?) ON CONFLICT (id) DO UPDATE SET value=excluded.value", opts.id, obj );
}
}
// manufacture a JS interface to _objectStorage.
class ObjectStorage {
cached = new Map();
cachedContainer = new Map();
stored = new WeakMap(); // objects which have alredy been through put()/get()
storedIn = new WeakMap(); // objects which have alredy been through put()/get()
decoding = [];
pending = [];
stringifier = sack.JSOX.stringifier();
parser = null; // this will be filled when .get() is called.
currentParser = null;
root = null; // this gets filled when the root file system is enabled.
encoders = [];
decoders = [];
constructor (...args) {
if( !( this instanceof ObjectStorage ) ) return new ObjectStorage( args );
const arg0 = args[0];
if( arg0 instanceof sack.Sqlite ) {
_debug && console.log( "Using a db storage interface..." );
preloadStorage = new DbStorage( arg0 );
}else
_debug && console.log( "Using a file interface?" );
const newStorage = preloadStorage || _objectStorage(...args);
if( newStorage === preloadStorage ) preloadStorage = null;
const thisStorage = this;
this.storage = newStorage;
setupStringifier( this, this.stringifier );
this.handleMessage = handleMessage;
this.StoredObject = StoredObject;
function handleMessage( ws, msg ) {
_debug_ll && console.log( "Storage Remote Message:", msg );
try {
if( msg.op === "connect" ) {
ws.send( `{op:connected,code:${jsonRemoteExtensions}}` );
return true;
}
if( msg.op === "get" ) {
newStorage.readRaw( currentReadId = msg.opts.id
, (data)=>{
const msgout = newStorage.stringifier.stringify( { op:"GET", id:msg.id, data:data } );
//console.log( "Read ID:", msg.opts.id, typeof data, msgout );
ws.send( msgout );
} )
return true;
}
if( msg.op === "put" ) {
// want to get back a file ID if possible...
// and/or use the data for encoding/salting/etc... which can determine the result ID.
//console.log( "PUT THIGN:", msg.id );
newStorage.storage.writeRaw( msg.rid, msg.data);
ws.send( { op:"PUT", id:msg.id, r:msg.rid } );
return true;
}
}catch(err) { console.log( "Failed?", err ) }
return false;
}
//console.log( "constructed storage..." );
}
setupStringifier(s) { return setupStringifier( this, s ); }
async map( o, opts ) {
//
// options = {
// depth : maximum distance to load...
// }
//
//thisStorage.mapping = true;
//this.parser.
if( !opts ) opts = defaultMapOpts;
//if( opts && opts.depth === 0 ) return;
//console.log( "External API invoked map...");
const rootId = this.stored.get( o );
if( !rootId ) { console.trace( "Object was not stored, cannot map", o ); return Promise.resolve( o ); }
if( rootId[0] === '~' ) {
//console.log( "this is mapping a directly referenced field; which means we have to resolve this at least.", rootId );
const loading = this.storedIn.get( o );
//console.log( "Get result:", loading );
return loadPending( this, loading );
}
//console.trace( "cached container on root ID check", rootId );
return this.cachedContainer.get( rootId ).map( opts );
}
async getRoot() {
console.log( "Getting root...!!!!" );
const this_ = this;
if( this_.root ) return this_.root;
if( loading ) {
return new Promise( (res,rej)=>{
loading.push( {res:res, rej:rej} );
} );
}
console.log( "Getting root..." );
this_.addEncoders( [ { tag: "d", p:FileDirectory, f:null}, { tag: "f", p:FileEntry, f:null} ] );
this_.addDecoders( [ {tag: "d", p:FileDirectory }, {tag: "f", p:FileEntry } ] )
const result = new FileDirectory( this, "?" );
//console.trace( "Who gets this promise for getting root?");
return this_.get( { id:result.id } )
.then( (dir)=>{
if( !dir ) {
return result.store(true)
.then( function(id){
//console.log( "1) Assigning ID to directory", id );
Object.defineProperty( result, "id", { value:id } );
finishLoad( result );
return dir;
} )
}
// foreach file, set file.folder
else{
for( var file of dir.files ) {
//console.log( "File:", dir, file );
Object.defineProperty( file, "folder", {value:dir} );
}
Object.defineProperty( dir, "id", { value:result.id } );
finishLoad(dir);
}
function finishLoad(dir) {
//console.log( "2) Assigning ID to directory(sub)", result.id );
Object.defineProperty( dir, "volume", {value:this_} );
this_.root = dir;
}
return dir;
} );
}
async dir() {
return (await this.getRoot).files;
}
// define a class... to be handled by stringification
defineClasss (a,b) {
this.stringifier.defineClass(a,b);
}
scan( from ) {
var fromTime = ( from.getTime() * 256 );
//this.loadSince( fromTime );
}
getContainerRef( obj, options ) {
var containerId = this.stored.get( obj );
if( containerId ) {
const container = this.storedIn.get( obj );
//console.log( "ID:", containerId, container );
return container;
}
return null;
}
getContainer( obj, options ) {
var container = this.stored.get( obj );
var storage;
if( container ) {
container = this.cachedContainer.get( container );
return container;
}
//console.trace( "Getting a new container...", options );
container = new ObjectStorageContainer(this,obj,options);
//console.log( " *** SETTING ID HERE", container.id);
//if( container.id === undefined ) throw new Error( "Error along path of setting container ID");
if( container.id ) {
this.stored.set( obj, container.id );
this.cached.set( container.id, container.data );
console.log( "Container storage gets updated here ---------------------------");
container.storage = this;
this.cachedContainer.set( container.id, container );
}else {
// should fix 'undefined' as a index in stored
//console.log( "Storage still has to come from real storage operation... not setting cached ID");
console.log( "Container storage NOT gets updated here ---------------------------");
}
}
delete( opts ) {
return this.storage.delete(opts);
}
remove( opts ) {
if( "string" === typeof opts ) {
var container = this.cached.get( opts );
if( !container ) throw new Error( "This is not a tracked object." );
this.delete( opts );
}
else if( "object" === typeof opts ) {
var container = this.stored.get( opts );
if( !container ) throw new Error( "This is not a tracked object." );
this.delete( container);
}
}
addEncoders(encoderList) {
const this_ = this;
encoderList.forEach( f=>{
this_.encoders.push( f );
//console.log( "encoders:", encoderList );
this_.stringifier.registerToJSOX( f.tag, f.p, f.f ) ;
});
}
addDecoders(encoderList) {
const this_ = this;
encoderList.forEach( f=>{
function redirect(f) {
return function redirect(field,val) {
// call loaded anyway. loaded can't change the object.
if( !field && ( this instanceof StoredObject ) ) this.loaded( this_, currentReadId );
// call registered handler; handler can change what this object is.
if(f) {
if( !field ) {
return f.call( this, field, this_ );
} else return f.call(this,field,val );
}else {
if( !field ) return this;
else return val;
}
}
}
f.f = redirect(f.f);
this_.decoders.push( f );
if( this.parser )
this.parser.fromJSOX( f.tag, f.p, f.f );
});
}
getCurrentParseRef() {
if( this.currentParser ){
return this.currentParser.currentRef();
}
return null;
}
stringify( obj ) {
const containerId = this.stored.get( obj );
if( containerId ) {
const container = this.cachedContainer.get( containerId );
const stringifier = this.stringifier;
if( container ) {
container.encoding = true;
const storage = stringifier.stringify( container );
container.encoding = false;
return storage;
}
}
const output = stringifier.stringify( container );
//console.log( "stringify default:", output, container ) ;
return output;
}
// this hides the original 'put'
put( obj, opts ) {
const this_ = this;
if( currentContainer && currentContainer.data === this ) {
saveObject( null, null );
_debug && console.log( "Returning same id; queued save to background...")
return Promise.resolve( currentContainer.id );
}else {
}
return new Promise( saveObject );
function saveObject(res,rej) {
if( "string" === typeof obj && opts.id ) {
console.log( "SAVING A STRING OBJECT" );
// this isn't cached on this side.
// we don't know the real object.
this_.storage.writeRaw( opts.id, obj );
return res?res( opts.id ):null;
}
var container = this_.stored.get( obj );
if( !container && opts && opts.id ) {
const oldObj = this_.cached.get( opts.id );
if( oldObj )
container = this_.stored.get( oldObj );
}
let storage;
_debug && console.log( "Put found object?", container, obj, opts );
if( container ) {
container = this_.cachedContainer.get( container );
if( obj !== container.data ) {
//console.log( "Overwrite old data with new?", container.data, obj );
if( val === this ){
console.trace( "This is so bad(3)...");
process.exit(0);
}
container.data = obj;
}
if( !container.nonce ) {
// make sure every item that is in an index
// has been written...
if( this_.def )
this_.def.indexes.forEach( index=>{
index.data.forEach( (item)=>{
this_.put( item );
});
})
var stringifier;
if( opts && opts.extraEncoders ) {
stringifier = sack.JSOX.stringifier();
this_.setupStringifier( stringifier );
opts.extraEncoders.forEach( f=>{
stringifier.registerToJSOX( f.tag, f.p, f.f )
});
}else {
stringifier = this_.stringifier;
}
container.encoding = true;
//console.log( "Setting root container mode....(object already contained)");
rootContainer = true;
storage = stringifier.stringify( container );
container.encoding = false;
if( !container.id || container.id === "null" ) {
console.trace( "0) Container has no ID or is nUll", container );
}
_debug_output && console.trace( "WRite:", container.id, storage );
this_.storage.writeRaw( { id:container.id, version:true }, storage );
return res?res( container.id ):null;
} else {
throw new Error( "record is signed, cannot put" );
}
}
if( opts && opts.id ) {
var stringifier;
if( opts.extraEncoders ) {
stringifier = sack.JSOX.stringifier();
this_.setupStringifier( stringifier );
opts.extraEncoders.forEach( f=>{
stringifier.registerToJSOX( f.tag, f.p, f.f )
});
}else {
stringifier = this_.stringifier;
}
// raw object; no encoding to set.
console.log( "Set rootObjectSet(1)....");
//rootObjectSet = true;
storage = stringifier.stringify( obj );
if( !opts.id || opts.id === "null" ) {
throw new Error( "Container has no ID or is null" );
}
_debug_output && console.trace( "Write(root set):", opts, storage );
if( this_.versioned ) {
const id = opts.id.split( '.' );
//console.log( "Storing with an ID already?", obj, opts );
const newVersion = this_.storage.writeRaw( {id:id[0]}, storage );
opts.id = id[0] + '.' + newVersion;
} else
this_.storage.writeRaw( opts, storage );
res && res( opts.id );
} else if( !opts || !opts.id ) {
// need a new ID for an object (not string)
_debug && console.log( "New bare object, create a container...", opts );
if( !opts ) opts = { id : shortId() }
else opts.id = shortId();
//console.log( "Storage ID is picked here:", opts );
if( "object" === typeof obj ) {
container = new ObjectStorageContainer(this_,obj,opts);
if( !container.id || "string" !== typeof container.id ) throw new Error( "Failed to get a conatiner ID for some reason" );
//console.log( "New container looks like... ", container.id, container );
//console.log( "saving stored container.id", typeof obj, obj, container.id );
//this_.stored.delete( obj );
//console.log( " *** SETTING ID HERE", container.id);
this_.stored.set( obj, container.id );
if( container.id === undefined ) throw new Error( "Error along path of setting container ID");
this_.cached.set( container.id, container.data );
//console.log( "3CACHE IS SET HERE ------------------------- ", obj)
this_.cachedContainer.set( container.id, container );
var stringifier;
if( opts.extraEncoders ) {
stringifier = sack.JSOX.stringifier();
this_.setupStringifier( stringifier );
opts.extraEncoders.forEach( f=>{
stringifier.registerToJSOX( f.tag, f.p, f.f )
});
}else {
stringifier = this_.stringifier;
}
opts.id = container.id
container.encoding = true;
console.log( "Setting root container mode(2)....");
rootContainer = true;
storage = stringifier.stringify( container );
container.encoding = false;
} else {
container = new ObjectStorageContainer(this_,obj,opts);
storage = obj;
}
if( !container.id || container.id === "null" ) {
console.trace( "Container has no ID or is nUll", container );
}
_debug_output && console.log( "Output container to storage... ", container, container.storage, container.id );
try {
this_.storage.writeRaw( opts, storage );
if( container.id === undefined ) throw new Error( "Error along path of setting container ID");
this_.cached.set( container.id, container.data );
//console.log( "2CACHE IS SET HERE ------------------------- ", obj)
this_.cachedContainer.set( container.id, container );
}catch(err) { console.log( "WRITE RAW?", this_ )}
//console.log( "OUTPUT:", storage );
res && res( container.id );
}
}
/*
sack.ObjectStorage.prototype.update( objId, obj ) {
var container = new ObjectStorageContainer(this,sack.JSOX.stringify(obj),sign);
this.stored.set( obj, container.id );
this.cached.set( container.id, container );
return container.id;
}
*/
} // end of put() method
get( opts ) {
//this.parser.
let resolve;
//let reject;
const os = this;
if( "string" === typeof opts ) {
opts = { id:opts
, extraDecoders : null };
}
if( !opts ){
console.trace( "Must specify options ", opts);
return null;
}
{
const priorLoad = this.cachedContainer.get( opts.id );
//console.log( "already found?", priorLoad );
if( priorLoad ) return Promise.resolve( priorLoad.data );
}
//console.trace( "TEST going to get:", opts )
const priorDecode = this.decoding.find( d=>d.opts.id === opts.id );
if( priorDecode ){
//console.trace( "already decoding...(use same promised result) things?", priorDecode );
return priorDecode.p;
}
if( !this.parser ){
this.parser = sack.JSOX.begin();
//console.log( "ADDING ~os");
this.parser.fromJSOX( "~os", ObjectStorageContainer, reviveContainer ); // I don't know ahead of time which this is.
this.parser.fromJSOX( "~or", objectStorageContainerRef, reviveContainerRef ); // I don't know ahead of time which this is.
for( let f of this.decoders )
this.parser.fromJSOX( f.tag, f.p, f.f );
}
const parser = this.parser;
if( opts.extraDecoders ) {
parser = sack.JSOX.begin( );
//console.log( "Adding ~os handler");
parser.fromJSOX( "~os", ObjectStorageContainer, reviveContainer ); // I don't know ahead of time which this is.
parser.fromJSOX( "~or", objectStorageContainerRef, reviveContainerRef ); // I don't know ahead of time which this is.
//console.log( "this has no decoders? ", this );
if( this.decoders && this.decoders.length )
this.decoders.forEach( f=>parser.fromJSOX( f.tag, f.p, f.f ) );
// allow extra to override default.
if( opts && opts.extraDecoders && opts.extraDecoders.length ) {
opts.extraDecoders.forEach( f=>parser.fromJSOX( f.tag, f.p, f.f ) );
}
//console.log( "Created a parser for revival..." );
}
this.currentParser = parser;
//console.log( "(get)Read Key:", opts.id );
const p = new Promise( function(res,rej) {
resolve = res; //reject = rej;
const priorReadId = currentReadId;
try {
currentStorage = os; // this part is synchronous... but leaves JS heap from os.read(), does callbacks using parser, but results with a parsed object
// all synchronously.
const parts = opts.id.split('.');
os.storage.read( currentReadId = parts[0], Number( parts[1] )
, parser, (obj)=>resultDecode(opts, priorReadId, obj,res) );
}catch(err) {
console.log( "ERROR:", err );
currentReadId = priorReadId;
rej(err);
}
} );
//console.log( "PUSHING THING TO LOAD LATER (decoding)?", opts );
this.decoding.push( { p:p, opts:opts } );
p.then( (r)=>{
//console.log( "Removing decoding object with then...", r);
const doneDecoding = os.decoding.findIndex( d=>d.opts === opts );
//console.log( "decode should already be resolved:", doneDecoding, os.decoding, opts, r );
if( doneDecoding >=0 ) os.decoding.splice( doneDecoding, 1 );
else console.log( "Failed to find decoding object?" );
return r;
} );
return p;
function resultDecode( opts,priorReadId, obj,resolve) {
// with a new parser, only a partial decode before revive again...
try {
_debug && console.log( "Read resulted with an object:", opts.id, obj );
if( obj instanceof ObjectStorageContainer ) {
//console.log( "SHOULD HAVE FIXED IT ALREDY!");
obj.storage = os;
}
let deleteId = -1;
currentStorage = null;
//const extraResolutions = [];
for( let n = 0; n < os.decoding.length; n++ ) {
const decode = os.decoding[n];
//console.log( "pending decode?", decode, opts, decode.opts === opts )
if( decode.opts === opts )
deleteId = n;
else if( decode.opts.id === opts.id ) {
console.log( "pending decode resolve?");
decode.res( obj );
}
}
if( deleteId >= 0 ) {
console.log( "Something else is alwso waiting for this result...", os.decoding, deleteId);
os.decoding.splice( deleteId, 1 );
}
var found;
do {
var found = os.pending.findIndex( pending=>{
console.log( "what is in pending?", key, opts.id, pending );
return pending.id === opts.id } );
if( found >= 0 ) {
os.pending[found].ref.o[os.pending[found].ref.f] = obj.data;
os.pending.splice( found, 1 );
}
} while( found >= 0 );
//console.log( "Object is what?", obj );
if( obj && ( obj instanceof ObjectStorageContainer ) ){
//console.log( "GOTzz:", obj, obj.id, obj.data );
if( !("id" in obj ))
Object.defineProperty( obj, "id", { value:currentReadId } );
//console.log( "Why does object have no data?", obj.data, obj );
os.stored.set( obj.data, obj.id );
os.cachedContainer.set( obj.id, obj );
currentReadId = priorReadId;
//for( let res in extraResolutions ) res.res(obj.data);
resolve(obj.data);
} else {
currentReadId = priorReadId;
//for( let res in extraResolutions ) res.res(obj);
//console.log( "RESOLVE WITH OBJECT NEED DATA?", ( obj instanceof ObjectStorageContainer ) );
resolve(obj)
}
}catch( err) {
console.log( "Less fatal error:", err );
}
}
function objectStorageContainerRef( s ) {
//_debug_dangling &&
try {
const existing = os.cachedContainer.get(s);
const here = os.getCurrentParseRef();
const thisDangling = dangling;
//console.log( "Conainer ref, this will resolve in-place")
this.d = {id:s,p:null,res:null,rej:null,r:{o:here.o,f:here.f},rootMap:null };
if( !existing ) {
_debug_dangling && console.log( "PUSHING DANGLING REFERNCE", this );
const requests = allDangling.get( this.d.id ) || [];
if( !requests.length ) allDangling.set( this.d.id, requests );
const p = this.d.p = new Promise( (res,rej)=>{
_debug_dangling && console.log( "setting up pending promise to resolve:", s );
this.d.res = res;
this.d.rej = rej;
}).then( (obj)=>{
_debug_dangling && console.log( "replace value at reference with real value:", here, obj, thisDangling.length, s )
//const dr = thisDangling.findIndex( d=> d.d.id === s );
//if( dr >= 0 ) thisDangling.splice( dr, 1 );
//else _debug_dangling && console.log( "FAILED TO FIND DANGLING REFERENCE 1" );
// this is the last time the promise exists...
os.storedIn.delete( p );
os.stored.delete( p );
const inObject = dangling.find( d=>d === this );
if(inObject >= 0 ) {
_debug_replace && console.log( "Removing request from container dangling" );
dangling.spice( inObject, 1 );
}
_debug_replace && console.log( "-------- REPLACE VALUE HERE:", this.d.r.o[this.d.r.f], this.d.r.o, this.d.r.f, obj );
return (this.d.r.o[this.d.r.f] = obj);
}).catch( (err)=>{
console.log( "CATCH UNCAUGHT PLEASE DO", err);
});
os.storedIn.set( this.d.p, this );
//console.log( " *** SETTING ID HERE", s);
os.stored.set( this.d.p, '~or"'+s+'"' );
requests.push( this );
dangling.push( this );
objectRefs++;
} else {
_debug_dangling && console.log( "NOT DANGLING; existing object already exists... " );
this.d.p = Promise.resolve( existing.data ); // this will still have to be swapped.
}
//console.log( "Container ref:", s, this.d.p );
} catch(err) { console.log( "Init ObjectContainerRef failed:", err)}
}
function reviveContainer( field, val ) {
//console.log( "(JS)Revive container:", this, field, val );
if( !field ) {
// finished.
if( objectRefs ) {
/* sets dangling property on container */
_debug_dangling && console.log( "Collapse dangling:", dangling.length );
if( !this.dangling )
Object.defineProperty( this, "dangling", { value:dangling } );
else{
this.dangling.push.apply(this.dangling, dangling )
//console.log( "Added more to the dangling things..." );
}
// resets to store new objects for next load.
dangling = [];
objectRefs = 0;
}
Object.defineProperty( this, "id", { value:currentReadId } );
//console.log( "handle object ??", this );
if( this.nonce ) {
let s = null;
var v = sack.SaltyRNG.verify( s = os.stringifier.stringify(this.data), this.nonce, this.sign.pad1, this.sign.pad2 );
if( v.key !== currentReadId ) {
throw new Error( `Data corrupted: s:'${s}' v:${v}, this Id:${currentReadId}` );
}
else console.log( "Valid record.", v, this.sign );
}
const request = allDangling.get( currentReadId );
//console.log( "Revive container final pass... does this resolve?", this, request, allDangling, currentReadId );
if( request ) {
_debug_replace && console.log( "Dispatching all resolutions waiting on this object..." );
allDangling.delete( currentReadId );
for( let load of request ) {
load.d.res( this.data );
}
}
return this;
}
else {
const this_ = this;
// new value isn't anything special; just set the value.
//console.log( "This sort of thing... val is just a thing - like a key part identifier...; but that should have been a container.");
if( val instanceof Promise ) {
//_debug_dangling && console.log( "Value is a promise, and needs resolution.")
var dangle = dangling.find( d=>d.d.p===val );
if( dangle )
dangle.d.n = field;
this_.data[field] = val
return val.then( (val)=>{
_debug_dangling && console.log( "(DOESN'T HAPPEN NOW?) THIS SHOULD BE WHAT REPLACES THE VALUE", field, val );
this_.data[field] = val
});
}
// a custom type might want something else...
if( field === "data" )
{
//console.log( "This should have fixed the 'pending value' value" );
if( val === this ){
console.trace( "This is so bad...");
process.exit(0);
}
this.data = val;
return undefined;
}
this[field] = val;
return undefined;
}
}
function reviveContainerRef( field, val ) {
//console.trace( "Revival of a container reference:", this, field, val );
if( !field ) {
const existing = os.cachedContainer.get( this.d.id );
if( existing ){
//console.log( "duplicate reference... return the existing promise(but this is still used later)", existing, this.d.id);
// even better, don't even store the reference, return the real
//console.log( "So, just return with the real object to assign. (and remove fom dangling)");
const id = dangling.find( d=>d.d === this.d );
objectRefs--;
if( id >= 0 ) dangling.splice( id, 1 );
//console.log( "this is the real value anyway1...", existing.data );
return existing.data;
}
os.stored.set( this.d.p, '~or"'+this.d.id+'"' );
//console.log( "this is the real value anyway2...", this.d.p );
return this.d.p;
}
else {
//console.log( "this is the real value anyway3...", val );
return val;
}
}
} // end of get() method
flush() {
// this doesn't really cache anything to flush?
}
} // end of ObjectStorage class
function loadPending(store, load, opts) {
//console.log( "doing real get, which results with ANOTHER promise", load, store );
return store.get( {id:load.d.id}).then( (obj)=>{
//console.log( "Storage requested:", load.d.id );
if( load.d.res ) {
load.d.res(obj); // result with real value.
} else {
load.d.p.then( o2=>{if( obj!==o2) throw new Error( "resolved and loaded object mismatch");return o2})
return load.d.p;
}
//const existobj = thisStorage.stored.get( load.d.p );
//const exist = thisStorage.stored.get( obj );
//console.log( "This is the promise object?", load.d.id, exist, existobj );
const objc = store.cachedContainer.get( load.d.id );
_debug_dangling && console.log( "Replaced promise with container", objc, obj );
// resolving this promis on load.d will set this.
if( opts && opts.depth ) {
_debug_replace && console.log( "Mapping the object, if there is any more" );
objc.map( {depth:opts.depth-1} ).then( (objc)=>{
_debug_replace && console.log( "mapped sub-object resultinged...", objc );
load.d.waiting--;
if( !load.d.waiting ) {
_debug_dangling && console.log( "1map is resolving with : ", objc, load.d.rootMap.data );
_debug_replace && console.log( "Resolving map request...", load.d.rootMap.data );
}else console.log( "waiting for something..." );
});
} else {
_debug_replace && console.log( "waiting for something, not resolving" );
}
//console.log( "This is just an object - from a then callback" );
return obj;
}).catch( (err)=>{
console.log( "Error:", err );
})
}
function objectToJSOX( stringifier, storage, obj ){
_debug_object_convert && console.log( "Convert Any Object to JSOX:", typeof obj, obj );
if( rootContainer ) {
//console.log( "know the next thing is is a new container, get the data..." );
rootContainer = false;
//rootObject = obj.data;
rootObjectContainer = obj;
fullObjectMaps.set( rootObjectContainer, obj.id );
}else {
if( rootObjectSet ) {
//console.log( "know the next thing is is a new container, get the data..." );
//const newId = shortId();
console.trace( "setting root container ID to ?", rootObjectSet, rootContainer, rootObjectContainer
, "\nbecomes", obj );
//rootObject = obj;
rootObjectContainer = obj;
// I don't know this has to be in this map... it's a
// un-named part of an object to encode, it might have a path
// reference, but that's not what this is for.
//fullObjectMaps.set( rootObjectContainer, newId );
}
//else
// console.log( "Sub Object of something...", this !== rootObjectContainer );
}
if( false /*mapFullObject*/ )
if( obj !== rootObjectContainer ) {
//const curMap = fullObjectMaps.get( rootObjectContainer );
const ref = stringifier.getReference( obj );
console.log( "(full map)saving internal reference:", ref, obj );
fullObjectMaps.set( obj, ref );
}
//const isRoot = (rootObjectContainer === obj);
if( _debug_object_convert )
if(obj instanceof Promise ) {
console.log( "This is still a pending object reference(?)", obj );
}
// see if we alread stored this... (or are currently storing this.) (back references container)
//console.log( "this?", obj, rootObjectContainer );
var exist = !rootObjectSet && rootObjectContainer && storage.stored.get( obj );
//console.log( "Exist after promise?", exist );
_debug_object_convert && console.log( "THIS GOT CALLED?", obj, Object.getPrototypeOf( obj ), exist );
if( exist ) {
//console.trace( "Is it this sort of exist?", exist );
if( exist[0] == '~' ) {
// this is a dangling promise
return exist;
}
var objContainer = storage.cachedContainer.get( exist );
_debug_object_convert && console.log( "Object stored independantly... RECOVERED:", exist, obj&&obj.encoding );
_debug_object_convert && console.log( "existing reference...", exist );
//console.log( "Cached container:", objContainer.encoding, obj )
if( objContainer.encoding )
return obj;
else {
return '~or"'+exist+'"';
}
}
else {
if( rootObjectContainer !== obj ) {
exist = fullObjectMaps.get( obj );
if( exist ) {
// sometimes it's a storage container, other times it's a raw object...