Is it possible to enable EcmaScript 6 Harmony Proxies in nodejs? If so, what are the pros and cons? And is there any documentation on how to use them? Thanks !
5 Answers
Invoking node with node --harmony-proxies
should do the trick.
Pros: proxies are a very powerful feature when you really need them.
Cons: proxies are a much too powerful feature when you don't need them (which should be most of the time). Also, the implementation should still be regarded experimental.
As for documentation, all there really is atm is the Harmony wiki, in particular this page, which reflects the current implementation of proxies in V8 (and thus node):

- 34,518
- 3
- 61
- 72
-
1This indeed was necessary, however I still had trouble getting it to work. The following thread and example was also very useful: https://github.com/joyent/node/issues/4138 – odedfos Aug 13 '13 at 08:41
-
What is "much too powerful" about such a feature? – JSON Mar 10 '15 at 07:22
-
9**UPDATE 2015/10/12**: As of now, V8 does not yet support ES6 `new Proxy()` handler, you will have to use `Proxy.create()` instead or you can use [this shim](https://www.npmjs.com/package/harmony-proxy) to alias the function call for you – Blake Regalia Oct 12 '15 at 21:39
i recommend harmony-reflect, which makes it easy to e.g. set up get/set traps:
UPDATE careful, below is CoffeeScript
require 'harmony-reflect'
handler =
get: ( target, name ) ->
console.log 'get' name
return target[ name ]
set: ( target, name, value ) ->
console.log 'set' name
target[ '%is-clean' ] = no if value isnt target[ name ]
if value is undefined then delete target[ name ]
else target[ name ] = value
return value
clean = ( x ) ->
x[ '%is-clean' ] = yes
return x
p = Proxy {}, handler
p[ 'a' ] = 1
p[ 'b' ] = undefined
console.log p[ 'a' ], p[ 'b' ]
console.log "c" of p, p[ 'c' ]
console.log p
clean p
p[ 'a' ] = 1
console.log p
p[ 'a' ] = 42
console.log p
the above is the inceptive code to do 'transparent object persistence' in JavaScript. using harmony-reflect
, it becomes trivial to make it so that all get
and set
actions on an object get intercepted—in this demo, we set an %is-clean
attribute so we can test whether object members have been changed, and we also delete members that have been set to undefined.

- 3,624
- 36
- 48
-
5
-
2correct. the question was on NodeJS, not JavaScript. CoffeeScript compiles to JavaScript; you can copy-and-paste above code to js2coffee.org (second tab) if you prefer JS over CS. – flow Jan 12 '14 at 22:16
-
well, it would be nice to have ecmascript-harmony code here as the question is tagged as such. – Ege Özcan Jan 13 '14 at 09:35
-
1I don't get why this gets downvoted ... I'm just now working on a node-cli coffeescript thingy using harmony, or parts of it ... this is exactly what I need. thx – Joehannes Oct 27 '15 at 15:24
-
You can use pimped-proxy which a lightweight implementation of proxies, making declaration easier and ES5 compatible. Unlike the native Proxy, it can only proxy properties known at creation time.

- 11
- 1
Proxy
is now available natively in Node versions >= 6.

- 6,105
- 3
- 38
- 35
-
-
I did at the time, try the change log on the NodeJS site, pretty sure it was there – Scott Jungwirth Aug 17 '17 at 14:58
Harmony Proxies won't work all that well for nodejs because they're effectively synchronous type function calls. That is, you can't implement a proxy method that's async.
See this GitHub repository for examples: https://github.com/mschwartz/SilkJS-Harmony

- 63
- 3
-
1I don't quite follow what you are saying. Surely, the `get` trap can return an async function as a "method"? – Andreas Rossberg Nov 09 '12 at 06:59
-
Harmony code looks like: `foo = proxy_thing.some_member;` some_member is a function call. I don't know how it could be async. – mschwartz Feb 21 '13 at 00:17
-
1SilkJS Fan, I don't think evert function call needs to be async — just don't do IO inside and that's all. – andreypopp Jun 19 '13 at 21:15
-
Not every function call needs to be async, and you can use proxies for primitive kinds of things in a sync manner with nodejs. However, if you're familiar with the tie keyword in Perl, it would be a perfect use case for Harmony Proxies, but you'd have to sync get your value by key from redis (for example) synchronously. – mschwartz Aug 09 '13 at 17:08
-
14proxies and (a)synchronicity are as orthogonal concepts as, say, cheese sandwiches and bank holidays. – flow Sep 19 '13 at 15:44
-
To add to the discussion on why this answer is wrong, the upcoming async await will allow you to return values from a getter using a synchronous-like pattern. Until then, there are node-fibers and generators that can also do this. – Levi Roberts Mar 10 '16 at 14:56