@nitpicker/crawler 0.22.2 → 0.24.0

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.
Files changed (158) hide show
  1. package/lib/archive/archive.d.ts +13 -5
  2. package/lib/archive/archive.js +16 -7
  3. package/lib/archive/cache/extract-archive-to-cache.d.ts +6 -1
  4. package/lib/archive/cache/extract-archive-to-cache.js +13 -6
  5. package/lib/archive/create-adjunct-tables.d.ts +0 -7
  6. package/lib/archive/create-adjunct-tables.js +0 -50
  7. package/lib/archive/create-template-tables.d.ts +30 -0
  8. package/lib/archive/create-template-tables.js +95 -0
  9. package/lib/archive/database.d.ts +1 -1
  10. package/lib/archive/database.js +1 -1
  11. package/lib/archive/db-ops/config/get-config.d.ts +1 -1
  12. package/lib/archive/db-ops/config/get-config.js +2 -1
  13. package/lib/archive/db-ops/config/info-column-allowlist.js +1 -0
  14. package/lib/archive/db-ops/config/info-json-columns.js +1 -0
  15. package/lib/archive/db-ops/lifecycle/init.js +2 -0
  16. package/lib/archive/db-ops/templates/assign-template-labels.d.ts +60 -0
  17. package/lib/archive/db-ops/templates/assign-template-labels.js +187 -0
  18. package/lib/archive/db-ops/{analysis → templates}/replace-page-templates.d.ts +19 -3
  19. package/lib/archive/db-ops/templates/replace-page-templates.js +174 -0
  20. package/lib/archive/db-ops/{analysis → templates}/types.d.ts +38 -0
  21. package/lib/archive/init-schema.d.ts +5 -1
  22. package/lib/archive/init-schema.js +14 -2
  23. package/lib/archive/migrate-info-request-header-names.d.ts +16 -0
  24. package/lib/archive/migrate-info-request-header-names.js +35 -0
  25. package/lib/archive/test-utils/build-full-schema-test-db.d.ts +34 -0
  26. package/lib/archive/test-utils/build-full-schema-test-db.js +74 -0
  27. package/lib/archive/transfer/attach-source-database.d.ts +31 -0
  28. package/lib/archive/transfer/attach-source-database.js +40 -0
  29. package/lib/archive/transfer/build-split-config.d.ts +30 -0
  30. package/lib/archive/transfer/build-split-config.js +42 -0
  31. package/lib/archive/transfer/clear-replaced-page-rows.d.ts +20 -0
  32. package/lib/archive/transfer/clear-replaced-page-rows.js +57 -0
  33. package/lib/archive/transfer/concat-archives.d.ts +55 -0
  34. package/lib/archive/transfer/concat-archives.js +95 -0
  35. package/lib/archive/transfer/content-item-rank-sql.d.ts +21 -0
  36. package/lib/archive/transfer/content-item-rank-sql.js +23 -0
  37. package/lib/archive/transfer/copy-analysis-violations.d.ts +19 -0
  38. package/lib/archive/transfer/copy-analysis-violations.js +43 -0
  39. package/lib/archive/transfer/copy-anchor-edges.d.ts +28 -0
  40. package/lib/archive/transfer/copy-anchor-edges.js +42 -0
  41. package/lib/archive/transfer/copy-dictionaries-for-concat.d.ts +39 -0
  42. package/lib/archive/transfer/copy-dictionaries-for-concat.js +81 -0
  43. package/lib/archive/transfer/copy-dictionaries-for-split.d.ts +29 -0
  44. package/lib/archive/transfer/copy-dictionaries-for-split.js +235 -0
  45. package/lib/archive/transfer/copy-header-set-children.d.ts +27 -0
  46. package/lib/archive/transfer/copy-header-set-children.js +51 -0
  47. package/lib/archive/transfer/copy-image-items.d.ts +21 -0
  48. package/lib/archive/transfer/copy-image-items.js +62 -0
  49. package/lib/archive/transfer/copy-inventory-source-lists.d.ts +30 -0
  50. package/lib/archive/transfer/copy-inventory-source-lists.js +61 -0
  51. package/lib/archive/transfer/copy-journal-tables.d.ts +30 -0
  52. package/lib/archive/transfer/copy-journal-tables.js +56 -0
  53. package/lib/archive/transfer/copy-page-console-logs.d.ts +16 -0
  54. package/lib/archive/transfer/copy-page-console-logs.js +27 -0
  55. package/lib/archive/transfer/copy-page-html-blobs.d.ts +31 -0
  56. package/lib/archive/transfer/copy-page-html-blobs.js +43 -0
  57. package/lib/archive/transfer/copy-page-meta.d.ts +27 -0
  58. package/lib/archive/transfer/copy-page-meta.js +68 -0
  59. package/lib/archive/transfer/copy-page-template-clusters.d.ts +31 -0
  60. package/lib/archive/transfer/copy-page-template-clusters.js +56 -0
  61. package/lib/archive/transfer/copy-page-template-labels.d.ts +34 -0
  62. package/lib/archive/transfer/copy-page-template-labels.js +88 -0
  63. package/lib/archive/transfer/copy-resource-items.d.ts +29 -0
  64. package/lib/archive/transfer/copy-resource-items.js +89 -0
  65. package/lib/archive/transfer/copy-resource-ref-edges.d.ts +20 -0
  66. package/lib/archive/transfer/copy-resource-ref-edges.js +33 -0
  67. package/lib/archive/transfer/copy-simple-page-scoped-tables.d.ts +15 -0
  68. package/lib/archive/transfer/copy-simple-page-scoped-tables.js +34 -0
  69. package/lib/archive/transfer/count-external-pages-in-scope.d.ts +26 -0
  70. package/lib/archive/transfer/count-external-pages-in-scope.js +40 -0
  71. package/lib/archive/transfer/create-transfer-temp-tables.d.ts +30 -0
  72. package/lib/archive/transfer/create-transfer-temp-tables.js +59 -0
  73. package/lib/archive/transfer/dictionary-copy-specs.d.ts +32 -0
  74. package/lib/archive/transfer/dictionary-copy-specs.js +29 -0
  75. package/lib/archive/transfer/drop-transfer-temp-tables.d.ts +9 -0
  76. package/lib/archive/transfer/drop-transfer-temp-tables.js +18 -0
  77. package/lib/archive/transfer/flatten-redirect-chains.d.ts +44 -0
  78. package/lib/archive/transfer/flatten-redirect-chains.js +74 -0
  79. package/lib/archive/transfer/insert-content-items.d.ts +35 -0
  80. package/lib/archive/transfer/insert-content-items.js +116 -0
  81. package/lib/archive/transfer/list-transfer-columns.d.ts +35 -0
  82. package/lib/archive/transfer/list-transfer-columns.js +45 -0
  83. package/lib/archive/transfer/mark-in-scope-content-items.d.ts +31 -0
  84. package/lib/archive/transfer/mark-in-scope-content-items.js +54 -0
  85. package/lib/archive/transfer/merge-archive-configs.d.ts +51 -0
  86. package/lib/archive/transfer/merge-archive-configs.js +85 -0
  87. package/lib/archive/transfer/plan-content-items-for-concat.d.ts +33 -0
  88. package/lib/archive/transfer/plan-content-items-for-concat.js +51 -0
  89. package/lib/archive/transfer/plan-content-items-for-split.d.ts +42 -0
  90. package/lib/archive/transfer/plan-content-items-for-split.js +83 -0
  91. package/lib/archive/transfer/plan-resource-items.d.ts +40 -0
  92. package/lib/archive/transfer/plan-resource-items.js +69 -0
  93. package/lib/archive/transfer/quote-transfer-identifier.d.ts +18 -0
  94. package/lib/archive/transfer/quote-transfer-identifier.js +20 -0
  95. package/lib/archive/transfer/reclassify-resource-externality.d.ts +37 -0
  96. package/lib/archive/transfer/reclassify-resource-externality.js +66 -0
  97. package/lib/archive/transfer/remap-redirect-dest-ids.d.ts +34 -0
  98. package/lib/archive/transfer/remap-redirect-dest-ids.js +51 -0
  99. package/lib/archive/transfer/replace-content-items.d.ts +39 -0
  100. package/lib/archive/transfer/replace-content-items.js +83 -0
  101. package/lib/archive/transfer/simple-page-scoped-tables.d.ts +25 -0
  102. package/lib/archive/transfer/simple-page-scoped-tables.js +38 -0
  103. package/lib/archive/transfer/split-archive.d.ts +55 -0
  104. package/lib/archive/transfer/split-archive.js +98 -0
  105. package/lib/archive/transfer/transfer-action.d.ts +34 -0
  106. package/lib/archive/transfer/transfer-action.js +32 -0
  107. package/lib/archive/transfer/transfer-archive-rows.d.ts +65 -0
  108. package/lib/archive/transfer/transfer-archive-rows.js +158 -0
  109. package/lib/archive/transfer/transfer-dictionary-tables.d.ts +12 -0
  110. package/lib/archive/transfer/transfer-dictionary-tables.js +23 -0
  111. package/lib/archive/transfer/transfer-source-alias.d.ts +9 -0
  112. package/lib/archive/transfer/transfer-source-alias.js +9 -0
  113. package/lib/archive/transfer/types.d.ts +111 -0
  114. package/lib/archive/transfer/types.js +35 -0
  115. package/lib/archive/types.d.ts +10 -0
  116. package/lib/concat-source-transfer-phases.d.ts +10 -0
  117. package/lib/concat-source-transfer-phases.js +17 -0
  118. package/lib/crawler/apply-request-header-interception.d.ts +22 -0
  119. package/lib/crawler/apply-request-header-interception.js +28 -0
  120. package/lib/crawler/build-scope-map.d.ts +21 -0
  121. package/lib/crawler/build-scope-map.js +32 -0
  122. package/lib/crawler/crawler.js +34 -0
  123. package/lib/crawler/create-scoped-header-request-handler.d.ts +40 -0
  124. package/lib/crawler/create-scoped-header-request-handler.js +64 -0
  125. package/lib/crawler/create-strip-headers-on-redirect.d.ts +41 -0
  126. package/lib/crawler/create-strip-headers-on-redirect.js +67 -0
  127. package/lib/crawler/fetch-destination.d.ts +14 -0
  128. package/lib/crawler/fetch-destination.js +56 -9
  129. package/lib/crawler/fetch-robots-txt.d.ts +7 -0
  130. package/lib/crawler/fetch-robots-txt.js +7 -0
  131. package/lib/crawler/scan-js-resource-for-license-comment.d.ts +6 -0
  132. package/lib/crawler/scan-js-resource-for-license-comment.js +6 -0
  133. package/lib/crawler/types.d.ts +44 -0
  134. package/lib/crawler-orchestrator.d.ts +6 -0
  135. package/lib/crawler-orchestrator.js +17 -17
  136. package/lib/crawler.d.ts +14 -1
  137. package/lib/crawler.js +23 -0
  138. package/lib/split-source-transfer-phases.d.ts +11 -0
  139. package/lib/split-source-transfer-phases.js +18 -0
  140. package/lib/template-classification/classify-archive-page-templates.d.ts +41 -0
  141. package/lib/template-classification/classify-archive-page-templates.js +79 -0
  142. package/lib/template-classification/classify-page-templates.d.ts +37 -0
  143. package/lib/template-classification/classify-page-templates.js +69 -0
  144. package/lib/template-classification/collect-page-stylesheet-urls.d.ts +32 -0
  145. package/lib/template-classification/collect-page-stylesheet-urls.js +130 -0
  146. package/lib/template-classification/create-content-root-hint.d.ts +36 -0
  147. package/lib/template-classification/create-content-root-hint.js +47 -0
  148. package/lib/template-classification/create-page-cluster-factory.d.ts +60 -0
  149. package/lib/template-classification/create-page-cluster-factory.js +98 -0
  150. package/lib/template-classification/types.d.ts +108 -0
  151. package/lib/template-classification/types.js +1 -0
  152. package/lib/transfer-post-phases.d.ts +10 -0
  153. package/lib/transfer-post-phases.js +13 -0
  154. package/lib/utils/object/redact-request-headers.d.ts +15 -0
  155. package/lib/utils/object/redact-request-headers.js +27 -0
  156. package/package.json +4 -3
  157. package/lib/archive/db-ops/analysis/replace-page-templates.js +0 -84
  158. /package/lib/archive/db-ops/{analysis → templates}/types.js +0 -0
@@ -0,0 +1,98 @@
1
+ import { createContentRootHint } from './create-content-root-hint.js';
2
+ /**
3
+ * Builds the `PageFactory` that `@d-zero/page-cluster`'s
4
+ * `resolvePageClusterKeys` reads the corpus through, plus a way to read back
5
+ * the URL list it actually yielded (see below for why that can't be known
6
+ * synchronously up front).
7
+ *
8
+ * Only internal HTML pages (`Page.isInternalPage()` — content-type based,
9
+ * matching the "a page is judged by content-type, not `isTarget`" archive
10
+ * invariant) with retrievable HTML are yielded. A page can be an internal
11
+ * HTML page and still have no retrievable HTML (e.g. a skipped or
12
+ * self-heal-affected row) — `page.getHtml()` returning falsy is only known
13
+ * by actually calling it, not from any synchronous field.
14
+ *
15
+ * `resolvePageClusterKeys` calls the returned factory one or more times
16
+ * (once for corpora at or below its inline threshold, twice above it — see
17
+ * its own docs) to re-read the corpus. Whichever pages end up excluded
18
+ * (empty HTML) must be excluded **identically** on every call, or the
19
+ * pass-to-pass page sequence drifts and the library's same-order contract
20
+ * breaks. Excluding by `isInternalPage()` is safe (synchronous,
21
+ * deterministic per page). Excluding by empty `getHtml()` is also safe,
22
+ * but only because the archive is immutable for the duration of one
23
+ * classification run — the same page's HTML never changes between calls.
24
+ *
25
+ * Each yielded signal carries the page's `paths`, `stylesheetHrefs`, `html`,
26
+ * `host`, and — when the crawler detected a main-content element for it —
27
+ * a `contentRoot` hint (see {@link createContentRootHint}) that
28
+ * `@d-zero/page-cluster` anchors its content-depth cap on.
29
+ *
30
+ * `getYieldedUrls()` reflects the URLs yielded by the **most recently
31
+ * completed** full iteration of the factory's generator, deliberately not a
32
+ * separate up-front probe pass over every candidate's HTML: every documented
33
+ * call shape of `resolvePageClusterKeys` fully drains the factory it's given
34
+ * (that's what "reading the factory" means in its own docs), so by the time
35
+ * `resolvePageClusterKeys` resolves, at least one full drain has already
36
+ * happened and `getYieldedUrls()` reflects it — a dedicated probe pass would
37
+ * only add a full corpus-wide decompression sweep beyond the library's own
38
+ * 1-2, for no benefit. `classifyPageTemplates` still defends against a
39
+ * violated assumption here with a hard length check against
40
+ * `resolvePageClusterKeys`'s result array.
41
+ * @param pages - Candidate pages (as already loaded by
42
+ * `classifyArchivePageTemplates` — this function does not re-query the archive).
43
+ * @param stylesheetsByUrl - Output of `collectPageStylesheetUrls`, keyed by
44
+ * `page.url.href`.
45
+ * @returns `factory` to pass to `resolvePageClusterKeys`, and
46
+ * `getYieldedUrls()` — call this only *after* `resolvePageClusterKeys`
47
+ * resolves; its result array has exactly the length and order of
48
+ * `getYieldedUrls()` at that point.
49
+ * @example
50
+ * ```ts
51
+ * const { factory, getYieldedUrls } = createPageClusterFactory(pages, stylesheetsByUrl);
52
+ * const clusterKeys = await resolvePageClusterKeys(factory);
53
+ * const yieldedUrls = getYieldedUrls(); // safe to read now
54
+ * ```
55
+ */
56
+ export function createPageClusterFactory(pages, stylesheetsByUrl) {
57
+ const candidates = pages.filter((page) => page.isInternalPage());
58
+ let yieldedUrls = [];
59
+ const factory = () => yieldPageClusterSignals(candidates, stylesheetsByUrl, (urls) => {
60
+ yieldedUrls = urls;
61
+ });
62
+ return { factory, getYieldedUrls: () => yieldedUrls };
63
+ }
64
+ /**
65
+ * The actual per-call iterator body for {@link createPageClusterFactory}'s
66
+ * `PageFactory`. HTML is fetched lazily, one page at a time, so the archive
67
+ * never holds more than one page's decompressed HTML in memory regardless
68
+ * of corpus size — matching `@d-zero/page-cluster`'s own memory-bounding
69
+ * design for its multi-pass reads.
70
+ * @param candidates - Pre-filtered internal HTML pages, in fixed order.
71
+ * @param stylesheetsByUrl - See {@link createPageClusterFactory}.
72
+ * @param onDrained - Called with every yielded URL, in order, once this
73
+ * particular iteration reaches the end of `candidates` — never called if
74
+ * the consumer stops iterating early.
75
+ * @yields {PageClusterSignals} One per candidate page with retrievable HTML.
76
+ */
77
+ async function* yieldPageClusterSignals(candidates, stylesheetsByUrl, onDrained) {
78
+ const yieldedUrls = [];
79
+ for (const page of candidates) {
80
+ const html = await page.getHtml();
81
+ if (!html) {
82
+ continue;
83
+ }
84
+ const url = page.url;
85
+ yieldedUrls.push(url.href);
86
+ const contentRoot = createContentRootHint(page);
87
+ yield {
88
+ paths: url.paths,
89
+ stylesheetHrefs: stylesheetsByUrl.get(url.href) ?? [],
90
+ html,
91
+ host: url.port ? `${url.hostname}:${url.port}` : url.hostname,
92
+ // Left off entirely (not `undefined`) for pages with no detected
93
+ // main content, so the yielded shape carries no undefined-valued key.
94
+ ...(contentRoot ? { contentRoot } : {}),
95
+ };
96
+ }
97
+ onDrained(yieldedUrls);
98
+ }
@@ -0,0 +1,108 @@
1
+ import type Archive from '../archive/archive.js';
2
+ import type { TemplateClusterReason } from '../archive/db-ops/templates/types.js';
3
+ import type Page from '../archive/page.js';
4
+ /**
5
+ * Progress of the clustering step itself. nitpicker's own copy of
6
+ * `@d-zero/page-cluster`'s `ProgressEvent` (which is structurally assignable
7
+ * to this type), kept independent of that package so `@nitpicker/cli` can
8
+ * format progress without depending on `@d-zero/page-cluster` — the same
9
+ * reason {@link TemplateClusterReason} mirrors `ClusterReason`.
10
+ */
11
+ export type TemplateClusteringProgress = {
12
+ readonly phase: 'pass0-signals';
13
+ readonly pagesSeen: number;
14
+ } | {
15
+ readonly phase: 'pass1-block-complete';
16
+ readonly blockKey: string;
17
+ readonly blocksProcessed: number;
18
+ readonly totalBlocks: number;
19
+ } | {
20
+ readonly phase: 'pass1b-assign';
21
+ readonly pagesAssigned: number;
22
+ readonly pagesToAssign: number;
23
+ } | {
24
+ readonly phase: 'stage-b-start';
25
+ readonly unitCount: number;
26
+ };
27
+ /**
28
+ * Progress of the whole crawl-end classification
29
+ * ({@link import('./classify-archive-page-templates.js').classifyArchivePageTemplates}):
30
+ * loading every page handle out of the archive, gathering stylesheet
31
+ * references, the clustering phases, then writing the result.
32
+ *
33
+ * `loading-pages-start`, `collecting-stylesheets` and `writing-results` carry
34
+ * no count: each is a multi-second step with no natural progress unit (a
35
+ * single batched query, three chained SQL passes, one write transaction), so
36
+ * they only announce that the step began — otherwise the previous step's last
37
+ * message would stay on screen and look like a stall.
38
+ */
39
+ export type TemplateClassificationProgress = {
40
+ readonly phase: 'loading-pages-start';
41
+ } | {
42
+ readonly phase: 'loading-pages';
43
+ readonly done: number;
44
+ readonly total: number;
45
+ } | {
46
+ readonly phase: 'collecting-stylesheets';
47
+ } | TemplateClusteringProgress | {
48
+ readonly phase: 'writing-results';
49
+ readonly templateCount: number;
50
+ };
51
+ /**
52
+ * Options for {@link import('./classify-page-templates.js').classifyPageTemplates}.
53
+ */
54
+ export interface ClassifyPageTemplatesOptions {
55
+ /** The archive to read stylesheet references from. */
56
+ archive: Archive;
57
+ /**
58
+ * Every page of the archive. Classification is a corpus-wide batch
59
+ * computation: template keys are only comparable within one call, so the
60
+ * pages must never be passed in per-batch slices.
61
+ */
62
+ pages: readonly Page[];
63
+ /**
64
+ * Progress callback forwarded to `@d-zero/page-cluster`'s
65
+ * `resolvePageClusterKeys`, so long-running classification on large
66
+ * archives isn't silently unresponsive. Independent of cluster-reason
67
+ * capture — `@d-zero/page-cluster` 0.5.3+ composes `onProgress` and
68
+ * `onClusterReason` without either one demoting the corpus off its
69
+ * progress-emitting path (see `classifyPageTemplates`'s own JSDoc).
70
+ */
71
+ onProgress?: (event: TemplateClassificationProgress) => void;
72
+ }
73
+ /**
74
+ * Result of {@link import('./classify-page-templates.js').classifyPageTemplates}.
75
+ */
76
+ export interface PageTemplateClassification {
77
+ /**
78
+ * Page URL (`page.url.href`) → template key. Only internal HTML pages
79
+ * with retrievable HTML have an entry.
80
+ */
81
+ readonly templateKeysByUrl: ReadonlyMap<string, string>;
82
+ /**
83
+ * Template key → `@d-zero/page-cluster`'s cluster-selection evidence for
84
+ * that key. **Not guaranteed to cover every key in `templateKeysByUrl`**:
85
+ * `@d-zero/page-cluster` only emits a reason for a final cluster it still
86
+ * holds full grouping state for at the moment `onClusterReason` fires,
87
+ * which is a best-effort side channel rather than a per-page guarantee
88
+ * (unlike `templateKeysByUrl`, which is verified 1:1 against the yielded
89
+ * page set — see the hard length check in `classifyPageTemplates`).
90
+ */
91
+ readonly clusterReasonsByTemplateKey: ReadonlyMap<string, TemplateClusterReason>;
92
+ }
93
+ /**
94
+ * Options for {@link import('./classify-archive-page-templates.js').classifyArchivePageTemplates}.
95
+ */
96
+ export interface ClassifyArchivePageTemplatesOptions {
97
+ /** Receives loading and clustering progress; omit for a silent run. */
98
+ readonly onProgress?: (event: TemplateClassificationProgress) => void;
99
+ }
100
+ /**
101
+ * Result of {@link import('./classify-archive-page-templates.js').classifyArchivePageTemplates}.
102
+ */
103
+ export interface ClassifyArchivePageTemplatesResult {
104
+ /** Number of pages that received a template key. */
105
+ readonly classifiedPageCount: number;
106
+ /** Number of distinct template keys among them. */
107
+ readonly templateCount: number;
108
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { TransferPhase } from './archive/transfer/types.js';
2
+ /**
3
+ * Ordered whole-operation phase sequence `concatArchives`/`splitArchive`
4
+ * announce (via `sourceIndex === sources.length`) once every source has
5
+ * been transferred: `flattenRedirectChains` → `reclassifyResourceExternality`
6
+ * → `countExternalPagesInScope`. Identical for both commands — split's
7
+ * single source is already flat and its scope only shrinks, so the first
8
+ * two are cheap no-ops there, but the sequence itself does not branch.
9
+ */
10
+ export declare const TRANSFER_POST_PHASES: readonly TransferPhase[];
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Ordered whole-operation phase sequence `concatArchives`/`splitArchive`
3
+ * announce (via `sourceIndex === sources.length`) once every source has
4
+ * been transferred: `flattenRedirectChains` → `reclassifyResourceExternality`
5
+ * → `countExternalPagesInScope`. Identical for both commands — split's
6
+ * single source is already flat and its scope only shrinks, so the first
7
+ * two are cheap no-ops there, but the sequence itself does not branch.
8
+ */
9
+ export const TRANSFER_POST_PHASES = [
10
+ 'flatteningRedirects',
11
+ 'reclassifyingResources',
12
+ 'countingExternalInScope',
13
+ ];
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Returns a copy of `config` whose `requestHeaders` values are replaced with
3
+ * `***`, leaving the header names visible. Use it before a config object is
4
+ * logged or printed.
5
+ *
6
+ * WHY: request header values are credentials. The runtime config that flows
7
+ * through the orchestrator carries them, and it is dumped by `debug` logs and
8
+ * the crawl-start header — neither may show a token.
9
+ * @param config - Any config object that may carry `requestHeaders`.
10
+ * @returns The same object when it has no `requestHeaders`, otherwise a shallow copy with masked values.
11
+ * @example
12
+ * redactRequestHeaders({ userAgent: 'x', requestHeaders: { Authorization: 'Bearer t' } });
13
+ * // => { userAgent: 'x', requestHeaders: { Authorization: '***' } }
14
+ */
15
+ export declare function redactRequestHeaders<T extends object>(config: T): T;
@@ -0,0 +1,27 @@
1
+ /** Placeholder shown instead of a request header value. */
2
+ const REDACTED = '***';
3
+ /**
4
+ * Returns a copy of `config` whose `requestHeaders` values are replaced with
5
+ * `***`, leaving the header names visible. Use it before a config object is
6
+ * logged or printed.
7
+ *
8
+ * WHY: request header values are credentials. The runtime config that flows
9
+ * through the orchestrator carries them, and it is dumped by `debug` logs and
10
+ * the crawl-start header — neither may show a token.
11
+ * @param config - Any config object that may carry `requestHeaders`.
12
+ * @returns The same object when it has no `requestHeaders`, otherwise a shallow copy with masked values.
13
+ * @example
14
+ * redactRequestHeaders({ userAgent: 'x', requestHeaders: { Authorization: 'Bearer t' } });
15
+ * // => { userAgent: 'x', requestHeaders: { Authorization: '***' } }
16
+ */
17
+ export function redactRequestHeaders(config) {
18
+ const { requestHeaders } = config;
19
+ if (!requestHeaders) {
20
+ return config;
21
+ }
22
+ const masked = {};
23
+ for (const name of Object.keys(requestHeaders)) {
24
+ masked[name] = REDACTED;
25
+ }
26
+ return { ...config, requestHeaders: masked };
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitpicker/crawler",
3
- "version": "0.22.2",
3
+ "version": "0.24.0",
4
4
  "description": "Web crawler engine with headless browser rendering and archive storage",
5
5
  "author": "D-ZERO",
6
6
  "license": "Apache-2.0",
@@ -33,11 +33,12 @@
33
33
  "@d-zero/beholder": "5.0.0",
34
34
  "@d-zero/dealer": "1.14.0",
35
35
  "@d-zero/fs": "0.2.7",
36
+ "@d-zero/page-cluster": "0.8.0",
36
37
  "@d-zero/shared": "0.23.0",
37
38
  "ansi-colors": "4.1.3",
38
39
  "debug": "4.4.3",
39
40
  "follow-redirects": "1.16.0",
40
- "fs-extra": "11.4.0",
41
+ "fs-extra": "11.4.1",
41
42
  "knex": "3.3.0",
42
43
  "libsql": "0.5.29",
43
44
  "puppeteer": "25.5.0",
@@ -51,5 +52,5 @@
51
52
  "@types/tar": "7.0.87",
52
53
  "@types/unzipper": "0.10.11"
53
54
  },
54
- "gitHead": "27db72abdebd19dc6f83a1b5087d73d8b574f229"
55
+ "gitHead": "840a594573c4abe0aaff1257469b7f2108fcdb19"
55
56
  }
@@ -1,84 +0,0 @@
1
- import { eachSplitted } from '../../../utils/array/each-splitted.js';
2
- import { compressPayload } from '../_shared/compress-payload.js';
3
- /**
4
- * Replaces the stored DOM-structure template classification (`--templates`)
5
- * with a freshly generated set.
6
- *
7
- * Every classified page is a full-archive, all-or-nothing recomputation
8
- * (see `@nitpicker/core`'s `classifyPageTemplates`), so this always deletes
9
- * every existing row before inserting the new set — there is no per-page
10
- * incremental update path, matching `replaceAnalysisViolations`'s
11
- * whole-table replace shape. Unlike violations, a page whose URL can't be
12
- * resolved back to a `content_items` row is silently skipped rather than
13
- * treated as a hard failure: losing one page's template classification
14
- * (e.g. a URL-normalization mismatch between the in-memory `Page.url.href`
15
- * and the stored `url_refs.url`) should not discard the rest of a
16
- * potentially multi-thousand-page classification run.
17
- *
18
- * `page_template_clusters` is always cleared alongside `page_templates`
19
- * regardless of whether `clusterReasonsByTemplateKey` is passed — "no
20
- * reason" must mean "not captured for this run", never "carry over the
21
- * previous run's reason". Reason rows are inserted for every key in
22
- * `clusterReasonsByTemplateKey` even if some have no surviving member page
23
- * in `templateKeysByUrl` after URL-resolution skips above — harmless
24
- * (nothing joins `page_template_clusters` back to `page_templates` by FK;
25
- * see the table's own JSDoc), and simpler than cross-filtering the two maps.
26
- * @param knex - Knex query builder connected to the archive DB.
27
- * @param params - See {@link ReplacePageTemplatesParams}.
28
- */
29
- export async function replacePageTemplates(knex, params) {
30
- const { templateKeysByUrl, clusterReasonsByTemplateKey } = params;
31
- // Compressing every reason is pure CPU work independent of the DB — done
32
- // before opening the transaction below so it doesn't extend how long the
33
- // SQLite write-lock is held for.
34
- const reasonRows = clusterReasonsByTemplateKey && clusterReasonsByTemplateKey.size > 0
35
- ? [...clusterReasonsByTemplateKey].map(([templateKey, reason]) => {
36
- const { body, codec, sizeRaw, sizeStored } = compressPayload(Buffer.from(JSON.stringify(reason), 'utf8'));
37
- return {
38
- template_key: templateKey,
39
- member_count: reason.memberCount,
40
- reason_json: body,
41
- codec,
42
- size_raw: sizeRaw,
43
- size_stored: sizeStored,
44
- };
45
- })
46
- : [];
47
- await knex.transaction(async (trx) => {
48
- await trx('page_templates').delete();
49
- await trx('page_template_clusters').delete();
50
- if (reasonRows.length > 0) {
51
- await eachSplitted(reasonRows, 100, async (chunk) => {
52
- await trx('page_template_clusters').insert(chunk);
53
- });
54
- }
55
- if (templateKeysByUrl.size === 0) {
56
- return;
57
- }
58
- const urls = [...templateKeysByUrl.keys()];
59
- const pageIdByUrl = new Map();
60
- await eachSplitted(urls, 500, async (chunk) => {
61
- const pageRows = await trx('content_items')
62
- .join('url_refs', 'url_refs.id', 'content_items.url_id')
63
- .select('content_items.id as id', 'url_refs.url as url')
64
- .whereIn('url_refs.url', chunk);
65
- for (const row of pageRows) {
66
- pageIdByUrl.set(row.url, row.id);
67
- }
68
- });
69
- const rows = [];
70
- for (const [url, templateKey] of templateKeysByUrl) {
71
- const pageId = pageIdByUrl.get(url);
72
- if (pageId == null) {
73
- continue;
74
- }
75
- rows.push({ page_id: pageId, template_key: templateKey });
76
- }
77
- if (rows.length === 0) {
78
- return;
79
- }
80
- await eachSplitted(rows, 500, async (chunk) => {
81
- await trx('page_templates').insert(chunk);
82
- });
83
- });
84
- }