@blotch/angular-lynx-native 22.2.21 → 22.2.23
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 +4 -0
- package/dist/android/build.d.ts +6 -1
- package/dist/android/build.d.ts.map +1 -1
- package/dist/android/build.js +14 -3
- package/dist/android/run.d.ts +10 -1
- package/dist/android/run.d.ts.map +1 -1
- package/dist/android/run.js +46 -1
- package/dist/dev-build.d.ts +16 -0
- package/dist/dev-build.d.ts.map +1 -0
- package/dist/dev-build.js +44 -0
- package/dist/generate.d.ts +24 -5
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +14 -6
- package/dist/index.d.ts +12 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +65 -9
- package/dist/ios/build.d.ts +14 -1
- package/dist/ios/build.d.ts.map +1 -1
- package/dist/ios/build.js +15 -6
- package/dist/ios/iphone.d.ts +1 -1
- package/dist/ios/iphone.d.ts.map +1 -1
- package/dist/ios/iphone.js +21 -4
- package/dist/ios/project.d.ts +8 -1
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +17 -3
- package/dist/ios/run.d.ts +1 -1
- package/dist/ios/run.d.ts.map +1 -1
- package/dist/ios/run.js +4 -1
- package/dist/run.d.ts +42 -2
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +16 -1
- package/package.json +1 -1
- package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServer.kt +159 -0
- package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServerModules.kt +163 -0
- package/templates/android/app/src/debug/res/xml/network_security_config.xml +13 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +22 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +8 -1
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ResourceFetchers.kt +12 -6
- package/templates/android/app/src/release/java/dev/angularlynx/shell/DevServer.kt +24 -0
- package/templates/ios/App/AssetsModule.swift +21 -3
- package/templates/ios/App/BundleResourceFetcher.swift +46 -2
- package/templates/ios/App/DevServer.swift +190 -0
- package/templates/ios/App/DevServerModules.swift +223 -0
- package/templates/ios/App/LynxViewController.swift +9 -1
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// The two native modules rspeedy's live reload needs, in the development
|
|
2
|
+
// build only (see DevServer.swift). Lynx Explorer has them from Lynx DevTool,
|
|
3
|
+
// which the shell doesn't link: the dev server's client, which rspeedy puts
|
|
4
|
+
// in every development bundle, talks to the server over
|
|
5
|
+
// `NativeModules.LynxWebSocketModule` (through @lynx-js/websocket) and reloads
|
|
6
|
+
// the page with `NativeModules.LynxDevToolSetModule.invokeCdp`, a DevTool
|
|
7
|
+
// protocol `Page.reload`. These answer to the same names and calls, so the
|
|
8
|
+
// bundle runs here as it does in Lynx Explorer.
|
|
9
|
+
#if DEBUG
|
|
10
|
+
import Foundation
|
|
11
|
+
import Lynx
|
|
12
|
+
|
|
13
|
+
/// `NativeModules.LynxWebSocketModule`, as Lynx DevTool's
|
|
14
|
+
/// (LynxWebSocketModule.mm): `connect`, `send`, `ping` and `close`, each
|
|
15
|
+
/// with the socket's ID, and the `websocketOpen`, `websocketMessage`,
|
|
16
|
+
/// `websocketClosed` and `websocketFailed` global events @lynx-js/websocket
|
|
17
|
+
/// listens for.
|
|
18
|
+
final class DevWebSocketModule: NSObject, LynxContextModule, URLSessionWebSocketDelegate {
|
|
19
|
+
static var name: String { "LynxWebSocketModule" }
|
|
20
|
+
|
|
21
|
+
static var methodLookup: [String: String] {
|
|
22
|
+
[
|
|
23
|
+
"connect": NSStringFromSelector(#selector(connect(_:protocols:options:socketID:))),
|
|
24
|
+
"send": NSStringFromSelector(#selector(send(_:socketID:))),
|
|
25
|
+
"ping": NSStringFromSelector(#selector(ping(_:))),
|
|
26
|
+
"close": NSStringFromSelector(#selector(close(_:reason:socketID:))),
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private weak var context: LynxContext?
|
|
31
|
+
private var sockets: [Int: URLSessionWebSocketTask] = [:]
|
|
32
|
+
private let lock = NSLock()
|
|
33
|
+
private lazy var session = URLSession(
|
|
34
|
+
configuration: .default, delegate: self, delegateQueue: nil)
|
|
35
|
+
|
|
36
|
+
override init() {
|
|
37
|
+
super.init()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Swift can't import an optional initializer as optional, so Lynx's
|
|
41
|
+
// initWithParam: and initWithLynxContext:WithParam: are required here.
|
|
42
|
+
init(param: Any) {
|
|
43
|
+
super.init()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
init(lynxContext context: LynxContext) {
|
|
47
|
+
self.context = context
|
|
48
|
+
super.init()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
init(lynxContext context: LynxContext, withParam param: Any) {
|
|
52
|
+
self.context = context
|
|
53
|
+
super.init()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
func destroy() {
|
|
57
|
+
lock.lock()
|
|
58
|
+
let open = Array(sockets.values)
|
|
59
|
+
sockets.removeAll()
|
|
60
|
+
lock.unlock()
|
|
61
|
+
open.forEach { $0.cancel(with: .goingAway, reason: nil) }
|
|
62
|
+
session.invalidateAndCancel()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@objc func connect(
|
|
66
|
+
_ url: String, protocols: [String]?, options: [String: Any]?, socketID: Double
|
|
67
|
+
) {
|
|
68
|
+
let id = Int(socketID)
|
|
69
|
+
// The address the bundle was built with may not be the one this
|
|
70
|
+
// device reaches the server at.
|
|
71
|
+
let address = DevServer.shared.remoteURL(for: url.replacingOccurrences(
|
|
72
|
+
of: "^ws", with: "http", options: .regularExpression))
|
|
73
|
+
.map { $0.absoluteString.replacingOccurrences(
|
|
74
|
+
of: "^http", with: "ws", options: .regularExpression) } ?? url
|
|
75
|
+
guard let target = URL(string: address) else {
|
|
76
|
+
send("websocketFailed", ["id": id, "message": "\(url) is not a valid URL."])
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
// With an empty list, URLSession still sends an empty
|
|
80
|
+
// Sec-WebSocket-Protocol header, which the dev server rejects.
|
|
81
|
+
let names = (protocols ?? []).filter { !$0.isEmpty }
|
|
82
|
+
let task =
|
|
83
|
+
names.isEmpty
|
|
84
|
+
? session.webSocketTask(with: target)
|
|
85
|
+
: session.webSocketTask(with: target, protocols: names)
|
|
86
|
+
task.taskDescription = String(id)
|
|
87
|
+
lock.lock()
|
|
88
|
+
sockets[id] = task
|
|
89
|
+
lock.unlock()
|
|
90
|
+
task.resume()
|
|
91
|
+
receive(task, id: id)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@objc func send(_ message: String, socketID: Double) {
|
|
95
|
+
socket(socketID)?.send(.string(message)) { [weak self] error in
|
|
96
|
+
if let error {
|
|
97
|
+
self?.send("websocketFailed", ["id": Int(socketID), "message": error.localizedDescription])
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@objc func ping(_ socketID: Double) {
|
|
103
|
+
socket(socketID)?.sendPing { _ in }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
@objc func close(_ code: Double, reason: String, socketID: Double) {
|
|
107
|
+
let id = Int(socketID)
|
|
108
|
+
lock.lock()
|
|
109
|
+
let task = sockets.removeValue(forKey: id)
|
|
110
|
+
lock.unlock()
|
|
111
|
+
task?.cancel(
|
|
112
|
+
with: URLSessionWebSocketTask.CloseCode(rawValue: Int(code)) ?? .normalClosure,
|
|
113
|
+
reason: reason.data(using: .utf8))
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private func socket(_ socketID: Double) -> URLSessionWebSocketTask? {
|
|
117
|
+
lock.lock()
|
|
118
|
+
defer { lock.unlock() }
|
|
119
|
+
return sockets[Int(socketID)]
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// Reads one message, sends it on, and waits for the next, for as long as
|
|
123
|
+
/// the socket is open.
|
|
124
|
+
private func receive(_ task: URLSessionWebSocketTask, id: Int) {
|
|
125
|
+
task.receive { [weak self] result in
|
|
126
|
+
guard let self else { return }
|
|
127
|
+
switch result {
|
|
128
|
+
case .success(.string(let text)):
|
|
129
|
+
self.send("websocketMessage", ["id": id, "type": "text", "data": text])
|
|
130
|
+
case .success(.data(let data)):
|
|
131
|
+
self.send(
|
|
132
|
+
"websocketMessage", ["id": id, "type": "binary", "data": data.base64EncodedString()])
|
|
133
|
+
case .success:
|
|
134
|
+
break
|
|
135
|
+
case .failure:
|
|
136
|
+
// The delegate reports why the socket ended.
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
self.receive(task, id: id)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private func send(_ event: String, _ params: [String: Any]) {
|
|
144
|
+
context?.sendGlobalEvent(event, withParams: [params])
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
func urlSession(
|
|
148
|
+
_ session: URLSession, webSocketTask: URLSessionWebSocketTask,
|
|
149
|
+
didOpenWithProtocol protocolName: String?
|
|
150
|
+
) {
|
|
151
|
+
guard let id = webSocketTask.taskDescription.flatMap(Int.init) else { return }
|
|
152
|
+
send("websocketOpen", ["id": id, "protocol": protocolName ?? ""])
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
func urlSession(
|
|
156
|
+
_ session: URLSession, webSocketTask: URLSessionWebSocketTask,
|
|
157
|
+
didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?
|
|
158
|
+
) {
|
|
159
|
+
guard let id = webSocketTask.taskDescription.flatMap(Int.init) else { return }
|
|
160
|
+
lock.lock()
|
|
161
|
+
sockets.removeValue(forKey: id)
|
|
162
|
+
lock.unlock()
|
|
163
|
+
send(
|
|
164
|
+
"websocketClosed",
|
|
165
|
+
[
|
|
166
|
+
"id": id, "code": closeCode.rawValue,
|
|
167
|
+
"reason": reason.flatMap { String(data: $0, encoding: .utf8) } ?? "",
|
|
168
|
+
])
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/// The connection failed or dropped (the server stopped): the dev
|
|
172
|
+
/// server's client then tries again, with a growing delay.
|
|
173
|
+
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
|
|
174
|
+
{
|
|
175
|
+
guard let error, let id = task.taskDescription.flatMap(Int.init) else { return }
|
|
176
|
+
lock.lock()
|
|
177
|
+
let known = sockets.removeValue(forKey: id) != nil
|
|
178
|
+
lock.unlock()
|
|
179
|
+
guard known else { return }
|
|
180
|
+
send("websocketFailed", ["id": id, "message": error.localizedDescription])
|
|
181
|
+
send("websocketClosed", ["id": id, "code": 1006, "reason": error.localizedDescription])
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/// `NativeModules.LynxDevToolSetModule`, as far as rspeedy's live reload
|
|
186
|
+
/// uses Lynx DevTool's: `invokeCdp` with a `Page.reload`, which loads the
|
|
187
|
+
/// bundle from the dev server again, as Lynx DevTool's LynxPageReloadHelper
|
|
188
|
+
/// does. It answers any other DevTool method with an error.
|
|
189
|
+
final class DevReloadModule: NSObject, LynxModule {
|
|
190
|
+
static var name: String { "LynxDevToolSetModule" }
|
|
191
|
+
|
|
192
|
+
static var methodLookup: [String: String] {
|
|
193
|
+
["invokeCdp": NSStringFromSelector(#selector(invokeCdp(_:callback:)))]
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
override init() {
|
|
197
|
+
super.init()
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
init(param: Any) {
|
|
201
|
+
super.init()
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
@objc func invokeCdp(_ message: String, callback: LynxCallbackBlock?) {
|
|
205
|
+
let request =
|
|
206
|
+
(try? JSONSerialization.jsonObject(with: Data(message.utf8))) as? [String: Any] ?? [:]
|
|
207
|
+
let id = request["id"] ?? 0
|
|
208
|
+
let method = request["method"] as? String ?? ""
|
|
209
|
+
let reply: [String: Any]
|
|
210
|
+
if method == "Page.reload" {
|
|
211
|
+
DevServer.shared.reloadBundle()
|
|
212
|
+
reply = ["id": id, "result": [String: Any]()]
|
|
213
|
+
} else {
|
|
214
|
+
reply = [
|
|
215
|
+
"id": id,
|
|
216
|
+
"error": ["code": -32601, "message": "\(method) needs Lynx DevTool, which this app doesn't have."],
|
|
217
|
+
]
|
|
218
|
+
}
|
|
219
|
+
let data = (try? JSONSerialization.data(withJSONObject: reply)) ?? Data()
|
|
220
|
+
callback?(String(data: data, encoding: .utf8) ?? "{}")
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
#endif
|
|
@@ -40,6 +40,8 @@ final class LynxViewController: UIViewController {
|
|
|
40
40
|
}
|
|
41
41
|
// Bundled files for the main thread's fetch and Image (AssetsModule).
|
|
42
42
|
config.register(AssetsModule.self)
|
|
43
|
+
// `ng serve --platform ios`'s live reload, in the development build.
|
|
44
|
+
DevServer.shared.register(in: config)
|
|
43
45
|
let lynxView = LynxView { builder in
|
|
44
46
|
builder.config = config
|
|
45
47
|
// Lets main-thread code, where Angular runs, call the config's modules
|
|
@@ -59,7 +61,13 @@ final class LynxViewController: UIViewController {
|
|
|
59
61
|
|
|
60
62
|
layoutLynxView()
|
|
61
63
|
lynxView.updateGlobalProps(with: globalProps())
|
|
62
|
-
|
|
64
|
+
// The bundle inside the app, or in the development build, the dev
|
|
65
|
+
// server's, which it loads again on every edit.
|
|
66
|
+
let templateURL = DevServer.shared.templateURL
|
|
67
|
+
lynxView.loadTemplate(fromURL: templateURL, initData: nil)
|
|
68
|
+
DevServer.shared.attach(to: view) { [weak self] in
|
|
69
|
+
self?.lynxView?.loadTemplate(fromURL: templateURL, initData: nil)
|
|
70
|
+
}
|
|
63
71
|
|
|
64
72
|
// Send the new theme when the phone switches between light and dark
|
|
65
73
|
// while the app is open. Before iOS 17, traitCollectionDidChange does.
|