From 56b8c85e3fffb58695f4cd5f4526d40c14afc0f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=A6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BA=D0=BE=D0=B2?= <49730476+DimaThenekov@users.noreply.github.com> Date: Sat, 15 Nov 2025 19:56:24 +0300 Subject: [PATCH] Adding zstd_decompress to the filestore --- lib/filesystem.js | 4 ++-- src/browser/filestorage.js | 22 ++++++++++++++++------ src/browser/starter.js | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 6d9262aa..24b5e160 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -1068,7 +1068,7 @@ FS.prototype.get_buffer = async function(idx) else if(inode.status === STATUS_ON_STORAGE) { dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum"); - return await this.storage.read(inode.sha256sum, 0, inode.size); + return await this.storage.read(inode.sha256sum, 0, inode.size, inode.size); } else { @@ -1095,7 +1095,7 @@ FS.prototype.get_data = async function(idx, offset, count) else if(inode.status === STATUS_ON_STORAGE) { dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum"); - return await this.storage.read(inode.sha256sum, offset, count); + return await this.storage.read(inode.sha256sum, offset, count, inode.size); } else { diff --git a/src/browser/filestorage.js b/src/browser/filestorage.js index c84c7c11..7ce76c80 100644 --- a/src/browser/filestorage.js +++ b/src/browser/filestorage.js @@ -9,9 +9,10 @@ export function FileStorageInterface() {} * @param {string} sha256sum * @param {number} offset * @param {number} count + * @param {number} file_size * @return {!Promise} null if file does not exist. */ -FileStorageInterface.prototype.read = function(sha256sum, offset, count) {}; +FileStorageInterface.prototype.read = function(sha256sum, offset, count, file_size) {}; /** * Add a read-only file to the filestorage. @@ -83,8 +84,9 @@ MemoryFileStorage.prototype.uncache = function(sha256sum) * @implements {FileStorageInterface} * @param {FileStorageInterface} file_storage * @param {string} baseurl + * @param {function(number,Uint8Array):ArrayBuffer} zstd_decompress */ -export function ServerFileStorageWrapper(file_storage, baseurl) +export function ServerFileStorageWrapper(file_storage, baseurl, zstd_decompress) { dbg_assert(baseurl, "ServerMemoryFileStorage: baseurl should not be empty"); @@ -95,19 +97,26 @@ export function ServerFileStorageWrapper(file_storage, baseurl) this.storage = file_storage; this.baseurl = baseurl; + this.zstd_decompress = zstd_decompress; } /** * @param {string} sha256sum + * @param {number} file_size * @return {!Promise} */ -ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum) +ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum, file_size) { return new Promise((resolve, reject) => { load_file(this.baseurl + sha256sum, { done: async buffer => { - const data = new Uint8Array(buffer); + let data = new Uint8Array(buffer); + if (sha256sum.endsWith('.zst')) { + data = new Uint8Array( + this.zstd_decompress(file_size, data) + ); + } await this.cache(sha256sum, data); resolve(data); }}); @@ -118,14 +127,15 @@ ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum) * @param {string} sha256sum * @param {number} offset * @param {number} count + * @param {number} file_size * @return {!Promise} */ -ServerFileStorageWrapper.prototype.read = async function(sha256sum, offset, count) +ServerFileStorageWrapper.prototype.read = async function(sha256sum, offset, count, file_size) { const data = await this.storage.read(sha256sum, offset, count); if(!data) { - const full_file = await this.load_from_server(sha256sum); + const full_file = await this.load_from_server(sha256sum, file_size); return full_file.subarray(offset, offset + count); } return data; diff --git a/src/browser/starter.js b/src/browser/starter.js index 87b66859..103f2199 100644 --- a/src/browser/starter.js +++ b/src/browser/starter.js @@ -436,7 +436,7 @@ V86.prototype.continue_init = async function(emulator, options) if(base_url) { - file_storage = new ServerFileStorageWrapper(file_storage, base_url); + file_storage = new ServerFileStorageWrapper(file_storage, base_url, this.zstd_decompress); } settings.fs9p = this.fs9p = new FS(file_storage);