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