Skip to content

Commit f45e7ae

Browse files
committed
refactor(ajax): replace ajax.js with superagent to follow redirection
Also: revert to internal POST protocal to prevent 307 pre-flight
1 parent d8767c4 commit f45e7ae

File tree

9 files changed

+100
-166
lines changed

9 files changed

+100
-166
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"localstorage-memory": "^1.0.1",
1616
"md5": "^2.0.0",
1717
"qiniu": "6.1.3",
18+
"superagent": "^1.8.3",
1819
"underscore": "^1.8.3"
1920
},
2021
"devDependencies": {
@@ -49,12 +50,10 @@
4950
},
5051
"browser": {
5152
"react-native": false,
52-
"./src/browserify-wrapper/ajax.js": "./src/browserify-wrapper/ajax-browser.js",
5353
"./src/uploader/qiniu.js": "./src/uploader/qiniu-browser.js",
5454
"./src/browserify-wrapper/localStorage.js": "./src/browserify-wrapper/localstorage-browser.js",
5555
"./src/browserify-wrapper/parse-base64.js": "./src/browserify-wrapper/parse-base64-browser.js",
5656
"./src/uploader/cos.js": "./src/uploader/cos-browser.js",
57-
"./dist/node/browserify-wrapper/ajax.js": "./dist/node/browserify-wrapper/ajax-browser.js",
5857
"./dist/node/uploader/qiniu.js": "./dist/node/uploader/qiniu-browser.js",
5958
"./dist/node/browserify-wrapper/localStorage.js": "./dist/node/browserify-wrapper/localstorage-browser.js",
6059
"./dist/node/browserify-wrapper/parse-base64.js": "./dist/node/browserify-wrapper/parse-base64-browser.js",

src/ajax.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 每位工程师都有保持代码优雅的义务
3+
* Each engineer has a duty to keep the code elegant
4+
**/
5+
6+
'use strict';
7+
8+
const request = require('superagent');
9+
const debug = require('debug')('ajax');
10+
11+
const Promise = require('./promise');
12+
13+
module.exports = function _ajax(method, resourceUrl, data, headers = {}, onprogress) {
14+
debug(method, resourceUrl, data, headers);
15+
16+
var promise = new Promise();
17+
18+
const req = request(method, resourceUrl)
19+
.set(headers)
20+
.send(data)
21+
.end((err, res) => {
22+
debug(res.status, res.body, res.text);
23+
if (err) {
24+
err.responseText = res.text;
25+
err.response = res.body;
26+
return promise.reject(err);
27+
}
28+
promise.resolve(res.body, res.status, res);
29+
});
30+
if (onprogress) {
31+
req.on('progress', onprogress);
32+
}
33+
34+
return promise;
35+
};

src/browserify-wrapper/ajax-browser.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/browserify-wrapper/ajax.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/uploader/cos-browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const ajax = require('../browserify-wrapper/ajax.js');
3+
const ajax = require('../ajax.js');
44

55
module.exports =function upload(uploadInfo, data, file, saveOptions) {
66
file.attributes.url = uploadInfo.url;

src/uploader/cos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const FormData = require('form-data');
33

4-
const ajax = require('../browserify-wrapper/ajax.js');
4+
const ajax = require('../ajax.js');
55
const Promise = require('../promise');
66
const debug = require('debug')('cos');
77

src/uploader/qiniu-browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77

8-
const ajax = require('../browserify-wrapper/ajax.js');
8+
const ajax = require('../ajax.js');
99

1010
module.exports = function upload(uploadInfo, data, file, saveOptions) {
1111
file.attributes.url = uploadInfo.url;

src/utils.js

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
const _ = require('underscore');
9-
const ajax = require('./browserify-wrapper/ajax');
9+
const ajax = require('./ajax');
1010
const Cache = require('./cache');
1111
const md5 = require('md5');
1212
const debug = require('debug')('utils');
@@ -394,6 +394,8 @@ const init = (AV) => {
394394
throw "Bad route: '" + route + "'.";
395395
}
396396

397+
dataObject = dataObject || {};
398+
397399
// 兼容 AV.serverURL 旧方式设置 API Host,后续去掉
398400
let apiURL = AV.serverURL || AVConfig.APIServerURL;
399401
if (AV.serverURL) {
@@ -422,21 +424,8 @@ const init = (AV) => {
422424
}
423425
}
424426

425-
if (method.toLowerCase() === 'get') {
426-
if (apiURL.indexOf('?') === -1) {
427-
apiURL += '?';
428-
}
429-
for (let k in dataObject) {
430-
if (typeof dataObject[k] === 'object') {
431-
dataObject[k] = JSON.stringify(dataObject[k]);
432-
}
433-
apiURL += '&' + k + '=' + encodeURIComponent(dataObject[k]);
434-
}
435-
}
436-
437427
var headers = {
438428
'X-LC-Id': AV.applicationId,
439-
'X-LC-UA': 'LC-Web-' + AV.version,
440429
'Content-Type': 'application/json;charset=UTF-8'
441430
};
442431
if (AV.masterKey && AV._useMasterKey) {
@@ -447,6 +436,8 @@ const init = (AV) => {
447436
if (!AV._isNullOrUndefined(AV.applicationProduction)) {
448437
headers['X-LC-Prod'] = AV.applicationProduction;
449438
}
439+
headers['User-Agent'] = AV._config.userAgent || `AV/${AV.version}; Node.js/${process.version}`;
440+
450441
return AV.Promise.as().then(function() {
451442
// Pass the session token
452443
if (sessionToken) {
@@ -459,18 +450,60 @@ const init = (AV) => {
459450
});
460451
}
461452
}).then(function() {
462-
return AV._ajax(method, apiURL, JSON.stringify(dataObject), headers).then(null, function(response) {
453+
// redirection for request with pre-flight is not supported by browser
454+
// fallback to plain POST
455+
if (!AVConfig.isNode) {
456+
try {
457+
if (_.isObject(dataObject)) {
458+
if (method.toLowerCase() !== 'post') {
459+
dataObject._method = method;
460+
method = 'post';
461+
}
462+
dataObject._ApplicationId = headers['X-LC-Id'];
463+
// TODO: use X-LC-Sign
464+
dataObject._ApplicationKey = AV.applicationKey;
465+
if (AV._useMasterKey) {
466+
dataObject._MasterKey = AV.masterKey;
467+
}
468+
dataObject._ApplicationProduction = headers['X-LC-Prod'];
469+
dataObject._ClientVersion = 'LC-Web-' + AV.version;
470+
dataObject._SessionToken = headers['X-LC-Session'];
471+
headers = {
472+
'content-type': 'text/plain',
473+
};
474+
dataObject = JSON.stringify(dataObject);
475+
}
476+
} catch (e) {}
477+
}
478+
479+
if (method.toLowerCase() === 'get') {
480+
if (apiURL.indexOf('?') === -1) {
481+
apiURL += '?';
482+
}
483+
for (let k in dataObject) {
484+
if (typeof dataObject[k] === 'object') {
485+
dataObject[k] = JSON.stringify(dataObject[k]);
486+
}
487+
apiURL += '&' + k + '=' + encodeURIComponent(dataObject[k]);
488+
}
489+
}
490+
491+
return AV._ajax(method, apiURL, dataObject, headers).then(null, function(response) {
463492
// Transform the error into an instance of AV.Error by trying to parse
464493
// the error string as JSON.
465494
var error;
466-
if (response && response.responseText) {
467-
try {
468-
var errorJSON = JSON.parse(response.responseText);
469-
if (errorJSON) {
470-
error = new AV.Error(errorJSON.code, errorJSON.error);
495+
if (response) {
496+
if (response.response) {
497+
error = new AV.Error(response.response.code, response.response.error);
498+
} else if (response.responseText) {
499+
try {
500+
var errorJSON = JSON.parse(response.responseText);
501+
if (errorJSON) {
502+
error = new AV.Error(errorJSON.code, errorJSON.error);
503+
}
504+
} catch (e) {
505+
// If we fail to parse the error text, that's okay.
471506
}
472-
} catch (e) {
473-
// If we fail to parse the error text, that's okay.
474507
}
475508
}
476509
error = error || new AV.Error(-1, response.responseText);

0 commit comments

Comments
 (0)