@cappitolian/http-local-server-swifter 0.0.14 → 0.0.16
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.
|
@@ -23,15 +23,25 @@ public protocol HttpLocalServerSwifterDelegate: AnyObject {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
@objc public func connect(_ call: CAPPluginCall) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
26
|
+
// Run server startup in a background thread to avoid blocking the Main/UI thread
|
|
27
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
28
|
+
self.disconnect()
|
|
29
|
+
self.webServer = HttpServer()
|
|
30
|
+
self.setupHandlers()
|
|
31
|
+
|
|
32
|
+
do {
|
|
33
|
+
try self.webServer?.start(self.defaultPort, forceIPv4: true)
|
|
34
|
+
let ip = Self.getWiFiAddress() ?? "127.0.0.1"
|
|
35
|
+
|
|
36
|
+
// Resolve the promise back to JS
|
|
37
|
+
call.resolve([
|
|
38
|
+
"ip": ip,
|
|
39
|
+
"port": Int(self.defaultPort)
|
|
40
|
+
])
|
|
41
|
+
print("🚀 Server started on \(ip):\(self.defaultPort)")
|
|
42
|
+
} catch {
|
|
43
|
+
call.reject("Failed to start server: \(error.localizedDescription)")
|
|
44
|
+
}
|
|
35
45
|
}
|
|
36
46
|
}
|
|
37
47
|
|
|
@@ -59,12 +69,19 @@ public protocol HttpLocalServerSwifterDelegate: AnyObject {
|
|
|
59
69
|
|
|
60
70
|
private func setupHandlers() {
|
|
61
71
|
guard let server = webServer else { return }
|
|
72
|
+
|
|
73
|
+
// This closure handles EVERY request regardless of the path
|
|
62
74
|
let handler: ((HttpRequest) -> HttpResponse) = { [weak self] request in
|
|
63
|
-
if request.method == "OPTIONS" {
|
|
64
|
-
|
|
75
|
+
if request.method == "OPTIONS" {
|
|
76
|
+
return self?.corsResponse() ?? .raw(204, "No Content", nil, nil)
|
|
77
|
+
}
|
|
78
|
+
return self?.processRequest(request) ?? .raw(500, "Internal Server Error", nil, nil)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Swifter needs a wildcard to catch everything including subpaths like /orders/123
|
|
82
|
+
server.middleware.append { request in
|
|
83
|
+
return handler(request)
|
|
65
84
|
}
|
|
66
|
-
server["/"] = handler
|
|
67
|
-
server["/:path"] = handler
|
|
68
85
|
}
|
|
69
86
|
|
|
70
87
|
private func processRequest(_ request: HttpRequest) -> HttpResponse {
|
|
@@ -91,10 +108,13 @@ public protocol HttpLocalServerSwifterDelegate: AnyObject {
|
|
|
91
108
|
|
|
92
109
|
DispatchQueue.main.async { self.delegate?.httpLocalServerSwifterDidReceiveRequest(requestData) }
|
|
93
110
|
|
|
94
|
-
|
|
95
|
-
|
|
111
|
+
// Inside HttpLocalServerSwifter.swift -> processRequest
|
|
112
|
+
let result = semaphore.wait(timeout: .now() + 5.0) // Lower timeout to 5 seconds for testing
|
|
113
|
+
|
|
96
114
|
if result == .timedOut {
|
|
97
|
-
|
|
115
|
+
print("⚠️ Request \(requestId) timed out waiting for JS")
|
|
116
|
+
Self.queue.async { Self.pendingResponses.removeValue(forKey: requestId) }
|
|
117
|
+
return .raw(408, "Request Timeout", nil, nil)
|
|
98
118
|
}
|
|
99
119
|
|
|
100
120
|
return createDynamicResponse(responseString ?? "{\"error\":\"no_response\"}")
|
|
@@ -159,4 +179,4 @@ public protocol HttpLocalServerSwifterDelegate: AnyObject {
|
|
|
159
179
|
}
|
|
160
180
|
return address
|
|
161
181
|
}
|
|
162
|
-
}
|
|
182
|
+
}
|
|
@@ -23,18 +23,22 @@ public class HttpLocalServerSwifterPlugin: CAPPlugin, CAPBridgedPlugin, HttpLoca
|
|
|
23
23
|
localServer = nil
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
@objc func sendResponse(_ call: CAPPluginCall) {
|
|
27
|
+
guard let requestId = call.getString("requestId") else {
|
|
28
|
+
call.reject("Missing requestId")
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Cast dictionaryRepresentation explicitly to [String: Any]
|
|
33
|
+
if let responseData = call.dictionaryRepresentation as? [String: Any] {
|
|
34
|
+
HttpLocalServerSwifter.handleJsResponse(requestId: requestId, responseData: responseData)
|
|
35
|
+
call.resolve()
|
|
36
|
+
} else {
|
|
37
|
+
call.reject("Could not parse response data")
|
|
38
|
+
}
|
|
39
|
+
}
|
|
36
40
|
|
|
37
41
|
public func httpLocalServerSwifterDidReceiveRequest(_ data: [String: Any]) {
|
|
38
42
|
notifyListeners("onRequest", data: data)
|
|
39
43
|
}
|
|
40
|
-
}
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cappitolian/http-local-server-swifter",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"description": "Runs a local HTTP server on your device, accessible over LAN. Supports connect, disconnect, GET, and POST methods with IP and port discovery.",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|