@capgo/capacitor-updater 9.0.1 → 9.0.4
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 +106 -81
- package/android/build.gradle +1 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +521 -21
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +675 -34
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +10 -2
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +82 -10
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +23 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +188 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +81 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +72 -21
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +5 -2
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +5 -1
- package/dist/docs.json +13 -13
- package/dist/esm/definitions.d.ts +57 -23
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +1278 -107
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +38 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +583 -56
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +8 -1
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +6 -1
- package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +2718 -77
- package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +9 -4
- package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +67 -2
- package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +130 -5
- package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +259 -119
- package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +4 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +19 -3
- package/package.json +2 -2
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import Foundation
|
|
2
|
-
import Network
|
|
3
2
|
import XCTest
|
|
4
3
|
@testable import CapacitorUpdaterPlugin
|
|
5
4
|
|
|
6
5
|
/// Minimal loopback HTTP/1.1 server so the real URLSession transport can be exercised without the network.
|
|
6
|
+
///
|
|
7
|
+
/// It listens on a BSD socket that is bound and listening before `init` returns, so its port is known and the
|
|
8
|
+
/// kernel completes connections into the backlog even while this process is starved of CPU. An NWListener only
|
|
9
|
+
/// reports `.ready` asynchronously, and on loaded CI runners it was not ready within 5s, so a test could send its
|
|
10
|
+
/// request to port 0. `stop()` closes the socket synchronously, so later connections are refused at once.
|
|
7
11
|
private final class LoopbackHTTPServer {
|
|
8
12
|
struct Response {
|
|
9
13
|
var status: Int
|
|
@@ -28,32 +32,56 @@ private final class LoopbackHTTPServer {
|
|
|
28
32
|
var body: Data
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
enum SetupError: Error {
|
|
36
|
+
case socket(String, Int32)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private let listenSocket: Int32
|
|
40
|
+
private let port: UInt16
|
|
41
|
+
private let acceptQueue = DispatchQueue(label: "capgo.loopback-http.accept")
|
|
42
|
+
private let acceptSource: DispatchSourceRead
|
|
33
43
|
private let lock = NSLock()
|
|
34
44
|
private var handler: (RecordedRequest) -> Response = { _ in Response(status: 200, body: Data()) }
|
|
35
45
|
private var recorded: [RecordedRequest] = []
|
|
36
46
|
private var activeConnections = 0
|
|
47
|
+
private var stopped = false
|
|
48
|
+
private let listenerClosed = DispatchSemaphore(value: 0)
|
|
37
49
|
|
|
38
50
|
init() throws {
|
|
39
|
-
let
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
let socketFD = socket(AF_INET, SOCK_STREAM, 0)
|
|
52
|
+
guard socketFD >= 0 else {
|
|
53
|
+
throw SetupError.socket("socket", errno)
|
|
54
|
+
}
|
|
55
|
+
var reuse: Int32 = 1
|
|
56
|
+
setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &reuse, socklen_t(MemoryLayout<Int32>.size))
|
|
57
|
+
do {
|
|
58
|
+
port = try Self.bindLoopback(socketFD)
|
|
59
|
+
guard listen(socketFD, 64) == 0 else {
|
|
60
|
+
throw SetupError.socket("listen", errno)
|
|
49
61
|
}
|
|
62
|
+
try Self.setNonBlocking(socketFD)
|
|
63
|
+
} catch {
|
|
64
|
+
close(socketFD)
|
|
65
|
+
throw error
|
|
66
|
+
}
|
|
67
|
+
listenSocket = socketFD
|
|
68
|
+
acceptSource = DispatchSource.makeReadSource(fileDescriptor: socketFD, queue: acceptQueue)
|
|
69
|
+
acceptSource.setEventHandler { [weak self] in
|
|
70
|
+
self?.acceptPendingConnections()
|
|
71
|
+
}
|
|
72
|
+
acceptSource.setCancelHandler { [listenerClosed] in
|
|
73
|
+
close(socketFD)
|
|
74
|
+
listenerClosed.signal()
|
|
50
75
|
}
|
|
51
|
-
|
|
52
|
-
|
|
76
|
+
acceptSource.resume()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
deinit {
|
|
80
|
+
stop()
|
|
53
81
|
}
|
|
54
82
|
|
|
55
83
|
var baseURL: URL {
|
|
56
|
-
return URL(string: "http://127.0.0.1:\(
|
|
84
|
+
return URL(string: "http://127.0.0.1:\(port)") ?? URL(fileURLWithPath: "/")
|
|
57
85
|
}
|
|
58
86
|
|
|
59
87
|
var requests: [RecordedRequest] {
|
|
@@ -68,8 +96,20 @@ private final class LoopbackHTTPServer {
|
|
|
68
96
|
lock.unlock()
|
|
69
97
|
}
|
|
70
98
|
|
|
99
|
+
/// Stops accepting connections. The listening socket is closed before this returns, so a new connection to
|
|
100
|
+
/// the old port is refused instead of reaching a listener that is still shutting down.
|
|
71
101
|
func stop() {
|
|
72
|
-
|
|
102
|
+
lock.lock()
|
|
103
|
+
let wasStopped = stopped
|
|
104
|
+
stopped = true
|
|
105
|
+
lock.unlock()
|
|
106
|
+
guard !wasStopped else {
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
// The accept source's cancel handler closes the listening socket; wait for it so the port refuses
|
|
110
|
+
// new connections once stop() returns.
|
|
111
|
+
acceptSource.cancel()
|
|
112
|
+
listenerClosed.wait()
|
|
73
113
|
}
|
|
74
114
|
|
|
75
115
|
/// Waits until accepted connections finish or `timeout` elapses (stray URLSession tasks after invalidate).
|
|
@@ -86,55 +126,77 @@ private final class LoopbackHTTPServer {
|
|
|
86
126
|
}
|
|
87
127
|
}
|
|
88
128
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
129
|
+
/// A loopback port with nothing listening on it: bind a socket to an ephemeral port, then close it.
|
|
130
|
+
/// Connecting to it is refused at once.
|
|
131
|
+
static func closedLoopbackPort() throws -> UInt16 {
|
|
132
|
+
let socketFD = socket(AF_INET, SOCK_STREAM, 0)
|
|
133
|
+
guard socketFD >= 0 else {
|
|
134
|
+
throw SetupError.socket("socket", errno)
|
|
135
|
+
}
|
|
136
|
+
defer { close(socketFD) }
|
|
137
|
+
return try bindLoopback(socketFD)
|
|
93
138
|
}
|
|
94
139
|
|
|
95
|
-
private func
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
140
|
+
private static func bindLoopback(_ socketFD: Int32) throws -> UInt16 {
|
|
141
|
+
var address = sockaddr_in()
|
|
142
|
+
address.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
|
|
143
|
+
address.sin_family = sa_family_t(AF_INET)
|
|
144
|
+
address.sin_addr.s_addr = inet_addr("127.0.0.1")
|
|
145
|
+
address.sin_port = 0
|
|
146
|
+
var length = socklen_t(MemoryLayout<sockaddr_in>.size)
|
|
147
|
+
let bound = withUnsafeMutablePointer(to: &address) { pointer in
|
|
148
|
+
pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { socketAddress in
|
|
149
|
+
bind(socketFD, socketAddress, length) == 0 && getsockname(socketFD, socketAddress, &length) == 0
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
guard bound else {
|
|
153
|
+
throw SetupError.socket("bind", errno)
|
|
154
|
+
}
|
|
155
|
+
return UInt16(bigEndian: address.sin_port)
|
|
99
156
|
}
|
|
100
157
|
|
|
101
|
-
private func
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
self?.connectionClosed()
|
|
106
|
-
}
|
|
158
|
+
private static func setNonBlocking(_ socketFD: Int32) throws {
|
|
159
|
+
let flags = fcntl(socketFD, F_GETFL)
|
|
160
|
+
guard flags >= 0, fcntl(socketFD, F_SETFL, flags | O_NONBLOCK) == 0 else {
|
|
161
|
+
throw SetupError.socket("fcntl", errno)
|
|
107
162
|
}
|
|
108
|
-
connection.start(queue: queue)
|
|
109
|
-
receive(on: connection, buffer: Data())
|
|
110
163
|
}
|
|
111
164
|
|
|
112
|
-
private func
|
|
113
|
-
|
|
114
|
-
|
|
165
|
+
private func acceptPendingConnections() {
|
|
166
|
+
while true {
|
|
167
|
+
let connectionFD = accept(listenSocket, nil, nil)
|
|
168
|
+
guard connectionFD >= 0 else {
|
|
115
169
|
return
|
|
116
170
|
}
|
|
117
|
-
var
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if
|
|
122
|
-
|
|
123
|
-
self.recorded.append(request)
|
|
124
|
-
let handler = self.handler
|
|
125
|
-
self.lock.unlock()
|
|
126
|
-
self.send(handler(request), on: connection)
|
|
127
|
-
return
|
|
128
|
-
}
|
|
129
|
-
if isComplete || error != nil {
|
|
130
|
-
connection.cancel()
|
|
131
|
-
return
|
|
171
|
+
var noSigPipe: Int32 = 1
|
|
172
|
+
setsockopt(connectionFD, SOL_SOCKET, SO_NOSIGPIPE, &noSigPipe, socklen_t(MemoryLayout<Int32>.size))
|
|
173
|
+
// Accepted sockets inherit O_NONBLOCK from the listener on Darwin; the connection writes block.
|
|
174
|
+
let flags = fcntl(connectionFD, F_GETFL)
|
|
175
|
+
if flags >= 0 {
|
|
176
|
+
_ = fcntl(connectionFD, F_SETFL, flags & ~O_NONBLOCK)
|
|
132
177
|
}
|
|
133
|
-
|
|
178
|
+
lock.lock()
|
|
179
|
+
activeConnections += 1
|
|
180
|
+
lock.unlock()
|
|
181
|
+
LoopbackConnection(socketFD: connectionFD, server: self).start()
|
|
134
182
|
}
|
|
135
183
|
}
|
|
136
184
|
|
|
137
|
-
|
|
185
|
+
fileprivate func connectionClosed() {
|
|
186
|
+
lock.lock()
|
|
187
|
+
activeConnections = max(0, activeConnections - 1)
|
|
188
|
+
lock.unlock()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
fileprivate func handle(_ request: RecordedRequest) -> Response {
|
|
192
|
+
lock.lock()
|
|
193
|
+
recorded.append(request)
|
|
194
|
+
let handler = self.handler
|
|
195
|
+
lock.unlock()
|
|
196
|
+
return handler(request)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
fileprivate static func parse(_ buffer: Data) -> RecordedRequest? {
|
|
138
200
|
guard let headerEnd = buffer.range(of: Data("\r\n\r\n".utf8)),
|
|
139
201
|
let head = String(data: buffer[buffer.startIndex..<headerEnd.lowerBound], encoding: .utf8) else {
|
|
140
202
|
return nil
|
|
@@ -161,14 +223,60 @@ private final class LoopbackHTTPServer {
|
|
|
161
223
|
body: Data(body.prefix(contentLength))
|
|
162
224
|
)
|
|
163
225
|
}
|
|
226
|
+
}
|
|
164
227
|
|
|
165
|
-
|
|
166
|
-
|
|
228
|
+
/// One accepted connection: reads a request, then writes the configured response on its own serial queue,
|
|
229
|
+
/// so a slow or paused response never delays another connection.
|
|
230
|
+
private final class LoopbackConnection {
|
|
231
|
+
private let socketFD: Int32
|
|
232
|
+
private let server: LoopbackHTTPServer
|
|
233
|
+
private let queue = DispatchQueue(label: "capgo.loopback-http.connection")
|
|
234
|
+
private var readSource: DispatchSourceRead?
|
|
235
|
+
private var buffer = Data()
|
|
236
|
+
private var answered = false
|
|
237
|
+
private var closed = false
|
|
238
|
+
|
|
239
|
+
init(socketFD: Int32, server: LoopbackHTTPServer) {
|
|
240
|
+
self.socketFD = socketFD
|
|
241
|
+
self.server = server
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
func start() {
|
|
245
|
+
let source = DispatchSource.makeReadSource(fileDescriptor: socketFD, queue: queue)
|
|
246
|
+
source.setEventHandler { [self] in
|
|
247
|
+
readAvailable()
|
|
248
|
+
}
|
|
249
|
+
readSource = source
|
|
250
|
+
source.resume()
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private func readAvailable() {
|
|
254
|
+
var chunk = [UInt8](repeating: 0, count: 65536)
|
|
255
|
+
let count = read(socketFD, &chunk, chunk.count)
|
|
256
|
+
if count <= 0 {
|
|
257
|
+
// The peer closed the connection (or it failed); nothing more can be sent.
|
|
258
|
+
finish()
|
|
259
|
+
return
|
|
260
|
+
}
|
|
261
|
+
// One request per connection: bytes after it are ignored.
|
|
262
|
+
guard !answered else {
|
|
263
|
+
return
|
|
264
|
+
}
|
|
265
|
+
buffer.append(contentsOf: chunk[0..<count])
|
|
266
|
+
guard let request = LoopbackHTTPServer.parse(buffer) else {
|
|
267
|
+
return
|
|
268
|
+
}
|
|
269
|
+
answered = true
|
|
270
|
+
send(server.handle(request))
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private func send(_ response: LoopbackHTTPServer.Response) {
|
|
167
274
|
if response.dropConnection {
|
|
168
|
-
|
|
275
|
+
finish()
|
|
169
276
|
return
|
|
170
277
|
}
|
|
171
|
-
var head = "HTTP/1.1 \(response.status) Status\r\
|
|
278
|
+
var head = "HTTP/1.1 \(response.status) Status\r\n"
|
|
279
|
+
head += "Content-Length: \(response.body.count)\r\nConnection: close\r\n"
|
|
172
280
|
for (name, value) in response.headers {
|
|
173
281
|
head += "\(name): \(value)\r\n"
|
|
174
282
|
}
|
|
@@ -176,73 +284,91 @@ private final class LoopbackHTTPServer {
|
|
|
176
284
|
let bodyData = response.truncateBodyAt.map { Data(response.body.prefix($0)) } ?? response.body
|
|
177
285
|
|
|
178
286
|
if let chunkSize = response.chunkSize, chunkSize > 0, bodyData.count > chunkSize {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
)
|
|
185
|
-
})
|
|
287
|
+
guard write(Data(head.utf8)) else {
|
|
288
|
+
finish()
|
|
289
|
+
return
|
|
290
|
+
}
|
|
291
|
+
sendChunks(of: bodyData, offset: 0, chunkSize: chunkSize, response: response)
|
|
186
292
|
return
|
|
187
293
|
}
|
|
188
|
-
|
|
189
294
|
if response.truncateBodyAt != nil {
|
|
190
|
-
//
|
|
295
|
+
// Headers and body go out in separate writes so a truncated body is a real mid-stream drop,
|
|
191
296
|
// not one atomic read containing a short body with Content-Length still outstanding.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
return
|
|
197
|
-
}
|
|
198
|
-
connection.send(content: bodyData, isComplete: true, completion: .contentProcessed { _ in
|
|
199
|
-
connection.cancel()
|
|
200
|
-
})
|
|
201
|
-
})
|
|
297
|
+
if write(Data(head.utf8)) {
|
|
298
|
+
_ = write(bodyData)
|
|
299
|
+
}
|
|
300
|
+
finish()
|
|
202
301
|
return
|
|
203
302
|
}
|
|
204
|
-
|
|
205
303
|
var payload = Data(head.utf8)
|
|
206
304
|
payload.append(bodyData)
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
})
|
|
305
|
+
_ = write(payload)
|
|
306
|
+
finish()
|
|
210
307
|
}
|
|
211
308
|
|
|
212
|
-
///
|
|
213
|
-
private func sendChunks(
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
guard offset < body.count else {
|
|
218
|
-
connection.cancel()
|
|
309
|
+
/// Writes slices of `body` separated by the response's chunk delay, then closes the connection.
|
|
310
|
+
private func sendChunks(of body: Data, offset: Int, chunkSize: Int, response: LoopbackHTTPServer.Response) {
|
|
311
|
+
let end = min(offset + chunkSize, body.count)
|
|
312
|
+
guard write(body.subdata(in: offset..<end)), end < body.count else {
|
|
313
|
+
finish()
|
|
219
314
|
return
|
|
220
315
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
316
|
+
queue.asyncAfter(deadline: .now() + response.chunkDelay) { [self] in
|
|
317
|
+
if let wait = response.waitBeforeContinuingBody {
|
|
318
|
+
let deadline = Date().addingTimeInterval(response.waitBeforeContinuingBodyTimeout)
|
|
319
|
+
while !wait() && Date() < deadline {
|
|
320
|
+
Thread.sleep(forTimeInterval: 0.005)
|
|
321
|
+
}
|
|
227
322
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
323
|
+
sendChunks(of: body, offset: end, chunkSize: chunkSize, response: response)
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/// Blocking write of all of `data`; false once the peer has gone away.
|
|
328
|
+
private func write(_ data: Data) -> Bool {
|
|
329
|
+
guard !closed else {
|
|
330
|
+
return false
|
|
331
|
+
}
|
|
332
|
+
return data.withUnsafeBytes { raw -> Bool in
|
|
333
|
+
guard var pointer = raw.baseAddress else {
|
|
334
|
+
return true
|
|
335
|
+
}
|
|
336
|
+
var remaining = raw.count
|
|
337
|
+
while remaining > 0 {
|
|
338
|
+
let written = Darwin.write(socketFD, pointer, remaining)
|
|
339
|
+
if written < 0 {
|
|
340
|
+
if errno == EINTR {
|
|
341
|
+
continue
|
|
235
342
|
}
|
|
236
|
-
|
|
237
|
-
of: body, offset: end, chunkSize: chunkSize, delay: delay,
|
|
238
|
-
waitBeforeContinuingBody: waitBeforeContinuingBody, waitTimeout: waitTimeout,
|
|
239
|
-
on: connection
|
|
240
|
-
)
|
|
343
|
+
return false
|
|
241
344
|
}
|
|
242
|
-
|
|
243
|
-
|
|
345
|
+
remaining -= written
|
|
346
|
+
pointer += written
|
|
244
347
|
}
|
|
245
|
-
|
|
348
|
+
return true
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private func finish() {
|
|
353
|
+
guard !closed else {
|
|
354
|
+
return
|
|
355
|
+
}
|
|
356
|
+
closed = true
|
|
357
|
+
// Close the socket from the read source's cancel handler, so the source is gone before the descriptor
|
|
358
|
+
// number can be reused by another connection.
|
|
359
|
+
let socketFD = self.socketFD
|
|
360
|
+
let server = self.server
|
|
361
|
+
guard let source = readSource else {
|
|
362
|
+
close(socketFD)
|
|
363
|
+
server.connectionClosed()
|
|
364
|
+
return
|
|
365
|
+
}
|
|
366
|
+
readSource = nil
|
|
367
|
+
source.setCancelHandler {
|
|
368
|
+
close(socketFD)
|
|
369
|
+
server.connectionClosed()
|
|
370
|
+
}
|
|
371
|
+
source.cancel()
|
|
246
372
|
}
|
|
247
373
|
}
|
|
248
374
|
|
|
@@ -793,9 +919,10 @@ final class NetworkTransportTests: XCTestCase {
|
|
|
793
919
|
}
|
|
794
920
|
|
|
795
921
|
func testPerformRequestWrapsTransportErrors() throws {
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
922
|
+
// A port that was never listening refuses the connection at once. Reusing the test server's port after
|
|
923
|
+
// stop() could still reach the listener while it shuts down, and that connection never gets a reply.
|
|
924
|
+
let port = try LoopbackHTTPServer.closedLoopbackPort()
|
|
925
|
+
var request = URLRequest(url: try XCTUnwrap(URL(string: "http://127.0.0.1:\(port)/closed")))
|
|
799
926
|
request.timeoutInterval = 5
|
|
800
927
|
|
|
801
928
|
let result = makeUpdater().performRequest(request, label: "test")
|
|
@@ -808,16 +935,16 @@ final class NetworkTransportTests: XCTestCase {
|
|
|
808
935
|
|
|
809
936
|
func testPerformRequestClassifiesURLSessionTimeouts() {
|
|
810
937
|
server.respond { _ in
|
|
811
|
-
Thread.sleep(forTimeInterval:
|
|
938
|
+
Thread.sleep(forTimeInterval: 2)
|
|
812
939
|
return .init(status: 200, body: Data("late".utf8))
|
|
813
940
|
}
|
|
814
941
|
var slowRequest = request("slow")
|
|
815
|
-
slowRequest.timeoutInterval =
|
|
942
|
+
slowRequest.timeoutInterval = 0.2
|
|
816
943
|
|
|
817
944
|
let result = makeUpdater().performRequest(slowRequest, label: "test")
|
|
818
945
|
|
|
819
946
|
XCTAssertTrue(result.timedOut, "a wrapped NSURLErrorTimedOut must be reported as a timeout")
|
|
820
|
-
XCTAssertNotNil(result.error)
|
|
947
|
+
XCTAssertNotNil(result.error, "a timed-out request must still carry its error")
|
|
821
948
|
}
|
|
822
949
|
|
|
823
950
|
func testPerformDownloadRequestMovesBodyToTemporaryFile() throws {
|
|
@@ -874,7 +1001,11 @@ final class NetworkTransportTests: XCTestCase {
|
|
|
874
1001
|
|
|
875
1002
|
let lock = NSLock()
|
|
876
1003
|
var samples: [(Int64, Int64)] = []
|
|
877
|
-
|
|
1004
|
+
var errorRequest = request("missing.zip")
|
|
1005
|
+
errorRequest.timeoutInterval = 30
|
|
1006
|
+
let updater = makeUpdater()
|
|
1007
|
+
updater.timeout = 30
|
|
1008
|
+
let result = updater.performDownloadRequest(errorRequest, label: "test") { received, expected in
|
|
878
1009
|
lock.lock()
|
|
879
1010
|
samples.append((received, expected))
|
|
880
1011
|
lock.unlock()
|
|
@@ -958,7 +1089,8 @@ final class NetworkTransportTests: XCTestCase {
|
|
|
958
1089
|
let flowLock = NSLock()
|
|
959
1090
|
var resumeBody = false
|
|
960
1091
|
server.respond { _ in
|
|
961
|
-
var response = LoopbackHTTPServer.Response(status: 206, body: tail, chunkSize: 5_000, chunkDelay: 0.
|
|
1092
|
+
var response = LoopbackHTTPServer.Response(status: 206, body: tail, chunkSize: 5_000, chunkDelay: 0.01)
|
|
1093
|
+
response.waitBeforeContinuingBodyTimeout = 8
|
|
962
1094
|
response.waitBeforeContinuingBody = {
|
|
963
1095
|
flowLock.lock()
|
|
964
1096
|
let go = resumeBody
|
|
@@ -969,13 +1101,16 @@ final class NetworkTransportTests: XCTestCase {
|
|
|
969
1101
|
}
|
|
970
1102
|
var rangeRequest = request("partial.zip")
|
|
971
1103
|
rangeRequest.setValue("bytes=10-", forHTTPHeaderField: "Range")
|
|
1104
|
+
rangeRequest.timeoutInterval = 45
|
|
972
1105
|
|
|
1106
|
+
let updater = makeUpdater()
|
|
1107
|
+
updater.timeout = 45
|
|
973
1108
|
let sampleLock = NSLock()
|
|
974
1109
|
var partialSamples: [(Int64, Int64)] = []
|
|
975
|
-
let result =
|
|
1110
|
+
let result = updater.performDownloadRequest(rangeRequest, label: "test") { received, expected in
|
|
976
1111
|
sampleLock.lock()
|
|
977
1112
|
partialSamples.append((received, expected))
|
|
978
|
-
if received >
|
|
1113
|
+
if received > 0 {
|
|
979
1114
|
flowLock.lock()
|
|
980
1115
|
resumeBody = true
|
|
981
1116
|
flowLock.unlock()
|
|
@@ -985,6 +1120,7 @@ final class NetworkTransportTests: XCTestCase {
|
|
|
985
1120
|
if let fileURL = result.fileURL {
|
|
986
1121
|
try? FileManager.default.removeItem(at: fileURL)
|
|
987
1122
|
}
|
|
1123
|
+
releaseUpdater(updater)
|
|
988
1124
|
|
|
989
1125
|
XCTAssertEqual(result.response?.statusCode, 206)
|
|
990
1126
|
XCTAssertEqual(server.requests.first?.headers["range"], "bytes=10-")
|
|
@@ -1001,7 +1137,10 @@ final class NetworkTransportTests: XCTestCase {
|
|
|
1001
1137
|
|
|
1002
1138
|
server.respond { _ in .init(status: 200, body: tail, chunkSize: 50_000, chunkDelay: 0.01) }
|
|
1003
1139
|
var fullSamples: [(Int64, Int64)] = []
|
|
1004
|
-
let
|
|
1140
|
+
let fullUpdater = makeUpdater()
|
|
1141
|
+
fullUpdater.timeout = 45
|
|
1142
|
+
rangeRequest.timeoutInterval = 45
|
|
1143
|
+
let fullResult = fullUpdater.performDownloadRequest(rangeRequest, label: "test") { received, expected in
|
|
1005
1144
|
sampleLock.lock()
|
|
1006
1145
|
fullSamples.append((received, expected))
|
|
1007
1146
|
sampleLock.unlock()
|
|
@@ -1009,6 +1148,7 @@ final class NetworkTransportTests: XCTestCase {
|
|
|
1009
1148
|
if let fileURL = fullResult.fileURL {
|
|
1010
1149
|
try? FileManager.default.removeItem(at: fileURL)
|
|
1011
1150
|
}
|
|
1151
|
+
releaseUpdater(fullUpdater)
|
|
1012
1152
|
XCTAssertFalse(fullSamples.isEmpty)
|
|
1013
1153
|
if let lastFull = fullSamples.last {
|
|
1014
1154
|
XCTAssertEqual(lastFull.1, Int64(tail.count))
|
|
@@ -87,6 +87,10 @@ final class SessionKeyRequiredTests: XCTestCase {
|
|
|
87
87
|
}
|
|
88
88
|
return nil
|
|
89
89
|
}
|
|
90
|
+
|
|
91
|
+
override func getBundleInfosByVersionName(version: String) -> [BundleInfo] {
|
|
92
|
+
getBundleInfoByVersionName(version: version).map { [$0] } ?? []
|
|
93
|
+
}
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
private final class TestableCapacitorUpdaterPlugin: CapacitorUpdaterPlugin {
|
|
@@ -83,6 +83,10 @@ private final class LifecycleCapgoUpdater: CapgoUpdater {
|
|
|
83
83
|
nil
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
override func getBundleInfosByVersionName(version: String) -> [BundleInfo] {
|
|
87
|
+
[]
|
|
88
|
+
}
|
|
89
|
+
|
|
86
90
|
override func download(url: URL, version: String, sessionKey: String, link: String? = nil, comment: String? = nil) throws -> BundleInfo {
|
|
87
91
|
try downloadVerified(
|
|
88
92
|
url: url,
|
|
@@ -94,14 +98,16 @@ private final class LifecycleCapgoUpdater: CapgoUpdater {
|
|
|
94
98
|
)
|
|
95
99
|
}
|
|
96
100
|
|
|
97
|
-
override func
|
|
101
|
+
override func downloadVerifiedWithTransferHook(
|
|
98
102
|
url _: URL,
|
|
99
103
|
version _: String,
|
|
100
104
|
sessionKey _: String,
|
|
101
105
|
expectedChecksum _: String,
|
|
102
|
-
link _: String
|
|
103
|
-
comment _: String
|
|
106
|
+
link _: String?,
|
|
107
|
+
comment _: String?,
|
|
108
|
+
beforeTransfer: (() throws -> Void)?
|
|
104
109
|
) throws -> BundleInfo {
|
|
110
|
+
try beforeTransfer?()
|
|
105
111
|
downloadCalls += 1
|
|
106
112
|
guard let downloadedBundleValue else {
|
|
107
113
|
throw NSError(domain: "UpdateLifecycleTests", code: 1)
|
|
@@ -196,15 +202,25 @@ final class UpdateLifecycleTests: XCTestCase {
|
|
|
196
202
|
)
|
|
197
203
|
UserDefaults.standard.removeObject(forKey: delayPreferencesKey)
|
|
198
204
|
UserDefaults.standard.removeObject(forKey: delayConditionModeKey)
|
|
205
|
+
resetRetryState()
|
|
199
206
|
}
|
|
200
207
|
|
|
201
208
|
override func tearDown() {
|
|
202
209
|
UserDefaults.standard.removeObject(forKey: delayPreferencesKey)
|
|
203
210
|
UserDefaults.standard.removeObject(forKey: delayConditionModeKey)
|
|
204
211
|
UserDefaults.standard.removeObject(forKey: lastFailedBundleKey)
|
|
212
|
+
resetRetryState()
|
|
205
213
|
super.tearDown()
|
|
206
214
|
}
|
|
207
215
|
|
|
216
|
+
/// Retry budgets and integrity blocks persist in UserDefaults until a successful install; other suites
|
|
217
|
+
/// (checksum failures, invalid manifests) use the same versions, so start each test from a clean budget.
|
|
218
|
+
private func resetRetryState() {
|
|
219
|
+
for version in ["1.0.0", "2.0.0", "3.0.0"] {
|
|
220
|
+
updater.resetIncompleteDownloadRetryCount(version: version)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
208
224
|
private func pendingBundle() -> BundleInfo {
|
|
209
225
|
BundleInfo(id: "bundle-2", version: "2.0.0", status: .PENDING, downloaded: Date(), checksum: "abc")
|
|
210
226
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capgo/capacitor-updater",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.0.4",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"description": "Live update for capacitor apps",
|
|
6
6
|
"main": "dist/plugin.cjs.js",
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
"clean": "rimraf ./dist",
|
|
77
77
|
"watch": "tsc --watch",
|
|
78
78
|
"prepublishOnly": "bun run build",
|
|
79
|
-
"check:wiring": "node scripts/check-capacitor-plugin-wiring.mjs && bun scripts/test-release-workflows.mjs && bun scripts/test-cli-crypto-output-guard.mjs && node scripts/test-builtin-hashes-hook.mjs && node scripts/check-ios-test-shards.mjs && node scripts/maestro/test-helpers.mjs && node scripts/maestro/test-smoke-sequence.mjs && bun scripts/maestro/test-deferred-flow.mjs && bun scripts/maestro/test-startup-update-flow.mjs &&
|
|
79
|
+
"check:wiring": "node scripts/check-capacitor-plugin-wiring.mjs && bun scripts/test-release-workflows.mjs && bun scripts/test-cli-crypto-output-guard.mjs && node scripts/test-builtin-hashes-hook.mjs && node scripts/check-ios-test-shards.mjs && node scripts/maestro/test-helpers.mjs && node scripts/maestro/test-smoke-sequence.mjs && bun scripts/maestro/test-deferred-flow.mjs && bun scripts/maestro/test-startup-update-flow.mjs && node scripts/maestro/test-local-plugin-manifest.mjs && node scripts/maestro/test-runner-attempt-cleanup.mjs",
|
|
80
80
|
"check:cap9-deprecated": "bun scripts/check-cap9-deprecated.mjs",
|
|
81
81
|
"changelog:ai": "node scripts/generate-ai-changelog.mjs",
|
|
82
82
|
"example:install": "cd example-app && bun install --frozen-lockfile",
|