1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-10-31 13:23:12 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
tdzl2003
2016-04-04 23:02:28 +08:00
parent b9ea739a44
commit 063c14ab45
10 changed files with 394 additions and 346 deletions

View File

@@ -1,8 +0,0 @@
/**
* Created by tdzl2003 on 2/6/16.
*/
// Hook resolvedAssetSource
__d('resolveAssetSource', function(_1, _2, module, _3) {
module.exports = require('./resolveAssetSource');
});

84
lib/index.js Normal file
View File

@@ -0,0 +1,84 @@
/**
* Created by tdzl2003 on 4/4/16.
*/
const HotUpdate = require('react-native').NativeModules.HotUpdate;
let host = 'http://update.reactnative.cn/api';
export const downloadRootDir = HotUpdate.downloadRootDir;
export const packageVersion = HotUpdate.packageVersion;
export const currentVersion = HotUpdate.currentVersion;
export const isFirstTime = HotUpdate.isFirstTime;
export const isRolledBack = HotUpdate.isRolledBack;
/*
Return json:
Package was expired:
{
expired: true,
downloadUrl: 'http://appstore/downloadUrl',
}
Package is up to date:
{
upToDate: true,
}
There is available update:
{
update: true,
name: '1.0.3-rc',
hash: 'hash',
description: '添加聊天功能\n修复商城页面BUG',
metaInfo: '{"silent":true}',
updateUrl: 'http://update-packages.reactnative.cn/hash',
pdiffUrl: 'http://update-packages.reactnative.cn/hash',
diffUrl: 'http://update-packages.reactnative.cn/hash',
}
*/
export async function checkUpdate(APPKEY) {
const resp = await fetch(`${host}/checkUpdate/${APPKEY}`, {
method: 'POST',
body: JSON.stringify({
packageVersion: packageVersion,
hash: currentVersion,
}),
});
if (resp.status !== 200) {
throw new Error(resp.message);
}
return await resp.json();
}
export async function downloadUpdate(options) {
if (!options.update) {
return;
}
if (options.diff) {
await HotUpdate.downloadPatchFromPpk({
updateUrl: options.diffUrl,
hashName: options.hash,
originHash: currentVersion,
});
} else if (options.diff) {
await HotUpdate.downloadPatchFromPackage({
updateUrl: options.pdiffUrl,
hashName: options.hash,
});
} else {
await HotUpdate.downloadPatchFromPackage({
updateUrl: options.updateUrl,
hashName: options.hash,
});
}
}
export async function switchVersion({hash}) {
HotUpdate.reloadUpdate({hashName:hash});
}
export async function switchVersionLater({hash}) {
HotUpdate.setNeedUpdate({hashName:hash});
}

View File

@@ -1,144 +0,0 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule resolveAssetSource
* @flow
*
* Resolves an asset into a `source` for `Image`.
*
* Hooked by @tdzl2003 for react-native-pushy
*/
'use strict';
var AssetRegistry = require('react-native/Libraries/Image/AssetRegistry');
var PixelRatio = require('react-native/Libraries/Utilities/PixelRatio');
var Platform = require('react-native/Libraries/Utilities/Platform');
var NativeModules = require('react-native/Libraries/BatchedBridge/BatchedBridgedModules/NativeModules.js');
var SourceCode = NativeModules.SourceCode;
var _serverURL, _offlinePath;
function getDevServerURL() {
if (_serverURL === undefined) {
var scriptURL = SourceCode.scriptURL;
var match = scriptURL && scriptURL.match(/^https?:\/\/.*?\//);
if (match) {
// Bundle was loaded from network
_serverURL = match[0];
} else {
// Bundle was loaded from file
_serverURL = null;
}
}
return _serverURL;
}
function getOfflinePath() {
if (_offlinePath === undefined) {
var scriptURL = SourceCode.scriptURL;
var match = scriptURL && scriptURL.match(/^file:\/\/(\/.*\/)/);
if (match) {
_offlinePath = match[1];
} else {
_offlinePath = '';
}
}
return _offlinePath;
}
/**
* Returns the path at which the asset can be found in the archive
*/
function getPathInArchive(asset) {
if (Platform.OS === 'android') {
var assetDir = getBasePath(asset);
// E.g. 'assets_awesomemodule_icon'
// The Android resource system picks the correct scale.
return (assetDir + '/' + asset.name)
.toLowerCase()
.replace(/\//g, '_') // Encode folder structure in file name
.replace(/([^a-z0-9_])/g, '') // Remove illegal chars
.replace(/^assets_/, ''); // Remove "assets_" prefix
} else {
// E.g. 'assets/AwesomeModule/icon@2x.png'
return getOfflinePath() + getScaledAssetPath(asset);
}
}
/**
* Returns an absolute URL which can be used to fetch the asset
* from the devserver
*/
function getPathOnDevserver(devServerUrl, asset) {
return devServerUrl + getScaledAssetPath(asset) + '?platform=' + Platform.OS +
'&hash=' + asset.hash;
}
/**
* Returns a path like 'assets/AwesomeModule'
*/
function getBasePath(asset) {
var path = asset.httpServerLocation;
if (path[0] === '/') {
path = path.substr(1);
}
return path;
}
/**
* Returns a path like 'assets/AwesomeModule/icon@2x.png'
*/
function getScaledAssetPath(asset) {
var scale = pickScale(asset.scales, PixelRatio.get());
var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
var assetDir = getBasePath(asset);
return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type;
}
function pickScale(scales, deviceScale) {
// Packager guarantees that `scales` array is sorted
for (var i = 0; i < scales.length; i++) {
if (scales[i] >= deviceScale) {
return scales[i];
}
}
// If nothing matches, device scale is larger than any available
// scales, so we return the biggest one. Unless the array is empty,
// in which case we default to 1
return scales[scales.length - 1] || 1;
}
function resolveAssetSource(source){
if (typeof source === 'object') {
return source;
}
var asset = AssetRegistry.getAssetByID(source);
if (asset) {
return assetToImageSource(asset);
}
return null;
}
function assetToImageSource(asset){
var devServerURL = getDevServerURL();
return {
__packager_asset: true,
width: asset.width,
height: asset.height,
uri: devServerURL ? getPathOnDevserver(devServerURL, asset) : getPathInArchive(asset),
scale: pickScale(asset.scales, PixelRatio.get()),
};
}
module.exports = resolveAssetSource;
module.exports.pickScale = pickScale;