-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
35 lines (31 loc) · 1.07 KB
/
index.ts
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
// tslint:disable-next-line:no-reference
/// <reference path="./untyped.d.ts" />
import * as amqp from 'amqplib'
import * as cluster from 'cluster'
import * as deepFreeze from 'deep-freeze'
import * as dotenv from 'dotenv'
import sandRequire = require('sand-require')
import { runInNewContext } from 'vm'
dotenv.config()
const queue = process.env.AMQP_QUEUE as string
const open = amqp.connect(process.env.AMQP_ENDPOINT as string)
open.then((conn) => conn.createChannel()).then((ch) => {
ch.assertQueue(queue).then((ok) => {
ch.consume(queue, (msg) => {
if (msg) {
ch.ack(msg)
try {
const message = JSON.parse(msg.content.toString())
runInNewContext(message.code, {
payload: deepFreeze(message.payload),
console: Object.freeze({ ...console }),
http: sandRequire('http'),
https: sandRequire('https'),
}, { filename: 'sandboxed.js', timeout: 1e5, displayErrors: true })
} catch (err) {
console.warn.apply(console, err)
}
}
})
})
}).catch(console.warn)