Skip to content

Commit

Permalink
Migrate to @fastify/busboy (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Dec 5, 2021
1 parent 9bd34c1 commit 94065e3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
12 changes: 6 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as busboy from "busboy";
import { Busboy, BusboyConfig } from "@fastify/busboy";
import { FastifyPluginCallback } from "fastify";
import { Readable } from 'stream';
import { FastifyErrorConstructor } from "fastify-error";
Expand Down Expand Up @@ -54,17 +54,17 @@ declare module "fastify" {
isMultipart: () => boolean;

// promise api
parts: (options?: busboy.BusboyConfig) => AsyncIterableIterator<Multipart>
parts: (options?: BusboyConfig) => AsyncIterableIterator<Multipart>

// legacy
multipart: (handler: MultipartHandler, next: (err: Error) => void, options?: busboy.BusboyConfig) => busboy.Busboy;
multipart: (handler: MultipartHandler, next: (err: Error) => void, options?: BusboyConfig) => Busboy;

// Stream mode
file: (options?: busboy.BusboyConfig) => Promise<Multipart>
files: (options?: busboy.BusboyConfig) => AsyncIterableIterator<Multipart>
file: (options?: BusboyConfig) => Promise<Multipart>
files: (options?: BusboyConfig) => AsyncIterableIterator<Multipart>

// Disk mode
saveRequestFiles: (options?: busboy.BusboyConfig & { tmpdir?: string }) => Promise<Array<Multipart>>
saveRequestFiles: (options?: BusboyConfig & { tmpdir?: string }) => Promise<Array<Multipart>>
cleanRequestFiles: () => Promise<void>
tmpUploads: Array<Multipart>
}
Expand Down
7 changes: 1 addition & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Busboy = require('busboy')
const Busboy = require('@fastify/busboy')
const os = require('os')
const fp = require('fastify-plugin')
const eos = require('end-of-stream')
Expand Down Expand Up @@ -348,11 +348,6 @@ function fastifyMultipart (fastify, options, done) {
onError(new FieldsLimitError())
})

// TODO: Won't need after https://github.com/mscdex/busboy/pull/237/files
if (bb._writableState.autoDestroy) {
bb._writableState.autoDestroy = false
}

request.pipe(bb)

function onField (name, fieldValue, fieldnameTruncated, valueTruncated, encoding, contentType) {
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"busboy": "^0.3.1",
"@fastify/busboy": "^1.0.0",
"deepmerge": "^4.2.2",
"end-of-stream": "^1.4.4",
"fastify-error": "^0.3.0",
Expand All @@ -15,9 +15,8 @@
"stream-wormhole": "^1.1.0"
},
"devDependencies": {
"@types/busboy": "^0.2.3",
"@types/node": "^16.0.0",
"@typescript-eslint/parser": "^4.0.0",
"@types/node": "^16.11.11",
"@typescript-eslint/parser": "^4.33.0",
"climem": "^1.0.3",
"eslint": "^7.7.0",
"eslint-config-standard": "^16.0.0",
Expand All @@ -26,7 +25,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-typescript": "^0.14.0",
"fastify": "^3.2.1",
"fastify": "^3.24.1",
"form-data": "^4.0.0",
"h2url": "^0.2.0",
"pre-commit": "^1.2.2",
Expand All @@ -36,7 +35,7 @@
"standard": "^16.0.1",
"tap": "^15.0.1",
"tsd": "^0.19.0",
"typescript": "^4.0.2"
"typescript": "^4.5.2"
},
"scripts": {
"coverage": "tap \"test/**/*.test.js\" --coverage-report=html",
Expand Down
6 changes: 4 additions & 2 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { pipeline, Readable } from 'stream'
import * as fs from 'fs'
import { expectError, expectType } from 'tsd'
import { FastifyErrorConstructor } from "fastify-error"
import { BusboyConfig } from "@fastify/busboy";

const pump = util.promisify(pipeline)

Expand Down Expand Up @@ -45,6 +46,7 @@ const runServer = async () => {
}, (err) => {
throw err
}, {
headers: { 'content-type': 'multipart/form-data' },
limits: {
fileSize: 10000
}
Expand Down Expand Up @@ -88,7 +90,7 @@ const runServer = async () => {

// busboy
app.post('/', async function (req, reply) {
const options: busboy.BusboyConfig = { limits: { fileSize: 1000 } };
const options: BusboyConfig = { headers: { 'content-type': 'multipart/form-data' }, limits: { fileSize: 1000 } };
const data = await req.file(options)
await pump(data.file, fs.createWriteStream(data.filename))
reply.send()
Expand Down Expand Up @@ -140,7 +142,7 @@ const runServer = async () => {

// upload files to disk with busboy options
app.post('/upload/files', async function (req, reply) {
const options: busboy.BusboyConfig = { limits: { fileSize: 1000 } };
const options: BusboyConfig = { headers: { 'content-type': 'multipart/form-data' }, limits: { fileSize: 1000 } };
await req.saveRequestFiles(options)

reply.send()
Expand Down

0 comments on commit 94065e3

Please sign in to comment.