Created
July 20, 2023 02:46
-
-
Save aztack/87b28ac0f222f57b4aecba379600b35a to your computer and use it in GitHub Desktop.
ArrayBuffer maybe JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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