@nitpicker/crawler 0.4.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/CHANGELOG.md +8 -0
- package/LICENSE +191 -0
- package/README.md +13 -0
- package/lib/archive/archive-accessor.d.ts +107 -0
- package/lib/archive/archive-accessor.js +264 -0
- package/lib/archive/archive.d.ts +174 -0
- package/lib/archive/archive.js +331 -0
- package/lib/archive/database.d.ts +207 -0
- package/lib/archive/database.js +972 -0
- package/lib/archive/debug.d.ts +8 -0
- package/lib/archive/debug.js +9 -0
- package/lib/archive/filesystem/append-text.d.ts +9 -0
- package/lib/archive/filesystem/append-text.js +14 -0
- package/lib/archive/filesystem/copy-dir-sync.d.ts +6 -0
- package/lib/archive/filesystem/copy-dir-sync.js +9 -0
- package/lib/archive/filesystem/copy-dir.d.ts +7 -0
- package/lib/archive/filesystem/copy-dir.js +13 -0
- package/lib/archive/filesystem/exists.d.ts +6 -0
- package/lib/archive/filesystem/exists.js +9 -0
- package/lib/archive/filesystem/get-file-list.d.ts +8 -0
- package/lib/archive/filesystem/get-file-list.js +12 -0
- package/lib/archive/filesystem/index.d.ts +17 -0
- package/lib/archive/filesystem/index.js +17 -0
- package/lib/archive/filesystem/is-dir.d.ts +6 -0
- package/lib/archive/filesystem/is-dir.js +10 -0
- package/lib/archive/filesystem/mkdir.d.ts +8 -0
- package/lib/archive/filesystem/mkdir.js +15 -0
- package/lib/archive/filesystem/output-json.d.ts +9 -0
- package/lib/archive/filesystem/output-json.js +14 -0
- package/lib/archive/filesystem/output-text.d.ts +11 -0
- package/lib/archive/filesystem/output-text.js +32 -0
- package/lib/archive/filesystem/read-json.d.ts +7 -0
- package/lib/archive/filesystem/read-json.js +11 -0
- package/lib/archive/filesystem/read-text.d.ts +6 -0
- package/lib/archive/filesystem/read-text.js +10 -0
- package/lib/archive/filesystem/readline.d.ts +11 -0
- package/lib/archive/filesystem/readline.js +26 -0
- package/lib/archive/filesystem/remove.d.ts +5 -0
- package/lib/archive/filesystem/remove.js +10 -0
- package/lib/archive/filesystem/rename.d.ts +11 -0
- package/lib/archive/filesystem/rename.js +18 -0
- package/lib/archive/filesystem/tar.d.ts +11 -0
- package/lib/archive/filesystem/tar.js +22 -0
- package/lib/archive/filesystem/untar.d.ts +20 -0
- package/lib/archive/filesystem/untar.js +24 -0
- package/lib/archive/filesystem/utils.d.ts +109 -0
- package/lib/archive/filesystem/utils.js +185 -0
- package/lib/archive/filesystem/zip.d.ts +29 -0
- package/lib/archive/filesystem/zip.js +53 -0
- package/lib/archive/index.d.ts +6 -0
- package/lib/archive/index.js +11 -0
- package/lib/archive/page.d.ts +263 -0
- package/lib/archive/page.js +316 -0
- package/lib/archive/resource.d.ts +46 -0
- package/lib/archive/resource.js +62 -0
- package/lib/archive/safe-path.d.ts +9 -0
- package/lib/archive/safe-path.js +17 -0
- package/lib/archive/types.d.ts +210 -0
- package/lib/archive/types.js +1 -0
- package/lib/crawler/clear-destination-cache.d.ts +5 -0
- package/lib/crawler/clear-destination-cache.js +8 -0
- package/lib/crawler/crawler.d.ts +73 -0
- package/lib/crawler/crawler.js +748 -0
- package/lib/crawler/decompose-url.d.ts +25 -0
- package/lib/crawler/decompose-url.js +71 -0
- package/lib/crawler/destination-cache.d.ts +7 -0
- package/lib/crawler/destination-cache.js +6 -0
- package/lib/crawler/detect-pagination-pattern.d.ts +16 -0
- package/lib/crawler/detect-pagination-pattern.js +61 -0
- package/lib/crawler/fetch-destination.d.ts +38 -0
- package/lib/crawler/fetch-destination.js +208 -0
- package/lib/crawler/fetch-robots-txt.d.ts +42 -0
- package/lib/crawler/fetch-robots-txt.js +44 -0
- package/lib/crawler/find-best-matching-scope.d.ts +12 -0
- package/lib/crawler/find-best-matching-scope.js +46 -0
- package/lib/crawler/generate-predicted-urls.d.ts +13 -0
- package/lib/crawler/generate-predicted-urls.js +27 -0
- package/lib/crawler/handle-ignore-and-skip.d.ts +16 -0
- package/lib/crawler/handle-ignore-and-skip.js +19 -0
- package/lib/crawler/handle-resource-response.d.ts +13 -0
- package/lib/crawler/handle-resource-response.js +16 -0
- package/lib/crawler/handle-scrape-end.d.ts +24 -0
- package/lib/crawler/handle-scrape-end.js +82 -0
- package/lib/crawler/handle-scrape-error.d.ts +37 -0
- package/lib/crawler/handle-scrape-error.js +38 -0
- package/lib/crawler/index.d.ts +2 -0
- package/lib/crawler/index.js +2 -0
- package/lib/crawler/inject-scope-auth.d.ts +11 -0
- package/lib/crawler/inject-scope-auth.js +21 -0
- package/lib/crawler/is-external-url.d.ts +11 -0
- package/lib/crawler/is-external-url.js +12 -0
- package/lib/crawler/is-in-any-lower-layer.d.ts +13 -0
- package/lib/crawler/is-in-any-lower-layer.js +15 -0
- package/lib/crawler/link-list.d.ts +112 -0
- package/lib/crawler/link-list.js +248 -0
- package/lib/crawler/link-to-page-data.d.ts +14 -0
- package/lib/crawler/link-to-page-data.js +32 -0
- package/lib/crawler/net-timeout-error.d.ts +9 -0
- package/lib/crawler/net-timeout-error.js +11 -0
- package/lib/crawler/network.d.ts +30 -0
- package/lib/crawler/network.js +226 -0
- package/lib/crawler/protocol-agnostic-key.d.ts +9 -0
- package/lib/crawler/protocol-agnostic-key.js +11 -0
- package/lib/crawler/reconstruct-url.d.ts +10 -0
- package/lib/crawler/reconstruct-url.js +28 -0
- package/lib/crawler/result-handler.d.ts +118 -0
- package/lib/crawler/result-handler.js +153 -0
- package/lib/crawler/robots-checker.d.ts +26 -0
- package/lib/crawler/robots-checker.js +62 -0
- package/lib/crawler/should-discard-predicted.d.ts +14 -0
- package/lib/crawler/should-discard-predicted.js +31 -0
- package/lib/crawler/should-skip-url.d.ts +23 -0
- package/lib/crawler/should-skip-url.js +15 -0
- package/lib/crawler/speculative-pagination.d.ts +52 -0
- package/lib/crawler/speculative-pagination.js +215 -0
- package/lib/crawler/types.d.ts +119 -0
- package/lib/crawler/types.js +1 -0
- package/lib/crawler/url-filter.d.ts +56 -0
- package/lib/crawler/url-filter.js +110 -0
- package/lib/crawler-orchestrator.d.ts +142 -0
- package/lib/crawler-orchestrator.js +309 -0
- package/lib/debug.d.ts +8 -0
- package/lib/debug.js +9 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +18 -0
- package/lib/qzilla.d.ts +136 -0
- package/lib/qzilla.js +292 -0
- package/lib/types.d.ts +27 -0
- package/lib/types.js +1 -0
- package/lib/utils/array/each-splitted.d.ts +10 -0
- package/lib/utils/array/each-splitted.js +14 -0
- package/lib/utils/array/index.d.ts +1 -0
- package/lib/utils/array/index.js +1 -0
- package/lib/utils/async/index.d.ts +1 -0
- package/lib/utils/async/index.js +1 -0
- package/lib/utils/debug.d.ts +5 -0
- package/lib/utils/debug.js +5 -0
- package/lib/utils/error/dom-evaluation-error.d.ts +7 -0
- package/lib/utils/error/dom-evaluation-error.js +7 -0
- package/lib/utils/error/error-emitter.d.ts +18 -0
- package/lib/utils/error/error-emitter.js +29 -0
- package/lib/utils/error/index.d.ts +3 -0
- package/lib/utils/error/index.js +2 -0
- package/lib/utils/event-emitter/index.d.ts +6 -0
- package/lib/utils/event-emitter/index.js +6 -0
- package/lib/utils/index.d.ts +5 -0
- package/lib/utils/index.js +5 -0
- package/lib/utils/network/index.d.ts +1 -0
- package/lib/utils/network/index.js +1 -0
- package/lib/utils/object/clean-object.d.ts +8 -0
- package/lib/utils/object/clean-object.js +13 -0
- package/lib/utils/object/index.d.ts +1 -0
- package/lib/utils/object/index.js +1 -0
- package/lib/utils/path/index.d.ts +1 -0
- package/lib/utils/path/index.js +1 -0
- package/lib/utils/path/safe-filepath.d.ts +7 -0
- package/lib/utils/path/safe-filepath.js +12 -0
- package/lib/utils/regexp/index.d.ts +1 -0
- package/lib/utils/regexp/index.js +1 -0
- package/lib/utils/retryable/index.d.ts +2 -0
- package/lib/utils/retryable/index.js +1 -0
- package/lib/utils/sort/index.d.ts +14 -0
- package/lib/utils/sort/index.js +61 -0
- package/lib/utils/sort/remove-matches.d.ts +9 -0
- package/lib/utils/sort/remove-matches.js +23 -0
- package/lib/utils/types/index.d.ts +1 -0
- package/lib/utils/types/index.js +1 -0
- package/lib/utils/types/types.d.ts +46 -0
- package/lib/utils/types/types.js +1 -0
- package/lib/utils/url/index.d.ts +5 -0
- package/lib/utils/url/index.js +5 -0
- package/lib/utils/url/is-lower-layer.d.ts +15 -0
- package/lib/utils/url/is-lower-layer.js +55 -0
- package/lib/utils/url/parse-url.d.ts +11 -0
- package/lib/utils/url/parse-url.js +20 -0
- package/lib/utils/url/path-match.d.ts +11 -0
- package/lib/utils/url/path-match.js +18 -0
- package/lib/utils/url/sort-url.d.ts +10 -0
- package/lib/utils/url/sort-url.js +24 -0
- package/lib/utils/url/url-partial-match.d.ts +11 -0
- package/lib/utils/url/url-partial-match.js +32 -0
- package/package.json +49 -0
- package/src/archive/__mock__/.gitignore +3 -0
- package/src/archive/__mock__/mock.sqlite +0 -0
- package/src/archive/archive-accessor.ts +337 -0
- package/src/archive/archive.ts +408 -0
- package/src/archive/database.spec.ts +469 -0
- package/src/archive/database.ts +1059 -0
- package/src/archive/debug.ts +10 -0
- package/src/archive/filesystem/append-text.spec.ts +26 -0
- package/src/archive/filesystem/append-text.ts +16 -0
- package/src/archive/filesystem/copy-dir-sync.spec.ts +27 -0
- package/src/archive/filesystem/copy-dir-sync.ts +10 -0
- package/src/archive/filesystem/copy-dir.spec.ts +33 -0
- package/src/archive/filesystem/copy-dir.ts +14 -0
- package/src/archive/filesystem/exists.spec.ts +33 -0
- package/src/archive/filesystem/exists.ts +10 -0
- package/src/archive/filesystem/get-file-list.spec.ts +37 -0
- package/src/archive/filesystem/get-file-list.ts +13 -0
- package/src/archive/filesystem/index.ts +17 -0
- package/src/archive/filesystem/is-dir.spec.ts +29 -0
- package/src/archive/filesystem/is-dir.ts +11 -0
- package/src/archive/filesystem/mkdir.spec.ts +37 -0
- package/src/archive/filesystem/mkdir.ts +16 -0
- package/src/archive/filesystem/output-json.spec.ts +34 -0
- package/src/archive/filesystem/output-json.ts +16 -0
- package/src/archive/filesystem/output-text.spec.ts +31 -0
- package/src/archive/filesystem/output-text.ts +35 -0
- package/src/archive/filesystem/read-json.spec.ts +26 -0
- package/src/archive/filesystem/read-json.ts +12 -0
- package/src/archive/filesystem/read-text.spec.ts +25 -0
- package/src/archive/filesystem/read-text.ts +11 -0
- package/src/archive/filesystem/readline.spec.ts +29 -0
- package/src/archive/filesystem/readline.ts +30 -0
- package/src/archive/filesystem/remove.spec.ts +34 -0
- package/src/archive/filesystem/remove.ts +11 -0
- package/src/archive/filesystem/rename.spec.ts +46 -0
- package/src/archive/filesystem/rename.ts +21 -0
- package/src/archive/filesystem/tar.spec.ts +33 -0
- package/src/archive/filesystem/tar.ts +27 -0
- package/src/archive/filesystem/untar.spec.ts +34 -0
- package/src/archive/filesystem/untar.ts +36 -0
- package/src/archive/index.ts +13 -0
- package/src/archive/page.spec.ts +368 -0
- package/src/archive/page.ts +420 -0
- package/src/archive/resource.spec.ts +101 -0
- package/src/archive/resource.ts +73 -0
- package/src/archive/safe-path.spec.ts +44 -0
- package/src/archive/safe-path.ts +18 -0
- package/src/archive/types.ts +227 -0
- package/src/crawler/clear-destination-cache.spec.ts +20 -0
- package/src/crawler/clear-destination-cache.ts +9 -0
- package/src/crawler/crawler.ts +873 -0
- package/src/crawler/decompose-url.spec.ts +48 -0
- package/src/crawler/decompose-url.ts +90 -0
- package/src/crawler/destination-cache.spec.ts +23 -0
- package/src/crawler/destination-cache.ts +8 -0
- package/src/crawler/detect-pagination-pattern.spec.ts +169 -0
- package/src/crawler/detect-pagination-pattern.ts +66 -0
- package/src/crawler/fetch-destination.ts +257 -0
- package/src/crawler/fetch-robots-txt.spec.ts +83 -0
- package/src/crawler/fetch-robots-txt.ts +91 -0
- package/src/crawler/find-best-matching-scope.spec.ts +39 -0
- package/src/crawler/find-best-matching-scope.ts +57 -0
- package/src/crawler/generate-predicted-urls.spec.ts +42 -0
- package/src/crawler/generate-predicted-urls.ts +34 -0
- package/src/crawler/handle-ignore-and-skip.spec.ts +66 -0
- package/src/crawler/handle-ignore-and-skip.ts +30 -0
- package/src/crawler/handle-resource-response.spec.ts +45 -0
- package/src/crawler/handle-resource-response.ts +21 -0
- package/src/crawler/handle-scrape-end.spec.ts +109 -0
- package/src/crawler/handle-scrape-end.ts +115 -0
- package/src/crawler/handle-scrape-error.spec.ts +105 -0
- package/src/crawler/handle-scrape-error.ts +58 -0
- package/src/crawler/index.ts +2 -0
- package/src/crawler/inject-scope-auth.spec.ts +36 -0
- package/src/crawler/inject-scope-auth.ts +27 -0
- package/src/crawler/is-external-url.spec.ts +31 -0
- package/src/crawler/is-external-url.ts +17 -0
- package/src/crawler/is-in-any-lower-layer.spec.ts +31 -0
- package/src/crawler/is-in-any-lower-layer.ts +22 -0
- package/src/crawler/link-list.spec.ts +355 -0
- package/src/crawler/link-list.ts +275 -0
- package/src/crawler/link-to-page-data.spec.ts +133 -0
- package/src/crawler/link-to-page-data.ts +34 -0
- package/src/crawler/net-timeout-error.spec.ts +25 -0
- package/src/crawler/net-timeout-error.ts +11 -0
- package/src/crawler/protocol-agnostic-key.spec.ts +40 -0
- package/src/crawler/protocol-agnostic-key.ts +11 -0
- package/src/crawler/reconstruct-url.spec.ts +37 -0
- package/src/crawler/reconstruct-url.ts +37 -0
- package/src/crawler/robots-checker.spec.ts +104 -0
- package/src/crawler/robots-checker.ts +73 -0
- package/src/crawler/should-discard-predicted.spec.ts +125 -0
- package/src/crawler/should-discard-predicted.ts +33 -0
- package/src/crawler/should-skip-url.spec.ts +77 -0
- package/src/crawler/should-skip-url.ts +37 -0
- package/src/crawler/types.ts +146 -0
- package/src/crawler-orchestrator.ts +401 -0
- package/src/debug.ts +10 -0
- package/src/index.ts +25 -0
- package/src/types.ts +30 -0
- package/src/utils/array/each-splitted.spec.ts +38 -0
- package/src/utils/array/each-splitted.ts +19 -0
- package/src/utils/array/index.ts +1 -0
- package/src/utils/debug.ts +6 -0
- package/src/utils/error/dom-evaluation-error.spec.ts +20 -0
- package/src/utils/error/dom-evaluation-error.ts +6 -0
- package/src/utils/error/error-emitter.spec.ts +78 -0
- package/src/utils/error/error-emitter.ts +44 -0
- package/src/utils/error/index.ts +3 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/object/clean-object.spec.ts +24 -0
- package/src/utils/object/clean-object.ts +13 -0
- package/src/utils/object/index.ts +1 -0
- package/src/utils/types/index.ts +1 -0
- package/src/utils/types/types.ts +65 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [0.4.1](https://github.com/d-zero-dev/nitpicker/compare/v0.4.0...v0.4.1) (2026-02-27)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @nitpicker/crawler
|
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2025 D-ZERO Co., Ltd.
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @nitpicker/crawler
|
|
2
|
+
|
|
3
|
+
ヘッドレスブラウザによる Web クローラーエンジン。
|
|
4
|
+
|
|
5
|
+
## 概要
|
|
6
|
+
|
|
7
|
+
Puppeteer を使用して Web サイトをクロールし、各ページのメタデータ・リンク構造・ネットワークリソース・レンダリング後 HTML スナップショットを SQLite ベースのアーカイブ(`.nitpicker`)に保存します。
|
|
8
|
+
|
|
9
|
+
このパッケージは [Nitpicker](../../README.md) モノレポの内部パッケージです。単体での利用は想定していません。
|
|
10
|
+
|
|
11
|
+
## ライセンス
|
|
12
|
+
|
|
13
|
+
Apache-2.0
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Database } from './database.js';
|
|
2
|
+
import type { DatabaseEvent, PageFilter } from './types.js';
|
|
3
|
+
import type { ParseURLOptions } from '@d-zero/shared/parse-url';
|
|
4
|
+
import { TypedAwaitEventEmitter as EventEmitter } from '@d-zero/shared/typed-await-event-emitter';
|
|
5
|
+
import Page from './page.js';
|
|
6
|
+
import Resource from './resource.js';
|
|
7
|
+
/**
|
|
8
|
+
* Provides read-only access to an archive's database and stored data files.
|
|
9
|
+
*
|
|
10
|
+
* This class is the base for the `Archive` class and is also returned
|
|
11
|
+
* by `Archive.connect` for read-only access to an existing archive.
|
|
12
|
+
* It supports querying pages, anchors, referrers, resources, and custom data.
|
|
13
|
+
*/
|
|
14
|
+
export declare class ArchiveAccessor extends EventEmitter<DatabaseEvent> {
|
|
15
|
+
#private;
|
|
16
|
+
/**
|
|
17
|
+
* The absolute path to the temporary working directory used by this accessor.
|
|
18
|
+
*/
|
|
19
|
+
get tmpDir(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new ArchiveAccessor instance.
|
|
22
|
+
* @param tmpDir - The path to the temporary directory containing the archive data.
|
|
23
|
+
* @param db - The Database instance for querying the SQLite database.
|
|
24
|
+
* @param namespace - An optional namespace for scoping custom data storage.
|
|
25
|
+
* When null, `setData` is not available.
|
|
26
|
+
*/
|
|
27
|
+
constructor(tmpDir: string, db: Database, namespace?: string | null);
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves anchor (link) data for a specific page by its database ID.
|
|
30
|
+
* @param pageId - The database ID of the page whose anchors to retrieve.
|
|
31
|
+
* @returns An array of anchor records found on the page.
|
|
32
|
+
*/
|
|
33
|
+
getAnchorsOnPage(pageId: number): Promise<any[]>;
|
|
34
|
+
/**
|
|
35
|
+
* Reads custom data stored in the archive by name.
|
|
36
|
+
* @param name - The base name of the data file (without extension).
|
|
37
|
+
* @param format - The file format: `'json'` (default), `'txt'`, or `'html'`.
|
|
38
|
+
* @returns The parsed JSON object for `'json'` format, or a string for `'txt'`/`'html'` format.
|
|
39
|
+
*/
|
|
40
|
+
getData<T>(name: string, format?: 'json'): Promise<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Reads custom data stored in the archive by name as a string.
|
|
43
|
+
* @param name - The base name of the data file (without extension).
|
|
44
|
+
* @param format - The file format: `'txt'` or `'html'`.
|
|
45
|
+
* @returns The file contents as a string.
|
|
46
|
+
*/
|
|
47
|
+
getData(name: string, format?: 'txt' | 'html'): Promise<string>;
|
|
48
|
+
/**
|
|
49
|
+
* Reads the HTML content of a page snapshot from the archive.
|
|
50
|
+
* Supports reading from both unzipped directories and zipped snapshot archives.
|
|
51
|
+
* @param filePath - The relative file path to the HTML snapshot, or null.
|
|
52
|
+
* @param openZipped - Whether to attempt unzipping the snapshot archive. Defaults to `true`.
|
|
53
|
+
* @returns The HTML content as a string, or null if the snapshot is not found or filePath is null.
|
|
54
|
+
*/
|
|
55
|
+
getHtmlOfPage(filePath: string | null, openZipped?: boolean): Promise<string | null>;
|
|
56
|
+
/**
|
|
57
|
+
* Retrieves all pages from the archive, optionally filtered by type.
|
|
58
|
+
* Eagerly loads redirect relationships (`redirectFrom`) but does NOT load
|
|
59
|
+
* anchor or referrer relationships.
|
|
60
|
+
* Use {@link getPagesWithRefs} if you need those relationships.
|
|
61
|
+
* @param filter - An optional filter to narrow the results (e.g., `'internal-page'`, `'external-page'`).
|
|
62
|
+
* @returns An array of {@link Page} instances.
|
|
63
|
+
*/
|
|
64
|
+
getPages(filter?: PageFilter): Promise<Page[]>;
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves pages with their related data (redirects, anchors, referrers) in batches.
|
|
67
|
+
* Processes pages in chunks of `limit` size, calling the callback for each batch.
|
|
68
|
+
* @param limit - The maximum number of pages to load per batch.
|
|
69
|
+
* @param callback - A function called for each batch of pages with the current offset and total count.
|
|
70
|
+
* @param options - Optional URL parsing options and whether to include referrer relationships.
|
|
71
|
+
*/
|
|
72
|
+
getPagesWithRefs(limit: number, callback: (pages: Page[], currentOffset: number, max: number) => void | Promise<void>, options?: ParseURLOptions & {
|
|
73
|
+
withRefs?: boolean;
|
|
74
|
+
}): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Retrieves pages that link to the specified page (incoming links).
|
|
77
|
+
* @param pageId - The database ID of the target page.
|
|
78
|
+
* @returns An array of referrer records.
|
|
79
|
+
*/
|
|
80
|
+
getReferrersOfPage(pageId: number): Promise<any[]>;
|
|
81
|
+
/**
|
|
82
|
+
* Retrieves page URLs that reference the specified resource.
|
|
83
|
+
* @param pageId - The database ID of the resource.
|
|
84
|
+
* @returns An array of page URL strings that reference this resource.
|
|
85
|
+
*/
|
|
86
|
+
getReferrersOfResource(pageId: number): Promise<string[]>;
|
|
87
|
+
/**
|
|
88
|
+
* Retrieves all sub-resources (CSS, JS, images, etc.) stored in the archive.
|
|
89
|
+
* @returns An array of {@link Resource} instances.
|
|
90
|
+
*/
|
|
91
|
+
getResources(): Promise<Resource[]>;
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves a flat list of all resource URLs stored in the archive.
|
|
94
|
+
* @returns An array of resource URL strings.
|
|
95
|
+
*/
|
|
96
|
+
getResourceUrlList(): Promise<any[]>;
|
|
97
|
+
/**
|
|
98
|
+
* Stores custom data in the archive under the configured namespace.
|
|
99
|
+
* Requires a namespace to be set on this accessor; throws if namespace is null.
|
|
100
|
+
* @param name - The base name of the data file (without extension).
|
|
101
|
+
* @param data - The data to store. For JSON format, this will be serialized. For text/HTML, it will be stringified.
|
|
102
|
+
* @param format - The file format: `'json'` (default), `'txt'`, or `'html'`.
|
|
103
|
+
* @returns The relative file path (from the tmp directory) of the stored data file.
|
|
104
|
+
* @throws {Error} If no namespace is set on this accessor.
|
|
105
|
+
*/
|
|
106
|
+
setData(name: string, data: unknown, format?: 'json' | 'txt' | 'html'): Promise<string>;
|
|
107
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { TypedAwaitEventEmitter as EventEmitter } from '@d-zero/shared/typed-await-event-emitter';
|
|
3
|
+
import { log } from './debug.js';
|
|
4
|
+
import { exists, extractZip, outputJSON, outputText, readJSON, readText, unzip, } from './filesystem/index.js';
|
|
5
|
+
import Page from './page.js';
|
|
6
|
+
import Resource from './resource.js';
|
|
7
|
+
import { safePath } from './safe-path.js';
|
|
8
|
+
/**
|
|
9
|
+
* Provides read-only access to an archive's database and stored data files.
|
|
10
|
+
*
|
|
11
|
+
* This class is the base for the `Archive` class and is also returned
|
|
12
|
+
* by `Archive.connect` for read-only access to an existing archive.
|
|
13
|
+
* It supports querying pages, anchors, referrers, resources, and custom data.
|
|
14
|
+
*/
|
|
15
|
+
export class ArchiveAccessor extends EventEmitter {
|
|
16
|
+
/** The SQLite database instance for querying archived data. */
|
|
17
|
+
#db;
|
|
18
|
+
/** Namespace prefix for custom data storage (e.g. `"analysis/plugin-name"`). `null` disables `setData`. */
|
|
19
|
+
#namespace = null;
|
|
20
|
+
/** Absolute path to the temporary working directory containing the database and files. */
|
|
21
|
+
#tmpDir;
|
|
22
|
+
/**
|
|
23
|
+
* The absolute path to the temporary working directory used by this accessor.
|
|
24
|
+
*/
|
|
25
|
+
get tmpDir() {
|
|
26
|
+
return this.#tmpDir;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new ArchiveAccessor instance.
|
|
30
|
+
* @param tmpDir - The path to the temporary directory containing the archive data.
|
|
31
|
+
* @param db - The Database instance for querying the SQLite database.
|
|
32
|
+
* @param namespace - An optional namespace for scoping custom data storage.
|
|
33
|
+
* When null, `setData` is not available.
|
|
34
|
+
*/
|
|
35
|
+
constructor(tmpDir, db, namespace = null) {
|
|
36
|
+
super();
|
|
37
|
+
this.#tmpDir = tmpDir;
|
|
38
|
+
this.#db = db;
|
|
39
|
+
this.#namespace = namespace;
|
|
40
|
+
this.#db.on('error', (e) => {
|
|
41
|
+
void this.emit('error', e);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Retrieves anchor (link) data for a specific page by its database ID.
|
|
46
|
+
* @param pageId - The database ID of the page whose anchors to retrieve.
|
|
47
|
+
* @returns An array of anchor records found on the page.
|
|
48
|
+
*/
|
|
49
|
+
async getAnchorsOnPage(pageId) {
|
|
50
|
+
const refs = await this.#db.getAnchorsOnPage(pageId);
|
|
51
|
+
return refs;
|
|
52
|
+
}
|
|
53
|
+
async getData(name, format = 'json') {
|
|
54
|
+
const namespace = this.#namespace || '';
|
|
55
|
+
const filePath = safePath(this.#tmpDir, namespace, `${name}.${format}`);
|
|
56
|
+
if (format === 'json') {
|
|
57
|
+
return await readJSON(filePath);
|
|
58
|
+
}
|
|
59
|
+
return await readText(filePath);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Reads the HTML content of a page snapshot from the archive.
|
|
63
|
+
* Supports reading from both unzipped directories and zipped snapshot archives.
|
|
64
|
+
* @param filePath - The relative file path to the HTML snapshot, or null.
|
|
65
|
+
* @param openZipped - Whether to attempt unzipping the snapshot archive. Defaults to `true`.
|
|
66
|
+
* @returns The HTML content as a string, or null if the snapshot is not found or filePath is null.
|
|
67
|
+
*/
|
|
68
|
+
async getHtmlOfPage(filePath, openZipped = true) {
|
|
69
|
+
if (!filePath) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
const snapshotDir = safePath(this.#tmpDir, path.dirname(filePath));
|
|
73
|
+
const name = path.basename(filePath);
|
|
74
|
+
if (openZipped) {
|
|
75
|
+
await unzip(`${snapshotDir}.zip`, snapshotDir);
|
|
76
|
+
}
|
|
77
|
+
if (exists(snapshotDir)) {
|
|
78
|
+
log('Load %s directly because snapshot dir is unzipped', name);
|
|
79
|
+
const html = await readText(path.resolve(snapshotDir, name)).catch((error) => error);
|
|
80
|
+
if (typeof html === 'string') {
|
|
81
|
+
log('Loaded: %s ...', html.split('\n')[0]);
|
|
82
|
+
return html;
|
|
83
|
+
}
|
|
84
|
+
log('Failed Loading: %O', html);
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
log('Extracts %s from zipped snapshots', name);
|
|
88
|
+
const zipDir = await extractZip(`${snapshotDir}.zip`);
|
|
89
|
+
const file = zipDir.files.find((f) => f.type === 'File' && f.path === name);
|
|
90
|
+
if (!file) {
|
|
91
|
+
log('Failed: Not found %s from zipped snapshots', name);
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
const buffer = await file.buffer();
|
|
95
|
+
const html = buffer.toString('utf8') || null;
|
|
96
|
+
log('Succeeded: Extracts %s from zipped snapshots', name);
|
|
97
|
+
return html;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Retrieves all pages from the archive, optionally filtered by type.
|
|
101
|
+
* Eagerly loads redirect relationships (`redirectFrom`) but does NOT load
|
|
102
|
+
* anchor or referrer relationships.
|
|
103
|
+
* Use {@link getPagesWithRefs} if you need those relationships.
|
|
104
|
+
* @param filter - An optional filter to narrow the results (e.g., `'internal-page'`, `'external-page'`).
|
|
105
|
+
* @returns An array of {@link Page} instances.
|
|
106
|
+
*/
|
|
107
|
+
async getPages(filter) {
|
|
108
|
+
const pages = await this.#db.getPages(filter);
|
|
109
|
+
if (pages.length === 0)
|
|
110
|
+
return [];
|
|
111
|
+
const pageIds = pages.map((p) => p.id);
|
|
112
|
+
const redirects = await this.#db.getRedirectsForPages(pageIds);
|
|
113
|
+
const redirectMap = new Map();
|
|
114
|
+
for (const redirect of redirects) {
|
|
115
|
+
const current = redirectMap.get(redirect.pageId);
|
|
116
|
+
if (current) {
|
|
117
|
+
current.push(redirect);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
redirectMap.set(redirect.pageId, [redirect]);
|
|
121
|
+
}
|
|
122
|
+
return pages.map((page) => new Page(this, page, redirectMap.get(page.id) || []));
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Retrieves pages with their related data (redirects, anchors, referrers) in batches.
|
|
126
|
+
* Processes pages in chunks of `limit` size, calling the callback for each batch.
|
|
127
|
+
* @param limit - The maximum number of pages to load per batch.
|
|
128
|
+
* @param callback - A function called for each batch of pages with the current offset and total count.
|
|
129
|
+
* @param options - Optional URL parsing options and whether to include referrer relationships.
|
|
130
|
+
*/
|
|
131
|
+
async getPagesWithRefs(limit, callback, options) {
|
|
132
|
+
const max = await this.#getPageCount();
|
|
133
|
+
let times = 0;
|
|
134
|
+
while (true) {
|
|
135
|
+
const offset = times * limit;
|
|
136
|
+
log('%d times loop: %o', times, {
|
|
137
|
+
offset,
|
|
138
|
+
limit,
|
|
139
|
+
max,
|
|
140
|
+
});
|
|
141
|
+
const pages = await this.#getPagesWithRels(offset, limit, options);
|
|
142
|
+
if (pages.length === 0) {
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
await callback(pages, offset, max);
|
|
146
|
+
times++;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Retrieves pages that link to the specified page (incoming links).
|
|
151
|
+
* @param pageId - The database ID of the target page.
|
|
152
|
+
* @returns An array of referrer records.
|
|
153
|
+
*/
|
|
154
|
+
async getReferrersOfPage(pageId) {
|
|
155
|
+
const refs = await this.#db.getReferrersOfPage(pageId);
|
|
156
|
+
return refs;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Retrieves page URLs that reference the specified resource.
|
|
160
|
+
* @param pageId - The database ID of the resource.
|
|
161
|
+
* @returns An array of page URL strings that reference this resource.
|
|
162
|
+
*/
|
|
163
|
+
async getReferrersOfResource(pageId) {
|
|
164
|
+
const refs = await this.#db.getReferrersOfResource(pageId);
|
|
165
|
+
return refs;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Retrieves all sub-resources (CSS, JS, images, etc.) stored in the archive.
|
|
169
|
+
* @returns An array of {@link Resource} instances.
|
|
170
|
+
*/
|
|
171
|
+
async getResources() {
|
|
172
|
+
const resources = await this.#db.getResources();
|
|
173
|
+
return resources.map((r) => new Resource(this, r));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Retrieves a flat list of all resource URLs stored in the archive.
|
|
177
|
+
* @returns An array of resource URL strings.
|
|
178
|
+
*/
|
|
179
|
+
async getResourceUrlList() {
|
|
180
|
+
return this.#db.getResourceUrlList();
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Stores custom data in the archive under the configured namespace.
|
|
184
|
+
* Requires a namespace to be set on this accessor; throws if namespace is null.
|
|
185
|
+
* @param name - The base name of the data file (without extension).
|
|
186
|
+
* @param data - The data to store. For JSON format, this will be serialized. For text/HTML, it will be stringified.
|
|
187
|
+
* @param format - The file format: `'json'` (default), `'txt'`, or `'html'`.
|
|
188
|
+
* @returns The relative file path (from the tmp directory) of the stored data file.
|
|
189
|
+
* @throws {Error} If no namespace is set on this accessor.
|
|
190
|
+
*/
|
|
191
|
+
async setData(name, data, format = 'json') {
|
|
192
|
+
if (this.#namespace == null) {
|
|
193
|
+
throw new Error('"setData" method of the ArchiveAccessor API must set namespace');
|
|
194
|
+
}
|
|
195
|
+
const filePath = safePath(this.#tmpDir, this.#namespace, `${name}.${format}`);
|
|
196
|
+
if (format === 'json') {
|
|
197
|
+
await outputJSON(filePath, data);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
await outputText(filePath, `${data}`);
|
|
201
|
+
}
|
|
202
|
+
return path.relative(this.#tmpDir, filePath);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Returns the total number of internal pages in the archive.
|
|
206
|
+
*/
|
|
207
|
+
async #getPageCount() {
|
|
208
|
+
return this.#db.getPageCount();
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Loads a batch of pages with their related data (redirects, anchors, referrers).
|
|
212
|
+
* When `withRefs` is false, loads only pages without relationships for better performance.
|
|
213
|
+
* @param offset - The number of pages to skip
|
|
214
|
+
* @param limit - The maximum number of pages to return
|
|
215
|
+
* @param options - URL parsing and referrer loading options
|
|
216
|
+
*/
|
|
217
|
+
async #getPagesWithRels(offset, limit, options) {
|
|
218
|
+
if (options?.withRefs === false) {
|
|
219
|
+
const pages = await this.#db.getPages('internal-page', offset, limit);
|
|
220
|
+
return pages.map((page) => new Page(this, page));
|
|
221
|
+
}
|
|
222
|
+
const { pages, redirects, anchors, referrers } = await this.#db.getPagesWithRels(offset, limit);
|
|
223
|
+
const redirectMap = new Map();
|
|
224
|
+
const anchorMap = new Map();
|
|
225
|
+
const refersMap = new Map();
|
|
226
|
+
log('Mapping redirects');
|
|
227
|
+
for (const redirect of redirects) {
|
|
228
|
+
const current = redirectMap.get(redirect.pageId);
|
|
229
|
+
if (current) {
|
|
230
|
+
current.push(redirect);
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
redirectMap.set(redirect.pageId, [redirect]);
|
|
234
|
+
}
|
|
235
|
+
log('Mapping anchors');
|
|
236
|
+
for (const anchor of anchors) {
|
|
237
|
+
const current = anchorMap.get(anchor.pageId);
|
|
238
|
+
if (current) {
|
|
239
|
+
current.push(anchor);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
anchorMap.set(anchor.pageId, [anchor]);
|
|
243
|
+
}
|
|
244
|
+
log('Mapping referrers');
|
|
245
|
+
for (const referrer of referrers) {
|
|
246
|
+
const current = refersMap.get(referrer.pageId);
|
|
247
|
+
if (current) {
|
|
248
|
+
current.push(referrer);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
refersMap.set(referrer.pageId, [referrer]);
|
|
252
|
+
}
|
|
253
|
+
log('Create Page Data');
|
|
254
|
+
const pPages = [];
|
|
255
|
+
for (const page of pages) {
|
|
256
|
+
const pRedirects = redirectMap.get(page.id) || [];
|
|
257
|
+
const pAnchors = anchorMap.get(page.id) || [];
|
|
258
|
+
const pRefers = refersMap.get(page.id) || [];
|
|
259
|
+
pPages.push(new Page(this, page, pRedirects, pAnchors, pRefers, options?.disableQueries));
|
|
260
|
+
}
|
|
261
|
+
log('Create Page Data: Done');
|
|
262
|
+
return pPages;
|
|
263
|
+
}
|
|
264
|
+
}
|