Review a generated telemetry parser

from Typed arrays and ArrayBuffer
Node 24 advanced 8 min 5 issues to find

Review this generated parser before it handles uploaded binary frames.

Parse an aligned Uint8Array window containing an 8-byte little-endian header and Float32 samples, cap untrusted counts, and return a borrowed sample view.

JavaScript
function decodeTelemetry(bytes) {
  const view = new DataView(bytes.buffer);
  const version = view.getUint8(0);
  const flags = view.getUint8(1);
  const sampleCount = view.getUint32(4, true);
  const copied = new Float32Array(sampleCount);
  const samples = new Float32Array(
    bytes.buffer,
    bytes.byteOffset + 8,
    sampleCount,
  );
  copied.set(samples);

  return { version, flags, samples: copied };
}

generated code is illustrative, not from any one model

Open in playground
Report an error