Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer is not defined #31

Open
jhorbulyk opened this issue Nov 25, 2019 · 1 comment · May be fixed by #33
Open

Buffer is not defined #31

jhorbulyk opened this issue Nov 25, 2019 · 1 comment · May be fixed by #33

Comments

@jhorbulyk
Copy link

jhorbulyk commented Nov 25, 2019

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.

@jhorbulyk jhorbulyk linked a pull request Dec 12, 2019 that will close this issue
@kirill-levitskiy
Copy link
Contributor

kirill-levitskiy commented Feb 12, 2020

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants