rig-parts 2.8.0 → 2.9.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.
@@ -4,6 +4,7 @@ import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, statSync, wr
4
4
  import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
5
5
  import { assembleStage, checkStage, rigStage, type AssembleOutputs, type AssembleStageInput, type CheckStageInput, type RigStageInput } from '../src/build.ts';
6
6
  import { rigcRunnerAt, type CheckReport, type RigcRunner } from '../src/check.ts';
7
+ import type { Mask } from '../src/raster/types.ts';
7
8
  import { PartsError, type Problem } from '../src/errors.ts';
8
9
 
9
10
  /** Preserve native/custom executables; adapt only a proven package-manager script shim. */
@@ -72,6 +73,20 @@ export type BuildProcessJob =
72
73
  function record(value: unknown): value is Record<string, unknown> {
73
74
  return value !== null && typeof value === 'object' && !Array.isArray(value);
74
75
  }
76
+ type AssemblyReading = { supportFigure?: Mask };
77
+ type StageReading = CheckReport | AssemblyReading | void;
78
+
79
+ /** Private inter-process source observation, never written to build outputs. */
80
+ function encodeSupportFigure(figure: Mask): { width: number; height: number; alpha_base64: string } {
81
+ return { width: figure.width, height: figure.height, alpha_base64: Buffer.from(figure.data).toString('base64') };
82
+ }
83
+ function decodeSupportFigure(value: unknown): Mask {
84
+ if (!record(value) || !Number.isSafeInteger(value.width) || !Number.isSafeInteger(value.height) ||
85
+ typeof value.width !== 'number' || typeof value.height !== 'number' || value.width <= 0 || value.height <= 0 || typeof value.alpha_base64 !== 'string') throw new Error('BUILD_STAGE_SUPPORT_FIGURE: positive native dimensions and encoded alpha are required');
86
+ const data = Buffer.from(value.alpha_base64, 'base64');
87
+ if (data.length !== value.width * value.height || data.toString('base64') !== value.alpha_base64 || data.some(p => p !== 0 && p !== 1)) throw new Error('BUILD_STAGE_SUPPORT_FIGURE: canonical binary alpha must fill the native grid');
88
+ return { width: value.width, height: value.height, data };
89
+ }
75
90
  function problems(value: unknown): value is Problem[] {
76
91
  return Array.isArray(value) && value.length > 0 && value.every((p) => record(p) && typeof p.code === 'string' && typeof p.object === 'string' && typeof p.detail === 'string');
77
92
  }
@@ -93,9 +108,10 @@ function checkReport(value: unknown): value is CheckReport {
93
108
 
94
109
  /** Decode only a matching successful stage or its original named input refusal. */
95
110
  export function readBuildReceipt(stage: 'check', status: number | null, signal: string | null, text: string): CheckReport;
96
- export function readBuildReceipt(stage: 'assemble' | 'rig', status: number | null, signal: string | null, text: string): void;
97
- export function readBuildReceipt(stage: Stage, status: number | null, signal: string | null, text: string): CheckReport | void;
98
- export function readBuildReceipt(stage: Stage, status: number | null, signal: string | null, text: string): CheckReport | void {
111
+ export function readBuildReceipt(stage: 'assemble', status: number | null, signal: string | null, text: string): AssemblyReading | void;
112
+ export function readBuildReceipt(stage: 'rig', status: number | null, signal: string | null, text: string): void;
113
+ export function readBuildReceipt(stage: Stage, status: number | null, signal: string | null, text: string): StageReading;
114
+ export function readBuildReceipt(stage: Stage, status: number | null, signal: string | null, text: string): StageReading {
99
115
  if (signal !== null || status === null || (status !== 0 && status !== 1) || (status !== 0 && text === '')) {
100
116
  throw new Error('BUILD_STAGE_CHILD: ' + stage + ' child — exit ' + status + ', signal ' + signal + '; no later stage ran');
101
117
  }
@@ -110,6 +126,8 @@ export function readBuildReceipt(stage: Stage, status: number | null, signal: st
110
126
  if (status !== 0) throw new Error('BUILD_STAGE_CHILD: ' + stage + ' child — exit ' + status + ' with a completion receipt; no later stage ran');
111
127
  if (stage !== 'check') {
112
128
  if (Object.hasOwn(row, 'report') || Object.hasOwn(row, 'sweepSteps')) return resultError(stage, 'a void stage returned an invented check report');
129
+ if (stage === 'assemble' && row.supportFigure !== undefined) return { supportFigure: decodeSupportFigure(row.supportFigure) };
130
+ if (row.supportFigure !== undefined) return resultError(stage, 'only assembly returns a source observation');
113
131
  return;
114
132
  }
115
133
  if (!checkReport(row.report)) return resultError(stage, 'check report is incomplete');
@@ -129,7 +147,7 @@ export function encodeCheckReceipt(report: CheckReport): string {
129
147
  }
130
148
 
131
149
  /** Minimum Bun may attach error to an ordinary nonzero exit; only an unstarted child is a launch failure. */
132
- export function readBuildChild(stage: Stage, child: { status: number | null; signal: string | null; error?: Error }, text: string): CheckReport | void {
150
+ export function readBuildChild(stage: Stage, child: { status: number | null; signal: string | null; error?: Error }, text: string): StageReading {
133
151
  if (!childStarted(child) && child.error !== undefined) throw new Error('BUILD_STAGE_CHILD: ' + stage + ' could not start — ' + child.error.message);
134
152
  return readBuildReceipt(stage, child.status ?? null, child.signal ?? null, text);
135
153
  }
@@ -142,13 +160,15 @@ export function launchBuildChild(argv: readonly string[]): { status: number | nu
142
160
  }
143
161
 
144
162
  export function runBuildProcess(job: Extract<BuildProcessJob, { stage: 'check' }>, home: string): CheckReport;
145
- export function runBuildProcess(job: Exclude<BuildProcessJob, { stage: 'check' }>, home: string): void;
146
- export function runBuildProcess(job: BuildProcessJob, home: string): CheckReport | void {
163
+ export function runBuildProcess(job: Extract<BuildProcessJob, { stage: 'assemble' }>, home: string): AssemblyReading | void;
164
+ export function runBuildProcess(job: Extract<BuildProcessJob, { stage: 'rig' }>, home: string): void;
165
+ export function runBuildProcess(job: BuildProcessJob, home: string): StageReading {
147
166
  mkdirSync(home, { recursive: true });
148
167
  const request = join(home, job.stage + '.request.json');
149
168
  const result = join(home, job.stage + '.result.json');
150
169
  rmSync(result, { force: true });
151
- writeFileSync(request, JSON.stringify(job));
170
+ const wire = job.stage === 'check' && job.input.supportFigure !== undefined ? { ...job, input: { ...job.input, supportFigure: encodeSupportFigure(job.input.supportFigure) } } : job;
171
+ writeFileSync(request, JSON.stringify(wire));
152
172
  try {
153
173
  const child = launchBuildChild([process.execPath, import.meta.path, request, result]);
154
174
  return readBuildChild(job.stage, child, existsSync(result) ? readFileSync(result, 'utf8') : '');
@@ -174,6 +194,7 @@ function readJob(path: string): BuildProcessJob {
174
194
  if (!strings(['config', 'parts', 'out']) || typeof job.scratch !== 'string' || Object.hasOwn(input, 'reduce')) return requestError('rig input or scratch is incomplete, or contains a nonserializable reducer');
175
195
  } else if (job.stage === 'check') {
176
196
  if (!strings(['rig', 'out'])) return requestError('check input paths are incomplete');
197
+ if (input.supportFigure !== undefined) input.supportFigure = decodeSupportFigure(input.supportFigure);
177
198
  } else return requestError('unknown stage ' + JSON.stringify(job.stage));
178
199
  return job as unknown as BuildProcessJob;
179
200
  }
@@ -185,10 +206,11 @@ function worker(args: string[]): number {
185
206
  const log = (line: string): void => { console.log('[' + job.stage + '] ' + line); };
186
207
  try {
187
208
  let report: CheckReport | undefined;
188
- if (job.stage === 'assemble') assembleStage(job.input, job.outputs, log);
209
+ let supportFigure: Mask | undefined;
210
+ if (job.stage === 'assemble') supportFigure = assembleStage(job.input, job.outputs, log).supportFigure;
189
211
  else if (job.stage === 'rig') rigStage(job.input, runner, job.scratch, log);
190
212
  else report = checkStage(job.input, runner, job.bin, log);
191
- writeFileSync(args[1], report === undefined ? JSON.stringify({ version: 1, stage: job.stage, kind: 'done' }) : encodeCheckReceipt(report));
213
+ writeFileSync(args[1], report === undefined ? JSON.stringify({ version: 1, stage: job.stage, kind: 'done', ...(supportFigure === undefined ? {} : { supportFigure: encodeSupportFigure(supportFigure) }) }) : encodeCheckReceipt(report));
192
214
  return 0;
193
215
  } catch (err) {
194
216
  if (!(err instanceof PartsError)) throw err;