Closed wubinhong closed 8 months ago
In the tutorial of Crawler
I cannot assign a null value to CrawlerRequestOptions, when following the tutorial as below:
null
CrawlerRequestOptions
encoding defined in @types/crawler
encoding
@types/crawler
interface CrawlerRequestOptions { encoding?: string | undefined; }
The implementation for encoding in file lib/crawler.js
lib/crawler.js
Change the encoding declaration in @types/crawler as below
interface CrawlerRequestOptions { encoding?: string | undefined | null; }
Change the implementation of handling encoding as below:
Crawler.prototype._doEncoding = function (options, response) { var self = this; if (!options.encoding) { return; } if (options.forceUTF8) { var charset = options.incomingEncoding || self._parseCharset(response); response.charset = charset; log('debug', 'Charset ' + charset); if (charset !== 'utf-8' && charset !== 'ascii') {// convert response.body into 'utf-8' encoded buffer response.body = iconvLite.decode(response.body, charset); } } response.body = response.body.toString(); };
Hi guy, I think this should be a bug of type inference in typescript. I don't like typescript, also suggest you no to use it.
typescript
Issue Description
In the tutorial of Crawler
Trouble-shooting
encoding
defined in@types/crawler
The implementation for
encoding
in filelib/crawler.js
Suggestion
Solution 1 (Recommended)
Change the
encoding
declaration in@types/crawler
as belowSolution 2
Change the implementation of handling encoding as below: