buffer_list.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. 'use strict';
  2. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
  3. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
  4. function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  7. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  8. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
  9. function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  10. var _require = require('buffer'),
  11. Buffer = _require.Buffer;
  12. var _require2 = require('util'),
  13. inspect = _require2.inspect;
  14. var custom = inspect && inspect.custom || 'inspect';
  15. function copyBuffer(src, target, offset) {
  16. Buffer.prototype.copy.call(src, target, offset);
  17. }
  18. module.exports = /*#__PURE__*/function () {
  19. function BufferList() {
  20. _classCallCheck(this, BufferList);
  21. this.head = null;
  22. this.tail = null;
  23. this.length = 0;
  24. }
  25. _createClass(BufferList, [{
  26. key: "push",
  27. value: function push(v) {
  28. var entry = {
  29. data: v,
  30. next: null
  31. };
  32. if (this.length > 0) this.tail.next = entry;else this.head = entry;
  33. this.tail = entry;
  34. ++this.length;
  35. }
  36. }, {
  37. key: "unshift",
  38. value: function unshift(v) {
  39. var entry = {
  40. data: v,
  41. next: this.head
  42. };
  43. if (this.length === 0) this.tail = entry;
  44. this.head = entry;
  45. ++this.length;
  46. }
  47. }, {
  48. key: "shift",
  49. value: function shift() {
  50. if (this.length === 0) return;
  51. var ret = this.head.data;
  52. if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
  53. --this.length;
  54. return ret;
  55. }
  56. }, {
  57. key: "clear",
  58. value: function clear() {
  59. this.head = this.tail = null;
  60. this.length = 0;
  61. }
  62. }, {
  63. key: "join",
  64. value: function join(s) {
  65. if (this.length === 0) return '';
  66. var p = this.head;
  67. var ret = '' + p.data;
  68. while (p = p.next) ret += s + p.data;
  69. return ret;
  70. }
  71. }, {
  72. key: "concat",
  73. value: function concat(n) {
  74. if (this.length === 0) return Buffer.alloc(0);
  75. var ret = Buffer.allocUnsafe(n >>> 0);
  76. var p = this.head;
  77. var i = 0;
  78. while (p) {
  79. copyBuffer(p.data, ret, i);
  80. i += p.data.length;
  81. p = p.next;
  82. }
  83. return ret;
  84. }
  85. // Consumes a specified amount of bytes or characters from the buffered data.
  86. }, {
  87. key: "consume",
  88. value: function consume(n, hasStrings) {
  89. var ret;
  90. if (n < this.head.data.length) {
  91. // `slice` is the same for buffers and strings.
  92. ret = this.head.data.slice(0, n);
  93. this.head.data = this.head.data.slice(n);
  94. } else if (n === this.head.data.length) {
  95. // First chunk is a perfect match.
  96. ret = this.shift();
  97. } else {
  98. // Result spans more than one buffer.
  99. ret = hasStrings ? this._getString(n) : this._getBuffer(n);
  100. }
  101. return ret;
  102. }
  103. }, {
  104. key: "first",
  105. value: function first() {
  106. return this.head.data;
  107. }
  108. // Consumes a specified amount of characters from the buffered data.
  109. }, {
  110. key: "_getString",
  111. value: function _getString(n) {
  112. var p = this.head;
  113. var c = 1;
  114. var ret = p.data;
  115. n -= ret.length;
  116. while (p = p.next) {
  117. var str = p.data;
  118. var nb = n > str.length ? str.length : n;
  119. if (nb === str.length) ret += str;else ret += str.slice(0, n);
  120. n -= nb;
  121. if (n === 0) {
  122. if (nb === str.length) {
  123. ++c;
  124. if (p.next) this.head = p.next;else this.head = this.tail = null;
  125. } else {
  126. this.head = p;
  127. p.data = str.slice(nb);
  128. }
  129. break;
  130. }
  131. ++c;
  132. }
  133. this.length -= c;
  134. return ret;
  135. }
  136. // Consumes a specified amount of bytes from the buffered data.
  137. }, {
  138. key: "_getBuffer",
  139. value: function _getBuffer(n) {
  140. var ret = Buffer.allocUnsafe(n);
  141. var p = this.head;
  142. var c = 1;
  143. p.data.copy(ret);
  144. n -= p.data.length;
  145. while (p = p.next) {
  146. var buf = p.data;
  147. var nb = n > buf.length ? buf.length : n;
  148. buf.copy(ret, ret.length - n, 0, nb);
  149. n -= nb;
  150. if (n === 0) {
  151. if (nb === buf.length) {
  152. ++c;
  153. if (p.next) this.head = p.next;else this.head = this.tail = null;
  154. } else {
  155. this.head = p;
  156. p.data = buf.slice(nb);
  157. }
  158. break;
  159. }
  160. ++c;
  161. }
  162. this.length -= c;
  163. return ret;
  164. }
  165. // Make sure the linked list only shows the minimal necessary information.
  166. }, {
  167. key: custom,
  168. value: function value(_, options) {
  169. return inspect(this, _objectSpread(_objectSpread({}, options), {}, {
  170. // Only inspect one level.
  171. depth: 0,
  172. // It should not recurse.
  173. customInspect: false
  174. }));
  175. }
  176. }]);
  177. return BufferList;
  178. }();