Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): add proxy support to stringify #2595

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion common/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ var stringify = function stringify (obj, depth) {
case 'undefined':
return 'undefined'
case 'function':
return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
try {
// function abc(a, b, c) { /* code goes here */ }
// -> function abc(a, b, c) { ... }
return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
} catch (err) {
if (err instanceof TypeError) {
// Proxy(function abc(...) { ... })
return 'Proxy(function ' + (obj.name || '') + '(...) { ... })'
} else {
throw err
}
}
case 'boolean':
return obj ? 'true' : 'false'
case 'object':
Expand Down
12 changes: 12 additions & 0 deletions test/client/stringify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ describe('stringify', function () {
})
})

// Conditionally run Proxy tests as it's not supported by all browsers yet
// http://caniuse.com/#feat=proxy
if (window.Proxy) {
it('should serialize proxied functions', function () {
var abcProxy = new Proxy(function abc (a, b, c) { return 'whatever' }, {})
var defProxy = new Proxy(function (d, e, f) { return 'whatever' }, {})

assert.deepEqual(stringify(abcProxy), 'Proxy(function abc(...) { ... })')
assert.deepEqual(stringify(defProxy), 'Proxy(function (...) { ... })')
})
}

it('should serialize arrays', function () {
assert.deepEqual(stringify(['a', 'b', null, true, false]), "['a', 'b', null, true, false]")
})
Expand Down