@gfean/react-native-bundle-drop 0.3.3 → 0.3.6

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 (84) hide show
  1. package/BundleDrop.podspec +2 -0
  2. package/README.md +8 -2
  3. package/android/build.gradle +91 -1
  4. package/android/gradle.properties +1 -0
  5. package/android/src/main/java/com/bundledrop/BundleDropFileOps.kt +131 -0
  6. package/android/src/main/java/com/bundledrop/BundleDropModule.kt +65 -89
  7. package/android/src/main/java/com/bundledrop/BundleDropOtaResolver.kt +93 -0
  8. package/ios/BundleDropBridge.m +14 -0
  9. package/ios/BundleDropFileOps.swift +220 -0
  10. package/ios/BundleDropLocator.h +11 -0
  11. package/ios/BundleDropLocator.swift +34 -45
  12. package/ios/BundleDropModule.swift +65 -15
  13. package/ios/BundleDropOtaResolver.swift +112 -0
  14. package/ios/BundleDropZipExtractor.h +13 -0
  15. package/ios/BundleDropZipExtractor.m +226 -0
  16. package/lib/CLI/cli.js +56 -6
  17. package/lib/CLI/scripts/aipowered/apply-plan.d.ts +10 -0
  18. package/lib/CLI/scripts/aipowered/apply-plan.js +50 -0
  19. package/lib/CLI/scripts/aipowered/backend-client.d.ts +6 -0
  20. package/lib/CLI/scripts/aipowered/backend-client.js +34 -0
  21. package/lib/CLI/scripts/aipowered/diff-preview.d.ts +7 -0
  22. package/lib/CLI/scripts/aipowered/diff-preview.js +33 -0
  23. package/lib/CLI/scripts/aipowered/init-native-config.d.ts +2 -0
  24. package/lib/CLI/scripts/aipowered/init-native-config.js +131 -0
  25. package/lib/CLI/scripts/aipowered/scanner.d.ts +5 -0
  26. package/lib/CLI/scripts/aipowered/scanner.js +349 -0
  27. package/lib/CLI/scripts/aipowered/types.d.ts +53 -0
  28. package/lib/CLI/scripts/aipowered/types.js +2 -0
  29. package/lib/CLI/scripts/aipowered/validate-plan.d.ts +8 -0
  30. package/lib/CLI/scripts/aipowered/validate-plan.js +164 -0
  31. package/lib/CLI/scripts/init-config.js +10 -5
  32. package/lib/CLI/scripts/init-native-config.js +1 -1
  33. package/lib/CLI/scripts/post-init.js +7 -18
  34. package/lib/CLI/scripts/upload-cli.d.ts +2 -0
  35. package/lib/CLI/scripts/upload-cli.js +67 -28
  36. package/lib/CLI/utils/ui.d.ts +3 -0
  37. package/lib/CLI/utils/ui.js +26 -0
  38. package/lib/api/clientApi.d.ts +2 -1
  39. package/lib/api/clientApi.js +9 -0
  40. package/lib/api/types.d.ts +21 -3
  41. package/lib/bundleInfo.d.ts +12 -0
  42. package/lib/context.d.ts +19 -2
  43. package/lib/context.js +5 -2
  44. package/lib/errors.d.ts +25 -0
  45. package/lib/errors.js +26 -1
  46. package/lib/fs/bundlePointer.d.ts +2 -0
  47. package/lib/fs/bundlePointer.js +14 -4
  48. package/lib/fs/fsUtils.d.ts +1 -2
  49. package/lib/fs/fsUtils.js +12 -27
  50. package/lib/fs/userProperties.d.ts +25 -4
  51. package/lib/fs/userProperties.js +51 -7
  52. package/lib/index.d.ts +7 -3
  53. package/lib/index.js +5 -1
  54. package/lib/injectImageResolver.js +6 -15
  55. package/lib/install/installFromZip.d.ts +1 -1
  56. package/lib/install/installFromZip.js +47 -42
  57. package/lib/loadConfig.d.ts +21 -2
  58. package/lib/loadConfig.js +11 -7
  59. package/lib/manager/downloadAndInstall.d.ts +3 -0
  60. package/lib/manager/downloadAndInstall.js +30 -28
  61. package/lib/manager/reporting.d.ts +2 -0
  62. package/lib/manager/reporting.js +27 -2
  63. package/lib/manager/rollbackState.d.ts +28 -11
  64. package/lib/manager/rollbackState.js +90 -38
  65. package/lib/manager/updateCheck.js +17 -3
  66. package/lib/manager/updateState.d.ts +5 -1
  67. package/lib/manager/updateState.js +20 -11
  68. package/lib/native/fs.d.ts +6 -0
  69. package/lib/native/fs.js +27 -0
  70. package/lib/runtime/initState.d.ts +2 -2
  71. package/lib/runtime/service.d.ts +28 -0
  72. package/lib/runtime/service.js +136 -10
  73. package/lib/scripts/bundle.d.ts +4 -1
  74. package/lib/scripts/bundle.js +42 -29
  75. package/lib/scripts/projectRoot.d.ts +1 -0
  76. package/lib/scripts/projectRoot.js +54 -0
  77. package/lib/scripts/resolve-ios-version.d.ts +2 -0
  78. package/lib/scripts/resolve-ios-version.js +74 -0
  79. package/lib/scripts/upload-bundle.d.ts +2 -1
  80. package/lib/scripts/upload-bundle.js +56 -41
  81. package/lib/types.d.ts +3 -0
  82. package/lib/useBundleDrop.d.ts +2 -0
  83. package/lib/useBundleDrop.js +1 -0
  84. package/package.json +9 -11
@@ -0,0 +1,220 @@
1
+ import Foundation
2
+
3
+ enum BundleDropFileOps {
4
+ static let defaultMaxDownloadBytes: Int64 = 256 * 1024 * 1024
5
+
6
+ static func validateHttpUrl(_ urlString: String) throws -> URL {
7
+ guard let url = URL(string: urlString),
8
+ let scheme = url.scheme?.lowercased(),
9
+ scheme == "https" || scheme == "http",
10
+ url.host != nil else {
11
+ throw error("Invalid or non-HTTP URL: \(urlString)")
12
+ }
13
+ return url
14
+ }
15
+
16
+ static func unlinkPath(
17
+ _ path: String,
18
+ fileManager: FileManager = .default
19
+ ) throws {
20
+ if fileManager.fileExists(atPath: path) {
21
+ try fileManager.removeItem(atPath: path)
22
+ }
23
+ }
24
+
25
+ static func moveFile(
26
+ srcPath: String,
27
+ destPath: String,
28
+ fileManager: FileManager = .default
29
+ ) throws {
30
+ let destURL = URL(fileURLWithPath: destPath)
31
+ let destDir = destURL.deletingLastPathComponent()
32
+ if !fileManager.fileExists(atPath: destDir.path) {
33
+ try fileManager.createDirectory(
34
+ at: destDir,
35
+ withIntermediateDirectories: true
36
+ )
37
+ }
38
+
39
+ if fileManager.fileExists(atPath: destPath) {
40
+ try fileManager.removeItem(atPath: destPath)
41
+ }
42
+
43
+ try fileManager.moveItem(
44
+ at: URL(fileURLWithPath: srcPath),
45
+ to: destURL
46
+ )
47
+ }
48
+
49
+ static func downloadToFile(
50
+ urlString: String,
51
+ destPath: String,
52
+ maxBytes: Int64 = defaultMaxDownloadBytes,
53
+ configuration: URLSessionConfiguration = .default,
54
+ fileManager: FileManager = .default,
55
+ completion: @escaping (Result<Void, Error>) -> Void
56
+ ) {
57
+ do {
58
+ let url = try validateHttpUrl(urlString)
59
+ downloadToFile(
60
+ url: url,
61
+ destPath: destPath,
62
+ maxBytes: maxBytes,
63
+ configuration: configuration,
64
+ fileManager: fileManager,
65
+ completion: completion
66
+ )
67
+ } catch {
68
+ completion(.failure(error))
69
+ }
70
+ }
71
+
72
+ static func downloadToFile(
73
+ url: URL,
74
+ destPath: String,
75
+ maxBytes: Int64 = defaultMaxDownloadBytes,
76
+ configuration: URLSessionConfiguration = .default,
77
+ fileManager: FileManager = .default,
78
+ completion: @escaping (Result<Void, Error>) -> Void
79
+ ) {
80
+ let delegate = BundleDropDownloadDelegate(
81
+ destURL: URL(fileURLWithPath: destPath),
82
+ maxBytes: maxBytes,
83
+ fileManager: fileManager,
84
+ completion: completion
85
+ )
86
+ let session = URLSession(
87
+ configuration: configuration,
88
+ delegate: delegate,
89
+ delegateQueue: nil
90
+ )
91
+ delegate.session = session
92
+ session.dataTask(with: url).resume()
93
+ }
94
+
95
+ static func error(_ message: String) -> NSError {
96
+ NSError(
97
+ domain: "BundleDrop",
98
+ code: 1,
99
+ userInfo: [NSLocalizedDescriptionKey: message]
100
+ )
101
+ }
102
+ }
103
+
104
+ private final class BundleDropDownloadDelegate: NSObject, URLSessionDataDelegate, @unchecked Sendable {
105
+ private let destURL: URL
106
+ private let maxBytes: Int64
107
+ private let fileManager: FileManager
108
+ private let completion: (Result<Void, Error>) -> Void
109
+ private var fileHandle: FileHandle?
110
+ private var bytesWritten: Int64 = 0
111
+ private var finished = false
112
+
113
+ weak var session: URLSession?
114
+
115
+ init(
116
+ destURL: URL,
117
+ maxBytes: Int64,
118
+ fileManager: FileManager,
119
+ completion: @escaping (Result<Void, Error>) -> Void
120
+ ) {
121
+ self.destURL = destURL
122
+ self.maxBytes = maxBytes
123
+ self.fileManager = fileManager
124
+ self.completion = completion
125
+ }
126
+
127
+ func urlSession(
128
+ _ session: URLSession,
129
+ dataTask: URLSessionDataTask,
130
+ didReceive response: URLResponse,
131
+ completionHandler: @escaping (URLSession.ResponseDisposition) -> Void
132
+ ) {
133
+ guard let httpResponse = response as? HTTPURLResponse else {
134
+ fail(BundleDropFileOps.error("Invalid HTTP response"))
135
+ completionHandler(.cancel)
136
+ return
137
+ }
138
+
139
+ guard (200...299).contains(httpResponse.statusCode) else {
140
+ fail(BundleDropFileOps.error("HTTP \(httpResponse.statusCode)"))
141
+ completionHandler(.cancel)
142
+ return
143
+ }
144
+
145
+ do {
146
+ let destDir = destURL.deletingLastPathComponent()
147
+ if !fileManager.fileExists(atPath: destDir.path) {
148
+ try fileManager.createDirectory(
149
+ at: destDir,
150
+ withIntermediateDirectories: true
151
+ )
152
+ }
153
+ if fileManager.fileExists(atPath: destURL.path) {
154
+ try fileManager.removeItem(at: destURL)
155
+ }
156
+ fileManager.createFile(atPath: destURL.path, contents: nil)
157
+ fileHandle = try FileHandle(forWritingTo: destURL)
158
+ completionHandler(.allow)
159
+ } catch {
160
+ fail(error)
161
+ completionHandler(.cancel)
162
+ }
163
+ }
164
+
165
+ func urlSession(
166
+ _ session: URLSession,
167
+ dataTask: URLSessionDataTask,
168
+ didReceive data: Data
169
+ ) {
170
+ guard !finished else { return }
171
+
172
+ let nextBytesWritten = bytesWritten + Int64(data.count)
173
+ if nextBytesWritten > maxBytes {
174
+ fail(BundleDropFileOps.error(
175
+ "Download exceeds \(maxBytes / (1024 * 1024)) MB limit"
176
+ ))
177
+ dataTask.cancel()
178
+ return
179
+ }
180
+
181
+ bytesWritten = nextBytesWritten
182
+ fileHandle?.write(data)
183
+ }
184
+
185
+ func urlSession(
186
+ _ session: URLSession,
187
+ task: URLSessionTask,
188
+ didCompleteWithError error: Error?
189
+ ) {
190
+ guard !finished else { return }
191
+
192
+ if let error = error {
193
+ fail(error)
194
+ return
195
+ }
196
+
197
+ finished = true
198
+ closeFile()
199
+ completion(.success(()))
200
+ session.finishTasksAndInvalidate()
201
+ }
202
+
203
+ private func fail(_ error: Error) {
204
+ guard !finished else { return }
205
+ finished = true
206
+ closeFile()
207
+ try? fileManager.removeItem(at: destURL)
208
+ completion(.failure(error))
209
+ session?.invalidateAndCancel()
210
+ }
211
+
212
+ private func closeFile() {
213
+ if #available(iOS 13.0, macOS 10.15, *) {
214
+ try? fileHandle?.close()
215
+ } else {
216
+ fileHandle?.closeFile()
217
+ }
218
+ fileHandle = nil
219
+ }
220
+ }
@@ -0,0 +1,11 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ NS_ASSUME_NONNULL_BEGIN
4
+
5
+ @interface BundleDropLocator : NSObject
6
+
7
+ + (nullable NSURL *)bundleURL;
8
+
9
+ @end
10
+
11
+ NS_ASSUME_NONNULL_END
@@ -1,64 +1,53 @@
1
1
  import Foundation
2
2
 
3
- @objc public final class BundleDropLocator: NSObject {
3
+ @objc(BundleDropLocator) public final class BundleDropLocator: NSObject {
4
4
 
5
- private static let binaryVersionKey = "BundleDropBinaryVersion"
5
+ static let binaryVersionKey = "BundleDropBinaryVersion"
6
6
 
7
- private static func getBinaryVersionKey() -> String {
8
- let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown"
9
- let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "0"
7
+ static func getBinaryVersionKey(bundle: Bundle = .main) -> String {
8
+ let version = bundle.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown"
9
+ let build = bundle.infoDictionary?["CFBundleVersion"] as? String ?? "0"
10
10
  return "\(version)-\(build)"
11
11
  }
12
12
 
13
- private static func clearOtaState(root: URL) {
14
- let fm = FileManager.default
15
- let filesToClear = [
16
- root.appendingPathComponent("current.json"),
17
- root.appendingPathComponent("previous.json"),
18
- root.appendingPathComponent("state.json"),
19
- ]
20
- filesToClear.forEach { try? fm.removeItem(at: $0) }
21
-
22
- if let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first {
23
- try? fm.removeItem(at: docs.appendingPathComponent("bundle-info.json"))
24
- }
25
- }
26
-
27
13
  @objc public static func bundleURL() -> URL? {
28
14
  let fm = FileManager.default
29
15
  guard let lib = fm.urls(for: .libraryDirectory, in: .userDomainMask).first else { return nil }
16
+ guard let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
30
17
  let root = lib.appendingPathComponent("bundle-drop", isDirectory: true)
31
- let current = root.appendingPathComponent("current.json")
32
18
 
33
19
  let currentVersion = getBinaryVersionKey()
20
+ return bundleURL(
21
+ bundleDropRoot: root,
22
+ documentsDirectory: docs,
23
+ currentBinaryVersion: currentVersion
24
+ )
25
+ }
34
26
 
35
- guard fm.fileExists(atPath: current.path) else {
36
- UserDefaults.standard.set(currentVersion, forKey: binaryVersionKey)
37
- return nil
38
- }
39
-
40
- let lastVersion = UserDefaults.standard.string(forKey: binaryVersionKey)
41
- if let lastVersion = lastVersion, lastVersion != currentVersion {
42
- print("BundleDrop: Binary updated (\(lastVersion) → \(currentVersion)), clearing OTA bundle")
43
- clearOtaState(root: root)
44
- UserDefaults.standard.set(currentVersion, forKey: binaryVersionKey)
45
- return nil
46
- }
47
-
48
- do {
49
- let data = try Data(contentsOf: current)
50
- let obj = try JSONSerialization.jsonObject(with: data) as? [String: Any]
51
- guard let bundlePath = obj?["bundlePath"] as? String else { return nil }
52
- let url = URL(fileURLWithPath: bundlePath)
53
- guard fm.fileExists(atPath: url.path) else { return nil }
54
-
55
- UserDefaults.standard.set(currentVersion, forKey: binaryVersionKey)
56
- return url
57
- } catch {
58
- print("⚠️ BundleDrop: failed reading current.json: \(error)")
27
+ static func bundleURL(
28
+ bundleDropRoot: URL,
29
+ documentsDirectory: URL,
30
+ currentBinaryVersion: String,
31
+ userDefaults: UserDefaults = .standard,
32
+ fileManager: FileManager = .default,
33
+ shouldLogBinaryUpdate: Bool = true,
34
+ log: (String) -> Void = { print($0) }
35
+ ) -> URL? {
36
+ let lastVersion = userDefaults.string(forKey: binaryVersionKey)
37
+ let result = BundleDropOtaResolver.resolve(
38
+ bundleDropRoot: bundleDropRoot,
39
+ documentsDirectory: documentsDirectory,
40
+ currentBinaryVersion: currentBinaryVersion,
41
+ storedBinaryVersion: lastVersion,
42
+ fileManager: fileManager
43
+ )
44
+
45
+ if shouldLogBinaryUpdate, result.clearedOta, let lastVersion = lastVersion {
46
+ log("BundleDrop: Binary updated (\(lastVersion) → \(currentBinaryVersion)), clearing OTA bundle")
59
47
  }
60
48
 
61
- return nil
49
+ userDefaults.set(result.storedVersion, forKey: binaryVersionKey)
50
+ return result.bundleURL
62
51
  }
63
52
 
64
53
  @objc public static func fileSize(at url: URL) -> UInt64 {
@@ -90,11 +90,12 @@ final class BundleDropModule: NSObject {
90
90
  return
91
91
  }
92
92
  do {
93
+ let fileURL = URL(fileURLWithPath: path)
93
94
  if encoding == "base64" {
94
- let data = try Data(contentsOf: URL(fileURLWithPath: path))
95
+ let data = try Data(contentsOf: fileURL)
95
96
  resolve(data.base64EncodedString())
96
97
  } else {
97
- let text = try String(contentsOfFile: path, encoding: .utf8)
98
+ let text = try String(contentsOf: fileURL, encoding: .utf8)
98
99
  resolve(text)
99
100
  }
100
101
  } catch {
@@ -115,14 +116,15 @@ final class BundleDropModule: NSObject {
115
116
  if !Self.fm.fileExists(atPath: dir) {
116
117
  try Self.fm.createDirectory(atPath: dir, withIntermediateDirectories: true)
117
118
  }
119
+ let fileURL = URL(fileURLWithPath: path)
118
120
  if encoding == "base64" {
119
121
  guard let data = Data(base64Encoded: content) else {
120
122
  reject("ERR_WRITE", "Invalid base64 data", nil)
121
123
  return
122
124
  }
123
- try data.write(to: URL(fileURLWithPath: path))
125
+ try data.write(to: fileURL)
124
126
  } else {
125
- try content.write(toFile: path, atomically: true, encoding: .utf8)
127
+ try content.write(to: fileURL, atomically: true, encoding: .utf8)
126
128
  }
127
129
  resolve(nil)
128
130
  } catch {
@@ -153,15 +155,27 @@ final class BundleDropModule: NSObject {
153
155
  reject: @escaping RCTPromiseRejectBlock
154
156
  ) {
155
157
  do {
156
- if Self.fm.fileExists(atPath: path) {
157
- try Self.fm.removeItem(atPath: path)
158
- }
158
+ try BundleDropFileOps.unlinkPath(path)
159
159
  resolve(nil)
160
160
  } catch {
161
161
  reject("ERR_UNLINK", error.localizedDescription, error)
162
162
  }
163
163
  }
164
164
 
165
+ @objc
166
+ func fsReadDir(
167
+ _ path: String,
168
+ resolve: @escaping RCTPromiseResolveBlock,
169
+ reject: @escaping RCTPromiseRejectBlock
170
+ ) {
171
+ do {
172
+ let items = try Self.fm.contentsOfDirectory(atPath: path)
173
+ resolve(items)
174
+ } catch {
175
+ reject("ERR_READDIR", error.localizedDescription, error)
176
+ }
177
+ }
178
+
165
179
  @objc
166
180
  func fsMoveFile(
167
181
  _ src: String,
@@ -170,17 +184,53 @@ final class BundleDropModule: NSObject {
170
184
  reject: @escaping RCTPromiseRejectBlock
171
185
  ) {
172
186
  do {
173
- let destDir = (dest as NSString).deletingLastPathComponent
174
- if !Self.fm.fileExists(atPath: destDir) {
175
- try Self.fm.createDirectory(atPath: destDir, withIntermediateDirectories: true)
176
- }
177
- if Self.fm.fileExists(atPath: dest) {
178
- try Self.fm.removeItem(atPath: dest)
179
- }
180
- try Self.fm.moveItem(atPath: src, toPath: dest)
187
+ try BundleDropFileOps.moveFile(srcPath: src, destPath: dest)
181
188
  resolve(nil)
182
189
  } catch {
183
190
  reject("ERR_MOVE", error.localizedDescription, error)
184
191
  }
185
192
  }
193
+
194
+ @objc
195
+ func fsUnzip(
196
+ _ zipPath: String,
197
+ destPath: String,
198
+ resolve: @escaping RCTPromiseResolveBlock,
199
+ reject: @escaping RCTPromiseRejectBlock
200
+ ) {
201
+ DispatchQueue.global(qos: .userInitiated).async {
202
+ do {
203
+ let filenames = try BundleDropZipExtractor.extractZip(atPath: zipPath,
204
+ toDirectory: destPath)
205
+ resolve(filenames ?? [])
206
+ } catch {
207
+ reject("ERR_UNZIP", error.localizedDescription, error)
208
+ }
209
+ }
210
+ }
211
+
212
+ @objc
213
+ func fsDownloadFile(
214
+ _ url: String,
215
+ destPath: String,
216
+ resolve: @escaping RCTPromiseResolveBlock,
217
+ reject: @escaping RCTPromiseRejectBlock
218
+ ) {
219
+ let config = URLSessionConfiguration.default
220
+ config.timeoutIntervalForRequest = 30
221
+ config.timeoutIntervalForResource = 300
222
+
223
+ BundleDropFileOps.downloadToFile(
224
+ urlString: url,
225
+ destPath: destPath,
226
+ configuration: config
227
+ ) { result in
228
+ switch result {
229
+ case .success:
230
+ resolve(nil)
231
+ case .failure(let error):
232
+ reject("ERR_DOWNLOAD", error.localizedDescription, error)
233
+ }
234
+ }
235
+ }
186
236
  }
@@ -0,0 +1,112 @@
1
+ import Foundation
2
+
3
+ struct BundleDropOtaResolveResult {
4
+ let bundleURL: URL?
5
+ let clearedOta: Bool
6
+ let storedVersion: String
7
+ }
8
+
9
+ enum BundleDropOtaResolver {
10
+ static func readCurrentPointer(
11
+ bundleDropRoot: URL,
12
+ fileManager: FileManager = .default
13
+ ) -> URL? {
14
+ let current = bundleDropRoot.appendingPathComponent("current.json")
15
+ guard fileManager.fileExists(atPath: current.path) else { return nil }
16
+
17
+ do {
18
+ let data = try Data(contentsOf: current)
19
+ let obj = try JSONSerialization.jsonObject(with: data) as? [String: Any]
20
+ guard let bundlePath = obj?["bundlePath"] as? String, !bundlePath.isEmpty else {
21
+ return nil
22
+ }
23
+
24
+ let url = URL(fileURLWithPath: bundlePath)
25
+ return fileManager.fileExists(atPath: url.path) ? url : nil
26
+ } catch {
27
+ return nil
28
+ }
29
+ }
30
+
31
+ static func clearOtaState(
32
+ bundleDropRoot: URL,
33
+ documentsDirectory: URL,
34
+ fileManager: FileManager = .default
35
+ ) {
36
+ let filesToClear = [
37
+ bundleDropRoot.appendingPathComponent("current.json"),
38
+ bundleDropRoot.appendingPathComponent("previous.json"),
39
+ bundleDropRoot.appendingPathComponent("state.json"),
40
+ documentsDirectory.appendingPathComponent("bundle-info.json"),
41
+ ]
42
+
43
+ filesToClear.forEach { url in
44
+ do {
45
+ if fileManager.fileExists(atPath: url.path) {
46
+ try fileManager.removeItem(at: url)
47
+ }
48
+ } catch {
49
+ // Best effort cleanup; a single bad file should not block native fallback.
50
+ }
51
+ }
52
+ }
53
+
54
+ static func hasOtaState(
55
+ bundleDropRoot: URL,
56
+ documentsDirectory: URL,
57
+ fileManager: FileManager = .default
58
+ ) -> Bool {
59
+ [
60
+ bundleDropRoot.appendingPathComponent("current.json"),
61
+ bundleDropRoot.appendingPathComponent("previous.json"),
62
+ bundleDropRoot.appendingPathComponent("state.json"),
63
+ documentsDirectory.appendingPathComponent("bundle-info.json"),
64
+ ].contains { url in
65
+ fileManager.fileExists(atPath: url.path)
66
+ }
67
+ }
68
+
69
+ static func resolve(
70
+ bundleDropRoot: URL,
71
+ documentsDirectory: URL,
72
+ currentBinaryVersion: String,
73
+ storedBinaryVersion: String?,
74
+ fileManager: FileManager = .default
75
+ ) -> BundleDropOtaResolveResult {
76
+ if let storedBinaryVersion = storedBinaryVersion,
77
+ storedBinaryVersion != currentBinaryVersion {
78
+ let hadOtaState = hasOtaState(
79
+ bundleDropRoot: bundleDropRoot,
80
+ documentsDirectory: documentsDirectory,
81
+ fileManager: fileManager
82
+ )
83
+ clearOtaState(
84
+ bundleDropRoot: bundleDropRoot,
85
+ documentsDirectory: documentsDirectory,
86
+ fileManager: fileManager
87
+ )
88
+ return BundleDropOtaResolveResult(
89
+ bundleURL: nil,
90
+ clearedOta: hadOtaState,
91
+ storedVersion: currentBinaryVersion
92
+ )
93
+ }
94
+
95
+ guard let bundleURL = readCurrentPointer(
96
+ bundleDropRoot: bundleDropRoot,
97
+ fileManager: fileManager
98
+ ) else {
99
+ return BundleDropOtaResolveResult(
100
+ bundleURL: nil,
101
+ clearedOta: false,
102
+ storedVersion: currentBinaryVersion
103
+ )
104
+ }
105
+
106
+ return BundleDropOtaResolveResult(
107
+ bundleURL: bundleURL,
108
+ clearedOta: false,
109
+ storedVersion: currentBinaryVersion
110
+ )
111
+ }
112
+ }
@@ -0,0 +1,13 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ @interface BundleDropZipExtractor : NSObject
4
+ + (nullable NSArray<NSString *> *)extractZipAtPath:(nonnull NSString *)zipPath
5
+ toDirectory:(nonnull NSString *)destPath
6
+ error:(NSError * _Nullable * _Nullable)error;
7
+ + (nullable NSArray<NSString *> *)extractZipAtPath:(nonnull NSString *)zipPath
8
+ toDirectory:(nonnull NSString *)destPath
9
+ maxZipFileSize:(NSUInteger)maxZipFileSize
10
+ maxEntrySize:(NSUInteger)maxEntrySize
11
+ maxTotalUncompressed:(NSUInteger)maxTotalUncompressed
12
+ error:(NSError * _Nullable * _Nullable)error;
13
+ @end