Skip to content

Commit

Permalink
feat: sunshine implemenetation
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed May 6, 2016
1 parent 17d6e78 commit e798955
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 18 deletions.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,17 @@
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)


kronos-interceptor
kronos-interceptor-decode-json
=====
introspects / modifies requests as they pass between endpoints

```javascript

interceptor1.connected = interceptor2

promise = interceptor1.receive(request);
```

decodes json streams into objects

install
=======

With [npm](http://npmjs.org) do:

```shell
npm install kronos-interceptor
npm install kronos-interceptor-decode-json
```

license
Expand Down
38 changes: 38 additions & 0 deletions decode-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* jslint node: true, esnext: true */

'use strict';

const pcs = require('parse-concat-stream'),
Interceptor = require('kronos-interceptor').Interceptor;

/**
*
*/
class DecodeJSONInterceptor extends Interceptor {
static get name() {
return 'decode-json';
}

get type() {
return DecodeJSONInterceptor.name;
}

receive(request, args) {
return new Promise((fullfilled, rejected) =>
request.payload.pipe(pcs((err, data) => {
if (err) {
rejected(err);
} else {
fullfilled({
data: data
});
}
})));
}
}

exports.decode = DecodeJSONInterceptor;

exports.registerWithManager = manager => Promise.all([
manager.registerInterceptor(DecodeJSONInterceptor),
]);
22 changes: 15 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "kronos-interceptor-decode-json",
"version": "0.0.0-semantic-release",
"description": "decodes json streams into objects",
"main": "decode-json.js",
"keywords": [
"kronos-interceptor"
],
Expand Down Expand Up @@ -29,13 +30,16 @@
"mocha": "2.4.5",
"semantic-release": "6.2.1"
},
"contributors": [{
"name": "Torsten Link",
"email": "torstenlink@gmx.de"
}, {
"name": "Markus Felten",
"email": "markus.felten@gmx.de"
}],
"contributors": [
{
"name": "Torsten Link",
"email": "torstenlink@gmx.de"
},
{
"name": "Markus Felten",
"email": "markus.felten@gmx.de"
}
],
"license": "BSD-2-Clause",
"engines": {
"node": ">=6"
Expand All @@ -44,5 +48,9 @@
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"kronos-interceptor": "^2.2.1",
"parse-concat-stream": "^0.2.0"
}
}
67 changes: 67 additions & 0 deletions tests/decode_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* global describe, it, xit */
/* jslint node: true, esnext: true */

'use strict';

const chai = require('chai'),
assert = chai.assert,
expect = chai.expect,
should = chai.should(),
fs = require('fs'),
path = require('path'),
kti = require('kronos-test-interceptor'),
JSONDecodeInterceptor = require('../decode-json').decode;

const mochaInterceptorTest = kti.mochaInterceptorTest,
testResponseHandler = kti.testResponseHandler;

const logger = {
debug(a) {
console.log(a);
}
};

/* simple owner with name */
function dummyEndpoint(name) {
return {
get name() {
return name;
},
get path() {
return '/get:id';
},
toString() {
return this.name;
},
"step": logger
};
}

describe('interceptors', () => {
const ep = dummyEndpoint('ep');

mochaInterceptorTest(JSONDecodeInterceptor, ep, {}, 'decode-json', (itc, withConfig) => {
if (!withConfig) return;

describe('json', () => {
it('toJSON', () => {
assert.deepEqual(itc.toJSON(), {
type: 'decode-json'
});
});
});

itc.connected = dummyEndpoint('ep');
itc.connected.receive = testResponseHandler;

it('passing request', () => itc.receive({
payload: fs.createReadStream(path.join(__dirname, 'fixtures/simple.json'))
}).then((response) => {
assert.deepEqual(response, {
data: {
a: 1
}
});
}));
});
});
3 changes: 3 additions & 0 deletions tests/fixtures/simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": 1
}

0 comments on commit e798955

Please sign in to comment.