Skip to content

Commit

Permalink
Node parse error fix (shakacode#641)
Browse files Browse the repository at this point in the history
* split line with too long code
* node json parse error fix
  • Loading branch information
alexeuler authored and justin808 committed Dec 9, 2016
1 parent 50a750a commit a600891
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project's source code will be documented in this fil
Contributors: please follow the recommendations outlined at [keepachangelog.com](http://keepachangelog.com/). Please use the existing headings and styling as a guide, and add a link for the version diff at the bottom of the file. Also, please update the `Unreleased` link to compare to the latest release version.

## [Unreleased]
##### Fixed
- "Node parse error" for node server rendering

## [6.3.2] - 2016-12-5
##### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const net = require('net');
const fs = require('fs');

const bundlePath = '../../app/assets/webpack/';
let bundleFileName = 'webpack-bundle.js';
let bundleFileName = 'server-bundle.js';

let currentArg;

Expand All @@ -13,13 +13,20 @@ function Handler() {

Handler.prototype.handle = (connection) => {
const callback = () => {
const terminator = '\r\n\0';
let request = '';
connection.setEncoding('utf8');
connection.on('data', (data) => {
console.log(`Processing request: ${data}`);

// eslint-disable-next-line no-eval
const result = eval(data);
connection.write(result);
console.log(`Processing chunk: ${data}`);
request += data;
if (data.slice(-terminator.length) === terminator) {
request = request.slice(0, -terminator.length);

// eslint-disable-next-line no-eval
const response = eval(request);
connection.write(`${response}${terminator}`);
request = '';
}
});
};

Expand Down Expand Up @@ -55,23 +62,32 @@ process.argv.forEach((val) => {
}
});

function loadBundle() {
if (handler.initialized) {
console.log('Reloading server bundle must be implemented by restarting the node process!');
return;
}

/* eslint-disable */
require(bundlePath + bundleFileName);
/* eslint-enable */
console.log(`Loaded server bundle: ${bundlePath}${bundleFileName}`);
handler.initialize();
}

try {
fs.mkdirSync(bundlePath);
} catch (e) {
if (e.code !== 'EEXIST') throw e;
if (e.code !== 'EEXIST') {
throw e;
} else {
loadBundle();
}
}

fs.watchFile(bundlePath + bundleFileName, (curr) => {
if (curr && curr.blocks && curr.blocks > 0) {
if (handler.initialized) {
console.log('Reloading server bundle must be implemented by restarting the node process!');
return;
}

// eslint-disable-next-line global-require, import/no-dynamic-require
require(bundlePath + bundleFileName);
console.log(`Loaded server bundle: ${bundlePath + bundleFileName}`);
handler.initialize();
loadBundle();
}
});

Expand Down
16 changes: 10 additions & 6 deletions lib/react_on_rails/server_rendering_pool/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ def trace_react_on_rails?
end

def eval_js(js_code)
max_int = (2**(32 - 2) - 1)
eof_symbol = "\r\n\0".freeze
max_int = (2**30 - 1)
@js_context_pool.with do |js_context|
js_context.send(js_code, 0)
result = js_context.recv(max_int)
result
js_context.send(js_code + eof_symbol, 0)
result = ""
while result[-eof_symbol.length..-1] != eof_symbol
result += js_context.recv(max_int)
end
result[0..-eof_symbol.length]
end
end

Expand All @@ -64,8 +68,8 @@ def create_js_context
client = UNIXSocket.new("client/node/node.sock")
client.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
rescue StandardError => e
Rails.logger.error("Unable to connect to socket: client/node/node.sock.
Make sure node server is up and running.")
Rails.logger.error("Unable to connect to socket: client/node/node.sock. \
Make sure node server is up and running.")
Rails.logger.error(e)
raise e
end
Expand Down

0 comments on commit a600891

Please sign in to comment.