@fastify/multipart
Advanced tools
Comparing version 7.3.0 to 7.4.0
83
index.js
@@ -15,3 +15,3 @@ 'use strict' | ||
const deepmergeAll = require('@fastify/deepmerge')({ all: true }) | ||
const { PassThrough, pipeline } = require('stream') | ||
const { PassThrough, pipeline, Readable } = require('stream') | ||
const pump = util.promisify(pipeline) | ||
@@ -24,2 +24,11 @@ const secureJSON = require('secure-json-parse') | ||
const PartsLimitError = createError('FST_PARTS_LIMIT', 'reach parts limit', 413) | ||
const FilesLimitError = createError('FST_FILES_LIMIT', 'reach files limit', 413) | ||
const FieldsLimitError = createError('FST_FIELDS_LIMIT', 'reach fields limit', 413) | ||
const RequestFileTooLargeError = createError('FST_REQ_FILE_TOO_LARGE', 'request file too large, please check multipart config', 413) | ||
const PrototypeViolationError = createError('FST_PROTO_VIOLATION', 'prototype property is not allowed as field name', 400) | ||
const InvalidMultipartContentTypeError = createError('FST_INVALID_MULTIPART_CONTENT_TYPE', 'the request is not multipart', 406) | ||
const InvalidJSONFieldError = createError('FST_INVALID_JSON_FIELD_ERROR', 'a request field is not a valid JSON as declared by its Content-Type', 406) | ||
const FileBufferNotFoundError = createError('FST_FILE_BUFFER_NOT_FOUND', 'the file buffer was not found', 500) | ||
function setMultipart (req, payload, done) { | ||
@@ -106,2 +115,3 @@ // nothing to do, it will be done by the Request.multipart object | ||
function fastifyMultipart (fastify, options, done) { | ||
const attachFieldsToBody = options.attachFieldsToBody | ||
if (options.addToBody === true) { | ||
@@ -175,15 +185,6 @@ if (typeof options.sharedSchemaId === 'string') { | ||
let throwFileSizeLimit = true | ||
if (typeof options.throwFileSizeLimit === 'boolean') { | ||
throwFileSizeLimit = options.throwFileSizeLimit | ||
} | ||
const defaultThrowFileSizeLimit = typeof options.throwFileSizeLimit === 'boolean' | ||
? options.throwFileSizeLimit | ||
: true | ||
const PartsLimitError = createError('FST_PARTS_LIMIT', 'reach parts limit', 413) | ||
const FilesLimitError = createError('FST_FILES_LIMIT', 'reach files limit', 413) | ||
const FieldsLimitError = createError('FST_FIELDS_LIMIT', 'reach fields limit', 413) | ||
const RequestFileTooLargeError = createError('FST_REQ_FILE_TOO_LARGE', 'request file too large, please check multipart config', 413) | ||
const PrototypeViolationError = createError('FST_PROTO_VIOLATION', 'prototype property is not allowed as field name', 400) | ||
const InvalidMultipartContentTypeError = createError('FST_INVALID_MULTIPART_CONTENT_TYPE', 'the request is not multipart', 406) | ||
const InvalidJSONFieldError = createError('FST_INVALID_JSON_FIELD_ERROR', 'a request field is not a valid JSON as declared by its Content-Type', 406) | ||
fastify.decorate('multipartErrors', { | ||
@@ -195,6 +196,7 @@ PartsLimitError, | ||
InvalidMultipartContentTypeError, | ||
RequestFileTooLargeError | ||
RequestFileTooLargeError, | ||
FileBufferNotFoundError | ||
}) | ||
fastify.addContentTypeParser('multipart', setMultipart) | ||
fastify.addContentTypeParser('multipart/form-data', setMultipart) | ||
fastify.decorateRequest(kMultipartHandler, handleMultipart) | ||
@@ -433,5 +435,5 @@ | ||
if (typeof opts.throwFileSizeLimit === 'boolean') { | ||
throwFileSizeLimit = opts.throwFileSizeLimit | ||
} | ||
const throwFileSizeLimit = typeof options.throwFileSizeLimit === 'boolean' | ||
? options.throwFileSizeLimit | ||
: defaultThrowFileSizeLimit | ||
@@ -517,6 +519,10 @@ const value = { | ||
async function saveRequestFiles (options) { | ||
let files | ||
if (attachFieldsToBody === true) { | ||
files = filesFromFields.call(this, this.body) | ||
} else { | ||
files = await this.files(options) | ||
} | ||
const requestFiles = [] | ||
const tmpdir = (options && options.tmpdir) || os.tmpdir() | ||
const files = await this.files(options) | ||
this.tmpUploads = [] | ||
@@ -539,2 +545,25 @@ for await (const file of files) { | ||
function * filesFromFields (container) { | ||
try { | ||
for (const field of Object.values(container)) { | ||
if (Array.isArray(field)) { | ||
for (const subField of filesFromFields.call(this, field)) { | ||
yield subField | ||
} | ||
} | ||
if (!field.file) { | ||
continue | ||
} | ||
if (!field._buf) { | ||
throw new FileBufferNotFoundError() | ||
} | ||
field.file = Readable.from(field._buf) | ||
yield field | ||
} | ||
} catch (err) { | ||
this.log.error({ err }, 'save request file failed') | ||
throw err | ||
} | ||
} | ||
async function cleanRequestFiles () { | ||
@@ -586,7 +615,2 @@ if (!this.tmpUploads) { | ||
const _fastifyMultipart = fp(fastifyMultipart, { | ||
fastify: '4.x', | ||
name: '@fastify/multipart' | ||
}) | ||
/** | ||
@@ -596,4 +620,7 @@ * These export configurations enable JS and TS developers | ||
*/ | ||
module.exports = _fastifyMultipart | ||
module.exports.fastifyMultipart = _fastifyMultipart | ||
module.exports.default = _fastifyMultipart | ||
module.exports = fp(fastifyMultipart, { | ||
fastify: '4.x', | ||
name: '@fastify/multipart' | ||
}) | ||
module.exports.default = fastifyMultipart | ||
module.exports.fastifyMultipart = fastifyMultipart |
{ | ||
"name": "@fastify/multipart", | ||
"version": "7.3.0", | ||
"version": "7.4.0", | ||
"description": "Multipart plugin for Fastify", | ||
@@ -25,3 +25,3 @@ "main": "index.js", | ||
"eslint": "^8.20.0", | ||
"eslint-config-standard-with-typescript": "^23.0.0", | ||
"eslint-config-standard-with-typescript": "^26.0.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
@@ -39,4 +39,14 @@ "eslint-plugin-n": "^15.2.4", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.24.1" | ||
"tsd": "^0.25.0" | ||
}, | ||
"scripts": { | ||
"coverage": "tap \"test/**/*.test.js\" --coverage-report=html", | ||
"climem": "climem 8999 localhost", | ||
"lint": "standard | snazzy", | ||
"lint:fix": "standard --fix", | ||
"start": "CLIMEM=8999 node -r climem ./examples/example", | ||
"test": "npm run lint && npm run unit && npm run typescript", | ||
"typescript": "tsd", | ||
"unit": "tap \"test/**/*.test.js\" -t 90" | ||
}, | ||
"repository": { | ||
@@ -58,13 +68,3 @@ "type": "git", | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"coverage": "tap \"test/**/*.test.js\" --coverage-report=html", | ||
"climem": "climem 8999 localhost", | ||
"lint": "standard | snazzy", | ||
"lint:fix": "standard --fix", | ||
"start": "CLIMEM=8999 node -r climem ./examples/example", | ||
"test": "npm run lint && npm run unit && npm run typescript", | ||
"typescript": "tsd", | ||
"unit": "tap \"test/**/*.test.js\" -t 90" | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
165366
36
4683
20
17