├── .gitignore
├── LICENSE
├── index.js
├── package.json
├── readme.md
└── test.js
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules/
2 | /npm-debug.log
3 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | function allocUnsafe (size) {
2 | if (typeof size !== 'number') {
3 | throw new TypeError('"size" argument must be a number')
4 | }
5 |
6 | if (size < 0) {
7 | throw new RangeError('"size" argument must not be negative')
8 | }
9 |
10 | if (Buffer.allocUnsafe) {
11 | return Buffer.allocUnsafe(size)
12 | } else {
13 | return new Buffer(size)
14 | }
15 | }
16 |
17 | module.exports = allocUnsafe
18 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "buffer-alloc-unsafe",
3 | "version": "1.1.0",
4 | "license": "MIT",
5 | "repository": "LinusU/buffer-alloc-unsafe",
6 | "files": [
7 | "index.js"
8 | ],
9 | "scripts": {
10 | "test": "standard && node test"
11 | },
12 | "devDependencies": {
13 | "standard": "^7.1.2"
14 | },
15 | "keywords": [
16 | "allocUnsafe",
17 | "allocate",
18 | "buffer allocUnsafe",
19 | "buffer unsafe allocate",
20 | "buffer",
21 | "ponyfill",
22 | "unsafe allocate"
23 | ]
24 | }
25 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # Buffer Alloc Unsafe
2 |
3 | A [ponyfill](https://ponyfill.com) for `Buffer.allocUnsafe`.
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-unsafe
12 | ```
13 |
14 | ## Usage
15 |
16 | ```js
17 | const allocUnsafe = require('buffer-alloc-unsafe')
18 |
19 | console.log(allocUnsafe(10))
20 | //=>
21 |
22 | console.log(allocUnsafe(10))
23 | //=>
24 |
25 | console.log(allocUnsafe(10))
26 | //=>
27 |
28 | allocUnsafe(-10)
29 | //=> RangeError: "size" argument must not be negative
30 | ```
31 |
32 | ## API
33 |
34 | ### allocUnsafe(size)
35 |
36 | - `size` <Integer> The desired length of the new `Buffer`
37 |
38 | Allocates a new *non-zero-filled* `Buffer` of `size` bytes. The `size` must be
39 | less than or equal to the value of `buffer.kMaxLength` and greater than or equal
40 | to zero. Otherwise, a `RangeError` is thrown.
41 |
42 | ## See also
43 |
44 | - [buffer-alloc](https://github.com/LinusU/buffer-alloc) A ponyfill for `Buffer.alloc`
45 | - [buffer-fill](https://github.com/LinusU/buffer-fill) A ponyfill for `Buffer.fill`
46 | - [buffer-from](https://github.com/LinusU/buffer-from) A ponyfill for `Buffer.from`
47 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | var allocUnsafe = require('./')
2 | var assert = require('assert')
3 |
4 | var b1 = allocUnsafe(4)
5 | assert.equal(b1.length, 4)
6 | assert.ok(Buffer.isBuffer(b1))
7 |
8 | var b2 = allocUnsafe(6)
9 | assert.equal(b2.length, 6)
10 | assert.ok(Buffer.isBuffer(b2))
11 |
12 | var b3 = allocUnsafe(10)
13 | assert.equal(b3.length, 10)
14 | assert.ok(Buffer.isBuffer(b3))
15 |
--------------------------------------------------------------------------------