node_modules/safe-buffer/index.js   A
last analyzed

Complexity

Total Complexity 17
Complexity/F 2.83

Size

Lines of Code 61
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 37
nc 2
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 17
mnd 2
bc 17
fnc 6
bpm 2.8333
cpm 2.8333
noi 1

6 Functions

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ copyProps 0 5 2
A SafeBuffer.alloc 0 16 4
A SafeBuffer.allocUnsafeSlow 0 6 2
A index.js ➔ SafeBuffer 0 3 1
A SafeBuffer.allocUnsafe 0 6 2
A SafeBuffer.from 0 6 2
1
/* 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) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
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
// Copy static methods from Buffer
24
copyProps(Buffer, SafeBuffer)
25
26
SafeBuffer.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
33
SafeBuffer.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
50
SafeBuffer.allocUnsafe = function (size) {
51
  if (typeof size !== 'number') {
52
    throw new TypeError('Argument must be a number')
53
  }
54
  return Buffer(size)
55
}
56
57
SafeBuffer.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