mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
Throw an exception instead of crashing when attempting to create `Buffer` objects from a Context that is not associated with a Node.js `Environment`. Possible alternatives for the future might be just returning a plain `Uint8Array`, or working on providing `Buffer` for all `Context`s. PR-URL: https://github.com/nodejs/node/pull/23938 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
23 lines
729 B
JavaScript
23 lines
729 B
JavaScript
'use strict';
|
||
|
||
const common = require('../../common');
|
||
const assert = require('assert');
|
||
const {
|
||
makeBufferInNewContext
|
||
} = require(`./build/${common.buildType}/binding`);
|
||
|
||
// Because the `Buffer` function and its protoype property only (currently)
|
||
// exist in a Node.js instance’s main context, trying to create buffers from
|
||
// another context throws an exception.
|
||
|
||
try {
|
||
makeBufferInNewContext();
|
||
} catch (exception) {
|
||
assert.strictEqual(exception.constructor.name, 'Error');
|
||
assert(!(exception.constructor instanceof Error));
|
||
|
||
assert.strictEqual(exception.code, 'ERR_BUFFER_CONTEXT_NOT_AVAILABLE');
|
||
assert.strictEqual(exception.message,
|
||
'Buffer is not available for the current Context');
|
||
}
|