Skip to content

Commit

Permalink
build(deps): bump undici from 6.21.0 to 7.2.0 (#4309)
Browse files Browse the repository at this point in the history
Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Felix <[email protected]>
  • Loading branch information
dependabot[bot] and fb55 authored Dec 25, 2024
1 parent 59a8332 commit 014d52e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"parse5": "^7.2.1",
"parse5-htmlparser2-tree-adapter": "^7.1.0",
"parse5-parser-stream": "^7.1.2",
"undici": "^6.21.0",
"undici": "^7.2.0",
"whatwg-mimetype": "^4.0.0"
},
"devDependencies": {
Expand Down
29 changes: 24 additions & 5 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, afterEach } from 'vitest';
import * as cheerio from './index.js';
import { Writable } from 'node:stream';
import { createServer, type Server } from 'node:http';
import { createServer, type Server, type RequestListener } from 'node:http';

function noop() {
// Ignore
Expand Down Expand Up @@ -119,12 +119,13 @@ describe('fromURL', () => {
function createTestServer(
contentType: string,
body: string | Buffer,
handler: RequestListener = (_req, res) => {
res.writeHead(200, { 'Content-Type': contentType });
res.end(body);
},
): Promise<number> {
return new Promise((resolve, reject) => {
server = createServer((_req, res) => {
res.writeHead(200, { 'Content-Type': contentType });
res.end(body);
});
server = createServer(handler);

server.listen(0, () => {
const address = server?.address();
Expand Down Expand Up @@ -177,4 +178,22 @@ describe('fromURL', () => {

expect($.html()).toBe(TEST_HTML);
});

it('should throw on non-HTML/XML Content-Type', async () => {
const port = await createTestServer('text/plain', TEST_HTML);
await expect(cheerio.fromURL(`http://localhost:${port}`)).rejects.toThrow(
'The content-type "text/plain" is neither HTML nor XML.',
);
});

it('should throw on non-2xx responses', async () => {
const port = await createTestServer('text/html', TEST_HTML, (_, res) => {
res.writeHead(500);
res.end();
});

await expect(cheerio.fromURL(`http://localhost:${port}`)).rejects.toThrow(
'Response Error',
);
});
});
22 changes: 13 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ const defaultRequestOptions: UndiciStreamOptions = {
method: 'GET',
// Allow redirects by default
maxRedirections: 5,
// NOTE: `throwOnError` currently doesn't work https://github.com/nodejs/undici/issues/1753
throwOnError: true,
// Set an Accept header
headers: {
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
Expand Down Expand Up @@ -217,26 +215,32 @@ export async function fromURL(
options: CheerioRequestOptions = {},
): Promise<CheerioAPI> {
const {
requestOptions = defaultRequestOptions,
requestOptions = {
...defaultRequestOptions,
dispatcher: undici
.getGlobalDispatcher()
.compose(undici.interceptors.responseError()),
},
encoding = {},
...cheerioOptions
} = options;
let undiciStream: Promise<undici.Dispatcher.StreamData> | undefined;
let undiciStream: Promise<undici.Dispatcher.StreamData<unknown>> | undefined;

// Add headers if none were supplied.
requestOptions.headers ??= defaultRequestOptions.headers;

const promise = new Promise<CheerioAPI>((resolve, reject) => {
undiciStream = undici.stream(url, requestOptions, (res) => {
const contentTypeHeader = res.headers['content-type'] ?? 'text/html';
const contentType = Array.isArray(contentTypeHeader)
? contentTypeHeader[0]
: contentTypeHeader;
const mimeType = new MIMEType(contentType);
const mimeType = new MIMEType(
Array.isArray(contentTypeHeader)
? contentTypeHeader[0]
: contentTypeHeader,
);

if (!mimeType.isHTML() && !mimeType.isXML()) {
throw new RangeError(
`The content-type "${contentType}" is neither HTML nor XML.`,
`The content-type "${mimeType.essence}" is neither HTML nor XML.`,
);
}

Expand Down

0 comments on commit 014d52e

Please sign in to comment.