UNPKG

1.48 kBJavaScriptView Raw
1/* eslint-disable node/no-deprecated-api */
2
3'use strict'
4
5var buffer = require('buffer')
6var Buffer = buffer.Buffer
7var safer = require('./safer.js')
8var Safer = safer.Buffer
9
10var dangerous = {}
11
12var key
13
14for (key in safer) {
15 if (!safer.hasOwnProperty(key)) continue
16 dangerous[key] = safer[key]
17}
18
19var Dangereous = dangerous.Buffer = {}
20
21// Copy Safer API
22for (key in Safer) {
23 if (!Safer.hasOwnProperty(key)) continue
24 Dangereous[key] = Safer[key]
25}
26
27// Copy those missing unsafe methods, if they are present
28for (key in Buffer) {
29 if (!Buffer.hasOwnProperty(key)) continue
30 if (Dangereous.hasOwnProperty(key)) continue
31 Dangereous[key] = Buffer[key]
32}
33
34if (!Dangereous.allocUnsafe) {
35 Dangereous.allocUnsafe = function (size) {
36 if (typeof size !== 'number') {
37 throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size)
38 }
39 if (size < 0 || size >= 2 * (1 << 30)) {
40 throw new RangeError('The value "' + size + '" is invalid for option "size"')
41 }
42 return Buffer(size)
43 }
44}
45
46if (!Dangereous.allocUnsafeSlow) {
47 Dangereous.allocUnsafeSlow = function (size) {
48 if (typeof size !== 'number') {
49 throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size)
50 }
51 if (size < 0 || size >= 2 * (1 << 30)) {
52 throw new RangeError('The value "' + size + '" is invalid for option "size"')
53 }
54 return buffer.SlowBuffer(size)
55 }
56}
57
58module.exports = dangerous