rss-parser
Advanced tools
Comparing version 3.5.4 to 3.6.0
@@ -24,31 +24,32 @@ import { Options } from 'xml2js'; | ||
interface Items | ||
{ | ||
readonly link: string; | ||
readonly guid: string; | ||
readonly title: string; | ||
readonly pubDate: string; | ||
readonly creator: string; | ||
readonly content: string; | ||
readonly isoDate: string; | ||
readonly categories: string[]; | ||
readonly contentSnippet: string; | ||
interface Items { | ||
[key: string]: any; | ||
link?: string; | ||
guid?: string; | ||
title?: string; | ||
pubDate?: string; | ||
creator?: string; | ||
content?: string; | ||
isoDate?: string; | ||
categories?: string[]; | ||
contentSnippet?: string; | ||
} | ||
interface Output | ||
{ | ||
readonly link: string; | ||
readonly title: string; | ||
readonly items: Items[]; | ||
readonly feedUrl: string; | ||
readonly description: string; | ||
readonly itunes: { | ||
image: string; | ||
owner: { | ||
name: string; | ||
email: string; | ||
interface Output { | ||
[key: string]: any; | ||
link?: string; | ||
title?: string; | ||
items?: Items[]; | ||
feedUrl?: string; | ||
description?: string; | ||
itunes?: { | ||
[key: string]: any; | ||
image?: string; | ||
owner?: { | ||
name?: string; | ||
email?: string; | ||
}; | ||
author: string; | ||
summary: string; | ||
explicit: string; | ||
author?: string; | ||
summary?: string; | ||
explicit?: string; | ||
}; | ||
@@ -65,10 +66,10 @@ } | ||
new(options?: ParserOptions): { | ||
/** | ||
* Parse XML content to JSON. | ||
* | ||
* @param xml - The xml to be parsed. | ||
* @param callback - Traditional callback. | ||
* | ||
* @returns Promise that has the same Output as the callback. | ||
*/ | ||
/** | ||
* Parse XML content to JSON. | ||
* | ||
* @param xml - The xml to be parsed. | ||
* @param callback - Traditional callback. | ||
* | ||
* @returns Promise that has the same Output as the callback. | ||
*/ | ||
parseString(xml: string, callback?: (err: Error, feed: Output) => void): Promise<Output>; | ||
@@ -75,0 +76,0 @@ |
@@ -15,2 +15,3 @@ "use strict"; | ||
const DEFAULT_MAX_REDIRECTS = 5; | ||
const DEFAULT_TIMEOUT = 60000; | ||
@@ -25,2 +26,3 @@ class Parser { | ||
if (!options.maxRedirects) options.maxRedirects = DEFAULT_MAX_REDIRECTS; | ||
if (!options.timeout) options.timeout = DEFAULT_TIMEOUT; | ||
this.options = options; | ||
@@ -102,2 +104,5 @@ this.xmlParser = new xml2js.Parser(this.options.xml2js); | ||
}) | ||
req.setTimeout(this.options.timeout, () => { | ||
return reject(new Error("Request timed out after " + this.options.timeout + "ms")); | ||
}); | ||
req.on('error', reject); | ||
@@ -104,0 +109,0 @@ }); |
{ | ||
"name": "rss-parser", | ||
"version": "3.5.4", | ||
"version": "3.6.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
@@ -108,3 +108,3 @@ # rss-parser | ||
## Options | ||
## XML Options | ||
@@ -180,2 +180,13 @@ ### Custom Fields | ||
## HTTP Options | ||
### Timeout | ||
You can set the amount of time (in milliseconds) to wait before the HTTP request times out (default 60 seconds): | ||
```js | ||
let parser = new Parser({ | ||
timeout: 1000, | ||
}); | ||
``` | ||
### Headers | ||
@@ -182,0 +193,0 @@ You can pass headers to the HTTP request: |
@@ -202,3 +202,20 @@ "use strict"; | ||
}) | ||
}) | ||
}); | ||
it('should respect timeout option', function(done) { | ||
var INPUT_FILE = __dirname + '/input/encoding.rss'; | ||
var OUTPUT_FILE = __dirname + '/output/encoding.json'; | ||
var ENCODING = 'latin1'; | ||
var server = HTTP.createServer(function(req, res) {}); | ||
server.listen(function() { | ||
var port = server.address().port; | ||
var url = 'http://localhost:' + port; | ||
var parser = new Parser({timeout: 1}); | ||
parser.parseURL(url, function(err, parsed) { | ||
Expect(err).to.not.equal(null); | ||
Expect(err.message).to.equal("Request timed out after 1ms"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}) |
4288711
25835
229