@orkestrel/capture 0.0.10 → 0.0.11

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.
@@ -4,7 +4,7 @@ import { dirname, isAbsolute, matchesGlob, relative, resolve, sep } from "node:p
4
4
  import { fileURLToPath, pathToFileURL } from "node:url";
5
5
  import { parseArgs } from "node:util";
6
6
  import { crc32, deflateSync, inflateSync } from "node:zlib";
7
- import { arrayOf, attempt, failure, isArray, isError, isFailure, isFunction, isRecord, isString, isSuccess, parseJSON, preview, success } from "@orkestrel/contract";
7
+ import { arrayOf, attempt, isArray, isCodedError, isError, isFailure, isFunction, isRecord, isSafeInteger, isString, isSuccess, parseJSON, preview, settle } from "@orkestrel/contract";
8
8
  import { createDuplex, executeSync, inspectProcess } from "@orkestrel/process/server";
9
9
  import { computeProgress, computeProgressRun, createProgressPlugin, extractProgressEvents, readFileSnapshot, renderProgress } from "@orkestrel/test/server";
10
10
  import { CAPTURE_LOOKUPS_FILE, CAPTURE_MANIFEST_FILE, CAPTURE_OWNER_PREFIX, CAPTURE_PNG_SIGNATURE, CAPTURE_REGISTRY_FILE, CAPTURE_REQUEST_PREFIX, CAPTURE_VERSION, CaptureError, buildCaptureTags, collectCaptureAncestors, collectCaptureChanges, collectCaptureDuplicates, describeCaptureDeclaration, describeCaptureDestination, describeCaptureError, describeCaptureIdentity, describeCaptureRequest, isCaptureError, isCaptureGeometry, isCaptureLookupJournal, isCaptureManifestDocument, isCapturePath, isCapturePattern, isCaptureReceipt, isCaptureRegistry, isCaptureRequest, isCaptureRun, isCaptureSegment, isCaptureSelector, matchesCaptureMembers, matchesCaptureSelector, readCaptureArtifact, readCaptureDirty, readCaptureReport, requireCaptureUnique, resolveCaptureKey, resolveCaptureOwner, resolveCaptureStatus, resolveLookupKey, validateCaptureReceipt, validateCaptureRun } from "../core/index.js";
@@ -632,7 +632,7 @@ function readCaptureAncestor(path) {
632
632
  lstatSync(path);
633
633
  return path;
634
634
  } catch (error) {
635
- if (!isError(error) || !("code" in error) || error.code !== "ENOENT" || dirname(path) === path) throw error;
635
+ if (!isCodedError(error) || error.code !== "ENOENT" || dirname(path) === path) throw error;
636
636
  return readCaptureAncestor(dirname(path));
637
637
  }
638
638
  }
@@ -1388,7 +1388,7 @@ function scanCaptureChunks(bytes, position, count) {
1388
1388
  return [...head, ...scanCaptureChunks(bytes, last + 12 + bytes.readUInt32BE(last), limit - half)];
1389
1389
  }
1390
1390
  if (!(limit >= 1)) return [];
1391
- if (!Number.isSafeInteger(position) || position < 8 || position + 12 > bytes.length) throw new CaptureError(`Truncated capture PNG: no 12-byte chunk frame fits at offset ${preview(position)} in ${String(bytes.length)} bytes; expected a whole offset from 8 with 12 bytes after it`, { code: "EVIDENCE" });
1391
+ if (!isSafeInteger(position) || position < 8 || position + 12 > bytes.length) throw new CaptureError(`Truncated capture PNG: no 12-byte chunk frame fits at offset ${preview(position)} in ${String(bytes.length)} bytes; expected a whole offset from 8 with 12 bytes after it`, { code: "EVIDENCE" });
1392
1392
  const end = position + 12 + bytes.readUInt32BE(position);
1393
1393
  if (end > bytes.length) throw new CaptureError(`Truncated capture PNG chunk: ${describeCaptureChunk(bytes, position)} ends at byte ${String(end)}; expected it to end within the file's ${String(bytes.length)} bytes`, { code: "EVIDENCE" });
1394
1394
  const stored = bytes.readUInt32BE(end - 4);
@@ -1442,7 +1442,7 @@ function readCaptureImage(file) {
1442
1442
  * ```
1443
1443
  */
1444
1444
  function requireCaptureImageSize(size) {
1445
- if (!Number.isSafeInteger(size) || size < 0 || size > CAPTURE_PNG_LIMITS.bytes) throw new CaptureError(`Capture PNG size is outside the file budget: ${preview(size)} bytes; expected a whole number of bytes from 0 to ${String(CAPTURE_PNG_LIMITS.bytes)}`, { code: "EVIDENCE" });
1445
+ if (!isSafeInteger(size) || size < 0 || size > CAPTURE_PNG_LIMITS.bytes) throw new CaptureError(`Capture PNG size is outside the file budget: ${preview(size)} bytes; expected a whole number of bytes from 0 to ${String(CAPTURE_PNG_LIMITS.bytes)}`, { code: "EVIDENCE" });
1446
1446
  }
1447
1447
  /**
1448
1448
  * Reconstructs one filtered PNG scanline from its filter byte and the row above.
@@ -1521,7 +1521,7 @@ function decodeCapturePicture(bytes) {
1521
1521
  if (width === 0 || height === 0) throw new CaptureError(`Invalid capture PNG dimensions: ${size}; expected a width and a height of at least 1`, { code: "EVIDENCE" });
1522
1522
  const stride = width * channels;
1523
1523
  const length = (stride + 1) * height;
1524
- if (!Number.isSafeInteger(length)) throw new CaptureError(`Invalid capture PNG dimensions: ${size} at ${String(channels)} bytes per pixel; expected scanlines of at most ${String(Number.MAX_SAFE_INTEGER)} bytes in all`, { code: "EVIDENCE" });
1524
+ if (!isSafeInteger(length)) throw new CaptureError(`Invalid capture PNG dimensions: ${size} at ${String(channels)} bytes per pixel; expected scanlines of at most ${String(Number.MAX_SAFE_INTEGER)} bytes in all`, { code: "EVIDENCE" });
1525
1525
  if (width * height > CAPTURE_PNG_LIMITS.pixels) throw new CaptureError(`Capture PNG exceeds the pixel budget: ${size} is ${String(width * height)} pixels; expected at most ${String(CAPTURE_PNG_LIMITS.pixels)} pixels`, { code: "EVIDENCE" });
1526
1526
  if (width > CAPTURE_PNG_LIMITS.side || height > CAPTURE_PNG_LIMITS.side) throw new CaptureError(`Capture PNG exceeds the dimension budget: ${size}; expected at most ${String(CAPTURE_PNG_LIMITS.side)} pixels per side`, { code: "EVIDENCE" });
1527
1527
  const chunks = readCaptureChunks(bytes);
@@ -1609,7 +1609,7 @@ function encodeCaptureChunk(name, data) {
1609
1609
  * ```
1610
1610
  */
1611
1611
  function encodeCapturePicture(picture, compressed = true) {
1612
- if (!Number.isSafeInteger(picture.width) || !Number.isSafeInteger(picture.height) || picture.width <= 0 || picture.height <= 0 || picture.pixels.length !== picture.width * picture.height * 3) throw new CaptureError("Invalid capture picture dimensions", { code: "EVIDENCE" });
1612
+ if (!isSafeInteger(picture.width) || !isSafeInteger(picture.height) || picture.width <= 0 || picture.height <= 0 || picture.pixels.length !== picture.width * picture.height * 3) throw new CaptureError("Invalid capture picture dimensions", { code: "EVIDENCE" });
1613
1613
  const stride = picture.width * 3;
1614
1614
  const raw = Buffer.concat(Array.from({ length: picture.height }, (_, row) => Buffer.concat([Buffer.from([0]), picture.pixels.subarray(row * stride, (row + 1) * stride)])));
1615
1615
  return Buffer.concat([
@@ -1752,7 +1752,7 @@ function shrinkCapturePicture(picture, factor, limit) {
1752
1752
  function composeCapturePictures(pictures, vertical, gap = 6) {
1753
1753
  if (pictures.length === 0) throw new CaptureError("Empty contact-sheet picture selection", { code: "USAGE" });
1754
1754
  const { side, pixels: budget } = CAPTURE_PNG_LIMITS;
1755
- const faults = [...pictures.flatMap((picture, position) => ["width", "height"].flatMap((axis) => Number.isSafeInteger(picture[axis]) && picture[axis] >= 0 && picture[axis] <= side ? [] : [`pictures[${String(position)}].${axis} is ${preview(picture[axis])}`])), ...Number.isSafeInteger(gap) && gap >= 0 && gap <= side ? [] : [`gap is ${preview(gap)}`]];
1755
+ const faults = [...pictures.flatMap((picture, position) => ["width", "height"].flatMap((axis) => isSafeInteger(picture[axis]) && picture[axis] >= 0 && picture[axis] <= side ? [] : [`pictures[${String(position)}].${axis} is ${preview(picture[axis])}`])), ...isSafeInteger(gap) && gap >= 0 && gap <= side ? [] : [`gap is ${preview(gap)}`]];
1756
1756
  const { width, height } = computeCaptureComposite(pictures, vertical, gap);
1757
1757
  if (faults.length > 0 || !matchesCaptureLimits({
1758
1758
  width,
@@ -1890,7 +1890,7 @@ function splitCaptureSheets(rows, size, gap) {
1890
1890
  function readCaptureRequestOwner(name) {
1891
1891
  const match = new RegExp(`^${CAPTURE_REQUEST_PREFIX}-(\\d+)\\.json(?:\\.tmp)?$`, "u").exec(name);
1892
1892
  const pid = Number(match?.[1]);
1893
- return Number.isSafeInteger(pid) && pid > 0 && pid <= 2147483647 ? pid : void 0;
1893
+ return isSafeInteger(pid) && pid > 0 && pid <= 2147483647 ? pid : void 0;
1894
1894
  }
1895
1895
  /**
1896
1896
  * Describes the request holders that keep a workspace busy, naming apart those the host could not inspect.
@@ -2992,7 +2992,7 @@ async function answerCaptureDiscovery(options) {
2992
2992
  function readCaptureListing(directory, list) {
2993
2993
  const listed = attempt(() => list(directory));
2994
2994
  if (listed.success) return listed.value;
2995
- if (isError(listed.error) && "code" in listed.error && listed.error.code === "ENOENT") return [];
2995
+ if (isCodedError(listed.error) && listed.error.code === "ENOENT") return [];
2996
2996
  throw new CaptureError(`Cannot list capture frames under ${directory}`, {
2997
2997
  code: "EVIDENCE",
2998
2998
  cause: listed.error,
@@ -3091,7 +3091,7 @@ function readCaptureProgress(root, requests, now = Date.now(), alive = inspectPr
3091
3091
  });
3092
3092
  const events = read.value;
3093
3093
  const computed = computeProgress(events, now);
3094
- const writers = new Map(computed.filter((run) => run.state === "live").map((run) => [run.pid, Number.isSafeInteger(run.pid) && run.pid > 0 ? alive(run.pid) : void 0]));
3094
+ const writers = new Map(computed.filter((run) => run.state === "live").map((run) => [run.pid, isSafeInteger(run.pid) && run.pid > 0 ? alive(run.pid) : void 0]));
3095
3095
  return {
3096
3096
  file: newest.file,
3097
3097
  runs: computed.map((run) => {
@@ -4477,7 +4477,7 @@ var CaptureInvocation = class {
4477
4477
  const deadline = performance.now() + CAPTURE_OWNER_TIMEOUT;
4478
4478
  const marker = attempt(() => this.#read(deadline));
4479
4479
  if (!marker.success) {
4480
- if (marker.error instanceof Error && "code" in marker.error && marker.error.code === "ENOENT") return;
4480
+ if (isCodedError(marker.error) && marker.error.code === "ENOENT") return;
4481
4481
  throw marker.error;
4482
4482
  }
4483
4483
  if (this.#owner(marker.value).id !== this.id) return;
@@ -4656,7 +4656,7 @@ var CaptureInvocation = class {
4656
4656
  else this.#options.ownership.remove(this.#lock);
4657
4657
  });
4658
4658
  if (result.success) return;
4659
- if (result.error instanceof Error && "code" in result.error && result.error.code === "ENOENT") return;
4659
+ if (isCodedError(result.error) && result.error.code === "ENOENT") return;
4660
4660
  if (!matchesCaptureTransient(result.error, this.#options.ownership?.platform)) throw result.error;
4661
4661
  this.#backoff(deadline, result.error);
4662
4662
  this.#remove(deadline);
@@ -4717,7 +4717,7 @@ var CaptureInvocation = class {
4717
4717
  for (const name of readdirSync(requests)) {
4718
4718
  if (!name.startsWith(`.capture-owner.`) || !name.endsWith(".tmp")) continue;
4719
4719
  const [pid] = name.slice(CAPTURE_OWNER_FILE.length + 1).split(".");
4720
- if (pid !== void 0 && /^\d+$/u.test(pid) && Number.isSafeInteger(Number(pid)) && Number(pid) > 0 && this.#alive(Number(pid)) === false) attempt(() => rmSync(resolve(requests, name), { force: true }));
4720
+ if (pid !== void 0 && /^\d+$/u.test(pid) && isSafeInteger(Number(pid)) && Number(pid) > 0 && this.#alive(Number(pid)) === false) attempt(() => rmSync(resolve(requests, name), { force: true }));
4721
4721
  }
4722
4722
  const temporary = `${this.#lock}.${String(process.pid)}.${this.id}.tmp`;
4723
4723
  try {
@@ -4755,10 +4755,10 @@ var CaptureInvocation = class {
4755
4755
  this.#backoff(deadline, opened.error);
4756
4756
  return this.#acquire(temporary, deadline);
4757
4757
  }
4758
- if (!(opened.error instanceof Error) || !("code" in opened.error) || opened.error.code !== "EEXIST") throw opened.error;
4758
+ if (!isCodedError(opened.error) || opened.error.code !== "EEXIST") throw opened.error;
4759
4759
  const marker = attempt(() => this.#read(deadline));
4760
4760
  if (!marker.success) {
4761
- if (marker.error instanceof Error && "code" in marker.error && marker.error.code === "ENOENT") {
4761
+ if (isCodedError(marker.error) && marker.error.code === "ENOENT") {
4762
4762
  this.#backoff(deadline);
4763
4763
  return this.#acquire(temporary, deadline);
4764
4764
  }
@@ -4773,7 +4773,7 @@ var CaptureInvocation = class {
4773
4773
  if (!owner.success) this.#backoff(deadline);
4774
4774
  const confirmed = attempt(() => this.#read(deadline));
4775
4775
  if (!confirmed.success) {
4776
- if (confirmed.error instanceof Error && "code" in confirmed.error && confirmed.error.code === "ENOENT") {
4776
+ if (isCodedError(confirmed.error) && confirmed.error.code === "ENOENT") {
4777
4777
  this.#backoff(deadline);
4778
4778
  return this.#acquire(temporary, deadline);
4779
4779
  }
@@ -4796,7 +4796,7 @@ var CaptureInvocation = class {
4796
4796
  #owner(marker) {
4797
4797
  if (marker === "") throw new CaptureError(`Capture ownership marker is empty; confirm no capture process or surviving child is running and inspect this file: ${this.#lock}; its holder cannot be established`, { code: "STAGE" });
4798
4798
  const owner = attempt(() => parseJSON(marker));
4799
- if (!owner.success || !isRecord(owner.value) || typeof owner.value.pid !== "number" || !Number.isSafeInteger(owner.value.pid) || owner.value.pid <= 0 || typeof owner.value.id !== "string" || owner.value.id === "") throw new CaptureError(`Capture ownership marker is malformed; expected { pid: positive integer, id: nonempty string }, received ${preview(marker)}; confirm no capture process or surviving child is running and inspect this file: ${this.#lock}`, { code: "STAGE" });
4799
+ if (!owner.success || !isRecord(owner.value) || typeof owner.value.pid !== "number" || !isSafeInteger(owner.value.pid) || owner.value.pid <= 0 || typeof owner.value.id !== "string" || owner.value.id === "") throw new CaptureError(`Capture ownership marker is malformed; expected { pid: positive integer, id: nonempty string }, received ${preview(marker)}; confirm no capture process or surviving child is running and inspect this file: ${this.#lock}`, { code: "STAGE" });
4800
4800
  return {
4801
4801
  pid: owner.value.pid,
4802
4802
  id: owner.value.id
@@ -4812,7 +4812,7 @@ var CaptureInvocation = class {
4812
4812
  const file = resolve(this.#options.root, this.#options.requests, CAPTURE_RUNS_DIRECTORY, holder.id, CAPTURE_SURVIVORS_FILE);
4813
4813
  try {
4814
4814
  const record = parseJSON(this.#options.ownership?.read === void 0 ? readFileSync(file, "utf8") : this.#options.ownership.read(file));
4815
- if (!isRecord(record) || record.pid !== holder.pid || record.id !== holder.id || typeof record.command !== "string" || record.command === "" || typeof record.at !== "string" || !Number.isFinite(Date.parse(record.at)) || !Array.isArray(record.children) || !record.children.every((pid) => typeof pid === "number" && Number.isSafeInteger(pid) && pid > 0)) return `Child record could not be read (EINVAL): ${file}.`;
4815
+ if (!isRecord(record) || record.pid !== holder.pid || record.id !== holder.id || typeof record.command !== "string" || record.command === "" || typeof record.at !== "string" || !Number.isFinite(Date.parse(record.at)) || !Array.isArray(record.children) || !record.children.every((pid) => typeof pid === "number" && isSafeInteger(pid) && pid > 0)) return `Child record could not be read (EINVAL): ${file}.`;
4816
4816
  const omitted = record.children.length - 16;
4817
4817
  return `Unconfirmed child processes: ${[...record.children.slice(0, 16), ...omitted > 0 ? [`… ${String(omitted)} more`] : []].join(", ")}; stop them before deleting. Child record: ${file}.`;
4818
4818
  } catch (error) {
@@ -5478,8 +5478,8 @@ var CaptureRunner = class {
5478
5478
  const invocations = [invocation];
5479
5479
  const reports = [];
5480
5480
  const argumentsResult = attempt(() => readCaptureArguments(argv, command, root, config));
5481
- const described = await this.#describe(invocation, argumentsResult).then((description) => success(description), (error) => failure(error));
5482
- const result = isFailure(described) ? described : await this.#dispatch(invocation, invocations, reports, described.value.parsed, described.value.projects).then((code) => success(code), (error) => failure(error));
5481
+ const described = await settle(() => this.#describe(invocation, argumentsResult));
5482
+ const result = isFailure(described) ? described : await settle(() => this.#dispatch(invocation, invocations, reports, described.value.parsed, described.value.projects));
5483
5483
  try {
5484
5484
  const code = isSuccess(result) ? result.value : resolveCaptureExit(result.error);
5485
5485
  if (code === CAPTURE_EXIT.usage) rmSync(invocation.directory, {
@@ -5682,7 +5682,7 @@ var CaptureRunner = class {
5682
5682
  const outcomes = [];
5683
5683
  const stages = resolveCaptureStages(variants, primary.first, command);
5684
5684
  const completed = [];
5685
- const result = await this.#stages(invocation, integrity, plan, stages, outcomes, completed).then((code) => success(code), (error) => failure(error));
5685
+ const result = await settle(() => this.#stages(invocation, integrity, plan, stages, outcomes, completed));
5686
5686
  const ran = stages.filter((stage) => invocation.launched(resolve(invocation.directory, `${command}-${stage.name}.log`)));
5687
5687
  const notrun = stages.filter((stage) => !ran.includes(stage));
5688
5688
  const failed = ran.filter((stage) => !completed.includes(stage));