elasticio / code-component

The holy grail of the elastic.io platform
Apache License 2.0
2 stars 12 forks source link

Buffer is not defined #31

Open jhorbulyk opened 4 years ago

jhorbulyk commented 4 years ago

Consider the following code:

// Please note only Node.js code is supported here
async function run(msg, cfg, snapshot) {
    console.log('Hello code, incoming message is msg=%j', msg);
    let s = "abc";
    let buff = Buffer.from(s);
    let base64data = buff.toString('base64');
    // Create message to be emitted
    var data = messages.newMessageWithBody({message: base64data});
    // Emit the data event
    emitter.emit('data', data);
    // No need to emit end
    console.log('Finished execution');
}

One would expect it to produce a message {message: 'YWJj'}. However, instead it produces

ReferenceError: Buffer is not defined
    at evalmachine.<anonymous>:8:12
    at ContextifyScript.Script.runInContext (vm.js:32:29)
    at Object.runInContext (vm.js:87:6)

because we forgot to bring Buffer and some other Node globals into the context in which the code is executed.

kirill-levitskiy commented 4 years ago

I used next snippet for solving the issue:

async function run(msg, cfg, snapshot) {
    const { user, password } = cfg;
    const buf = require('buffer').Buffer;
    const auth = buf(`${user}:${password}`).toString('base64');
    this.logger.info(auth);
}