Skip to content

Commit e2199e0

Browse files
committed
streams: refactor BufferList into ES6 class
PR-URL: #12644 Reviewed-By: Brian White <[email protected]>
1 parent ed0716f commit e2199e0

File tree

1 file changed

+58
-58
lines changed

1 file changed

+58
-58
lines changed

lib/internal/streams/BufferList.js

+58-58
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,71 @@
22

33
const Buffer = require('buffer').Buffer;
44

5-
module.exports = BufferList;
5+
module.exports = class BufferList {
6+
constructor() {
7+
this.head = null;
8+
this.tail = null;
9+
this.length = 0;
10+
}
611

7-
function BufferList() {
8-
this.head = null;
9-
this.tail = null;
10-
this.length = 0;
11-
}
12+
push(v) {
13+
const entry = { data: v, next: null };
14+
if (this.length > 0)
15+
this.tail.next = entry;
16+
else
17+
this.head = entry;
18+
this.tail = entry;
19+
++this.length;
20+
}
1221

13-
BufferList.prototype.push = function(v) {
14-
const entry = { data: v, next: null };
15-
if (this.length > 0)
16-
this.tail.next = entry;
17-
else
22+
unshift(v) {
23+
const entry = { data: v, next: this.head };
24+
if (this.length === 0)
25+
this.tail = entry;
1826
this.head = entry;
19-
this.tail = entry;
20-
++this.length;
21-
};
27+
++this.length;
28+
}
2229

23-
BufferList.prototype.unshift = function(v) {
24-
const entry = { data: v, next: this.head };
25-
if (this.length === 0)
26-
this.tail = entry;
27-
this.head = entry;
28-
++this.length;
29-
};
30+
shift() {
31+
if (this.length === 0)
32+
return;
33+
const ret = this.head.data;
34+
if (this.length === 1)
35+
this.head = this.tail = null;
36+
else
37+
this.head = this.head.next;
38+
--this.length;
39+
return ret;
40+
}
3041

31-
BufferList.prototype.shift = function() {
32-
if (this.length === 0)
33-
return;
34-
const ret = this.head.data;
35-
if (this.length === 1)
42+
clear() {
3643
this.head = this.tail = null;
37-
else
38-
this.head = this.head.next;
39-
--this.length;
40-
return ret;
41-
};
42-
43-
BufferList.prototype.clear = function() {
44-
this.head = this.tail = null;
45-
this.length = 0;
46-
};
44+
this.length = 0;
45+
}
4746

48-
BufferList.prototype.join = function(s) {
49-
if (this.length === 0)
50-
return '';
51-
var p = this.head;
52-
var ret = '' + p.data;
53-
while (p = p.next)
54-
ret += s + p.data;
55-
return ret;
56-
};
47+
join(s) {
48+
if (this.length === 0)
49+
return '';
50+
var p = this.head;
51+
var ret = '' + p.data;
52+
while (p = p.next)
53+
ret += s + p.data;
54+
return ret;
55+
}
5756

58-
BufferList.prototype.concat = function(n) {
59-
if (this.length === 0)
60-
return Buffer.alloc(0);
61-
if (this.length === 1)
62-
return this.head.data;
63-
const ret = Buffer.allocUnsafe(n >>> 0);
64-
var p = this.head;
65-
var i = 0;
66-
while (p) {
67-
p.data.copy(ret, i);
68-
i += p.data.length;
69-
p = p.next;
57+
concat(n) {
58+
if (this.length === 0)
59+
return Buffer.alloc(0);
60+
if (this.length === 1)
61+
return this.head.data;
62+
const ret = Buffer.allocUnsafe(n >>> 0);
63+
var p = this.head;
64+
var i = 0;
65+
while (p) {
66+
p.data.copy(ret, i);
67+
i += p.data.length;
68+
p = p.next;
69+
}
70+
return ret;
7071
}
71-
return ret;
7272
};

0 commit comments

Comments
 (0)