Skip to content

Commit 30a7940

Browse files
committed
_debug should allow for valid streams if they exist.
1 parent e6791ff commit 30a7940

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

lib/gateway.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,12 +1102,14 @@ class Gateway extends EventEmitter {
11021102

11031103
}
11041104

1105-
__validateDebug__ (debugObject) {
1105+
__validateDebug__ (debugObject, streams) {
1106+
streams = streams || [];
11061107
debugObject = typeof debugObject === 'object'
11071108
? Array.isArray(debugObject) || Buffer.isBuffer(debugObject)
11081109
? {}
11091110
: debugObject || {}
11101111
: {};
1112+
let streamObject = {};
11111113
Object.keys(debugObject).forEach(key => {
11121114
if (
11131115
key === '*' ||
@@ -1118,7 +1120,12 @@ class Gateway extends EventEmitter {
11181120
) {
11191121
return;
11201122
} else {
1121-
throw new Error(`Invalid debug listener: "${key}"`);
1123+
let stream = streams.find(stream => stream.name === key);
1124+
if (!stream) {
1125+
throw new Error(`Invalid debug listener: "${key}"`);
1126+
} else {
1127+
return;
1128+
}
11221129
}
11231130
});
11241131
return true;
@@ -1348,7 +1355,7 @@ class Gateway extends EventEmitter {
13481355
return this.__debugError__(req, res, 'Can not debug with "background" mode set', headers);
13491356
} else {
13501357
try {
1351-
this.__validateDebug__(validated.params._debug);
1358+
this.__validateDebug__(validated.params._debug, definition.streams);
13521359
} catch (e) {
13531360
this.log(req, `Debug Error`, 'error');
13541361
return this.__debugError__(req, res, e.message, headers);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "functionscript",
3-
"version": "2.9.0",
3+
"version": "2.9.1",
44
"description": "An API gateway and framework for turning functions into web services",
55
"author": "Keith Horwood <keithwhor@gmail.com>",
66
"main": "index.js",

tests/gateway/tests.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5141,6 +5141,53 @@ module.exports = (expect) => {
51415141
});
51425142
});
51435143

5144+
it('Streaming endpoints should succeed if contains a valid stream in _debug', done => {
5145+
request('POST', {}, '/stream/basic/', {alpha: 'hello', _stream: {}, _debug: {hello: true}}, (err, res, result) => {
5146+
5147+
expect(err).to.not.exist;
5148+
expect(res.statusCode).to.equal(200);
5149+
expect(res.headers['content-type']).to.equal('text/event-stream');
5150+
expect(res.headers['x-debug']).to.equal('true');
5151+
expect(result).to.exist;
5152+
5153+
let events = parseServerSentEvents(result);
5154+
expect(Object.keys(events).length).to.equal(3);
5155+
5156+
expect(events['@begin']).to.exist;
5157+
expect(events['@begin'].length).to.equal(1);
5158+
expect(events['@begin'][0]).to.be.a.string;
5159+
5160+
expect(events['hello']).to.exist;
5161+
expect(events['hello'].length).to.equal(1);
5162+
expect(events['hello'][0]).to.equal('true');
5163+
5164+
expect(events['@response']).to.exist;
5165+
let response = JSON.parse(events['@response'][0]);
5166+
expect(response.headers['Content-Type']).to.equal('application/json');
5167+
expect(response.body).to.equal('true');
5168+
5169+
done();
5170+
5171+
});
5172+
});
5173+
5174+
it('Streaming endpoints should fail if contains an invalid stream in _debug', done => {
5175+
request('POST', {}, '/stream/basic/', {alpha: 'hello', _stream: {}, _debug: {test: true}}, (err, res, result) => {
5176+
5177+
expect(err).to.not.exist;
5178+
expect(res.statusCode).to.equal(403);
5179+
expect(res.headers['content-type']).to.equal('application/json');
5180+
expect(res.headers['x-debug']).to.equal('true');
5181+
expect(result).to.exist;
5182+
expect(result.error).to.exist;
5183+
expect(result.error.type).to.equal('DebugError');
5184+
expect(result.error.message).to.contain('"test"');
5185+
5186+
done();
5187+
5188+
});
5189+
});
5190+
51445191
it('Streaming endpoints should fail with StreamError if contains an invalid stream when _debug', done => {
51455192
request('POST', {}, '/stream/basic/', {alpha: 'hello', _stream: {test: true}, _debug: true}, (err, res, result) => {
51465193

0 commit comments

Comments
 (0)