sitemap-stream
Advanced tools
Comparing version 1.6.1 to 2.0.0
@@ -0,1 +1,8 @@ | ||
## [2.0.0] - 2016-05-02 | ||
### Fixed | ||
- Fix sitemap url content | ||
### Changed | ||
- The variable that refers the sitemaps output path | ||
## [1.6.1] - 2016-02-12 | ||
@@ -2,0 +9,0 @@ ### Fixed |
16
index.js
'use strict'; | ||
const fs = require('fs'); | ||
const url = require('url'); | ||
const EventEmitter = require('events').EventEmitter; | ||
@@ -15,3 +16,3 @@ const zlib = require('zlib'); | ||
this.hostname = conf.hostname; | ||
this.sitemapDirectoryUrl = conf.sitemapDirectoryUrl; | ||
this.date = conf.date; | ||
@@ -94,3 +95,3 @@ this.limit = conf.limit; | ||
const loc = `<loc>${this.hostname}${entry.url}</loc>\n`; | ||
const loc = `<loc>${entry.url}</loc>\n`; | ||
const lastMod = `<lastmod>${this.date}</lastmod>\n`; | ||
@@ -101,5 +102,5 @@ const changeFreq = entry.changeFreq ? `<changefreq>${entry.changeFreq}</changefreq>\n` : ''; | ||
const isWritten = this.writer.write(`${loc}${lastMod}${changeFreq}${priority}${mobile}`); | ||
const isWritten = this.writer.write(`<url>\n${loc}${lastMod}${changeFreq}${priority}${mobile}</url>\n`); | ||
if (isWritten) this.nbInjectedUrls++; | ||
this.nbInjectedUrls++; | ||
@@ -145,4 +146,9 @@ return isWritten; | ||
const nbSitemaps = (this.nbInjectedUrls / this.limit) + 1; | ||
for (let i=1; i<=nbSitemaps; i++) this.writer.write(`<sitemap>\n<loc>http://www.example.com/sitemap-${i}.xml</loc>\n<lastmod>${this.date}</lastmod>\n</sitemap>\n`); | ||
for (let i=1; i<=nbSitemaps; i++) { | ||
const ext = this.toCompress ? 'xml.gz' : 'xml'; | ||
const loc = url.resolve(this.sitemapDirectoryUrl, `sitemap-${i}.${ext}`); | ||
this.writer.write(`<sitemap>\n<loc>${loc}</loc>\n<lastmod>${this.date}</lastmod>\n</sitemap>\n`); | ||
} | ||
this.writer.end(); | ||
@@ -149,0 +155,0 @@ } |
{ | ||
"name": "sitemap-stream", | ||
"version": "1.6.1", | ||
"version": "2.0.0", | ||
"description": "Simple and fast sitemap stream that respects google rules", | ||
@@ -32,2 +32,3 @@ "main": "index.js", | ||
"mocha": "^2.3.4", | ||
"readable-stream": "^2.0.5", | ||
"sinon": "^1.17.2" | ||
@@ -34,0 +35,0 @@ }, |
@@ -22,3 +22,3 @@ [](https://travis-ci.org/ludoblues/sitemap-stream) | ||
{ | ||
hostname: String, | ||
sitemapDirectoryUrl: String, | ||
limit: Number, // (default: 50000) | ||
@@ -25,0 +25,0 @@ isMobile: Boolean, // (default: false) |
@@ -7,3 +7,3 @@ 'use strict'; | ||
config: Joi.object({ | ||
hostname: Joi.string().hostname().default(''), | ||
sitemapDirectoryUrl: Joi.string().uri().default(''), | ||
date: Joi.date().iso().default(new Date().toISOString()), | ||
@@ -10,0 +10,0 @@ limit: Joi.number().integer().min(1).default(50000), |
73
test.js
@@ -6,2 +6,3 @@ 'use strict'; | ||
const zlib = require('zlib'); | ||
const WritableStream = require('readable-stream').Writable; | ||
@@ -420,3 +421,3 @@ const expect = require('chai').expect; | ||
expect(fileContent.toString()).to.be.equal(`<loc>/some-path</loc>\n<lastmod>${now}</lastmod>\n`); | ||
expect(fileContent.toString()).to.be.equal(`<url>\n<loc>/some-path</loc>\n<lastmod>${now}</lastmod>\n</url>\n`); | ||
@@ -472,3 +473,3 @@ done(); | ||
expect(fileContent.toString()).to.be.equal(`<loc>/some-path</loc>\n<lastmod>${now}</lastmod>\n<changefreq>monthly</changefreq>\n<priority>0.9</priority>\n<mobile:mobile/>\n`); | ||
expect(fileContent.toString()).to.be.equal(`<url>\n<loc>/some-path</loc>\n<lastmod>${now}</lastmod>\n<changefreq>monthly</changefreq>\n<priority>0.9</priority>\n<mobile:mobile/>\n</url>\n`); | ||
@@ -591,2 +592,40 @@ done(); | ||
}); | ||
context('We inject after high watermark reached', () => { | ||
const limit = 20; | ||
const highWaterMark = 16; | ||
let sitemap; | ||
beforeEach('generate a new sitemap generator', () => { | ||
sitemap = require('./index')({ limit }); | ||
}); | ||
beforeEach('stub #changeWriteStream and spy on #writer', () => { | ||
sinon.stub(sitemap, 'changeWriteStream', () => { | ||
sitemap.writer = new WritableStream({ | ||
objectMode: true, | ||
highWaterMark, | ||
write: (chunk, encoding, callback) => { | ||
setTimeout(() => callback(), 0); | ||
} | ||
}); | ||
sinon.spy(sitemap.writer, 'write'); | ||
}); | ||
}); | ||
it('should create new sitemap writer even when some entries was buffered', () => { | ||
expect(sitemap.changeWriteStream.callCount).to.be.equal(0); | ||
for (let i = 1; i <= (highWaterMark * 2); i++) { | ||
const buffered = i >= highWaterMark && i <= limit; | ||
let written = sitemap.inject('http://example.com/path'); | ||
expect(written).to.equal(!buffered); | ||
} | ||
expect(sitemap.changeWriteStream.callCount).to.be.equal(2); | ||
expect(sitemap.writer.write.callCount).to.be.equal(12); | ||
}); | ||
}); | ||
}); | ||
@@ -598,3 +637,5 @@ | ||
beforeEach('generate a new sitemap generator', () => { | ||
sitemap = require('./index')(); | ||
sitemap = require('./index')({ | ||
sitemapDirectoryUrl: 'http://www.example.com' | ||
}); | ||
}); | ||
@@ -617,20 +658,22 @@ | ||
it('should emit the stream error events', done => { | ||
sitemap.on('error', err => { | ||
expect(err).to.be.equal('broadcast the error'); | ||
done(); | ||
}); | ||
sitemap.generateIndexFile(); | ||
sitemap.on('done', () => { | ||
sitemap.on('error', err => { | ||
expect(err).to.be.equal('broadcast the error'); | ||
done(); | ||
}); | ||
sitemap.writer.emit('error', 'broadcast the error'); | ||
sitemap.writer.emit('error', 'broadcast the error'); | ||
}); | ||
}); | ||
it('should emit the stream drain events', done => { | ||
sitemap.on('drain', err => { | ||
done(); | ||
}); | ||
sitemap.generateIndexFile(); | ||
sitemap.on('done', () => { | ||
sitemap.on('drain', err => { | ||
done(); | ||
}); | ||
sitemap.writer.emit('drain'); | ||
sitemap.writer.emit('drain'); | ||
}); | ||
}); | ||
@@ -717,3 +760,3 @@ | ||
expect(fileContent.toString()).to.be.equal(`<?xml version="1.0" encoding="UTF-8"?>\n<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n<sitemap>\n<loc>http://www.example.com/sitemap-1.xml</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n<sitemap>\n<loc>http://www.example.com/sitemap-2.xml</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n<sitemap>\n<loc>http://www.example.com/sitemap-3.xml</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n<sitemap>\n<loc>http://www.example.com/sitemap-4.xml</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n<sitemap>\n<loc>http://www.example.com/sitemap-5.xml</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n`); | ||
expect(fileContent.toString()).to.be.equal(`<?xml version="1.0" encoding="UTF-8"?>\n<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n<sitemap>\n<loc>http://www.example.com/sitemap-1.xml.gz</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n<sitemap>\n<loc>http://www.example.com/sitemap-2.xml.gz</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n<sitemap>\n<loc>http://www.example.com/sitemap-3.xml.gz</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n<sitemap>\n<loc>http://www.example.com/sitemap-4.xml.gz</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n<sitemap>\n<loc>http://www.example.com/sitemap-5.xml.gz</loc>\n<lastmod>${sitemap.date}</lastmod>\n</sitemap>\n`); | ||
@@ -720,0 +763,0 @@ done(); |
39807
804
6