44 * If it cannot find one, it will prompt the user to select a directory.
55 * @see https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API
66*/
7- import '@types/wicg-file-system-access'
87import { BaseAdaptor , BaseAdaptorOptions } from '../../base' ;
98import { FileStat , PathMap } from '../../definitions' ;
109
@@ -33,10 +32,10 @@ interface FileCacheItem {
3332}
3433
3534export class FSAAdaptor extends BaseAdaptor {
36- declare protected options : FSAAdaptorOptions ;
37- protected root : FileSystemDirectoryHandle | null = null ;
38- protected pathCache : PathCache = { } ;
39- private fileCache : FileCacheItem [ ] = [ ] ;
35+ declare options : FSAAdaptorOptions ;
36+ root : FileSystemDirectoryHandle | null = null ;
37+ pathCache : PathCache = { } ;
38+ fileCache : FileCacheItem [ ] = [ ] ;
4039
4140 get isInitialized ( ) : boolean {
4241 return ! ! this . root ;
@@ -48,7 +47,7 @@ export class FSAAdaptor extends BaseAdaptor {
4847 && typeof window . showDirectoryPicker !== 'undefined' ;
4948 }
5049
51- async #getFile ( handle : FileSystemFileHandle ) : Promise < File > {
50+ async _getFile ( handle : FileSystemFileHandle ) : Promise < File > {
5251 const cached = this . fileCache . find ( item => item . handler === handle ) ;
5352 if ( cached ) return cached . file ;
5453 const file = await handle . getFile ( ) ;
@@ -59,7 +58,7 @@ export class FSAAdaptor extends BaseAdaptor {
5958 return file ;
6059 }
6160
62- #purgeCache ( handleOrPath ?: FileSystemFileHandle | string ) : void {
61+ _purgeCache ( handleOrPath ?: FileSystemFileHandle | string ) : void {
6362 if ( typeof handleOrPath === 'string' ) {
6463 delete this . pathCache [ handleOrPath ] ;
6564 } else if ( handleOrPath ) {
@@ -71,7 +70,7 @@ export class FSAAdaptor extends BaseAdaptor {
7170 }
7271 }
7372
74- #getHandleFromDB ( store : IDBObjectStore ) : Promise < FileSystemDirectoryHandle | null > {
73+ _getHandleFromDB ( store : IDBObjectStore ) : Promise < FileSystemDirectoryHandle | null > {
7574 return new Promise ( ( resolve , reject ) => {
7675 const index = store . index ( 'ref' ) ;
7776 const request = index . get ( this . ref ) ;
@@ -85,7 +84,7 @@ export class FSAAdaptor extends BaseAdaptor {
8584 } ) ;
8685 }
8786
88- #saveHandleToDB ( store : IDBObjectStore , handle : FileSystemDirectoryHandle ) : Promise < void > {
87+ _saveHandleToDB ( store : IDBObjectStore , handle : FileSystemDirectoryHandle ) : Promise < void > {
8988 return new Promise ( ( resolve , reject ) => {
9089 const request = store . put ( {
9190 ref : this . ref ,
@@ -100,7 +99,7 @@ export class FSAAdaptor extends BaseAdaptor {
10099 } ) ;
101100 }
102101
103- #dropHandleFromDB ( store : IDBObjectStore ) : Promise < void > {
102+ _dropHandleFromDB ( store : IDBObjectStore ) : Promise < void > {
104103 return new Promise ( ( resolve , reject ) => {
105104 const request = store . delete ( this . ref ) ;
106105 request . onerror = ( event ) => {
@@ -112,7 +111,7 @@ export class FSAAdaptor extends BaseAdaptor {
112111 } ) ;
113112 }
114113
115- async #verifyHandle ( handle : FileSystemDirectoryHandle ) : Promise < void > {
114+ async _verifyHandle ( handle : FileSystemDirectoryHandle ) : Promise < void > {
116115 const opts = {
117116 mode : 'readwrite' as FileSystemPermissionMode ,
118117 } ;
@@ -151,7 +150,7 @@ export class FSAAdaptor extends BaseAdaptor {
151150 . transaction ( this . options . store || INDEXDB_DEFAULT_STORE , 'readwrite' )
152151 . objectStore ( this . options . store || INDEXDB_DEFAULT_STORE ) ;
153152
154- let handle = await this . #getHandleFromDB ( store ) ;
153+ let handle = await this . _getHandleFromDB ( store ) ;
155154
156155 const startIn = this . options . startIn || 'documents' ;
157156 if ( ! handle ) {
@@ -160,18 +159,18 @@ export class FSAAdaptor extends BaseAdaptor {
160159 mode : 'readwrite' ,
161160 startIn,
162161 } ) ;
163- await this . #saveHandleToDB ( store , handle ) ;
162+ await this . _saveHandleToDB ( store , handle ) ;
164163 } else {
165164 try {
166- await this . #verifyHandle ( handle ) ;
165+ await this . _verifyHandle ( handle ) ;
167166 } catch ( err ) {
168- await this . #dropHandleFromDB ( store ) ;
167+ await this . _dropHandleFromDB ( store ) ;
169168 handle = await window . showDirectoryPicker ( {
170169 id : this . ref ,
171170 mode : 'readwrite' ,
172171 startIn,
173172 } ) ;
174- await this . #saveHandleToDB ( store , handle ) ;
173+ await this . _saveHandleToDB ( store , handle ) ;
175174 }
176175 }
177176
@@ -182,10 +181,10 @@ export class FSAAdaptor extends BaseAdaptor {
182181
183182 async destroy ( ) : Promise < void > {
184183 this . root = null ;
185- this . #purgeCache ( ) ;
184+ this . _purgeCache ( ) ;
186185 }
187186
188- async #getParentHandle ( path : string , create ?: boolean ) : Promise < FileSystemDirectoryHandle > {
187+ async _getParentHandle ( path : string , create ?: boolean ) : Promise < FileSystemDirectoryHandle > {
189188 if ( ! this . isInitialized ) throw new Error ( 'Adaptor not initialized' ) ;
190189 if ( path === '' ) return this . root ! ;
191190 if ( this . pathCache [ path ] ) return this . pathCache [ path ] as FileSystemDirectoryHandle ;
@@ -198,11 +197,11 @@ export class FSAAdaptor extends BaseAdaptor {
198197 return handle ;
199198 }
200199
201- async #getPathHandle ( path : string , create ?: boolean ) : Promise < FileSystemHandle > {
200+ async _getPathHandle ( path : string , create ?: boolean ) : Promise < FileSystemHandle > {
202201 if ( ! this . isInitialized ) throw new Error ( 'Adaptor not initialized' ) ;
203202 if ( path === '' ) return this . root ! ;
204203 if ( this . pathCache [ path ] ) return this . pathCache [ path ] ;
205- const parent = await this . #getParentHandle ( path , create ) ;
204+ const parent = await this . _getParentHandle ( path , create ) ;
206205 const subject = path . split ( '/' ) . pop ( ) ! ;
207206
208207 for await ( const [ key , value ] of parent . entries ( ) ) {
@@ -223,7 +222,7 @@ export class FSAAdaptor extends BaseAdaptor {
223222 throw new Error ( 'Path not found' ) ;
224223 }
225224
226- async #getHandleStat ( path : string , handle : FileSystemHandle ) : Promise < FileStat > {
225+ async _getHandleStat ( path : string , handle : FileSystemHandle ) : Promise < FileStat > {
227226 if ( handle . kind === 'directory' ) {
228227 return {
229228 path,
@@ -233,7 +232,7 @@ export class FSAAdaptor extends BaseAdaptor {
233232 size : 0 ,
234233 } ;
235234 } else if ( handle . kind === 'file' ) {
236- let file = await this . #getFile ( handle as FileSystemFileHandle ) ;
235+ let file = await this . _getFile ( handle as FileSystemFileHandle ) ;
237236 return {
238237 path,
239238 parentPath : path . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ,
@@ -250,8 +249,8 @@ export class FSAAdaptor extends BaseAdaptor {
250249
251250 async stat ( path : string ) : Promise < FileStat | null > {
252251 try {
253- const handle = await this . #getPathHandle ( path ) ;
254- return this . #getHandleStat ( path , handle ) ;
252+ const handle = await this . _getPathHandle ( path ) ;
253+ return this . _getHandleStat ( path , handle ) ;
255254 } catch ( err ) {
256255 if (
257256 ( err as Error ) . name === 'NotFoundError'
@@ -262,31 +261,31 @@ export class FSAAdaptor extends BaseAdaptor {
262261 }
263262
264263 async readFile ( path : string ) : Promise < Buffer > {
265- const handle = await this . #getPathHandle ( path ) ;
264+ const handle = await this . _getPathHandle ( path ) ;
266265 if ( handle . kind !== 'file' ) throw new Error ( 'Not a file' ) ;
267- const file = await this . #getFile ( handle as FileSystemFileHandle ) ;
266+ const file = await this . _getFile ( handle as FileSystemFileHandle ) ;
268267 const buffer = await file . arrayBuffer ( ) ;
269268 return Buffer . from ( buffer ) ;
270269 }
271270
272271 async writeFile ( path : string , data : Buffer ) : Promise < void > {
273- const parent = await this . #getParentHandle ( path ) ;
272+ const parent = await this . _getParentHandle ( path ) ;
274273 const fileName = path . split ( '/' ) . pop ( ) ! ;
275274 const file = await parent . getFileHandle ( fileName , { create : true } ) ;
276275 const writable = await file . createWritable ( ) ;
277276 await writable . write ( data as ArrayBuffer ) ;
278277 await writable . close ( ) ;
279- this . #purgeCache ( file ) ;
278+ this . _purgeCache ( file ) ;
280279 }
281280
282281 async deleteFile ( path : string ) : Promise < void > {
283- const parent = await this . #getParentHandle ( path ) ;
282+ const parent = await this . _getParentHandle ( path ) ;
284283 const fileName = path . split ( '/' ) . pop ( ) ! ;
285284
286285 try {
287286 const handle = await parent . getFileHandle ( fileName ) ;
288- this . #purgeCache ( handle ) ;
289- this . #purgeCache ( path ) ;
287+ this . _purgeCache ( handle ) ;
288+ this . _purgeCache ( path ) ;
290289 } catch ( err ) {
291290 // ignore
292291 }
@@ -295,53 +294,53 @@ export class FSAAdaptor extends BaseAdaptor {
295294 }
296295
297296 async list ( path : string ) : Promise < PathMap > {
298- const handle = await this . #getPathHandle ( path ) as FileSystemDirectoryHandle ;
297+ const handle = await this . _getPathHandle ( path ) as FileSystemDirectoryHandle ;
299298 if ( handle . kind !== 'directory' ) throw new Error ( 'Not a directory' ) ;
300299
301300 const map : PathMap = { } ;
302301 for await ( const [ key , value ] of handle . entries ( ) ) {
303302 const fullPath = [ path , key ] . join ( '/' ) ;
304- map [ fullPath ] = await this . #getHandleStat ( fullPath , value ) ;
303+ map [ fullPath ] = await this . _getHandleStat ( fullPath , value ) ;
305304 }
306305
307306 return map ;
308307 }
309308
310309 async mkdir ( path : string ) : Promise < void > {
311- const parent = await this . #getParentHandle ( path ) ;
310+ const parent = await this . _getParentHandle ( path ) ;
312311 const dirName = path . split ( '/' ) . pop ( ) ! ;
313312 await parent . getDirectoryHandle ( dirName , { create : true } ) ;
314313 }
315314
316315 async rmdir ( path : string ) : Promise < void > {
317- const parent = await this . #getParentHandle ( path ) ;
316+ const parent = await this . _getParentHandle ( path ) ;
318317 const dirName = path . split ( '/' ) . pop ( ) ! ;
319318 await parent . removeEntry ( dirName , { recursive : true } ) ;
320- await this . #purgeCache ( ) ;
319+ await this . _purgeCache ( ) ;
321320 }
322321
323322 async _copyFile (
324323 path : string ,
325324 targetPath : string ,
326325 newFileName ?: string ,
327326 ) : Promise < void > {
328- const source = await this . #getPathHandle ( path ) as FileSystemFileHandle ;
329- const destination = await this . #getPathHandle ( targetPath , true ) as FileSystemDirectoryHandle ;
330- const file = await this . #getFile ( source ) ;
327+ const source = await this . _getPathHandle ( path ) as FileSystemFileHandle ;
328+ const destination = await this . _getPathHandle ( targetPath , true ) as FileSystemDirectoryHandle ;
329+ const file = await this . _getFile ( source ) ;
331330 const newFile = await destination . getFileHandle ( newFileName || source . name , { create : true } ) ;
332331 const writer = await newFile . createWritable ( ) ;
333332 await writer . write ( file ) ;
334333 await writer . close ( ) ;
335- this . #purgeCache ( newFile ) ;
334+ this . _purgeCache ( newFile ) ;
336335 }
337336
338337 async _copyDirectory (
339338 path : string ,
340339 targetPath : string ,
341340 recursive ?: boolean ,
342341 ) : Promise < void > {
343- const source = await this . #getPathHandle ( path ) as FileSystemDirectoryHandle ;
344- await this . #getPathHandle ( targetPath , true ) ;
342+ const source = await this . _getPathHandle ( path ) as FileSystemDirectoryHandle ;
343+ await this . _getPathHandle ( targetPath , true ) ;
345344 for await ( const [ key , value ] of source . entries ( ) ) {
346345 const fullPath = [ path , key ] . join ( '/' ) ;
347346 const fullTargetPath = [ targetPath , key ] . join ( '/' ) ;
@@ -354,12 +353,12 @@ export class FSAAdaptor extends BaseAdaptor {
354353 }
355354
356355 async isDirectory ( path : string ) : Promise < boolean > {
357- const handle = await this . #getPathHandle ( path ) ;
356+ const handle = await this . _getPathHandle ( path ) ;
358357 return handle . kind === 'directory' ;
359358 }
360359
361360 async isFile ( path : string ) : Promise < boolean > {
362- const handle = await this . #getPathHandle ( path ) ;
361+ const handle = await this . _getPathHandle ( path ) ;
363362 return handle . kind === 'file' ;
364363 }
365364}
0 commit comments