Skip to content

Buffer is not defined #31

Description

@jhorbulyk

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions