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

Rewrite to Typescript #854

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -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,
},
},
],
};
23 changes: 0 additions & 23 deletions .prettierrc.js

This file was deleted.

27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -78,21 +89,11 @@
"yarn run lint"
]
},
"packageManager": "[email protected]",
"renovate": {
"extends": [
"@tunnckocore",
":pinAllExceptPeerDependencies"
]
},
"packageManager": "[email protected]",
"keywords": [
"multipart",
"form",
"data",
"querystring",
"www",
"json",
"ulpoad",
"file"
]
}
}
88 changes: 0 additions & 88 deletions src/PersistentFile.js

This file was deleted.

80 changes: 0 additions & 80 deletions src/VolatileFile.js

This file was deleted.

98 changes: 98 additions & 0 deletions src/file/abstract.ts
Original file line number Diff line number Diff line change
@@ -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<typeof createHash>;
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<typeof createHash>[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;
26 changes: 26 additions & 0 deletions src/file/persistent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createWriteStream, unlink } from 'node:fs';
import FormidableFile from './abstract.js';

export type PersistentFileOptions = Pick<FormidableFile, 'filepath' | 'newFilename' | 'originalFilename' | 'mimetype' | 'hashAlgorithm'>;

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)
}
}
Loading