@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.
- package/BundleDrop.podspec +2 -0
- package/README.md +8 -2
- package/android/build.gradle +91 -1
- package/android/gradle.properties +1 -0
- package/android/src/main/java/com/bundledrop/BundleDropFileOps.kt +131 -0
- package/android/src/main/java/com/bundledrop/BundleDropModule.kt +65 -89
- package/android/src/main/java/com/bundledrop/BundleDropOtaResolver.kt +93 -0
- package/ios/BundleDropBridge.m +14 -0
- package/ios/BundleDropFileOps.swift +220 -0
- package/ios/BundleDropLocator.h +11 -0
- package/ios/BundleDropLocator.swift +34 -45
- package/ios/BundleDropModule.swift +65 -15
- package/ios/BundleDropOtaResolver.swift +112 -0
- package/ios/BundleDropZipExtractor.h +13 -0
- package/ios/BundleDropZipExtractor.m +226 -0
- package/lib/CLI/cli.js +56 -6
- package/lib/CLI/scripts/aipowered/apply-plan.d.ts +10 -0
- package/lib/CLI/scripts/aipowered/apply-plan.js +50 -0
- package/lib/CLI/scripts/aipowered/backend-client.d.ts +6 -0
- package/lib/CLI/scripts/aipowered/backend-client.js +34 -0
- package/lib/CLI/scripts/aipowered/diff-preview.d.ts +7 -0
- package/lib/CLI/scripts/aipowered/diff-preview.js +33 -0
- package/lib/CLI/scripts/aipowered/init-native-config.d.ts +2 -0
- package/lib/CLI/scripts/aipowered/init-native-config.js +131 -0
- package/lib/CLI/scripts/aipowered/scanner.d.ts +5 -0
- package/lib/CLI/scripts/aipowered/scanner.js +349 -0
- package/lib/CLI/scripts/aipowered/types.d.ts +53 -0
- package/lib/CLI/scripts/aipowered/types.js +2 -0
- package/lib/CLI/scripts/aipowered/validate-plan.d.ts +8 -0
- package/lib/CLI/scripts/aipowered/validate-plan.js +164 -0
- package/lib/CLI/scripts/init-config.js +10 -5
- package/lib/CLI/scripts/init-native-config.js +1 -1
- package/lib/CLI/scripts/post-init.js +7 -18
- package/lib/CLI/scripts/upload-cli.d.ts +2 -0
- package/lib/CLI/scripts/upload-cli.js +67 -28
- package/lib/CLI/utils/ui.d.ts +3 -0
- package/lib/CLI/utils/ui.js +26 -0
- package/lib/api/clientApi.d.ts +2 -1
- package/lib/api/clientApi.js +9 -0
- package/lib/api/types.d.ts +21 -3
- package/lib/bundleInfo.d.ts +12 -0
- package/lib/context.d.ts +19 -2
- package/lib/context.js +5 -2
- package/lib/errors.d.ts +25 -0
- package/lib/errors.js +26 -1
- package/lib/fs/bundlePointer.d.ts +2 -0
- package/lib/fs/bundlePointer.js +14 -4
- package/lib/fs/fsUtils.d.ts +1 -2
- package/lib/fs/fsUtils.js +12 -27
- package/lib/fs/userProperties.d.ts +25 -4
- package/lib/fs/userProperties.js +51 -7
- package/lib/index.d.ts +7 -3
- package/lib/index.js +5 -1
- package/lib/injectImageResolver.js +6 -15
- package/lib/install/installFromZip.d.ts +1 -1
- package/lib/install/installFromZip.js +47 -42
- package/lib/loadConfig.d.ts +21 -2
- package/lib/loadConfig.js +11 -7
- package/lib/manager/downloadAndInstall.d.ts +3 -0
- package/lib/manager/downloadAndInstall.js +30 -28
- package/lib/manager/reporting.d.ts +2 -0
- package/lib/manager/reporting.js +27 -2
- package/lib/manager/rollbackState.d.ts +28 -11
- package/lib/manager/rollbackState.js +90 -38
- package/lib/manager/updateCheck.js +17 -3
- package/lib/manager/updateState.d.ts +5 -1
- package/lib/manager/updateState.js +20 -11
- package/lib/native/fs.d.ts +6 -0
- package/lib/native/fs.js +27 -0
- package/lib/runtime/initState.d.ts +2 -2
- package/lib/runtime/service.d.ts +28 -0
- package/lib/runtime/service.js +136 -10
- package/lib/scripts/bundle.d.ts +4 -1
- package/lib/scripts/bundle.js +42 -29
- package/lib/scripts/projectRoot.d.ts +1 -0
- package/lib/scripts/projectRoot.js +54 -0
- package/lib/scripts/resolve-ios-version.d.ts +2 -0
- package/lib/scripts/resolve-ios-version.js +74 -0
- package/lib/scripts/upload-bundle.d.ts +2 -1
- package/lib/scripts/upload-bundle.js +56 -41
- package/lib/types.d.ts +3 -0
- package/lib/useBundleDrop.d.ts +2 -0
- package/lib/useBundleDrop.js +1 -0
- 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
|
+
}
|
|
@@ -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
|
-
|
|
5
|
+
static let binaryVersionKey = "BundleDropBinaryVersion"
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
let version =
|
|
9
|
-
let build =
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
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:
|
|
95
|
+
let data = try Data(contentsOf: fileURL)
|
|
95
96
|
resolve(data.base64EncodedString())
|
|
96
97
|
} else {
|
|
97
|
-
let text = try String(
|
|
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:
|
|
125
|
+
try data.write(to: fileURL)
|
|
124
126
|
} else {
|
|
125
|
-
try content.write(
|
|
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
|
-
|
|
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
|
-
|
|
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
|