index.js 664 B

1234567891011121314151617181920212223242526272829303132
  1. var bufferFill = require('buffer-fill')
  2. var allocUnsafe = require('buffer-alloc-unsafe')
  3. module.exports = function alloc (size, fill, encoding) {
  4. if (typeof size !== 'number') {
  5. throw new TypeError('"size" argument must be a number')
  6. }
  7. if (size < 0) {
  8. throw new RangeError('"size" argument must not be negative')
  9. }
  10. if (Buffer.alloc) {
  11. return Buffer.alloc(size, fill, encoding)
  12. }
  13. var buffer = allocUnsafe(size)
  14. if (size === 0) {
  15. return buffer
  16. }
  17. if (fill === undefined) {
  18. return bufferFill(buffer, 0)
  19. }
  20. if (typeof encoding !== 'string') {
  21. encoding = undefined
  22. }
  23. return bufferFill(buffer, fill, encoding)
  24. }