deathcap / nodeachrome

run Node.js scripts in Google Chrome (experimental)
MIT License
2 stars 0 forks source link

browserify: fs.read: Error: Unknown encoding: 65536 #8

Closed deathcap closed 8 years ago

deathcap commented 8 years ago

Now that https://github.com/deathcap/nodeachrome/issues/3 is fixed, browserify gets further, but has trouble reading the file from disk using fs.read. Testing with:

g.evalsb('G.browserify("h.js").bundle().pipe(G.process.stdout).toString()')

Console output:

undefined send-native.js:56 main event data: Object {result: "[object Object]"} bundle-sandboxed.js:48266 proxiedSendNative fs.readFile ["/package.json", null] send-native.js:56 main event data: Object {cmd: "sendNative", msg: Object} send-native.js:61 received main thread sendNative event: MessageEvent {isTrusted: true, data: Object, origin: "null", lastEventId: "", source: Window…} send-native.js:35 sendNative Object {method: "fs.readFile", params: Array[2], id: 1} send-native.js:21 received incoming native msg: Object {result: Object, id: 1} bundle-sandboxed.js:48242 recvNative in sandbox Object {cmd: "recvNative", msg: Object} bundle-sandboxed.js:48258 decodeResponse Object {result: Object, id: 1} bundle-sandboxed.js:48266 proxiedSendNative fs.readFile ["/package.json", "utf8"] send-native.js:56 main event data: Object {cmd: "sendNative", msg: Object} send-native.js:61 received main thread sendNative event: MessageEvent {isTrusted: true, data: Object, origin: "null", lastEventId: "", source: Window…} send-native.js:35 sendNative Object {method: "fs.readFile", params: Array[2], id: 2} send-native.js:21 received incoming native msg: Object {result: "{↵ "name": "test",↵ "version": "1.0.0",↵ "descr…",↵ "license": "ISC",↵ "dependencies": {↵ }↵}↵", id: 2} bundle-sandboxed.js:48242 recvNative in sandbox Object {cmd: "recvNative", msg: Object} bundle-sandboxed.js:48258 decodeResponse Object {result: "{↵ "name": "test",↵ "version": "1.0.0",↵ "descr…",↵ "license": "ISC",↵ "dependencies": {↵ }↵}↵", id: 2} bundle-sandboxed.js:48266 proxiedSendNative fs.stat ["/h.js"] send-native.js:56 main event data: Object {cmd: "sendNative", msg: Object} send-native.js:61 received main thread sendNative event: MessageEvent {isTrusted: true, data: Object, origin: "null", lastEventId: "", source: Window…} send-native.js:35 sendNative Object {method: "fs.stat", params: Array[1], id: 3} send-native.js:21 received incoming native msg: Object {result: Object, id: 3} bundle-sandboxed.js:48242 recvNative in sandbox Object {cmd: "recvNative", msg: Object} bundle-sandboxed.js:48258 decodeResponse Object {result: Object, id: 3} bundle-sandboxed.js:48266 proxiedSendNative fs.realpath ["/h.js"] send-native.js:56 main event data: Object {cmd: "sendNative", msg: Object} send-native.js:61 received main thread sendNative event: MessageEvent {isTrusted: true, data: Object, origin: "null", lastEventId: "", source: Window…} send-native.js:35 sendNative Object {method: "fs.realpath", params: Array[1], id: 4} send-native.js:21 received incoming native msg: Object {result: "/h.js", id: 4} bundle-sandboxed.js:48242 recvNative in sandbox Object {cmd: "recvNative", msg: Object} bundle-sandboxed.js:48258 decodeResponse Object {result: "/h.js", id: 4} bundle-sandboxed.js:48266 proxiedSendNative fs.open ["/h.js", "r", 438] send-native.js:56 main event data: Object {cmd: "sendNative", msg: Object} send-native.js:61 received main thread sendNative event: MessageEvent {isTrusted: true, data: Object, origin: "null", lastEventId: "", source: Window…} send-native.js:35 sendNative Object {method: "fs.open", params: Array[3], id: 5} send-native.js:21 received incoming native msg: Object {result: 14, id: 5} bundle-sandboxed.js:48242 recvNative in sandbox Object {cmd: "recvNative", msg: Object} bundle-sandboxed.js:48258 decodeResponse Object {result: 14, id: 5} bundle-sandboxed.js:48266 proxiedSendNative fs.read [14, Uint8Array[65536], 0, 65536] send-native.js:56 main event data: Object {cmd: "sendNative", msg: Object} send-native.js:61 received main thread sendNative event: MessageEvent {isTrusted: true, data: Object, origin: "null", lastEventId: "", source: Window…} send-native.js:35 sendNative Object {method: "fs.read", params: Array[4], id: 6} send-native.js:15 unexpected native host disconnect: Port {} extensions::uncaught_exception_handler:8 Error in event handler for (unknown): Error: unexpected native host disconnect:[object Object] at disconnected (chrome-extension://ldcjdgpgjjhmjomfiekbmeogiamokiec/bundle-main.js:48299:9)handler @ extensions::uncaught_exception_handler:8(anonymous function) @ extensions::uncaught_exceptionhandler:100EventImpl.dispatch @ extensions::event_bindings:376EventImpl.dispatch @ extensions::event_bindings:393target.(anonymous function) @ extensions::SafeBuiltins:19publicClass.(anonymous function) @ extensions::utils:94dispatchOnDisconnect @ extensions::messaging:306

Native host crashes with:

received { method: 'fs.readFile', params: [ '/package.json', null ], id: 1 } sending { result: <Buffer 7b 0a 20 20 22 6e 61 6d 65 22 3a 20 22 74 65 73 74 22 2c 0a 20 20 22 76 65 72 73 69 6f 6e 22 3a 20 22 31 2e 30 2e 30 22 2c 0a 20 20 22 64 65 73 63 72 ... >, id: 1 } received { method: 'fs.readFile', params: [ '/package.json', 'utf8' ], id: 2 } sending { result: '{\n "name": "test",\n "version": "1.0.0",\n "description": "",\n "main": "index.js",\n "scripts": {\n "test": "echo \"Error: no test specified\" && exit 1"\n },\n "author": "",\n "license": "ISC",\n "dependencies": {\n }\n}\n', id: 2 } received { method: 'fs.stat', params: [ '/h.js' ], id: 3 } sending { result: { dev: 16777220, mode: 33188, nlink: 1, uid: 501, gid: 20, rdev: 0, blksize: 4096, ino: 62240272, size: 28, blocks: 8, atime: Mon Apr 25 2016 23:07:13 GMT-0700 (PDT), mtime: Sun Apr 24 2016 00:12:31 GMT-0700 (PDT), ctime: Sun Apr 24 2016 00:12:31 GMT-0700 (PDT), birthtime: Sun Apr 24 2016 00:12:31 GMT-0700 (PDT) }, id: 3 } received { method: 'fs.realpath', params: [ '/h.js' ], id: 4 } sending { result: '/h.js', id: 4 } received { method: 'fs.open', params: [ '/h.js', 'r', 438 ], id: 5 } fs.open /Users/admin/games/voxeljs/nodeachrome/sandbox/h.js 14 sending { result: 14, id: 5 } received { method: 'fs.read', params: [ 14, { '0': 0, … bunch of lines omitted… all zeros '65535': 0 }, 0, 65536 ], id: 6 } fs.js:88 throw new Error('Unknown encoding: ' + encoding); ^

Error: Unknown encoding: 65536 at assertEncoding (fs.js:88:11) at Object.fs.read (fs.js:558:5) at Transform.messageHandler as handler at Transform._transform (/Users/admin/games/voxeljs/nodeachrome/host/node_modules/chrome-native-messaging/index.js:107:10) at Transform._read (_stream_transform.js:167:10) at Transform._write (_stream_transform.js:155:12) at doWrite (_stream_writable.js:292:12) at writeOrBuffer (_stream_writable.js:278:5) at Transform.Writable.write (_stream_writable.js:207:11) at Input.ondata (_stream_readable.js:528:20)

Probably the buffer passed to fs.read() should not be passed all the way down to the native host, and back, since it is an 'inout' parameter of sorts. But what's this complaint about "encoding"? Attempting to ad-hoc convert Buffers in host fs.read() with:

   const buffer = typeof params[1] === 'object' && params[1].type === 'Buffer' ? new Buffer(params[1].data) : params[1]; // TODO: pass which args should be Buffers
deathcap commented 8 years ago

require('fs').read(0, new Buffer(1), 0, 1, (a,b)=>{console.log(a,b)}) require('fs').read(0, {}, 0, 1, (a,b)=>{console.log(a,b)}) Error: Unknown encoding: 1

Looks like Buffer is losing its type when passed between main and sandbox, so it degenerates to an object, which causes this error in fs.read. Messaging with postMessage uses the cloning algorithm, IIRC this can preserve typed arrays, but not Node.js Buffers, or what browserify represents them as.