Skip to content

Commit

Permalink
update @hapi/joi, fix delegate prototype bug #784
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Jan 28, 2020
1 parent 8ee3552 commit 67169c7
Show file tree
Hide file tree
Showing 6 changed files with 645 additions and 430 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.19.0 2020-01-28

* Fix #784 by using Object.defineProperty to set delegate properties.
* Upgrade optioner to support @hapi/joi@17.


## 3.18.0 2020-01-16

* Action subscriptions now provide proper error messages.
Expand Down
37 changes: 26 additions & 11 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,28 +209,34 @@ exports.prior = function() {
}

// TODO: rename fixedargs
exports.delegate = function(fixedargs, fixedmeta) {
exports.delegate = function(fixedargs, fixedmeta, xlog) {
var self = this
var root = this.root
var opts = this.options()

fixedargs = fixedargs || {}
fixedmeta = fixedmeta || {}


var delegate = Object.create(self)

delegate.private$ = Object.create(self.private$)

delegate.did =
(delegate.did ? delegate.did + '/' : '') + self.private$.didnid()

delegate.log = function log() {

function delegate_log() {
return root.log.apply(delegate, arguments)
}
Object.assign(delegate.log, root.log)
delegate.log.self = () => delegate


Object.assign(delegate_log, root.log)
delegate_log.self = () => delegate

var strdesc
delegate.toString = function toString() {

function delegate_toString() {
if (strdesc) return strdesc
var vfa = {}
_.each(fixedargs, function(v, k) {
Expand All @@ -244,25 +250,34 @@ exports.delegate = function(fixedargs, fixedmeta) {
return strdesc
}

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

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

delegate.delegate = function delegate(further_fixedargs, further_fixedmeta) {
function delegate_delegate(further_fixedargs, 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)
}

// Somewhere to put contextual data for this delegate.
// For example, data for individual web requests.
// TODO: works better if prototype with Object.create
delegate.context = Object.assign({}, self.context)

var delegate_context = Object.assign({}, self.context)

// Prevents incorrect prototype properties in mocha test contexts
Object.defineProperties(delegate,{
log: {value:delegate_log},
toString: {value:delegate_toString},
fixedargs: {value:delegate_fixedargs},
fixedmeta: {value:delegate_fixedmeta},
delegate: {value:delegate_delegate},
context: {value:delegate_context}
})

return delegate
}

Expand Down
Loading

0 comments on commit 67169c7

Please sign in to comment.