|
1 | 1 | import type { HttpMethod, Rec } from './types.ts';
|
2 | 2 | import * as errors from './errors.ts';
|
3 | 3 | import { cheerio } from './deps.ts';
|
| 4 | +import { normalizeHeaders } from './utils.ts'; |
4 | 5 |
|
5 | 6 | export type ConfigData = {
|
6 | 7 | url: string;
|
@@ -208,7 +209,8 @@ export class ScrapeResult {
|
208 | 209 |
|
209 | 210 | get selector(): cheerio.CheerioAPI {
|
210 | 211 | if (!this._selector) {
|
211 |
| - if (!this.result.response_headers['content-type'].includes('text/html')) { |
| 212 | + const headers = normalizeHeaders(this.result.response_headers); |
| 213 | + if (!headers['content-type'].includes('text/html')) { |
212 | 214 | throw new errors.ContentTypeError(
|
213 | 215 | `Cannot use selector on non-html content-type, received: ${this.result.response_headers['content-type']}`,
|
214 | 216 | );
|
@@ -287,20 +289,22 @@ export class ScreenshotResult {
|
287 | 289 | }
|
288 | 290 |
|
289 | 291 | private defineMetadata(response: Response): ScreenshotMetadata {
|
290 |
| - const contentType = response.headers.get('content-type'); |
| 292 | + const headers = normalizeHeaders(response.headers); |
| 293 | + const contentType = headers['content-type']; |
291 | 294 | let extension_name = '';
|
292 | 295 | if (contentType) {
|
293 | 296 | extension_name = contentType.split('/')[1].split(';')[0];
|
294 | 297 | }
|
295 | 298 | return {
|
296 | 299 | extension_name: extension_name,
|
297 |
| - upstream_status_code: parseInt(response.headers.get('X-Scrapfly-Upstream-Http-Code') || '200', 10), |
298 |
| - upstream_url: response.headers.get('X-Scrapfly-Upstream-Url') || '', |
| 300 | + upstream_status_code: parseInt(headers['x-scrapfly-upstream-http-code'] || '200', 10), |
| 301 | + upstream_url: headers['x-scrapfly-upstream-url'] || '', |
299 | 302 | };
|
300 | 303 | }
|
301 | 304 |
|
302 | 305 | private decodeResponse(response: Response, data: ArrayBuffer): object | null {
|
303 |
| - if (response.headers.get('content-type') === 'json') { |
| 306 | + const headers = normalizeHeaders(response.headers); |
| 307 | + if (headers['content-type'] === 'json') { |
304 | 308 | return JSON.parse(new TextDecoder().decode(data));
|
305 | 309 | }
|
306 | 310 | return null;
|
|
0 commit comments