Skip to content

Commit

Permalink
Improved file loading in nodejs, simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
copy committed May 10, 2017
1 parent 61d3d11 commit aaa75be
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 69 deletions.
177 changes: 122 additions & 55 deletions src/browser/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ var ASYNC_SAFE = false;
}
}

if(options.range)
{
let start = options.range.start;
let end = start + options.range.length - 1;
http.setRequestHeader("Range", "bytes=" + start + "-" + end);
}

http.onload = function(e)
{
if(http.readyState === 4)
Expand Down Expand Up @@ -72,28 +79,107 @@ var ASYNC_SAFE = false;

function load_file_nodejs(filename, options)
{
var o = {
encoding: options.as_text ? "utf-8" : null,
};
let fs = require("fs");

require("fs")["readFile"](filename, o, function(err, data)
if(options.range)
{
if(err)
{
console.log("Could not read file:", filename);
}
else
dbg_assert(!options.as_text);

fs["open"](filename, "r", (err, fd) =>
{
var result = data;
if(err) throw err;

if(!options.as_text)
let length = options.range.length;
var buffer = new global["Buffer"](length);

fs["read"](fd, buffer, 0, length, options.range.start, (err, bytes_read) =>
{
if(err) throw err;

dbg_assert(bytes_read === length);
options.done && options.done(new Uint8Array(buffer));

fs["close"](fd, (err) => {
if(err) throw err;
});
});
});
}
else
{
var o = {
encoding: options.as_text ? "utf-8" : null,
};

fs["readFile"](filename, o, function(err, data)
{
if(err)
{
result = new Uint8Array(result).buffer;
console.log("Could not read file:", filename, err);
}
else
{
var result = data;

options.done(result);
}
});
if(!options.as_text)
{
result = new Uint8Array(result).buffer;
}

options.done(result);
}
});
}
}

if(typeof XMLHttpRequest === "undefined")
{
var determine_size = function(path, cb)
{
require("fs")["stat"](path, (err, stats) =>
{
if(err)
{
cb(err);
}
else
{
cb(null, stats.size);
}
});
};
}
else
{
var determine_size = function(url, cb)
{
v86util.load_file(url, {
done: (buffer, http) =>
{
var header = http.getResponseHeader("Content-Range") || "";
var match = header.match(/\/(\d+)\s*$/);

if(match)
{
cb(null, +match[1]);
}
else
{
cb({ header });
}
},
headers: {
Range: "bytes=0-0",

//"Accept-Encoding": "",

// Added by Chromium, but can cause the whole file to be sent
// Settings this to empty also causes problems and Chromium
// doesn't seem to create this header any more
//"If-Range": "",
}
});
};
}

/**
Expand Down Expand Up @@ -128,36 +214,22 @@ var ASYNC_SAFE = false;

// Determine the size using a request

load_file(this.filename, {
done: function done(buffer, http)
determine_size(this.filename, (error, size) =>
{
if(error)
{
var header = http.getResponseHeader("Content-Range") || "";
var match = header.match(/\/(\d+)\s*$/);

if(match)
{
this.byteLength = +match[1]
this.onload && this.onload({});
}
else
{
console.assert(false,
"Cannot use: " + this.filename + ". " +
"`Range: bytes=...` header not supported (Got `" + header + "`)");
}
}.bind(this),
headers: {
Range: "bytes=0-0",

//"Accept-Encoding": "",

// Added by Chromium, but can cause the whole file to be sent
// Settings this to empty also causes problems and Chromium
// doesn't seem to create this header any more
//"If-Range": "",
console.assert(false,
"Cannot use: " + this.filename + ". " +
"`Range: bytes=...` header not supported (Got `" + error.header + "`)");
}
else
{
dbg_assert(size >= 0);
this.byteLength = size;
this.onload && this.onload({});
}
});
}
};

/**
* @param {number} offset
Expand Down Expand Up @@ -220,21 +292,16 @@ var ASYNC_SAFE = false;
return;
}

var range_start = offset;
var range_end = offset + len - 1;

load_file(this.filename, {
v86util.load_file(this.filename, {
done: function done(buffer)
{
var block = new Uint8Array(buffer);
this.handle_read(offset, len, block);
fn(block);
}.bind(this),
headers: {
Range: "bytes=" + range_start + "-" + range_end,
}
range: { start: offset, length: len },
});
}
};

/**
* Relies on this.byteLength, this.loaded_blocks and this.block_size
Expand Down Expand Up @@ -274,7 +341,7 @@ var ASYNC_SAFE = false;
}

fn();
}
};

/**
* @this {AsyncFileBuffer|AsyncXHRBuffer}
Expand Down Expand Up @@ -368,7 +435,7 @@ var ASYNC_SAFE = false;
SyncFileBuffer.prototype.load = function()
{
this.load_next(0);
}
};

/**
* @param {number} start
Expand Down Expand Up @@ -407,7 +474,7 @@ var ASYNC_SAFE = false;
this.file = undefined;
this.onload && this.onload({ buffer: this.buffer });
}
}
};

/**
* @param {number} start
Expand Down Expand Up @@ -449,7 +516,7 @@ var ASYNC_SAFE = false;
this.byteLength = file.size;

/** @const */
this.block_size = 256
this.block_size = 256;
this.loaded_blocks = {};

this.onload = undefined;
Expand Down Expand Up @@ -491,7 +558,7 @@ var ASYNC_SAFE = false;
}.bind(this);

fr.readAsArrayBuffer(this.file.slice(offset, offset + len));
}
};
AsyncFileBuffer.prototype.get_from_cache = AsyncXHRBuffer.prototype.get_from_cache;
AsyncFileBuffer.prototype.set = AsyncXHRBuffer.prototype.set;
AsyncFileBuffer.prototype.handle_read = AsyncXHRBuffer.prototype.handle_read;
Expand Down
23 changes: 9 additions & 14 deletions tests/full/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ var root_path = __dirname + "/../..";

var SCREEN_WIDTH = 80;

function readfile(path)
{
return new Uint8Array(fs.readFileSync(path)).buffer;
}

function get_line(screen, y)
{
return screen.subarray(y * SCREEN_WIDTH, (y + 1) * SCREEN_WIDTH);
Expand Down Expand Up @@ -254,18 +249,18 @@ function run_test(test, done)

if(test.alternative_bios)
{
var bios = readfile(root_path + "/bios/bochs-bios.bin");
var vga_bios = readfile(root_path + "/bios/bochs-vgabios.bin");
var bios = root_path + "/bios/bochs-bios.bin";
var vga_bios = root_path + "/bios/bochs-vgabios.bin";
}
else
{
var bios = readfile(root_path + "/bios/seabios.bin");
var vga_bios = readfile(root_path + "/bios/vgabios.bin");
var bios = root_path + "/bios/seabios.bin";
var vga_bios = root_path + "/bios/vgabios.bin";
}

var settings = {
bios: { buffer: bios },
vga_bios: { buffer: vga_bios },
bios: { url: bios },
vga_bios: { url: vga_bios },
autostart: true,
};

Expand All @@ -275,15 +270,15 @@ function run_test(test, done)
{
if(test.cdrom)
{
settings.cdrom = { buffer: readfile(test.cdrom) };
settings.cdrom = { url: test.cdrom };
}
if(test.fda)
{
settings.fda = { buffer: readfile(test.fda) };
settings.fda = { url: test.fda };
}
if(test.hda)
{
settings.hda = { buffer: readfile(test.hda) };
settings.hda = { url: test.hda };
}
}
catch(e)
Expand Down

0 comments on commit aaa75be

Please sign in to comment.