@depup/react-native-worklets 0.12.1-depup.0 → 0.13.0-depup.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 (311) hide show
  1. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +1 -2
  2. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.h +1 -1
  3. package/Common/cpp/worklets/Compat/StableApi.cpp +24 -8
  4. package/Common/cpp/worklets/Compat/StableApi.h +1 -1
  5. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +113 -50
  6. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +10 -1
  7. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +95 -37
  8. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +57 -7
  9. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxyInitializer.cpp +94 -0
  10. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxyInitializer.h +58 -0
  11. package/Common/cpp/worklets/Networking/Networking.cpp +283 -0
  12. package/Common/cpp/worklets/Networking/Networking.h +51 -0
  13. package/Common/cpp/worklets/Networking/NetworkingBackend.h +47 -0
  14. package/Common/cpp/worklets/Networking/NetworkingInstaller.cpp +127 -0
  15. package/Common/cpp/worklets/Networking/NetworkingInstaller.h +19 -0
  16. package/Common/cpp/worklets/Networking/NetworkingTypes.h +33 -0
  17. package/Common/cpp/worklets/Networking/TextDecoding.cpp +189 -0
  18. package/Common/cpp/worklets/Networking/TextDecoding.h +29 -0
  19. package/Common/cpp/worklets/RunLoop/AsyncQueue.h +28 -0
  20. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +9 -0
  21. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.h +6 -0
  22. package/Common/cpp/worklets/RunLoop/EventLoop.cpp +19 -4
  23. package/Common/cpp/worklets/RunLoop/EventLoop.h +3 -0
  24. package/Common/cpp/worklets/SharedItems/MemoryManager.cpp +1 -1
  25. package/Common/cpp/worklets/SharedItems/MemoryManager.h +2 -1
  26. package/Common/cpp/worklets/SharedItems/Serializable/CustomSerializable.cpp +37 -0
  27. package/Common/cpp/worklets/SharedItems/Serializable/CustomSerializable.h +33 -0
  28. package/Common/cpp/worklets/SharedItems/Serializable/RetainingSerializable.h +112 -0
  29. package/Common/cpp/worklets/SharedItems/Serializable/Serializable.cpp +129 -0
  30. package/Common/cpp/worklets/SharedItems/Serializable/Serializable.h +121 -0
  31. package/Common/cpp/worklets/SharedItems/Serializable/SerializableArray.cpp +38 -0
  32. package/Common/cpp/worklets/SharedItems/Serializable/SerializableArray.h +39 -0
  33. package/Common/cpp/worklets/SharedItems/Serializable/SerializableArrayBuffer.cpp +35 -0
  34. package/Common/cpp/worklets/SharedItems/Serializable/SerializableArrayBuffer.h +43 -0
  35. package/Common/cpp/worklets/SharedItems/Serializable/SerializableBigInt.cpp +23 -0
  36. package/Common/cpp/worklets/SharedItems/Serializable/SerializableBigInt.h +35 -0
  37. package/Common/cpp/worklets/SharedItems/Serializable/SerializableError.cpp +38 -0
  38. package/Common/cpp/worklets/SharedItems/Serializable/SerializableError.h +30 -0
  39. package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostFunction.cpp +24 -0
  40. package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostFunction.h +32 -0
  41. package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostObject.cpp +19 -0
  42. package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostObject.h +25 -0
  43. package/Common/cpp/worklets/SharedItems/Serializable/SerializableImport.cpp +29 -0
  44. package/Common/cpp/worklets/SharedItems/Serializable/SerializableImport.h +25 -0
  45. package/Common/cpp/worklets/SharedItems/Serializable/SerializableMap.cpp +44 -0
  46. package/Common/cpp/worklets/SharedItems/Serializable/SerializableMap.h +25 -0
  47. package/Common/cpp/worklets/SharedItems/Serializable/SerializableObject.cpp +80 -0
  48. package/Common/cpp/worklets/SharedItems/Serializable/SerializableObject.h +35 -0
  49. package/Common/cpp/worklets/SharedItems/Serializable/SerializableRegExp.cpp +22 -0
  50. package/Common/cpp/worklets/SharedItems/Serializable/SerializableRegExp.h +25 -0
  51. package/Common/cpp/worklets/SharedItems/{SerializableRemoteFunction.cpp → Serializable/SerializableRemoteFunction.cpp} +30 -2
  52. package/Common/cpp/worklets/SharedItems/{SerializableRemoteFunction.h → Serializable/SerializableRemoteFunction.h} +24 -10
  53. package/Common/cpp/worklets/SharedItems/Serializable/SerializableScalar.cpp +46 -0
  54. package/Common/cpp/worklets/SharedItems/Serializable/SerializableScalar.h +41 -0
  55. package/Common/cpp/worklets/SharedItems/Serializable/SerializableSet.cpp +36 -0
  56. package/Common/cpp/worklets/SharedItems/Serializable/SerializableSet.h +23 -0
  57. package/Common/cpp/worklets/SharedItems/Serializable/SerializableString.cpp +19 -0
  58. package/Common/cpp/worklets/SharedItems/Serializable/SerializableString.h +22 -0
  59. package/Common/cpp/worklets/SharedItems/Serializable/SerializableTurboModuleLike.cpp +26 -0
  60. package/Common/cpp/worklets/SharedItems/Serializable/SerializableTurboModuleLike.h +33 -0
  61. package/Common/cpp/worklets/SharedItems/Serializable/SerializableWorklet.cpp +31 -0
  62. package/Common/cpp/worklets/SharedItems/Serializable/SerializableWorklet.h +23 -0
  63. package/Common/cpp/worklets/SharedItems/Serializable.h +21 -409
  64. package/Common/cpp/worklets/SharedItems/Shareable.cpp +1 -1
  65. package/Common/cpp/worklets/SharedItems/Shareable.h +1 -1
  66. package/Common/cpp/worklets/SharedItems/Synchronizable.h +43 -20
  67. package/Common/cpp/worklets/SharedItems/SynchronizableAccess.cpp +24 -26
  68. package/Common/cpp/worklets/SharedItems/SynchronizableAccess.h +5 -7
  69. package/Common/cpp/worklets/SharedItems/SynchronizableDynamic.cpp +36 -0
  70. package/Common/cpp/worklets/SharedItems/SynchronizableDynamic.h +27 -0
  71. package/Common/cpp/worklets/SharedItems/SynchronizableFixed.cpp +76 -0
  72. package/Common/cpp/worklets/SharedItems/SynchronizableFixed.h +48 -0
  73. package/Common/cpp/worklets/Tools/ScriptBuffer.h +0 -5
  74. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +16 -30
  75. package/Common/cpp/worklets/WorkletRuntime/RuntimeBindings.h +27 -30
  76. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.cpp +4 -3
  77. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.h +2 -1
  78. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +79 -29
  79. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +34 -19
  80. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +13 -86
  81. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +1 -7
  82. package/Package.swift +147 -0
  83. package/README.md +7 -7
  84. package/RNWorklets.podspec +1 -2
  85. package/android/CMakeLists.txt +7 -26
  86. package/android/build.gradle.kts +5 -31
  87. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +46 -129
  88. package/android/src/main/cpp/worklets/android/WorkletsModule.h +21 -11
  89. package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +2 -4
  90. package/android/src/main/cpp/worklets/android/networking/AndroidNetworkingBackend.cpp +82 -0
  91. package/android/src/main/cpp/worklets/android/networking/AndroidNetworkingBackend.h +32 -0
  92. package/android/src/main/cpp/worklets/android/networking/JNetworkRequestListener.cpp +73 -0
  93. package/android/src/main/cpp/worklets/android/networking/JNetworkRequestListener.h +38 -0
  94. package/android/src/{no-networking → main/java}/com/swmansion/worklets/WorkletsModule.kt +40 -28
  95. package/android/src/main/java/com/swmansion/worklets/networking/NetworkRequestListener.kt +37 -0
  96. package/android/src/main/java/com/swmansion/worklets/networking/Networking.kt +244 -0
  97. package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameQueue.kt +40 -5
  98. package/apple/worklets/apple/AnimationFrameQueue.mm +9 -1
  99. package/apple/worklets/apple/AssertJavaScriptQueue.h +3 -2
  100. package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +4 -0
  101. package/apple/worklets/apple/IOSUIScheduler.mm +3 -0
  102. package/apple/worklets/apple/Networking/AppleNetworkingBackend.h +29 -0
  103. package/apple/worklets/apple/Networking/AppleNetworkingBackend.mm +93 -0
  104. package/apple/worklets/apple/Networking/WorkletsURLSessionDelegate.h +37 -0
  105. package/apple/worklets/apple/Networking/WorkletsURLSessionDelegate.mm +447 -0
  106. package/apple/worklets/apple/WorkletsModule.h +3 -2
  107. package/apple/worklets/apple/WorkletsModule.mm +82 -74
  108. package/bundleMode/index.js +10 -47
  109. package/bundleMode/polyfills/prepareBundleMode.js +16 -0
  110. package/changes.json +6 -6
  111. package/compatibility.json +4 -1
  112. package/lib/module/WorkletsModule/NativeWorklets.native.js +12 -9
  113. package/lib/module/WorkletsModule/NativeWorklets.native.js.map +1 -1
  114. package/lib/module/debug/jsVersion.js +1 -1
  115. package/lib/module/featureFlags/staticFlags.json +0 -1
  116. package/lib/module/featureFlags/types.js +0 -1
  117. package/lib/module/featureFlags/types.js.map +1 -1
  118. package/lib/module/index.js +1 -1
  119. package/lib/module/index.js.map +1 -1
  120. package/lib/module/initializers/initializers.native.js +5 -12
  121. package/lib/module/initializers/initializers.native.js.map +1 -1
  122. package/lib/module/memory/bundleUnpacker.native.js +2 -10
  123. package/lib/module/memory/bundleUnpacker.native.js.map +1 -1
  124. package/lib/module/memory/serializable.native.js +3 -31
  125. package/lib/module/memory/serializable.native.js.map +1 -1
  126. package/lib/module/memory/synchronizable.js +1 -1
  127. package/lib/module/memory/synchronizable.js.map +1 -1
  128. package/lib/module/memory/synchronizable.native.js +7 -3
  129. package/lib/module/memory/synchronizable.native.js.map +1 -1
  130. package/lib/module/memory/synchronizableUnpacker.native.js +22 -9
  131. package/lib/module/memory/synchronizableUnpacker.native.js.map +1 -1
  132. package/lib/module/memory/valueUnpacker.native.js +0 -8
  133. package/lib/module/memory/valueUnpacker.native.js.map +1 -1
  134. package/lib/module/mock.js +22 -1
  135. package/lib/module/mock.js.map +1 -1
  136. package/lib/module/networking/AbortController.js +43 -0
  137. package/lib/module/networking/AbortController.js.map +1 -0
  138. package/lib/module/networking/Blob.js +60 -0
  139. package/lib/module/networking/Blob.js.map +1 -0
  140. package/lib/module/networking/DOMException.js +35 -0
  141. package/lib/module/networking/DOMException.js.map +1 -0
  142. package/lib/module/networking/FileReader.js +83 -0
  143. package/lib/module/networking/FileReader.js.map +1 -0
  144. package/lib/module/networking/FormData.js +105 -0
  145. package/lib/module/networking/FormData.js.map +1 -0
  146. package/lib/module/networking/XMLHttpRequest.js +450 -0
  147. package/lib/module/networking/XMLHttpRequest.js.map +1 -0
  148. package/lib/module/networking/bytes.js +6 -0
  149. package/lib/module/networking/bytes.js.map +1 -0
  150. package/lib/module/networking/events.js +44 -0
  151. package/lib/module/networking/events.js.map +1 -0
  152. package/lib/module/networking/http.js +100 -0
  153. package/lib/module/networking/http.js.map +1 -0
  154. package/lib/module/networking/install.js +37 -0
  155. package/lib/module/networking/install.js.map +1 -0
  156. package/lib/module/networking/utf8.js +99 -0
  157. package/lib/module/networking/utf8.js.map +1 -0
  158. package/lib/module/privateGlobals.d.js +0 -1
  159. package/lib/module/privateGlobals.d.js.map +1 -1
  160. package/lib/module/runLoop/common/queueMicrotaskPolyfill.js +9 -0
  161. package/lib/module/runLoop/common/queueMicrotaskPolyfill.js.map +1 -0
  162. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +7 -3
  163. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js.map +1 -1
  164. package/lib/module/runLoop/workletRuntime/index.js +1 -1
  165. package/lib/module/runLoop/workletRuntime/index.js.map +1 -1
  166. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js +1 -1
  167. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js.map +1 -1
  168. package/lib/module/runLoop/workletRuntime/taskQueue.js +0 -13
  169. package/lib/module/runLoop/workletRuntime/taskQueue.js.map +1 -1
  170. package/lib/module/runtimes.native.js +8 -1
  171. package/lib/module/runtimes.native.js.map +1 -1
  172. package/lib/module/specs/NativeWorkletsModule.js.map +1 -1
  173. package/lib/module/threads.js +11 -0
  174. package/lib/module/threads.js.map +1 -1
  175. package/lib/module/threads.native.js +35 -42
  176. package/lib/module/threads.native.js.map +1 -1
  177. package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts.map +1 -1
  178. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +6 -5
  179. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -1
  180. package/lib/typescript/debug/jsVersion.d.ts +1 -1
  181. package/lib/typescript/featureFlags/types.d.ts +0 -1
  182. package/lib/typescript/featureFlags/types.d.ts.map +1 -1
  183. package/lib/typescript/index.d.ts +2 -2
  184. package/lib/typescript/index.d.ts.map +1 -1
  185. package/lib/typescript/memory/bundleUnpacker.native.d.ts.map +1 -1
  186. package/lib/typescript/memory/serializable.native.d.ts.map +1 -1
  187. package/lib/typescript/memory/synchronizable.d.ts +6 -2
  188. package/lib/typescript/memory/synchronizable.d.ts.map +1 -1
  189. package/lib/typescript/memory/synchronizable.native.d.ts +6 -2
  190. package/lib/typescript/memory/synchronizable.native.d.ts.map +1 -1
  191. package/lib/typescript/memory/synchronizableUnpacker.native.d.ts +1 -1
  192. package/lib/typescript/memory/synchronizableUnpacker.native.d.ts.map +1 -1
  193. package/lib/typescript/memory/types.d.ts +6 -0
  194. package/lib/typescript/memory/types.d.ts.map +1 -1
  195. package/lib/typescript/memory/valueUnpacker.native.d.ts.map +1 -1
  196. package/lib/typescript/networking/AbortController.d.ts +16 -0
  197. package/lib/typescript/networking/AbortController.d.ts.map +1 -0
  198. package/lib/typescript/networking/Blob.d.ts +16 -0
  199. package/lib/typescript/networking/Blob.d.ts.map +1 -0
  200. package/lib/typescript/networking/DOMException.d.ts +5 -0
  201. package/lib/typescript/networking/DOMException.d.ts.map +1 -0
  202. package/lib/typescript/networking/FileReader.d.ts +19 -0
  203. package/lib/typescript/networking/FileReader.d.ts.map +1 -0
  204. package/lib/typescript/networking/FormData.d.ts +30 -0
  205. package/lib/typescript/networking/FormData.d.ts.map +1 -0
  206. package/lib/typescript/networking/XMLHttpRequest.d.ts +77 -0
  207. package/lib/typescript/networking/XMLHttpRequest.d.ts.map +1 -0
  208. package/lib/typescript/networking/bytes.d.ts +2 -0
  209. package/lib/typescript/networking/bytes.d.ts.map +1 -0
  210. package/lib/typescript/networking/events.d.ts +18 -0
  211. package/lib/typescript/networking/events.d.ts.map +1 -0
  212. package/lib/typescript/networking/http.d.ts +14 -0
  213. package/lib/typescript/networking/http.d.ts.map +1 -0
  214. package/lib/typescript/networking/install.d.ts +2 -0
  215. package/lib/typescript/networking/install.d.ts.map +1 -0
  216. package/lib/typescript/networking/utf8.d.ts +4 -0
  217. package/lib/typescript/networking/utf8.d.ts.map +1 -0
  218. package/lib/typescript/runLoop/{workletRuntime/queueMicrotask.d.ts → common/queueMicrotaskPolyfill.d.ts} +1 -1
  219. package/lib/typescript/runLoop/common/queueMicrotaskPolyfill.d.ts.map +1 -0
  220. package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts.map +1 -1
  221. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts +0 -2
  222. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts.map +1 -1
  223. package/lib/typescript/runtimes.native.d.ts.map +1 -1
  224. package/lib/typescript/specs/NativeWorkletsModule.d.ts +1 -0
  225. package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -1
  226. package/lib/typescript/threads.d.ts +8 -0
  227. package/lib/typescript/threads.d.ts.map +1 -1
  228. package/lib/typescript/threads.native.d.ts +8 -1
  229. package/lib/typescript/threads.native.d.ts.map +1 -1
  230. package/lib/typescript/types.d.ts +9 -5
  231. package/lib/typescript/types.d.ts.map +1 -1
  232. package/package.json +36 -22
  233. package/plugin/index.js +63 -93
  234. package/plugin-oxc/babel.d.ts +7 -0
  235. package/plugin-oxc/babel.js +200 -0
  236. package/plugin-oxc/index.d.ts +33 -0
  237. package/plugin-oxc/index.js +36 -0
  238. package/plugin-oxc/worklets-oxc-plugin.darwin-arm64.node +0 -0
  239. package/plugin-oxc/worklets-oxc-plugin.darwin-x64.node +0 -0
  240. package/plugin-oxc/worklets-oxc-plugin.linux-arm64.node +0 -0
  241. package/plugin-oxc/worklets-oxc-plugin.linux-x64.node +0 -0
  242. package/plugin-oxc/worklets-oxc-plugin.win32-arm64.node +0 -0
  243. package/plugin-oxc/worklets-oxc-plugin.win32-x64.node +0 -0
  244. package/react-native.config.js +5 -0
  245. package/scripts/worklets_utils.rb +0 -10
  246. package/src/WorkletsModule/NativeWorklets.native.ts +26 -9
  247. package/src/WorkletsModule/workletsModuleProxy.ts +15 -5
  248. package/src/debug/jsVersion.ts +1 -1
  249. package/src/featureFlags/staticFlags.json +0 -1
  250. package/src/featureFlags/types.ts +0 -1
  251. package/src/index.ts +3 -0
  252. package/src/initializers/initializers.native.ts +5 -13
  253. package/src/memory/bundleUnpacker.native.ts +6 -13
  254. package/src/memory/serializable.native.ts +6 -46
  255. package/src/memory/synchronizable.native.ts +38 -4
  256. package/src/memory/synchronizable.ts +22 -2
  257. package/src/memory/synchronizableUnpacker.native.ts +38 -13
  258. package/src/memory/types.ts +9 -0
  259. package/src/memory/valueUnpacker.native.ts +0 -8
  260. package/src/mock.ts +28 -1
  261. package/src/networking/AbortController.ts +57 -0
  262. package/src/networking/Blob.ts +74 -0
  263. package/src/networking/DOMException.ts +37 -0
  264. package/src/networking/FileReader.ts +99 -0
  265. package/src/networking/FormData.ts +135 -0
  266. package/src/networking/XMLHttpRequest.ts +557 -0
  267. package/src/networking/bytes.ts +8 -0
  268. package/src/networking/events.ts +61 -0
  269. package/src/networking/http.ts +173 -0
  270. package/src/networking/install.ts +38 -0
  271. package/src/networking/utf8.ts +122 -0
  272. package/src/privateGlobals.d.ts +20 -13
  273. package/src/runLoop/common/queueMicrotaskPolyfill.ts +12 -0
  274. package/src/runLoop/uiRuntime/requestAnimationFrame.ts +7 -4
  275. package/src/runLoop/workletRuntime/index.ts +1 -1
  276. package/src/runLoop/workletRuntime/requestAnimationFramePolyfill.ts +1 -1
  277. package/src/runLoop/workletRuntime/taskQueue.ts +0 -15
  278. package/src/runtimes.native.ts +10 -1
  279. package/src/specs/NativeWorkletsModule.ts +1 -0
  280. package/src/threads.native.ts +49 -54
  281. package/src/threads.ts +11 -0
  282. package/src/types.ts +9 -5
  283. package/Common/cpp/worklets/SharedItems/Serializable.cpp +0 -373
  284. package/Common/cpp/worklets/SharedItems/SerializableFactory.cpp +0 -172
  285. package/Common/cpp/worklets/SharedItems/SerializableFactory.h +0 -85
  286. package/Common/cpp/worklets/SharedItems/Synchronizable.cpp +0 -59
  287. package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.cpp +0 -51
  288. package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.h +0 -43
  289. package/android/src/networking/com/swmansion/worklets/WorkletRuntimeWrapper.kt +0 -27
  290. package/android/src/networking/com/swmansion/worklets/WorkletsHeaderUtil.kt +0 -30
  291. package/android/src/networking/com/swmansion/worklets/WorkletsModule.kt +0 -203
  292. package/android/src/networking/com/swmansion/worklets/WorkletsNetworkEventUtil.kt +0 -272
  293. package/android/src/networking/com/swmansion/worklets/WorkletsNetworking.kt +0 -1124
  294. package/android/src/networking/com/swmansion/worklets/WorkletsOkHttpCallUtil.kt +0 -40
  295. package/android/src/networking/com/swmansion/worklets/WorkletsProgressListener.kt +0 -13
  296. package/android/src/networking/com/swmansion/worklets/WorkletsProgressRequestBody.kt +0 -98
  297. package/android/src/networking/com/swmansion/worklets/WorkletsProgressResponseBody.kt +0 -60
  298. package/android/src/networking/com/swmansion/worklets/WorkletsProgressiveStringDecoder.kt +0 -87
  299. package/android/src/networking/com/swmansion/worklets/WorkletsRequestBodyUtil.kt +0 -181
  300. package/apple/worklets/apple/Networking/WorkletsNetworking.h +0 -25
  301. package/apple/worklets/apple/Networking/WorkletsNetworking.mm +0 -707
  302. package/bundleMode/shims/turboModuleRegistryShim.js +0 -47
  303. package/lib/module/bundleMode/network.native.js +0 -44
  304. package/lib/module/bundleMode/network.native.js.map +0 -1
  305. package/lib/module/runLoop/workletRuntime/queueMicrotask.js +0 -11
  306. package/lib/module/runLoop/workletRuntime/queueMicrotask.js.map +0 -1
  307. package/lib/typescript/bundleMode/network.native.d.ts +0 -7
  308. package/lib/typescript/bundleMode/network.native.d.ts.map +0 -1
  309. package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts.map +0 -1
  310. package/src/bundleMode/network.native.ts +0 -63
  311. package/src/runLoop/workletRuntime/queueMicrotask.ts +0 -13
@@ -1,1124 +0,0 @@
1
- /*
2
- * Based on Networking.kt from React Native
3
- */
4
-
5
- // Conflicting okhttp versions
6
- @file:Suppress("DEPRECATION_ERROR")
7
-
8
- package com.swmansion.worklets
9
-
10
- import android.net.Uri
11
- import android.util.Base64
12
- import com.facebook.react.bridge.ReactApplicationContext
13
- import com.facebook.react.bridge.ReadableArray
14
- import com.facebook.react.bridge.ReadableMap
15
- import com.facebook.react.bridge.ReadableType
16
- import com.facebook.react.bridge.WritableMap
17
- import com.facebook.react.modules.network.CookieJarContainer
18
- import com.facebook.react.modules.network.ForwardingCookieHandler
19
- import com.facebook.react.modules.network.NetworkInterceptorCreator
20
- import com.facebook.react.modules.network.NetworkingModule
21
- import com.facebook.react.modules.network.OkHttpClientProvider
22
- import okhttp3.Call
23
- import okhttp3.Callback
24
- import okhttp3.CookieJar
25
- import okhttp3.Headers
26
- import okhttp3.JavaNetCookieJar
27
- import okhttp3.MediaType
28
- import okhttp3.MultipartBody
29
- import okhttp3.OkHttpClient
30
- import okhttp3.Protocol
31
- import okhttp3.Request
32
- import okhttp3.RequestBody
33
- import okhttp3.Response
34
- import okhttp3.ResponseBody
35
- import okio.ByteString
36
- import okio.GzipSource
37
- import okio.Okio
38
- import java.io.IOException
39
- import java.nio.charset.StandardCharsets
40
- import java.util.ArrayList
41
- import java.util.HashSet
42
- import java.util.UUID
43
- import java.util.concurrent.TimeUnit
44
-
45
- private class RequestId(
46
- runtimeId: Int,
47
- requestId: Int,
48
- )
49
-
50
- /** Implements the XMLHttpRequest JavaScript interface. */
51
- public class WorkletsNetworking(
52
- defaultUserAgent: String?,
53
- client: OkHttpClient,
54
- networkInterceptorCreators: List<NetworkInterceptorCreator>?,
55
- ) {
56
- /**
57
- * Allows to implement a custom fetching process for specific URIs. It is the handler's job to
58
- * fetch the URI and return the JS body payload.
59
- */
60
- internal interface UriHandler {
61
- /** Returns if the handler should be used for an URI. */
62
- public fun supports(
63
- uri: Uri,
64
- responseType: String,
65
- ): Boolean
66
-
67
- /**
68
- * Fetch the URI and return a tuple containing the JS body payload and the raw response body.
69
- */
70
- @Throws(IOException::class)
71
- public fun fetch(uri: Uri): Pair<WritableMap, ByteArray>
72
- }
73
-
74
- /** Allows adding custom handling to build the [RequestBody] from the JS body payload. */
75
- internal interface RequestBodyHandler {
76
- /** Returns if the handler should be used for a JS body payload. */
77
- public fun supports(map: ReadableMap): Boolean
78
-
79
- /** Returns the [RequestBody] for the JS body payload. */
80
- public fun toRequestBody(
81
- map: ReadableMap,
82
- contentType: String?,
83
- ): RequestBody?
84
- }
85
-
86
- /** Allows adding custom handling to build the JS body payload from the [ResponseBody]. */
87
- internal interface ResponseHandler {
88
- /** Returns if the handler should be used for a response type. */
89
- public fun supports(responseType: String): Boolean
90
-
91
- /** Returns the JS body payload for the [ResponseBody]. */
92
- @Throws(IOException::class)
93
- public fun toResponseData(data: ByteArray): WritableMap
94
- }
95
-
96
- private val client: OkHttpClient
97
- private val cookieHandler = ForwardingCookieHandler()
98
- private val defaultUserAgent: String?
99
- private var cookieJarContainer: CookieJarContainer? = null
100
-
101
- // private val requestIds: MutableSet<Int> = HashSet()
102
- private val requestIds: MutableMap<Int, MutableSet<Int>> = HashMap()
103
- private val requestBodyHandlers: MutableList<RequestBodyHandler> = ArrayList()
104
- private val uriHandlers: MutableList<UriHandler> = ArrayList()
105
- private val responseHandlers: MutableList<ResponseHandler> = ArrayList()
106
- private var shuttingDown = false
107
-
108
- init {
109
- var resolvedClient: OkHttpClient = client
110
- if (networkInterceptorCreators != null) {
111
- val clientBuilder = client.newBuilder()
112
- for (networkInterceptorCreator in networkInterceptorCreators) {
113
- clientBuilder.addNetworkInterceptor(networkInterceptorCreator.create())
114
- }
115
- resolvedClient = clientBuilder.build()
116
- }
117
- this.client = resolvedClient
118
-
119
- val cookieJar = resolvedClient.cookieJar()
120
- cookieJarContainer =
121
- if (cookieJar is CookieJarContainer) {
122
- cookieJar
123
- } else {
124
- null
125
- }
126
- this.defaultUserAgent = defaultUserAgent
127
- }
128
-
129
- /**
130
- * @param context the ReactContext of the application
131
- * @param defaultUserAgent the User-Agent header that will be set for all requests where the
132
- * caller does not provide one explicitly
133
- * @param client the [OkHttpClient] to be used for networking
134
- */
135
- internal constructor(
136
- defaultUserAgent: String?,
137
- client: OkHttpClient,
138
- ) : this(defaultUserAgent, client, null)
139
-
140
- /** @param context the ReactContext of the application */
141
- public constructor() : this(
142
- null,
143
- OkHttpClientProvider.createClient(),
144
- null,
145
- )
146
-
147
- /**
148
- * @param context the ReactContext of the application
149
- * @param networkInterceptorCreators list of [NetworkInterceptorCreator]'s whose create() methods
150
- * would be called to attach the interceptors to the client.
151
- */
152
- public constructor(
153
- networkInterceptorCreators: List<NetworkInterceptorCreator>?,
154
- ) : this(
155
- null,
156
- OkHttpClientProvider.createClient(),
157
- networkInterceptorCreators,
158
- )
159
-
160
- /**
161
- * @param context the ReactContext of the application
162
- * @param defaultUserAgent the User-Agent header that will be set for all requests where the
163
- * caller does not provide one explicitly
164
- */
165
- public constructor(
166
- defaultUserAgent: String?,
167
- ) : this(
168
- defaultUserAgent,
169
- OkHttpClientProvider.createClient(),
170
- null,
171
- )
172
-
173
- @Deprecated(
174
- """To be removed in a future release. See
175
- https://github.com/facebook/react-native/pull/37798#pullrequestreview-1518338914""",
176
- )
177
- public interface CustomClientBuilder : com.facebook.react.modules.network.CustomClientBuilder
178
-
179
- fun initialize() {
180
- cookieJarContainer?.setCookieJar(JavaNetCookieJar(cookieHandler))
181
- }
182
-
183
- fun invalidate() {
184
- shuttingDown = true
185
- cancelAllRequests()
186
-
187
- cookieHandler.destroy()
188
- cookieJarContainer?.removeCookieJar()
189
-
190
- requestBodyHandlers.clear()
191
- responseHandlers.clear()
192
- uriHandlers.clear()
193
- }
194
-
195
- internal fun addUriHandler(handler: UriHandler) {
196
- uriHandlers.add(handler)
197
- }
198
-
199
- internal fun addRequestBodyHandler(handler: RequestBodyHandler) {
200
- requestBodyHandlers.add(handler)
201
- }
202
-
203
- internal fun addResponseHandler(handler: ResponseHandler) {
204
- responseHandlers.add(handler)
205
- }
206
-
207
- internal fun removeUriHandler(handler: UriHandler) {
208
- uriHandlers.remove(handler)
209
- }
210
-
211
- internal fun removeRequestBodyHandler(handler: RequestBodyHandler) {
212
- requestBodyHandlers.remove(handler)
213
- }
214
-
215
- internal fun removeResponseHandler(handler: ResponseHandler) {
216
- responseHandlers.remove(handler)
217
- }
218
-
219
- private fun extractOrGenerateDevToolsRequestId(data: ReadableMap?): String =
220
- (
221
- if (
222
- data != null &&
223
- data.hasKey(REQUEST_DATA_KEY_DEVTOOLS_REQUEST_ID) &&
224
- data.getType(REQUEST_DATA_KEY_DEVTOOLS_REQUEST_ID) == ReadableType.String
225
- ) {
226
- data.getString(REQUEST_DATA_KEY_DEVTOOLS_REQUEST_ID)
227
- } else {
228
- null
229
- }
230
- ) ?: UUID.randomUUID().toString()
231
-
232
- fun jsiSendRequest(
233
- runtimeWrapper: WorkletRuntimeWrapper,
234
- method: String,
235
- url: String,
236
- requestIdAsDouble: Double,
237
- headers: ReadableArray?,
238
- data: ReadableMap?,
239
- responseType: String,
240
- useIncrementalUpdates: Boolean,
241
- timeoutAsDouble: Double,
242
- withCredentials: Boolean,
243
- ) {
244
- val requestId = requestIdAsDouble.toInt()
245
- val timeout = timeoutAsDouble.toInt()
246
- val devToolsRequestId = extractOrGenerateDevToolsRequestId(data)
247
- try {
248
- sendRequestInternalReal(
249
- runtimeWrapper,
250
- method,
251
- url,
252
- requestId,
253
- headers,
254
- data,
255
- responseType,
256
- useIncrementalUpdates,
257
- timeout,
258
- withCredentials,
259
- devToolsRequestId,
260
- )
261
- } catch (th: Throwable) {
262
- WorkletsNetworkEventUtil.onRequestError(
263
- runtimeWrapper,
264
- requestId,
265
- devToolsRequestId,
266
- th.message,
267
- th,
268
- )
269
- }
270
- }
271
-
272
- // @Deprecated("""sendRequestInternal is internal and will be made private in a future release.""")
273
- // /** @param timeout value of 0 results in no timeout */
274
- // public fun sendRequestInternal(
275
- // runtimeWrapper: WorkletRuntimeWrapper,
276
- // method: String,
277
- // url: String?,
278
- // requestId: Int,
279
- // headers: ReadableArray?,
280
- // data: ReadableMap?,
281
- // responseType: String,
282
- // useIncrementalUpdates: Boolean,
283
- // timeout: Int,
284
- // withCredentials: Boolean,
285
- // ) {
286
- // sendRequestInternalReal(
287
- // runtimeWrapper,
288
- // method,
289
- // url,
290
- // requestId,
291
- // headers,
292
- // data,
293
- // responseType,
294
- // useIncrementalUpdates,
295
- // timeout,
296
- // withCredentials,
297
- // extractOrGenerateDevToolsRequestId(data),
298
- // )
299
- // }
300
-
301
- /** @param timeout value of 0 results in no timeout */
302
- private fun sendRequestInternalReal(
303
- runtimeWrapper: WorkletRuntimeWrapper,
304
- method: String,
305
- url: String?,
306
- requestId: Int,
307
- headers: ReadableArray?,
308
- data: ReadableMap?,
309
- responseType: String,
310
- useIncrementalUpdates: Boolean,
311
- timeout: Int,
312
- withCredentials: Boolean,
313
- devToolsRequestId: String,
314
- ) {
315
- try {
316
- val uri = Uri.parse(url)
317
-
318
- // Check if a handler is registered
319
- for (handler in uriHandlers) {
320
- if (handler.supports(uri, responseType)) {
321
- val (res, rawBody) = handler.fetch(uri)
322
- val encodedDataLength = res.toString().toByteArray().size
323
- // fix: UriHandlers which are not using file:// scheme fail in whatwg-fetch at this line
324
- // https://github.com/JakeChampion/fetch/blob/main/fetch.js#L547
325
- val response =
326
- Response
327
- .Builder()
328
- .protocol(Protocol.HTTP_1_1)
329
- .request(Request.Builder().url(url.orEmpty()).build())
330
- .code(200)
331
- .message("OK")
332
- .build()
333
- WorkletsNetworkEventUtil.onResponseReceived(
334
- runtimeWrapper,
335
- requestId,
336
- devToolsRequestId,
337
- url,
338
- response,
339
- )
340
- WorkletsNetworkEventUtil.onDataReceived(
341
- runtimeWrapper,
342
- requestId,
343
- devToolsRequestId,
344
- res,
345
- rawBody,
346
- )
347
- WorkletsNetworkEventUtil.onRequestSuccess(
348
- runtimeWrapper,
349
- requestId,
350
- devToolsRequestId,
351
- encodedDataLength.toLong(),
352
- )
353
- return
354
- }
355
- }
356
- } catch (e: IOException) {
357
- WorkletsNetworkEventUtil.onRequestError(
358
- runtimeWrapper,
359
- requestId,
360
- devToolsRequestId,
361
- e.message,
362
- e,
363
- )
364
- return
365
- }
366
-
367
- val requestBuilder: Request.Builder
368
- try {
369
- requestBuilder = Request.Builder().url(url.orEmpty())
370
- } catch (e: Exception) {
371
- WorkletsNetworkEventUtil.onRequestError(
372
- runtimeWrapper,
373
- requestId,
374
- devToolsRequestId,
375
- e.message,
376
- e,
377
- )
378
- return
379
- }
380
-
381
- if (requestId != 0) {
382
- requestBuilder.tag(RequestId(runtimeWrapper.getRuntimeId(), requestId).hashCode())
383
- }
384
-
385
- val clientBuilder = client.newBuilder()
386
-
387
- applyCustomBuilder(clientBuilder)
388
-
389
- if (!withCredentials) {
390
- clientBuilder.cookieJar(CookieJar.NO_COOKIES)
391
- }
392
-
393
- // If JS is listening for progress updates, install a ProgressResponseBody that intercepts the
394
- // response and counts bytes received.
395
- if (useIncrementalUpdates) {
396
- clientBuilder.addNetworkInterceptor { chain ->
397
- val originalResponse = chain.proceed(chain.request())
398
- val originalResponseBody = checkNotNull(originalResponse.body())
399
- val responseBody =
400
- WorkletsProgressResponseBody(
401
- originalResponseBody,
402
- object : WorkletsProgressListener {
403
- var last: Long = System.nanoTime()
404
-
405
- override fun onProgress(
406
- bytesWritten: Long,
407
- contentLength: Long,
408
- done: Boolean,
409
- ) {
410
- val now = System.nanoTime()
411
- if (!done && !shouldDispatch(now, last)) {
412
- return
413
- }
414
- if (responseType == "text") {
415
- // For 'text' responses we continuously send response data with progress
416
- // info to
417
- // JS below, so no need to do anything here.
418
- return
419
- }
420
- WorkletsNetworkEventUtil.onDataReceivedProgress(
421
- runtimeWrapper,
422
- requestId,
423
- bytesWritten,
424
- contentLength,
425
- )
426
- last = now
427
- }
428
- },
429
- )
430
- originalResponse.newBuilder().body(responseBody).build()
431
- }
432
- }
433
-
434
- // If the current timeout does not equal the passed in timeout, we need to clone the existing
435
- // client and set the timeout explicitly on the clone. This is cheap as everything else is
436
- // shared under the hood.
437
- // See https://github.com/square/okhttp/wiki/Recipes#per-call-configuration for more information
438
- if (timeout != client.callTimeoutMillis()) {
439
- clientBuilder.callTimeout(timeout.toLong(), TimeUnit.MILLISECONDS)
440
- }
441
- val client = clientBuilder.build()
442
-
443
- val requestHeaders = extractHeaders(headers, data)
444
- if (requestHeaders == null) {
445
- WorkletsNetworkEventUtil.onRequestError(
446
- runtimeWrapper,
447
- requestId,
448
- devToolsRequestId,
449
- "Unrecognized headers format",
450
- null,
451
- )
452
- return
453
- }
454
- var contentType = requestHeaders[CONTENT_TYPE_HEADER_NAME]
455
- val contentEncoding = requestHeaders[CONTENT_ENCODING_HEADER_NAME]
456
- requestBuilder.headers(requestHeaders)
457
-
458
- // Check if a handler is registered
459
- var handler: RequestBodyHandler? = null
460
- if (data != null) {
461
- for (curHandler in requestBodyHandlers) {
462
- if (curHandler.supports(data)) {
463
- handler = curHandler
464
- break
465
- }
466
- }
467
- }
468
-
469
- var requestBody: RequestBody?
470
- when {
471
- data == null || method.lowercase() == "get" || method.lowercase() == "head" ->
472
- requestBody = WorkletsRequestBodyUtil.getEmptyBody(method)
473
- handler != null -> requestBody = handler.toRequestBody(data, contentType)
474
- data.hasKey(REQUEST_BODY_KEY_STRING) -> {
475
- if (contentType == null) {
476
- WorkletsNetworkEventUtil.onRequestError(
477
- runtimeWrapper,
478
- requestId,
479
- devToolsRequestId,
480
- "Payload is set but no content-type header specified",
481
- null,
482
- )
483
- return
484
- }
485
- val body = data.getString(REQUEST_BODY_KEY_STRING)
486
- val contentMediaType = MediaType.parse(contentType)
487
- if (WorkletsRequestBodyUtil.isGzipEncoding(contentEncoding)) {
488
- requestBody = null
489
- if (contentMediaType != null && body != null) {
490
- requestBody = WorkletsRequestBodyUtil.createGzip(contentMediaType, body)
491
- }
492
- if (requestBody == null) {
493
- WorkletsNetworkEventUtil.onRequestError(
494
- runtimeWrapper,
495
- requestId,
496
- devToolsRequestId,
497
- "Failed to gzip request body",
498
- null,
499
- )
500
- return
501
- }
502
- } else {
503
- // Use getBytes() to convert the body into a byte[], preventing okhttp from
504
- // appending the character set to the Content-Type header when otherwise unspecified
505
- // https://github.com/facebook/react-native/issues/8237
506
- val charset =
507
- if (contentMediaType == null) {
508
- StandardCharsets.UTF_8
509
- } else {
510
- checkNotNull(contentMediaType.charset(StandardCharsets.UTF_8))
511
- }
512
- if (body == null) {
513
- WorkletsNetworkEventUtil.onRequestError(
514
- runtimeWrapper,
515
- requestId,
516
- devToolsRequestId,
517
- "Received request but body was empty",
518
- null,
519
- )
520
- return
521
- }
522
- @Suppress("DEPRECATION")
523
- requestBody = RequestBody.create(contentMediaType, body.toByteArray(charset))
524
- }
525
- }
526
- data.hasKey(REQUEST_BODY_KEY_BASE64) -> {
527
- if (contentType == null) {
528
- WorkletsNetworkEventUtil.onRequestError(
529
- runtimeWrapper,
530
- requestId,
531
- devToolsRequestId,
532
- "Payload is set but no content-type header specified",
533
- null,
534
- )
535
- return
536
- }
537
- val base64String = data.getString(REQUEST_BODY_KEY_BASE64)
538
- checkNotNull(base64String)
539
-
540
- val contentMediaType = MediaType.parse(contentType)
541
- if (contentMediaType == null) {
542
- WorkletsNetworkEventUtil.onRequestError(
543
- runtimeWrapper,
544
- requestId,
545
- devToolsRequestId,
546
- "Invalid content type specified: $contentType",
547
- null,
548
- )
549
- return
550
- }
551
- val base64DecodedString = ByteString.decodeBase64(base64String)
552
- if (base64DecodedString == null) {
553
- WorkletsNetworkEventUtil.onRequestError(
554
- runtimeWrapper,
555
- requestId,
556
- devToolsRequestId,
557
- "Request body base64 string was invalid",
558
- null,
559
- )
560
- return
561
- }
562
- @Suppress("DEPRECATION")
563
- requestBody = RequestBody.create(contentMediaType, base64DecodedString)
564
- }
565
- data.hasKey(REQUEST_BODY_KEY_URI) -> {
566
- if (contentType == null) {
567
- WorkletsNetworkEventUtil.onRequestError(
568
- runtimeWrapper,
569
- requestId,
570
- devToolsRequestId,
571
- "Payload is set but no content-type header specified",
572
- null,
573
- )
574
- return
575
- }
576
- val uri = data.getString(REQUEST_BODY_KEY_URI)
577
- if (uri == null) {
578
- WorkletsNetworkEventUtil.onRequestError(
579
- runtimeWrapper,
580
- requestId,
581
- devToolsRequestId,
582
- "Request body URI field was set but null",
583
- null,
584
- )
585
- return
586
- }
587
-
588
- // TODO: Fix me
589
- val fileInputStream = null
590
- // WorkletsRequestBodyUtil.getFileInputStream(getReactApplicationContext(), uri)
591
- if (fileInputStream == null) {
592
- WorkletsNetworkEventUtil.onRequestError(
593
- runtimeWrapper,
594
- requestId,
595
- devToolsRequestId,
596
- "Could not retrieve file for uri $uri",
597
- null,
598
- )
599
- return
600
- }
601
- requestBody = WorkletsRequestBodyUtil.create(MediaType.parse(contentType), fileInputStream)
602
- }
603
- data.hasKey(REQUEST_BODY_KEY_FORMDATA) -> {
604
- if (contentType == null) {
605
- contentType = "multipart/form-data"
606
- }
607
- val parts = data.getArray(REQUEST_BODY_KEY_FORMDATA)
608
- if (parts == null) {
609
- WorkletsNetworkEventUtil.onRequestError(
610
- runtimeWrapper,
611
- requestId,
612
- devToolsRequestId,
613
- "Received request but form data was empty",
614
- null,
615
- )
616
- return
617
- }
618
- val multipartBuilder =
619
- constructMultipartBody(runtimeWrapper, parts, contentType, requestId, devToolsRequestId) ?: return
620
- requestBody = multipartBuilder.build()
621
- }
622
- else -> {
623
- // Nothing in data payload, at least nothing we could understand anyway.
624
- requestBody = WorkletsRequestBodyUtil.getEmptyBody(method)
625
- }
626
- }
627
-
628
- requestBuilder.method(method, wrapRequestBodyWithProgressEmitter(runtimeWrapper, requestBody, requestId))
629
-
630
- addRequest(runtimeWrapper.getRuntimeId(), requestId)
631
- val request = requestBuilder.build()
632
- WorkletsNetworkEventUtil.onCreateRequest(devToolsRequestId, request)
633
-
634
- client
635
- .newCall(request)
636
- .enqueue(
637
- object : Callback {
638
- override fun onFailure(
639
- call: Call,
640
- e: IOException,
641
- ) {
642
- if (shuttingDown) {
643
- return
644
- }
645
- removeRequest(runtimeWrapper.getRuntimeId(), requestId)
646
- val errorMessage =
647
- e.message ?: ("Error while executing request: ${e.javaClass.simpleName}")
648
- WorkletsNetworkEventUtil.onRequestError(
649
- runtimeWrapper,
650
- requestId,
651
- devToolsRequestId,
652
- errorMessage,
653
- e,
654
- )
655
- }
656
-
657
- @Throws(IOException::class)
658
- override fun onResponse(
659
- call: Call,
660
- response: Response,
661
- ) {
662
- if (shuttingDown) {
663
- return
664
- }
665
- removeRequest(runtimeWrapper.getRuntimeId(), requestId)
666
- // Before we touch the body send headers to JS
667
- WorkletsNetworkEventUtil.onResponseReceived(
668
- runtimeWrapper,
669
- requestId,
670
- devToolsRequestId,
671
- url,
672
- response,
673
- )
674
-
675
- try {
676
- // OkHttp implements something called transparent gzip, which mean that it will
677
- // automatically add the Accept-Encoding gzip header and handle decoding
678
- // internally.
679
- // The issue is that it won't handle decoding if the user provides a
680
- // Accept-Encoding
681
- // header. This is also undesirable considering that iOS does handle the decoding
682
- // even
683
- // when the header is provided. To make sure this works in all cases, handle gzip
684
- // body
685
- // here also. This works fine since OKHttp will remove the Content-Encoding header
686
- // if
687
- // it used transparent gzip.
688
- // See
689
- // https://github.com/square/okhttp/blob/5b37cda9e00626f43acf354df145fd452c3031f1/okhttp/src/main/java/okhttp3/internal/http/BridgeInterceptor.java#L76-L111
690
- var responseBody: ResponseBody? = response.body()
691
- if (responseBody == null) {
692
- WorkletsNetworkEventUtil.onRequestError(
693
- // reactApplicationContext,
694
- runtimeWrapper,
695
- requestId,
696
- devToolsRequestId,
697
- "Response body is null",
698
- null,
699
- )
700
- return
701
- }
702
- if ("gzip".equals(response.header("Content-Encoding"), ignoreCase = true)) {
703
- val gzipSource = GzipSource(responseBody.source())
704
- val parsedContentType = response.header("Content-Type")?.let(MediaType::parse)
705
- responseBody =
706
- @Suppress("DEPRECATION")
707
- ResponseBody.create(
708
- parsedContentType,
709
- -1L,
710
- Okio.buffer(gzipSource),
711
- )
712
- }
713
- // To satisfy the compiler, this is already checked above
714
- checkNotNull(responseBody)
715
- // Check if a handler is registered
716
- for (responseHandler in responseHandlers) {
717
- if (responseHandler.supports(responseType)) {
718
- val responseData = responseBody.bytes()
719
- val res = responseHandler.toResponseData(responseData)
720
- WorkletsNetworkEventUtil.onDataReceived(
721
- runtimeWrapper,
722
- requestId,
723
- devToolsRequestId,
724
- res,
725
- responseData,
726
- )
727
- WorkletsNetworkEventUtil.onRequestSuccess(
728
- runtimeWrapper,
729
- requestId,
730
- devToolsRequestId,
731
- responseBody.contentLength(),
732
- )
733
- return
734
- }
735
- }
736
-
737
- // If JS wants progress updates during the download, and it requested a text
738
- // response,
739
- // periodically send response data updates to JS.
740
- if (useIncrementalUpdates && responseType == "text") {
741
- readWithProgress(runtimeWrapper, requestId, devToolsRequestId, responseBody)
742
- WorkletsNetworkEventUtil.onRequestSuccess(
743
- runtimeWrapper,
744
- requestId,
745
- devToolsRequestId,
746
- responseBody.contentLength(),
747
- )
748
- return
749
- }
750
-
751
- // Otherwise send the data in one big chunk, in the format that JS requested.
752
- var responseString: String? = ""
753
- if (responseType == "text") {
754
- try {
755
- responseString = responseBody.string()
756
- } catch (e: IOException) {
757
- if (response.request().method().equals("HEAD", ignoreCase = true)) {
758
- // The request is an `HEAD` and the body is empty,
759
- // the OkHttp will produce an exception.
760
- // Ignore the exception to not invalidate the request in the
761
- // Javascript layer.
762
- // Introduced to fix issue #7463.
763
- } else {
764
- WorkletsNetworkEventUtil.onRequestError(
765
- runtimeWrapper,
766
- requestId,
767
- devToolsRequestId,
768
- e.message,
769
- e,
770
- )
771
- }
772
- }
773
- } else if (responseType == "base64") {
774
- responseString = Base64.encodeToString(responseBody.bytes(), Base64.NO_WRAP)
775
- }
776
- WorkletsNetworkEventUtil.onDataReceived(
777
- runtimeWrapper,
778
- requestId,
779
- devToolsRequestId,
780
- responseString,
781
- responseType,
782
- )
783
- WorkletsNetworkEventUtil.onRequestSuccess(
784
- runtimeWrapper,
785
- requestId,
786
- devToolsRequestId,
787
- responseBody.contentLength(),
788
- )
789
- } catch (e: IOException) {
790
- WorkletsNetworkEventUtil.onRequestError(
791
- runtimeWrapper,
792
- requestId,
793
- devToolsRequestId,
794
- e.message,
795
- e,
796
- )
797
- }
798
- }
799
- },
800
- )
801
- }
802
-
803
- private fun wrapRequestBodyWithProgressEmitter(
804
- runtimeWrapper: WorkletRuntimeWrapper,
805
- requestBody: RequestBody?,
806
- requestId: Int,
807
- ): RequestBody? {
808
- if (requestBody == null) {
809
- return null
810
- }
811
- // val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
812
- return WorkletsRequestBodyUtil.createProgressRequest(
813
- requestBody,
814
- object : WorkletsProgressListener {
815
- var last: Long = System.nanoTime()
816
-
817
- override fun onProgress(
818
- bytesWritten: Long,
819
- contentLength: Long,
820
- done: Boolean,
821
- ) {
822
- val now = System.nanoTime()
823
- if (done || shouldDispatch(now, last)) {
824
- WorkletsNetworkEventUtil.onDataSend(
825
- runtimeWrapper,
826
- requestId,
827
- bytesWritten,
828
- contentLength,
829
- )
830
- last = now
831
- }
832
- }
833
- },
834
- )
835
- }
836
-
837
- @Throws(IOException::class)
838
- private fun readWithProgress(
839
- runtimeWrapper: WorkletRuntimeWrapper,
840
- requestId: Int,
841
- devToolsRequestId: String,
842
- responseBody: ResponseBody,
843
- ) {
844
- var totalBytesRead: Long = -1
845
- var contentLength: Long = -1
846
- try {
847
- val progressResponseBody = responseBody as WorkletsProgressResponseBody
848
- totalBytesRead = progressResponseBody.totalBytesRead()
849
- contentLength = progressResponseBody.contentLength()
850
- } catch (e: ClassCastException) {
851
- // Ignore
852
- }
853
-
854
- val charset =
855
- if (responseBody.contentType() == null) {
856
- StandardCharsets.UTF_8
857
- } else {
858
- checkNotNull(responseBody.contentType()?.charset(StandardCharsets.UTF_8)) {
859
- "Null character set for Content-Type: ${responseBody.contentType()}"
860
- }
861
- }
862
- val streamDecoder = WorkletsProgressiveStringDecoder(charset)
863
- val inputStream = responseBody.byteStream()
864
- try {
865
- val buffer = ByteArray(MAX_CHUNK_SIZE_BETWEEN_FLUSHES)
866
- var read: Int
867
- // val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
868
- while ((inputStream.read(buffer).also { read = it }) != -1) {
869
- WorkletsNetworkEventUtil.onIncrementalDataReceived(
870
- runtimeWrapper,
871
- requestId,
872
- devToolsRequestId,
873
- streamDecoder.decodeNext(buffer, read),
874
- totalBytesRead,
875
- contentLength,
876
- )
877
- }
878
- } finally {
879
- inputStream.close()
880
- }
881
- }
882
-
883
- @Synchronized
884
- private fun addRequest(
885
- runtimeId: Int,
886
- requestId: Int,
887
- ) {
888
- val requests = requestIds[runtimeId]
889
- if (requests == null) {
890
- requestIds[runtimeId] = hashSetOf(requestId)
891
- } else {
892
- requests.add(requestId)
893
- }
894
- }
895
-
896
- @Synchronized
897
- private fun removeRequest(
898
- runtimeId: Int,
899
- requestId: Int,
900
- ) {
901
- val requests = requestIds[runtimeId]
902
- requests?.remove(requestId)
903
- }
904
-
905
- @Synchronized
906
- private fun cancelAllRequests() {
907
- for (runtimeId in requestIds) {
908
- for (requestId in runtimeId.value) {
909
- cancelRequest(runtimeId.key, requestId)
910
- removeRequest(runtimeId.key, requestId)
911
- }
912
- }
913
- requestIds.clear()
914
- }
915
-
916
- fun jsiAbortRequest(
917
- runtimeId: Int,
918
- requestIdAsDouble: Double,
919
- ) {
920
- val requestId = requestIdAsDouble.toInt()
921
- cancelRequest(runtimeId, requestId)
922
- removeRequest(runtimeId, requestId)
923
- }
924
-
925
- private fun cancelRequest(
926
- runtimeId: Int,
927
- requestId: Int,
928
- ) {
929
- WorkletsOkHttpCallUtil.cancelTag(client, RequestId(runtimeId, requestId).hashCode())
930
- }
931
-
932
- fun jsiClearCookies(callback: com.facebook.react.bridge.Callback) {
933
- cookieHandler.clearCookies(callback)
934
- }
935
-
936
- public fun addListener(eventName: String?): Unit = Unit
937
-
938
- public fun removeListeners(count: Double): Unit = Unit
939
-
940
- private fun constructMultipartBody(
941
- runtimeWrapper: WorkletRuntimeWrapper,
942
- body: ReadableArray,
943
- contentType: String,
944
- requestId: Int,
945
- devToolsRequestId: String,
946
- ): MultipartBody.Builder? {
947
- // val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
948
- val multipartBuilder = MultipartBody.Builder()
949
- val mediaType = MediaType.parse(contentType)
950
- if (mediaType == null) {
951
- WorkletsNetworkEventUtil.onRequestError(
952
- runtimeWrapper,
953
- requestId,
954
- devToolsRequestId,
955
- "Invalid media type.",
956
- null,
957
- )
958
- return null
959
- }
960
- multipartBuilder.setType(mediaType)
961
-
962
- for (i in 0 until body.size()) {
963
- val bodyPart = body.getMap(i)
964
- if (bodyPart == null) {
965
- WorkletsNetworkEventUtil.onRequestError(
966
- runtimeWrapper,
967
- requestId,
968
- devToolsRequestId,
969
- "Unrecognized FormData part.",
970
- null,
971
- )
972
- return null
973
- }
974
-
975
- // Determine part's content type.
976
- val headersArray = bodyPart.getArray("headers")
977
- var headers = extractHeaders(headersArray, null)
978
- if (headers == null) {
979
- WorkletsNetworkEventUtil.onRequestError(
980
- runtimeWrapper,
981
- requestId,
982
- devToolsRequestId,
983
- "Missing or invalid header format for FormData part.",
984
- null,
985
- )
986
- return null
987
- }
988
- var partContentType: MediaType? = null
989
- val partContentTypeStr = headers[CONTENT_TYPE_HEADER_NAME]
990
- if (partContentTypeStr != null) {
991
- partContentType = MediaType.parse(partContentTypeStr)
992
- // Remove the content-type header because MultipartBuilder gets it explicitly as an
993
- // argument and doesn't expect it in the headers array.
994
- headers = headers.newBuilder().removeAll(CONTENT_TYPE_HEADER_NAME).build()
995
- }
996
-
997
- if (
998
- bodyPart.hasKey(REQUEST_BODY_KEY_STRING) &&
999
- bodyPart.getString(REQUEST_BODY_KEY_STRING) != null
1000
- ) {
1001
- val bodyValue = bodyPart.getString(REQUEST_BODY_KEY_STRING).orEmpty()
1002
- @Suppress("DEPRECATION")
1003
- multipartBuilder.addPart(headers, RequestBody.create(partContentType, bodyValue))
1004
- } else if (
1005
- bodyPart.hasKey(REQUEST_BODY_KEY_URI) && bodyPart.getString(REQUEST_BODY_KEY_URI) != null
1006
- ) {
1007
- if (partContentType == null) {
1008
- WorkletsNetworkEventUtil.onRequestError(
1009
- runtimeWrapper,
1010
- requestId,
1011
- devToolsRequestId,
1012
- "Binary FormData part needs a content-type header.",
1013
- null,
1014
- )
1015
- return null
1016
- }
1017
- val fileContentUriStr = bodyPart.getString(REQUEST_BODY_KEY_URI)
1018
- if (fileContentUriStr == null) {
1019
- WorkletsNetworkEventUtil.onRequestError(
1020
- runtimeWrapper,
1021
- requestId,
1022
- devToolsRequestId,
1023
- "Body must have a valid file uri",
1024
- null,
1025
- )
1026
- return null
1027
- }
1028
- // TODO: Fix me
1029
- val fileInputStream = null
1030
- // WorkletsRequestBodyUtil.getFileInputStream(getReactApplicationContext(), fileContentUriStr)
1031
- if (fileInputStream == null) {
1032
- WorkletsNetworkEventUtil.onRequestError(
1033
- runtimeWrapper,
1034
- requestId,
1035
- devToolsRequestId,
1036
- "Could not retrieve file for uri $fileContentUriStr",
1037
- null,
1038
- )
1039
- return null
1040
- }
1041
- multipartBuilder.addPart(headers, WorkletsRequestBodyUtil.create(partContentType, fileInputStream))
1042
- } else {
1043
- WorkletsNetworkEventUtil.onRequestError(
1044
- runtimeWrapper,
1045
- requestId,
1046
- devToolsRequestId,
1047
- "Unrecognized FormData part.",
1048
- null,
1049
- )
1050
- }
1051
- }
1052
- return multipartBuilder
1053
- }
1054
-
1055
- /**
1056
- * Extracts the headers from the Array. If the format is invalid, this method will return null.
1057
- */
1058
- private fun extractHeaders(
1059
- headersArray: ReadableArray?,
1060
- requestData: ReadableMap?,
1061
- ): Headers? {
1062
- if (headersArray == null) {
1063
- return null
1064
- }
1065
- val headersBuilder = Headers.Builder()
1066
- for (headersIdx in 0 until headersArray.size()) {
1067
- val header = headersArray.getArray(headersIdx)
1068
- if (header == null || header.size() != 2) {
1069
- return null
1070
- }
1071
- var headerName: String? = header.getString(0)
1072
- if (headerName != null) {
1073
- headerName = WorkletsHeaderUtil.stripHeaderName(headerName)
1074
- }
1075
- val headerValue = header.getString(1)
1076
- if (headerName == null || headerValue == null) {
1077
- return null
1078
- }
1079
- headersBuilder.addUnsafeNonAscii(headerName, headerValue)
1080
- }
1081
- if (headersBuilder[USER_AGENT_HEADER_NAME] == null && defaultUserAgent != null) {
1082
- headersBuilder.add(USER_AGENT_HEADER_NAME, defaultUserAgent)
1083
- }
1084
-
1085
- // Sanitize content encoding header, supported only when request specify payload as string
1086
- val isGzipSupported = requestData?.hasKey(REQUEST_BODY_KEY_STRING) == true
1087
- if (!isGzipSupported) {
1088
- headersBuilder.removeAll(CONTENT_ENCODING_HEADER_NAME)
1089
- }
1090
-
1091
- return headersBuilder.build()
1092
- }
1093
-
1094
- public companion object {
1095
- // public const val NAME: String = NativeNetworkingAndroidSpec.NAME
1096
- // private const val TAG: String = NativeNetworkingAndroidSpec.NAME
1097
- private const val CONTENT_ENCODING_HEADER_NAME = "content-encoding"
1098
- private const val CONTENT_TYPE_HEADER_NAME = "content-type"
1099
- private const val REQUEST_BODY_KEY_STRING = "string"
1100
- private const val REQUEST_BODY_KEY_URI = "uri"
1101
- private const val REQUEST_BODY_KEY_FORMDATA = "formData"
1102
- private const val REQUEST_BODY_KEY_BASE64 = "base64"
1103
- private const val REQUEST_DATA_KEY_DEVTOOLS_REQUEST_ID = "devToolsRequestId"
1104
- private const val USER_AGENT_HEADER_NAME = "user-agent"
1105
- private const val CHUNK_TIMEOUT_NS = 100 * 1_000_000 // 100ms
1106
- private const val MAX_CHUNK_SIZE_BETWEEN_FLUSHES = 8 * 1_024 // 8K
1107
-
1108
- private var customClientBuilder: com.facebook.react.modules.network.CustomClientBuilder? = null
1109
-
1110
- @JvmStatic
1111
- public fun setCustomClientBuilder(ccb: com.facebook.react.modules.network.CustomClientBuilder?) {
1112
- customClientBuilder = ccb
1113
- }
1114
-
1115
- private fun applyCustomBuilder(builder: OkHttpClient.Builder) {
1116
- customClientBuilder?.apply(builder)
1117
- }
1118
-
1119
- private fun shouldDispatch(
1120
- now: Long,
1121
- last: Long,
1122
- ): Boolean = last + CHUNK_TIMEOUT_NS < now
1123
- }
1124
- }