From f57ef2c12bcce0e7b99f033366952745dd6daae8 Mon Sep 17 00:00:00 2001 From: Mateusz Niezgoda Date: Thu, 13 Feb 2020 13:43:56 +0100 Subject: [PATCH 1/3] feat(solr): Add ability to inject options to the request --- lib/solr.js | 92 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/lib/solr.js b/lib/solr.js index 73cbddc8..4704186b 100644 --- a/lib/solr.js +++ b/lib/solr.js @@ -11,18 +11,18 @@ */ var http = require('http'), - https = require('https'), - Query = require('./query'), - Collection = require('./collection'), - querystring = require('querystring'), - format = require('./utils/format'), - SolrError = require('./error/solr-error'), - JSONStream = require('JSONStream'), - duplexer = require('duplexer'), - request = require('request'), - JSONbig = require('json-bigint'), - versionUtils = require('./utils/version'), - Promise = require('bluebird'); + https = require('https'), + Query = require('./query'), + Collection = require('./collection'), + querystring = require('querystring'), + format = require('./utils/format'), + SolrError = require('./error/solr-error'), + JSONStream = require('JSONStream'), + duplexer = require('duplexer'), + request = require('request'), + JSONbig = require('json-bigint'), + versionUtils = require('./utils/version'), + Promise = require('bluebird'); /** * Expose `createClient()`. @@ -43,13 +43,14 @@ exports.createClient = createClient; * of JSON native serializer/deserializer * @param solrVersion ['3.2', '4.0', '5.0', '5.1'], check lib/utils/version.js for full reference * @param {Number} [ipVersion=4] - pass it to http/https lib's "family" option + * @param {Object} request - request options * * @return {Client} * @api public */ -function createClient(host, port, core, path, agent, secure, bigint, solrVersion, ipVersion){ - var options = (typeof host === 'object') ? host : { +function createClient(host, port, core, path, agent, secure, bigint, solrVersion, ipVersion, request){ + var options = (typeof host === 'object') ? host : { host : host, port : port, core : core, @@ -58,9 +59,10 @@ function createClient(host, port, core, path, agent, secure, bigint, solrVersion secure : secure, bigint : bigint, solrVersion: solrVersion, - ipVersion: ipVersion == 6 ? 6 : 4 + ipVersion: ipVersion == 6 ? 6 : 4, + request: request }; - return new Client(options); + return new Client(options); } /** @@ -76,6 +78,7 @@ function createClient(host, port, core, path, agent, secure, bigint, solrVersion * @param {Boolean} [options.secure=false] - if true HTTPS will be used instead of HTTP * @param {Boolean} [options.bigint=false] - if true JSONbig serializer/deserializer will be used instead * of JSON native serializer/deserializer + * @param {Object} options.request - request options * @param {Number} [ipVersion=4] - pass it to http/https lib's "family" option * * @return {Client} @@ -93,7 +96,8 @@ function Client(options){ bigint : options.bigint || false, get_max_request_entity_size: options.get_max_request_entity_size || false, solrVersion: options.solrVersion || versionUtils.Solr3_2, - ipVersion: options.ipVersion == 6 ? 6 : 4 + ipVersion: options.ipVersion == 6 ? 6 : 4, + request: options.request || null }; // Default paths of all request handlers @@ -244,10 +248,10 @@ Client.prototype.addRemoteResource = function(options,callback){ Client.prototype.createAddStream = function(options){ var path = [this.options.path,this.options.core, this.UPDATE_JSON_HANDLER + '?' + querystring.stringify(options) +'&wt=json'] - .filter(function(element){ - return element; - }) - .join('/'); + .filter(function(element){ + return element; + }) + .join('/'); var headers = { 'content-type' : 'application/json', 'charset' : 'utf-8' @@ -506,10 +510,10 @@ Client.prototype.update = function(data,options,callback){ var json = pickJSON(this.options.bigint).stringify(data); var fullPath = [this.options.path,this.options.core, this.UPDATE_JSON_HANDLER + '?' + querystring.stringify(options) +'&wt=json'] - .filter(function(element){ - return element; - }) - .join('/'); + .filter(function(element){ + return element; + }) + .join('/'); var params = { host : this.options.host, @@ -520,7 +524,8 @@ Client.prototype.update = function(data,options,callback){ bigint : this.options.bigint, authorization : this.options.authorization, agent : this.options.agent, - ipVersion : this.options.ipVersion + ipVersion : this.options.ipVersion, + request: this.request }; return postJSON(params,callback); } @@ -605,7 +610,7 @@ Client.prototype.spell = function(query,callback){ */ Client.prototype.termsSearch = function(query,callback){ - return this.get(this.TERMS_HANDLER, query, callback); + return this.get(this.TERMS_HANDLER, query, callback); } /** @@ -642,8 +647,8 @@ Client.prototype.get = function(handler,query,callback){ } var fullPath = pathArray.filter(function(element){ - return element; - }).join('/'); + return element; + }).join('/'); var approxUrlLength = 10 + Buffer.byteLength(this.options.host) + (this.options.port+"").length + Buffer.byteLength(fullPath); // Buffer (10) accounts for protocol and special characters like ://, port colon, and initial slash etc @@ -656,11 +661,12 @@ Client.prototype.get = function(handler,query,callback){ bigint: this.options.bigint, authorization: this.options.authorization, agent: this.options.agent, - ipVersion: this.options.ipVersion + ipVersion: this.options.ipVersion, + request: this.options.request }; + return getJSON(params, callback); } else { - // Funnel this through a POST because it's too large return this.post(handler, query, callback); } @@ -700,8 +706,8 @@ Client.prototype.post = function(handler,query,callback){ } var fullPath = pathArray.filter(function(element){ - return element; - }).join('/'); + return element; + }).join('/'); var params = { host : this.options.host, @@ -712,7 +718,8 @@ Client.prototype.post = function(handler,query,callback){ bigint : this.options.bigint, authorization : this.options.authorization, agent : this.options.agent, - ipVersion : this.options.ipVersion + ipVersion : this.options.ipVersion, + request: this.options.request }; return postForm(params,callback); } @@ -809,6 +816,7 @@ function pickJSON(bigint){ * @param {Boolean} params.secure - * @param {Boolean} params.bigint - * @param {http.Agent} [params.agent] - + * @param {Object} params.request - request options * @param {Function} callback(err,obj) - a function executed when the Solr server responds or an error occurs * @param {Error} callback().err * @param {Object} callback().obj - JSON response sent by the Solr server deserialized @@ -835,6 +843,10 @@ function postJSON(params,callback){ family : params.ipversion }; + if (params.request) { + options = Object.assign(options, params.request); + } + if(params.agent !== undefined){ options.agent = params.agent; } @@ -866,6 +878,7 @@ function postJSON(params,callback){ * @param {Boolean} params.secure - * @param {Boolean} params.bigint - * @param {http.Agent} [params.agent] - + * @param {Object} params.request - request options * @param {Function} callback(err,obj) - a function executed when the Solr server responds or an error occurs * @param {Error} callback().err * @param {Object} callback().obj - JSON response sent by the Solr server deserialized @@ -892,6 +905,10 @@ function postForm(params,callback){ family : params.ipVersion }; + if (params.request) { + options = Object.assign(options, params.request); + } + if(params.agent !== undefined){ options.agent = params.agent; } @@ -922,6 +939,7 @@ function postForm(params,callback){ * @param {Boolean} params.secure - * @param {Boolean} params.bigint - * @param {http.Agent} [params.agent] - + * @param {Object} params.request - request options * @param {Function} callback(err,obj) - a function executed when the Solr server responds or an error occurs * @param {Error} callback().err * @param {Object} callback().obj - JSON response sent by the Solr server deserialized @@ -942,11 +960,15 @@ function getJSON(params,callback){ family: params.ipVersion }; + if (params.request) { + options = Object.assign(options, params.request); + } + if(params.agent !== undefined){ options.agent = params.agent; } - if(params.authorization){ + if(params.authorization){ var headers = { 'authorization' : params.authorization }; From 9514405e8fe939e5f37bb337dab00654c953d5a5 Mon Sep 17 00:00:00 2001 From: Mateusz Niezgoda Date: Fri, 14 Feb 2020 10:20:51 +0100 Subject: [PATCH 2/3] Revert whitespace changes --- lib/solr.js | 56 ++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/solr.js b/lib/solr.js index 4704186b..2b0bc2c2 100644 --- a/lib/solr.js +++ b/lib/solr.js @@ -11,18 +11,18 @@ */ var http = require('http'), - https = require('https'), - Query = require('./query'), - Collection = require('./collection'), - querystring = require('querystring'), - format = require('./utils/format'), - SolrError = require('./error/solr-error'), - JSONStream = require('JSONStream'), - duplexer = require('duplexer'), - request = require('request'), - JSONbig = require('json-bigint'), - versionUtils = require('./utils/version'), - Promise = require('bluebird'); + https = require('https'), + Query = require('./query'), + Collection = require('./collection'), + querystring = require('querystring'), + format = require('./utils/format'), + SolrError = require('./error/solr-error'), + JSONStream = require('JSONStream'), + duplexer = require('duplexer'), + request = require('request'), + JSONbig = require('json-bigint'), + versionUtils = require('./utils/version'), + Promise = require('bluebird'); /** * Expose `createClient()`. @@ -50,7 +50,7 @@ exports.createClient = createClient; */ function createClient(host, port, core, path, agent, secure, bigint, solrVersion, ipVersion, request){ - var options = (typeof host === 'object') ? host : { + var options = (typeof host === 'object') ? host : { host : host, port : port, core : core, @@ -62,7 +62,7 @@ function createClient(host, port, core, path, agent, secure, bigint, solrVersion ipVersion: ipVersion == 6 ? 6 : 4, request: request }; - return new Client(options); + return new Client(options); } /** @@ -248,10 +248,10 @@ Client.prototype.addRemoteResource = function(options,callback){ Client.prototype.createAddStream = function(options){ var path = [this.options.path,this.options.core, this.UPDATE_JSON_HANDLER + '?' + querystring.stringify(options) +'&wt=json'] - .filter(function(element){ - return element; - }) - .join('/'); + .filter(function(element){ + return element; + }) + .join('/'); var headers = { 'content-type' : 'application/json', 'charset' : 'utf-8' @@ -510,10 +510,10 @@ Client.prototype.update = function(data,options,callback){ var json = pickJSON(this.options.bigint).stringify(data); var fullPath = [this.options.path,this.options.core, this.UPDATE_JSON_HANDLER + '?' + querystring.stringify(options) +'&wt=json'] - .filter(function(element){ - return element; - }) - .join('/'); + .filter(function(element){ + return element; + }) + .join('/'); var params = { host : this.options.host, @@ -610,7 +610,7 @@ Client.prototype.spell = function(query,callback){ */ Client.prototype.termsSearch = function(query,callback){ - return this.get(this.TERMS_HANDLER, query, callback); + return this.get(this.TERMS_HANDLER, query, callback); } /** @@ -647,8 +647,8 @@ Client.prototype.get = function(handler,query,callback){ } var fullPath = pathArray.filter(function(element){ - return element; - }).join('/'); + return element; + }).join('/'); var approxUrlLength = 10 + Buffer.byteLength(this.options.host) + (this.options.port+"").length + Buffer.byteLength(fullPath); // Buffer (10) accounts for protocol and special characters like ://, port colon, and initial slash etc @@ -706,8 +706,8 @@ Client.prototype.post = function(handler,query,callback){ } var fullPath = pathArray.filter(function(element){ - return element; - }).join('/'); + return element; + }).join('/'); var params = { host : this.options.host, @@ -968,7 +968,7 @@ function getJSON(params,callback){ options.agent = params.agent; } - if(params.authorization){ + if(params.authorization){ var headers = { 'authorization' : params.authorization }; From 33572661f10b520cd3b48689a266aa0cfbc43286 Mon Sep 17 00:00:00 2001 From: Mateusz Niezgoda Date: Fri, 14 Feb 2020 10:22:29 +0100 Subject: [PATCH 3/3] revert ommited whitespace change --- lib/solr.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/solr.js b/lib/solr.js index 2b0bc2c2..49e61735 100644 --- a/lib/solr.js +++ b/lib/solr.js @@ -510,10 +510,10 @@ Client.prototype.update = function(data,options,callback){ var json = pickJSON(this.options.bigint).stringify(data); var fullPath = [this.options.path,this.options.core, this.UPDATE_JSON_HANDLER + '?' + querystring.stringify(options) +'&wt=json'] - .filter(function(element){ - return element; - }) - .join('/'); + .filter(function(element){ + return element; + }) + .join('/'); var params = { host : this.options.host,