-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathincoming-message.js
109 lines (95 loc) · 2.28 KB
/
incoming-message.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
var capability = require('./capability')
var inherits = require('inherits')
var stream = require('readable-stream')
var IncomingMessage = function (response, mode, fetchTimer) {
stream.Readable.call(this)
this._mode = mode
this.headers = {}
this.rawHeaders = []
this.trailers = {}
this.rawTrailers = []
// Fake the 'close' event, but only once 'end' fires
this.on('end', () => {
// The nextTick is necessary to prevent the 'request' module from causing an infinite loop
process.nextTick(() =>this.emit('close'))
})
this._fetchResponse = response
this.url = response.url
this.statusCode = response.status
this.statusMessage = response.statusText
response.headers.forEach((header, key) => {
this.headers[key.toLowerCase()] = header
this.rawHeaders.push(key, header)
})
if (capability.writableStream) {
var writable = new WritableStream({
write: chunk => {
return new Promise((resolve, reject) => {
if (this._destroyed) {
reject()
} else if(this.push(Buffer.from(chunk))) {
resolve()
} else {
this._resumeFetch = resolve
}
})
},
close: () => {
clearTimeout(fetchTimer)
if (!this._destroyed){
this.push(null)
}
},
abort: err => {
if (!this._destroyed){
this.emit('error', err)
}
}
})
try {
response.body
.pipeTo(writable)
.catch(err => {
console.log(err);
self.clearTimeout(fetchTimer)
if (!this._destroyed){
this.emit('error', err)
}
})
return
} catch (e) {} // pipeTo method isn't defined. Can't find a better way to feature test this
}
// fallback for when writableStream or pipeTo aren't available
var reader = response.body.getReader()
function read (context) {
reader.read()
.then(result => {
if (context._destroyed){
return
}
if (result.done) {
clearTimeout(fetchTimer)
context.push(null)
return
}
context.push(Buffer.from(result.value))
read(context)
})
.catch(err => {
clearTimeout(fetchTimer)
if (!context._destroyed){
context.emit('error', err)
}
})
}
read(this)
}
inherits(IncomingMessage, stream.Readable)
IncomingMessage.prototype._read = function () {
var resolve = this._resumeFetch
if (resolve) {
this._resumeFetch = null
resolve()
}
}
module.exports = IncomingMessage