mirror of
https://github.com/nodejs/node.git
synced 2025-05-11 01:04:44 +00:00

Currently inspecting the BufferList can result a maximum call stack size error. Adding a individual inspect function prevents this. PR-URL: https://github.com/nodejs/node/pull/17907 Refs: https://github.com/nodejs/node/issues/12693 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
83 lines
1.6 KiB
JavaScript
83 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const { Buffer } = require('buffer');
|
|
const { inspect } = require('util');
|
|
|
|
function copyBuffer(src, target, offset) {
|
|
Buffer.prototype.copy.call(src, target, offset);
|
|
}
|
|
|
|
module.exports = class BufferList {
|
|
constructor() {
|
|
this.head = null;
|
|
this.tail = null;
|
|
this.length = 0;
|
|
}
|
|
|
|
push(v) {
|
|
const entry = { data: v, next: null };
|
|
if (this.length > 0)
|
|
this.tail.next = entry;
|
|
else
|
|
this.head = entry;
|
|
this.tail = entry;
|
|
++this.length;
|
|
}
|
|
|
|
unshift(v) {
|
|
const entry = { data: v, next: this.head };
|
|
if (this.length === 0)
|
|
this.tail = entry;
|
|
this.head = entry;
|
|
++this.length;
|
|
}
|
|
|
|
shift() {
|
|
if (this.length === 0)
|
|
return;
|
|
const ret = this.head.data;
|
|
if (this.length === 1)
|
|
this.head = this.tail = null;
|
|
else
|
|
this.head = this.head.next;
|
|
--this.length;
|
|
return ret;
|
|
}
|
|
|
|
clear() {
|
|
this.head = this.tail = null;
|
|
this.length = 0;
|
|
}
|
|
|
|
join(s) {
|
|
if (this.length === 0)
|
|
return '';
|
|
var p = this.head;
|
|
var ret = '' + p.data;
|
|
while (p = p.next)
|
|
ret += s + p.data;
|
|
return ret;
|
|
}
|
|
|
|
concat(n) {
|
|
if (this.length === 0)
|
|
return Buffer.alloc(0);
|
|
if (this.length === 1)
|
|
return this.head.data;
|
|
const ret = Buffer.allocUnsafe(n >>> 0);
|
|
var p = this.head;
|
|
var i = 0;
|
|
while (p) {
|
|
copyBuffer(p.data, ret, i);
|
|
i += p.data.length;
|
|
p = p.next;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
[inspect.custom]() {
|
|
const obj = inspect({ length: this.length });
|
|
return `${this.constructor.name} ${obj}`;
|
|
}
|
|
};
|