@nitpicker/crawler 0.22.2 → 0.23.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 (133) hide show
  1. package/lib/archive/archive.d.ts +13 -5
  2. package/lib/archive/archive.js +13 -5
  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/templates/assign-template-labels.d.ts +60 -0
  12. package/lib/archive/db-ops/templates/assign-template-labels.js +187 -0
  13. package/lib/archive/db-ops/{analysis → templates}/replace-page-templates.d.ts +19 -3
  14. package/lib/archive/db-ops/templates/replace-page-templates.js +174 -0
  15. package/lib/archive/db-ops/{analysis → templates}/types.d.ts +38 -0
  16. package/lib/archive/init-schema.d.ts +5 -1
  17. package/lib/archive/init-schema.js +13 -2
  18. package/lib/archive/test-utils/build-full-schema-test-db.d.ts +34 -0
  19. package/lib/archive/test-utils/build-full-schema-test-db.js +74 -0
  20. package/lib/archive/transfer/attach-source-database.d.ts +31 -0
  21. package/lib/archive/transfer/attach-source-database.js +40 -0
  22. package/lib/archive/transfer/build-split-config.d.ts +30 -0
  23. package/lib/archive/transfer/build-split-config.js +42 -0
  24. package/lib/archive/transfer/clear-replaced-page-rows.d.ts +20 -0
  25. package/lib/archive/transfer/clear-replaced-page-rows.js +57 -0
  26. package/lib/archive/transfer/concat-archives.d.ts +55 -0
  27. package/lib/archive/transfer/concat-archives.js +95 -0
  28. package/lib/archive/transfer/content-item-rank-sql.d.ts +21 -0
  29. package/lib/archive/transfer/content-item-rank-sql.js +23 -0
  30. package/lib/archive/transfer/copy-analysis-violations.d.ts +19 -0
  31. package/lib/archive/transfer/copy-analysis-violations.js +43 -0
  32. package/lib/archive/transfer/copy-anchor-edges.d.ts +28 -0
  33. package/lib/archive/transfer/copy-anchor-edges.js +42 -0
  34. package/lib/archive/transfer/copy-dictionaries-for-concat.d.ts +39 -0
  35. package/lib/archive/transfer/copy-dictionaries-for-concat.js +81 -0
  36. package/lib/archive/transfer/copy-dictionaries-for-split.d.ts +29 -0
  37. package/lib/archive/transfer/copy-dictionaries-for-split.js +235 -0
  38. package/lib/archive/transfer/copy-header-set-children.d.ts +27 -0
  39. package/lib/archive/transfer/copy-header-set-children.js +51 -0
  40. package/lib/archive/transfer/copy-image-items.d.ts +21 -0
  41. package/lib/archive/transfer/copy-image-items.js +62 -0
  42. package/lib/archive/transfer/copy-inventory-source-lists.d.ts +30 -0
  43. package/lib/archive/transfer/copy-inventory-source-lists.js +61 -0
  44. package/lib/archive/transfer/copy-journal-tables.d.ts +30 -0
  45. package/lib/archive/transfer/copy-journal-tables.js +56 -0
  46. package/lib/archive/transfer/copy-page-console-logs.d.ts +16 -0
  47. package/lib/archive/transfer/copy-page-console-logs.js +27 -0
  48. package/lib/archive/transfer/copy-page-html-blobs.d.ts +31 -0
  49. package/lib/archive/transfer/copy-page-html-blobs.js +43 -0
  50. package/lib/archive/transfer/copy-page-meta.d.ts +27 -0
  51. package/lib/archive/transfer/copy-page-meta.js +68 -0
  52. package/lib/archive/transfer/copy-page-template-clusters.d.ts +31 -0
  53. package/lib/archive/transfer/copy-page-template-clusters.js +56 -0
  54. package/lib/archive/transfer/copy-page-template-labels.d.ts +34 -0
  55. package/lib/archive/transfer/copy-page-template-labels.js +88 -0
  56. package/lib/archive/transfer/copy-resource-items.d.ts +29 -0
  57. package/lib/archive/transfer/copy-resource-items.js +89 -0
  58. package/lib/archive/transfer/copy-resource-ref-edges.d.ts +20 -0
  59. package/lib/archive/transfer/copy-resource-ref-edges.js +33 -0
  60. package/lib/archive/transfer/copy-simple-page-scoped-tables.d.ts +15 -0
  61. package/lib/archive/transfer/copy-simple-page-scoped-tables.js +34 -0
  62. package/lib/archive/transfer/count-external-pages-in-scope.d.ts +26 -0
  63. package/lib/archive/transfer/count-external-pages-in-scope.js +40 -0
  64. package/lib/archive/transfer/create-transfer-temp-tables.d.ts +30 -0
  65. package/lib/archive/transfer/create-transfer-temp-tables.js +59 -0
  66. package/lib/archive/transfer/dictionary-copy-specs.d.ts +32 -0
  67. package/lib/archive/transfer/dictionary-copy-specs.js +29 -0
  68. package/lib/archive/transfer/drop-transfer-temp-tables.d.ts +9 -0
  69. package/lib/archive/transfer/drop-transfer-temp-tables.js +18 -0
  70. package/lib/archive/transfer/flatten-redirect-chains.d.ts +44 -0
  71. package/lib/archive/transfer/flatten-redirect-chains.js +74 -0
  72. package/lib/archive/transfer/insert-content-items.d.ts +35 -0
  73. package/lib/archive/transfer/insert-content-items.js +116 -0
  74. package/lib/archive/transfer/list-transfer-columns.d.ts +35 -0
  75. package/lib/archive/transfer/list-transfer-columns.js +45 -0
  76. package/lib/archive/transfer/mark-in-scope-content-items.d.ts +31 -0
  77. package/lib/archive/transfer/mark-in-scope-content-items.js +54 -0
  78. package/lib/archive/transfer/merge-archive-configs.d.ts +48 -0
  79. package/lib/archive/transfer/merge-archive-configs.js +78 -0
  80. package/lib/archive/transfer/plan-content-items-for-concat.d.ts +33 -0
  81. package/lib/archive/transfer/plan-content-items-for-concat.js +51 -0
  82. package/lib/archive/transfer/plan-content-items-for-split.d.ts +42 -0
  83. package/lib/archive/transfer/plan-content-items-for-split.js +83 -0
  84. package/lib/archive/transfer/plan-resource-items.d.ts +40 -0
  85. package/lib/archive/transfer/plan-resource-items.js +69 -0
  86. package/lib/archive/transfer/quote-transfer-identifier.d.ts +18 -0
  87. package/lib/archive/transfer/quote-transfer-identifier.js +20 -0
  88. package/lib/archive/transfer/reclassify-resource-externality.d.ts +37 -0
  89. package/lib/archive/transfer/reclassify-resource-externality.js +66 -0
  90. package/lib/archive/transfer/remap-redirect-dest-ids.d.ts +34 -0
  91. package/lib/archive/transfer/remap-redirect-dest-ids.js +51 -0
  92. package/lib/archive/transfer/replace-content-items.d.ts +39 -0
  93. package/lib/archive/transfer/replace-content-items.js +83 -0
  94. package/lib/archive/transfer/simple-page-scoped-tables.d.ts +25 -0
  95. package/lib/archive/transfer/simple-page-scoped-tables.js +38 -0
  96. package/lib/archive/transfer/split-archive.d.ts +55 -0
  97. package/lib/archive/transfer/split-archive.js +98 -0
  98. package/lib/archive/transfer/transfer-action.d.ts +34 -0
  99. package/lib/archive/transfer/transfer-action.js +32 -0
  100. package/lib/archive/transfer/transfer-archive-rows.d.ts +65 -0
  101. package/lib/archive/transfer/transfer-archive-rows.js +158 -0
  102. package/lib/archive/transfer/transfer-dictionary-tables.d.ts +12 -0
  103. package/lib/archive/transfer/transfer-dictionary-tables.js +23 -0
  104. package/lib/archive/transfer/transfer-source-alias.d.ts +9 -0
  105. package/lib/archive/transfer/transfer-source-alias.js +9 -0
  106. package/lib/archive/transfer/types.d.ts +111 -0
  107. package/lib/archive/transfer/types.js +35 -0
  108. package/lib/concat-source-transfer-phases.d.ts +10 -0
  109. package/lib/concat-source-transfer-phases.js +17 -0
  110. package/lib/crawler/build-scope-map.d.ts +21 -0
  111. package/lib/crawler/build-scope-map.js +32 -0
  112. package/lib/crawler-orchestrator.js +3 -16
  113. package/lib/crawler.d.ts +13 -1
  114. package/lib/crawler.js +22 -0
  115. package/lib/split-source-transfer-phases.d.ts +11 -0
  116. package/lib/split-source-transfer-phases.js +18 -0
  117. package/lib/template-classification/classify-archive-page-templates.d.ts +41 -0
  118. package/lib/template-classification/classify-archive-page-templates.js +79 -0
  119. package/lib/template-classification/classify-page-templates.d.ts +37 -0
  120. package/lib/template-classification/classify-page-templates.js +69 -0
  121. package/lib/template-classification/collect-page-stylesheet-urls.d.ts +32 -0
  122. package/lib/template-classification/collect-page-stylesheet-urls.js +130 -0
  123. package/lib/template-classification/create-content-root-hint.d.ts +36 -0
  124. package/lib/template-classification/create-content-root-hint.js +47 -0
  125. package/lib/template-classification/create-page-cluster-factory.d.ts +60 -0
  126. package/lib/template-classification/create-page-cluster-factory.js +98 -0
  127. package/lib/template-classification/types.d.ts +108 -0
  128. package/lib/template-classification/types.js +1 -0
  129. package/lib/transfer-post-phases.d.ts +10 -0
  130. package/lib/transfer-post-phases.js +13 -0
  131. package/package.json +3 -2
  132. package/lib/archive/db-ops/analysis/replace-page-templates.js +0 -84
  133. /package/lib/archive/db-ops/{analysis → templates}/types.js +0 -0
@@ -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
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitpicker/crawler",
3
- "version": "0.22.2",
3
+ "version": "0.23.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,6 +33,7 @@
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",
@@ -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": "c62b266e166b1a1e98f0572c8579012abb25e3a7"
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
- }