@youtyan/code-viewer 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@youtyan/code-viewer",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Local browser-based git diff viewer",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,13 @@
1
+ // Short enough that a browser reload self-heals stale git data, while still
2
+ // coalescing bursts from one render pass.
3
+ export const CACHE_TTL_MS = 1500;
4
+
5
+ export type TimedCacheEntry<T> = T & { storedAt: number };
6
+
7
+ export function cacheFresh<T>(
8
+ cached: TimedCacheEntry<T> | undefined,
9
+ now = Date.now(),
10
+ ttlMs = CACHE_TTL_MS,
11
+ ): cached is TimedCacheEntry<T> {
12
+ return !!cached && now - cached.storedAt <= ttlMs;
13
+ }
@@ -4,6 +4,7 @@ import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
4
4
  import { basename, extname, join, normalize, relative } from 'node:path';
5
5
  import { APP_ENTRY_PATHS, SPA_PATHS } from '../routes';
6
6
  import type { DiffMeta, FileDiffResponse, FileMeta, FileRangeResponse, RepoTreeResponse } from '../types';
7
+ import { cacheFresh, type TimedCacheEntry } from './cache';
7
8
  import * as git from './git';
8
9
  import { isSameWorktreeRange } from './range';
9
10
 
@@ -24,8 +25,8 @@ let listenPort = 0;
24
25
 
25
26
  const enc = new TextEncoder();
26
27
  const sseClients = new Set<ReadableStreamDefaultController<Uint8Array>>();
27
- const fileCache = new Map<string, string>();
28
- const metaCache = new Map<string, { body: string; sig: string }>();
28
+ const fileCache = new Map<string, TimedCacheEntry<{ diffText: string }>>();
29
+ const metaCache = new Map<string, TimedCacheEntry<{ body: string; sig: string }>>();
29
30
 
30
31
  function parseCli() {
31
32
  const rest: string[] = [];
@@ -242,14 +243,14 @@ function handleDiffJson(url: URL) {
242
243
  fileCache.clear();
243
244
  }
244
245
  const body = JSON.stringify(payload);
245
- metaCache.set(key, { body, sig });
246
+ metaCache.set(key, { body, sig, storedAt: Date.now() });
246
247
  return new Response(body, { headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' } });
247
248
  }
248
249
  const cached = metaCache.get(key);
249
- if (cached) return new Response(cached.body, { headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' } });
250
+ if (cacheFresh(cached)) return new Response(cached.body, { headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' } });
250
251
  const payload = computePayload(extras, range);
251
252
  const body = JSON.stringify(payload);
252
- metaCache.set(key, { body, sig: JSON.stringify({ ...payload, generation: undefined }) });
253
+ metaCache.set(key, { body, sig: JSON.stringify({ ...payload, generation: undefined }), storedAt: Date.now() });
253
254
  return new Response(body, { headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' } });
254
255
  }
255
256
 
@@ -343,9 +344,12 @@ function handleFileDiff(url: URL) {
343
344
  const cacheKey = isUntracked
344
345
  ? `u\0${path}\0${extras.join('\0')}`
345
346
  : `t\0${path}\0${oldPath || ''}\0${[...extras, ...args].join('\0')}`;
346
- let diffText = fileCache.get(cacheKey);
347
+ const cached = fileCache.get(cacheKey);
348
+ let diffText: string;
347
349
  let errText = '';
348
- if (!diffText) {
350
+ if (cacheFresh(cached)) {
351
+ diffText = cached.diffText;
352
+ } else {
349
353
  if (isUntracked) {
350
354
  diffText = git.untrackedFileDiff(extras, path, cwd).stdout || '';
351
355
  } else {
@@ -353,7 +357,7 @@ function handleFileDiff(url: URL) {
353
357
  diffText = res.stdout || '';
354
358
  if (res.code !== 0) errText = res.stderr;
355
359
  }
356
- fileCache.set(cacheKey, diffText);
360
+ fileCache.set(cacheKey, { diffText, storedAt: Date.now() });
357
361
  }
358
362
  const mode = url.searchParams.get('mode') || 'full';
359
363
  const truncated = mode === 'preview'