-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocknode.js
31 lines (30 loc) · 1.28 KB
/
mocknode.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
//rugged interface for vanilla js to nodejs modules
const require = (() => {
let nodejs = {};
nodejs.modules = Object.create(null);
nodejs.version = 16;
function main(module) {
//console.log(nodejs.modules)
if (!(module in nodejs.modules)) throw new Error(`Module '${module}' does not exist, check if you misspelled `)
return nodejs.modules[module];
};
Object.defineProperty(main, 'addMod', {
value: (name, val) => {
if (!name) {
//if name is set to false,or undefined, protocols are established that many modules need to be uploaded to this "database"
Object.entries(val).forEach((entry) => {
let [modn, cont] = entry;
if (modn in nodejs.modules) throw new Error(`Module '${modn}' already exists.`)
nodejs.modules[modn] = cont;
//console.log(`Sucessfully added '${modn}'`)
});
} else {
if (name in nodejs.modules) throw new Error(`Module '${modn}' already exists.`)
nodejs.modules[name] = val//should be object
//console.log(`Sucessfully added '${name}'`)
}
}
})
return main
})()
window.require = require;