multi-byte.js 711 B

12345678910111213141516171819
  1. import { assertU8 } from './fallback/_utils.js'
  2. import { multibyteDecoder, multibyteEncoder } from './fallback/multi-byte.js'
  3. export function createMultibyteDecoder(encoding, loose = false) {
  4. const jsDecoder = multibyteDecoder(encoding, loose) // asserts
  5. let streaming = false
  6. return (arr, stream = false) => {
  7. assertU8(arr)
  8. if (!streaming && arr.byteLength === 0) return ''
  9. streaming = stream
  10. return jsDecoder(arr, stream)
  11. }
  12. }
  13. export function createMultibyteEncoder(encoding, { mode = 'fatal' } = {}) {
  14. // TODO: replacement, truncate (replacement will need varying length)
  15. if (mode !== 'fatal') throw new Error('Unsupported mode')
  16. return multibyteEncoder(encoding) // asserts
  17. }