-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap-wrapper.ts
273 lines (246 loc) · 6.76 KB
/
sitemap-wrapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { ErrorLevel, SitemapItemLoose, SitemapStream } from 'sitemap';
import path from 'path';
import cloneDeep from 'lodash/cloneDeep';
import { SitemapWrapperBase } from './sitemap-wrapper-base';
import { createReadStream } from 'fs-extra';
export type SitemapFileWrapperOptions = {
compress?: boolean;
filenameRoot?: string;
limitCount?: number;
limitBytes?: number;
siteBaseURL: string;
localDirectory?: string;
};
export class SitemapFileWrapper extends SitemapWrapperBase {
protected _siteBaseURL: string;
private _options: SitemapFileWrapperOptions;
public get options(): SitemapFileWrapperOptions {
return this._options;
}
/**
* Asynchronous helper for SitemapStream.
*/
constructor(options: SitemapFileWrapperOptions) {
const {
compress = true,
filenameRoot = 'sitemap',
limitCount,
limitBytes,
siteBaseURL,
localDirectory = '/tmp',
} = options;
super({ compress, filenameRoot, limitCount, limitBytes, localDirectory });
this._options = options;
this._siteBaseURL = siteBaseURL;
// Create the map
this._sitemapOrIndex = new SitemapStream({
hostname: siteBaseURL,
level: ErrorLevel.SILENT,
});
// Pipe to either file or gzip (then to file)
this._sitemapOrIndex.pipe(this._sitemapDest);
// Track written bytes
// Note: This is only accuate after `.end()` is called
// because of buffering in the streams - it causes
// the computed size to lag non-deterministically from the current size
// buffered + size written.
this._sitemapOrIndex.on('data', this.countBytes.bind(this));
}
/**
* Asynchronously pull and hydrate from local file
* @param bucketName - S3 bucket name
* @returns
*/
public static async fromFile({
sourceFileAndPath,
compress = true,
filenameRoot = 'sitemap',
limitCount,
limitBytes,
localDirectory = '/tmp',
siteBaseURL,
}: {
sourceFileAndPath: string;
compress?: boolean;
filenameRoot?: string;
limitCount?: number;
limitBytes?: number;
localDirectory?: string;
bucketName: string;
s3Directory?: string;
siteBaseURL: string;
}): Promise<{
sitemap: SitemapFileWrapper;
existing: boolean;
items: SitemapItemLoose[];
}> {
const result = await super._fromStream({
stream: createReadStream(sourceFileAndPath),
compressed: sourceFileAndPath.endsWith('.gz'),
type: 'sitemap',
});
const { items } = result;
let existing = true;
if (items.length === 0) {
existing = false;
}
// Let the wrapper create a new sitemap stream
const sitemap = new SitemapFileWrapper({
compress,
filenameRoot,
limitCount,
limitBytes,
localDirectory,
siteBaseURL,
});
await sitemap.writeArray({ items, disregardByteLimit: true });
return { sitemap, existing, items };
}
/**
* Asynchronously pull and hydrate from S3
*
* Does *not* create a new `SitemapFileWrapper`
*/
public static async itemsFromS3({
bucketName,
s3Directory = 'sitemaps/',
compress = true,
filenameRoot = 'sitemap',
}: {
compress?: boolean;
filenameRoot?: string;
bucketName: string;
s3Directory?: string;
}): Promise<{
existing: boolean;
items: SitemapItemLoose[];
}> {
const filename = `${filenameRoot}.xml${compress ? '.gz' : ''}`;
const s3Key = path.join(s3Directory, filename);
const s3Result = await super._fromS3({
bucketName,
s3Key,
type: 'sitemap',
});
const { items } = s3Result;
let existing = true;
if (items.length === 0) {
existing = false;
}
return { existing, items };
}
/**
* Asynchronously pull and hydrate from S3
* @param bucketName - S3 bucket name
* @returns
*/
public static async fromS3({
bucketName,
s3Directory = 'sitemaps/',
compress = true,
filenameRoot = 'sitemap',
limitCount,
limitBytes,
localDirectory = '/tmp',
siteBaseURL,
}: {
compress?: boolean;
filenameRoot?: string;
limitCount?: number;
limitBytes?: number;
localDirectory?: string;
bucketName: string;
s3Directory?: string;
siteBaseURL: string;
}): Promise<{
sitemap: SitemapFileWrapper;
existing: boolean;
items: SitemapItemLoose[];
}> {
const { items, existing } = await SitemapFileWrapper.itemsFromS3({
bucketName,
s3Directory,
compress,
filenameRoot,
});
// Let the wrapper create a new sitemap stream
const sitemap = new SitemapFileWrapper({
compress,
filenameRoot,
limitCount,
limitBytes,
localDirectory,
siteBaseURL,
});
await sitemap.writeArray({ items, disregardByteLimit: true });
return { sitemap, existing, items };
}
public async write({
item,
disregardByteLimit = false,
disregardCountLimit = false,
}: {
item: SitemapItemLoose;
disregardByteLimit?: boolean;
disregardCountLimit?: boolean;
}): Promise<void> {
await super.write({ item, disregardByteLimit, disregardCountLimit });
}
public async writeArray({
items,
disregardByteLimit = false,
disregardCountLimit = false,
}: {
items: SitemapItemLoose[];
disregardByteLimit?: boolean;
disregardCountLimit?: boolean;
}): Promise<void> {
await super.writeArray({ items, disregardByteLimit, disregardCountLimit });
}
protected itemSize(item: SitemapItemLoose): number {
const itemClone = cloneDeep(item);
// Remove the arrays if empty
if (
itemClone.video !== undefined &&
itemClone.video instanceof Array &&
itemClone.video.length === 0
) {
delete itemClone.video;
}
if (
itemClone.img !== undefined &&
itemClone.img instanceof Array &&
itemClone.img.length === 0
) {
delete itemClone.img;
}
if (
itemClone.news !== undefined &&
itemClone.news instanceof Array &&
itemClone.news.length === 0
) {
delete itemClone.news;
}
if (
itemClone.links !== undefined &&
itemClone.links instanceof Array &&
itemClone.links.length === 0
) {
delete itemClone.links;
}
// Fully justify the item path
// Writing a new map will often just have the path in the items,
// but the written XML map will have the base url prefixed on each item.
// Reading a file back from S3 will have the base url on each item as a result.
// We want the space computation for new and existing to be roughly the same.
itemClone.url = new URL(item.url, this._siteBaseURL).toString();
const length = JSON.stringify(itemClone).length;
return length;
}
/**
* All items written to the index via construction or `write`
*/
public get items(): SitemapItemLoose[] {
return this._items;
}
}