buffer-util.js 3.0 KB

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