forked from airbnb/hypernova
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createVM.js
39 lines (29 loc) · 1.08 KB
/
createVM.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
import lruCache from 'lru-cache';
import crypto from 'crypto';
import Module from './Module';
function defaultGetKey(name, code) {
const hash = crypto.createHash('sha1').update(code).digest('hex');
return `${name}::${hash}`;
}
export default (options = {}) => {
// This is to cache the entry point of all bundles which makes running on a vm blazing fast.
// Everyone gets their own sandbox to play with and nothing is leaked between requests.
// We're caching with `code` as the key to ensure that if the code changes we break the cache.
const exportsCache = lruCache({
max: options.cacheSize,
});
const getKey = options.getKey || defaultGetKey;
return {
exportsCache,
run(name, code) {
const key = getKey(name, code);
if (exportsCache.has(key)) return exportsCache.get(key);
const environment = options.environment && options.environment(name);
const module = new Module(name, environment);
module.load(name);
module._compile(code, name);
exportsCache.set(key, module.exports);
return module.exports;
},
};
};