diff --git a/dist/buffer-peek-stream.d.ts b/dist/buffer-peek-stream.d.ts new file mode 100644 index 0000000..7315d6f --- /dev/null +++ b/dist/buffer-peek-stream.d.ts @@ -0,0 +1,24 @@ +/// +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 | 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; +export default peek; diff --git a/dist/buffer-peek-stream.js b/dist/buffer-peek-stream.js new file mode 100644 index 0000000..f32da6a --- /dev/null +++ b/dist/buffer-peek-stream.js @@ -0,0 +1,97 @@ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.promise = exports.BufferPeekStream = void 0; +var stream_1 = __importDefault(require("stream")); +var BufferPeekStream = (function (_super) { + __extends(BufferPeekStream, _super); + function BufferPeekStream(opts) { + var _this = this; + if (!opts) + opts = {}; + _this = _super.call(this, opts) || this; + opts.highWaterMark = opts.peekBytes || 65536; + stream_1.default.Transform.call(_this, opts); + _this._peekState = { + buffer: [], + bytes: 0, + maxBytes: opts.peekBytes || 65536, + peeked: false, + }; + return _this; + } + BufferPeekStream.prototype._transform = function (chunk, _encoding, callback) { + var _a; + var state = this._peekState; + (_a = state.buffer) === null || _a === void 0 ? void 0 : _a.push(chunk); + state.bytes += chunk.length; + if (state.bytes || 0 >= state.maxBytes) + _peek(this, callback); + else + callback(); + }; + BufferPeekStream.prototype._flush = function (callback) { + if (this._peekState.peeked) + callback(); + else + _peek(this, callback); + }; + return BufferPeekStream; +}(stream_1.default.Transform)); +exports.BufferPeekStream = BufferPeekStream; +function _peek(stream, callback) { + var state = stream._peekState; + if (!state.buffer) + return; + var buffer = Buffer.concat(state.buffer); + stream.emit("peek", buffer.slice(0, state.maxBytes)); + stream.push(buffer); + state.buffer = null; + state.bytes = null; + state.peeked = true; + stream._transform = function (chunk, _enc, callback) { + this.push(chunk); + callback(); + }; + callback(); +} +function peek(source, bytes, callback) { + if (!callback) + return peek(source, undefined, bytes); + var dest = new BufferPeekStream({ peekBytes: bytes }); + dest.once("peek", function (buffer) { + callback(null, buffer, dest); + }); + return source.pipe(dest); +} +function promise(source, bytes) { + return new Promise(function (resolve, reject) { + peek(source, bytes, function (err, buffer, dest) { + if (err) + return reject(err); + resolve([buffer, dest]); + }); + }); +} +exports.promise = promise; +peek.BufferPeekStream = BufferPeekStream; +peek.promise = promise; +exports.default = peek; +//# sourceMappingURL=buffer-peek-stream.js.map \ No newline at end of file diff --git a/dist/buffer-peek-stream.js.map b/dist/buffer-peek-stream.js.map new file mode 100644 index 0000000..0e7e9c7 --- /dev/null +++ b/dist/buffer-peek-stream.js.map @@ -0,0 +1 @@ +{"version":3,"file":"buffer-peek-stream.js","sourceRoot":"","sources":["../src/buffer-peek-stream.ts"],"names":[],"mappings":";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pC,IAAI,GAAG;gBAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;YAC5B,OAAO,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAPD,0BAOC;AAED,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;AACzC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;AAEvB,kBAAe,IAAI,CAAC"} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2989015..dc8c050 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,21 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/concat-stream": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@types/concat-stream/-/concat-stream-1.6.1.tgz", + "integrity": "sha512-eHE4cQPoj6ngxBZMvVf6Hw7Mh4jMW4U9lpGmS5GBPB9RYxlFg+CHaVN7ErNY4W9XfLIEn20b4VDYaIrbq0q4uA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, + "@types/node": { + "version": "17.0.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.8.tgz", + "integrity": "sha512-YofkM6fGv4gDJq78g4j0mMuGMkZVxZDgtU0JRdx6FgiJDG+0fY0GKVolOV8WqVmEhLCXkQRjwDdKyPxJp/uucg==", + "dev": true + }, "@ungap/promise-all-settled": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", @@ -813,6 +828,12 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", "dev": true }, + "typescript": { + "version": "4.5.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.4.tgz", + "integrity": "sha512-VgYs2A2QIRuGphtzFV7aQJduJ2gyfTljngLzjpfW9FoYZF6xuw1W0vW9ghCKLfcWrCFxK81CSGRAvS1pn4fIUg==", + "dev": true + }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index ebf5a4b..bc7c5e6 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -16,7 +18,8 @@ "peek", "parse", "buffer", - "inspect" + "inspect", + "typescript" ], "author": "Sean Garner", "license": "MIT", @@ -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" } } diff --git a/buffer-peek-stream.js b/prev/buffer-peek-stream.js similarity index 100% rename from buffer-peek-stream.js rename to prev/buffer-peek-stream.js diff --git a/src/buffer-peek-stream.ts b/src/buffer-peek-stream.ts new file mode 100644 index 0000000..26c13f4 --- /dev/null +++ b/src/buffer-peek-stream.ts @@ -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 | 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; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..297b9fb --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,102 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Projects */ + // "incremental": true, /* Enable incremental compilation */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "lib": ["es6"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */ + // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + + /* Modules */ + "module": "commonjs", /* Specify what module code is generated. */ + "rootDir": "src", /* Specify the root folder within your source files. */ + "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "resolveJsonModule": true, /* Enable importing .json files */ + // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ + + /* Emit */ + "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + "outDir": "dist", /* Specify an output folder for all emitted files. */ + "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + + /* Interop Constraints */ + "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + + /* Type Checking */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ + "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ + // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + }, + "include": ["./src/**/*"], + "exclude": ["dist"] +}