2018-05-25 23:42:30 +08:00
|
|
|
const zlib = require('zlib');
|
|
|
|
const PSQL = require('cartodb-psql');
|
|
|
|
const copyTo = require('pg-copy-streams').to;
|
|
|
|
const copyFrom = require('pg-copy-streams').from;
|
2018-05-26 00:47:41 +08:00
|
|
|
const StreamCopyMetrics = require('./stream_copy_metrics');
|
2018-05-31 23:27:35 +08:00
|
|
|
const { Client } = require('pg');
|
2018-05-25 23:42:30 +08:00
|
|
|
|
|
|
|
module.exports = {
|
2018-05-26 00:47:41 +08:00
|
|
|
to (res, sql, userDbParams, logger, cb) {
|
2018-05-26 00:50:56 +08:00
|
|
|
let metrics = new StreamCopyMetrics(logger, 'copyto', sql);
|
2018-05-25 23:42:30 +08:00
|
|
|
|
|
|
|
const pg = new PSQL(userDbParams);
|
2018-06-01 00:59:17 +08:00
|
|
|
pg.connect(function (err, client, done) {
|
2018-05-25 23:42:30 +08:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2018-05-31 23:06:17 +08:00
|
|
|
let responseEnded = false;
|
2018-06-04 22:34:37 +08:00
|
|
|
let connectionClosedByClient = false;
|
|
|
|
const copyToStream = copyTo(sql);
|
|
|
|
const pgstream = client.query(copyToStream);
|
2018-05-31 23:06:17 +08:00
|
|
|
|
|
|
|
res
|
|
|
|
.on('error', err => {
|
|
|
|
pgstream.unpipe(res);
|
2018-06-01 00:59:17 +08:00
|
|
|
done();
|
2018-05-31 23:06:17 +08:00
|
|
|
return cb(err);
|
|
|
|
})
|
|
|
|
.on('close', () => {
|
|
|
|
if (!responseEnded) {
|
2018-06-04 22:34:37 +08:00
|
|
|
connectionClosedByClient = true;
|
|
|
|
// Cancel the running COPY TO query
|
2018-05-31 23:27:35 +08:00
|
|
|
// See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY
|
|
|
|
const runningClient = client;
|
|
|
|
const cancelingClient = new Client(runningClient.connectionParameters);
|
2018-06-04 22:34:37 +08:00
|
|
|
cancelingClient.cancel(runningClient, pgstream);
|
2018-06-05 02:36:16 +08:00
|
|
|
|
|
|
|
const err = new Error('Connection closed by client');
|
2018-06-04 22:34:37 +08:00
|
|
|
pgstream.unpipe(res);
|
2018-06-05 02:36:16 +08:00
|
|
|
// see https://node-postgres.com/api/pool#release-err-error-
|
|
|
|
done(err);
|
2018-06-04 22:34:37 +08:00
|
|
|
return cb(err);
|
2018-05-31 23:06:17 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('end', () => responseEnded = true);
|
|
|
|
|
2018-05-25 23:42:30 +08:00
|
|
|
pgstream
|
|
|
|
.on('error', err => {
|
2018-06-04 22:34:37 +08:00
|
|
|
if (!connectionClosedByClient) {
|
|
|
|
pgstream.unpipe(res);
|
2018-06-05 02:36:16 +08:00
|
|
|
done();
|
2018-06-04 22:34:37 +08:00
|
|
|
return cb(err);
|
|
|
|
}
|
2018-05-25 23:42:30 +08:00
|
|
|
})
|
2018-05-26 00:47:41 +08:00
|
|
|
.on('data', data => metrics.addSize(data.length))
|
2018-05-25 23:42:30 +08:00
|
|
|
.on('end', () => {
|
2018-05-26 00:47:41 +08:00
|
|
|
metrics.end(copyToStream.rowCount);
|
2018-06-01 00:59:17 +08:00
|
|
|
done();
|
2018-05-25 23:42:30 +08:00
|
|
|
return cb(null, metrics);
|
|
|
|
})
|
|
|
|
.pipe(res);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-05-26 00:47:41 +08:00
|
|
|
from (req, sql, userDbParams, gzip, logger, cb) {
|
2018-05-26 00:50:56 +08:00
|
|
|
let metrics = new StreamCopyMetrics(logger, 'copyfrom', sql, gzip);
|
2018-05-25 23:42:30 +08:00
|
|
|
|
|
|
|
const pg = new PSQL(userDbParams);
|
2018-06-01 00:59:17 +08:00
|
|
|
pg.connect(function (err, client, done) {
|
2018-05-25 23:42:30 +08:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let copyFromStream = copyFrom(sql);
|
|
|
|
const pgstream = client.query(copyFromStream);
|
|
|
|
pgstream
|
|
|
|
.on('error', err => {
|
|
|
|
req.unpipe(pgstream);
|
2018-06-01 00:59:17 +08:00
|
|
|
done();
|
2018-05-29 22:19:53 +08:00
|
|
|
return cb(err);
|
2018-05-25 23:42:30 +08:00
|
|
|
})
|
|
|
|
.on('end', function () {
|
2018-05-26 00:47:41 +08:00
|
|
|
metrics.end(copyFromStream.rowCount);
|
2018-06-01 00:59:17 +08:00
|
|
|
done();
|
2018-05-26 00:47:41 +08:00
|
|
|
return cb(null, metrics);
|
2018-05-25 23:42:30 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
let requestEnded = false;
|
|
|
|
|
|
|
|
req
|
|
|
|
.on('error', err => {
|
|
|
|
req.unpipe(pgstream);
|
|
|
|
pgstream.end();
|
2018-06-01 00:59:17 +08:00
|
|
|
done();
|
2018-05-25 23:42:30 +08:00
|
|
|
return cb(err);
|
|
|
|
})
|
|
|
|
.on('close', () => {
|
|
|
|
if (!requestEnded) {
|
2018-05-30 18:59:49 +08:00
|
|
|
const connection = client.connection;
|
2018-05-31 22:41:22 +08:00
|
|
|
connection.sendCopyFail('CARTO SQL API: Connection closed by client');
|
2018-05-25 23:42:30 +08:00
|
|
|
req.unpipe(pgstream);
|
2018-06-01 00:59:17 +08:00
|
|
|
done();
|
2018-05-25 23:42:30 +08:00
|
|
|
return cb(new Error('Connection closed by client'));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('data', data => metrics.size += data.length)
|
2018-05-29 22:19:53 +08:00
|
|
|
.on('end', () => requestEnded = true);
|
|
|
|
|
|
|
|
if (gzip) {
|
|
|
|
req.pipe(zlib.createGunzip()).pipe(pgstream);
|
|
|
|
} else {
|
|
|
|
req.pipe(pgstream);
|
|
|
|
}
|
2018-05-25 23:42:30 +08:00
|
|
|
});
|
|
|
|
}
|
2018-05-26 00:50:56 +08:00
|
|
|
};
|