@@ -149,6 +149,12 @@ Now let's create 2 commends:
1491491) chkfbdb
1501502) mkfbdb
151151
152+ Now we have the 2 main variables (sid and nextNodeId) as the initialization variables
153+ of a 'NetFsDb' object. So now the idea is to add methods to this object to make
154+ it look like the system 'FsDb' object.
155+
156+
157+
152158»*/
153159/*10/13/25: I want a tidy database object (something like 'FsDb' in sys/fs.js):«
154160
@@ -518,6 +524,135 @@ gh_provider.setCustomParameters({
518524 prompt : 'select_account'
519525} ) ;
520526
527+ let is_connected = false ;
528+ let cur_user ;
529+ let db ;
530+ //let next_node_id;
531+ //let cur_sid;
532+
533+ // Attach a listener to detect changes in the connection state
534+ //When we do tons of dev reloading, this never gets unregistered, and we end up getting tons
535+ //of warnings, upon reconnecting after disconnecting (e.g. due to closing the laptop cover).
536+ //Perhaps we can export an onkill method.
537+
538+ const DISCON_CB = onValue ( ref ( fbase_db , ".info/connected" ) , ( snap ) => {
539+ is_connected = snap . val ( ) ;
540+ cwarn ( `Connected: ${ is_connected } ` ) ;
541+ } ) ;
542+ const AUTH_CB = onAuthStateChanged ( fbase_auth , user => {
543+ cur_user = user ;
544+ if ( ! cur_user ) {
545+ // next_node_id = undefined;
546+ // cur_sid = undefined;
547+ db = undefined ;
548+ }
549+ cwarn ( "User" , cur_user ) ;
550+ } ) ;
551+
552+ const ONKILL = ( ) => {
553+ cwarn ( "KILL" ) ;
554+ AUTH_CB ( ) ;
555+ DISCON_CB ( ) ;
556+ } ;
557+
558+ //»
559+
560+ class NetFsDB { //«
561+
562+ constructor ( sid_arg , next_node_id_arg ) { //«
563+ this . sid = sid_arg ;
564+ this . nextNodeId = next_node_id_arg ;
565+ } //»
566+
567+ async createDirNode ( parId , name ) { //«
568+
569+ if ( ! ( Number . isFinite ( parId ) && isStr ( name ) ) ) {
570+ throw new Error ( `Invalid args to createDirNode` ) ;
571+ }
572+
573+ let path = `${ parId } /${ name } ` ;
574+ let node = { parId, path, type : "d" } ;
575+ this . nextNodeId ++ ;
576+ await UPDATE ( UBASE ( ) , {
577+ next_node_id : {
578+ nodeid : this . nextNodeId + 1 ,
579+ sid : this . sid
580+ } ,
581+ [ `nodes/${ this . nextNodeId } ` ] : {
582+ parId,
583+ path,
584+ sid : this . sid ,
585+ type : "d"
586+ }
587+ } ) ;
588+ return true ;
589+ } //»
590+ async createFileNode ( parId , name , val ) { //«
591+
592+ if ( ! ( Number . isFinite ( parId ) && isStr ( name ) ) ) {
593+ throw new Error ( `Invalid args to createFileNode` ) ;
594+ }
595+
596+ let path = `${ parId } /${ name } ` ;
597+ let use_blob_id ;
598+ if ( ! val ) use_blob_id = 0 ;
599+ else use_blob_id = this . nextNodeId ;
600+
601+ let obj = {
602+ next_node_id : {
603+ nodeid : this . nextNodeId + 1 ,
604+ sid : this . sid
605+ } ,
606+ [ `nodes/${ this . nextNodeId } ` ] : {
607+ parId,
608+ path,
609+ blobId : use_blob_id ,
610+ sid : this . sid ,
611+ type : "f"
612+ }
613+ } ;
614+ if ( val ) {
615+ let bytes ;
616+ if ( isStr ( val ) ) {
617+ bytes = ( new TextEncoder ( ) ) . encode ( val ) ;
618+ }
619+ else if ( val instanceof Uint8Array ) {
620+ bytes = val ;
621+ }
622+ else {
623+ cwarn ( "WHAT THE HELL TYPE IS THIS???" ) ;
624+ log ( val ) ;
625+ throw new Error ( "UNKNOWN FILE VALUE TYPE IN createFileNode!!! (see value above)" ) ;
626+ }
627+ obj [ `blobs/${ this . nextNodeId } ` ] = {
628+ sid : this . sid ,
629+ contents : B64 ( bytes )
630+ }
631+ }
632+
633+ this . nextNodeId ++ ;
634+ await UPDATE ( UBASE ( ) , obj ) ;
635+ return true ;
636+
637+ } //»
638+
639+ } //»
640+
641+ //Funcs«
642+
643+ const UID = ( ) => { if ( cur_user && cur_user . providerData ) return cur_user . providerData [ 0 ] . uid ; } ;
644+ const UBASE = ( ) => { //«
645+ let id = UID ( ) ;
646+ if ( ! id ) return ;
647+ return `LOTW_FS/${ id } ` ;
648+ } ; //»
649+ const SID = ( val ) => { //«
650+ let id = UID ( ) ;
651+ if ( ! id ) return ;
652+ let key = `fbase-sid-gh-${ id } ` ;
653+ if ( ! val ) return localStorage . getItem ( key ) ;
654+ localStorage . setItem ( key , val ) ;
655+ } ; //»
521656const REF = ( path ) => { //«
522657 return ref ( fbase_db , path ) ;
523658// if (path) return ref(fbase_db, path);
@@ -546,60 +681,50 @@ catch(e){
546681}
547682
548683} ; //»
549- const UID = ( ) => {
550- if ( cur_user && cur_user . providerData ) return cur_user . providerData [ 0 ] . uid ;
551- } ;
552- const UBASE = ( ) => {
553- let id = UID ( ) ;
554- if ( ! id ) return ;
555- return `LOTW_FS/${ id } ` ;
556- } ;
557- const SID_KEY = ( ) => {
684+ const B64 = bytes => { //«
685+ if ( bytes . toBase64 ) return bytes . toBase64 ( ) ;
686+ return btoa ( String . fromCharCode ( ...bytes ) ) ;
687+ } ; //»
688+ const FROMB64 = str => { //«
689+ if ( ! Uint8Array . fromBase64 ) {
690+ try {
691+ return Uint8Array . fromBase64 ( str ) ;
692+ }
693+ catch ( e ) {
694+ cerr ( s ) ;
695+ return null ;
696+ }
697+ }
698+ str = atob ( str ) ;
699+ let arr = new Uint8Array ( str . length ) ;
700+ let len = arr . length ;
701+ for ( let i = 0 ; i < len ; i ++ ) {
702+ arr [ i ] = str . charCodeAt ( i ) ;
703+ }
704+ return arr ;
705+
706+ } ; //»
707+
708+ /*
709+ const SID_KEY = ()=>{//«
558710 let id = UID();
559711 if (!id) return;
560712 return `fbase-sid-gh-${id}`;
561- } ;
562-
563- // Attach a listener to detect changes in the connection state
564- let is_connected = false ;
565- //When we do tons of dev reloading, this never gets unregistered, and we end up getting tons
566- //of warnings, upon reconnecting after disconnecting (e.g. due to closing the laptop cover).
567- //Perhaps we can export an onkill method.
568- const DISCON_CB = onValue ( ref ( fbase_db , ".info/connected" ) , ( snap ) => {
569- is_connected = snap . val ( ) ;
570- cwarn ( `Connected: ${ is_connected } ` ) ;
571- } ) ;
572- let cur_user ;
573- const AUTH_CB = onAuthStateChanged ( fbase_auth , user => {
574- cur_user = user ;
575- cwarn ( "User" , cur_user ) ;
576- } ) ;
577-
578- const onkill = ( ) => {
579- cwarn ( "KILL OIMPT!" ) ;
580- AUTH_CB ( ) ;
581- DISCON_CB ( ) ;
582- } ;
583-
584- let next_node_id ;
585- //»
713+ };//»
714+ */
586715
587- //Funcs«
588- const get_session_id = ( ) => {
716+ const get_session_id = ( ) => { //«
589717 let sess_id = ( crypto . getRandomValues ( new Uint8Array ( SESS_ID_BYTE_LEN ) ) ) . toBase64 ( )
590718 . replace ( / \+ / g, '-' )
591719 . replace ( / \x2f / g, '_' )
592720 . replace ( / = + $ / , '' ) ;
593721 return sess_id ;
594- } ;
595- //»
722+ } ; //»
596723
597- /*
598- class NetFsDB {}
599- const db = new NetFsDB();
600- */
724+ //»
601725
602726//Commands«
727+
603728/*«
604729class extends Com{
605730async run(){
@@ -663,13 +788,21 @@ class com_chkfbdb extends Com {//«
663788 let base = UBASE ( ) ;
664789 if ( ! base ) return this . no ( "not signed in!" ) ;
665790 if ( ! is_connected ) return this . no ( "not connected!" ) ;
791+ if ( db ) return this . ok ( "already checked" ) ;
666792 let snap = await GET ( `${ base } /next_node_id/nodeid` ) ;
667793 if ( isErr ( snap ) ) return this . no ( snap . message ) ;
668- next_node_id = snap . val ( ) ;
794+ let next_node_id = snap . val ( ) ;
669795 if ( next_node_id ) {
670- log ( `NNI: ${ next_node_id } ` ) ;
796+ //log(`NNI: ${next_node_id}`);
797+ let cur_sid = SID ( ) ;
798+ if ( ! cur_sid ) {
799+ return this . no ( `cur_sid: not found in local storage, but next_node_id exists` ) ;
800+ }
801+ db = new NetFsDB ( cur_sid , next_node_id ) ;
802+ log ( db ) ;
671803 return this . ok ( "OK" ) ;
672804 }
805+ // if (cur_sid) return this.no(`cur_sid: was found in local storage, but next_node_id doesn't exist`);
673806 this . no ( "NO" ) ;
674807 }
675808} //»
@@ -694,10 +827,16 @@ class com_mkfbdb extends Com {//«
694827 sid : sid // Added: !data.exists()
695828 }
696829 } ) ;
697- next_node_id = 1 ;
830+ // next_node_id = 1;
831+ SID ( sid ) ;
832+ // cur_sid = sid;
833+ /*
698834 let key = SID_KEY();
699835cwarn(`localStorage.setItem(${key}, ${sid})`);
700836 localStorage.setItem(key, sid);
837+ */
838+ db = new NetFsDB ( sid , 1 ) ;
839+ log ( db ) ;
701840 this . ok ( ) ;
702841 }
703842
@@ -714,7 +853,7 @@ mkfbdb: com_mkfbdb,
714853
715854//»
716855
717- export { coms , onkill } ;
856+ export { coms , ONKILL as onkill } ;
718857
719858
720859//Old«
@@ -916,29 +1055,6 @@ const DEF_TRANS_DELAY = 5000;
9161055//incrementCounter('posts/postId123/likes', 1);
9171056//incrementCounter('pages/pageId456/views', -1);
9181057
919- const B64 = bytes =>{//«
920- if (bytes.toBase64) return bytes.toBase64();
921- return btoa(String.fromCharCode(...bytes));
922- };//»
923- const FROMB64 = str => {//«
924- if (!Uint8Array.fromBase64){
925- try{
926- return Uint8Array.fromBase64(str);
927- }
928- catch(e){
929- cerr(s);
930- return null;
931- }
932- }
933- str = atob(str);
934- let arr = new Uint8Array(str.length);
935- let len = arr.length;
936- for (let i = 0; i < len; i++) {
937- arr[i] = str.charCodeAt(i);
938- }
939- return arr;
940-
941- };//»
9421058
9431059const encode_key=(key) => {//«
9441060 const encoder = new TextEncoder();
0 commit comments