From 8fd048ea62f43155d1c5805f166575ed53218c6e Mon Sep 17 00:00:00 2001 From: Jerome Valentin Date: Sun, 30 Oct 2016 07:45:02 +0100 Subject: [PATCH 1/6] Fix #97: Pins not working in TCP / HTTP listen options --- lib/http.js | 16 +++++++++++-- lib/tcp.js | 17 ++++++++++++- lib/transport-utils.js | 3 ++- test/misc.test.js | 54 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 4 deletions(-) diff --git a/lib/http.js b/lib/http.js index 39295be..80251d4 100644 --- a/lib/http.js +++ b/lib/http.js @@ -25,6 +25,7 @@ exports.listen = function (options, transportUtil) { var listener var listenAttempts = 0 var listen_details = _.clone(msg) + var pins = transportUtil.resolve_pins(msg) server.on('request', function (req, res) { internals.timeout(listenOptions, req, res) @@ -34,7 +35,7 @@ exports.listen = function (options, transportUtil) { return res.end() } - internals.trackHeaders(listenOptions, seneca, transportUtil, req, res) + internals.trackHeaders(listenOptions, seneca, transportUtil, req, res, pins) }) }) @@ -200,7 +201,7 @@ internals.setBody = function (seneca, transportUtil, req, res, next) { }) } -internals.trackHeaders = function (listenOptions, seneca, transportUtil, req, res) { +internals.trackHeaders = function (listenOptions, seneca, transportUtil, req, res, pins) { if (req.url.indexOf(listenOptions.path) !== 0) { return } @@ -232,6 +233,17 @@ internals.trackHeaders = function (listenOptions, seneca, transportUtil, req, re } } + // Issue: #97 + if (pins) { + // any topic is not pinned, verifying the calling one is pinned + var argspatrun = transportUtil.make_argspatrun(pins) + if (!argspatrun.find(req.body)) { + data.error = transportUtil.error('not_pinned', { act: req.body }) + data.error.statusCode = 404 + return internals.sendResponse(seneca, transportUtil, res, data, {}) + } + } + transportUtil.handle_request(seneca, data, listenOptions, function (out) { internals.sendResponse(seneca, transportUtil, res, out, data) }) diff --git a/lib/tcp.js b/lib/tcp.js index a7a113e..ef1689b 100644 --- a/lib/tcp.js +++ b/lib/tcp.js @@ -20,6 +20,8 @@ exports.listen = function (options, transportUtil) { var connections = [] var listenAttempts = 0 + var pins = transportUtil.resolve_pins(args) + var listener = Net.createServer(function (connection) { seneca.log.debug('listen', 'connection', listenOptions, 'remote', connection.remoteAddress, connection.remotePort) @@ -40,7 +42,20 @@ exports.listen = function (options, transportUtil) { return } - transportUtil.handle_request(seneca, data, options, function (out) { + // Issue: #97 + if (pins) { + // any topic is not pinned, verifying the calling one is pinned + var argspatrun = transportUtil.make_argspatrun(pins) + if (!argspatrun.find(data.act)) { + out = transportUtil.prepareResponse(seneca, data) + out.error = transportUtil.error('not_pinned', data) + + stringifier.write(out) + return + } + } + + transportUtil.handle_request(seneca, data, listenOptions, function (out) { if (out === null || !out.sync) { return } diff --git a/lib/transport-utils.js b/lib/transport-utils.js index b8e692b..c906a15 100644 --- a/lib/transport-utils.js +++ b/lib/transport-utils.js @@ -24,7 +24,8 @@ var internals = { 'message_loop': 'Inbound message rejected as looping back to this server.', 'data_error': 'Inbound message included an error description.', 'invalid_json': 'Invalid JSON: <%=input%>.', - 'unexcepted_async_error': 'Unexcepted error response to asynchronous message.' + 'unexcepted_async_error': 'Unexcepted error response to asynchronous message.', + 'not_pinned': 'Inbound message <%=act%> rejected as not pinned' }, override: true }) diff --git a/test/misc.test.js b/test/misc.test.js index 28b31d2..6f3e132 100644 --- a/test/misc.test.js +++ b/test/misc.test.js @@ -348,4 +348,58 @@ describe('Miscellaneous', function () { } }) }) + + it('listen-tcp-pin (#97)', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, {FOO: 1}) + }) + .add('bar:1', function (args, done) { + done(null, {BAR: 1}) + }) + .listen({type: 'tcp', port: '9999', pin: 'foo:*'}) + + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '9999'}) + + siClient.act('foo:1', function (err, out) { + Assert.equal(err, null) + Assert.equal(1, out.FOO) + + siClient.act('bar:1', function (err, out) { + Assert.equal(err.code, 'not_pinned') + if (err) return fin() + fin(new Error('Not pinned service called')) + }) + }) + }) + }) + it('listen-http-pin (#97)', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, {FOO: 1}) + }) + .add('bar:1', function (args, done) { + done(null, {BAR: 1}) + }) + .listen({type: 'http', port: '9998', pin: 'foo:*'}) + + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '9998'}) + + siClient.act('foo:1', function (err, out) { + Assert.equal(err, null) + Assert.equal(1, out.FOO) + + siClient.act('bar:1', function (err, out) { + Assert.equal(err.statusCode, 404) + Assert.equal(err.code, 'not_pinned') + if (err) return fin() + fin(new Error('Not pinned service called')) + }) + }) + }) + }) }) From 465e27ec9169106c95ab9dc1c096bc1b1d426a20 Mon Sep 17 00:00:00 2001 From: Jerome Valentin Date: Sun, 30 Oct 2016 19:02:09 +0100 Subject: [PATCH 2/6] Fix #135: Support optional entity in client --- lib/transport-utils.js | 22 ++++++++++++++-------- test/entity.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/transport-utils.js b/lib/transport-utils.js index c906a15..c7cd3f3 100644 --- a/lib/transport-utils.js +++ b/lib/transport-utils.js @@ -484,15 +484,21 @@ internals.Utils.prototype.handle_entity = function (seneca, raw) { raw = _.isObject(raw) ? raw : {} - if (raw.entity$) { - return seneca.make$(raw) - } - - _.each(raw, function (value, key) { - if (_.isObject(value) && value.entity$) { - raw[key] = seneca.make$(value) + // FIX #135 - entity is now optional in seneca, entity sent remotely may arrive + // in a seneca context that does not support entity. + // Don't try to "make" them as it is not supported and lead to: + // TypeError: seneca.make$ is not a function + if (seneca.make$) { + if (raw.entity$) { + return seneca.make$(raw) } - }) + + _.each(raw, function (value, key) { + if (_.isObject(value) && value.entity$) { + raw[key] = seneca.make$(value) + } + }) + } return raw } diff --git a/test/entity.test.js b/test/entity.test.js index cd6f154..0aaf373 100644 --- a/test/entity.test.js +++ b/test/entity.test.js @@ -91,4 +91,36 @@ describe('Transporting Entities', function () { }) }) }) + + it('supports client that are not using entity (#135)', function (done) { + var server = CreateInstance() + + if (server.version >= '2.0.0') { + server.use(Entity) + } + + server.ready(function () { + server.add({cmd: 'test'}, function (args, cb) { + let entity = this.make$('test').data$(args.entity) + entity.save$(function (err, entitySaveResponse) { + if (err) return cb(err) + cb(null, entitySaveResponse) + }) + }) + .add({role: 'entity', cmd: 'save'}, function (args, cb) { + cb(null, { entity: args.ent, tx: args.tx$ }) + }) + .listen({ type: 'tcp', port: 20105 }) + + var client = CreateInstance() + client.client({ type: 'tcp', port: 20105 }) + client.ready(function () { + this.act({cmd: 'test', entity: { name: 'bar' }}, function (err, res) { + Assert(!err) + Assert(res.entity.name === 'bar') + done() + }) + }) + }) + }) }) From 0b1347418b01622d0c3fd8188323686678e593d4 Mon Sep 17 00:00:00 2001 From: Jerome Valentin Date: Mon, 31 Oct 2016 18:39:25 +0100 Subject: [PATCH 3/6] #140: priviledge error to result in case both are provided --- lib/http.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/http.js b/lib/http.js index 80251d4..963d2a0 100644 --- a/lib/http.js +++ b/lib/http.js @@ -253,14 +253,14 @@ internals.sendResponse = function (seneca, transportUtil, res, out, data) { var outJson = 'null' var httpcode = 200 - if (out && out.res) { - httpcode = out.res.statusCode || httpcode - outJson = transportUtil.stringifyJSON(seneca, 'listen-web', out.res) - } - else if (out && out.error) { + if (out && out.error) { httpcode = out.error.statusCode || 500 outJson = transportUtil.stringifyJSON(seneca, 'listen-web', out.error) } + else if (out && out.res) { + httpcode = out.res.statusCode || httpcode + outJson = transportUtil.stringifyJSON(seneca, 'listen-web', out.res) + } var headers = { 'Content-Type': 'application/json', From 27fa44c3b4bbbcf16f88eda49d46976c3691e3c4 Mon Sep 17 00:00:00 2001 From: Jerome Valentin Date: Mon, 31 Oct 2016 18:41:53 +0100 Subject: [PATCH 4/6] #139: Add inward/outward support on TCP / HTTP --- lib/transport-utils.js | 25 ++++++++-- test/http.test.js | 101 ++++++++++++++++++++++++++++++++++++++++- test/misc.test.js | 4 +- test/tcp.test.js | 101 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 223 insertions(+), 8 deletions(-) diff --git a/lib/transport-utils.js b/lib/transport-utils.js index c7cd3f3..675ed3d 100644 --- a/lib/transport-utils.js +++ b/lib/transport-utils.js @@ -99,7 +99,6 @@ internals.Utils.prototype.handle_response = function (seneca, data, client_optio return false } - var actinfo = { id: data.id, accept: data.accept, @@ -234,14 +233,34 @@ internals.Utils.prototype.handle_request = function (seneca, data, listen_option input.id$ = data.id - this.requestAct(seneca, input, output, respond) + this.requestAct(seneca, input, output, respond, listen_options.inward, listen_options.outward) } -internals.Utils.prototype.requestAct = function (seneca, input, output, respond) { +internals.Utils.prototype.requestAct = function (seneca, input, output, respond, inward, outward) { var self = this try { + if (inward) { + inward({ seneca }, { msg: input }) + } seneca.act(input, function (err, out) { + if (outward) { + try { + var outward_data = { + err: err, + msg: input, + res: out + } + outward({ seneca }, outward_data) + err = outward_data.err + out = outward_data.res + } + catch (e) { + // outward failed, keep a trace of the original err if there was one + e.act_error = err + err = e + } + } self.update_output(input, output, err, out) respond(output) }) diff --git a/test/http.test.js b/test/http.test.js index 6669786..7707238 100644 --- a/test/http.test.js +++ b/test/http.test.js @@ -68,7 +68,6 @@ describe('Specific http', function () { }) }) - it('http-query', function (fin) { CreateInstance({errhandler: fin}) .add('a:1', function (args, done) { @@ -193,6 +192,106 @@ describe('Specific http', function () { }) }) }) + + it('apply inward/outward listen options on HTTP remote act call', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'http', port: '18997', + inward: (context, data) => { + data.msg.bar += 1 + data.msg.inward = 'INPUT UPGRADED' + }, + outward: (context, data) => { + data.res.BAR += 10 + data.res.inward = data.msg.inward + data.res.outward = 'OUTPUT UPGRADED' + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '18997'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 13) + Assert.equal(out.inward, 'INPUT UPGRADED') + Assert.equal(out.outward, 'OUTPUT UPGRADED') + fin() + }) + }) + }) + + it('reject HTTP remote act call in inward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'http', port: '18996', + inward: (context, data) => { + var e = new Error('HTTP inward rejected!') + e.error_code = 'inward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '18996'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'inward_rejected') + if (err) return fin() + fin(new Error('Inward does not reject remote call')) + }) + }) + }) + + it('reject HTTP remote act call in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'http', port: '18995', + outward: (context, data) => { + var e = new Error('HTTP outward rejected!') + e.error_code = 'outward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '18995'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'outward_rejected') + if (err) return fin() + fin(new Error('Outward does not reject remote call')) + }) + }) + }) + + it('catch HTTP remote act call error in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(new Error('Catchable failure'), {BAR: args.bar}) + }) + .listen({type: 'http', port: '18994', + outward: (context, data) => { + delete data.err + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '18994'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 2) + fin() + }) + }) + }) }) describe('Specific https', function () { diff --git a/test/misc.test.js b/test/misc.test.js index 6f3e132..a9535cc 100644 --- a/test/misc.test.js +++ b/test/misc.test.js @@ -44,7 +44,6 @@ describe('Miscellaneous', function () { counters.own++ } - var a = CreateInstance({ log: {map: [ {level: 'debug', regex: /\{a:1\}/, handler: log_a}, @@ -66,7 +65,6 @@ describe('Miscellaneous', function () { .listen({type: type, port: 40406}) .client({type: type, port: 40405}) - a.ready(function () { b.ready(function () { a.act('a:1', function (err, out) { @@ -193,7 +191,6 @@ describe('Miscellaneous', function () { .listen({type: type, port: 40407}) .client({type: type, port: 40405}) - a.ready(function () { b.ready(function () { c.ready(function () { @@ -375,6 +372,7 @@ describe('Miscellaneous', function () { }) }) }) + it('listen-http-pin (#97)', function (fin) { CreateInstance() .add('foo:1', function (args, done) { diff --git a/test/tcp.test.js b/test/tcp.test.js index f4806d2..5fcf1cf 100644 --- a/test/tcp.test.js +++ b/test/tcp.test.js @@ -65,7 +65,6 @@ describe('Specific tcp', function () { var settings = {tcp: {port: 0, host: 'localhost'}} - var transportUtil = new TransportUtil({ callmap: {}, seneca: seneca, @@ -215,5 +214,105 @@ describe('Specific tcp', function () { setTimeout(finish, 2000) }) + + it('apply inward/outward listen options on TCP remote act call', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19997', + inward: (context, data) => { + data.msg.bar += 1 + data.msg.inward = 'INPUT UPGRADED' + }, + outward: (context, data) => { + data.res.BAR += 10 + data.res.inward = data.msg.inward + data.res.outward = 'OUTPUT UPGRADED' + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19997'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 13) + Assert.equal(out.inward, 'INPUT UPGRADED') + Assert.equal(out.outward, 'OUTPUT UPGRADED') + fin() + }) + }) + }) + + it('reject TCP remote act call in inward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19996', + inward: (context, data) => { + var e = new Error('TCP inward rejected!') + e.error_code = 'inward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19996'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'inward_rejected') + if (err) return fin() + fin(new Error('Inward does not reject remote call')) + }) + }) + }) + + it('reject TCP remote act call in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19995', + outward: (context, data) => { + var e = new Error('TCP outward rejected!') + e.error_code = 'outward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19995'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'outward_rejected') + if (err) return fin() + fin(new Error('Outward does not reject remote call')) + }) + }) + }) + + it('catch TCP remote act call error in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(new Error('Catchable failure'), {BAR: args.bar}) + }) + .listen({type: 'tcp', port: '19994', + outward: (context, data) => { + delete data.err + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19994'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 2) + fin() + }) + }) + }) }) }) From 8097ba7a49f6e5df26b1a5d51fcb62ba7088aa27 Mon Sep 17 00:00:00 2001 From: Jerome Valentin Date: Mon, 31 Oct 2016 23:50:23 +0100 Subject: [PATCH 5/6] Fix TCP tests to verify #139: inward/outward --- test/tcp.test.js | 184 +++++++++++++++++++++++------------------------ 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/test/tcp.test.js b/test/tcp.test.js index 5fcf1cf..6e17d1b 100644 --- a/test/tcp.test.js +++ b/test/tcp.test.js @@ -214,105 +214,105 @@ describe('Specific tcp', function () { setTimeout(finish, 2000) }) + }) - it('apply inward/outward listen options on TCP remote act call', function (fin) { - CreateInstance() - .add('foo:1', function (args, done) { - done(null, { BAR: args.bar }) - }) - .listen({type: 'tcp', port: '19997', - inward: (context, data) => { - data.msg.bar += 1 - data.msg.inward = 'INPUT UPGRADED' - }, - outward: (context, data) => { - data.res.BAR += 10 - data.res.inward = data.msg.inward - data.res.outward = 'OUTPUT UPGRADED' - } - }) - .ready(function () { - var siClient = CreateInstance() - .client({type: 'tcp', port: '19997'}) - - siClient.act('foo:1,bar:2', function (err, out) { - if (err) return fin(err) - Assert.equal(out.BAR, 13) - Assert.equal(out.inward, 'INPUT UPGRADED') - Assert.equal(out.outward, 'OUTPUT UPGRADED') - fin() - }) + it('apply inward/outward listen options on TCP remote act call', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19997', + inward: (context, data) => { + data.msg.bar += 1 + data.msg.inward = 'INPUT UPGRADED' + }, + outward: (context, data) => { + data.res.BAR += 10 + data.res.inward = data.msg.inward + data.res.outward = 'OUTPUT UPGRADED' + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19997'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 13) + Assert.equal(out.inward, 'INPUT UPGRADED') + Assert.equal(out.outward, 'OUTPUT UPGRADED') + fin() }) - }) + }) + }) - it('reject TCP remote act call in inward listen option', function (fin) { - CreateInstance() - .add('foo:1', function (args, done) { - done(null, { BAR: args.bar }) - }) - .listen({type: 'tcp', port: '19996', - inward: (context, data) => { - var e = new Error('TCP inward rejected!') - e.error_code = 'inward_rejected' - throw e - } - }) - .ready(function () { - var siClient = CreateInstance() - .client({type: 'tcp', port: '19996'}) - - siClient.act('foo:1,bar:2', function (err, out) { - Assert.equal(err.error_code, 'inward_rejected') - if (err) return fin() - fin(new Error('Inward does not reject remote call')) - }) + it('reject TCP remote act call in inward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19996', + inward: (context, data) => { + var e = new Error('TCP inward rejected!') + e.error_code = 'inward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19996'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'inward_rejected') + if (err) return fin() + fin(new Error('Inward does not reject remote call')) }) - }) + }) + }) - it('reject TCP remote act call in outward listen option', function (fin) { - CreateInstance() - .add('foo:1', function (args, done) { - done(null, { BAR: args.bar }) - }) - .listen({type: 'tcp', port: '19995', - outward: (context, data) => { - var e = new Error('TCP outward rejected!') - e.error_code = 'outward_rejected' - throw e - } - }) - .ready(function () { - var siClient = CreateInstance() - .client({type: 'tcp', port: '19995'}) - - siClient.act('foo:1,bar:2', function (err, out) { - Assert.equal(err.error_code, 'outward_rejected') - if (err) return fin() - fin(new Error('Outward does not reject remote call')) - }) + it('reject TCP remote act call in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19995', + outward: (context, data) => { + var e = new Error('TCP outward rejected!') + e.error_code = 'outward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19995'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'outward_rejected') + if (err) return fin() + fin(new Error('Outward does not reject remote call')) }) - }) + }) + }) - it('catch TCP remote act call error in outward listen option', function (fin) { - CreateInstance() - .add('foo:1', function (args, done) { - done(new Error('Catchable failure'), {BAR: args.bar}) - }) - .listen({type: 'tcp', port: '19994', - outward: (context, data) => { - delete data.err - } - }) - .ready(function () { - var siClient = CreateInstance() - .client({type: 'tcp', port: '19994'}) - - siClient.act('foo:1,bar:2', function (err, out) { - if (err) return fin(err) - Assert.equal(out.BAR, 2) - fin() - }) + it('catch TCP remote act call error in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(new Error('Catchable failure'), {BAR: args.bar}) + }) + .listen({type: 'tcp', port: '19994', + outward: (context, data) => { + delete data.err + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19994'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 2) + fin() }) - }) + }) }) }) From 2a1f3109e323595d054e93426ce6bc19cf9d266e Mon Sep 17 00:00:00 2001 From: Jerome Valentin Date: Sat, 3 Jun 2017 17:30:47 +0200 Subject: [PATCH 6/6] Integrate killerspaz remarks A seneca warn --- lib/transport-utils.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/transport-utils.js b/lib/transport-utils.js index 675ed3d..04ec329 100644 --- a/lib/transport-utils.js +++ b/lib/transport-utils.js @@ -507,18 +507,21 @@ internals.Utils.prototype.handle_entity = function (seneca, raw) { // in a seneca context that does not support entity. // Don't try to "make" them as it is not supported and lead to: // TypeError: seneca.make$ is not a function - if (seneca.make$) { - if (raw.entity$) { - return seneca.make$(raw) - } + function make (entity) { + if (seneca.make$) return seneca.make$(entity) + else seneca.log.warn('[', entity, '] cannot be used as entity. seneca-entity plugin is missing.') + return entity + } - _.each(raw, function (value, key) { - if (_.isObject(value) && value.entity$) { - raw[key] = seneca.make$(value) - } - }) + if (raw.entity$) { + return make(raw) } + _.each(raw, function (value, key) { + if (_.isObject(value) && value.entity$) { + raw[key] = make(value) + } + }) return raw }