« get me outta code hell

search, client: use json-compress and msgpackr for search indexes - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static/js/search-worker.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-13 14:03:03 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:53 -0300
commit30914eb56468b388e4b5cb2090292c5932171eb3 (patch)
tree6419b871ac9b8523a97b5d171e4e3cfa3c694d42 /src/static/js/search-worker.js
parent3b9f95a125d28b8cda3ebca1fe248e2026d2e56d (diff)
search, client: use json-compress and msgpackr for search indexes
Diffstat (limited to 'src/static/js/search-worker.js')
-rw-r--r--src/static/js/search-worker.js44
1 files changed, 33 insertions, 11 deletions
diff --git a/src/static/js/search-worker.js b/src/static/js/search-worker.js
index c3975380..b8ab4a63 100644
--- a/src/static/js/search-worker.js
+++ b/src/static/js/search-worker.js
@@ -1,8 +1,14 @@
+import FlexSearch from '../lib/flexsearch/flexsearch.bundle.module.min.js';
+
 import {makeSearchIndex, searchSpec} from '../shared-util/search-spec.js';
 import {empty, groupArray, stitchArrays, unique, withEntries}
   from '../shared-util/sugar.js';
 
-import FlexSearch from '../lib/flexsearch/flexsearch.bundle.module.min.js';
+import {loadDependency} from './module-import-shims.js';
+
+// Will be loaded from dependencies.
+let decompress;
+let unpack;
 
 let status = null;
 let indexes = null;
@@ -10,14 +16,27 @@ let indexes = null;
 onmessage = handleWindowMessage;
 postStatus('alive');
 
-main().then(
-  () => {
-    postStatus('ready');
-  },
-  error => {
-    console.error(`Search worker setup error:`, error);
-    postStatus('setup-error');
-  });
+loadDependencies()
+  .then(main)
+  .then(
+    () => {
+      postStatus('ready');
+    },
+    error => {
+      console.error(`Search worker setup error:`, error);
+      postStatus('setup-error');
+    });
+
+async function loadDependencies() {
+  const {compressJSON} =
+    await loadDependency.fromWindow('../lib/compress-json/bundle.min.js');
+
+  const msgpackr =
+    await loadDependency.fromModuleExports('../lib/msgpackr/index.js');
+
+  ({decompress} = compressJSON);
+  ({unpack} = msgpackr);
+}
 
 function rebase(path) {
   return `/search-data/` + path;
@@ -38,8 +57,11 @@ async function main() {
   await Promise.all(
     Object.entries(indexData)
       .map(([key, _info]) =>
-        fetch(rebase(key + '.json'))
-          .then(res => res.json())
+        fetch(rebase(key + '.json.msgpack'))
+          .then(res => res.arrayBuffer())
+          .then(buffer => new Uint8Array(buffer))
+          .then(data => unpack(data))
+          .then(data => decompress(data))
           .then(data => {
             importIndex(key, data);
           })));