google/closure-compiler-js

Google Closure Compiler REST API, AJAX, No Compiled Code Returned

tomgallagher opened this issue · 1 comments

OK. Here's a headscratcher.

Setting up the various params for the Closure Compiler, with text as my Javascript string:

var scriptRequestData = {
        'output_format': 'json',
        'output_info': 'compiled_code',
        'output_info': 'warnings',
        'output_info': 'errors',
        'output_info': 'statistics',
        'compilation_level': 'WHITESPACE_ONLY',
        'language': 'ECMASCRIPT6',
        'warning_level': 'default',
        'js_code': text
    };

Here's the settings object for the AJAX call:

var RXpostSettingsObject = {
        method:'POST',
        url: "https://closure-compiler.appspot.com/compile",
        timeout: 500,
        headers: {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"},
        responseType: 'json',
        body: $.param( scriptRequestData )
    };

The body yields this on check:

output_format=json&output_info=statistics&compilation_level=WHITESPACE_ONLY&language=ECMASCRIPT6&warning_level=default&js_code=document.write('%20%26middot%3B%20%3Ca%20href%3D%22'%2B'mai'%2B'l_to%3Apet_er_%40mrc_oles.c_om'.split('_').join('')%2B'%22%3Econtact%3C%2Fa%3E')

All good so far. So I send the request in using Rx wrapper for Ajax (I've also tried plain Ajax):

Rx.Observable.ajax(RXpostSettingsObject).retry(3)
        .subscribe(
            function(data) {
                console.log(data.response);
            },
            function(errors) {
                console.log(errors); 
            },
            function(complete) {
                console.log("Closure Compiler Script Check Complete"); }
        });

And it clearly works, as I get this back:

statistics: {compileTime:0, compressedGzipSize:117, compressedSize:109,
originalGzipSize:115, originalSize:108 }

But I get no compiled code, which should be returned in the JSON response as compiledCode. Nothing there. I've run the exact same query on Postman and I get the following response:

{"compiledCode":"document.write(' \u0026middot; \u003ca href\u003d\"'+\"mai\"+\"l_to:pet_er_@mrc_oles.c_om\".split(\"_\").join(\"\")+'\"\u003econtact\u003c/a\u003e');","statistics":{"originalSize":108,"originalGzipSize":115,"compressedSize":109,"compressedGzipSize":117,"compileTime":0}}

So, my question is, WTF? My only thought is that AJAX is somehow filtering out the top level of JSON data as it is at the root. Anyone with any ideas on what's going wrong?

Any help much appreciated.

So just to update if anyone's reading this: if I remove all the output info options apart from the compiled code, I get the compiled code back. So it seems that when you add errors and statistics in, the format of the JSON response cannot be read by ajax, so it just does its best and returns the statistics. Indeed, when I get the JSON response from Postman and try to parse it using JSON.parse, I get Uncaught SyntaxError: Unexpected token o in JSON at position 1