Skip to content

Commit 056d77d

Browse files
committed
更名 node-ajax.js 到 ajax.js 且不再包含在浏览器版本中;剥离 ajax.js 对全局变量 AV 的依赖
1 parent 9d07f23 commit 056d77d

File tree

5 files changed

+70
-65
lines changed

5 files changed

+70
-65
lines changed

lib/av.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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;
1516
AV.localStorage = require('./localstorage');
1617

1718
// 以下模块为了兼容原有代码,使用这种加载方式。
@@ -33,8 +34,10 @@ require('./status')(AV);
3334
require('./search')(AV);
3435
require('./insight')(AV);
3536

36-
// Node.js platform specific
37-
require('./browserify-wrapper/node-ajax')(AV);
37+
// Node.js platform specific patches
38+
if (AV._isNode) {
39+
AV._ajax = require('./browserify-wrapper/ajax');
40+
}
3841

3942
// Backward compatibility
4043
AV.AV = AV;

lib/browserify-wrapper/ajax.js

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

lib/browserify-wrapper/node-ajax.js

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

lib/browserify-wrapper/xmlhttprequest.js

Whitespace-only changes.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"browser": {
4242
"react-native": false,
43+
"./lib/browserify-wrapper/ajax.js": false,
4344
"./lib/browserify-wrapper/upload.js": "./lib/browserify-wrapper/upload-browser.js",
4445
"./lib/browserify-wrapper/xmlhttprequest.js": "./lib/browserify-wrapper/xmlhttprequest-browser.js",
4546
"./lib/browserify-wrapper/localStorage.js": "./lib/browserify-wrapper/localstorage-browser.js",

0 commit comments

Comments
 (0)