expo-http-server 0.1.6 → 0.1.7
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/ios/ExpoHttpServerModule.swift +26 -30
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ 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 responses
|
|
8
|
+
private var responses = [String: CRResponse]()
|
|
9
9
|
private var bgTaskIdentifier = UIBackgroundTaskIdentifier.invalid
|
|
10
10
|
|
|
11
11
|
public func definition() -> ModuleDefinition {
|
|
@@ -35,27 +35,23 @@ public class ExpoHttpServerModule: Module {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
private func routeHandler(path: String, method: String) {
|
|
38
|
-
server.add(path, block: {
|
|
39
|
-
guard let strongSelf = self else { return }
|
|
40
|
-
let uuid = UUID().uuidString
|
|
41
|
-
var bodyString = "{}"
|
|
42
|
-
if let body = req.body, let bodyData = try? JSONSerialization.data(withJSONObject: body) {
|
|
43
|
-
bodyString = String(data: bodyData, encoding: .utf8) ?? "{}"
|
|
44
|
-
}
|
|
45
|
-
strongSelf.sendEvent("onRequest", [
|
|
46
|
-
"uuid": uuid,
|
|
47
|
-
"method": req.method.toString(),
|
|
48
|
-
"path": path,
|
|
49
|
-
"body": bodyString,
|
|
50
|
-
"headersJson": req.allHTTPHeaderFields.jsonString,
|
|
51
|
-
"paramsJson": req.query.jsonString,
|
|
52
|
-
"cookiesJson": req.cookies?.jsonString ?? "{}"
|
|
53
|
-
])
|
|
38
|
+
server.add(path, block: { (req, res, next) in
|
|
54
39
|
DispatchQueue.main.async {
|
|
55
|
-
|
|
56
|
-
|
|
40
|
+
let uuid = UUID().uuidString
|
|
41
|
+
var bodyString = "{}"
|
|
42
|
+
if let body = req.body, let bodyData = try? JSONSerialization.data(withJSONObject: body) {
|
|
43
|
+
bodyString = String(data: bodyData, encoding: .utf8) ?? "{}"
|
|
57
44
|
}
|
|
58
|
-
|
|
45
|
+
self.responses[uuid] = res
|
|
46
|
+
self.sendEvent("onRequest", [
|
|
47
|
+
"uuid": uuid,
|
|
48
|
+
"method": req.method.toString(),
|
|
49
|
+
"path": path,
|
|
50
|
+
"body": bodyString,
|
|
51
|
+
"headersJson": req.allHTTPHeaderFields.jsonString,
|
|
52
|
+
"paramsJson": req.query.jsonString,
|
|
53
|
+
"cookiesJson": req.cookies?.jsonString ?? "{}"
|
|
54
|
+
])
|
|
59
55
|
}
|
|
60
56
|
}, recursive: false, method: CRHTTPMethod.fromString(method))
|
|
61
57
|
}
|
|
@@ -64,15 +60,16 @@ public class ExpoHttpServerModule: Module {
|
|
|
64
60
|
statusCode: Int,
|
|
65
61
|
contentType: String,
|
|
66
62
|
body: String) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
63
|
+
DispatchQueue.main.async {
|
|
64
|
+
if let response = self.responses[udid] {
|
|
65
|
+
response.setStatusCode(UInt(statusCode), description: "ExpoHttpServer")
|
|
66
|
+
response.setValue(contentType, forHTTPHeaderField: "Content-type")
|
|
67
|
+
response.setValue("\(body.count)", forHTTPHeaderField: "Content-Length")
|
|
68
|
+
response.send(body);
|
|
69
|
+
self.responses[udid] = nil;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
76
73
|
}
|
|
77
74
|
|
|
78
75
|
private func stopHandler() {
|
|
@@ -81,7 +78,6 @@ public class ExpoHttpServerModule: Module {
|
|
|
81
78
|
}
|
|
82
79
|
|
|
83
80
|
private func startServer(status: String, message: String) {
|
|
84
|
-
responses = [:];
|
|
85
81
|
stopServer()
|
|
86
82
|
if let port = port {
|
|
87
83
|
var error: NSError?
|