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

fastify / busboy / 8923145258

02 May 2024 11:33AM UTC coverage: 96.157% (+0.8%) from 95.403%
8923145258

push

github

web-flow
More automated tests (#152)

* started writing a couple of automated tests to chip in, working towards the 100% test coverage goal

* added a couple more tests and eslint fixes

* added all the cases for the basename

* better test description

* added the plan for the subtest too in the dicer-write.test.js

* PR review: removed the t.end() in dicer-write.test.js

---------

Co-authored-by: Giovanni Bucci <giovanni.bucci@nearform.com>

496 of 530 branches covered (93.58%)

Branch coverage included in aggregate %.

780 of 797 relevant lines covered (97.87%)

158.43 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')
10✔
5
const textDecoders = new Map([
10✔
6
  ['utf-8', utf8Decoder],
7
  ['utf8', utf8Decoder]
8
])
9

10
function getDecoder (charset) {
11
  let lc
12
  while (true) {
394✔
13
    switch (charset) {
396✔
14
      case 'utf-8':
15
      case 'utf8':
16
        return decoders.utf8
377✔
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 = {
10✔
48
  utf8: (data, sourceEncoding) => {
49
    if (data.length === 0) {
377✔
50
      return ''
1✔
51
    }
52
    if (typeof data === 'string') {
376✔
53
      data = Buffer.from(data, sourceEncoding)
375✔
54
    }
55
    return data.utf8Slice(0, data.length)
376✔
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) {
402✔
109
    return getDecoder(destEncoding)(text, sourceEncoding)
394✔
110
  }
111
  return text
8✔
112
}
113

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

© 2026 Coveralls, Inc