Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Created October 25, 2019 20:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswilburn/45e4d41696a3637eece49a9e8546cf26 to your computer and use it in GitHub Desktop.
Save thomaswilburn/45e4d41696a3637eece49a9e8546cf26 to your computer and use it in GitHub Desktop.
source-un-map
var http = require("http");
var https = require("https");
var { SourceMapConsumer } = require("source-map");
var fs = require("fs").promises;
var path = require("path");
var fetch = function(address) {
return new Promise(function(ok, fail) {
var parsed = new URL(address);
var remote = parsed.protocol == "http:" ? http : https;
var { host, pathname, search } = parsed;
var p = remote.get({
host,
path: pathname + search,
headers: {
"User-Agent": "Radio"
}
}, function(proxied) {
if (proxied.statusCode > 300 && proxied.headers.location) {
return fetch(proxied.headers.location, output);
}
var body = "";
proxied.on("data", chunk => body += chunk.toString("utf-8"));
proxied.on("end", () => ok(body));
});
p.on("error", fail);
});
}
var main = async function(input, output = "./output") {
var js = await fetch(input);
var smFile = js.match(/^\/\/# sourceMappingURL\s*=\s*(.+)$/m)[1];
var smURL = new URL(smFile, input);
var map = await fetch(smURL.toString());
var consumer = await new SourceMapConsumer(map);
output = path.resolve(output);
for (var source of consumer.sources) {
if (source.match(/node_modules/)) continue;
var file = path.join(output, source);
var contents = consumer.sourceContentFor(source);
await fs.mkdir(path.dirname(file), { recursive: true });
await fs.writeFile(file, contents);
}
consumer.destroy();
}
var args = process.argv.slice(2);
var [input, output] = args;
main(input, output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment