All files cache.ts

100% Statements 173/173
100% Branches 92/92
100% Functions 40/40
100% Lines 151/151

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304                                      59x           510x 510x 510x 546x   510x 29x 29x   514x 510x   477x 477x     33x 33x               287x 4x 283x 6x 277x 3x 274x 819x                   287x 287x 287x   287x         807x 807x 807x       623x       598x 598x 598x 581x       521x 521x 299x 299x   521x       2177x       316x 316x       125x       493x 433x 433x 433x 400x 400x 393x   13x     432x 432x 432x       292x 292x 292x 291x 291x 269x 269x 264x   264x 232x               19x 17x             249x 249x 249x 249x       12x   248x 248x   247x 247x   292x     292x 292x   292x 292x       309x 309x 22x 17x 2x 2x   15x 13x 13x 13x 13x   13x   15x           56x 56x 56x 56x 56x       69x 69x 69x 69x 69x     69x         287x   332x 332x 320x 320x 309x             206x 206x 206x 199x         35x 35x 35x 35x 33x 31x 31x 5x 5x           35x 35x     25x 25x 25x 23x 15x   25x 25x     25x 25x 25x 23x 16x 16x 5x     25x 25x     52x 46x 46x 46x 46x   46x 3x 3x   46x   52x        
import type { Entry, StorageBackend, Stored } from "./storage";
import type { CacheOptions, ResourceCache } from "./types";
 
interface Job<T> {
  promise: Promise<T>;
  started: boolean;
  force: boolean;
}
 
interface State<T> {
  controller: AbortController;
  entry?: Entry<T>;
  // A scope token distinguishes inherited clear barriers from later per-key cleanup.
  blocked: boolean | object;
  job?: Job<T>;
  queued?: Promise<T>;
}
 
function abortError() {
  return new DOMException("Cache operation aborted", "AbortError");
}
 
// Rejection handlers stay attached even after cancellation: a host may ignore
// AbortSignal and settle its request much later.
function wait<T>(promise: Promise<T>, lifetime: AbortSignal, caller?: AbortSignal): Promise<T> {
  const signals = caller ? [lifetime, caller] : [lifetime];
  return new Promise<T>((resolve, reject) => {
    const cleanup = () => {
      for (const signal of signals) signal.removeEventListener("abort", abort);
    };
    const abort = () => {
      cleanup();
      reject(abortError());
    };
    for (const signal of signals) signal.addEventListener("abort", abort, { once: true });
    promise.then(
      (value) => {
        cleanup();
        resolve(value);
      },
      (error) => {
        cleanup();
        reject(error);
      },
    );
  });
}
 
/** @internal */
export function validateOptions<T, Q>(options: CacheOptions<T, Q>): void {
  if (typeof options.namespace !== "string" || !options.namespace.trim())
    throw new TypeError("namespace must be nonempty");
  if (!Number.isSafeInteger(options.schemaVersion) || options.schemaVersion <= 0)
    throw new TypeError("schemaVersion must be a positive safe integer");
  if (!Number.isFinite(options.maxAge) || options.maxAge < 0)
    throw new TypeError("maxAge must be finite and nonnegative");
  for (const name of ["key", "decode", "load"] as const) {
    if (typeof options[name] !== "function") throw new TypeError(`${name} must be a function`);
  }
}
 
/** @internal */
export function createCache<T, Q>(
  options: CacheOptions<T, Q>,
  backend: StorageBackend,
  session: boolean,
): ResourceCache<T, Q> {
  const states = new Map<string, State<T>>();
  let disposed = false;
  let scopeBlocked: false | object = false;
  let disposal: Promise<void> | undefined;
  let io: Promise<unknown> = Promise.resolve();
 
  // Queue storage work, not network work. A removal is ordered after writes
  // already submitted, and every later write checks its lifetime inside the queue.
  function storage<R>(action: () => Promise<R>): Promise<R> {
    const result = io.then(action);
    io = result.catch(() => {});
    return result;
  }
 
  function checkOpen() {
    if (disposed) throw new Error("Cache is disposed");
  }
 
  function identity(query: Q): string {
    checkOpen();
    const key = options.key(query);
    if (typeof key !== "string") throw new TypeError("key must return a string");
    return key;
  }
 
  function stateFor(key: string): State<T> {
    let state = states.get(key);
    if (!state) {
      state = { controller: new AbortController(), blocked: scopeBlocked };
      states.set(key, state);
    }
    return state;
  }
 
  function checkState(state: State<T>) {
    if (state.controller.signal.aborted) throw abortError();
  }
 
  function fresh(entry: Entry<T>): boolean {
    const now = Date.now();
    return !entry.invalidated && entry.validatedAt <= now && now - entry.validatedAt < options.maxAge;
  }
 
  function restore(value: Stored, previous?: Entry<T>): Entry<T> {
    return { ...value.meta, data: value.body ? options.decode(value.body.data) : (previous as Entry<T>).data };
  }
 
  async function read(key: string, state: State<T>): Promise<Entry<T> | undefined> {
    if (state.entry && fresh(state.entry)) return state.entry;
    const entry = await storage(async () => {
      checkState(state);
      if (state.blocked) return state.entry;
      try {
        const value = await backend.read(key, state.entry?.version);
        return value ? restore(value, state.entry) : state.entry;
      } catch {
        return state.entry;
      }
    });
    checkState(state);
    state.entry = entry;
    return entry;
  }
 
  function schedule(key: string, query: Q, state: State<T>, force: boolean): Job<T> {
    const job = { started: false, force } as Job<T>;
    job.promise = (async () => {
      const previous = await read(key, state);
      checkState(state);
      if (!job.force && previous && fresh(previous)) return previous.data;
      job.started = true;
      const result = await options.load(query, { etag: previous?.etag ?? null, signal: state.controller.signal });
      checkState(state);
      let entry: Entry<T>;
      if (result.modified) {
        entry = {
          data: result.data,
          etag: result.etag,
          version: crypto.getRandomValues(new Uint32Array(4)).join("-"),
          validatedAt: Date.now(),
          invalidated: false,
        };
      } else {
        if (!previous) throw new Error("Load protocol error: unmodified response without cached data");
        entry = {
          ...previous,
          etag: result.etag === undefined ? previous.etag : result.etag,
          validatedAt: Date.now(),
          invalidated: false,
        };
      }
      await storage(async () => {
        checkState(state);
        try {
          if (await backend.write(key, entry, result.modified)) state.blocked = false;
        } catch {
          // Retain the successful in-memory version. Old storage must not replace
          // a response that could not be persisted, or bypass failed cleanup.
          state.blocked = true;
        }
        checkState(state);
        state.entry = entry;
      });
      checkState(state);
      return entry.data;
    })();
    state.job = job;
    // Registered before queued continuations, so the slot is released before
    // any next round starts, on either outcome.
    const release = () => {
      state.job = undefined;
    };
    job.promise.then(release, release);
    return job;
  }
 
  function refresh(key: string, query: Q, state: State<T>, force: boolean): Promise<T> {
    const active = state.job;
    if (!active) return schedule(key, query, state, force).promise;
    if (!force) return active.promise;
    if (!active.started) {
      active.force = true;
      return active.promise;
    }
    if (!state.queued) {
      const start = () => {
        checkState(state);
        state.queued = undefined;
        return schedule(key, query, state, true).promise;
      };
      state.queued = active.promise.then(start, start);
    }
    return state.queued;
  }
 
  // Publish the new lifetime first. Notify host abort listeners only after the
  // operation has queued its cleanup; listeners may synchronously call back in.
  function reset(key: string, keep: boolean) {
    const old = states.get(key);
    const state: State<T> = { controller: new AbortController(), blocked: true };
    if (keep && old?.entry) state.entry = { ...old.entry, invalidated: true };
    states.set(key, state);
    return { state, cancel: () => old?.controller.abort() };
  }
 
  function resetAll() {
    const old = [...states.values()];
    states.clear();
    const barrier = {};
    scopeBlocked = barrier;
    return {
      barrier,
      cancel() {
        for (const state of old) state.controller.abort();
      },
    };
  }
 
  return {
    async get(query, getOptions = {}) {
      const key = identity(query);
      if (getOptions.signal?.aborted) throw abortError();
      const state = stateFor(key);
      if (!getOptions.revalidate && state.entry && fresh(state.entry)) return state.entry.data;
      return wait(
        refresh(key, query, state, getOptions.revalidate === true),
        state.controller.signal,
        getOptions.signal,
      );
    },
    async peek(query) {
      const key = identity(query);
      const state = stateFor(key);
      const entry = await wait(read(key, state), state.controller.signal);
      return entry
        ? { data: entry.data, etag: entry.etag, validatedAt: entry.validatedAt, fresh: fresh(entry) }
        : undefined;
    },
    async invalidate(query) {
      const key = identity(query);
      const canRestore = !(states.get(key)?.blocked ?? scopeBlocked);
      const { state, cancel } = reset(key, true);
      const cleanup = storage(async () => {
        const value = await backend.invalidate(key);
        if (canRestore) state.blocked = false;
        if (canRestore && !state.controller.signal.aborted && !state.entry && value) {
          try {
            state.entry = restore(value);
          } catch {
            /* Invalid stored bodies remain misses. */
          }
        }
      });
      cancel();
      await cleanup;
    },
    async remove(query) {
      const key = identity(query);
      const { state, cancel } = reset(key, false);
      const cleanup = storage(async () => {
        await backend.remove(key);
        state.blocked = false;
      });
      cancel();
      await cleanup;
    },
    async clear() {
      checkOpen();
      const { barrier, cancel } = resetAll();
      const cleanup = storage(async () => {
        await backend.clear(false);
        if (scopeBlocked === barrier) scopeBlocked = false;
        for (const state of states.values()) {
          if (state.blocked === barrier) state.blocked = false;
        }
      });
      cancel();
      await cleanup;
    },
    dispose() {
      if (!disposal) {
        disposed = true;
        const { cancel } = resetAll();
        disposal = storage(async () => {
          if (session) await backend.clear(true);
        });
        disposal = disposal.catch((error) => {
          disposal = undefined;
          throw error;
        });
        cancel();
      }
      return disposal;
    },
  };
}