Skip to content
Merged
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ Requirements
Install
=======

npm i @fastify/busboy
```sh
npm i @fastify/busboy
```


Examples
Expand Down Expand Up @@ -201,6 +203,16 @@ Busboy (special) events
* 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.

* **limit**() - Emitted when a file exceeds the configured `fileSize` limit. You can listen on the file stream to handle it:

```js
busboy.on('file', (fieldname, stream) => {
stream.on('limit', () => {
console.log('File size exceeded')
})
})
```

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

* **partsLimit**() - Emitted when specified `parts` limit has been reached. No more 'file' or 'field' events will be emitted.
Expand Down
11 changes: 11 additions & 0 deletions lib/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
// Igor Savin <https://github.com/kibertoad>

/// <reference types="node" />
declare module 'stream' {
interface Readable {
/**
* Emitted when the configured file size limit is reached.
*/
on(event: 'limit', listener: () => void): this;
once(event: 'limit', listener: () => void): this;
addListener(event: 'limit', listener: () => void): this;
removeListener(event: 'limit', listener: () => void): this;
}
}

import * as http from 'node:http';
import { Readable, Writable } from 'node:stream';
Expand Down
39 changes: 39 additions & 0 deletions test/file-stream-limit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

const { PassThrough } = require('stream')
const Busboy = require('../').default
const { test } = require('node:test')
const assert = require('node:assert')

test('BusboyFileStream emits limit', async (t) => {
await new Promise((resolve, reject) => {
const bigPayload = Buffer.alloc(20, 'a')
const boundary = 'foo'
const req = new PassThrough()
req.headers = {
'content-type': `multipart/form-data; boundary=${boundary}`
}

const bb = new Busboy({
headers: req.headers,
limits: { fileSize: 10 }
})

bb.on('file', (_fieldname, stream) => {
stream.on('limit', () => {
assert.ok(true, 'limit event emitted')
resolve()
})
stream.resume()
})

req.pipe(bb)

const delimiter = `--${boundary}`
req.write(`${delimiter}\r\n`)
req.write('Content-Disposition: form-data; name="file"; filename="a.txt"\r\n\r\n')
req.write(bigPayload)
req.write(`\r\n${delimiter}--\r\n`)
req.end()
})
})