Skip to content

Commit 9d07f23

Browse files
committed
在 Node.js 下用 http/https 模块重新实现 AV._ajax, 取代 xmlhttprequest 模块
1 parent 3eff015 commit 9d07f23

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

lib/av.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var AV = module.exports = {};
1212
AV._ = require('underscore');
1313
AV.VERSION = require('./version');
1414
AV.Promise = require('./promise');
15-
AV.XMLHttpRequest = require('./browserify-wrapper/xmlhttprequest').XMLHttpRequest;
1615
AV.localStorage = require('./localstorage');
1716

1817
// 以下模块为了兼容原有代码,使用这种加载方式。
@@ -34,5 +33,8 @@ require('./status')(AV);
3433
require('./search')(AV);
3534
require('./insight')(AV);
3635

36+
// Node.js platform specific
37+
require('./browserify-wrapper/node-ajax')(AV);
38+
3739
// Backward compatibility
3840
AV.AV = AV;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
var http = require('http');
4+
var https = require('https');
5+
var url = require('url');
6+
7+
module.exports = function(AV) {
8+
// `keepAlive` option only work on Node.js 0.12+
9+
AV._httpAgent = new http.Agent({keepAlive: true});
10+
AV._httpsAgent = new https.Agent({keepAlive: true});
11+
12+
AV._ajax = function _ajax(method, resourceUrl, data, success, error) {
13+
var parsedUrl = url.parse(resourceUrl);
14+
var promise = new AV.Promise();
15+
16+
var transportModule = http;
17+
var transportAgent = AV._httpAgent;
18+
19+
if (parsedUrl.protocol === 'https:') {
20+
transportModule = https;
21+
transportAgent = AV._httpsAgent;
22+
}
23+
24+
var req = transportModule.request({
25+
method: method,
26+
protocol: parsedUrl.protocol,
27+
hostname: parsedUrl.hostname,
28+
port: parsedUrl.port,
29+
path: parsedUrl.path,
30+
agent: transportAgent,
31+
headers: {
32+
'Content-Type': 'text/plain',
33+
'User-Agent': 'AV/' + AV.VERSION + ' (Node.js' + process.version + ')'
34+
}
35+
});
36+
37+
req.on('response', function(res) {
38+
var responseText = '';
39+
40+
res.on('data', function(chunk) {
41+
responseText += chunk.toString();
42+
});
43+
44+
res.on('end', function() {
45+
try {
46+
promise.resolve(JSON.parse(responseText), res.statusCode, res);
47+
} catch (err) {
48+
promise.reject(err);
49+
}
50+
});
51+
});
52+
53+
req.on('error', function(err) {
54+
promise.reject(err);
55+
});
56+
57+
req.end(data);
58+
return promise._thenRunCallbacks({
59+
success: success,
60+
error: error
61+
});
62+
};
63+
}

lib/browserify-wrapper/xmlhttprequest.js

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

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"dependencies": {
1414
"localstorage-memory": "^1.0.1",
1515
"qiniu": "6.1.3",
16-
"underscore": "^1.8.3",
17-
"leancloud-xmlhttprequest": "^1.8.0"
16+
"underscore": "^1.8.3"
1817
},
1918
"devDependencies": {
2019
"browserify": "^11.0.1",

0 commit comments

Comments
 (0)