Skip to content

Commit

Permalink
fix(read-body): memory leaks (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
derevnjuk authored Jul 21, 2022
1 parent cf14590 commit f09a690
Show file tree
Hide file tree
Showing 16 changed files with 4,362 additions and 5,659 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/auto-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ on:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 13.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
node-version: 14.x
- run: npm ci
- run: npm run lint
- run: npm run test
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 14
- run: npm ci
- run: npm run semantic-release
env:
Expand Down
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

git update-index --again
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged --relative
8 changes: 4 additions & 4 deletions lib/file-appender.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Strategies = {
'NONE': 'NONE',
'VALUE': 'VALUE',
'ARRAY': 'ARRAY',
'OBJECT': 'OBJECT'
NONE: 'NONE',
VALUE: 'VALUE',
ARRAY: 'ARRAY',
OBJECT: 'OBJECT'
}

exports.Strategies = Strategies
Expand Down
2 changes: 1 addition & 1 deletion lib/file-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FileFilter extends BaseFileFilter {
if (left <= 0) {
throw new MulterError(
Codes.LIMIT_FILE_COUNT,
`The max file uploads exceeded.`,
'The max file uploads exceeded.',
file.fieldName
)
}
Expand Down
10 changes: 5 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class Multer {

_middleware (limits, fields, fileStrategy) {
return createMiddleware(() => ({
fields: fields,
limits: limits,
fields,
limits,
fileFilter: createFileFilter(fields),
fileStrategy: fileStrategy
fileStrategy
}))
}

single (name) {
return this._middleware(this.limits, [{ name: name, maxCount: 1 }], Strategies.VALUE)
return this._middleware(this.limits, [{ name, maxCount: 1 }], Strategies.VALUE)
}

array (name, maxCount) {
return this._middleware(this.limits, [{ name: name, maxCount: maxCount }], Strategies.ARRAY)
return this._middleware(this.limits, [{ name, maxCount }], Strategies.ARRAY)
}

fields (fields) {
Expand Down
14 changes: 12 additions & 2 deletions lib/read-body.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { extname } = require('path')
const Busboy = require('busboy')
const Busboy = require('@fastify/busboy')
const FileType = require('file-type')
const { MulterError, Codes } = require('./error')
const { createWriteStream } = require('fs-temp')
Expand Down Expand Up @@ -34,7 +34,7 @@ const collectFields = (busboy, limits) => new Promise((resolve, reject) => {
))
}

result.push({ key: fieldname, value: value })
result.push({ key: fieldname, value })
})

busboy.once('finish', () => resolve(result))
Expand Down Expand Up @@ -123,6 +123,14 @@ const readBody = async (req, limits, fileFilter) => {
headers: req.headers
})

const requestError = error => {
if (!busboy) {
return
}

busboy.destroy(error)
}

const fields = collectFields(busboy, limits)
const files = collectFiles(busboy, limits, fileFilter)
const guard = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -150,12 +158,14 @@ const readBody = async (req, limits, fileFilter) => {
})

req.pipe(busboy)
req.on('error', requestError)

try {
const result = await Promise.all([fields, files, guard])
return { fields: result[0], files: result[1] }
} catch (err) {
req.unpipe(busboy)
req.removeListener('error', requestError)
busboy.removeAllListeners()
setImmediate(() => req.resume())

Expand Down
Loading

0 comments on commit f09a690

Please sign in to comment.