@hot-updater/core 0.1.6-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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Sungyu Kang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = function(exports1, definition) {
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = function(obj, prop) {
13
+ return Object.prototype.hasOwnProperty.call(obj, prop);
14
+ };
15
+ })();
16
+ (()=>{
17
+ __webpack_require__.r = function(exports1) {
18
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
19
+ value: 'Module'
20
+ });
21
+ Object.defineProperty(exports1, '__esModule', {
22
+ value: true
23
+ });
24
+ };
25
+ })();
26
+ var __webpack_exports__ = {};
27
+ __webpack_require__.r(__webpack_exports__);
28
+ __webpack_require__.d(__webpack_exports__, {
29
+ NIL_UUID: ()=>NIL_UUID
30
+ });
31
+ const NIL_UUID = "00000000-0000-0000-0000-000000000000";
32
+ var __webpack_export_target__ = exports;
33
+ for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
34
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
35
+ value: true
36
+ });
@@ -0,0 +1,2 @@
1
+ export type * from "./types";
2
+ export * from "./uuid";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ const NIL_UUID = "00000000-0000-0000-0000-000000000000";
2
+ export { NIL_UUID };
@@ -0,0 +1,2 @@
1
+ export * from "./setupGetUpdateInfoTestSuite";
2
+ export * from "./setupSemverSatisfiesTestSuite";
@@ -0,0 +1,4 @@
1
+ import type { Bundle, GetBundlesArgs, UpdateInfo } from "../types";
2
+ export declare const setupGetUpdateInfoTestSuite: ({ getUpdateInfo, }: {
3
+ getUpdateInfo: (bundles: Bundle[], options: GetBundlesArgs) => Promise<UpdateInfo | null>;
4
+ }) => void;
@@ -0,0 +1,20 @@
1
+ /**
2
+ *
3
+ * Filters based on semver. And sorts by the highest bundle version.
4
+ *
5
+ * * Range Expression Table:
6
+ *
7
+ * | Range Expression | Who gets the update |
8
+ * |------------------|------------------------------------------------------------------------|
9
+ * | 1.2.3 | Only devices running the specific binary app store version 1.2.3 of your app |
10
+ * | * | Any device configured to consume updates from your CodePush app |
11
+ * | 1.2.x | Devices running major version 1, minor version 2 and any patch version of your app |
12
+ * | 1.2.3 - 1.2.7 | Devices running any binary version between 1.2.3 (inclusive) and 1.2.7 (inclusive) |
13
+ * | >=1.2.3 <1.2.7 | Devices running any binary version between 1.2.3 (inclusive) and 1.2.7 (exclusive) |
14
+ * | 1.2 | Equivalent to >=1.2.0 <1.3.0 |
15
+ * | ~1.2.3 | Equivalent to >=1.2.3 <1.3.0 |
16
+ * | ^1.2.3 | Equivalent to >=1.2.3 <2.0.0 |
17
+ */
18
+ export declare const setupSemverSatisfiesTestSuite: ({ semverSatisfies, }: {
19
+ semverSatisfies: (targetVersion: string, currentVersion: string) => Promise<boolean> | boolean;
20
+ }) => void;
@@ -0,0 +1,53 @@
1
+ export type Platform = "ios" | "android";
2
+ export interface Bundle {
3
+ /**
4
+ * The unique identifier for the bundle. uuidv7
5
+ */
6
+ id: string;
7
+ /**
8
+ * The platform the bundle is for.
9
+ */
10
+ platform: Platform;
11
+ /**
12
+ * The target version of the bundle.
13
+ */
14
+ targetVersion: string;
15
+ /**
16
+ * Whether the bundle should force an update.
17
+ */
18
+ forceUpdate: boolean;
19
+ /**
20
+ * Whether the bundle is enabled.
21
+ */
22
+ enabled: boolean;
23
+ /**
24
+ * The file URL of the bundle.
25
+ */
26
+ fileUrl: string;
27
+ /**
28
+ * The hash of the bundle.
29
+ */
30
+ fileHash: string;
31
+ /**
32
+ * The git commit hash of the bundle.
33
+ */
34
+ gitCommitHash: string | null;
35
+ /**
36
+ * The message of the bundle.
37
+ */
38
+ message: string | null;
39
+ }
40
+ export type BundleArg = string | Bundle[] | (() => Promise<Bundle[]>) | (() => Bundle[]);
41
+ export type UpdateStatus = "ROLLBACK" | "UPDATE";
42
+ export interface UpdateInfo {
43
+ id: string;
44
+ forceUpdate: boolean;
45
+ fileUrl: string | null;
46
+ fileHash: string | null;
47
+ status: UpdateStatus;
48
+ }
49
+ export interface GetBundlesArgs {
50
+ platform: Platform;
51
+ bundleId: string;
52
+ appVersion: string;
53
+ }
package/dist/uuid.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const NIL_UUID = "00000000-0000-0000-0000-000000000000";
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@hot-updater/core",
3
+ "version": "0.1.6-0",
4
+ "type": "module",
5
+ "description": "React Native OTA solution for self-hosted",
6
+ "sideEffects": false,
7
+ "main": "dist/index.cjs",
8
+ "module": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ },
16
+ "./test-utils": {
17
+ "types": "./src/test-utils/index.ts",
18
+ "import": "./src/test-utils/index.ts",
19
+ "require": "./src/test-utils/index.ts"
20
+ }
21
+ },
22
+ "files": [
23
+ "src",
24
+ "dist",
25
+ "package.json"
26
+ ],
27
+ "keywords": [
28
+ "react-native",
29
+ "react-native-code-push",
30
+ "code-push",
31
+ "eas",
32
+ "eas-update",
33
+ "expo",
34
+ "expo-update",
35
+ "self-hosted"
36
+ ],
37
+ "license": "MIT",
38
+ "repository": "https://github.com/gronxb/hot-updater",
39
+ "author": "gronxb <gron1gh1@gmail.com> (https://github.com/gronxb)",
40
+ "bugs": {
41
+ "url": "https://github.com/gronxb/hot-updater/issues"
42
+ },
43
+ "homepage": "https://github.com/gronxb/hot-updater#readme",
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "devDependencies": {
48
+ "@rslib/core": "^0.2.2",
49
+ "@types/node": "^20.9.4",
50
+ "vitest": "^2.1.8"
51
+ },
52
+ "scripts": {
53
+ "build": "rslib build",
54
+ "test:type": "tsc --noEmit"
55
+ }
56
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type * from "./types";
2
+ export * from "./uuid";
@@ -0,0 +1,2 @@
1
+ export * from "./setupGetUpdateInfoTestSuite";
2
+ export * from "./setupSemverSatisfiesTestSuite";
@@ -0,0 +1,556 @@
1
+ import { expect, it } from "vitest";
2
+ import type { Bundle, GetBundlesArgs, UpdateInfo } from "../types";
3
+ import { NIL_UUID } from "../uuid";
4
+
5
+ const DEFAULT_BUNDLE = {
6
+ fileUrl: "http://example.com/bundle.zip",
7
+ fileHash: "hash",
8
+ platform: "ios",
9
+ gitCommitHash: null,
10
+ message: null,
11
+ } as const;
12
+
13
+ export const setupGetUpdateInfoTestSuite = ({
14
+ getUpdateInfo,
15
+ }: {
16
+ getUpdateInfo: (
17
+ bundles: Bundle[],
18
+ options: GetBundlesArgs,
19
+ ) => Promise<UpdateInfo | null>;
20
+ }) => {
21
+ it("applies an update when a '*' bundle is available", async () => {
22
+ const bundles: Bundle[] = [
23
+ {
24
+ ...DEFAULT_BUNDLE,
25
+ targetVersion: "*",
26
+ forceUpdate: false,
27
+ enabled: true,
28
+ id: "00000000-0000-0000-0000-000000000001",
29
+ },
30
+ ];
31
+
32
+ const update = await getUpdateInfo(bundles, {
33
+ appVersion: "1.0",
34
+ bundleId: NIL_UUID,
35
+ platform: "ios",
36
+ });
37
+
38
+ expect(update).toStrictEqual({
39
+ id: "00000000-0000-0000-0000-000000000001",
40
+ fileUrl: "http://example.com/bundle.zip",
41
+ fileHash: "hash",
42
+ forceUpdate: false,
43
+ status: "UPDATE",
44
+ });
45
+ });
46
+
47
+ it("returns null when no bundles are provided", async () => {
48
+ const bundles: Bundle[] = [];
49
+
50
+ const update = await getUpdateInfo(bundles, {
51
+ appVersion: "1.0",
52
+ bundleId: NIL_UUID,
53
+ platform: "ios",
54
+ });
55
+ expect(update).toBeNull();
56
+ });
57
+
58
+ it("returns null when the app version does not qualify for the available higher version", async () => {
59
+ const bundles: Bundle[] = [
60
+ {
61
+ ...DEFAULT_BUNDLE,
62
+ targetVersion: "1.1",
63
+ enabled: true,
64
+ id: "00000000-0000-0000-0000-000000000001",
65
+ forceUpdate: false,
66
+ },
67
+ ];
68
+
69
+ const update = await getUpdateInfo(bundles, {
70
+ appVersion: "1.0",
71
+ bundleId: NIL_UUID,
72
+ platform: "ios",
73
+ });
74
+ expect(update).toBeNull();
75
+ });
76
+
77
+ it("applies an update when a higher semver-compatible bundle is available", async () => {
78
+ const bundles: Bundle[] = [
79
+ {
80
+ ...DEFAULT_BUNDLE,
81
+ targetVersion: "1.x.x",
82
+ enabled: true,
83
+ id: "00000000-0000-0000-0000-000000000001",
84
+ forceUpdate: false,
85
+ },
86
+ {
87
+ ...DEFAULT_BUNDLE,
88
+ targetVersion: "1.0",
89
+ enabled: true,
90
+ id: "00000000-0000-0000-0000-000000000002",
91
+ forceUpdate: false,
92
+ },
93
+ ];
94
+
95
+ const update = await getUpdateInfo(bundles, {
96
+ appVersion: "1.0",
97
+ bundleId: NIL_UUID,
98
+ platform: "ios",
99
+ });
100
+ expect(update).toStrictEqual({
101
+ id: "00000000-0000-0000-0000-000000000002",
102
+ forceUpdate: false,
103
+ status: "UPDATE",
104
+ fileUrl: "http://example.com/bundle.zip",
105
+ fileHash: "hash",
106
+ });
107
+ });
108
+
109
+ it("applies an update if forceUpdate is true for a matching version", async () => {
110
+ const bundles: Bundle[] = [
111
+ {
112
+ ...DEFAULT_BUNDLE,
113
+ targetVersion: "1.0",
114
+ enabled: true,
115
+ id: "00000000-0000-0000-0000-000000000001",
116
+ forceUpdate: true,
117
+ },
118
+ ];
119
+ const update = await getUpdateInfo(bundles, {
120
+ appVersion: "1.0",
121
+ bundleId: NIL_UUID,
122
+ platform: "ios",
123
+ });
124
+
125
+ expect(update).toStrictEqual({
126
+ id: "00000000-0000-0000-0000-000000000001",
127
+ forceUpdate: true,
128
+ status: "UPDATE",
129
+ fileUrl: "http://example.com/bundle.zip",
130
+ fileHash: "hash",
131
+ });
132
+ });
133
+
134
+ it("applies an update for a matching version even if forceUpdate is false", async () => {
135
+ const bundles: Bundle[] = [
136
+ {
137
+ ...DEFAULT_BUNDLE,
138
+ targetVersion: "1.0",
139
+ enabled: true,
140
+ id: "00000000-0000-0000-0000-000000000001",
141
+ forceUpdate: false,
142
+ },
143
+ ];
144
+
145
+ const update = await getUpdateInfo(bundles, {
146
+ appVersion: "1.0",
147
+ bundleId: NIL_UUID,
148
+ platform: "ios",
149
+ });
150
+ expect(update).toStrictEqual({
151
+ id: "00000000-0000-0000-0000-000000000001",
152
+ forceUpdate: false,
153
+ status: "UPDATE",
154
+ fileUrl: "http://example.com/bundle.zip",
155
+ fileHash: "hash",
156
+ });
157
+ });
158
+
159
+ it("applies an update when the app version is the same but the bundle is still considered higher", async () => {
160
+ const bundles: Bundle[] = [
161
+ {
162
+ ...DEFAULT_BUNDLE,
163
+ targetVersion: "1.0",
164
+ forceUpdate: false,
165
+ enabled: true,
166
+ id: "00000000-0000-0000-0000-000000000005",
167
+ },
168
+ ];
169
+
170
+ const update = await getUpdateInfo(bundles, {
171
+ appVersion: "1.0",
172
+ bundleId: NIL_UUID,
173
+ platform: "ios",
174
+ });
175
+ expect(update).toStrictEqual({
176
+ id: "00000000-0000-0000-0000-000000000005",
177
+ forceUpdate: false,
178
+ fileUrl: "http://example.com/bundle.zip",
179
+ fileHash: "hash",
180
+ status: "UPDATE",
181
+ });
182
+ });
183
+
184
+ it("falls back to an older enabled bundle when the latest is disabled", async () => {
185
+ const bundles: Bundle[] = [
186
+ {
187
+ ...DEFAULT_BUNDLE,
188
+ targetVersion: "1.0",
189
+ forceUpdate: true,
190
+ enabled: false, // Disabled
191
+ id: "00000000-0000-0000-0000-000000000002",
192
+ },
193
+ {
194
+ ...DEFAULT_BUNDLE,
195
+ targetVersion: "1.0",
196
+ forceUpdate: false,
197
+ enabled: true,
198
+ id: "00000000-0000-0000-0000-000000000001",
199
+ },
200
+ ];
201
+
202
+ const update = await getUpdateInfo(bundles, {
203
+ appVersion: "1.0",
204
+ bundleId: NIL_UUID,
205
+ platform: "ios",
206
+ });
207
+ expect(update).toStrictEqual({
208
+ fileUrl: "http://example.com/bundle.zip",
209
+ fileHash: "hash",
210
+ id: "00000000-0000-0000-0000-000000000001",
211
+ forceUpdate: false,
212
+ status: "UPDATE",
213
+ });
214
+ });
215
+
216
+ it("returns null if all bundles are disabled", async () => {
217
+ const bundles: Bundle[] = [
218
+ {
219
+ ...DEFAULT_BUNDLE,
220
+ targetVersion: "1.0",
221
+ forceUpdate: true,
222
+ enabled: false, // Disabled
223
+ id: "00000000-0000-0000-0000-000000000002",
224
+ },
225
+ {
226
+ ...DEFAULT_BUNDLE,
227
+ targetVersion: "1.0",
228
+ forceUpdate: false,
229
+ enabled: false, // Disabled
230
+ id: "00000000-0000-0000-0000-000000000001",
231
+ },
232
+ ];
233
+
234
+ const update = await getUpdateInfo(bundles, {
235
+ appVersion: "1.0",
236
+ bundleId: NIL_UUID,
237
+ platform: "ios",
238
+ });
239
+ expect(update).toBeNull();
240
+ });
241
+
242
+ it("triggers a rollback if the latest bundle is disabled and no other updates are enabled", async () => {
243
+ const bundles: Bundle[] = [
244
+ {
245
+ ...DEFAULT_BUNDLE,
246
+ targetVersion: "1.0",
247
+ forceUpdate: true,
248
+ enabled: false, // Disabled
249
+ id: "00000000-0000-0000-0000-000000000002",
250
+ },
251
+ {
252
+ ...DEFAULT_BUNDLE,
253
+ targetVersion: "1.0",
254
+ forceUpdate: false,
255
+ enabled: false, // Disabled
256
+ id: "00000000-0000-0000-0000-000000000001",
257
+ },
258
+ ];
259
+
260
+ const update = await getUpdateInfo(bundles, {
261
+ appVersion: "1.0",
262
+ bundleId: NIL_UUID,
263
+ platform: "ios",
264
+ });
265
+ expect(update).toStrictEqual(null);
266
+ });
267
+
268
+ it("applies an update when a same-version bundle is available and enabled", async () => {
269
+ const bundles: Bundle[] = [
270
+ {
271
+ ...DEFAULT_BUNDLE,
272
+ forceUpdate: false,
273
+ fileUrl: "20240722210327/build.zip",
274
+ fileHash:
275
+ "a5cbf59a627759a88d472c502423ff55a4f6cd1aafeed3536f6a5f6e870c2290",
276
+ message: "",
277
+ targetVersion: "1.0",
278
+ id: "00000000-0000-0000-0000-000000000001",
279
+ enabled: true,
280
+ },
281
+ ];
282
+
283
+ const update = await getUpdateInfo(bundles, {
284
+ appVersion: "1.0",
285
+ bundleId: NIL_UUID,
286
+ platform: "ios",
287
+ });
288
+ expect(update).toStrictEqual({
289
+ id: "00000000-0000-0000-0000-000000000001",
290
+ forceUpdate: false,
291
+ status: "UPDATE",
292
+ fileUrl: "20240722210327/build.zip",
293
+ fileHash:
294
+ "a5cbf59a627759a88d472c502423ff55a4f6cd1aafeed3536f6a5f6e870c2290",
295
+ });
296
+ });
297
+
298
+ it("forces a rollback if no matching bundle exists for the provided bundleId", async () => {
299
+ const bundles: Bundle[] = [];
300
+
301
+ const update = await getUpdateInfo(bundles, {
302
+ appVersion: "1.0",
303
+ bundleId: "00000000-0000-0000-0000-000000000002",
304
+ platform: "ios",
305
+ });
306
+ expect(update).toStrictEqual({
307
+ fileUrl: null,
308
+ fileHash: null,
309
+ id: NIL_UUID,
310
+ forceUpdate: true, // Cause the app to reload
311
+ status: "ROLLBACK",
312
+ });
313
+ });
314
+
315
+ it("returns null if the user is already up-to-date with an available bundle", async () => {
316
+ const bundles: Bundle[] = [
317
+ {
318
+ ...DEFAULT_BUNDLE,
319
+ targetVersion: "1.0",
320
+ forceUpdate: false,
321
+ enabled: true,
322
+ id: "00000000-0000-0000-0000-000000000002",
323
+ },
324
+ {
325
+ ...DEFAULT_BUNDLE,
326
+ targetVersion: "1.0",
327
+ forceUpdate: false,
328
+ enabled: true,
329
+ id: "00000000-0000-0000-0000-000000000001",
330
+ },
331
+ ];
332
+
333
+ const update = await getUpdateInfo(bundles, {
334
+ appVersion: "1.0",
335
+ bundleId: "00000000-0000-0000-0000-000000000002",
336
+ platform: "ios",
337
+ });
338
+ expect(update).toBeNull();
339
+ });
340
+
341
+ it("triggers a rollback if the previously used bundle no longer exists", async () => {
342
+ const bundles: Bundle[] = [
343
+ {
344
+ ...DEFAULT_BUNDLE,
345
+ targetVersion: "1.0",
346
+ forceUpdate: false,
347
+ enabled: true,
348
+ id: "00000000-0000-0000-0000-000000000001",
349
+ },
350
+ ];
351
+
352
+ const update = await getUpdateInfo(bundles, {
353
+ appVersion: "1.0",
354
+ bundleId: "00000000-0000-0000-0000-000000000002",
355
+ platform: "ios",
356
+ });
357
+ expect(update).toStrictEqual({
358
+ fileHash: "hash",
359
+ fileUrl: "http://example.com/bundle.zip",
360
+ id: "00000000-0000-0000-0000-000000000001",
361
+ forceUpdate: true,
362
+ status: "ROLLBACK",
363
+ });
364
+ });
365
+
366
+ it("selects the next available bundle even if forceUpdate is false", async () => {
367
+ const bundles: Bundle[] = [
368
+ {
369
+ ...DEFAULT_BUNDLE,
370
+ targetVersion: "1.0",
371
+ forceUpdate: false,
372
+ enabled: true,
373
+ id: "00000000-0000-0000-0000-000000000003",
374
+ },
375
+ {
376
+ ...DEFAULT_BUNDLE,
377
+ targetVersion: "1.0",
378
+ forceUpdate: false,
379
+ enabled: true,
380
+ id: "00000000-0000-0000-0000-000000000002",
381
+ },
382
+ {
383
+ ...DEFAULT_BUNDLE,
384
+ targetVersion: "1.0",
385
+ forceUpdate: false,
386
+ enabled: true,
387
+ id: "00000000-0000-0000-0000-000000000001",
388
+ },
389
+ ];
390
+
391
+ const update = await getUpdateInfo(bundles, {
392
+ appVersion: "1.0",
393
+ bundleId: "00000000-0000-0000-0000-000000000002",
394
+ platform: "ios",
395
+ });
396
+ expect(update).toStrictEqual({
397
+ fileUrl: "http://example.com/bundle.zip",
398
+ fileHash: "hash",
399
+ id: "00000000-0000-0000-0000-000000000003",
400
+ forceUpdate: false,
401
+ status: "UPDATE",
402
+ });
403
+ });
404
+
405
+ it("applies the highest available bundle even if the app version is unchanged", async () => {
406
+ const bundles: Bundle[] = [
407
+ {
408
+ ...DEFAULT_BUNDLE,
409
+ targetVersion: "1.0",
410
+ forceUpdate: false,
411
+ enabled: true,
412
+ id: "00000000-0000-0000-0000-000000000005", // Higher than the current version
413
+ },
414
+ {
415
+ ...DEFAULT_BUNDLE,
416
+ targetVersion: "1.0",
417
+ forceUpdate: false,
418
+ enabled: true,
419
+ id: "00000000-0000-0000-0000-000000000004",
420
+ },
421
+ {
422
+ ...DEFAULT_BUNDLE,
423
+ platform: "ios",
424
+ targetVersion: "1.0",
425
+ forceUpdate: false,
426
+ enabled: true,
427
+ id: "00000000-0000-0000-0000-000000000003",
428
+ },
429
+ {
430
+ ...DEFAULT_BUNDLE,
431
+ targetVersion: "1.0",
432
+ forceUpdate: false,
433
+ enabled: true,
434
+ id: "00000000-0000-0000-0000-000000000002",
435
+ },
436
+ {
437
+ ...DEFAULT_BUNDLE,
438
+ targetVersion: "1.0",
439
+ forceUpdate: false,
440
+ enabled: true,
441
+ id: "00000000-0000-0000-0000-000000000001",
442
+ },
443
+ ];
444
+
445
+ const update = await getUpdateInfo(bundles, {
446
+ appVersion: "1.0",
447
+ bundleId: "00000000-0000-0000-0000-000000000002",
448
+ platform: "ios",
449
+ });
450
+ expect(update).toStrictEqual({
451
+ fileUrl: "http://example.com/bundle.zip",
452
+ fileHash: "hash",
453
+ id: "00000000-0000-0000-0000-000000000005",
454
+ forceUpdate: false,
455
+ status: "UPDATE",
456
+ });
457
+ });
458
+
459
+ it("returns null if the newest matching bundle is disabled", async () => {
460
+ const bundles: Bundle[] = [
461
+ {
462
+ ...DEFAULT_BUNDLE,
463
+ targetVersion: "1.0",
464
+ forceUpdate: true,
465
+ enabled: false, // Disabled
466
+ id: "00000000-0000-0000-0000-000000000003",
467
+ },
468
+ {
469
+ ...DEFAULT_BUNDLE,
470
+ targetVersion: "1.0",
471
+ forceUpdate: true,
472
+ enabled: true,
473
+ id: "00000000-0000-0000-0000-000000000002",
474
+ },
475
+ {
476
+ ...DEFAULT_BUNDLE,
477
+ targetVersion: "1.0",
478
+ forceUpdate: false,
479
+ enabled: true,
480
+ id: "00000000-0000-0000-0000-000000000001",
481
+ },
482
+ ];
483
+
484
+ const update = await getUpdateInfo(bundles, {
485
+ appVersion: "1.0",
486
+ bundleId: "00000000-0000-0000-0000-000000000002",
487
+ platform: "ios",
488
+ });
489
+ expect(update).toBeNull();
490
+ });
491
+
492
+ it("rolls back to an older enabled bundle if the current one is disabled", async () => {
493
+ const bundles: Bundle[] = [
494
+ {
495
+ ...DEFAULT_BUNDLE,
496
+ targetVersion: "1.0",
497
+ forceUpdate: true,
498
+ enabled: false, // Disabled
499
+ id: "00000000-0000-0000-0000-000000000002",
500
+ },
501
+ {
502
+ ...DEFAULT_BUNDLE,
503
+ targetVersion: "1.0",
504
+ forceUpdate: false,
505
+ enabled: true,
506
+ id: "00000000-0000-0000-0000-000000000001",
507
+ },
508
+ ];
509
+
510
+ const update = await getUpdateInfo(bundles, {
511
+ appVersion: "1.0",
512
+ bundleId: "00000000-0000-0000-0000-000000000002",
513
+ platform: "ios",
514
+ });
515
+
516
+ expect(update).toStrictEqual({
517
+ fileUrl: "http://example.com/bundle.zip",
518
+ fileHash: "hash",
519
+ id: "00000000-0000-0000-0000-000000000001",
520
+ forceUpdate: true, // Cause the app to reload
521
+ status: "ROLLBACK",
522
+ });
523
+ });
524
+
525
+ it("rolls back to the original bundle when all available bundles are disabled", async () => {
526
+ const bundles: Bundle[] = [
527
+ {
528
+ ...DEFAULT_BUNDLE,
529
+ targetVersion: "1.0",
530
+ forceUpdate: true,
531
+ enabled: false, // Disabled
532
+ id: "00000000-0000-0000-0000-000000000002",
533
+ },
534
+ {
535
+ ...DEFAULT_BUNDLE,
536
+ targetVersion: "1.0",
537
+ forceUpdate: false,
538
+ enabled: false, // Disabled
539
+ id: "00000000-0000-0000-0000-000000000001",
540
+ },
541
+ ];
542
+
543
+ const update = await getUpdateInfo(bundles, {
544
+ appVersion: "1.0",
545
+ bundleId: "00000000-0000-0000-0000-000000000002",
546
+ platform: "ios",
547
+ });
548
+ expect(update).toStrictEqual({
549
+ id: NIL_UUID,
550
+ fileUrl: null,
551
+ fileHash: null,
552
+ forceUpdate: true, // Cause the app to reload
553
+ status: "ROLLBACK",
554
+ });
555
+ });
556
+ };
@@ -0,0 +1,139 @@
1
+ import { expect, it } from "vitest";
2
+
3
+ /**
4
+ *
5
+ * Filters based on semver. And sorts by the highest bundle version.
6
+ *
7
+ * * Range Expression Table:
8
+ *
9
+ * | Range Expression | Who gets the update |
10
+ * |------------------|------------------------------------------------------------------------|
11
+ * | 1.2.3 | Only devices running the specific binary app store version 1.2.3 of your app |
12
+ * | * | Any device configured to consume updates from your CodePush app |
13
+ * | 1.2.x | Devices running major version 1, minor version 2 and any patch version of your app |
14
+ * | 1.2.3 - 1.2.7 | Devices running any binary version between 1.2.3 (inclusive) and 1.2.7 (inclusive) |
15
+ * | >=1.2.3 <1.2.7 | Devices running any binary version between 1.2.3 (inclusive) and 1.2.7 (exclusive) |
16
+ * | 1.2 | Equivalent to >=1.2.0 <1.3.0 |
17
+ * | ~1.2.3 | Equivalent to >=1.2.3 <1.3.0 |
18
+ * | ^1.2.3 | Equivalent to >=1.2.3 <2.0.0 |
19
+ */
20
+ export const setupSemverSatisfiesTestSuite = ({
21
+ semverSatisfies,
22
+ }: {
23
+ semverSatisfies: (
24
+ targetVersion: string,
25
+ currentVersion: string,
26
+ ) => Promise<boolean> | boolean;
27
+ }) => {
28
+ it("version 1.2.3 should satisfy version 1.2.3", async () => {
29
+ expect(await semverSatisfies("1.2.3", "1.2.3")).toBe(true);
30
+ });
31
+
32
+ it("version 1.2.4 should not satisfy version 1.2.3", async () => {
33
+ expect(await semverSatisfies("1.2.3", "1.2.4")).toBe(false);
34
+ });
35
+
36
+ it("version 1.2.4 should not satisfy version 1.2.3", async () => {
37
+ expect(await semverSatisfies("1.2.3", "1.2.4")).toBe(false);
38
+ });
39
+
40
+ it("version 1.2.4 should not satisfy version 1.2.3", async () => {
41
+ expect(await semverSatisfies("1.2.3", "1.2.4")).toBe(false);
42
+ });
43
+
44
+ it("1.x.x should satisfy version 1.0", async () => {
45
+ expect(await semverSatisfies("1.x.x", "1.0")).toBe(true);
46
+ });
47
+
48
+ it("1.x.x should satisfy version 1.12", async () => {
49
+ expect(await semverSatisfies("1.x.x", "1.12")).toBe(true);
50
+ });
51
+
52
+ it("1.x.x should satisfy version 1.0.0", async () => {
53
+ expect(await semverSatisfies("1.x.x", "1.0.0")).toBe(true);
54
+ });
55
+
56
+ it("1.x.x should satisfy version 1.2.3", async () => {
57
+ expect(await semverSatisfies("1.x.x", "1.2.3")).toBe(true);
58
+ });
59
+
60
+ it("1.x.x should not satisfy version 2.0.0", async () => {
61
+ expect(await semverSatisfies("1.x.x", "2.0.0")).toBe(false);
62
+ });
63
+
64
+ it("1.x.x should not satisfy version 2.0.0", async () => {
65
+ expect(await semverSatisfies("1.x.x", "2.0.0")).toBe(false);
66
+ });
67
+
68
+ it("1.2.x should satisfy version 1.2.5", async () => {
69
+ expect(await semverSatisfies("1.2.x", "1.2.5")).toBe(true);
70
+ });
71
+
72
+ it("1.2.x should not satisfy version 1.3.0", async () => {
73
+ expect(await semverSatisfies("1.2.x", "1.3.0")).toBe(false);
74
+ });
75
+
76
+ it("1.2.x should not satisfy version 1.3.0", async () => {
77
+ expect(await semverSatisfies("1.2.x", "1.3.0")).toBe(false);
78
+ });
79
+
80
+ it("range 1.2.3-1.2.7 should satisfy version 1.2.5", async () => {
81
+ expect(await semverSatisfies("1.2.3 - 1.2.7", "1.2.5")).toBe(true);
82
+ });
83
+
84
+ it("range 1.2.3-1.2.7 should satisfy version 1.2.5", async () => {
85
+ expect(await semverSatisfies("1.2.3 - 1.2.7", "1.2.5")).toBe(true);
86
+ });
87
+
88
+ it("range 1.2.3-1.2.7 should not satisfy version 1.3.0", async () => {
89
+ expect(await semverSatisfies("1.2.3 - 1.2.7", "1.3.0")).toBe(false);
90
+ });
91
+
92
+ it("range >=1.2.3 <1.2.7 should satisfy version 1.2.5", async () => {
93
+ expect(await semverSatisfies(">=1.2.3 <1.2.7", "1.2.5")).toBe(true);
94
+ });
95
+
96
+ it("range >=1.2.3 <1.2.7 should satisfy version 1.2.5", async () => {
97
+ expect(await semverSatisfies(">=1.2.3 <1.2.7", "1.2.5")).toBe(true);
98
+ });
99
+
100
+ it("range >=1.2.3 <1.2.7 should not satisfy version 1.2.7", async () => {
101
+ expect(await semverSatisfies(">=1.2.3 <1.2.7", "1.2.7")).toBe(false);
102
+ });
103
+
104
+ it("~1.2.3 should satisfy version 1.2.3", async () => {
105
+ expect(await semverSatisfies("~1.2.3", "1.2.3")).toBe(true);
106
+ });
107
+
108
+ it("~1.2.3 should satisfy version 1.2.4", async () => {
109
+ expect(await semverSatisfies("~1.2.3", "1.2.4")).toBe(true);
110
+ });
111
+
112
+ it("~1.2.3 should not satisfy version 1.3.0", async () => {
113
+ expect(await semverSatisfies("~1.2.3", "1.3.0")).toBe(false);
114
+ });
115
+
116
+ it("~1.2.3 should satisfy version 1.2.3", async () => {
117
+ expect(await semverSatisfies("~1.2.3", "1.2.3")).toBe(true);
118
+ });
119
+
120
+ it("~1.2.3 should satisfy version 1.2.4", async () => {
121
+ expect(await semverSatisfies("~1.2.3", "1.2.4")).toBe(true);
122
+ });
123
+
124
+ it("~1.2.3 should not satisfy version 1.3.0", async () => {
125
+ expect(await semverSatisfies("~1.2.3", "1.3.0")).toBe(false);
126
+ });
127
+
128
+ it("^1.2.3 should satisfy version 1.3.0", async () => {
129
+ expect(await semverSatisfies("^1.2.3", "1.3.0")).toBe(true);
130
+ });
131
+
132
+ it("^1.2.3 should satisfy version 1.3.0", async () => {
133
+ expect(await semverSatisfies("^1.2.3", "1.3.0")).toBe(true);
134
+ });
135
+
136
+ it("^1.2.3 should not satisfy version 2.0.0", async () => {
137
+ expect(await semverSatisfies("^1.2.3", "2.0.0")).toBe(false);
138
+ });
139
+ };
package/src/types.ts ADDED
@@ -0,0 +1,62 @@
1
+ export type Platform = "ios" | "android";
2
+
3
+ export interface Bundle {
4
+ /**
5
+ * The unique identifier for the bundle. uuidv7
6
+ */
7
+ id: string;
8
+ /**
9
+ * The platform the bundle is for.
10
+ */
11
+ platform: Platform;
12
+ /**
13
+ * The target version of the bundle.
14
+ */
15
+ targetVersion: string;
16
+ /**
17
+ * Whether the bundle should force an update.
18
+ */
19
+ forceUpdate: boolean;
20
+ /**
21
+ * Whether the bundle is enabled.
22
+ */
23
+ enabled: boolean;
24
+ /**
25
+ * The file URL of the bundle.
26
+ */
27
+ fileUrl: string;
28
+ /**
29
+ * The hash of the bundle.
30
+ */
31
+ fileHash: string;
32
+ /**
33
+ * The git commit hash of the bundle.
34
+ */
35
+ gitCommitHash: string | null;
36
+ /**
37
+ * The message of the bundle.
38
+ */
39
+ message: string | null;
40
+ }
41
+
42
+ export type BundleArg =
43
+ | string
44
+ | Bundle[]
45
+ | (() => Promise<Bundle[]>)
46
+ | (() => Bundle[]);
47
+
48
+ export type UpdateStatus = "ROLLBACK" | "UPDATE";
49
+
50
+ export interface UpdateInfo {
51
+ id: string;
52
+ forceUpdate: boolean;
53
+ fileUrl: string | null;
54
+ fileHash: string | null;
55
+ status: UpdateStatus;
56
+ }
57
+
58
+ export interface GetBundlesArgs {
59
+ platform: Platform;
60
+ bundleId: string;
61
+ appVersion: string;
62
+ }
package/src/uuid.ts ADDED
@@ -0,0 +1 @@
1
+ export const NIL_UUID = "00000000-0000-0000-0000-000000000000";