1 |
3a515b92
|
cagy
|
/* eslint-disable node/no-deprecated-api */
|
2 |
|
|
var buffer = require('buffer')
|
3 |
|
|
var Buffer = buffer.Buffer
|
4 |
|
|
|
5 |
|
|
// alternative to using Object.keys for old browsers
|
6 |
|
|
function copyProps (src, dst) {
|
7 |
|
|
for (var key in src) {
|
8 |
|
|
dst[key] = src[key]
|
9 |
|
|
}
|
10 |
|
|
}
|
11 |
|
|
if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
|
12 |
|
|
module.exports = buffer
|
13 |
|
|
} else {
|
14 |
|
|
// Copy properties from require('buffer')
|
15 |
|
|
copyProps(buffer, exports)
|
16 |
|
|
exports.Buffer = SafeBuffer
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
function SafeBuffer (arg, encodingOrOffset, length) {
|
20 |
|
|
return Buffer(arg, encodingOrOffset, length)
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
SafeBuffer.prototype = Object.create(Buffer.prototype)
|
24 |
|
|
|
25 |
|
|
// Copy static methods from Buffer
|
26 |
|
|
copyProps(Buffer, SafeBuffer)
|
27 |
|
|
|
28 |
|
|
SafeBuffer.from = function (arg, encodingOrOffset, length) {
|
29 |
|
|
if (typeof arg === 'number') {
|
30 |
|
|
throw new TypeError('Argument must not be a number')
|
31 |
|
|
}
|
32 |
|
|
return Buffer(arg, encodingOrOffset, length)
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
SafeBuffer.alloc = function (size, fill, encoding) {
|
36 |
|
|
if (typeof size !== 'number') {
|
37 |
|
|
throw new TypeError('Argument must be a number')
|
38 |
|
|
}
|
39 |
|
|
var buf = Buffer(size)
|
40 |
|
|
if (fill !== undefined) {
|
41 |
|
|
if (typeof encoding === 'string') {
|
42 |
|
|
buf.fill(fill, encoding)
|
43 |
|
|
} else {
|
44 |
|
|
buf.fill(fill)
|
45 |
|
|
}
|
46 |
|
|
} else {
|
47 |
|
|
buf.fill(0)
|
48 |
|
|
}
|
49 |
|
|
return buf
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
SafeBuffer.allocUnsafe = function (size) {
|
53 |
|
|
if (typeof size !== 'number') {
|
54 |
|
|
throw new TypeError('Argument must be a number')
|
55 |
|
|
}
|
56 |
|
|
return Buffer(size)
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
SafeBuffer.allocUnsafeSlow = function (size) {
|
60 |
|
|
if (typeof size !== 'number') {
|
61 |
|
|
throw new TypeError('Argument must be a number')
|
62 |
|
|
}
|
63 |
|
|
return buffer.SlowBuffer(size)
|
64 |
|
|
}
|