Skip to content

Commit

Permalink
[ADD] libzim integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabhg71 committed Dec 7, 2023
1 parent 54562fd commit 9fbbdb7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
9 changes: 8 additions & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,15 @@ <h3 data-i18n="configure-expert-settings-title">Expert settings</h3>
</div>
<div class="checkbox" id="">
<label title="Libzim wasm mode">
<input type="checkbox" name="" id="libzimWasmModeToggle">
<span><b>Libzim Wasm mode</b> (<i>Uses unstable wasm api</i>)</span>
<select id="libzimModeSelect">
<option value="default">Default</option>
<option value="wasm">WASM</option>
<option value="asm">ASM</option>
<option value="wasm-dev">WASM Debug</option>
<option value="asm-dev">ASM Debug</option>
</select>
<button type="button" id="testButtonToBeRemoved">test button</button>
</label>
</div>
<p data-i18n="configure-expert-resetapp-description" class="pt-2">Reset the app to default settings and erase all caches:</p>
Expand Down
73 changes: 69 additions & 4 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import uiUtil from './lib/uiUtil.js';
import settingsStore from './lib/settingsStore.js';
import abstractFilesystemAccess from './lib/abstractFilesystemAccess.js';
import translateUI from './lib/translateUI.js';
import { type } from 'jquery';

if (params.abort) {
// If the app was loaded only to pass a message from the remote code, then we exit immediately
Expand All @@ -57,7 +58,7 @@ const ASSETS_CACHE = 'kiwixjs-assetsCache';
var appstate = {};

/**
* @type ZIMArchive
* @type ZIMArchive | null
*/
var selectedArchive = null;

Expand Down Expand Up @@ -475,8 +476,20 @@ document.getElementById('bypassAppCacheCheck').addEventListener('change', functi
refreshCacheStatus();
});

document.getElementById('libzimWasmModeToggle').addEventListener('change', function () {
settingsStore.setItem('libzimWasmMode', this.checked);
document.getElementById('libzimModeSelect').addEventListener('change', function (e) {
// console.log(settingsStore.getItem('libzimMode'));
settingsStore.setItem('libzimMode', e.target.value);
});

document.getElementById('testButtonToBeRemoved').addEventListener('click', function (e) {
console.log('clicked on libzimModeSelect', selectedArchive);
if (selectedArchive !== null) {
console.log('calling function getDirEntryByPath', selectedArchive.isReady());
// selectedArchive.callLibzimWorker({ action: 'getEntryByPath', path: 'Di' })
selectedArchive.getEntryByPath('Home.html') // ✅
// selectedArchive.search2('Di')
// selectedArchive.getDirEntryByPath('/')
}
});

document.getElementById('disableDragAndDropCheck').addEventListener('change', function () {
Expand Down Expand Up @@ -846,7 +859,8 @@ function initServiceWorkerMessaging () {
// Until we find a way to tell where it is coming from, we allow the request through on all controlled clients and try to load the content
console.warn('>>> Allowing passthrough of SW request to process Zimit video <<<');
}
handleMessageChannelMessage(event);
// handleMessageChannelMessage(event);
handleMessageChannelByWasm(event);
}
} else if (event.data.msg_type) {
// Messages received from the ReplayWorker
Expand Down Expand Up @@ -891,6 +905,57 @@ function initServiceWorkerMessaging () {
}
}

/**
* Function that handles a message of the messageChannel.
* It tries to read the content in the backend, and sends it back to the ServiceWorker
*
* @param {Event} event The event object of the message channel
*/
async function handleMessageChannelByWasm (event) {
// We received a message from the ServiceWorker
// The ServiceWorker asks for some content
const title = event.data.title;
const messagePort = event.ports[0];
const ret = await selectedArchive.getEntryDirByWasm(title);
if (ret === null) {
console.error('Title ' + title + ' not found in archive.');
messagePort.postMessage({ action: 'giveContent', title: title, content: '' });
return
}

// // Let's send the content to the ServiceWorker
var message = { action: 'giveContent', title: title, content: ret.content, mimetype: ret.mimetype };
// messagePort.postMessage(message, [ret.content]);
messagePort.postMessage(message);

// var readFile = function (dirEntry) {
// if (dirEntry === null) {
// console.error('Title ' + title + ' not found in archive.');
// messagePort.postMessage({ action: 'giveContent', title: title, content: '' });
// } else if (dirEntry.isRedirect()) {
// selectedArchive.resolveRedirect(dirEntry, function (resolvedDirEntry) {
// var redirectURL = resolvedDirEntry.namespace + '/' + resolvedDirEntry.url;
// // Ask the ServiceWorker to send an HTTP redirect to the browser.
// // We could send the final content directly, but it is necessary to let the browser know in which directory it ends up.
// // Else, if the redirect URL is in a different directory than the original URL,
// // the relative links in the HTML content would fail. See #312
// messagePort.postMessage({ action: 'sendRedirect', title: title, redirectUrl: redirectURL });
// });
// } else {
// // Let's read the content in the ZIM file
// selectedArchive.readBinaryFile(dirEntry, function (fileDirEntry, content) {
// var mimetype = fileDirEntry.getMimetype();
// // Let's send the content to the ServiceWorker
// var message = { action: 'giveContent', title: title, content: content.buffer, mimetype: mimetype };
// messagePort.postMessage(message, [content.buffer]);
// });
// }
// };
// selectedArchive.getDirEntryByPath(title).then(readFile).catch(function () {
// messagePort.postMessage({ action: 'giveContent', title: title, content: new Uint8Array() });
// });
}

/**
* Sets the given injection mode.
* This involves registering (or re-enabling) the Service Worker if necessary
Expand Down
6 changes: 3 additions & 3 deletions www/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* @property {string} cacheIDB - Name of the Indexed DB database
* @property {boolean} isFileSystemApiSupported - A boolean indicating whether the FileSystem API is supported.
* @property {boolean} isWebkitDirApiSupported - A boolean indicating whether the Webkit Directory API is supported.
* @property {boolean} libzimWasmMode - A boolean indicating whether the Libzim wasm mode is turned on.
* @property {"wasm-dev" | "wasm" | "asm" | "asm-dev" | "default"} libzimMode - A value indicating which libzim mode is selected.
* @property {DecompressorAPI} decompressorAPI
/**
Expand Down Expand Up @@ -124,7 +124,7 @@ params['cacheAPI'] = 'kiwix-js'; // Sets name of the prefix used to identify the
params['cacheIDB'] = 'kiwix-zim'; // Sets name of the Indexed DB database
params['isFileSystemApiSupported'] = typeof window.showOpenFilePicker === 'function'; // Sets a boolean indicating whether the FileSystem API is supported
params['isWebkitDirApiSupported'] = 'webkitdirectory' in document.createElement('input'); // Sets a boolean indicating whether the Webkit Directory API is supported
params['libzimWasmMode'] = getSetting('libzimWasmMode'); // Sets a boolean indicating whether the libzim WASM mode is turned on
params['libzimMode'] = getSetting('libzimMode') ?? 'default'; // Sets a value indicating which libzim mode is selected

/**
* Apply any override parameters that might be in the querystring.
Expand Down Expand Up @@ -187,7 +187,7 @@ document.getElementById('useHomeKeyToFocusSearchBarCheck').checked = params.useH
document.getElementById('openExternalLinksInNewTabsCheck').checked = params.openExternalLinksInNewTabs;
document.getElementById('languageSelector').value = params.overrideBrowserLanguage || 'default';
document.getElementById('bypassAppCacheCheck').checked = !params.appCache;
document.getElementById('libzimWasmModeToggle').checked = params.libzimWasmMode;
document.getElementById('libzimModeSelect').value = params.libzimMode;
document.getElementById('appVersion').textContent = 'Kiwix ' + params.appVersion;

// This is a simplified version of code in settingsStore, because that module is not available in init.js
Expand Down
10 changes: 10 additions & 0 deletions www/js/lib/zimArchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,16 @@ ZIMArchive.prototype.findDirEntriesFromFullTextSearch = function (search, dirEnt
});
};

ZIMArchive.prototype.getEntryDirByWasm = async function (path) {
// this.file.mainPage
// const cns = this.getContentNamespace()
// path = cns + '/' + path;
const ret = await this.callLibzimWorker({ action: 'getEntryByPath', path: path });
// const r = await this.getDirEntryByPath(path)
// console.log(path, ret, r);
return ret;
}

/**
* Calls the libzim Web Worker with the given parameters, and returns a Promise with its response
*
Expand Down

0 comments on commit 9fbbdb7

Please sign in to comment.