@gfean/react-native-bundle-drop 0.6.0 → 0.8.0
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 +96 -0
- package/README.md +249 -9
- package/android/build.gradle +72 -5
- package/android/src/main/java/com/bundledrop/BundleDropModule.kt +137 -13
- package/android/src/main/java/com/bundledrop/BundleDropNativePaths.kt +43 -13
- package/android/src/main/java/com/bundledrop/BundleDropOtaResolver.kt +35 -0
- package/android/src/main/java/com/bundledrop/BundleDropStartupRecovery.kt +1117 -0
- package/ios/BundleDropBridge.m +27 -0
- package/ios/BundleDropLocator.swift +31 -15
- package/ios/BundleDropModule.swift +106 -3
- package/ios/BundleDropOtaResolver.swift +137 -29
- package/ios/BundleDropStartupRecovery.swift +1108 -0
- package/ios/BundleDropStartupRecoveryAdapter.swift +301 -0
- package/lib/CLI/cli.js +9 -5
- package/lib/CLI/scripts/doctor.js +28 -9
- package/lib/CLI/scripts/expo/configure-expo.js +2 -1
- package/lib/CLI/scripts/init-config.d.ts +2 -2
- package/lib/CLI/scripts/init-config.js +23 -14
- package/lib/CLI/scripts/login-cli.js +2 -3
- package/lib/CLI/scripts/native/write-runtime-identity.d.ts +19 -0
- package/lib/CLI/scripts/native/write-runtime-identity.js +92 -0
- package/lib/CLI/scripts/sight-artifacts.d.ts +9 -1
- package/lib/CLI/scripts/sight-artifacts.js +20 -8
- package/lib/CLI/scripts/sight-assets.d.ts +15 -0
- package/lib/CLI/scripts/sight-assets.js +71 -0
- package/lib/CLI/scripts/sight-cli.d.ts +3 -0
- package/lib/CLI/scripts/sight-cli.js +50 -4
- package/lib/CLI/scripts/sight-compare/assets.d.ts +6 -0
- package/lib/CLI/scripts/sight-compare/assets.js +73 -0
- package/lib/CLI/scripts/sight-compare/dependencies.d.ts +22 -0
- package/lib/CLI/scripts/sight-compare/dependencies.js +289 -0
- package/lib/CLI/scripts/sight-compare/git.d.ts +22 -0
- package/lib/CLI/scripts/sight-compare/git.js +116 -0
- package/lib/CLI/scripts/sight-compare/inspect-project.d.ts +37 -0
- package/lib/CLI/scripts/sight-compare/inspect-project.js +191 -0
- package/lib/CLI/scripts/sight-compare/metadata.d.ts +4 -0
- package/lib/CLI/scripts/sight-compare/metadata.js +39 -0
- package/lib/CLI/scripts/sight-compare/process.d.ts +22 -0
- package/lib/CLI/scripts/sight-compare/process.js +221 -0
- package/lib/CLI/scripts/sight-compare/run.d.ts +3 -0
- package/lib/CLI/scripts/sight-compare/run.js +290 -0
- package/lib/CLI/scripts/sight-compare/snapshot.d.ts +26 -0
- package/lib/CLI/scripts/sight-compare/snapshot.js +279 -0
- package/lib/CLI/scripts/sight-compare/types.d.ts +87 -0
- package/lib/CLI/scripts/sight-compare/types.js +2 -0
- package/lib/CLI/scripts/sight-ota.d.ts +18 -0
- package/lib/CLI/scripts/sight-ota.js +260 -0
- package/lib/CLI/scripts/sight-session.d.ts +9 -2
- package/lib/CLI/scripts/sight-session.js +58 -20
- package/lib/CLI/scripts/upload-cli.d.ts +12 -2
- package/lib/CLI/scripts/upload-cli.js +74 -11
- package/lib/CLI/serverUrl.d.ts +3 -0
- package/lib/CLI/serverUrl.js +31 -0
- package/lib/bundleInfo.d.ts +2 -0
- package/lib/bundleInfo.js +16 -1
- package/lib/context.js +12 -2
- package/lib/fs/bundlePointer.d.ts +1 -17
- package/lib/fs/bundlePointer.js +5 -93
- package/lib/install/bundleInstallShared.js +4 -4
- package/lib/manager/downloadAndInstall.js +25 -22
- package/lib/manager/reporting.d.ts +8 -1
- package/lib/manager/reporting.js +17 -22
- package/lib/manager/rollbackState.d.ts +18 -54
- package/lib/manager/rollbackState.js +115 -216
- package/lib/manager/updateCheck.js +9 -3
- package/lib/manager/updateState.d.ts +2 -1
- package/lib/manager/updateState.js +59 -6
- package/lib/metro.d.ts +1 -1
- package/lib/metro.js +4 -3
- package/lib/native/bundleDropNative.d.ts +62 -0
- package/lib/native/bundleDropNative.js +266 -1
- package/lib/patch-engine/installFromPatchSet.js +2 -2
- package/lib/runtime/service.js +29 -71
- package/lib/runtime-delivery/bootstrapConfig.d.ts +29 -11
- package/lib/runtime-delivery/bootstrapConfig.js +83 -22
- package/lib/runtime-delivery/manifestState.d.ts +3 -1
- package/lib/runtime-delivery/manifestState.js +22 -1
- package/lib/runtime-delivery/manifestVerifier.js +7 -1
- package/lib/scripts/bundle.d.ts +12 -2
- package/lib/scripts/bundle.js +74 -21
- package/lib/scripts/download-bundle.d.ts +1 -0
- package/lib/scripts/download-bundle.js +2 -2
- package/lib/scripts/resolveModule.d.ts +2 -0
- package/lib/scripts/resolveModule.js +5 -0
- package/package.json +5 -2
- package/plugin/xcodeBuildPhase.js +2 -1
package/ios/BundleDropBridge.m
CHANGED
|
@@ -5,6 +5,33 @@
|
|
|
5
5
|
RCT_EXTERN_METHOD(getDownloadedBundlePath:(RCTPromiseResolveBlock)resolve
|
|
6
6
|
rejecter:(RCTPromiseRejectBlock)reject)
|
|
7
7
|
|
|
8
|
+
RCT_EXTERN_METHOD(activateStartupCandidate:(NSString *)hash
|
|
9
|
+
maxCrashCount:(nonnull NSNumber *)maxCrashCount
|
|
10
|
+
healthCheckMode:(NSString *)healthCheckMode
|
|
11
|
+
healthyAfterSec:(nonnull NSNumber *)healthyAfterSec
|
|
12
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
13
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
14
|
+
|
|
15
|
+
RCT_EXTERN_METHOD(markStartupHealthy:(NSString *)hash
|
|
16
|
+
attemptId:(NSString *)attemptId
|
|
17
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
18
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
19
|
+
|
|
20
|
+
RCT_EXTERN_METHOD(getStartupRecoveryState:(RCTPromiseResolveBlock)resolve
|
|
21
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
22
|
+
|
|
23
|
+
RCT_EXTERN_METHOD(setStartupRecoveryRevokedHashes:(NSArray<NSString *> *)hashes
|
|
24
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
25
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
26
|
+
|
|
27
|
+
RCT_EXTERN_METHOD(acknowledgeStartupRecovery:(NSString *)eventId
|
|
28
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
29
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
30
|
+
|
|
31
|
+
RCT_EXTERN_METHOD(rollbackStartupBundle:(BOOL)forceEmbedded
|
|
32
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
33
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
34
|
+
|
|
8
35
|
RCT_EXTERN_METHOD(restartReactNative)
|
|
9
36
|
|
|
10
37
|
RCT_EXTERN_METHOD(setOtaEnabled:(BOOL)enabled
|
|
@@ -6,6 +6,7 @@ import Foundation
|
|
|
6
6
|
static let runtimeVersionInfoKey = "BundleDropRuntimeVersion"
|
|
7
7
|
static let expoEnabledInfoKey = "BundleDropExpoEnabled"
|
|
8
8
|
static let embeddedBuildIdentityFilename = ".bundle-drop-build-identity.json"
|
|
9
|
+
static let bareEmbeddedBuildIdentityFilename = "bundle-drop-build-identity.json"
|
|
9
10
|
|
|
10
11
|
private struct EmbeddedBuildIdentity: Decodable {
|
|
11
12
|
let schemaVersion: Int
|
|
@@ -44,20 +45,23 @@ import Foundation
|
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
private static func getEmbeddedRuntimeVersion(bundle: Bundle) -> String? {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
for filename in [embeddedBuildIdentityFilename, bareEmbeddedBuildIdentityFilename] {
|
|
49
|
+
let candidateURL = bundle.bundleURL.appendingPathComponent(filename)
|
|
50
|
+
guard let attributes = try? FileManager.default.attributesOfItem(atPath: candidateURL.path),
|
|
51
|
+
let fileType = attributes[.type] as? FileAttributeType,
|
|
52
|
+
fileType == .typeRegular,
|
|
53
|
+
let fileSize = attributes[.size] as? NSNumber,
|
|
54
|
+
fileSize.intValue > 0,
|
|
55
|
+
fileSize.intValue <= 64 * 1024,
|
|
56
|
+
let data = try? Data(contentsOf: candidateURL),
|
|
57
|
+
let candidate = try? JSONDecoder().decode(EmbeddedBuildIdentity.self, from: data),
|
|
58
|
+
candidate.schemaVersion == 1,
|
|
59
|
+
candidate.platform == "ios" else {
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
return normalizeRuntimeVersion(candidate.runtimeVersion)
|
|
59
63
|
}
|
|
60
|
-
return
|
|
64
|
+
return nil
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
private static func normalizeRuntimeVersion(_ runtimeVersion: String?) -> String? {
|
|
@@ -68,9 +72,14 @@ import Foundation
|
|
|
68
72
|
|
|
69
73
|
@objc public static func bundleURL() -> URL? {
|
|
70
74
|
guard hasRuntimeIdentityForOta() else {
|
|
75
|
+
BundleDropStartupRecoveryAdapter.clearCapturedSelection()
|
|
71
76
|
print("BundleDrop: Expo runtime identity is missing; using the embedded bundle")
|
|
72
77
|
return nil
|
|
73
78
|
}
|
|
79
|
+
guard isOtaEnabled() else {
|
|
80
|
+
BundleDropStartupRecoveryAdapter.clearCapturedSelection()
|
|
81
|
+
return nil
|
|
82
|
+
}
|
|
74
83
|
let fm = FileManager.default
|
|
75
84
|
guard let lib = fm.urls(for: .libraryDirectory, in: .userDomainMask).first else { return nil }
|
|
76
85
|
guard let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
|
|
@@ -80,7 +89,10 @@ import Foundation
|
|
|
80
89
|
return bundleURL(
|
|
81
90
|
bundleDropRoot: root,
|
|
82
91
|
documentsDirectory: docs,
|
|
83
|
-
currentBinaryVersion: currentVersion
|
|
92
|
+
currentBinaryVersion: currentVersion,
|
|
93
|
+
startupSelection: {
|
|
94
|
+
BundleDropStartupRecoveryAdapter.selectStartupBundle(bundleDropRoot: root)
|
|
95
|
+
}
|
|
84
96
|
)
|
|
85
97
|
}
|
|
86
98
|
|
|
@@ -91,7 +103,8 @@ import Foundation
|
|
|
91
103
|
userDefaults: UserDefaults = .standard,
|
|
92
104
|
fileManager: FileManager = .default,
|
|
93
105
|
shouldLogBinaryUpdate: Bool = true,
|
|
94
|
-
log: (String) -> Void = { print($0) }
|
|
106
|
+
log: (String) -> Void = { print($0) },
|
|
107
|
+
startupSelection: (() -> URL?)? = nil
|
|
95
108
|
) -> URL? {
|
|
96
109
|
// Single gate for public and internal entrypoints (tests inject `userDefaults`).
|
|
97
110
|
if !isOtaEnabled(userDefaults: userDefaults) { return nil }
|
|
@@ -109,6 +122,9 @@ import Foundation
|
|
|
109
122
|
}
|
|
110
123
|
|
|
111
124
|
userDefaults.set(result.storedVersion, forKey: binaryVersionKey)
|
|
125
|
+
if let startupSelection {
|
|
126
|
+
return startupSelection()
|
|
127
|
+
}
|
|
112
128
|
return result.bundleURL
|
|
113
129
|
}
|
|
114
130
|
|
|
@@ -30,7 +30,7 @@ final class BundleDropModule: NSObject {
|
|
|
30
30
|
_ resolve: @escaping RCTPromiseResolveBlock,
|
|
31
31
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
32
32
|
) {
|
|
33
|
-
if let url =
|
|
33
|
+
if let url = BundleDropStartupRecoveryAdapter.downloadedBundleURL() {
|
|
34
34
|
print("📦 Found downloaded bundle at: \(url.path)")
|
|
35
35
|
resolve(url.path)
|
|
36
36
|
return
|
|
@@ -39,6 +39,99 @@ final class BundleDropModule: NSObject {
|
|
|
39
39
|
resolve(nil)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
@objc
|
|
43
|
+
func activateStartupCandidate(
|
|
44
|
+
_ hash: String,
|
|
45
|
+
maxCrashCount: NSNumber,
|
|
46
|
+
healthCheckMode: String,
|
|
47
|
+
healthyAfterSec: NSNumber,
|
|
48
|
+
resolve: @escaping RCTPromiseResolveBlock,
|
|
49
|
+
reject: @escaping RCTPromiseRejectBlock
|
|
50
|
+
) {
|
|
51
|
+
runFileOperation(errorCode: "ERR_STARTUP_RECOVERY_ACTIVATE", resolve: resolve, reject: reject) {
|
|
52
|
+
let crashCount = maxCrashCount.doubleValue
|
|
53
|
+
let healthDelay = healthyAfterSec.doubleValue
|
|
54
|
+
guard crashCount.isFinite,
|
|
55
|
+
crashCount >= 0,
|
|
56
|
+
crashCount.rounded(.towardZero) == crashCount,
|
|
57
|
+
crashCount <= Double(Int32.max),
|
|
58
|
+
healthDelay.isFinite,
|
|
59
|
+
healthDelay >= 0 else {
|
|
60
|
+
throw BundleDropStartupRecoveryError.invalidPolicy
|
|
61
|
+
}
|
|
62
|
+
let result = try BundleDropStartupRecoveryAdapter.activateCandidate(
|
|
63
|
+
hash: hash,
|
|
64
|
+
maxCrashCount: Int(crashCount),
|
|
65
|
+
healthCheckMode: healthCheckMode,
|
|
66
|
+
healthyAfterSec: healthDelay
|
|
67
|
+
)
|
|
68
|
+
return ["hash": result.hash, "bundlePath": result.bundleURL.path]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@objc
|
|
73
|
+
func markStartupHealthy(
|
|
74
|
+
_ hash: String,
|
|
75
|
+
attemptId: String,
|
|
76
|
+
resolve: @escaping RCTPromiseResolveBlock,
|
|
77
|
+
reject: @escaping RCTPromiseRejectBlock
|
|
78
|
+
) {
|
|
79
|
+
runFileOperation(errorCode: "ERR_STARTUP_RECOVERY_HEALTH", resolve: resolve, reject: reject) {
|
|
80
|
+
BundleDropStartupRecoveryAdapter.markHealthy(hash: hash, attemptId: attemptId)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@objc
|
|
85
|
+
func getStartupRecoveryState(
|
|
86
|
+
_ resolve: @escaping RCTPromiseResolveBlock,
|
|
87
|
+
reject: @escaping RCTPromiseRejectBlock
|
|
88
|
+
) {
|
|
89
|
+
runFileOperation(errorCode: "ERR_STARTUP_RECOVERY_STATE", resolve: resolve, reject: reject) {
|
|
90
|
+
try BundleDropStartupRecoveryAdapter.snapshot()
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@objc
|
|
95
|
+
func setStartupRecoveryRevokedHashes(
|
|
96
|
+
_ hashes: [String],
|
|
97
|
+
resolve: @escaping RCTPromiseResolveBlock,
|
|
98
|
+
reject: @escaping RCTPromiseRejectBlock
|
|
99
|
+
) {
|
|
100
|
+
runFileOperation(errorCode: "ERR_STARTUP_RECOVERY_REVOKE", resolve: resolve, reject: reject) {
|
|
101
|
+
try BundleDropStartupRecoveryAdapter.setRevokedHashes(hashes)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@objc
|
|
106
|
+
func acknowledgeStartupRecovery(
|
|
107
|
+
_ eventId: String,
|
|
108
|
+
resolve: @escaping RCTPromiseResolveBlock,
|
|
109
|
+
reject: @escaping RCTPromiseRejectBlock
|
|
110
|
+
) {
|
|
111
|
+
runFileOperation(errorCode: "ERR_STARTUP_RECOVERY_ACK", resolve: resolve, reject: reject) {
|
|
112
|
+
try BundleDropStartupRecoveryAdapter.acknowledgeRecovery(eventId: eventId)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@objc
|
|
117
|
+
func rollbackStartupBundle(
|
|
118
|
+
_ forceEmbedded: Bool,
|
|
119
|
+
resolve: @escaping RCTPromiseResolveBlock,
|
|
120
|
+
reject: @escaping RCTPromiseRejectBlock
|
|
121
|
+
) {
|
|
122
|
+
runFileOperation(errorCode: "ERR_STARTUP_RECOVERY_ROLLBACK", resolve: resolve, reject: reject) {
|
|
123
|
+
let result = try BundleDropStartupRecoveryAdapter.rollback(forceEmbedded: forceEmbedded)
|
|
124
|
+
var response: [String: Any] = [
|
|
125
|
+
"rolledBack": result.rolledBack,
|
|
126
|
+
"toEmbedded": result.toEmbedded,
|
|
127
|
+
]
|
|
128
|
+
if let hash = result.hash {
|
|
129
|
+
response["hash"] = hash
|
|
130
|
+
}
|
|
131
|
+
return response
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
42
135
|
@objc
|
|
43
136
|
func setOtaEnabled(
|
|
44
137
|
_ enabled: Bool,
|
|
@@ -57,17 +150,22 @@ final class BundleDropModule: NSObject {
|
|
|
57
150
|
return
|
|
58
151
|
}
|
|
59
152
|
|
|
60
|
-
guard let
|
|
153
|
+
guard let stagedURL = BundleDropStartupRecoveryAdapter.downloadedBundleURL() else {
|
|
61
154
|
print("⚠️ No downloaded bundle found; skipping restart.")
|
|
62
155
|
return
|
|
63
156
|
}
|
|
64
157
|
|
|
65
|
-
let size = BundleDropLocatorCore.fileSize(at:
|
|
158
|
+
let size = BundleDropLocatorCore.fileSize(at: stagedURL)
|
|
66
159
|
if size < 1024 {
|
|
67
160
|
print("⚠️ Bundle exists but looks invalid (size=\(size)). Skipping restart.")
|
|
68
161
|
return
|
|
69
162
|
}
|
|
70
163
|
|
|
164
|
+
guard let url = BundleDropStartupRecoveryAdapter.beginReload() else {
|
|
165
|
+
print("⚠️ Downloaded bundle became unavailable; skipping restart.")
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
|
|
71
169
|
Self.isReloading = true
|
|
72
170
|
print("🔄 Restarting RN from \(url.path) size=\(size)")
|
|
73
171
|
|
|
@@ -87,9 +185,14 @@ final class BundleDropModule: NSObject {
|
|
|
87
185
|
let fm = FileManager.default
|
|
88
186
|
let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first?.path ?? ""
|
|
89
187
|
let lib = fm.urls(for: .libraryDirectory, in: .userDomainMask).first?.path ?? ""
|
|
188
|
+
let attempt = BundleDropStartupRecoveryAdapter.capturedAttempt()
|
|
90
189
|
return [
|
|
91
190
|
"DocumentDirectoryPath": docs,
|
|
92
191
|
"LibraryDirectoryPath": lib,
|
|
192
|
+
"startupRecoveryProtocolVersion": BundleDropStartupRecoveryAdapter.protocolVersion,
|
|
193
|
+
"startupRecoverySelectedHash": BundleDropStartupRecoveryAdapter.capturedSelectedHash() ?? NSNull(),
|
|
194
|
+
"startupRecoveryAttemptHash": attempt.hash ?? NSNull(),
|
|
195
|
+
"startupRecoveryAttemptId": attempt.attemptId ?? NSNull(),
|
|
93
196
|
]
|
|
94
197
|
}
|
|
95
198
|
|
|
@@ -7,50 +7,152 @@ struct BundleDropOtaResolveResult {
|
|
|
7
7
|
let storedVersion: String
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
struct BundleDropOtaPointer {
|
|
11
|
+
let hash: String
|
|
12
|
+
let bundleURL: URL
|
|
13
|
+
let runtimeVersion: String
|
|
14
|
+
}
|
|
15
|
+
|
|
10
16
|
enum BundleDropOtaResolver {
|
|
11
17
|
static func readCurrentPointer(
|
|
12
18
|
bundleDropRoot: URL,
|
|
19
|
+
expectedRuntimeVersion: String? = nil,
|
|
13
20
|
fileManager: FileManager = .default
|
|
14
21
|
) -> URL? {
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
readPointer(
|
|
23
|
+
named: "current.json",
|
|
24
|
+
bundleDropRoot: bundleDropRoot,
|
|
25
|
+
expectedRuntimeVersion: expectedRuntimeVersion,
|
|
26
|
+
fileManager: fileManager
|
|
27
|
+
)?.bundleURL
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static func readPointer(
|
|
31
|
+
named filename: String,
|
|
32
|
+
bundleDropRoot: URL,
|
|
33
|
+
expectedRuntimeVersion: String? = nil,
|
|
34
|
+
fileManager: FileManager = .default
|
|
35
|
+
) -> BundleDropOtaPointer? {
|
|
36
|
+
let pointerURL = bundleDropRoot.appendingPathComponent(filename)
|
|
37
|
+
guard fileManager.fileExists(atPath: pointerURL.path) else { return nil }
|
|
17
38
|
|
|
18
39
|
do {
|
|
19
|
-
let data = try Data(contentsOf:
|
|
40
|
+
let data = try Data(contentsOf: pointerURL)
|
|
20
41
|
let obj = try JSONSerialization.jsonObject(with: data) as? [String: Any]
|
|
21
42
|
guard let hash = obj?["hash"] as? String,
|
|
22
43
|
hash.range(of: "^[a-f0-9]{64}$", options: .regularExpression) != nil else {
|
|
23
44
|
return nil
|
|
24
45
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
.appendingPathComponent("main.jsbundle")
|
|
30
|
-
let manifestURL = expectedURL
|
|
31
|
-
.deletingLastPathComponent()
|
|
32
|
-
.appendingPathComponent("bundle-manifest.json")
|
|
33
|
-
guard fileManager.fileExists(atPath: manifestURL.path),
|
|
34
|
-
let manifestData = try? Data(contentsOf: manifestURL),
|
|
35
|
-
let manifest = try? JSONSerialization.jsonObject(with: manifestData) as? [String: Any],
|
|
36
|
-
(manifest["manifestVersion"] as? NSNumber)?.intValue == 1,
|
|
37
|
-
manifest["bundleHash"] as? String == hash else {
|
|
38
|
-
return nil
|
|
39
|
-
}
|
|
40
|
-
guard verifyBundleDir(
|
|
41
|
-
bundleDir: expectedURL.deletingLastPathComponent(),
|
|
42
|
-
manifest: manifest,
|
|
43
|
-
expectedHash: hash,
|
|
46
|
+
return verifiedBundle(
|
|
47
|
+
hash: hash,
|
|
48
|
+
bundleDropRoot: bundleDropRoot,
|
|
49
|
+
expectedRuntimeVersion: expectedRuntimeVersion,
|
|
44
50
|
fileManager: fileManager
|
|
45
|
-
)
|
|
46
|
-
return nil
|
|
47
|
-
}
|
|
48
|
-
return fileManager.fileExists(atPath: expectedURL.path) ? expectedURL : nil
|
|
51
|
+
)
|
|
49
52
|
} catch {
|
|
50
53
|
return nil
|
|
51
54
|
}
|
|
52
55
|
}
|
|
53
56
|
|
|
57
|
+
static func readBundle(
|
|
58
|
+
hash: String,
|
|
59
|
+
bundleDropRoot: URL,
|
|
60
|
+
expectedRuntimeVersion: String? = nil,
|
|
61
|
+
fileManager: FileManager = .default
|
|
62
|
+
) -> BundleDropOtaPointer? {
|
|
63
|
+
guard hash.range(of: "^[a-f0-9]{64}$", options: .regularExpression) != nil else {
|
|
64
|
+
return nil
|
|
65
|
+
}
|
|
66
|
+
return verifiedBundle(
|
|
67
|
+
hash: hash,
|
|
68
|
+
bundleDropRoot: bundleDropRoot,
|
|
69
|
+
expectedRuntimeVersion: expectedRuntimeVersion,
|
|
70
|
+
fileManager: fileManager
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private static func verifiedBundle(
|
|
75
|
+
hash: String,
|
|
76
|
+
bundleDropRoot: URL,
|
|
77
|
+
expectedRuntimeVersion: String?,
|
|
78
|
+
fileManager: FileManager
|
|
79
|
+
) -> BundleDropOtaPointer? {
|
|
80
|
+
let bundleURL = bundleDropRoot
|
|
81
|
+
.appendingPathComponent("bundles")
|
|
82
|
+
.appendingPathComponent(hash)
|
|
83
|
+
.appendingPathComponent("main.jsbundle")
|
|
84
|
+
let manifestURL = bundleURL
|
|
85
|
+
.deletingLastPathComponent()
|
|
86
|
+
.appendingPathComponent("bundle-manifest.json")
|
|
87
|
+
guard fileManager.fileExists(atPath: manifestURL.path),
|
|
88
|
+
let manifestData = try? Data(contentsOf: manifestURL),
|
|
89
|
+
let manifest = try? JSONSerialization.jsonObject(with: manifestData) as? [String: Any],
|
|
90
|
+
(manifest["manifestVersion"] as? NSNumber)?.intValue == 1,
|
|
91
|
+
manifest["bundleHash"] as? String == hash,
|
|
92
|
+
let runtimeVersion = manifest["runtimeVersion"] as? String,
|
|
93
|
+
expectedRuntimeVersion == nil || runtimeVersion == expectedRuntimeVersion,
|
|
94
|
+
verifyBundleDir(
|
|
95
|
+
bundleDir: bundleURL.deletingLastPathComponent(),
|
|
96
|
+
manifest: manifest,
|
|
97
|
+
expectedHash: hash,
|
|
98
|
+
fileManager: fileManager
|
|
99
|
+
),
|
|
100
|
+
fileManager.fileExists(atPath: bundleURL.path) else {
|
|
101
|
+
return nil
|
|
102
|
+
}
|
|
103
|
+
return BundleDropOtaPointer(hash: hash, bundleURL: bundleURL, runtimeVersion: runtimeVersion)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static func writePointer(
|
|
107
|
+
named filename: String,
|
|
108
|
+
hash: String,
|
|
109
|
+
bundleDropRoot: URL,
|
|
110
|
+
fileManager: FileManager = .default
|
|
111
|
+
) throws {
|
|
112
|
+
guard hash.range(of: "^[a-f0-9]{64}$", options: .regularExpression) != nil else {
|
|
113
|
+
throw NSError(
|
|
114
|
+
domain: "BundleDropStartupRecovery",
|
|
115
|
+
code: 1,
|
|
116
|
+
userInfo: [NSLocalizedDescriptionKey: "Bundle pointer hash is invalid"]
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
try fileManager.createDirectory(at: bundleDropRoot, withIntermediateDirectories: true)
|
|
120
|
+
let pointerURL = bundleDropRoot.appendingPathComponent(filename)
|
|
121
|
+
let pointer: [String: Any] = [
|
|
122
|
+
"hash": hash,
|
|
123
|
+
"updatedAt": ISO8601DateFormatter().string(from: Date()),
|
|
124
|
+
]
|
|
125
|
+
let temporaryURL = bundleDropRoot.appendingPathComponent(
|
|
126
|
+
".\(filename)-\(UUID().uuidString).tmp"
|
|
127
|
+
)
|
|
128
|
+
do {
|
|
129
|
+
try JSONSerialization.data(withJSONObject: pointer, options: [.sortedKeys])
|
|
130
|
+
.write(to: temporaryURL)
|
|
131
|
+
let handle = try FileHandle(forWritingTo: temporaryURL)
|
|
132
|
+
try handle.synchronize()
|
|
133
|
+
try handle.close()
|
|
134
|
+
if fileManager.fileExists(atPath: pointerURL.path) {
|
|
135
|
+
_ = try fileManager.replaceItemAt(pointerURL, withItemAt: temporaryURL)
|
|
136
|
+
} else {
|
|
137
|
+
try fileManager.moveItem(at: temporaryURL, to: pointerURL)
|
|
138
|
+
}
|
|
139
|
+
} catch {
|
|
140
|
+
try? fileManager.removeItem(at: temporaryURL)
|
|
141
|
+
throw error
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static func deletePointer(
|
|
146
|
+
named filename: String,
|
|
147
|
+
bundleDropRoot: URL,
|
|
148
|
+
fileManager: FileManager = .default
|
|
149
|
+
) throws {
|
|
150
|
+
let pointerURL = bundleDropRoot.appendingPathComponent(filename)
|
|
151
|
+
if fileManager.fileExists(atPath: pointerURL.path) {
|
|
152
|
+
try fileManager.removeItem(at: pointerURL)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
54
156
|
private static func verifyBundleDir(
|
|
55
157
|
bundleDir: URL,
|
|
56
158
|
manifest: [String: Any],
|
|
@@ -174,27 +276,32 @@ enum BundleDropOtaResolver {
|
|
|
174
276
|
return String(encoded.dropFirst().dropLast()).replacingOccurrences(of: "\\/", with: "/")
|
|
175
277
|
}
|
|
176
278
|
|
|
279
|
+
@discardableResult
|
|
177
280
|
static func clearOtaState(
|
|
178
281
|
bundleDropRoot: URL,
|
|
179
282
|
documentsDirectory: URL,
|
|
180
283
|
fileManager: FileManager = .default
|
|
181
|
-
) {
|
|
284
|
+
) -> Bool {
|
|
182
285
|
let filesToClear = [
|
|
183
286
|
bundleDropRoot.appendingPathComponent("current.json"),
|
|
184
287
|
bundleDropRoot.appendingPathComponent("previous.json"),
|
|
185
288
|
bundleDropRoot.appendingPathComponent("state.json"),
|
|
289
|
+
bundleDropRoot.appendingPathComponent("recovery-ledger.json"),
|
|
186
290
|
documentsDirectory.appendingPathComponent("bundle-info.json"),
|
|
187
291
|
]
|
|
188
292
|
|
|
293
|
+
var clearedAllFiles = true
|
|
189
294
|
filesToClear.forEach { url in
|
|
190
295
|
do {
|
|
191
296
|
if fileManager.fileExists(atPath: url.path) {
|
|
192
297
|
try fileManager.removeItem(at: url)
|
|
193
298
|
}
|
|
194
299
|
} catch {
|
|
300
|
+
clearedAllFiles = false
|
|
195
301
|
// Best effort cleanup; a single bad file should not block native fallback.
|
|
196
302
|
}
|
|
197
303
|
}
|
|
304
|
+
return clearedAllFiles
|
|
198
305
|
}
|
|
199
306
|
|
|
200
307
|
static func hasOtaState(
|
|
@@ -206,6 +313,7 @@ enum BundleDropOtaResolver {
|
|
|
206
313
|
bundleDropRoot.appendingPathComponent("current.json"),
|
|
207
314
|
bundleDropRoot.appendingPathComponent("previous.json"),
|
|
208
315
|
bundleDropRoot.appendingPathComponent("state.json"),
|
|
316
|
+
bundleDropRoot.appendingPathComponent("recovery-ledger.json"),
|
|
209
317
|
documentsDirectory.appendingPathComponent("bundle-info.json"),
|
|
210
318
|
].contains { url in
|
|
211
319
|
fileManager.fileExists(atPath: url.path)
|
|
@@ -226,7 +334,7 @@ enum BundleDropOtaResolver {
|
|
|
226
334
|
documentsDirectory: documentsDirectory,
|
|
227
335
|
fileManager: fileManager
|
|
228
336
|
)
|
|
229
|
-
clearOtaState(
|
|
337
|
+
let clearedAllFiles = clearOtaState(
|
|
230
338
|
bundleDropRoot: bundleDropRoot,
|
|
231
339
|
documentsDirectory: documentsDirectory,
|
|
232
340
|
fileManager: fileManager
|
|
@@ -234,7 +342,7 @@ enum BundleDropOtaResolver {
|
|
|
234
342
|
return BundleDropOtaResolveResult(
|
|
235
343
|
bundleURL: nil,
|
|
236
344
|
clearedOta: hadOtaState,
|
|
237
|
-
storedVersion: currentBinaryVersion
|
|
345
|
+
storedVersion: clearedAllFiles ? currentBinaryVersion : storedBinaryVersion
|
|
238
346
|
)
|
|
239
347
|
}
|
|
240
348
|
|