html-get 2.27.15 → 2.27.17

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/index.d.ts CHANGED
@@ -1,139 +1,183 @@
1
1
  import type { CheerioAPI } from 'cheerio'
2
+ import PCancelable = require('p-cancelable')
3
+
4
+ declare namespace htmlGet {
5
+ /**
6
+ * Result returned by html-get
7
+ */
8
+ interface HtmlGetResult {
9
+ /** The HTML content */
10
+ html: string
11
+ /** Response headers */
12
+ headers: Record<string, string | string[] | undefined>
13
+ /** Final URL after redirects */
14
+ url: string
15
+ /** HTTP status code */
16
+ statusCode: number | undefined
17
+ /** Redirect history */
18
+ redirects: Array<{ statusCode: number; url: string }>
19
+ /** Statistics about the request */
20
+ stats: {
21
+ mode: 'fetch' | 'prerender'
22
+ timing: number
23
+ shadowDOM: boolean
24
+ }
25
+ }
2
26
 
3
- /**
4
- * Result returned by html-get
5
- */
6
- export interface HtmlGetResult {
7
- /** The HTML content */
8
- html: string
9
- /** Response headers */
10
- headers: Record<string, string | string[] | undefined>
11
- /** Final URL after redirects */
12
- url: string
13
- /** HTTP status code */
14
- statusCode: number
15
- /** Redirect history */
16
- redirects: Array<{ statusCode: number; url: string }>
17
- /** Mode used: 'fetch' or 'prerender' */
18
- mode: 'fetch' | 'prerender'
19
- /** Parsed HTML (Cheerio) */
20
- $?: CheerioAPI
21
- /** Statistics about the request */
22
- stats: {
27
+ interface HtmlGetContent extends Omit<HtmlGetResult, 'stats'> {
28
+ /** Mode used: 'fetch' or 'prerender' */
23
29
  mode: 'fetch' | 'prerender'
24
- timing: number
30
+ /** Parsed HTML (Cheerio) */
31
+ $: CheerioAPI
25
32
  }
26
- }
27
-
28
- /**
29
- * Options for html-get
30
- */
31
- export interface HtmlGetOptions {
32
- /** Character encoding for HTML (default: 'utf-8') */
33
- encoding?: string
34
- /** Function that returns a browserless instance (required unless prerender is false) */
35
- getBrowserless?: () => Promise<any>
36
- /** Function to determine the mode ('fetch' or 'prerender') */
37
- getMode?: (url: string, options: { prerender: boolean | 'auto' }) => 'fetch' | 'prerender'
38
- /** Function to create temporary files */
39
- getTemporalFile?: (input: string, ext?: string) => { path: string }
40
- /** Options passed to got (the HTTP client) */
41
- gotOpts?: Record<string, any>
42
- /** Request headers */
43
- headers?: Record<string, string>
44
- /** Mutool function for PDF processing, or false to disable */
45
- mutool?: ((...args: string[]) => any) | false
46
- /** Prerender mode: true, false, or 'auto' (default) */
47
- prerender?: boolean | 'auto'
48
- /** Options passed to Puppeteer */
49
- puppeteerOpts?: Record<string, any>
50
- /** Rewrite relative URLs to absolute */
51
- rewriteUrls?: boolean
52
- /** Rewrite common HTML meta tag mistakes */
53
- rewriteHtml?: boolean
54
- /** Function to serialize HTML (default: $ => ({ html: $.html() })) */
55
- serializeHtml?: ($: CheerioAPI) => { html: string }
56
- }
57
-
58
- /**
59
- * Main function to get HTML from a URL
60
- */
61
- export function htmlGet(
62
- targetUrl: string,
63
- options?: HtmlGetOptions
64
- ): Promise<HtmlGetResult>
65
-
66
- /**
67
- * Check if a URL should use 'fetch' mode (no prerender needed)
68
- */
69
- export function isFetchMode(url: string): boolean
70
33
 
71
- /**
72
- * Get content directly with a specific mode
73
- */
74
- export function getContent(
75
- url: string,
76
- mode: 'fetch' | 'prerender',
77
- options?: HtmlGetOptions
78
- ): Promise<HtmlGetResult>
34
+ type Pandoc = (format: string, filepath: string) => Promise<string | undefined>
35
+
36
+ type Mutool = (...args: string[]) => any
37
+
38
+ /**
39
+ * Options for html-get
40
+ */
41
+ interface HtmlGetOptions {
42
+ /** Character encoding for HTML (default: 'utf-8') */
43
+ encoding?: string
44
+ /** Function that returns a browserless instance (required unless prerender is false) */
45
+ getBrowserless?: () => Promise<any>
46
+ /** Function to determine the mode ('fetch' or 'prerender') */
47
+ getMode?: (url: string, options: { prerender: boolean | 'auto' }) => 'fetch' | 'prerender'
48
+ /** Function to create temporary files */
49
+ getTemporalFile?: (input: string, ext?: string) => { path: string }
50
+ /** Options passed to got (the HTTP client) */
51
+ gotOpts?: Record<string, any>
52
+ /** Request headers */
53
+ headers?: Record<string, string>
54
+ /** Mutool function for PDF processing, or false to disable */
55
+ mutool?: Mutool | false
56
+ /** Pandoc function for office documents, or false to disable */
57
+ pandoc?: Pandoc | false
58
+ /** Prerender mode: true, false, or 'auto' (default) */
59
+ prerender?: boolean | 'auto'
60
+ /** Options passed to Puppeteer */
61
+ puppeteerOpts?: Record<string, any>
62
+ /** Rewrite relative URLs to absolute */
63
+ rewriteUrls?: boolean
64
+ /** Rewrite common HTML meta tag mistakes */
65
+ rewriteHtml?: boolean
66
+ /** Function to serialize HTML (default: $ => ({ html: $.html() })) */
67
+ serializeHtml?: ($: CheerioAPI) => { html: string }
68
+ }
79
69
 
80
- /**
81
- * Default mutool function (returns undefined if mutool is not installed)
82
- */
83
- export function defaultMutool(): ((...args: string[]) => any) | undefined
70
+ interface GetContentOptions
71
+ extends Pick<
72
+ HtmlGetOptions,
73
+ | 'getBrowserless'
74
+ | 'getTemporalFile'
75
+ | 'gotOpts'
76
+ | 'headers'
77
+ | 'mutool'
78
+ | 'pandoc'
79
+ | 'puppeteerOpts'
80
+ | 'rewriteUrls'
81
+ | 'rewriteHtml'
82
+ > {
83
+ toEncode: (content: Uint8Array, contentType?: string) => string | Promise<string>
84
+ }
84
85
 
85
- /**
86
- * Default request timeout in milliseconds
87
- */
88
- export const REQ_TIMEOUT: number
86
+ interface PdfMeta {
87
+ title?: string | null
88
+ author?: string | null
89
+ authors?: string | null
90
+ description?: string | null
91
+ publisher?: string | null
92
+ date?: string | null
93
+ lang?: string | null
94
+ image?: string | null
95
+ logo?: string | null
96
+ }
89
97
 
90
- /**
91
- * Default abort types for prerendering
92
- */
93
- export const ABORT_TYPES: string[]
98
+ /**
99
+ * Check if a URL should use 'fetch' mode (no prerender needed)
100
+ */
101
+ function isFetchMode(url: string): boolean
102
+
103
+ /**
104
+ * Get content directly with a specific mode
105
+ */
106
+ function getContent(
107
+ url: string,
108
+ mode: 'fetch' | 'prerender',
109
+ options: GetContentOptions
110
+ ): PCancelable<HtmlGetContent>
111
+
112
+ /**
113
+ * Default mutool function (returns undefined if mutool is not installed)
114
+ */
115
+ function defaultMutool(): Mutool | undefined
116
+
117
+ /**
118
+ * Default pandoc function (returns undefined if pandoc is not installed)
119
+ */
120
+ function defaultPandoc(): Pandoc | undefined
121
+
122
+ /**
123
+ * Path of the pandoc binary, or undefined if pandoc is not installed
124
+ */
125
+ function getPandocPath(): string | undefined
126
+
127
+ /**
128
+ * Path of the mutool binary, or undefined if mutool is not installed
129
+ */
130
+ function getMutoolPath(): string | undefined
131
+
132
+ /**
133
+ * Default request timeout in milliseconds
134
+ */
135
+ const REQ_TIMEOUT: number
136
+
137
+ /**
138
+ * Default abort types for prerendering
139
+ */
140
+ const ABORT_TYPES: string[]
141
+
142
+ /**
143
+ * PDF size threshold in bytes (150KB)
144
+ */
145
+ const PDF_SIZE_TRESHOLD: number
146
+
147
+ /** Read title, author, date, and the rest from PDF bytes. */
148
+ function extractPdf(input: {
149
+ url: string
150
+ pdf: ArrayBuffer | ArrayBufferView
151
+ maxPages?: number
152
+ }): Promise<PdfMeta>
153
+
154
+ /** `extractPdf` that returns undefined on a non-PDF or a parse error. */
155
+ function extractPdfSafe(
156
+ input: ArrayBuffer | ArrayBufferView,
157
+ url: string,
158
+ opts?: { maxPages?: number }
159
+ ): Promise<PdfMeta | undefined>
160
+
161
+ /** `true` when the bytes start with `%PDF` (junk before the header is allowed). */
162
+ function isPdf(input: ArrayBuffer | ArrayBufferView): boolean
163
+
164
+ /** `true` when `url` looks like a PDF link. */
165
+ function isPdfLink(url?: string): boolean
166
+
167
+ /** Empty HTML document with PDF metadata stamped as meta tags. */
168
+ function pdfToHtml(
169
+ input: ArrayBuffer | ArrayBufferView,
170
+ url: string,
171
+ opts?: { maxPages?: number }
172
+ ): Promise<string>
173
+ }
94
174
 
95
175
  /**
96
- * PDF size threshold in bytes (150KB)
176
+ * Main function to get HTML from a URL
97
177
  */
98
- export const PDF_SIZE_TRESHOLD: number
99
-
100
- export interface PdfMeta {
101
- title?: string | null
102
- author?: string | null
103
- authors?: string | null
104
- description?: string | null
105
- publisher?: string | null
106
- date?: string | null
107
- lang?: string | null
108
- image?: string | null
109
- logo?: string | null
110
- }
178
+ declare function htmlGet(
179
+ targetUrl: string,
180
+ options?: htmlGet.HtmlGetOptions
181
+ ): PCancelable<htmlGet.HtmlGetResult>
111
182
 
112
- /** Read title, author, date, and the rest from PDF bytes. */
113
- export function extractPdf(input: {
114
- url: string
115
- pdf: ArrayBuffer | ArrayBufferView
116
- maxPages?: number
117
- }): Promise<PdfMeta>
118
-
119
- /** `extractPdf` that returns undefined on a non-PDF or a parse error. */
120
- export function extractPdfSafe(
121
- input: ArrayBuffer | ArrayBufferView,
122
- url: string,
123
- opts?: { maxPages?: number }
124
- ): Promise<PdfMeta | undefined>
125
-
126
- /** `true` when the bytes start with `%PDF` (junk before the header is allowed). */
127
- export function isPdf(input: ArrayBuffer | ArrayBufferView): boolean
128
-
129
- /** `true` when `url` looks like a PDF link. */
130
- export function isPdfLink(url?: string): boolean
131
-
132
- /** Empty HTML document with PDF metadata stamped as meta tags. */
133
- export function pdfToHtml(
134
- input: ArrayBuffer | ArrayBufferView,
135
- url: string,
136
- opts?: { maxPages?: number }
137
- ): Promise<string>
138
-
139
- export default htmlGet
183
+ export = htmlGet
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "html-get",
3
3
  "description": "Get the HTML from any website, fine-tuned for correction & speed",
4
4
  "homepage": "https://nicedoc.com/microlinkhq/html-get",
5
- "version": "2.27.15",
5
+ "version": "2.27.17",
6
6
  "types": "index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "bin": {
@@ -95,7 +95,8 @@
95
95
  "regex-iso-date": "latest",
96
96
  "simple-git-hooks": "latest",
97
97
  "standard": "latest",
98
- "standard-version": "latest"
98
+ "standard-version": "latest",
99
+ "typescript": "latest"
99
100
  },
100
101
  "engines": {
101
102
  "node": ">= 22"
@@ -117,7 +118,7 @@
117
118
  "release": "standard-version -a",
118
119
  "release:github": "github-generate-release",
119
120
  "release:tags": "git push --follow-tags origin HEAD:master",
120
- "test": "c8 ava"
121
+ "test": "tsc --noEmit --strict --module nodenext --moduleResolution nodenext --target es2022 test/types.mts && c8 ava"
121
122
  },
122
123
  "license": "MIT",
123
124
  "ava": {
package/src/html.js CHANGED
@@ -30,7 +30,7 @@ const addHead = ({ $, url, headers, pdfMeta = {} }) => {
30
30
  const charset = getCharset(headers)
31
31
  const { domain } = parseUrl(url)
32
32
  const head = $('head')
33
- const title = pdfMeta.title || path.basename(url)
33
+ const title = pdfMeta.title || path.basename(url.split('#')[0])
34
34
  const siteName = pdfMeta.publisher || domain
35
35
 
36
36
  upsert(head.find('title'), tags, `<title>${escapeAttr(title)}</title>`)
package/src/index.js CHANGED
@@ -26,6 +26,12 @@ const ABORT_TYPES = ['image', 'stylesheet', 'font']
26
26
 
27
27
  const PDF_SIZE_TRESHOLD = 150 * 1024 // 150kb
28
28
 
29
+ // `URL#hash` is empty both when the fragment is missing and when it is
30
+ // empty. An empty fragment is still present on `href`, and it must win.
31
+ const inheritRequestFragment = (options, response) => {
32
+ if (!options.url.href.includes('#')) options.url.hash = new URL(response.url).hash
33
+ }
34
+
29
35
  const fetch = PCancelable.fn(
30
36
  async (
31
37
  url,
@@ -36,6 +42,10 @@ const fetch = PCancelable.fn(
36
42
 
37
43
  const req = got(url, {
38
44
  ...opts,
45
+ hooks: {
46
+ ...opts.hooks,
47
+ beforeRedirect: [inheritRequestFragment].concat(opts.hooks?.beforeRedirect || [])
48
+ },
39
49
  timeout: reqTimeout,
40
50
  responseType: 'buffer'
41
51
  })