recime-message-responder
Advanced tools
Comparing version 1.0.6 to 1.0.7
export declare class CodeBlock { | ||
private code; | ||
constructor(code: any); | ||
private message; | ||
constructor(message: any); | ||
execute(data: any): Promise<{}>; | ||
processMessage(message: any): any; | ||
transformCode(code: any): any; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const vm = require("vm"); | ||
const HandleBars = require("handlebars"); | ||
const recime_bot_extension_1 = require("recime-bot-extension"); | ||
@@ -10,6 +12,4 @@ const __ = recime_bot_extension_1.default.default; | ||
class CodeBlock { | ||
constructor(code) { | ||
this.code = babel.transform(code, { | ||
"presets": [presetEnv] | ||
}).code; | ||
constructor(message) { | ||
this.message = message; | ||
} | ||
@@ -48,3 +48,10 @@ execute(data) { | ||
local.run(() => { | ||
const handler = vm.runInNewContext(this.code, sandbox); | ||
const config = this.message.content.config; | ||
if (config) { | ||
const str = JSON.stringify(config); | ||
const template = HandleBars.compile(str); | ||
data.config = JSON.parse(template(data.vars.vals)); | ||
} | ||
const message = this.processMessage(this.message); | ||
const handler = vm.runInNewContext(message, sandbox); | ||
handler(data, (result) => { | ||
@@ -69,3 +76,22 @@ if (Array.isArray(result)) { | ||
} | ||
processMessage(message) { | ||
switch (message.type) { | ||
case "json-api": { | ||
console.log(message.content.config); | ||
const filepath = path.join(__dirname, "plugins", `${message.type}.js`); | ||
if (fs.existsSync(filepath)) | ||
return this.transformCode(fs.readFileSync(filepath, 'utf-8')); | ||
throw new Error(`Unsupported message type "${message.type}"!`); | ||
} | ||
default: { | ||
return this.transformCode(message.content.code); | ||
} | ||
} | ||
} | ||
transformCode(code) { | ||
return babel.transform(code, { | ||
"presets": [presetEnv] | ||
}).code; | ||
} | ||
} | ||
exports.CodeBlock = CodeBlock; |
@@ -23,6 +23,7 @@ "use strict"; | ||
} | ||
case 'code': { | ||
case 'code': | ||
case 'json-api': { | ||
console.time("babel-compile"); | ||
try { | ||
const block = new code_block_1.CodeBlock(message.content.code); | ||
const block = new code_block_1.CodeBlock(message); | ||
return block.execute(options).then((r) => { | ||
@@ -29,0 +30,0 @@ console.timeEnd("babel-compile"); |
{ | ||
"name": "recime-message-responder", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,2 +5,4 @@ import fs = require("fs"); | ||
import HandleBars = require("handlebars"); | ||
import Ext from 'recime-bot-extension'; | ||
@@ -13,10 +15,8 @@ const __ = Ext.default; | ||
export class CodeBlock { | ||
private code: any; | ||
private message: any; | ||
constructor(code){ | ||
this.code = babel.transform(code, { | ||
"presets" : [presetEnv] | ||
}).code; | ||
constructor(message){ | ||
this.message = message; | ||
} | ||
execute(data){ | ||
@@ -58,3 +58,14 @@ return new Promise((resolve, reject)=>{ | ||
local.run (()=>{ | ||
const handler = vm.runInNewContext(this.code, sandbox); | ||
const config = this.message.content.config; | ||
if (config){ | ||
const str = JSON.stringify(config); | ||
const template = HandleBars.compile(str); | ||
data.config = JSON.parse(template(data.vars.vals)); | ||
} | ||
const message = this.processMessage(this.message); | ||
const handler = vm.runInNewContext(message, sandbox); | ||
handler(data, (result)=>{ | ||
@@ -66,3 +77,3 @@ if (Array.isArray(result)){ | ||
r.push(result); | ||
} | ||
} | ||
resolve(r); | ||
@@ -80,2 +91,23 @@ }); | ||
} | ||
processMessage(message){ | ||
switch(message.type){ | ||
case "json-api":{ | ||
console.log(message.content.config); | ||
const filepath = path.join(__dirname, "plugins", `${message.type}.js`); | ||
if (fs.existsSync(filepath)) | ||
return this.transformCode(fs.readFileSync(filepath, 'utf-8')); | ||
throw new Error(`Unsupported message type "${message.type}"!`) | ||
} | ||
default : { | ||
return this.transformCode(message.content.code); | ||
} | ||
} | ||
} | ||
transformCode(code){ | ||
return babel.transform(code, { | ||
"presets" : [presetEnv] | ||
}).code; | ||
} | ||
} |
@@ -33,7 +33,8 @@ import Ext from 'recime-bot-extension'; | ||
} | ||
case 'code':{ | ||
case 'code': | ||
case 'json-api': { | ||
console.time("babel-compile"); | ||
try { | ||
const block = new CodeBlock(message.content.code); | ||
const block = new CodeBlock(message); | ||
@@ -40,0 +41,0 @@ return block.execute(options).then((r) => { |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
20741
21
549
12