• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22// Flags: --expose-internals
23'use strict';
24require('../common');
25const assert = require('assert');
26const fromList = require('_stream_readable')._fromList;
27const BufferList = require('internal/streams/buffer_list');
28const util = require('util');
29
30function bufferListFromArray(arr) {
31  const bl = new BufferList();
32  for (let i = 0; i < arr.length; ++i)
33    bl.push(arr[i]);
34  return bl;
35}
36
37{
38  // Verify behavior with buffers
39  let list = [ Buffer.from('foog'),
40               Buffer.from('bark'),
41               Buffer.from('bazy'),
42               Buffer.from('kuel') ];
43  list = bufferListFromArray(list);
44
45  assert.strictEqual(
46    util.inspect([ list ], { compact: false }),
47    `[
48  BufferList {
49    head: [Object],
50    tail: [Object],
51    length: 4
52  }
53]`);
54
55  // Read more than the first element.
56  let ret = fromList(6, { buffer: list, length: 16 });
57  assert.strictEqual(ret.toString(), 'foogba');
58
59  // Read exactly the first element.
60  ret = fromList(2, { buffer: list, length: 10 });
61  assert.strictEqual(ret.toString(), 'rk');
62
63  // Read less than the first element.
64  ret = fromList(2, { buffer: list, length: 8 });
65  assert.strictEqual(ret.toString(), 'ba');
66
67  // Read more than we have.
68  ret = fromList(100, { buffer: list, length: 6 });
69  assert.strictEqual(ret.toString(), 'zykuel');
70
71  // all consumed.
72  assert.deepStrictEqual(list, new BufferList());
73}
74
75{
76  // Verify behavior with strings
77  let list = [ 'foog',
78               'bark',
79               'bazy',
80               'kuel' ];
81  list = bufferListFromArray(list);
82
83  // Read more than the first element.
84  let ret = fromList(6, { buffer: list, length: 16, decoder: true });
85  assert.strictEqual(ret, 'foogba');
86
87  // Read exactly the first element.
88  ret = fromList(2, { buffer: list, length: 10, decoder: true });
89  assert.strictEqual(ret, 'rk');
90
91  // Read less than the first element.
92  ret = fromList(2, { buffer: list, length: 8, decoder: true });
93  assert.strictEqual(ret, 'ba');
94
95  // Read more than we have.
96  ret = fromList(100, { buffer: list, length: 6, decoder: true });
97  assert.strictEqual(ret, 'zykuel');
98
99  // all consumed.
100  assert.deepStrictEqual(list, new BufferList());
101}
102