-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (136 loc) · 4.09 KB
/
index.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
var maptalks = require('maptalks'),
svg2img = require('svg2img'),
http = require('http'),
https = require('https'),
fs = require('fs'),
urlParser = require('url');
var httpAgent = http.globalAgent;
maptalks.setupHttpAgent = function(options) {
httpAgent = new http.Agent(options);
}
var loadRemoteImage = function (img, url, onComplete) {
// http
var request;
if (url.indexOf('https://') === 0) {
request = https.request;
} else {
request = http.request;
}
var urlObj = urlParser.parse(url);
//mimic the browser to prevent server blocking.
urlObj.headers = {
'Accept': 'image/*,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Cache-Control': 'no-cache',
// This is header used in http/1.0, nodejs use http/1.1 by default
// 'Connection': 'keep-alive',
'Host': urlObj.host,
'Pragma': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'
};
urlObj.agent = httpAgent;
request(urlObj, function (res) {
var data = [];
res.on('data', function (chunk) {
data.push(chunk);
});
res.on('end', function () {
onComplete(null, Buffer.concat(data));
});
}).on('error', onComplete).end();
};
var loadLocalImage = function (img, url, onComplete) {
// local file
fs.readFile(url, onComplete);
};
maptalks.Util.loadImage.node = function (img, imgDesc) {
function onError(err) {
if (err) {
console.error(err);
console.error(err.stack);
}
var onerrorFn = img.onerror;
if (onerrorFn) {
onerrorFn.call(img);
}
}
function onLoadComplete(err, data) {
if (err) {
onError(err);
return;
}
var onloadFn = img.onload;
if (onloadFn) {
img.onload = function () {
onloadFn.call(img);
};
}
img.src = data;
}
var url = imgDesc[0],
w = imgDesc[1],
h = imgDesc[2];
try {
if (maptalks.Util.isSVG(url)) {
//use svg2img to convert svg to png.
//https://github.com/fuzhenn/node-svg2img
svg2img(url, {'width':w, 'height':h}, onLoadComplete);
} else if (maptalks.Util.isURL(url)) {
// canvas-node的Image对象
loadRemoteImage(img, url, onLoadComplete);
} else {
loadLocalImage(img, url, onLoadComplete);
}
} catch (error) {
onError(error);
}
}
function wrapCallback (cb) {
return function (res) {
var data = [],
isBuffer = false;
res.setEncoding('utf8');
res.on('data', function (chunk) {
if (chunk instanceof Buffer) {
isBuffer = true;
}
data.push(chunk);
});
res.on('end', function () {
cb(null, isBuffer ? Buffer.concat(data).toString('utf8') : data.join(''));
});
};
};
function getClient(protocol) {
return (protocol && protocol === 'https:') ? https : http;
};
maptalks.Ajax.get.node = function (url, cb) {
var reqOpts = urlParser.parse(url);
reqOpts.method = 'GET';
reqOpts.agent = httpAgent;
getClient(reqOpts.protocol)
.get(reqOpts, wrapCallback(cb))
.on('error', cb);
return maptalks.Ajax;
};
maptalks.Ajax.post.node = function (options, postData, cb) {
var reqOpts = urlParser.parse(options.url);
reqOpts.method = 'POST';
reqOpts.agent = httpAgent;
if (!options.headers) {
options.headers = {};
}
if (!options.headers['Content-Type']) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
reqOpts.headers = options.headers;
var req = getClient(reqOpts.protocol).request(reqOpts, wrapCallback(cb));
req.on('error', cb);
if (!isString(postData)) {
postData = JSON.stringify(postData);
}
req.write(postData);
req.end();
return maptalks.Ajax;
};
module.exports = maptalks;