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

fastify / busboy / 6467288002

10 Oct 2023 09:19AM UTC coverage: 91.314% (-4.2%) from 95.484%
6467288002

push

github

web-flow
parseParams and decodeText (#109)

459 of 530 branches covered (0.0%)

Branch coverage included in aggregate %.

59 of 59 new or added lines in 2 files covered. (100.0%)

750 of 794 relevant lines covered (94.46%)

157.09 hits per line

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

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

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

10
function getDecoder (charset) {
11
  let lc
12
  while (true) {
377✔
13
    switch (charset) {
377!
14
      case 'utf-8':
15
      case 'utf8':
16
        return decoders.utf8
372✔
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
5✔
29
      case 'utf16le':
30
      case 'utf-16le':
31
      case 'ucs2':
32
      case 'ucs-2':
33
        return decoders.utf16le
×
34
      case 'base64':
35
        return decoders.base64
×
36
      default:
37
        if (lc === undefined) {
×
38
          lc = true
×
39
          charset = charset.toLowerCase()
×
40
          continue
×
41
        }
42
        return decoders.other.bind(charset)
×
43
    }
44
  }
45
}
46

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

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

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

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

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

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

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

114
module.exports = decodeText
6✔
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