1 | 'use strict';
|
2 |
|
3 | var buffer = require('buffer');
|
4 | var Buffer = buffer.Buffer;
|
5 | var SlowBuffer = buffer.SlowBuffer;
|
6 | var MAX_LEN = buffer.kMaxLength || 2147483647;
|
7 | exports.alloc = function alloc(size, fill, encoding) {
|
8 | if (typeof Buffer.alloc === 'function') {
|
9 | return Buffer.alloc(size, fill, encoding);
|
10 | }
|
11 | if (typeof encoding === 'number') {
|
12 | throw new TypeError('encoding must not be number');
|
13 | }
|
14 | if (typeof size !== 'number') {
|
15 | throw new TypeError('size must be a number');
|
16 | }
|
17 | if (size > MAX_LEN) {
|
18 | throw new RangeError('size is too large');
|
19 | }
|
20 | var enc = encoding;
|
21 | var _fill = fill;
|
22 | if (_fill === undefined) {
|
23 | enc = undefined;
|
24 | _fill = 0;
|
25 | }
|
26 | var buf = new Buffer(size);
|
27 | if (typeof _fill === 'string') {
|
28 | var fillBuf = new Buffer(_fill, enc);
|
29 | var flen = fillBuf.length;
|
30 | var i = -1;
|
31 | while (++i < size) {
|
32 | buf[i] = fillBuf[i % flen];
|
33 | }
|
34 | } else {
|
35 | buf.fill(_fill);
|
36 | }
|
37 | return buf;
|
38 | }
|
39 | exports.allocUnsafe = function allocUnsafe(size) {
|
40 | if (typeof Buffer.allocUnsafe === 'function') {
|
41 | return Buffer.allocUnsafe(size);
|
42 | }
|
43 | if (typeof size !== 'number') {
|
44 | throw new TypeError('size must be a number');
|
45 | }
|
46 | if (size > MAX_LEN) {
|
47 | throw new RangeError('size is too large');
|
48 | }
|
49 | return new Buffer(size);
|
50 | }
|
51 | exports.from = function from(value, encodingOrOffset, length) {
|
52 | if (typeof Buffer.from === 'function' && (!global.Uint8Array || Uint8Array.from !== Buffer.from)) {
|
53 | return Buffer.from(value, encodingOrOffset, length);
|
54 | }
|
55 | if (typeof value === 'number') {
|
56 | throw new TypeError('"value" argument must not be a number');
|
57 | }
|
58 | if (typeof value === 'string') {
|
59 | return new Buffer(value, encodingOrOffset);
|
60 | }
|
61 | if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
|
62 | var offset = encodingOrOffset;
|
63 | if (arguments.length === 1) {
|
64 | return new Buffer(value);
|
65 | }
|
66 | if (typeof offset === 'undefined') {
|
67 | offset = 0;
|
68 | }
|
69 | var len = length;
|
70 | if (typeof len === 'undefined') {
|
71 | len = value.byteLength - offset;
|
72 | }
|
73 | if (offset >= value.byteLength) {
|
74 | throw new RangeError('\'offset\' is out of bounds');
|
75 | }
|
76 | if (len > value.byteLength - offset) {
|
77 | throw new RangeError('\'length\' is out of bounds');
|
78 | }
|
79 | return new Buffer(value.slice(offset, offset + len));
|
80 | }
|
81 | if (Buffer.isBuffer(value)) {
|
82 | var out = new Buffer(value.length);
|
83 | value.copy(out, 0, 0, value.length);
|
84 | return out;
|
85 | }
|
86 | if (value) {
|
87 | if (Array.isArray(value) || (typeof ArrayBuffer !== 'undefined' && value.buffer instanceof ArrayBuffer) || 'length' in value) {
|
88 | return new Buffer(value);
|
89 | }
|
90 | if (value.type === 'Buffer' && Array.isArray(value.data)) {
|
91 | return new Buffer(value.data);
|
92 | }
|
93 | }
|
94 |
|
95 | throw new TypeError('First argument must be a string, Buffer, ' + 'ArrayBuffer, Array, or array-like object.');
|
96 | }
|
97 | exports.allocUnsafeSlow = function allocUnsafeSlow(size) {
|
98 | if (typeof Buffer.allocUnsafeSlow === 'function') {
|
99 | return Buffer.allocUnsafeSlow(size);
|
100 | }
|
101 | if (typeof size !== 'number') {
|
102 | throw new TypeError('size must be a number');
|
103 | }
|
104 | if (size >= MAX_LEN) {
|
105 | throw new RangeError('size is too large');
|
106 | }
|
107 | return new SlowBuffer(size);
|
108 | }
|