/
handler.js
71 lines (60 loc) · 1.79 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const sourceMap = require("source-map");
const fs = require("fs");
const mkdirp = require("mkdirp");
const getDirName = require("path").dirname;
const fetch = require("node-fetch");
const { zip } = require("zip-a-folder");
const getRawSourceMap = url =>
fetch(url)
.then(res => res.json())
.then(json => json);
async function writeFolderAndFile(path, content) {
await mkdirp(getDirName("/tmp/generated/" + path));
fs.writeFileSync("/tmp/generated/" + path, content);
}
async function writeSourceToFile(consumer) {
console.log("writeSourceToFile");
return Promise.all(
consumer.sources.map(async (source, index) => {
await writeFolderAndFile(source, consumer.sourcesContent[index]);
})
);
}
async function generateSource(url) {
console.log("generateSource");
const parsedMap = await getRawSourceMap(url);
await sourceMap.SourceMapConsumer.with(
parsedMap,
null,
writeSourceToFile
).catch(error => handleError(error));
}
function handleError(message) {
console.log(message);
return {
body: message || "An error occurred",
statusCode: 500
};
}
module.exports.generate = async event => {
console.log(event);
if (event && event.queryStringParameters && event.queryStringParameters.url) {
const url = event.queryStringParameters.url;
await generateSource(url)
.then(async () => {
await zip("/tmp/generated", "/tmp/archive.zip");
})
.catch(e => handleError(e));
return {
headers: {
"Content-Type": "application/zip, application/octet-stream",
"Content-disposition": "attachment; filename=archive.zip"
},
body: fs.readFileSync("/tmp/archive.zip").toString("base64"),
isBase64Encoded: true,
statusCode: 200
};
} else {
return handleError("A url is required");
}
};