-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (106 loc) · 2.88 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
const qs = require('querystring');
const url = require('url');
const https = require('https');
const csv = require('csv-write-stream');
const zlib = require('zlib');
const wkt = require('terraformer-wkt-parser');
const terraformer = require('terraformer');
const stream = require('readable-stream')
module.exports = createStream;
const makeSQL = (headers, table) =>
`COPY ${table} (${headers.join(',')}) FROM STDIN WITH (FORMAT csv, HEADER true)`
class ToCSV extends stream.Transform {
constructor(headers) {
super({
objectMode: true
});
this.headers = headers;
}
_transform(chunk, _, next) {
let out = chunk.properties;
if (chunk.geometry) {
out.the_geom = `SRID=4326;${wkt.convert(chunk.geometry)}`;
out.the_geom_webmercator = `SRID=3857;${wkt.convert(terraformer.toMercator(chunk.geometry))}`;
}
this.push(out);
next();
}
}
function createBaseUrl(user, credentials) {
if (!credentials.domain && !credentials.subdomainless) {
return `https://${user}.carto.com`;
}
if (credentials.domain) {
if (credentials.subdomainless) {
return `https://${credentials.domain}/user/${user}`;
} else {
return `https://${user}.${credentials.domain}`;
}
} else if (credentials.subdomainless) {
return `https://carto.com/user/${user}`;
}
}
function createStream(user, key, table, headers, cartoOpts, cb) {
if (typeof cartoOpts === 'function') {
cb = cartoOpts;
cartoOpts = {};
}
if (!cartoOpts) {
cartoOpts = {};
}
if (typeof cb !== 'function') {
cb = ()=>{}
}
if (!Array.isArray(headers)) {
return cb(new TypeError('headers must be an array'))
}
if (headers.length === 0){
return cb(new TypeError('headers array must have stuff in it'))
}
headers = headers.slice();
if (!headers.includes('the_geom')) {
headers.push('the_geom');
}
if (!headers.includes('the_geom_webmercator')) {
headers.push('the_geom_webmercator');
}
let sql = makeSQL(headers, table);
let ourURL = `${createBaseUrl(user, cartoOpts)}/api/v2/sql/copyfrom?${qs.stringify({
api_key: key,
q: sql
})}`;
const opts = url.parse(ourURL);
opts.method='post';
opts.headers = {
'transfer-encoding': 'chunked',
'content-type': 'application/octet-stream',
'content-encoding': 'gzip'
}
const res = https.request(opts, res => {
const out = [Buffer.alloc(0)];
res.on('data', d=>out.push(d));
res.on('error', cb);
res.on('end', ()=>{
let resp = Buffer.concat(out).toString();
try {
resp = JSON.parse(resp);
} catch (e) {
console.log('err', resp);
}
if (res.statusCode > 299) {
return cb(resp)
} else {
return cb(null, resp);
}
})
});
var input = new ToCSV(headers);
stream.pipeline(input, csv({headers}), zlib.createGzip({
level: 2
}), res, e=> {
if (e) {
return cb(e);
}
});
return input;
}