dangerous.js 1.4KB

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