Created
January 20, 2017 22:28
-
-
Save HyphnKnight/ff5cde0801107d8f3414ca4b129de63d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const | |
| querystring = require('querystring'), | |
| http = require('http'), | |
| /** | |
| * Converts options and buffer arguments into a proper data object. | |
| * @param {!Object} options Google Closure Compiler options | |
| * @return {function(!string): Object} Function that assembles data based on specific buffer | |
| */ | |
| assembleData = options => buffer => querystring.stringify({ | |
| 'js_code' : buffer, | |
| 'compilation_level' : !!options.compilationLevel ? options.compilationLevel : 'ADVANCED_OPTIMIZATIONS', | |
| 'output_format' : !!options.outputFormat ? options.outputFormat : 'json', | |
| 'output_info' : !!options.outputInfo ? options.outputInfo : ['compiled_code','warnings','errors','statistics'], | |
| 'warning_level' : !!options.warningLevel ? options.warningLevel : 'VERBOSE', | |
| 'language' : 'ECMASCRIPT6_STRICT' | |
| }), | |
| /** | |
| * Converts compiler data into html request options | |
| * @param {!Object} | |
| * @return {Object} | |
| */ | |
| assembleRequestOptions = assembledData => ({ | |
| host: 'closure-compiler.appspot.com', | |
| port: '80', | |
| path: '/compile', | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| 'Content-Length': Buffer.byteLength(assembledData) | |
| } | |
| }); | |
| /** | |
| * Google Closure Compiler | |
| * @param {string} buffer | |
| * @param {!Object} options | |
| * @return {promise} | |
| */ | |
| module.exports = ( buffer , options ) => new Promise( ( resolve , reject ) => { | |
| let result = ''; | |
| const | |
| assembledData = assembleData(options)(buffer), | |
| request = http.request( assembleRequestOptions(assembledData) , response => { | |
| response.setEncoding('utf8'); | |
| response.on('data' , data => { | |
| result += data; | |
| } ); | |
| response.on('end', () => resolve(JSON.parse(result)) ); | |
| } ); | |
| request.on('error', error => reject(error) ); | |
| request.write(assembledData); | |
| request.end(); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment