Skip to content

Commit

Permalink
feat(Add getStream): Addition to resource API (#119)
Browse files Browse the repository at this point in the history
This change facilitates a Promised-based API (see [1] for discussion). The
original `request`-like behavior is unchanged: return a stream if the caller
omits a callback.

[1]: #37
  • Loading branch information
silasbw authored and jcrugzz committed Jul 29, 2017
1 parent 43d59ff commit 6268dde
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,13 @@ core.ns.rc.delete({ name: 'http-rc', preservePods: true }, print);

### Watching and streaming

If you don't pass a callback to `get`, node-kubernetes returns a
stream. This is useful for watching:
You can call `.getStream` to stream results. This is useful for watching:

```js
const JSONStream = require('json-stream');
const jsonStream = new JSONStream();

const stream = core.ns.po.get({ qs: { watch: true } });
const stream = core.ns.po.getStream({ qs: { watch: true } });
stream.pipe(jsonStream);
jsonStream.on('data', object => {
console.log('Pod:', JSON.stringify(object, null, 2));
Expand All @@ -239,7 +238,7 @@ jsonStream.on('data', object => {

You can access logs in a similar fashion:
```js
const stream = core.ns.po.log({ name: 'http-123', qs: { follow: true } });
const stream = core.ns.po('http-123').log.getStream({ qs: { follow: true } });
stream.on('data', chunk => {
process.stdout.write(chunk.toString());
});
Expand Down
7 changes: 7 additions & 0 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ class BaseObject extends CallableObject {
}, cb && cb200(cb));
}

/**
* Get a Kubernetes resource
* @param {RequestOptions|string} options - GET options, or resource name
* @returns {Stream} Result stream
*/
getStream(options) { return this.get(options); }

/**
* Patch a Kubernetes resource
* @param {RequestOptions} options - PATCH options
Expand Down
11 changes: 11 additions & 0 deletions test/pods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ describe('lib.pods', () => {
done();
});
});
it('returns the Pod via a stream', done => {
const stream = common.api.ns.pods('test-pod').getStream();
const pieces = [];
stream.on('data', data => pieces.push(data.toString()));
stream.on('error', err => assume(err).is.falsy());
stream.on('end', () => {
const object = JSON.parse(pieces.join(''));
assume(object.kind).is.equal('Pod');
done();
});
});
only('unit', 'returns the Pod via the legacy method', done => {
common.api.ns.pods.get('test-pod', (err, pod) => {
assume(err).is.falsy();
Expand Down

0 comments on commit 6268dde

Please sign in to comment.