buffer-util.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. const { EMPTY_BUFFER } = require('./constants');
  3. /**
  4. * Merges an array of buffers into a new buffer.
  5. *
  6. * @param {Buffer[]} list The array of buffers to concat
  7. * @param {Number} totalLength The total length of buffers in the list
  8. * @return {Buffer} The resulting buffer
  9. * @public
  10. */
  11. function concat(list, totalLength) {
  12. if (list.length === 0) return EMPTY_BUFFER;
  13. if (list.length === 1) return list[0];
  14. const target = Buffer.allocUnsafe(totalLength);
  15. let offset = 0;
  16. for (let i = 0; i < list.length; i++) {
  17. const buf = list[i];
  18. target.set(buf, offset);
  19. offset += buf.length;
  20. }
  21. if (offset < totalLength) return target.slice(0, offset);
  22. return target;
  23. }
  24. /**
  25. * Masks a buffer using the given mask.
  26. *
  27. * @param {Buffer} source The buffer to mask
  28. * @param {Buffer} mask The mask to use
  29. * @param {Buffer} output The buffer where to store the result
  30. * @param {Number} offset The offset at which to start writing
  31. * @param {Number} length The number of bytes to mask.
  32. * @public
  33. */
  34. function _mask(source, mask, output, offset, length) {
  35. for (let i = 0; i < length; i++) {
  36. output[offset + i] = source[i] ^ mask[i & 3];
  37. }
  38. }
  39. /**
  40. * Unmasks a buffer using the given mask.
  41. *
  42. * @param {Buffer} buffer The buffer to unmask
  43. * @param {Buffer} mask The mask to use
  44. * @public
  45. */
  46. function _unmask(buffer, mask) {
  47. for (let i = 0; i < buffer.length; i++) {
  48. buffer[i] ^= mask[i & 3];
  49. }
  50. }
  51. /**
  52. * Converts a buffer to an `ArrayBuffer`.
  53. *
  54. * @param {Buffer} buf The buffer to convert
  55. * @return {ArrayBuffer} Converted buffer
  56. * @public
  57. */
  58. function toArrayBuffer(buf) {
  59. if (buf.byteLength === buf.buffer.byteLength) {
  60. return buf.buffer;
  61. }
  62. return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  63. }
  64. /**
  65. * Converts `data` to a `Buffer`.
  66. *
  67. * @param {*} data The data to convert
  68. * @return {Buffer} The buffer
  69. * @throws {TypeError}
  70. * @public
  71. */
  72. function toBuffer(data) {
  73. toBuffer.readOnly = true;
  74. if (Buffer.isBuffer(data)) return data;
  75. let buf;
  76. if (data instanceof ArrayBuffer) {
  77. buf = Buffer.from(data);
  78. } else if (ArrayBuffer.isView(data)) {
  79. buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
  80. } else {
  81. buf = Buffer.from(data);
  82. toBuffer.readOnly = false;
  83. }
  84. return buf;
  85. }
  86. module.exports = {
  87. concat,
  88. mask: _mask,
  89. toArrayBuffer,
  90. toBuffer,
  91. unmask: _unmask
  92. };
  93. /* istanbul ignore else */
  94. if (!process.env.WS_NO_BUFFER_UTIL) {
  95. try {
  96. const bufferUtil = require('bufferutil');
  97. module.exports.mask = function (source, mask, output, offset, length) {
  98. if (length < 48) _mask(source, mask, output, offset, length);
  99. else bufferUtil.mask(source, mask, output, offset, length);
  100. };
  101. module.exports.unmask = function (buffer, mask) {
  102. if (buffer.length < 32) _unmask(buffer, mask);
  103. else bufferUtil.unmask(buffer, mask);
  104. };
  105. } catch (e) {
  106. // Continue regardless of the error.
  107. }
  108. }