diff --git a/src/Formidable.js b/src/Formidable.js index 2f42eab6..664c9058 100644 --- a/src/Formidable.js +++ b/src/Formidable.js @@ -8,12 +8,13 @@ import once from 'once'; import dezalgo from 'dezalgo'; import { EventEmitter } from 'events'; import { StringDecoder } from 'string_decoder'; -import { - octetstream, - querystring, - multipart, - json, -} from "./plugins/index.js"; +import { octetstream, querystring, multipart, json } from './plugins/index.js'; +import PersistentFile from './PersistentFile.js'; +import VolatileFile from './VolatileFile.js'; +import DummyParser from './parsers/Dummy.js'; +import MultipartParser from './parsers/Multipart.js'; +import * as errors from './FormidableError.js'; +import FormidableError from './FormidableError.js'; const toHexoId = hexoid(25); const DEFAULT_OPTIONS = { @@ -26,27 +27,14 @@ const DEFAULT_OPTIONS = { encoding: 'utf-8', hashAlgorithm: false, uploadDir: os.tmpdir(), - enabledPlugins: [ - octetstream, - querystring, - multipart, - json, - ], + enabledPlugins: [octetstream, querystring, multipart, json], fileWriteStreamHandler: null, defaultInvalidName: 'invalid-name', - filter: function () { + filter() { return true; }, }; -import PersistentFile from './PersistentFile.js'; -import VolatileFile from './VolatileFile.js'; -import DummyParser from './parsers/Dummy.js'; -import MultipartParser from './parsers/Multipart.js'; -import * as errors from './FormidableError.js'; -import FormidableError from './FormidableError.js'; - - function hasOwnProp(obj, key) { return Object.prototype.hasOwnProperty.call(obj, key); } @@ -151,26 +139,22 @@ class IncomingForm extends EventEmitter { const files = {}; this.on('field', (name, value) => { - if ( - this.type === 'multipart' || this.type === 'urlencoded' - ) { + if (this.type === 'multipart' || this.type === 'urlencoded') { if (!hasOwnProp(this.fields, name)) { this.fields[name] = [value]; } else { this.fields[name].push(value); } - } else { this.fields[name] = value; } }); this.on('file', (name, file) => { - if (!hasOwnProp(files, name)) { - files[name] = [file]; - } else { - files[name].push(file); - } - + if (!hasOwnProp(files, name)) { + files[name] = [file]; + } else { + files[name].push(file); + } }); this.on('error', (err) => { callback(err, this.fields, files); @@ -537,8 +521,6 @@ class IncomingForm extends EventEmitter { return basename.slice(firstDot, lastDot) + extname; } - - _joinDirectoryName(name) { const newPath = path.join(this.uploadDir, name); @@ -570,12 +552,13 @@ class IncomingForm extends EventEmitter { const name = toHexoId(); if (part && this.options.keepExtensions) { - const originalFilename = typeof part === 'string' ? part : part.originalFilename; + const originalFilename = + typeof part === 'string' ? part : part.originalFilename; return `${name}${this._getExtension(originalFilename)}`; } - + return name; - } + }; } } @@ -609,8 +592,5 @@ class IncomingForm extends EventEmitter { } } - export default IncomingForm; -export { - DEFAULT_OPTIONS, -}; +export { DEFAULT_OPTIONS }; diff --git a/src/FormidableError.js b/src/FormidableError.js index c2210255..7cbdb15c 100644 --- a/src/FormidableError.js +++ b/src/FormidableError.js @@ -1,4 +1,3 @@ - const missingPlugin = 1000; const pluginFunction = 1001; const aborted = 1002; @@ -41,4 +40,4 @@ export { unknownTransferEncoding, }; -export default FormidableError; \ No newline at end of file +export default FormidableError; diff --git a/src/helpers/firstValues.js b/src/helpers/firstValues.js index ea1c9b44..6c9944cc 100644 --- a/src/helpers/firstValues.js +++ b/src/helpers/firstValues.js @@ -1,16 +1,19 @@ -import { multipartType } from "../plugins/multipart.js"; -import { querystringType } from "../plugins/querystring.js"; - -export { firstValues }; +import { multipartType } from '../plugins/multipart.js'; +import { querystringType } from '../plugins/querystring.js'; const firstValues = (form, fields, exceptions = []) => { - if (form.type !== querystringType && form.type !== multipartType) { - return fields; - } - return Object.fromEntries(Object.entries(fields).map(([key, value]) => { - if (exceptions.includes(key)) { - return [key, value]; - } - return [key, value[0]]; - })); + if (form.type !== querystringType && form.type !== multipartType) { + return fields; + } + + return Object.fromEntries( + Object.entries(fields).map(([key, value]) => { + if (exceptions.includes(key)) { + return [key, value]; + } + return [key, value[0]]; + }), + ); }; + +export { firstValues }; diff --git a/src/helpers/readBooleans.js b/src/helpers/readBooleans.js index 40fb3361..bd969134 100644 --- a/src/helpers/readBooleans.js +++ b/src/helpers/readBooleans.js @@ -1,10 +1,10 @@ -export { readBooleans }; - const readBooleans = (fields, listOfBooleans) => { - // html forms do not send off at all - const fieldsWithBooleans = Object.assign({}, fields); - listOfBooleans.forEach(key => { - fieldsWithBooleans[key] = fields[key] === `on` || fields[key] === true; - }); - return fieldsWithBooleans; + // html forms do not send off at all + const fieldsWithBooleans = { ...fields }; + listOfBooleans.forEach((key) => { + fieldsWithBooleans[key] = fields[key] === `on` || fields[key] === true; + }); + return fieldsWithBooleans; }; + +export { readBooleans }; diff --git a/src/parsers/Dummy.js b/src/parsers/Dummy.js index ab2d4bfb..49409b61 100644 --- a/src/parsers/Dummy.js +++ b/src/parsers/Dummy.js @@ -1,6 +1,5 @@ /* eslint-disable no-underscore-dangle */ - import { Transform } from 'stream'; class DummyParser extends Transform { diff --git a/src/parsers/JSON.js b/src/parsers/JSON.js index 9b977044..9f8dede2 100644 --- a/src/parsers/JSON.js +++ b/src/parsers/JSON.js @@ -1,6 +1,5 @@ /* eslint-disable no-underscore-dangle */ - import { Transform } from 'stream'; class JSONParser extends Transform { diff --git a/src/parsers/Multipart.js b/src/parsers/Multipart.js index e6b663a2..25badce4 100644 --- a/src/parsers/Multipart.js +++ b/src/parsers/Multipart.js @@ -3,12 +3,10 @@ /* eslint-disable no-plusplus */ /* eslint-disable no-underscore-dangle */ - import { Transform } from 'stream'; import * as errors from '../FormidableError.js'; import FormidableError from '../FormidableError.js'; - let s = 0; const STATE = { PARSER_UNINITIALIZED: s++, @@ -343,4 +341,4 @@ MultipartParser.stateToString = (stateNumber) => { } }; -export default Object.assign(MultipartParser, { STATES: STATES }); +export default Object.assign(MultipartParser, { STATES }); diff --git a/src/parsers/Querystring.js b/src/parsers/Querystring.js index 372377f4..0cc6e3fa 100644 --- a/src/parsers/Querystring.js +++ b/src/parsers/Querystring.js @@ -2,7 +2,6 @@ import { Transform } from 'stream'; - // This is a buffering parser, not quite as nice as the multipart one. // If I find time I'll rewrite this to be fully streaming as well class QuerystringParser extends Transform { diff --git a/src/parsers/StreamingQuerystring.js b/src/parsers/StreamingQuerystring.js index 9fab49c0..92c6c419 100644 --- a/src/parsers/StreamingQuerystring.js +++ b/src/parsers/StreamingQuerystring.js @@ -1,11 +1,9 @@ // not used /* eslint-disable no-underscore-dangle */ - import { Transform } from 'stream'; import FormidableError, { maxFieldsSizeExceeded } from '../FormidableError.js'; - const AMPERSAND = 38; const EQUALS = 61; @@ -80,9 +78,9 @@ class QuerystringParser extends Transform { // Emit the last field if (this.readingKey) { // we only have a key if there's something in the buffer. We definitely have no value - if (this.buffer && this.buffer.length){ - this.emitField(this.buffer.toString('ascii')); - } + if (this.buffer && this.buffer.length) { + this.emitField(this.buffer.toString('ascii')); + } } else { // We have a key, we may or may not have a value this.emitField( diff --git a/src/parsers/index.js b/src/parsers/index.js index 17ea31c5..a24b4f12 100644 --- a/src/parsers/index.js +++ b/src/parsers/index.js @@ -1,5 +1,3 @@ -'use strict'; - import JSONParser from './JSON.js'; import DummyParser from './Dummy.js'; import MultipartParser from './Multipart.js'; diff --git a/src/plugins/index.js b/src/plugins/index.js index 2b181c75..18318c5f 100644 --- a/src/plugins/index.js +++ b/src/plugins/index.js @@ -1,13 +1,6 @@ -'use strict'; - import octetstream from './octetstream.js'; import querystring from './querystring.js'; import multipart from './multipart.js'; import json from './json.js'; -export { - octetstream, - querystring, - multipart, - json, -}; +export { octetstream, querystring, multipart, json }; diff --git a/src/plugins/multipart.js b/src/plugins/multipart.js index 6072a356..52c836cb 100644 --- a/src/plugins/multipart.js +++ b/src/plugins/multipart.js @@ -1,13 +1,10 @@ /* eslint-disable no-underscore-dangle */ -'use strict'; - import { Stream } from 'stream'; import MultipartParser from '../parsers/Multipart.js'; import * as errors from '../FormidableError.js'; import FormidableError from '../FormidableError.js'; - export const multipartType = 'multipart'; // the `options` is also available through the `options` / `formidable.options` export default function plugin(formidable, options) { @@ -36,7 +33,7 @@ export default function plugin(formidable, options) { self._error(err); } } -}; +} // Note that it's a good practice (but it's up to you) to use the `this.options` instead // of the passed `options` (second) param, because when you decide diff --git a/src/plugins/octetstream.js b/src/plugins/octetstream.js index cfbe721a..5d29ad17 100644 --- a/src/plugins/octetstream.js +++ b/src/plugins/octetstream.js @@ -1,6 +1,5 @@ /* eslint-disable no-underscore-dangle */ - import OctetStreamParser from '../parsers/OctetStream.js'; export const octetStreamType = 'octet-stream'; @@ -17,7 +16,7 @@ export default function plugin(formidable, options) { } return self; -}; +} // Note that it's a good practice (but it's up to you) to use the `this.options` instead // of the passed `options` (second) param, because when you decide diff --git a/test/fixture/js/encoding.js b/test/fixture/js/encoding.js index a227ae46..b887db3e 100644 --- a/test/fixture/js/encoding.js +++ b/test/fixture/js/encoding.js @@ -1,10 +1,3 @@ -export { - menu_separator_png_http, - beta_sticker_1_png_http, - blank_gif_http, - binaryfile_tar_gz_http, - plain_txt_http, -}; const menu_separator_png_http = [ { type: 'file', @@ -54,3 +47,11 @@ const plain_txt_http = [ sha1: 'b31d07bac24ac32734de88b3687dddb10e976872', }, ]; + +export { + menu_separator_png_http, + beta_sticker_1_png_http, + blank_gif_http, + binaryfile_tar_gz_http, + plain_txt_http, +}; diff --git a/test/fixture/js/misc.js b/test/fixture/js/misc.js index e3c530ee..80e36eab 100644 --- a/test/fixture/js/misc.js +++ b/test/fixture/js/misc.js @@ -1,3 +1,9 @@ +const empty_http = []; +const empty_urlencoded_http = []; +const empty_multipart_http = []; +const empty_multipart2_http = []; +const _minimal_http = []; + export { empty_http, empty_urlencoded_http, @@ -5,9 +11,3 @@ export { empty_multipart2_http, _minimal_http, }; - -const empty_http = []; -const empty_urlencoded_http = []; -const empty_multipart_http = []; -const empty_multipart2_http = []; -const _minimal_http = []; \ No newline at end of file diff --git a/test/fixture/js/no-filename.js b/test/fixture/js/no-filename.js index 2f71b527..c39355f7 100644 --- a/test/fixture/js/no-filename.js +++ b/test/fixture/js/no-filename.js @@ -1,8 +1,3 @@ -export { - generic_http, - filename_name_http, -} - const generic_http = [ { type: 'file', @@ -22,3 +17,5 @@ const filename_name_http = [ sha1: 'b31d07bac24ac32734de88b3687dddb10e976872', }, ]; + +export { generic_http, filename_name_http }; diff --git a/test/fixture/js/preamble.js b/test/fixture/js/preamble.js index a60aa476..aed3d462 100644 --- a/test/fixture/js/preamble.js +++ b/test/fixture/js/preamble.js @@ -1,8 +1,3 @@ -export { - crlf_http, - preamble_http, -}; - const crlf_http = [ { type: 'file', @@ -22,3 +17,5 @@ const preamble_http = [ sha1: 'a47f7a8a7959f36c3f151ba8b0bd28f2d6b606e2', }, ]; + +export { crlf_http, preamble_http }; diff --git a/test/fixture/js/special-chars-in-filename.js b/test/fixture/js/special-chars-in-filename.js index 66517073..1aa9d991 100644 --- a/test/fixture/js/special-chars-in-filename.js +++ b/test/fixture/js/special-chars-in-filename.js @@ -2,7 +2,7 @@ const properFilename = 'funkyfilename.txt'; function expect(originalFilename, fixtureName) { return [ - { + { type: 'field', name: 'title', originalFilename: properFilename, @@ -11,7 +11,7 @@ function expect(originalFilename, fixtureName) { { type: 'file', name: 'upload', - originalFilename: originalFilename, + originalFilename, fixture: fixtureName, }, ]; diff --git a/test/fixture/js/workarounds.js b/test/fixture/js/workarounds.js index 9503b6fc..c3ab14b7 100644 --- a/test/fixture/js/workarounds.js +++ b/test/fixture/js/workarounds.js @@ -1,8 +1,3 @@ -export { - missing_hyphens1_http, - missing_hyphens2_http, -} - const missing_hyphens1_http = [ { type: 'file', @@ -22,3 +17,5 @@ const missing_hyphens2_http = [ sha1: '798e39a4a1034232ed26e0aadd67f5d1ff10b966', }, ]; + +export { missing_hyphens1_http, missing_hyphens2_http };