美文网首页我爱编程
Node.js + Bsdiff 实现增量更新

Node.js + Bsdiff 实现增量更新

作者: ONEWateR | 来源:发表于2015-11-13 14:35 被阅读1763次

随着一些功能的持续添加,软件变得有点大。这时候,需要增量更新避免用户消耗更多流量来进行更新。

实现原理

原理相对比较简单,通过对比客户端版本的apk和最新版本的apk的二进制差异,生成的作为补丁。然后下载只需下载这个补丁,然后在客户端进行合成,即可实现增量更新。

由于后台使用到LeanCloud以及Node.js,所以自动生成差异包就选择用Node.js来完成。
客户端只需要添加一个合成apk的so文件即可。

Node.js 实现方案

生成差异包

var path = require('path'),
    child_process = require('child_process');

module.exports = function(originalFile, targetFile, patchFile, cb) {
  
  var bsdiffPath = path.join(__dirname, 'bin', process.platform, 'bsdiff');
  
  if (process.platform === 'win32') {
    bsdiffPath += '.exe';
  }
  
  var options = {
    timeout: 60 * 1000
  };
  
  var args = [originalFile, targetFile, patchFile];
  
  return child_process.execFile(bsdiffPath, args, options, cb);
};

处理上传

    var genPatch = require('./cloud/genPatch')
    var md5 = require('./cloud/md5')
    var fs = require('fs');
    var async = require('async');

    // 版本差的限制
    var LIMIT = 5;

    if (!request || !request.params.versionCode) {
        response.error("need versionCode")
    }

    var versionCode = request.params.versionCode;

    console.log(versionCode)

    var versions = [];
    for (var index = 1; index < LIMIT; index++) {
        versions.push(versionCode - index);
    }

    console.info(versions)
    
    md5("./apk/" + versionCode + ".apk", function(md5Code) {
        
        async.each(versions, function(oldVersion, callback) {
          fs.exists("./apk/" + oldVersion + ".apk", function (exists) {
            if (!exists) {
                callback(null);
                return;
            }
            console.log("生成差异包 old : " + oldVersion + " new : " + versionCode)
            genPatch(oldVersion, versionCode, function (error, path) {
                if (error) {
                    console.error(error);
                    callback(error);
                    return;
                }
                upload(path, oldVersion, versionCode, md5Code)
                callback(null);
                console.log(path + " 生成完毕.")
            })

        })
    }, function(err) {
        if (err) {
            console.error(err)
        }
    });
    })

    function upload(path, oldVersion, newVersion, md5Code) {
        if (newVersion <= oldVersion) {
            console.error("你他妈在逗我吗")
            return;
        }

        var SmartUpdatePatch = AV.Object.extend("SmartUpdatePatch");

        fs.readFile(path, function(err, buffer) {
            if (err) {
                console.error(err);
                return;
            }
            console.log("开始上传" + path)
            var file = new AV.File(oldVersion + "" + newVersion + ".patch", buffer);
            file.save().then(function() {
                var patch = new SmartUpdatePatch();
                patch.set("oldVersion", oldVersion);
                patch.set("newVersion", newVersion);
                patch.set("file", file);
                patch.set("md5", md5Code);

                patch.save().then(function() {
                    console.log(path + " 上传成功");
                }, function(err) {
                    console.error(path + " 上传失败,错误" + err);
                })
            }, function(error) {
                console.error("文件上传失败,错误" + error);
            });
        })

    }

客户端实现方案

客户端实现比较简单,根据 SmartAppUpdates 开源项目提供的jni,自行编译生成自己所需架构的so文件,其他的代码该开源项目都有提供。

相关文章

  • Node.js + Bsdiff 实现增量更新

    随着一些功能的持续添加,软件变得有点大。这时候,需要增量更新避免用户消耗更多流量来进行更新。 实现原理 原理相对比...

  • centos7安装bsdiff

    centos7安装bsdiff bsdiff是一个差异包比较工具,可以用来实现增量更新. 安装bsdiff 参考链...

  • bsdiff实现增量更新

    概述 在目前的大部分热门应用中(QQ、微信、抖音等)都包含了一个名称类似libbspatch.so 的动态库。 可...

  • Android 增量更新实现(Bsdiff)

    一,增量更新的原理,主要是根据新旧包生成差分包,这个差分包由服务器端生成,客户端下载,下载完成后,进行合并,合并成...

  • bsdiff 编译与运行

    bsdiff是一个差异包比较工具,可以用来实现增量更新. 1.mac解压下载的bsdiff-4.3.tar.gz ...

  • 增量更新之服务器端解决方案

    准备工作: 增量更新开源库:http://www.daemonology.net/bsdiff/bsdiff还依赖...

  • 增量更新之APP端解决方案

    准备工作: 增量更新开源库:http://www.daemonology.net/bsdiff/bsdiff还依赖...

  • APP升级---BsDiff增量更新

    BsDiff算法介绍: bsdiff是由Conlin Percival开源的一个优秀的差分算法,而且是跨平台的。在...

  • Android_增量更新(BSDiff)

    Android中的增量更新指的是利用差分算法,计算两个App版本的差异,生成差分包,只需要下载差分包,不需要下载整...

  • Android增量更新(bsdiff使用)

    简单了解 增量更新是什么?APP版本更新时不需要客户端下载新的apk,只需要下载差分包(两个apk的差异)。 差分...

网友评论

    本文标题:Node.js + Bsdiff 实现增量更新

    本文链接:https://www.haomeiwen.com/subject/ssychttx.html