• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5const buffer = require('buffer');
6const SlowBuffer = buffer.SlowBuffer;
7
8const ones = [1, 1, 1, 1];
9
10// Should create a Buffer
11let sb = SlowBuffer(4);
12assert(sb instanceof Buffer);
13assert.strictEqual(sb.length, 4);
14sb.fill(1);
15for (const [key, value] of sb.entries()) {
16  assert.deepStrictEqual(value, ones[key]);
17}
18
19// underlying ArrayBuffer should have the same length
20assert.strictEqual(sb.buffer.byteLength, 4);
21
22// Should work without new
23sb = SlowBuffer(4);
24assert(sb instanceof Buffer);
25assert.strictEqual(sb.length, 4);
26sb.fill(1);
27for (const [key, value] of sb.entries()) {
28  assert.deepStrictEqual(value, ones[key]);
29}
30
31// Should work with edge cases
32assert.strictEqual(SlowBuffer(0).length, 0);
33try {
34  assert.strictEqual(
35    SlowBuffer(buffer.kMaxLength).length, buffer.kMaxLength);
36} catch (e) {
37  // Don't match on message as it is from the JavaScript engine. V8 and
38  // ChakraCore provide different messages.
39  assert.strictEqual(e.name, 'RangeError');
40}
41
42// Should throw with invalid length type
43const bufferInvalidTypeMsg = {
44  code: 'ERR_INVALID_ARG_TYPE',
45  name: 'TypeError',
46  message: /^The "size" argument must be of type number/,
47};
48assert.throws(() => SlowBuffer(), bufferInvalidTypeMsg);
49assert.throws(() => SlowBuffer({}), bufferInvalidTypeMsg);
50assert.throws(() => SlowBuffer('6'), bufferInvalidTypeMsg);
51assert.throws(() => SlowBuffer(true), bufferInvalidTypeMsg);
52
53// Should throw with invalid length value
54const bufferMaxSizeMsg = {
55  code: 'ERR_INVALID_OPT_VALUE',
56  name: 'RangeError',
57  message: /^The value "[^"]*" is invalid for option "size"$/
58};
59assert.throws(() => SlowBuffer(NaN), bufferMaxSizeMsg);
60assert.throws(() => SlowBuffer(Infinity), bufferMaxSizeMsg);
61assert.throws(() => SlowBuffer(-1), bufferMaxSizeMsg);
62assert.throws(() => SlowBuffer(buffer.kMaxLength + 1), bufferMaxSizeMsg);
63