Skip to content

Commit

Permalink
remove text-decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Aug 13, 2023
1 parent 9261ecb commit 8b5d18d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 43 deletions.
46 changes: 6 additions & 40 deletions lib/utils/decodeText.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
'use strict'

let TextDecoder
try {
TextDecoder = require('util').TextDecoder
} catch (e) { }

const { TextDecoder: PolyfillTextDecoder, getEncoding } = require('text-decoding')

// Node has always utf-8
const textDecoders = new Map()
if (TextDecoder) {
textDecoders.set('utf-8', new TextDecoder('utf-8'))
} else {
textDecoders.set('utf-8', new PolyfillTextDecoder('utf-8'))
}
textDecoders.set('utf8', textDecoders.get('utf-8'))
const utf8Decoder = new TextDecoder('utf-8')
const textDecoders = new Map([
['utf-8', utf8Decoder],
['utf8', utf8Decoder]
])

function decodeText (text, textEncoding, destEncoding) {
if (text) {
Expand All @@ -26,35 +17,10 @@ function decodeText (text, textEncoding, destEncoding) {
try {
textDecoders.set(destEncoding, new TextDecoder(destEncoding))
return textDecoders.get(destEncoding).decode(Buffer.from(text, textEncoding))
} catch (e) {
if (getEncoding(destEncoding)) {
try {
textDecoders.set(destEncoding, new PolyfillTextDecoder(destEncoding))
return textDecoders.get(destEncoding).decode(Buffer.from(text, textEncoding))
} catch (e) { }
}
}
}
}
return text
}

function decodeTextPolyfill (text, textEncoding, destEncoding) {
if (text) {
if (textDecoders.has(destEncoding)) {
try {
return textDecoders.get(destEncoding).decode(Buffer.from(text, textEncoding))
} catch (e) { }
} else {
if (getEncoding(destEncoding)) {
try {
textDecoders.set(destEncoding, new PolyfillTextDecoder(destEncoding))
return textDecoders.get(destEncoding).decode(Buffer.from(text, textEncoding))
} catch (e) { }
}
}
}
return text
}

module.exports = TextDecoder ? decodeText : decodeTextPolyfill
module.exports = decodeText
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
"engines": {
"node": ">=14"
},
"dependencies": {
"text-decoding": "^1.0.0"
},
"devDependencies": {
"@types/node": "^20.1.0",
"busboy": "^1.0.0",
Expand Down

0 comments on commit 8b5d18d

Please sign in to comment.