Skip to content

Commit

Permalink
cleanup (#76)
Browse files Browse the repository at this point in the history
* feat: support gzip sitemaps

* refactor: simplify code and change method name

* chore: cleanup

* chore: cleanup

* Adding comments

* auto generated

Co-authored-by: Jason Ibrahim <[email protected]>
Co-authored-by: Sean Thomas Burke <[email protected]>
  • Loading branch information
3 people authored Jan 13, 2021
1 parent 6a72c7a commit 68bf930
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"words": [
"sitemapper",
"esmodules"
"esmodules",
"gzipped"
],
"allowCompoundWords": true,
"flagWords": [],
Expand Down
2 changes: 1 addition & 1 deletion lib/assets/sitemapper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/assets/sitemapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ export default class Sitemapper {


/**
* /**
* Gets the sites from a sitemap.xml with a given URL
*
* @deprecated
* @param {string} url - url to query
* @param {getSitesCallback} callback - callback for sites and error
Expand All @@ -278,16 +278,28 @@ export default class Sitemapper {
return callback(err, sites);
}

/**
* Check to see if the url is a gzipped url
*
* @param {string} url - url to query
* @returns {Boolean}
*/
isGzip(url) {
const parsed = Url.parse(url);
const ext = path.extname(parsed.path);
return ext === '.gz';
}

/**
* Decompress the gzipped response body using zlib.gunzip
*
* @param {Buffer} body - body of the gzipped file
* @returns {Boolean}
*/
decompressResponseBody(body) {
return new Promise((resolve, reject) => {
const buffer = Buffer.from(body);
zlib.gunzip(buffer, function (err, result) {
zlib.gunzip(buffer, (err, result) => {
if (err) {
reject(err);
} else {
Expand Down
26 changes: 26 additions & 0 deletions src/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,32 @@ describe('Sitemapper', function () {
});
});

describe('gzipped sitemaps', function () {
beforeEach(() => {
sitemapper = new Sitemapper({
requestHeaders: {
'Accept-Encoding': 'gzip,deflate,sdch',
}
});
});

it('https://www.banggood.com/sitemap/products-Toys-Hobbies-and-Robot-5-hu-HU.xml.gz gzip should be a non-empty array', function (done) {
this.timeout(30000);
const url = 'https://www.banggood.com/sitemap/products-Toys-Hobbies-and-Robot-5-hu-HU.xml.gz';
sitemapper.timeout = 10000;
sitemapper.fetch(url)
.then(data => {
data.sites.should.be.Array;
data.sites.length.should.be.greaterThan(0);
done();
})
.catch(error => {
console.error('Test failed');
done(error);
});
});
});

describe('getSites method', function () {
it('getSites should be backwards compatible', function (done) {
this.timeout(30000);
Expand Down

0 comments on commit 68bf930

Please sign in to comment.