-
Notifications
You must be signed in to change notification settings - Fork 2
/
xhr.js
59 lines (57 loc) · 1.49 KB
/
xhr.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
"use strict"
var createParser = require('./lib/parse').createParser
exports.XMLHttpRequest = function() {
var xhr = new XMLHttpRequest()
var parser
var responseResolve
var responseRejected
var requestResolved
xhr.addEventListener('progress', receivedData)
var acceptSet
var originalSetRequestHeader = xhr.setRequestHeader
var lastOffset = 0
xhr.setRequestHeader = function(name, value) {
if (name.toLowerCase() == 'accept')
acceptSet = true
return originalSetRequestHeader.call(this, name, value)
}
var originalSend = xhr.send
xhr.send = function() {
if (!acceptSet)
this.setRequestHeader('Accept', 'text/dpack;q=1,application/json;q=0.7')
originalSend.apply(this, arguments)
}
function receivedData(event) {
var sourceText = xhr.responseText
try {
if (parser) {
if (parser.onResume) {
var updatedData = parser.onResume(sourceText.slice(lastOffset), true, true)
xhr.responseParsed = xhr.responseParsed || updatedData
}
} else {
if (sourceText && /dpack/.test(xhr.getResponseHeader('Content-Type'))) {
parser = createParser()
parser.setSource(sourceText, 0, true)
xhr.responseParsed = parser.read()
}
else
return
}
lastOffset = sourceText.length
} catch (error) {
if (xhr.onerror) {
xhr.onerror(error)
} else {
throw error
}
}
}
xhr.addEventListener('load', function(event) {
receivedData()
if (parser && parser.isPaused()) {
throw new Error('Unexpected end of dpack stream')
}
})
return xhr
}