forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhr.js
167 lines (140 loc) · 4.32 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
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
/*
* Copyright 2012-2016 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
'use strict';
var normalizeHeaderName, responsePromise, client, headerSplitRE;
normalizeHeaderName = require('../util/normalizeHeaderName');
responsePromise = require('../util/responsePromise');
client = require('../client');
// according to the spec, the line break is '\r\n', but doesn't hold true in practice
headerSplitRE = /[\r|\n]+/;
function parseHeaders(raw) {
// Note: Set-Cookie will be removed by the browser
var headers = {};
if (!raw) { return headers; }
raw.trim().split(headerSplitRE).forEach(function (header) {
var boundary, name, value;
boundary = header.indexOf(':');
name = normalizeHeaderName(header.substring(0, boundary).trim());
value = header.substring(boundary + 1).trim();
if (headers[name]) {
if (Array.isArray(headers[name])) {
// add to an existing array
headers[name].push(value);
}
else {
// convert single value to array
headers[name] = [headers[name], value];
}
}
else {
// new, single value
headers[name] = value;
}
});
return headers;
}
function safeMixin(target, source) {
Object.keys(source || {}).forEach(function (prop) {
// make sure the property already exists as
// IE 6 will blow up if we add a new prop
if (source.hasOwnProperty(prop) && prop in target) {
try {
target[prop] = source[prop];
}
catch (e) {
// ignore, expected for some properties at some points in the request lifecycle
}
}
});
return target;
}
module.exports = client(function xhr(request) {
return responsePromise.promise(function (resolve, reject) {
/*jshint maxcomplexity:20 */
var client, method, url, headers, entity, headerName, response, XHR;
request = typeof request === 'string' ? { path: request } : request || {};
response = { request: request };
if (request.canceled) {
response.error = 'precanceled';
reject(response);
return;
}
XHR = request.engine || XMLHttpRequest;
if (!XHR) {
reject({ request: request, error: 'xhr-not-available' });
return;
}
entity = request.entity;
request.method = request.method || (entity ? 'POST' : 'GET');
method = request.method;
url = response.url = request.path || '';
try {
client = response.raw = new XHR();
// mixin extra request properties before and after opening the request as some properties require being set at different phases of the request
safeMixin(client, request.mixin);
client.open(method, url, true);
safeMixin(client, request.mixin);
headers = request.headers;
for (headerName in headers) {
/*jshint forin:false */
if (headerName === 'Content-Type' && headers[headerName] === 'multipart/form-data') {
// XMLHttpRequest generates its own Content-Type header with the
// appropriate multipart boundary when sending multipart/form-data.
continue;
}
client.setRequestHeader(headerName, headers[headerName]);
}
request.canceled = false;
request.cancel = function cancel() {
request.canceled = true;
client.abort();
reject(response);
};
client.onreadystatechange = function (/* e */) {
if (request.canceled) { return; }
if (client.readyState === (XHR.DONE || 4)) {
response.status = {
code: client.status,
text: client.statusText
};
response.headers = parseHeaders(client.getAllResponseHeaders());
response.entity = client.responseText;
// #125 -- Sometimes IE8-9 uses 1223 instead of 204
// http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
if (response.status.code === 1223) {
response.status.code = 204;
}
if (response.status.code > 0) {
// check status code as readystatechange fires before error event
resolve(response);
}
else {
// give the error callback a chance to fire before resolving
// requests for file:// URLs do not have a status code
setTimeout(function () {
resolve(response);
}, 0);
}
}
};
try {
client.onerror = function (/* e */) {
response.error = 'loaderror';
reject(response);
};
}
catch (e) {
// IE 6 will not support error handling
}
client.send(entity);
}
catch (e) {
response.error = 'loaderror';
reject(response);
}
});
});