This repository was archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathunsupported-requester.js
73 lines (60 loc) · 1.96 KB
/
unsupported-requester.js
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
import { EventEmitter } from 'events';
import { URL } from 'url';
// `npm i --save caseless` although it's available if `request` is installed
import caseless from 'caseless';
export default function (options) {
return new Request(options);
};
// All of the properties that are defined in this class are required.
class Request extends EventEmitter {
constructor (options) {
super();
const self = this;
self.uri = typeof options.uri === 'string'
? new URL(options.uri) : options.uri;
// Use options.headers instead of `this.headers` if serializing
self.headers = caseless(options.headers);
// Cloudscraper will only call `request.callback` for the very last request
self.callback = options.callback;
// The actual request should be performed at this point.
// Pseudo error event
const error = null;
if (error) {
self.emit('error', new Error('Request error'));
}
// Pseudo response arguments
const body = Buffer.from('Response content', 'utf-8');
const status = 200;
const headers = {
// Response headers
};
// Create a response object that `request` normally provides
const response = new Response(headers, status, body);
response.request = self;
// Advanced, update the cookie jar, use `tough-cookie` if needed
if (response.caseless.has('set-cookie')) {
options.jar.setCookie(
response.caseless['set-cookie'],
self.uri.href,
{ ignoreError: true }
);
}
// Emit the complete event
setImmediate(() => self.emit('complete', response, response.body));
}
getHeader (name) {
return this.headers.get(name);
}
setHeader (name, value) {
this.headers.set(name, value);
}
}
// All of the properties that are defined in this class are required.
class Response {
constructor (headers, statusCode, body) {
this.headers = headers;
this.caseless = caseless(headers);
this.statusCode = statusCode;
this.body = body;
}
}