@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.
@@ -633,7 +633,7 @@ function readCaptureAncestor(path) {
633
633
  (0, node_fs.lstatSync)(path);
634
634
  return path;
635
635
  } catch (error) {
636
- if (!(0, _orkestrel_contract.isError)(error) || !("code" in error) || error.code !== "ENOENT" || (0, node_path.dirname)(path) === path) throw error;
636
+ if (!(0, _orkestrel_contract.isCodedError)(error) || error.code !== "ENOENT" || (0, node_path.dirname)(path) === path) throw error;
637
637
  return readCaptureAncestor((0, node_path.dirname)(path));
638
638
  }
639
639
  }
@@ -1389,7 +1389,7 @@ function scanCaptureChunks(bytes, position, count) {
1389
1389
  return [...head, ...scanCaptureChunks(bytes, last + 12 + bytes.readUInt32BE(last), limit - half)];
1390
1390
  }
1391
1391
  if (!(limit >= 1)) return [];
1392
- if (!Number.isSafeInteger(position) || position < 8 || position + 12 > bytes.length) throw new _src_core.CaptureError(`Truncated capture PNG: no 12-byte chunk frame fits at offset ${(0, _orkestrel_contract.preview)(position)} in ${String(bytes.length)} bytes; expected a whole offset from 8 with 12 bytes after it`, { code: "EVIDENCE" });
1392
+ if (!(0, _orkestrel_contract.isSafeInteger)(position) || position < 8 || position + 12 > bytes.length) throw new _src_core.CaptureError(`Truncated capture PNG: no 12-byte chunk frame fits at offset ${(0, _orkestrel_contract.preview)(position)} in ${String(bytes.length)} bytes; expected a whole offset from 8 with 12 bytes after it`, { code: "EVIDENCE" });
1393
1393
  const end = position + 12 + bytes.readUInt32BE(position);
1394
1394
  if (end > bytes.length) throw new _src_core.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" });
1395
1395
  const stored = bytes.readUInt32BE(end - 4);
@@ -1443,7 +1443,7 @@ function readCaptureImage(file) {
1443
1443
  * ```
1444
1444
  */
1445
1445
  function requireCaptureImageSize(size) {
1446
- if (!Number.isSafeInteger(size) || size < 0 || size > CAPTURE_PNG_LIMITS.bytes) throw new _src_core.CaptureError(`Capture PNG size is outside the file budget: ${(0, _orkestrel_contract.preview)(size)} bytes; expected a whole number of bytes from 0 to ${String(CAPTURE_PNG_LIMITS.bytes)}`, { code: "EVIDENCE" });
1446
+ if (!(0, _orkestrel_contract.isSafeInteger)(size) || size < 0 || size > CAPTURE_PNG_LIMITS.bytes) throw new _src_core.CaptureError(`Capture PNG size is outside the file budget: ${(0, _orkestrel_contract.preview)(size)} bytes; expected a whole number of bytes from 0 to ${String(CAPTURE_PNG_LIMITS.bytes)}`, { code: "EVIDENCE" });
1447
1447
  }
1448
1448
  /**
1449
1449
  * Reconstructs one filtered PNG scanline from its filter byte and the row above.
@@ -1522,7 +1522,7 @@ function decodeCapturePicture(bytes) {
1522
1522
  if (width === 0 || height === 0) throw new _src_core.CaptureError(`Invalid capture PNG dimensions: ${size}; expected a width and a height of at least 1`, { code: "EVIDENCE" });
1523
1523
  const stride = width * channels;
1524
1524
  const length = (stride + 1) * height;
1525
- if (!Number.isSafeInteger(length)) throw new _src_core.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
+ if (!(0, _orkestrel_contract.isSafeInteger)(length)) throw new _src_core.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" });
1526
1526
  if (width * height > CAPTURE_PNG_LIMITS.pixels) throw new _src_core.CaptureError(`Capture PNG exceeds the pixel budget: ${size} is ${String(width * height)} pixels; expected at most ${String(CAPTURE_PNG_LIMITS.pixels)} pixels`, { code: "EVIDENCE" });
1527
1527
  if (width > CAPTURE_PNG_LIMITS.side || height > CAPTURE_PNG_LIMITS.side) throw new _src_core.CaptureError(`Capture PNG exceeds the dimension budget: ${size}; expected at most ${String(CAPTURE_PNG_LIMITS.side)} pixels per side`, { code: "EVIDENCE" });
1528
1528
  const chunks = readCaptureChunks(bytes);
@@ -1610,7 +1610,7 @@ function encodeCaptureChunk(name, data) {
1610
1610
  * ```
1611
1611
  */
1612
1612
  function encodeCapturePicture(picture, compressed = true) {
1613
- 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 _src_core.CaptureError("Invalid capture picture dimensions", { code: "EVIDENCE" });
1613
+ if (!(0, _orkestrel_contract.isSafeInteger)(picture.width) || !(0, _orkestrel_contract.isSafeInteger)(picture.height) || picture.width <= 0 || picture.height <= 0 || picture.pixels.length !== picture.width * picture.height * 3) throw new _src_core.CaptureError("Invalid capture picture dimensions", { code: "EVIDENCE" });
1614
1614
  const stride = picture.width * 3;
1615
1615
  const raw = Buffer.concat(Array.from({ length: picture.height }, (_, row) => Buffer.concat([Buffer.from([0]), picture.pixels.subarray(row * stride, (row + 1) * stride)])));
1616
1616
  return Buffer.concat([
@@ -1753,7 +1753,7 @@ function shrinkCapturePicture(picture, factor, limit) {
1753
1753
  function composeCapturePictures(pictures, vertical, gap = 6) {
1754
1754
  if (pictures.length === 0) throw new _src_core.CaptureError("Empty contact-sheet picture selection", { code: "USAGE" });
1755
1755
  const { side, pixels: budget } = CAPTURE_PNG_LIMITS;
1756
- 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 ${(0, _orkestrel_contract.preview)(picture[axis])}`])), ...Number.isSafeInteger(gap) && gap >= 0 && gap <= side ? [] : [`gap is ${(0, _orkestrel_contract.preview)(gap)}`]];
1756
+ const faults = [...pictures.flatMap((picture, position) => ["width", "height"].flatMap((axis) => (0, _orkestrel_contract.isSafeInteger)(picture[axis]) && picture[axis] >= 0 && picture[axis] <= side ? [] : [`pictures[${String(position)}].${axis} is ${(0, _orkestrel_contract.preview)(picture[axis])}`])), ...(0, _orkestrel_contract.isSafeInteger)(gap) && gap >= 0 && gap <= side ? [] : [`gap is ${(0, _orkestrel_contract.preview)(gap)}`]];
1757
1757
  const { width, height } = computeCaptureComposite(pictures, vertical, gap);
1758
1758
  if (faults.length > 0 || !matchesCaptureLimits({
1759
1759
  width,
@@ -1891,7 +1891,7 @@ function splitCaptureSheets(rows, size, gap) {
1891
1891
  function readCaptureRequestOwner(name) {
1892
1892
  const match = new RegExp(`^${_src_core.CAPTURE_REQUEST_PREFIX}-(\\d+)\\.json(?:\\.tmp)?$`, "u").exec(name);
1893
1893
  const pid = Number(match?.[1]);
1894
- return Number.isSafeInteger(pid) && pid > 0 && pid <= 2147483647 ? pid : void 0;
1894
+ return (0, _orkestrel_contract.isSafeInteger)(pid) && pid > 0 && pid <= 2147483647 ? pid : void 0;
1895
1895
  }
1896
1896
  /**
1897
1897
  * Describes the request holders that keep a workspace busy, naming apart those the host could not inspect.
@@ -2993,7 +2993,7 @@ async function answerCaptureDiscovery(options) {
2993
2993
  function readCaptureListing(directory, list) {
2994
2994
  const listed = (0, _orkestrel_contract.attempt)(() => list(directory));
2995
2995
  if (listed.success) return listed.value;
2996
- if ((0, _orkestrel_contract.isError)(listed.error) && "code" in listed.error && listed.error.code === "ENOENT") return [];
2996
+ if ((0, _orkestrel_contract.isCodedError)(listed.error) && listed.error.code === "ENOENT") return [];
2997
2997
  throw new _src_core.CaptureError(`Cannot list capture frames under ${directory}`, {
2998
2998
  code: "EVIDENCE",
2999
2999
  cause: listed.error,
@@ -3092,7 +3092,7 @@ function readCaptureProgress(root, requests, now = Date.now(), alive = _orkestre
3092
3092
  });
3093
3093
  const events = read.value;
3094
3094
  const computed = (0, _orkestrel_test_server.computeProgress)(events, now);
3095
- 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]));
3095
+ const writers = new Map(computed.filter((run) => run.state === "live").map((run) => [run.pid, (0, _orkestrel_contract.isSafeInteger)(run.pid) && run.pid > 0 ? alive(run.pid) : void 0]));
3096
3096
  return {
3097
3097
  file: newest.file,
3098
3098
  runs: computed.map((run) => {
@@ -4478,7 +4478,7 @@ var CaptureInvocation = class {
4478
4478
  const deadline = performance.now() + CAPTURE_OWNER_TIMEOUT;
4479
4479
  const marker = (0, _orkestrel_contract.attempt)(() => this.#read(deadline));
4480
4480
  if (!marker.success) {
4481
- if (marker.error instanceof Error && "code" in marker.error && marker.error.code === "ENOENT") return;
4481
+ if ((0, _orkestrel_contract.isCodedError)(marker.error) && marker.error.code === "ENOENT") return;
4482
4482
  throw marker.error;
4483
4483
  }
4484
4484
  if (this.#owner(marker.value).id !== this.id) return;
@@ -4657,7 +4657,7 @@ var CaptureInvocation = class {
4657
4657
  else this.#options.ownership.remove(this.#lock);
4658
4658
  });
4659
4659
  if (result.success) return;
4660
- if (result.error instanceof Error && "code" in result.error && result.error.code === "ENOENT") return;
4660
+ if ((0, _orkestrel_contract.isCodedError)(result.error) && result.error.code === "ENOENT") return;
4661
4661
  if (!matchesCaptureTransient(result.error, this.#options.ownership?.platform)) throw result.error;
4662
4662
  this.#backoff(deadline, result.error);
4663
4663
  this.#remove(deadline);
@@ -4718,7 +4718,7 @@ var CaptureInvocation = class {
4718
4718
  for (const name of (0, node_fs.readdirSync)(requests)) {
4719
4719
  if (!name.startsWith(`.capture-owner.`) || !name.endsWith(".tmp")) continue;
4720
4720
  const [pid] = name.slice(CAPTURE_OWNER_FILE.length + 1).split(".");
4721
- if (pid !== void 0 && /^\d+$/u.test(pid) && Number.isSafeInteger(Number(pid)) && Number(pid) > 0 && this.#alive(Number(pid)) === false) (0, _orkestrel_contract.attempt)(() => (0, node_fs.rmSync)((0, node_path.resolve)(requests, name), { force: true }));
4721
+ if (pid !== void 0 && /^\d+$/u.test(pid) && (0, _orkestrel_contract.isSafeInteger)(Number(pid)) && Number(pid) > 0 && this.#alive(Number(pid)) === false) (0, _orkestrel_contract.attempt)(() => (0, node_fs.rmSync)((0, node_path.resolve)(requests, name), { force: true }));
4722
4722
  }
4723
4723
  const temporary = `${this.#lock}.${String(process.pid)}.${this.id}.tmp`;
4724
4724
  try {
@@ -4756,10 +4756,10 @@ var CaptureInvocation = class {
4756
4756
  this.#backoff(deadline, opened.error);
4757
4757
  return this.#acquire(temporary, deadline);
4758
4758
  }
4759
- if (!(opened.error instanceof Error) || !("code" in opened.error) || opened.error.code !== "EEXIST") throw opened.error;
4759
+ if (!(0, _orkestrel_contract.isCodedError)(opened.error) || opened.error.code !== "EEXIST") throw opened.error;
4760
4760
  const marker = (0, _orkestrel_contract.attempt)(() => this.#read(deadline));
4761
4761
  if (!marker.success) {
4762
- if (marker.error instanceof Error && "code" in marker.error && marker.error.code === "ENOENT") {
4762
+ if ((0, _orkestrel_contract.isCodedError)(marker.error) && marker.error.code === "ENOENT") {
4763
4763
  this.#backoff(deadline);
4764
4764
  return this.#acquire(temporary, deadline);
4765
4765
  }
@@ -4774,7 +4774,7 @@ var CaptureInvocation = class {
4774
4774
  if (!owner.success) this.#backoff(deadline);
4775
4775
  const confirmed = (0, _orkestrel_contract.attempt)(() => this.#read(deadline));
4776
4776
  if (!confirmed.success) {
4777
- if (confirmed.error instanceof Error && "code" in confirmed.error && confirmed.error.code === "ENOENT") {
4777
+ if ((0, _orkestrel_contract.isCodedError)(confirmed.error) && confirmed.error.code === "ENOENT") {
4778
4778
  this.#backoff(deadline);
4779
4779
  return this.#acquire(temporary, deadline);
4780
4780
  }
@@ -4797,7 +4797,7 @@ var CaptureInvocation = class {
4797
4797
  #owner(marker) {
4798
4798
  if (marker === "") throw new _src_core.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" });
4799
4799
  const owner = (0, _orkestrel_contract.attempt)(() => (0, _orkestrel_contract.parseJSON)(marker));
4800
- if (!owner.success || !(0, _orkestrel_contract.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 _src_core.CaptureError(`Capture ownership marker is malformed; expected { pid: positive integer, id: nonempty string }, received ${(0, _orkestrel_contract.preview)(marker)}; confirm no capture process or surviving child is running and inspect this file: ${this.#lock}`, { code: "STAGE" });
4800
+ if (!owner.success || !(0, _orkestrel_contract.isRecord)(owner.value) || typeof owner.value.pid !== "number" || !(0, _orkestrel_contract.isSafeInteger)(owner.value.pid) || owner.value.pid <= 0 || typeof owner.value.id !== "string" || owner.value.id === "") throw new _src_core.CaptureError(`Capture ownership marker is malformed; expected { pid: positive integer, id: nonempty string }, received ${(0, _orkestrel_contract.preview)(marker)}; confirm no capture process or surviving child is running and inspect this file: ${this.#lock}`, { code: "STAGE" });
4801
4801
  return {
4802
4802
  pid: owner.value.pid,
4803
4803
  id: owner.value.id
@@ -4813,7 +4813,7 @@ var CaptureInvocation = class {
4813
4813
  const file = (0, node_path.resolve)(this.#options.root, this.#options.requests, CAPTURE_RUNS_DIRECTORY, holder.id, CAPTURE_SURVIVORS_FILE);
4814
4814
  try {
4815
4815
  const record = (0, _orkestrel_contract.parseJSON)(this.#options.ownership?.read === void 0 ? (0, node_fs.readFileSync)(file, "utf8") : this.#options.ownership.read(file));
4816
- if (!(0, _orkestrel_contract.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}.`;
4816
+ if (!(0, _orkestrel_contract.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" && (0, _orkestrel_contract.isSafeInteger)(pid) && pid > 0)) return `Child record could not be read (EINVAL): ${file}.`;
4817
4817
  const omitted = record.children.length - 16;
4818
4818
  return `Unconfirmed child processes: ${[...record.children.slice(0, 16), ...omitted > 0 ? [`… ${String(omitted)} more`] : []].join(", ")}; stop them before deleting. Child record: ${file}.`;
4819
4819
  } catch (error) {
@@ -5479,8 +5479,8 @@ var CaptureRunner = class {
5479
5479
  const invocations = [invocation];
5480
5480
  const reports = [];
5481
5481
  const argumentsResult = (0, _orkestrel_contract.attempt)(() => readCaptureArguments(argv, command, root, config));
5482
- const described = await this.#describe(invocation, argumentsResult).then((description) => (0, _orkestrel_contract.success)(description), (error) => (0, _orkestrel_contract.failure)(error));
5483
- const result = (0, _orkestrel_contract.isFailure)(described) ? described : await this.#dispatch(invocation, invocations, reports, described.value.parsed, described.value.projects).then((code) => (0, _orkestrel_contract.success)(code), (error) => (0, _orkestrel_contract.failure)(error));
5482
+ const described = await (0, _orkestrel_contract.settle)(() => this.#describe(invocation, argumentsResult));
5483
+ const result = (0, _orkestrel_contract.isFailure)(described) ? described : await (0, _orkestrel_contract.settle)(() => this.#dispatch(invocation, invocations, reports, described.value.parsed, described.value.projects));
5484
5484
  try {
5485
5485
  const code = (0, _orkestrel_contract.isSuccess)(result) ? result.value : resolveCaptureExit(result.error);
5486
5486
  if (code === CAPTURE_EXIT.usage) (0, node_fs.rmSync)(invocation.directory, {
@@ -5683,7 +5683,7 @@ var CaptureRunner = class {
5683
5683
  const outcomes = [];
5684
5684
  const stages = resolveCaptureStages(variants, primary.first, command);
5685
5685
  const completed = [];
5686
- const result = await this.#stages(invocation, integrity, plan, stages, outcomes, completed).then((code) => (0, _orkestrel_contract.success)(code), (error) => (0, _orkestrel_contract.failure)(error));
5686
+ const result = await (0, _orkestrel_contract.settle)(() => this.#stages(invocation, integrity, plan, stages, outcomes, completed));
5687
5687
  const ran = stages.filter((stage) => invocation.launched((0, node_path.resolve)(invocation.directory, `${command}-${stage.name}.log`)));
5688
5688
  const notrun = stages.filter((stage) => !ran.includes(stage));
5689
5689
  const failed = ran.filter((stage) => !completed.includes(stage));