Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Dec 9, 2020
1 parent 31d2b36 commit 6c66b4a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
15 changes: 6 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 12 additions & 8 deletions test/unit/utils.test.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -35,7 +35,7 @@ describe('utils', function () {
});
});

context('makeInterruptableAsyncInterval', function () {
context('makeInterruptibleAsyncInterval', function () {
before(function () {
this.clock = sinon.useFakeTimers();
});
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6c66b4a

Please sign in to comment.