index.d.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @file Batteries-included version of Cheerio. This module includes several
  3. * convenience methods for loading documents from various sources.
  4. */
  5. export * from './load-parse.js';
  6. export { contains, merge } from './static.js';
  7. export type * from './types.js';
  8. export type { Cheerio, CheerioAPI, CheerioOptions, HTMLParser2Options, } from './slim.js';
  9. import { type SnifferOptions } from 'encoding-sniffer';
  10. import * as undici from 'undici';
  11. import { Writable } from 'node:stream';
  12. import type { CheerioAPI } from './load.js';
  13. import { type CheerioOptions } from './options.js';
  14. /**
  15. * Sniffs the encoding of a buffer, then creates a querying function bound to a
  16. * document created from the buffer.
  17. *
  18. * @category Loading
  19. * @example
  20. *
  21. * ```js
  22. * import * as cheerio from 'cheerio';
  23. *
  24. * const buffer = fs.readFileSync('index.html');
  25. * const $ = cheerio.fromBuffer(buffer);
  26. * ```
  27. *
  28. * @param buffer - The buffer to sniff the encoding of.
  29. * @param options - The options to pass to Cheerio.
  30. * @returns The loaded document.
  31. */
  32. export declare function loadBuffer(buffer: Buffer, options?: DecodeStreamOptions): CheerioAPI;
  33. /**
  34. * Creates a stream that parses a sequence of strings into a document.
  35. *
  36. * The stream is a `Writable` stream that accepts strings. When the stream is
  37. * finished, the callback is called with the loaded document.
  38. *
  39. * @category Loading
  40. * @example
  41. *
  42. * ```js
  43. * import * as cheerio from 'cheerio';
  44. * import * as fs from 'fs';
  45. *
  46. * const writeStream = cheerio.stringStream({}, (err, $) => {
  47. * if (err) {
  48. * // Handle error
  49. * }
  50. *
  51. * console.log($('h1').text());
  52. * // Output: Hello, world!
  53. * });
  54. *
  55. * fs.createReadStream('my-document.html', { encoding: 'utf8' }).pipe(
  56. * writeStream,
  57. * );
  58. * ```
  59. *
  60. * @param options - The options to pass to Cheerio.
  61. * @param cb - The callback to call when the stream is finished.
  62. * @returns The writable stream.
  63. */
  64. export declare function stringStream(options: CheerioOptions, cb: (err: Error | null | undefined, $: CheerioAPI) => void): Writable;
  65. export interface DecodeStreamOptions extends CheerioOptions {
  66. encoding?: SnifferOptions;
  67. }
  68. /**
  69. * Parses a stream of buffers into a document.
  70. *
  71. * The stream is a `Writable` stream that accepts buffers. When the stream is
  72. * finished, the callback is called with the loaded document.
  73. *
  74. * @category Loading
  75. * @param options - The options to pass to Cheerio.
  76. * @param cb - The callback to call when the stream is finished.
  77. * @returns The writable stream.
  78. */
  79. export declare function decodeStream(options: DecodeStreamOptions, cb: (err: Error | null | undefined, $: CheerioAPI) => void): Writable;
  80. type UndiciStreamOptions = Parameters<typeof undici.stream>[1];
  81. export interface CheerioRequestOptions extends DecodeStreamOptions {
  82. /** The options passed to `undici`'s `stream` method. */
  83. requestOptions?: UndiciStreamOptions;
  84. }
  85. /**
  86. * `fromURL` loads a document from a URL.
  87. *
  88. * By default, redirects are allowed and non-2xx responses are rejected.
  89. *
  90. * @category Loading
  91. * @example
  92. *
  93. * ```js
  94. * import * as cheerio from 'cheerio';
  95. *
  96. * const $ = await cheerio.fromURL('https://example.com');
  97. * ```
  98. *
  99. * @param url - The URL to load the document from.
  100. * @param options - The options to pass to Cheerio.
  101. * @returns The loaded document.
  102. */
  103. export declare function fromURL(url: string | URL, options?: CheerioRequestOptions): Promise<CheerioAPI>;
  104. //# sourceMappingURL=index.d.ts.map