From 271c8a9c53e0b2ddc22abfb3948c7d5e55ecdbe6 Mon Sep 17 00:00:00 2001 From: pixelsuft Date: Fri, 4 Jun 2021 09:34:08 +0700 Subject: [PATCH] Add splitting without server side --- src/browser/lib.js | 56 +++++++++++++++++++++++++++++++++--------- src/browser/starter.js | 3 ++- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/browser/lib.js b/src/browser/lib.js index 929f6b39..ed4d1d13 100644 --- a/src/browser/lib.js +++ b/src/browser/lib.js @@ -460,7 +460,7 @@ var ASYNC_SAFE = false; * @param {string} filename Name of the file to download * @param {number|undefined} size */ - function AsyncXHRPartfileBuffer(filename, size) + function AsyncXHRPartfileBuffer(filename, size, step) { const parts = filename.match(/(.*)(\..*)/); @@ -478,6 +478,8 @@ var ASYNC_SAFE = false; /** @const */ this.block_size = 256; this.byteLength = size; + this.use_step = typeof step === "number"; + this.step = step; this.loaded_blocks = Object.create(null); @@ -522,18 +524,48 @@ var ASYNC_SAFE = false; } return; } - - const part_filename = this.basename + "-" + offset + "-" + (offset + len) + this.extension; - - v86util.load_file(part_filename, { - done: function done(buffer) + + if(this.use_step) + { + const fake_offset = parseInt(offset / this.step, undefined) * this.step; + const m_offset = offset - fake_offset; + const total_count = parseInt(len / this.step, undefined) + 2; + var blocks = new Uint8Array(m_offset + (total_count * this.step)); + var finished = 0; + + for(var i = 0; i < total_count; i++) { - dbg_assert(buffer.byteLength === len); - var block = new Uint8Array(buffer); - this.handle_read(offset, len, block); - fn(block); - }.bind(this), - }); + const cur = i * this.step; + const part_filename = this.basename + "-" + (cur + fake_offset) + this.extension; + + v86util.load_file(part_filename, { + done: function done(buffer) { + const block = new Uint8Array(buffer); + blocks.set(block, cur); + const tmp_blocks = blocks.slice(m_offset, m_offset + len); + finished++; + if(finished === total_count) + { + fn(tmp_blocks); + } + }.bind(this), + }); + } + } + else + { + const part_filename = this.basename + "-" + offset + "-" + (offset + len) + this.extension; + + v86util.load_file(part_filename, { + done: function done(buffer) + { + dbg_assert(buffer.byteLength === len); + var block = new Uint8Array(buffer); + this.handle_read(offset, len, block); + fn(block); + }.bind(this), + }); + } }; AsyncXHRPartfileBuffer.prototype.set = AsyncXHRBuffer.prototype.set; diff --git a/src/browser/starter.js b/src/browser/starter.js index 095b9954..1d7049a4 100644 --- a/src/browser/starter.js +++ b/src/browser/starter.js @@ -380,6 +380,7 @@ V86Starter.prototype.continue_init = async function(emulator, options) async: file["async"], url: file["url"], size: file["size"], + step: file["step"], use_parts: file.use_parts, }; @@ -439,7 +440,7 @@ V86Starter.prototype.continue_init = async function(emulator, options) if(file.use_parts) { - buffer = new v86util.AsyncXHRPartfileBuffer(file.url, file.size); + buffer = new v86util.AsyncXHRPartfileBuffer(file.url, file.size, file.step); } else {