1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-16 07:01:38 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

add cresc

This commit is contained in:
sunnylqm
2025-02-18 13:16:37 +08:00
parent 0b52cf35d2
commit bbda7217ac
8 changed files with 91 additions and 66 deletions

1
endpoints_cresc.json Normal file
View File

@@ -0,0 +1 @@
["https://cresc-server-pthxtmvcnf.ap-southeast-1.fcapp.run"]

View File

@@ -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<any>;
@@ -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<PushyOptions>) => {
setOptions = (options: Partial<ClientOptions>) => {
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<string, any>) => {
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,
};
}

View File

@@ -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<void>;
switchVersion: () => Promise<void>;
switchVersionLater: () => Promise<void>;
@@ -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);

View File

@@ -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<string, any>) {
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,

View File

@@ -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';

View File

@@ -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 (
<PushyContext.Provider
<ClientContext.Provider
value={{
checkUpdate,
switchVersion,
@@ -354,6 +354,8 @@ export const PushyProvider = ({
parseTestQrCode,
}}>
{children}
</PushyContext.Provider>
</ClientContext.Provider>
);
};
export const PushyProvider = UpdateProvider;

View File

@@ -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<boolean>;
}
export interface PushyTestPayload {
export interface UpdateTestPayload {
type: '__rnPushyVersionHash' | string | null;
data: any;
}

View File

@@ -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<T>(promises: Promise<T>[]) {