Объединение большого количества массивов UInt8 - PullRequest
0 голосов
/ 21 апреля 2020

У меня есть некоторый код, чтобы, например, загрузить Arch Linux в браузере с js-ipfs. В настоящее время он работает.

async function start(event) {

  console.log("Starting IPFS...");
  node = await Ipfs.create();

  for await (const file of node.get('QmQxBX5ZKRY8k6W2UqYTMxhdFTvkmNw8X7GJN3t5UiyBpe')) {
    console.log("Starting");
    var content = [];
    for await (const chunk of file.content) {
      console.log("Gathering");
      content = mergeTypedArrays(content, chunk); // slow
    }
    console.log("Assembling");
    saveFile("arch.iso", "application/octet-stream", content);
    console.log("Done");
  };
}

// https://stackoverflow.com/a/35633935/2700296
function mergeTypedArrays(a, b) {
  // Checks for truthy values on both arrays
  if(!a && !b) throw 'Please specify valid arguments for parameters a and b.';  

  // Checks for truthy values or empty arrays on each argument
  // to avoid the unnecessary construction of a new array and
  // the type comparison
  if(!b || b.length === 0) return a;
  if(!a || a.length === 0) return b;

  // Make sure that both typed arrays are of the same type
  if(Object.prototype.toString.call(a) !== Object.prototype.toString.call(b))
      throw 'The types of the two arguments passed for parameters a and b do not match.';

  var c = new a.constructor(a.length + b.length);
  c.set(a);
  c.set(b, a.length);

  return c;
}

// https://stackoverflow.com/a/36899900/2700296
function saveFile (name, type, data) {
  if (data !== null && navigator.msSaveBlob) {
    return navigator.msSaveBlob(new Blob([data], { type: type }), name);
  }
  var a = document.createElement('a');
  a.style.display = "none";
  var url = window.URL.createObjectURL(new Blob([data], {type: type}));
  a.setAttribute("href", url);
  a.setAttribute("download", name);
  document.body.appendChild(a);
  a.click();
  window.URL.revokeObjectURL(url);
  a.remove();
}

Проблема в том, что текущий метод сборки UInt8Array для отправки на saveFile включает повторное создание нового UInt8Array на всех ~ 2600 загруженных чанках, что очень медленно и неэффективно. Я попытался поместить все эти куски в массив и объединить их впоследствии, но я не могу понять, как взять массив из ~ 2600 UInt8Arrays и сгладить их до одного UInt8Array. У кого-нибудь есть совет для меня?

1 Ответ

1 голос
/ 21 апреля 2020

переосмыслил это. В итоге мы создали двоичный объект прямо из массива UInt8Array s.

async function start(event) {

  console.log("Starting IPFS...");
  node = await Ipfs.create();

  for await (const file of node.get('QmQxBX5ZKRY8k6W2UqYTMxhdFTvkmNw8X7GJN3t5UiyBpe')) {
    console.log("Starting");
    var content = [];
    for await (const chunk of file.content) {
      console.log("Gathering");
      content.push(chunk);
    }
    console.log("Assembling");
    saveFile(content, "archlinux-2020.04.01-x86_64.iso");
    console.log("Done");
  };

}

// https://stackoverflow.com/a/36899900/2700296
function saveFile(data, fileName) {
  if (data !== null && navigator.msSaveBlob) {
    return navigator.msSaveBlob(new Blob(data, { "type": "application/octet-stream" }), fileName);
  }
  var a = document.createElement('a');
  a.style.display = "none";
  var url = window.URL.createObjectURL(new Blob(data, {type: "application/octet-stream"}));
  a.setAttribute("href", url);
  a.setAttribute("download", fileName);
  document.body.appendChild(a);
  a.click();
  window.URL.revokeObjectURL(url);
  a.remove();
}
...