hookio/hook.js — Issue #2: Problem with prototype.js
Author: molnarg
Created: 2011-11-24 01:29:08+00:00
State: open

I got this strange error when trying to send a message with hook.js from a webpage which had prototype.js included:

```
TypeError: Object [] has no method 'forEach'
at Object.unscrub (hook.js/node_modules/hook.io-webserver/node_modules/dnode/node_modules/dnode-protocol/index.js:203:11)
at EventEmitter.handle (hook.js/node_modules/hook.io-webserver/node_modules/dnode/node_modules/dnode-protocol/index.js:86:29)
at hook.js/node_modules/hook.io-webserver/node_modules/dnode/node_modules/dnode-protocol/index.js:81:20
at EventEmitter. (hook.js/node_modules/hook.io-webserver/node_modules/dnode/node_modules/lazy/lazy.js:62:13)
at EventEmitter. (hook.js/node_modules/hook.io-webserver/node_modules/dnode/node_modules/lazy/lazy.js:46:19)
at EventEmitter.emit (events.js:64:17)
at EventEmitter. (hook.js/node_modules/hook.io-webserver/node_modules/dnode/node_modules/lazy/lazy.js:46:39)
at EventEmitter.emit (events.js:64:17)
at hook.js/node_modules/hook.io-webserver/node_modules/dnode/node_modules/lazy/lazy.js:152:18
at Function. (hook.js/node_modules/hook.io-webserver/node_modules/dnode/node_modules/lazy/lazy.js:186:17)
```

The packet causing the error as seen by dnode-protocol is this:

{ method: 0,
arguments: [ 'browser-41::name', 'browser-41', '[Function]' ],
callbacks:
{ '2': '["each"]',
'3': '["eachSlice"]',
'4': '["all"]',
'5': '["any"]',
'6': '["collect"]',
'7': '["detect"]',
'8': '["findAll"]',
'9': '["grep"]',
'10': '["include"]',
'11': '["inGroupsOf"]',
'12': '["inject"]',
'13': '["invoke"]',
'14': '["max"]',
'15': '["min"]',
'16': '["partition"]',
'17': '["pluck"]',
'18': '["reject"]',
'19': '["sortBy"]',
'20': '["toArray"]',
'21': '["zip"]',
'22': '["size"]',
'23': '["inspect"]',
'29': '["entries"]',
'32': '["_reverse"]',
'33': '["_each"]',
'34': '["clear"]',
'35': '["first"]',
'36': '["last"]',
'37': '["compact"]',
'38': '["flatten"]',
'39': '["without"]',
'40': '["reverse"]',
'41': '["reduce"]',
'42': '["uniq"]',
'43': '["intersect"]',
'45': '["toJSON"]',
'46': '["remove"]',
'56': '["2"]',
'57': '["map"]',
'58': '["find"]',
'59': '["select"]',
'60': '["filter"]',
'61': '["member"]',
'62': '["every"]',
'63': '["some"]',
'64': '["clone"]' },
links: '[]' }

A normal packet should be something like this:

{ method: 0,
arguments: [ 'event::name', 'data', '[Function]' ],
callbacks: { '3': [ '2' ] },
links: [] }

dnode-protocol tries to call forEach on links, which is a string when the error is occuring. If it were an Array (as in the normal case), it would have a forEach because they use the traverse library which augments Array objects with functions like forEach. The long list with function names is a list of functions which are provided by prototype.js, see http://www.prototypejs.org/api/enumerable .

So I'm sure this bug is related to prototype.js, and the browser is sending some not well formed data packet to the server, but i don't know if it is an upstream dnode bug or it is something in hook.js. I haven't had the the time to test it with dnode without hook.js.