Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren authored Feb 6, 2024
2 parents e8007ef + e00b7d1 commit e320db7
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Parser {
if (!options.timeout) options.timeout = DEFAULT_TIMEOUT;
this.options = options;
this.xmlParser = new xml2js.Parser(this.options.xml2js);
this.etags = {};
this.lastModified = {};
}

parseString(xml, callback) {
Expand Down Expand Up @@ -73,6 +75,12 @@ class Parser {
let get = feedUrl.indexOf('https') === 0 ? https.get : http.get;
let urlParts = url.parse(feedUrl);
let headers = Object.assign({}, DEFAULT_HEADERS, this.options.headers);
if (this.etags[feedUrl]) {
headers['If-None-Match'] = this.etags[feedUrl];
}
if (this.lastModified[feedUrl]) {
headers['If-Modified-Since'] = this.lastModified[feedUrl];
}
let timeout = null;
let prom = new Promise((resolve, reject) => {
const requestOpts = Object.assign({headers}, urlParts, this.options.requestOptions);
Expand All @@ -84,9 +92,21 @@ class Parser {
const newLocation = url.resolve(feedUrl, res.headers['location']);
return this.parseURL(newLocation, null, redirectCount + 1).then(resolve, reject);
}
} else if (res.statusCode >= 300) {
return reject(new Error("Status code " + res.statusCode))
} else if (res.statusCode === 304) {
return resolve(null);
}
else if (res.statusCode >= 300) {
return reject(new Error("Status code " + res.statusCode));
}

if (res.headers['etag']) {
this.etags[feedUrl] = res.headers['etag'];
}

if (res.headers['last-modified']) {
this.lastModified[feedUrl] = res.headers['last-modified'];
}

let encoding = utils.getEncodingFromContentType(res.headers['content-type']);
res.setEncoding(encoding);
res.on('data', (chunk) => {
Expand Down Expand Up @@ -226,7 +246,7 @@ class Parser {
}
if (xmlItem.category) item.categories = xmlItem.category;

var mediaContent = xmlItem['media:content'][0].$;
var mediaContent = xmlItem['media:content']?.[0]?.$ ?? null;
if(mediaContent) item.mediaContent = mediaContent;

this.setISODate(item);
Expand Down

0 comments on commit e320db7

Please sign in to comment.