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

add bytesRead to FileStream #51

Merged
merged 8 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Major changes since the last busboy release (0.31):
* Error on non-number limit rather than ignoring (#7)
* Dicer is now part of the busboy itself and not an external dependency (#14)
* Tests were converted to Mocha (#11, #12, #22, #23)
* FileStreams also provide the property `bytesRead`
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Busboy (special) events
* **file**(< _string_ >fieldname, < _ReadableStream_ >stream, < _string_ >filename, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new file form field found. `transferEncoding` contains the 'Content-Transfer-Encoding' value for the file stream. `mimeType` contains the 'Content-Type' value for the file stream.
* Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits).
* If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
* The property `bytesRead` informs about the number of bytes that have been read so far.

* **field**(< _string_ >fieldname, < _string_ >value, < _boolean_ >fieldnameTruncated, < _boolean_ >valueTruncated, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new non-file field found.

Expand Down
19 changes: 15 additions & 4 deletions lib/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <reference types="node" />

import * as http from 'http';
import {Readable, Writable} from 'stream';
import { Readable, Writable } from 'stream';

declare const busboy: BusboyConstructor;
export default busboy
Expand Down Expand Up @@ -39,7 +39,7 @@ export interface BusboyConfig {
* Various limits on incoming data.
*/
limits?:
| {
| {
/**
* Max field name size (in bytes)
* @default 100 bytes
Expand Down Expand Up @@ -76,11 +76,22 @@ export interface BusboyConfig {
*/
headerPairs?: number | undefined;
}
| undefined;
| undefined;
}

export type BusboyHeaders = { 'content-type': string } & http.IncomingHttpHeaders;

export interface BusboyFileStream extends
Readable {

truncated: boolean;

/**
* The number of bytes that have been read so far.
*/
bytesRead: number;
}

export interface Busboy extends Writable {
addListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;

Expand Down Expand Up @@ -128,7 +139,7 @@ export interface BusboyEvents {
*/
file: (
fieldname: string,
stream: Readable,
stream: BusboyFileStream,
filename: string,
transferEncoding: string,
mimeType: string,
Expand Down
8 changes: 6 additions & 2 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function Multipart (boy, cfg) {

for (i = 0, len = parsedConType.length; i < len; ++i) {
if (Array.isArray(parsedConType[i]) &&
RE_BOUNDARY.test(parsedConType[i][0])) {
RE_BOUNDARY.test(parsedConType[i][0])) {
boundary = parsedConType[i][1]
break
}
Expand Down Expand Up @@ -199,6 +199,8 @@ function Multipart (boy, cfg) {
file.truncated = true
part.removeAllListeners('data')
} else if (!file.push(data)) { self._pause = true }

file.bytesRead = nsize > fileSizeLimit ? fileSizeLimit : nsize
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
}

onEnd = function () {
Expand Down Expand Up @@ -285,10 +287,12 @@ function FileStream (opts) {
if (!(this instanceof FileStream)) { return new FileStream(opts) }
ReadableStream.call(this, opts)

this.bytesRead = 0

this.truncated = false
}
inherits(FileStream, ReadableStream)

FileStream.prototype._read = function (n) {}
FileStream.prototype._read = function (n) { }

module.exports = Multipart
2 changes: 2 additions & 0 deletions test/types-multipart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ describe('types-multipart', () => {
++info[3]
}).on('end', function () {
info[2] = nb
assert(typeof (stream.bytesRead) === 'number', 'file.bytesRead is missing')
assert(stream.bytesRead === nb, 'file.bytesRead is not equal to filesize')
if (stream.truncated) { ++info[3] }
})
})
Expand Down
17 changes: 8 additions & 9 deletions test/types/main.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BusboyDefault, { BusboyConstructor, BusboyConfig, BusboyHeaders, Busboy, BusboyEvents } from '../..';
import BusboyDefault, { BusboyConstructor, BusboyConfig, BusboyHeaders, Busboy, BusboyEvents, BusboyFileStream } from '../..';
import {expectError, expectType} from "tsd";
import {Readable} from "stream";

// test type exports
type Constructor = BusboyConstructor;
Expand All @@ -26,7 +25,7 @@ new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { headerPairs: 2

busboy.addListener('file', (fieldname, file, filename, encoding, mimetype) => {
expectType<string> (fieldname)
expectType<Readable>(file);
expectType<BusboyFileStream>(file);
expectType<string>(filename);
expectType<string>(encoding);
expectType<string>(mimetype);
Expand Down Expand Up @@ -56,7 +55,7 @@ busboy.on(Symbol('foo'), foo => {

busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
expectType<string> (fieldname);
expectType<Readable> (file);
expectType<BusboyFileStream> (file);
expectType<string> (filename);
expectType<string> (encoding);
expectType<string> (mimetype);
Expand Down Expand Up @@ -86,7 +85,7 @@ busboy.on(Symbol('foo'), foo => {

busboy.once('file', (fieldname, file, filename, encoding, mimetype) => {
expectType<string> (fieldname);
expectType<Readable> (file);
expectType<BusboyFileStream> (file);
expectType<string> (filename);
expectType<string> (encoding);
expectType<string> (mimetype);
Expand Down Expand Up @@ -116,7 +115,7 @@ busboy.once(Symbol('foo'), foo => {

busboy.removeListener('file', (fieldname, file, filename, encoding, mimetype) => {
expectType<string> (fieldname);
expectType<Readable> (file);
expectType<BusboyFileStream> (file);
expectType<string> (filename);
expectType<string> (encoding);
expectType<string> (mimetype);
Expand Down Expand Up @@ -146,7 +145,7 @@ busboy.removeListener(Symbol('foo'), foo => {

busboy.off('file', (fieldname, file, filename, encoding, mimetype) => {
expectType<string> (fieldname);
expectType<Readable> (file);
expectType<BusboyFileStream> (file);
expectType<string> (filename);
expectType<string> (encoding);
expectType<string> (mimetype);
Expand Down Expand Up @@ -176,7 +175,7 @@ busboy.off(Symbol('foo'), foo => {

busboy.prependListener('file', (fieldname, file, filename, encoding, mimetype) => {
expectType<string> (fieldname);
expectType<Readable> (file);
expectType<BusboyFileStream> (file);
expectType<string> (filename);
expectType<string> (encoding);
expectType<string> (mimetype);
Expand Down Expand Up @@ -206,7 +205,7 @@ busboy.prependListener(Symbol('foo'), foo => {

busboy.prependOnceListener('file', (fieldname, file, filename, encoding, mimetype) => {
expectType<string> (fieldname);
expectType<Readable> (file);
expectType<BusboyFileStream> (file);
expectType<string> (filename);
expectType<string> (encoding);
expectType<string> (mimetype);
Expand Down