1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.SitemapAndIndexStream = exports.SitemapIndexStream = exports.IndexTagNames = void 0;
|
4 | const stream_1 = require("stream");
|
5 | const types_1 = require("./types");
|
6 | const sitemap_stream_1 = require("./sitemap-stream");
|
7 | const sitemap_xml_1 = require("./sitemap-xml");
|
8 | var IndexTagNames;
|
9 | (function (IndexTagNames) {
|
10 | IndexTagNames["sitemap"] = "sitemap";
|
11 | IndexTagNames["loc"] = "loc";
|
12 | IndexTagNames["lastmod"] = "lastmod";
|
13 | })(IndexTagNames = exports.IndexTagNames || (exports.IndexTagNames = {}));
|
14 | const xmlDec = '<?xml version="1.0" encoding="UTF-8"?>';
|
15 | const sitemapIndexTagStart = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
|
16 | const closetag = '</sitemapindex>';
|
17 | const defaultStreamOpts = {};
|
18 | class SitemapIndexStream extends stream_1.Transform {
|
19 | constructor(opts = defaultStreamOpts) {
|
20 | var _a;
|
21 | opts.objectMode = true;
|
22 | super(opts);
|
23 | this.hasHeadOutput = false;
|
24 | this.level = (_a = opts.level) !== null && _a !== void 0 ? _a : types_1.ErrorLevel.WARN;
|
25 | this.xslUrl = opts.xslUrl;
|
26 | }
|
27 | _transform(item, encoding, callback) {
|
28 | if (!this.hasHeadOutput) {
|
29 | this.hasHeadOutput = true;
|
30 | let stylesheet = '';
|
31 | if (this.xslUrl) {
|
32 | stylesheet = sitemap_stream_1.stylesheetInclude(this.xslUrl);
|
33 | }
|
34 | this.push(xmlDec + stylesheet + sitemapIndexTagStart);
|
35 | }
|
36 | this.push(sitemap_xml_1.otag(IndexTagNames.sitemap));
|
37 | if (typeof item === 'string') {
|
38 | this.push(sitemap_xml_1.element(IndexTagNames.loc, item));
|
39 | }
|
40 | else {
|
41 | this.push(sitemap_xml_1.element(IndexTagNames.loc, item.url));
|
42 | if (item.lastmod) {
|
43 | this.push(sitemap_xml_1.element(IndexTagNames.lastmod, new Date(item.lastmod).toISOString()));
|
44 | }
|
45 | }
|
46 | this.push(sitemap_xml_1.ctag(IndexTagNames.sitemap));
|
47 | callback();
|
48 | }
|
49 | _flush(cb) {
|
50 | this.push(closetag);
|
51 | cb();
|
52 | }
|
53 | }
|
54 | exports.SitemapIndexStream = SitemapIndexStream;
|
55 |
|
56 | class SitemapAndIndexStream extends SitemapIndexStream {
|
57 | constructor(opts) {
|
58 | var _a;
|
59 | opts.objectMode = true;
|
60 | super(opts);
|
61 | this.i = 0;
|
62 | this.getSitemapStream = opts.getSitemapStream;
|
63 | [
|
64 | this.idxItem,
|
65 | this.currentSitemap,
|
66 | this.currentSitemapPipeline,
|
67 | ] = this.getSitemapStream(0);
|
68 | this.limit = (_a = opts.limit) !== null && _a !== void 0 ? _a : 45000;
|
69 | }
|
70 | _writeSMI(item) {
|
71 | this.currentSitemap.write(item);
|
72 | this.i++;
|
73 | }
|
74 | _transform(item, encoding, callback) {
|
75 | var _a;
|
76 | if (this.i === 0) {
|
77 | this._writeSMI(item);
|
78 | super._transform(this.idxItem, encoding, callback);
|
79 | }
|
80 | else if (this.i % this.limit === 0) {
|
81 | const onFinish = () => {
|
82 | const [idxItem, currentSitemap, currentSitemapPipeline,] = this.getSitemapStream(this.i / this.limit);
|
83 | this.currentSitemap = currentSitemap;
|
84 | this.currentSitemapPipeline = currentSitemapPipeline;
|
85 | this._writeSMI(item);
|
86 |
|
87 | super._transform(idxItem, encoding, callback);
|
88 | };
|
89 | (_a = this.currentSitemapPipeline) === null || _a === void 0 ? void 0 : _a.on('finish', onFinish);
|
90 | this.currentSitemap.end(!this.currentSitemapPipeline ? onFinish : undefined);
|
91 | }
|
92 | else {
|
93 | this._writeSMI(item);
|
94 | callback();
|
95 | }
|
96 | }
|
97 | _flush(cb) {
|
98 | var _a;
|
99 | const onFinish = () => super._flush(cb);
|
100 | (_a = this.currentSitemapPipeline) === null || _a === void 0 ? void 0 : _a.on('finish', onFinish);
|
101 | this.currentSitemap.end(!this.currentSitemapPipeline ? onFinish : undefined);
|
102 | }
|
103 | }
|
104 | exports.SitemapAndIndexStream = SitemapAndIndexStream;
|