4
function FileStorageInterface() {}
13
FileStorageInterface.prototype.read = function(sha256sum, offset, count) {};
21
FileStorageInterface.prototype.cache = function(sha256sum, data) {};
28
FileStorageInterface.prototype.uncache = function(sha256sum) {};
34
function MemoryFileStorage()
40
this.filedata = new Map();
49
MemoryFileStorage.prototype.read = async function(sha256sum, offset, count)
51
dbg_assert(sha256sum, "MemoryFileStorage read: sha256sum should be a non-empty string");
52
const data = this.filedata.get(sha256sum);
59
return data.subarray(offset, offset + count);
66
MemoryFileStorage.prototype.cache = async function(sha256sum, data)
68
dbg_assert(sha256sum, "MemoryFileStorage cache: sha256sum should be a non-empty string");
69
this.filedata.set(sha256sum, data);
75
MemoryFileStorage.prototype.uncache = function(sha256sum)
77
this.filedata.delete(sha256sum);
86
function ServerFileStorageWrapper(file_storage, baseurl)
88
dbg_assert(baseurl, "ServerMemoryFileStorage: baseurl should not be empty");
90
this.storage = file_storage;
91
this.baseurl = baseurl;
98
ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum)
100
return new Promise((resolve, reject) =>
102
v86util.load_file(this.baseurl + sha256sum, { done: buffer =>
104
const data = new Uint8Array(buffer);
105
this.cache(sha256sum, data).then(() => resolve(data));
116
ServerFileStorageWrapper.prototype.read = async function(sha256sum, offset, count)
118
const data = await this.storage.read(sha256sum, offset, count);
121
const full_file = await this.load_from_server(sha256sum);
122
return full_file.subarray(offset, offset + count);
131
ServerFileStorageWrapper.prototype.cache = async function(sha256sum, data)
133
return await this.storage.cache(sha256sum, data);
139
ServerFileStorageWrapper.prototype.uncache = function(sha256sum)
141
this.storage.uncache(sha256sum);
145
if(typeof window !== "undefined")
147
window["MemoryFileStorage"] = MemoryFileStorage;
148
window["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
150
else if(typeof module !== "undefined" && typeof module.exports !== "undefined")
152
module.exports["MemoryFileStorage"] = MemoryFileStorage;
153
module.exports["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
155
else if(typeof importScripts === "function")
158
self["MemoryFileStorage"] = MemoryFileStorage;
159
self["ServerFileStorageWrapper"] = ServerFileStorageWrapper;