@crawlee/core 4.0.0-beta.99 → 4.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/configuration.d.ts +16 -47
- package/configuration.js +13 -25
- package/debug.js +4 -4
- package/errors.d.ts +28 -38
- package/errors.js +33 -47
- package/events/event_manager.d.ts +2 -2
- package/events/event_manager.js +7 -6
- package/events/index.d.ts +1 -0
- package/events/local_event_manager.d.ts +1 -8
- package/events/local_event_manager.js +13 -13
- package/events/system_info.d.ts +38 -0
- package/index.d.ts +2 -8
- package/index.js +4 -8
- package/internal.d.ts +8 -0
- package/internal.js +9 -0
- package/log.d.ts +10 -11
- package/log.js +52 -20
- package/memory-storage/memory-storage.d.ts +15 -18
- package/memory-storage/memory-storage.js +80 -58
- package/memory-storage/resource-clients/dataset.d.ts +1 -6
- package/memory-storage/resource-clients/dataset.js +23 -31
- package/memory-storage/resource-clients/key-value-store.d.ts +1 -10
- package/memory-storage/resource-clients/key-value-store.js +43 -67
- package/memory-storage/resource-clients/request-queue.d.ts +1 -42
- package/memory-storage/resource-clients/request-queue.js +109 -117
- package/owned_or_injected.d.ts +1 -3
- package/owned_or_injected.js +17 -17
- package/package.json +17 -20
- package/proxy_configuration.d.ts +21 -26
- package/proxy_configuration.js +35 -25
- package/recoverable_state.d.ts +104 -47
- package/recoverable_state.js +199 -74
- package/request.d.ts +20 -107
- package/request.js +78 -244
- package/serialization.js +17 -16
- package/service_locator.d.ts +22 -10
- package/service_locator.js +59 -48
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +13 -8
- package/storages/dataset.js +149 -40
- package/storages/index.d.ts +4 -4
- package/storages/index.js +2 -4
- package/storages/key_value_store.d.ts +16 -35
- package/storages/key_value_store.js +223 -110
- package/storages/key_value_store_codec.js +6 -11
- package/storages/request_dedup_cache.d.ts +1 -4
- package/storages/request_dedup_cache.js +15 -15
- package/storages/request_list.d.ts +9 -104
- package/storages/request_list.js +236 -233
- package/storages/request_loader.d.ts +49 -18
- package/storages/request_loader.js +36 -1
- package/storages/request_manager.d.ts +86 -0
- package/storages/request_manager_tandem.d.ts +14 -38
- package/storages/request_manager_tandem.js +67 -64
- package/storages/request_queue.d.ts +23 -50
- package/storages/request_queue.js +371 -226
- package/storages/storage_instance_manager.d.ts +2 -4
- package/storages/storage_instance_manager.js +21 -21
- package/storages/storage_stats.d.ts +1 -1
- package/storages/storage_stats.js +4 -4
- package/storages/transaction.d.ts +270 -0
- package/storages/transaction.js +296 -0
- package/storages/utils.d.ts +6 -3
- package/storages/utils.js +11 -2
- package/system-info/runtime.js +7 -7
- package/url.d.ts +9 -0
- package/url.js +11 -0
- package/validators.d.ts +23 -25
- package/validators.js +14 -25
- package/autoscaling/autoscaled_pool.d.ts +0 -213
- package/autoscaling/autoscaled_pool.js +0 -378
- package/autoscaling/client_load_signal.d.ts +0 -59
- package/autoscaling/client_load_signal.js +0 -73
- package/autoscaling/concurrency_system.d.ts +0 -283
- package/autoscaling/concurrency_system.js +0 -350
- package/autoscaling/cpu_load_signal.d.ts +0 -44
- package/autoscaling/cpu_load_signal.js +0 -46
- package/autoscaling/event_loop_load_signal.d.ts +0 -54
- package/autoscaling/event_loop_load_signal.js +0 -60
- package/autoscaling/index.d.ts +0 -9
- package/autoscaling/index.js +0 -9
- package/autoscaling/load_signal.d.ts +0 -99
- package/autoscaling/load_signal.js +0 -103
- package/autoscaling/memory_load_signal.d.ts +0 -56
- package/autoscaling/memory_load_signal.js +0 -106
- package/autoscaling/snapshotter.d.ts +0 -87
- package/autoscaling/snapshotter.js +0 -67
- package/autoscaling/system_status.d.ts +0 -161
- package/autoscaling/system_status.js +0 -139
- package/autoscaling/weighted_avg.d.ts +0 -5
- package/autoscaling/weighted_avg.js +0 -14
- package/cookie_utils.d.ts +0 -44
- package/cookie_utils.js +0 -122
- package/crawlers/context_pipeline.d.ts +0 -70
- package/crawlers/context_pipeline.js +0 -122
- package/crawlers/crawler_commons.d.ts +0 -257
- package/crawlers/crawler_commons.js +0 -107
- package/crawlers/error_snapshotter.d.ts +0 -59
- package/crawlers/error_snapshotter.js +0 -117
- package/crawlers/error_tracker.d.ts +0 -54
- package/crawlers/error_tracker.js +0 -308
- package/crawlers/index.d.ts +0 -5
- package/crawlers/index.js +0 -5
- package/crawlers/internals/types.d.ts +0 -7
- package/crawlers/statistics.d.ts +0 -209
- package/crawlers/statistics.js +0 -350
- package/enqueue_links/enqueue_links.d.ts +0 -264
- package/enqueue_links/enqueue_links.js +0 -271
- package/enqueue_links/index.d.ts +0 -2
- package/enqueue_links/index.js +0 -2
- package/enqueue_links/shared.d.ts +0 -83
- package/enqueue_links/shared.js +0 -221
- package/router.d.ts +0 -309
- package/router.js +0 -309
- package/session_pool/consts.d.ts +0 -3
- package/session_pool/consts.js +0 -3
- package/session_pool/errors.d.ts +0 -7
- package/session_pool/errors.js +0 -11
- package/session_pool/fingerprint.d.ts +0 -9
- package/session_pool/fingerprint.js +0 -30
- package/session_pool/index.d.ts +0 -4
- package/session_pool/index.js +0 -4
- package/session_pool/session.d.ts +0 -161
- package/session_pool/session.js +0 -218
- package/session_pool/session_pool.d.ts +0 -246
- package/session_pool/session_pool.js +0 -386
- package/storages/access_checking.d.ts +0 -12
- package/storages/access_checking.js +0 -17
- package/storages/sitemap_request_loader.d.ts +0 -249
- package/storages/sitemap_request_loader.js +0 -432
- /package/{crawlers/internals/types.js → events/system_info.js} +0 -0
|
@@ -1,432 +0,0 @@
|
|
|
1
|
-
import { Transform } from 'node:stream';
|
|
2
|
-
import { parseSitemap } from '@crawlee/utils';
|
|
3
|
-
import { minimatch } from 'minimatch';
|
|
4
|
-
import ow from 'ow';
|
|
5
|
-
import { constructGlobObjectsFromGlobs, constructRegExpObjectsFromRegExps } from '../enqueue_links/shared.js';
|
|
6
|
-
import { Request } from '../request.js';
|
|
7
|
-
import { serviceLocator } from '../service_locator.js';
|
|
8
|
-
import { KeyValueStore } from './key_value_store.js';
|
|
9
|
-
import { purgeDefaultStorages } from './utils.js';
|
|
10
|
-
/** @internal */
|
|
11
|
-
const STATE_PERSISTENCE_KEY = 'SITEMAP_REQUEST_LOADER_STATE';
|
|
12
|
-
/**
|
|
13
|
-
* A list of URLs to crawl parsed from a sitemap.
|
|
14
|
-
*
|
|
15
|
-
* The loading of the sitemap is performed in the background so that crawling can start before the sitemap is fully loaded.
|
|
16
|
-
*/
|
|
17
|
-
export class SitemapRequestLoader {
|
|
18
|
-
/**
|
|
19
|
-
* Set of URLs that were returned by `fetchNextRequest()` and not marked as handled yet.
|
|
20
|
-
* @internal
|
|
21
|
-
*/
|
|
22
|
-
inProgress = new Set();
|
|
23
|
-
/**
|
|
24
|
-
* Map of returned Request objects that have not been marked as handled yet.
|
|
25
|
-
*
|
|
26
|
-
* We use this to persist custom user fields on the in-progress requests.
|
|
27
|
-
*/
|
|
28
|
-
requestData = new Map();
|
|
29
|
-
/**
|
|
30
|
-
* Object for keeping track of the sitemap parsing progress.
|
|
31
|
-
*/
|
|
32
|
-
sitemapParsingProgress = {
|
|
33
|
-
/**
|
|
34
|
-
* URL of the sitemap that is currently being parsed. `null` if no sitemap is being parsed.
|
|
35
|
-
*/
|
|
36
|
-
inProgressSitemapUrl: null,
|
|
37
|
-
/**
|
|
38
|
-
* Buffer for URLs from the currently parsed sitemap. Used for tracking partially loaded sitemaps across migrations.
|
|
39
|
-
*/
|
|
40
|
-
inProgressEntries: new Set(),
|
|
41
|
-
/**
|
|
42
|
-
* Set of sitemap URLs that have not been parsed yet. If the set is empty and `inProgressSitemapUrl` is `null`, the sitemap loading is finished.
|
|
43
|
-
*/
|
|
44
|
-
pendingSitemapUrls: new Set(),
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Object stream of URLs parsed from the sitemaps.
|
|
48
|
-
* Using `highWaterMark`, this can manage the speed of the sitemap loading.
|
|
49
|
-
*
|
|
50
|
-
* Fetch the next URL to be processed using `fetchNextRequest()`.
|
|
51
|
-
*/
|
|
52
|
-
urlQueueStream;
|
|
53
|
-
/**
|
|
54
|
-
* Indicates whether the request list sitemap loading was aborted.
|
|
55
|
-
*
|
|
56
|
-
* If the loading was aborted before the sitemaps were fully loaded, the request list might be missing some URLs.
|
|
57
|
-
* The `isSitemapFullyLoaded` method can be used to check if the sitemaps were fully loaded.
|
|
58
|
-
*
|
|
59
|
-
* If the loading is aborted and all the requests are handled, `isFinished()` will return `true`.
|
|
60
|
-
*/
|
|
61
|
-
abortLoading = false;
|
|
62
|
-
/** Number of URLs that were marked as handled */
|
|
63
|
-
handledUrlCount = 0;
|
|
64
|
-
persistStateKey;
|
|
65
|
-
store;
|
|
66
|
-
closed = false;
|
|
67
|
-
/**
|
|
68
|
-
* Proxy URL to be used for sitemap loading.
|
|
69
|
-
*/
|
|
70
|
-
proxyUrl;
|
|
71
|
-
/**
|
|
72
|
-
* Logger instance.
|
|
73
|
-
*/
|
|
74
|
-
log;
|
|
75
|
-
urlExcludePatternObjects = [];
|
|
76
|
-
urlPatternObjects = [];
|
|
77
|
-
/** EventManager used to handle persistence */
|
|
78
|
-
events;
|
|
79
|
-
persistenceOptions;
|
|
80
|
-
/** @internal */
|
|
81
|
-
constructor(options) {
|
|
82
|
-
ow(options, ow.object.exactShape({
|
|
83
|
-
sitemapUrls: ow.array.ofType(ow.string),
|
|
84
|
-
proxyUrl: ow.optional.string,
|
|
85
|
-
persistStateKey: ow.optional.string,
|
|
86
|
-
signal: ow.optional.any(),
|
|
87
|
-
timeoutMillis: ow.optional.number,
|
|
88
|
-
maxBufferSize: ow.optional.number,
|
|
89
|
-
parseSitemapOptions: ow.optional.object,
|
|
90
|
-
globs: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('glob'))),
|
|
91
|
-
exclude: ow.optional.array.ofType(ow.any(ow.string, ow.regExp, ow.object.hasKeys('glob'), ow.object.hasKeys('regexp'))),
|
|
92
|
-
regexps: ow.optional.array.ofType(ow.any(ow.regExp, ow.object.hasKeys('regexp'))),
|
|
93
|
-
persistenceOptions: ow.optional.object,
|
|
94
|
-
}));
|
|
95
|
-
const { globs, exclude, regexps } = options;
|
|
96
|
-
this.log = serviceLocator.getLogger().child({ prefix: 'SitemapRequestLoader' });
|
|
97
|
-
if (exclude?.length) {
|
|
98
|
-
for (const excl of exclude) {
|
|
99
|
-
if (typeof excl === 'string' || 'glob' in excl) {
|
|
100
|
-
this.urlExcludePatternObjects.push(...constructGlobObjectsFromGlobs([excl]));
|
|
101
|
-
}
|
|
102
|
-
else if (excl instanceof RegExp || 'regexp' in excl) {
|
|
103
|
-
this.urlExcludePatternObjects.push(...constructRegExpObjectsFromRegExps([excl]));
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (globs?.length) {
|
|
108
|
-
this.urlPatternObjects.push(...constructGlobObjectsFromGlobs(globs));
|
|
109
|
-
}
|
|
110
|
-
if (regexps?.length) {
|
|
111
|
-
this.urlPatternObjects.push(...constructRegExpObjectsFromRegExps(regexps));
|
|
112
|
-
}
|
|
113
|
-
this.persistStateKey = options.persistStateKey;
|
|
114
|
-
this.persistenceOptions = { enable: true, ...options.persistenceOptions };
|
|
115
|
-
this.proxyUrl = options.proxyUrl;
|
|
116
|
-
this.urlQueueStream = this.createNewStream(options.maxBufferSize ?? 200);
|
|
117
|
-
this.sitemapParsingProgress.pendingSitemapUrls = new Set(options.sitemapUrls);
|
|
118
|
-
this.events = serviceLocator.getEventManager();
|
|
119
|
-
this.persistState = this.persistState.bind(this);
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Creates a new object stream with the specified highWaterMark.
|
|
123
|
-
* @param highWaterMark High water mark for the stream (the maximum number of objects the stream will buffer).
|
|
124
|
-
* @returns A new object stream.
|
|
125
|
-
*/
|
|
126
|
-
createNewStream(highWaterMark) {
|
|
127
|
-
return new Transform({
|
|
128
|
-
objectMode: true,
|
|
129
|
-
highWaterMark,
|
|
130
|
-
}).pause();
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Returns a function that checks whether the provided pattern matches the closure URL.
|
|
134
|
-
* @param url URL to be checked.
|
|
135
|
-
* @returns A matcher function that checks whether the pattern matches the closure URL.
|
|
136
|
-
*/
|
|
137
|
-
matchesUrl(url) {
|
|
138
|
-
return (patternObject) => {
|
|
139
|
-
const { regexp, glob } = patternObject;
|
|
140
|
-
const matchesRegex = (regexp && url.match(regexp)) || false;
|
|
141
|
-
const matchesGlob = (glob && minimatch(url, glob, { nocase: true })) || false;
|
|
142
|
-
return Boolean(matchesRegex || matchesGlob);
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Checks whether the URL matches the `globs` / `regexps` / `exclude` provided in the `options`.
|
|
147
|
-
* @param url URL to be checked.
|
|
148
|
-
* @returns `true` if the URL matches the patterns, `false` otherwise.
|
|
149
|
-
*/
|
|
150
|
-
isUrlMatchingPatterns(url) {
|
|
151
|
-
return (!this.urlExcludePatternObjects.some(this.matchesUrl(url)) &&
|
|
152
|
-
(this.urlPatternObjects.length === 0 || this.urlPatternObjects.some(this.matchesUrl(url))));
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Adds a URL to the queue of parsed URLs.
|
|
156
|
-
*
|
|
157
|
-
* Blocks if the stream is full until it is drained.
|
|
158
|
-
*/
|
|
159
|
-
async pushNextUrl(url) {
|
|
160
|
-
return new Promise((resolve) => {
|
|
161
|
-
if (this.closed || (url && !this.isUrlMatchingPatterns(url))) {
|
|
162
|
-
resolve();
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
if (!this.urlQueueStream.push(url)) {
|
|
166
|
-
// This doesn't work with the 'drain' event (it's not emitted for some reason).
|
|
167
|
-
this.urlQueueStream.once('readdata', () => {
|
|
168
|
-
resolve();
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
resolve();
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Reads the next URL from the queue of parsed URLs.
|
|
178
|
-
*
|
|
179
|
-
* If the stream is empty, blocks until a new URL is pushed.
|
|
180
|
-
* @returns The next URL from the queue or `null` if we have read all URLs.
|
|
181
|
-
*/
|
|
182
|
-
async readNextUrl() {
|
|
183
|
-
return new Promise((resolve) => {
|
|
184
|
-
if (this.closed) {
|
|
185
|
-
resolve(null);
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
const result = this.urlQueueStream.read();
|
|
189
|
-
if (!result && !this.isSitemapFullyLoaded()) {
|
|
190
|
-
this.urlQueueStream.once('readable', () => {
|
|
191
|
-
const nextUrl = this.urlQueueStream.read();
|
|
192
|
-
resolve(nextUrl);
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
resolve(result);
|
|
197
|
-
}
|
|
198
|
-
this.urlQueueStream.emit('readdata');
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Indicates whether the background processing of sitemap contents has successfully finished.
|
|
203
|
-
*
|
|
204
|
-
* If this is `false`, the background processing is either still in progress or was aborted.
|
|
205
|
-
*/
|
|
206
|
-
isSitemapFullyLoaded() {
|
|
207
|
-
return (this.sitemapParsingProgress.inProgressSitemapUrl === null &&
|
|
208
|
-
this.sitemapParsingProgress.pendingSitemapUrls.size === 0);
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Start processing the sitemaps and loading the URLs.
|
|
212
|
-
*
|
|
213
|
-
* Resolves once all the sitemaps URLs have been fully loaded (sets `isSitemapFullyLoaded` to `true`).
|
|
214
|
-
*/
|
|
215
|
-
async load({ parseSitemapOptions, }) {
|
|
216
|
-
while (!this.isSitemapFullyLoaded() && !this.abortLoading) {
|
|
217
|
-
const sitemapUrl = this.sitemapParsingProgress.inProgressSitemapUrl ??
|
|
218
|
-
this.sitemapParsingProgress.pendingSitemapUrls.values().next().value;
|
|
219
|
-
try {
|
|
220
|
-
for await (const item of parseSitemap([{ type: 'url', url: sitemapUrl }], this.proxyUrl, {
|
|
221
|
-
...parseSitemapOptions,
|
|
222
|
-
maxDepth: 0,
|
|
223
|
-
emitNestedSitemaps: true,
|
|
224
|
-
})) {
|
|
225
|
-
if (!item.originSitemapUrl) {
|
|
226
|
-
// This is a nested sitemap
|
|
227
|
-
this.sitemapParsingProgress.pendingSitemapUrls.add(item.loc);
|
|
228
|
-
continue;
|
|
229
|
-
}
|
|
230
|
-
if (!this.sitemapParsingProgress.inProgressEntries.has(item.loc)) {
|
|
231
|
-
await this.pushNextUrl(item.loc);
|
|
232
|
-
this.sitemapParsingProgress.inProgressEntries.add(item.loc);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
catch (e) {
|
|
237
|
-
this.log.error('Error loading sitemap contents:', e);
|
|
238
|
-
}
|
|
239
|
-
this.sitemapParsingProgress.pendingSitemapUrls.delete(sitemapUrl);
|
|
240
|
-
this.sitemapParsingProgress.inProgressEntries.clear();
|
|
241
|
-
this.sitemapParsingProgress.inProgressSitemapUrl = null;
|
|
242
|
-
}
|
|
243
|
-
this.urlQueueStream.end();
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Open a sitemap and start processing it.
|
|
247
|
-
*
|
|
248
|
-
* Resolves to a new instance of `SitemapRequestLoader`, which **might not be fully loaded yet** - i.e. the sitemap might still be loading in the background.
|
|
249
|
-
*
|
|
250
|
-
* Track the loading progress using the `isSitemapFullyLoaded` property.
|
|
251
|
-
*/
|
|
252
|
-
static async open(options) {
|
|
253
|
-
const { httpClient, ...restOptions } = options;
|
|
254
|
-
const requestList = new SitemapRequestLoader({
|
|
255
|
-
...restOptions,
|
|
256
|
-
persistStateKey: options.persistStateKey ?? STATE_PERSISTENCE_KEY,
|
|
257
|
-
});
|
|
258
|
-
await requestList.restoreState();
|
|
259
|
-
void requestList.load({
|
|
260
|
-
parseSitemapOptions: { logger: serviceLocator.getLogger(), ...options.parseSitemapOptions, httpClient },
|
|
261
|
-
});
|
|
262
|
-
if (requestList.persistenceOptions.enable) {
|
|
263
|
-
requestList.events.on("persistState" /* EventType.PERSIST_STATE */, requestList.persistState);
|
|
264
|
-
}
|
|
265
|
-
options?.signal?.addEventListener('abort', () => {
|
|
266
|
-
requestList.abortLoading = true;
|
|
267
|
-
});
|
|
268
|
-
if (options.timeoutMillis) {
|
|
269
|
-
setTimeout(() => {
|
|
270
|
-
requestList.abortLoading = true;
|
|
271
|
-
}, options.timeoutMillis);
|
|
272
|
-
}
|
|
273
|
-
return requestList;
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* @inheritDoc
|
|
277
|
-
*/
|
|
278
|
-
async getTotalCount() {
|
|
279
|
-
// Total known so far = not-yet-fetched (still buffered in the stream) + in-progress (fetched but not
|
|
280
|
-
// yet handled) + already handled.
|
|
281
|
-
return this.urlQueueStream.readableLength + this.inProgress.size + this.handledUrlCount;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* @inheritDoc
|
|
285
|
-
*/
|
|
286
|
-
async getPendingCount() {
|
|
287
|
-
// Pending = everything not yet handled = not-yet-fetched + in-progress.
|
|
288
|
-
return this.urlQueueStream.readableLength + this.inProgress.size;
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Combines this list with a request manager (a {@link RequestQueue} by default) into a
|
|
292
|
-
* {@link RequestManagerTandem}, allowing requests to be added and reclaimed while still
|
|
293
|
-
* being read from this list first.
|
|
294
|
-
*/
|
|
295
|
-
async toTandem(requestManager) {
|
|
296
|
-
// Import here to avoid circular imports.
|
|
297
|
-
const { RequestManagerTandem } = await import('./request_manager_tandem.js');
|
|
298
|
-
const { RequestQueue } = await import('./request_queue.js');
|
|
299
|
-
return new RequestManagerTandem(this, requestManager ?? (await RequestQueue.open()));
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* @inheritDoc
|
|
303
|
-
*/
|
|
304
|
-
async isFinished() {
|
|
305
|
-
return ((await this.isEmpty()) && this.inProgress.size === 0 && (this.isSitemapFullyLoaded() || this.abortLoading));
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* @inheritDoc
|
|
309
|
-
*/
|
|
310
|
-
async isEmpty() {
|
|
311
|
-
return this.urlQueueStream.readableLength === 0;
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* @inheritDoc
|
|
315
|
-
*/
|
|
316
|
-
async getHandledCount() {
|
|
317
|
-
return this.handledUrlCount;
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* @inheritDoc
|
|
321
|
-
*/
|
|
322
|
-
async persistState() {
|
|
323
|
-
if (this.persistStateKey === undefined) {
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
|
-
this.store ??= await KeyValueStore.open();
|
|
327
|
-
const urlQueue = [];
|
|
328
|
-
while (this.urlQueueStream.readableLength > 0) {
|
|
329
|
-
const url = this.urlQueueStream.read();
|
|
330
|
-
if (url === null) {
|
|
331
|
-
break;
|
|
332
|
-
}
|
|
333
|
-
urlQueue.push(url);
|
|
334
|
-
}
|
|
335
|
-
// Create a new stream, as we have read all the URLs from the current one.
|
|
336
|
-
// Pushing the urls back to the original stream might not be possible if it has been ended.
|
|
337
|
-
const newStream = this.createNewStream(this.urlQueueStream.readableHighWaterMark);
|
|
338
|
-
for (const url of urlQueue) {
|
|
339
|
-
newStream.push(url);
|
|
340
|
-
}
|
|
341
|
-
if (this.urlQueueStream.writableEnded) {
|
|
342
|
-
newStream.end();
|
|
343
|
-
}
|
|
344
|
-
this.urlQueueStream = newStream;
|
|
345
|
-
await this.store.setValue(this.persistStateKey, {
|
|
346
|
-
sitemapParsingProgress: {
|
|
347
|
-
pendingSitemapUrls: Array.from(this.sitemapParsingProgress.pendingSitemapUrls),
|
|
348
|
-
inProgressSitemapUrl: this.sitemapParsingProgress.inProgressSitemapUrl,
|
|
349
|
-
inProgressEntries: Array.from(this.sitemapParsingProgress.inProgressEntries),
|
|
350
|
-
},
|
|
351
|
-
// Re-queue in-progress requests to the front so they are retried if the state is restored.
|
|
352
|
-
urlQueue: [...this.inProgress, ...urlQueue],
|
|
353
|
-
requestData: Array.from(this.requestData.entries()),
|
|
354
|
-
abortLoading: this.abortLoading,
|
|
355
|
-
closed: this.closed,
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
async restoreState() {
|
|
359
|
-
await purgeDefaultStorages({ onlyPurgeOnce: true });
|
|
360
|
-
if (this.persistStateKey === undefined) {
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
this.store ??= await KeyValueStore.open();
|
|
364
|
-
const state = await this.store.getValue(this.persistStateKey);
|
|
365
|
-
if (state === null) {
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
this.sitemapParsingProgress = {
|
|
369
|
-
pendingSitemapUrls: new Set(state.sitemapParsingProgress.pendingSitemapUrls),
|
|
370
|
-
inProgressSitemapUrl: state.sitemapParsingProgress.inProgressSitemapUrl,
|
|
371
|
-
inProgressEntries: new Set(state.sitemapParsingProgress.inProgressEntries),
|
|
372
|
-
};
|
|
373
|
-
this.requestData = new Map(state.requestData ?? []);
|
|
374
|
-
for (const url of state.urlQueue) {
|
|
375
|
-
this.urlQueueStream.push(url);
|
|
376
|
-
}
|
|
377
|
-
this.abortLoading = state.abortLoading;
|
|
378
|
-
this.closed = state.closed;
|
|
379
|
-
}
|
|
380
|
-
/**
|
|
381
|
-
* @inheritDoc
|
|
382
|
-
*/
|
|
383
|
-
async fetchNextRequest() {
|
|
384
|
-
const nextUrl = await this.readNextUrl();
|
|
385
|
-
if (!nextUrl) {
|
|
386
|
-
return null;
|
|
387
|
-
}
|
|
388
|
-
// A restored in-progress request already has its Request data; don't overwrite it.
|
|
389
|
-
if (!this.requestData.has(nextUrl)) {
|
|
390
|
-
this.requestData.set(nextUrl, new Request({ url: nextUrl }));
|
|
391
|
-
}
|
|
392
|
-
this.inProgress.add(nextUrl);
|
|
393
|
-
return this.requestData.get(nextUrl);
|
|
394
|
-
}
|
|
395
|
-
/**
|
|
396
|
-
* @inheritDoc
|
|
397
|
-
*/
|
|
398
|
-
async *[Symbol.asyncIterator]() {
|
|
399
|
-
while (!(await this.isFinished())) {
|
|
400
|
-
const request = await this.fetchNextRequest();
|
|
401
|
-
if (!request)
|
|
402
|
-
break;
|
|
403
|
-
yield request;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Aborts the internal sitemap loading, stops the processing of the sitemap contents and drops all the pending URLs.
|
|
408
|
-
*
|
|
409
|
-
* Calling `fetchNextRequest()` after this method will always return `null`.
|
|
410
|
-
*/
|
|
411
|
-
async teardown() {
|
|
412
|
-
this.closed = true;
|
|
413
|
-
this.abortLoading = true;
|
|
414
|
-
this.events.off("persistState" /* EventType.PERSIST_STATE */, this.persistState);
|
|
415
|
-
await this.persistState();
|
|
416
|
-
this.urlQueueStream.emit('readdata'); // unblocks the potentially waiting `pushNextUrl` call
|
|
417
|
-
}
|
|
418
|
-
/**
|
|
419
|
-
* @inheritDoc
|
|
420
|
-
*/
|
|
421
|
-
async markRequestAsHandled(request) {
|
|
422
|
-
this.handledUrlCount += 1;
|
|
423
|
-
this.ensureInProgress(request.url);
|
|
424
|
-
this.inProgress.delete(request.url);
|
|
425
|
-
this.requestData.delete(request.url);
|
|
426
|
-
}
|
|
427
|
-
ensureInProgress(url) {
|
|
428
|
-
if (!this.inProgress.has(url)) {
|
|
429
|
-
throw new Error(`The request is not being processed (url: ${url})`);
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
}
|
|
File without changes
|