index.js 793 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict'
  2. const textDecoder = new TextDecoder()
  3. /**
  4. * @see https://encoding.spec.whatwg.org/#utf-8-decode
  5. * @param {Uint8Array} buffer
  6. */
  7. function utf8DecodeBytes (buffer) {
  8. if (buffer.length === 0) {
  9. return ''
  10. }
  11. // 1. Let buffer be the result of peeking three bytes from
  12. // ioQueue, converted to a byte sequence.
  13. // 2. If buffer is 0xEF 0xBB 0xBF, then read three
  14. // bytes from ioQueue. (Do nothing with those bytes.)
  15. if (buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
  16. buffer = buffer.subarray(3)
  17. }
  18. // 3. Process a queue with an instance of UTF-8’s
  19. // decoder, ioQueue, output, and "replacement".
  20. const output = textDecoder.decode(buffer)
  21. // 4. Return output.
  22. return output
  23. }
  24. module.exports = {
  25. utf8DecodeBytes
  26. }