Created
March 26, 2019 20:58
-
-
Save edewit/25c726e6572db865f8e90676acb5f2ce to your computer and use it in GitHub Desktop.
This file contains 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
var svgToMiniDataURI = require("mini-svg-data-uri"); | |
var http = require('http'); | |
var https = require('https'); | |
const url = process.argv[2]; | |
var client = http; | |
if (url.startsWith("https")){ | |
client = https; | |
} | |
client.get(url, (res) => { | |
const { statusCode } = res; | |
const contentType = res.headers['content-type']; | |
let error; | |
if (statusCode !== 200) { | |
error = new Error('Request Failed.\n' + | |
`Status Code: ${statusCode}`); | |
} | |
if (error) { | |
console.error(error.message); | |
// consume response data to free up memory | |
res.resume(); | |
return; | |
} | |
res.setEncoding('utf8'); | |
let rawData = ''; | |
res.on('data', (chunk) => { rawData += chunk; }); | |
res.on('end', () => { | |
try { | |
var optimizedSVGDataURI = svgToMiniDataURI(rawData); | |
console.log(optimizedSVGDataURI); | |
} catch (e) { | |
console.error(e.message); | |
} | |
}); | |
}).on('error', (e) => { | |
console.error(`Got error: ${e.message}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment