Skip to content

Commit bbda721

Browse files
committed
add cresc
1 parent 0b52cf3 commit bbda721

8 files changed

+90
-65
lines changed

endpoints_cresc.json

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

src/client.ts

+53-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
1+
import { CheckResult, ClientOptions, ProgressData, EventType } from './type';
22
import { emptyObj, joinUrls, log, noop, promiseAny, testUrls } from './utils';
33
import { EmitterSubscription, Platform } from 'react-native';
44
import { PermissionsAndroid } from './permissions';
@@ -15,31 +15,45 @@ import {
1515
isRolledBack,
1616
} from './core';
1717

18-
const defaultServer = {
19-
main: 'https://update.react-native.cn/api',
20-
backups: ['https://update.reactnative.cn/api'],
21-
queryUrls: [
22-
'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json',
23-
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-update@master/endpoints.json',
24-
],
18+
const SERVER_PRESETS = {
19+
pushy: {
20+
main: 'https://update.react-native.cn/api',
21+
backups: ['https://update.reactnative.cn/api'],
22+
queryUrls: [
23+
'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json',
24+
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-update@master/endpoints.json',
25+
],
26+
},
27+
cresc: {
28+
main: 'https://api.cresc.dev',
29+
backups: ['https://api.cresc.app'],
30+
queryUrls: [
31+
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-update@master/endpoints_cresc.json',
32+
],
33+
},
2534
};
26-
2735
if (Platform.OS === 'web') {
28-
console.warn('react-native-update 不支持 web 端热更,不会执行操作');
36+
console.warn(
37+
'react-native-update does not support hot updates on the web platform and will not perform any operations',
38+
);
2939
}
3040

41+
const defaultClientOptions: ClientOptions = {
42+
appKey: '',
43+
autoMarkSuccess: true,
44+
updateStrategy: __DEV__ ? 'alwaysAlert' : 'alertUpdateAndIgnoreError',
45+
checkStrategy: 'both',
46+
logger: noop,
47+
debug: false,
48+
throwError: false,
49+
};
50+
3151
export class Pushy {
32-
options: PushyOptions = {
33-
appKey: '',
34-
server: defaultServer,
35-
autoMarkSuccess: true,
36-
updateStrategy: __DEV__ ? 'alwaysAlert' : 'alertUpdateAndIgnoreError',
37-
checkStrategy: 'both',
38-
logger: noop,
39-
debug: false,
40-
throwError: false,
52+
options: ClientOptions = {
53+
...defaultClientOptions,
54+
server: SERVER_PRESETS.pushy,
4155
};
42-
56+
clientType: 'pushy' | 'cresc' = 'pushy';
4357
lastChecking?: number;
4458
lastRespJson?: Promise<any>;
4559

@@ -50,7 +64,7 @@ export class Pushy {
5064

5165
static marked = false;
5266
static applyingUpdate = false;
53-
version = cInfo.pushy;
67+
version = cInfo.rnu;
5468
loggerPromise = (() => {
5569
let resolve: (value?: unknown) => void = () => {};
5670
const promise = new Promise(res => {
@@ -62,7 +76,7 @@ export class Pushy {
6276
};
6377
})();
6478

65-
constructor(options: PushyOptions) {
79+
constructor(options: ClientOptions) {
6680
if (Platform.OS === 'ios' || Platform.OS === 'android') {
6781
if (!options.appKey) {
6882
throw new Error('appKey is required');
@@ -79,7 +93,7 @@ export class Pushy {
7993
}
8094
}
8195

82-
setOptions = (options: Partial<PushyOptions>) => {
96+
setOptions = (options: Partial<ClientOptions>) => {
8397
for (const [key, value] of Object.entries(options)) {
8498
if (value !== undefined) {
8599
(this.options as any)[key] = value;
@@ -124,10 +138,10 @@ export class Pushy {
124138
return `${endpoint}/checkUpdate/${this.options.appKey}`;
125139
};
126140
static assertHash = (hash: string) => {
127-
if (!Pushy.downloadedHash) {
141+
if (!this.downloadedHash) {
128142
return;
129143
}
130-
if (hash !== Pushy.downloadedHash) {
144+
if (hash !== this.downloadedHash) {
131145
log(`use downloaded hash ${Pushy.downloadedHash} first`);
132146
return;
133147
}
@@ -144,7 +158,7 @@ export class Pushy {
144158
switchVersion = async (hash: string) => {
145159
if (__DEV__) {
146160
console.warn(
147-
'您调用了switchVersion方法,但是当前是开发环境,不会进行任何操作。',
161+
'switchVersion() is not supported in development environment; no action taken.',
148162
);
149163
return;
150164
}
@@ -158,7 +172,7 @@ export class Pushy {
158172
switchVersionLater = async (hash: string) => {
159173
if (__DEV__) {
160174
console.warn(
161-
'您调用了switchVersionLater方法,但是当前是开发环境,不会进行任何操作。',
175+
'switchVersionLater() is not supported in development environment; no action taken.',
162176
);
163177
return;
164178
}
@@ -170,19 +184,19 @@ export class Pushy {
170184
checkUpdate = async (extra?: Record<string, any>) => {
171185
if (__DEV__ && !this.options.debug) {
172186
console.info(
173-
'您当前处于开发环境且未启用 debug,不会进行热更检查。如需在开发环境中调试热更,请在 client 中设置 debug true',
187+
'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.',
174188
);
175189
return;
176190
}
177191
if (Platform.OS === 'web') {
178-
console.warn('web 端不支持热更新检查');
192+
console.warn('web platform does not support hot update check');
179193
return;
180194
}
181195
if (
182196
this.options.beforeCheckUpdate &&
183197
(await this.options.beforeCheckUpdate()) === false
184198
) {
185-
log('beforeCheckUpdate 返回 false, 忽略检查');
199+
log('beforeCheckUpdate returned false, skipping check');
186200
return;
187201
}
188202
const now = Date.now();
@@ -310,7 +324,7 @@ export class Pushy {
310324
this.options.beforeDownloadUpdate &&
311325
(await this.options.beforeDownloadUpdate(info)) === false
312326
) {
313-
log('beforeDownloadUpdate 返回 false, 忽略下载');
327+
log('beforeDownloadUpdate returned false, skipping download');
314328
return;
315329
}
316330
if (!info.update || !hash) {
@@ -489,3 +503,11 @@ export class Pushy {
489503
}
490504
};
491505
}
506+
507+
export class Cresc extends Pushy {
508+
clientType: 'cresc' | 'pushy' = 'cresc';
509+
options: ClientOptions = {
510+
...defaultClientOptions,
511+
server: SERVER_PRESETS.cresc,
512+
};
513+
}

src/context.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createContext, useContext } from 'react';
22
import { CheckResult, ProgressData } from './type';
3-
import { Pushy } from './client';
3+
import { Pushy, Cresc } from './client';
44

55
const noop = () => {};
66
const asyncNoop = () => Promise.resolve();
@@ -19,7 +19,7 @@ export const defaultContext = {
1919
packageVersion: '',
2020
};
2121

22-
export const PushyContext = createContext<{
22+
export const UpdateContext = createContext<{
2323
checkUpdate: () => Promise<void>;
2424
switchVersion: () => Promise<void>;
2525
switchVersionLater: () => Promise<void>;
@@ -35,10 +35,12 @@ export const PushyContext = createContext<{
3535
parseTestQrCode: (code: string) => boolean;
3636
currentHash: string;
3737
packageVersion: string;
38-
client?: Pushy;
38+
client?: Pushy | Cresc;
3939
progress?: ProgressData;
4040
updateInfo?: CheckResult;
4141
lastError?: Error;
4242
}>(defaultContext);
4343

44-
export const usePushy = () => useContext(PushyContext);
44+
export const usePushy = () => useContext(UpdateContext);
45+
46+
export const useCresc = () => useContext(UpdateContext);

src/core.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ export const PushyModule =
1313
? require('./NativePushy').default
1414
: NativeModules.Pushy;
1515

16+
export const UpdateModule = PushyModule;
17+
1618
if (!PushyModule) {
17-
throw new Error('react-native-update 模块无法加载,请尝试重新编译');
19+
throw new Error(
20+
'Failed to load react-native-update native module, please try to recompile',
21+
);
1822
}
1923

2024
const PushyConstants = isTurboModuleEnabled
@@ -31,12 +35,6 @@ export const isRolledBack: boolean = typeof rolledBackVersion === 'string';
3135
export const buildTime: string = PushyConstants.buildTime;
3236
let uuid = PushyConstants.uuid;
3337

34-
if (Platform.OS === 'android' && !PushyConstants.isUsingBundleUrl) {
35-
console.warn(
36-
'react-native-update 没有检测到 Bundle URL 的配置,这可能是因为您使用了某种懒加载机制(比如使用 expo,可忽略本警告),或是 Bundle URL 的配置不正确(请对照文档检查)',
37-
);
38-
}
39-
4038
export function setLocalHashInfo(hash: string, info: Record<string, any>) {
4139
PushyModule.setLocalHashInfo(hash, JSON.stringify(info));
4240
}
@@ -63,7 +61,7 @@ if (!uuid) {
6361
log('uuid: ' + uuid);
6462

6563
export const cInfo = {
66-
pushy: require('../package.json').version,
64+
rnu: require('../package.json').version,
6765
rn: RNVersion,
6866
os: Platform.OS + ' ' + Platform.Version,
6967
uuid,

src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { Pushy } from './client';
2-
export { PushyContext, usePushy } from './context';
3-
export { PushyProvider } from './provider';
4-
export { PushyModule } from './core';
1+
export { Pushy, Cresc } from './client';
2+
export { UpdateContext, usePushy, useCresc } from './context';
3+
export { PushyProvider, UpdateProvider } from './provider';
4+
export { PushyModule, UpdateModule } from './core';

src/provider.tsx

+14-12
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ import {
1212
Platform,
1313
Linking,
1414
} from 'react-native';
15-
import { Pushy } from './client';
15+
import { Pushy, Cresc } from './client';
1616
import { currentVersion, packageVersion, getCurrentVersionInfo } from './core';
17-
import { CheckResult, ProgressData, PushyTestPayload } from './type';
18-
import { PushyContext } from './context';
17+
import { CheckResult, ProgressData, UpdateTestPayload } from './type';
18+
import { ClientContext } from './context';
1919
import { URL } from 'react-native-url-polyfill';
2020
import { isInRollout } from './isInRollout';
2121
import { log } from './utils';
2222

23-
export const PushyProvider = ({
23+
export const UpdateProvider = ({
2424
client,
2525
children,
2626
}: {
27-
client: Pushy;
27+
client: Pushy | Cresc;
2828
children: ReactNode;
2929
}) => {
3030
const { options } = client;
@@ -275,8 +275,8 @@ export const PushyProvider = ({
275275
}, [checkUpdate, options, dismissError, markSuccess]);
276276

277277
const parseTestPayload = useCallback(
278-
(payload: PushyTestPayload) => {
279-
if (payload && payload.type && payload.type.startsWith('__rnPushy')) {
278+
(payload: UpdateTestPayload) => {
279+
if (payload && payload.type && payload.type.startsWith('__rnUpdate')) {
280280
const logger = options.logger || (() => {});
281281
options.logger = ({ type, data }) => {
282282
logger({ type, data });
@@ -286,8 +286,8 @@ export const PushyProvider = ({
286286
checkUpdate({ extra: { toHash: payload.data } }).then(() => {
287287
if (updateInfoRef.current && updateInfoRef.current.upToDate) {
288288
Alert.alert(
289-
'提示',
290-
'当前尚未检测到更新版本,如果是首次扫码,请等待服务器端生成补丁包后再试(约10秒)',
289+
'Info',
290+
'No update found, please wait 10s for the server to generate the patch package',
291291
);
292292
}
293293
options.logger = logger;
@@ -301,7 +301,7 @@ export const PushyProvider = ({
301301
);
302302

303303
const parseTestQrCode = useCallback(
304-
(code: string | PushyTestPayload) => {
304+
(code: string | UpdateTestPayload) => {
305305
try {
306306
const payload = typeof code === 'string' ? JSON.parse(code) : code;
307307
return parseTestPayload(payload);
@@ -335,7 +335,7 @@ export const PushyProvider = ({
335335
}, [parseTestPayload]);
336336

337337
return (
338-
<PushyContext.Provider
338+
<ClientContext.Provider
339339
value={{
340340
checkUpdate,
341341
switchVersion,
@@ -354,6 +354,8 @@ export const PushyProvider = ({
354354
parseTestQrCode,
355355
}}>
356356
{children}
357-
</PushyContext.Provider>
357+
</ClientContext.Provider>
358358
);
359359
};
360+
361+
export const PushyProvider = UpdateProvider;

src/type.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export type EventType =
4444
export interface EventData {
4545
currentVersion: string;
4646
cInfo: {
47-
pushy: string;
47+
rnu: string;
4848
rn: string;
4949
os: string;
5050
uuid: string;
@@ -65,15 +65,15 @@ export type UpdateEventsLogger = ({
6565
data: EventData;
6666
}) => void;
6767

68-
export interface PushyServerConfig {
68+
export interface UpdateServerConfig {
6969
main: string;
7070
backups?: string[];
7171
queryUrls?: string[];
7272
}
7373

74-
export interface PushyOptions {
74+
export interface ClientOptions {
7575
appKey: string;
76-
server?: PushyServerConfig;
76+
server?: UpdateServerConfig;
7777
logger?: UpdateEventsLogger;
7878
updateStrategy?:
7979
| 'alwaysAlert'
@@ -90,7 +90,7 @@ export interface PushyOptions {
9090
beforeDownloadUpdate?: (info: CheckResult) => Promise<boolean>;
9191
}
9292

93-
export interface PushyTestPayload {
93+
export interface UpdateTestPayload {
9494
type: '__rnPushyVersionHash' | string | null;
9595
data: any;
9696
}

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Platform } from 'react-native';
22

33
export function log(...args: any[]) {
4-
console.log('pushy: ', ...args);
4+
console.log('react-native-update: ', ...args);
55
}
66

77
export function promiseAny<T>(promises: Promise<T>[]) {

0 commit comments

Comments
 (0)