-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes6-https-loader.mjs
112 lines (93 loc) · 3.33 KB
/
es6-https-loader.mjs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Modified from https://nodejs.org/api/esm.html#esm_https_loader
// [Experimental] https://github.com/nodejs/node/tree/main/lib/internal/modules/esm
import https from 'https';
import http from 'http';
import readline from 'readline';
import fs from 'fs';
function isMatch(url){
return /^(http:\/\/localhost:|http:\/\/localhost\/|https:\/\/).*?\.(js|mjs)$/m.test(url);
}
// [Experimental] Node.js may have changes for the API
export function resolve(specifier, context, nextResolve) {
const { parentURL = null } = context;
// Normally Node.js would error on specifiers starting with 'https://', so
// this hook intercepts them and converts them into absolute URLs to be
// passed along to the later hooks below.
if(isMatch(specifier))
return { shortCircuit: true, url: specifier };
else if(parentURL && isMatch(parentURL))
return { shortCircuit: true, url: new URL(specifier, parentURL).href };
// Let Node.js handle all other specifiers.
return nextResolve(specifier, context);
}
export function request(url, callback) {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
process.stdout.write(`\x1b[1;32m[Downloading]\x1b[0m ${url}\r`);
let loader = https;
if(url.startsWith('http://')){
if(!isMatch(url)) throw new Error("URL must use https or localhost");
loader = http;
}
loader.get(url, function(response){
if(response.headers.location)
request(response.headers.location, callback);
else callback(response);
})
.on('error', function(msg){
console.error("Get an error when loading", url, ":\n", msg);
});
}
// [Experimental] Node.js may have changes for the API
export function load(url, context, nextLoad) {
// For JavaScript to be loaded over the network, we need to fetch and return it.
if (!url.startsWith('file:') && isMatch(url)) {
let dir = url.replace(/(https|http):\/\//, '')
.replace(/\\/g, '/').replace(/[*"|:?<>]/g, '-');
if(dir.includes('/../')){
console.log(dir);
console.error("/../ currently not allowed");
throw new Error("Can't import module");
}
dir = dir.split('/');
let fileName = dir.pop();
if(dir.includes('')){
console.error("The URL address was invalid");
throw new Error("Can't import module");
}
dir.unshift('.', '.bp_cache', 'modules'); //> ./.bp_cache/modules
dir = dir.join('/');
if(fileName.includes('.') === false)
fileName += '_.js';
else if(fileName === '')
fileName = 'index.js';
return new Promise((resolve, reject) => {
fs.readFile(dir+`/${fileName}`, (err, data) => {
if(err){
return request(url, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk)
.on('error', console.log)
.on('end', () => {
if(!fs.existsSync(dir))
fs.mkdirSync(dir, {recursive: true});
fs.writeFile(dir+`/${fileName}`, data, (err) => {
if(err)
console.log("Error writing cache for", url, 'with message:\n', err);
else{
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
process.stdout.write(' '.repeat(14+url.length)+`\r`);
}
resolve({ format: 'module', shortCircuit: true, source: data });
});
});
});
}
resolve({ format: 'module', shortCircuit: true, source: data });
});
});
}
// Let Node.js handle all other URLs.
return nextLoad(url, context);
}