forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
87 lines (74 loc) · 1.8 KB
/
util.js
File metadata and controls
87 lines (74 loc) · 1.8 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
'use strict'
const assert = require('assert')
const { kDestroyed } = require('./symbols')
const { IncomingMessage } = require('http')
function nop () {}
function isStream (body) {
return !!(body && typeof body.on === 'function')
}
function bodyLength (body, doRead) {
if (body && typeof body.on === 'function') {
if (doRead && typeof body.read === 'function') {
body.read(0)
}
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.length : 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
}
}
function parseHeaders (headers, obj) {
obj = obj || {}
if (!headers) {
return obj
}
for (var i = 0; i < headers.length; i += 2) {
var key = headers[i].toLowerCase()
var val = obj[key]
if (!val) {
obj[key] = headers[i + 1]
} else {
if (!Array.isArray(val)) {
val = [val]
obj[key] = val
}
val.push(headers[i + 1])
}
}
return obj
}
function isBuffer (buffer) {
// See, https://github.com/mcollina/undici/pull/319
return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
}
module.exports = {
nop,
isStream,
isDestroyed,
parseHeaders,
destroy,
bodyLength,
isBuffer
}