mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-16 07:01:38 +08:00
add cresc
This commit is contained in:
1
endpoints_cresc.json
Normal file
1
endpoints_cresc.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
["https://cresc-server-pthxtmvcnf.ap-southeast-1.fcapp.run"]
|
@@ -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 { emptyObj, joinUrls, log, noop, promiseAny, testUrls } from './utils';
|
||||||
import { EmitterSubscription, Platform } from 'react-native';
|
import { EmitterSubscription, Platform } from 'react-native';
|
||||||
import { PermissionsAndroid } from './permissions';
|
import { PermissionsAndroid } from './permissions';
|
||||||
@@ -15,31 +15,45 @@ import {
|
|||||||
isRolledBack,
|
isRolledBack,
|
||||||
} from './core';
|
} from './core';
|
||||||
|
|
||||||
const defaultServer = {
|
const SERVER_PRESETS = {
|
||||||
main: 'https://update.react-native.cn/api',
|
pushy: {
|
||||||
backups: ['https://update.reactnative.cn/api'],
|
main: 'https://update.react-native.cn/api',
|
||||||
queryUrls: [
|
backups: ['https://update.reactnative.cn/api'],
|
||||||
'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json',
|
queryUrls: [
|
||||||
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-update@master/endpoints.json',
|
'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') {
|
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 {
|
const defaultClientOptions: ClientOptions = {
|
||||||
options: PushyOptions = {
|
appKey: '',
|
||||||
appKey: '',
|
autoMarkSuccess: true,
|
||||||
server: defaultServer,
|
updateStrategy: __DEV__ ? 'alwaysAlert' : 'alertUpdateAndIgnoreError',
|
||||||
autoMarkSuccess: true,
|
checkStrategy: 'both',
|
||||||
updateStrategy: __DEV__ ? 'alwaysAlert' : 'alertUpdateAndIgnoreError',
|
logger: noop,
|
||||||
checkStrategy: 'both',
|
debug: false,
|
||||||
logger: noop,
|
throwError: false,
|
||||||
debug: false,
|
};
|
||||||
throwError: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
export class Pushy {
|
||||||
|
options: ClientOptions = {
|
||||||
|
...defaultClientOptions,
|
||||||
|
server: SERVER_PRESETS.pushy,
|
||||||
|
};
|
||||||
|
clientType: 'pushy' | 'cresc' = 'pushy';
|
||||||
lastChecking?: number;
|
lastChecking?: number;
|
||||||
lastRespJson?: Promise<any>;
|
lastRespJson?: Promise<any>;
|
||||||
|
|
||||||
@@ -50,7 +64,7 @@ export class Pushy {
|
|||||||
|
|
||||||
static marked = false;
|
static marked = false;
|
||||||
static applyingUpdate = false;
|
static applyingUpdate = false;
|
||||||
version = cInfo.pushy;
|
version = cInfo.rnu;
|
||||||
loggerPromise = (() => {
|
loggerPromise = (() => {
|
||||||
let resolve: (value?: unknown) => void = () => {};
|
let resolve: (value?: unknown) => void = () => {};
|
||||||
const promise = new Promise(res => {
|
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 (Platform.OS === 'ios' || Platform.OS === 'android') {
|
||||||
if (!options.appKey) {
|
if (!options.appKey) {
|
||||||
throw new Error('appKey is required');
|
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)) {
|
for (const [key, value] of Object.entries(options)) {
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
(this.options as any)[key] = value;
|
(this.options as any)[key] = value;
|
||||||
@@ -124,10 +138,10 @@ export class Pushy {
|
|||||||
return `${endpoint}/checkUpdate/${this.options.appKey}`;
|
return `${endpoint}/checkUpdate/${this.options.appKey}`;
|
||||||
};
|
};
|
||||||
static assertHash = (hash: string) => {
|
static assertHash = (hash: string) => {
|
||||||
if (!Pushy.downloadedHash) {
|
if (!this.downloadedHash) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (hash !== Pushy.downloadedHash) {
|
if (hash !== this.downloadedHash) {
|
||||||
log(`use downloaded hash ${Pushy.downloadedHash} first`);
|
log(`use downloaded hash ${Pushy.downloadedHash} first`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -144,7 +158,7 @@ export class Pushy {
|
|||||||
switchVersion = async (hash: string) => {
|
switchVersion = async (hash: string) => {
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
console.warn(
|
console.warn(
|
||||||
'您调用了switchVersion方法,但是当前是开发环境,不会进行任何操作。',
|
'switchVersion() is not supported in development environment; no action taken.',
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -158,7 +172,7 @@ export class Pushy {
|
|||||||
switchVersionLater = async (hash: string) => {
|
switchVersionLater = async (hash: string) => {
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
console.warn(
|
console.warn(
|
||||||
'您调用了switchVersionLater方法,但是当前是开发环境,不会进行任何操作。',
|
'switchVersionLater() is not supported in development environment; no action taken.',
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -170,19 +184,19 @@ export class Pushy {
|
|||||||
checkUpdate = async (extra?: Record<string, any>) => {
|
checkUpdate = async (extra?: Record<string, any>) => {
|
||||||
if (__DEV__ && !this.options.debug) {
|
if (__DEV__ && !this.options.debug) {
|
||||||
console.info(
|
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;
|
return;
|
||||||
}
|
}
|
||||||
if (Platform.OS === 'web') {
|
if (Platform.OS === 'web') {
|
||||||
console.warn('web 端不支持热更新检查');
|
console.warn('web platform does not support hot update check');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
this.options.beforeCheckUpdate &&
|
this.options.beforeCheckUpdate &&
|
||||||
(await this.options.beforeCheckUpdate()) === false
|
(await this.options.beforeCheckUpdate()) === false
|
||||||
) {
|
) {
|
||||||
log('beforeCheckUpdate 返回 false, 忽略检查');
|
log('beforeCheckUpdate returned false, skipping check');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
@@ -310,7 +324,7 @@ export class Pushy {
|
|||||||
this.options.beforeDownloadUpdate &&
|
this.options.beforeDownloadUpdate &&
|
||||||
(await this.options.beforeDownloadUpdate(info)) === false
|
(await this.options.beforeDownloadUpdate(info)) === false
|
||||||
) {
|
) {
|
||||||
log('beforeDownloadUpdate 返回 false, 忽略下载');
|
log('beforeDownloadUpdate returned false, skipping download');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!info.update || !hash) {
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { createContext, useContext } from 'react';
|
import { createContext, useContext } from 'react';
|
||||||
import { CheckResult, ProgressData } from './type';
|
import { CheckResult, ProgressData } from './type';
|
||||||
import { Pushy } from './client';
|
import { Pushy, Cresc } from './client';
|
||||||
|
|
||||||
const noop = () => {};
|
const noop = () => {};
|
||||||
const asyncNoop = () => Promise.resolve();
|
const asyncNoop = () => Promise.resolve();
|
||||||
@@ -19,7 +19,7 @@ export const defaultContext = {
|
|||||||
packageVersion: '',
|
packageVersion: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const PushyContext = createContext<{
|
export const UpdateContext = createContext<{
|
||||||
checkUpdate: () => Promise<void>;
|
checkUpdate: () => Promise<void>;
|
||||||
switchVersion: () => Promise<void>;
|
switchVersion: () => Promise<void>;
|
||||||
switchVersionLater: () => Promise<void>;
|
switchVersionLater: () => Promise<void>;
|
||||||
@@ -35,10 +35,12 @@ export const PushyContext = createContext<{
|
|||||||
parseTestQrCode: (code: string) => boolean;
|
parseTestQrCode: (code: string) => boolean;
|
||||||
currentHash: string;
|
currentHash: string;
|
||||||
packageVersion: string;
|
packageVersion: string;
|
||||||
client?: Pushy;
|
client?: Pushy | Cresc;
|
||||||
progress?: ProgressData;
|
progress?: ProgressData;
|
||||||
updateInfo?: CheckResult;
|
updateInfo?: CheckResult;
|
||||||
lastError?: Error;
|
lastError?: Error;
|
||||||
}>(defaultContext);
|
}>(defaultContext);
|
||||||
|
|
||||||
export const usePushy = () => useContext(PushyContext);
|
export const usePushy = () => useContext(UpdateContext);
|
||||||
|
|
||||||
|
export const useCresc = () => useContext(UpdateContext);
|
||||||
|
14
src/core.ts
14
src/core.ts
@@ -13,8 +13,12 @@ export const PushyModule =
|
|||||||
? require('./NativePushy').default
|
? require('./NativePushy').default
|
||||||
: NativeModules.Pushy;
|
: NativeModules.Pushy;
|
||||||
|
|
||||||
|
export const UpdateModule = PushyModule;
|
||||||
|
|
||||||
if (!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
|
const PushyConstants = isTurboModuleEnabled
|
||||||
@@ -31,12 +35,6 @@ export const isRolledBack: boolean = typeof rolledBackVersion === 'string';
|
|||||||
export const buildTime: string = PushyConstants.buildTime;
|
export const buildTime: string = PushyConstants.buildTime;
|
||||||
let uuid = PushyConstants.uuid;
|
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>) {
|
export function setLocalHashInfo(hash: string, info: Record<string, any>) {
|
||||||
PushyModule.setLocalHashInfo(hash, JSON.stringify(info));
|
PushyModule.setLocalHashInfo(hash, JSON.stringify(info));
|
||||||
}
|
}
|
||||||
@@ -63,7 +61,7 @@ if (!uuid) {
|
|||||||
log('uuid: ' + uuid);
|
log('uuid: ' + uuid);
|
||||||
|
|
||||||
export const cInfo = {
|
export const cInfo = {
|
||||||
pushy: require('../package.json').version,
|
rnu: require('../package.json').version,
|
||||||
rn: RNVersion,
|
rn: RNVersion,
|
||||||
os: Platform.OS + ' ' + Platform.Version,
|
os: Platform.OS + ' ' + Platform.Version,
|
||||||
uuid,
|
uuid,
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
export { Pushy } from './client';
|
export { Pushy, Cresc } from './client';
|
||||||
export { PushyContext, usePushy } from './context';
|
export { UpdateContext, usePushy, useCresc } from './context';
|
||||||
export { PushyProvider } from './provider';
|
export { PushyProvider, UpdateProvider } from './provider';
|
||||||
export { PushyModule } from './core';
|
export { PushyModule, UpdateModule } from './core';
|
||||||
|
@@ -12,19 +12,19 @@ import {
|
|||||||
Platform,
|
Platform,
|
||||||
Linking,
|
Linking,
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
import { Pushy } from './client';
|
import { Pushy, Cresc } from './client';
|
||||||
import { currentVersion, packageVersion, getCurrentVersionInfo } from './core';
|
import { currentVersion, packageVersion, getCurrentVersionInfo } from './core';
|
||||||
import { CheckResult, ProgressData, PushyTestPayload } from './type';
|
import { CheckResult, ProgressData, UpdateTestPayload } from './type';
|
||||||
import { PushyContext } from './context';
|
import { ClientContext } from './context';
|
||||||
import { URL } from 'react-native-url-polyfill';
|
import { URL } from 'react-native-url-polyfill';
|
||||||
import { isInRollout } from './isInRollout';
|
import { isInRollout } from './isInRollout';
|
||||||
import { log } from './utils';
|
import { log } from './utils';
|
||||||
|
|
||||||
export const PushyProvider = ({
|
export const UpdateProvider = ({
|
||||||
client,
|
client,
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
client: Pushy;
|
client: Pushy | Cresc;
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
}) => {
|
}) => {
|
||||||
const { options } = client;
|
const { options } = client;
|
||||||
@@ -275,8 +275,8 @@ export const PushyProvider = ({
|
|||||||
}, [checkUpdate, options, dismissError, markSuccess]);
|
}, [checkUpdate, options, dismissError, markSuccess]);
|
||||||
|
|
||||||
const parseTestPayload = useCallback(
|
const parseTestPayload = useCallback(
|
||||||
(payload: PushyTestPayload) => {
|
(payload: UpdateTestPayload) => {
|
||||||
if (payload && payload.type && payload.type.startsWith('__rnPushy')) {
|
if (payload && payload.type && payload.type.startsWith('__rnUpdate')) {
|
||||||
const logger = options.logger || (() => {});
|
const logger = options.logger || (() => {});
|
||||||
options.logger = ({ type, data }) => {
|
options.logger = ({ type, data }) => {
|
||||||
logger({ type, data });
|
logger({ type, data });
|
||||||
@@ -286,8 +286,8 @@ export const PushyProvider = ({
|
|||||||
checkUpdate({ extra: { toHash: payload.data } }).then(() => {
|
checkUpdate({ extra: { toHash: payload.data } }).then(() => {
|
||||||
if (updateInfoRef.current && updateInfoRef.current.upToDate) {
|
if (updateInfoRef.current && updateInfoRef.current.upToDate) {
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
'提示',
|
'Info',
|
||||||
'当前尚未检测到更新版本,如果是首次扫码,请等待服务器端生成补丁包后再试(约10秒)',
|
'No update found, please wait 10s for the server to generate the patch package',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
options.logger = logger;
|
options.logger = logger;
|
||||||
@@ -301,7 +301,7 @@ export const PushyProvider = ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
const parseTestQrCode = useCallback(
|
const parseTestQrCode = useCallback(
|
||||||
(code: string | PushyTestPayload) => {
|
(code: string | UpdateTestPayload) => {
|
||||||
try {
|
try {
|
||||||
const payload = typeof code === 'string' ? JSON.parse(code) : code;
|
const payload = typeof code === 'string' ? JSON.parse(code) : code;
|
||||||
return parseTestPayload(payload);
|
return parseTestPayload(payload);
|
||||||
@@ -335,7 +335,7 @@ export const PushyProvider = ({
|
|||||||
}, [parseTestPayload]);
|
}, [parseTestPayload]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PushyContext.Provider
|
<ClientContext.Provider
|
||||||
value={{
|
value={{
|
||||||
checkUpdate,
|
checkUpdate,
|
||||||
switchVersion,
|
switchVersion,
|
||||||
@@ -354,6 +354,8 @@ export const PushyProvider = ({
|
|||||||
parseTestQrCode,
|
parseTestQrCode,
|
||||||
}}>
|
}}>
|
||||||
{children}
|
{children}
|
||||||
</PushyContext.Provider>
|
</ClientContext.Provider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const PushyProvider = UpdateProvider;
|
||||||
|
10
src/type.ts
10
src/type.ts
@@ -44,7 +44,7 @@ export type EventType =
|
|||||||
export interface EventData {
|
export interface EventData {
|
||||||
currentVersion: string;
|
currentVersion: string;
|
||||||
cInfo: {
|
cInfo: {
|
||||||
pushy: string;
|
rnu: string;
|
||||||
rn: string;
|
rn: string;
|
||||||
os: string;
|
os: string;
|
||||||
uuid: string;
|
uuid: string;
|
||||||
@@ -65,15 +65,15 @@ export type UpdateEventsLogger = ({
|
|||||||
data: EventData;
|
data: EventData;
|
||||||
}) => void;
|
}) => void;
|
||||||
|
|
||||||
export interface PushyServerConfig {
|
export interface UpdateServerConfig {
|
||||||
main: string;
|
main: string;
|
||||||
backups?: string[];
|
backups?: string[];
|
||||||
queryUrls?: string[];
|
queryUrls?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PushyOptions {
|
export interface ClientOptions {
|
||||||
appKey: string;
|
appKey: string;
|
||||||
server?: PushyServerConfig;
|
server?: UpdateServerConfig;
|
||||||
logger?: UpdateEventsLogger;
|
logger?: UpdateEventsLogger;
|
||||||
updateStrategy?:
|
updateStrategy?:
|
||||||
| 'alwaysAlert'
|
| 'alwaysAlert'
|
||||||
@@ -90,7 +90,7 @@ export interface PushyOptions {
|
|||||||
beforeDownloadUpdate?: (info: CheckResult) => Promise<boolean>;
|
beforeDownloadUpdate?: (info: CheckResult) => Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PushyTestPayload {
|
export interface UpdateTestPayload {
|
||||||
type: '__rnPushyVersionHash' | string | null;
|
type: '__rnPushyVersionHash' | string | null;
|
||||||
data: any;
|
data: any;
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import { Platform } from 'react-native';
|
import { Platform } from 'react-native';
|
||||||
|
|
||||||
export function log(...args: any[]) {
|
export function log(...args: any[]) {
|
||||||
console.log('pushy: ', ...args);
|
console.log('react-native-update: ', ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function promiseAny<T>(promises: Promise<T>[]) {
|
export function promiseAny<T>(promises: Promise<T>[]) {
|
||||||
|
Reference in New Issue
Block a user