encoding.api.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // TODO: make this more strict against Symbol.toStringTag
  2. // Is not very significant though, anything faking Symbol.toStringTag could as well override
  3. // prototypes, which is not something we protect against
  4. function isAnyArrayBuffer(x) {
  5. if (x instanceof ArrayBuffer) return true
  6. if (globalThis.SharedArrayBuffer && x instanceof SharedArrayBuffer) return true
  7. if (!x || typeof x.byteLength !== 'number') return false
  8. const s = Object.prototype.toString.call(x)
  9. return s === '[object ArrayBuffer]' || s === '[object SharedArrayBuffer]'
  10. }
  11. export function fromSource(x) {
  12. if (x instanceof Uint8Array) return x
  13. if (ArrayBuffer.isView(x)) return new Uint8Array(x.buffer, x.byteOffset, x.byteLength)
  14. if (isAnyArrayBuffer(x)) {
  15. if ('detached' in x) return x.detached === true ? new Uint8Array() : new Uint8Array(x)
  16. // Old engines without .detached, try-catch
  17. try {
  18. return new Uint8Array(x)
  19. } catch {
  20. return new Uint8Array()
  21. }
  22. }
  23. throw new TypeError('Argument must be a SharedArrayBuffer, ArrayBuffer or ArrayBufferView')
  24. }
  25. // Warning: unlike whatwg-encoding, returns lowercased labels
  26. // Those are case-insensitive and that's how TextDecoder encoding getter normalizes them
  27. export function getBOMEncoding(input) {
  28. const u8 = fromSource(input) // asserts
  29. if (u8.length >= 3 && u8[0] === 0xef && u8[1] === 0xbb && u8[2] === 0xbf) return 'utf-8'
  30. if (u8.length < 2) return null
  31. if (u8[0] === 0xff && u8[1] === 0xfe) return 'utf-16le'
  32. if (u8[0] === 0xfe && u8[1] === 0xff) return 'utf-16be'
  33. return null
  34. }