New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@athombv/node-stream-trim

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@athombv/node-stream-trim - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3-beta.0

4

dist/lib/index.d.ts

@@ -11,6 +11,6 @@ /// <reference types="node" />

declare type Options = {
start: number | undefined;
end: number | undefined;
start?: number;
end?: number;
};
declare function trimStream(options: Options): TrimStream;
export default trimStream;

@@ -26,2 +26,11 @@ 'use strict';

_this.index = 0;
if (start && start < 0) {
throw new Error('start must be greater than 0');
}
if (end && end < 0) {
throw new Error('end must be greater than 0');
}
if (start && end && start > end) {
throw new Error('end must be lower than start');
}
_this.startByte = start || 0;

@@ -36,5 +45,5 @@ _this.endByte = end || -1;

// We need the last part of this chunk
this.push(chunk.slice(this.startByte - startChunk, Math.min(endChunk, this.endByte) - startChunk));
this.push(chunk.slice(this.startByte - startChunk, this.endByte !== -1 ? (Math.min(endChunk, this.endByte) - startChunk) : endChunk));
}
else if (startChunk >= this.startByte && endChunk <= this.endByte) {
else if (startChunk >= this.startByte && (this.endByte === -1 || endChunk <= this.endByte)) {
// We need the entire chunk

@@ -44,6 +53,6 @@ this.push(chunk);

else if (startChunk < this.endByte && endChunk > this.endByte && startChunk >= this.startByte) {
// We need the first part of this chunk
// We need the first part of this chunk (when endByte is -1 we hit the previous if)
this.push(chunk.slice(0, this.endByte - startChunk));
}
else if (startChunk <= this.startByte && endChunk >= this.endByte) {
else if (startChunk <= this.startByte && (this.endByte !== -1 && endChunk >= this.endByte)) {
// We need the middle part of this chunk

@@ -50,0 +59,0 @@ this.push(chunk.slice(this.startByte - startChunk, this.endByte - (startChunk - this.startByte)));

@@ -61,3 +61,3 @@ 'use strict';

result = Buffer.concat(bufs);
assert.strictEqual(result.toString(), 'st');
assert.equal(result.toString(), 'st');
return [2 /*return*/];

@@ -84,3 +84,3 @@ }

result = Buffer.concat(bufs);
assert(result.toString() === 't');
assert.equal(result.toString(), 't');
return [2 /*return*/];

@@ -115,3 +115,3 @@ }

result = Buffer.concat(bufs);
assert(result.toString() === 'st');
assert.equal(result.toString(), 'st');
return [2 /*return*/];

@@ -144,3 +144,3 @@ }

result = Buffer.concat(bufs);
assert(result.toString() === 'st');
assert.equal(result.toString(), 'st');
return [2 /*return*/];

@@ -167,3 +167,3 @@ }

result = Buffer.concat(bufs);
assert(result.byteLength === 0);
assert.equal(result.byteLength, 0);
return [2 /*return*/];

@@ -196,3 +196,3 @@ }

result = Buffer.concat(bufs);
assert(result.toString() === 'i');
assert.equal(result.toString(), 'i');
return [2 /*return*/];

@@ -203,2 +203,106 @@ }

});
describe('Error handling', function () {
it('should throw an error when start > end', function () {
assert.throws(function () {
(0, index_1.default)({ start: 10, end: 2 });
});
});
it('should throw an error when start or end is negative', function () {
assert.throws(function () {
(0, index_1.default)({ start: -10, end: 2 });
});
assert.throws(function () {
(0, index_1.default)({ start: 10, end: -2 });
});
assert.throws(function () {
(0, index_1.default)({ start: -10, end: -2 });
});
});
});
describe('Unset parameters', function () {
it('should return until the end when end is undefined', function () { return __awaiter(void 0, void 0, void 0, function () {
var stream, bufs, chunk, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stream = (0, index_1.default)({ start: 5 });
bufs = [];
stream.on('data', function (chunk) {
bufs.push(chunk);
});
chunk = Buffer.from('this');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' is');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' a');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' test');
stream._transform(chunk, 'binary', function () { });
stream.end();
return [4 /*yield*/, new Promise(function (fulfill) { return stream.on('finish', fulfill); })];
case 1:
_a.sent();
result = Buffer.concat(bufs);
assert.equal(result.toString(), 'is a test');
return [2 /*return*/];
}
});
}); });
it('should return everything when end and start are undefined', function () { return __awaiter(void 0, void 0, void 0, function () {
var stream, bufs, chunk, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stream = (0, index_1.default)({});
bufs = [];
stream.on('data', function (chunk) {
bufs.push(chunk);
});
chunk = Buffer.from('this');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' is');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' a');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' test');
stream._transform(chunk, 'binary', function () { });
stream.end();
return [4 /*yield*/, new Promise(function (fulfill) { return stream.on('finish', fulfill); })];
case 1:
_a.sent();
result = Buffer.concat(bufs);
assert.equal(result.toString(), 'this is a test');
return [2 /*return*/];
}
});
}); });
it('should return from 0 when start is undefined', function () { return __awaiter(void 0, void 0, void 0, function () {
var stream, bufs, chunk, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stream = (0, index_1.default)({ end: 4 });
bufs = [];
stream.on('data', function (chunk) {
bufs.push(chunk);
});
chunk = Buffer.from('this');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' is');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' a');
stream._transform(chunk, 'binary', function () { });
chunk = Buffer.from(' test');
stream._transform(chunk, 'binary', function () { });
stream.end();
return [4 /*yield*/, new Promise(function (fulfill) { return stream.on('finish', fulfill); })];
case 1:
_a.sent();
result = Buffer.concat(bufs);
assert.equal(result.toString(), 'this');
return [2 /*return*/];
}
});
}); });
});
});

@@ -13,2 +13,11 @@ 'use strict';

super();
if (start && start < 0) {
throw new Error('start must be greater than 0');
}
if (end && end < 0) {
throw new Error('end must be greater than 0');
}
if (start && end && start > end) {
throw new Error('end must be lower than start');
}
this.startByte = start || 0;

@@ -24,10 +33,10 @@ this.endByte = end || -1;

// We need the last part of this chunk
this.push(chunk.slice(this.startByte - startChunk, Math.min(endChunk, this.endByte) - startChunk));
} else if (startChunk >= this.startByte && endChunk <= this.endByte) {
this.push(chunk.slice(this.startByte - startChunk, this.endByte !== -1 ? (Math.min(endChunk, this.endByte) - startChunk) : endChunk));
} else if (startChunk >= this.startByte && (this.endByte === -1 || endChunk <= this.endByte)) {
// We need the entire chunk
this.push(chunk);
} else if (startChunk < this.endByte && endChunk > this.endByte && startChunk >= this.startByte) {
// We need the first part of this chunk
// We need the first part of this chunk (when endByte is -1 we hit the previous if)
this.push(chunk.slice(0, this.endByte - startChunk));
} else if (startChunk <= this.startByte && endChunk >= this.endByte) {
} else if (startChunk <= this.startByte && (this.endByte !== -1 && endChunk >= this.endByte)) {
// We need the middle part of this chunk

@@ -43,4 +52,4 @@ this.push(chunk.slice(this.startByte - startChunk, this.endByte - (startChunk - this.startByte)));

type Options = {
start: number | undefined;
end: number | undefined;
start?: number;
end?: number;
}

@@ -47,0 +56,0 @@

{
"name": "@athombv/node-stream-trim",
"version": "1.0.2",
"version": "1.0.3-beta.0",
"description": "Trims a given portion from an input stream",

@@ -5,0 +5,0 @@ "main": "dist/lib/index.js",

@@ -20,3 +20,3 @@ 'use strict';

const result = Buffer.concat(bufs);
assert.strictEqual(result.toString(), 'st');
assert.equal(result.toString(), 'st');
});

@@ -34,3 +34,3 @@ it('should return first part of chunk', async () => {

const result = Buffer.concat(bufs);
assert(result.toString() === 't');
assert.equal(result.toString(), 't');
});

@@ -57,3 +57,3 @@ });

const result = Buffer.concat(bufs);
assert(result.toString() === 'st');
assert.equal(result.toString(), 'st');
});

@@ -77,3 +77,3 @@ it('should return all it gets if not enough is available', async () => {

const result = Buffer.concat(bufs);
assert(result.toString() === 'st');
assert.equal(result.toString(), 'st');
});

@@ -91,3 +91,3 @@ it('should not return anything if none matches', async () => {

const result = Buffer.concat(bufs);
assert(result.byteLength === 0);
assert.equal(result.byteLength, 0);
});

@@ -111,9 +111,84 @@ it('should return middle part of chunk', async () => {

const result = Buffer.concat(bufs);
assert(result.toString() === 'i');
assert.equal(result.toString(), 'i');
});
});
});
// import transportStream from '../lib/index';
describe('Error handling', () => {
it('should throw an error when start > end', () => {
assert.throws(() => {
trimStream({ start: 10, end: 2 });
});
});
it('should throw an error when start or end is negative', () => {
assert.throws(() => {
trimStream({ start: -10, end: 2 });
});
assert.throws(() => {
trimStream({ start: 10, end: -2 });
});
assert.throws(() => {
trimStream({ start: -10, end: -2 });
});
});
});
// process.stdin.pipe(transportStream({ start: Number.parseInt(process.argv[2], 10), end: Number.parseInt(process.argv[3], 10) })).pipe(process.stdout);
describe('Unset parameters', () => {
it('should return until the end when end is undefined', async () => {
const stream = trimStream({ start: 5 });
const bufs: Array<Buffer> = [];
stream.on('data', (chunk: Buffer) => {
bufs.push(chunk);
});
let chunk = Buffer.from('this');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' is');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' a');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' test');
stream._transform(chunk, 'binary', () => {});
stream.end();
await new Promise(fulfill => stream.on('finish', fulfill));
const result = Buffer.concat(bufs);
assert.equal(result.toString(), 'is a test');
});
it('should return everything when end and start are undefined', async () => {
const stream = trimStream({ });
const bufs: Array<Buffer> = [];
stream.on('data', (chunk: Buffer) => {
bufs.push(chunk);
});
let chunk = Buffer.from('this');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' is');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' a');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' test');
stream._transform(chunk, 'binary', () => {});
stream.end();
await new Promise(fulfill => stream.on('finish', fulfill));
const result = Buffer.concat(bufs);
assert.equal(result.toString(), 'this is a test');
});
it('should return from 0 when start is undefined', async () => {
const stream = trimStream({ end: 4 });
const bufs: Array<Buffer> = [];
stream.on('data', (chunk: Buffer) => {
bufs.push(chunk);
});
let chunk = Buffer.from('this');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' is');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' a');
stream._transform(chunk, 'binary', () => {});
chunk = Buffer.from(' test');
stream._transform(chunk, 'binary', () => {});
stream.end();
await new Promise(fulfill => stream.on('finish', fulfill));
const result = Buffer.concat(bufs);
assert.equal(result.toString(), 'this');
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc