summaryrefslogtreecommitdiff
path: root/node_modules/needle/lib/decoder.js
diff options
context:
space:
mode:
author8cy <[email protected]>2020-04-03 02:37:42 -0700
committer8cy <[email protected]>2020-04-03 02:37:42 -0700
commit60867fb030bae582082340ead7dbc7efdc2f5398 (patch)
tree4c6a7356351be2e4914e15c4703172597c45656e /node_modules/needle/lib/decoder.js
parentcommenting (diff)
downloads5nical-60867fb030bae582082340ead7dbc7efdc2f5398.tar.xz
s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.zip
2020/04/03, 02:34, v1.2.0
Diffstat (limited to 'node_modules/needle/lib/decoder.js')
-rw-r--r--node_modules/needle/lib/decoder.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/node_modules/needle/lib/decoder.js b/node_modules/needle/lib/decoder.js
new file mode 100644
index 0000000..d82bb5a
--- /dev/null
+++ b/node_modules/needle/lib/decoder.js
@@ -0,0 +1,53 @@
+var iconv,
+ inherits = require('util').inherits,
+ stream = require('stream');
+
+var regex = /(?:charset|encoding)\s*=\s*['"]? *([\w\-]+)/i;
+
+inherits(StreamDecoder, stream.Transform);
+
+function StreamDecoder(charset) {
+ if (!(this instanceof StreamDecoder))
+ return new StreamDecoder(charset);
+
+ stream.Transform.call(this, charset);
+ this.charset = charset;
+ this.parsed_chunk = false;
+}
+
+StreamDecoder.prototype._transform = function(chunk, encoding, done) {
+ var res, found;
+
+ // try get charset from chunk, just once
+ if (this.charset == 'iso-8859-1' && !this.parsed_chunk) {
+ this.parsed_chunk = true;
+
+ var matches = regex.exec(chunk.toString());
+ if (matches) {
+ found = matches[1].toLowerCase();
+ this.charset = found == 'utf-8' ? 'utf8' : found;
+ }
+ }
+
+ try {
+ res = iconv.decode(chunk, this.charset);
+ } catch(e) { // something went wrong, just return original chunk
+ res = chunk;
+ }
+
+ this.push(res);
+ done();
+}
+
+module.exports = function(charset) {
+ try {
+ if (!iconv) iconv = require('iconv-lite');
+ } catch(e) {
+ /* iconv not found */
+ }
+
+ if (iconv)
+ return new StreamDecoder(charset);
+ else
+ return new stream.PassThrough;
+}