Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

safer.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* eslint-disable node/no-deprecated-api */
  2. 'use strict'
  3. var buffer = require('buffer')
  4. var Buffer = buffer.Buffer
  5. var safer = {}
  6. var key
  7. for (key in buffer) {
  8. if (!buffer.hasOwnProperty(key)) continue
  9. if (key === 'SlowBuffer' || key === 'Buffer') continue
  10. safer[key] = buffer[key]
  11. }
  12. var Safer = safer.Buffer = {}
  13. for (key in Buffer) {
  14. if (!Buffer.hasOwnProperty(key)) continue
  15. if (key === 'allocUnsafe' || key === 'allocUnsafeSlow') continue
  16. Safer[key] = Buffer[key]
  17. }
  18. safer.Buffer.prototype = Buffer.prototype
  19. if (!Safer.from || Safer.from === Uint8Array.from) {
  20. Safer.from = function (value, encodingOrOffset, length) {
  21. if (typeof value === 'number') {
  22. throw new TypeError('The "value" argument must not be of type number. Received type ' + typeof value)
  23. }
  24. if (value && typeof value.length === 'undefined') {
  25. throw new TypeError('The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type ' + typeof value)
  26. }
  27. return Buffer(value, encodingOrOffset, length)
  28. }
  29. }
  30. if (!Safer.alloc) {
  31. Safer.alloc = function (size, fill, encoding) {
  32. if (typeof size !== 'number') {
  33. throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size)
  34. }
  35. if (size < 0 || size >= 2 * (1 << 30)) {
  36. throw new RangeError('The value "' + size + '" is invalid for option "size"')
  37. }
  38. var buf = Buffer(size)
  39. if (!fill || fill.length === 0) {
  40. buf.fill(0)
  41. } else if (typeof encoding === 'string') {
  42. buf.fill(fill, encoding)
  43. } else {
  44. buf.fill(fill)
  45. }
  46. return buf
  47. }
  48. }
  49. if (!safer.kStringMaxLength) {
  50. try {
  51. safer.kStringMaxLength = process.binding('buffer').kStringMaxLength
  52. } catch (e) {
  53. // we can't determine kStringMaxLength in environments where process.binding
  54. // is unsupported, so let's not set it
  55. }
  56. }
  57. if (!safer.constants) {
  58. safer.constants = {
  59. MAX_LENGTH: safer.kMaxLength
  60. }
  61. if (safer.kStringMaxLength) {
  62. safer.constants.MAX_STRING_LENGTH = safer.kStringMaxLength
  63. }
  64. }
  65. module.exports = safer