├── .gitignore ├── package.json ├── test.js ├── index.js ├── LICENSE └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /npm-debug.log 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "buffer-alloc", 3 | "version": "1.2.0", 4 | "license": "MIT", 5 | "repository": "LinusU/buffer-alloc", 6 | "files": [ 7 | "index.js" 8 | ], 9 | "scripts": { 10 | "test": "standard && node test" 11 | }, 12 | "dependencies": { 13 | "buffer-alloc-unsafe": "^1.1.0", 14 | "buffer-fill": "^1.0.0" 15 | }, 16 | "devDependencies": { 17 | "standard": "^7.1.2" 18 | }, 19 | "keywords": [ 20 | "alloc", 21 | "allocate", 22 | "buffer alloc", 23 | "buffer allocate", 24 | "buffer" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var alloc = require('./') 2 | var assert = require('assert') 3 | 4 | var b1 = alloc(4) 5 | assert.ok(Buffer.isBuffer(b1)) 6 | assert.equal(b1.length, 4) 7 | assert.equal(b1.toString('hex'), '00000000') 8 | 9 | var b2 = alloc(6, 0x41) 10 | assert.ok(Buffer.isBuffer(b2)) 11 | assert.equal(b2.length, 6) 12 | assert.equal(b2.toString('hex'), '414141414141') 13 | 14 | var b3 = alloc(10, 'linus', 'utf8') 15 | assert.ok(Buffer.isBuffer(b3)) 16 | assert.equal(b3.length, 10) 17 | assert.equal(b3.toString('hex'), '6c696e75736c696e7573') 18 | 19 | var b4 = alloc(8192) 20 | assert.ok(Buffer.isBuffer(b4)) 21 | for (var i = 0; i < 8192; i++) { 22 | assert.equal(b4[i], 0) 23 | } 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var bufferFill = require('buffer-fill') 2 | var allocUnsafe = require('buffer-alloc-unsafe') 3 | 4 | module.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 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016, 2018 Linus Unnebäck 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Buffer Alloc 2 | 3 | A [ponyfill](https://ponyfill.com) for `Buffer.alloc`. 4 | 5 | Works as Node.js: `v7.0.0`
6 | Works on Node.js: `v0.10.0` 7 | 8 | ## Installation 9 | 10 | ```sh 11 | npm install --save buffer-alloc 12 | ``` 13 | 14 | ## Usage 15 | 16 | ```js 17 | const alloc = require('buffer-alloc') 18 | 19 | console.log(alloc(4)) 20 | //=> 21 | 22 | console.log(alloc(6, 0x41)) 23 | //=> 24 | 25 | console.log(alloc(10, 'linus', 'utf8')) 26 | //=> 27 | ``` 28 | 29 | ## API 30 | 31 | ### alloc(size[, fill[, encoding]]) 32 | 33 | - `size` <Integer> The desired length of the new `Buffer` 34 | - `fill` <String> | <Buffer> | <Integer> A value to pre-fill the new `Buffer` with. **Default:** `0` 35 | - `encoding` <String> If `fill` is a string, this is its encoding. **Default:** `'utf8'` 36 | 37 | Allocates a new `Buffer` of `size` bytes. If `fill` is `undefined`, the `Buffer` will be zero-filled. 38 | 39 | ## See also 40 | 41 | - [buffer-alloc-unsafe](https://github.com/LinusU/buffer-alloc-unsafe) A ponyfill for `Buffer.allocUnsafe` 42 | - [buffer-fill](https://github.com/LinusU/buffer-fill) A ponyfill for `Buffer.fill` 43 | - [buffer-from](https://github.com/LinusU/buffer-from) A ponyfill for `Buffer.from` 44 | --------------------------------------------------------------------------------