index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. var buffer = require('buffer');
  3. var Buffer = buffer.Buffer;
  4. var SlowBuffer = buffer.SlowBuffer;
  5. var MAX_LEN = buffer.kMaxLength || 2147483647;
  6. exports.alloc = function alloc(size, fill, encoding) {
  7. if (typeof Buffer.alloc === 'function') {
  8. return Buffer.alloc(size, fill, encoding);
  9. }
  10. if (typeof encoding === 'number') {
  11. throw new TypeError('encoding must not be number');
  12. }
  13. if (typeof size !== 'number') {
  14. throw new TypeError('size must be a number');
  15. }
  16. if (size > MAX_LEN) {
  17. throw new RangeError('size is too large');
  18. }
  19. var enc = encoding;
  20. var _fill = fill;
  21. if (_fill === undefined) {
  22. enc = undefined;
  23. _fill = 0;
  24. }
  25. var buf = new Buffer(size);
  26. if (typeof _fill === 'string') {
  27. var fillBuf = new Buffer(_fill, enc);
  28. var flen = fillBuf.length;
  29. var i = -1;
  30. while (++i < size) {
  31. buf[i] = fillBuf[i % flen];
  32. }
  33. } else {
  34. buf.fill(_fill);
  35. }
  36. return buf;
  37. }
  38. exports.allocUnsafe = function allocUnsafe(size) {
  39. if (typeof Buffer.allocUnsafe === 'function') {
  40. return Buffer.allocUnsafe(size);
  41. }
  42. if (typeof size !== 'number') {
  43. throw new TypeError('size must be a number');
  44. }
  45. if (size > MAX_LEN) {
  46. throw new RangeError('size is too large');
  47. }
  48. return new Buffer(size);
  49. }
  50. exports.from = function from(value, encodingOrOffset, length) {
  51. if (typeof Buffer.from === 'function' && (!global.Uint8Array || Uint8Array.from !== Buffer.from)) {
  52. return Buffer.from(value, encodingOrOffset, length);
  53. }
  54. if (typeof value === 'number') {
  55. throw new TypeError('"value" argument must not be a number');
  56. }
  57. if (typeof value === 'string') {
  58. return new Buffer(value, encodingOrOffset);
  59. }
  60. if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
  61. var offset = encodingOrOffset;
  62. if (arguments.length === 1) {
  63. return new Buffer(value);
  64. }
  65. if (typeof offset === 'undefined') {
  66. offset = 0;
  67. }
  68. var len = length;
  69. if (typeof len === 'undefined') {
  70. len = value.byteLength - offset;
  71. }
  72. if (offset >= value.byteLength) {
  73. throw new RangeError('\'offset\' is out of bounds');
  74. }
  75. if (len > value.byteLength - offset) {
  76. throw new RangeError('\'length\' is out of bounds');
  77. }
  78. return new Buffer(value.slice(offset, offset + len));
  79. }
  80. if (Buffer.isBuffer(value)) {
  81. var out = new Buffer(value.length);
  82. value.copy(out, 0, 0, value.length);
  83. return out;
  84. }
  85. if (value) {
  86. if (Array.isArray(value) || (typeof ArrayBuffer !== 'undefined' && value.buffer instanceof ArrayBuffer) || 'length' in value) {
  87. return new Buffer(value);
  88. }
  89. if (value.type === 'Buffer' && Array.isArray(value.data)) {
  90. return new Buffer(value.data);
  91. }
  92. }
  93. throw new TypeError('First argument must be a string, Buffer, ' + 'ArrayBuffer, Array, or array-like object.');
  94. }
  95. exports.allocUnsafeSlow = function allocUnsafeSlow(size) {
  96. if (typeof Buffer.allocUnsafeSlow === 'function') {
  97. return Buffer.allocUnsafeSlow(size);
  98. }
  99. if (typeof size !== 'number') {
  100. throw new TypeError('size must be a number');
  101. }
  102. if (size >= MAX_LEN) {
  103. throw new RangeError('size is too large');
  104. }
  105. return new SlowBuffer(size);
  106. }