@ng-native/desktop 0.0.0 → 0.24.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 (401) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -0
  3. package/app.plugin.cjs +17 -0
  4. package/bin/app-config.mjs +168 -0
  5. package/bin/autolink.mjs +224 -0
  6. package/bin/cli.mjs +158 -0
  7. package/bin/host.mjs +427 -0
  8. package/bin/ng-native-desktop.mjs +4 -0
  9. package/bin/package.mjs +971 -0
  10. package/bin/plist.mjs +68 -0
  11. package/bin/prebuilt.mjs +328 -0
  12. package/bin/toolchain.mjs +192 -0
  13. package/native/CMakeLists.txt +277 -0
  14. package/native/appkit/CMakeLists.txt +130 -0
  15. package/native/appkit/address-search.h +26 -0
  16. package/native/appkit/address-search.mm +277 -0
  17. package/native/appkit/animation-frames.h +14 -0
  18. package/native/appkit/animation-frames.mm +106 -0
  19. package/native/appkit/app-info.h +12 -0
  20. package/native/appkit/app-info.mm +98 -0
  21. package/native/appkit/app-updater.mm +128 -0
  22. package/native/appkit/app-windows.h +81 -0
  23. package/native/appkit/app-windows.mm +318 -0
  24. package/native/appkit/appkit-backend.h +73 -0
  25. package/native/appkit/appkit-backend.mm +276 -0
  26. package/native/appkit/apple-authentication.h +19 -0
  27. package/native/appkit/apple-authentication.mm +248 -0
  28. package/native/appkit/apple-button-view.h +10 -0
  29. package/native/appkit/apple-button-view.mm +37 -0
  30. package/native/appkit/apple-id-button.h +16 -0
  31. package/native/appkit/apple-id-button.mm +50 -0
  32. package/native/appkit/audio.h +16 -0
  33. package/native/appkit/audio.mm +275 -0
  34. package/native/appkit/battery.h +13 -0
  35. package/native/appkit/battery.mm +92 -0
  36. package/native/appkit/bookmarks.h +17 -0
  37. package/native/appkit/bookmarks.mm +145 -0
  38. package/native/appkit/calendar.h +25 -0
  39. package/native/appkit/calendar.mm +660 -0
  40. package/native/appkit/camera-sheet.h +59 -0
  41. package/native/appkit/camera-sheet.mm +344 -0
  42. package/native/appkit/camera-view.h +15 -0
  43. package/native/appkit/camera-view.mm +134 -0
  44. package/native/appkit/camera.h +61 -0
  45. package/native/appkit/camera.mm +496 -0
  46. package/native/appkit/cg-canvas.h +24 -0
  47. package/native/appkit/cg-canvas.mm +663 -0
  48. package/native/appkit/component-view.h +17 -0
  49. package/native/appkit/crypto.h +19 -0
  50. package/native/appkit/crypto.mm +78 -0
  51. package/native/appkit/desktop-expo.h +29 -0
  52. package/native/appkit/desktop-expo.mm +385 -0
  53. package/native/appkit/expo-image-drawing.h +46 -0
  54. package/native/appkit/expo-image-drawing.mm +131 -0
  55. package/native/appkit/expo-image-view.h +15 -0
  56. package/native/appkit/expo-image-view.mm +182 -0
  57. package/native/appkit/expo-ui-renderer.h +68 -0
  58. package/native/appkit/expo-ui-renderer.mm +101 -0
  59. package/native/appkit/expo-ui-view.h +26 -0
  60. package/native/appkit/expo-ui-view.mm +134 -0
  61. package/native/appkit/expo-ui.swift +1284 -0
  62. package/native/appkit/file-system.h +16 -0
  63. package/native/appkit/file-system.mm +140 -0
  64. package/native/appkit/fonts.h +12 -0
  65. package/native/appkit/fonts.mm +48 -0
  66. package/native/appkit/geocoder.h +19 -0
  67. package/native/appkit/geocoder.mm +101 -0
  68. package/native/appkit/glass-effect.h +22 -0
  69. package/native/appkit/glass-effect.mm +63 -0
  70. package/native/appkit/glass-view.h +8 -0
  71. package/native/appkit/glass-view.mm +65 -0
  72. package/native/appkit/header-toolbar.h +60 -0
  73. package/native/appkit/header-toolbar.mm +632 -0
  74. package/native/appkit/host-view.h +54 -0
  75. package/native/appkit/host-view.mm +1228 -0
  76. package/native/appkit/image-manipulator.h +14 -0
  77. package/native/appkit/image-manipulator.mm +191 -0
  78. package/native/appkit/image-request.h +22 -0
  79. package/native/appkit/image-request.mm +62 -0
  80. package/native/appkit/image-view.h +8 -0
  81. package/native/appkit/image-view.mm +142 -0
  82. package/native/appkit/input-replay.h +25 -0
  83. package/native/appkit/input-replay.mm +418 -0
  84. package/native/appkit/language-model.h +14 -0
  85. package/native/appkit/language-model.mm +99 -0
  86. package/native/appkit/language-model.swift +472 -0
  87. package/native/appkit/local-authentication.h +15 -0
  88. package/native/appkit/local-authentication.mm +133 -0
  89. package/native/appkit/location.h +16 -0
  90. package/native/appkit/location.mm +214 -0
  91. package/native/appkit/main.mm +1475 -0
  92. package/native/appkit/map-view.h +15 -0
  93. package/native/appkit/map-view.mm +109 -0
  94. package/native/appkit/maps.h +35 -0
  95. package/native/appkit/maps.mm +343 -0
  96. package/native/appkit/menu-bar.h +22 -0
  97. package/native/appkit/menu-bar.mm +208 -0
  98. package/native/appkit/modal-focus.h +16 -0
  99. package/native/appkit/modal-focus.mm +26 -0
  100. package/native/appkit/modal-host-view.h +79 -0
  101. package/native/appkit/modal-host-view.mm +442 -0
  102. package/native/appkit/native-controls.h +22 -0
  103. package/native/appkit/native-controls.mm +431 -0
  104. package/native/appkit/network-state.h +12 -0
  105. package/native/appkit/network-state.mm +64 -0
  106. package/native/appkit/network.h +14 -0
  107. package/native/appkit/network.mm +396 -0
  108. package/native/appkit/notifications.h +19 -0
  109. package/native/appkit/notifications.mm +309 -0
  110. package/native/appkit/photo-library.h +33 -0
  111. package/native/appkit/photo-library.mm +548 -0
  112. package/native/appkit/pointer-buttons.h +32 -0
  113. package/native/appkit/presentation-label.h +15 -0
  114. package/native/appkit/presentation-label.mm +12 -0
  115. package/native/appkit/presented-screen.h +19 -0
  116. package/native/appkit/presented-screen.mm +181 -0
  117. package/native/appkit/printing.h +42 -0
  118. package/native/appkit/printing.mm +393 -0
  119. package/native/appkit/push.h +19 -0
  120. package/native/appkit/push.mm +81 -0
  121. package/native/appkit/red-box.h +26 -0
  122. package/native/appkit/red-box.mm +387 -0
  123. package/native/appkit/screen-capture.h +21 -0
  124. package/native/appkit/screen-capture.mm +92 -0
  125. package/native/appkit/scroll-view.h +13 -0
  126. package/native/appkit/scroll-view.mm +251 -0
  127. package/native/appkit/scrollers.h +25 -0
  128. package/native/appkit/scrollers.mm +43 -0
  129. package/native/appkit/search-field.h +34 -0
  130. package/native/appkit/search-field.mm +291 -0
  131. package/native/appkit/search-toolbar-item.h +19 -0
  132. package/native/appkit/search-toolbar-item.mm +269 -0
  133. package/native/appkit/share-picker.h +34 -0
  134. package/native/appkit/share-picker.mm +121 -0
  135. package/native/appkit/split-view.h +77 -0
  136. package/native/appkit/split-view.mm +1206 -0
  137. package/native/appkit/store-review.h +13 -0
  138. package/native/appkit/store-review.mm +28 -0
  139. package/native/appkit/surface-view.h +45 -0
  140. package/native/appkit/surface-view.mm +562 -0
  141. package/native/appkit/svg-view.h +18 -0
  142. package/native/appkit/svg-view.mm +103 -0
  143. package/native/appkit/symbol-drawing.h +24 -0
  144. package/native/appkit/symbol-drawing.mm +117 -0
  145. package/native/appkit/symbol-view.h +8 -0
  146. package/native/appkit/symbol-view.mm +33 -0
  147. package/native/appkit/system-access.h +34 -0
  148. package/native/appkit/system-access.mm +210 -0
  149. package/native/appkit/system.h +24 -0
  150. package/native/appkit/system.mm +403 -0
  151. package/native/appkit/tab-walk.h +31 -0
  152. package/native/appkit/tab-walk.mm +64 -0
  153. package/native/appkit/test-launch.h +36 -0
  154. package/native/appkit/test-launch.mm +115 -0
  155. package/native/appkit/text-input-field.h +81 -0
  156. package/native/appkit/text-input-field.mm +525 -0
  157. package/native/appkit/text-layout.h +58 -0
  158. package/native/appkit/text-layout.mm +485 -0
  159. package/native/appkit/text-recognition.h +13 -0
  160. package/native/appkit/text-recognition.mm +102 -0
  161. package/native/appkit/tracking.h +13 -0
  162. package/native/appkit/tracking.mm +51 -0
  163. package/native/appkit/tray-and-dock.h +45 -0
  164. package/native/appkit/tray-and-dock.mm +130 -0
  165. package/native/appkit/undo.h +23 -0
  166. package/native/appkit/undo.mm +125 -0
  167. package/native/appkit/updates.h +17 -0
  168. package/native/appkit/updates.mm +71 -0
  169. package/native/appkit/video-view.h +10 -0
  170. package/native/appkit/video-view.mm +42 -0
  171. package/native/appkit/video.h +38 -0
  172. package/native/appkit/video.mm +404 -0
  173. package/native/appkit/web-browser.h +12 -0
  174. package/native/appkit/web-browser.mm +97 -0
  175. package/native/appkit/web-view-component.h +10 -0
  176. package/native/appkit/web-view-component.mm +62 -0
  177. package/native/appkit/web-view.h +31 -0
  178. package/native/appkit/web-view.mm +401 -0
  179. package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
  180. package/native/cmake/libraries/react-native-worklets.cmake +46 -0
  181. package/native/cmake/libraries.cmake +109 -0
  182. package/native/cmake/linked-modules.cmake +112 -0
  183. package/native/cmake/react-native.cmake +175 -0
  184. package/native/cmake/runtime-dependencies.cmake +34 -0
  185. package/native/cmake/swift.cmake +43 -0
  186. package/native/cmake/third-party.cmake +74 -0
  187. package/native/core/include/ngn/absent-views.h +27 -0
  188. package/native/core/include/ngn/accessibility.h +140 -0
  189. package/native/core/include/ngn/address-search.h +69 -0
  190. package/native/core/include/ngn/animation-frames.h +39 -0
  191. package/native/core/include/ngn/app-info.h +52 -0
  192. package/native/core/include/ngn/app-menus.h +151 -0
  193. package/native/core/include/ngn/app-windows.h +181 -0
  194. package/native/core/include/ngn/apple-authentication.h +116 -0
  195. package/native/core/include/ngn/asset.h +28 -0
  196. package/native/core/include/ngn/audio.h +125 -0
  197. package/native/core/include/ngn/back-swipe.h +121 -0
  198. package/native/core/include/ngn/battery.h +48 -0
  199. package/native/core/include/ngn/bytes.h +59 -0
  200. package/native/core/include/ngn/calendar.h +180 -0
  201. package/native/core/include/ngn/camera.h +222 -0
  202. package/native/core/include/ngn/control-sizes.h +111 -0
  203. package/native/core/include/ngn/core-modules.h +108 -0
  204. package/native/core/include/ngn/crypto.h +38 -0
  205. package/native/core/include/ngn/default-menu.h +91 -0
  206. package/native/core/include/ngn/desktop-expo.h +193 -0
  207. package/native/core/include/ngn/desktop-modal.h +109 -0
  208. package/native/core/include/ngn/desktop-props.h +75 -0
  209. package/native/core/include/ngn/desktop-view-props.h +110 -0
  210. package/native/core/include/ngn/dev-server.h +33 -0
  211. package/native/core/include/ngn/dom-web-view.h +53 -0
  212. package/native/core/include/ngn/download.h +24 -0
  213. package/native/core/include/ngn/expo-image.h +192 -0
  214. package/native/core/include/ngn/expo-ui.h +133 -0
  215. package/native/core/include/ngn/file-system.h +274 -0
  216. package/native/core/include/ngn/focus-order.h +63 -0
  217. package/native/core/include/ngn/fonts.h +64 -0
  218. package/native/core/include/ngn/gestures.h +305 -0
  219. package/native/core/include/ngn/glass.h +67 -0
  220. package/native/core/include/ngn/header-bar.h +112 -0
  221. package/native/core/include/ngn/host-state.h +243 -0
  222. package/native/core/include/ngn/image-manipulator.h +70 -0
  223. package/native/core/include/ngn/input-dispatcher.h +211 -0
  224. package/native/core/include/ngn/input-script.h +56 -0
  225. package/native/core/include/ngn/input.h +112 -0
  226. package/native/core/include/ngn/key-value-store.h +41 -0
  227. package/native/core/include/ngn/language-model.h +69 -0
  228. package/native/core/include/ngn/launch-frame.h +47 -0
  229. package/native/core/include/ngn/launch-settings.h +98 -0
  230. package/native/core/include/ngn/layout-animations.h +24 -0
  231. package/native/core/include/ngn/library-components.h +13 -0
  232. package/native/core/include/ngn/library-glue.h +53 -0
  233. package/native/core/include/ngn/live-regions.h +54 -0
  234. package/native/core/include/ngn/local-authentication.h +74 -0
  235. package/native/core/include/ngn/location.h +89 -0
  236. package/native/core/include/ngn/maps.h +226 -0
  237. package/native/core/include/ngn/module-registry.h +51 -0
  238. package/native/core/include/ngn/mounting-backend.h +96 -0
  239. package/native/core/include/ngn/mounting-manager.h +155 -0
  240. package/native/core/include/ngn/native-controls.h +161 -0
  241. package/native/core/include/ngn/native-module.h +109 -0
  242. package/native/core/include/ngn/network-state.h +44 -0
  243. package/native/core/include/ngn/network.h +100 -0
  244. package/native/core/include/ngn/notifications.h +185 -0
  245. package/native/core/include/ngn/photo-library.h +134 -0
  246. package/native/core/include/ngn/printing.h +69 -0
  247. package/native/core/include/ngn/processes.h +114 -0
  248. package/native/core/include/ngn/red-box.h +119 -0
  249. package/native/core/include/ngn/reload-thread.h +258 -0
  250. package/native/core/include/ngn/runtime.h +163 -0
  251. package/native/core/include/ngn/safe-areas.h +82 -0
  252. package/native/core/include/ngn/screen-capture.h +35 -0
  253. package/native/core/include/ngn/screen-presentation.h +85 -0
  254. package/native/core/include/ngn/screen-stacks.h +80 -0
  255. package/native/core/include/ngn/scrollers.h +106 -0
  256. package/native/core/include/ngn/sharing.h +39 -0
  257. package/native/core/include/ngn/spawn-rules.h +78 -0
  258. package/native/core/include/ngn/splash-screen.h +41 -0
  259. package/native/core/include/ngn/split-view.h +270 -0
  260. package/native/core/include/ngn/sqlite.h +38 -0
  261. package/native/core/include/ngn/store-review.h +26 -0
  262. package/native/core/include/ngn/surface-book.h +101 -0
  263. package/native/core/include/ngn/surface-metrics.h +51 -0
  264. package/native/core/include/ngn/svg.h +434 -0
  265. package/native/core/include/ngn/symbol.h +90 -0
  266. package/native/core/include/ngn/system.h +102 -0
  267. package/native/core/include/ngn/tabs-host.h +171 -0
  268. package/native/core/include/ngn/text-input.h +134 -0
  269. package/native/core/include/ngn/text-measurer.h +38 -0
  270. package/native/core/include/ngn/text-recognition.h +55 -0
  271. package/native/core/include/ngn/tracking.h +34 -0
  272. package/native/core/include/ngn/tray-and-dock.h +107 -0
  273. package/native/core/include/ngn/undo.h +37 -0
  274. package/native/core/include/ngn/updates.h +253 -0
  275. package/native/core/include/ngn/video.h +126 -0
  276. package/native/core/include/ngn/view-box.h +126 -0
  277. package/native/core/include/ngn/view-transform.h +26 -0
  278. package/native/core/include/ngn/view-tree.h +85 -0
  279. package/native/core/include/ngn/web-browser.h +45 -0
  280. package/native/core/include/ngn/web-view.h +137 -0
  281. package/native/core/include/ngn/worklet-host.h +98 -0
  282. package/native/core/src/absent-views.cpp +41 -0
  283. package/native/core/src/accessibility.cpp +374 -0
  284. package/native/core/src/animation-frames.cpp +49 -0
  285. package/native/core/src/app-menus.cpp +359 -0
  286. package/native/core/src/app-windows.cpp +444 -0
  287. package/native/core/src/back-swipe.cpp +98 -0
  288. package/native/core/src/bytes.cpp +158 -0
  289. package/native/core/src/control-sizes.cpp +232 -0
  290. package/native/core/src/default-menu.cpp +96 -0
  291. package/native/core/src/desktop-modal.cpp +162 -0
  292. package/native/core/src/desktop-view-props.cpp +80 -0
  293. package/native/core/src/dev-server-helper.cpp +152 -0
  294. package/native/core/src/dev-server.cpp +103 -0
  295. package/native/core/src/dom-web-view.cpp +45 -0
  296. package/native/core/src/download.cpp +53 -0
  297. package/native/core/src/expo-image.cpp +548 -0
  298. package/native/core/src/expo-ui.cpp +203 -0
  299. package/native/core/src/focus-order.cpp +441 -0
  300. package/native/core/src/gestures.cpp +1806 -0
  301. package/native/core/src/glass.cpp +49 -0
  302. package/native/core/src/header-bar.cpp +104 -0
  303. package/native/core/src/host-services.cpp +50 -0
  304. package/native/core/src/host-state.cpp +61 -0
  305. package/native/core/src/input-dispatcher.cpp +727 -0
  306. package/native/core/src/input-script.cpp +123 -0
  307. package/native/core/src/key-value-store.cpp +169 -0
  308. package/native/core/src/launch-settings.cpp +148 -0
  309. package/native/core/src/layout-animations.cpp +62 -0
  310. package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
  311. package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
  312. package/native/core/src/libraries/react-native-worklets.cpp +192 -0
  313. package/native/core/src/libraries/rnscreens.cpp +125 -0
  314. package/native/core/src/libraries/rnsvg.cpp +358 -0
  315. package/native/core/src/libraries/safeareacontext.cpp +70 -0
  316. package/native/core/src/libraries/worklets-idle-frames.js +70 -0
  317. package/native/core/src/library-glue.cpp +35 -0
  318. package/native/core/src/live-regions.cpp +122 -0
  319. package/native/core/src/module-registry.cpp +50 -0
  320. package/native/core/src/modules/accessibility-manager.cpp +98 -0
  321. package/native/core/src/modules/address-search.cpp +219 -0
  322. package/native/core/src/modules/alert-manager.cpp +140 -0
  323. package/native/core/src/modules/app-info.cpp +74 -0
  324. package/native/core/src/modules/app-modules.cpp +412 -0
  325. package/native/core/src/modules/apple-authentication.cpp +188 -0
  326. package/native/core/src/modules/asset.cpp +111 -0
  327. package/native/core/src/modules/async-storage.cpp +192 -0
  328. package/native/core/src/modules/audio.cpp +541 -0
  329. package/native/core/src/modules/battery.cpp +88 -0
  330. package/native/core/src/modules/calendar.cpp +457 -0
  331. package/native/core/src/modules/camera.cpp +401 -0
  332. package/native/core/src/modules/core-modules.cpp +62 -0
  333. package/native/core/src/modules/crypto.cpp +117 -0
  334. package/native/core/src/modules/desktop-expo.cpp +527 -0
  335. package/native/core/src/modules/file-system.cpp +941 -0
  336. package/native/core/src/modules/fonts.cpp +163 -0
  337. package/native/core/src/modules/image-loader.cpp +105 -0
  338. package/native/core/src/modules/image-manipulator.cpp +305 -0
  339. package/native/core/src/modules/language-model.cpp +195 -0
  340. package/native/core/src/modules/local-authentication.cpp +140 -0
  341. package/native/core/src/modules/location.cpp +186 -0
  342. package/native/core/src/modules/maps.cpp +500 -0
  343. package/native/core/src/modules/network-state.cpp +72 -0
  344. package/native/core/src/modules/networking.cpp +387 -0
  345. package/native/core/src/modules/notifications.cpp +549 -0
  346. package/native/core/src/modules/photo-library.cpp +385 -0
  347. package/native/core/src/modules/printing.cpp +201 -0
  348. package/native/core/src/modules/screen-capture.cpp +71 -0
  349. package/native/core/src/modules/sharing.cpp +59 -0
  350. package/native/core/src/modules/splash-screen.cpp +70 -0
  351. package/native/core/src/modules/sqlite.cpp +421 -0
  352. package/native/core/src/modules/store-review.cpp +23 -0
  353. package/native/core/src/modules/text-recognition.cpp +107 -0
  354. package/native/core/src/modules/tracking.cpp +46 -0
  355. package/native/core/src/modules/undo.cpp +59 -0
  356. package/native/core/src/modules/video.cpp +366 -0
  357. package/native/core/src/modules/web-browser.cpp +100 -0
  358. package/native/core/src/modules/web-socket.cpp +266 -0
  359. package/native/core/src/mounting-manager.cpp +384 -0
  360. package/native/core/src/native-controls.cpp +129 -0
  361. package/native/core/src/native-module.cpp +250 -0
  362. package/native/core/src/network.cpp +219 -0
  363. package/native/core/src/posix-launcher.cpp +366 -0
  364. package/native/core/src/processes.cpp +324 -0
  365. package/native/core/src/red-box-errors.js +83 -0
  366. package/native/core/src/red-box.cpp +306 -0
  367. package/native/core/src/runtime.cpp +420 -0
  368. package/native/core/src/safe-areas.cpp +137 -0
  369. package/native/core/src/screen-presentation.cpp +61 -0
  370. package/native/core/src/screen-stacks.cpp +254 -0
  371. package/native/core/src/scrollers.cpp +181 -0
  372. package/native/core/src/spawn-rules.cpp +292 -0
  373. package/native/core/src/split-view.cpp +343 -0
  374. package/native/core/src/svg.cpp +2190 -0
  375. package/native/core/src/symbol.cpp +94 -0
  376. package/native/core/src/tabs-host.cpp +234 -0
  377. package/native/core/src/text-input.cpp +139 -0
  378. package/native/core/src/text-layout-manager.cpp +63 -0
  379. package/native/core/src/tray-and-dock.cpp +154 -0
  380. package/native/core/src/updates.cpp +965 -0
  381. package/native/core/src/view-box.cpp +788 -0
  382. package/native/core/src/view-transform.cpp +45 -0
  383. package/native/core/src/view-tree.cpp +234 -0
  384. package/native/core/src/web-view.cpp +143 -0
  385. package/native/core/src/worklet-host.cpp +10 -0
  386. package/native/scripts/build-hermes.sh +44 -0
  387. package/native/scripts/build.sh +175 -0
  388. package/native/scripts/cache-dir.sh +18 -0
  389. package/native/scripts/codegen-library.sh +193 -0
  390. package/native/scripts/compiler-launcher.sh +17 -0
  391. package/native/scripts/fetch.sh +174 -0
  392. package/native/scripts/hermes-built.sh +10 -0
  393. package/native/scripts/host-dir.sh +16 -0
  394. package/native/scripts/launch-probe.sh +60 -0
  395. package/native/scripts/macos-target.sh +8 -0
  396. package/native/scripts/make-prebuilt.mjs +360 -0
  397. package/native/scripts/prebuilt-host-cache.mjs +158 -0
  398. package/native/shim/openssl/sha.h +22 -0
  399. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
  400. package/package.json +73 -1
  401. package/prebuilt.json +33 -0
@@ -0,0 +1,59 @@
1
+ #include "ngn/sharing.h"
2
+
3
+ #include <atomic>
4
+
5
+ namespace ngn {
6
+
7
+ namespace {
8
+
9
+ std::optional<std::string> field(const folly::dynamic &request, const char *key) {
10
+ const auto *value = request.isObject() ? request.get_ptr(key) : nullptr;
11
+ return value != nullptr && value->isString() && !value->asString().empty() ? std::optional(value->asString()) : std::nullopt;
12
+ }
13
+
14
+ } // namespace
15
+
16
+ std::shared_ptr<NativeModule> makeSharingModule(std::shared_ptr<SharePicker> picker, bool noPrompts, std::shared_ptr<FileAccess> files) {
17
+ struct Sharing : NativeModule {
18
+ Sharing(std::shared_ptr<SharePicker> picker, bool noPrompts, std::shared_ptr<FileAccess> files) : NativeModule("NgNativeSharing") {
19
+ auto showing = std::make_shared<std::atomic<bool>>(false);
20
+ promise("share", [picker, showing, noPrompts, files](const folly::dynamic &args, JsPromise promise) {
21
+ const folly::dynamic request = args.empty() ? folly::dynamic(nullptr) : args[0];
22
+ SharePicker::Request content{.message = field(request, "message"), .url = field(request, "url"), .title = field(request, "title")};
23
+ if (!content.message && !content.url) {
24
+ promise.reject("Sharing.share: a request needs a message or a url");
25
+ return;
26
+ }
27
+ // A file only inside the file boundary, shared as exactly the file checked.
28
+ std::shared_ptr<FileAccess::Use> use;
29
+ if (content.url) {
30
+ try {
31
+ auto source = readableSource(files.get(), *content.url);
32
+ content.url = source.uri;
33
+ use = source.use;
34
+ } catch (const FileRefused &refused) {
35
+ promise.reject(std::string("Sharing.share: ") + refused.what());
36
+ return;
37
+ }
38
+ }
39
+ if (noPrompts) {
40
+ promise.resolve(folly::dynamic::object("action", "dismissedAction")("activityType", nullptr));
41
+ return;
42
+ }
43
+ if (showing->exchange(true)) {
44
+ promise.reject("Sharing.share: the share picker is already showing");
45
+ return;
46
+ }
47
+ picker->show(content, [promise, showing, use](std::optional<std::string> service) {
48
+ showing->store(false);
49
+ promise.resolve(
50
+ service ? folly::dynamic::object("action", "sharedAction")("activityType", *service)
51
+ : folly::dynamic::object("action", "dismissedAction")("activityType", nullptr));
52
+ });
53
+ });
54
+ }
55
+ };
56
+ return std::make_shared<Sharing>(std::move(picker), noPrompts, std::move(files));
57
+ }
58
+
59
+ } // namespace ngn
@@ -0,0 +1,70 @@
1
+ #include "ngn/splash-screen.h"
2
+
3
+ namespace ngn {
4
+
5
+ bool SplashGate::hold() {
6
+ std::lock_guard lock(mutex_);
7
+ if (shown_) {
8
+ return false;
9
+ }
10
+ held_ = true;
11
+ return true;
12
+ }
13
+
14
+ void SplashGate::release() {
15
+ std::vector<std::function<void()>> waiting;
16
+ {
17
+ std::lock_guard lock(mutex_);
18
+ held_ = false;
19
+ waiting.swap(waiting_);
20
+ // The held windows show now, so a later hold comes too late.
21
+ if (!waiting.empty()) {
22
+ shown_ = true;
23
+ }
24
+ }
25
+ for (auto &show : waiting) {
26
+ show();
27
+ }
28
+ }
29
+
30
+ void SplashGate::show(std::function<void()> show) {
31
+ {
32
+ std::lock_guard lock(mutex_);
33
+ if (held_) {
34
+ waiting_.push_back(std::move(show));
35
+ return;
36
+ }
37
+ shown_ = true;
38
+ }
39
+ show();
40
+ }
41
+
42
+ bool SplashGate::held() {
43
+ std::lock_guard lock(mutex_);
44
+ return held_;
45
+ }
46
+
47
+ std::shared_ptr<NativeModule> makeSplashScreenModule(std::shared_ptr<SplashGate> gate) {
48
+ struct SplashScreen : NativeModule {
49
+ std::shared_ptr<SplashGate> gate;
50
+ explicit SplashScreen(std::shared_ptr<SplashGate> from) : NativeModule("NgNativeSplashScreen"), gate(std::move(from)) {
51
+ auto splash = gate;
52
+ promise("preventAutoHideAsync", [splash](const folly::dynamic &, JsPromise promise) { promise.resolve(splash->hold()); });
53
+ promise("hideAsync", [splash](const folly::dynamic &, JsPromise promise) {
54
+ splash->release();
55
+ promise.resolve(nullptr);
56
+ });
57
+ sync("hide", [splash](const folly::dynamic &) {
58
+ splash->release();
59
+ return folly::dynamic(nullptr);
60
+ });
61
+ }
62
+ ~SplashScreen() override {
63
+ // A reloaded app is no reason to keep its windows hidden.
64
+ gate->release();
65
+ }
66
+ };
67
+ return std::make_shared<SplashScreen>(std::move(gate));
68
+ }
69
+
70
+ } // namespace ngn
@@ -0,0 +1,421 @@
1
+ #include "ngn/sqlite.h"
2
+
3
+ #include "ngn/bytes.h"
4
+ #include "ngn/file-system.h"
5
+
6
+ #include <sqlite3.h>
7
+
8
+ #include <cmath>
9
+ #include <filesystem>
10
+ #include <map>
11
+ #include <mutex>
12
+ #include <stdexcept>
13
+ #include <string_view>
14
+ #include <vector>
15
+
16
+ namespace fs = std::filesystem;
17
+
18
+ namespace ngn {
19
+
20
+ namespace {
21
+
22
+ struct SqlError : std::runtime_error {
23
+ using std::runtime_error::runtime_error;
24
+ };
25
+
26
+ std::string stringArg(const folly::dynamic &args, size_t index) {
27
+ return args.size() > index && args[index].isString() ? args[index].asString() : std::string{};
28
+ }
29
+
30
+ // A path or a file:// uri, as a directory argument may be; one that is neither is refused.
31
+ std::string pathOf(const std::string &where) {
32
+ auto path = pathOfUri(where);
33
+ if (!path) {
34
+ throw FileRefused(where, true);
35
+ }
36
+ return *path;
37
+ }
38
+
39
+ // The file a database name names in `directory`, refusing one that would leave it.
40
+ fs::path fileFor(const std::string &name, const std::string &directory) {
41
+ fs::path relative(name);
42
+ if (name.empty() || relative.is_absolute()) {
43
+ throw SqlError("Invalid database name '" + name + "': it must be a file name inside the database directory");
44
+ }
45
+ for (const auto &part : relative) {
46
+ if (part == "..") {
47
+ throw SqlError("Invalid database name '" + name + "': it must be a file name inside the database directory");
48
+ }
49
+ }
50
+ return fs::path(directory) / relative;
51
+ }
52
+
53
+ class Statement {
54
+ public:
55
+ Statement(sqlite3 *db, const std::string &source) : db_(db) {
56
+ if (sqlite3_prepare_v2(db, source.c_str(), static_cast<int>(source.size()), &stmt_, nullptr) != SQLITE_OK) {
57
+ throw SqlError(failure(db));
58
+ }
59
+ if (!stmt_) {
60
+ throw SqlError("The statement is empty");
61
+ }
62
+ }
63
+ ~Statement() {
64
+ sqlite3_finalize(stmt_);
65
+ }
66
+ Statement(const Statement &) = delete;
67
+ Statement &operator=(const Statement &) = delete;
68
+
69
+ void bind(const folly::dynamic &params) {
70
+ if (params.isArray()) {
71
+ // One past the statement's params fails to bind (SQLITE_RANGE).
72
+ for (size_t i = 0; i < params.size(); i++) {
73
+ bindOne(static_cast<int>(i) + 1, params[i]);
74
+ }
75
+ } else if (params.isObject()) {
76
+ for (const auto &[key, value] : params.items()) {
77
+ if (!key.isString()) {
78
+ continue;
79
+ }
80
+ if (int index = sqlite3_bind_parameter_index(stmt_, key.asString().c_str())) {
81
+ bindOne(index, value);
82
+ }
83
+ }
84
+ } else if (!params.isNull()) {
85
+ bindOne(1, params);
86
+ }
87
+ }
88
+
89
+ // Steps once: a row, or false at the end.
90
+ bool step() {
91
+ int code = sqlite3_step(stmt_);
92
+ if (code == SQLITE_ROW) {
93
+ return true;
94
+ }
95
+ if (code == SQLITE_DONE) {
96
+ return false;
97
+ }
98
+ throw SqlError(failure(db_));
99
+ }
100
+
101
+ folly::dynamic row() const {
102
+ folly::dynamic row = folly::dynamic::object();
103
+ for (int i = 0, n = sqlite3_column_count(stmt_); i < n; i++) {
104
+ row[sqlite3_column_name(stmt_, i)] = column(i);
105
+ }
106
+ return row;
107
+ }
108
+
109
+ static std::string failure(sqlite3 *db) {
110
+ return "Error code " + std::to_string(sqlite3_extended_errcode(db)) + ": " + sqlite3_errmsg(db);
111
+ }
112
+
113
+ private:
114
+ void bindOne(int index, const folly::dynamic &value) {
115
+ int code = SQLITE_OK;
116
+ if (value.isNull()) {
117
+ code = sqlite3_bind_null(stmt_, index);
118
+ } else if (value.isBool()) {
119
+ code = sqlite3_bind_int64(stmt_, index, value.asBool() ? 1 : 0);
120
+ } else if (value.isInt()) {
121
+ code = sqlite3_bind_int64(stmt_, index, value.asInt());
122
+ } else if (value.isDouble()) {
123
+ // JavaScript's numbers are all doubles: a whole one binds as an integer, as it was written.
124
+ double number = value.asDouble();
125
+ if (std::trunc(number) == number && std::abs(number) < 9007199254740992.0) {
126
+ code = sqlite3_bind_int64(stmt_, index, static_cast<sqlite3_int64>(number));
127
+ } else {
128
+ code = sqlite3_bind_double(stmt_, index, number);
129
+ }
130
+ } else if (value.isString()) {
131
+ const auto &text = value.getString();
132
+ code = sqlite3_bind_text(stmt_, index, text.data(), static_cast<int>(text.size()), SQLITE_TRANSIENT);
133
+ } else if (value.isObject() && value.count("base64") && value["base64"].isString()) {
134
+ auto bytes = base64Decode(value["base64"].asString());
135
+ if (!bytes) {
136
+ throw SqlError("A blob param's base64 is not valid");
137
+ }
138
+ code = sqlite3_bind_blob64(stmt_, index, bytes->data(), bytes->size(), SQLITE_TRANSIENT);
139
+ } else {
140
+ throw SqlError("A param must be null, a boolean, a number, a string or {base64}");
141
+ }
142
+ if (code != SQLITE_OK) {
143
+ throw SqlError(failure(db_));
144
+ }
145
+ }
146
+
147
+ folly::dynamic column(int i) const {
148
+ switch (sqlite3_column_type(stmt_, i)) {
149
+ case SQLITE_INTEGER:
150
+ return static_cast<int64_t>(sqlite3_column_int64(stmt_, i));
151
+ case SQLITE_FLOAT:
152
+ return sqlite3_column_double(stmt_, i);
153
+ case SQLITE_TEXT:
154
+ return std::string(
155
+ reinterpret_cast<const char *>(sqlite3_column_text(stmt_, i)), static_cast<size_t>(sqlite3_column_bytes(stmt_, i)));
156
+ case SQLITE_BLOB: {
157
+ auto data = static_cast<const char *>(sqlite3_column_blob(stmt_, i));
158
+ std::string bytes(data ? data : "", static_cast<size_t>(sqlite3_column_bytes(stmt_, i)));
159
+ return folly::dynamic::object("base64", base64Encode(bytes));
160
+ }
161
+ default:
162
+ return nullptr;
163
+ }
164
+ }
165
+
166
+ sqlite3 *db_;
167
+ sqlite3_stmt *stmt_{nullptr};
168
+ };
169
+
170
+ class Databases {
171
+ public:
172
+ Databases(std::string defaultDirectory, std::shared_ptr<FileAccess> files)
173
+ : defaultDirectory_(std::move(defaultDirectory)), files_(std::move(files)) {}
174
+ ~Databases() {
175
+ for (auto &[id, open] : open_) {
176
+ sqlite3_close_v2(open.db);
177
+ }
178
+ }
179
+
180
+ const std::string &defaultDirectory() const {
181
+ return defaultDirectory_;
182
+ }
183
+
184
+ folly::dynamic open(const folly::dynamic &args) {
185
+ auto name = stringArg(args, 0);
186
+ std::string path = ":memory:";
187
+ std::shared_ptr<FileAccess::Use> use;
188
+ if (name != ":memory:") {
189
+ auto file = fileFor(name, directoryArg(args, 2));
190
+ use = writable(file);
191
+ fs::create_directories(file.parent_path());
192
+ path = file.string();
193
+ }
194
+ sqlite3 *db = nullptr;
195
+ int code = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, nullptr);
196
+ if (code != SQLITE_OK) {
197
+ std::string message = db ? Statement::failure(db) : "Error code " + std::to_string(code);
198
+ sqlite3_close_v2(db);
199
+ throw SqlError("Could not open database '" + name + "': " + message);
200
+ }
201
+ // SQL opens files too (ATTACH, and VACUUM INTO through it): only where the app may write.
202
+ sqlite3_set_authorizer(db, &Databases::authorize, this);
203
+ if (path != ":memory:") {
204
+ sqlite3_exec(db, "PRAGMA journal_mode = WAL", nullptr, nullptr, nullptr);
205
+ }
206
+ std::lock_guard lock(mutex_);
207
+ int id = ++next_;
208
+ open_[id] = Open{.db = db, .path = path, .use = std::move(use)};
209
+ return folly::dynamic::object("id", id)("databasePath", path)("databaseName", name);
210
+ }
211
+
212
+ void close(const folly::dynamic &args) {
213
+ std::lock_guard lock(mutex_);
214
+ auto it = open_.find(idOf(args));
215
+ if (it == open_.end()) {
216
+ throw SqlError("The database is closed");
217
+ }
218
+ sqlite3_close_v2(it->second.db);
219
+ open_.erase(it);
220
+ }
221
+
222
+ void remove(const folly::dynamic &args) {
223
+ auto name = stringArg(args, 0);
224
+ auto file = fileFor(name, directoryArg(args, 1));
225
+ auto use = writable(file);
226
+ {
227
+ // The same file however its folder is spelled: through a symlink, a `.` or a trailing slash.
228
+ // Open as a connection's own database, or attached to one.
229
+ std::lock_guard lock(mutex_);
230
+ for (const auto &[id, open] : open_) {
231
+ for (const auto &path : filesOf(open.db)) {
232
+ std::error_code error;
233
+ if (path == file.string() || fs::equivalent(path, file, error)) {
234
+ throw SqlError("Unable to delete database '" + name + "' that is currently open. Close it before deleting it.");
235
+ }
236
+ }
237
+ }
238
+ }
239
+ if (!fs::exists(file)) {
240
+ throw SqlError("Database '" + name + "' not found");
241
+ }
242
+ fs::remove(file);
243
+ for (const char *suffix : {"-wal", "-shm", "-journal"}) {
244
+ fs::remove(file.string() + suffix);
245
+ }
246
+ }
247
+
248
+ sqlite3 *db(const folly::dynamic &args) {
249
+ std::lock_guard lock(mutex_);
250
+ auto it = open_.find(idOf(args));
251
+ if (it == open_.end()) {
252
+ throw SqlError("The database is closed");
253
+ }
254
+ return it->second.db;
255
+ }
256
+
257
+ private:
258
+ struct Open {
259
+ sqlite3 *db;
260
+ std::string path;
261
+ // A pick of an earlier launch holding the file, open while the database is.
262
+ std::shared_ptr<FileAccess::Use> use;
263
+ };
264
+
265
+ // The files a connection has open: its own database and those attached to it. A temporary or
266
+ // in-memory database has none.
267
+ static std::vector<std::string> filesOf(sqlite3 *db) {
268
+ std::vector<std::string> files;
269
+ sqlite3_stmt *stmt = nullptr;
270
+ if (sqlite3_prepare_v2(db, "PRAGMA database_list", -1, &stmt, nullptr) != SQLITE_OK) {
271
+ sqlite3_finalize(stmt);
272
+ throw SqlError("Could not list the open databases: " + Statement::failure(db));
273
+ }
274
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
275
+ auto file = reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2));
276
+ if (file != nullptr && *file != '\0') {
277
+ files.emplace_back(file);
278
+ }
279
+ }
280
+ sqlite3_finalize(stmt);
281
+ return files;
282
+ }
283
+
284
+ // Refuses an ATTACH of a file outside the files the app may write. A temporary or in-memory
285
+ // database is no file. A name the statement computes (a bound parameter, an expression) comes
286
+ // here unread, as null, so it is refused: only a literal name can be checked.
287
+ static int authorize(void *self, int action, const char *file, const char *, const char *, const char *) {
288
+ if (action != SQLITE_ATTACH) {
289
+ return SQLITE_OK;
290
+ }
291
+ if (file == nullptr) {
292
+ return SQLITE_DENY;
293
+ }
294
+ if (*file == '\0' || std::string_view(file) == ":memory:") {
295
+ return SQLITE_OK;
296
+ }
297
+ auto *databases = static_cast<Databases *>(self);
298
+ try {
299
+ auto use = databases->writable(fs::path(file));
300
+ // Not mutex_: a delete holds it while it asks each connection for its files.
301
+ std::lock_guard lock(databases->attachedMutex_);
302
+ databases->attached_.push_back(std::move(use));
303
+ return SQLITE_OK;
304
+ } catch (const std::exception &) {
305
+ return SQLITE_DENY;
306
+ }
307
+ }
308
+
309
+ // The database file, and its -wal and -shm beside it, only where the app may write: its own
310
+ // folders, picks and readwrite declarations (FileAccess::writeFor).
311
+ std::shared_ptr<FileAccess::Use> writable(const fs::path &file) const {
312
+ if (!files_) {
313
+ throw FileRefused(file.string(), true);
314
+ }
315
+ return files_->writeFor(file.string());
316
+ }
317
+
318
+ std::string directoryArg(const folly::dynamic &args, size_t index) const {
319
+ auto directory = stringArg(args, index);
320
+ return directory.empty() ? defaultDirectory_ : pathOf(directory);
321
+ }
322
+
323
+ static int idOf(const folly::dynamic &args) {
324
+ return !args.empty() && args[0].isNumber() ? static_cast<int>(args[0].asDouble()) : 0;
325
+ }
326
+
327
+ std::string defaultDirectory_;
328
+ std::shared_ptr<FileAccess> files_;
329
+ std::mutex mutex_;
330
+ std::map<int, Open> open_;
331
+ // The picks attached files are in, held while the module is.
332
+ std::mutex attachedMutex_;
333
+ std::vector<std::shared_ptr<FileAccess::Use>> attached_;
334
+ int next_{0};
335
+ };
336
+
337
+ folly::dynamic paramsOf(const folly::dynamic &args) {
338
+ return args.size() > 2 ? args[2] : folly::dynamic(nullptr);
339
+ }
340
+
341
+ void exec(sqlite3 *db, const std::string &source) {
342
+ char *error = nullptr;
343
+ if (sqlite3_exec(db, source.c_str(), nullptr, nullptr, &error) != SQLITE_OK) {
344
+ std::string message = "Error code " + std::to_string(sqlite3_extended_errcode(db)) + ": " + (error ? error : "");
345
+ sqlite3_free(error);
346
+ throw SqlError(message);
347
+ }
348
+ }
349
+
350
+ folly::dynamic run(sqlite3 *db, const folly::dynamic &args) {
351
+ Statement statement(db, stringArg(args, 1));
352
+ statement.bind(paramsOf(args));
353
+ while (statement.step()) {
354
+ }
355
+ return folly::dynamic::object("lastInsertRowId", static_cast<int64_t>(sqlite3_last_insert_rowid(db)))(
356
+ "changes", sqlite3_changes(db));
357
+ }
358
+
359
+ folly::dynamic rows(sqlite3 *db, const folly::dynamic &args, bool firstOnly) {
360
+ Statement statement(db, stringArg(args, 1));
361
+ statement.bind(paramsOf(args));
362
+ folly::dynamic all = folly::dynamic::array();
363
+ while (statement.step()) {
364
+ if (firstOnly) {
365
+ return statement.row();
366
+ }
367
+ all.push_back(statement.row());
368
+ }
369
+ return firstOnly ? folly::dynamic(nullptr) : all;
370
+ }
371
+
372
+ } // namespace
373
+
374
+ std::shared_ptr<NativeModule> makeSqliteModule(std::string defaultDatabaseDirectory, std::shared_ptr<FileAccess> files) {
375
+ struct Sqlite : NativeModule {
376
+ Sqlite(std::string directory, std::shared_ptr<FileAccess> files)
377
+ : NativeModule("NgNativeSqlite"), databases(std::make_shared<Databases>(std::move(directory), std::move(files))) {
378
+ auto dbs = databases;
379
+ property("defaultDatabaseDirectory", [dbs] { return folly::dynamic(dbs->defaultDirectory()); });
380
+ constants([dbs] { return folly::dynamic::object("defaultDatabaseDirectory", dbs->defaultDirectory()); });
381
+
382
+ // Each call as a promise and as a synchronous method, over one function.
383
+ auto both = [this](const std::string &name, std::function<folly::dynamic(const folly::dynamic &)> fn) {
384
+ promise(name + "Async", [fn](const folly::dynamic &args, JsPromise promise) {
385
+ folly::dynamic value;
386
+ try {
387
+ value = fn(args);
388
+ } catch (const std::exception &error) {
389
+ promise.reject(error.what());
390
+ return;
391
+ }
392
+ promise.resolve(std::move(value));
393
+ });
394
+ sync(name + "Sync", fn);
395
+ };
396
+ both("openDatabase", [dbs](const folly::dynamic &args) { return dbs->open(args); });
397
+ both("close", [dbs](const folly::dynamic &args) {
398
+ dbs->close(args);
399
+ return folly::dynamic(nullptr);
400
+ });
401
+ both("deleteDatabase", [dbs](const folly::dynamic &args) {
402
+ dbs->remove(args);
403
+ return folly::dynamic(nullptr);
404
+ });
405
+ both("exec", [dbs](const folly::dynamic &args) {
406
+ exec(dbs->db(args), stringArg(args, 1));
407
+ return folly::dynamic(nullptr);
408
+ });
409
+ both("run", [dbs](const folly::dynamic &args) { return run(dbs->db(args), args); });
410
+ both("getFirst", [dbs](const folly::dynamic &args) { return rows(dbs->db(args), args, true); });
411
+ both("getAll", [dbs](const folly::dynamic &args) { return rows(dbs->db(args), args, false); });
412
+ both("isInTransaction", [dbs](const folly::dynamic &args) {
413
+ return folly::dynamic(sqlite3_get_autocommit(dbs->db(args)) == 0);
414
+ });
415
+ }
416
+ std::shared_ptr<Databases> databases;
417
+ };
418
+ return std::make_shared<Sqlite>(std::move(defaultDatabaseDirectory), std::move(files));
419
+ }
420
+
421
+ } // namespace ngn
@@ -0,0 +1,23 @@
1
+ #include "ngn/store-review.h"
2
+
3
+ namespace ngn {
4
+
5
+ std::shared_ptr<NativeModule> makeStoreReviewModule(std::shared_ptr<ReviewPrompt> prompt, bool noPrompts) {
6
+ struct StoreReview : NativeModule {
7
+ StoreReview(std::shared_ptr<ReviewPrompt> prompt, bool noPrompts) : NativeModule("NgNativeStoreReview") {
8
+ bool available = prompt != nullptr;
9
+ promise("isAvailableAsync", [available](const folly::dynamic &, JsPromise promise) { promise.resolve(available); });
10
+ promise("hasAction", [available](const folly::dynamic &, JsPromise promise) { promise.resolve(available); });
11
+ promise("requestReview", [prompt, noPrompts](const folly::dynamic &, JsPromise promise) {
12
+ if (prompt && !noPrompts) {
13
+ prompt->request();
14
+ }
15
+ promise.resolve(nullptr);
16
+ });
17
+ sync("storeUrl", [](const folly::dynamic &) { return folly::dynamic(nullptr); });
18
+ }
19
+ };
20
+ return std::make_shared<StoreReview>(std::move(prompt), noPrompts);
21
+ }
22
+
23
+ } // namespace ngn
@@ -0,0 +1,107 @@
1
+ #include "ngn/text-recognition.h"
2
+
3
+ #include "ngn/file-system.h"
4
+
5
+ #include <algorithm>
6
+ #include <cctype>
7
+
8
+ #include <folly/json.h>
9
+
10
+ namespace ngn {
11
+
12
+ namespace {
13
+
14
+ std::string primary(const std::string &code) {
15
+ std::string language = code.substr(0, code.find('-'));
16
+ std::transform(language.begin(), language.end(), language.begin(), [](unsigned char c) { return std::tolower(c); });
17
+ return language;
18
+ }
19
+
20
+ std::string joined(const std::vector<std::string> &values) {
21
+ std::string out;
22
+ for (const auto &value : values) {
23
+ out += (out.empty() ? "" : ", ") + value;
24
+ }
25
+ return out;
26
+ }
27
+
28
+ folly::dynamic readingOf(const TextReader::Reading &reading) {
29
+ folly::dynamic lines = folly::dynamic::array();
30
+ std::string text;
31
+ for (const auto &line : reading.lines) {
32
+ text += (lines.empty() ? "" : "\n") + line.text;
33
+ lines.push_back(folly::dynamic::object("text", line.text)("confidence", line.confidence)(
34
+ "box",
35
+ folly::dynamic::object("x", line.x * reading.width)("y", (1 - line.y - line.height) * reading.height)(
36
+ "width", line.width * reading.width)("height", line.height * reading.height)));
37
+ }
38
+ return folly::dynamic::object("text", text)("lines", std::move(lines))("width", reading.width)("height", reading.height);
39
+ }
40
+
41
+ } // namespace
42
+
43
+ std::optional<std::string> visionLanguage(const std::string &code, const std::vector<std::string> &supported) {
44
+ if (std::find(supported.begin(), supported.end(), code) != supported.end()) {
45
+ return code;
46
+ }
47
+ auto wanted = primary(code);
48
+ for (const auto &candidate : supported) {
49
+ if (primary(candidate) == wanted) {
50
+ return candidate;
51
+ }
52
+ }
53
+ return std::nullopt;
54
+ }
55
+
56
+ std::shared_ptr<NativeModule> makeTextRecognitionModule(std::shared_ptr<TextReader> reader, std::shared_ptr<FileAccess> files) {
57
+ struct TextRecognition : NativeModule {
58
+ TextRecognition(std::shared_ptr<TextReader> reader, std::shared_ptr<FileAccess> files) : NativeModule("NgNativeTextRecognition") {
59
+ promise("recognize", [reader, files](const folly::dynamic &args, JsPromise promise) {
60
+ std::string uri = !args.empty() && args[0].isString() ? args[0].asString() : "";
61
+ auto path = pathOfUri(uri);
62
+ if (!path) {
63
+ promise.reject("No image could be read at " + uri);
64
+ return;
65
+ }
66
+ std::shared_ptr<FileAccess::Use> use;
67
+ try {
68
+ use = readableFile(files.get(), *path);
69
+ } catch (const FileRefused &refused) {
70
+ promise.reject(refused.what());
71
+ return;
72
+ }
73
+ const folly::dynamic options = args.size() > 1 && args[1].isObject() ? args[1] : folly::dynamic::object();
74
+ const auto *level = options.get_ptr("level");
75
+ bool fast = level != nullptr && level->isString() && level->asString() == "fast";
76
+ std::vector<std::string> languages;
77
+ const auto *given = options.get_ptr("languages");
78
+ if (given != nullptr && given->isArray() && !given->empty()) {
79
+ auto supported = reader->languages(fast);
80
+ std::vector<std::string> unsupported;
81
+ for (const auto &code : *given) {
82
+ auto language = code.isString() ? visionLanguage(code.asString(), supported) : std::nullopt;
83
+ if (language) {
84
+ languages.push_back(*language);
85
+ } else {
86
+ unsupported.push_back(code.isString() ? code.asString() : folly::toJson(code));
87
+ }
88
+ }
89
+ if (!unsupported.empty()) {
90
+ promise.reject("Vision cannot read " + joined(unsupported) + " at this level. It can read " + joined(supported));
91
+ return;
92
+ }
93
+ }
94
+ reader->read(*path, languages, fast, [promise, use](std::optional<TextReader::Reading> reading, std::string error) {
95
+ if (reading) {
96
+ promise.resolve(readingOf(*reading));
97
+ } else {
98
+ promise.reject(error);
99
+ }
100
+ });
101
+ });
102
+ }
103
+ };
104
+ return std::make_shared<TextRecognition>(std::move(reader), std::move(files));
105
+ }
106
+
107
+ } // namespace ngn