Skip to content

Commit a13a2cf

Browse files
kevinelliottclaudemakrsmark
authored
Fix: Buffer.from() in CRC helpers prevents browser/Deno/Bun execution (#444)
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) <noreply@anthropic.com> Co-authored-by: Mark Bumiller <mark.bumiller@gmail.com>
1 parent f3dd9e2 commit a13a2cf

2 files changed

Lines changed: 2193 additions & 2451 deletions

File tree

lib/utils/arinc_702_helper.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import { FlightPlanUtils } from './flight_plan_utils';
77
import { ResultFormatter } from './result_formatter';
88
import { RouteUtils } from './route_utils';
99

10+
// One byte per ASCII char — replaces the Buffer.from(data, 'ascii') idiom so
11+
// the CRC helpers run in browser / Deno / Bun / edge runtimes without Node
12+
// polyfills. Matches the broader Web APIs migration in c78b720.
13+
function asciiStringToBytes(data: string): Uint8Array {
14+
const bytes = new Uint8Array(data.length);
15+
for (let i = 0; i < data.length; i++) bytes[i] = data.charCodeAt(i) & 0xff;
16+
return bytes;
17+
}
18+
1019
/**
1120
* Helper class for decoding ARINC 702 messages
1221
* contains core logic for decoding different types of ARINC 702 messages,
@@ -658,7 +667,7 @@ function processSummary(decodeResult: DecodeResult, data: string[]) {
658667
// CRC-16/IBM-SDLC but nibbles are reversed
659668
function crc16IbmSdlcRev(data: string): number {
660669
let crc = 0xffff;
661-
const bytes = Buffer.from(data, 'ascii');
670+
const bytes = asciiStringToBytes(data);
662671

663672
for (const byte of bytes) {
664673
crc ^= byte;
@@ -689,7 +698,7 @@ function crc16Genibus(data: string): number {
689698
let crc = 0xffff;
690699
const polynomial = 0x1021;
691700

692-
const bytes = Buffer.from(data, 'ascii');
701+
const bytes = asciiStringToBytes(data);
693702

694703
for (const byte of bytes) {
695704
crc ^= byte << 8;

0 commit comments

Comments
 (0)