@swyng/react-native-code-push 1.0.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.
Files changed (239) hide show
  1. package/AlertAdapter.js +24 -0
  2. package/CLAUDE.md +57 -0
  3. package/CONTRIBUTING.md +134 -0
  4. package/CodePush.js +671 -0
  5. package/CodePush.podspec +28 -0
  6. package/LICENSE.md +13 -0
  7. package/README.md +323 -0
  8. package/SECURITY.md +24 -0
  9. package/android/app/build.gradle +48 -0
  10. package/android/app/proguard-rules.pro +33 -0
  11. package/android/app/src/main/AndroidManifest.xml +5 -0
  12. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePush.java +426 -0
  13. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushConstants.java +35 -0
  14. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushDialog.java +102 -0
  15. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushInstallMode.java +16 -0
  16. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushInvalidPublicKeyException.java +12 -0
  17. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushInvalidUpdateException.java +7 -0
  18. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushMalformedDataException.java +12 -0
  19. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushNativeModule.java +781 -0
  20. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushNotInitializedException.java +12 -0
  21. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushTelemetryManager.java +175 -0
  22. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushUnknownException.java +12 -0
  23. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushUpdateManager.java +383 -0
  24. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushUpdateState.java +15 -0
  25. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushUpdateUtils.java +275 -0
  26. package/android/app/src/main/java/com/swyngpush/codepush/react/CodePushUtils.java +238 -0
  27. package/android/app/src/main/java/com/swyngpush/codepush/react/DownloadProgress.java +30 -0
  28. package/android/app/src/main/java/com/swyngpush/codepush/react/DownloadProgressCallback.java +5 -0
  29. package/android/app/src/main/java/com/swyngpush/codepush/react/FileUtils.java +203 -0
  30. package/android/app/src/main/java/com/swyngpush/codepush/react/ReactHostHolder.java +11 -0
  31. package/android/app/src/main/java/com/swyngpush/codepush/react/SettingsManager.java +173 -0
  32. package/android/app/src/main/java/com/swyngpush/codepush/react/TLSSocketFactory.java +72 -0
  33. package/android/build.gradle +23 -0
  34. package/android/codepush.gradle +162 -0
  35. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  36. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  37. package/android/gradle.properties +18 -0
  38. package/android/gradlew +164 -0
  39. package/android/gradlew.bat +90 -0
  40. package/android/settings.gradle +2 -0
  41. package/docs/LOAD_BUNDLE_DELAY_ANALYSIS.md +68 -0
  42. package/docs/api-android.md +52 -0
  43. package/docs/api-ios.md +31 -0
  44. package/docs/api-js.md +592 -0
  45. package/docs/multi-deployment-testing-android.md +55 -0
  46. package/docs/multi-deployment-testing-ios.md +59 -0
  47. package/docs/setup-android.md +121 -0
  48. package/docs/setup-ios.md +141 -0
  49. package/docs/setup-windows.md +121 -0
  50. package/expo.js +347 -0
  51. package/ios/CodePush/Base64/Base64/MF_Base64Additions.h +34 -0
  52. package/ios/CodePush/Base64/Base64/MF_Base64Additions.m +252 -0
  53. package/ios/CodePush/Base64/README.md +47 -0
  54. package/ios/CodePush/CodePush.h +235 -0
  55. package/ios/CodePush/CodePush.m +1131 -0
  56. package/ios/CodePush/CodePushConfig.m +115 -0
  57. package/ios/CodePush/CodePushDownloadHandler.m +130 -0
  58. package/ios/CodePush/CodePushErrorUtils.m +20 -0
  59. package/ios/CodePush/CodePushPackage.m +602 -0
  60. package/ios/CodePush/CodePushTelemetryManager.m +175 -0
  61. package/ios/CodePush/CodePushUpdateUtils.m +376 -0
  62. package/ios/CodePush/CodePushUtils.m +9 -0
  63. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithm.h +69 -0
  64. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmFactory.h +16 -0
  65. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmFactory.m +51 -0
  66. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmNone.h +15 -0
  67. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmNone.m +55 -0
  68. package/ios/CodePush/JWT/Core/Algorithms/ESFamily/JWTAlgorithmESBase.h +24 -0
  69. package/ios/CodePush/JWT/Core/Algorithms/ESFamily/JWTAlgorithmESBase.m +41 -0
  70. package/ios/CodePush/JWT/Core/Algorithms/HSFamily/JWTAlgorithmHSBase.h +28 -0
  71. package/ios/CodePush/JWT/Core/Algorithms/HSFamily/JWTAlgorithmHSBase.m +205 -0
  72. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolder.h +103 -0
  73. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolder.m +322 -0
  74. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolderChain.h +37 -0
  75. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolderChain.m +145 -0
  76. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTAlgorithmRSBase.h +35 -0
  77. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTAlgorithmRSBase.m +551 -0
  78. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTRSAlgorithm.h +23 -0
  79. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKey.h +43 -0
  80. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKey.m +230 -0
  81. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKeyExtractor.h +31 -0
  82. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKeyExtractor.m +113 -0
  83. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.h +38 -0
  84. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.m +500 -0
  85. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaim.h +18 -0
  86. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaim.m +214 -0
  87. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSet.h +23 -0
  88. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSet.m +29 -0
  89. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetSerializer.h +19 -0
  90. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetSerializer.m +68 -0
  91. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetVerifier.h +18 -0
  92. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetVerifier.m +72 -0
  93. package/ios/CodePush/JWT/Core/Coding/JWTCoding+ResultTypes.h +67 -0
  94. package/ios/CodePush/JWT/Core/Coding/JWTCoding+ResultTypes.m +111 -0
  95. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionOne.h +119 -0
  96. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionOne.m +307 -0
  97. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionThree.h +94 -0
  98. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionThree.m +619 -0
  99. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionTwo.h +164 -0
  100. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionTwo.m +514 -0
  101. package/ios/CodePush/JWT/Core/Coding/JWTCoding.h +24 -0
  102. package/ios/CodePush/JWT/Core/Coding/JWTCoding.m +11 -0
  103. package/ios/CodePush/JWT/Core/FrameworkSupplement/JWT.h +52 -0
  104. package/ios/CodePush/JWT/Core/FrameworkSupplement/Map.modulemap +5 -0
  105. package/ios/CodePush/JWT/Core/Supplement/JWTBase64Coder.h +28 -0
  106. package/ios/CodePush/JWT/Core/Supplement/JWTBase64Coder.m +70 -0
  107. package/ios/CodePush/JWT/Core/Supplement/JWTDeprecations.h +22 -0
  108. package/ios/CodePush/JWT/Core/Supplement/JWTErrorDescription.h +34 -0
  109. package/ios/CodePush/JWT/Core/Supplement/JWTErrorDescription.m +73 -0
  110. package/ios/CodePush/JWT/LICENSE +19 -0
  111. package/ios/CodePush/JWT/README.md +489 -0
  112. package/ios/CodePush/RCTConvert+CodePushInstallMode.m +20 -0
  113. package/ios/CodePush/RCTConvert+CodePushUpdateState.m +20 -0
  114. package/ios/CodePush/SSZipArchive/Info.plist +26 -0
  115. package/ios/CodePush/SSZipArchive/README.md +1 -0
  116. package/ios/CodePush/SSZipArchive/SSZipArchive.h +178 -0
  117. package/ios/CodePush/SSZipArchive/SSZipArchive.m +1496 -0
  118. package/ios/CodePush/SSZipArchive/SSZipCommon.h +71 -0
  119. package/ios/CodePush/SSZipArchive/Supporting Files/PrivacyInfo.xcprivacy +23 -0
  120. package/ios/CodePush/SSZipArchive/include/ZipArchive.h +25 -0
  121. package/ios/CodePush/SSZipArchive/minizip/LICENSE +17 -0
  122. package/ios/CodePush/SSZipArchive/minizip/mz.h +273 -0
  123. package/ios/CodePush/SSZipArchive/minizip/mz_compat.c +1306 -0
  124. package/ios/CodePush/SSZipArchive/minizip/mz_compat.h +346 -0
  125. package/ios/CodePush/SSZipArchive/minizip/mz_crypt.c +187 -0
  126. package/ios/CodePush/SSZipArchive/minizip/mz_crypt.h +65 -0
  127. package/ios/CodePush/SSZipArchive/minizip/mz_crypt_apple.c +526 -0
  128. package/ios/CodePush/SSZipArchive/minizip/mz_os.c +348 -0
  129. package/ios/CodePush/SSZipArchive/minizip/mz_os.h +176 -0
  130. package/ios/CodePush/SSZipArchive/minizip/mz_os_posix.c +350 -0
  131. package/ios/CodePush/SSZipArchive/minizip/mz_strm.c +556 -0
  132. package/ios/CodePush/SSZipArchive/minizip/mz_strm.h +132 -0
  133. package/ios/CodePush/SSZipArchive/minizip/mz_strm_buf.c +383 -0
  134. package/ios/CodePush/SSZipArchive/minizip/mz_strm_buf.h +42 -0
  135. package/ios/CodePush/SSZipArchive/minizip/mz_strm_mem.c +269 -0
  136. package/ios/CodePush/SSZipArchive/minizip/mz_strm_mem.h +48 -0
  137. package/ios/CodePush/SSZipArchive/minizip/mz_strm_os.h +40 -0
  138. package/ios/CodePush/SSZipArchive/minizip/mz_strm_os_posix.c +203 -0
  139. package/ios/CodePush/SSZipArchive/minizip/mz_strm_pkcrypt.c +334 -0
  140. package/ios/CodePush/SSZipArchive/minizip/mz_strm_pkcrypt.h +46 -0
  141. package/ios/CodePush/SSZipArchive/minizip/mz_strm_split.c +429 -0
  142. package/ios/CodePush/SSZipArchive/minizip/mz_strm_split.h +43 -0
  143. package/ios/CodePush/SSZipArchive/minizip/mz_strm_wzaes.c +360 -0
  144. package/ios/CodePush/SSZipArchive/minizip/mz_strm_wzaes.h +46 -0
  145. package/ios/CodePush/SSZipArchive/minizip/mz_strm_zlib.c +389 -0
  146. package/ios/CodePush/SSZipArchive/minizip/mz_strm_zlib.h +43 -0
  147. package/ios/CodePush/SSZipArchive/minizip/mz_zip.c +2782 -0
  148. package/ios/CodePush/SSZipArchive/minizip/mz_zip.h +262 -0
  149. package/ios/CodePush/SSZipArchive/minizip/mz_zip_rw.c +1942 -0
  150. package/ios/CodePush/SSZipArchive/minizip/mz_zip_rw.h +285 -0
  151. package/ios/CodePush.xcodeproj/project.pbxproj +1052 -0
  152. package/ios/CodePush.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  153. package/ios/CodePush.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  154. package/ios/PrivacyInfo.xcprivacy +31 -0
  155. package/logging.js +6 -0
  156. package/package-mixins.js +68 -0
  157. package/package.json +91 -0
  158. package/react-native.config.js +11 -0
  159. package/request-fetch-adapter.js +52 -0
  160. package/scripts/generateBundledResourcesHash.js +125 -0
  161. package/scripts/getFilesInFolder.js +19 -0
  162. package/scripts/postlink/android/postlink.js +87 -0
  163. package/scripts/postlink/ios/postlink.js +116 -0
  164. package/scripts/postlink/run.js +11 -0
  165. package/scripts/postunlink/android/postunlink.js +74 -0
  166. package/scripts/postunlink/ios/postunlink.js +87 -0
  167. package/scripts/postunlink/run.js +11 -0
  168. package/scripts/recordFilesBeforeBundleCommand.js +41 -0
  169. package/scripts/tools/linkToolsAndroid.js +57 -0
  170. package/scripts/tools/linkToolsIos.js +130 -0
  171. package/tsconfig.json +17 -0
  172. package/tslint.json +32 -0
  173. package/typings/react-native-code-push.d.ts +455 -0
  174. package/windows/CodePush/.!12419!CodePushNativeModule.h +154 -0
  175. package/windows/CodePush/.!12440!CodePushNativeModule.h +154 -0
  176. package/windows/CodePush/CodePush.def +3 -0
  177. package/windows/CodePush/CodePush.vcxproj +198 -0
  178. package/windows/CodePush/CodePush.vcxproj.filters +91 -0
  179. package/windows/CodePush/CodePushConfig.cpp +99 -0
  180. package/windows/CodePush/CodePushConfig.h +66 -0
  181. package/windows/CodePush/CodePushConfig.idl +12 -0
  182. package/windows/CodePush/CodePushDownloadHandler.cpp +73 -0
  183. package/windows/CodePush/CodePushDownloadHandler.h +32 -0
  184. package/windows/CodePush/CodePushNativeModule.cpp +934 -0
  185. package/windows/CodePush/CodePushNativeModule.h +247 -0
  186. package/windows/CodePush/CodePushPackage.cpp +456 -0
  187. package/windows/CodePush/CodePushPackage.h +49 -0
  188. package/windows/CodePush/CodePushTelemetryManager.cpp +213 -0
  189. package/windows/CodePush/CodePushTelemetryManager.h +29 -0
  190. package/windows/CodePush/CodePushUpdateUtils.cpp +86 -0
  191. package/windows/CodePush/CodePushUpdateUtils.h +38 -0
  192. package/windows/CodePush/CodePushUtils.cpp +29 -0
  193. package/windows/CodePush/CodePushUtils.h +18 -0
  194. package/windows/CodePush/FileUtils.cpp +131 -0
  195. package/windows/CodePush/FileUtils.h +28 -0
  196. package/windows/CodePush/PropertySheet.props +16 -0
  197. package/windows/CodePush/ReactPackageProvider.cpp +15 -0
  198. package/windows/CodePush/ReactPackageProvider.h +22 -0
  199. package/windows/CodePush/ReactPackageProvider.idl +9 -0
  200. package/windows/CodePush/miniz/LICENSE +22 -0
  201. package/windows/CodePush/miniz/miniz.c +7657 -0
  202. package/windows/CodePush/miniz/miniz.h +1338 -0
  203. package/windows/CodePush/miniz/readme.md +37 -0
  204. package/windows/CodePush/packages.config +4 -0
  205. package/windows/CodePush/pch.cpp +1 -0
  206. package/windows/CodePush/pch.h +4 -0
  207. package/windows-legacy/CodePush/CodePush.csproj +128 -0
  208. package/windows-legacy/CodePush/CodePushUtils.cs +47 -0
  209. package/windows-legacy/CodePush/FileUtils.cs +40 -0
  210. package/windows-legacy/CodePush/Properties/AssemblyInfo.cs +29 -0
  211. package/windows-legacy/CodePush/Properties/CodePush.rd.xml +33 -0
  212. package/windows-legacy/CodePush/UpdateManager.cs +305 -0
  213. package/windows-legacy/CodePush/UpdateUtils.cs +46 -0
  214. package/windows-legacy/CodePush.Net46/Adapters/Http/HttpProgress.cs +28 -0
  215. package/windows-legacy/CodePush.Net46/Adapters/Storage/ApplicationDataContainer.cs +106 -0
  216. package/windows-legacy/CodePush.Net46/CodePush.Net46.csproj +103 -0
  217. package/windows-legacy/CodePush.Net46/CodePushUtils.cs +158 -0
  218. package/windows-legacy/CodePush.Net46/FileUtils.cs +55 -0
  219. package/windows-legacy/CodePush.Net46/Properties/AssemblyInfo.cs +36 -0
  220. package/windows-legacy/CodePush.Net46/UpdateManager.cs +330 -0
  221. package/windows-legacy/CodePush.Net46/UpdateUtils.cs +70 -0
  222. package/windows-legacy/CodePush.Net46/packages.config +5 -0
  223. package/windows-legacy/CodePush.Net46.Test/ApplicationDataContainerTest.cs +105 -0
  224. package/windows-legacy/CodePush.Net46.Test/CodePush.Net46.Test.csproj +137 -0
  225. package/windows-legacy/CodePush.Net46.Test/Properties/AssemblyInfo.cs +36 -0
  226. package/windows-legacy/CodePush.Net46.Test/TelemetryManagerTest.cs +117 -0
  227. package/windows-legacy/CodePush.Net46.Test/app.config +11 -0
  228. package/windows-legacy/CodePush.Net46.Test/packages.config +4 -0
  229. package/windows-legacy/CodePush.Shared/CodePush.Shared.projitems +22 -0
  230. package/windows-legacy/CodePush.Shared/CodePush.Shared.shproj +13 -0
  231. package/windows-legacy/CodePush.Shared/CodePushConstants.cs +35 -0
  232. package/windows-legacy/CodePush.Shared/CodePushNativeModule.cs +329 -0
  233. package/windows-legacy/CodePush.Shared/CodePushReactPackage.cs +235 -0
  234. package/windows-legacy/CodePush.Shared/CodePushUtils.cs +70 -0
  235. package/windows-legacy/CodePush.Shared/InstallMode.cs +9 -0
  236. package/windows-legacy/CodePush.Shared/MinimumBackgroundListener.cs +44 -0
  237. package/windows-legacy/CodePush.Shared/SettingsManager.cs +148 -0
  238. package/windows-legacy/CodePush.Shared/TelemetryManager.cs +250 -0
  239. package/windows-legacy/CodePush.Shared/UpdateState.cs +9 -0
package/expo.js ADDED
@@ -0,0 +1,347 @@
1
+ const {
2
+ withAppDelegate,
3
+ withInfoPlist,
4
+ withStringsXml,
5
+ withAppBuildGradle,
6
+ withMainApplication, // Using the safer, higher-level helper
7
+ WarningAggregator,
8
+ } = require('@expo/config-plugins');
9
+ const fs = require('fs');
10
+ const path = require('path');
11
+
12
+ //================ iOS (Unchanged) ================
13
+ function addImportIOS(content) {
14
+ const lines = content.split('\n');
15
+ const importIndices = lines
16
+ .map((line, idx) => (line.trim().startsWith('import ') || line.trim().startsWith('#import ') ? idx : -1))
17
+ .filter(idx => idx !== -1);
18
+ if (content.includes('import CodePush') || content.includes('#import <CodePush/CodePush.h>')) {
19
+ return content;
20
+ }
21
+ const codePushImport = 'import CodePush';
22
+ if (importIndices.length > 0) {
23
+ const lastImportIdx = importIndices[importIndices.length - 1];
24
+ lines.splice(lastImportIdx + 1, 0, codePushImport);
25
+ return lines.join('\n');
26
+ } else {
27
+ const swiftClassRegex = /class\s+AppDelegate\s*:\s*RCTAppDelegate\s*\{/m;
28
+ if (swiftClassRegex.test(content)) {
29
+ return content.replace(swiftClassRegex, `$&\n${codePushImport}`);
30
+ }
31
+ return codePushImport + '\n' + content;
32
+ }
33
+ }
34
+
35
+ function ensureBundleURLMethodIOS(content) {
36
+ const methodRegexOld = /-\s*\(NSURL\s*\*\s*\)\s*bundleURL\s*\{[^}]*\}/s;
37
+ const methodRegexSwift = /override func bundleURL\(\) -> URL\? \{[^}]*\}/s;
38
+ const newMethodBodyObjC = `- (NSURL *)bundleURL
39
+ {
40
+ #if DEBUG
41
+ return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@".expo/.virtual-metro-entry"];
42
+ #else
43
+ return [CodePush bundleURL];
44
+ #endif
45
+ }`;
46
+ const newMethodBodySwift = `override func bundleURL() -> URL? {
47
+ #if DEBUG
48
+ return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: ".expo/.virtual-metro-entry")
49
+ #else
50
+ return CodePush.bundleURL()
51
+ #endif
52
+ }`;
53
+
54
+ if (methodRegexSwift.test(content)) {
55
+ return content.replace(methodRegexSwift, newMethodBodySwift);
56
+ } else if (methodRegexOld.test(content)) {
57
+ return content.replace(methodRegexOld, newMethodBodyObjC);
58
+ } else {
59
+ const appDelegateEndRegexObjC = /\@end/m;
60
+ const appDelegateEndRegexSwift = /\}\s*$/m;
61
+ if (appDelegateEndRegexObjC.test(content)) {
62
+ return content.replace(appDelegateEndRegexObjC, `\n${newMethodBodyObjC}\n\n@end`);
63
+ } else if (appDelegateEndRegexSwift.test(content)) {
64
+ const lastBraceIndex = content.lastIndexOf('}');
65
+ if (lastBraceIndex !== -1) {
66
+ return content.substring(0, lastBraceIndex) + `\n ${newMethodBodySwift}\n` + content.substring(lastBraceIndex);
67
+ }
68
+ }
69
+ }
70
+ WarningAggregator.addWarningIOS('codepush-plugin', 'Could not find a suitable place to insert bundleURL() method in AppDelegate.');
71
+ return content;
72
+ }
73
+
74
+ const withCodePushAppDelegate = (config) => {
75
+ return withAppDelegate(config, (config) => {
76
+ let content = config.modResults.contents;
77
+ content = addImportIOS(content);
78
+ content = ensureBundleURLMethodIOS(content);
79
+ config.modResults.contents = content;
80
+ return config;
81
+ });
82
+ };
83
+
84
+ const withCodePushInfoPlist = (config, options = {}) => {
85
+ return withInfoPlist(config, (config) => {
86
+ if (options.ios && options.ios.CodePushDeploymentKey) {
87
+ config.modResults.CodePushDeploymentKey = options.ios.CodePushDeploymentKey;
88
+ }
89
+ if (options.ios && options.ios.CodePushServerURL) {
90
+ config.modResults.CodePushServerURL = options.ios.CodePushServerURL;
91
+ }
92
+ return config;
93
+ });
94
+ };
95
+
96
+ //================ Android (Refactored to use withMainApplication) ================
97
+
98
+ const withAndroidMainApplication = (config) => {
99
+ return withMainApplication(config, (modConfig) => {
100
+ // Check for Kotlin
101
+ if (modConfig.modResults.language !== 'kt') {
102
+ WarningAggregator.addWarningAndroid(
103
+ 'codepush-plugin',
104
+ `The CodePush plugin is skipping modifications to 'MainApplication' because it is not a Kotlin file. Your project must be configured to use Kotlin for Android.`
105
+ );
106
+ return modConfig;
107
+ }
108
+
109
+ let content = modConfig.modResults.contents;
110
+ const packageName = config.android.package;
111
+
112
+ // --- 1. Add Imports ---
113
+ const requiredImports = [
114
+ 'import com.swyngpush.codepush.react.CodePush',
115
+ `import ${packageName}.R`,
116
+ 'import android.util.Log',
117
+ ];
118
+ // Simple import adder
119
+ const lines = content.split('\n');
120
+ let lastImportIndex = -1;
121
+ const existingImports = new Set();
122
+ lines.forEach((line, index) => {
123
+ if (line.trim().startsWith('import ')) {
124
+ lastImportIndex = index;
125
+ existingImports.add(line.trim());
126
+ }
127
+ });
128
+ const importsToAdd = requiredImports.filter(imp => !existingImports.has(imp));
129
+ if (importsToAdd.length > 0) {
130
+ lines.splice(lastImportIndex + 1, 0, ...importsToAdd);
131
+ }
132
+ content = lines.join('\n');
133
+
134
+ // --- 2. Modify onCreate method ---
135
+ const onCreateRegex = /(override fun onCreate\(\)\s*\{)([\s\S]*?)(\n\s*\})/m;
136
+ const onCreateInjection = `
137
+ // CodePush: Initialize instance on app startup.
138
+ try {
139
+ Log.d("CodePushDebug", "Attempting to pre-initialize CodePush in onCreate...");
140
+ val deploymentKey = getString(R.string.CodePushDeploymentKey);
141
+ val isDebugMode = BuildConfig.DEBUG;
142
+ CodePush.getInstance(deploymentKey, this, isDebugMode);
143
+ Log.d("CodePushDebug", "CodePush.getInstance() called in onCreate()");
144
+ } catch (e: Exception) {
145
+ Log.e("CodePushDebug", "Error pre-initializing CodePush in onCreate: " + e.message, e);
146
+ }
147
+ `;
148
+ if (onCreateRegex.test(content) && !content.includes('CodePush.getInstance(deploymentKey, this, isDebugMode)')) {
149
+ content = content.replace(onCreateRegex, (match, onCreateStart, onCreateContent, onCreateEnd) => {
150
+ const soLoaderInitLineRegex = /^\s*SoLoader\.init\(this,.*\)/m;
151
+ const superOnCreateLineRegex = /^\s*super\.onCreate\(\)/m;
152
+
153
+ if (soLoaderInitLineRegex.test(onCreateContent)) {
154
+ // Found SoLoader.init(), insert after it
155
+ const modifiedContent = onCreateContent.replace(soLoaderInitLineRegex, `$&${onCreateInjection}`);
156
+ return `${onCreateStart}${modifiedContent}${onCreateEnd}`;
157
+ } else if (superOnCreateLineRegex.test(onCreateContent)) {
158
+ // SoLoader.init() not found, insert after super.onCreate()
159
+ const modifiedContent = onCreateContent.replace(superOnCreateLineRegex, `$&${onCreateInjection}`);
160
+ WarningAggregator.addWarningAndroid('codepush-plugin', 'SoLoader.init() not found in onCreate(). Placing CodePush initialization after super.onCreate().');
161
+ return `${onCreateStart}${modifiedContent}${onCreateEnd}`;
162
+ } else {
163
+ // Neither found, inject at the start of the method
164
+ WarningAggregator.addWarningAndroid('codepush-plugin', 'Could not find super.onCreate() or SoLoader.init() in onCreate(). CodePush initialization may be misplaced.');
165
+ return `${onCreateStart}${onCreateInjection}${onCreateContent}${onCreateEnd}`;
166
+ }
167
+ });
168
+ }
169
+
170
+ // --- 3. Modify getPackages method ---
171
+ const getPackagesRegex = /override fun getPackages\(\): List<ReactPackage>\s*\{/m;
172
+ const originalPackagesLineRegex = /val\s+packages\s*=\s*PackageList\(this\)\.packages/m;
173
+ const mutablePackagesLine = "val packages: MutableList<ReactPackage> = PackageList(this).packages.toMutableList()";
174
+ const packagesInjection = `
175
+ try {
176
+ val codePushInstance = CodePush.getInstance(getString(R.string.CodePushDeploymentKey), this@MainApplication, BuildConfig.DEBUG);
177
+ if (!packages.contains(codePushInstance)) {
178
+ packages.add(codePushInstance);
179
+ Log.d("CodePushDebug", "CodePush instance added to packages.");
180
+ } else {
181
+ Log.d("CodePushDebug", "CodePush instance was already present in packages list; not adding again.");
182
+ }
183
+ } catch (e: Exception) {
184
+ Log.e("CodePushDebug", "Error adding CodePush to packages list: " + e.message, e);
185
+ }
186
+ `;
187
+ if (getPackagesRegex.test(content) && !content.includes('CodePush.getInstance(getString(R.string.CodePushDeploymentKey)')) {
188
+ if (originalPackagesLineRegex.test(content)) {
189
+ content = content.replace(originalPackagesLineRegex, mutablePackagesLine);
190
+ content = content.replace(/(\n\s*return\s+packages)/m, `\n${packagesInjection}$1`);
191
+ } else {
192
+ WarningAggregator.addWarningAndroid('codepush-plugin', 'Could not find standard packages list initialization in getPackages(). CodePush package not registered.');
193
+ }
194
+ }
195
+
196
+ // --- 4. Wire up CodePush bundle file ---
197
+ if (!content.includes("CodePush.getJSBundleFile()")) {
198
+ const getJSBundleFileMethodString = `
199
+ override fun getJSBundleFile(): String {
200
+ return CodePush.getJSBundleFile()
201
+ }`;
202
+ const reactNativeHostAnchors = [
203
+ /(override\s+fun\s+getJSMainModuleName\(\):\s*String\s*=\s*[^\n]+)\s*\n/m,
204
+ /(override\s+fun\s+getUseDeveloperSupport\(\):\s*Boolean\s*=\s*BuildConfig\.DEBUG)\s*\n/m,
205
+ /(override\s+val\s+isHermesEnabled:\s*Boolean\s*=\s*BuildConfig\.IS_HERMES_ENABLED)\s*\n/m,
206
+ /(override\s+val\s+isNewArchEnabled:\s*Boolean\s*=\s*BuildConfig\.IS_NEW_ARCHITECTURE_ENABLED)\s*\n/m,
207
+ ];
208
+ const reactNativeHostAnchor = reactNativeHostAnchors.find(anchor => anchor.test(content));
209
+
210
+ if (reactNativeHostAnchor) {
211
+ // RN <= 0.81 and Expo SDK 54 still configure the bundle via ReactNativeHost.
212
+ // Expo wraps the host, but ReactNativeHostWrapper delegates getJSBundleFile() to the wrapped host.
213
+ content = content.replace(reactNativeHostAnchor, `$1\n${getJSBundleFileMethodString}\n`);
214
+ } else {
215
+ // RN 0.82+: uses ReactHost via getDefaultReactHost() — pass jsBundleFilePath parameter
216
+ // Match the closing parenthesis of the getDefaultReactHost() call
217
+ const reactHostCallRegex = /(getDefaultReactHost\([\s\S]*?packageList\s*=[\s\S]*?\})([\s\S]*?\))/m;
218
+ if (reactHostCallRegex.test(content)) {
219
+ content = content.replace(reactHostCallRegex, (match, beforeClose, closing) => {
220
+ // Check if jsBundleFilePath is already set
221
+ if (match.includes('jsBundleFilePath')) return match;
222
+ // Insert the parameter before the closing parentheses
223
+ return `${beforeClose},\n jsBundleFilePath = CodePush.getJSBundleFile()${closing}`;
224
+ });
225
+ } else {
226
+ WarningAggregator.addWarningAndroid(
227
+ 'codepush-plugin',
228
+ 'Could not detect a supported React host configuration in MainApplication. CodePush bundle file path not configured.'
229
+ );
230
+ }
231
+ }
232
+ }
233
+
234
+ modConfig.modResults.contents = content;
235
+ return modConfig;
236
+ });
237
+ };
238
+
239
+ const withAndroidGradle = (config) => {
240
+ return withAppBuildGradle(config, (modConfig) => {
241
+ if (modConfig.modResults.language === 'groovy') {
242
+ let content = modConfig.modResults.contents;
243
+
244
+ // This part adds the codepush.gradle apply line and is correct.
245
+ const codePushApplyLine = 'apply from: "../../node_modules/@code-push-next/react-native-code-push/android/codepush.gradle"';
246
+ if (!content.includes(codePushApplyLine)) {
247
+ content += `\n${codePushApplyLine}\n`;
248
+ }
249
+
250
+ const debugConfigFieldLine = 'buildConfigField "boolean", "DEBUG", "true"';
251
+ const releaseConfigFieldLine = 'buildConfigField "boolean", "DEBUG", "false"';
252
+ const indent = ' ';
253
+
254
+ /**
255
+ * A robust function to add a configuration field to a specific build type.
256
+ * It operates on the entire gradle content to ensure context is never lost.
257
+ * @param {string} gradleContent The entire content of the build.gradle file.
258
+ * @param {string} buildType The name of the build type (e.g., "debug").
259
+ * @param {string} field The configuration line to add.
260
+ * @returns {string} The modified gradle content.
261
+ */
262
+ const addFieldToBuildType = (gradleContent, buildType, field) => {
263
+ // Regex to find a specific buildType block that is inside the buildTypes block
264
+ const buildTypeRegex = new RegExp(`(buildTypes\\s*\\{[\\s\\S]*?${buildType}\\s*\\{)([\\s\\S]*?)(\\n\\s*\\})`, 'm');
265
+
266
+ if (buildTypeRegex.test(gradleContent)) {
267
+ // The build type block (e.g., debug {}) already exists.
268
+ return gradleContent.replace(buildTypeRegex, (match, startBlock, innerContent, endBlock) => {
269
+ if (innerContent.includes(field.trim())) {
270
+ return match; // Field already exists, no changes needed.
271
+ }
272
+ // Add the field to the existing block.
273
+ const newInnerContent = innerContent.trim() ? `${innerContent.trim()}\n${indent}${field.trim()}` : `\n${indent}${field.trim()}`;
274
+ return `${startBlock}${newInnerContent}\n ${endBlock}`;
275
+ });
276
+ } else {
277
+ // The build type block does not exist, so we need to add it.
278
+ const buildTypesRegex = /(buildTypes\s*\{)([\s\S]*?)(\n\s*\})/m;
279
+ if (buildTypesRegex.test(gradleContent)) {
280
+ // The buildTypes block exists, so add the new build type to it.
281
+ return gradleContent.replace(buildTypesRegex, (match, startBlock, innerContent, endBlock) => {
282
+ const newBlock = `\n ${buildType} {\n${indent}${field.trim()}\n }`;
283
+ return `${startBlock}${innerContent.trim()}${newBlock}${endBlock}`;
284
+ });
285
+ } else {
286
+ // The buildTypes block itself doesn't exist, a rare edge case.
287
+ // Expo projects should always have a buildTypes block.
288
+ WarningAggregator.addWarningAndroid('codepush-plugin', `Could not find buildTypes { ... } block in app/build.gradle to add the '${buildType}' configuration.`);
289
+ return gradleContent;
290
+ }
291
+ }
292
+ };
293
+
294
+ // Apply the modifications for debug and release builds sequentially.
295
+ // Each function call operates on the full, updated content, preventing nesting errors.
296
+ content = addFieldToBuildType(content, 'debug', debugConfigFieldLine);
297
+ content = addFieldToBuildType(content, 'release', releaseConfigFieldLine);
298
+
299
+ modConfig.modResults.contents = content;
300
+ } else {
301
+ WarningAggregator.addWarningAndroid('codepush-plugin', 'Cannot apply build.gradle modifications because it is not a groovy file.');
302
+ }
303
+ return modConfig;
304
+ });
305
+ };
306
+
307
+ const withAndroidStrings = (config, options) => {
308
+ return withStringsXml(config, (config) => {
309
+ if (!config.modResults.resources) config.modResults.resources = {};
310
+ if (!config.modResults.resources.string) config.modResults.resources.string = [];
311
+ const strings = config.modResults.resources.string;
312
+
313
+ const setString = (name, value) => {
314
+ const existing = strings.find(s => s.$.name === name);
315
+ if (existing) {
316
+ existing._ = value;
317
+ } else {
318
+ strings.push({$: { name, translatable: 'false' }, _: value });
319
+ }
320
+ };
321
+ if (options.android?.CodePushDeploymentKey) {
322
+ setString('CodePushDeploymentKey', options.android.CodePushDeploymentKey);
323
+ }
324
+ if (options.android?.CodePushServerURL) {
325
+ setString('CodePushServerUrl', options.android.CodePushServerURL);
326
+ }
327
+ return config;
328
+ });
329
+ };
330
+
331
+ // --- CORRECTED EXPORT BLOCK ---
332
+ module.exports = (config, options = {}) => {
333
+ if (!options) options = {};
334
+
335
+ if (options.ios) {
336
+ config = withCodePushAppDelegate(config, options);
337
+ config = withCodePushInfoPlist(config, options);
338
+ }
339
+
340
+ if (options.android) {
341
+ config = withAndroidStrings(config, options);
342
+ // Correctly call the wrapper functions we defined
343
+ config = withAndroidMainApplication(config);
344
+ config = withAndroidGradle(config);
345
+ }
346
+ return config;
347
+ };
@@ -0,0 +1,34 @@
1
+ //
2
+ // MF_Base64Additions.h
3
+ // Base64 -- RFC 4648 compatible implementation
4
+ // see http://www.ietf.org/rfc/rfc4648.txt for more details
5
+ //
6
+ // Designed to be compiled with Automatic Reference Counting
7
+ //
8
+ // Created by Dave Poirier on 2012-06-14.
9
+ // Public Domain
10
+ // Hosted at https://github.com/ekscrypto/Base64
11
+ //
12
+
13
+ #import <Foundation/Foundation.h>
14
+
15
+ @interface NSString (Base64Addition)
16
+ +(NSString *)stringFromBase64String:(NSString *)base64String;
17
+ +(NSString *)stringFromBase64UrlEncodedString:(NSString *)base64UrlEncodedString;
18
+ -(NSString *)base64String;
19
+ -(NSString *)base64UrlEncodedString;
20
+ @end
21
+
22
+ @interface NSData (Base64Addition)
23
+ +(NSData *)dataWithBase64String:(NSString *)base64String;
24
+ +(NSData *)dataWithBase64UrlEncodedString:(NSString *)base64UrlEncodedString;
25
+ -(NSString *)base64String;
26
+ -(NSString *)base64UrlEncodedString;
27
+ @end
28
+
29
+ @interface MF_Base64Codec : NSObject
30
+ +(NSData *)dataFromBase64String:(NSString *)base64String;
31
+ +(NSString *)base64StringFromData:(NSData *)data;
32
+ +(NSString *)base64UrlEncodedStringFromBase64String:(NSString *)base64String;
33
+ +(NSString *)base64StringFromBase64UrlEncodedString:(NSString *)base64UrlEncodedString;
34
+ @end
@@ -0,0 +1,252 @@
1
+ //
2
+ // MF_Base64Additions.m
3
+ // Base64 -- RFC 4648 compatible implementation
4
+ // see http://www.ietf.org/rfc/rfc4648.txt for more details
5
+ //
6
+ // Designed to be compiled with Automatic Reference Counting
7
+ //
8
+ // Created by Dave Poirier on 2012-06-14.
9
+ // Public Domain
10
+ // Hosted at https://github.com/ekscrypto/Base64
11
+ //
12
+
13
+ #import "MF_Base64Additions.h"
14
+
15
+ @implementation MF_Base64Codec
16
+
17
+ +(NSString *)base64StringFromBase64UrlEncodedString:(NSString *)base64UrlEncodedString
18
+ {
19
+ NSString *s = base64UrlEncodedString;
20
+ s = [s stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
21
+ s = [s stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
22
+ switch (s.length % 4) {
23
+ case 2:
24
+ s = [s stringByAppendingString:@"=="];
25
+ break;
26
+ case 3:
27
+ s = [s stringByAppendingString:@"="];
28
+ break;
29
+ default:
30
+ break;
31
+ }
32
+ return s;
33
+ }
34
+
35
+ +(NSString *)base64UrlEncodedStringFromBase64String:(NSString *)base64String
36
+ {
37
+ NSString *s = base64String;
38
+ s = [s stringByReplacingOccurrencesOfString:@"=" withString:@""];
39
+ s = [s stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
40
+ s = [s stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
41
+ return s;
42
+ }
43
+
44
+ +(NSData *)dataFromBase64String:(NSString *)encoding
45
+ {
46
+ NSData *data = nil;
47
+ unsigned char *decodedBytes = NULL;
48
+ @try {
49
+ #define __ 255
50
+ static char decodingTable[256] = {
51
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0x00 - 0x0F
52
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0x10 - 0x1F
53
+ __,__,__,__, __,__,__,__, __,__,__,62, __,__,__,63, // 0x20 - 0x2F
54
+ 52,53,54,55, 56,57,58,59, 60,61,__,__, __, 0,__,__, // 0x30 - 0x3F
55
+ __, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, // 0x40 - 0x4F
56
+ 15,16,17,18, 19,20,21,22, 23,24,25,__, __,__,__,__, // 0x50 - 0x5F
57
+ __,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, // 0x60 - 0x6F
58
+ 41,42,43,44, 45,46,47,48, 49,50,51,__, __,__,__,__, // 0x70 - 0x7F
59
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0x80 - 0x8F
60
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0x90 - 0x9F
61
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0xA0 - 0xAF
62
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0xB0 - 0xBF
63
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0xC0 - 0xCF
64
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0xD0 - 0xDF
65
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0xE0 - 0xEF
66
+ __,__,__,__, __,__,__,__, __,__,__,__, __,__,__,__, // 0xF0 - 0xFF
67
+ };
68
+ encoding = [encoding stringByReplacingOccurrencesOfString:@"=" withString:@""];
69
+ NSData *encodedData = [encoding dataUsingEncoding:NSASCIIStringEncoding];
70
+ unsigned char *encodedBytes = (unsigned char *)[encodedData bytes];
71
+
72
+ NSUInteger encodedLength = [encodedData length];
73
+ if( encodedLength >= (NSUIntegerMax - 3) ) return nil; // NSUInteger overflow check
74
+ NSUInteger encodedBlocks = (encodedLength+3) >> 2;
75
+ NSUInteger expectedDataLength = encodedBlocks * 3;
76
+
77
+ unsigned char decodingBlock[4];
78
+
79
+ decodedBytes = malloc(expectedDataLength);
80
+ if( decodedBytes != NULL ) {
81
+
82
+ NSUInteger i = 0;
83
+ NSUInteger j = 0;
84
+ NSUInteger k = 0;
85
+ unsigned char c;
86
+ while( i < encodedLength ) {
87
+ c = decodingTable[encodedBytes[i]];
88
+ i++;
89
+ if( c != __ ) {
90
+ decodingBlock[j] = c;
91
+ j++;
92
+ if( j == 4 ) {
93
+ decodedBytes[k] = (decodingBlock[0] << 2) | (decodingBlock[1] >> 4);
94
+ decodedBytes[k+1] = (decodingBlock[1] << 4) | (decodingBlock[2] >> 2);
95
+ decodedBytes[k+2] = (decodingBlock[2] << 6) | (decodingBlock[3]);
96
+ j = 0;
97
+ k += 3;
98
+ }
99
+ }
100
+ }
101
+
102
+ // Process left over bytes, if any
103
+ if( j == 3 ) {
104
+ decodedBytes[k] = (decodingBlock[0] << 2) | (decodingBlock[1] >> 4);
105
+ decodedBytes[k+1] = (decodingBlock[1] << 4) | (decodingBlock[2] >> 2);
106
+ k += 2;
107
+ } else if( j == 2 ) {
108
+ decodedBytes[k] = (decodingBlock[0] << 2) | (decodingBlock[1] >> 4);
109
+ k += 1;
110
+ }
111
+ data = [[NSData alloc] initWithBytes:decodedBytes length:k];
112
+ }
113
+ }
114
+ @catch (NSException *exception) {
115
+ data = nil;
116
+ NSLog(@"WARNING: error occured while decoding base 32 string: %@", exception);
117
+ }
118
+ @finally {
119
+ if( decodedBytes != NULL ) {
120
+ free( decodedBytes );
121
+ }
122
+ }
123
+ return data;
124
+ }
125
+ +(NSString *)base64StringFromData:(NSData *)data
126
+ {
127
+ NSString *encoding = nil;
128
+ unsigned char *encodingBytes = NULL;
129
+ @try {
130
+ static char encodingTable[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
131
+ static NSUInteger paddingTable[] = {0,2,1};
132
+ // Table 1: The Base 64 Alphabet
133
+ //
134
+ // Value Encoding Value Encoding Value Encoding Value Encoding
135
+ // 0 A 17 R 34 i 51 z
136
+ // 1 B 18 S 35 j 52 0
137
+ // 2 C 19 T 36 k 53 1
138
+ // 3 D 20 U 37 l 54 2
139
+ // 4 E 21 V 38 m 55 3
140
+ // 5 F 22 W 39 n 56 4
141
+ // 6 G 23 X 40 o 57 5
142
+ // 7 H 24 Y 41 p 58 6
143
+ // 8 I 25 Z 42 q 59 7
144
+ // 9 J 26 a 43 r 60 8
145
+ // 10 K 27 b 44 s 61 9
146
+ // 11 L 28 c 45 t 62 +
147
+ // 12 M 29 d 46 u 63 /
148
+ // 13 N 30 e 47 v
149
+ // 14 O 31 f 48 w (pad) =
150
+ // 15 P 32 g 49 x
151
+ // 16 Q 33 h 50 y
152
+
153
+ NSUInteger dataLength = [data length];
154
+ NSUInteger encodedBlocks = dataLength / 3;
155
+ if( (encodedBlocks + 1) >= (NSUIntegerMax / 4) ) return nil; // NSUInteger overflow check
156
+ NSUInteger padding = paddingTable[dataLength % 3];
157
+ if( padding > 0 ) encodedBlocks++;
158
+ NSUInteger encodedLength = encodedBlocks * 4;
159
+
160
+ encodingBytes = malloc(encodedLength);
161
+ if( encodingBytes != NULL ) {
162
+ NSUInteger rawBytesToProcess = dataLength;
163
+ NSUInteger rawBaseIndex = 0;
164
+ NSUInteger encodingBaseIndex = 0;
165
+ unsigned char *rawBytes = (unsigned char *)[data bytes];
166
+ unsigned char rawByte1, rawByte2, rawByte3;
167
+ while( rawBytesToProcess >= 3 ) {
168
+ rawByte1 = rawBytes[rawBaseIndex];
169
+ rawByte2 = rawBytes[rawBaseIndex+1];
170
+ rawByte3 = rawBytes[rawBaseIndex+2];
171
+ encodingBytes[encodingBaseIndex] = encodingTable[((rawByte1 >> 2) & 0x3F)];
172
+ encodingBytes[encodingBaseIndex+1] = encodingTable[((rawByte1 << 4) & 0x30) | ((rawByte2 >> 4) & 0x0F) ];
173
+ encodingBytes[encodingBaseIndex+2] = encodingTable[((rawByte2 << 2) & 0x3C) | ((rawByte3 >> 6) & 0x03) ];
174
+ encodingBytes[encodingBaseIndex+3] = encodingTable[(rawByte3 & 0x3F)];
175
+
176
+ rawBaseIndex += 3;
177
+ encodingBaseIndex += 4;
178
+ rawBytesToProcess -= 3;
179
+ }
180
+ rawByte2 = 0;
181
+ switch (dataLength-rawBaseIndex) {
182
+ case 2:
183
+ rawByte2 = rawBytes[rawBaseIndex+1];
184
+ case 1:
185
+ rawByte1 = rawBytes[rawBaseIndex];
186
+ encodingBytes[encodingBaseIndex] = encodingTable[((rawByte1 >> 2) & 0x3F)];
187
+ encodingBytes[encodingBaseIndex+1] = encodingTable[((rawByte1 << 4) & 0x30) | ((rawByte2 >> 4) & 0x0F) ];
188
+ encodingBytes[encodingBaseIndex+2] = encodingTable[((rawByte2 << 2) & 0x3C) ];
189
+ // we can skip rawByte3 since we have a partial block it would always be 0
190
+ break;
191
+ }
192
+ // compute location from where to begin inserting padding, it may overwrite some bytes from the partial block encoding
193
+ // if their value was 0 (cases 1-2).
194
+ encodingBaseIndex = encodedLength - padding;
195
+ while( padding-- > 0 ) {
196
+ encodingBytes[encodingBaseIndex++] = '=';
197
+ }
198
+ encoding = [[NSString alloc] initWithBytes:encodingBytes length:encodedLength encoding:NSASCIIStringEncoding];
199
+ }
200
+ }
201
+ @catch (NSException *exception) {
202
+ encoding = nil;
203
+ NSLog(@"WARNING: error occured while tring to encode base 32 data: %@", exception);
204
+ }
205
+ @finally {
206
+ if( encodingBytes != NULL ) {
207
+ free( encodingBytes );
208
+ }
209
+ }
210
+ return encoding;
211
+ }
212
+ @end
213
+
214
+ @implementation NSString (Base64Addition)
215
+ -(NSString *)base64String
216
+ {
217
+ NSData *utf8encoding = [self dataUsingEncoding:NSUTF8StringEncoding];
218
+ return [MF_Base64Codec base64StringFromData:utf8encoding];
219
+ }
220
+ -(NSString *)base64UrlEncodedString
221
+ {
222
+ return [MF_Base64Codec base64UrlEncodedStringFromBase64String:[self base64String]];
223
+ }
224
+ +(NSString *)stringFromBase64String:(NSString *)base64String
225
+ {
226
+ NSData *utf8encoding = [MF_Base64Codec dataFromBase64String:base64String];
227
+ return [[NSString alloc] initWithData:utf8encoding encoding:NSUTF8StringEncoding];
228
+ }
229
+ +(NSString *)stringFromBase64UrlEncodedString:(NSString *)base64UrlEncodedString
230
+ {
231
+ return [self stringFromBase64String:[MF_Base64Codec base64StringFromBase64UrlEncodedString:base64UrlEncodedString]];
232
+ }
233
+ @end
234
+
235
+ @implementation NSData (Base64Addition)
236
+ +(NSData *)dataWithBase64String:(NSString *)base64String
237
+ {
238
+ return [MF_Base64Codec dataFromBase64String:base64String];
239
+ }
240
+ +(NSData *)dataWithBase64UrlEncodedString:(NSString *)base64UrlEncodedString
241
+ {
242
+ return [self dataWithBase64String:[MF_Base64Codec base64StringFromBase64UrlEncodedString:base64UrlEncodedString]];
243
+ }
244
+ -(NSString *)base64String
245
+ {
246
+ return [MF_Base64Codec base64StringFromData:self];
247
+ }
248
+ -(NSString *)base64UrlEncodedString
249
+ {
250
+ return [MF_Base64Codec base64UrlEncodedStringFromBase64String:[self base64String]];
251
+ }
252
+ @end
@@ -0,0 +1,47 @@
1
+ [![CI Status](https://travis-ci.org/ekscrypto/Base64.svg?branch=master)](https://github.com/ekscrypto/Base64)
2
+
3
+ Base64 Additions for Objective-C on Mac OS X and iOS
4
+ =======
5
+
6
+
7
+ Usage
8
+ ----
9
+ Open the Xcode project file, and drag MF_Base64Additions.m/.h into your project.
10
+
11
+ In files where you want to use Base64 encoding/decoding, simply include the header file and use one of the provided NSData or NSString additions.
12
+
13
+ Example use:
14
+ #import "MF_Base64Additions.h"
15
+
16
+ NSString *helloWorld = @"Hello World";
17
+ NSString *helloInBase64 = [helloWorld base64String];
18
+ NSString *helloDecoded = [NSString stringFromBase64String:helloInBase64];
19
+
20
+
21
+
22
+
23
+ Performance
24
+ ----
25
+ * Encoding: Approximately 4 to 5 times faster than using the equivalent SecTransform.
26
+ * Encoding: 30% faster than https://github.com/l4u/NSData-Base64
27
+ * Decoding: 5% faster than using the equivalent SecTransform.
28
+ * Decoding: 5% faster than https://github.com/l4u/NSData-Base64
29
+
30
+
31
+
32
+ Requirements
33
+ -----
34
+ * Compile with Automatic Reference Counting
35
+ * Compatible with Mac OSX 10.6+ and iOS 4.0+
36
+
37
+
38
+
39
+ Implementation
40
+ ----
41
+ * Implemented as per RFC 4648, see http://www.ietf.org/rfc/rfc4648.txt for more details.
42
+
43
+
44
+
45
+ Licensing
46
+ ----
47
+ * Public Domain