Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/rocketchat-integrations/server/api/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* globals Meteor Restivus logger processWebhookMessage*/
// TODO: remove globals

import Fiber from 'fibers';
import Future from 'fibers/future';
import _ from 'underscore';
import s from 'underscore.string';
import vm from 'vm';
Expand Down Expand Up @@ -60,10 +62,15 @@ const Api = new Restivus({
const compiledScripts = {};
function buildSandbox(store = {}) {
const sandbox = {
scriptTimeout(reject) {
return setTimeout(() => reject('timed out'), 3000);
},
_,
s,
console,
moment,
Fiber,
Promise,
Livechat: RocketChat.Livechat,
Store: {
set(key, val) {
Expand Down Expand Up @@ -236,9 +243,20 @@ function executeIntegrationRest() {
sandbox.script = script;
sandbox.request = request;

const result = vm.runInNewContext('script.process_incoming_request({ request: request })', sandbox, {
const result = Future.fromPromise(vm.runInNewContext(`
new Promise((resolve, reject) => {
Copy link
Member

@sampaiodiego sampaiodiego Jun 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, add a catch to this Promise, otherwise calling reject will thrown a unhandled promise rejection

edit: I've added 👍

Fiber(() => {
scriptTimeout(reject);
try {
resolve(script.process_incoming_request({ request: request }));
} catch(e) {
reject(e);
}
}).run();
}).catch((error) => { throw new Error(error); });
`, sandbox, {
timeout: 3000
});
})).wait();

if (!result) {
logger.incoming.debug('[Process Incoming Request result of Trigger', this.integration.name, ':] No data');
Expand Down