forked from fullscale/elastic.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic-node-client.js
401 lines (324 loc) · 10.8 KB
/
elastic-node-client.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*! elastic.js - v1.1.1 - 2013-08-14
* https://github.com/fullscale/elastic.js
* Copyright (c) 2013 FullScale Labs, LLC; Licensed MIT */
/*global require:true */
(function () {
'use strict';
var
// node imports
protocol,
http = require('http'),
https = require('https'),
querystring = require('querystring'),
// save reference to global object
// `window` in browser
// `exports` on server
root = this,
ejs;
// create namespace
// use existing ejs object if it exists
if (typeof exports !== 'undefined') {
ejs = exports;
} else {
if (root.ejs == null) {
ejs = root.ejs = {};
} else {
ejs = root.ejs;
}
}
/**
@class
A <code>NodeClient</code> is a type of <code>client</code> that uses
NodeJS http or https module for communication with ElasticSearch.
@name ejs.NodeClient
@desc
A client that uses the NodeJS http or https module for communication.
@param {String} host the optional hostname of your ES server. Default localhost.
@param {String} post the optional port of your ES server. Default 9200.
@param {String} proto the optional protocol to use (http or https). Default http.
*/
ejs.NodeClient = function (host, port, proto) {
var
// http option defaults
options = {
headers: {
'Content-Type': 'application/json'
}
},
// clone defaults
getOptions = function () {
var option,
opts = {};
for (option in options) {
if (!options.hasOwnProperty(option)) {
continue;
}
opts[option] = options[option];
}
return opts;
},
// method to ensure the path always starts with a slash
getPath = function (path) {
if (path.charAt(0) !== '/') {
path = '/' + path;
}
return path;
};
if (host == null) {
host = 'localhost';
}
if (proto === 'https' || proto === 'HTTPS') {
proto = 'https';
protocol = https;
} else {
proto = 'http';
protocol = http;
}
if (port == null && proto === 'http') {
port = '9200';
} else if (port == null && proto === 'https') {
port = '443';
}
return {
/**
Sets the ElasticSearch host.
@member ejs.NodeClient
@param {String} h the hostname of the ElasticSearch server
@returns {Object} returns <code>this</code> so that calls can be
chained. Returns {String} current value if `h` is not specified.
*/
host: function (h) {
if (h == null) {
return host;
}
host = h;
return this;
},
/**
Sets the ElasticSearch port.
@member ejs.NodeClient
@param {String} p the port of the ElasticSearch server
@returns {Object} returns <code>this</code> so that calls can be
chained. Returns {String} current value if `p` is not specified.
*/
port: function (p) {
if (p == null) {
return port;
}
port = p;
return this;
},
/**
Sets the transport protocol, currently only http or https.
@member ejs.NodeClient
@param {String} p the protocol to use, http or https.
@returns {Object} returns <code>this</code> so that calls can be
chained. Returns {String} current value if `p` is not specified.
*/
protocol: function (p) {
if (p == null) {
return proto;
}
p = p.toLowerCase();
if (p === 'https') {
protocol = https;
proto = p;
} else {
proto = 'http';
protocol = http;
}
return this;
},
/**
Sets a request option.
@member ejs.NodeClient
@param {String} oKey The option name
@param {String} oVal The option value
@returns {Object} returns <code>this</code> so that calls can be
chained. Returns the current value of oKey if oVal is not set.
*/
option: function (oKey, oVal) {
if (oKey == null) {
return options;
}
if (oVal == null) {
return options[oKey];
}
options[oKey] = oVal;
return this;
},
/**
Performs HTTP GET requests against the server.
@member ejs.NodeClient
@param {String} path the path to GET from the server
@param {Object} data an object of url parameters for the request
@param {Function} successcb a callback function that will be called with
the results of the request.
@param {Function} errorcb a callback function that will be called
when there is an error with the request
*/
get: function (path, data, successcb, errorcb) {
var req,
opt = getOptions();
opt.host = host;
opt.port = port;
opt.path = path + '?' + querystring.stringify(data);
opt.method = 'GET';
req = protocol.request(opt, function (res) {
var resData = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
resData = resData + chunk;
});
res.on('end', function () {
if (successcb != null) {
successcb(JSON.parse(resData));
}
});
});
// handle request errors
if (errorcb != null) {
req.on('error', errorcb);
}
req.end();
},
/**
Performs HTTP POST requests against the server.
@member ejs.NodeClient
@param {String} path the path to POST to on the server
@param {String} data the POST body
@param {Function} successcb a callback function that will be called with
the results of the request.
@param {Function} errorcb a callback function that will be called
when there is an error with the request
*/
post: function (path, data, successcb, errorcb) {
var req,
opt = getOptions();
opt.host = host;
opt.port = port;
opt.path = path;
opt.method = 'POST';
req = protocol.request(opt, function (res) {
var resData = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
resData = resData + chunk;
});
res.on('end', function () {
if (successcb != null) {
successcb(JSON.parse(resData));
}
});
});
// handle request errors
if (errorcb != null) {
req.on('error', errorcb);
}
req.write(data);
req.end();
},
/**
Performs HTTP PUT requests against the server.
@member ejs.NodeClient
@param {String} path the path to PUT to on the server
@param {String} data the PUT body
@param {Function} successcb a callback function that will be called with
the results of the request.
@param {Function} errorcb a callback function that will be called
when there is an error with the request
*/
put: function (path, data, successcb, errorcb) {
var req,
opt = getOptions();
opt.host = host;
opt.port = port;
opt.path = path;
opt.method = 'PUT';
req = protocol.request(opt, function (res) {
var resData = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
resData = resData + chunk;
});
res.on('end', function () {
if (successcb != null) {
successcb(JSON.parse(resData));
}
});
});
// handle request errors
if (errorcb != null) {
req.on('error', errorcb);
}
req.write(data);
req.end();
},
/**
Performs HTTP DELETE requests against the server.
@member ejs.NodeClient
@param {String} path the path to DELETE to on the server
@param {String} data the DELETE body
@param {Function} successcb a callback function that will be called with
the results of the request.
@param {Function} errorcb a callback function that will be called
when there is an error with the request
*/
del: function (path, data, successcb, errorcb) {
var req,
opt = getOptions();
opt.host = host;
opt.port = port;
opt.path = path;
opt.method = 'DELETE';
req = protocol.request(opt, function (res) {
var resData = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
resData = resData + chunk;
});
res.on('end', function () {
if (successcb != null) {
successcb(JSON.parse(resData));
}
});
});
// handle request errors
if (errorcb != null) {
req.on('error', errorcb);
}
req.write(data);
req.end();
},
/**
Performs HTTP HEAD requests against the server. Same as
<code>get</code>, except only returns headers.
@member ejs.NodeClient
@param {String} path the path to HEAD to on the server
@param {Object} data an object of url parameters.
@param {Function} successcb a callback function that will be called with
the an object of the returned headers.
@param {Function} errorcb a callback function that will be called
when there is an error with the request
*/
head: function (path, data, successcb, errorcb) {
var req,
opt = getOptions();
opt.host = host;
opt.port = port;
opt.path = path + '?' + querystring.stringify(data);
opt.method = 'HEAD';
req = protocol.request(opt, function (res) {
if (successcb != null) {
successcb(res.headers);
}
});
// handle request errors
if (errorcb != null) {
req.on('error', errorcb);
}
req.end();
}
};
};
}).call(this);