You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@fastify/multipart

Package Overview
Dependencies
Maintainers
19
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.6.0 to 7.6.1

benchmark/generateId.js

27

examples/example-validation.js

@@ -11,16 +11,21 @@ 'use strict'

fastify.post('/upload/files', {
schema: {
body: {
type: 'object',
required: ['myField'],
properties: {
myField: { $ref: '#mySharedSchema' }
fastify.post(
'/upload/files',
{
schema: {
consumes: ['multipart/form-data'],
body: {
type: 'object',
required: ['myField'],
properties: {
myField: { $ref: '#mySharedSchema' }
}
}
}
},
function (req, reply) {
console.log({ body: req.body })
reply.send('done')
}
}, function (req, reply) {
console.log({ body: req.body })
reply.send('done')
})
)

@@ -27,0 +32,0 @@ fastify.listen({ port: 3000 }, err => {

@@ -36,2 +36,4 @@ import { Busboy, BusboyConfig, BusboyFileStream } from "@fastify/busboy";

tmpUploads: Array<string> | null;
/** This will get populated as soon as a call to `saveRequestFiles` gets resolved. Avoiding any future duplicate work */
savedRequestFiles: Array<fastifyMultipart.SavedMultipartFile> | null;
}

@@ -38,0 +40,0 @@

@@ -10,3 +10,3 @@ 'use strict'

const path = require('path')
const hexoid = require('hexoid')
const { generateId } = require('./lib/generateId')
const util = require('util')

@@ -210,2 +210,3 @@ const createError = require('@fastify/error')

fastify.decorateRequest('tmpUploads', null)
fastify.decorateRequest('savedRequestFiles', null)

@@ -227,4 +228,2 @@ // legacy

const toID = hexoid()
function isMultipart () {

@@ -530,2 +529,6 @@ return this.raw[kMultipart] || false

async function saveRequestFiles (options) {
// Checks if this has already been run
if (this.savedRequestFiles) {
return this.savedRequestFiles
}
let files

@@ -541,11 +544,11 @@ if (attachFieldsToBody === true) {

}
const requestFiles = []
this.savedRequestFiles = []
const tmpdir = (options && options.tmpdir) || os.tmpdir()
this.tmpUploads = []
for await (const file of files) {
const filepath = path.join(tmpdir, toID() + path.extname(file.filename))
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename))
const target = createWriteStream(filepath)
try {
await pump(file.file, target)
requestFiles.push({ ...file, filepath })
this.savedRequestFiles.push({ ...file, filepath })
this.tmpUploads.push(filepath)

@@ -558,3 +561,3 @@ } catch (err) {

return requestFiles
return this.savedRequestFiles
}

@@ -561,0 +564,0 @@

{
"name": "@fastify/multipart",
"version": "7.6.0",
"version": "7.6.1",
"description": "Multipart plugin for Fastify",

@@ -11,5 +11,6 @@ "main": "index.js",

"@fastify/error": "^3.0.0",
"@fastify/swagger": "^8.3.1",
"@fastify/swagger-ui": "^1.8.0",
"end-of-stream": "^1.4.4",
"fastify-plugin": "^4.0.0",
"hexoid": "^1.0.0",
"secure-json-parse": "^2.4.0",

@@ -20,5 +21,6 @@ "stream-wormhole": "^1.1.0"

"@fastify/pre-commit": "^2.0.2",
"@types/node": "^18.0.6",
"@types/node": "^20.1.0",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"benchmark": "^2.1.4",
"climem": "^1.0.3",

@@ -25,0 +27,0 @@ "concat-stream": "^2.0.0",

@@ -300,2 +300,3 @@ # @fastify/multipart

schema: {
consumes: ['multipart/form-data'],
body: {

@@ -335,2 +336,3 @@ type: 'object',

schema: {
consumes: ['multipart/form-data'],
body: {

@@ -377,2 +379,53 @@ type: 'object',

### JSON Schema with Swagger
If you want to use `@fastify/multipart` with `@fastify/swagger` and `@fastify/swagger-ui` you must add a new type called `isFile` and use custom instance of validator compiler [Docs](https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/#validator-compiler).
```js
const ajvFilePlugin = (ajv, options = {}) => {
return ajv.addKeyword({
keyword: "isFile",
compile: (_schema, parent, _it) => {
parent.type = "file";
delete parent.isFile;
return () => true;
},
});
};
const fastify = require('fastify')({
// ...
ajv: {
// add the new ajv plugin
plugins: [/*...*/ ajvFilePlugin]
}
})
const opts = {
attachFieldsToBody: true,
};
fastify.register(require(".."), opts);
fastify.post(
"/upload/files",
{
schema: {
consumes: ["multipart/form-data"],
body: {
type: "object",
required: ["myField"],
properties: {
myField: { isFile: true },
},
},
},
},
function (req, reply) {
console.log({ body: req.body });
reply.send("done");
}
);
```
### JSON Schema non-file field

@@ -439,2 +492,3 @@ When sending fields with the body (`attachFieldsToBody` set to true), the field might look like this in the `request.body`:

schema: {
consumes: ['multipart/form-data'],
body: {

@@ -441,0 +495,0 @@ type: 'object',

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc