首页 > 解决方案 > Node.js 缓冲区异或意外行为

问题描述

设想:

工作场景代码

function erasureChunkCreate(chunks,callback) {
let erasureChunks = {};
let locale = undefined;
let global = undefined;
erasureChunks = {'local': {}, 'global': {}};
async.eachSeries(chunks, (item, callback_times) => {
    if (locale === undefined)
        locale = item.chunk.slice();
    else
        locale = xor(locale, item.chunk);

    if (item.seq_no % 3 === 2) {
        erasureChunks['local'][Math.ceil(item.seq_no / 3)] = locale;
        if (global === undefined)
            global = locale.slice();
        else
            global = xor(global, locale);
        locale = undefined;
    }
    callback_times(null);
}, (err_times) => {
    if (err_times)
        return callback(err_times, null);
    erasureChunks['global'] = global;
    return callback(null, erasureChunks);
});

}

问题:

意外场景引导代码部分

    if (locale === undefined)
        locale = item.chunk.slice();
    else
        locale = xor(locale, item.chunk);

    if (global === undefined)
        global = locale.slice();
    else
        global = xor(global, locale);

    if (item.seq_no % 3 === 2) {
        erasureChunks['local'][Math.ceil(item.seq_no / 3)] = locale;
        locale = undefined;
    }

我尝试了什么:

使用的模块:

标签: node.jsxorasync.js

解决方案


推荐阅读