THRIFT-2397 Add CORS and CSP support for JavaScript and Node.js libraries
Patch: Randy Abernethy
diff --git a/lib/js/src/thrift.js b/lib/js/src/thrift.js
index 411eead..8fc7cd2 100644
--- a/lib/js/src/thrift.js
+++ b/lib/js/src/thrift.js
@@ -286,11 +286,11 @@
  * @example
  *     var transport = new Thrift.TXHRTransport("http://localhost:8585");
  */
-Thrift.Transport = Thrift.TXHRTransport = function(url) {
+Thrift.Transport = Thrift.TXHRTransport = function(url, options) {
     this.url = url;
     this.wpos = 0;
     this.rpos = 0;
-
+    this.useCORS = (options && options.useCORS);
     this.send_buf = '';
     this.recv_buf = '';
 };
@@ -683,7 +683,7 @@
  * @example
  *     var protocol  = new Thrift.Protocol(transport);
  */
-Thrift.Protocol = function(transport) {
+Thrift.TJSONProtocol = Thrift.Protocol = function(transport) {
     this.transport = transport;
 };
 
@@ -977,16 +977,8 @@
                 var ch = str.charAt(i);      // a single double quote: "
                 if (ch === '\"') {
                     escapedString += '\\\"'; // write out as: \"
-                } else if (ch === '\\') {    // a single backslash: \
-                    escapedString += '\\\\'; // write out as: \\
-                /* Currently escaped forward slashes break TJSONProtocol.
-                 * As it stands, we can simply pass forward slashes into
-                 * our strings across the wire without being escaped.
-                 * I think this is the protocol's bug, not thrift.js
-                 * } else if(ch === '/') {   // a single forward slash: /
-                 *  escapedString += '\\/';  // write out as \/
-                 * }
-                 */
+                } else if (ch === '\\') {    // a single backslash
+                    escapedString += '\\\\'; // write out as double backslash 
                 } else if (ch === '\b') {    // a single backspace: invisible
                     escapedString += '\\b';  // write out as: \b"
                 } else if (ch === '\f') {    // a single formfeed: invisible
@@ -1025,7 +1017,9 @@
         this.rstack = [];
         this.rpos = [];
 
-        if (typeof jQuery !== 'undefined') {
+        if (typeof JSON !== 'undefined' && typeof JSON.parse === 'function') {
+            this.robj = JSON.parse(this.transport.readAll());
+        } else if (typeof jQuery !== 'undefined') {
             this.robj = jQuery.parseJSON(this.transport.readAll());
         } else {
             this.robj = eval(this.transport.readAll());