-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bun-loader.mjs
125 lines (106 loc) · 3.56 KB
/
bun-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
113
114
115
116
117
118
119
120
121
122
123
124
125
import { plugin } from "bun";
import fs from "node:fs";
import readline from 'readline';
let instanceJS = `
let Blackprint = globalThis.Blackprint;
let allowedDomain = process.env.BLACKPRINT_ALLOWED_MODULE_ORIGIN;
if(!!allowedDomain){
if(allowedDomain.includes('|'))
Blackprint.allowModuleOrigin(allowedDomain.split('|'));
else Blackprint.allowModuleOrigin(allowedDomain);
}
let instance = new Blackprint.Engine();
instance.importJSON(%json_here%);
export default instance;
let {
variables: Variables,
events: Events,
ref: Ports,
} = instance;
export {
instance,
Variables,
Events,
Ports,
};
await instance.ready();
`;
// https://github.com/oven-sh/bun/blob/bfaf095c2ed2ba1f0cd35f1658e2babee8b51985/test/js/bun/plugin/plugins.test.ts#L16-L34
plugin({
name: "HTTPS module loader",
setup(builder) {
builder.onResolve({ namespace: "http", filter: /.*\.(js|mjs)($|\?|#)/m }, ({ path }) => {
return { path, namespace: "url" };
});
builder.onLoad({ namespace: "url", filter: /.*\.(js|mjs)($|\?|#)/m }, async ({ path, namespace }) => {
let dir = path.replace(/(https|http):\/\//, '').replace(/\/\//, '').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:", dir);
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 await new Promise((resolve, reject) => {
fs.readFile(dir+`/${fileName}`, {encoding: 'utf8'}, async (err, data) => {
if(err){
if(!fs.existsSync(dir)) fs.mkdirSync(dir, {recursive: true});
// readline.clearLine(process.stdout, 0);
// readline.cursorTo(process.stdout, 0, null);
// process.stdout.write(`\x1b[1;32m[Downloading]\x1b[0m ${path}\r`);
console.log(`\x1b[1;32m[Downloading]\x1b[0m ${path}`);
let res;
if(path.startsWith('//localhost:') || path.startsWith('//localhost/'))
res = await fetch("http:" + path);
else res = await fetch("https://" + path);
data = await res.text();
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({ contents: data, loader: "js" });
});
}
else resolve({ contents: data, loader: "js" });
});
});
});
},
});
plugin({
name: "Blackprint instance loader",
setup(builder) {
builder.onLoad({ filter: /.*?\.(bpi)($|\?|#)/m }, ({ path }) => {
return new Promise(async resolve => {
function buildInstance(json){
let temp = JSON.parse(json);
if(temp.instance == null) throw new Error("Blackprint instance file is not valid: " + url);
resolve({
loader: "js",
contents: instanceJS.replace('%json_here%', JSON.stringify(json)),
});
}
if(path.includes('//')){
if(path.startsWith('//localhost:') || path.startsWith('//localhost/'))
buildInstance(await fetch("http:" + path));
else buildInstance(await fetch("https://" + path));
}
else buildInstance(await Bun.file(path).text());
});
});
},
});