source: trip-planner-front/node_modules/needle/lib/decoder.js@ 84d0fbb

Last change on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.2 KB
Line 
1var iconv,
2 inherits = require('util').inherits,
3 stream = require('stream');
4
5var regex = /(?:charset|encoding)\s*=\s*['"]? *([\w\-]+)/i;
6
7inherits(StreamDecoder, stream.Transform);
8
9function StreamDecoder(charset) {
10 if (!(this instanceof StreamDecoder))
11 return new StreamDecoder(charset);
12
13 stream.Transform.call(this, charset);
14 this.charset = charset;
15 this.parsed_chunk = false;
16}
17
18StreamDecoder.prototype._transform = function(chunk, encoding, done) {
19 var res, found;
20
21 // try get charset from chunk, just once
22 if (this.charset == 'utf8' && !this.parsed_chunk) {
23 this.parsed_chunk = true;
24
25 var matches = regex.exec(chunk.toString());
26 if (matches) {
27 found = matches[1].toLowerCase();
28 this.charset = found == 'utf-8' ? 'utf8' : found;
29 }
30 }
31
32 try {
33 res = iconv.decode(chunk, this.charset);
34 } catch(e) { // something went wrong, just return original chunk
35 res = chunk;
36 }
37
38 this.push(res);
39 done();
40}
41
42module.exports = function(charset) {
43 try {
44 if (!iconv) iconv = require('iconv-lite');
45 } catch(e) {
46 /* iconv not found */
47 }
48
49 if (iconv)
50 return new StreamDecoder(charset);
51 else
52 return new stream.PassThrough;
53}
Note: See TracBrowser for help on using the repository browser.