@mentra/crust 3.3.0-dev.514 → 3.3.0-dev.516

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.
@@ -170,13 +170,23 @@ class CrustModule : Module() {
170
170
  }
171
171
 
172
172
  AsyncFunction("nativeHttpRequest") {
173
- method: String, url: String, headers: Map<String, String>, body: String? ->
174
- val result = JSCPolyfillBridge.executeHttp(method, url, headers, body)
175
- mapOf(
176
- "status" to result.status,
177
- "statusText" to result.statusText,
178
- "headers" to result.headers,
179
- "body" to result.body,
173
+ method: String, url: String, headers: Map<String, String>, body: String?, promise: expo.modules.kotlin.Promise ->
174
+ JSCPolyfillBridge.enqueueHttp(
175
+ method,
176
+ url,
177
+ headers,
178
+ body,
179
+ onResult = { result ->
180
+ promise.resolve(
181
+ mapOf(
182
+ "status" to result.status,
183
+ "statusText" to result.statusText,
184
+ "headers" to result.headers,
185
+ "body" to result.body,
186
+ )
187
+ )
188
+ },
189
+ onError = { error -> promise.reject("E_NATIVE_HTTP", error.message ?: "Native HTTP request failed", error) },
180
190
  )
181
191
  }
182
192
 
@@ -52,21 +52,49 @@ object JSCPolyfillBridge {
52
52
  val body: String,
53
53
  )
54
54
 
55
- /** Shared OkHttp execution path for host cloud-client requests. */
56
- fun executeHttp(method: String, url: String, headers: Map<String, String>, bodyString: String?): HttpResult {
57
- val request = buildHttpRequest(method, url, headers, bodyString)
58
- httpClient.newCall(request).execute().use { response ->
59
- val responseHeaders = mutableMapOf<String, String>()
60
- for (name in response.headers.names()) {
61
- responseHeaders[name.lowercase()] = response.headers.values(name).joinToString(", ")
62
- }
63
- return HttpResult(
64
- status = response.code,
65
- statusText = response.message,
66
- headers = responseHeaders,
67
- body = response.body?.string() ?: "",
68
- )
55
+ /**
56
+ * Shared OkHttp path for host cloud-client requests. Asynchronous because Expo runs every
57
+ * module's AsyncFunction on one shared thread: a blocking call here stalls miniapp delivery
58
+ * and every other native module for the length of the request, up to [callTimeout].
59
+ */
60
+ fun enqueueHttp(
61
+ method: String,
62
+ url: String,
63
+ headers: Map<String, String>,
64
+ bodyString: String?,
65
+ onResult: (HttpResult) -> Unit,
66
+ onError: (Throwable) -> Unit,
67
+ ) {
68
+ val request = try {
69
+ buildHttpRequest(method, url, headers, bodyString)
70
+ } catch (e: Throwable) {
71
+ onError(e)
72
+ return
69
73
  }
74
+ httpClient.newCall(request).enqueue(object : Callback {
75
+ override fun onFailure(call: Call, e: IOException) = onError(e)
76
+
77
+ override fun onResponse(call: Call, response: Response) {
78
+ try {
79
+ response.use { r ->
80
+ val responseHeaders = mutableMapOf<String, String>()
81
+ for (name in r.headers.names()) {
82
+ responseHeaders[name.lowercase()] = r.headers.values(name).joinToString(", ")
83
+ }
84
+ onResult(
85
+ HttpResult(
86
+ status = r.code,
87
+ statusText = r.message,
88
+ headers = responseHeaders,
89
+ body = r.body?.string() ?: "",
90
+ )
91
+ )
92
+ }
93
+ } catch (e: Throwable) {
94
+ onError(e)
95
+ }
96
+ }
97
+ })
70
98
  }
71
99
 
72
100
  /** Keep host HTTP and miniapp fetch body semantics identical, including empty POSTs. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/crust",
3
- "version": "3.3.0-dev.514",
3
+ "version": "3.3.0-dev.516",
4
4
  "description": "Mentra Native Module",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  "license": "Apache-2.0",
36
36
  "homepage": "https://github.com/Mentra-Community/MentraOS/tree/dev/mobile/modules/crust#readme",
37
37
  "dependencies": {
38
- "@mentra/jspolyfill": "3.3.0-dev.514"
38
+ "@mentra/jspolyfill": "3.3.0-dev.516"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@expo/config-plugins": ">=8.0.0",