@@ -72,18 +72,20 @@ export class OPFSCoopSyncVFS2 extends FacadeVFS {
7272 unboundAccessHandles = new Set < FileSystemSyncAccessHandle > ( ) ;
7373 accessiblePaths = new Set < string > ( ) ;
7474 releaser : null | ( ( ) => void ) = null ;
75+ private readonly : boolean ;
7576
76- static async create ( name , module ) {
77- const vfs = new OPFSCoopSyncVFS2 ( name , module ) ;
77+ static async create ( name : string , module , readonly : boolean ) {
78+ const vfs = new OPFSCoopSyncVFS2 ( name , module , readonly ) ;
7879 await Promise . all ( [
7980 ( vfs as any ) . isReady ( ) ,
8081 vfs . #initialize( DEFAULT_TEMPORARY_FILES )
8182 ] ) ;
8283 return vfs ;
8384 }
8485
85- constructor ( name , module ) {
86+ constructor ( name : string , module , readonly : boolean ) {
8687 super ( name , module ) ;
88+ this . readonly = readonly ;
8789 }
8890
8991 get #module( ) {
@@ -143,7 +145,7 @@ export class OPFSCoopSyncVFS2 extends FacadeVFS {
143145 for ( let i = 0 ; i < nTemporaryFiles ; i ++ ) {
144146 const tmpFile = await tmpDir . getFileHandle ( `${ i } .tmp` , { create : true } ) ;
145147 const tmpAccessHandle = await ( tmpFile as any ) . createSyncAccessHandle ( {
146- mode : 'readwrite-unsafe '
148+ mode : this . readonly ? 'read-only' : 'readwrite '
147149 } ) ;
148150 this . unboundAccessHandles . add ( tmpAccessHandle ) ;
149151 }
@@ -543,7 +545,9 @@ export class OPFSCoopSyncVFS2 extends FacadeVFS {
543545 if ( subPersistentFile ) {
544546 subPersistentFile . accessHandle = await (
545547 subPersistentFile . fileHandle as any
546- ) . createSyncAccessHandle ( { mode : 'readwrite-unsafe' } ) ;
548+ ) . createSyncAccessHandle ( {
549+ mode : this . readonly ? 'read-only' : 'readwrite'
550+ } ) ;
547551 }
548552 } )
549553 ) ;
@@ -584,14 +588,18 @@ export class OPFSCoopSyncVFS2 extends FacadeVFS {
584588 setTimeout ( notify ) ;
585589
586590 this . log ?.( `lock requested: ${ lockName } ` ) ;
587- navigator . locks . request ( lockName , { mode : 'shared' } , ( lock ) => {
588- // We have the lock. Stop asking other connections for it.
589- this . log ?.( `lock acquired: ${ lockName } ` , lock ) ;
590- clearInterval ( notifyId ) ;
591- return new Promise < ( ) => void > ( ( res ) => {
592- resolve ( res as ( ) => void ) ;
593- } ) ;
594- } ) ;
591+ navigator . locks . request (
592+ lockName ,
593+ { mode : this . readonly ? 'shared' : 'exclusive' } ,
594+ ( lock ) => {
595+ // We have the lock. Stop asking other connections for it.
596+ this . log ?.( `lock acquired: ${ lockName } ` , lock ) ;
597+ clearInterval ( notifyId ) ;
598+ return new Promise < ( ) => void > ( ( res ) => {
599+ resolve ( res as ( ) => void ) ;
600+ } ) ;
601+ }
602+ ) ;
595603 } ) ;
596604 }
597605}
0 commit comments