presto-client
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,45 +0,43 @@ | ||
var Headers = exports.Headers = function(){ | ||
}; | ||
var Headers = exports.Headers = {}; | ||
var TrinoHeaders = exports.TrinoHeaders = {}; | ||
Headers.USER = 'X-Presto-User'; | ||
Headers.SOURCE = 'X-Presto-Source'; | ||
Headers.CATALOG = 'X-Presto-Catalog'; | ||
Headers.SCHEMA = 'X-Presto-Schema'; | ||
Headers.TIME_ZONE = 'X-Presto-Time-Zone'; | ||
Headers.CURRENT_STATE = 'X-Presto-Current-State'; | ||
Headers.MAX_WAIT = 'X-Presto-Max-Wait'; | ||
Headers.MAX_SIZE = 'X-Presto-Max-Size'; | ||
Headers.PAGE_SEQUENCE_ID = 'X-Presto-Page-Sequence-Id'; | ||
Headers.SESSION = 'X-Presto-Session'; | ||
Headers.PREPARE = 'X-Presto-Prepared-Statement'; | ||
Headers.USER_AGENT = 'User-Agent'; | ||
Headers.AUTHORIZATION = 'Authorization'; | ||
var TrinoHeaders = exports.TrinoHeaders = function(){ | ||
// All values in this array are prefixed with X-Presto-/X-Trino-, | ||
// with the key being the value uppercased and dashes replaced | ||
// with underscores, e.g. `Max-Size` becomes | ||
// `{ MAX_SIZE: 'X-Presto-Max-Size' }` on Headers object. | ||
const commonSuffix = [ | ||
'User', | ||
'Source', | ||
'Catalog', | ||
'Schema', | ||
'Time-Zone', | ||
'Current-State', | ||
'Max-Wait', | ||
'Max-Size', | ||
'Page-Sequence-Id', | ||
'Session', | ||
'Prepared-Statement', | ||
]; | ||
const legacySuffix = { | ||
'PREPARE': 'Prepared-Statement', | ||
}; | ||
const common = [ | ||
'User-Agent', | ||
'Authorization', | ||
]; | ||
TrinoHeaders.USER = 'X-Trino-User'; | ||
TrinoHeaders.SOURCE = 'X-Trino-Source'; | ||
TrinoHeaders.CATALOG = 'X-Trino-Catalog'; | ||
TrinoHeaders.SCHEMA = 'X-Trino-Schema'; | ||
TrinoHeaders.TIME_ZONE = 'X-Trino-Time-Zone'; | ||
TrinoHeaders.CURRENT_STATE = 'X-Trino-Current-State'; | ||
TrinoHeaders.MAX_WAIT = 'X-Trino-Max-Wait'; | ||
TrinoHeaders.MAX_SIZE = 'X-Trino-Max-Size'; | ||
TrinoHeaders.PAGE_SEQUENCE_ID = 'X-Trino-Page-Sequence-Id'; | ||
TrinoHeaders.SESSION = 'X-Trino-Session'; | ||
TrinoHeaders.PREPARE = 'X-Trino-Prepared-Statement'; | ||
TrinoHeaders.USER_AGENT = 'User-Agent'; | ||
TrinoHeaders.AUTHORIZATION = 'Authorization'; | ||
const transformToHeaderKeyName = (key) => { | ||
return key.replace('-', '_').toUpperCase(); | ||
} | ||
commonSuffix.forEach((header) => { | ||
Headers[transformToHeaderKeyName(header)] = `X-Presto-${header}`; | ||
TrinoHeaders[transformToHeaderKeyName(header)] = `X-Trino-${header}`; | ||
}); | ||
Object.entries(legacySuffix).forEach(([key, value]) => { | ||
Headers[key] = `X-Presto-${value}`; | ||
TrinoHeaders[key] = `X-Trino-${value}`; | ||
}); | ||
common.forEach((header) => { | ||
Headers[transformToHeaderKeyName(header)] = header; | ||
TrinoHeaders[transformToHeaderKeyName(header)] = header; | ||
}); |
const { URL } = require('url') ; | ||
const http = require('follow-redirects/http'); | ||
const https = require('follow-redirects/https'); | ||
var adapterFor = (function() { | ||
var adapters = { | ||
'http:': require('http'), | ||
'https:': require('https'), | ||
}; | ||
var adapters = { | ||
'http:': http, | ||
'https:': https, | ||
}; | ||
return function(protocol) { | ||
return adapters[protocol]; | ||
}; | ||
}()); | ||
var PrestoHeaders = require('./headers').Headers; | ||
@@ -73,3 +69,2 @@ var TrinoHeaders = require('./headers').TrinoHeaders; | ||
var client = this; | ||
var adapter = adapterFor(client.protocol); | ||
var contentBody = null; | ||
@@ -80,2 +75,3 @@ | ||
opts.port = client.port; | ||
opts.protocol = client.protocol; | ||
if (! opts.user) | ||
@@ -95,2 +91,3 @@ opts.user = client.user | ||
headers: {}, | ||
protocol: href.protocol, | ||
}; | ||
@@ -101,6 +98,4 @@ } catch (error) { | ||
} | ||
opts.protocol = client.protocol; | ||
var adapter = adapters[opts.protocol]; | ||
opts = Object.assign({}, opts, client.ssl); | ||
if (opts.user) | ||
@@ -120,3 +115,11 @@ opts.headers[client.headers.USER] = opts.user; | ||
opts.agent = new adapter.Agent(opts); // Otherwise SSL params are ignored. | ||
// `agents` is a custom property that follow-redirects supports, where | ||
// it's similar to setting `agent`, but allows for setting it per protocol, | ||
// so that if we redirect from http -> https (or vice versa) we use the right | ||
// agent, instead of trying to reuse http.Agent for https (or vice versa) which | ||
// will error. | ||
opts.agents = { | ||
http: new http.Agent({ keepAlive: false }), | ||
https: new https.Agent({ ...client.ssl, keepAlive: false }), | ||
}; | ||
@@ -135,4 +138,8 @@ var parser = this.jsonParser; | ||
var error = null; | ||
// Only a 200 response code should be considered successful | ||
if (response_code === 200) { | ||
// A DELETE request to cancel a query should return 204 No Content, while | ||
// all other API requests should return 200 OK with a JSON body. | ||
if (opts.method === 'DELETE' && response_code === 204) { | ||
data = null; | ||
} | ||
else if (response_code === 200) { | ||
try { | ||
@@ -197,9 +204,11 @@ if (data[0] !== '{' && data[0] !== '[') { | ||
this.request({ method: 'DELETE', path: '/v1/query/' + query_id }, function(error, code, data){ | ||
if (error || code !== 204) { | ||
var message = "query kill api returns error" + (data && data.length > 0 ? ":" + data : ""); | ||
callback({message: message, error: error, code: code}); | ||
if (!callback) { | ||
return; | ||
} | ||
if (callback) | ||
callback(null); | ||
var ret = null; | ||
if (error) { | ||
var message = "query kill api returns error" + (data && data.length > 0 ? ":" + data : ""); | ||
ret = {message: message, error: error, code: code}; | ||
} | ||
callback(ret); | ||
}); | ||
@@ -232,3 +241,3 @@ }; | ||
if (opts.prepares) { | ||
header[client.headers.PREPARE] = opts.prepares.map((s, index) => 'query' + index + '=' + encodeURIComponent(s)).join(','); | ||
header[client.headers.PREPARED_STATEMENT] = opts.prepares.map((s, index) => 'query' + index + '=' + encodeURIComponent(s)).join(','); | ||
} | ||
@@ -355,3 +364,3 @@ | ||
client.request({ method: 'DELETE', path: uri_obj }, function(error, code, data){ | ||
if (error || code !== 204) { | ||
if (error) { | ||
error_callback({message: "query fetch canceled, but Presto query cancel may fail", error: error, code: code}); | ||
@@ -358,0 +367,0 @@ } else { |
{ | ||
"name": "presto-client", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Distributed query engine Presto/Trino client library for node.js", | ||
@@ -23,3 +23,6 @@ "main": "index.js", | ||
"jest": "29.5.0" | ||
}, | ||
"dependencies": { | ||
"follow-redirects": "^1.15.3" | ||
} | ||
} |
@@ -212,2 +212,7 @@ # presto-client-node | ||
* 1.1.0: | ||
* add automatic retries for server errors | ||
* follow redirects if servers simply redirect client's request | ||
* fix bug of HTTP/HTTPS procotol handling | ||
* fix bug about cancelling queries | ||
* 1.0.0: | ||
@@ -214,0 +219,0 @@ * add test cases and CI setting, new options and others, thanks to the many contributions from Matthew Peveler (@MasterOdin) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
34660
10
443
288
3
1
+ Addedfollow-redirects@^1.15.3
+ Addedfollow-redirects@1.15.9(transitive)