website-to-markdown 1.1.1 → 2.0.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 (85) hide show
  1. package/README.md +135 -5
  2. package/dist/cli.d.ts.map +1 -1
  3. package/dist/cli.js +3 -1
  4. package/dist/cli.js.map +1 -1
  5. package/dist/content-type.d.ts.map +1 -1
  6. package/dist/content-type.js +9 -8
  7. package/dist/content-type.js.map +1 -1
  8. package/dist/crawl-snapshot.d.ts +2 -1
  9. package/dist/crawl-snapshot.d.ts.map +1 -1
  10. package/dist/crawl-snapshot.js +15 -8
  11. package/dist/crawl-snapshot.js.map +1 -1
  12. package/dist/fetch-page.d.ts +38 -22
  13. package/dist/fetch-page.d.ts.map +1 -1
  14. package/dist/fetch-page.js +209 -167
  15. package/dist/fetch-page.js.map +1 -1
  16. package/dist/hash.d.ts +3 -7
  17. package/dist/hash.d.ts.map +1 -1
  18. package/dist/hash.js +3 -7
  19. package/dist/hash.js.map +1 -1
  20. package/dist/html-metadata.d.ts.map +1 -1
  21. package/dist/html-metadata.js +16 -20
  22. package/dist/html-metadata.js.map +1 -1
  23. package/dist/index.d.ts +3 -3
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +2 -2
  26. package/dist/index.js.map +1 -1
  27. package/dist/limit.d.ts +1 -1
  28. package/dist/limit.js +1 -1
  29. package/dist/limit.js.map +1 -1
  30. package/dist/markdown-refs.d.ts +8 -0
  31. package/dist/markdown-refs.d.ts.map +1 -1
  32. package/dist/markdown-refs.js +104 -59
  33. package/dist/markdown-refs.js.map +1 -1
  34. package/dist/memory-limit.d.ts +1 -1
  35. package/dist/memory-limit.js +1 -1
  36. package/dist/memory-limit.js.map +1 -1
  37. package/dist/net-guard.d.ts +38 -2
  38. package/dist/net-guard.d.ts.map +1 -1
  39. package/dist/net-guard.js +88 -17
  40. package/dist/net-guard.js.map +1 -1
  41. package/dist/rate-limit-retry.d.ts.map +1 -1
  42. package/dist/rate-limit-retry.js +12 -21
  43. package/dist/rate-limit-retry.js.map +1 -1
  44. package/dist/read-body.d.ts +12 -0
  45. package/dist/read-body.d.ts.map +1 -0
  46. package/dist/read-body.js +40 -0
  47. package/dist/read-body.js.map +1 -0
  48. package/dist/retry-after.d.ts.map +1 -1
  49. package/dist/retry-after.js +8 -12
  50. package/dist/retry-after.js.map +1 -1
  51. package/dist/schema/index.d.ts +9 -0
  52. package/dist/schema/index.d.ts.map +1 -1
  53. package/dist/schema/index.js +28 -0
  54. package/dist/schema/index.js.map +1 -1
  55. package/dist/sitemap/index.d.ts +13 -4
  56. package/dist/sitemap/index.d.ts.map +1 -1
  57. package/dist/sitemap/index.js +58 -86
  58. package/dist/sitemap/index.js.map +1 -1
  59. package/dist/testing/index.d.ts +16 -0
  60. package/dist/testing/index.d.ts.map +1 -0
  61. package/dist/testing/index.js +54 -0
  62. package/dist/testing/index.js.map +1 -0
  63. package/dist/unchanged-page.d.ts +9 -0
  64. package/dist/unchanged-page.d.ts.map +1 -0
  65. package/dist/unchanged-page.js +31 -0
  66. package/dist/unchanged-page.js.map +1 -0
  67. package/package.json +12 -7
  68. package/src/cli.ts +3 -1
  69. package/src/content-type.ts +11 -9
  70. package/src/crawl-snapshot.ts +21 -8
  71. package/src/fetch-page.ts +304 -221
  72. package/src/hash.ts +3 -7
  73. package/src/html-metadata.ts +20 -18
  74. package/src/index.ts +7 -0
  75. package/src/limit.ts +1 -1
  76. package/src/markdown-refs.ts +129 -57
  77. package/src/memory-limit.ts +1 -1
  78. package/src/net-guard.ts +126 -16
  79. package/src/rate-limit-retry.ts +20 -20
  80. package/src/read-body.ts +38 -0
  81. package/src/retry-after.ts +11 -18
  82. package/src/schema/index.ts +34 -0
  83. package/src/sitemap/index.ts +83 -86
  84. package/src/testing/index.ts +60 -0
  85. package/src/unchanged-page.ts +37 -0
@@ -1,6 +1,7 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks'
2
2
  import { mkdir, readFile, writeFile } from 'node:fs/promises'
3
3
  import { resolve } from 'node:path'
4
+ import { invariant, type Nullable } from 'hanzio'
4
5
  import { sha256Hex } from './hash.js'
5
6
  import type { Logger } from './logger.js'
6
7
  import {
@@ -40,17 +41,19 @@ export type CrawlSnapshotOptions = {
40
41
  const snapshot = new AsyncLocalStorage<CrawlSnapshot>()
41
42
 
42
43
  export function withCrawlSnapshot<T>(
43
- input: CrawlSnapshotInput | null | undefined,
44
+ input: Nullable<CrawlSnapshotInput>,
44
45
  run: () => Promise<T>,
45
46
  options: CrawlSnapshotOptions
46
47
  ) {
47
48
  if (!input) return run()
48
- if (process.env.NODE_ENV === 'production') {
49
- throw new Error('A crawl snapshot is a local bench tool')
50
- }
51
- if (!CRAWL_SNAPSHOT_NAME_PATTERN.test(input.name)) {
52
- throw new Error(`Invalid crawl snapshot name: ${input.name}`)
53
- }
49
+ invariant(
50
+ process.env.NODE_ENV !== 'production',
51
+ 'A crawl snapshot is a local bench tool'
52
+ )
53
+ invariant(
54
+ CRAWL_SNAPSHOT_NAME_PATTERN.test(input.name),
55
+ `Invalid crawl snapshot name: ${input.name}`
56
+ )
54
57
 
55
58
  return snapshot.run(
56
59
  {
@@ -79,7 +82,9 @@ export async function throughCrawlSnapshot<T>(
79
82
  const file = resolve(active.directory, `${sha256Hex(key).slice(0, 32)}.json`)
80
83
  if (active.mode === 'replay') {
81
84
  const stored = await readFile(file, 'utf8').catch(() => null)
82
- if (stored !== null) return (JSON.parse(stored) as { value: T }).value
85
+ // A file cut short by a killed recording is a miss, not a crash.
86
+ const parsed = stored === null ? null : parseStored<T>(stored)
87
+ if (parsed) return parsed.value
83
88
  active.logger?.warn('crawl-snapshot.miss', { key })
84
89
  }
85
90
 
@@ -89,6 +94,14 @@ export async function throughCrawlSnapshot<T>(
89
94
  return value
90
95
  }
91
96
 
97
+ function parseStored<T>(stored: string) {
98
+ try {
99
+ return JSON.parse(stored) as { value: T }
100
+ } catch {
101
+ return null
102
+ }
103
+ }
104
+
92
105
  /** The snapshot this job reads or writes, to hand on to the job it queues. */
93
106
  export function currentCrawlSnapshot(): CrawlSnapshotInput | null {
94
107
  const active = snapshot.getStore()