1 | 'use strict';
|
2 |
|
3 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
4 |
|
5 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
6 |
|
7 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
8 |
|
9 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
10 |
|
11 | 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, descriptor.key, descriptor); } }
|
12 |
|
13 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
14 |
|
15 | var _require = require('buffer'),
|
16 | Buffer = _require.Buffer;
|
17 |
|
18 | var _require2 = require('util'),
|
19 | inspect = _require2.inspect;
|
20 |
|
21 | var custom = inspect && inspect.custom || 'inspect';
|
22 |
|
23 | function copyBuffer(src, target, offset) {
|
24 | Buffer.prototype.copy.call(src, target, offset);
|
25 | }
|
26 |
|
27 | module.exports =
|
28 |
|
29 | function () {
|
30 | function BufferList() {
|
31 | _classCallCheck(this, BufferList);
|
32 |
|
33 | this.head = null;
|
34 | this.tail = null;
|
35 | this.length = 0;
|
36 | }
|
37 |
|
38 | _createClass(BufferList, [{
|
39 | key: "push",
|
40 | value: function push(v) {
|
41 | var entry = {
|
42 | data: v,
|
43 | next: null
|
44 | };
|
45 | if (this.length > 0) this.tail.next = entry;else this.head = entry;
|
46 | this.tail = entry;
|
47 | ++this.length;
|
48 | }
|
49 | }, {
|
50 | key: "unshift",
|
51 | value: function unshift(v) {
|
52 | var entry = {
|
53 | data: v,
|
54 | next: this.head
|
55 | };
|
56 | if (this.length === 0) this.tail = entry;
|
57 | this.head = entry;
|
58 | ++this.length;
|
59 | }
|
60 | }, {
|
61 | key: "shift",
|
62 | value: function shift() {
|
63 | if (this.length === 0) return;
|
64 | var ret = this.head.data;
|
65 | if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
|
66 | --this.length;
|
67 | return ret;
|
68 | }
|
69 | }, {
|
70 | key: "clear",
|
71 | value: function clear() {
|
72 | this.head = this.tail = null;
|
73 | this.length = 0;
|
74 | }
|
75 | }, {
|
76 | key: "join",
|
77 | value: function join(s) {
|
78 | if (this.length === 0) return '';
|
79 | var p = this.head;
|
80 | var ret = '' + p.data;
|
81 |
|
82 | while (p = p.next) {
|
83 | ret += s + p.data;
|
84 | }
|
85 |
|
86 | return ret;
|
87 | }
|
88 | }, {
|
89 | key: "concat",
|
90 | value: function concat(n) {
|
91 | if (this.length === 0) return Buffer.alloc(0);
|
92 | var ret = Buffer.allocUnsafe(n >>> 0);
|
93 | var p = this.head;
|
94 | var i = 0;
|
95 |
|
96 | while (p) {
|
97 | copyBuffer(p.data, ret, i);
|
98 | i += p.data.length;
|
99 | p = p.next;
|
100 | }
|
101 |
|
102 | return ret;
|
103 | }
|
104 |
|
105 | }, {
|
106 | key: "consume",
|
107 | value: function consume(n, hasStrings) {
|
108 | var ret;
|
109 |
|
110 | if (n < this.head.data.length) {
|
111 |
|
112 | ret = this.head.data.slice(0, n);
|
113 | this.head.data = this.head.data.slice(n);
|
114 | } else if (n === this.head.data.length) {
|
115 |
|
116 | ret = this.shift();
|
117 | } else {
|
118 |
|
119 | ret = hasStrings ? this._getString(n) : this._getBuffer(n);
|
120 | }
|
121 |
|
122 | return ret;
|
123 | }
|
124 | }, {
|
125 | key: "first",
|
126 | value: function first() {
|
127 | return this.head.data;
|
128 | }
|
129 |
|
130 | }, {
|
131 | key: "_getString",
|
132 | value: function _getString(n) {
|
133 | var p = this.head;
|
134 | var c = 1;
|
135 | var ret = p.data;
|
136 | n -= ret.length;
|
137 |
|
138 | while (p = p.next) {
|
139 | var str = p.data;
|
140 | var nb = n > str.length ? str.length : n;
|
141 | if (nb === str.length) ret += str;else ret += str.slice(0, n);
|
142 | n -= nb;
|
143 |
|
144 | if (n === 0) {
|
145 | if (nb === str.length) {
|
146 | ++c;
|
147 | if (p.next) this.head = p.next;else this.head = this.tail = null;
|
148 | } else {
|
149 | this.head = p;
|
150 | p.data = str.slice(nb);
|
151 | }
|
152 |
|
153 | break;
|
154 | }
|
155 |
|
156 | ++c;
|
157 | }
|
158 |
|
159 | this.length -= c;
|
160 | return ret;
|
161 | }
|
162 |
|
163 | }, {
|
164 | key: "_getBuffer",
|
165 | value: function _getBuffer(n) {
|
166 | var ret = Buffer.allocUnsafe(n);
|
167 | var p = this.head;
|
168 | var c = 1;
|
169 | p.data.copy(ret);
|
170 | n -= p.data.length;
|
171 |
|
172 | while (p = p.next) {
|
173 | var buf = p.data;
|
174 | var nb = n > buf.length ? buf.length : n;
|
175 | buf.copy(ret, ret.length - n, 0, nb);
|
176 | n -= nb;
|
177 |
|
178 | if (n === 0) {
|
179 | if (nb === buf.length) {
|
180 | ++c;
|
181 | if (p.next) this.head = p.next;else this.head = this.tail = null;
|
182 | } else {
|
183 | this.head = p;
|
184 | p.data = buf.slice(nb);
|
185 | }
|
186 |
|
187 | break;
|
188 | }
|
189 |
|
190 | ++c;
|
191 | }
|
192 |
|
193 | this.length -= c;
|
194 | return ret;
|
195 | }
|
196 |
|
197 | }, {
|
198 | key: custom,
|
199 | value: function value(_, options) {
|
200 | return inspect(this, _objectSpread({}, options, {
|
201 |
|
202 | depth: 0,
|
203 |
|
204 | customInspect: false
|
205 | }));
|
206 | }
|
207 | }]);
|
208 |
|
209 | return BufferList;
|
210 | }(); |
\ | No newline at end of file |