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