Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(solr): Add ability to inject options to the request #223

Merged
merged 4 commits into from
Dec 31, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 57 additions & 35 deletions lib/solr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand All @@ -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,
Expand All @@ -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);
}

/**
Expand All @@ -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}
Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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

Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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
};
Expand Down