dangerous.js (1483B)
1 /* eslint-disable node/no-deprecated-api */ 2 3 'use strict' 4 5 var buffer = require('buffer') 6 var Buffer = buffer.Buffer 7 var safer = require('./safer.js') 8 var Safer = safer.Buffer 9 10 var dangerous = {} 11 12 var key 13 14 for (key in safer) { 15 if (!safer.hasOwnProperty(key)) continue 16 dangerous[key] = safer[key] 17 } 18 19 var Dangereous = dangerous.Buffer = {} 20 21 // Copy Safer API 22 for (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 28 for (key in Buffer) { 29 if (!Buffer.hasOwnProperty(key)) continue 30 if (Dangereous.hasOwnProperty(key)) continue 31 Dangereous[key] = Buffer[key] 32 } 33 34 if (!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 46 if (!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 58 module.exports = dangerous