You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.0 KiB

4 years ago
  1. # bufferutil
  2. [![Version npm](https://img.shields.io/npm/v/bufferutil.svg)](https://www.npmjs.com/package/bufferutil)
  3. [![Linux/macOS Build](https://travis-ci.org/websockets/bufferutil.svg?branch=master)](https://travis-ci.org/websockets/bufferutil)
  4. [![Windows Build](https://ci.appveyor.com/api/projects/status/github/websockets/bufferutil?branch=master&svg=true)](https://ci.appveyor.com/project/lpinca/bufferutil)
  5. `bufferutil` is what makes `ws` fast. It provides some utilities to efficiently
  6. perform some operations such as masking and unmasking the data payload of
  7. WebSocket frames.
  8. ## Installation
  9. ```
  10. npm install bufferutil --save-optional
  11. ```
  12. The `--save-optional` flag tells npm to save the package in your package.json
  13. under the [`optionalDependencies`](https://docs.npmjs.com/files/package.json#optionaldependencies)
  14. key.
  15. ## API
  16. The module exports two functions.
  17. ### `bufferUtil.mask(source, mask, output, offset, length)`
  18. Masks a buffer using the given masking-key as specified by the WebSocket
  19. protocol.
  20. #### Arguments
  21. - `source` - The buffer to mask.
  22. - `mask` - A buffer representing the masking-key.
  23. - `output` - The buffer where to store the result.
  24. - `offset` - The offset at which to start writing.
  25. - `length` - The number of bytes to mask.
  26. #### Example
  27. ```js
  28. 'use strict';
  29. const bufferUtil = require('bufferutil');
  30. const crypto = require('crypto');
  31. const source = crypto.randomBytes(10);
  32. const mask = crypto.randomBytes(4);
  33. bufferUtil.mask(source, mask, source, 0, source.length);
  34. ```
  35. ### `bufferUtil.unmask(buffer, mask)`
  36. Unmasks a buffer using the given masking-key as specified by the WebSocket
  37. protocol.
  38. #### Arguments
  39. - `buffer` - The buffer to unmask.
  40. - `mask` - A buffer representing the masking-key.
  41. #### Example
  42. ```js
  43. 'use strict';
  44. const bufferUtil = require('bufferutil');
  45. const crypto = require('crypto');
  46. const buffer = crypto.randomBytes(10);
  47. const mask = crypto.randomBytes(4);
  48. bufferUtil.unmask(buffer, mask);
  49. ```
  50. ## License
  51. [MIT](LICENSE)