-
Notifications
You must be signed in to change notification settings - Fork 500
/
util.js
188 lines (151 loc) · 4.17 KB
/
util.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict'
const assert = require('assert')
const { kDestroyed } = require('./symbols')
const { IncomingMessage } = require('http')
const net = require('net')
const { InvalidArgumentError } = require('./errors')
function nop () {}
function isReadable (obj) {
return !!(obj && typeof obj.pipe === 'function' &&
typeof obj.on === 'function')
}
function isWritable (obj) {
return !!(obj && typeof obj.write === 'function' &&
typeof obj.on === 'function')
}
function isStream (obj) {
return isReadable(obj) || isWritable(obj)
}
function parseURL (url) {
if (typeof url === 'string') {
url = new URL(url)
}
if (!url || typeof url !== 'object') {
throw new InvalidArgumentError('invalid url')
}
if (url.port != null && url.port !== '' && !Number.isFinite(parseInt(url.port))) {
throw new InvalidArgumentError('invalid port')
}
if (url.path != null && typeof url.path !== 'string') {
throw new InvalidArgumentError('invalid path')
}
if (url.pathname != null && typeof url.pathname !== 'string') {
throw new InvalidArgumentError('invalid pathname')
}
if (url.hostname != null && typeof url.hostname !== 'string') {
throw new InvalidArgumentError('invalid hostname')
}
if (url.origin != null && typeof url.origin !== 'string') {
throw new InvalidArgumentError('invalid origin')
}
if (!/^https?:/.test(url.origin || url.protocol)) {
throw new InvalidArgumentError('invalid protocol')
}
if (!(url instanceof URL)) {
const port = url.port != null
? url.port
: { 'http:': 80, 'https:': 443 }[url.protocol]
const origin = url.origin != null
? url.origin
: `${url.protocol}//${url.hostname}:${port}`
const path = url.path != null
? url.path
: `${url.pathname || ''}${url.search || ''}`
url = new URL(path, origin)
}
return url
}
function parseOrigin (url) {
url = parseURL(url)
if (/\/.+/.test(url.pathname) || url.search || url.hash) {
throw new InvalidArgumentError('invalid url')
}
return url
}
function getServerName (host) {
if (!host) {
return null
}
assert.strictEqual(typeof host, 'string')
let servername = host
if (servername.startsWith('[')) {
const idx = servername.indexOf(']')
assert(idx !== -1)
servername = servername.substr(1, idx - 1)
} else {
servername = servername.split(':', 1)[0]
}
if (net.isIP(servername)) {
servername = null
}
return servername
}
function bodyLength (body) {
if (body && typeof body.on === 'function') {
const state = body._readableState
return state && state.ended === true && Number.isFinite(state.length)
? state.length
: null
}
assert(!body || Number.isFinite(body.byteLength))
return body ? body.byteLength : 0
}
function isDestroyed (stream) {
return !stream || !!(stream.destroyed || stream[kDestroyed])
}
function destroy (stream, err) {
if (!isStream(stream) || isDestroyed(stream)) {
return
}
if (typeof stream.destroy === 'function') {
if (err || Object.getPrototypeOf(stream).constructor !== IncomingMessage) {
stream.destroy(err)
}
} else if (err) {
process.nextTick((stream, err) => {
stream.emit('error', err)
}, stream, err)
}
if (stream.destroyed !== true) {
stream[kDestroyed] = true
}
}
const KEEPALIVE_TIMEOUT_EXPR = /timeout=(\d+)/
function parseKeepAliveTimeout (val) {
const m = val.toString().match(KEEPALIVE_TIMEOUT_EXPR)
return m ? parseInt(m[1], 10) * 1000 : null
}
function parseHeaders (headers, obj = {}) {
for (let i = 0; i < headers.length; i += 2) {
const key = headers[i].toString().toLowerCase()
let val = obj[key]
if (!val) {
obj[key] = headers[i + 1].toString()
} else {
if (!Array.isArray(val)) {
val = [val]
obj[key] = val
}
val.push(headers[i + 1].toString())
}
}
return obj
}
function isBuffer (buffer) {
// See, https://github.com/mcollina/undici/pull/319
return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
}
module.exports = {
nop,
parseOrigin,
parseURL,
getServerName,
isStream,
isReadable,
isDestroyed,
parseHeaders,
parseKeepAliveTimeout,
destroy,
bodyLength,
isBuffer
}