From 6c66b4ae8f6273bbbccb154e06c61a59c2ce4b7a Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Wed, 9 Dec 2020 09:28:17 -0500 Subject: [PATCH] review feedback --- src/utils.ts | 15 ++++++--------- test/unit/utils.test.js | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 0426d92199e..9bee4e4736a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1159,7 +1159,10 @@ export function isRecord( const kBuffers = Symbol('buffers'); const kLength = Symbol('length'); -/** @internal */ +/** + * A pool of Buffers which allow you to read them as if they were one + * @internal + */ export class BufferPool { [kBuffers]: Buffer[]; [kLength]: number; @@ -1173,11 +1176,13 @@ export class BufferPool { return this[kLength]; } + /** Adds a buffer to the internal buffer pool list */ append(buffer: Buffer): void { this[kBuffers].push(buffer); this[kLength] += buffer.length; } + /** Returns the requested number of bytes without consuming them */ peek(size: number): Buffer { return this.read(size, false); } @@ -1187,10 +1192,6 @@ export class BufferPool { throw new TypeError('Parameter size must be a non-negative number'); } - if (typeof size === 'number' && size < 0) { - size += this.length; - } - if (size > this[kLength]) { return Buffer.alloc(0); } @@ -1244,10 +1245,6 @@ export class BufferPool { if (consume) { this[kBuffers] = this[kBuffers].slice(idx); } - - if (result.length > offset) { - return result.slice(0, offset); - } } return result; diff --git a/test/unit/utils.test.js b/test/unit/utils.test.js index a31f83f5803..d2d21ccdc16 100644 --- a/test/unit/utils.test.js +++ b/test/unit/utils.test.js @@ -1,5 +1,5 @@ 'use strict'; -const { eachAsync, now, makeInterruptableAsyncInterval, BufferPool } = require('../../src/utils'); +const { eachAsync, now, makeInterruptibleAsyncInterval, BufferPool } = require('../../src/utils'); const { expect } = require('chai'); const sinon = require('sinon'); @@ -35,7 +35,7 @@ describe('utils', function () { }); }); - context('makeInterruptableAsyncInterval', function () { + context('makeInterruptibleAsyncInterval', function () { before(function () { this.clock = sinon.useFakeTimers(); }); @@ -47,7 +47,7 @@ describe('utils', function () { it('should execute a method in an repeating interval', function (done) { let lastTime = now(); const marks = []; - const executor = makeInterruptableAsyncInterval( + const executor = makeInterruptibleAsyncInterval( callback => { marks.push(now() - lastTime); lastTime = now(); @@ -69,7 +69,7 @@ describe('utils', function () { it('should schedule execution sooner if requested within min interval threshold', function (done) { let lastTime = now(); const marks = []; - const executor = makeInterruptableAsyncInterval( + const executor = makeInterruptibleAsyncInterval( callback => { marks.push(now() - lastTime); lastTime = now(); @@ -93,7 +93,7 @@ describe('utils', function () { it('should debounce multiple requests to wake the interval sooner', function (done) { let lastTime = now(); const marks = []; - const executor = makeInterruptableAsyncInterval( + const executor = makeInterruptibleAsyncInterval( callback => { marks.push(now() - lastTime); lastTime = now(); @@ -119,7 +119,7 @@ describe('utils', function () { let clockCalled = 0; let lastTime = now(); const marks = []; - const executor = makeInterruptableAsyncInterval( + const executor = makeInterruptibleAsyncInterval( callback => { marks.push(now() - lastTime); lastTime = now(); @@ -198,7 +198,9 @@ describe('utils', function () { it('across multiple buffers', function () { const buffer = new BufferPool(); - buffer.append(Buffer.from([0, 1, 2, 3, 4, 5])); + buffer.append(Buffer.from([0, 1])); + buffer.append(Buffer.from([2, 3])); + buffer.append(Buffer.from([4, 5])); const data = buffer.peek(6); expect(data).to.eql(Buffer.from([0, 1, 2, 3, 4, 5])); expect(buffer).property('length').to.equal(6); @@ -234,7 +236,9 @@ describe('utils', function () { it('across multiple buffers', function () { const buffer = new BufferPool(); - buffer.append(Buffer.from([0, 1, 2, 3, 4, 5])); + buffer.append(Buffer.from([0, 1])); + buffer.append(Buffer.from([2, 3])); + buffer.append(Buffer.from([4, 5])); const data = buffer.read(6); expect(data).to.eql(Buffer.from([0, 1, 2, 3, 4, 5])); expect(buffer).property('length').to.equal(0);