@hot-updater/react-native 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -5,6 +5,9 @@ React Native OTA solution for internal infrastructure
5
5
  * as-is
6
6
  ```objective-c
7
7
  // filename: ios/MyApp/AppDelegate.mm
8
+ // ...
9
+ #import <HotUpdater/HotUpdater.h>
10
+
8
11
  // ...
9
12
 
10
13
  - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
@@ -29,5 +32,5 @@ React Native OTA solution for internal infrastructure
29
32
  return [HotUpdater bundleURL];
30
33
  }
31
34
 
32
- / ...
35
+ // ...
33
36
  ```
@@ -5,19 +5,33 @@
5
5
  RCT_EXPORT_MODULE();
6
6
 
7
7
  static NSURL *_bundleURL = nil;
8
- static dispatch_once_t setBundleURLOnceToken;
9
8
 
10
9
  #pragma mark - Bundle URL Management
11
10
 
12
- + (void)setBundleURL:(NSURL *)url {
13
- dispatch_once(&setBundleURLOnceToken, ^{
14
- NSString *path = [self pathFromURL:url];
15
-
16
- if (![self downloadDataFromURL:url andSaveToPath:path]) {
17
- return;
18
- }
11
+ + (void)setVersionId:(NSString*)versionId {
12
+ static dispatch_once_t onceToken;
13
+ dispatch_once(&onceToken, ^{
14
+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
15
+ [defaults setObject:versionId forKey:@"HotUpdaterVersionId"];
16
+ [defaults synchronize];
17
+ });
18
+ }
19
+
20
+ + (NSString *)getVersionId {
21
+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
22
+ NSString *versionId = [defaults objectForKey:@"HotUpdaterVersionId"];
23
+ if (versionId && ![versionId isKindOfClass:[NSNull class]] && versionId.length > 0) {
24
+ return versionId;
25
+ } else {
26
+ return nil;
27
+ }
28
+ }
29
+
19
30
 
20
- _bundleURL = [NSURL fileURLWithPath:path];
31
+ + (void)setBundleURL:(NSString *)localPath {
32
+ static dispatch_once_t onceToken;
33
+ dispatch_once(&onceToken, ^{
34
+ _bundleURL = [NSURL fileURLWithPath:localPath];
21
35
  [[NSUserDefaults standardUserDefaults] setObject:[_bundleURL absoluteString] forKey:@"HotUpdaterBundleURL"];
22
36
  [[NSUserDefaults standardUserDefaults] synchronize];
23
37
  });
@@ -54,66 +68,94 @@ static dispatch_once_t setBundleURLOnceToken;
54
68
 
55
69
  #pragma mark - Utility Methods
56
70
 
57
- + (NSString *)pathForFilename:(NSString *)filename {
58
- return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:filename];
71
+ + (NSString *)convertFileSystemPathFromBasePath:(NSString *)basePath {
72
+ return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:basePath];
59
73
  }
60
74
 
61
- + (NSString *)pathFromURL:(NSURL *)url {
62
- NSString *pathComponent = url.path;
63
-
64
- if ([pathComponent hasPrefix:@"/"]) {
65
- pathComponent = [pathComponent substringFromIndex:1];
75
+ + (NSString *)removePrefixFromPath:(NSString *)path prefix:(NSString *)prefix {
76
+ if ([path hasPrefix:[NSString stringWithFormat:@"/%@/", prefix]]) {
77
+ return [path stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"/%@/", prefix] withString:@""];
66
78
  }
67
-
68
- return [self pathForFilename:pathComponent];
79
+ return path;
69
80
  }
70
81
 
71
- + (BOOL)downloadDataFromURL:(NSURL *)url andSaveToPath:(NSString *)path {
72
- NSData *data = [NSData dataWithContentsOfURL:url];
73
-
74
- if (!data) {
75
- NSLog(@"Failed to download data from URL: %@", url);
76
- return NO;
77
- }
78
-
79
- NSFileManager *fileManager = [NSFileManager defaultManager];
80
- NSError *folderError;
81
- if (![fileManager createDirectoryAtPath:[path stringByDeletingLastPathComponent]
82
- withIntermediateDirectories:YES
83
- attributes:nil
84
- error:&folderError]) {
85
- NSLog(@"Failed to create folder: %@", folderError);
86
- return NO;
82
+ + (BOOL)downloadFilesFromURLs:(NSArray<NSURL *> *)urls prefix:(NSString *)prefix {
83
+ NSOperationQueue *queue = [[NSOperationQueue alloc] init];
84
+ queue.maxConcurrentOperationCount = urls.count;
85
+
86
+ __block BOOL allSuccess = YES;
87
+ dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
88
+
89
+ for (NSURL *url in urls) {
90
+ NSString *filename = [url lastPathComponent];
91
+ NSString *basePath = [self removePrefixFromPath:[url path] prefix:prefix];
92
+ NSString *path = [self convertFileSystemPathFromBasePath:basePath];
93
+
94
+ [queue addOperationWithBlock:^{
95
+ NSData *data = [NSData dataWithContentsOfURL:url];
96
+
97
+ if (!data) {
98
+ NSLog(@"Failed to download data from URL: %@", url);
99
+ allSuccess = NO;
100
+ dispatch_semaphore_signal(semaphore);
101
+ return;
102
+ }
103
+
104
+ NSFileManager *fileManager = [NSFileManager defaultManager];
105
+ NSError *folderError;
106
+ if (![fileManager createDirectoryAtPath:[path stringByDeletingLastPathComponent]
107
+ withIntermediateDirectories:YES
108
+ attributes:nil
109
+ error:&folderError]) {
110
+ NSLog(@"Failed to create folder: %@", folderError);
111
+ allSuccess = NO;
112
+ dispatch_semaphore_signal(semaphore);
113
+ return;
114
+ }
115
+
116
+ NSError *error;
117
+ [data writeToFile:path options:NSDataWritingAtomic error:&error];
118
+
119
+ if (error) {
120
+ NSLog(@"Failed to save data: %@", error);
121
+ allSuccess = NO;
122
+ }
123
+
124
+ if ([filename hasPrefix:@"index"] && [filename hasSuffix:@".bundle"]) {
125
+ [self setBundleURL:path];
126
+ }
127
+ dispatch_semaphore_signal(semaphore);
128
+ }];
87
129
  }
88
130
 
89
- NSError *error;
90
- [data writeToFile:path options:NSDataWritingAtomic error:&error];
91
-
92
- if (error) {
93
- NSLog(@"Failed to save data: %@", error);
94
- return NO;
131
+ for (int i = 0; i < urls.count; i++) {
132
+ dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
95
133
  }
96
-
97
- return YES;
134
+
135
+ [self setVersionId:prefix];
136
+ NSLog(@"Downloaded all files.");
137
+ return allSuccess;
98
138
  }
99
139
 
100
140
  #pragma mark - React Native Exports
101
141
 
102
- RCT_EXPORT_METHOD(getBundleURL:(RCTResponseSenderBlock)callback) {
103
- NSString *urlString = [HotUpdater.bundleURL absoluteString];
104
- callback(@[urlString]);
142
+ RCT_EXPORT_METHOD(getAppVersionId:(RCTResponseSenderBlock)callback) {
143
+ NSString *versionId = [HotUpdater getVersionId];
144
+ callback(@[versionId ?: [NSNull null]]);
105
145
  }
106
146
 
107
- RCT_EXPORT_METHOD(setBundleURL:(NSString *)urlString) {
108
- [HotUpdater setBundleURL:[NSURL URLWithString:urlString]];
109
- }
147
+ RCT_EXPORT_METHOD(downloadFilesFromURLs:(NSArray<NSString *> *)urlStrings prefix:(NSString *)prefix callback:(RCTResponseSenderBlock)callback) {
148
+ NSMutableArray<NSURL *> *urls = [NSMutableArray array];
149
+ for (NSString *urlString in urlStrings) {
150
+ NSLog(@"urlString: %@", urlString);
151
+ NSURL *url = [NSURL URLWithString:urlString];
110
152
 
111
- RCT_EXPORT_METHOD(downloadAndSave:(NSString *)urlString callback:(RCTResponseSenderBlock)callback) {
112
- NSURL *url = [NSURL URLWithString:urlString];
113
- NSString *path = [HotUpdater pathFromURL:url];
114
- NSLog(@"Downloading %@ to %@", url, path);
115
- BOOL success = [HotUpdater downloadDataFromURL:url andSaveToPath:path];
116
- callback(@[@(success)]);
117
- }
153
+ if (url) {
154
+ [urls addObject:url];
155
+ }
156
+ }
118
157
 
158
+ BOOL result = [HotUpdater downloadFilesFromURLs:urls prefix:prefix];
159
+ callback(@[@(result)]);
160
+ }
119
161
  @end
package/lib/index.d.ts CHANGED
@@ -1,22 +1,26 @@
1
+ import { HotUpdaterMetaData } from "./types";
1
2
  /**
2
- * Retrieves the bundle URL.
3
+ * Fetches the current app version id.
3
4
  *
4
- * @returns {Promise<string>} A promise that resolves to the bundle URL.
5
+ * @async
6
+ * @returns {Promise<string|null>} Resolves with the current version id or null if not available.
5
7
  */
6
- export declare const getBundleURL: () => Promise<string>;
8
+ export declare const getAppVersionId: () => Promise<string | null>;
7
9
  /**
8
- * Sets the bundle URL.
10
+ * Downloads files from given URLs.
9
11
  *
10
- * @param {string} url - The URL to be set as the bundle URL.
11
- * @returns {void} No return value.
12
+ * @async
13
+ * @param {string[]} urlStrings - An array of URL strings to download files from.
14
+ * @param {string} prefix - The prefix to be added to each file name.
15
+ * @returns {Promise<boolean>} Resolves with true if download was successful, otherwise rejects with an error.
12
16
  */
13
- export declare const setBundleURL: (url: string) => any;
14
- /**
15
- * Downloads and saves data from the given URL.
16
- *
17
- * @param {string} url - The URL to download data from.
18
- * @returns {Promise<boolean>} Resolves with `true` if the operation is successful, otherwise rejects with `false`.
19
- *
20
- */
21
- export declare const downloadAndSave: (url: string) => Promise<boolean>;
17
+ export declare const downloadFilesFromURLs: (urlStrings: string[], prefix: string) => Promise<boolean>;
18
+ export type HotUpdaterContext = {
19
+ ios: string;
20
+ android: string;
21
+ } | string;
22
+ export interface HotUpdaterInit {
23
+ metadata: HotUpdaterMetaData | (() => HotUpdaterMetaData) | (() => Promise<HotUpdaterMetaData>);
24
+ }
25
+ export declare const init: ({ metadata }: HotUpdaterInit) => Promise<void>;
22
26
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,eAAO,MAAM,YAAY,uBAIxB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,QAAS,MAAM,QAEvC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,QAAS,MAAM,qBAM1C,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAI7C;;;;;GAKG;AACH,eAAO,MAAM,eAAe,QAAa,QAAQ,MAAM,GAAG,IAAI,CAM7D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB,eACpB,MAAM,EAAE,UACZ,MAAM,KACb,QAAQ,OAAO,CAMjB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GACzB;IACE,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;CACjB,GACD,MAAM,CAAC;AAEX,MAAM,WAAW,cAAc;IAC7B,QAAQ,EACJ,kBAAkB,GAClB,CAAC,MAAM,kBAAkB,CAAC,GAC1B,CAAC,MAAM,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC;CACzC;AAED,eAAO,MAAM,IAAI,iBAAwB,cAAc,kBAYtD,CAAC"}
package/lib/index.js CHANGED
@@ -1,35 +1,101 @@
1
- import { NativeModules } from "react-native";
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { NativeModules, Platform } from "react-native";
2
38
  var HotUpdater = NativeModules.HotUpdater;
3
39
  /**
4
- * Retrieves the bundle URL.
40
+ * Fetches the current app version id.
5
41
  *
6
- * @returns {Promise<string>} A promise that resolves to the bundle URL.
42
+ * @async
43
+ * @returns {Promise<string|null>} Resolves with the current version id or null if not available.
7
44
  */
8
- export var getBundleURL = function () {
9
- return new Promise(function (resolve) {
10
- return HotUpdater.getBundleURL(function (url) { return resolve(url); });
45
+ export var getAppVersionId = function () { return __awaiter(void 0, void 0, void 0, function () {
46
+ return __generator(this, function (_a) {
47
+ return [2 /*return*/, new Promise(function (resolve) {
48
+ HotUpdater.getAppVersionId(function (versionId) {
49
+ resolve(versionId);
50
+ });
51
+ })];
11
52
  });
12
- };
53
+ }); };
13
54
  /**
14
- * Sets the bundle URL.
55
+ * Downloads files from given URLs.
15
56
  *
16
- * @param {string} url - The URL to be set as the bundle URL.
17
- * @returns {void} No return value.
57
+ * @async
58
+ * @param {string[]} urlStrings - An array of URL strings to download files from.
59
+ * @param {string} prefix - The prefix to be added to each file name.
60
+ * @returns {Promise<boolean>} Resolves with true if download was successful, otherwise rejects with an error.
18
61
  */
19
- export var setBundleURL = function (url) {
20
- return HotUpdater.setBundleURL(url);
62
+ export var downloadFilesFromURLs = function (urlStrings, prefix) {
63
+ return new Promise(function (resolve) {
64
+ HotUpdater.downloadFilesFromURLs(urlStrings, prefix, function (success) {
65
+ resolve(success);
66
+ });
67
+ });
21
68
  };
22
- /**
23
- * Downloads and saves data from the given URL.
24
- *
25
- * @param {string} url - The URL to download data from.
26
- * @returns {Promise<boolean>} Resolves with `true` if the operation is successful, otherwise rejects with `false`.
27
- *
28
- */
29
- export var downloadAndSave = function (url) {
30
- return new Promise(function (resolve, reject) {
31
- return HotUpdater.downloadAndSave(url, function (isSuccess) {
32
- return isSuccess ? resolve(true) : reject(false);
69
+ export var init = function (_a) {
70
+ var metadata = _a.metadata;
71
+ return __awaiter(void 0, void 0, void 0, function () {
72
+ var _b, files, id, _c, appVersionId;
73
+ return __generator(this, function (_d) {
74
+ switch (_d.label) {
75
+ case 0:
76
+ if (!["ios", "android"].includes(Platform.OS)) {
77
+ throw new Error("HotUpdater is only supported on iOS and Android");
78
+ }
79
+ if (!(typeof metadata === "function")) return [3 /*break*/, 2];
80
+ return [4 /*yield*/, metadata()];
81
+ case 1:
82
+ _c = _d.sent();
83
+ return [3 /*break*/, 3];
84
+ case 2:
85
+ _c = metadata;
86
+ _d.label = 3;
87
+ case 3:
88
+ _b = _c, files = _b.files, id = _b.id;
89
+ return [4 /*yield*/, getAppVersionId()];
90
+ case 4:
91
+ appVersionId = _d.sent();
92
+ if (!(id !== appVersionId && id != null)) return [3 /*break*/, 6];
93
+ return [4 /*yield*/, downloadFilesFromURLs(files, id)];
94
+ case 5:
95
+ _d.sent();
96
+ _d.label = 6;
97
+ case 6: return [2 /*return*/];
98
+ }
33
99
  });
34
100
  });
35
101
  };
package/lib/types.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export type Version = `${number}.${number}.${number}` | `${number}.${number}` | `${number}`;
2
+ export type HotUpdaterMetaData = {
3
+ files: string[];
4
+ version: Version;
5
+ id: string;
6
+ };
7
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GACf,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,EAAE,GAC/B,GAAG,MAAM,IAAI,MAAM,EAAE,GACrB,GAAG,MAAM,EAAE,CAAC;AAEhB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;IACjB,EAAE,EAAE,MAAM,CAAC;CACZ,CAAC"}
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "@hot-updater/react-native",
3
- "version": "0.0.1",
4
- "description": "React Native OTA solution for internal infrastructure",
3
+ "version": "0.0.2",
4
+ "description": "React Native OTA solution for self-hosted",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "files": [
8
- "src",
9
8
  "lib",
10
9
  "android",
11
10
  "ios",
@@ -22,7 +21,13 @@
22
21
  "!**/__mocks__",
23
22
  "!**/.*"
24
23
  ],
25
- "keywords": [],
24
+ "keywords": [
25
+ "react-native",
26
+ "code",
27
+ "push",
28
+ "code-push",
29
+ "self-hosted"
30
+ ],
26
31
  "license": "MIT",
27
32
  "repository": "https://github.com/gronxb/hot-updater",
28
33
  "author": "gronxb <gron1gh1@gmail.com> (https://github.com/gronxb)",
@@ -45,6 +50,7 @@
45
50
  },
46
51
  "scripts": {
47
52
  "preinstall": "npx only-allow pnpm",
53
+ "typecheck": "tsc --noEmit",
48
54
  "build": "tsc"
49
55
  }
50
56
  }
package/src/index.ts DELETED
@@ -1,39 +0,0 @@
1
- import { NativeModules } from "react-native";
2
-
3
- const { HotUpdater } = NativeModules;
4
-
5
- /**
6
- * Retrieves the bundle URL.
7
- *
8
- * @returns {Promise<string>} A promise that resolves to the bundle URL.
9
- */
10
- export const getBundleURL = () => {
11
- return new Promise<string>((resolve) =>
12
- HotUpdater.getBundleURL((url: string) => resolve(url))
13
- );
14
- };
15
-
16
- /**
17
- * Sets the bundle URL.
18
- *
19
- * @param {string} url - The URL to be set as the bundle URL.
20
- * @returns {void} No return value.
21
- */
22
- export const setBundleURL = (url: string) => {
23
- return HotUpdater.setBundleURL(url);
24
- };
25
-
26
- /**
27
- * Downloads and saves data from the given URL.
28
- *
29
- * @param {string} url - The URL to download data from.
30
- * @returns {Promise<boolean>} Resolves with `true` if the operation is successful, otherwise rejects with `false`.
31
- *
32
- */
33
- export const downloadAndSave = (url: string) => {
34
- return new Promise<boolean>((resolve, reject) =>
35
- HotUpdater.downloadAndSave(url, (isSuccess: boolean) =>
36
- isSuccess ? resolve(true) : reject(false)
37
- )
38
- );
39
- };