Skip to content

Commit 37711f7

Browse files
committed
fix: convert File to class, fmt more
Signed-off-by: Charlike Mike Reagent <[email protected]>
1 parent 0224482 commit 37711f7

7 files changed

+93
-73
lines changed

src/default_options.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const defaultOptions = {
24
maxFields: 1000,
35
maxFieldsSize: 20 * 1024 * 1024,

src/dummy_parser.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/* eslint-disable no-underscore-dangle */
2+
3+
'use strict';
4+
25
const { Transform } = require('stream');
36

47
class DummyParser extends Transform {

src/file.js

+69-65
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,88 @@
11
/* eslint-disable no-underscore-dangle */
2-
const util = require('util');
2+
3+
'use strict';
4+
35
const fs = require('fs');
4-
const { EventEmitter } = require('events');
56
const crypto = require('crypto');
7+
const { EventEmitter } = require('events');
68

7-
function File(properties) {
8-
EventEmitter.call(this);
9+
class File extends EventEmitter {
10+
constructor(properties) {
11+
super();
912

10-
this.size = 0;
11-
this.path = null;
12-
this.name = null;
13-
this.type = null;
14-
this.hash = null;
15-
this.lastModifiedDate = null;
13+
this.size = 0;
14+
this.path = null;
15+
this.name = null;
16+
this.type = null;
17+
this.hash = null;
18+
this.lastModifiedDate = null;
19+
20+
this._writeStream = null;
1621

17-
this._writeStream = null;
22+
// eslint-disable-next-line guard-for-in, no-restricted-syntax
23+
for (const key in properties) {
24+
this[key] = properties[key];
25+
}
1826

19-
// eslint-disable-next-line guard-for-in, no-restricted-syntax
20-
for (const key in properties) {
21-
this[key] = properties[key];
27+
if (typeof this.hash === 'string') {
28+
this.hash = crypto.createHash(properties.hash);
29+
} else {
30+
this.hash = null;
31+
}
2232
}
2333

24-
if (typeof this.hash === 'string') {
25-
this.hash = crypto.createHash(properties.hash);
26-
} else {
27-
this.hash = null;
34+
open() {
35+
this._writeStream = new fs.WriteStream(this.path);
2836
}
29-
}
30-
module.exports = File;
31-
util.inherits(File, EventEmitter);
3237

33-
File.prototype.open = function onpenStream() {
34-
this._writeStream = new fs.WriteStream(this.path);
35-
};
38+
toJSON() {
39+
const json = {
40+
size: this.size,
41+
path: this.path,
42+
name: this.name,
43+
type: this.type,
44+
mtime: this.lastModifiedDate,
45+
length: this.length,
46+
filename: this.filename,
47+
mime: this.mime,
48+
};
49+
if (this.hash && this.hash !== '') {
50+
json.hash = this.hash;
51+
}
52+
return json;
53+
}
3654

37-
File.prototype.toJSON = function toJSON() {
38-
const json = {
39-
size: this.size,
40-
path: this.path,
41-
name: this.name,
42-
type: this.type,
43-
mtime: this.lastModifiedDate,
44-
length: this.length,
45-
filename: this.filename,
46-
mime: this.mime,
47-
};
48-
if (this.hash && this.hash !== '') {
49-
json.hash = this.hash;
55+
toString() {
56+
return `File: ${this.name}, Path: ${this.path}`;
5057
}
51-
return json;
52-
};
5358

54-
File.prototype.toString = function toString() {
55-
return `File: ${this.name}, Path: ${this.path}`;
56-
};
59+
writeFn(buffer, cb) {
60+
if (this.hash) {
61+
this.hash.update(buffer);
62+
}
5763

58-
File.prototype.write = function writeFn(buffer, cb) {
59-
if (this.hash) {
60-
this.hash.update(buffer);
61-
}
64+
if (this._writeStream.closed) {
65+
cb();
66+
return;
67+
}
6268

63-
if (this._writeStream.closed) {
64-
cb();
65-
return;
69+
this._writeStream.write(buffer, () => {
70+
this.lastModifiedDate = new Date();
71+
this.size += buffer.length;
72+
this.emit('progress', this.size);
73+
cb();
74+
});
6675
}
6776

68-
this._writeStream.write(buffer, () => {
69-
this.lastModifiedDate = new Date();
70-
this.size += buffer.length;
71-
this.emit('progress', this.size);
72-
cb();
73-
});
74-
};
75-
76-
File.prototype.end = function endFn(cb) {
77-
if (this.hash) {
78-
this.hash = this.hash.digest('hex');
77+
endFn(cb) {
78+
if (this.hash) {
79+
this.hash = this.hash.digest('hex');
80+
}
81+
this._writeStream.end(() => {
82+
this.emit('end');
83+
cb();
84+
});
7985
}
80-
this._writeStream.end(() => {
81-
this.emit('end');
82-
cb();
83-
});
84-
};
86+
}
87+
88+
exports.File = File;

src/incoming_form.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
/* eslint-disable class-methods-use-this */
22
/* eslint-disable no-underscore-dangle */
3-
const crypto = require('crypto');
3+
4+
'use strict';
5+
6+
const os = require('os');
47
const fs = require('fs');
58
const path = require('path');
6-
const { StringDecoder } = require('string_decoder');
7-
const { EventEmitter } = require('events');
9+
const crypto = require('crypto');
810
const { Stream } = require('stream');
9-
const os = require('os');
11+
const { EventEmitter } = require('events');
12+
const { StringDecoder } = require('string_decoder');
1013

11-
const File = require('./file');
12-
const { defaultOptions } = require('./default_options');
14+
const { File } = require('./file');
15+
const { JSONParser } = require('./json_parser');
1316
const { DummyParser } = require('./dummy_parser');
17+
const { OctetParser } = require('./octet_parser');
18+
const { defaultOptions } = require('./default_options');
1419
const { MultipartParser } = require('./multipart_parser');
1520
const { QuerystringParser } = require('./querystring_parser');
16-
const { OctetParser } = require('./octet_parser');
17-
const { JSONParser } = require('./json_parser');
1821

1922
function hasOwnProp(obj, key) {
2023
return Object.prototype.hasOwnProperty.call(obj, key);

src/json_parser.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/* eslint-disable no-underscore-dangle */
2+
3+
'use strict';
4+
25
const { Transform } = require('stream');
36

47
class JSONParser extends Transform {

src/octet_parser.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const { PassThrough } = require('stream');
24

35
class OctetParser extends PassThrough {}

src/querystring_parser.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/* eslint-disable no-underscore-dangle */
2+
3+
'use strict';
4+
25
const { Transform } = require('stream');
36
const querystring = require('querystring');
47

0 commit comments

Comments
 (0)