feat: add bounded Quantum cabinet extraction

This commit is contained in:
2026-07-22 19:23:00 +02:00
parent 531612829d
commit a7b1cd7d16
20 changed files with 1099 additions and 66 deletions

View File

@@ -0,0 +1,44 @@
/*
* SPDX-License-Identifier: MIT
*
* Quantum decoder support adapted from compcol `src/quantum/*` at commit
* 04a6db2aa7bd487a89c559631d79d1b384139f50.
* Copyright (c) 2026 Karpeles Lab Inc., MIT License.
*/
import { fail } from '../errors.js';
export interface QuantumDecoderOptions {
maxOperations: number;
checkCancellation?: () => void;
}
export class QuantumBudget {
private operations = 0;
private nextCancellationCheck = 4_096;
constructor(private readonly options: QuantumDecoderOptions) {}
consume(count = 1): void {
if (!Number.isSafeInteger(count) || count < 0) {
fail('quantum-operation-overflow', 'Invalid Quantum operation count.', {
structure: 'Quantum stream',
});
}
this.operations += count;
if (
!Number.isSafeInteger(this.operations) ||
this.operations > this.options.maxOperations
) {
fail(
'quantum-operation-limit',
`Quantum decoding exceeded the configured ${this.options.maxOperations}-operation limit.`,
{ structure: 'Quantum stream' }
);
}
if (this.operations >= this.nextCancellationCheck) {
this.options.checkCancellation?.();
this.nextCancellationCheck = this.operations + 4_096;
}
}
}