forked from calvinmetcalf/shapefile-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinaryajax.js
48 lines (46 loc) · 1.25 KB
/
binaryajax.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
'use strict';
const Promise = require('lie');
const http = require('http');
const https = require('https');
const URL = global.URL;
const Buffer = require('buffer').Buffer;
const combine = require('./combine');
module.exports = binaryAjax;
function binaryAjax (_url, type) {
return new Promise(function (resolve, reject) {
const url = combine(_url, type);
const parsed = new URL(url);
let method = http;
if (parsed.protocol === 'https:') {
method = https;
}
method.get(parsed, function (res) {
let len = 0;
const buffers = [];
res.on('data', function (d) {
len += d.length;
buffers.push(d);
});
res.on('error', function (e) {
reject(e);
});
res.on('end', function () {
if (res.statusCode > 399) {
if (type === 'prj' || type === 'cpg' || type === 'dbf') {
return resolve(false);
} else {
return reject(new Error(Buffer.concat(buffers, len).toString()));
}
}
const buffer = Buffer.concat(buffers, len);
if (type === 'prj') {
resolve(buffer.toString());
} else {
resolve(buffer);
}
});
}).on('error', function (e) {
reject(e);
});
});
}