Skip to content

Commit

Permalink
use Object.assign; move decorate to api.js; move --seneca.print.tree …
Browse files Browse the repository at this point in the history
…to senec/@debug
  • Loading branch information
rjrodger committed Jul 22, 2019
1 parent 0c13309 commit 25772e1
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 163 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 3.12.0 2019-07-22

* Allow remote services to fully override local action patterns.
* Moved --seneca.print.tree debug print out to @seneca/debug plugin.
* Better action timeout log message.


## 3.11.0 2019-05-15

* Fixed explain$ transport issues.
Expand Down
39 changes: 32 additions & 7 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Copyright © 2010-2018 Richard Rodger and other contributors, MIT License. */
'use strict'

const Assert = require('assert')

var _ = require('lodash')
var Jsonic = require('jsonic')
var Norma = require('norma')
Expand Down Expand Up @@ -131,16 +133,16 @@ exports.delegate = function(fixedargs, fixedmeta) {
}

delegate.fixedargs = opts.strict.fixedargs
? _.extend({}, fixedargs, self.fixedargs)
: _.extend({}, self.fixedargs, fixedargs)
? Object.assign({}, fixedargs, self.fixedargs)
: Object.assign({}, self.fixedargs, fixedargs)

delegate.fixedmeta = opts.strict.fixedmeta
? _.extend({}, fixedmeta, self.fixedmeta)
: _.extend({}, self.fixedmeta, fixedmeta)
? Object.assign({}, fixedmeta, self.fixedmeta)
: Object.assign({}, self.fixedmeta, fixedmeta)

delegate.delegate = function delegate(further_fixedargs, further_fixedmeta) {
var args = _.extend({}, delegate.fixedargs, further_fixedargs || {})
var meta = _.extend({}, delegate.fixedmeta, further_fixedmeta || {})
var args = Object.assign({}, delegate.fixedargs, further_fixedargs || {})
var meta = Object.assign({}, delegate.fixedmeta, further_fixedmeta || {})
return self.delegate.call(this, args, meta)
}

Expand Down Expand Up @@ -676,6 +678,29 @@ exports.sub = function() {
return self
}


// Inspired by https://github.com/hapijs/hapi/blob/master/lib/plugin.js decorate
// TODO: convert to plugin configuration, with standard errors
exports.decorate = function() {
var args = Norma('property:s value:.', arguments)

var property = args.property
Assert(property[0] !== '_', 'property cannot start with _')
Assert(
this.private$.decorations[property] === undefined,
'seneca is already decorated with the property: ' + property
)
Assert(
this.root[property] === undefined,
'cannot override a core seneca property: ' + property
)

this.root[property] = this.private$.decorations[property] = args.value
}




intern.parse_config = function(args) {
var out = {}

Expand Down Expand Up @@ -723,7 +748,7 @@ intern.resolve_config = function(config, options) {

var base = options[out.type] || {}

out = _.extend({}, base, out)
out = Object.assign({}, base, out)

if (out.type === 'web' || out.type === 'tcp') {
out.port = out.port == null ? base.port : out.port
Expand Down
8 changes: 4 additions & 4 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ exports.each = function each(collect, func) {
}

exports.makedie = function(instance, ctxt) {
ctxt = _.extend(ctxt, instance.die ? instance.die.context : {})
ctxt = Object.assign(ctxt, instance.die ? instance.die.context : {})

var diecount = 0

Expand Down Expand Up @@ -511,7 +511,7 @@ exports.make_standard_act_log_entry = function(
var prior = callmeta.prior || {}
actdef = actdef || {}

return _.extend(
return Object.assign(
{
actid: callmeta.id,
msg: msg,
Expand Down Expand Up @@ -539,7 +539,7 @@ exports.make_standard_err_log_entry = function(err, ctxt) {
err.details.caller = ctxt.caller
}

return _.extend(
return Object.assign(
{
notice: err.message,
code: err.code,
Expand Down Expand Up @@ -692,4 +692,4 @@ History.prototype.toString = function toString() {
})
}

History.prototype.inspect = History.prototype.toString
History.prototype[Util.inspect.custom] = History.prototype.toString
2 changes: 1 addition & 1 deletion lib/inward.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function inward_msg_meta(ctxt, data) {
meta.pattern = ctxt.actdef.pattern
meta.client_pattern = ctxt.actdef.client_pattern
meta.action = ctxt.actdef.id
meta.plugin = _.extend(meta.plugin, ctxt.actdef.plugin)
meta.plugin = Object.assign({}, meta.plugin, ctxt.actdef.plugin)
meta.start = null == meta.start ? ctxt.start : meta.start
meta.parents = meta.parents || []
meta.trace = meta.trace || []
Expand Down
4 changes: 2 additions & 2 deletions lib/outward.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function outward_msg_meta(ctxt, data) {
var reply_meta = data.reply_meta

if (meta && reply_meta) {
meta.custom = _.extend(meta.custom, reply_meta.custom)
meta.custom = Object.assign(meta.custom, reply_meta.custom)
}
}

Expand Down Expand Up @@ -239,7 +239,7 @@ intern.act_error = function(instance, ctxt, data) {
var err = data.res || data.err

if (!err.seneca) {
var details = _.extend({}, err.details, {
var details = Object.assign({}, err.details, {
message: err.eraro && err.orig ? err.orig.message : err.message,
pattern: actdef.pattern,
fn: actdef.func,
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function resolve_options(fullname, plugindef, seneca) {

var defaults = plugindef.defaults || {}

var fullname_options = _.extend(
var fullname_options = Object.assign(
{},

// DEPRECATED: remove in 4
Expand All @@ -215,7 +215,7 @@ function resolve_options(fullname, plugindef, seneca) {
shortname = fullname.substring('seneca-'.length)
}

var shortname_options = _.extend(
var shortname_options = Object.assign(
{},

// DEPRECATED: remove in 4
Expand All @@ -238,7 +238,7 @@ function resolve_options(fullname, plugindef, seneca) {
base.errors = errors
}

var outopts = _.extend(
var outopts = Object.assign(
base,
shortname_options,
fullname_options,
Expand Down
97 changes: 5 additions & 92 deletions lib/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,20 @@
var Util = require('util')

// External modules.
var _ = require('lodash')
var Archy = require('archy')
var Minimist = require('minimist')

/** Handle command line specific functionality */
module.exports = function(seneca, process_argv) {
var argv = Minimist(process_argv.slice(2))

var cmdspec = argv.seneca
seneca.root.argv = cmdspec

if (!argv.seneca) {
return
}

var cmdspec = argv.seneca
if (cmdspec.print) {
if (cmdspec.print.tree) {
// Hack! Complex init means non-deterministic or multiple ready calls,
// so just delay tree print by some number of seconds to capture full tree.
var delay_seconds = cmdspec.print.tree.all || cmdspec.print.tree
if (_.isNumber(delay_seconds)) {
setTimeout(function() {
module.exports.print_tree(seneca, cmdspec)
}, 1000 * delay_seconds)
} else {
// Print after first ready
seneca.ready(function() {
module.exports.print_tree(this, cmdspec)
})
}
}


if (cmdspec.print) {
if (cmdspec.print.options) {
seneca.options({ debug: { print: { options: true } } })
}
Expand All @@ -51,77 +35,6 @@ module.exports.print_options = function print_options(instance, options) {
}
}

// TODO: remove to a plugin to remove dep on archy
module.exports.print_tree = function print_tree(seneca, cmdspec) {
var tree = {
label: 'Seneca action patterns for instance: ' + seneca.id,
nodes: []
}

function insert(nodes, current) {
if (nodes.length === 0) return

for (var i = 0; i < current.nodes.length; i++) {
if (nodes[0] === current.nodes[i].label) {
return insert(nodes.slice(1), current.nodes[i])
}
}

var nn = { label: nodes[0], nodes: [] }
current.nodes.push(nn)
insert(nodes.slice(1), nn)
}

_.each(seneca.list(), function(pat) {
var nodes = []
var ignore = false
_.each(pat, function(v, k) {
if (
(!cmdspec.print.tree.all &&
(k === 'role' &&
(v === 'seneca' ||
v === 'basic' ||
v === 'util' ||
v === 'entity' ||
v === 'web' ||
v === 'transport' ||
v === 'options' ||
v === 'mem-store' ||
v === 'seneca'))) ||
k === 'init'
) {
ignore = true
} else {
nodes.push(k + ':' + v)
}
})

if (!ignore) {
var meta = seneca.find(pat)

var metadesc = []
while (meta) {
metadesc.push(
'# ' +
(meta.plugin_fullname || '-') +
', ' +
meta.id +
', ' +
meta.func.name
)
meta = meta.priormeta
}

nodes.push(metadesc.join('\n'))

insert(nodes, tree)
}
})

/* eslint no-console: 0 */
console.log(Archy(tree))
}

module.exports.print = function print(err, out) {
if (err) {
console.log('ERROR: ' + err.message)
Expand Down
10 changes: 5 additions & 5 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function close(seneca, closer) {
function action_listen(msg, reply) {
var seneca = this

var config = _.extend({}, msg.config, { role: 'transport', hook: 'listen' })
var config = Object.assign({}, msg.config, { role: 'transport', hook: 'listen' })
var listen_msg = seneca.util.clean(_.omit(config, 'cmd'))

seneca.act(listen_msg, register(listen_msg, reply))
Expand All @@ -216,7 +216,7 @@ function action_listen(msg, reply) {
function action_client(msg, reply) {
var seneca = this

var config = _.extend({}, msg.config, { role: 'transport', hook: 'client' })
var config = Object.assign({}, msg.config, { role: 'transport', hook: 'client' })
var client_msg = seneca.util.clean(_.omit(config, 'cmd'))

seneca.act(client_msg, register(client_msg, reply))
Expand All @@ -225,7 +225,7 @@ function action_client(msg, reply) {
function hook_listen_web(msg, reply) {
var seneca = this.root.delegate()
var transport_options = seneca.options().transport
var config = _.clone(msg)
var config = seneca.util.deepextend(msg)

config.port = (null == config.port) ? transport_options.port : config.port
config.modify_response = config.modify_response || web_modify_response
Expand Down Expand Up @@ -284,7 +284,7 @@ function hook_listen_web(msg, reply) {
err: body
}
} else {
msg = _.extend(
msg = Object.assign(
body,
req.query && req.query.msg$ ? Jsonic(req.query.msg$) : {},
req.query || {}
Expand Down Expand Up @@ -362,7 +362,7 @@ function makeWreck() {
function hook_client_web(msg, hook_reply) {
var seneca = this.root.delegate()
var transport_options = seneca.options().transport
var config = _.clone(msg)
var config = seneca.util.deepextend(msg)

config.port = (null == config.port) ? transport_options.port : config.port

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
},
"dependencies": {
"@hapi/joi": "^15.0.3",
"archy": "^1.0.0",
"eraro": "^1.1.0",
"gate-executor": "^2.0.1",
"gex": "^0.3.0",
Expand Down
Loading

0 comments on commit 25772e1

Please sign in to comment.