Skip to content

Commit

Permalink
feat: support node 16+ (#23)
Browse files Browse the repository at this point in the history
Migrates to `fs-temp@2` to resolve LinusU/fs-temp#5

Co-authored-by: Artem Derevnjuk <[email protected]>
  • Loading branch information
denis-maiorov-brightsec and derevnjuk authored Aug 4, 2023
1 parent 85be5f9 commit d8b39e5
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 54 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/auto-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ on:
- master

jobs:
build:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 14.x
node-version: 18
cache: npm
- run: npm ci
- run: npm run lint
test:
strategy:
matrix:
node: [ 14, 16, 18 ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm run test
8 changes: 5 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 14
node-version: 18
cache: npm
- run: npm ci
- run: npm run semantic-release
env:
Expand Down
13 changes: 9 additions & 4 deletions lib/read-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { extname } = require('path')
const Busboy = require('@fastify/busboy')
const FileType = require('file-type')
const { MulterError, Codes } = require('./error')
const { createWriteStream } = require('fs-temp')
const { createReadStream } = require('fs')
const stream = require('stream')
const { promisify } = require('util')
Expand Down Expand Up @@ -40,10 +39,12 @@ const collectFields = (busboy, limits) => new Promise((resolve, reject) => {
busboy.once('finish', () => resolve(result))
})

let createWriteStream

const collectFiles = (busboy, limits, fileFilter) => new Promise((resolve, reject) => {
const fileTasks = []

busboy.on('file', (fieldname, fileStream, filename, encoding, mimetype) => {
busboy.on('file', async (fieldname, fileStream, filename, encoding, mimetype) => {
if (
typeof limits.fieldNameSize === 'number' &&
fieldname.length > limits.fieldNameSize
Expand Down Expand Up @@ -81,6 +82,10 @@ const collectFiles = (busboy, limits, fileFilter) => new Promise((resolve, rejec
return reject(err)
}

if (!createWriteStream) {
({ createWriteStream } = await import('fs-temp'))
}

const target = createWriteStream()

const fileTask = FileType.stream(fileStream)
Expand Down Expand Up @@ -134,7 +139,7 @@ const readBody = async (req, limits, fileFilter) => {
const fields = collectFields(busboy, limits)
const files = collectFiles(busboy, limits, fileFilter)
const guard = new Promise((resolve, reject) => {
req.on('error', reject)
req.once('error', reject)
busboy.once('error', reject)

req.once('aborted', () => reject(new MulterError(
Expand All @@ -158,7 +163,7 @@ const readBody = async (req, limits, fileFilter) => {
})

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

try {
const result = await Promise.all([fields, files, guard])
Expand Down
63 changes: 27 additions & 36 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@fastify/busboy": "^1.1.0",
"append-field": "^1.0.0",
"file-type": "^16.5.3",
"fs-temp": "^1.2.1",
"fs-temp": "^2.0.1",
"on-finished": "^2.4.1",
"type-is": "^1.6.18"
},
Expand Down
8 changes: 5 additions & 3 deletions test/_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ exports.submitForm = async (multer, form) => {
const req = new stream.PassThrough()

req.complete = false
form.once('end', () => { req.complete = true })

form.pipe(req)
req.headers = {
'content-type': `multipart/form-data; boundary=${form.getBoundary()}`,
'content-length': length
}

form
.once('end', () => { req.complete = true })
.once('error', err => req.emit('error', err))
.pipe(req)

await pify(multer)(req, null)
await onFinished(req)

Expand Down
9 changes: 6 additions & 3 deletions test/misc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */

const assert = require('assert')
const { PassThrough } = require('stream')
const util = require('./_util')
const { Multer } = require('../lib')
const FormData = require('form-data')
Expand All @@ -26,10 +27,12 @@ describe('Misc', () => {
const stream = util.file('small')

// Don't let FormData figure out a filename
delete stream.fd
delete stream.path
const hidden = new PassThrough()
stream
.once('error', err => hidden.emit('error', err))
.pipe(hidden)

form.append('file', stream, { knownLength: util.knownFileLength('small') })
form.append('file', hidden, { knownLength: util.knownFileLength('small') })

const req = await util.submitForm(parser, form)
assert.strictEqual(req.file.originalName, undefined)
Expand Down

0 comments on commit d8b39e5

Please sign in to comment.