Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor to typescript #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions dist/buffer-peek-stream.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference types="node" />
import { ReadStream } from "fs";
import stream, { TransformCallback, TransformOptions } from "stream";
interface IOpts extends TransformOptions {
peekBytes?: number;
}
export declare class BufferPeekStream extends stream.Transform {
_peekState: {
buffer: Array<any> | null;
bytes: number | null;
maxBytes: number;
peeked: boolean;
};
constructor(opts?: IOpts);
_transform(chunk: any, _encoding: BufferEncoding, callback: TransformCallback): void;
_flush(callback: TransformCallback): void;
}
declare function peek(source: ReadStream, bytes?: number, callback?: (err: any, data: Buffer, outputStream: BufferPeekStream) => void): BufferPeekStream;
declare namespace peek {
var BufferPeekStream: typeof import("./buffer-peek-stream").BufferPeekStream;
var promise: typeof import("./buffer-peek-stream").promise;
}
export declare function promise(source: ReadStream, bytes: number): Promise<unknown>;
export default peek;
97 changes: 97 additions & 0 deletions dist/buffer-peek-stream.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/buffer-peek-stream.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "buffer-peek-stream",
"version": "1.1.0",
"description": "Transform stream that lets you inspect the start of a readable stream before deciding what to do with it",
"main": "buffer-peek-stream.js",
"main": "dist/buffer-peek-stream.js",
"typings": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "node_modules/mocha/bin/mocha test.js"
},
"repository": {
Expand All @@ -16,7 +18,8 @@
"peek",
"parse",
"buffer",
"inspect"
"inspect",
"typescript"
],
"author": "Sean Garner",
"license": "MIT",
Expand All @@ -25,11 +28,14 @@
},
"homepage": "https://github.com/seangarner/node-buffer-peek-stream",
"devDependencies": {
"@types/concat-stream": "^1.6.1",
"@types/node": "^17.0.8",
"chai": "^4.3.4",
"concat-stream": "^1.4.6",
"dev-null-stream": "0.0.1",
"mocha": "^9.0.2",
"randstream": "^0.3.2",
"truncate-stream": "^1.0.1"
"truncate-stream": "^1.0.1",
"typescript": "^4.5.4"
}
}
File renamed without changes.
110 changes: 110 additions & 0 deletions src/buffer-peek-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { ReadStream } from "fs";
import stream, { TransformCallback, TransformOptions } from "stream";

interface IOpts extends TransformOptions {
peekBytes?: number;
}

export class BufferPeekStream extends stream.Transform {
_peekState: {
buffer: Array<any> | null;
bytes: number | null;
maxBytes: number;
peeked: boolean;
};

constructor(opts?: IOpts) {
if (!opts) opts = {};

super(opts);

opts.highWaterMark = opts.peekBytes || 65536;

stream.Transform.call(this, opts);

this._peekState = {
buffer: [],
bytes: 0,
maxBytes: opts.peekBytes || 65536,
peeked: false,
};
}

_transform(
chunk: any,
_encoding: BufferEncoding,
callback: TransformCallback
) {
const state = this._peekState;

// buffer incoming chunks until we have enough for our peek
state.buffer?.push(chunk);
state.bytes += chunk.length;

// get more?
if (state.bytes || 0 >= state.maxBytes) _peek(this, callback);
else callback();
}

_flush(callback: TransformCallback) {
if (this._peekState.peeked) callback();
else _peek(this, callback);
}
}

function _peek(stream: BufferPeekStream, callback: TransformCallback) {
const state = stream._peekState;

if (!state.buffer) return;
const buffer = Buffer.concat(state.buffer);

// emit exactly the number of bytes we wanted to peek
stream.emit("peek", buffer.slice(0, state.maxBytes));

stream.push(buffer);

state.buffer = null;
state.bytes = null;
state.peeked = true;

stream._transform = function (
chunk: any,
_enc: BufferEncoding,
callback: TransformCallback
) {
this.push(chunk);
callback();
};

callback();
}

function peek(
source: ReadStream,
bytes?: number,
callback?: (err: any, data: Buffer, outputStream: BufferPeekStream) => void
): BufferPeekStream {
if (!callback) return peek(source, undefined, bytes as any);

var dest = new BufferPeekStream({ peekBytes: bytes });

dest.once("peek", function (buffer: Buffer) {
callback(null, buffer, dest);
});

return source.pipe(dest);
}

export function promise(source: ReadStream, bytes: number) {
return new Promise((resolve, reject) => {
peek(source, bytes, (err, buffer, dest) => {
if (err) return reject(err);
resolve([buffer, dest]);
});
});
}

peek.BufferPeekStream = BufferPeekStream;
peek.promise = promise;

export default peek;
Loading