Skip to content

Instantly share code, notes, and snippets.

@aztack
Created July 20, 2023 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/87b28ac0f222f57b4aecba379600b35a to your computer and use it in GitHub Desktop.
Save aztack/87b28ac0f222f57b4aecba379600b35a to your computer and use it in GitHub Desktop.
ArrayBuffer maybe JSON
function maybeJson(arrayBuffer: ArrayBuffer) {
const firstDelimiter = '{'.charCodeAt(0);
const lastDelimiters = ['}\n'.charCodeAt(0), '}\n'.charCodeAt(1), '}\r\n'.charCodeAt(0), '}\r\n'.charCodeAt(1)];
const uint8View = new Uint8Array(arrayBuffer);
const firstByte = uint8View[0];
const lastTwoBytes = [uint8View[uint8View.length - 2], uint8View[uint8View.length - 1]];
return (firstByte === firstDelimiter) && (lastDelimiters.includes(lastTwoBytes[0]) && lastDelimiters.includes(lastTwoBytes[1]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment