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.
@@ -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: [String: CRResponse]?
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: { [weak self] (req, res, next) in
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
- if (strongSelf.responses == nil) {
56
- strongSelf.responses = [:];
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
- strongSelf.responses?[uuid] = res
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
- if let responses = responses, let response = responses[udid] {
68
- response.setStatusCode(UInt(statusCode), description: "ExpoHttpServer")
69
- response.setValue(contentType, forHTTPHeaderField: "Content-type")
70
- response.setValue("\(body.count)", forHTTPHeaderField: "Content-Length")
71
- response.send(body);
72
- DispatchQueue.main.async {
73
- self.responses?[udid] = nil;
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?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-http-server",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "A simple HTTP server expo module (iOS/Android)",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",