expo-http-server 0.1.13 → 0.1.14-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/expo/modules/httpserver/ExpoHttpServerModule.kt +22 -11
- package/build/index.d.ts.map +1 -1
- package/build/index.js +13 -7
- package/build/index.js.map +1 -1
- package/ios/ExpoHttpServer.podspec +1 -1
- package/ios/ExpoHttpServerModule.swift +57 -32
- package/package.json +1 -1
- package/src/index.ts +23 -19
|
@@ -9,6 +9,7 @@ import com.safframework.server.core.http.HttpMethod
|
|
|
9
9
|
import com.safframework.server.core.http.Request
|
|
10
10
|
import com.safframework.server.core.http.Response
|
|
11
11
|
import org.json.JSONObject
|
|
12
|
+
import java.util.concurrent.ConcurrentHashMap
|
|
12
13
|
|
|
13
14
|
class ExpoHttpServerModule : Module() {
|
|
14
15
|
class SimpleHttpResponse(val statusCode: Int,
|
|
@@ -19,7 +20,7 @@ class ExpoHttpServerModule : Module() {
|
|
|
19
20
|
|
|
20
21
|
private var server: Server? = null;
|
|
21
22
|
private var started = false;
|
|
22
|
-
private val responses =
|
|
23
|
+
private val responses = ConcurrentHashMap<String, SimpleHttpResponse>()
|
|
23
24
|
|
|
24
25
|
override fun definition() = ModuleDefinition {
|
|
25
26
|
|
|
@@ -28,6 +29,8 @@ class ExpoHttpServerModule : Module() {
|
|
|
28
29
|
Events("onStatusUpdate", "onRequest")
|
|
29
30
|
|
|
30
31
|
Function("setup") { port: Int ->
|
|
32
|
+
server?.close()
|
|
33
|
+
started = false
|
|
31
34
|
server = AndroidServer.Builder{
|
|
32
35
|
port {
|
|
33
36
|
port
|
|
@@ -49,10 +52,11 @@ class ExpoHttpServerModule : Module() {
|
|
|
49
52
|
"paramsJson" to JSONObject(params).toString(),
|
|
50
53
|
"cookiesJson" to JSONObject(cookies).toString(),
|
|
51
54
|
))
|
|
52
|
-
|
|
55
|
+
val deadline = System.currentTimeMillis() + 30_000
|
|
56
|
+
while (!responses.containsKey(uuid) && System.currentTimeMillis() < deadline) {
|
|
53
57
|
Thread.sleep(10)
|
|
54
58
|
}
|
|
55
|
-
val res = responses
|
|
59
|
+
val res = responses.remove(uuid) ?: return@request response
|
|
56
60
|
response.setBodyText(res.body)
|
|
57
61
|
response.setStatus(res.statusCode)
|
|
58
62
|
response.addHeader("Content-Length", "" + res.body.length)
|
|
@@ -60,7 +64,6 @@ class ExpoHttpServerModule : Module() {
|
|
|
60
64
|
for ((key, value) in res.headers) {
|
|
61
65
|
response.addHeader(key, value)
|
|
62
66
|
}
|
|
63
|
-
responses.remove(uuid);
|
|
64
67
|
return@request response
|
|
65
68
|
};
|
|
66
69
|
}
|
|
@@ -73,12 +76,20 @@ class ExpoHttpServerModule : Module() {
|
|
|
73
76
|
))
|
|
74
77
|
} else {
|
|
75
78
|
if (!started) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
"
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
try {
|
|
80
|
+
started = true
|
|
81
|
+
server?.start()
|
|
82
|
+
sendEvent("onStatusUpdate", bundleOf(
|
|
83
|
+
"status" to "STARTED",
|
|
84
|
+
"message" to "Server started"
|
|
85
|
+
))
|
|
86
|
+
} catch (e: Exception) {
|
|
87
|
+
started = false
|
|
88
|
+
sendEvent("onStatusUpdate", bundleOf(
|
|
89
|
+
"status" to "ERROR",
|
|
90
|
+
"message" to (e.message ?: "Failed to start server")
|
|
91
|
+
))
|
|
92
|
+
}
|
|
82
93
|
}
|
|
83
94
|
}
|
|
84
95
|
}
|
|
@@ -101,4 +112,4 @@ class ExpoHttpServerModule : Module() {
|
|
|
101
112
|
))
|
|
102
113
|
}
|
|
103
114
|
}
|
|
104
|
-
}
|
|
115
|
+
}
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,UAAU,GAAG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MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,QAAQ;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CACxD;AAED,eAAO,MAAM,KAAK,YA2BjB,CAAC;AAEF,eAAO,MAAM,KAAK,SACV,MAAM,UACJ,UAAU,sBACE,YAAY,KAAK,QAAQ,QAAQ,CAAC,SAUvD,CAAC;AAEF,eAAO,MAAM,KAAK,SACV,MAAM,4BACa,WAAW,KAAK,IAAI,sBAU9C,CAAC;AAEF,eAAO,MAAM,IAAI,WAAoC,CAAC"}
|
package/build/index.js
CHANGED
|
@@ -4,13 +4,16 @@ const emitter = new EventEmitter(ExpoHttpServerModule);
|
|
|
4
4
|
const requestCallbacks = [];
|
|
5
5
|
export const start = () => {
|
|
6
6
|
emitter.addListener("onRequest", async (event) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
try {
|
|
8
|
+
const responseHandler = requestCallbacks.find((c) => c.uuid === event.uuid);
|
|
9
|
+
if (!responseHandler) {
|
|
10
|
+
ExpoHttpServerModule.respond(event.uuid, 404, "Not Found", "application/json", {}, JSON.stringify({ error: "Handler not found" }));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const response = await responseHandler.callback(event);
|
|
14
|
+
ExpoHttpServerModule.respond(event.uuid, response.statusCode || 200, response.statusDescription || "OK", response.contentType || "application/json", response.headers || {}, response.body || "{}");
|
|
11
15
|
}
|
|
12
|
-
|
|
13
|
-
ExpoHttpServerModule.respond(event.uuid, response.statusCode || 200, response.statusDescription || "OK", response.contentType || "application/json", response.headers || {}, response.body || "{}");
|
|
16
|
+
catch { }
|
|
14
17
|
});
|
|
15
18
|
ExpoHttpServerModule.start();
|
|
16
19
|
};
|
|
@@ -27,7 +30,10 @@ export const route = (path, method, callback) => {
|
|
|
27
30
|
export const setup = (port, onStatusUpdate) => {
|
|
28
31
|
if (onStatusUpdate) {
|
|
29
32
|
emitter.addListener("onStatusUpdate", async (event) => {
|
|
30
|
-
|
|
33
|
+
try {
|
|
34
|
+
await onStatusUpdate(event);
|
|
35
|
+
}
|
|
36
|
+
catch { }
|
|
31
37
|
});
|
|
32
38
|
}
|
|
33
39
|
ExpoHttpServerModule.setup(port);
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,oBAAoB,MAAM,wBAAwB,CAAC;AAE1D,MAAM,OAAO,GAAG,IAAI,YAAY,CAAC,oBAAoB,CAAC,CAAC;AACvD,MAAM,gBAAgB,GAAe,EAAE,CAAC;AAsCxC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,EAAE;IACxB,OAAO,CAAC,WAAW,CAAe,WAAW,EAAE,KAAK,EAAE,KAA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AAI,IAAI,IAAI,CACtB,CAAC;QACJ,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;IACZ,CAAC,CAAC,CAAC;IACH,oBAAoB,CAAC,KAAK,EAAE,CAAC;AAC/B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,KAAK,GAAG,CACnB,IAAY,EACZ,MAAkB,EAClB,QAAsD,EACtD,EAAE;IACF,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACjD,gBAAgB,CAAC,IAAI,CAAC;QACpB,MAAM;QACN,IAAI;QACJ,IAAI;QACJ,QAAQ;KACT,CAAC,CAAC;IACH,oBAAoB,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;AACjD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,KAAK,GAAG,CACnB,IAAY,EACZ,cAA6C,EAC7C,EAAE;IACF,IAAI,cAAc,EAAE,CAAC;QACnB,OAAO,CAAC,WAAW,CAAc,gBAAgB,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;YACjE,IAAI,CAAC;gBACH,MAAM,cAAc,CAAC,KAAK,CAAC,CAAC;YAC9B,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC;IACD,oBAAoB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC,oBAAoB,CAAC,IAAI,EAAE,CAAC","sourcesContent":["import { EventEmitter } from \"expo-modules-core\";\n\nimport ExpoHttpServerModule from \"./ExpoHttpServerModule\";\n\nconst emitter = new EventEmitter(ExpoHttpServerModule);\nconst requestCallbacks: Callback[] = [];\n\nexport type HttpMethod = \"GET\" | \"POST\" | \"PUT\" | \"DELETE\" | \"OPTIONS\";\n/**\n * PAUSED AND RESUMED are iOS only\n */\nexport type Status = \"STARTED\" | \"PAUSED\" | \"RESUMED\" | \"STOPPED\" | \"ERROR\";\n\nexport interface StatusEvent {\n status: Status;\n message: string;\n}\n\nexport interface RequestEvent {\n uuid: string;\n method: string;\n path: string;\n body: string;\n headersJson: string;\n paramsJson: string;\n cookiesJson: string;\n}\n\nexport interface Response {\n statusCode?: number;\n statusDescription?: string;\n contentType?: string;\n headers?: Record<string, string>;\n body?: string;\n}\n\nexport interface Callback {\n method: string;\n path: string;\n uuid: string;\n callback: (request: RequestEvent) => Promise<Response>;\n}\n\nexport const start = () => {\n emitter.addListener<RequestEvent>(\"onRequest\", async (event) => {\n try {\n const responseHandler = requestCallbacks.find((c) => c.uuid === event.uuid);\n if (!responseHandler) {\n ExpoHttpServerModule.respond(\n event.uuid,\n 404,\n \"Not Found\",\n \"application/json\",\n {},\n JSON.stringify({ error: \"Handler not found\" }),\n );\n return;\n }\n const response = await responseHandler.callback(event);\n ExpoHttpServerModule.respond(\n event.uuid,\n response.statusCode || 200,\n response.statusDescription || \"OK\",\n response.contentType || \"application/json\",\n response.headers || {},\n response.body || \"{}\",\n );\n } catch {}\n });\n ExpoHttpServerModule.start();\n};\n\nexport const route = (\n path: string,\n method: HttpMethod,\n callback: (request: RequestEvent) => Promise<Response>,\n) => {\n const uuid = Math.random().toString(16).slice(2);\n requestCallbacks.push({\n method,\n path,\n uuid,\n callback,\n });\n ExpoHttpServerModule.route(path, method, uuid);\n};\n\nexport const setup = (\n port: number,\n onStatusUpdate?: (event: StatusEvent) => void,\n) => {\n if (onStatusUpdate) {\n emitter.addListener<StatusEvent>(\"onStatusUpdate\", async (event) => {\n try {\n await onStatusUpdate(event);\n } catch {}\n });\n }\n ExpoHttpServerModule.setup(port);\n};\n\nexport const stop = () => ExpoHttpServerModule.stop();\n"]}
|
|
@@ -10,7 +10,7 @@ Pod::Spec.new do |s|
|
|
|
10
10
|
s.license = package['license']
|
|
11
11
|
s.author = package['author']
|
|
12
12
|
s.homepage = package['homepage']
|
|
13
|
-
s.platform = :ios, '
|
|
13
|
+
s.platform = :ios, '16.4'
|
|
14
14
|
s.swift_version = '5.4'
|
|
15
15
|
s.source = { git: 'https://github.com/simonsturge/expo-http-server' }
|
|
16
16
|
s.static_framework = true
|
|
@@ -5,35 +5,43 @@ public class ExpoHttpServerModule: Module {
|
|
|
5
5
|
private let server = CRHTTPServer()
|
|
6
6
|
private var port: Int?
|
|
7
7
|
private var stopped = false
|
|
8
|
+
private var isRunning = false
|
|
8
9
|
private var responses = [String: CRResponse]()
|
|
9
10
|
private var bgTaskIdentifier = UIBackgroundTaskIdentifier.invalid
|
|
10
|
-
|
|
11
|
+
private var foregroundObserver: NSObjectProtocol?
|
|
12
|
+
|
|
11
13
|
public func definition() -> ModuleDefinition {
|
|
12
14
|
Name("ExpoHttpServer")
|
|
13
15
|
|
|
14
16
|
Events("onStatusUpdate", "onRequest")
|
|
15
|
-
|
|
17
|
+
|
|
16
18
|
Function("setup", setupHandler)
|
|
17
19
|
Function("start", startHandler)
|
|
18
20
|
Function("route", routeHandler)
|
|
19
21
|
Function("respond", respondHandler)
|
|
20
22
|
Function("stop", stopHandler)
|
|
21
23
|
}
|
|
22
|
-
|
|
24
|
+
|
|
23
25
|
private func setupHandler(port: Int) {
|
|
24
26
|
self.port = port;
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
private func startHandler() {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
self.startServer(status: "RESUMED", message: "Server resumed")
|
|
31
|
-
}
|
|
30
|
+
if let obs = foregroundObserver {
|
|
31
|
+
NotificationCenter.default.removeObserver(obs)
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
foregroundObserver = NotificationCenter.default.addObserver(
|
|
34
|
+
forName: UIApplication.willEnterForegroundNotification,
|
|
35
|
+
object: nil,
|
|
36
|
+
queue: .main
|
|
37
|
+
) { [weak self] _ in
|
|
38
|
+
guard let self, !self.stopped else { return }
|
|
39
|
+
self.startServer(status: "RESUMED", message: "Server resumed")
|
|
40
|
+
}
|
|
41
|
+
stopped = false
|
|
34
42
|
startServer(status: "STARTED", message: "Server started")
|
|
35
43
|
}
|
|
36
|
-
|
|
44
|
+
|
|
37
45
|
private func routeHandler(path: String, method: String, uuid: String) {
|
|
38
46
|
server.add(path, block: { (req, res, next) in
|
|
39
47
|
DispatchQueue.main.async {
|
|
@@ -54,7 +62,7 @@ public class ExpoHttpServerModule: Module {
|
|
|
54
62
|
}
|
|
55
63
|
}, recursive: false, method: CRHTTPMethod.fromString(method))
|
|
56
64
|
}
|
|
57
|
-
|
|
65
|
+
|
|
58
66
|
private func respondHandler(udid: String,
|
|
59
67
|
statusCode: Int,
|
|
60
68
|
statusDescription: String,
|
|
@@ -73,41 +81,58 @@ public class ExpoHttpServerModule: Module {
|
|
|
73
81
|
self.responses[udid] = nil;
|
|
74
82
|
}
|
|
75
83
|
}
|
|
76
|
-
|
|
77
84
|
}
|
|
78
|
-
|
|
85
|
+
|
|
79
86
|
private func stopHandler() {
|
|
80
|
-
stopped = true
|
|
87
|
+
stopped = true
|
|
88
|
+
if let obs = foregroundObserver {
|
|
89
|
+
NotificationCenter.default.removeObserver(obs)
|
|
90
|
+
foregroundObserver = nil
|
|
91
|
+
}
|
|
81
92
|
stopServer(status: "STOPPED", message: "Server stopped")
|
|
82
93
|
}
|
|
83
|
-
|
|
94
|
+
|
|
84
95
|
private func startServer(status: String, message: String) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
"status": "ERROR",
|
|
92
|
-
"message": error?.localizedDescription ?? "Unknown error starting server"
|
|
93
|
-
])
|
|
94
|
-
} else {
|
|
95
|
-
beginBackgroundTask()
|
|
96
|
-
sendEvent("onStatusUpdate", [
|
|
97
|
-
"status": status,
|
|
98
|
-
"message": message
|
|
99
|
-
])
|
|
96
|
+
if isRunning {
|
|
97
|
+
server.stopListening()
|
|
98
|
+
isRunning = false
|
|
99
|
+
endBackgroundTask()
|
|
100
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
|
|
101
|
+
self?.doStart(status: status, message: message)
|
|
100
102
|
}
|
|
101
103
|
} else {
|
|
104
|
+
doStart(status: status, message: message)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private func doStart(status: String, message: String) {
|
|
109
|
+
guard let port = port else {
|
|
102
110
|
sendEvent("onStatusUpdate", [
|
|
103
111
|
"status": "ERROR",
|
|
104
112
|
"message": "Can't start server with port configured"
|
|
105
113
|
])
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
var error: NSError?
|
|
117
|
+
server.startListening(&error, portNumber: UInt(port))
|
|
118
|
+
if let error = error {
|
|
119
|
+
sendEvent("onStatusUpdate", [
|
|
120
|
+
"status": "ERROR",
|
|
121
|
+
"message": error.localizedDescription
|
|
122
|
+
])
|
|
123
|
+
} else {
|
|
124
|
+
isRunning = true
|
|
125
|
+
beginBackgroundTask()
|
|
126
|
+
sendEvent("onStatusUpdate", [
|
|
127
|
+
"status": status,
|
|
128
|
+
"message": message
|
|
129
|
+
])
|
|
106
130
|
}
|
|
107
131
|
}
|
|
108
|
-
|
|
132
|
+
|
|
109
133
|
private func stopServer(status: String? = nil, message: String? = nil) {
|
|
110
134
|
server.stopListening()
|
|
135
|
+
isRunning = false
|
|
111
136
|
endBackgroundTask()
|
|
112
137
|
if let status = status, let message = message {
|
|
113
138
|
sendEvent("onStatusUpdate", [
|
|
@@ -116,7 +141,7 @@ public class ExpoHttpServerModule: Module {
|
|
|
116
141
|
])
|
|
117
142
|
}
|
|
118
143
|
}
|
|
119
|
-
|
|
144
|
+
|
|
120
145
|
private func beginBackgroundTask() {
|
|
121
146
|
if (bgTaskIdentifier == UIBackgroundTaskIdentifier.invalid) {
|
|
122
147
|
self.bgTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "BgTask", expirationHandler: {
|
|
@@ -155,7 +180,7 @@ extension CRHTTPMethod {
|
|
|
155
180
|
return "GET"
|
|
156
181
|
}
|
|
157
182
|
}
|
|
158
|
-
|
|
183
|
+
|
|
159
184
|
static func fromString(_ string: String) -> Self {
|
|
160
185
|
var httpMethod: CRHTTPMethod
|
|
161
186
|
switch (string) {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -43,27 +43,29 @@ export interface Callback {
|
|
|
43
43
|
|
|
44
44
|
export const start = () => {
|
|
45
45
|
emitter.addListener<RequestEvent>("onRequest", async (event) => {
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
try {
|
|
47
|
+
const responseHandler = requestCallbacks.find((c) => c.uuid === event.uuid);
|
|
48
|
+
if (!responseHandler) {
|
|
49
|
+
ExpoHttpServerModule.respond(
|
|
50
|
+
event.uuid,
|
|
51
|
+
404,
|
|
52
|
+
"Not Found",
|
|
53
|
+
"application/json",
|
|
54
|
+
{},
|
|
55
|
+
JSON.stringify({ error: "Handler not found" }),
|
|
56
|
+
);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const response = await responseHandler.callback(event);
|
|
48
60
|
ExpoHttpServerModule.respond(
|
|
49
61
|
event.uuid,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"application/json",
|
|
53
|
-
{},
|
|
54
|
-
|
|
62
|
+
response.statusCode || 200,
|
|
63
|
+
response.statusDescription || "OK",
|
|
64
|
+
response.contentType || "application/json",
|
|
65
|
+
response.headers || {},
|
|
66
|
+
response.body || "{}",
|
|
55
67
|
);
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
const response = await responseHandler.callback(event);
|
|
59
|
-
ExpoHttpServerModule.respond(
|
|
60
|
-
event.uuid,
|
|
61
|
-
response.statusCode || 200,
|
|
62
|
-
response.statusDescription || "OK",
|
|
63
|
-
response.contentType || "application/json",
|
|
64
|
-
response.headers || {},
|
|
65
|
-
response.body || "{}",
|
|
66
|
-
);
|
|
68
|
+
} catch {}
|
|
67
69
|
});
|
|
68
70
|
ExpoHttpServerModule.start();
|
|
69
71
|
};
|
|
@@ -89,7 +91,9 @@ export const setup = (
|
|
|
89
91
|
) => {
|
|
90
92
|
if (onStatusUpdate) {
|
|
91
93
|
emitter.addListener<StatusEvent>("onStatusUpdate", async (event) => {
|
|
92
|
-
|
|
94
|
+
try {
|
|
95
|
+
await onStatusUpdate(event);
|
|
96
|
+
} catch {}
|
|
93
97
|
});
|
|
94
98
|
}
|
|
95
99
|
ExpoHttpServerModule.setup(port);
|