Skip to content

Commit

Permalink
Add splitting without server side
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixelsuft authored and copy committed Oct 20, 2021
1 parent 2034526 commit 271c8a9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
56 changes: 44 additions & 12 deletions src/browser/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/(.*)(\..*)/);

Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/browser/starter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 271c8a9

Please sign in to comment.