expo-http-server 0.1.5 → 0.1.6
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/README.md +0 -2
- package/ios/ExpoHttpServerModule.swift +14 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,8 +14,6 @@ iOS: [Criollo](https://github.com/thecatalinstan/Criollo)
|
|
|
14
14
|
Android: [AndroidServer](https://github.com/fengzhizi715/AndroidServer)
|
|
15
15
|
Web: **Not implemented**
|
|
16
16
|
|
|
17
|
-
This is a work in progress and not yet used in a production app (there are surely things I've overlooked), but please do make a PR for any improvements / problems you can see!
|
|
18
|
-
|
|
19
17
|
## Install
|
|
20
18
|
|
|
21
19
|
```shell
|
|
@@ -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 {
|
|
@@ -36,12 +36,13 @@ public class ExpoHttpServerModule: Module {
|
|
|
36
36
|
|
|
37
37
|
private func routeHandler(path: String, method: String) {
|
|
38
38
|
server.add(path, block: { [weak self] (req, res, next) in
|
|
39
|
+
guard let strongSelf = self else { return }
|
|
39
40
|
let uuid = UUID().uuidString
|
|
40
41
|
var bodyString = "{}"
|
|
41
42
|
if let body = req.body, let bodyData = try? JSONSerialization.data(withJSONObject: body) {
|
|
42
43
|
bodyString = String(data: bodyData, encoding: .utf8) ?? "{}"
|
|
43
44
|
}
|
|
44
|
-
|
|
45
|
+
strongSelf.sendEvent("onRequest", [
|
|
45
46
|
"uuid": uuid,
|
|
46
47
|
"method": req.method.toString(),
|
|
47
48
|
"path": path,
|
|
@@ -50,7 +51,12 @@ public class ExpoHttpServerModule: Module {
|
|
|
50
51
|
"paramsJson": req.query.jsonString,
|
|
51
52
|
"cookiesJson": req.cookies?.jsonString ?? "{}"
|
|
52
53
|
])
|
|
53
|
-
|
|
54
|
+
DispatchQueue.main.async {
|
|
55
|
+
if (strongSelf.responses == nil) {
|
|
56
|
+
strongSelf.responses = [:];
|
|
57
|
+
}
|
|
58
|
+
strongSelf.responses?[uuid] = res
|
|
59
|
+
}
|
|
54
60
|
}, recursive: false, method: CRHTTPMethod.fromString(method))
|
|
55
61
|
}
|
|
56
62
|
|
|
@@ -58,12 +64,14 @@ public class ExpoHttpServerModule: Module {
|
|
|
58
64
|
statusCode: Int,
|
|
59
65
|
contentType: String,
|
|
60
66
|
body: String) {
|
|
61
|
-
if let response = responses[udid] {
|
|
67
|
+
if let responses = responses, let response = responses[udid] {
|
|
62
68
|
response.setStatusCode(UInt(statusCode), description: "ExpoHttpServer")
|
|
63
69
|
response.setValue(contentType, forHTTPHeaderField: "Content-type")
|
|
64
70
|
response.setValue("\(body.count)", forHTTPHeaderField: "Content-Length")
|
|
65
71
|
response.send(body);
|
|
66
|
-
|
|
72
|
+
DispatchQueue.main.async {
|
|
73
|
+
self.responses?[udid] = nil;
|
|
74
|
+
}
|
|
67
75
|
}
|
|
68
76
|
}
|
|
69
77
|
|
|
@@ -73,6 +81,7 @@ public class ExpoHttpServerModule: Module {
|
|
|
73
81
|
}
|
|
74
82
|
|
|
75
83
|
private func startServer(status: String, message: String) {
|
|
84
|
+
responses = [:];
|
|
76
85
|
stopServer()
|
|
77
86
|
if let port = port {
|
|
78
87
|
var error: NSError?
|