-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.ts
124 lines (108 loc) · 3.04 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import http from 'node:http';
import https from 'node:https';
import { got as originalGot, Options } from 'got';
import { HeaderGenerator } from 'header-generator';
import { TransformHeadersAgent } from './agent/transform-headers-agent.js';
import { browserHeadersHook } from './hooks/browser-headers.js';
import { customOptionsHook } from './hooks/custom-options.js';
import { fixDecompress } from './hooks/fix-decompress.js';
import { http2Hook } from './hooks/http2.js';
import { insecureParserHook } from './hooks/insecure-parser.js';
import { optionsValidationHandler } from './hooks/options-validation.js';
import { proxyHook } from './hooks/proxy.js';
import { refererHook } from './hooks/referer.js';
import { sessionDataHook } from './hooks/storage.js';
import { tlsHook } from './hooks/tls.js';
import type { GotScraping } from './types.js';
const handlers = [
fixDecompress,
];
const beforeRequest = [
insecureParserHook,
sessionDataHook,
http2Hook,
proxyHook,
browserHeadersHook,
tlsHook,
];
const init = [
optionsValidationHandler,
customOptionsHook,
];
const beforeRedirect = [
refererHook,
];
const gotScraping = originalGot.extend({
handlers,
mutableDefaults: true,
// Most of the new browsers use HTTP/2
http2: true,
https: {
// In contrast to browsers, we don't usually do login operations.
// We want the content.
rejectUnauthorized: false,
},
// Don't fail on 404
throwHttpErrors: false,
timeout: { request: 60_000 },
retry: { limit: 0 },
headers: {
'user-agent': undefined,
},
context: {
headerGenerator: new HeaderGenerator(),
useHeaderGenerator: true,
insecureHTTPParser: true,
},
agent: {
http: new TransformHeadersAgent(http.globalAgent),
https: new TransformHeadersAgent(https.globalAgent),
},
hooks: {
init,
beforeRequest,
beforeRedirect,
},
}) as GotScraping;
/**
* Mock the `decodeURI` global for the time when Got is normalizing the URL.
* @see https://github.com/apify/apify-js/issues/1205
*/
const setupDecodeURI = () => {
const { set } = Object.getOwnPropertyDescriptor(Options.prototype, 'url')!;
Object.defineProperty(Options.prototype, 'url', {
set(value) {
const originalDecodeURI = global.decodeURI;
global.decodeURI = (str) => str;
try {
return set!.call(this, value);
} finally {
global.decodeURI = originalDecodeURI;
}
},
});
};
setupDecodeURI();
export * from 'got';
export { gotScraping, TransformHeadersAgent };
export const hooks = {
init,
beforeRequest,
beforeRedirect,
fixDecompress,
insecureParserHook,
sessionDataHook,
http2Hook,
proxyHook,
browserHeadersHook,
tlsHook,
optionsValidationHandler,
customOptionsHook,
refererHook,
};
export {
type Context,
type GotOptionsInit,
type OptionsInit,
} from './context.js';
export type * from './types.js';