forked from Kishanjay/LacunaV2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lacuna_lazyloader.js
56 lines (48 loc) · 1.51 KB
/
lacuna_lazyloader.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
/**
* Everything related to lazy loading
*/
const fs = require("fs"),
path = require("path")
const settings = require("./_settings");
module.exports = class LazyLoader {
constructor() {
this.storage = {};
}
/**
* Adds a function to the lazyload storage database
*/
add(functionData, functionBody) {
var id = settings.functionDataToId(functionData);
this.storage[id] = functionBody;
}
/**
* Fetches the lazy load replacement for a function
*/
getLazyLoadReplacement(functionData) {
var id = settings.functionDataToId(functionData);
return `lacuna_lazy_load("${id}", functionData => eval(functionData))`;
}
/**
* Function that actually does the http request
*/
getLazyLoadFrame() {
return `// LACUNA LAZY LOAD FALLBACK
function lacuna_lazy_load(id, callback){
fetch("http://127.0.0.1:${settings.LAZY_LOAD_SERVER_PORT}/lazyload/", {
method: "POST",
headers: { "Accept": "application/json", "Content-Type": "application/json" },
body: JSON.stringify({id})
}).then(response => {
return response.text();
}).then(callback);
}\n`
}
/**
* Exports a server that is capable of retrieving all functions
* present in the storage
*/
exportStorage(destinationFolder) {
console.log("exporting serveR)");
fs.writeFileSync(path.join(destinationFolder, "lacuna_lazyload_storage.json"), JSON.stringify(this.storage, null, 4), 'utf8');
}
}