File tree Expand file tree Collapse file tree 5 files changed +116
-0
lines changed Expand file tree Collapse file tree 5 files changed +116
-0
lines changed Original file line number Diff line number Diff line change
1
+ /node_modules /
2
+ /npm-debug.log
Original file line number Diff line number Diff line change
1
+ function alloc ( size , fill , encoding ) {
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
+ var buffer = new Buffer ( size )
11
+
12
+ if ( size === 0 ) {
13
+ return buffer
14
+ }
15
+
16
+ if ( fill === undefined ) {
17
+ buffer . fill ( 0 )
18
+ return buffer
19
+ }
20
+
21
+ if ( typeof encoding !== 'string' ) {
22
+ encoding = undefined
23
+ }
24
+
25
+ buffer . fill ( fill , encoding )
26
+ return buffer
27
+ }
28
+
29
+ module . exports = ( Buffer . alloc || alloc )
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " buffer-alloc" ,
3
+ "version" : " 0.0.0" ,
4
+ "license" : " MIT" ,
5
+ "repository" : " LinusU/buffer-alloc" ,
6
+ "scripts" : {
7
+ "test" : " standard && node test"
8
+ },
9
+ "devDependencies" : {
10
+ "standard" : " ^7.1.2"
11
+ },
12
+ "engineStrict" : true ,
13
+ "engines" : {
14
+ "node" : " >= 0.12"
15
+ },
16
+ "keywords" : [
17
+ " alloc" ,
18
+ " allocate" ,
19
+ " buffer alloc" ,
20
+ " buffer allocate" ,
21
+ " buffer"
22
+ ]
23
+ }
Original file line number Diff line number Diff line change
1
+ # Buffer Alloc
2
+
3
+ A ponyfill for ` Buffer.alloc ` , uses native implementation if available.
4
+
5
+ ## Installation
6
+
7
+ ``` sh
8
+ npm install --save buffer-alloc
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ``` js
14
+ const alloc = require (' buffer-alloc' )
15
+
16
+ console .log (alloc (4 ))
17
+ // => <Buffer 00 00 00 00>
18
+
19
+ console .log (alloc (6 , 0x41 ))
20
+ // => <Buffer 41 41 41 41 41 41>
21
+
22
+ console .log (alloc (10 , ' linus' , ' utf8' ))
23
+ // => <Buffer 6c 69 6e 75 73 6c 69 6e 75 73>
24
+ ```
25
+
26
+ ## API
27
+
28
+ ### alloc(size[ , fill[ , encoding]] )
29
+
30
+ - ` size ` < ; Integer> ; The desired length of the new ` Buffer `
31
+ - ` fill ` < ; String> ; | < ; Buffer> ; | < ; Integer> ; A value to pre-fill the new ` Buffer ` with. ** Default:** ` 0 `
32
+ - ` encoding ` < ; String> ; If ` fill ` is a string, this is its encoding. ** Default:** ` 'utf8' `
33
+
34
+ Allocates a new ` Buffer ` of ` size ` bytes. If ` fill ` is ` undefined ` , the ` Buffer ` will be zero-filled.
35
+
36
+ ## See also
37
+
38
+ - [ buffer-from] ( https://github.com/LinusU/buffer-from ) A ponyfill for ` Buffer.from `
39
+ - [ buffer-alloc-unsafe] ( https://github.com/LinusU/buffer-alloc-unsafe ) A ponyfill for ` Buffer.allocUnsafe `
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments