-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (157 loc) · 4.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const {Transform, pipeline, PassThrough} = require('stream');
const got = require('got');
const iconv = require('iconv-lite');
const MIMEType = require('whatwg-mimetype');
const encodeMapper = require('whatwg-encoding-mapper');
const htmlEncodingSniffer = require('html-encoding-sniffer-xs');
const xmlEncodingSniffer = require('./xml-encoding-sniff');
const gotIconv = got.extend({
handlers: [
iconvConvert
]
});
function detecteFromBuffer(mime, buffer) {
let encodingDetected;
const charset = mime && mime.parameters.get('charset');
if (mime) {
if (mime.isHTML()) {
encodingDetected = htmlEncodingSniffer(buffer, {transportLayerEncodingLabel: charset});
} else if (mime.isXML()) {
encodingDetected = xmlEncodingSniffer(buffer);
}
}
if (!encodingDetected && charset) {
encodingDetected = encodeMapper.labelToName(charset);
}
return encodingDetected;
}
function convertStream(options, next) {
let mime;
const kbStream = new BufferKbStream();
kbStream
.once('kb', function (buffer) {
const encodingDetected = detecteFromBuffer(mime, buffer);
if (!encodingDetected && options._throwEncodingNotDetected === true) {
const error = new EncodingNotDetectedError('can not detecte any of encoding');
this.destroy(error);
}
const encoding = encodingDetected || 'utf8';
if (iconv.encodingExists(encodingDetected)) {
this.emit('encoding', encoding);
} else {
if (options._throwEncodingNotSupported) {
const error = new EncodingNotSupportError(`${encoding} not supported by iconv-lite`);
this.destroy(error);
}
this.emit('encoding', encoding);
}
});
const source = next(options);
source
.on('response', response => {
mime = response.headers['content-type'] && new MIMEType(response.headers['content-type']);
});
const r = new PassThrough();
const p1 = pipeline(source, kbStream, error => {
if (error) {
r.emit('error', error);
}
});
kbStream.once('encoding', enc => {
pipeline(p1, iconv.decodeStream(enc), r, error => {
if (error) {
r.emit('error', error);
}
});
});
return r;
}
class BufferKbStream extends Transform {
constructor() {
super();
this._data = [];
this._len = 0;
this._kbFlag = false;
}
_transform(chunk, encoding, callback) {
this._len += chunk.byteLength;
if (!this._kbFlag) {
this._data.push(chunk);
}
if (this._len >= 1024) {
this.emit('kb', Buffer.concat(this._data));
this._kbFlag = true;
}
callback(null, chunk);
}
_final(callback) {
if (!this._kbFlag) {
this.emit('kb', Buffer.concat(this._data));
}
callback();
}
}
class EncodingNotSupportError extends Error {
constructor(message) {
super(message);
this.name = 'EncodingNotSupportError';
}
}
class EncodingNotDetectedError extends Error {
constructor(message) {
super(message);
this.name = 'EncodingNotDetectedError';
}
}
function iconvConvert(options, next) {
if (options.responseType === 'buffer') {
return next(options);
}
if (options.isStream) {
return convertStream(options, next);
}
return (async () => {
if (options.resolveBodyOnly) {
options._resolveBodyOnly = true;
options.resolveBodyOnly = false;
}
if (options.responseType === 'json') {
options._jsonResponse = true;
options.responseType = 'buffer';
}
const resp = await next(options);
const buffer = resp.rawBody;
const mime = resp.headers['content-type'] && new MIMEType(resp.headers['content-type']);
const encodingDetected = detecteFromBuffer(mime, buffer);
if (!encodingDetected && options._throwEncodingNotDetected) {
return Promise.reject(new EncodingNotDetectedError('can not detecte any of encoding'));
}
const encoding = encodingDetected || 'utf8';
if (iconv.encodingExists(encoding)) {
resp.body = iconv.decode(buffer, encoding);
} else {
if (options._throwEncodingNotSupported) {
return Promise.reject(new EncodingNotSupportError(`${encoding} not supported by iconv-lite`));
}
resp.body = iconv.decode(buffer, 'utf8');
}
if (options._jsonResponse) {
options.responseType = 'json';
delete options._jsonResponse;
try {
resp.body = JSON.parse(resp.body);
if (options._resolveBodyOnly) {
return resp.body;
}
return resp;
} catch (error) {
return Promise.reject(new got.ParseError(error, resp, options));
}
}
return resp;
})();
}
module.exports = gotIconv;
module.exports.default = gotIconv;
module.exports.EncodingNotDetectedError = EncodingNotDetectedError;
module.exports.EncodingNotSupportError = EncodingNotSupportError;