UNPKG

3.62 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.SitemapFile = undefined;
7
8var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9
10var _xml_escape = require('./xml_escape');
11
12var _config = require('./config');
13
14function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
16function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
17
18function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
19
20var Transform = require('stream').Transform;
21var fs = require('fs');
22var zlib = require('zlib');
23
24var SitemapFile = exports.SitemapFile = function (_Transform) {
25 _inherits(SitemapFile, _Transform);
26
27 function SitemapFile(_ref) {
28 var location = _ref.location;
29 var fileName = _ref.fileName;
30
31 _classCallCheck(this, SitemapFile);
32
33 var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(SitemapFile).call(this, { objectMode: true }));
34
35 _this.location = location;
36 _this.fileName = fileName;
37 _this.gzipDone = false;
38 _this.fileDone = false;
39 _this.urlCount = 0;
40 return _this;
41 }
42
43 _createClass(SitemapFile, [{
44 key: 'open',
45 value: function open() {
46 var _this2 = this;
47
48 _config.config.log.trace('Opening sitemap ' + this.fileName);
49 this.gzipper = zlib.createGzip().on('finish', function () {
50 _this2.gzipDone = true;
51 });
52 this.file = fs.createWriteStream(this.fileName).on('finish', function () {
53 _this2.fileDone = true;
54 });
55 this.output = this.pipe(this.gzipper).pipe(this.file).on('error', function (err) {
56 _this2.emit("error", err);
57 });
58 this.gzipper.write(_config.config.sitemapHeader);
59 }
60 }, {
61 key: '_transform',
62 value: function _transform(url, encoding, callback) {
63 if (typeof url === "string") {
64 this.push(url);
65 } else {
66 this.push(url.toXml());
67 this.urlCount += 1;
68 }
69 callback();
70 }
71 }, {
72 key: 'toIndexXml',
73 value: function toIndexXml() {
74 return '<sitemap><loc>' + (0, _xml_escape.escapeXmlValue)(this.location) + '</loc><lastmod>' + new Date().toISOString() + '</lastmod></sitemap>';
75 }
76 }, {
77 key: 'isDrained',
78 value: function isDrained() {
79 return this.gzipDone && this.fileDone;
80 }
81 }, {
82 key: 'close',
83 value: function close() {
84 this.write("</urlset>");
85 this.end();
86 }
87 }]);
88
89 return SitemapFile;
90}(Transform);
\No newline at end of file