• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fastify / busboy / 8076850476

28 Feb 2024 07:35AM UTC coverage: 95.407% (+0.01%) from 95.393%
8076850476

push

github

web-flow
fix: dont use internal _events attribute (#146)

487 of 530 branches covered (91.89%)

Branch coverage included in aggregate %.

780 of 798 relevant lines covered (97.74%)

156.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.62
/lib/utils/decodeText.js
1
'use strict'
2

3
// Node has always utf-8
4
const utf8Decoder = new TextDecoder('utf-8')
7✔
5
const textDecoders = new Map([
7✔
6
  ['utf-8', utf8Decoder],
7
  ['utf8', utf8Decoder]
8
])
9

10
function getDecoder (charset) {
11
  let lc
12
  while (true) {
392✔
13
    switch (charset) {
394✔
14
      case 'utf-8':
15
      case 'utf8':
16
        return decoders.utf8
375✔
17
      case 'latin1':
18
      case 'ascii': // TODO: Make these a separate, strict decoder?
19
      case 'us-ascii':
20
      case 'iso-8859-1':
21
      case 'iso8859-1':
22
      case 'iso88591':
23
      case 'iso_8859-1':
24
      case 'windows-1252':
25
      case 'iso_8859-1:1987':
26
      case 'cp1252':
27
      case 'x-cp1252':
28
        return decoders.latin1
7✔
29
      case 'utf16le':
30
      case 'utf-16le':
31
      case 'ucs2':
32
      case 'ucs-2':
33
        return decoders.utf16le
4✔
34
      case 'base64':
35
        return decoders.base64
4✔
36
      default:
37
        if (lc === undefined) {
4✔
38
          lc = true
2✔
39
          charset = charset.toLowerCase()
2✔
40
          continue
2✔
41
        }
42
        return decoders.other.bind(charset)
2✔
43
    }
44
  }
45
}
46

47
const decoders = {
7✔
48
  utf8: (data, sourceEncoding) => {
49
    if (data.length === 0) {
375✔
50
      return ''
1✔
51
    }
52
    if (typeof data === 'string') {
374✔
53
      data = Buffer.from(data, sourceEncoding)
373✔
54
    }
55
    return data.utf8Slice(0, data.length)
374✔
56
  },
57

58
  latin1: (data, sourceEncoding) => {
59
    if (data.length === 0) {
7✔
60
      return ''
1✔
61
    }
62
    if (typeof data === 'string') {
6✔
63
      return data
5✔
64
    }
65
    return data.latin1Slice(0, data.length)
1✔
66
  },
67

68
  utf16le: (data, sourceEncoding) => {
69
    if (data.length === 0) {
4✔
70
      return ''
1✔
71
    }
72
    if (typeof data === 'string') {
3✔
73
      data = Buffer.from(data, sourceEncoding)
1✔
74
    }
75
    return data.ucs2Slice(0, data.length)
3✔
76
  },
77

78
  base64: (data, sourceEncoding) => {
79
    if (data.length === 0) {
4✔
80
      return ''
1✔
81
    }
82
    if (typeof data === 'string') {
3✔
83
      data = Buffer.from(data, sourceEncoding)
2✔
84
    }
85
    return data.base64Slice(0, data.length)
3✔
86
  },
87

88
  other: (data, sourceEncoding) => {
89
    if (data.length === 0) {
2✔
90
      return ''
1✔
91
    }
92
    if (typeof data === 'string') {
1!
93
      data = Buffer.from(data, sourceEncoding)
1✔
94
    }
95

96
    if (textDecoders.has(this.toString())) {
1!
97
      try {
×
98
        return textDecoders.get(this).decode(data)
×
99
      } catch {}
100
    }
101
    return typeof data === 'string'
1!
102
      ? data
103
      : data.toString()
104
  }
105
}
106

107
function decodeText (text, sourceEncoding, destEncoding) {
108
  if (text) {
400✔
109
    return getDecoder(destEncoding)(text, sourceEncoding)
392✔
110
  }
111
  return text
8✔
112
}
113

114
module.exports = decodeText
7✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc