1- import * as FileSystem from 'expo-file-system' ;
1+ import { getInfoAsync , makeDirectoryAsync , deleteAsync , writeAsStringAsync , readAsStringAsync , documentDirectory } from 'expo-file-system' ;
22import { decode as decodeBase64 , encode as encodeBase64 } from 'base64-arraybuffer' ;
33import { AttachmentData , EncodingType , LocalStorageAdapter } from '@powersync/common' ;
44
55/**
6- * ExpoFileSystemAdapter implements LocalStorageAdapter using Expo's FileSystem.
6+ * ExpoFileSystemAdapter implements LocalStorageAdapter using Expo's
77 * Suitable for React Native applications using Expo or Expo modules.
88 */
99export class ExpoFileSystemAdapter implements LocalStorageAdapter {
1010 private storageDirectory : string ;
1111
1212 constructor ( storageDirectory ?: string ) {
1313 // Default to a subdirectory in the document directory
14- this . storageDirectory = storageDirectory ?? `${ FileSystem . documentDirectory } attachments/` ;
14+ this . storageDirectory = storageDirectory ?? `${ documentDirectory } attachments/` ;
1515 }
1616
1717 async initialize ( ) : Promise < void > {
18- const dirInfo = await FileSystem . getInfoAsync ( this . storageDirectory ) ;
18+ const dirInfo = await getInfoAsync ( this . storageDirectory ) ;
1919 if ( ! dirInfo . exists ) {
20- await FileSystem . makeDirectoryAsync ( this . storageDirectory , { intermediates : true } ) ;
20+ await makeDirectoryAsync ( this . storageDirectory , { intermediates : true } ) ;
2121 }
2222 }
2323
2424 async clear ( ) : Promise < void > {
25- const dirInfo = await FileSystem . getInfoAsync ( this . storageDirectory ) ;
25+ const dirInfo = await getInfoAsync ( this . storageDirectory ) ;
2626 if ( dirInfo . exists ) {
27- await FileSystem . deleteAsync ( this . storageDirectory ) ;
27+ await deleteAsync ( this . storageDirectory ) ;
2828 }
2929 }
3030
@@ -42,8 +42,8 @@ export class ExpoFileSystemAdapter implements LocalStorageAdapter {
4242 if ( typeof data === 'string' ) {
4343 // Handle string data (typically base64 or UTF8)
4444 const encoding = options ?. encoding ?? EncodingType . Base64 ;
45- await FileSystem . writeAsStringAsync ( filePath , data , {
46- encoding : encoding === EncodingType . Base64 ? FileSystem . EncodingType . Base64 : FileSystem . EncodingType . UTF8
45+ await writeAsStringAsync ( filePath , data , {
46+ encoding : encoding === EncodingType . Base64 ? EncodingType . Base64 : EncodingType . UTF8
4747 } ) ;
4848
4949 // Calculate size based on encoding
@@ -58,8 +58,8 @@ export class ExpoFileSystemAdapter implements LocalStorageAdapter {
5858 } else {
5959 // Handle ArrayBuffer data
6060 const base64 = encodeBase64 ( data ) ;
61- await FileSystem . writeAsStringAsync ( filePath , base64 , {
62- encoding : FileSystem . EncodingType . Base64
61+ await writeAsStringAsync ( filePath , base64 , {
62+ encoding : EncodingType . Base64
6363 } ) ;
6464 size = data . byteLength ;
6565 }
@@ -71,8 +71,8 @@ export class ExpoFileSystemAdapter implements LocalStorageAdapter {
7171 const encoding = options ?. encoding ?? EncodingType . Base64 ;
7272
7373 // Let the native function throw if file doesn't exist
74- const content = await FileSystem . readAsStringAsync ( filePath , {
75- encoding : encoding === EncodingType . Base64 ? FileSystem . EncodingType . Base64 : FileSystem . EncodingType . UTF8
74+ const content = await readAsStringAsync ( filePath , {
75+ encoding : encoding === EncodingType . Base64 ? EncodingType . Base64 : EncodingType . UTF8
7676 } ) ;
7777
7878 if ( encoding === EncodingType . UTF8 ) {
@@ -86,7 +86,7 @@ export class ExpoFileSystemAdapter implements LocalStorageAdapter {
8686 }
8787
8888 async deleteFile ( filePath : string , options ?: { filename ?: string } ) : Promise < void > {
89- await FileSystem . deleteAsync ( filePath ) . catch ( ( error : any ) => {
89+ await deleteAsync ( filePath ) . catch ( ( error : any ) => {
9090 // Gracefully ignore file not found errors, throw others
9191 if ( error ?. message ?. includes ( 'not exist' ) || error ?. message ?. includes ( 'ENOENT' ) ) {
9292 return ;
@@ -97,18 +97,18 @@ export class ExpoFileSystemAdapter implements LocalStorageAdapter {
9797
9898 async fileExists ( filePath : string ) : Promise < boolean > {
9999 try {
100- const info = await FileSystem . getInfoAsync ( filePath ) ;
100+ const info = await getInfoAsync ( filePath ) ;
101101 return info . exists ;
102102 } catch {
103103 return false ;
104104 }
105105 }
106106
107107 async makeDir ( path : string ) : Promise < void > {
108- await FileSystem . makeDirectoryAsync ( path , { intermediates : true } ) ;
108+ await makeDirectoryAsync ( path , { intermediates : true } ) ;
109109 }
110110
111111 async rmDir ( path : string ) : Promise < void > {
112- await FileSystem . deleteAsync ( path ) ;
112+ await deleteAsync ( path ) ;
113113 }
114114}
0 commit comments