delta

tcmts.cli.web.delta · #gkunu5xi · term · runs on the server

uses node:path

A chunk compressed against an earlier version of it, as Compression Dictionary Transport's dcb encoding: its 36-byte header (\xffDCB and the dictionary's SHA-256) and brotli with the earlier chunk as its dictionary. Made once per pair (both are named by their content), in deltas; undefined if it's no smaller than plain brotli.

import { createHash } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import zlib from "node:zlib";

export function delta(file: string, dictionary: string, deltas: string): string | undefined {
  const out = path.join(deltas, `${path.basename(dictionary)}--${path.basename(file)}.dcb`);
  if (fs.existsSync(out)) return out;
  const none = `${out}.none`;
  if (fs.existsSync(none)) return undefined;
  const dict = fs.readFileSync(dictionary);
  const data = fs.readFileSync(file);
  const body = zlib.brotliCompressSync(data, {
    dictionary: dict,
    params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 11, [zlib.constants.BROTLI_PARAM_LGWIN]: 24 },
  } as zlib.BrotliOptions);
  const packed = Buffer.concat([
    Buffer.from([0xff, 0x44, 0x43, 0x42]),
    createHash("sha256").update(dict).digest(),
    body,
  ]);
  fs.mkdirSync(deltas, { recursive: true });
  const plain = fs.existsSync(`${file}.br`) ? fs.statSync(`${file}.br`).size : data.length;
  // (written beside, then moved: two requests at once write it once each, and neither reads half of one)
  const part = `${packed.length < plain ? out : none}.${process.pid}`;
  fs.writeFileSync(part, packed.length < plain ? packed : "");
  fs.renameSync(part, packed.length < plain ? out : none);
  return packed.length < plain ? out : undefined;
}

used by