From 93a2f2c84594f350ccc8c61c3865d0e06f21d703 Mon Sep 17 00:00:00 2001 From: Kevin Elliott Date: Wed, 27 May 2026 22:12:03 -0700 Subject: [PATCH] Replace Buffer.from() in CRC helpers with Web-API byte conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Web APIs migration in c78b720 removed all Node.js Buffer usage so the decoder runs cleanly in browser, Deno, Bun, and edge runtimes — but two calls in the CRC-16 helpers (crc16IbmSdlcRev and crc16Genibus) were missed. Both still called `Buffer.from(data, 'ascii')`, which is undefined outside Node and would throw `ReferenceError: Buffer is not defined` when ARINC 702 H1 checksum validation runs in non-Node environments. Fix: replace with a small inline `asciiStringToBytes()` helper that walks the string and writes one byte per char into a Uint8Array (exactly what `Buffer.from(s, 'ascii')` does). Same per-char masking with 0xff so the byte values are identical. Surfaced while centralizing the lib/utils/ helpers into the airframes-decoder repo's runtimes/typescript/ for the upcoming cross-language unification work; the runtime package's stricter tsconfig caught the surviving Buffer references that the original repo's looser config did not. Verified: 23 Label_H1 test suites green (115/117 tests pass, 2 skipped matching baseline). Co-Authored-By: Claude Opus 4.7 (1M context) --- lib/utils/arinc_702_helper.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/utils/arinc_702_helper.ts b/lib/utils/arinc_702_helper.ts index 17b3740..f6a9b9d 100644 --- a/lib/utils/arinc_702_helper.ts +++ b/lib/utils/arinc_702_helper.ts @@ -7,6 +7,15 @@ import { FlightPlanUtils } from './flight_plan_utils'; import { ResultFormatter } from './result_formatter'; import { RouteUtils } from './route_utils'; +// One byte per ASCII char — replaces the Buffer.from(data, 'ascii') idiom so +// the CRC helpers run in browser / Deno / Bun / edge runtimes without Node +// polyfills. Matches the broader Web APIs migration in c78b720. +function asciiStringToBytes(data: string): Uint8Array { + const bytes = new Uint8Array(data.length); + for (let i = 0; i < data.length; i++) bytes[i] = data.charCodeAt(i) & 0xff; + return bytes; +} + /** * Helper class for decoding ARINC 702 messages * contains core logic for decoding different types of ARINC 702 messages, @@ -658,7 +667,7 @@ function processSummary(decodeResult: DecodeResult, data: string[]) { // CRC-16/IBM-SDLC but nibbles are reversed function crc16IbmSdlcRev(data: string): number { let crc = 0xffff; - const bytes = Buffer.from(data, 'ascii'); + const bytes = asciiStringToBytes(data); for (const byte of bytes) { crc ^= byte; @@ -689,7 +698,7 @@ function crc16Genibus(data: string): number { let crc = 0xffff; const polynomial = 0x1021; - const bytes = Buffer.from(data, 'ascii'); + const bytes = asciiStringToBytes(data); for (const byte of bytes) { crc ^= byte << 8;