-
Notifications
You must be signed in to change notification settings - Fork 3
/
sandbox.js
83 lines (75 loc) · 2.14 KB
/
sandbox.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
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
const fs = require('fs')
const EventEmitter = require('events').EventEmitter
const { VM, VMScript } = require('vm2');
const git = require('isomorphic-git')
const { pgp } = require('@isomorphic-git/pgp-plugin')
const { lookup, demote } = require('./lookup.js')
const curry = ({ core, dir, gitdir }) => fn => argObject => fn({ ...argObject, core, dir, gitdir })
const sandbox = ({ name, core, dir, gitdir, res, oids, updates, script }) => {
let ee = new EventEmitter()
plugincore = git.cores.create(core)
plugincore.set('emitter', ee)
plugincore.set('fs', fs)
plugincore.set('pgp', pgp)
const $ = curry({ core, dir, gitdir })
const $git = {
E: { ...git.E },
eventEmitter: ee,
expandOid: $(git.expandOid),
expandRef: $(git.expandRef),
findMergeBase: $(git.findMergeBase),
getRemoteInfo: $(git.getRemoteInfo),
hashBlob: $(git.hashBlob),
isDescendent: $(git.isDescendent),
listBranches: $(git.listBranches),
listFiles: $(git.listFiles),
listRemotes: $(git.listRemotes),
listTags: $(git.listTags),
log: $(git.log),
readObject: $(git.readObject),
resolveRef: $(git.resolveRef),
serveReceivePack: $(git.serveReceivePack),
verify: $(git.verify),
walkBeta2: $(git.walkBeta2),
}
const $res = {
write: res.write.bind(res)
}
return new Promise((resolve, reject) => {
const $console = {
log: async (...args) => {
res.write(await git.serveReceivePack({ type: 'print', message: args.join() }))
},
error: (...args) => {
reject(new Error(args.join()))
}
}
const vm = new VM({
timeout: 10000,
eval: false,
wasm: false,
sandbox: {
updates,
oids,
git: $git,
pgp: { lookup, demote },
done: (err) => err ? reject(err) : resolve(),
console: $console,
}
});
try {
script = new VMScript(script, name).compile();
} catch (err) {
reject(err);
}
try {
vm.run(script);
} catch (e) {
reject(e);
}
})
}
module.exports = {
sandbox
}
// console.log(runVM({ core: 'default', dir: '', gitdir: '', script: `String(Object.keys(git))` }))