diff --git a/endpoints_cresc.json b/endpoints_cresc.json new file mode 100644 index 0000000..409c42e --- /dev/null +++ b/endpoints_cresc.json @@ -0,0 +1 @@ +["https://cresc-server-pthxtmvcnf.ap-southeast-1.fcapp.run"] diff --git a/src/client.ts b/src/client.ts index 97273d2..9849ecc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,4 +1,4 @@ -import { CheckResult, PushyOptions, ProgressData, EventType } from './type'; +import { CheckResult, ClientOptions, ProgressData, EventType } from './type'; import { emptyObj, joinUrls, log, noop, promiseAny, testUrls } from './utils'; import { EmitterSubscription, Platform } from 'react-native'; import { PermissionsAndroid } from './permissions'; @@ -15,31 +15,45 @@ import { isRolledBack, } from './core'; -const defaultServer = { - main: 'https://update.react-native.cn/api', - backups: ['https://update.reactnative.cn/api'], - queryUrls: [ - 'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json', - 'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-update@master/endpoints.json', - ], +const SERVER_PRESETS = { + pushy: { + main: 'https://update.react-native.cn/api', + backups: ['https://update.reactnative.cn/api'], + queryUrls: [ + 'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json', + 'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-update@master/endpoints.json', + ], + }, + cresc: { + main: 'https://api.cresc.dev', + backups: ['https://api.cresc.app'], + queryUrls: [ + 'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-update@master/endpoints_cresc.json', + ], + }, }; - if (Platform.OS === 'web') { - console.warn('react-native-update 不支持 web 端热更,不会执行操作'); + console.warn( + 'react-native-update does not support hot updates on the web platform and will not perform any operations', + ); } -export class Pushy { - options: PushyOptions = { - appKey: '', - server: defaultServer, - autoMarkSuccess: true, - updateStrategy: __DEV__ ? 'alwaysAlert' : 'alertUpdateAndIgnoreError', - checkStrategy: 'both', - logger: noop, - debug: false, - throwError: false, - }; +const defaultClientOptions: ClientOptions = { + appKey: '', + autoMarkSuccess: true, + updateStrategy: __DEV__ ? 'alwaysAlert' : 'alertUpdateAndIgnoreError', + checkStrategy: 'both', + logger: noop, + debug: false, + throwError: false, +}; +export class Pushy { + options: ClientOptions = { + ...defaultClientOptions, + server: SERVER_PRESETS.pushy, + }; + clientType: 'pushy' | 'cresc' = 'pushy'; lastChecking?: number; lastRespJson?: Promise; @@ -50,7 +64,7 @@ export class Pushy { static marked = false; static applyingUpdate = false; - version = cInfo.pushy; + version = cInfo.rnu; loggerPromise = (() => { let resolve: (value?: unknown) => void = () => {}; const promise = new Promise(res => { @@ -62,7 +76,7 @@ export class Pushy { }; })(); - constructor(options: PushyOptions) { + constructor(options: ClientOptions) { if (Platform.OS === 'ios' || Platform.OS === 'android') { if (!options.appKey) { throw new Error('appKey is required'); @@ -79,7 +93,7 @@ export class Pushy { } } - setOptions = (options: Partial) => { + setOptions = (options: Partial) => { for (const [key, value] of Object.entries(options)) { if (value !== undefined) { (this.options as any)[key] = value; @@ -124,10 +138,10 @@ export class Pushy { return `${endpoint}/checkUpdate/${this.options.appKey}`; }; static assertHash = (hash: string) => { - if (!Pushy.downloadedHash) { + if (!this.downloadedHash) { return; } - if (hash !== Pushy.downloadedHash) { + if (hash !== this.downloadedHash) { log(`use downloaded hash ${Pushy.downloadedHash} first`); return; } @@ -144,7 +158,7 @@ export class Pushy { switchVersion = async (hash: string) => { if (__DEV__) { console.warn( - '您调用了switchVersion方法,但是当前是开发环境,不会进行任何操作。', + 'switchVersion() is not supported in development environment; no action taken.', ); return; } @@ -158,7 +172,7 @@ export class Pushy { switchVersionLater = async (hash: string) => { if (__DEV__) { console.warn( - '您调用了switchVersionLater方法,但是当前是开发环境,不会进行任何操作。', + 'switchVersionLater() is not supported in development environment; no action taken.', ); return; } @@ -170,19 +184,19 @@ export class Pushy { checkUpdate = async (extra?: Record) => { if (__DEV__ && !this.options.debug) { console.info( - '您当前处于开发环境且未启用 debug,不会进行热更检查。如需在开发环境中调试热更,请在 client 中设置 debug 为 true', + 'You are currently in the development environment and have not enabled debug mode. The hot update check will not be performed. If you need to debug hot updates in the development environment, please set debug to true in the client.', ); return; } if (Platform.OS === 'web') { - console.warn('web 端不支持热更新检查'); + console.warn('web platform does not support hot update check'); return; } if ( this.options.beforeCheckUpdate && (await this.options.beforeCheckUpdate()) === false ) { - log('beforeCheckUpdate 返回 false, 忽略检查'); + log('beforeCheckUpdate returned false, skipping check'); return; } const now = Date.now(); @@ -310,7 +324,7 @@ export class Pushy { this.options.beforeDownloadUpdate && (await this.options.beforeDownloadUpdate(info)) === false ) { - log('beforeDownloadUpdate 返回 false, 忽略下载'); + log('beforeDownloadUpdate returned false, skipping download'); return; } if (!info.update || !hash) { @@ -489,3 +503,11 @@ export class Pushy { } }; } + +export class Cresc extends Pushy { + clientType: 'cresc' | 'pushy' = 'cresc'; + options: ClientOptions = { + ...defaultClientOptions, + server: SERVER_PRESETS.cresc, + }; +} diff --git a/src/context.ts b/src/context.ts index 93d1ad7..48a2354 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,6 +1,6 @@ import { createContext, useContext } from 'react'; import { CheckResult, ProgressData } from './type'; -import { Pushy } from './client'; +import { Pushy, Cresc } from './client'; const noop = () => {}; const asyncNoop = () => Promise.resolve(); @@ -19,7 +19,7 @@ export const defaultContext = { packageVersion: '', }; -export const PushyContext = createContext<{ +export const UpdateContext = createContext<{ checkUpdate: () => Promise; switchVersion: () => Promise; switchVersionLater: () => Promise; @@ -35,10 +35,12 @@ export const PushyContext = createContext<{ parseTestQrCode: (code: string) => boolean; currentHash: string; packageVersion: string; - client?: Pushy; + client?: Pushy | Cresc; progress?: ProgressData; updateInfo?: CheckResult; lastError?: Error; }>(defaultContext); -export const usePushy = () => useContext(PushyContext); +export const usePushy = () => useContext(UpdateContext); + +export const useCresc = () => useContext(UpdateContext); diff --git a/src/core.ts b/src/core.ts index 4f34825..a86ee7f 100644 --- a/src/core.ts +++ b/src/core.ts @@ -13,8 +13,12 @@ export const PushyModule = ? require('./NativePushy').default : NativeModules.Pushy; +export const UpdateModule = PushyModule; + if (!PushyModule) { - throw new Error('react-native-update 模块无法加载,请尝试重新编译'); + throw new Error( + 'Failed to load react-native-update native module, please try to recompile', + ); } const PushyConstants = isTurboModuleEnabled @@ -31,12 +35,6 @@ export const isRolledBack: boolean = typeof rolledBackVersion === 'string'; export const buildTime: string = PushyConstants.buildTime; let uuid = PushyConstants.uuid; -if (Platform.OS === 'android' && !PushyConstants.isUsingBundleUrl) { - console.warn( - 'react-native-update 没有检测到 Bundle URL 的配置,这可能是因为您使用了某种懒加载机制(比如使用 expo,可忽略本警告),或是 Bundle URL 的配置不正确(请对照文档检查)', - ); -} - export function setLocalHashInfo(hash: string, info: Record) { PushyModule.setLocalHashInfo(hash, JSON.stringify(info)); } @@ -63,7 +61,7 @@ if (!uuid) { log('uuid: ' + uuid); export const cInfo = { - pushy: require('../package.json').version, + rnu: require('../package.json').version, rn: RNVersion, os: Platform.OS + ' ' + Platform.Version, uuid, diff --git a/src/index.ts b/src/index.ts index 9275f8f..327d7e2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export { Pushy } from './client'; -export { PushyContext, usePushy } from './context'; -export { PushyProvider } from './provider'; -export { PushyModule } from './core'; +export { Pushy, Cresc } from './client'; +export { UpdateContext, usePushy, useCresc } from './context'; +export { PushyProvider, UpdateProvider } from './provider'; +export { PushyModule, UpdateModule } from './core'; diff --git a/src/provider.tsx b/src/provider.tsx index 480812a..450490b 100644 --- a/src/provider.tsx +++ b/src/provider.tsx @@ -12,19 +12,19 @@ import { Platform, Linking, } from 'react-native'; -import { Pushy } from './client'; +import { Pushy, Cresc } from './client'; import { currentVersion, packageVersion, getCurrentVersionInfo } from './core'; -import { CheckResult, ProgressData, PushyTestPayload } from './type'; -import { PushyContext } from './context'; +import { CheckResult, ProgressData, UpdateTestPayload } from './type'; +import { ClientContext } from './context'; import { URL } from 'react-native-url-polyfill'; import { isInRollout } from './isInRollout'; import { log } from './utils'; -export const PushyProvider = ({ +export const UpdateProvider = ({ client, children, }: { - client: Pushy; + client: Pushy | Cresc; children: ReactNode; }) => { const { options } = client; @@ -275,8 +275,8 @@ export const PushyProvider = ({ }, [checkUpdate, options, dismissError, markSuccess]); const parseTestPayload = useCallback( - (payload: PushyTestPayload) => { - if (payload && payload.type && payload.type.startsWith('__rnPushy')) { + (payload: UpdateTestPayload) => { + if (payload && payload.type && payload.type.startsWith('__rnUpdate')) { const logger = options.logger || (() => {}); options.logger = ({ type, data }) => { logger({ type, data }); @@ -286,8 +286,8 @@ export const PushyProvider = ({ checkUpdate({ extra: { toHash: payload.data } }).then(() => { if (updateInfoRef.current && updateInfoRef.current.upToDate) { Alert.alert( - '提示', - '当前尚未检测到更新版本,如果是首次扫码,请等待服务器端生成补丁包后再试(约10秒)', + 'Info', + 'No update found, please wait 10s for the server to generate the patch package', ); } options.logger = logger; @@ -301,7 +301,7 @@ export const PushyProvider = ({ ); const parseTestQrCode = useCallback( - (code: string | PushyTestPayload) => { + (code: string | UpdateTestPayload) => { try { const payload = typeof code === 'string' ? JSON.parse(code) : code; return parseTestPayload(payload); @@ -335,7 +335,7 @@ export const PushyProvider = ({ }, [parseTestPayload]); return ( - {children} - + ); }; + +export const PushyProvider = UpdateProvider; diff --git a/src/type.ts b/src/type.ts index 3cd3bf6..2ed2768 100644 --- a/src/type.ts +++ b/src/type.ts @@ -44,7 +44,7 @@ export type EventType = export interface EventData { currentVersion: string; cInfo: { - pushy: string; + rnu: string; rn: string; os: string; uuid: string; @@ -65,15 +65,15 @@ export type UpdateEventsLogger = ({ data: EventData; }) => void; -export interface PushyServerConfig { +export interface UpdateServerConfig { main: string; backups?: string[]; queryUrls?: string[]; } -export interface PushyOptions { +export interface ClientOptions { appKey: string; - server?: PushyServerConfig; + server?: UpdateServerConfig; logger?: UpdateEventsLogger; updateStrategy?: | 'alwaysAlert' @@ -90,7 +90,7 @@ export interface PushyOptions { beforeDownloadUpdate?: (info: CheckResult) => Promise; } -export interface PushyTestPayload { +export interface UpdateTestPayload { type: '__rnPushyVersionHash' | string | null; data: any; } diff --git a/src/utils.ts b/src/utils.ts index 84f49c1..69c94b3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import { Platform } from 'react-native'; export function log(...args: any[]) { - console.log('pushy: ', ...args); + console.log('react-native-update: ', ...args); } export function promiseAny(promises: Promise[]) {