« get me outta code hell

upd8: file size preloader prefix - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/file-size-preloader.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-01-23 08:47:35 -0400
committer(quasar) nebula <qznebula@protonmail.com>2025-01-23 08:47:35 -0400
commit56a68bdb87189870d69e8a658b5804f41264f732 (patch)
tree78be2e0ae581d45b332ff3852a1d30007b150af5 /src/file-size-preloader.js
parent2f1b3e6dac14e26af7f2f184198d497990aef7a4 (diff)
upd8: file size preloader prefix
Diffstat (limited to 'src/file-size-preloader.js')
-rw-r--r--src/file-size-preloader.js26
1 files changed, 21 insertions, 5 deletions
diff --git a/src/file-size-preloader.js b/src/file-size-preloader.js
index a5db1cb0..5eff243a 100644
--- a/src/file-size-preloader.js
+++ b/src/file-size-preloader.js
@@ -18,6 +18,7 @@
 // are very, very fast.
 
 import {stat} from 'node:fs/promises';
+import {relative, resolve} from 'node:path';
 
 import {logWarn} from '#cli';
 import {transposeArrays} from '#sugar';
@@ -32,6 +33,10 @@ export default class FileSizePreloader {
 
   hadErrored = false;
 
+  constructor({prefix = ''} = {}) {
+    this.prefix = prefix;
+  }
+
   loadPaths(...paths) {
     this.#paths.push(...paths.filter((p) => !this.#paths.includes(p)));
     return this.#startLoadingPaths();
@@ -46,9 +51,9 @@ export default class FileSizePreloader {
       return this.#loadingPromise;
     }
 
-    this.#loadingPromise = new Promise((resolve) => {
-      this.#resolveLoadingPromise = resolve;
-    });
+    ({promise: this.#loadingPromise,
+      resolve: this.#resolveLoadingPromise} =
+        Promise.withResolvers());
 
     this.#loadNextPath();
 
@@ -97,6 +102,14 @@ export default class FileSizePreloader {
   }
 
   getSizeOfPath(path) {
+    let size = this.#getSizeOfPath(path);
+    if (size || !this.prefix) return size;
+    const path2 = resolve(this.prefix, path);
+    if (path2 === path) return null;
+    return this.#getSizeOfPath(path2);
+  }
+
+  #getSizeOfPath(path) {
     const index = this.#paths.indexOf(path);
     if (index === -1) return null;
     if (index > this.#loadedPathIndex) return null;
@@ -106,7 +119,9 @@ export default class FileSizePreloader {
   saveAsCache() {
     const entries =
       transposeArrays([
-        this.#paths.slice(0, this.#loadedPathIndex),
+        this.#paths.slice(0, this.#loadedPathIndex)
+          .map(path => relative(this.prefix, path)),
+
         this.#sizes.slice(0, this.#loadedPathIndex),
       ]);
 
@@ -118,7 +133,8 @@ export default class FileSizePreloader {
       Object.entries(cache)
         .filter(([p]) => !this.#paths.includes(p));
 
-    const [newPaths, newSizes] = transposeArrays(entries);
+    let [newPaths, newSizes] = transposeArrays(entries);
+    newPaths = newPaths.map(p => resolve(this.prefix, p));
 
     this.#paths.splice(this.#loadedPathIndex + 1, 0, ...newPaths);
     this.#sizes.splice(this.#loadedPathIndex + 1, 0, ...newSizes);