@a16n/web-sim 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/bin/cli.ts +255 -0
- package/bin/web-sim.js +39 -0
- package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
- package/overrides/@react-native-firebase/app/overrides.ts +34 -0
- package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
- package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
- package/overrides/expo-application/ExpoApplication.ts +81 -0
- package/overrides/expo-application/overrides.ts +16 -0
- package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
- package/overrides/expo-background-task/overrides.ts +9 -0
- package/overrides/expo-constants/ExponentConstants.ts +66 -0
- package/overrides/expo-constants/overrides.ts +9 -0
- package/overrides/expo-device/ExpoDevice.ts +104 -0
- package/overrides/expo-device/overrides.ts +9 -0
- package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
- package/overrides/expo-glass-effect/GlassView.tsx +95 -0
- package/overrides/expo-glass-effect/availability.ts +14 -0
- package/overrides/expo-glass-effect/overrides.ts +14 -0
- package/overrides/expo-haptics/Haptics.ts +67 -0
- package/overrides/expo-haptics/overrides.ts +5 -0
- package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
- package/overrides/expo-mail-composer/overrides.ts +9 -0
- package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
- package/overrides/expo-mesh-gradient/overrides.ts +9 -0
- package/overrides/expo-modules-core/overrides.ts +12 -0
- package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
- package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
- package/overrides/expo-navigation-bar/overrides.ts +12 -0
- package/overrides/expo-router/NativeStackView.tsx +74 -0
- package/overrides/expo-router/NativeTabsView.tsx +263 -0
- package/overrides/expo-router/SafeAreaProvider.ts +6 -0
- package/overrides/expo-router/overrides.ts +23 -0
- package/overrides/expo-router/statusBarRequests.ts +51 -0
- package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
- package/overrides/expo-screen-orientation/overrides.ts +21 -0
- package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
- package/overrides/expo-sharing/overrides.ts +9 -0
- package/overrides/expo-sms/ExpoSMS.ts +24 -0
- package/overrides/expo-sms/overrides.ts +9 -0
- package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
- package/overrides/expo-splash-screen/overrides.ts +5 -0
- package/overrides/expo-status-bar/StatusBar.tsx +44 -0
- package/overrides/expo-status-bar/overrides.ts +5 -0
- package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
- package/overrides/expo-store-review/overrides.ts +9 -0
- package/overrides/expo-symbols/SymbolView.tsx +192 -0
- package/overrides/expo-symbols/overrides.ts +9 -0
- package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
- package/overrides/expo-system-ui/overrides.ts +9 -0
- package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
- package/overrides/expo-task-manager/overrides.ts +9 -0
- package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
- package/overrides/expo-tracking-transparency/overrides.ts +11 -0
- package/overrides/expo-video/ExpoVideo.ts +16 -0
- package/overrides/expo-video/overrides.ts +9 -0
- package/overrides/lottie-react-native/LottieView.tsx +87 -0
- package/overrides/lottie-react-native/overrides.ts +9 -0
- package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
- package/overrides/react-native-pager-view/overrides.ts +9 -0
- package/overrides/react-native-reanimated/overrides.ts +28 -0
- package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
- package/overrides/react-native-safe-area-context/overrides.ts +12 -0
- package/overrides/react-native-share/NativeRNShare.ts +22 -0
- package/overrides/react-native-share/overrides.ts +12 -0
- package/overrides/react-native-share/platform.ts +14 -0
- package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
- package/overrides/react-native-turbo-image/overrides.ts +9 -0
- package/overrides/react-native-web/RefreshControl.tsx +57 -0
- package/overrides/react-native-web/overrides.ts +153 -0
- package/overrides/react-native-web/useElementLayout.ts +140 -0
- package/package.json +78 -4
- package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
- package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
- package/plugins/battery/plugin.ts +8 -0
- package/plugins/battery/shell.tsx +56 -0
- package/plugins/battery/state.ts +11 -0
- package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
- package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
- package/plugins/biometrics/plugin.ts +8 -0
- package/plugins/biometrics/shell.tsx +88 -0
- package/plugins/biometrics/state.ts +41 -0
- package/plugins/brightness/app.ts +5 -0
- package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
- package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
- package/plugins/brightness/plugin.ts +9 -0
- package/plugins/brightness/shell.tsx +55 -0
- package/plugins/brightness/state.ts +19 -0
- package/plugins/calendar/calendar.ts +299 -0
- package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
- package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
- package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
- package/plugins/calendar/plugin.ts +8 -0
- package/plugins/calendar/shell.tsx +123 -0
- package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
- package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
- package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
- package/plugins/camera/plugin.ts +9 -0
- package/plugins/camera/shell.tsx +107 -0
- package/plugins/camera/state.ts +61 -0
- package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
- package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
- package/plugins/clipboard/plugin.ts +8 -0
- package/plugins/clipboard/shell.tsx +122 -0
- package/plugins/clipboard/state.ts +93 -0
- package/plugins/clock/app.ts +25 -0
- package/plugins/clock/plugin.ts +8 -0
- package/plugins/clock/shell.tsx +225 -0
- package/plugins/clock/state.ts +12 -0
- package/plugins/contacts/contacts.ts +125 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
- package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
- package/plugins/contacts/plugin.ts +8 -0
- package/plugins/contacts/shell.tsx +109 -0
- package/plugins/home-indicator/app.ts +5 -0
- package/plugins/home-indicator/plugin.ts +9 -0
- package/plugins/home-indicator/requests.ts +48 -0
- package/plugins/home-indicator/shell.tsx +68 -0
- package/plugins/home-indicator/state.ts +31 -0
- package/plugins/keep-awake/app.ts +5 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
- package/plugins/keep-awake/plugin.ts +9 -0
- package/plugins/keep-awake/shell.tsx +80 -0
- package/plugins/keep-awake/state.ts +12 -0
- package/plugins/keyboard/Keyboard.tsx +354 -0
- package/plugins/keyboard/app.ts +90 -0
- package/plugins/keyboard/controller.ts +384 -0
- package/plugins/keyboard/fields.ts +244 -0
- package/plugins/keyboard/icons.tsx +283 -0
- package/plugins/keyboard/keyboard.css +362 -0
- package/plugins/keyboard/layouts/android.ts +175 -0
- package/plugins/keyboard/layouts/index.ts +10 -0
- package/plugins/keyboard/layouts/ios.ts +219 -0
- package/plugins/keyboard/layouts/types.ts +80 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
- package/plugins/keyboard/plugin.ts +13 -0
- package/plugins/keyboard/shell.tsx +133 -0
- package/plugins/keyboard/state.ts +92 -0
- package/plugins/keyboard/suggestions.ts +63 -0
- package/plugins/keyboard/taps.ts +77 -0
- package/plugins/keyboard/typing.ts +112 -0
- package/plugins/locale/app.ts +232 -0
- package/plugins/locale/locale.ts +143 -0
- package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
- package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
- package/plugins/locale/plugin.ts +9 -0
- package/plugins/locale/shell.tsx +147 -0
- package/plugins/locale/state.ts +36 -0
- package/plugins/location/geo.ts +56 -0
- package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
- package/plugins/location/overrides/expo-location/overrides.ts +11 -0
- package/plugins/location/places.ts +38 -0
- package/plugins/location/plugin.ts +8 -0
- package/plugins/location/shell.tsx +188 -0
- package/plugins/location/state.ts +51 -0
- package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
- package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
- package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
- package/plugins/motion/plugin.ts +8 -0
- package/plugins/motion/sensors.ts +116 -0
- package/plugins/motion/shell.tsx +136 -0
- package/plugins/motion/state.ts +46 -0
- package/plugins/network/app.ts +400 -0
- package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
- package/plugins/network/overrides/expo-network/overrides.ts +9 -0
- package/plugins/network/plugin.ts +8 -0
- package/plugins/network/shell.tsx +67 -0
- package/plugins/network/state.ts +29 -0
- package/plugins/notifications/center.ts +255 -0
- package/plugins/notifications/content.ts +36 -0
- package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
- package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
- package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
- package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
- package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
- package/plugins/notifications/plugin.ts +8 -0
- package/plugins/notifications/shell.tsx +192 -0
- package/plugins/notifications/state.ts +56 -0
- package/plugins/notifications/tokens.ts +47 -0
- package/plugins/notifications/trigger.ts +107 -0
- package/plugins/permissions/plugin.ts +6 -0
- package/plugins/permissions/shell.tsx +50 -0
- package/plugins/permissions/state.ts +38 -0
- package/plugins/photos/appLibrary.ts +114 -0
- package/plugins/photos/library.ts +322 -0
- package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
- package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
- package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
- package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
- package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
- package/plugins/photos/plugin.ts +8 -0
- package/plugins/photos/samples.ts +367 -0
- package/plugins/photos/shell.tsx +164 -0
- package/plugins/photos/state.ts +88 -0
- package/plugins/storage/files.ts +351 -0
- package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
- package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
- package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
- package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
- package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
- package/plugins/storage/plugin.ts +8 -0
- package/plugins/storage/reset.ts +136 -0
- package/plugins/storage/secure.ts +71 -0
- package/plugins/storage/shell.tsx +365 -0
- package/plugins/storage/state.ts +13 -0
- package/src/client/app/appDocument.ts +113 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +59 -0
- package/src/client/app/main.tsx +99 -0
- package/src/client/app/routeContext.ts +45 -0
- package/src/client/app/touch.ts +640 -0
- package/src/client/entry.ts +20 -0
- package/src/client/modules.d.ts +8 -0
- package/src/client/react-native/app.ts +57 -0
- package/src/client/react-native/deep.ts +13 -0
- package/src/client/react-native/keyboard.tsx +192 -0
- package/src/client/react-native/library.ts +256 -0
- package/src/client/shell/Cookies.tsx +95 -0
- package/src/client/shell/LogDrawer.tsx +415 -0
- package/src/client/shell/PhotoPicker.tsx +174 -0
- package/src/client/shell/PluginOverlays.tsx +42 -0
- package/src/client/shell/PluginPanels.tsx +82 -0
- package/src/client/shell/ResetApp.tsx +87 -0
- package/src/client/shell/Resizer.tsx +99 -0
- package/src/client/shell/Shell.tsx +531 -0
- package/src/client/shell/SystemBars.tsx +376 -0
- package/src/client/shell/SystemCamera.tsx +181 -0
- package/src/client/shell/SystemOverlay.tsx +422 -0
- package/src/client/shell/SystemSheets.tsx +462 -0
- package/src/client/shell/backdrop.ts +229 -0
- package/src/client/shell/control.ts +91 -0
- package/src/client/shell/events.ts +226 -0
- package/src/client/shell/geometry.ts +68 -0
- package/src/client/shell/host.ts +492 -0
- package/src/client/shell/icons.tsx +34 -0
- package/src/client/shell/logFeed.ts +102 -0
- package/src/client/shell/main.tsx +22 -0
- package/src/client/shell/permissionCopy.ts +169 -0
- package/src/client/shell/plugins.ts +266 -0
- package/src/client/shell/popout.ts +185 -0
- package/src/client/shell/popover.ts +27 -0
- package/src/client/shell/shell.css +878 -0
- package/src/client/shell/statusTime.ts +31 -0
- package/src/client/shell/tooltips.ts +104 -0
- package/src/client/virtual.d.ts +21 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +186 -0
- package/src/node/appSource.ts +138 -0
- package/src/node/config.ts +180 -0
- package/src/node/control.ts +144 -0
- package/src/node/doctor.ts +199 -0
- package/src/node/events/cli.ts +206 -0
- package/src/node/events/shared.ts +238 -0
- package/src/node/events/store.ts +266 -0
- package/src/node/index.ts +5 -0
- package/src/node/overrides/define.ts +56 -0
- package/src/node/overrides/packages.ts +136 -0
- package/src/node/overrides/platformPatch.ts +56 -0
- package/src/node/overrides/plugin.ts +166 -0
- package/src/node/overrides/registry.ts +258 -0
- package/src/node/paths.ts +18 -0
- package/src/node/platform.ts +171 -0
- package/src/node/plugins.ts +98 -0
- package/src/node/proxy.ts +421 -0
- package/src/node/server.ts +253 -0
- package/src/plugin/index.ts +33 -0
- package/src/plugin/types.ts +127 -0
- package/src/plugin/ui.tsx +562 -0
- package/src/runtime/clock.ts +112 -0
- package/src/runtime/draw.ts +196 -0
- package/src/runtime/index.ts +513 -0
- package/src/runtime/media.ts +379 -0
- package/src/runtime/records.ts +76 -0
- package/src/runtime/timezone.ts +81 -0
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
import { NativeModule, registerWebModule } from 'expo'
|
|
2
|
+
import { getState, hardware } from 'web-sim/runtime'
|
|
3
|
+
import {
|
|
4
|
+
children,
|
|
5
|
+
copy,
|
|
6
|
+
descendants,
|
|
7
|
+
exists,
|
|
8
|
+
fromBase64,
|
|
9
|
+
isDirectory,
|
|
10
|
+
isFile,
|
|
11
|
+
isSystemDirectory,
|
|
12
|
+
makeDirectory,
|
|
13
|
+
mimeType,
|
|
14
|
+
move,
|
|
15
|
+
nameOf,
|
|
16
|
+
node,
|
|
17
|
+
own,
|
|
18
|
+
packageName,
|
|
19
|
+
parentOf,
|
|
20
|
+
readFile,
|
|
21
|
+
remove,
|
|
22
|
+
requireParent,
|
|
23
|
+
roots,
|
|
24
|
+
size,
|
|
25
|
+
store,
|
|
26
|
+
toBase64,
|
|
27
|
+
toPath,
|
|
28
|
+
toUri,
|
|
29
|
+
writeFile,
|
|
30
|
+
} from '../../files.ts'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The phone's file system, for the `File` / `Directory` / `Paths` API (and, through `__webSim` below, the legacy API),
|
|
34
|
+
* over the storage plugin's file store (`files.ts`), which the storage panel browses and changes too.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
38
|
+
// Encodings and digests
|
|
39
|
+
|
|
40
|
+
function toBytes(content: string | Uint8Array | ArrayBuffer, encoding?: string): Uint8Array {
|
|
41
|
+
if (typeof content === 'string') {
|
|
42
|
+
return encoding === 'base64' ? fromBase64(content) : new TextEncoder().encode(content)
|
|
43
|
+
}
|
|
44
|
+
return content instanceof Uint8Array ? content : new Uint8Array(content)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** MD5, which `crypto.subtle` does not offer and file info reports. */
|
|
48
|
+
export function md5(bytes: Uint8Array): string {
|
|
49
|
+
const shifts = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21]
|
|
50
|
+
const constants = Array.from({ length: 64 }, (_, i) => Math.floor(Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0)
|
|
51
|
+
const length = bytes.length
|
|
52
|
+
const padded = new Uint8Array(((length + 8) >>> 6) * 64 + 64)
|
|
53
|
+
padded.set(bytes)
|
|
54
|
+
padded[length] = 0x80
|
|
55
|
+
const view = new DataView(padded.buffer)
|
|
56
|
+
view.setUint32(padded.length - 8, (length * 8) >>> 0, true)
|
|
57
|
+
view.setUint32(padded.length - 4, Math.floor((length * 8) / 2 ** 32), true)
|
|
58
|
+
let [a0, b0, c0, d0] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
|
|
59
|
+
for (let offset = 0; offset < padded.length; offset += 64) {
|
|
60
|
+
let [a, b, c, d] = [a0, b0, c0, d0]
|
|
61
|
+
for (let i = 0; i < 64; i++) {
|
|
62
|
+
const round = i >> 4
|
|
63
|
+
const f = round === 0 ? (b & c) | (~b & d) : round === 1 ? (d & b) | (~d & c) : round === 2 ? b ^ c ^ d : c ^ (b | ~d)
|
|
64
|
+
const g = round === 0 ? i : round === 1 ? (5 * i + 1) % 16 : round === 2 ? (3 * i + 5) % 16 : (7 * i) % 16
|
|
65
|
+
const sum = (a + f + constants[i] + view.getUint32(offset + g * 4, true)) | 0
|
|
66
|
+
const shift = shifts[round * 4 + (i % 4)]
|
|
67
|
+
;[a, d, c] = [d, c, b]
|
|
68
|
+
b = (b + ((sum << shift) | (sum >>> (32 - shift)))) | 0
|
|
69
|
+
}
|
|
70
|
+
a0 = (a0 + a) | 0
|
|
71
|
+
b0 = (b0 + b) | 0
|
|
72
|
+
c0 = (c0 + c) | 0
|
|
73
|
+
d0 = (d0 + d) | 0
|
|
74
|
+
}
|
|
75
|
+
return [a0, b0, c0, d0]
|
|
76
|
+
.map((word) => Array.from({ length: 4 }, (_, i) => ((word >>> (i * 8)) & 0xff).toString(16).padStart(2, '0')).join(''))
|
|
77
|
+
.join('')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
82
|
+
// Network
|
|
83
|
+
|
|
84
|
+
type Emit = (eventName: string, payload: unknown) => void
|
|
85
|
+
|
|
86
|
+
/** Where a download lands: into a directory under the server's file name, or at the file given. */
|
|
87
|
+
function downloadTarget(destination: string, response: Response, url: string, intoDirectory: boolean): string {
|
|
88
|
+
if (!intoDirectory) {
|
|
89
|
+
return destination
|
|
90
|
+
}
|
|
91
|
+
const disposition = response.headers.get('content-disposition') ?? ''
|
|
92
|
+
const fromHeader = /filename\*?=(?:UTF-8'')?"?([^";]+)"?/i.exec(disposition)?.[1]
|
|
93
|
+
let name = fromHeader ?? ''
|
|
94
|
+
if (name === '') {
|
|
95
|
+
try {
|
|
96
|
+
name = nameOf(new URL(url).pathname)
|
|
97
|
+
} catch {
|
|
98
|
+
name = ''
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return `${destination}/${decodeURIComponent(name || 'download')}`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Reads a response's body, reporting progress as it arrives. */
|
|
105
|
+
async function readBody(response: Response, onProgress: (written: number, total: number) => void): Promise<Uint8Array> {
|
|
106
|
+
const total = Number(response.headers.get('content-length') ?? 0)
|
|
107
|
+
if (response.body === null) {
|
|
108
|
+
return new Uint8Array(await response.arrayBuffer())
|
|
109
|
+
}
|
|
110
|
+
const reader = response.body.getReader()
|
|
111
|
+
const chunks: Uint8Array[] = []
|
|
112
|
+
let written = 0
|
|
113
|
+
for (;;) {
|
|
114
|
+
const { done, value } = await reader.read()
|
|
115
|
+
if (done) {
|
|
116
|
+
break
|
|
117
|
+
}
|
|
118
|
+
chunks.push(value)
|
|
119
|
+
written += value.length
|
|
120
|
+
onProgress(written, total || written)
|
|
121
|
+
}
|
|
122
|
+
const bytes = new Uint8Array(written)
|
|
123
|
+
let offset = 0
|
|
124
|
+
for (const chunk of chunks) {
|
|
125
|
+
bytes.set(chunk, offset)
|
|
126
|
+
offset += chunk.length
|
|
127
|
+
}
|
|
128
|
+
return bytes
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function headersOf(response: Response): Record<string, string> {
|
|
132
|
+
return Object.fromEntries(response.headers.entries())
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function isDirectoryTarget(target: { uri: string } | string): boolean {
|
|
136
|
+
const uri = typeof target === 'string' ? target : target.uri
|
|
137
|
+
return target instanceof FileSystemDirectory || uri.endsWith('/') || isDirectory(toPath(uri))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** `fetch`, which needs the server to allow this origin (CORS), as a browser does and a phone does not. */
|
|
141
|
+
async function download(
|
|
142
|
+
url: string,
|
|
143
|
+
to: { uri: string } | string,
|
|
144
|
+
options: { headers?: Record<string, string>; idempotent?: boolean; signal?: AbortSignal },
|
|
145
|
+
onProgress: (written: number, total: number) => void,
|
|
146
|
+
): Promise<{ path: string; response: Response }> {
|
|
147
|
+
await store().ready
|
|
148
|
+
const destination = toPath(typeof to === 'string' ? to : to.uri)
|
|
149
|
+
const response = await fetch(url, { headers: options.headers, signal: options.signal })
|
|
150
|
+
if (!response.ok) {
|
|
151
|
+
throw new Error(`Unable to download '${url}': the server responded ${response.status}`)
|
|
152
|
+
}
|
|
153
|
+
const path = downloadTarget(destination, response, url, isDirectoryTarget(to))
|
|
154
|
+
if (exists(path) && !options.idempotent) {
|
|
155
|
+
throw new Error(`Unable to download '${url}': '${toUri(path, false)}' already exists`)
|
|
156
|
+
}
|
|
157
|
+
writeFile(path, await readBody(response, onProgress))
|
|
158
|
+
return { path, response }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
type UploadOptions = {
|
|
162
|
+
httpMethod?: string
|
|
163
|
+
uploadType?: number
|
|
164
|
+
headers?: Record<string, string>
|
|
165
|
+
fieldName?: string
|
|
166
|
+
mimeType?: string
|
|
167
|
+
parameters?: Record<string, string>
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function upload(url: string, path: string, options: UploadOptions, signal?: AbortSignal) {
|
|
171
|
+
await store().ready
|
|
172
|
+
const bytes = own(readFile(path))
|
|
173
|
+
let body: BodyInit
|
|
174
|
+
const headers = { ...options.headers }
|
|
175
|
+
if (options.uploadType === 1) {
|
|
176
|
+
const form = new FormData()
|
|
177
|
+
for (const [key, value] of Object.entries(options.parameters ?? {})) {
|
|
178
|
+
form.append(key, value)
|
|
179
|
+
}
|
|
180
|
+
form.append(options.fieldName ?? 'file', new Blob([bytes], { type: options.mimeType ?? mimeType(path) }), nameOf(path))
|
|
181
|
+
body = form
|
|
182
|
+
} else {
|
|
183
|
+
body = new Blob([bytes], { type: options.mimeType ?? mimeType(path) })
|
|
184
|
+
}
|
|
185
|
+
const response = await fetch(url, { method: options.httpMethod ?? 'POST', headers, body, signal })
|
|
186
|
+
return { body: await response.text(), status: response.status, headers: headersOf(response), bytes: bytes.length }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
190
|
+
// The native classes `File` and `Directory` extend
|
|
191
|
+
|
|
192
|
+
type Uriish = string | { uri: string }
|
|
193
|
+
|
|
194
|
+
function uriOf(value: Uriish): string {
|
|
195
|
+
return typeof value === 'string' ? value : value.uri
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function checkScheme(uri: string): void {
|
|
199
|
+
if (!/^(file|asset|content):/.test(uri) && !uri.startsWith('/')) {
|
|
200
|
+
throw new Error(`'${uri}' is not a file URI`)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Where a copy or move lands: inside a directory under the same name, or at the path given. */
|
|
205
|
+
function destinationOf(path: string, destination: Uriish): string {
|
|
206
|
+
const target = toPath(uriOf(destination))
|
|
207
|
+
return isDirectoryTarget(destination) && isDirectory(target) ? `${target}/${nameOf(path)}` : target
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
class FileSystemFile {
|
|
211
|
+
uri: string
|
|
212
|
+
|
|
213
|
+
constructor(uri: Uriish) {
|
|
214
|
+
this.uri = toUri(toPath(uriOf(uri)), false)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private get path(): string {
|
|
218
|
+
return toPath(this.uri)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
validatePath(): void {
|
|
222
|
+
checkScheme(this.uri)
|
|
223
|
+
if (node(this.path)?.kind === 'directory' || isSystemDirectory(this.path)) {
|
|
224
|
+
throw new Error(`'${this.uri}' is a directory`)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
get exists(): boolean {
|
|
229
|
+
return isFile(this.path)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
get size(): number {
|
|
233
|
+
return isFile(this.path) ? readFile(this.path).length : 0
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
get md5(): string | null {
|
|
237
|
+
return isFile(this.path) ? md5(readFile(this.path)) : null
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
get modificationTime(): number | null {
|
|
241
|
+
return node(this.path)?.modified ?? null
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
get lastModified(): number | null {
|
|
245
|
+
return this.modificationTime
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
get creationTime(): number | null {
|
|
249
|
+
return node(this.path)?.created ?? null
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
get type(): string {
|
|
253
|
+
return mimeType(this.path)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
get contentUri(): string {
|
|
257
|
+
return getState().device.platform === 'android' ? `content://${packageName()}.FileSystemFileProvider/root${this.path}` : ''
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
textSync(): string {
|
|
261
|
+
return new TextDecoder().decode(readFile(this.path))
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async text(): Promise<string> {
|
|
265
|
+
await store().ready
|
|
266
|
+
return this.textSync()
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
base64Sync(): string {
|
|
270
|
+
return toBase64(readFile(this.path))
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async base64(): Promise<string> {
|
|
274
|
+
await store().ready
|
|
275
|
+
return this.base64Sync()
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
bytesSync(): Uint8Array<ArrayBuffer> {
|
|
279
|
+
return own(readFile(this.path))
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async bytes(): Promise<Uint8Array<ArrayBuffer>> {
|
|
283
|
+
await store().ready
|
|
284
|
+
return this.bytesSync()
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Writes at once; the promise is for callers of the async form. */
|
|
288
|
+
write(content: string | Uint8Array | ArrayBuffer, options: { encoding?: string; append?: boolean } = {}): Promise<void> & void {
|
|
289
|
+
this.writeSync(content, options)
|
|
290
|
+
return Promise.resolve() as Promise<void> & void
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
writeSync(content: string | Uint8Array | ArrayBuffer, options: { encoding?: string; append?: boolean } = {}): void {
|
|
294
|
+
writeFile(this.path, toBytes(content, options.encoding), options.append)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
delete(): void {
|
|
298
|
+
if (!isFile(this.path)) {
|
|
299
|
+
throw new Error(`Unable to delete '${this.uri}': the file does not exist`)
|
|
300
|
+
}
|
|
301
|
+
remove(this.path)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
create(options: { intermediates?: boolean; overwrite?: boolean } = {}): void {
|
|
305
|
+
if (exists(this.path) && !options.overwrite) {
|
|
306
|
+
throw new Error(`Unable to create '${this.uri}': it already exists`)
|
|
307
|
+
}
|
|
308
|
+
requireParent(this.path, options.intermediates)
|
|
309
|
+
writeFile(this.path, new Uint8Array())
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
info(options: { md5?: boolean } = {}) {
|
|
313
|
+
const file = node(this.path)
|
|
314
|
+
if (file?.kind !== 'file') {
|
|
315
|
+
return { exists: false, uri: this.uri }
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
exists: true,
|
|
319
|
+
uri: this.uri,
|
|
320
|
+
size: file.data?.length ?? 0,
|
|
321
|
+
modificationTime: file.modified,
|
|
322
|
+
creationTime: file.created,
|
|
323
|
+
...(options.md5 ? { md5: md5(file.data ?? new Uint8Array()) } : {}),
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async digest(algorithm: 'MD5' | 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512'): Promise<string> {
|
|
328
|
+
await store().ready
|
|
329
|
+
const bytes = own(readFile(this.path))
|
|
330
|
+
if (algorithm === 'MD5') {
|
|
331
|
+
return md5(bytes)
|
|
332
|
+
}
|
|
333
|
+
const digest = new Uint8Array(await crypto.subtle.digest(algorithm, bytes))
|
|
334
|
+
return Array.from(digest, (byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
copySync(destination: Uriish, options: { overwrite?: boolean } = {}): void {
|
|
338
|
+
copy(this.path, destinationOf(this.path, destination), options.overwrite ?? false)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async copy(destination: Uriish, options: { overwrite?: boolean } = {}): Promise<void> {
|
|
342
|
+
await store().ready
|
|
343
|
+
this.copySync(destination, options)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** As on the device, the file then refers to where it moved. */
|
|
347
|
+
moveSync(destination: Uriish, options: { overwrite?: boolean } = {}): void {
|
|
348
|
+
const target = destinationOf(this.path, destination)
|
|
349
|
+
move(this.path, target, options.overwrite ?? false)
|
|
350
|
+
this.uri = toUri(target, false)
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async move(destination: Uriish, options: { overwrite?: boolean } = {}): Promise<void> {
|
|
354
|
+
await store().ready
|
|
355
|
+
this.moveSync(destination, options)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
rename(newName: string): void {
|
|
359
|
+
const target = `${parentOf(this.path)}/${newName}`
|
|
360
|
+
move(this.path, target, false)
|
|
361
|
+
this.uri = toUri(target, false)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
open(mode = 'rw') {
|
|
365
|
+
return new FileHandle(this.path, mode)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async canPreview(): Promise<boolean> {
|
|
369
|
+
return false
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async preview(): Promise<void> {
|
|
373
|
+
throw new Error('Quick Look previews are not simulated')
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** A cursor over a file, for `File.open()` and the readable and writable streams. */
|
|
378
|
+
class FileHandle {
|
|
379
|
+
offset: number | null = 0
|
|
380
|
+
private path: string | null
|
|
381
|
+
private mode: string
|
|
382
|
+
|
|
383
|
+
constructor(path: string, mode: string) {
|
|
384
|
+
this.path = path
|
|
385
|
+
this.mode = mode
|
|
386
|
+
if (!isFile(path)) {
|
|
387
|
+
if (mode === 'r') {
|
|
388
|
+
readFile(path)
|
|
389
|
+
}
|
|
390
|
+
writeFile(path, new Uint8Array())
|
|
391
|
+
} else if (mode === 'wt') {
|
|
392
|
+
writeFile(path, new Uint8Array())
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
get size(): number | null {
|
|
397
|
+
return this.path === null ? null : readFile(this.path).length
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
close(): void {
|
|
401
|
+
this.path = null
|
|
402
|
+
this.offset = null
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
readBytesSync(length: number): Uint8Array<ArrayBuffer> {
|
|
406
|
+
if (this.path === null || this.offset === null) {
|
|
407
|
+
throw new Error('The file handle is closed')
|
|
408
|
+
}
|
|
409
|
+
const bytes = readFile(this.path).subarray(this.offset, this.offset + length)
|
|
410
|
+
this.offset += bytes.length
|
|
411
|
+
return own(bytes)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
async readBytes(length: number): Promise<Uint8Array<ArrayBuffer>> {
|
|
415
|
+
return this.readBytesSync(length)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
writeBytesSync(bytes: Uint8Array): void {
|
|
419
|
+
if (this.path === null || this.offset === null) {
|
|
420
|
+
throw new Error('The file handle is closed')
|
|
421
|
+
}
|
|
422
|
+
const current = readFile(this.path)
|
|
423
|
+
const start = this.mode === 'wa' ? current.length : this.offset
|
|
424
|
+
const next = new Uint8Array(Math.max(current.length, start + bytes.length))
|
|
425
|
+
next.set(current)
|
|
426
|
+
next.set(bytes, start)
|
|
427
|
+
writeFile(this.path, next)
|
|
428
|
+
this.offset = start + bytes.length
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async writeBytes(bytes: Uint8Array): Promise<void> {
|
|
432
|
+
this.writeBytesSync(bytes)
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
class FileSystemDirectory {
|
|
437
|
+
uri: string
|
|
438
|
+
|
|
439
|
+
constructor(uri: Uriish) {
|
|
440
|
+
this.uri = toUri(toPath(uriOf(uri)), true)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private get path(): string {
|
|
444
|
+
return toPath(this.uri)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
validatePath(): void {
|
|
448
|
+
checkScheme(this.uri)
|
|
449
|
+
if (isFile(this.path)) {
|
|
450
|
+
throw new Error(`'${this.uri}' is a file`)
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
get exists(): boolean {
|
|
455
|
+
return isDirectory(this.path)
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
get size(): number | null {
|
|
459
|
+
return isDirectory(this.path) ? size(this.path) : null
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
delete(): void {
|
|
463
|
+
if (!isDirectory(this.path)) {
|
|
464
|
+
throw new Error(`Unable to delete '${this.uri}': the directory does not exist`)
|
|
465
|
+
}
|
|
466
|
+
remove(this.path)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
create(options: { intermediates?: boolean; overwrite?: boolean; idempotent?: boolean } = {}): void {
|
|
470
|
+
if (exists(this.path)) {
|
|
471
|
+
if (options.idempotent && isDirectory(this.path)) {
|
|
472
|
+
return
|
|
473
|
+
}
|
|
474
|
+
if (!options.overwrite) {
|
|
475
|
+
throw new Error(`Unable to create '${this.uri}': it already exists`)
|
|
476
|
+
}
|
|
477
|
+
remove(this.path)
|
|
478
|
+
}
|
|
479
|
+
makeDirectory(this.path, options.intermediates ?? false)
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
createFile(name: string, _mimeType: string | null): { uri: string } {
|
|
483
|
+
const path = `${this.path}/${name}`
|
|
484
|
+
if (exists(path)) {
|
|
485
|
+
throw new Error(`Unable to create '${toUri(path, false)}': it already exists`)
|
|
486
|
+
}
|
|
487
|
+
writeFile(path, new Uint8Array())
|
|
488
|
+
return { uri: toUri(path, false) }
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
createDirectory(name: string): { uri: string } {
|
|
492
|
+
const path = `${this.path}/${name}`
|
|
493
|
+
if (exists(path)) {
|
|
494
|
+
throw new Error(`Unable to create '${toUri(path, true)}': it already exists`)
|
|
495
|
+
}
|
|
496
|
+
makeDirectory(path, false)
|
|
497
|
+
return { uri: toUri(path, true) }
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
listAsRecords(): { isDirectory: boolean; uri: string }[] {
|
|
501
|
+
if (!isDirectory(this.path)) {
|
|
502
|
+
throw new Error(`Unable to list '${this.uri}': the directory does not exist`)
|
|
503
|
+
}
|
|
504
|
+
return children(this.path).map((path) => ({ isDirectory: isDirectory(path), uri: toUri(path, isDirectory(path)) }))
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
list(): { uri: string }[] {
|
|
508
|
+
return this.listAsRecords().map(({ isDirectory: directory, uri }) =>
|
|
509
|
+
directory ? new FileSystemDirectory(uri) : new FileSystemFile(uri),
|
|
510
|
+
)
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
info() {
|
|
514
|
+
if (!isDirectory(this.path)) {
|
|
515
|
+
return { exists: false, uri: this.uri }
|
|
516
|
+
}
|
|
517
|
+
const directory = node(this.path)
|
|
518
|
+
return {
|
|
519
|
+
exists: true,
|
|
520
|
+
uri: this.uri,
|
|
521
|
+
size: size(this.path),
|
|
522
|
+
modificationTime: directory?.modified,
|
|
523
|
+
creationTime: directory?.created,
|
|
524
|
+
files: children(this.path).map(nameOf),
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
copySync(destination: Uriish, options: { overwrite?: boolean } = {}): void {
|
|
529
|
+
copy(this.path, destinationOf(this.path, destination), options.overwrite ?? false)
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
async copy(destination: Uriish, options: { overwrite?: boolean } = {}): Promise<void> {
|
|
533
|
+
await store().ready
|
|
534
|
+
this.copySync(destination, options)
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
moveSync(destination: Uriish, options: { overwrite?: boolean } = {}): void {
|
|
538
|
+
const target = destinationOf(this.path, destination)
|
|
539
|
+
move(this.path, target, options.overwrite ?? false)
|
|
540
|
+
this.uri = toUri(target, true)
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
async move(destination: Uriish, options: { overwrite?: boolean } = {}): Promise<void> {
|
|
544
|
+
await store().ready
|
|
545
|
+
this.moveSync(destination, options)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
rename(newName: string): void {
|
|
549
|
+
const target = `${parentOf(this.path)}/${newName}`
|
|
550
|
+
move(this.path, target, false)
|
|
551
|
+
this.uri = toUri(target, true)
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
type Listener = (payload: any) => void
|
|
556
|
+
|
|
557
|
+
/** What the native network tasks share: their own `progress` listeners. */
|
|
558
|
+
class TaskEvents {
|
|
559
|
+
private listeners = new Set<Listener>()
|
|
560
|
+
|
|
561
|
+
addListener(_eventName: string, listener: Listener) {
|
|
562
|
+
this.listeners.add(listener)
|
|
563
|
+
return { remove: () => this.listeners.delete(listener) }
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
removeAllListeners(): void {
|
|
567
|
+
this.listeners.clear()
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
protected emit(payload: unknown): void {
|
|
571
|
+
this.listeners.forEach((listener) => listener(payload))
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
release(): void {
|
|
575
|
+
this.listeners.clear()
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
class FileSystemUploadTask extends TaskEvents {
|
|
580
|
+
private controller: AbortController | null = null
|
|
581
|
+
|
|
582
|
+
async start(url: string, file: { uri: string }, options: UploadOptions) {
|
|
583
|
+
this.controller = new AbortController()
|
|
584
|
+
const { bytes, ...result } = await upload(url, toPath(file.uri), options, this.controller.signal)
|
|
585
|
+
this.emit({ bytesSent: bytes, totalBytes: bytes })
|
|
586
|
+
return result
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
cancel(): void {
|
|
590
|
+
this.controller?.abort()
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** A download that can pause and resume. Resuming starts the transfer again. */
|
|
595
|
+
class FileSystemDownloadTask extends TaskEvents {
|
|
596
|
+
private controller: AbortController | null = null
|
|
597
|
+
private paused = false
|
|
598
|
+
|
|
599
|
+
async start(url: string, to: Uriish, options: { headers?: Record<string, string> } = {}): Promise<string | null> {
|
|
600
|
+
return this.run(url, to, options)
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
pause(): { resumeData: string } {
|
|
604
|
+
this.paused = true
|
|
605
|
+
this.controller?.abort()
|
|
606
|
+
return { resumeData: 'web-sim' }
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
async resume(url: string, to: Uriish, _resumeData: string, options: { headers?: Record<string, string> } = {}): Promise<string | null> {
|
|
610
|
+
return this.run(url, to, options)
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
async acknowledgeBackgroundCompletionAsync(): Promise<void> {}
|
|
614
|
+
|
|
615
|
+
cancel(): void {
|
|
616
|
+
this.paused = false
|
|
617
|
+
this.controller?.abort()
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
private async run(url: string, to: Uriish, options: { headers?: Record<string, string> }): Promise<string | null> {
|
|
621
|
+
this.paused = false
|
|
622
|
+
this.controller = new AbortController()
|
|
623
|
+
try {
|
|
624
|
+
const { path } = await download(url, to, { headers: options.headers, idempotent: true, signal: this.controller.signal }, (written, total) =>
|
|
625
|
+
this.emit({ bytesWritten: written, totalBytes: total }),
|
|
626
|
+
)
|
|
627
|
+
return toUri(path, false)
|
|
628
|
+
} catch (error) {
|
|
629
|
+
if (this.paused) {
|
|
630
|
+
return null
|
|
631
|
+
}
|
|
632
|
+
throw error
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
638
|
+
// The module
|
|
639
|
+
|
|
640
|
+
/** Picks files with the browser's file chooser and copies them into the app's cache, as the document picker does. */
|
|
641
|
+
async function pickFiles(multiple: boolean, mimeTypes?: string | string[]): Promise<{ uri: string }[]> {
|
|
642
|
+
const input = document.createElement('input')
|
|
643
|
+
input.type = 'file'
|
|
644
|
+
input.multiple = multiple
|
|
645
|
+
if (mimeTypes) {
|
|
646
|
+
input.accept = (Array.isArray(mimeTypes) ? mimeTypes : [mimeTypes]).join(',')
|
|
647
|
+
}
|
|
648
|
+
const files = await new Promise<File[]>((resolve) => {
|
|
649
|
+
input.addEventListener('change', () => resolve([...(input.files ?? [])]))
|
|
650
|
+
input.addEventListener('cancel', () => resolve([]))
|
|
651
|
+
input.click()
|
|
652
|
+
})
|
|
653
|
+
if (files.length === 0) {
|
|
654
|
+
throw new Error('The user canceled the file picker')
|
|
655
|
+
}
|
|
656
|
+
await store().ready
|
|
657
|
+
const folder = `${roots().cache}/DocumentPicker`
|
|
658
|
+
makeDirectory(folder, true)
|
|
659
|
+
const picked = []
|
|
660
|
+
for (const file of files) {
|
|
661
|
+
const path = `${folder}/${file.name}`
|
|
662
|
+
writeFile(path, new Uint8Array(await file.arrayBuffer()))
|
|
663
|
+
picked.push({ uri: toUri(path, false) })
|
|
664
|
+
}
|
|
665
|
+
return picked
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
class ExpoFileSystemModule extends NativeModule<{ downloadProgress: (event: unknown) => void }> {
|
|
669
|
+
FileSystemFile = FileSystemFile
|
|
670
|
+
FileSystemDirectory = FileSystemDirectory
|
|
671
|
+
FileSystemUploadTask = FileSystemUploadTask
|
|
672
|
+
FileSystemDownloadTask = FileSystemDownloadTask
|
|
673
|
+
appleSharedContainers = {}
|
|
674
|
+
private downloads = new Map<string, AbortController>()
|
|
675
|
+
|
|
676
|
+
get documentDirectory(): string {
|
|
677
|
+
return toUri(roots().document, true)
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
get cacheDirectory(): string {
|
|
681
|
+
return toUri(roots().cache, true)
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
get bundleDirectory(): string {
|
|
685
|
+
return getState().device.platform === 'android' ? 'asset:///' : toUri(roots().bundle, true)
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
get totalDiskSpace(): number {
|
|
689
|
+
return hardware().totalStorage
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
get availableDiskSpace(): number {
|
|
693
|
+
return Math.round(hardware().totalStorage * 0.41)
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
info(uri: string): { exists: boolean; isDirectory: boolean | null } {
|
|
697
|
+
const path = toPath(uri)
|
|
698
|
+
return { exists: exists(path), isDirectory: exists(path) ? isDirectory(path) : null }
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
async downloadFileAsync(url: string, to: Uriish, options: { headers?: Record<string, string>; idempotent?: boolean } = {}, uuid?: string): Promise<string> {
|
|
702
|
+
const controller = new AbortController()
|
|
703
|
+
if (uuid !== undefined) {
|
|
704
|
+
this.downloads.set(uuid, controller)
|
|
705
|
+
}
|
|
706
|
+
try {
|
|
707
|
+
const { path } = await download(url, to, { ...options, signal: controller.signal }, (bytesWritten, totalBytes) => {
|
|
708
|
+
if (uuid !== undefined) {
|
|
709
|
+
;(this.emit as Emit)('downloadProgress', { uuid, data: { bytesWritten, totalBytes } })
|
|
710
|
+
}
|
|
711
|
+
})
|
|
712
|
+
return toUri(path, false)
|
|
713
|
+
} finally {
|
|
714
|
+
if (uuid !== undefined) {
|
|
715
|
+
this.downloads.delete(uuid)
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
async cancelDownloadAsync(uuid: string): Promise<void> {
|
|
721
|
+
this.downloads.get(uuid)?.abort()
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
async pickFileAsync(options: { multipleFiles?: boolean; mimeTypes?: string | string[] } = {}) {
|
|
725
|
+
const picked = await pickFiles(options.multipleFiles ?? false, options.mimeTypes)
|
|
726
|
+
return options.multipleFiles ? picked : picked[0]
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
async pickDirectoryAsync(): Promise<{ uri: string }> {
|
|
730
|
+
throw new Error('Picking a directory is not simulated')
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
async canPreview(): Promise<boolean> {
|
|
734
|
+
return false
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
async preview(): Promise<void> {
|
|
738
|
+
throw new Error('Quick Look previews are not simulated')
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/** For the legacy API's module, which shares these files. */
|
|
742
|
+
__webSim = {
|
|
743
|
+
ready: () => store().ready,
|
|
744
|
+
roots,
|
|
745
|
+
toPath,
|
|
746
|
+
toUri,
|
|
747
|
+
exists,
|
|
748
|
+
isFile,
|
|
749
|
+
isDirectory,
|
|
750
|
+
node,
|
|
751
|
+
children,
|
|
752
|
+
readFile,
|
|
753
|
+
writeFile,
|
|
754
|
+
makeDirectory,
|
|
755
|
+
remove,
|
|
756
|
+
copy,
|
|
757
|
+
move,
|
|
758
|
+
size,
|
|
759
|
+
md5,
|
|
760
|
+
toBase64,
|
|
761
|
+
fromBase64,
|
|
762
|
+
mimeType,
|
|
763
|
+
download,
|
|
764
|
+
upload,
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
export type WebSimFileSystem = ExpoFileSystemModule['__webSim']
|
|
769
|
+
|
|
770
|
+
export default registerWebModule(ExpoFileSystemModule, 'FileSystem')
|