multi-byte.node.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. import { assertU8, toBuf } from './fallback/_utils.js'
  2. import { isDeno } from './fallback/platform.js'
  3. import { isAsciiSuperset, multibyteDecoder, multibyteEncoder } from './fallback/multi-byte.js'
  4. import { isAscii } from 'node:buffer'
  5. export function createMultibyteDecoder(encoding, loose = false) {
  6. const jsDecoder = multibyteDecoder(encoding, loose) // asserts
  7. let streaming = false
  8. const asciiSuperset = isAsciiSuperset(encoding)
  9. return (arr, stream = false) => {
  10. assertU8(arr)
  11. if (!streaming) {
  12. if (arr.byteLength === 0) return ''
  13. if (asciiSuperset && isAscii(arr)) {
  14. if (isDeno) return toBuf(arr).toString()
  15. return toBuf(arr).latin1Slice(0, arr.byteLength) // .latin1Slice is faster than .asciiSlice
  16. }
  17. }
  18. streaming = stream
  19. return jsDecoder(arr, stream)
  20. }
  21. }
  22. export function createMultibyteEncoder(encoding, { mode = 'fatal' } = {}) {
  23. // TODO: replacement, truncate (replacement will need varying length)
  24. if (mode !== 'fatal') throw new Error('Unsupported mode')
  25. return multibyteEncoder(encoding) // asserts
  26. }