mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-18 00:16:10 +08:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1614a21968 | ||
![]() |
d7879ac2fe | ||
![]() |
a564770597 | ||
![]() |
7229f8847a | ||
![]() |
1daafb0142 | ||
![]() |
c1679a4cea | ||
![]() |
7ab7dffb0f | ||
![]() |
8622935bdf | ||
![]() |
b747b1f356 | ||
![]() |
7752581470 | ||
![]() |
33eb89d2a7 | ||
![]() |
d111bf5a9c |
@@ -9,6 +9,19 @@ def isNewArchitectureEnabled() {
|
||||
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
|
||||
}
|
||||
|
||||
def supportsNamespace() {
|
||||
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
||||
def major = parsed[0].toInteger()
|
||||
def minor = parsed[1].toInteger()
|
||||
|
||||
// Namespace support was added in 7.3.0
|
||||
if (major == 7 && minor >= 3) {
|
||||
return true
|
||||
}
|
||||
|
||||
return major >= 8
|
||||
}
|
||||
|
||||
apply plugin: 'com.android.library'
|
||||
if (isNewArchitectureEnabled()) {
|
||||
apply plugin: 'com.facebook.react'
|
||||
@@ -16,6 +29,16 @@ if (isNewArchitectureEnabled()) {
|
||||
|
||||
|
||||
android {
|
||||
|
||||
if (supportsNamespace()) {
|
||||
namespace "cn.reactnative.modules.update"
|
||||
|
||||
sourceSets {
|
||||
main {
|
||||
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
||||
}
|
||||
}
|
||||
}
|
||||
compileSdkVersion safeExtGet('compileSdkVersion', 28)
|
||||
buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')
|
||||
|
||||
|
@@ -1,4 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="cn.reactnative.modules.update">
|
||||
<application>
|
||||
|
14
android/src/main/AndroidManifestNew.xml
Normal file
14
android/src/main/AndroidManifestNew.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<application>
|
||||
<meta-data android:name="pushy_build_time" android:value="@string/pushy_build_time" />
|
||||
<provider
|
||||
android:name=".PushyFileProvider"
|
||||
android:authorities="${applicationId}.pushy.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/pushy_file_paths" />
|
||||
</provider>
|
||||
</application>
|
||||
</manifest>
|
@@ -237,19 +237,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry ze = entries.nextElement();
|
||||
|
||||
String fn = ze.getName();
|
||||
File fmd = new File(param.unzipDirectory, fn);
|
||||
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Unzipping " + fn);
|
||||
}
|
||||
|
||||
if (ze.isDirectory()) {
|
||||
fmd.mkdirs();
|
||||
continue;
|
||||
}
|
||||
|
||||
zipFile.unzipToFile(ze, fmd);
|
||||
zipFile.unzipToPath(ze, param.unzipDirectory);
|
||||
}
|
||||
|
||||
zipFile.close();
|
||||
@@ -324,8 +312,15 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
||||
} else {
|
||||
target = copyList.get((from));
|
||||
}
|
||||
target.add(new File(param.unzipDirectory, to));
|
||||
//copyFromResource(from, new File(param.unzipDirectory, to));
|
||||
File toFile = new File(param.unzipDirectory, to);
|
||||
|
||||
// Fixing a Zip Path Traversal Vulnerability
|
||||
// https://support.google.com/faqs/answer/9294009
|
||||
String canonicalPath = toFile.getCanonicalPath();
|
||||
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
|
||||
throw new SecurityException("Illegal name: " + to);
|
||||
}
|
||||
target.add(toFile);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -339,18 +334,9 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
||||
fout.close();
|
||||
continue;
|
||||
}
|
||||
File fmd = new File(param.unzipDirectory, fn);
|
||||
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Unzipping " + fn);
|
||||
}
|
||||
|
||||
if (ze.isDirectory()) {
|
||||
fmd.mkdirs();
|
||||
continue;
|
||||
}
|
||||
|
||||
zipFile.unzipToFile(ze, fmd);
|
||||
zipFile.unzipToPath(ze, param.unzipDirectory);
|
||||
}
|
||||
|
||||
zipFile.close();
|
||||
@@ -419,18 +405,8 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
||||
fout.close();
|
||||
continue;
|
||||
}
|
||||
File fmd = new File(param.unzipDirectory, fn);
|
||||
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Unzipping " + fn);
|
||||
}
|
||||
|
||||
if (ze.isDirectory()) {
|
||||
fmd.mkdirs();
|
||||
continue;
|
||||
}
|
||||
|
||||
zipFile.unzipToFile(ze, fmd);
|
||||
zipFile.unzipToPath(ze, param.unzipDirectory);
|
||||
}
|
||||
|
||||
zipFile.close();
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package cn.reactnative.modules.update;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
@@ -10,12 +12,15 @@ import java.util.Enumeration;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
|
||||
public class SafeZipFile extends ZipFile {
|
||||
|
||||
public SafeZipFile(File file) throws IOException {
|
||||
super(file);
|
||||
}
|
||||
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
@Override
|
||||
public Enumeration<? extends ZipEntry> entries() {
|
||||
return new SafeZipEntryIterator(super.entries());
|
||||
@@ -43,40 +48,46 @@ public class SafeZipFile extends ZipFile {
|
||||
* avoid ZipperDown
|
||||
*/
|
||||
if (null != name && (name.contains("../") || name.contains("..\\"))) {
|
||||
throw new SecurityException("illegal entry: " + entry.getName());
|
||||
throw new SecurityException("illegal entry: " + name);
|
||||
}
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
public void unzipToFile(ZipEntry entry, File output) throws IOException {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = getInputStream(entry);
|
||||
writeOutInputStream(output, inputStream);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
public void unzipToPath(ZipEntry ze, File targetPath) throws IOException {
|
||||
String name = ze.getName();
|
||||
File target = new File(targetPath, name);
|
||||
|
||||
// Fixing a Zip Path Traversal Vulnerability
|
||||
// https://support.google.com/faqs/answer/9294009
|
||||
String canonicalPath = target.getCanonicalPath();
|
||||
if (!canonicalPath.startsWith(targetPath.getCanonicalPath() + File.separator)) {
|
||||
throw new SecurityException("Illegal name: " + name);
|
||||
}
|
||||
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Unzipping " + name);
|
||||
}
|
||||
|
||||
if (ze.isDirectory()) {
|
||||
target.mkdirs();
|
||||
return;
|
||||
}
|
||||
unzipToFile(ze, target);
|
||||
}
|
||||
|
||||
public void unzipToFile(ZipEntry ze, File target) throws IOException {
|
||||
try (InputStream inputStream = getInputStream(ze)) {
|
||||
try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(target));
|
||||
BufferedInputStream input = new BufferedInputStream(inputStream)) {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int n;
|
||||
while ((n = input.read(buffer, 0, BUFFER_SIZE)) >= 0) {
|
||||
output.write(buffer, 0, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void writeOutInputStream(File file, InputStream inputStream) throws IOException {
|
||||
BufferedOutputStream output = null;
|
||||
try {
|
||||
output = new BufferedOutputStream(
|
||||
new FileOutputStream(file));
|
||||
BufferedInputStream input = new BufferedInputStream(inputStream);
|
||||
byte b[] = new byte[8192];
|
||||
int n;
|
||||
while ((n = input.read(b, 0, 8192)) >= 0) {
|
||||
output.write(b, 0, n);
|
||||
}
|
||||
} finally {
|
||||
if (output != null) {
|
||||
output.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -349,8 +349,7 @@ RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(markSuccess:
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
RCT_EXPORT_METHOD(markSuccess:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
|
||||
|
@@ -1,13 +1,21 @@
|
||||
import { logger } from './utils';
|
||||
import { logger, promiseAny } from './utils';
|
||||
|
||||
let currentEndpoint = 'https://update.react-native.cn/api';
|
||||
let backupEndpoints: string[] = ['https://update.reactnative.cn/api'];
|
||||
let backupEndpointsQueryUrl: string | null = null;
|
||||
let backupEndpoints: string[] = [
|
||||
'https://pushy-koa-qgbgqmcpis.cn-beijing.fcapp.run',
|
||||
'https://update.reactnative.cn/api',
|
||||
];
|
||||
let backupEndpointsQueryUrls = [
|
||||
'https://gitee.com/sunnylqm/react-native-pushy/raw/master/endpoints.json',
|
||||
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json',
|
||||
];
|
||||
|
||||
export async function updateBackupEndpoints() {
|
||||
if (backupEndpointsQueryUrl) {
|
||||
if (backupEndpointsQueryUrls) {
|
||||
try {
|
||||
const resp = await fetch(backupEndpointsQueryUrl);
|
||||
const resp = await promiseAny(
|
||||
backupEndpointsQueryUrls.map(queryUrl => fetch(queryUrl)),
|
||||
);
|
||||
const remoteEndpoints = await resp.json();
|
||||
if (Array.isArray(remoteEndpoints)) {
|
||||
backupEndpoints = Array.from(
|
||||
@@ -36,18 +44,17 @@ export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
|
||||
export function setCustomEndpoints({
|
||||
main,
|
||||
backups,
|
||||
backupQueryUrl,
|
||||
backupQueryUrls,
|
||||
}: {
|
||||
main: string;
|
||||
backups?: string[];
|
||||
backupQueryUrl?: string;
|
||||
backupQueryUrls?: string[];
|
||||
}) {
|
||||
currentEndpoint = main;
|
||||
backupEndpointsQueryUrl = null;
|
||||
if (Array.isArray(backups) && backups.length > 0) {
|
||||
backupEndpoints = backups;
|
||||
}
|
||||
if (typeof backupQueryUrl === 'string') {
|
||||
backupEndpointsQueryUrl = backupQueryUrl;
|
||||
if (Array.isArray(backupQueryUrls) && backupQueryUrls.length > 0) {
|
||||
backupEndpointsQueryUrls = backupQueryUrls;
|
||||
}
|
||||
}
|
||||
|
@@ -15,4 +15,4 @@ export const downloadAndInstallApk = noop;
|
||||
export const setCustomEndpoints = noop;
|
||||
export const getCurrentVersionInfo = noop;
|
||||
export const simpleUpdate = (app) => app;
|
||||
export const onEvents = noop;
|
||||
export const onPushyEvents = noop;
|
||||
|
31
lib/main.ts
31
lib/main.ts
@@ -16,7 +16,7 @@ import {
|
||||
UpdateAvailableResult,
|
||||
UpdateEventsListener,
|
||||
} from './type';
|
||||
import { assertRelease, logger } from './utils';
|
||||
import { assertRelease, logger, promiseAny, testUrls } from './utils';
|
||||
export { setCustomEndpoints };
|
||||
const {
|
||||
version: v,
|
||||
@@ -78,7 +78,7 @@ if (!uuid) {
|
||||
const noop = () => {};
|
||||
let reporter: UpdateEventsListener = noop;
|
||||
|
||||
export function onEvents(customReporter: UpdateEventsListener) {
|
||||
export function onPushyEvents(customReporter: UpdateEventsListener) {
|
||||
reporter = customReporter;
|
||||
if (isRolledBack) {
|
||||
report({
|
||||
@@ -167,8 +167,8 @@ export async function checkUpdate(APPKEY: string) {
|
||||
const backupEndpoints = await updateBackupEndpoints();
|
||||
if (backupEndpoints) {
|
||||
try {
|
||||
resp = await Promise.race(
|
||||
backupEndpoints.map((endpoint) =>
|
||||
resp = await promiseAny(
|
||||
backupEndpoints.map(endpoint =>
|
||||
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||
),
|
||||
);
|
||||
@@ -205,7 +205,7 @@ function checkOperation(
|
||||
if (!Array.isArray(op)) {
|
||||
return;
|
||||
}
|
||||
op.forEach((action) => {
|
||||
op.forEach(action => {
|
||||
if (action.type === 'block') {
|
||||
blockUpdate = {
|
||||
reason: action.reason,
|
||||
@@ -250,7 +250,7 @@ export async function downloadUpdate(
|
||||
const downloadCallback = eventListeners.onDownloadProgress;
|
||||
progressHandler = eventEmitter.addListener(
|
||||
'RCTPushyDownloadProgress',
|
||||
(progressData) => {
|
||||
progressData => {
|
||||
if (progressData.hash === options.hash) {
|
||||
downloadCallback(progressData);
|
||||
}
|
||||
@@ -260,11 +260,12 @@ export async function downloadUpdate(
|
||||
}
|
||||
let succeeded = false;
|
||||
report({ type: 'downloading' });
|
||||
if (options.diffUrl) {
|
||||
const diffUrl = (await testUrls(options.diffUrls)) || options.diffUrl;
|
||||
if (diffUrl) {
|
||||
logger('downloading diff');
|
||||
try {
|
||||
await PushyModule.downloadPatchFromPpk({
|
||||
updateUrl: options.diffUrl,
|
||||
updateUrl: diffUrl,
|
||||
hash: options.hash,
|
||||
originHash: currentVersion,
|
||||
});
|
||||
@@ -273,11 +274,13 @@ export async function downloadUpdate(
|
||||
logger(`diff error: ${e.message}, try pdiff`);
|
||||
}
|
||||
}
|
||||
if (!succeeded && options.pdiffUrl) {
|
||||
if (!succeeded) {
|
||||
const pdiffUrl = (await testUrls(options.pdiffUrls)) || options.pdiffUrl;
|
||||
if (pdiffUrl) {
|
||||
logger('downloading pdiff');
|
||||
try {
|
||||
await PushyModule.downloadPatchFromPackage({
|
||||
updateUrl: options.pdiffUrl,
|
||||
updateUrl: pdiffUrl,
|
||||
hash: options.hash,
|
||||
});
|
||||
succeeded = true;
|
||||
@@ -285,11 +288,14 @@ export async function downloadUpdate(
|
||||
logger(`pdiff error: ${e.message}, try full patch`);
|
||||
}
|
||||
}
|
||||
if (!succeeded && options.updateUrl) {
|
||||
}
|
||||
if (!succeeded) {
|
||||
const updateUrl = (await testUrls(options.updateUrls)) || options.updateUrl;
|
||||
if (updateUrl) {
|
||||
logger('downloading full patch');
|
||||
try {
|
||||
await PushyModule.downloadFullUpdate({
|
||||
updateUrl: options.updateUrl,
|
||||
updateUrl: updateUrl,
|
||||
hash: options.hash,
|
||||
});
|
||||
succeeded = true;
|
||||
@@ -297,6 +303,7 @@ export async function downloadUpdate(
|
||||
logger(`full patch error: ${e.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
progressHandler && progressHandler.remove();
|
||||
if (!succeeded) {
|
||||
return report({ type: 'errorUpdate', data: { newVersion: options.hash } });
|
||||
|
@@ -16,20 +16,20 @@ import {
|
||||
switchVersionLater,
|
||||
markSuccess,
|
||||
downloadAndInstallApk,
|
||||
onEvents,
|
||||
onPushyEvents,
|
||||
} from './main';
|
||||
import { UpdateEventsListener } from './type';
|
||||
|
||||
export function simpleUpdate(
|
||||
WrappedComponent: ComponentType,
|
||||
options: { appKey?: string; onEvents?: UpdateEventsListener } = {},
|
||||
options: { appKey?: string; onPushyEvents?: UpdateEventsListener } = {},
|
||||
) {
|
||||
const { appKey, onEvents: eventListeners } = options;
|
||||
const { appKey, onPushyEvents: eventListeners } = options;
|
||||
if (!appKey) {
|
||||
throw new Error('appKey is required for simpleUpdate()');
|
||||
}
|
||||
if (typeof eventListeners === 'function') {
|
||||
onEvents(eventListeners);
|
||||
onPushyEvents(eventListeners);
|
||||
}
|
||||
return __DEV__
|
||||
? WrappedComponent
|
||||
|
@@ -19,8 +19,11 @@ export interface UpdateAvailableResult {
|
||||
description: string;
|
||||
metaInfo: string;
|
||||
pdiffUrl: string;
|
||||
pdiffUrls?: string[];
|
||||
diffUrl?: string;
|
||||
diffUrls?: string[];
|
||||
updateUrl?: string;
|
||||
updateUrls?: string[];
|
||||
}
|
||||
|
||||
export type CheckResult =
|
||||
|
37
lib/utils.ts
37
lib/utils.ts
@@ -1,9 +1,46 @@
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
export function logger(...args: any[]) {
|
||||
console.log('Pushy: ', ...args);
|
||||
}
|
||||
|
||||
export function promiseAny<T>(promises: Promise<T>[]) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
let count = 0;
|
||||
|
||||
promises.forEach(promise => {
|
||||
Promise.resolve(promise)
|
||||
.then(resolve)
|
||||
.catch(() => {
|
||||
count++;
|
||||
if (count === promises.length) {
|
||||
reject(new Error('All promises were rejected'));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function assertRelease() {
|
||||
if (__DEV__) {
|
||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
||||
}
|
||||
}
|
||||
|
||||
const ping =
|
||||
Platform.OS === 'web'
|
||||
? Promise.resolve
|
||||
: async (url: string) =>
|
||||
Promise.race([
|
||||
fetch(url, {
|
||||
method: 'HEAD',
|
||||
}).then(({ status }) => (status === 200 ? url : null)),
|
||||
new Promise(r => setTimeout(() => r(null), 2000)),
|
||||
]);
|
||||
|
||||
export const testUrls = async (urls?: string[]) => {
|
||||
if (!urls?.length) {
|
||||
return null;
|
||||
}
|
||||
return promiseAny(urls.map(ping)).catch(() => null);
|
||||
};
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-update",
|
||||
"version": "9.1.2",
|
||||
"version": "9.2.2",
|
||||
"description": "react-native hot update",
|
||||
"main": "lib/index.ts",
|
||||
"scripts": {
|
||||
@@ -67,5 +67,6 @@
|
||||
"react-native": "^0.72.6",
|
||||
"ts-jest": "^29.0.3",
|
||||
"typescript": "^5.2.2"
|
||||
}
|
||||
},
|
||||
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
||||
}
|
||||
|
Reference in New Issue
Block a user