Socket
Socket
Sign inDemoInstall

fastify-multer

Package Overview
Dependencies
23
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.5.4 to 2.0.0

14

lib/index.d.ts

@@ -1,4 +0,2 @@

/// <reference types="node" />
import { IncomingMessage, Server, ServerResponse } from 'http';
import { FastifyMiddleware } from 'fastify';
import { preHandlerHookHandler } from 'fastify';
import diskStorage from './storage/disk';

@@ -17,7 +15,7 @@ import memoryStorage from './storage/memory';

private _makePreHandler;
single(name: string): FastifyMiddleware<Server, IncomingMessage, ServerResponse>;
array(name: string, maxCount?: number): FastifyMiddleware<Server, IncomingMessage, ServerResponse>;
fields(fields: Field[]): FastifyMiddleware<Server, IncomingMessage, ServerResponse>;
none(): FastifyMiddleware<Server, IncomingMessage, ServerResponse>;
any(): FastifyMiddleware<Server, IncomingMessage, ServerResponse>;
single(name: string): preHandlerHookHandler;
array(name: string, maxCount?: number): preHandlerHookHandler;
fields(fields: Field[]): preHandlerHookHandler;
none(): preHandlerHookHandler;
any(): preHandlerHookHandler;
}

@@ -24,0 +22,0 @@ interface MulterFactory {

"use strict";
const make_prehandler_1 = require("./lib/make-prehandler");
const disk_1 = require("./storage/disk");
const memory_1 = require("./storage/memory");
const multer_error_1 = require("./lib/multer-error");
const content_parser_1 = require("./lib/content-parser");
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const make_prehandler_1 = __importDefault(require("./lib/make-prehandler"));
const disk_1 = __importDefault(require("./storage/disk"));
const memory_1 = __importDefault(require("./storage/memory"));
const multer_error_1 = __importDefault(require("./lib/multer-error"));
const content_parser_1 = __importDefault(require("./lib/content-parser"));
function allowAll(req, file, cb) {

@@ -8,0 +11,0 @@ cb(null, true);

/// <reference types="node" />
import { IncomingMessage } from 'http';
import { FastifyRequest } from 'fastify';

@@ -25,3 +24,3 @@ import { Strategy } from './lib/file-appender';

export declare type FileFilterCallback = (error: Error | null, acceptFile?: boolean) => void;
export declare type FileFilter = (req: FastifyRequest<IncomingMessage>, file: File, callback: FileFilterCallback) => void;
export declare type FileFilter = (req: FastifyRequest, file: File, callback: FileFilterCallback) => void;
export interface Options {

@@ -43,7 +42,7 @@ dest?: string;

export interface StorageEngine {
_handleFile(req: FastifyRequest<IncomingMessage>, file: File, callback: (error?: Error | null, info?: Partial<File>) => void): void;
_removeFile(req: FastifyRequest<IncomingMessage>, file: File, callback: (error?: Error | null) => void): void;
_handleFile(req: FastifyRequest, file: File, callback: (error?: Error | null, info?: Partial<File>) => void): void;
_removeFile(req: FastifyRequest, file: File, callback: (error?: Error | null) => void): void;
}
export declare type GetFileName = (req: FastifyRequest<IncomingMessage>, file: File, callback: (error: Error | null, filename?: string) => void) => void;
export declare type GetDestination = (req: FastifyRequest<IncomingMessage>, file: File, callback: (error: Error | null, destination: string) => void) => void;
export declare type GetFileName = (req: FastifyRequest, file: File, callback: (error: Error | null, filename?: string) => void) => void;
export declare type GetDestination = (req: FastifyRequest, file: File, callback: (error: Error | null, destination: string) => void) => void;
export interface DiskStorageOptions {

@@ -50,0 +49,0 @@ destination?: string | GetDestination;

@@ -1,8 +0,6 @@

/// <reference path="../../typings/fastify/index.d.ts" />
/// <reference types="node" />
import { IncomingMessage } from 'http';
import fp = require('fastify-plugin');
import { FastifyInstance, FastifyRequest } from 'fastify';
export declare function isMultipart(this: FastifyRequest<IncomingMessage>): boolean;
declare const multer: (instance: FastifyInstance<import("http").Server, IncomingMessage, import("http").ServerResponse>, options: fp.PluginOptions, callback: (err?: import("fastify").FastifyError | undefined) => void) => void;
import { PluginOptions } from 'fastify-plugin';
import { FastifyRequest } from 'fastify';
export declare function isMultipart(this: FastifyRequest): boolean;
declare const multer: import("fastify").FastifyPluginCallback<PluginOptions, import("http").Server>;
export default multer;
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fp = require("fastify-plugin");
exports.isMultipart = void 0;
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
const kMultipart = Symbol('multipart');
function setMultipart(req, done) {
function setMultipart(req, _payload, done) {
;

@@ -11,6 +15,6 @@ req[kMultipart] = true;

function isMultipart() {
return this.req[kMultipart] || false;
return this.raw[kMultipart] || false;
}
exports.isMultipart = isMultipart;
function fastifyMulter(fastify, options, next) {
function fastifyMulter(fastify, _options, next) {
fastify.addContentTypeParser('multipart', setMultipart);

@@ -20,4 +24,4 @@ fastify.decorateRequest('isMultipart', isMultipart);

}
const multer = fp(fastifyMulter, {
fastify: '>= 2.0.0',
const multer = fastify_plugin_1.default(fastifyMulter, {
fastify: '>= 3.0.0',
name: 'fastify-multer',

@@ -24,0 +28,0 @@ });

@@ -1,7 +0,4 @@

/// <reference path="../../typings/fastify/index.d.ts" />
/// <reference types="node" />
import { ServerResponse } from 'http';
import { FastifyReply, FastifyRequest } from 'fastify';
import { Setup } from '../interfaces';
declare function makePreHandler(setup: Setup): (request: FastifyRequest<import("http").IncomingMessage, import("fastify").DefaultQuery, import("fastify").DefaultParams, import("fastify").DefaultHeaders, any>, _: FastifyReply<ServerResponse>, next: (err?: Error | undefined) => void) => void;
declare function makePreHandler(setup: Setup): (request: FastifyRequest, _: FastifyReply, next: (err?: Error | undefined) => void) => void;
export default makePreHandler;
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const is = require("type-is");
const Busboy = require("busboy");
const extend = require("xtend");
const onFinished = require("on-finished");
const appendField = require("append-field");
const counter_1 = require("./counter");
const multer_error_1 = require("./multer-error");
const file_appender_1 = require("./file-appender");
const remove_uploaded_files_1 = require("./remove-uploaded-files");
const type_is_1 = __importDefault(require("type-is"));
const busboy_1 = __importDefault(require("busboy"));
const xtend_1 = __importDefault(require("xtend"));
const on_finished_1 = __importDefault(require("on-finished"));
const append_field_1 = __importDefault(require("append-field"));
const counter_1 = __importDefault(require("./counter"));
const multer_error_1 = __importDefault(require("./multer-error"));
const file_appender_1 = __importDefault(require("./file-appender"));
const remove_uploaded_files_1 = __importDefault(require("./remove-uploaded-files"));
function drainStream(stream) {

@@ -17,3 +20,4 @@ stream.on('readable', stream.read.bind(stream));

return function multerPreHandler(request, _, next) {
if (!is(request.req, ['multipart'])) {
const rawRequest = request.raw || request.req;
if (!type_is_1.default(rawRequest, ['multipart'])) {
return next();

@@ -30,4 +34,4 @@ }

try {
busboy = new Busboy({
headers: request.req.headers,
busboy = new busboy_1.default({
headers: rawRequest.headers,
limits: limits,

@@ -51,6 +55,6 @@ preservePath: preservePath,

isDone = true;
request.req.unpipe(busboy);
drainStream(request.req);
rawRequest.unpipe(busboy);
drainStream(rawRequest);
busboy.removeAllListeners();
onFinished(request.req, function () {
on_finished_1.default(rawRequest, function () {
next(err);

@@ -97,3 +101,3 @@ });

}
appendField(request.body, fieldname, value);
append_field_1.default(request.body, fieldname, value);
});

@@ -143,3 +147,3 @@ busboy.on('file', function (fieldname, fileStream, filename, encoding, mimetype) {

appender.removePlaceholder(placeholder);
uploadedFiles.push(extend(file, info));
uploadedFiles.push(xtend_1.default(file, info));
return pendingWrites.decrement();

@@ -152,3 +156,3 @@ }

}
const fileInfo = extend(file, info);
const fileInfo = xtend_1.default(file, info);
appender.replacePlaceholder(placeholder, fileInfo);

@@ -177,3 +181,3 @@ uploadedFiles.push(fileInfo);

});
request.req.pipe(busboy);
rawRequest.pipe(busboy);
};

@@ -180,0 +184,0 @@ }

@@ -1,3 +0,1 @@

/// <reference types="node" />
import { IncomingMessage } from 'http';
import { FastifyRequest } from 'fastify';

@@ -9,6 +7,6 @@ import { GetFileName, GetDestination, DiskStorageOptions, File, StorageEngine } from '../interfaces';

constructor(opts: DiskStorageOptions);
_handleFile(req: FastifyRequest<IncomingMessage>, file: File, cb: (error: Error | null, info?: Partial<File>) => void): void;
_removeFile(req: FastifyRequest<IncomingMessage>, file: File, cb: (error?: Error | null) => void): void;
_handleFile(req: FastifyRequest, file: File, cb: (error: Error | null, info?: Partial<File>) => void): void;
_removeFile(req: FastifyRequest, file: File, cb: (error?: Error | null) => void): void;
}
declare const _default: (opts: DiskStorageOptions) => DiskStorage;
export default _default;
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const os = require("os");
const os_1 = __importDefault(require("os"));
const path_1 = require("path");
const crypto = require("crypto");
const mkdirp = require("mkdirp");
const crypto_1 = __importDefault(require("crypto"));
const mkdirp_1 = __importDefault(require("mkdirp"));
const getFilename = (req, file, cb) => {
crypto.randomBytes(16, function (err, raw) {
crypto_1.default.randomBytes(16, function (err, raw) {
cb(err, err ? undefined : raw.toString('hex'));

@@ -14,3 +17,3 @@ });

const getDestination = (req, file, cb) => {
cb(null, os.tmpdir());
cb(null, os_1.default.tmpdir());
};

@@ -21,3 +24,3 @@ class DiskStorage {

if (typeof opts.destination === 'string') {
mkdirp.sync(opts.destination);
mkdirp_1.default.sync(opts.destination);
this.getDestination = function ($0, $1, cb) {

@@ -24,0 +27,0 @@ cb(null, opts.destination);

@@ -1,10 +0,8 @@

/// <reference types="node" />
import { IncomingMessage } from 'http';
import { FastifyRequest } from 'fastify';
import { StorageEngine, File } from '../interfaces';
declare class MemoryStorage implements StorageEngine {
_handleFile(req: FastifyRequest<IncomingMessage>, file: File, cb: (error: Error | null, info?: Partial<File>) => void): void;
_removeFile(req: FastifyRequest<IncomingMessage>, file: File, cb: (error?: Error) => void): void;
_handleFile(req: FastifyRequest, file: File, cb: (error: Error | null, info?: Partial<File>) => void): void;
_removeFile(req: FastifyRequest, file: File, cb: (error?: Error) => void): void;
}
declare const _default: () => MemoryStorage;
export default _default;
{
"name": "fastify-multer",
"description": "Fastify plugin for handling `multipart/form-data`.",
"version": "1.5.4",
"version": "2.0.0",
"main": "lib/index.js",

@@ -28,3 +28,3 @@ "types": "lib/",

"concat-stream": "^2.0.0",
"fastify-plugin": "~1.6.0",
"fastify-plugin": "^2.0.1",
"mkdirp": "^0.5.1",

@@ -44,3 +44,3 @@ "on-finished": "^2.3.0",

"@types/xtend": "^4.0.2",
"fastify": "^2.9.0",
"fastify": "^3.0.2",
"form-data": "^2.5.1",

@@ -47,0 +47,0 @@ "fs-temp": "^1.1.2",

@@ -14,2 +14,5 @@ ![Logo](https://user-images.githubusercontent.com/6388707/66124653-463a2d00-e5e5-11e9-8fed-b5bca26b66ea.png)

## Version Note
**fastify-multer 2.x.x version supports Fastify >= 3.0.0. For Fastify < 3.0.0 use fastify-multer 1.x.x version**
## Installation

@@ -16,0 +19,0 @@

@@ -1,3 +0,2 @@

import { IncomingMessage, Server, ServerResponse } from 'http'
import { FastifyRequest, FastifyMiddleware } from 'fastify'
import { FastifyRequest, preHandlerHookHandler } from 'fastify'
import makePreHandler from './lib/make-prehandler'

@@ -20,3 +19,3 @@ import diskStorage from './storage/disk'

function allowAll(req: FastifyRequest<IncomingMessage>, file: File, cb: FileFilterCallback) {
function allowAll(req: FastifyRequest, file: File, cb: FileFilterCallback) {
cb(null, true)

@@ -60,7 +59,3 @@ }

function wrappedFileFilter(
req: FastifyRequest<IncomingMessage>,
file: File,
cb: FileFilterCallback,
) {
function wrappedFileFilter(req: FastifyRequest, file: File, cb: FileFilterCallback) {
if ((filesLeft[file.fieldname] || 0) <= 0) {

@@ -86,22 +81,19 @@ return cb(new MulterError('LIMIT_UNEXPECTED_FILE', file.fieldname))

single(name: string): FastifyMiddleware<Server, IncomingMessage, ServerResponse> {
single(name: string): preHandlerHookHandler {
return this._makePreHandler([{ name, maxCount: 1 }], 'VALUE')
}
array(
name: string,
maxCount?: number,
): FastifyMiddleware<Server, IncomingMessage, ServerResponse> {
array(name: string, maxCount?: number): preHandlerHookHandler {
return this._makePreHandler([{ name, maxCount }], 'ARRAY')
}
fields(fields: Field[]): FastifyMiddleware<Server, IncomingMessage, ServerResponse> {
fields(fields: Field[]): preHandlerHookHandler {
return this._makePreHandler(fields, 'OBJECT')
}
none(): FastifyMiddleware<Server, IncomingMessage, ServerResponse> {
none(): preHandlerHookHandler {
return this._makePreHandler([], 'NONE')
}
any(): FastifyMiddleware<Server, IncomingMessage, ServerResponse> {
any(): preHandlerHookHandler {
const setup: Setup = () => ({

@@ -108,0 +100,0 @@ limits: this.limits,

@@ -1,2 +0,1 @@

import { IncomingMessage } from 'http'
import { FastifyRequest } from 'fastify'

@@ -40,7 +39,3 @@ import { Strategy } from './lib/file-appender'

export type FileFilter = (
req: FastifyRequest<IncomingMessage>,
file: File,
callback: FileFilterCallback,
) => void
export type FileFilter = (req: FastifyRequest, file: File, callback: FileFilterCallback) => void

@@ -80,15 +75,11 @@ export interface Options {

_handleFile(
req: FastifyRequest<IncomingMessage>,
req: FastifyRequest,
file: File,
callback: (error?: Error | null, info?: Partial<File>) => void,
): void
_removeFile(
req: FastifyRequest<IncomingMessage>,
file: File,
callback: (error?: Error | null) => void,
): void
_removeFile(req: FastifyRequest, file: File, callback: (error?: Error | null) => void): void
}
export type GetFileName = (
req: FastifyRequest<IncomingMessage>,
req: FastifyRequest,
file: File,

@@ -99,3 +90,3 @@ callback: (error: Error | null, filename?: string) => void,

export type GetDestination = (
req: FastifyRequest<IncomingMessage>,
req: FastifyRequest,
file: File,

@@ -102,0 +93,0 @@ callback: (error: Error | null, destination: string) => void,

import { IncomingMessage } from 'http'
import fp = require('fastify-plugin')
import { PluginOptions, nextCallback } from 'fastify-plugin'
import fp from 'fastify-plugin'
import { PluginOptions } from 'fastify-plugin'
import { FastifyInstance, FastifyRequest } from 'fastify'

@@ -8,3 +8,7 @@

function setMultipart(req: IncomingMessage, done: (err: Error | null) => void) {
function setMultipart(
req: FastifyRequest,
_payload: IncomingMessage,
done: (err: Error | null) => void,
) {
// nothing to do, it will be done by multer in beforeHandler method

@@ -15,7 +19,11 @@ ;(req as any)[kMultipart] = true

export function isMultipart(this: FastifyRequest<IncomingMessage>): boolean {
return (this.req as any)[kMultipart] || false
export function isMultipart(this: FastifyRequest): boolean {
return (this.raw as any)[kMultipart] || false
}
function fastifyMulter(fastify: FastifyInstance, options: PluginOptions, next: nextCallback) {
function fastifyMulter(
fastify: FastifyInstance,
_options: PluginOptions,
next: (err?: Error) => void,
) {
fastify.addContentTypeParser('multipart', setMultipart)

@@ -28,3 +36,3 @@ fastify.decorateRequest('isMultipart', isMultipart)

const multer = fp(fastifyMulter, {
fastify: '>= 2.0.0',
fastify: '>= 3.0.0',
name: 'fastify-multer',

@@ -31,0 +39,0 @@ })

@@ -1,8 +0,7 @@

import { ServerResponse } from 'http'
import { FastifyReply, FastifyRequest } from 'fastify'
import * as is from 'type-is'
import * as Busboy from 'busboy'
import * as extend from 'xtend'
import * as onFinished from 'on-finished'
import * as appendField from 'append-field'
import is from 'type-is'
import Busboy from 'busboy'
import extend from 'xtend'
import onFinished from 'on-finished'
import appendField from 'append-field'

@@ -24,6 +23,8 @@ import Counter from './counter'

request: FastifyRequest,
_: FastifyReply<ServerResponse>,
_: FastifyReply,
next: (err?: Error) => void,
) {
if (!is(request.req, ['multipart'])) {
const rawRequest = request.raw || (request as any).req // this is needed just for testing
if (!is(rawRequest, ['multipart'])) {
return next()

@@ -46,3 +47,3 @@ }

busboy = new Busboy({
headers: request.req.headers,
headers: rawRequest.headers,
limits: limits,

@@ -68,7 +69,7 @@ preservePath: preservePath,

request.req.unpipe(busboy)
drainStream(request.req)
rawRequest.unpipe(busboy)
drainStream(rawRequest)
busboy.removeAllListeners()
onFinished(request.req, function() {
onFinished(rawRequest, function() {
next(err)

@@ -225,3 +226,3 @@ })

request.req.pipe(busboy)
rawRequest.pipe(busboy)
}

@@ -228,0 +229,0 @@ }

@@ -1,8 +0,7 @@

import { IncomingMessage } from 'http'
import { FastifyRequest } from 'fastify'
import { createWriteStream, unlink } from 'fs'
import * as os from 'os'
import os from 'os'
import { join } from 'path'
import * as crypto from 'crypto'
import * as mkdirp from 'mkdirp'
import crypto from 'crypto'
import mkdirp from 'mkdirp'

@@ -39,3 +38,3 @@ import { GetFileName, GetDestination, DiskStorageOptions, File, StorageEngine } from '../interfaces'

_handleFile(
req: FastifyRequest<IncomingMessage>,
req: FastifyRequest,
file: File,

@@ -71,7 +70,3 @@ cb: (error: Error | null, info?: Partial<File>) => void,

_removeFile(
req: FastifyRequest<IncomingMessage>,
file: File,
cb: (error?: Error | null) => void,
): void {
_removeFile(req: FastifyRequest, file: File, cb: (error?: Error | null) => void): void {
const path = file.path!

@@ -78,0 +73,0 @@

@@ -1,2 +0,1 @@

import { IncomingMessage } from 'http'
import { FastifyRequest } from 'fastify'

@@ -9,3 +8,3 @@ import concat = require('concat-stream')

_handleFile(
req: FastifyRequest<IncomingMessage>,
req: FastifyRequest,
file: File,

@@ -24,3 +23,3 @@ cb: (error: Error | null, info?: Partial<File>) => void,

_removeFile(req: FastifyRequest<IncomingMessage>, file: File, cb: (error?: Error) => void) {
_removeFile(req: FastifyRequest, file: File, cb: (error?: Error) => void) {
delete file.buffer

@@ -27,0 +26,0 @@ cb(undefined)

@@ -8,3 +8,3 @@ import 'fastify'

declare module 'fastify' {
interface FastifyRequest<HttpRequest, Query, Params, Headers, Body> {
interface FastifyRequest {
isMultipart: typeof isMultipart

@@ -11,0 +11,0 @@ file: File

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc