dsh-hashline-edit 0.4.2 → 0.5.0

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.
@@ -1,7 +1,9 @@
1
1
  /**
2
2
  * Lazy, hash-pinned grammar cache.
3
3
  *
4
- * WHY fetch on first use instead of bundling: the full grammar set measured megabytes against a
4
+ * WHY bundled first and fetched only as a fallback: ADR-0026 ships all seven grammars inside the
5
+ * package (`grammars/`), so a first read never waits on — and never needs — the network. Bytes are
6
+ * digest-verified whichever source they come from; the committed manifest stays the trust anchor.
5
7
  * small extension bundle, and most sessions never touch most languages. The committed manifest is
6
8
  * the trust anchor: bytes are cached only after their SHA-256 matches, and a cache entry whose
7
9
  * digest no longer matches is deleted rather than parsed.
@@ -14,6 +16,7 @@ import { createHash } from "node:crypto";
14
16
  import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
15
17
  import { dirname, join } from "node:path";
16
18
  import { stateRoot } from "../config-dir.js";
19
+ import { readBundledAsset } from "../bundled-assets.js";
17
20
  import {
18
21
  GRAMMAR_MANIFEST,
19
22
  type GrammarManifest,
@@ -179,6 +182,13 @@ export async function requestGrammar(
179
182
  const path = grammarCachePath(language, entry, options.cacheRoot);
180
183
  const cached = readVerifiedCache(path, entry.sha256);
181
184
  if (cached !== undefined) return { status: "ready", artifact: { language, path, bytes: cached } };
185
+ // WHY: bundled before the network — the shipped grammars are the common path now, so a first
186
+ // WHY: read never waits on a fetch. A miss falls through to the lazy download below.
187
+ const bundled = bundledGrammarBytes(language, entry);
188
+ if (bundled !== undefined) {
189
+ writeCache(path, bundled);
190
+ return { status: "ready", artifact: { language, path, bytes: bundled } };
191
+ }
182
192
 
183
193
  let pending = inFlight.get(path);
184
194
  if (pending === undefined) {
@@ -207,3 +217,25 @@ export async function ensureGrammar(
207
217
  const result = await requestGrammar(language, options);
208
218
  return result.status === "ready" ? result.artifact : undefined;
209
219
  }
220
+
221
+ /**
222
+ * Verified bytes taken from this package's own `grammars/` directory, or undefined.
223
+ *
224
+ * WHY bundle AND keep the fetch: the shipped artifact must work offline and instantly, but a
225
+ * language can enter the manifest before it enters the tarball, so the download stays as the
226
+ * fallback for that window.
227
+ * @param language - manifest key.
228
+ * @param entry - the pinned version and digest for that key.
229
+ * @returns the verified bytes, or undefined when the asset is absent or no longer matches the pin.
230
+ */
231
+ function bundledGrammarBytes(
232
+ language: string,
233
+ entry: GrammarManifestEntry,
234
+ ): Uint8Array | undefined {
235
+ const name = `${language}-${entry.version}-${entry.sha256.slice(0, 16)}.wasm`;
236
+ const bytes = readBundledAsset(`grammars/${name}`);
237
+ if (bytes === undefined) return undefined;
238
+ // WHY: bundled bytes are verified too — a truncated download, a bad publish or a hand-edited
239
+ // WHY: file in the tarball must never reach the parser, exactly as for a fetched grammar.
240
+ return sha256Hex(bytes) === entry.sha256 ? bytes : undefined;
241
+ }
@@ -6,7 +6,9 @@
6
6
  * cache file, so a manifest bump cannot keep serving the previous grammar.
7
7
  *
8
8
  * WHY nothing here is imported as a module: the runtime (`web-tree-sitter`) is the only dependency;
9
- * grammar `.wasm` files are fetched on first use and are never part of the published package.
9
+ * WHY nothing here is imported as a module: the runtime (`web-tree-sitter`) is the only dependency;
10
+ * the pinned `.wasm` files ship inside this package under `grammars/` (ADR-0026) and are fetched only
11
+ * when that copy is missing, so a first read parses offline and never waits on the network.
10
12
  */
11
13
  export interface GrammarManifestEntry {
12
14
  readonly language: string;
@@ -6,7 +6,7 @@
6
6
  * the wasm host. `hasWebTreeSitterRuntime` is the cheap preflight; grammar bytes are only fetched
7
7
  * after it answered yes, and a resolution failure is the same answer as an offline grammar.
8
8
  *
9
- * WHY the runtime is the only dependency: grammars are fetched bytes, so the package grows only by
9
+ * WHY the runtime is the only DEPENDENCY: grammar bytes ship in this package (`grammars/`, ADR-0026)
10
10
  * the wasm host, never by a grammar set. `Language.load` accepts the verified bytes directly, so no
11
11
  * second disk read is needed after the cache check.
12
12
  */