From 88db107172c94fe0ade26df626e35f26c8ad1bfb Mon Sep 17 00:00:00 2001 From: Philip O'Brien Date: Mon, 13 Oct 2014 17:40:36 +0100 Subject: [PATCH] correct indentation to 3 --- .jshintrc | 2 +- lib/binaryutils.js | 276 ++++++++++----------- lib/m2nodehandler.js | 570 +++++++++++++++++++++---------------------- 3 files changed, 424 insertions(+), 424 deletions(-) diff --git a/.jshintrc b/.jshintrc index 2caaa28..6a7236b 100644 --- a/.jshintrc +++ b/.jshintrc @@ -11,7 +11,7 @@ "boss" : true, "eqnull" : true, "node" : true, - "indent" : 2, + "indent" : 3, "strict" : true, "forin" : true, "maxparams" : 5, diff --git a/lib/binaryutils.js b/lib/binaryutils.js index 359488f..f6be10c 100644 --- a/lib/binaryutils.js +++ b/lib/binaryutils.js @@ -6,169 +6,169 @@ var mongrel2_upload_dir = ''; var arrayToObj = function(lines, boundry) { - var post_data = {}; - - var meta = true; - var name = ""; - var data = ""; - var nvp; - var nvpName; - var nvpSplit; - var nvpValue; - - for(var i = 0, len = lines.length; i < len; i++) { - - var line = lines[i]; - - if (-1 !== line.indexOf(boundry)) { - continue; - } else if (0 === line.length) { - post_data[name]["value"] = lines[i+1]; - i++; - } else if (0 === line.indexOf("Content-Disposition: form-data; ")) { - line = line.replace("Content-Disposition: form-data; ", ''); - nvp = line.split("; "); - - for (var j = 0, k = nvp.length; j < k; j++){ - nvpSplit = nvp[j].split("="); - nvpName = nvpSplit[0]; - nvpValue = nvpSplit[1].substring(1, nvpSplit[1].length -1); - - if ("name" === nvpName) { - name = nvpValue; - post_data[name] = {}; - } else { - post_data[name][nvpName] = nvpValue; - } + var post_data = {}; + + var meta = true; + var name = ""; + var data = ""; + var nvp; + var nvpName; + var nvpSplit; + var nvpValue; + + for(var i = 0, len = lines.length; i < len; i++) { + + var line = lines[i]; + + if (-1 !== line.indexOf(boundry)) { + continue; + } else if (0 === line.length) { + post_data[name]["value"] = lines[i+1]; + i++; + } else if (0 === line.indexOf("Content-Disposition: form-data; ")) { + line = line.replace("Content-Disposition: form-data; ", ''); + nvp = line.split("; "); + + for (var j = 0, k = nvp.length; j < k; j++){ + nvpSplit = nvp[j].split("="); + nvpName = nvpSplit[0]; + nvpValue = nvpSplit[1].substring(1, nvpSplit[1].length -1); + + if ("name" === nvpName) { + name = nvpValue; + post_data[name] = {}; + } else { + post_data[name][nvpName] = nvpValue; + } + } + } else { + nvp = line.split(": "); + nvpName = nvp[0]; + nvpValue = nvp[1]; + post_data[name][nvpName] = nvpValue; } - } else { - nvp = line.split(": "); - nvpName = nvp[0]; - nvpValue = nvp[1]; - post_data[name][nvpName] = nvpValue; - } - } - - return post_data; + } + + return post_data; }; var parseBinaryBody = function(data, boundary) { - var _lines = []; - var start = 0; - var payloadStart = 0; - var has_payload = false; - - for (var i = 0, len = data.length; i < len; i++) { - - if (10 === data.readInt8(i) && 13 === data.readInt8(i-1)) { - - var end = i-1; - - if (start >= end) { - _lines.push(''); - } else { - var line = data.toString('utf8', start, end); - - if (-1 !== line.indexOf(boundary)) { - - if (has_payload) { - if ((payloadStart + 1000) < start) { - _lines.push(data.toString('base64', payloadStart, start-2)); - } else { - _lines.push(data.toString('utf8', payloadStart, start-2)); - } - has_payload = false; - } - _lines.push(line); - } else { - if (!has_payload) { - //find if not a normal line - if (0 !== line.indexOf('Content-')) { - has_payload = true; - payloadStart = start; - } else { - _lines.push(line); - } + var _lines = []; + var start = 0; + var payloadStart = 0; + var has_payload = false; + + for (var i = 0, len = data.length; i < len; i++) { + + if (10 === data.readInt8(i) && 13 === data.readInt8(i-1)) { + + var end = i-1; + + if (start >= end) { + _lines.push(''); + } else { + var line = data.toString('utf8', start, end); + + if (-1 !== line.indexOf(boundary)) { + + if (has_payload) { + if ((payloadStart + 1000) < start) { + _lines.push(data.toString('base64', payloadStart, start-2)); + } else { + _lines.push(data.toString('utf8', payloadStart, start-2)); + } + has_payload = false; + } + _lines.push(line); + } else { + if (!has_payload) { + //find if not a normal line + if (0 !== line.indexOf('Content-')) { + has_payload = true; + payloadStart = start; + } else { + _lines.push(line); + } + } } - } + } + start = i + 1; } - start = i + 1; - } - } + } - var post_data = arrayToObj(_lines, boundary); - //post_data.file.buffer = new Buffer(post_data.file.value, 'base64'); + var post_data = arrayToObj(_lines, boundary); + //post_data.file.buffer = new Buffer(post_data.file.value, 'base64'); - return post_data; + return post_data; }; var fileToStringArr = function (filePath, boundary, zmqMsg, callback) { - fs.readFile(filePath, function (err, data) { + fs.readFile(filePath, function (err, data) { - if (err) { - throw err; - } + if (err) { + throw err; + } - zmqMsg.json = parseBinaryBody(data, boundary); + zmqMsg.json = parseBinaryBody(data, boundary); - callback(zmqMsg); + callback(zmqMsg); - }); + }); }; var processBinaryUpload = function(headersAndMongrel2, client, envelope, callback) { - var headers = headersAndMongrel2[3]; - - var uploadStart = headers['x-mongrel2-upload-start']; - var uploadDone = headers['x-mongrel2-upload-done']; - var contentLength = headers['content-length']; - var boundry; - var zmqMsg; - - if (contentLength >= 20000000) { - client.send(headersAndMongrel2[0], headersAndMongrel2[1], resp, 417, header_json, {"error" : "File too large."}); - } else if (undefined === uploadStart && undefined === uploadDone) { - - boundry = headers['content-type'].replace('multipart/form-data; boundary=', '' ); - var out = parseBinaryBody(envelope, boundry); - //construct the same object but - zmqMsg = { - headers : headers, - json : out, - uuid : headersAndMongrel2[0], - connId : headersAndMongrel2[1], - path : headersAndMongrel2[2], - valid_json : true - }; - callback(zmqMsg); - } else if(undefined === uploadDone) { + var headers = headersAndMongrel2[3]; + + var uploadStart = headers['x-mongrel2-upload-start']; + var uploadDone = headers['x-mongrel2-upload-done']; + var contentLength = headers['content-length']; + var boundry; + var zmqMsg; + + if (contentLength >= 20000000) { + client.send(headersAndMongrel2[0], headersAndMongrel2[1], resp, 417, header_json, {"error" : "File too large."}); + } else if (undefined === uploadStart && undefined === uploadDone) { + + boundry = headers['content-type'].replace('multipart/form-data; boundary=', '' ); + var out = parseBinaryBody(envelope, boundry); + //construct the same object but + zmqMsg = { + headers : headers, + json : out, + uuid : headersAndMongrel2[0], + connId : headersAndMongrel2[1], + path : headersAndMongrel2[2], + valid_json : true + }; + callback(zmqMsg); + } else if(undefined === uploadDone) { //do nothing, wait for upload complete message - } else if (undefined !== uploadDone && uploadStart !== uploadDone) { - - client.send(headersAndMongrel2[0], headersAndMongrel2[1], responseObj, status.INTERNAL_SERVER_ERROR_500, header_json, {"error" : "Got the wrong file:"}); - } else if(undefined !== uploadDone) { - var filePath = mongrel2_upload_dir + headers['x-mongrel2-upload-done']; - - boundry = headers['content-type'].replace('multipart/form-data; boundary=', '' ); - - zmqMsg = { - headers : headers, - uuid : headersAndMongrel2[0], - connId : headersAndMongrel2[1], - path : headersAndMongrel2[2], - valid_json : true - }; - - fileToStringArr(filePath, boundry, zmqMsg, callback); - } else { - callback(parse(envelope.toString('utf8'))); - } + } else if (undefined !== uploadDone && uploadStart !== uploadDone) { + + client.send(headersAndMongrel2[0], headersAndMongrel2[1], responseObj, status.INTERNAL_SERVER_ERROR_500, header_json, {"error" : "Got the wrong file:"}); + } else if(undefined !== uploadDone) { + var filePath = mongrel2_upload_dir + headers['x-mongrel2-upload-done']; + + boundry = headers['content-type'].replace('multipart/form-data; boundary=', '' ); + + zmqMsg = { + headers : headers, + uuid : headersAndMongrel2[0], + connId : headersAndMongrel2[1], + path : headersAndMongrel2[2], + valid_json : true + }; + + fileToStringArr(filePath, boundry, zmqMsg, callback); + } else { + callback(parse(envelope.toString('utf8'))); + } }; diff --git a/lib/m2nodehandler.js b/lib/m2nodehandler.js index d961422..6e9f164 100644 --- a/lib/m2nodehandler.js +++ b/lib/m2nodehandler.js @@ -10,401 +10,401 @@ var pre_process_filters = []; var post_process_filters = []; var status = { - OK_200 : 200, - NO_CONTENT_204 : 204, - BAD_REQUEST_400 : 400, - UNAUTHORIZED_401 : 401, - FORBIDDEN_402 : 402, - NOT_FOUND_404 : 404, - INTERNAL_SERVER_ERROR_500 : 500 + OK_200 : 200, + NO_CONTENT_204 : 204, + BAD_REQUEST_400 : 400, + UNAUTHORIZED_401 : 401, + FORBIDDEN_402 : 402, + NOT_FOUND_404 : 404, + INTERNAL_SERVER_ERROR_500 : 500 }; var standard_headers = { - plain : { - "access-control-allow-origin" : '*', - 'Content-Type': 'text/plain; charset=utf-8' - }, - json : { - "access-control-allow-origin" : '*', - 'Content-Type': 'application/json; charset=utf-8' - }, - html : { - "access-control-allow-origin" : '*', - 'Content-Type': 'text/html; charset=utf-8' - }, - stream: { - "access-control-allow-origin" : '*', - 'Content-Type': 'text/event-stream; charset=utf-8', - 'Cache-Control' : 'no-cache', - 'Connection' : 'keep-alive' - } + plain : { + "access-control-allow-origin" : '*', + 'Content-Type': 'text/plain; charset=utf-8' + }, + json : { + "access-control-allow-origin" : '*', + 'Content-Type': 'application/json; charset=utf-8' + }, + html : { + "access-control-allow-origin" : '*', + 'Content-Type': 'text/html; charset=utf-8' + }, + stream: { + "access-control-allow-origin" : '*', + 'Content-Type': 'text/event-stream; charset=utf-8', + 'Cache-Control' : 'no-cache', + 'Connection' : 'keep-alive' + } }; var header_cors = { - "access-control-allow-origin" : "*", - "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS, PATCH", - "access-control-allow-headers": "content-type, accept, authorization", - "access-control-max-age" : 10, // Seconds. - "content-length" : 0 + "access-control-allow-origin" : "*", + "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS, PATCH", + "access-control-allow-headers": "content-type, accept, authorization", + "access-control-max-age" : 10, // Seconds. + "content-length" : 0 }; var statusMessages = { - '100': 'Continue', - '101': 'Switching Protocols', - '200': 'OK', - '201': 'Created', - '202': 'Accepted', - '203': 'Non-Authoritative Information', - '204': 'No Content', - '205': 'Reset Content', - '206': 'Partial Content', - '300': 'Multiple Choices', - '301': 'Moved Permanently', - '302': 'Found', - '303': 'See Other', - '304': 'Not Modified', - '305': 'Use Proxy', - '307': 'Temporary Redirect', - '400': 'Bad Request', - '401': 'Unauthorized', - '402': 'Payment Required', - '403': 'Forbidden', - '404': 'Not Found', - '405': 'Method Not Allowed', - '406': 'Not Acceptable', - '407': 'Proxy Authentication Required', - '408': 'Request Timeout', - '409': 'Conflict', - '410': 'Gone', - '411': 'Length Required', - '412': 'Precondition Failed', - '413': 'Request Entity Too Large', - '414': 'Request-URI Too Large', - '415': 'Unsupported Media Type', - '416': 'Request Range Not Satisfiable', - '417': 'Expectation Failed', - '500': 'Internal Server Error', - '501': 'Not Implemented', - '502': 'Bad Gateway', - '503': 'Service Unavailable', - '504': 'Gateway Timeout', - '505': 'HTTP Version Not Supported' + '100': 'Continue', + '101': 'Switching Protocols', + '200': 'OK', + '201': 'Created', + '202': 'Accepted', + '203': 'Non-Authoritative Information', + '204': 'No Content', + '205': 'Reset Content', + '206': 'Partial Content', + '300': 'Multiple Choices', + '301': 'Moved Permanently', + '302': 'Found', + '303': 'See Other', + '304': 'Not Modified', + '305': 'Use Proxy', + '307': 'Temporary Redirect', + '400': 'Bad Request', + '401': 'Unauthorized', + '402': 'Payment Required', + '403': 'Forbidden', + '404': 'Not Found', + '405': 'Method Not Allowed', + '406': 'Not Acceptable', + '407': 'Proxy Authentication Required', + '408': 'Request Timeout', + '409': 'Conflict', + '410': 'Gone', + '411': 'Length Required', + '412': 'Precondition Failed', + '413': 'Request Entity Too Large', + '414': 'Request-URI Too Large', + '415': 'Unsupported Media Type', + '416': 'Request Range Not Satisfiable', + '417': 'Expectation Failed', + '500': 'Internal Server Error', + '501': 'Not Implemented', + '502': 'Bad Gateway', + '503': 'Service Unavailable', + '504': 'Gateway Timeout', + '505': 'HTTP Version Not Supported' }; var setCors = function(headers){ - header_cors = headers; + header_cors = headers; }; var addPreProcessFilter = function(fun){ - pre_process_filters.push(fun); + pre_process_filters.push(fun); }; var addPostProcessFilter = function(fun){ - post_process_filters.push(fun); + post_process_filters.push(fun); }; var split = function(str, chr, limit) { - var parts = str.split (chr); - var ret = parts.slice(0, limit - 1); + var parts = str.split (chr); + var ret = parts.slice(0, limit - 1); - ret.push(parts.slice(limit - 1).join(chr)); - return ret; + ret.push(parts.slice(limit - 1).join(chr)); + return ret; }; var parseNetstring = function(ns) { - var nsSplit = split(ns, ':', 2); - var length = parseInt (nsSplit[0], 10); - var payload = nsSplit[1]; + var nsSplit = split(ns, ':', 2); + var length = parseInt (nsSplit[0], 10); + var payload = nsSplit[1]; - if (payload[length] !== ',') { - throw 'Netstring did not end in ","'; - } + if (payload[length] !== ',') { + throw 'Netstring did not end in ","'; + } - return [ payload.slice(0, length), payload.slice(length + 1) ]; + return [ payload.slice(0, length), payload.slice(length + 1) ]; }; var getJSON = function(str){ - try { - return JSON.parse(str); - } catch(e) { - return null; - } + try { + return JSON.parse(str); + } catch(e) { + return null; + } }; var isValidJSON = function(str){ - if (0 === str.length){ - return true; - } - try { - JSON.parse(str); - return true; - } catch(e) { - return false; - } + if (0 === str.length){ + return true; + } + try { + JSON.parse(str); + return true; + } catch(e) { + return false; + } }; var parse = function(mongrelString) { - var mongrelStringSplit = split(mongrelString, ' ', 4); - - var uuid = mongrelStringSplit[0]; - var connId = parseInt(mongrelStringSplit[1]); - var path = mongrelStringSplit[2]; - var payload = mongrelStringSplit[3]; - - var paySplit = parseNetstring(payload); - var headers = getJSON (paySplit[0]); - var body = parseNetstring(paySplit[1])[0]; - - var valid_json = isValidJSON(body); - var json = getJSON(body); - - return { - headers : headers, - uuid : uuid, - connId : connId, - path : path, - json : json, - valid_json : valid_json - }; + var mongrelStringSplit = split(mongrelString, ' ', 4); + + var uuid = mongrelStringSplit[0]; + var connId = parseInt(mongrelStringSplit[1]); + var path = mongrelStringSplit[2]; + var payload = mongrelStringSplit[3]; + + var paySplit = parseNetstring(payload); + var headers = getJSON (paySplit[0]); + var body = parseNetstring(paySplit[1])[0]; + + var valid_json = isValidJSON(body); + var json = getJSON(body); + + return { + headers : headers, + uuid : uuid, + connId : connId, + path : path, + json : json, + valid_json : valid_json + }; }; var dataToBuffer = function(data){ - if (Buffer.isBuffer(data)){ - return data; - } + if (Buffer.isBuffer(data)){ + return data; + } - var body = (Object.prototype.toString.call(data) === '[object String]') ? data : JSON.stringify(data); + var body = (Object.prototype.toString.call(data) === '[object String]') ? data : JSON.stringify(data); - if (undefined === body){ - body = ''; - } + if (undefined === body){ + body = ''; + } - return new Buffer(body, 'utf8' ); + return new Buffer(body, 'utf8' ); }; var buildZmqResponse = function(uuid, connId, status, headers, data) { - var header_arr = []; - var dataBuffer = dataToBuffer(data); - - header_arr.push('Content-Length: ' + dataBuffer.length); - - for (var header in headers) { - if (__hasProp.call(headers, header)) { - header_arr.push('' + header + ': ' + headers[header]); - } - } - - var str = '' - + uuid - + ' ' - + String(connId).length - + ':' - + connId - + ', ' - + 'HTTP/1.1 ' - + status - + ' ' - + statusMessages[status] - + '\r\n' - + header_arr.join('\r\n') - + '\r\n\r\n'; - - var strBuffer = new Buffer(str, 'utf8' ); - - return Buffer.concat([strBuffer, dataBuffer]); + var header_arr = []; + var dataBuffer = dataToBuffer(data); + + header_arr.push('Content-Length: ' + dataBuffer.length); + + for (var header in headers) { + if (__hasProp.call(headers, header)) { + header_arr.push('' + header + ': ' + headers[header]); + } + } + + var str = '' + + uuid + + ' ' + + String(connId).length + + ':' + + connId + + ', ' + + 'HTTP/1.1 ' + + status + + ' ' + + statusMessages[status] + + '\r\n' + + header_arr.join('\r\n') + + '\r\n\r\n'; + + var strBuffer = new Buffer(str, 'utf8' ); + + return Buffer.concat([strBuffer, dataBuffer]); }; var sender = function(config) { - dbc.hasMember(config, 'spec'); - dbc.hasMemberIn(config, 'type', ['pub', 'push']); + dbc.hasMember(config, 'spec'); + dbc.hasMemberIn(config, 'type', ['pub', 'push']); - var send = zmq.socket(config.type); - send.identity = config.id; + var send = zmq.socket(config.type); + send.identity = config.id; - process.on('exit', function(){ - if (send){ - send.close(); - process.exit(); - } - }); - - if (config.bind) { - send.bindSync(config.spec); - } else { - send.connect(config.spec); - } - - if (config.isMongrel2) { - return { - 'send' : function (uuid, connId, status, headers, body) { - - var msg = { - 'uuid' : uuid, - 'connId' : connId, - 'status' : status, - 'headers' : headers, - 'body' : body - }; - - for (var i in post_process_filters) { - if(post_process_filters.hasOwnProperty(i)) { - post_process_filters[i](msg); - } - - } - - var message = buildZmqResponse(msg.uuid, msg.connId, msg.status, msg.headers, msg.body); - return send.send(message); - } - }; - } else { - return { - 'send' : function (message) { - send.send(JSON.stringify(message)); + process.on('exit', function(){ + if (send){ + send.close(); + process.exit(); } - }; - } + }); + + if (config.bind) { + send.bindSync(config.spec); + } else { + send.connect(config.spec); + } + + if (config.isMongrel2) { + return { + 'send' : function (uuid, connId, status, headers, body) { + + var msg = { + 'uuid' : uuid, + 'connId' : connId, + 'status' : status, + 'headers' : headers, + 'body' : body + }; + + for (var i in post_process_filters) { + if(post_process_filters.hasOwnProperty(i)) { + post_process_filters[i](msg); + } + + } + + var message = buildZmqResponse(msg.uuid, msg.connId, msg.status, msg.headers, msg.body); + return send.send(message); + } + }; + } else { + return { + 'send' : function (message) { + send.send(JSON.stringify(message)); + } + }; + } }; var readFirstLine = function(data) { - for (var i = 0; i < data.length; i++) { - if (10 === data.readInt8(i) && 13 === data.readInt8(i-1)) { - return data.toString('utf8', 0, i); - } - } - return data.toString('utf8'); + for (var i = 0; i < data.length; i++) { + if (10 === data.readInt8(i) && 13 === data.readInt8(i-1)) { + return data.toString('utf8', 0, i); + } + } + return data.toString('utf8'); }; var getHeadersAndMongrel2 = function(data) { - var mongrelString = readFirstLine(data); - var mongrelStringSplit = split(mongrelString, ' ', 4); - var payload = mongrelStringSplit[3]; - var paySplit = parseNetstring(payload); + var mongrelString = readFirstLine(data); + var mongrelStringSplit = split(mongrelString, ' ', 4); + var payload = mongrelStringSplit[3]; + var paySplit = parseNetstring(payload); - return [mongrelStringSplit[0], mongrelStringSplit[1], mongrelStringSplit[2], getJSON(paySplit[0])]; + return [mongrelStringSplit[0], mongrelStringSplit[1], mongrelStringSplit[2], getJSON(paySplit[0])]; }; var receiver = function(config, error_config, callback) { - dbc.hasMember (config, 'spec'); - dbc.hasMemberIn (config, 'type', ['sub', 'pull']); - dbc.conditionalHasMember(config, 'subscribe', config.type === 'sub'); + dbc.hasMember (config, 'spec'); + dbc.hasMemberIn (config, 'type', ['sub', 'pull']); + dbc.conditionalHasMember(config, 'subscribe', config.type === 'sub'); - var receive = zmq.socket(config.type); - receive.identity = config.id; + var receive = zmq.socket(config.type); + receive.identity = config.id; - var client = null; + var client = null; - if (config.isMongrel2) { - dbc.hasMember (error_config, 'spec'); - dbc.assert (error_config.isMongrel2 === true); - client = zmq.socket('pub'); - client.connect(error_config.spec); - } + if (config.isMongrel2) { + dbc.hasMember (error_config, 'spec'); + dbc.assert (error_config.isMongrel2 === true); + client = zmq.socket('pub'); + client.connect(error_config.spec); + } - process.on('exit', function() { - if (receive) { - receive.close(); - } - if (config.isMongrel2 && client) { - client.close(); - } - process.exit(); - }); + process.on('exit', function() { + if (receive) { + receive.close(); + } + if (config.isMongrel2 && client) { + client.close(); + } + process.exit(); + }); - if (config.isMongrel2) { - receive.on('message', function (envelope) { + if (config.isMongrel2) { + receive.on('message', function (envelope) { - var headersAndMongrel2 = getHeadersAndMongrel2(envelope); - var headers = headersAndMongrel2[3]; + var headersAndMongrel2 = getHeadersAndMongrel2(envelope); + var headers = headersAndMongrel2[3]; - var proceed = true; + var proceed = true; - var filterResponse = function (uuid, connId, status, headers, body) { - proceed = false; - var message = buildZmqResponse(uuid, connId, status, headers, body); - return client.send(message); - }; + var filterResponse = function (uuid, connId, status, headers, body) { + proceed = false; + var message = buildZmqResponse(uuid, connId, status, headers, body); + return client.send(message); + }; - var callbackWithFilter = function(zmqMsg) { + var callbackWithFilter = function(zmqMsg) { - for (var i in pre_process_filters) { - if(pre_process_filters.hasOwnProperty(i)) { - pre_process_filters[i](zmqMsg, filterResponse); - if (false === proceed) { - return; + for (var i in pre_process_filters) { + if(pre_process_filters.hasOwnProperty(i)) { + pre_process_filters[i](zmqMsg, filterResponse); + if (false === proceed) { + return; + } + } } - } - } - callback(zmqMsg); - }; + callback(zmqMsg); + }; - if( binaryutils.isBinaryUpload(headers)) { - binaryutils.processBinaryUpload(headersAndMongrel2, client, envelope, callbackWithFilter); - } else { - var zmqMsg = parse(envelope.toString('utf8')); - - if(zmqMsg.headers.METHOD === 'JSON' && 'disconnect' === zmqMsg.json.type) { - //do nothing - } else if (zmqMsg.headers.METHOD === 'OPTIONS') { - client.send(buildZmqResponse(zmqMsg.uuid, zmqMsg.connId, status.NO_CONTENT_204, header_cors, '')); - } else if (!zmqMsg.valid_json) { - client.send(buildZmqResponse(zmqMsg.uuid, zmqMsg.connId, status.BAD_REQUEST_400, standard_headers.json, {'message':'Invalid JSON'})); - } else { - delete zmqMsg['valid_json']; - return callbackWithFilter(zmqMsg); - } - } - }); - } else { + if( binaryutils.isBinaryUpload(headers)) { + binaryutils.processBinaryUpload(headersAndMongrel2, client, envelope, callbackWithFilter); + } else { + var zmqMsg = parse(envelope.toString('utf8')); + + if(zmqMsg.headers.METHOD === 'JSON' && 'disconnect' === zmqMsg.json.type) { + //do nothing + } else if (zmqMsg.headers.METHOD === 'OPTIONS') { + client.send(buildZmqResponse(zmqMsg.uuid, zmqMsg.connId, status.NO_CONTENT_204, header_cors, '')); + } else if (!zmqMsg.valid_json) { + client.send(buildZmqResponse(zmqMsg.uuid, zmqMsg.connId, status.BAD_REQUEST_400, standard_headers.json, {'message':'Invalid JSON'})); + } else { + delete zmqMsg['valid_json']; + return callbackWithFilter(zmqMsg); + } + } + }); + } else { receive.on('message', function (envelope) { - var zmqMsg = envelope.toString('utf8'); - return callback(JSON.parse(zmqMsg)); + var zmqMsg = envelope.toString('utf8'); + return callback(JSON.parse(zmqMsg)); }); - } + } - if (config.bind) { - receive.bindSync(config.spec); - } else { - receive.connect(config.spec); - } + if (config.bind) { + receive.bindSync(config.spec); + } else { + receive.connect(config.spec); + } - if ('sub' === config.type){ - receive.subscribe(config.subscribe); - } + if ('sub' === config.type){ + receive.subscribe(config.subscribe); + } }; var setMongrel2UploadDir = function(dir) { - binaryutils.setMongrel2UploadDir(dir); + binaryutils.setMongrel2UploadDir(dir); };