@gfean/react-native-bundle-drop 0.3.2 → 0.3.5
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 +4 -1
- package/LICENSE +15 -0
- package/README.md +120 -253
- 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.swift +33 -44
- 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 +15 -3
- package/lib/CLI/scripts/init-config.js +12 -5
- package/lib/CLI/scripts/init-native-config.js +17 -9
- package/lib/CLI/scripts/post-init.js +9 -3
- package/lib/CLI/scripts/upload-cli.js +26 -9
- package/lib/api/types.d.ts +4 -3
- package/lib/bundleInfo.d.ts +12 -0
- package/lib/context.d.ts +17 -0
- package/lib/context.js +3 -0
- package/lib/errors.d.ts +25 -0
- package/lib/errors.js +26 -1
- 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 +1 -0
- package/lib/install/installFromZip.d.ts +1 -1
- package/lib/install/installFromZip.js +47 -42
- package/lib/loadConfig.d.ts +19 -0
- package/lib/loadConfig.js +11 -7
- package/lib/manager/downloadAndInstall.js +7 -25
- package/lib/manager/reporting.js +2 -2
- package/lib/manager/rollbackState.d.ts +3 -3
- package/lib/manager/rollbackState.js +6 -19
- package/lib/manager/updateCheck.js +2 -2
- 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.js +2 -0
- package/lib/scripts/bundle.js +2 -2
- package/lib/scripts/resolve-ios-version.d.ts +2 -0
- package/lib/scripts/resolve-ios-version.js +74 -0
- package/lib/scripts/upload-bundle.js +10 -4
- package/lib/types.d.ts +3 -0
- package/package.json +53 -16
- package/CLIENT_README.md +0 -411
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
package com.bundledrop
|
|
2
2
|
|
|
3
|
-
import android.app.AlarmManager
|
|
4
|
-
import android.app.PendingIntent
|
|
5
3
|
import android.content.Context
|
|
6
4
|
import android.content.Intent
|
|
7
5
|
import android.util.Log
|
|
8
|
-
import com.facebook.react.ReactApplication
|
|
9
6
|
import com.facebook.react.bridge.*
|
|
10
7
|
import java.io.File
|
|
11
|
-
import android.os.Handler
|
|
12
|
-
import android.os.Looper
|
|
13
|
-
import com.facebook.react.bridge.JSBundleLoader
|
|
14
|
-
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
15
|
-
import org.json.JSONObject
|
|
16
8
|
|
|
17
9
|
class BundleDropModule(reactContext: ReactApplicationContext) :
|
|
18
10
|
ReactContextBaseJavaModule(reactContext) {
|
|
@@ -26,34 +18,6 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
|
|
|
26
18
|
private const val PREFS_NAME = "BundleDropPrefs"
|
|
27
19
|
private const val KEY_BINARY_VERSION = "binary_version"
|
|
28
20
|
|
|
29
|
-
private fun currentJsonPath(context: Context): File {
|
|
30
|
-
return File(context.filesDir, "bundle-drop/current.json")
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private fun readCurrentPointer(context: Context): String? {
|
|
34
|
-
return try {
|
|
35
|
-
val jsonFile = currentJsonPath(context)
|
|
36
|
-
if (!jsonFile.exists()) return null
|
|
37
|
-
val raw = jsonFile.readText()
|
|
38
|
-
val parsed = JSONObject(raw)
|
|
39
|
-
val path = parsed.optString("bundlePath", null)
|
|
40
|
-
if (path != null && path.isNotEmpty()) {
|
|
41
|
-
val file = File(path)
|
|
42
|
-
if (file.exists()) path else null
|
|
43
|
-
} else {
|
|
44
|
-
null
|
|
45
|
-
}
|
|
46
|
-
} catch (e: Exception) {
|
|
47
|
-
Log.w("BundleDrop", "Failed to read current.json: ${e.message}", e)
|
|
48
|
-
null
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private fun legacyBundlePath(context: Context): String? {
|
|
53
|
-
val file = File(context.filesDir, "main.jsbundle")
|
|
54
|
-
return if (file.exists()) file.absolutePath else null
|
|
55
|
-
}
|
|
56
|
-
|
|
57
21
|
private fun getBinaryVersionKey(context: Context): String {
|
|
58
22
|
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
|
|
59
23
|
val versionName = pInfo.versionName ?: "unknown"
|
|
@@ -76,43 +40,28 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
|
|
|
76
40
|
.edit().putString(KEY_BINARY_VERSION, version).apply()
|
|
77
41
|
}
|
|
78
42
|
|
|
79
|
-
private fun clearOtaState(context: Context) {
|
|
80
|
-
try {
|
|
81
|
-
val root = File(context.filesDir, "bundle-drop")
|
|
82
|
-
val filesToClear = listOf(
|
|
83
|
-
File(root, "current.json"),
|
|
84
|
-
File(root, "previous.json"),
|
|
85
|
-
File(root, "state.json"),
|
|
86
|
-
File(context.filesDir, "bundle-info.json"),
|
|
87
|
-
)
|
|
88
|
-
filesToClear.forEach { if (it.exists()) it.delete() }
|
|
89
|
-
} catch (e: Exception) {
|
|
90
|
-
Log.w("BundleDrop", "Failed to clear OTA state: ${e.message}", e)
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
43
|
fun getDownloadedBundlePath(context: Context): String? {
|
|
95
|
-
val
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
44
|
+
val bundleDropRoot = File(context.filesDir, "bundle-drop")
|
|
45
|
+
val result = BundleDropOtaResolver.resolve(
|
|
46
|
+
bundleDropRoot = bundleDropRoot,
|
|
47
|
+
filesDir = context.filesDir,
|
|
48
|
+
currentBinaryVersion = getBinaryVersionKey(context),
|
|
49
|
+
storedBinaryVersion = getStoredBinaryVersion(context),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
result.storedVersion?.let { setStoredBinaryVersion(context, it) }
|
|
53
|
+
|
|
54
|
+
if (result.bundlePath == null) {
|
|
55
|
+
if (result.clearedOta) {
|
|
56
|
+
Log.d("BundleDrop", "Binary updated, clearing OTA bundle")
|
|
57
|
+
} else {
|
|
58
|
+
Log.d("BundleDrop", "📦 No OTA bundle found.")
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
Log.d("BundleDrop", "🔁 Using OTA bundle at: ${result.bundlePath}")
|
|
111
62
|
}
|
|
112
63
|
|
|
113
|
-
|
|
114
|
-
Log.d("BundleDrop", "🔁 Using OTA bundle at: $path")
|
|
115
|
-
return path
|
|
64
|
+
return result.bundlePath
|
|
116
65
|
}
|
|
117
66
|
|
|
118
67
|
@JvmStatic
|
|
@@ -134,7 +83,6 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
|
|
|
134
83
|
|
|
135
84
|
@ReactMethod
|
|
136
85
|
fun restartReactNative() {
|
|
137
|
-
// ✅ Only change: don't access `currentActivity` directly; use reactApplicationContext.currentActivity
|
|
138
86
|
val activity = reactApplicationContext.currentActivity ?: return
|
|
139
87
|
val context = reactApplicationContext
|
|
140
88
|
|
|
@@ -174,15 +122,9 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
|
|
|
174
122
|
|
|
175
123
|
private fun readImageManifestRaw(): String? {
|
|
176
124
|
val context = reactApplicationContext
|
|
177
|
-
val
|
|
178
|
-
val
|
|
179
|
-
return
|
|
180
|
-
val file = File(bundleDir, "image-manifest.json")
|
|
181
|
-
if (file.exists()) file.readText() else null
|
|
182
|
-
} catch (e: Exception) {
|
|
183
|
-
Log.e("BundleDrop", "Failed to read manifest: ${e.message}", e)
|
|
184
|
-
null
|
|
185
|
-
}
|
|
125
|
+
val bundleDropRoot = File(context.filesDir, "bundle-drop")
|
|
126
|
+
val current = BundleDropOtaResolver.readCurrentPointer(bundleDropRoot) ?: return null
|
|
127
|
+
return BundleDropOtaResolver.readImageManifest(current)
|
|
186
128
|
}
|
|
187
129
|
|
|
188
130
|
// ---------------------------------------------------------------------------
|
|
@@ -241,11 +183,23 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
|
|
|
241
183
|
}
|
|
242
184
|
}
|
|
243
185
|
|
|
186
|
+
@ReactMethod
|
|
187
|
+
fun fsReadDir(path: String, promise: Promise) {
|
|
188
|
+
try {
|
|
189
|
+
val dir = File(path)
|
|
190
|
+
val names = dir.list() ?: emptyArray()
|
|
191
|
+
val result = Arguments.createArray()
|
|
192
|
+
names.forEach { result.pushString(it) }
|
|
193
|
+
promise.resolve(result)
|
|
194
|
+
} catch (e: Exception) {
|
|
195
|
+
promise.reject("ERR_READDIR", e.message, e)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
244
199
|
@ReactMethod
|
|
245
200
|
fun fsUnlink(path: String, promise: Promise) {
|
|
246
201
|
try {
|
|
247
|
-
|
|
248
|
-
if (file.exists()) file.delete()
|
|
202
|
+
BundleDropFileOps.unlinkPath(path)
|
|
249
203
|
promise.resolve(null)
|
|
250
204
|
} catch (e: Exception) {
|
|
251
205
|
promise.reject("ERR_UNLINK", e.message, e)
|
|
@@ -255,19 +209,41 @@ class BundleDropModule(reactContext: ReactApplicationContext) :
|
|
|
255
209
|
@ReactMethod
|
|
256
210
|
fun fsMoveFile(src: String, dest: String, promise: Promise) {
|
|
257
211
|
try {
|
|
258
|
-
|
|
259
|
-
val destFile = File(dest)
|
|
260
|
-
destFile.parentFile?.mkdirs()
|
|
261
|
-
if (!srcFile.renameTo(destFile)) {
|
|
262
|
-
srcFile.copyTo(destFile, overwrite = true)
|
|
263
|
-
srcFile.delete()
|
|
264
|
-
}
|
|
212
|
+
BundleDropFileOps.moveFile(File(src), File(dest))
|
|
265
213
|
promise.resolve(null)
|
|
266
214
|
} catch (e: Exception) {
|
|
267
215
|
promise.reject("ERR_MOVE", e.message, e)
|
|
268
216
|
}
|
|
269
217
|
}
|
|
270
218
|
|
|
219
|
+
@ReactMethod
|
|
220
|
+
fun fsUnzip(zipPath: String, destPath: String, promise: Promise) {
|
|
221
|
+
Thread {
|
|
222
|
+
try {
|
|
223
|
+
val filenames = BundleDropFileOps.unzipFile(zipPath, destPath)
|
|
224
|
+
val result = Arguments.createArray()
|
|
225
|
+
filenames.forEach { result.pushString(it) }
|
|
226
|
+
promise.resolve(result)
|
|
227
|
+
} catch (e: Exception) {
|
|
228
|
+
promise.reject("ERR_UNZIP", e.message, e)
|
|
229
|
+
}
|
|
230
|
+
}.start()
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
@ReactMethod
|
|
234
|
+
fun fsDownloadFile(url: String, destPath: String, promise: Promise) {
|
|
235
|
+
Thread {
|
|
236
|
+
try {
|
|
237
|
+
val parsedUrl = BundleDropFileOps.validateHttpUrl(url)
|
|
238
|
+
BundleDropFileOps.downloadToFile(parsedUrl, File(destPath))
|
|
239
|
+
promise.resolve(null)
|
|
240
|
+
} catch (e: Exception) {
|
|
241
|
+
try { File(destPath).delete() } catch (_: Exception) {}
|
|
242
|
+
promise.reject("ERR_DOWNLOAD", e.message, e)
|
|
243
|
+
}
|
|
244
|
+
}.start()
|
|
245
|
+
}
|
|
246
|
+
|
|
271
247
|
override fun getConstants(): MutableMap<String, Any> {
|
|
272
248
|
val map = mutableMapOf<String, Any>()
|
|
273
249
|
downloadedBundlePath?.let {
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
package com.bundledrop
|
|
2
|
+
|
|
3
|
+
import java.io.File
|
|
4
|
+
import org.json.JSONObject
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Pure OTA bundle resolution logic, decoupled from Android Context for testability.
|
|
8
|
+
* All file system operations go through the provided root directory.
|
|
9
|
+
*/
|
|
10
|
+
object BundleDropOtaResolver {
|
|
11
|
+
|
|
12
|
+
data class ResolveResult(
|
|
13
|
+
val bundlePath: String?,
|
|
14
|
+
val clearedOta: Boolean = false,
|
|
15
|
+
val storedVersion: String? = null,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
fun readCurrentPointer(bundleDropRoot: File): String? {
|
|
19
|
+
return try {
|
|
20
|
+
val jsonFile = File(bundleDropRoot, "current.json")
|
|
21
|
+
if (!jsonFile.exists()) return null
|
|
22
|
+
val raw = jsonFile.readText()
|
|
23
|
+
val parsed = JSONObject(raw)
|
|
24
|
+
val path = parsed.optString("bundlePath", null)
|
|
25
|
+
if (!path.isNullOrEmpty() && File(path).exists()) path else null
|
|
26
|
+
} catch (_: Exception) {
|
|
27
|
+
null
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
fun clearOtaState(bundleDropRoot: File, filesDir: File) {
|
|
32
|
+
val filesToClear = listOf(
|
|
33
|
+
File(bundleDropRoot, "current.json"),
|
|
34
|
+
File(bundleDropRoot, "previous.json"),
|
|
35
|
+
File(bundleDropRoot, "state.json"),
|
|
36
|
+
File(filesDir, "bundle-info.json"),
|
|
37
|
+
)
|
|
38
|
+
filesToClear.forEach {
|
|
39
|
+
try { if (it.exists()) it.delete() } catch (_: Exception) {}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
fun hasOtaState(bundleDropRoot: File, filesDir: File): Boolean {
|
|
44
|
+
return listOf(
|
|
45
|
+
File(bundleDropRoot, "current.json"),
|
|
46
|
+
File(bundleDropRoot, "previous.json"),
|
|
47
|
+
File(bundleDropRoot, "state.json"),
|
|
48
|
+
File(filesDir, "bundle-info.json"),
|
|
49
|
+
).any { it.exists() }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fun resolve(
|
|
53
|
+
bundleDropRoot: File,
|
|
54
|
+
filesDir: File,
|
|
55
|
+
currentBinaryVersion: String,
|
|
56
|
+
storedBinaryVersion: String?,
|
|
57
|
+
): ResolveResult {
|
|
58
|
+
if (storedBinaryVersion != null && storedBinaryVersion != currentBinaryVersion) {
|
|
59
|
+
val hadOtaState = hasOtaState(bundleDropRoot, filesDir)
|
|
60
|
+
clearOtaState(bundleDropRoot, filesDir)
|
|
61
|
+
return ResolveResult(
|
|
62
|
+
bundlePath = null,
|
|
63
|
+
clearedOta = hadOtaState,
|
|
64
|
+
storedVersion = currentBinaryVersion,
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
val path = readCurrentPointer(bundleDropRoot)
|
|
69
|
+
|
|
70
|
+
if (path == null) {
|
|
71
|
+
return ResolveResult(
|
|
72
|
+
bundlePath = null,
|
|
73
|
+
storedVersion = currentBinaryVersion,
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return ResolveResult(
|
|
78
|
+
bundlePath = path,
|
|
79
|
+
storedVersion = currentBinaryVersion,
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fun readImageManifest(bundlePath: String): String? {
|
|
84
|
+
return try {
|
|
85
|
+
val bundleDir = File(bundlePath).parentFile ?: return null
|
|
86
|
+
val file = File(bundleDir, "image-manifest.json")
|
|
87
|
+
if (file.exists()) file.readText() else null
|
|
88
|
+
} catch (_: Exception) {
|
|
89
|
+
null
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
}
|
package/ios/BundleDropBridge.m
CHANGED
|
@@ -31,11 +31,25 @@ RCT_EXTERN_METHOD(fsUnlink:(NSString *)path
|
|
|
31
31
|
resolve:(RCTPromiseResolveBlock)resolve
|
|
32
32
|
reject:(RCTPromiseRejectBlock)reject)
|
|
33
33
|
|
|
34
|
+
RCT_EXTERN_METHOD(fsReadDir:(NSString *)path
|
|
35
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
36
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
37
|
+
|
|
34
38
|
RCT_EXTERN_METHOD(fsMoveFile:(NSString *)src
|
|
35
39
|
dest:(NSString *)dest
|
|
36
40
|
resolve:(RCTPromiseResolveBlock)resolve
|
|
37
41
|
reject:(RCTPromiseRejectBlock)reject)
|
|
38
42
|
|
|
43
|
+
RCT_EXTERN_METHOD(fsUnzip:(NSString *)zipPath
|
|
44
|
+
destPath:(NSString *)destPath
|
|
45
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
46
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
47
|
+
|
|
48
|
+
RCT_EXTERN_METHOD(fsDownloadFile:(NSString *)url
|
|
49
|
+
destPath:(NSString *)destPath
|
|
50
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
51
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
52
|
+
|
|
39
53
|
+ (BOOL)requiresMainQueueSetup
|
|
40
54
|
{
|
|
41
55
|
return NO;
|
|
@@ -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
|
+
}
|
|
@@ -2,63 +2,52 @@ import Foundation
|
|
|
2
2
|
|
|
3
3
|
@objc 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 {
|