@onekeyfe/react-native-range-downloader 3.0.81-alpha.8 → 3.0.81

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.
Files changed (111) hide show
  1. package/ReactNativeRangeDownloader.podspec +0 -8
  2. package/android/build.gradle +0 -1
  3. package/android/src/main/java/com/margelo/nitro/reactnativerangedownloader/ReactNativeRangeDownloader.kt +0 -144
  4. package/ios/RangeDownloadLogic.swift +0 -120
  5. package/ios/ReactNativeRangeDownloader.swift +6 -623
  6. package/lib/module/index.js +1 -2
  7. package/lib/module/index.js.map +1 -1
  8. package/lib/typescript/src/ReactNativeRangeDownloader.nitro.d.ts +1 -91
  9. package/lib/typescript/src/ReactNativeRangeDownloader.nitro.d.ts.map +1 -1
  10. package/lib/typescript/src/index.d.ts +0 -1
  11. package/lib/typescript/src/index.d.ts.map +1 -1
  12. package/nitrogen/generated/android/c++/JDownloadChannel.hpp +0 -3
  13. package/nitrogen/generated/android/c++/JHybridReactNativeRangeDownloaderSpec.cpp +0 -248
  14. package/nitrogen/generated/android/c++/JHybridReactNativeRangeDownloaderSpec.hpp +0 -12
  15. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/DownloadChannel.kt +1 -2
  16. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/HybridReactNativeRangeDownloaderSpec.kt +0 -49
  17. package/nitrogen/generated/ios/ReactNativeRangeDownloader-Swift-Cxx-Bridge.cpp +0 -48
  18. package/nitrogen/generated/ios/ReactNativeRangeDownloader-Swift-Cxx-Bridge.hpp +0 -329
  19. package/nitrogen/generated/ios/ReactNativeRangeDownloader-Swift-Cxx-Umbrella.hpp +0 -53
  20. package/nitrogen/generated/ios/c++/HybridReactNativeRangeDownloaderSpecSwift.hpp +0 -152
  21. package/nitrogen/generated/ios/swift/DownloadChannel.swift +0 -4
  22. package/nitrogen/generated/ios/swift/HybridReactNativeRangeDownloaderSpec.swift +0 -12
  23. package/nitrogen/generated/ios/swift/HybridReactNativeRangeDownloaderSpec_cxx.swift +0 -221
  24. package/nitrogen/generated/shared/c++/DownloadChannel.hpp +0 -4
  25. package/nitrogen/generated/shared/c++/HybridReactNativeRangeDownloaderSpec.cpp +0 -12
  26. package/nitrogen/generated/shared/c++/HybridReactNativeRangeDownloaderSpec.hpp +0 -58
  27. package/package.json +2 -4
  28. package/src/ReactNativeRangeDownloader.nitro.ts +1 -127
  29. package/src/index.tsx +1 -3
  30. package/android/src/main/java/com/margelo/nitro/reactnativerangedownloader/FirmwareArchiveRules.kt +0 -284
  31. package/android/src/main/java/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactStore.kt +0 -774
  32. package/android/src/test/java/com/margelo/nitro/reactnativerangedownloader/FirmwareArchiveRulesTest.kt +0 -75
  33. package/android/src/test/java/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactDeadlineTest.kt +0 -26
  34. package/ios/FirmwareArchiveMinizipBridge.h +0 -33
  35. package/ios/FirmwareArchiveMinizipBridge.mm +0 -213
  36. package/ios/FirmwareArtifactStore.swift +0 -1445
  37. package/ios/FirmwareBackgroundSessionEventRouter.swift +0 -17
  38. package/nitrogen/generated/android/c++/JFirmwareArchiveExpectedEntry.hpp +0 -69
  39. package/nitrogen/generated/android/c++/JFirmwareArchiveMaterializeParams.hpp +0 -86
  40. package/nitrogen/generated/android/c++/JFirmwareArchiveMaterializeResult.hpp +0 -80
  41. package/nitrogen/generated/android/c++/JFirmwareArchiveMaterializedArtifact.hpp +0 -63
  42. package/nitrogen/generated/android/c++/JFirmwareArtifactCancelParams.hpp +0 -57
  43. package/nitrogen/generated/android/c++/JFirmwareArtifactCapabilities.hpp +0 -88
  44. package/nitrogen/generated/android/c++/JFirmwareArtifactDownloadParams.hpp +0 -98
  45. package/nitrogen/generated/android/c++/JFirmwareArtifactLease.hpp +0 -57
  46. package/nitrogen/generated/android/c++/JFirmwareArtifactLeaseCreateParams.hpp +0 -57
  47. package/nitrogen/generated/android/c++/JFirmwareArtifactLeaseReleaseParams.hpp +0 -61
  48. package/nitrogen/generated/android/c++/JFirmwareArtifactLeaseRetainParams.hpp +0 -61
  49. package/nitrogen/generated/android/c++/JFirmwareArtifactReaderCloseParams.hpp +0 -57
  50. package/nitrogen/generated/android/c++/JFirmwareArtifactReaderInfo.hpp +0 -61
  51. package/nitrogen/generated/android/c++/JFirmwareArtifactReaderReadParams.hpp +0 -65
  52. package/nitrogen/generated/android/c++/JFirmwareArtifactReceipt.hpp +0 -65
  53. package/nitrogen/generated/android/c++/JFirmwareArtifactRefParams.hpp +0 -57
  54. package/nitrogen/generated/android/c++/JFirmwareArtifactSweepResult.hpp +0 -61
  55. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArchiveExpectedEntry.kt +0 -47
  56. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArchiveMaterializeParams.kt +0 -44
  57. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArchiveMaterializeResult.kt +0 -38
  58. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArchiveMaterializedArtifact.kt +0 -41
  59. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactCancelParams.kt +0 -38
  60. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactCapabilities.kt +0 -47
  61. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactDownloadParams.kt +0 -68
  62. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactLease.kt +0 -38
  63. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactLeaseCreateParams.kt +0 -38
  64. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactLeaseReleaseParams.kt +0 -41
  65. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactLeaseRetainParams.kt +0 -41
  66. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactReaderCloseParams.kt +0 -38
  67. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactReaderInfo.kt +0 -41
  68. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactReaderReadParams.kt +0 -44
  69. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactReceipt.kt +0 -44
  70. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactRefParams.kt +0 -38
  71. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativerangedownloader/FirmwareArtifactSweepResult.kt +0 -41
  72. package/nitrogen/generated/ios/swift/FirmwareArchiveExpectedEntry.swift +0 -69
  73. package/nitrogen/generated/ios/swift/FirmwareArchiveMaterializeParams.swift +0 -70
  74. package/nitrogen/generated/ios/swift/FirmwareArchiveMaterializeResult.swift +0 -48
  75. package/nitrogen/generated/ios/swift/FirmwareArchiveMaterializedArtifact.swift +0 -47
  76. package/nitrogen/generated/ios/swift/FirmwareArtifactCancelParams.swift +0 -36
  77. package/nitrogen/generated/ios/swift/FirmwareArtifactCapabilities.swift +0 -81
  78. package/nitrogen/generated/ios/swift/FirmwareArtifactDownloadParams.swift +0 -177
  79. package/nitrogen/generated/ios/swift/FirmwareArtifactLease.swift +0 -36
  80. package/nitrogen/generated/ios/swift/FirmwareArtifactLeaseCreateParams.swift +0 -36
  81. package/nitrogen/generated/ios/swift/FirmwareArtifactLeaseReleaseParams.swift +0 -47
  82. package/nitrogen/generated/ios/swift/FirmwareArtifactLeaseRetainParams.swift +0 -47
  83. package/nitrogen/generated/ios/swift/FirmwareArtifactReaderCloseParams.swift +0 -36
  84. package/nitrogen/generated/ios/swift/FirmwareArtifactReaderInfo.swift +0 -47
  85. package/nitrogen/generated/ios/swift/FirmwareArtifactReaderReadParams.swift +0 -58
  86. package/nitrogen/generated/ios/swift/FirmwareArtifactReceipt.swift +0 -58
  87. package/nitrogen/generated/ios/swift/FirmwareArtifactRefParams.swift +0 -36
  88. package/nitrogen/generated/ios/swift/FirmwareArtifactSweepResult.swift +0 -47
  89. package/nitrogen/generated/ios/swift/Func_void_FirmwareArchiveMaterializeResult.swift +0 -47
  90. package/nitrogen/generated/ios/swift/Func_void_FirmwareArtifactLease.swift +0 -47
  91. package/nitrogen/generated/ios/swift/Func_void_FirmwareArtifactReaderInfo.swift +0 -47
  92. package/nitrogen/generated/ios/swift/Func_void_FirmwareArtifactReceipt.swift +0 -47
  93. package/nitrogen/generated/ios/swift/Func_void_FirmwareArtifactSweepResult.swift +0 -47
  94. package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_ArrayBuffer_.swift +0 -47
  95. package/nitrogen/generated/shared/c++/FirmwareArchiveExpectedEntry.hpp +0 -87
  96. package/nitrogen/generated/shared/c++/FirmwareArchiveMaterializeParams.hpp +0 -86
  97. package/nitrogen/generated/shared/c++/FirmwareArchiveMaterializeResult.hpp +0 -77
  98. package/nitrogen/generated/shared/c++/FirmwareArchiveMaterializedArtifact.hpp +0 -81
  99. package/nitrogen/generated/shared/c++/FirmwareArtifactCancelParams.hpp +0 -75
  100. package/nitrogen/generated/shared/c++/FirmwareArtifactCapabilities.hpp +0 -88
  101. package/nitrogen/generated/shared/c++/FirmwareArtifactDownloadParams.hpp +0 -116
  102. package/nitrogen/generated/shared/c++/FirmwareArtifactLease.hpp +0 -75
  103. package/nitrogen/generated/shared/c++/FirmwareArtifactLeaseCreateParams.hpp +0 -75
  104. package/nitrogen/generated/shared/c++/FirmwareArtifactLeaseReleaseParams.hpp +0 -79
  105. package/nitrogen/generated/shared/c++/FirmwareArtifactLeaseRetainParams.hpp +0 -79
  106. package/nitrogen/generated/shared/c++/FirmwareArtifactReaderCloseParams.hpp +0 -75
  107. package/nitrogen/generated/shared/c++/FirmwareArtifactReaderInfo.hpp +0 -79
  108. package/nitrogen/generated/shared/c++/FirmwareArtifactReaderReadParams.hpp +0 -83
  109. package/nitrogen/generated/shared/c++/FirmwareArtifactReceipt.hpp +0 -83
  110. package/nitrogen/generated/shared/c++/FirmwareArtifactRefParams.hpp +0 -75
  111. package/nitrogen/generated/shared/c++/FirmwareArtifactSweepResult.hpp +0 -79
@@ -2,16 +2,6 @@ import Foundation
2
2
  import NitroModules
3
3
  import ReactNativeNativeLogger
4
4
 
5
- private func settledFirmwareArtifactPromise<T>(
6
- _ operation: () throws -> T
7
- ) -> Promise<T> {
8
- do {
9
- return Promise.resolved(withResult: try operation())
10
- } catch {
11
- return Promise.rejected(withError: error)
12
- }
13
- }
14
-
15
5
  // MARK: - Nitro HybridObject entry point
16
6
  //
17
7
  // Thin Nitro shim over `RangeDownloader.shared`. The heavy lifting — concurrent
@@ -105,176 +95,6 @@ class ReactNativeRangeDownloader: HybridReactNativeRangeDownloaderSpec {
105
95
  }
106
96
  return NSTemporaryDirectory()
107
97
  }
108
-
109
- func getFirmwareArtifactCapabilities() throws -> FirmwareArtifactCapabilities {
110
- FirmwareArtifactCapabilities(
111
- firmwareArtifactProtocolVersion: 2,
112
- supportedRouteTypes: ["domain", "pinnedIp"],
113
- supportsArchiveMaterialization: true,
114
- maxReadBytes: Double(FirmwareArtifactStore.maxReadBytes)
115
- )
116
- }
117
-
118
- func downloadFirmwareArtifact(
119
- params: FirmwareArtifactDownloadParams
120
- ) throws -> Promise<FirmwareArtifactReceipt> {
121
- do {
122
- if let artifact = try FirmwareArtifactStore.shared.cachedArtifact(params) {
123
- return Promise.resolved(
124
- withResult: FirmwareArtifactReceipt(
125
- artifactRef: artifact.artifactRef,
126
- size: Double(artifact.size),
127
- sha256: artifact.sha256
128
- )
129
- )
130
- }
131
- } catch {
132
- return Promise.rejected(withError: error)
133
- }
134
- return Promise.async {
135
- let artifact = try await FirmwareArtifactStore.shared.download(params)
136
- return FirmwareArtifactReceipt(
137
- artifactRef: artifact.artifactRef,
138
- size: Double(artifact.size),
139
- sha256: artifact.sha256
140
- )
141
- }
142
- }
143
-
144
- func cancelFirmwareArtifactDownloads(
145
- params: FirmwareArtifactCancelParams
146
- ) throws -> Promise<Void> {
147
- Promise.async {
148
- try await FirmwareArtifactStore.shared.cancelDownloads(
149
- transactionId: params.transactionId
150
- )
151
- }
152
- }
153
-
154
- func discardFirmwareArtifact(
155
- params: FirmwareArtifactRefParams
156
- ) throws -> Promise<Void> {
157
- settledFirmwareArtifactPromise {
158
- try FirmwareArtifactStore.shared.discard(artifactRef: params.artifactRef)
159
- }
160
- }
161
-
162
- func openFirmwareArtifact(
163
- params: FirmwareArtifactRefParams
164
- ) throws -> Promise<FirmwareArtifactReaderInfo> {
165
- settledFirmwareArtifactPromise {
166
- let reader = try FirmwareArtifactStore.shared.open(
167
- artifactRef: params.artifactRef
168
- )
169
- return FirmwareArtifactReaderInfo(
170
- readerId: reader.readerId,
171
- size: Double(reader.size)
172
- )
173
- }
174
- }
175
-
176
- func readFirmwareArtifact(
177
- params: FirmwareArtifactReaderReadParams
178
- ) throws -> Promise<ArrayBuffer> {
179
- settledFirmwareArtifactPromise {
180
- guard
181
- params.offset.isFinite,
182
- params.offset >= 0,
183
- params.offset <= Double(Int64.max),
184
- params.offset.rounded() == params.offset,
185
- params.length.isFinite,
186
- params.length > 0,
187
- params.length <= Double(Int.max),
188
- params.length.rounded() == params.length
189
- else {
190
- throw FirmwareArtifactStoreError.readerInvalid(
191
- "Invalid firmware artifact read"
192
- )
193
- }
194
- let data = try FirmwareArtifactStore.shared.read(
195
- readerId: params.readerId,
196
- offset: Int64(params.offset),
197
- length: Int(params.length)
198
- )
199
- return try ArrayBuffer.copy(data: data)
200
- }
201
- }
202
-
203
- func closeFirmwareArtifact(
204
- params: FirmwareArtifactReaderCloseParams
205
- ) throws -> Promise<Void> {
206
- settledFirmwareArtifactPromise {
207
- try FirmwareArtifactStore.shared.close(readerId: params.readerId)
208
- }
209
- }
210
-
211
- func materializeFirmwareArchive(
212
- params: FirmwareArchiveMaterializeParams
213
- ) throws -> Promise<FirmwareArchiveMaterializeResult> {
214
- settledFirmwareArtifactPromise {
215
- let entries = try FirmwareArtifactStore.shared.materializeArchive(
216
- leaseRef: params.leaseRef,
217
- artifactRef: params.archiveArtifactRef,
218
- expectedEntries: params.expectedEntries
219
- )
220
- return FirmwareArchiveMaterializeResult(
221
- artifacts: entries.map { entry in
222
- FirmwareArchiveMaterializedArtifact(
223
- entryName: entry.entryName,
224
- receipt: FirmwareArtifactReceipt(
225
- artifactRef: entry.artifact.artifactRef,
226
- size: Double(entry.artifact.size),
227
- sha256: entry.artifact.sha256
228
- )
229
- )
230
- }
231
- )
232
- }
233
- }
234
-
235
- func createFirmwareArtifactLease(
236
- params: FirmwareArtifactLeaseCreateParams
237
- ) throws -> Promise<FirmwareArtifactLease> {
238
- settledFirmwareArtifactPromise {
239
- FirmwareArtifactLease(
240
- leaseRef: try FirmwareArtifactStore.shared.createLease(
241
- transactionId: params.transactionId
242
- )
243
- )
244
- }
245
- }
246
-
247
- func retainFirmwareArtifact(
248
- params: FirmwareArtifactLeaseRetainParams
249
- ) throws -> Promise<Void> {
250
- settledFirmwareArtifactPromise {
251
- try FirmwareArtifactStore.shared.retain(
252
- leaseRef: params.leaseRef,
253
- artifactRef: params.artifactRef
254
- )
255
- }
256
- }
257
-
258
- func releaseFirmwareArtifactLease(
259
- params: FirmwareArtifactLeaseReleaseParams
260
- ) throws -> Promise<Void> {
261
- settledFirmwareArtifactPromise {
262
- try FirmwareArtifactStore.shared.releaseLease(
263
- leaseRef: params.leaseRef,
264
- disposition: params.disposition
265
- )
266
- }
267
- }
268
-
269
- func sweepFirmwareArtifactOrphans() throws -> Promise<FirmwareArtifactSweepResult> {
270
- settledFirmwareArtifactPromise {
271
- let result = try FirmwareArtifactStore.shared.sweepOrphans()
272
- return FirmwareArtifactSweepResult(
273
- deletedFiles: Double(result.deletedFiles),
274
- deletedBytes: Double(result.deletedBytes)
275
- )
276
- }
277
- }
278
98
  }
279
99
 
280
100
  // MARK: - RangeDownloader (migrated core)
@@ -336,50 +156,10 @@ extension RangeFallbackClass {
336
156
  }
337
157
  }
338
158
 
339
- private struct FirmwareBackgroundTaskDescriptor: Codable, Equatable {
340
- let schemaVersion: Int
341
- let taskId: String
342
- let transactionId: String
343
- let expectedSize: Int64
344
- let expectedSha256: String
345
- let hostname: String
346
- let deadlineAt: TimeInterval
347
-
348
- var key: String {
349
- firmwareArtifactDownloadKey(
350
- transactionId: transactionId,
351
- expectedSize: expectedSize,
352
- expectedSha256: expectedSha256
353
- )
354
- }
355
-
356
- func hasSameDownloadIdentity(
357
- as other: FirmwareBackgroundTaskDescriptor
358
- ) -> Bool {
359
- transactionId == other.transactionId &&
360
- expectedSize == other.expectedSize &&
361
- expectedSha256 == other.expectedSha256
362
- }
363
- }
364
-
365
159
  public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
366
160
 
367
161
  public static let shared = RangeDownloader()
368
162
 
369
- public static func routeFirmwareBackgroundEvents(
370
- identifier: String,
371
- completionHandler: @escaping () -> Void
372
- ) -> Bool {
373
- guard identifier == sessionIdentifier(for: .firmware) else {
374
- return false
375
- }
376
- shared.attachBackgroundEvents(
377
- identifier: identifier,
378
- completionHandler: completionHandler
379
- )
380
- return true
381
- }
382
-
383
163
  /// Posted by the AppDelegate from
384
164
  /// application(_:handleEventsForBackgroundURLSession:completionHandler:).
385
165
  /// userInfo: ["identifier": String, "completionHandler": () -> Void].
@@ -416,11 +196,6 @@ public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
416
196
  /// notification), keyed by session identifier, so we can call each back once
417
197
  /// all its queued background events have been delivered.
418
198
  private var backgroundCompletionHandlers: [String: () -> Void] = [:]
419
- private var firmwareWaiters: [
420
- String: [CheckedContinuation<StoredFirmwareArtifact, Error>]
421
- ] = [:]
422
- private var firmwareTaskErrors: [Int: Error] = [:]
423
- private var completedFirmwareTasks: Set<Int> = []
424
199
 
425
200
  // Per-run mutable state.
426
201
  private final class RunState {
@@ -551,30 +326,14 @@ public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
551
326
  @objc private func handleBackgroundEventsNotification(_ note: Notification) {
552
327
  guard let identifier = note.userInfo?["identifier"] as? String,
553
328
  Self.channel(forIdentifier: identifier) != nil else { return }
329
+ // Re-create the session with this delegate so queued completion events are
330
+ // delivered here on a background relaunch.
331
+ _ = session(forIdentifier: identifier)
554
332
  if let handler = note.userInfo?["completionHandler"] as? () -> Void {
555
- attachBackgroundEvents(
556
- identifier: identifier,
557
- completionHandler: handler
558
- )
559
- }
560
- }
561
-
562
- private func attachBackgroundEvents(
563
- identifier: String,
564
- completionHandler: @escaping () -> Void
565
- ) {
566
- let replacedHandler: (() -> Void)? = lock.withLockValue {
567
- backgroundCompletionHandlers.updateValue(
568
- completionHandler,
569
- forKey: identifier
570
- )
333
+ lock.lock()
334
+ backgroundCompletionHandlers[identifier] = handler
335
+ lock.unlock()
571
336
  }
572
- // UIKit should provide one live handler per session. Complete an older
573
- // handler instead of leaking it if the callback is unexpectedly repeated.
574
- replacedHandler?()
575
- // Store the handler before creating the session. Delegate delivery can
576
- // begin immediately when a background relaunch reattaches this identifier.
577
- _ = session(forIdentifier: identifier)
578
337
  }
579
338
 
580
339
  // MARK: - Session cache
@@ -678,238 +437,6 @@ public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
678
437
  return (r, decoded.segIndex)
679
438
  }
680
439
 
681
- private static let firmwareTaskDescriptionPrefix = "firmware-v2:"
682
-
683
- private static func encodeFirmwareTaskDescription(
684
- _ descriptor: FirmwareBackgroundTaskDescriptor
685
- ) throws -> String {
686
- let data = try JSONEncoder().encode(descriptor)
687
- return firmwareTaskDescriptionPrefix + data.base64EncodedString()
688
- }
689
-
690
- private static func decodeFirmwareTaskDescription(
691
- _ description: String?
692
- ) -> FirmwareBackgroundTaskDescriptor? {
693
- guard
694
- let description,
695
- description.hasPrefix(firmwareTaskDescriptionPrefix),
696
- let data = Data(
697
- base64Encoded: String(
698
- description.dropFirst(firmwareTaskDescriptionPrefix.count)
699
- )
700
- ),
701
- data.count <= 2048,
702
- let descriptor = try? JSONDecoder().decode(
703
- FirmwareBackgroundTaskDescriptor.self,
704
- from: data
705
- ),
706
- descriptor.schemaVersion == 2,
707
- descriptor.taskId.range(
708
- of: "^[A-Za-z0-9._-]{1,100}$",
709
- options: .regularExpression
710
- ) != nil,
711
- descriptor.transactionId.range(
712
- of: "^[A-Za-z0-9._:-]{1,160}$",
713
- options: .regularExpression
714
- ) != nil,
715
- descriptor.expectedSize > 0,
716
- descriptor.expectedSize <= 512 * 1024 * 1024,
717
- descriptor.expectedSha256.range(
718
- of: "^[a-f0-9]{64}$",
719
- options: .regularExpression
720
- ) != nil,
721
- !descriptor.hostname.isEmpty,
722
- descriptor.hostname.count <= 253,
723
- descriptor.deadlineAt.isFinite,
724
- descriptor.deadlineAt > 0
725
- else {
726
- return nil
727
- }
728
- return descriptor
729
- }
730
-
731
- func downloadFirmwareArtifact(
732
- params: FirmwareArtifactDownloadParams
733
- ) async throws -> StoredFirmwareArtifact {
734
- guard
735
- params.routeType == "domain",
736
- let url = URL(string: params.url),
737
- let hostname = url.host?.lowercased()
738
- else {
739
- throw FirmwareBackgroundDownloadError.invalidTask
740
- }
741
- let deadlineSeconds = params.overallDeadlineSeconds ?? 180
742
- guard
743
- deadlineSeconds.isFinite,
744
- deadlineSeconds > 0,
745
- deadlineSeconds <= 24 * 60 * 60
746
- else {
747
- throw FirmwareBackgroundDownloadError.invalidTask
748
- }
749
- let descriptor = FirmwareBackgroundTaskDescriptor(
750
- schemaVersion: 2,
751
- taskId: params.taskId,
752
- transactionId: params.transactionId,
753
- expectedSize: Int64(params.expectedSize),
754
- expectedSha256: params.expectedSha256.lowercased(),
755
- hostname: hostname,
756
- deadlineAt: Date().timeIntervalSince1970 + deadlineSeconds
757
- )
758
- guard !FirmwareArtifactStore.shared.isTransactionCancelled(
759
- descriptor.transactionId
760
- ) else {
761
- throw FirmwareBackgroundDownloadError.cancelled
762
- }
763
- if let stored = try FirmwareArtifactStore.shared.storedArtifact(
764
- expectedSize: descriptor.expectedSize,
765
- expectedSha256: descriptor.expectedSha256
766
- ) {
767
- return stored
768
- }
769
-
770
- return try await withCheckedThrowingContinuation { continuation in
771
- lock.withLockValue {
772
- firmwareWaiters[descriptor.key, default: []].append(continuation)
773
- }
774
- Task { [weak self] in
775
- await self?.reconcileOrStartFirmwareTask(
776
- descriptor: descriptor,
777
- url: url
778
- )
779
- }
780
- }
781
- }
782
-
783
- func cancelFirmwareArtifactDownloads(
784
- transactionId: String
785
- ) async throws {
786
- guard transactionId.range(
787
- of: "^[A-Za-z0-9._:-]{1,160}$",
788
- options: .regularExpression
789
- ) != nil else {
790
- throw FirmwareBackgroundDownloadError.invalidTask
791
- }
792
- let session = session(forChannel: .firmware, segmentCount: 1)
793
- let tasks = await allTasks(in: session)
794
- for task in tasks {
795
- guard
796
- let descriptor = Self.decodeFirmwareTaskDescription(
797
- task.taskDescription
798
- ),
799
- descriptor.transactionId == transactionId
800
- else {
801
- continue
802
- }
803
- recordFirmwareTaskError(
804
- taskIdentifier: task.taskIdentifier,
805
- error: FirmwareBackgroundDownloadError.cancelled
806
- )
807
- task.cancel()
808
- }
809
- }
810
-
811
- private func reconcileOrStartFirmwareTask(
812
- descriptor: FirmwareBackgroundTaskDescriptor,
813
- url: URL
814
- ) async {
815
- guard !FirmwareArtifactStore.shared.isTransactionCancelled(
816
- descriptor.transactionId
817
- ) else {
818
- finishFirmwareTask(
819
- key: descriptor.key,
820
- result: .failure(FirmwareBackgroundDownloadError.cancelled)
821
- )
822
- return
823
- }
824
- let session = session(forChannel: .firmware, segmentCount: 1)
825
- let tasks = await allTasks(in: session)
826
- var matchingTaskFound = false
827
- for task in tasks {
828
- guard
829
- let candidate = Self.decodeFirmwareTaskDescription(
830
- task.taskDescription
831
- )
832
- else {
833
- continue
834
- }
835
- if candidate.hasSameDownloadIdentity(as: descriptor) {
836
- matchingTaskFound = true
837
- } else if
838
- candidate.transactionId == descriptor.transactionId &&
839
- candidate.taskId == descriptor.taskId
840
- {
841
- task.cancel()
842
- }
843
- }
844
- if matchingTaskFound {
845
- return
846
- }
847
- do {
848
- if let stored = try FirmwareArtifactStore.shared.storedArtifact(
849
- expectedSize: descriptor.expectedSize,
850
- expectedSha256: descriptor.expectedSha256
851
- ) {
852
- finishFirmwareTask(
853
- key: descriptor.key,
854
- result: .success(stored)
855
- )
856
- return
857
- }
858
- guard Date().timeIntervalSince1970 < descriptor.deadlineAt else {
859
- throw FirmwareBackgroundDownloadError.deadlineExceeded
860
- }
861
- guard !FirmwareArtifactStore.shared.isTransactionCancelled(
862
- descriptor.transactionId
863
- ) else {
864
- throw FirmwareBackgroundDownloadError.cancelled
865
- }
866
- var request = URLRequest(url: url)
867
- request.cachePolicy = .reloadIgnoringLocalCacheData
868
- request.timeoutInterval = max(
869
- 1,
870
- descriptor.deadlineAt - Date().timeIntervalSince1970
871
- )
872
- request.setValue("identity", forHTTPHeaderField: "Accept-Encoding")
873
- let task = session.downloadTask(with: request)
874
- task.taskDescription = try Self.encodeFirmwareTaskDescription(
875
- descriptor
876
- )
877
- task.resume()
878
- } catch {
879
- finishFirmwareTask(
880
- key: descriptor.key,
881
- result: .failure(error)
882
- )
883
- }
884
- }
885
-
886
- private func allTasks(in session: URLSession) async -> [URLSessionTask] {
887
- await withCheckedContinuation { continuation in
888
- session.getAllTasks { tasks in
889
- continuation.resume(returning: tasks)
890
- }
891
- }
892
- }
893
-
894
- private func finishFirmwareTask(
895
- key: String,
896
- result: Result<StoredFirmwareArtifact, Error>
897
- ) {
898
- let continuations: [
899
- CheckedContinuation<StoredFirmwareArtifact, Error>
900
- ] = lock.withLockValue {
901
- firmwareWaiters.removeValue(forKey: key) ?? []
902
- }
903
- for continuation in continuations {
904
- switch result {
905
- case let .success(artifact):
906
- continuation.resume(returning: artifact)
907
- case let .failure(error):
908
- continuation.resume(throwing: error)
909
- }
910
- }
911
- }
912
-
913
440
  // MARK: - Public entry
914
441
 
915
442
  /// Downloads [urlString] into [filePath] using concurrent background ranges.
@@ -1445,28 +972,6 @@ public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
1445
972
  public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
1446
973
  didWriteData bytesWritten: Int64, totalBytesWritten: Int64,
1447
974
  totalBytesExpectedToWrite: Int64) {
1448
- if let descriptor = Self.decodeFirmwareTaskDescription(
1449
- downloadTask.taskDescription
1450
- ) {
1451
- let exceedsBound =
1452
- totalBytesWritten > descriptor.expectedSize ||
1453
- (
1454
- totalBytesExpectedToWrite != NSURLSessionTransferSizeUnknown &&
1455
- totalBytesExpectedToWrite != descriptor.expectedSize
1456
- )
1457
- let expired =
1458
- Date().timeIntervalSince1970 >= descriptor.deadlineAt
1459
- if exceedsBound || expired {
1460
- recordFirmwareTaskError(
1461
- taskIdentifier: downloadTask.taskIdentifier,
1462
- error: exceedsBound
1463
- ? FirmwareBackgroundDownloadError.sizeRejected
1464
- : FirmwareBackgroundDownloadError.deadlineExceeded
1465
- )
1466
- downloadTask.cancel()
1467
- }
1468
- return
1469
- }
1470
975
  guard let desc = downloadTask.taskDescription,
1471
976
  let (state, idx) = run(for: desc) else { return }
1472
977
  lock.lock()
@@ -1497,52 +1002,6 @@ public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
1497
1002
 
1498
1003
  public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
1499
1004
  didFinishDownloadingTo location: URL) {
1500
- if let descriptor = Self.decodeFirmwareTaskDescription(
1501
- downloadTask.taskDescription
1502
- ) {
1503
- guard firmwareTaskError(
1504
- taskIdentifier: downloadTask.taskIdentifier
1505
- ) == nil else {
1506
- return
1507
- }
1508
- do {
1509
- guard
1510
- !FirmwareArtifactStore.shared.isTransactionCancelled(
1511
- descriptor.transactionId
1512
- ),
1513
- Date().timeIntervalSince1970 < descriptor.deadlineAt,
1514
- let response = downloadTask.response as? HTTPURLResponse,
1515
- response.statusCode == 200,
1516
- response.url?.scheme?.lowercased() == "https",
1517
- response.url?.host?.lowercased() == descriptor.hostname,
1518
- response.url?.port == nil || response.url?.port == 443
1519
- else {
1520
- throw FirmwareBackgroundDownloadError.responseRejected
1521
- }
1522
- let artifact = try FirmwareArtifactStore.shared.acceptBackgroundDownload(
1523
- temporaryURL: location,
1524
- transactionId: descriptor.transactionId,
1525
- expectedSize: descriptor.expectedSize,
1526
- expectedSha256: descriptor.expectedSha256
1527
- )
1528
- lock.withLockValue {
1529
- firmwareTaskErrors.removeValue(
1530
- forKey: downloadTask.taskIdentifier
1531
- )
1532
- completedFirmwareTasks.insert(downloadTask.taskIdentifier)
1533
- }
1534
- finishFirmwareTask(
1535
- key: descriptor.key,
1536
- result: .success(artifact)
1537
- )
1538
- } catch {
1539
- recordFirmwareTaskError(
1540
- taskIdentifier: downloadTask.taskIdentifier,
1541
- error: error
1542
- )
1543
- }
1544
- return
1545
- }
1546
1005
  guard let desc = downloadTask.taskDescription,
1547
1006
  let (state, idx) = run(for: desc) else { return }
1548
1007
  let ranges = lock.withLockValue { state.ranges }
@@ -1641,43 +1100,6 @@ public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
1641
1100
  }
1642
1101
 
1643
1102
  public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
1644
- if let descriptor = Self.decodeFirmwareTaskDescription(
1645
- task.taskDescription
1646
- ) {
1647
- let completion: (completed: Bool, error: Error?) =
1648
- lock.withLockValue {
1649
- let completed = completedFirmwareTasks.remove(
1650
- task.taskIdentifier
1651
- ) != nil
1652
- let recordedError = firmwareTaskErrors.removeValue(
1653
- forKey: task.taskIdentifier
1654
- )
1655
- return (completed, recordedError)
1656
- }
1657
- if completion.completed {
1658
- return
1659
- }
1660
- let failure: Error
1661
- if let recordedError = completion.error {
1662
- failure = recordedError
1663
- } else if FirmwareArtifactStore.shared.isTransactionCancelled(
1664
- descriptor.transactionId
1665
- ) {
1666
- failure = FirmwareBackgroundDownloadError.cancelled
1667
- } else if let taskError = error {
1668
- failure =
1669
- isFirmwareArtifactTLSError(taskError)
1670
- ? FirmwareBackgroundDownloadError.tlsRejected
1671
- : FirmwareBackgroundDownloadError.transferFailed
1672
- } else {
1673
- failure = FirmwareBackgroundDownloadError.transferFailed
1674
- }
1675
- finishFirmwareTask(
1676
- key: descriptor.key,
1677
- result: .failure(failure)
1678
- )
1679
- return
1680
- }
1681
1103
  guard let desc = task.taskDescription,
1682
1104
  let (state, idx) = run(for: desc) else { return }
1683
1105
  let ranges = lock.withLockValue { state.ranges }
@@ -1853,26 +1275,6 @@ public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
1853
1275
  willPerformHTTPRedirection response: HTTPURLResponse,
1854
1276
  newRequest request: URLRequest,
1855
1277
  completionHandler: @escaping (URLRequest?) -> Void) {
1856
- if let descriptor = Self.decodeFirmwareTaskDescription(
1857
- task.taskDescription
1858
- ) {
1859
- guard
1860
- request.url?.scheme?.lowercased() == "https",
1861
- request.url?.host?.lowercased() == descriptor.hostname,
1862
- request.url?.port == nil || request.url?.port == 443,
1863
- request.url?.user == nil,
1864
- request.url?.password == nil
1865
- else {
1866
- recordFirmwareTaskError(
1867
- taskIdentifier: task.taskIdentifier,
1868
- error: FirmwareBackgroundDownloadError.redirectRejected
1869
- )
1870
- completionHandler(nil)
1871
- return
1872
- }
1873
- completionHandler(request)
1874
- return
1875
- }
1876
1278
  if request.url?.scheme?.lowercased() != "https" {
1877
1279
  OneKeyLog.error("RangeDownloader", "blocked redirect to non-HTTPS URL")
1878
1280
  if let desc = task.taskDescription, let (state, _) = run(for: desc) {
@@ -1884,25 +1286,6 @@ public final class RangeDownloader: NSObject, URLSessionDownloadDelegate {
1884
1286
  }
1885
1287
  }
1886
1288
 
1887
- private func recordFirmwareTaskError(
1888
- taskIdentifier: Int,
1889
- error: Error
1890
- ) {
1891
- lock.withLockValue {
1892
- if firmwareTaskErrors[taskIdentifier] == nil {
1893
- firmwareTaskErrors[taskIdentifier] = error
1894
- }
1895
- }
1896
- }
1897
-
1898
- private func firmwareTaskError(
1899
- taskIdentifier: Int
1900
- ) -> Error? {
1901
- lock.withLockValue {
1902
- firmwareTaskErrors[taskIdentifier]
1903
- }
1904
- }
1905
-
1906
1289
  /// Called on the session delegate queue when all background events for this
1907
1290
  /// session have been delivered after a background relaunch.
1908
1291
  public func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
@@ -10,7 +10,6 @@ export const ReactNativeRangeDownloader = ReactNativeRangeDownloaderHybridObject
10
10
  export const RangeDownloadChannel = {
11
11
  Bundle: 'bundle',
12
12
  Apk: 'apk',
13
- Chart: 'chart',
14
- Firmware: 'firmware'
13
+ Chart: 'chart'
15
14
  };
16
15
  //# sourceMappingURL=index.js.map