blob: fe658606b8ef41bef45114afc312f480220188fc [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001var bufferFill = require('buffer-fill')
2var allocUnsafe = require('buffer-alloc-unsafe')
3
4module.exports = function alloc (size, fill, encoding) {
5 if (typeof size !== 'number') {
6 throw new TypeError('"size" argument must be a number')
7 }
8
9 if (size < 0) {
10 throw new RangeError('"size" argument must not be negative')
11 }
12
13 if (Buffer.alloc) {
14 return Buffer.alloc(size, fill, encoding)
15 }
16
17 var buffer = allocUnsafe(size)
18
19 if (size === 0) {
20 return buffer
21 }
22
23 if (fill === undefined) {
24 return bufferFill(buffer, 0)
25 }
26
27 if (typeof encoding !== 'string') {
28 encoding = undefined
29 }
30
31 return bufferFill(buffer, fill, encoding)
32}