|
1 | 1 | # react-native-nitro-fs |
2 | 2 |
|
| 3 | + |
| 4 | + |
| 5 | +A high-performance file system module for React Native that handles file operations and transfers with native speed. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- 💾 File system operations (read, write, copy, delete) |
| 10 | +- 📊 Directory management |
| 11 | +- ⬆️ File uploads with progress tracking |
| 12 | +- ⬇️ File downloads with progress tracking |
| 13 | +- 🔎 File existence and stat checking |
| 14 | +- ⚡ Native performance with Swift and Kotlin implementations |
| 15 | +- 📲 Cross-platform support (iOS and Android) |
| 16 | + |
3 | 17 | ## Requirements |
4 | 18 |
|
5 | | -- React Native v0.76.0 or higher |
| 19 | +- React Native v0.78.0 or higher |
6 | 20 | - Node 18.0.0 or higher |
7 | 21 |
|
8 | 22 | > [!IMPORTANT] |
|
14 | 28 | bun add react-native-nitro-fs react-native-nitro-modules |
15 | 29 | ``` |
16 | 30 |
|
17 | | -## Credits |
| 31 | +## Quick Start |
18 | 32 |
|
19 | | -Bootstrapped with [create-nitro-module](https://github.com/patrickkabwe/create-nitro-module). |
| 33 | +```typescript |
| 34 | +import { NitroFS } from 'react-native-nitro-fs' |
| 35 | + |
| 36 | +// Check if a file exists |
| 37 | +const exists = await NitroFS.exists('/path/to/file') |
| 38 | + |
| 39 | +// Read a file |
| 40 | +const content = await NitroFS.readFile('/path/to/file', 'utf8') |
| 41 | + |
| 42 | +// Write to a file |
| 43 | +await NitroFS.writeFile('/path/to/file', 'Hello, World!', 'utf8') |
| 44 | + |
| 45 | +// Download a file |
| 46 | +const file = await NitroFS.downloadFile( |
| 47 | + 'https://example.com/file.txt', |
| 48 | + 'file.txt', |
| 49 | + NitroFS.DOWNLOAD_DIR + '/file.txt', |
| 50 | + (downloadedBytes, totalBytes) => { |
| 51 | + console.log(`Downloading ${(downloadedBytes / totalBytes) * 100}%`) |
| 52 | + } |
| 53 | +) |
| 54 | +``` |
| 55 | + |
| 56 | +## API Reference |
| 57 | + |
| 58 | +### Constants |
| 59 | + |
| 60 | +The module provides several directory constants: |
| 61 | + |
| 62 | +- `BUNDLE_DIR`: Directory for storing bundle files |
| 63 | +- `DOCUMENT_DIR`: Directory for storing documents |
| 64 | +- `CACHE_DIR`: Directory for storing cache |
| 65 | +- `DOWNLOAD_DIR`: Directory for storing downloads |
| 66 | + |
| 67 | +### Methods |
| 68 | + |
| 69 | +#### `exists(path: string): Promise<boolean>` |
| 70 | + |
| 71 | +Checks if a file or directory exists at the specified path. |
| 72 | + |
| 73 | +```typescript |
| 74 | +const exists = await NitroFS.exists('/path/to/file') |
| 75 | +``` |
| 76 | + |
| 77 | +#### `writeFile(path: string, data: string, encoding: NitroFileEncoding): Promise<void>` |
| 78 | + |
| 79 | +Writes data to a file at the specified path. |
| 80 | + |
| 81 | +```typescript |
| 82 | +await NitroFS.writeFile('/path/to/file', 'Hello, world!', 'utf8') |
| 83 | +``` |
| 84 | + |
| 85 | +#### `readFile(path: string, encoding: NitroFileEncoding): Promise<string>` |
| 86 | + |
| 87 | +Reads the contents of a file at the specified path. |
| 88 | + |
| 89 | +```typescript |
| 90 | +const data = await NitroFS.readFile('/path/to/file', 'utf8') |
| 91 | +``` |
| 92 | + |
| 93 | +#### `copyFile(srcPath: string, destPath: string): Promise<void>` |
| 94 | + |
| 95 | +Copies a file from source path to destination path. |
| 96 | + |
| 97 | +```typescript |
| 98 | +await NitroFS.copyFile('/path/to/file', '/path/to/destination') |
| 99 | +``` |
| 100 | + |
| 101 | +#### `copy(srcPath: string, destPath: string): Promise<void>` |
| 102 | + |
| 103 | +Copies a file or directory from source path to destination path. |
| 104 | + |
| 105 | +```typescript |
| 106 | +await NitroFS.copy('/path/to/file', '/path/to/destination') |
| 107 | +``` |
| 108 | + |
| 109 | +#### `unlink(path: string): Promise<boolean>` |
| 110 | + |
| 111 | +Deletes a file or directory from the file system. |
| 112 | + |
| 113 | +```typescript |
| 114 | +await NitroFS.unlink('/path/to/file') |
| 115 | +``` |
| 116 | + |
| 117 | +#### `mkdir(path: string): Promise<boolean>` |
| 118 | + |
| 119 | +Creates a directory in the file system. |
| 120 | + |
| 121 | +```typescript |
| 122 | +await NitroFS.mkdir('/path/to/directory') |
| 123 | +``` |
| 124 | + |
| 125 | +#### `stat(path: string): Promise<NitroFileStat>` |
| 126 | + |
| 127 | +Gets the stat information of a file or directory. |
| 128 | + |
| 129 | +```typescript |
| 130 | +const stat = await NitroFS.stat('/path/to/file') |
| 131 | +``` |
| 132 | + |
| 133 | +#### `uploadFile(file: NitroFile, uploadOptions: NitroUploadOptions, onProgress?: (uploadedBytes: number, totalBytes: number) => void): Promise<void>` |
| 134 | + |
| 135 | +Uploads a file to a server with progress tracking. |
| 136 | + |
| 137 | +```typescript |
| 138 | +const options: NitroUploadOptions = { |
| 139 | + file: { |
| 140 | + name: 'test.txt', |
| 141 | + mimeType: 'text/plain', |
| 142 | + path: 'test.txt', |
| 143 | + }, |
| 144 | + url: 'https://example.com/upload', |
| 145 | + headers: { |
| 146 | + 'X-Filename': 'test.txt', |
| 147 | + }, |
| 148 | +} |
| 149 | + |
| 150 | +await NitroFS.uploadFile(options, (uploadedBytes, totalBytes) => { |
| 151 | + console.log(`Uploading ${(uploadedBytes / totalBytes) * 100}%`) |
| 152 | +}) |
| 153 | +``` |
| 154 | + |
| 155 | +#### `downloadFile(serverUrl: string, fileName: string, destinationPath: string, onProgress?: (downloadedBytes: number, totalBytes: number) => void): Promise<NitroFile>` |
| 156 | + |
| 157 | +Downloads a file from a server to the specified destination path with progress tracking. |
| 158 | + |
| 159 | +```typescript |
| 160 | +const serverUrl = 'https://example.com/download' |
| 161 | +const fileName = 'file.txt' |
| 162 | +const destinationPath = NitroFS.DOWNLOAD_DIR + '/file.txt' |
| 163 | + |
| 164 | +const file = await NitroFS.downloadFile( |
| 165 | + serverUrl, |
| 166 | + fileName, |
| 167 | + destinationPath, |
| 168 | + (downloadedBytes, totalBytes) => { |
| 169 | + console.log(`Downloading ${(downloadedBytes / totalBytes) * 100}%`) |
| 170 | + } |
| 171 | +) |
| 172 | +``` |
| 173 | + |
| 174 | +### Types |
| 175 | + |
| 176 | +#### `NitroFile` |
| 177 | + |
| 178 | +```typescript |
| 179 | +interface NitroFile { |
| 180 | + name: string |
| 181 | + mimeType: string |
| 182 | + path: string |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +#### `NitroUploadOptions` |
| 187 | + |
| 188 | +```typescript |
| 189 | +interface NitroUploadOptions { |
| 190 | + file: NitroFile |
| 191 | + url: string |
| 192 | + headers?: Record<string, string> |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +#### `NitroFileStat` |
| 197 | + |
| 198 | +Contains file/directory statistics information. |
| 199 | + |
| 200 | +#### `NitroFileEncoding` |
| 201 | + |
| 202 | +Type for file encoding options (e.g., 'utf8'). |
20 | 203 |
|
21 | 204 | ## Contributing |
22 | 205 |
|
23 | 206 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 207 | + |
| 208 | +## Credits |
| 209 | + |
| 210 | +Bootstrapped with [create-nitro-module](https://github.com/patrickkabwe/create-nitro-module). |
| 211 | + |
| 212 | +## License |
| 213 | + |
| 214 | +[MIT](LICENSE) |
0 commit comments