@exodus/bytes 1.0.0-rc.8 → 1.0.0-rc.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utf8.node.js CHANGED
@@ -5,16 +5,17 @@ import { isAscii } from 'node:buffer'
5
5
 
6
6
  if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
7
7
 
8
- const decoderFatal = new TextDecoder('utf8', { ignoreBOM: true, fatal: true })
9
- const decoderLoose = new TextDecoder('utf8', { ignoreBOM: true })
8
+ const decoderFatal = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true })
9
+ const decoderLoose = new TextDecoder('utf-8', { ignoreBOM: true })
10
10
  const { isWellFormed } = String.prototype
11
+ const isDeno = Boolean(globalThis.Deno)
11
12
 
12
13
  function encode(str, loose = false) {
13
14
  if (typeof str !== 'string') throw new TypeError('Input is not a string')
14
15
  const strLength = str.length
15
16
  if (strLength === 0) return new Uint8Array() // faster than Uint8Array.of
16
17
  let res
17
- if (strLength > 0x4_00) {
18
+ if (strLength > 0x4_00 && !isDeno) {
18
19
  // Faster for large strings
19
20
  const byteLength = Buffer.byteLength(str)
20
21
  res = Buffer.allocUnsafe(byteLength)
@@ -35,11 +36,13 @@ function decode(arr, loose = false) {
35
36
  assertUint8(arr)
36
37
  const byteLength = arr.byteLength
37
38
  if (byteLength === 0) return ''
38
- if (byteLength > 0x6_00 && isAscii(arr)) {
39
+ if (byteLength > 0x6_00 && !(isDeno && loose) && isAscii(arr)) {
39
40
  // On non-ascii strings, this loses ~10% * [relative position of the first non-ascii byte] (up to 10% total)
40
41
  // On ascii strings, this wins 1.5x on loose = false and 1.3x on loose = true
41
42
  // Only makes sense for large enough strings
42
- return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength).latin1Slice(0, arr.byteLength) // .latin1Slice is faster than .asciiSlice
43
+ const buf = Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)
44
+ if (isDeno) return buf.toString() // Deno suffers from .latin1Slice
45
+ return buf.latin1Slice(0, arr.byteLength) // .latin1Slice is faster than .asciiSlice
43
46
  }
44
47
 
45
48
  return loose ? decoderLoose.decode(arr) : decoderFatal.decode(arr)