@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.
Files changed (279) hide show
  1. package/README.md +13 -2
  2. package/bin/cli.ts +255 -0
  3. package/bin/web-sim.js +39 -0
  4. package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
  5. package/overrides/@react-native-firebase/app/overrides.ts +34 -0
  6. package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
  7. package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
  8. package/overrides/expo-application/ExpoApplication.ts +81 -0
  9. package/overrides/expo-application/overrides.ts +16 -0
  10. package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
  11. package/overrides/expo-background-task/overrides.ts +9 -0
  12. package/overrides/expo-constants/ExponentConstants.ts +66 -0
  13. package/overrides/expo-constants/overrides.ts +9 -0
  14. package/overrides/expo-device/ExpoDevice.ts +104 -0
  15. package/overrides/expo-device/overrides.ts +9 -0
  16. package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
  17. package/overrides/expo-glass-effect/GlassView.tsx +95 -0
  18. package/overrides/expo-glass-effect/availability.ts +14 -0
  19. package/overrides/expo-glass-effect/overrides.ts +14 -0
  20. package/overrides/expo-haptics/Haptics.ts +67 -0
  21. package/overrides/expo-haptics/overrides.ts +5 -0
  22. package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
  23. package/overrides/expo-mail-composer/overrides.ts +9 -0
  24. package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
  25. package/overrides/expo-mesh-gradient/overrides.ts +9 -0
  26. package/overrides/expo-modules-core/overrides.ts +12 -0
  27. package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
  28. package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
  29. package/overrides/expo-navigation-bar/overrides.ts +12 -0
  30. package/overrides/expo-router/NativeStackView.tsx +74 -0
  31. package/overrides/expo-router/NativeTabsView.tsx +263 -0
  32. package/overrides/expo-router/SafeAreaProvider.ts +6 -0
  33. package/overrides/expo-router/overrides.ts +23 -0
  34. package/overrides/expo-router/statusBarRequests.ts +51 -0
  35. package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
  36. package/overrides/expo-screen-orientation/overrides.ts +21 -0
  37. package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
  38. package/overrides/expo-sharing/overrides.ts +9 -0
  39. package/overrides/expo-sms/ExpoSMS.ts +24 -0
  40. package/overrides/expo-sms/overrides.ts +9 -0
  41. package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
  42. package/overrides/expo-splash-screen/overrides.ts +5 -0
  43. package/overrides/expo-status-bar/StatusBar.tsx +44 -0
  44. package/overrides/expo-status-bar/overrides.ts +5 -0
  45. package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
  46. package/overrides/expo-store-review/overrides.ts +9 -0
  47. package/overrides/expo-symbols/SymbolView.tsx +192 -0
  48. package/overrides/expo-symbols/overrides.ts +9 -0
  49. package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
  50. package/overrides/expo-system-ui/overrides.ts +9 -0
  51. package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
  52. package/overrides/expo-task-manager/overrides.ts +9 -0
  53. package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
  54. package/overrides/expo-tracking-transparency/overrides.ts +11 -0
  55. package/overrides/expo-video/ExpoVideo.ts +16 -0
  56. package/overrides/expo-video/overrides.ts +9 -0
  57. package/overrides/lottie-react-native/LottieView.tsx +87 -0
  58. package/overrides/lottie-react-native/overrides.ts +9 -0
  59. package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
  60. package/overrides/react-native-pager-view/overrides.ts +9 -0
  61. package/overrides/react-native-reanimated/overrides.ts +28 -0
  62. package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
  63. package/overrides/react-native-safe-area-context/overrides.ts +12 -0
  64. package/overrides/react-native-share/NativeRNShare.ts +22 -0
  65. package/overrides/react-native-share/overrides.ts +12 -0
  66. package/overrides/react-native-share/platform.ts +14 -0
  67. package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
  68. package/overrides/react-native-turbo-image/overrides.ts +9 -0
  69. package/overrides/react-native-web/RefreshControl.tsx +57 -0
  70. package/overrides/react-native-web/overrides.ts +153 -0
  71. package/overrides/react-native-web/useElementLayout.ts +140 -0
  72. package/package.json +78 -4
  73. package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
  74. package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
  75. package/plugins/battery/plugin.ts +8 -0
  76. package/plugins/battery/shell.tsx +56 -0
  77. package/plugins/battery/state.ts +11 -0
  78. package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
  79. package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
  80. package/plugins/biometrics/plugin.ts +8 -0
  81. package/plugins/biometrics/shell.tsx +88 -0
  82. package/plugins/biometrics/state.ts +41 -0
  83. package/plugins/brightness/app.ts +5 -0
  84. package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
  85. package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
  86. package/plugins/brightness/plugin.ts +9 -0
  87. package/plugins/brightness/shell.tsx +55 -0
  88. package/plugins/brightness/state.ts +19 -0
  89. package/plugins/calendar/calendar.ts +299 -0
  90. package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
  91. package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
  92. package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
  93. package/plugins/calendar/plugin.ts +8 -0
  94. package/plugins/calendar/shell.tsx +123 -0
  95. package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
  96. package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
  97. package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
  98. package/plugins/camera/plugin.ts +9 -0
  99. package/plugins/camera/shell.tsx +107 -0
  100. package/plugins/camera/state.ts +61 -0
  101. package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
  102. package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
  103. package/plugins/clipboard/plugin.ts +8 -0
  104. package/plugins/clipboard/shell.tsx +122 -0
  105. package/plugins/clipboard/state.ts +93 -0
  106. package/plugins/clock/app.ts +25 -0
  107. package/plugins/clock/plugin.ts +8 -0
  108. package/plugins/clock/shell.tsx +225 -0
  109. package/plugins/clock/state.ts +12 -0
  110. package/plugins/contacts/contacts.ts +125 -0
  111. package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
  112. package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
  113. package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
  114. package/plugins/contacts/plugin.ts +8 -0
  115. package/plugins/contacts/shell.tsx +109 -0
  116. package/plugins/home-indicator/app.ts +5 -0
  117. package/plugins/home-indicator/plugin.ts +9 -0
  118. package/plugins/home-indicator/requests.ts +48 -0
  119. package/plugins/home-indicator/shell.tsx +68 -0
  120. package/plugins/home-indicator/state.ts +31 -0
  121. package/plugins/keep-awake/app.ts +5 -0
  122. package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
  123. package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
  124. package/plugins/keep-awake/plugin.ts +9 -0
  125. package/plugins/keep-awake/shell.tsx +80 -0
  126. package/plugins/keep-awake/state.ts +12 -0
  127. package/plugins/keyboard/Keyboard.tsx +354 -0
  128. package/plugins/keyboard/app.ts +90 -0
  129. package/plugins/keyboard/controller.ts +384 -0
  130. package/plugins/keyboard/fields.ts +244 -0
  131. package/plugins/keyboard/icons.tsx +283 -0
  132. package/plugins/keyboard/keyboard.css +362 -0
  133. package/plugins/keyboard/layouts/android.ts +175 -0
  134. package/plugins/keyboard/layouts/index.ts +10 -0
  135. package/plugins/keyboard/layouts/ios.ts +219 -0
  136. package/plugins/keyboard/layouts/types.ts +80 -0
  137. package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
  138. package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
  139. package/plugins/keyboard/plugin.ts +13 -0
  140. package/plugins/keyboard/shell.tsx +133 -0
  141. package/plugins/keyboard/state.ts +92 -0
  142. package/plugins/keyboard/suggestions.ts +63 -0
  143. package/plugins/keyboard/taps.ts +77 -0
  144. package/plugins/keyboard/typing.ts +112 -0
  145. package/plugins/locale/app.ts +232 -0
  146. package/plugins/locale/locale.ts +143 -0
  147. package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
  148. package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
  149. package/plugins/locale/plugin.ts +9 -0
  150. package/plugins/locale/shell.tsx +147 -0
  151. package/plugins/locale/state.ts +36 -0
  152. package/plugins/location/geo.ts +56 -0
  153. package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
  154. package/plugins/location/overrides/expo-location/overrides.ts +11 -0
  155. package/plugins/location/places.ts +38 -0
  156. package/plugins/location/plugin.ts +8 -0
  157. package/plugins/location/shell.tsx +188 -0
  158. package/plugins/location/state.ts +51 -0
  159. package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
  160. package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
  161. package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
  162. package/plugins/motion/plugin.ts +8 -0
  163. package/plugins/motion/sensors.ts +116 -0
  164. package/plugins/motion/shell.tsx +136 -0
  165. package/plugins/motion/state.ts +46 -0
  166. package/plugins/network/app.ts +400 -0
  167. package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
  168. package/plugins/network/overrides/expo-network/overrides.ts +9 -0
  169. package/plugins/network/plugin.ts +8 -0
  170. package/plugins/network/shell.tsx +67 -0
  171. package/plugins/network/state.ts +29 -0
  172. package/plugins/notifications/center.ts +255 -0
  173. package/plugins/notifications/content.ts +36 -0
  174. package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
  175. package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
  176. package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
  177. package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
  178. package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
  179. package/plugins/notifications/plugin.ts +8 -0
  180. package/plugins/notifications/shell.tsx +192 -0
  181. package/plugins/notifications/state.ts +56 -0
  182. package/plugins/notifications/tokens.ts +47 -0
  183. package/plugins/notifications/trigger.ts +107 -0
  184. package/plugins/permissions/plugin.ts +6 -0
  185. package/plugins/permissions/shell.tsx +50 -0
  186. package/plugins/permissions/state.ts +38 -0
  187. package/plugins/photos/appLibrary.ts +114 -0
  188. package/plugins/photos/library.ts +322 -0
  189. package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
  190. package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
  191. package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
  192. package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
  193. package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
  194. package/plugins/photos/plugin.ts +8 -0
  195. package/plugins/photos/samples.ts +367 -0
  196. package/plugins/photos/shell.tsx +164 -0
  197. package/plugins/photos/state.ts +88 -0
  198. package/plugins/storage/files.ts +351 -0
  199. package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
  200. package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
  201. package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
  202. package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
  203. package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
  204. package/plugins/storage/plugin.ts +8 -0
  205. package/plugins/storage/reset.ts +136 -0
  206. package/plugins/storage/secure.ts +71 -0
  207. package/plugins/storage/shell.tsx +365 -0
  208. package/plugins/storage/state.ts +13 -0
  209. package/src/client/app/appDocument.ts +113 -0
  210. package/src/client/app/appFocus.ts +97 -0
  211. package/src/client/app/console.ts +118 -0
  212. package/src/client/app/fonts.ts +59 -0
  213. package/src/client/app/main.tsx +99 -0
  214. package/src/client/app/routeContext.ts +45 -0
  215. package/src/client/app/touch.ts +640 -0
  216. package/src/client/entry.ts +20 -0
  217. package/src/client/modules.d.ts +8 -0
  218. package/src/client/react-native/app.ts +57 -0
  219. package/src/client/react-native/deep.ts +13 -0
  220. package/src/client/react-native/keyboard.tsx +192 -0
  221. package/src/client/react-native/library.ts +256 -0
  222. package/src/client/shell/Cookies.tsx +95 -0
  223. package/src/client/shell/LogDrawer.tsx +415 -0
  224. package/src/client/shell/PhotoPicker.tsx +174 -0
  225. package/src/client/shell/PluginOverlays.tsx +42 -0
  226. package/src/client/shell/PluginPanels.tsx +82 -0
  227. package/src/client/shell/ResetApp.tsx +87 -0
  228. package/src/client/shell/Resizer.tsx +99 -0
  229. package/src/client/shell/Shell.tsx +531 -0
  230. package/src/client/shell/SystemBars.tsx +376 -0
  231. package/src/client/shell/SystemCamera.tsx +181 -0
  232. package/src/client/shell/SystemOverlay.tsx +422 -0
  233. package/src/client/shell/SystemSheets.tsx +462 -0
  234. package/src/client/shell/backdrop.ts +229 -0
  235. package/src/client/shell/control.ts +91 -0
  236. package/src/client/shell/events.ts +226 -0
  237. package/src/client/shell/geometry.ts +68 -0
  238. package/src/client/shell/host.ts +492 -0
  239. package/src/client/shell/icons.tsx +34 -0
  240. package/src/client/shell/logFeed.ts +102 -0
  241. package/src/client/shell/main.tsx +22 -0
  242. package/src/client/shell/permissionCopy.ts +169 -0
  243. package/src/client/shell/plugins.ts +266 -0
  244. package/src/client/shell/popout.ts +185 -0
  245. package/src/client/shell/popover.ts +27 -0
  246. package/src/client/shell/shell.css +878 -0
  247. package/src/client/shell/statusTime.ts +31 -0
  248. package/src/client/shell/tooltips.ts +104 -0
  249. package/src/client/virtual.d.ts +21 -0
  250. package/src/devices/catalog.ts +223 -0
  251. package/src/devices/types.ts +83 -0
  252. package/src/node/appInfo.ts +186 -0
  253. package/src/node/appSource.ts +138 -0
  254. package/src/node/config.ts +180 -0
  255. package/src/node/control.ts +144 -0
  256. package/src/node/doctor.ts +199 -0
  257. package/src/node/events/cli.ts +206 -0
  258. package/src/node/events/shared.ts +238 -0
  259. package/src/node/events/store.ts +266 -0
  260. package/src/node/index.ts +5 -0
  261. package/src/node/overrides/define.ts +56 -0
  262. package/src/node/overrides/packages.ts +136 -0
  263. package/src/node/overrides/platformPatch.ts +56 -0
  264. package/src/node/overrides/plugin.ts +166 -0
  265. package/src/node/overrides/registry.ts +258 -0
  266. package/src/node/paths.ts +18 -0
  267. package/src/node/platform.ts +171 -0
  268. package/src/node/plugins.ts +98 -0
  269. package/src/node/proxy.ts +421 -0
  270. package/src/node/server.ts +253 -0
  271. package/src/plugin/index.ts +33 -0
  272. package/src/plugin/types.ts +127 -0
  273. package/src/plugin/ui.tsx +562 -0
  274. package/src/runtime/clock.ts +112 -0
  275. package/src/runtime/draw.ts +196 -0
  276. package/src/runtime/index.ts +513 -0
  277. package/src/runtime/media.ts +379 -0
  278. package/src/runtime/records.ts +76 -0
  279. package/src/runtime/timezone.ts +81 -0
@@ -0,0 +1,235 @@
1
+ import { CodedError } from 'expo'
2
+ import { getPermission, platform, requestPermission } from 'web-sim/runtime'
3
+ import {
4
+ createLibraryAlbum,
5
+ deleteFromLibrary,
6
+ deleteLibraryAlbums,
7
+ editLibraryAlbum,
8
+ type LibraryAlbum,
9
+ libraryAlbums,
10
+ type LibraryPhoto,
11
+ libraryPhoto,
12
+ libraryPhotos,
13
+ onLibraryChange,
14
+ photoDataUrl,
15
+ saveToLibrary,
16
+ setLibraryFavorite,
17
+ } from '../../appLibrary.ts'
18
+
19
+ /**
20
+ * The legacy `expo-media-library` API (`expo-media-library/legacy`) over the simulator's photo library, the same
21
+ * one the photo picker and the current API show. Assets' `uri`s are data URLs a `<Image>` can show, sized for lists;
22
+ * `getAssetInfoAsync` gives the full-size `localUri`.
23
+ */
24
+
25
+ type AssetsOptions = {
26
+ first: number
27
+ after?: string
28
+ album?: string
29
+ sortBy: string[]
30
+ mediaType: string[]
31
+ createdAfter?: number
32
+ createdBefore?: number
33
+ }
34
+
35
+ function ensurePermission() {
36
+ if (!getPermission('photos').granted) {
37
+ throw new CodedError('E_NO_PERMISSIONS', 'Missing MEDIA_LIBRARY permissions.')
38
+ }
39
+ }
40
+
41
+ function albumOf(id: string): LibraryAlbum {
42
+ const album = libraryAlbums().find((candidate) => candidate.id === id)
43
+ if (album === undefined) {
44
+ throw new CodedError('E_ALBUM_NOT_FOUND', `Album ${id} not found`)
45
+ }
46
+ return album
47
+ }
48
+
49
+ function albumFor(photo: LibraryPhoto): string | undefined {
50
+ return libraryAlbums().find((album) => album.type === 'album' && album.assetIds.includes(photo.id))?.id
51
+ }
52
+
53
+ async function asset(photo: LibraryPhoto) {
54
+ return {
55
+ id: photo.id,
56
+ filename: photo.filename,
57
+ uri: (await photoDataUrl(photo, { maxSize: 800, quality: 0.8 })).uri,
58
+ mediaType: 'photo',
59
+ mediaSubtypes: [],
60
+ width: photo.width,
61
+ height: photo.height,
62
+ creationTime: photo.creationTime,
63
+ modificationTime: photo.creationTime,
64
+ duration: 0,
65
+ albumId: albumFor(photo),
66
+ }
67
+ }
68
+
69
+ function album(album: LibraryAlbum) {
70
+ const photos = libraryPhotos().filter((photo) => album.assetIds.includes(photo.id))
71
+ return {
72
+ id: album.id,
73
+ title: album.title,
74
+ assetCount: album.assetIds.length,
75
+ ...(platform() === 'ios' && {
76
+ type: album.type,
77
+ startTime: Math.min(...photos.map((photo) => photo.creationTime)),
78
+ endTime: Math.max(...photos.map((photo) => photo.creationTime)),
79
+ }),
80
+ }
81
+ }
82
+
83
+ function libraryPermission(response: ReturnType<typeof getPermission>) {
84
+ return { ...response, accessPrivileges: response.granted ? 'all' : 'none' }
85
+ }
86
+
87
+ export default {
88
+ CHANGE_LISTENER_NAME: 'mediaLibraryDidChange',
89
+ MediaType: { audio: 'audio', photo: 'photo', video: 'video', unknown: 'unknown' },
90
+ SortBy: {
91
+ default: 'default',
92
+ mediaType: 'mediaType',
93
+ width: 'width',
94
+ height: 'height',
95
+ creationTime: 'creationTime',
96
+ modificationTime: 'modificationTime',
97
+ duration: 'duration',
98
+ },
99
+
100
+ async getPermissionsAsync(_writeOnly?: boolean) {
101
+ return libraryPermission(getPermission('photos'))
102
+ },
103
+ async requestPermissionsAsync(_writeOnly?: boolean) {
104
+ return libraryPermission(await requestPermission('photos'))
105
+ },
106
+ async presentPermissionsPickerAsync() {},
107
+
108
+ async getAssetsAsync(options: AssetsOptions) {
109
+ ensurePermission()
110
+ let photos = libraryPhotos()
111
+ if (!options.mediaType.includes('photo') && !options.mediaType.includes('all')) {
112
+ photos = []
113
+ }
114
+ if (options.album) {
115
+ const ids = new Set(albumOf(options.album).assetIds)
116
+ photos = photos.filter((photo) => ids.has(photo.id))
117
+ }
118
+ if (options.createdAfter !== undefined) {
119
+ photos = photos.filter((photo) => photo.creationTime > options.createdAfter!)
120
+ }
121
+ if (options.createdBefore !== undefined) {
122
+ photos = photos.filter((photo) => photo.creationTime < options.createdBefore!)
123
+ }
124
+ for (const sort of [...options.sortBy].reverse()) {
125
+ const [key, direction] = sort.split(' ')
126
+ const field = key === 'modificationTime' || key === 'default' ? 'creationTime' : key
127
+ if (field === 'creationTime' || field === 'width' || field === 'height') {
128
+ photos = [...photos].sort((a, b) => (direction === 'ASC' ? 1 : -1) * (a[field] - b[field]))
129
+ }
130
+ }
131
+ const start = options.after === undefined ? 0 : photos.findIndex((photo) => photo.id === options.after) + 1
132
+ const page = photos.slice(start, start + options.first)
133
+ return {
134
+ assets: await Promise.all(page.map(asset)),
135
+ endCursor: page.at(-1)?.id ?? options.after ?? '',
136
+ hasNextPage: start + page.length < photos.length,
137
+ totalCount: photos.length,
138
+ }
139
+ },
140
+
141
+ async getAssetInfoAsync(id: string) {
142
+ ensurePermission()
143
+ const photo = libraryPhoto(id)
144
+ if (photo === null) {
145
+ throw new CodedError('E_ASSET_NOT_FOUND', `Asset ${id} not found`)
146
+ }
147
+ return {
148
+ ...(await asset(photo)),
149
+ localUri: (await photoDataUrl(photo)).uri,
150
+ location: photo.location ?? undefined,
151
+ exif: { PixelXDimension: photo.width, PixelYDimension: photo.height, Orientation: 1 },
152
+ isFavorite: photo.favorite,
153
+ isNetworkAsset: false,
154
+ orientation: 0,
155
+ }
156
+ },
157
+
158
+ async createAssetAsync(localUri: string, albumId?: string) {
159
+ ensurePermission()
160
+ return asset(await saveToLibrary(localUri, albumId || undefined))
161
+ },
162
+
163
+ async saveToLibraryAsync(localUri: string) {
164
+ // Saving needs only add access, which the photo library permission covers here.
165
+ ensurePermission()
166
+ await saveToLibrary(localUri)
167
+ },
168
+
169
+ async deleteAssetsAsync(ids: string[]) {
170
+ ensurePermission()
171
+ deleteFromLibrary(ids)
172
+ return true
173
+ },
174
+
175
+ async setAssetFavoriteAsync(id: string, isFavorite: boolean) {
176
+ ensurePermission()
177
+ setLibraryFavorite(id, isFavorite)
178
+ },
179
+
180
+ async getAlbumsAsync({ includeSmartAlbums = false }: { includeSmartAlbums?: boolean } = {}) {
181
+ ensurePermission()
182
+ return libraryAlbums()
183
+ .filter((candidate) => includeSmartAlbums || candidate.type === 'album')
184
+ .map(album)
185
+ },
186
+
187
+ async getAlbumAsync(title: string) {
188
+ ensurePermission()
189
+ const found = libraryAlbums().find((candidate) => candidate.title === title)
190
+ return found === undefined ? null : album(found)
191
+ },
192
+
193
+ async createAlbumAsync(title: string, assetId?: string, ...rest: unknown[]) {
194
+ ensurePermission()
195
+ const initialUri = rest.find((value): value is string => typeof value === 'string')
196
+ const ids = assetId ? [assetId] : initialUri ? [(await saveToLibrary(initialUri)).id] : []
197
+ return album(createLibraryAlbum(title, ids))
198
+ },
199
+
200
+ async deleteAlbumsAsync(ids: string[], deleteAssets = false) {
201
+ ensurePermission()
202
+ if (deleteAssets) {
203
+ deleteFromLibrary(ids.flatMap((id) => albumOf(id).assetIds))
204
+ }
205
+ deleteLibraryAlbums(ids)
206
+ return true
207
+ },
208
+
209
+ async addAssetsToAlbumAsync(assetIds: string[], albumId: string) {
210
+ ensurePermission()
211
+ editLibraryAlbum(albumId, { add: assetIds })
212
+ return true
213
+ },
214
+
215
+ async removeAssetsFromAlbumAsync(assetIds: string[], albumId: string) {
216
+ ensurePermission()
217
+ editLibraryAlbum(albumId, { remove: assetIds })
218
+ return true
219
+ },
220
+
221
+ async getMomentsAsync() {
222
+ ensurePermission()
223
+ return []
224
+ },
225
+
226
+ async migrateAlbumIfNeededAsync() {},
227
+ async albumNeedsMigrationAsync() {
228
+ return false
229
+ },
230
+
231
+ addListener(_eventName: string, listener: (event: unknown) => void) {
232
+ return { remove: onLibraryChange((change) => listener(platform() === 'android' ? {} : change)) }
233
+ },
234
+ removeAllListeners() {},
235
+ }
@@ -0,0 +1,318 @@
1
+ import { CodedError } from 'expo'
2
+ import { getPermission, platform, requestPermission } from 'web-sim/runtime'
3
+ import {
4
+ createLibraryAlbum,
5
+ deleteFromLibrary,
6
+ deleteLibraryAlbums,
7
+ editLibraryAlbum,
8
+ type LibraryAlbum,
9
+ libraryAlbums,
10
+ type LibraryPhoto,
11
+ libraryPhoto,
12
+ libraryPhotos,
13
+ onLibraryChange,
14
+ photoDataUrl,
15
+ saveToLibrary,
16
+ setLibraryFavorite,
17
+ } from '../../appLibrary.ts'
18
+
19
+ /**
20
+ * `expo-media-library`'s native module over the simulator's photo library: the same photos and albums the photo
21
+ * picker shows. Reading it needs the photo library permission, as on the device.
22
+ */
23
+
24
+ function ensurePermission() {
25
+ if (!getPermission('photos').granted) {
26
+ throw new CodedError('ERR_NO_PERMISSIONS', 'Missing photo library permission')
27
+ }
28
+ }
29
+
30
+ function photoOf(id: string): LibraryPhoto {
31
+ ensurePermission()
32
+ const photo = libraryPhoto(id)
33
+ if (photo === null) {
34
+ throw new CodedError('ERR_ASSET_NOT_FOUND', `Asset ${id} not found`)
35
+ }
36
+ return photo
37
+ }
38
+
39
+ function albumOf(id: string): LibraryAlbum {
40
+ ensurePermission()
41
+ const album = libraryAlbums().find((candidate) => candidate.id === id)
42
+ if (album === undefined) {
43
+ throw new CodedError('ERR_ALBUM_NOT_FOUND', `Album ${id} not found`)
44
+ }
45
+ return album
46
+ }
47
+
48
+ function metadata(photo: LibraryPhoto) {
49
+ return {
50
+ id: photo.id,
51
+ filename: photo.filename,
52
+ mediaType: 'image',
53
+ width: photo.width,
54
+ height: photo.height,
55
+ duration: null,
56
+ creationTime: photo.creationTime,
57
+ modificationTime: photo.creationTime,
58
+ isFavorite: photo.favorite,
59
+ mediaSubtypes: [],
60
+ }
61
+ }
62
+
63
+ function libraryPermission(response: ReturnType<typeof getPermission>) {
64
+ return { ...response, accessPrivileges: response.granted ? 'all' : 'none' }
65
+ }
66
+
67
+ class NativeAsset {
68
+ id: string
69
+ constructor(id: string) {
70
+ this.id = id
71
+ }
72
+ async getCreationTime() {
73
+ return photoOf(this.id).creationTime
74
+ }
75
+ async getModificationTime() {
76
+ return photoOf(this.id).creationTime
77
+ }
78
+ async getDuration() {
79
+ photoOf(this.id)
80
+ return null
81
+ }
82
+ async getFilename() {
83
+ return photoOf(this.id).filename
84
+ }
85
+ async getWidth() {
86
+ return photoOf(this.id).width
87
+ }
88
+ async getHeight() {
89
+ return photoOf(this.id).height
90
+ }
91
+ async getShape() {
92
+ const photo = photoOf(this.id)
93
+ return { width: photo.width, height: photo.height }
94
+ }
95
+ async getMediaType() {
96
+ photoOf(this.id)
97
+ return 'image'
98
+ }
99
+ async getMediaSubtypes() {
100
+ photoOf(this.id)
101
+ return []
102
+ }
103
+ async getLivePhotoVideoUri() {
104
+ return null
105
+ }
106
+ async getIsInCloud() {
107
+ return false
108
+ }
109
+ async getOrientation() {
110
+ return 0
111
+ }
112
+ async getUri() {
113
+ return (await photoDataUrl(photoOf(this.id))).uri
114
+ }
115
+ async getInfo() {
116
+ const photo = photoOf(this.id)
117
+ return { ...metadata(photo), uri: (await photoDataUrl(photo)).uri }
118
+ }
119
+ async getAlbums() {
120
+ photoOf(this.id)
121
+ return libraryAlbums()
122
+ .filter((album) => album.type === 'album' && album.assetIds.includes(this.id))
123
+ .map((album) => new NativeAlbum(album.id))
124
+ }
125
+ async getLocation() {
126
+ return photoOf(this.id).location
127
+ }
128
+ async getExif() {
129
+ const photo = photoOf(this.id)
130
+ return { PixelXDimension: photo.width, PixelYDimension: photo.height, Orientation: 1 }
131
+ }
132
+ async getFavorite() {
133
+ return photoOf(this.id).favorite
134
+ }
135
+ async setFavorite(isFavorite: boolean) {
136
+ photoOf(this.id)
137
+ setLibraryFavorite(this.id, isFavorite)
138
+ }
139
+ async delete() {
140
+ await NativeAsset.delete([this])
141
+ }
142
+ static async create(filePath: string, album?: NativeAlbum) {
143
+ ensurePermission()
144
+ return new NativeAsset((await saveToLibrary(filePath, album?.id)).id)
145
+ }
146
+ static async delete(assets: NativeAsset[]) {
147
+ ensurePermission()
148
+ deleteFromLibrary(assets.map((asset) => asset.id))
149
+ }
150
+ }
151
+
152
+ class NativeAlbum {
153
+ id: string
154
+ constructor(id: string) {
155
+ this.id = id
156
+ }
157
+ async getAssets() {
158
+ return albumOf(this.id).assetIds.map((id) => new NativeAsset(id))
159
+ }
160
+ async getTitle() {
161
+ return albumOf(this.id).title
162
+ }
163
+ async getAssetCount() {
164
+ return albumOf(this.id).assetIds.length
165
+ }
166
+ async getType() {
167
+ return albumOf(this.id).type
168
+ }
169
+ async delete() {
170
+ await NativeAlbum.delete([this])
171
+ }
172
+ async add(assets: NativeAsset | NativeAsset[]) {
173
+ albumOf(this.id)
174
+ editLibraryAlbum(this.id, { add: (Array.isArray(assets) ? assets : [assets]).map((asset) => asset.id) })
175
+ }
176
+ async removeAssets(assets: NativeAsset[]) {
177
+ albumOf(this.id)
178
+ editLibraryAlbum(this.id, { remove: assets.map((asset) => asset.id) })
179
+ }
180
+ static async create(name: string, assetsRefs: (string | NativeAsset)[] = [], _moveAssets?: boolean) {
181
+ ensurePermission()
182
+ const ids: string[] = []
183
+ for (const ref of assetsRefs) {
184
+ // A string is a file to add; an asset is one already in the library.
185
+ ids.push(typeof ref === 'string' ? (await saveToLibrary(ref)).id : ref.id)
186
+ }
187
+ return new NativeAlbum(createLibraryAlbum(name, ids).id)
188
+ }
189
+ static async delete(albums: NativeAlbum[], deleteAssets = false) {
190
+ ensurePermission()
191
+ if (deleteAssets) {
192
+ deleteFromLibrary(albums.flatMap((album) => albumOf(album.id).assetIds))
193
+ }
194
+ deleteLibraryAlbums(albums.map((album) => album.id))
195
+ }
196
+ static async get(title: string) {
197
+ ensurePermission()
198
+ const album = libraryAlbums().find((candidate) => candidate.title === title)
199
+ return album === undefined ? null : new NativeAlbum(album.id)
200
+ }
201
+ static async getAll() {
202
+ ensurePermission()
203
+ return libraryAlbums()
204
+ .filter((album) => album.type === 'album')
205
+ .map((album) => new NativeAlbum(album.id))
206
+ }
207
+ static async getAlbumsMetadata() {
208
+ ensurePermission()
209
+ return libraryAlbums()
210
+ .filter((album) => album.type === 'album')
211
+ .map((album) => ({ id: album.id, title: album.title, type: platform() === 'ios' ? album.type : null }))
212
+ }
213
+ static async getSmartAlbums() {
214
+ ensurePermission()
215
+ return libraryAlbums()
216
+ .filter((album) => album.type === 'smartAlbum')
217
+ .map((album) => new NativeAlbum(album.id))
218
+ }
219
+ }
220
+
221
+ type Comparable = 'creationTime' | 'modificationTime' | 'width' | 'height' | 'duration'
222
+ type Filter = (asset: ReturnType<typeof metadata>) => boolean
223
+
224
+ class NativeQuery {
225
+ filters: Filter[] = []
226
+ sort: { key: string; ascending: boolean }[] = []
227
+ count: number | null = null
228
+ skip = 0
229
+ albumId: string | null = null
230
+
231
+ private field(asset: ReturnType<typeof metadata>, field: string): unknown {
232
+ return (asset as Record<string, unknown>)[field]
233
+ }
234
+ eq(field: string, value: unknown) {
235
+ this.filters.push((asset) => this.field(asset, field) === value)
236
+ return this
237
+ }
238
+ within(field: string, values: unknown[]) {
239
+ this.filters.push((asset) => values.includes(this.field(asset, field)))
240
+ return this
241
+ }
242
+ contains(field: string, value: unknown) {
243
+ this.filters.push((asset) => (this.field(asset, field) as unknown[] | undefined)?.includes(value) ?? false)
244
+ return this
245
+ }
246
+ gt(field: Comparable, value: number) {
247
+ this.filters.push((asset) => Number(this.field(asset, field)) > value)
248
+ return this
249
+ }
250
+ gte(field: Comparable, value: number) {
251
+ this.filters.push((asset) => Number(this.field(asset, field)) >= value)
252
+ return this
253
+ }
254
+ lt(field: Comparable, value: number) {
255
+ this.filters.push((asset) => Number(this.field(asset, field)) < value)
256
+ return this
257
+ }
258
+ lte(field: Comparable, value: number) {
259
+ this.filters.push((asset) => Number(this.field(asset, field)) <= value)
260
+ return this
261
+ }
262
+ limit(limit: number) {
263
+ this.count = limit
264
+ return this
265
+ }
266
+ offset(offset: number) {
267
+ this.skip = offset
268
+ return this
269
+ }
270
+ orderBy(descriptors: string | { key: string; ascending?: boolean } | (string | { key: string; ascending?: boolean })[]) {
271
+ const list = Array.isArray(descriptors) ? descriptors : [descriptors]
272
+ this.sort = list.map((item) => (typeof item === 'string' ? { key: item, ascending: true } : { key: item.key, ascending: item.ascending ?? true }))
273
+ return this
274
+ }
275
+ album(album: NativeAlbum) {
276
+ this.albumId = album.id
277
+ return this
278
+ }
279
+ private run() {
280
+ ensurePermission()
281
+ const inAlbum = this.albumId === null ? null : new Set(albumOf(this.albumId).assetIds)
282
+ let assets = libraryPhotos()
283
+ .filter((photo) => inAlbum === null || inAlbum.has(photo.id))
284
+ .map(metadata)
285
+ .filter((asset) => this.filters.every((filter) => filter(asset)))
286
+ for (const { key, ascending } of [...this.sort].reverse()) {
287
+ assets = [...assets].sort((a, b) => {
288
+ const order = Number(this.field(a, key) ?? 0) - Number(this.field(b, key) ?? 0)
289
+ return ascending ? order : -order
290
+ })
291
+ }
292
+ return assets.slice(this.skip, this.count === null ? undefined : this.skip + this.count)
293
+ }
294
+ async exe() {
295
+ return this.run().map((asset) => new NativeAsset(asset.id))
296
+ }
297
+ async exeForMetadata() {
298
+ return this.run()
299
+ }
300
+ }
301
+
302
+ export const NativeMediaLibraryModule = {
303
+ Asset: NativeAsset,
304
+ Album: NativeAlbum,
305
+ Query: NativeQuery,
306
+ async getPermissionsAsync(_writeOnly?: boolean, _granular?: string[]) {
307
+ return libraryPermission(getPermission('photos'))
308
+ },
309
+ async requestPermissionsAsync(_writeOnly?: boolean, _granular?: string[]) {
310
+ return libraryPermission(await requestPermission('photos'))
311
+ },
312
+ async presentPermissionsPicker(_mediaTypes?: string[]) {},
313
+ addListener(eventName: string, listener: (event: unknown) => void) {
314
+ return { remove: eventName === 'mediaLibraryDidChange' ? onLibraryChange(listener) : () => undefined }
315
+ },
316
+ removeAllListeners(_eventName: string) {},
317
+ }
318
+ export { NativeAsset, NativeAlbum, NativeQuery }
@@ -0,0 +1,19 @@
1
+ import { defineOverrides } from '../../../../src/node/overrides/define.ts'
2
+ import { simulatedPlatform } from '../../../../src/node/overrides/platformPatch.ts'
3
+
4
+ export default defineOverrides('expo-media-library', [
5
+ {
6
+ versions: '>=58 <59',
7
+ note: "the photos panel's library and albums, the same the photo picker shows; what the app saves shows in the panel",
8
+ files: {
9
+ 'build/next/native/NativeMediaLibraryModule.web.js': './NativeMediaLibraryModule.ts',
10
+ 'build/legacy/ExpoMediaLibrary.web.js': './ExpoMediaLibrary.ts',
11
+ },
12
+ patch: [
13
+ simulatedPlatform('build/next/js/AssetAlbum.js'),
14
+ simulatedPlatform('build/next/js/Permissions.js'),
15
+ simulatedPlatform('build/next/js/Query.js'),
16
+ simulatedPlatform('build/legacy/MediaLibrary.js'),
17
+ ],
18
+ },
19
+ ])
@@ -0,0 +1,8 @@
1
+ import { definePlugin } from '../../src/node/plugins.ts'
2
+
3
+ export default definePlugin({
4
+ name: 'photos',
5
+ packages: ['expo-image-picker', 'expo-media-library'],
6
+ overrides: './overrides',
7
+ shell: './shell.tsx',
8
+ })