Skip to content

Commit e8ee016

Browse files
committed
bufferlist
1 parent d22eb88 commit e8ee016

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

build/files.js

+9
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ const headRegexp = /(^module.exports = \w+;?)/m
171171
/Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
172172
`bufferShim.$1`
173173
]
174+
, internalDirectory = [
175+
/require\('internal\/streams\/BufferList'\)/,
176+
'require(\'./internal/streams/BufferList\')'
177+
]
174178

175179
module.exports['_stream_duplex.js'] = [
176180
requireReplacement
@@ -215,6 +219,7 @@ module.exports['_stream_readable.js'] = [
215219
, eventEmittterListenerCountReplacement
216220
, bufferShimFix
217221
, bufferStaticMethods
222+
, internalDirectory
218223
]
219224

220225
module.exports['_stream_transform.js'] = [
@@ -246,3 +251,7 @@ module.exports['_stream_writable.js'] = [
246251
, bufferShimFix
247252
, bufferStaticMethods
248253
]
254+
module.exports['internal/streams/BufferList.js'] = [
255+
bufferShimFix
256+
, bufferStaticMethods
257+
]

build/test-replacements.js

+6
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,9 @@ module.exports['test-stream3-cork-uncork.js'] = module.exports['test-stream3-cor
264264
'assert.deepEqual(seen, expected);'
265265
]
266266
]
267+
module.exports['test-stream2-readable-from-list.js'] = [
268+
[
269+
/require\('internal\/streams\/BufferList'\)/,
270+
'require(\'../../lib/internal/streams/BufferList\')'
271+
]
272+
]

lib/_stream_readable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if (debugUtil && debugUtil.debuglog) {
5151
}
5252
/*</replacement>*/
5353

54-
var BufferList = require('internal/streams/BufferList');
54+
var BufferList = require('./internal/streams/BufferList');
5555
var StringDecoder;
5656

5757
util.inherits(Readable, Stream);

lib/internal/streams/BufferList.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
var Buffer = require('buffer').Buffer;
4+
/*<replacement>*/
5+
var bufferShim = require('buffer-shims');
6+
/*</replacement>*/
7+
8+
module.exports = BufferList;
9+
10+
function BufferList() {
11+
this.head = null;
12+
this.tail = null;
13+
this.length = 0;
14+
}
15+
16+
BufferList.prototype.push = function (v) {
17+
var entry = { data: v, next: null };
18+
if (this.length > 0) this.tail.next = entry;else this.head = entry;
19+
this.tail = entry;
20+
++this.length;
21+
};
22+
23+
BufferList.prototype.unshift = function (v) {
24+
var entry = { data: v, next: this.head };
25+
if (this.length === 0) this.tail = entry;
26+
this.head = entry;
27+
++this.length;
28+
};
29+
30+
BufferList.prototype.shift = function () {
31+
if (this.length === 0) return;
32+
var ret = this.head.data;
33+
if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
34+
--this.length;
35+
return ret;
36+
};
37+
38+
BufferList.prototype.clear = function () {
39+
this.head = this.tail = null;
40+
this.length = 0;
41+
};
42+
43+
BufferList.prototype.join = function (s) {
44+
if (this.length === 0) return '';
45+
var p = this.head;
46+
var ret = '' + p.data;
47+
while (p = p.next) {
48+
ret += s + p.data;
49+
}return ret;
50+
};
51+
52+
BufferList.prototype.concat = function (n) {
53+
if (this.length === 0) return bufferShim.alloc(0);
54+
if (this.length === 1) return this.head.data;
55+
var ret = bufferShim.allocUnsafe(n >>> 0);
56+
var p = this.head;
57+
var i = 0;
58+
while (p) {
59+
p.data.copy(ret, i);
60+
i += p.data.length;
61+
p = p.next;
62+
}
63+
return ret;
64+
};

test/parallel/test-stream2-readable-from-list.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var bufferShim = require('buffer-shims');
55
require('../common');
66
var assert = require('assert/');
77
var fromList = require('../../lib/_stream_readable')._fromList;
8-
var BufferList = require('internal/streams/BufferList');
8+
var BufferList = require('../../lib/internal/streams/BufferList');
99

1010
// tiny node-tap lookalike.
1111
var tests = [];

0 commit comments

Comments
 (0)