Skip to content

Commit 3aced89

Browse files
committed
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: #13465
1 parent eef94a8 commit 3aced89

File tree

3 files changed

+85
-12
lines changed

3 files changed

+85
-12
lines changed

doc/api/buffer.md

+49-12
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ changes:
447447
* `size` {integer} The desired length of the new `Buffer`
448448

449449
Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
450-
[`buffer.kMaxLength`] or smaller than 0, a [`RangeError`] will be thrown.
451-
A zero-length `Buffer` will be created if `size` is 0.
450+
[`buffer.constants.MAX_LENGTH`] or smaller than 0, a [`RangeError`] will be
451+
thrown. A zero-length `Buffer` will be created if `size` is 0.
452452

453453
Prior to Node.js 8.0.0, the underlying memory for `Buffer` instances
454454
created in this way is *not initialized*. The contents of a newly created
@@ -528,8 +528,8 @@ console.log(buf);
528528
```
529529

530530
Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
531-
[`buffer.kMaxLength`] or smaller than 0, a [`RangeError`] will be thrown.
532-
A zero-length `Buffer` will be created if `size` is 0.
531+
[`buffer.constants.MAX_LENGTH`] or smaller than 0, a [`RangeError`] will be
532+
thrown. A zero-length `Buffer` will be created if `size` is 0.
533533

534534
If `fill` is specified, the allocated `Buffer` will be initialized by calling
535535
[`buf.fill(fill)`][`buf.fill()`].
@@ -573,8 +573,8 @@ changes:
573573
* `size` {integer} The desired length of the new `Buffer`
574574

575575
Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
576-
[`buffer.kMaxLength`] or smaller than 0, a [`RangeError`] will be thrown.
577-
A zero-length `Buffer` will be created if `size` is 0.
576+
[`buffer.constants.MAX_LENGTH`] or smaller than 0, a [`RangeError`] will be
577+
thrown. A zero-length `Buffer` will be created if `size` is 0.
578578

579579
The underlying memory for `Buffer` instances created in this way is *not
580580
initialized*. The contents of the newly created `Buffer` are unknown and
@@ -619,8 +619,8 @@ added: v5.10.0
619619
* `size` {integer} The desired length of the new `Buffer`
620620

621621
Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
622-
[`buffer.kMaxLength`] or smaller than 0, a [`RangeError`] will be thrown.
623-
A zero-length `Buffer` will be created if `size` is 0.
622+
[`buffer.constants.MAX_LENGTH`] or smaller than 0, a [`RangeError`] will be
623+
thrown. A zero-length `Buffer` will be created if `size` is 0.
624624

625625
The underlying memory for `Buffer` instances created in this way is *not
626626
initialized*. The contents of the newly created `Buffer` are unknown and
@@ -2050,6 +2050,9 @@ added: v0.1.90
20502050
Decodes `buf` to a string according to the specified character encoding in
20512051
`encoding`. `start` and `end` may be passed to decode only a subset of `buf`.
20522052

2053+
The maximum length of a string instance (in UTF-16 code units) is available
2054+
as [`buffer.constants.MAX_STRING_LENGTH`][].
2055+
20532056
Examples:
20542057

20552058
```js
@@ -2507,8 +2510,7 @@ added: v3.0.0
25072510

25082511
* {integer} The largest size allowed for a single `Buffer` instance
25092512

2510-
On 32-bit architectures, this value is `(2^30)-1` (~1GB).
2511-
On 64-bit architectures, this value is `(2^31)-1` (~2GB).
2513+
An alias for [`buffer.constants.MAX_LENGTH`][]
25122514

25132515
Note that this is a property on the `buffer` module returned by
25142516
`require('buffer')`, not on the `Buffer` global or a `Buffer` instance.
@@ -2599,8 +2601,8 @@ deprecated: v6.0.0
25992601
* `size` {integer} The desired length of the new `SlowBuffer`
26002602

26012603
Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
2602-
[`buffer.kMaxLength`] or smaller than 0, a [`RangeError`] will be thrown.
2603-
A zero-length `Buffer` will be created if `size` is 0.
2604+
[`buffer.constants.MAX_LENGTH`] or smaller than 0, a [`RangeError`] will be
2605+
thrown. A zero-length `Buffer` will be created if `size` is 0.
26042606

26052607
The underlying memory for `SlowBuffer` instances is *not initialized*. The
26062608
contents of a newly created `SlowBuffer` are unknown and may contain
@@ -2622,6 +2624,39 @@ buf.fill(0);
26222624
console.log(buf);
26232625
```
26242626

2627+
2628+
## Buffer Constants
2629+
<!-- YAML
2630+
added: REPLACEME
2631+
-->
2632+
2633+
Note that `buffer.constants` is a property on the `buffer` module returned by
2634+
`require('buffer')`, not on the `Buffer` global or a `Buffer` instance.
2635+
2636+
### buffer.constants.MAX_LENGTH
2637+
<!-- YAML
2638+
added: REPLACEME
2639+
-->
2640+
2641+
* {integer} The largest size allowed for a single `Buffer` instance
2642+
2643+
On 32-bit architectures, this value is `(2^30)-1` (~1GB).
2644+
On 64-bit architectures, this value is `(2^31)-1` (~2GB).
2645+
2646+
This value is also available as [`buffer.kMaxLength`][].
2647+
2648+
### buffer.constants.MAX_STRING_LENGTH
2649+
<!-- YAML
2650+
added: REPLACEME
2651+
-->
2652+
2653+
* {integer} The largest length allowed for a single `string` instance
2654+
2655+
Represents the largest `length` that a `string` primitive can have, counted
2656+
in UTF-16 code units.
2657+
2658+
This value may depend on the JS engine that is being used.
2659+
26252660
[`ArrayBuffer#slice()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice
26262661
[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
26272662
[`Buffer.alloc()`]: #buffer_class_method_buffer_alloc_size_fill_encoding
@@ -2652,6 +2687,8 @@ console.log(buf);
26522687
[`buf.slice()`]: #buffer_buf_slice_start_end
26532688
[`buf.values()`]: #buffer_buf_values
26542689
[`buffer.kMaxLength`]: #buffer_buffer_kmaxlength
2690+
[`buffer.constants.MAX_LENGTH`]: #buffer_buffer_constants_max_length
2691+
[`buffer.constants.MAX_STRING_LENGTH`]: #buffer_buffer_constants_max_string_length
26552692
[`util.inspect()`]: util.html#util_util_inspect_object_options
26562693
[RFC1345]: https://tools.ietf.org/html/rfc1345
26572694
[RFC4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5

lib/buffer.js

+23
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,31 @@ Buffer.prototype = FastBuffer.prototype;
4242
exports.Buffer = Buffer;
4343
exports.SlowBuffer = SlowBuffer;
4444
exports.INSPECT_MAX_BYTES = 50;
45+
46+
// Legacy.
4547
exports.kMaxLength = binding.kMaxLength;
4648

49+
const constants = Object.defineProperties({}, {
50+
MAX_LENGTH: {
51+
value: binding.kStringMaxLength,
52+
writable: false,
53+
enumerable: true
54+
},
55+
MAX_STRING_LENGTH: {
56+
value: binding.kStringMaxLength,
57+
writable: false,
58+
enumerable: true
59+
}
60+
});
61+
62+
Object.defineProperty(exports, 'constants', {
63+
configurable: false,
64+
enumerable: true,
65+
value: constants
66+
});
67+
68+
exports.kStringMaxLength = binding.kStringMaxLength;
69+
4770
const kFromErrorMsg = 'First argument must be a string, Buffer, ' +
4871
'ArrayBuffer, Array, or array-like object.';
4972

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
const { MAX_LENGTH, MAX_STRING_LENGTH } = require('buffer').constants;
6+
7+
assert.strictEqual(typeof MAX_LENGTH, 'number');
8+
assert.strictEqual(typeof MAX_STRING_LENGTH, 'number');
9+
assert(MAX_STRING_LENGTH <= MAX_LENGTH);
10+
assert.throws(() => ' '.repeat(MAX_STRING_LENGTH + 1),
11+
/^RangeError: Invalid string length$/);
12+
13+
assert.doesNotThrow(() => ' '.repeat(MAX_STRING_LENGTH));

0 commit comments

Comments
 (0)