From a941e3a0d4869ec070c24ed5b381f81167a090b8 Mon Sep 17 00:00:00 2001 From: Adam Eisenreich Date: Fri, 29 Apr 2022 00:22:20 +0200 Subject: [PATCH 1/3] chore: install typescript into project --- .prettierrc.cjs | 23 +++++++++++++ .prettierrc.js | 23 ------------- package.json | 27 ++++++++------- tsconfig.json | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 5 +++ 5 files changed, 133 insertions(+), 36 deletions(-) create mode 100644 .prettierrc.cjs delete mode 100644 .prettierrc.js create mode 100644 tsconfig.json diff --git a/.prettierrc.cjs b/.prettierrc.cjs new file mode 100644 index 00000000..823cf706 --- /dev/null +++ b/.prettierrc.cjs @@ -0,0 +1,23 @@ +"use strict"; + +const config = require("@tunnckocore/prettier-config"); + +module.exports = { + ...config, + overrides: [ + { + files: ["**/*.md*"], + options: { + proseWrap: "always", + printWidth: 80, + }, + }, + { + files: ["**/.all-contributorsrc"], + options: { + parser: "json-stringify", + singleQuote: false, + }, + }, + ], +}; diff --git a/.prettierrc.js b/.prettierrc.js deleted file mode 100644 index 88a5da2e..00000000 --- a/.prettierrc.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -const config = require('@tunnckocore/prettier-config'); - -module.exports = { - ...config, - overrides: [ - { - files: ['**/*.md*'], - options: { - proseWrap: 'always', - printWidth: 80, - }, - }, - { - files: ['**/.all-contributorsrc'], - options: { - parser: 'json-stringify', - singleQuote: false, - }, - }, - ], -}; diff --git a/package.json b/package.json index da21d4e8..978ffe6d 100644 --- a/package.json +++ b/package.json @@ -54,11 +54,22 @@ "nyc": "15.1.0", "prettier": "2.0.5", "prettier-plugin-pkgjson": "0.2.8", - "supertest": "6.1.6" + "supertest": "6.1.6", + "typescript": "^4.6.3" }, "jest": { "verbose": true }, + "keywords": [ + "multipart", + "form", + "data", + "querystring", + "www", + "json", + "ulpoad", + "file" + ], "husky": { "hooks": { "pre-commit": "git status --porcelain && yarn lint-staged", @@ -78,21 +89,11 @@ "yarn run lint" ] }, + "packageManager": "yarn@1.22.17", "renovate": { "extends": [ "@tunnckocore", ":pinAllExceptPeerDependencies" ] - }, - "packageManager": "yarn@1.22.17", - "keywords": [ - "multipart", - "form", - "data", - "querystring", - "www", - "json", - "ulpoad", - "file" - ] + } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..e4d63d7a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,91 @@ +{ + "compilerOptions": { + /* Language and Environment */ + "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */ + // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + + /* Modules */ + "module": "Node12" /* Specify what module code is generated. */, + // "rootDir": "./", /* Specify the root folder within your source files. */ + // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "resolveJsonModule": true, /* Enable importing .json files */ + // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */, + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + + /* Type Checking */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ + // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ + // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} diff --git a/yarn.lock b/yarn.lock index 31b449b6..7a7176a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5233,6 +5233,11 @@ typedarray-to-buffer@^3.1.5: dependencies: is-typedarray "^1.0.0" +typescript@^4.6.3: + version "4.6.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.3.tgz#eefeafa6afdd31d725584c67a0eaba80f6fc6c6c" + integrity sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw== + universalify@^0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66" From d80362cb0c1a9fd003aeb947617c2dca4b278116 Mon Sep 17 00:00:00 2001 From: Adam Eisenreich Date: Fri, 29 Apr 2022 00:50:17 +0200 Subject: [PATCH 2/3] feat: type safe file classes --- src/PersistentFile.js | 88 ------------------------------------- src/VolatileFile.js | 80 ---------------------------------- src/file/abstract.ts | 98 ++++++++++++++++++++++++++++++++++++++++++ src/file/persistent.ts | 26 +++++++++++ src/file/volatile.ts | 21 +++++++++ 5 files changed, 145 insertions(+), 168 deletions(-) delete mode 100644 src/PersistentFile.js delete mode 100644 src/VolatileFile.js create mode 100644 src/file/abstract.ts create mode 100644 src/file/persistent.ts create mode 100644 src/file/volatile.ts diff --git a/src/PersistentFile.js b/src/PersistentFile.js deleted file mode 100644 index fec6a4de..00000000 --- a/src/PersistentFile.js +++ /dev/null @@ -1,88 +0,0 @@ -/* eslint-disable no-underscore-dangle */ - -import { WriteStream, unlink } from 'node:fs'; -import { createHash } from 'node:crypto'; -import { EventEmitter } from 'node:events'; - -class PersistentFile extends EventEmitter { - constructor({ filepath, newFilename, originalFilename, mimetype, hashAlgorithm }) { - super(); - - this.lastModifiedDate = null; - Object.assign(this, { filepath, newFilename, originalFilename, mimetype, hashAlgorithm }); - - this.size = 0; - this._writeStream = null; - - if (typeof this.hashAlgorithm === 'string') { - this.hash = createHash(this.hashAlgorithm); - } else { - this.hash = null; - } - } - - open() { - this._writeStream = new WriteStream(this.filepath); - this._writeStream.on('error', (err) => { - this.emit('error', err); - }); - } - - toJSON() { - const json = { - size: this.size, - filepath: this.filepath, - newFilename: this.newFilename, - mimetype: this.mimetype, - mtime: this.lastModifiedDate, - length: this.length, - originalFilename: this.originalFilename, - }; - if (this.hash && this.hash !== '') { - json.hash = this.hash; - } - return json; - } - - toString() { - return `PersistentFile: ${this.newFilename}, Original: ${this.originalFilename}, Path: ${this.filepath}`; - } - - write(buffer, cb) { - if (this.hash) { - this.hash.update(buffer); - } - - if (this._writeStream.closed) { - cb(); - return; - } - - this._writeStream.write(buffer, () => { - this.lastModifiedDate = new Date(); - this.size += buffer.length; - this.emit('progress', this.size); - cb(); - }); - } - - end(cb) { - if (this.hash) { - this.hash = this.hash.digest('hex'); - } - this._writeStream.end(() => { - this.emit('end'); - cb(); - }); - } - - destroy() { - this._writeStream.destroy(); - const filepath = this.filepath; - setTimeout(function () { - unlink(filepath, () => {}); - }, 1) - } -} - -export default PersistentFile; diff --git a/src/VolatileFile.js b/src/VolatileFile.js deleted file mode 100644 index 6f594b0c..00000000 --- a/src/VolatileFile.js +++ /dev/null @@ -1,80 +0,0 @@ -/* eslint-disable no-underscore-dangle */ - -import { createHash } from 'node:crypto'; -import { EventEmitter } from 'node:events'; - -class VolatileFile extends EventEmitter { - constructor({ filepath, newFilename, originalFilename, mimetype, hashAlgorithm, createFileWriteStream }) { - super(); - - this.lastModifiedDate = null; - Object.assign(this, { filepath, newFilename, originalFilename, mimetype, hashAlgorithm, createFileWriteStream }); - - this.size = 0; - this._writeStream = null; - - if (typeof this.hashAlgorithm === 'string') { - this.hash = createHash(this.hashAlgorithm); - } else { - this.hash = null; - } - } - - open() { - this._writeStream = this.createFileWriteStream(this); - this._writeStream.on('error', (err) => { - this.emit('error', err); - }); - } - - destroy() { - this._writeStream.destroy(); - } - - toJSON() { - const json = { - size: this.size, - newFilename: this.newFilename, - length: this.length, - originalFilename: this.originalFilename, - mimetype: this.mimetype, - }; - if (this.hash && this.hash !== '') { - json.hash = this.hash; - } - return json; - } - - toString() { - return `VolatileFile: ${this.originalFilename}`; - } - - write(buffer, cb) { - if (this.hash) { - this.hash.update(buffer); - } - - if (this._writeStream.closed || this._writeStream.destroyed) { - cb(); - return; - } - - this._writeStream.write(buffer, () => { - this.size += buffer.length; - this.emit('progress', this.size); - cb(); - }); - } - - end(cb) { - if (this.hash) { - this.hash = this.hash.digest('hex'); - } - this._writeStream.end(() => { - this.emit('end'); - cb(); - }); - } -} - -export default VolatileFile; diff --git a/src/file/abstract.ts b/src/file/abstract.ts new file mode 100644 index 00000000..76dd9df6 --- /dev/null +++ b/src/file/abstract.ts @@ -0,0 +1,98 @@ +import { Writable } from 'node:stream'; +import { createHash } from 'node:crypto'; +import { EventEmitter } from 'node:events'; + + +export class FormidableFile extends EventEmitter { + public readonly hashCreator?: ReturnType; + public hash?: string; + + public lastModifiedDate?: Date; + /** + * The size of the uploaded file in bytes. + * If the file is still being uploaded (see `'fileBegin'` event), this property says how many bytes of the file have been written to disk so far. + */ + public size = 0; + + + constructor( + public readonly fileType: string, + public readonly writeStream: Writable, + /** If `false`, hash will not generate. Otherwise specify a string accepted by node's `createHash` */ + public readonly hashAlgorithm: false | Parameters[0], + /** The path this file is being written to. You can modify this in the `'fileBegin'` event in case you are unhappy with the way formidable generates a temporary path for your files. */ + public filepath: string, + /** The name this file had according to the uploading client. */ + public originalFilename: string | undefined, + /** Calculated based on options provided */ + public newFilename: string | undefined, + /** The mime type of this file, according to the uploading client. */ + public mimetype: string | undefined, + ) { + super(); + + if (typeof this.hashAlgorithm === 'string') { + this.hashCreator = createHash(this.hashAlgorithm); + this.hash = ''; + } + + this.writeStream.on('error', (err) => { + this.emit('error', err); + }); + } + + write( + buffer: Buffer, + callback: () => void, + ) { + if (this.hashCreator) { + this.hashCreator.update(buffer); + } + + if (this.writeStream.destroyed) { + callback(); + return; + } + + this.writeStream.write(buffer, () => { + this.size += buffer.length; + this.emit('progress', this.size); + callback(); + }); + } + + end(callback: () => void) { + if (this.hashCreator) { + this.hash = this.hashCreator.digest('hex'); + } + this.writeStream.end(() => { + this.emit('end'); + callback(); + }); + } + + + destroy() { + this.writeStream.destroy(); + } + + toJSON() { + const json = { + size: this.size, + newFilename: this.newFilename, + originalFilename: this.originalFilename, + mimetype: this.mimetype, + hash: this.hashCreator + }; + if (this.hashCreator && this.hash !== '') { + json.hash = this.hashCreator; + } + return json; + } + + toString() { + return `${this.fileType}: ${this.newFilename}, Original: ${this.originalFilename}, Path: ${this.filepath}`; + } +} + +export default FormidableFile; diff --git a/src/file/persistent.ts b/src/file/persistent.ts new file mode 100644 index 00000000..87ac86d2 --- /dev/null +++ b/src/file/persistent.ts @@ -0,0 +1,26 @@ +import { createWriteStream, unlink } from 'node:fs'; +import FormidableFile from './abstract.js'; + +export type PersistentFileOptions = Pick; + +export class PersistentFile extends FormidableFile { + constructor(options: PersistentFileOptions) { + super( + 'PersistentFile', + createWriteStream(options.filepath), + options.hashAlgorithm, + options.filepath, + options.originalFilename, + options.originalFilename, + options.mimetype, + ); + } + + destroy() { + super.destroy(); + + setTimeout(() => { + unlink(this.filepath, () => {}); + }, 1) + } +} diff --git a/src/file/volatile.ts b/src/file/volatile.ts new file mode 100644 index 00000000..e463a945 --- /dev/null +++ b/src/file/volatile.ts @@ -0,0 +1,21 @@ +import { Writable } from 'node:stream'; +import FormidableFile from './abstract.js'; +import { PersistentFileOptions } from './persistent.js'; + +export interface VolatileFileOptions extends PersistentFileOptions { + createFileWriteStream: (options: PersistentFileOptions) => Writable; +} + +export class VolatileFile extends FormidableFile { + constructor(options: VolatileFileOptions) { + super( + 'VolatileFile', + options.createFileWriteStream(options), + options.hashAlgorithm, + options.filepath, + options.originalFilename, + options.originalFilename, + options.mimetype, + ); + } +} From 4cf74892734da12347b20a5ef7ecf712f723c269 Mon Sep 17 00:00:00 2001 From: Adam Eisenreich Date: Fri, 29 Apr 2022 00:58:58 +0200 Subject: [PATCH 3/3] feat: typed readBooleans --- src/helpers/readBooleans.js | 10 ---------- src/helpers/readBooleans.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 10 deletions(-) delete mode 100644 src/helpers/readBooleans.js create mode 100644 src/helpers/readBooleans.ts diff --git a/src/helpers/readBooleans.js b/src/helpers/readBooleans.js deleted file mode 100644 index bd969134..00000000 --- a/src/helpers/readBooleans.js +++ /dev/null @@ -1,10 +0,0 @@ -const readBooleans = (fields, listOfBooleans) => { - // 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/helpers/readBooleans.ts b/src/helpers/readBooleans.ts new file mode 100644 index 00000000..da0e4dec --- /dev/null +++ b/src/helpers/readBooleans.ts @@ -0,0 +1,26 @@ +/** + * Html form input type="checkbox" only send the value "on" if checked, + * convert it to booleans for each input that is expected to be sent as a checkbox, + * only use after firstValues or similar was called. + * + * @example ```ts + * form.parse(request, async (error, fieldsMultiple, files) => { + * const fieldsSingle = firstValues(form, fieldsMultiple); + * + * const expectedBooleans = ['checkbox1', 'wantsNewsLetter', 'hasACar']; + * const fieldsWithBooleans = readBooleans(fieldsSingle, expectedBooleans); + * }); + * ``` + */ +export function readBooleans, K extends keyof F>( + fields: F, + listOfBooleans: readonly K[], +): Omit & Record { + // html forms do not send off at all + const fieldsWithBooleans: Omit & Record = { ...fields }; + listOfBooleans.forEach((key) => { + // We can safely assert this because of the type safety of listOfBooleans + (fieldsWithBooleans as Record)[key] = fields[key] === `on` || fields[key] === true; + }); + return fieldsWithBooleans; +}