Skip to content

Commit 28e99cf

Browse files
author
Spencer Alger
committed
Switched the color package to one with more reasonable deps, added some examples to README.md, and included which will soon be used to make promisses.
1 parent ca29fbe commit 28e99cf

File tree

7 files changed

+246
-125
lines changed

7 files changed

+246
-125
lines changed

Gruntfile.js

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module.exports = function (grunt) {
55

66
var _ = require('lodash');
7+
var child_process = require('child_process');
78
var sharedBrowserfyExclusions = [
89
'src/lib/connectors/http.js',
910
'src/lib/loggers/file.js',
@@ -72,26 +73,25 @@ module.exports = function (grunt) {
7273
interupt: true
7374
}
7475
},
75-
generate: {
76+
run: {
7677
js_api: {
77-
cmd: 'node',
7878
args: [
7979
'scripts/generate/js_api'
8080
]
8181
},
8282
yaml_tests: {
83-
cmd: 'node',
8483
args: [
8584
'scripts/generate/yaml_tests'
8685
]
87-
}
88-
},
89-
start: {
86+
},
9087
integration_server: {
91-
cmd: 'node',
9288
args: [
9389
'test/browser_integration/server.js'
94-
]
90+
],
91+
options: {
92+
wait: false,
93+
ready: /server listening/
94+
}
9595
}
9696
},
9797
browserify: {
@@ -168,10 +168,18 @@ module.exports = function (grunt) {
168168
'--web-security': false
169169
}
170170
}
171+
},
172+
open: {
173+
yaml_suite: {
174+
path: 'http://localhost:8888',
175+
app: 'Google Chrome'
176+
}
171177
}
172178
});
173179

174180
// load plugins
181+
grunt.loadNpmTasks('grunt-run');
182+
grunt.loadNpmTasks('grunt-open');
175183
grunt.loadNpmTasks('grunt-mocha');
176184
grunt.loadNpmTasks('grunt-browserify');
177185
grunt.loadNpmTasks('grunt-mocha-test');
@@ -197,73 +205,14 @@ module.exports = function (grunt) {
197205
'browserify',
198206
'uglify:dist',
199207
'concat:dist_banners',
200-
'generate:yaml_tests',
201-
'generate:js_api'
208+
'run:yaml_tests',
209+
'run:js_api'
202210
]);
203211

204-
grunt.task.registerMultiTask('generate', 'used to generate things', function () {
205-
var done = this.async();
206-
var proc = require('child_process').spawn(
207-
this.data.cmd,
208-
this.data.args,
209-
{
210-
stdio: ['ignore', 'pipe', 'pipe']
211-
}
212-
);
213-
214-
proc.stdout.on('data', grunt.log.write);
215-
proc.stderr.on('data', grunt.log.error);
216-
217-
proc.on('close', function (exitCode) {
218-
done(!exitCode);
219-
});
220-
});
221-
222-
var runningProcs = {};
223-
224-
process.on('exit', function () {
225-
_.each(runningProcs, function (proc) {
226-
proc.kill();
227-
});
228-
});
229-
230-
grunt.task.registerMultiTask('start', 'used to start external processes (like servers)', function () {
231-
var self = this;
232-
233-
234-
var proc = require('child_process').spawn(
235-
self.data.cmd,
236-
self.data.args,
237-
{
238-
stdio: ['ignore', 'pipe', 'pipe']
239-
}
240-
);
241-
242-
proc.stdout.on('data', grunt.log.write);
243-
proc.stderr.on('data', function (chunk) {
244-
grunt.log.error(chunk);
245-
proc.kill();
246-
self.ansyc()(new Error('Error output received'));
247-
clearTimeout(timeoutId);
248-
});
249-
250-
runningProcs[self.nameArgs] = proc;
251-
252-
proc.on('close', function (exitCode) {
253-
delete runningProcs[self.nameArgs];
254-
});
255-
256-
// operates asyncronously to give the processes a moment to start up, not sure if there is a signal for "I'm ready"
257-
var timeoutId = setTimeout(self.async(), 1000);
258-
});
259-
260-
grunt.task.registerMultiTask('stop', 'used to stop external processes (like servers)', function () {
261-
var proc = runningProcs[this.nameArgs.replace(/^start:/, 'stop:')];
262-
if (proc) {
263-
proc.kill();
264-
} else {
265-
grunt.log.error(this.nameArgs + ' failed to find active process');
266-
}
267-
});
212+
grunt.registerTask('browser', [
213+
'run:integration_server',
214+
'open:yaml_suite',
215+
'wait:integration_server'
216+
]);
268217

269218
};

README.md

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,101 @@
1-
elasticsearch-js
2-
=================
3-
4-
[![Build Status](https://magnum.travis-ci.com/spenceralger/elasticsearch-js.png?token=tsFxSKHtVKG8EZavSjXY)](https://magnum.travis-ci.com/spenceralger/elasticsearch-js)
1+
# elasticsearch.js
52

63
Official *low-level* client for Elasticsearch.
74

8-
This project's goal it to give the JavaScript community a solif foundation for all Elasticsearch-related code. It features a complete API, provides a module for use in Node.js as well as several different build for use in the browser. We have tried to be opinion-free and very plugable.
9-
10-
To maintain consistency across all the low-level clients (Ruby, Python, etc), clients accept all of their parameters via a single object, along with a single callback.
5+
This project's goal it to give the JavaScript community a solif foundation for all Elasticsearch-related code. It features a complete API, provides a module for use in Node.js as well as several different builds for use in the browser. We have tried to be opinion-free and very plugable.
116

12-
Features
13-
--------
7+
## Features
148

159
- One-to-one mapping with REST API and other language clients
10+
- Generalized, pluggable architecture. See [replacing core components](docs/replacing-core-components.md)
1611
- Configurable, automatic discovery of cluster nodes
1712
- Persistent, Keep-Alive connections
1813
- Load balancing (with pluggable selection strategy) across all availible nodes. Defaults to round-robin
1914
- Pluggable connection pools to offer different connection strategies
20-
- Generalized, pluggable architecture - most components can be replaced with your own custom class if specialized behavior is required
15+
16+
17+
18+
## Node and the browser
19+
20+
elasticsearch.js works great in node, as well as the browser (many thanks to [browserify](https://github.com/substack/browserify)).
21+
22+
- Node: [![Build Status](https://magnum.travis-ci.com/spenceralger/elasticsearch-js.png?token=tsFxSKHtVKG8EZavSjXY)](https://magnum.travis-ci.com/spenceralger/elasticsearch-js)
23+
- Browsers:
24+
+ ![testling results for browser clients](https://ci.testling.com/spenceralger/xhr-method-test.png)
25+
26+
## Install in Node
27+
28+
```
29+
npm install --save elasticsearch
30+
```
31+
32+
## Browser Builds
33+
Download one of these builds:
34+
35+
- [elasticsearch.js](dist/elasticsearch.js) - [minified](dist/elasticsearch.min.js)
36+
- uses the browser's native XHR object
37+
- Node style callbacks with a bare-bones `.then()` method
38+
- [elasticsearch.angular.js](dist/elasticsearch.angular.js) - [minified](dist/elasticsearch.angular.min.js)
39+
- Uses angular's $http servive
40+
- returns promisses using angular's $q servive
41+
42+
## API
43+
44+
To maintain consistency across all the low-level clients (Ruby, Python, etc), clients accept all of their parameters via a single object, along with a single callback.
45+
46+
#### create the client
47+
```
48+
var es = new elasticsearch.Client({
49+
hosts: [
50+
'localhost:9200'
51+
],
52+
log: 'trace',
53+
sniffOnStart: true
54+
});
55+
```
56+
57+
#### call an endpoint
58+
```
59+
es.cluster.nodeInfo({
60+
clear: true,
61+
jvm: true,
62+
os: ture
63+
}, function (err, resp, status) {
64+
// do your thing
65+
})
66+
```
67+
68+
#### skip the callback to get a promise back
69+
```
70+
es.search({
71+
q: 'pants'
72+
}).then(function (resp) {
73+
// use resp.body and resp.status
74+
}, function (err) {
75+
// freak out!
76+
})
77+
```
78+
79+
#### abort a request
80+
```
81+
var req = es.search({
82+
q: 'robots'
83+
}, function (err, body, status) {
84+
clearTimeout(timeout);
85+
// do something
86+
});
87+
88+
var timeout = setTimeout(function () {
89+
req.abort();
90+
}, 200);
91+
```
92+
93+
#### or just use the timeout param
94+
```
95+
es.search({
96+
q: '*',
97+
timeout: 200
98+
}).then(function (resp) {
99+
// Iterate all the hits
100+
})
101+
```

docs/replacing-core-components.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
==

package.json

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,36 @@
77
"homepage": "https://github.com/elasticsearch/elasticsearch-js",
88
"version": "0.0.1",
99
"devDependencies": {
10-
"grunt": "*",
11-
"grunt-contrib-jshint": "*",
12-
"grunt-contrib-nodeunit": "*",
13-
"js-yaml": "*",
10+
"tar": "*",
11+
"mocha": "*",
12+
"async": "*",
1413
"mkdirp": "*",
1514
"moment": "*",
1615
"should": "*",
17-
"grunt-mocha-test": "*",
18-
"grunt-contrib-watch": "*",
19-
"expect.js": "*",
20-
"async": "*",
16+
"js-yaml": "*",
2117
"optimist": "*",
18+
"expect.js": "*",
2219
"minimatch": "*",
2320
"browserify": "*",
21+
"grunt": "*",
22+
"grunt-contrib-jshint": "*",
23+
"grunt-contrib-nodeunit": "*",
24+
"grunt-mocha-test": "*",
25+
"grunt-contrib-watch": "*",
2426
"grunt-browserify": "*",
2527
"grunt-contrib-clean": "*",
2628
"grunt-contrib-uglify": "*",
27-
"mocha": "*",
2829
"grunt-mocha": "*",
29-
"grunt-contrib-concat": "~0.3.0"
30+
"grunt-contrib-concat": "~0.3.0",
31+
"grunt-open": "~0.2.2",
32+
"grunt-run": "~0.1.0"
3033
},
3134
"license": "Apache License",
3235
"dependencies": {
33-
"cli-color": "*",
36+
"when": "~2.6.0",
3437
"lodash": "*",
35-
"tar": "*",
36-
"agentkeepalive": "*"
38+
"agentkeepalive": "*",
39+
"chalk": "~0.3.0"
3740
},
3841
"scripts": {
3942
"test": "grunt"

scripts/grunt-run-wait-stop.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var runningProcs = [];
2+
3+
process.on('exit', function () {
4+
_.each(runningProcs, function (proc) {
5+
proc.kill();
6+
});
7+
});
8+
9+
grunt.task.registerMultiTask('run', 'used to start external processes (like servers)', function () {
10+
var self = this;
11+
var name = this.target;
12+
var opts = this.options({
13+
wait: true,
14+
killOnError: true
15+
});
16+
17+
console.log(opts);
18+
19+
var proc = child_process.spawn(
20+
self.data.cmd,
21+
self.data.args,
22+
{
23+
stdio: ['ignore', 'pipe', 'pipe']
24+
}
25+
);
26+
27+
proc.stdout.on('data', grunt.log.write);
28+
29+
var done = this.async();
30+
31+
if (opts.killOnError) {
32+
proc.stderr.on('data', function (chunk) {
33+
grunt.log.error(chunk);
34+
proc.kill();
35+
self.ansyc()(new Error('Error output received'));
36+
clearTimeout(timeoutId);
37+
});
38+
}
39+
40+
if (opts.wait) {
41+
proc.on('close', function (exitCode) {
42+
done(!exitCode);
43+
});
44+
} else {
45+
grunt.config.set('stop.' + name + '._pid', proc.pid);
46+
grunt.config.set('wait.' + name + '._pid', proc.pid);
47+
runningProcs.push(proc);
48+
var timeoutId = setTimeout(done, 1000);
49+
}
50+
51+
proc.on('close', function (exitCode) {
52+
var i;
53+
if ((i = runningProcs.indexOf(proc)) !== -1) {
54+
runningProcs.splice(i, 1);
55+
}
56+
grunt.log.debug('Process ' + name + ' closed.');
57+
});
58+
});
59+
60+
grunt.task.registerMultiTask('stop', 'stop a process started with "start" ' +
61+
'(only works for tasks that use wait:false)', function () {
62+
var pid = this.data._pid;
63+
child_process.kill(pid);
64+
});
65+
66+
grunt.task.registerMultiTask('wait', 'wait for a process to close ' +
67+
'(only works for tasks that use wait:false)', function () {
68+
69+
var pid = this.data._pid;
70+
var proc = _.find(runningProcs, { pid: pid });
71+
if (proc) {
72+
proc.on('close', this.async());
73+
} else {
74+
grunt.log.writeLn('process already closed');
75+
}
76+
});

0 commit comments

Comments
 (0)