Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some prettier issues #764

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 20 additions & 40 deletions src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -537,8 +521,6 @@ class IncomingForm extends EventEmitter {
return basename.slice(firstDot, lastDot) + extname;
}



_joinDirectoryName(name) {
const newPath = path.join(this.uploadDir, name);

Expand Down Expand Up @@ -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;
}
};
}
}

Expand Down Expand Up @@ -609,8 +592,5 @@ class IncomingForm extends EventEmitter {
}
}


export default IncomingForm;
export {
DEFAULT_OPTIONS,
};
export { DEFAULT_OPTIONS };
3 changes: 1 addition & 2 deletions src/FormidableError.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const missingPlugin = 1000;
const pluginFunction = 1001;
const aborted = 1002;
Expand Down Expand Up @@ -41,4 +40,4 @@ export {
unknownTransferEncoding,
};

export default FormidableError;
export default FormidableError;
29 changes: 16 additions & 13 deletions src/helpers/firstValues.js
Original file line number Diff line number Diff line change
@@ -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 };
16 changes: 8 additions & 8 deletions src/helpers/readBooleans.js
Original file line number Diff line number Diff line change
@@ -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 };
1 change: 0 additions & 1 deletion src/parsers/Dummy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-underscore-dangle */


import { Transform } from 'stream';

class DummyParser extends Transform {
Expand Down
1 change: 0 additions & 1 deletion src/parsers/JSON.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-underscore-dangle */


import { Transform } from 'stream';

class JSONParser extends Transform {
Expand Down
4 changes: 1 addition & 3 deletions src/parsers/Multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++,
Expand Down Expand Up @@ -343,4 +341,4 @@ MultipartParser.stateToString = (stateNumber) => {
}
};

export default Object.assign(MultipartParser, { STATES: STATES });
export default Object.assign(MultipartParser, { STATES });
1 change: 0 additions & 1 deletion src/parsers/Querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions src/parsers/StreamingQuerystring.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions src/parsers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import JSONParser from './JSON.js';
import DummyParser from './Dummy.js';
import MultipartParser from './Multipart.js';
Expand Down
9 changes: 1 addition & 8 deletions src/plugins/index.js
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 1 addition & 4 deletions src/plugins/multipart.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/octetstream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-underscore-dangle */


import OctetStreamParser from '../parsers/OctetStream.js';

export const octetStreamType = 'octet-stream';
Expand All @@ -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
Expand Down
15 changes: 8 additions & 7 deletions test/fixture/js/encoding.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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,
};
12 changes: 6 additions & 6 deletions test/fixture/js/misc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const empty_http = [];
const empty_urlencoded_http = [];
const empty_multipart_http = [];
const empty_multipart2_http = [];
const _minimal_http = [];

export {
empty_http,
empty_urlencoded_http,
empty_multipart_http,
empty_multipart2_http,
_minimal_http,
};

const empty_http = [];
const empty_urlencoded_http = [];
const empty_multipart_http = [];
const empty_multipart2_http = [];
const _minimal_http = [];
7 changes: 2 additions & 5 deletions test/fixture/js/no-filename.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export {
generic_http,
filename_name_http,
}

const generic_http = [
{
type: 'file',
Expand All @@ -22,3 +17,5 @@ const filename_name_http = [
sha1: 'b31d07bac24ac32734de88b3687dddb10e976872',
},
];

export { generic_http, filename_name_http };
7 changes: 2 additions & 5 deletions test/fixture/js/preamble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export {
crlf_http,
preamble_http,
};

const crlf_http = [
{
type: 'file',
Expand All @@ -22,3 +17,5 @@ const preamble_http = [
sha1: 'a47f7a8a7959f36c3f151ba8b0bd28f2d6b606e2',
},
];

export { crlf_http, preamble_http };
Loading