request.js
4.47 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'
/**
* request.js
*
* Request class contains server only options
*/
const url = require('url')
const Headers = require('./headers.js')
const Body = require('./body.js')
const clone = Body.clone
const extractContentType = Body.extractContentType
const getTotalBytes = Body.getTotalBytes
const PARSED_URL = Symbol('url')
/**
* Request class
*
* @param Mixed input Url or Request instance
* @param Object init Custom options
* @return Void
*/
class Request {
constructor (input, init) {
if (!init) init = {}
let parsedURL
// normalize input
if (!(input instanceof Request)) {
if (input && input.href) {
// in order to support Node.js' Url objects; though WHATWG's URL objects
// will fall into this branch also (since their `toString()` will return
// `href` property anyway)
parsedURL = url.parse(input.href)
} else {
// coerce input to a string before attempting to parse
parsedURL = url.parse(`${input}`)
}
input = {}
} else {
parsedURL = url.parse(input.url)
}
let method = init.method || input.method || 'GET'
if ((init.body != null || (input instanceof Request && input.body !== null)) &&
(method === 'GET' || method === 'HEAD')) {
throw new TypeError('Request with GET/HEAD method cannot have body')
}
let inputBody = init.body != null
? init.body
: input instanceof Request && input.body !== null
? clone(input)
: null
Body.call(this, inputBody, {
timeout: init.timeout || input.timeout || 0,
size: init.size || input.size || 0
})
// fetch spec options
this.method = method.toUpperCase()
this.redirect = init.redirect || input.redirect || 'follow'
this.headers = new Headers(init.headers || input.headers || {})
if (init.body != null) {
const contentType = extractContentType(this)
if (contentType !== null && !this.headers.has('Content-Type')) {
this.headers.append('Content-Type', contentType)
}
}
// server only options
this.follow = init.follow !== undefined
? init.follow : input.follow !== undefined
? input.follow : 20
this.compress = init.compress !== undefined
? init.compress : input.compress !== undefined
? input.compress : true
this.counter = init.counter || input.counter || 0
this.agent = init.agent || input.agent
this[PARSED_URL] = parsedURL
Object.defineProperty(this, Symbol.toStringTag, {
value: 'Request',
writable: false,
enumerable: false,
configurable: true
})
}
get url () {
return url.format(this[PARSED_URL])
}
/**
* Clone this request
*
* @return Request
*/
clone () {
return new Request(this)
}
}
Body.mixIn(Request.prototype)
Object.defineProperty(Request.prototype, Symbol.toStringTag, {
value: 'RequestPrototype',
writable: false,
enumerable: false,
configurable: true
})
exports = module.exports = Request
exports.getNodeRequestOptions = function getNodeRequestOptions (request) {
const parsedURL = request[PARSED_URL]
const headers = new Headers(request.headers)
// fetch step 3
if (!headers.has('Accept')) {
headers.set('Accept', '*/*')
}
// Basic fetch
if (!parsedURL.protocol || !parsedURL.hostname) {
throw new TypeError('Only absolute URLs are supported')
}
if (!/^https?:$/.test(parsedURL.protocol)) {
throw new TypeError('Only HTTP(S) protocols are supported')
}
// HTTP-network-or-cache fetch steps 5-9
let contentLengthValue = null
if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
contentLengthValue = '0'
}
if (request.body != null) {
const totalBytes = getTotalBytes(request)
if (typeof totalBytes === 'number') {
contentLengthValue = String(totalBytes)
}
}
if (contentLengthValue) {
headers.set('Content-Length', contentLengthValue)
}
// HTTP-network-or-cache fetch step 12
if (!headers.has('User-Agent')) {
headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)')
}
// HTTP-network-or-cache fetch step 16
if (request.compress) {
headers.set('Accept-Encoding', 'gzip,deflate')
}
if (!headers.has('Connection') && !request.agent) {
headers.set('Connection', 'close')
}
// HTTP-network fetch step 4
// chunked encoding is handled by Node.js
return Object.assign({}, parsedURL, {
method: request.method,
headers: headers.raw(),
agent: request.agent
})
}