• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

websockets / ws / 8308418604

16 Mar 2024 02:24PM UTC coverage: 99.936%. Remained the same
8308418604

push

github

web-flow
[doc] Replace `url.parse()` with `new URL()` (#2208)

1066 of 1071 branches covered (99.53%)

1550 of 1551 relevant lines covered (99.94%)

26498.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/buffer-util.js
1
'use strict';
2

3
const { EMPTY_BUFFER } = require('./constants');
27✔
4

5
const FastBuffer = Buffer[Symbol.species];
27✔
6

7
/**
8
 * Merges an array of buffers into a new buffer.
9
 *
10
 * @param {Buffer[]} list The array of buffers to concat
11
 * @param {Number} totalLength The total length of buffers in the list
12
 * @return {Buffer} The resulting buffer
13
 * @public
14
 */
15
function concat(list, totalLength) {
16
  if (list.length === 0) return EMPTY_BUFFER;
587,790✔
17
  if (list.length === 1) return list[0];
28,701✔
18

19
  const target = Buffer.allocUnsafe(totalLength);
270✔
20
  let offset = 0;
270✔
21

22
  for (let i = 0; i < list.length; i++) {
270✔
23
    const buf = list[i];
621✔
24
    target.set(buf, offset);
621✔
25
    offset += buf.length;
621✔
26
  }
27

28
  if (offset < totalLength) {
270✔
29
    return new FastBuffer(target.buffer, target.byteOffset, offset);
27✔
30
  }
31

32
  return target;
243✔
33
}
34

35
/**
36
 * Masks a buffer using the given mask.
37
 *
38
 * @param {Buffer} source The buffer to mask
39
 * @param {Buffer} mask The mask to use
40
 * @param {Buffer} output The buffer where to store the result
41
 * @param {Number} offset The offset at which to start writing
42
 * @param {Number} length The number of bytes to mask.
43
 * @public
44
 */
45
function _mask(source, mask, output, offset, length) {
46
  for (let i = 0; i < length; i++) {
4,375✔
47
    output[offset + i] = source[i] ^ mask[i & 3];
7,533✔
48
  }
49
}
50

51
/**
52
 * Unmasks a buffer using the given mask.
53
 *
54
 * @param {Buffer} buffer The buffer to unmask
55
 * @param {Buffer} mask The mask to use
56
 * @public
57
 */
58
function _unmask(buffer, mask) {
59
  for (let i = 0; i < buffer.length; i++) {
1,350✔
60
    buffer[i] ^= mask[i & 3];
7,744✔
61
  }
62
}
63

64
/**
65
 * Converts a buffer to an `ArrayBuffer`.
66
 *
67
 * @param {Buffer} buf The buffer to convert
68
 * @return {ArrayBuffer} Converted buffer
69
 * @public
70
 */
71
function toArrayBuffer(buf) {
72
  if (buf.length === buf.buffer.byteLength) {
54✔
73
    return buf.buffer;
27✔
74
  }
75

76
  return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
27✔
77
}
78

79
/**
80
 * Converts `data` to a `Buffer`.
81
 *
82
 * @param {*} data The data to convert
83
 * @return {Buffer} The buffer
84
 * @throws {TypeError}
85
 * @public
86
 */
87
function toBuffer(data) {
88
  toBuffer.readOnly = true;
25,676✔
89

90
  if (Buffer.isBuffer(data)) return data;
25,676✔
91

92
  let buf;
93

94
  if (data instanceof ArrayBuffer) {
594✔
95
    buf = new FastBuffer(data);
135✔
96
  } else if (ArrayBuffer.isView(data)) {
459✔
97
    buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
189✔
98
  } else {
99
    buf = Buffer.from(data);
270✔
100
    toBuffer.readOnly = false;
270✔
101
  }
102

103
  return buf;
594✔
104
}
105

106
module.exports = {
27✔
107
  concat,
108
  mask: _mask,
109
  toArrayBuffer,
110
  toBuffer,
111
  unmask: _unmask
112
};
113

114
/* istanbul ignore else  */
115
if (!process.env.WS_NO_BUFFER_UTIL) {
27✔
116
  try {
27✔
117
    const bufferUtil = require('bufferutil');
27✔
118

119
    module.exports.mask = function (source, mask, output, offset, length) {
27✔
120
      if (length < 48) _mask(source, mask, output, offset, length);
4,834✔
121
      else bufferUtil.mask(source, mask, output, offset, length);
459✔
122
    };
123

124
    module.exports.unmask = function (buffer, mask) {
27✔
125
      if (buffer.length < 32) _unmask(buffer, mask);
1,755✔
126
      else bufferUtil.unmask(buffer, mask);
405✔
127
    };
128
  } catch (e) {
129
    // Continue regardless of the error.
130
  }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc