@capgo/capacitor-updater 8.51.8 → 8.51.9

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.
@@ -141,8 +141,43 @@ public struct CryptoCipher {
141
141
  }
142
142
  }
143
143
 
144
+ private static let twoGiB: UInt64 = 2 * 1024 * 1024 * 1024
145
+ private static let threeGiB: UInt64 = 3 * 1024 * 1024 * 1024
146
+ private static let fourGiB: UInt64 = 4 * 1024 * 1024 * 1024
147
+ private static let eightGiB: UInt64 = 8 * 1024 * 1024 * 1024
148
+ private static let flagshipIoBufferBytes = 5 * 1024 * 1024
149
+
150
+ /// Checksum and copy share one ladder. 64-wide peak RAM = 64 * buffer.
151
+ /// <2GB: 64KB. <3GB: 256KB. <4GB: 512KB. <8GB: 1MB. Else 5MB (flagship / unknown).
152
+ static func ioBufferBytes(_ physicalRamBytes: UInt64 = ProcessInfo.processInfo.physicalMemory) -> Int {
153
+ if physicalRamBytes == 0 {
154
+ return flagshipIoBufferBytes
155
+ }
156
+ if physicalRamBytes < twoGiB {
157
+ return 64 * 1024
158
+ }
159
+ if physicalRamBytes < threeGiB {
160
+ return 256 * 1024
161
+ }
162
+ if physicalRamBytes < fourGiB {
163
+ return 512 * 1024
164
+ }
165
+ if physicalRamBytes < eightGiB {
166
+ return 1024 * 1024
167
+ }
168
+ return flagshipIoBufferBytes
169
+ }
170
+
171
+ static func checksumBufferBytes(_ physicalRamBytes: UInt64 = ProcessInfo.processInfo.physicalMemory) -> Int {
172
+ return ioBufferBytes(physicalRamBytes)
173
+ }
174
+
175
+ static func copyBufferBytes(_ physicalRamBytes: UInt64 = ProcessInfo.processInfo.physicalMemory) -> Int {
176
+ return ioBufferBytes(physicalRamBytes)
177
+ }
178
+
144
179
  public static func calcChecksum(filePath: URL) -> String {
145
- let bufferSize = 1024 * 1024 * 5 // 5 MB
180
+ let bufferSize = checksumBufferBytes()
146
181
  var sha256 = SHA256()
147
182
 
148
183
  do {
@@ -243,42 +278,19 @@ public struct CryptoCipher {
243
278
 
244
279
  let aesPrivateKey = AES128Key(iv: ivData, aes128Key: sessionKeyDataDecrypted, logger: logger)
245
280
 
246
- let encryptedData: Data
247
- do {
248
- encryptedData = try Data(contentsOf: filePath)
249
- } catch {
250
- logger.error("Failed to read encrypted data")
251
- logger.debug("Error: \(error)")
252
- throw NSError(domain: "Failed to read encrypted data", code: 3, userInfo: nil)
253
- }
254
-
255
- if encryptedData.isEmpty {
281
+ let encryptedSize = (try FileManager.default.attributesOfItem(atPath: filePath.path)[.size] as? NSNumber)?.uint64Value ?? 0
282
+ if encryptedSize == 0 {
256
283
  logger.error("Encrypted file data is empty")
257
284
  throw NSError(domain: "Empty encrypted data", code: 6, userInfo: nil)
258
285
  }
259
286
 
260
- guard let decryptedData = aesPrivateKey.decrypt(data: encryptedData) else {
261
- logger.error("Failed to decrypt data")
262
- throw NSError(domain: "Failed to decrypt data", code: 4, userInfo: nil)
263
- }
264
-
265
- if decryptedData.isEmpty {
287
+ try aesPrivateKey.decrypt(from: filePath, to: filePath)
288
+ let decryptedSize = (try FileManager.default.attributesOfItem(atPath: filePath.path)[.size] as? NSNumber)?.uint64Value ?? 0
289
+ if decryptedSize == 0 {
266
290
  logger.error("Decrypted data is empty")
267
291
  throw NSError(domain: "Empty decrypted data", code: 7, userInfo: nil)
268
292
  }
269
293
 
270
- do {
271
- try decryptedData.write(to: filePath, options: .atomic)
272
- if !FileManager.default.fileExists(atPath: filePath.path) {
273
- logger.error("File was not created after write")
274
- throw NSError(domain: "File write failed", code: 8, userInfo: nil)
275
- }
276
- } catch {
277
- logger.error("Error writing decrypted file")
278
- logger.debug("Error: \(error)")
279
- throw error
280
- }
281
-
282
294
  } catch {
283
295
  logger.error("File decryption failed")
284
296
  throw CustomError.cannotDecode
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-updater",
3
- "version": "8.51.8",
3
+ "version": "8.51.9",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Live update for capacitor apps",
6
6
  "main": "dist/plugin.cjs.js",