From ab621b13fd061ac00dc1ebda52667b7983e3a56b Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sun, 4 Jun 2017 00:29:52 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + concatenate-garbage.js | 14 ++++++++++++++ lib/extract-garbage.js | 15 +++++++++++++++ locate-snippets.js | 21 +++++++++++++++++++++ package.json | 18 ++++++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 .gitignore create mode 100644 concatenate-garbage.js create mode 100644 lib/extract-garbage.js create mode 100644 locate-snippets.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/concatenate-garbage.js b/concatenate-garbage.js new file mode 100644 index 0000000..3536b84 --- /dev/null +++ b/concatenate-garbage.js @@ -0,0 +1,14 @@ +'use strict'; + +const bhttp = require("bhttp"); +const Promise = require("bluebird"); + +const extractGarbage = require("./lib/extract-garbage"); + +return Promise.try(() => { + return bhttp.get(process.argv[2]); +}).then((response) => { + let snippets = extractGarbage(response.body.toString()); + + console.log(snippets.map(snippet => snippet.garbage).join("")); +}); diff --git a/lib/extract-garbage.js b/lib/extract-garbage.js new file mode 100644 index 0000000..4d374cc --- /dev/null +++ b/lib/extract-garbage.js @@ -0,0 +1,15 @@ +'use strict'; + +module.exports = function extractGarbage(body) { + let snippetRegex = /(^|[^\n]+\n)([0-9a-f]+)(\s*)($|\n[^\n]+)/g; + + let snippets = []; + let match; + + while (match = snippetRegex.exec(body)) { + snippets.push({before: match[1], garbage: match[2], whitespace: match[3], after: match[4]}); + snippetRegex.lastIndex -= match[4].length; // To allow overlapping matches + } + + return snippets; +}; diff --git a/locate-snippets.js b/locate-snippets.js new file mode 100644 index 0000000..c52b052 --- /dev/null +++ b/locate-snippets.js @@ -0,0 +1,21 @@ +'use strict'; + +const Promise = require("bluebird"); +const bhttp = require("bhttp"); +const chalk = require("chalk"); +const repeatString = require("repeat-string"); + +const extractGarbage = require("./lib/extract-garbage"); + +Promise.map(urls, (url) => { + return Promise.try(() => { + return bhttp.get(process.argv[2]); + }).then((response) => { + let snippets = extractGarbage(response.body.toString()); + + snippets.forEach((snippet) => { + console.log(`${snippet.before}${chalk.red.bold(snippet.garbage)}${repeatString(".", snippet.whitespace.length)}${snippet.after}`); + console.log("---------"); + }); + }); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..968037e --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "garbagechecker", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "Sven Slootweg", + "license": "WTFPL", + "dependencies": { + "bhttp": "^1.2.4", + "bluebird": "^3.5.0", + "chalk": "^1.1.3", + "repeat-string": "^1.6.1" + } +}