/* * 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; } } }