Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.