Front end of the Slack clone application.

BufferUtil.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*!
  2. * ws: a node.js websocket client
  3. * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
  4. * MIT Licensed
  5. */
  6. 'use strict';
  7. const safeBuffer = require('safe-buffer');
  8. const Buffer = safeBuffer.Buffer;
  9. /**
  10. * Merges an array of buffers into a new buffer.
  11. *
  12. * @param {Buffer[]} list The array of buffers to concat
  13. * @param {Number} totalLength The total length of buffers in the list
  14. * @return {Buffer} The resulting buffer
  15. * @public
  16. */
  17. const concat = (list, totalLength) => {
  18. const target = Buffer.allocUnsafe(totalLength);
  19. var offset = 0;
  20. for (var i = 0; i < list.length; i++) {
  21. const buf = list[i];
  22. buf.copy(target, offset);
  23. offset += buf.length;
  24. }
  25. return target;
  26. };
  27. try {
  28. const bufferUtil = require('bufferutil');
  29. module.exports = Object.assign({ concat }, bufferUtil.BufferUtil || bufferUtil);
  30. } catch (e) /* istanbul ignore next */ {
  31. /**
  32. * Masks a buffer using the given mask.
  33. *
  34. * @param {Buffer} source The buffer to mask
  35. * @param {Buffer} mask The mask to use
  36. * @param {Buffer} output The buffer where to store the result
  37. * @param {Number} offset The offset at which to start writing
  38. * @param {Number} length The number of bytes to mask.
  39. * @public
  40. */
  41. const mask = (source, mask, output, offset, length) => {
  42. for (var i = 0; i < length; i++) {
  43. output[offset + i] = source[i] ^ mask[i & 3];
  44. }
  45. };
  46. /**
  47. * Unmasks a buffer using the given mask.
  48. *
  49. * @param {Buffer} buffer The buffer to unmask
  50. * @param {Buffer} mask The mask to use
  51. * @public
  52. */
  53. const unmask = (buffer, mask) => {
  54. // Required until https://github.com/nodejs/node/issues/9006 is resolved.
  55. const length = buffer.length;
  56. for (var i = 0; i < length; i++) {
  57. buffer[i] ^= mask[i & 3];
  58. }
  59. };
  60. module.exports = { concat, mask, unmask };
  61. }