@hot-updater/react-native 0.1.6-0 → 0.3.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.
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @license React
3
+ * react.development.js
4
+ *
5
+ * Copyright (c) Facebook, Inc. and its affiliates.
6
+ *
7
+ * This source code is licensed under the MIT license found in the
8
+ * LICENSE file in the root directory of this source tree.
9
+ */
10
+
11
+ /**
12
+ * @license React
13
+ * react.production.min.js
14
+ *
15
+ * Copyright (c) Facebook, Inc. and its affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ */
@@ -0,0 +1,29 @@
1
+ export type HotUpdaterEvent = {
2
+ onProgress: {
3
+ progress: number;
4
+ };
5
+ };
6
+ export declare const addListener: <T extends keyof HotUpdaterEvent>(eventName: T, listener: (event: HotUpdaterEvent[T]) => void) => void;
7
+ /**
8
+ * Downloads files from given URLs.
9
+ *
10
+ * @param {string} bundleId - identifier for the bundle version.
11
+ * @param {string | null} zipUrl - zip file URL.
12
+ * @returns {Promise<boolean>} Resolves with true if download was successful, otherwise rejects with an error.
13
+ */
14
+ export declare const updateBundle: (bundleId: string, zipUrl: string | null) => Promise<boolean>;
15
+ /**
16
+ * Fetches the current app version.
17
+ */
18
+ export declare const getAppVersion: () => Promise<string | null>;
19
+ /**
20
+ * Reloads the app.
21
+ */
22
+ export declare const reload: () => void;
23
+ /**
24
+ * Fetches the current bundle version id.
25
+ *
26
+ * @async
27
+ * @returns {Promise<string>} Resolves with the current version id or null if not available.
28
+ */
29
+ export declare const getBundleId: () => string;
@@ -0,0 +1,9 @@
1
+ export type HotUpdaterState = {
2
+ progress: number;
3
+ };
4
+ export declare const hotUpdaterStore: {
5
+ getState: () => HotUpdaterState;
6
+ setState: (newState: Partial<HotUpdaterState>) => void;
7
+ subscribe: (listener: () => void) => () => boolean;
8
+ };
9
+ export declare const useHotUpdaterStore: () => HotUpdaterState;
package/dist/wrap.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type { BundleArg, UpdateInfo } from "@hot-updater/core";
2
+ import type React from "react";
3
+ import { HotUpdaterError } from "./error";
4
+ export type HotUpdaterStatus = "INSTALLING_UPDATE" | "UP_TO_DATE" | "UPDATING";
5
+ export interface CheckUpdateConfig {
6
+ source: BundleArg;
7
+ requestHeaders?: Record<string, string>;
8
+ }
9
+ export interface HotUpdaterConfig extends CheckUpdateConfig {
10
+ fallbackComponent?: React.FC<HotUpdaterFallbackProps>;
11
+ }
12
+ export interface HotUpdaterFallbackProps {
13
+ progress: number;
14
+ }
15
+ export interface WithHotUpdaterProps {
16
+ updateStatus: HotUpdaterStatus | null;
17
+ updateError: HotUpdaterError | null;
18
+ }
19
+ export declare function checkUpdate(config: CheckUpdateConfig): Promise<UpdateInfo | null>;
20
+ export declare function wrap<P>(config: HotUpdaterConfig): (WrappedComponent: React.ComponentType<P & WithHotUpdaterProps>) => React.ComponentType<P>;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@hot-updater/react-native",
3
- "version": "0.1.6-0",
3
+ "version": "0.3.0",
4
4
  "description": "React Native OTA solution for self-hosted",
5
- "main": "dist/index.cjs",
6
- "module": "dist/index.js",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
7
  "source": "src/index.ts",
8
8
  "react-native": "src/index.ts",
9
9
  "types": "dist/index.d.ts",
@@ -73,13 +73,13 @@
73
73
  "react": "18.3.1",
74
74
  "react-native": "0.76.2",
75
75
  "react-native-builder-bob": "^0.33.1",
76
- "@hot-updater/js": "0.1.6-0"
76
+ "@hot-updater/js": "0.3.0"
77
77
  },
78
78
  "dependencies": {
79
- "@hot-updater/core": "0.1.6-0"
79
+ "@hot-updater/core": "0.3.0"
80
80
  },
81
81
  "scripts": {
82
- "build": "tsup src/index.ts --format esm,cjs --dts",
82
+ "build": "rslib build",
83
83
  "test:type": "tsc --noEmit",
84
84
  "test": "vitest",
85
85
  "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib"
@@ -0,0 +1,36 @@
1
+ import type {
2
+ Bundle,
3
+ BundleArg,
4
+ GetBundlesArgs,
5
+ UpdateInfo,
6
+ } from "@hot-updater/core";
7
+
8
+ export const ensureUpdateInfo = async (
9
+ source: BundleArg,
10
+ { appVersion, bundleId, platform }: GetBundlesArgs,
11
+ requestHeaders?: Record<string, string>,
12
+ ): Promise<Bundle[] | UpdateInfo> => {
13
+ try {
14
+ let bundles: Bundle[] | null = null;
15
+ if (typeof source === "string") {
16
+ if (source.startsWith("http")) {
17
+ return await fetch(source, {
18
+ headers: {
19
+ "x-app-platform": platform,
20
+ "x-app-version": appVersion,
21
+ "x-bundle-id": bundleId,
22
+ ...requestHeaders,
23
+ },
24
+ }).then((res) => res.json());
25
+ }
26
+ } else if (typeof source === "function") {
27
+ bundles = await source();
28
+ } else {
29
+ bundles = source;
30
+ }
31
+
32
+ return bundles ?? [];
33
+ } catch {
34
+ return [];
35
+ }
36
+ };
package/src/index.ts CHANGED
@@ -1,7 +1,5 @@
1
- import { NIL_UUID } from "@hot-updater/core";
2
1
  import { getUpdateInfo } from "@hot-updater/js";
3
- import { ensureBundles } from "./ensureBundles";
4
- import { init } from "./init";
2
+ import { ensureUpdateInfo } from "./ensureUpdateInfo";
5
3
  import {
6
4
  addListener,
7
5
  getAppVersion,
@@ -10,8 +8,9 @@ import {
10
8
  updateBundle,
11
9
  } from "./native";
12
10
  import { hotUpdaterStore } from "./store";
11
+ import { wrap } from "./wrap";
13
12
 
14
- export type * from "./init";
13
+ export type * from "./wrap";
15
14
  export type * from "./native";
16
15
 
17
16
  export * from "./store";
@@ -21,17 +20,14 @@ addListener("onProgress", ({ progress }) => {
21
20
  });
22
21
 
23
22
  export const HotUpdater = {
24
- init,
23
+ wrap,
24
+
25
25
  reload,
26
26
  getAppVersion,
27
27
  getBundleId,
28
28
  addListener,
29
29
 
30
- ensureBundles,
30
+ ensureUpdateInfo,
31
31
  updateBundle,
32
32
  getUpdateInfo,
33
- /**
34
- * In production environment, this value will be replaced with a uuidv7.
35
- */
36
- HOT_UPDATER_BUNDLE_ID: NIL_UUID,
37
33
  };
package/src/native.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import { NIL_UUID } from "@hot-updater/core";
2
2
  import { NativeEventEmitter, NativeModules, Platform } from "react-native";
3
3
 
4
+ const HotUpdater = {
5
+ HOT_UPDATER_BUNDLE_ID: NIL_UUID,
6
+ };
7
+
4
8
  const LINKING_ERROR =
5
9
  // biome-ignore lint/style/useTemplate: <explanation>
6
10
  `The package '@hot-updater/react-native' doesn't seem to be linked. Make sure: \n\n` +
@@ -41,16 +45,6 @@ export const addListener = <T extends keyof HotUpdaterEvent>(
41
45
  eventEmitter?.addListener(eventName, listener);
42
46
  };
43
47
 
44
- /**
45
- * Fetches the current bundle version id.
46
- *
47
- * @async
48
- * @returns {Promise<string>} Resolves with the current version id or null if not available.
49
- */
50
- export const getBundleId = (): string => {
51
- return HotUpdater.HOT_UPDATER_BUNDLE_ID ?? NIL_UUID;
52
- };
53
-
54
48
  /**
55
49
  * Downloads files from given URLs.
56
50
  *
@@ -78,3 +72,13 @@ export const getAppVersion = (): Promise<string | null> => {
78
72
  export const reload = () => {
79
73
  HotUpdaterNative.reload();
80
74
  };
75
+
76
+ /**
77
+ * Fetches the current bundle version id.
78
+ *
79
+ * @async
80
+ * @returns {Promise<string>} Resolves with the current version id or null if not available.
81
+ */
82
+ export const getBundleId = (): string => {
83
+ return HotUpdater.HOT_UPDATER_BUNDLE_ID;
84
+ };
package/src/wrap.tsx ADDED
@@ -0,0 +1,148 @@
1
+ import type { Bundle, BundleArg, UpdateInfo } from "@hot-updater/core";
2
+ import { getUpdateInfo } from "@hot-updater/js";
3
+ import type React from "react";
4
+ import { useEffect, useState } from "react";
5
+ import { Platform } from "react-native";
6
+ import { ensureUpdateInfo } from "./ensureUpdateInfo";
7
+ import { HotUpdaterError } from "./error";
8
+ import { getAppVersion, getBundleId, reload, updateBundle } from "./native";
9
+ import { useHotUpdaterStore } from "./store";
10
+
11
+ export type HotUpdaterStatus = "INSTALLING_UPDATE" | "UP_TO_DATE" | "UPDATING";
12
+
13
+ export interface CheckUpdateConfig {
14
+ source: BundleArg;
15
+ requestHeaders?: Record<string, string>;
16
+ }
17
+
18
+ export interface HotUpdaterConfig extends CheckUpdateConfig {
19
+ fallbackComponent?: React.FC<HotUpdaterFallbackProps>;
20
+ }
21
+
22
+ export interface HotUpdaterFallbackProps {
23
+ progress: number;
24
+ }
25
+
26
+ export interface WithHotUpdaterProps {
27
+ updateStatus: HotUpdaterStatus | null;
28
+ updateError: HotUpdaterError | null;
29
+ }
30
+
31
+ export async function checkUpdate(config: CheckUpdateConfig) {
32
+ if (__DEV__) {
33
+ console.warn(
34
+ "[HotUpdater] __DEV__ is true, HotUpdater is only supported in production",
35
+ );
36
+ return null;
37
+ }
38
+
39
+ if (!["ios", "android"].includes(Platform.OS)) {
40
+ throw new HotUpdaterError(
41
+ "HotUpdater is only supported on iOS and Android",
42
+ );
43
+ }
44
+
45
+ const currentAppVersion = await getAppVersion();
46
+ const platform = Platform.OS as "ios" | "android";
47
+ const currentBundleId = await getBundleId();
48
+
49
+ if (!currentAppVersion) {
50
+ throw new HotUpdaterError("Failed to get app version");
51
+ }
52
+
53
+ const ensuredUpdateInfo = await ensureUpdateInfo(
54
+ config.source,
55
+ {
56
+ appVersion: currentAppVersion,
57
+ bundleId: currentBundleId,
58
+ platform,
59
+ },
60
+ config.requestHeaders,
61
+ );
62
+
63
+ let updateInfo: UpdateInfo | null = null;
64
+ if (Array.isArray(ensuredUpdateInfo)) {
65
+ const bundles: Bundle[] = ensuredUpdateInfo;
66
+
67
+ updateInfo = await getUpdateInfo(bundles, {
68
+ appVersion: currentAppVersion,
69
+ bundleId: currentBundleId,
70
+ platform,
71
+ });
72
+ } else {
73
+ updateInfo = ensuredUpdateInfo;
74
+ }
75
+
76
+ return updateInfo;
77
+ }
78
+
79
+ async function installUpdate(updateInfo: UpdateInfo) {
80
+ const isSuccess = await updateBundle(updateInfo.id, updateInfo.fileUrl || "");
81
+
82
+ if (isSuccess && updateInfo.forceUpdate) {
83
+ reload();
84
+ return true;
85
+ }
86
+
87
+ return isSuccess;
88
+ }
89
+
90
+ export function wrap<P>(
91
+ config: HotUpdaterConfig,
92
+ ): (
93
+ WrappedComponent: React.ComponentType<P & WithHotUpdaterProps>,
94
+ ) => React.ComponentType<P> {
95
+ return (WrappedComponent) => {
96
+ const HotUpdaterHOC: React.FC<P> = (props) => {
97
+ const [updateStatus, setUpdateStatus] = useState<HotUpdaterStatus | null>(
98
+ null,
99
+ );
100
+ const [updateError, setUpdateError] = useState<HotUpdaterError | null>(
101
+ null,
102
+ );
103
+
104
+ const { progress } = useHotUpdaterStore();
105
+
106
+ useEffect(() => {
107
+ const initHotUpdater = async () => {
108
+ try {
109
+ const updateInfo = await checkUpdate(config);
110
+
111
+ if (!updateInfo) {
112
+ setUpdateStatus("UP_TO_DATE");
113
+ return;
114
+ }
115
+
116
+ setUpdateStatus("UPDATING");
117
+ const isSuccess = await installUpdate(updateInfo);
118
+ if (isSuccess) {
119
+ setUpdateStatus("INSTALLING_UPDATE");
120
+ }
121
+ } catch (error) {
122
+ if (error instanceof HotUpdaterError) {
123
+ setUpdateError(error);
124
+ }
125
+ throw error;
126
+ }
127
+ };
128
+
129
+ initHotUpdater();
130
+ }, [config.source, config.requestHeaders]);
131
+
132
+ if (updateStatus === "UPDATING" && config.fallbackComponent) {
133
+ const Fallback = config.fallbackComponent;
134
+ return <Fallback progress={progress} />;
135
+ }
136
+
137
+ return (
138
+ <WrappedComponent
139
+ {...props}
140
+ updateStatus={updateStatus}
141
+ updateError={updateError}
142
+ />
143
+ );
144
+ };
145
+
146
+ return HotUpdaterHOC;
147
+ };
148
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
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.d.mts DELETED
@@ -1,71 +0,0 @@
1
- import * as _hot_updater_core from '@hot-updater/core';
2
- import { BundleArg } from '@hot-updater/core';
3
-
4
- type HotUpdaterEvent = {
5
- onProgress: {
6
- progress: number;
7
- };
8
- };
9
- declare const addListener: <T extends keyof HotUpdaterEvent>(eventName: T, listener: (event: HotUpdaterEvent[T]) => void) => void;
10
- /**
11
- * Fetches the current bundle version id.
12
- *
13
- * @async
14
- * @returns {Promise<string>} Resolves with the current version id or null if not available.
15
- */
16
- declare const getBundleId: () => string;
17
- /**
18
- * Downloads files from given URLs.
19
- *
20
- * @param {string} bundleId - identifier for the bundle version.
21
- * @param {string | null} zipUrl - zip file URL.
22
- * @returns {Promise<boolean>} Resolves with true if download was successful, otherwise rejects with an error.
23
- */
24
- declare const updateBundle: (bundleId: string, zipUrl: string | null) => Promise<boolean>;
25
- /**
26
- * Fetches the current app version.
27
- */
28
- declare const getAppVersion: () => Promise<string | null>;
29
- /**
30
- * Reloads the app.
31
- */
32
- declare const reload: () => void;
33
-
34
- declare class HotUpdaterError extends Error {
35
- constructor(message: string);
36
- }
37
-
38
- type HotUpdaterStatus = "INSTALLING_UPDATE" | "UP_TO_DATE";
39
- interface HotUpdaterInitConfig {
40
- source: BundleArg;
41
- onSuccess?: (status: HotUpdaterStatus) => void;
42
- onError?: (error: HotUpdaterError) => void;
43
- }
44
- declare const init: (config: HotUpdaterInitConfig) => Promise<void>;
45
-
46
- type HotUpdaterState = {
47
- progress: number;
48
- };
49
- declare const hotUpdaterStore: {
50
- getState: () => HotUpdaterState;
51
- setState: (newState: Partial<HotUpdaterState>) => void;
52
- subscribe: (listener: () => void) => () => boolean;
53
- };
54
- declare const useHotUpdaterStore: () => HotUpdaterState;
55
-
56
- declare const HotUpdater: {
57
- init: (config: HotUpdaterInitConfig) => Promise<void>;
58
- reload: () => void;
59
- getAppVersion: () => Promise<string | null>;
60
- getBundleId: () => string;
61
- addListener: <T extends keyof HotUpdaterEvent>(eventName: T, listener: (event: HotUpdaterEvent[T]) => void) => void;
62
- ensureBundles: (bundle: _hot_updater_core.BundleArg) => Promise<_hot_updater_core.Bundle[]>;
63
- updateBundle: (bundleId: string, zipUrl: string | null) => Promise<boolean>;
64
- getUpdateInfo: (bundles: _hot_updater_core.Bundle[], { platform, bundleId, appVersion }: _hot_updater_core.GetBundlesArgs) => Promise<_hot_updater_core.UpdateInfo | null>;
65
- /**
66
- * In production environment, this value will be replaced with a uuidv7.
67
- */
68
- HOT_UPDATER_BUNDLE_ID: string;
69
- };
70
-
71
- export { HotUpdater, type HotUpdaterEvent, type HotUpdaterInitConfig, type HotUpdaterState, type HotUpdaterStatus, addListener, getAppVersion, getBundleId, hotUpdaterStore, init, reload, updateBundle, useHotUpdaterStore };
@@ -1,21 +0,0 @@
1
- import type { Bundle, BundleArg } from "@hot-updater/core";
2
-
3
- export const ensureBundles = async (bundle: BundleArg) => {
4
- try {
5
- let bundles: Bundle[] | null = null;
6
- if (typeof bundle === "string") {
7
- if (bundle.startsWith("http")) {
8
- const response = await fetch(bundle);
9
- bundles = (await response.json()) as Bundle[];
10
- }
11
- } else if (typeof bundle === "function") {
12
- bundles = await bundle();
13
- } else {
14
- bundles = bundle;
15
- }
16
-
17
- return bundles ?? [];
18
- } catch {
19
- return [];
20
- }
21
- };
package/src/init.tsx DELETED
@@ -1,69 +0,0 @@
1
- import type { BundleArg } from "@hot-updater/core";
2
- import { getUpdateInfo } from "@hot-updater/js";
3
- import { Platform } from "react-native";
4
- import { ensureBundles } from "./ensureBundles";
5
- import { HotUpdaterError } from "./error";
6
- import { getAppVersion, getBundleId, reload, updateBundle } from "./native";
7
-
8
- export type HotUpdaterStatus = "INSTALLING_UPDATE" | "UP_TO_DATE";
9
-
10
- export interface HotUpdaterInitConfig {
11
- source: BundleArg;
12
- onSuccess?: (status: HotUpdaterStatus) => void;
13
- onError?: (error: HotUpdaterError) => void;
14
- }
15
-
16
- export const init = async (config: HotUpdaterInitConfig) => {
17
- if (__DEV__) {
18
- console.warn(
19
- "[HotUpdater] __DEV__ is true, HotUpdater is only supported in production",
20
- );
21
- return;
22
- }
23
-
24
- if (!["ios", "android"].includes(Platform.OS)) {
25
- const error = new HotUpdaterError(
26
- "HotUpdater is only supported on iOS and Android",
27
- );
28
-
29
- config?.onError?.(error);
30
- throw error;
31
- }
32
-
33
- const currentAppVersion = await getAppVersion();
34
- const platform = Platform.OS as "ios" | "android";
35
- const currentBundleId = await getBundleId();
36
-
37
- if (!currentAppVersion) {
38
- const error = new HotUpdaterError("Failed to get app version");
39
- config?.onError?.(error);
40
- throw error;
41
- }
42
-
43
- const bundles = await ensureBundles(config.source);
44
-
45
- const update = await getUpdateInfo(bundles, {
46
- appVersion: currentAppVersion,
47
- bundleId: currentBundleId,
48
- platform,
49
- });
50
-
51
- if (!update) {
52
- config?.onSuccess?.("UP_TO_DATE");
53
- return;
54
- }
55
-
56
- try {
57
- const isSuccess = await updateBundle(update.id, update.fileUrl || "");
58
- if (isSuccess && update.forceUpdate) {
59
- reload();
60
-
61
- config?.onSuccess?.("INSTALLING_UPDATE");
62
- }
63
- } catch (error) {
64
- if (error instanceof HotUpdaterError) {
65
- config?.onError?.(error);
66
- }
67
- throw error;
68
- }
69
- };