buffer: add constants object

Add `buffer.constants`, containing length limits for `Buffer` and
`string` instances.

This could be useful for programmers to tell whether a value can
be turned into a string or not.

Ref: https://github.com/nodejs/node/issues/13465
PR-URL: https://github.com/nodejs/node/pull/13467
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Anna Henningsen 2017-06-05 11:58:20 +02:00
parent 50d151578d
commit 1e2905f46a
No known key found for this signature in database
GPG key ID: D8B9F5AEAE84E4CF
3 changed files with 85 additions and 12 deletions

View file

@ -0,0 +1,13 @@
'use strict';
require('../common');
const assert = require('assert');
const { MAX_LENGTH, MAX_STRING_LENGTH } = require('buffer').constants;
assert.strictEqual(typeof MAX_LENGTH, 'number');
assert.strictEqual(typeof MAX_STRING_LENGTH, 'number');
assert(MAX_STRING_LENGTH <= MAX_LENGTH);
assert.throws(() => ' '.repeat(MAX_STRING_LENGTH + 1),
/^RangeError: Invalid string length$/);
assert.doesNotThrow(() => ' '.repeat(MAX_STRING_LENGTH));