node/test/pummel/test-string-decoder-large-buffer.js
Luigi Pinca 3d608bbe8b
test: skip the test if the buffer allocation fails
Use the error message as another condition to skip the test when the
buffer allocation fails.

Refs: 795dd8eb79
Refs: e9c6004a2d
PR-URL: https://github.com/nodejs/node/pull/58738
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2025-06-20 06:12:31 +00:00

36 lines
869 B
JavaScript

'use strict';
const common = require('../common');
// Buffer with size > INT32_MAX
common.skipIf32Bits();
const assert = require('assert');
const { StringDecoder } = require('node:string_decoder');
const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;
const size = 2 ** 31;
const stringTooLongError = {
message: `Cannot create a string longer than 0x${kStringMaxLength.toString(16)}` +
' characters',
code: 'ERR_STRING_TOO_LONG',
name: 'Error',
};
let largeBuffer;
try {
largeBuffer = Buffer.allocUnsafe(size);
} catch (e) {
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for Buffer.allocUnsafe');
}
throw e;
}
const decoder = new StringDecoder('utf8');
assert.throws(() => decoder.write(largeBuffer), stringTooLongError);