formflux 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/EventHandlers.d.ts +7 -0
  2. package/dist/ExtractContent.d.ts +11 -0
  3. package/dist/FormFlux.Types.d.ts +51 -0
  4. package/dist/FormFlux.d.ts +16 -0
  5. package/dist/FormFluxError.d.ts +5 -0
  6. package/dist/SetBodyContentToReq.d.ts +10 -0
  7. package/dist/SetFileContentToReqFile.d.ts +11 -0
  8. package/dist/WriteFileContent.d.ts +17 -0
  9. package/dist/cjs/EventHandlers.js +16 -0
  10. package/dist/cjs/EventHandlers.js.map +1 -0
  11. package/dist/cjs/ExtractContent.js +146 -0
  12. package/dist/cjs/ExtractContent.js.map +1 -0
  13. package/dist/cjs/FormFlux.Types.js +2 -0
  14. package/dist/cjs/FormFlux.Types.js.map +1 -0
  15. package/dist/cjs/FormFlux.js +480 -0
  16. package/dist/cjs/FormFlux.js.map +1 -0
  17. package/dist/cjs/FormFluxError.js +14 -0
  18. package/dist/cjs/FormFluxError.js.map +1 -0
  19. package/dist/cjs/SetBodyContentToReq.js +100 -0
  20. package/dist/cjs/SetBodyContentToReq.js.map +1 -0
  21. package/dist/cjs/SetFileContentToReqFile.js +64 -0
  22. package/dist/cjs/SetFileContentToReqFile.js.map +1 -0
  23. package/dist/cjs/WriteFileContent.js +139 -0
  24. package/dist/cjs/WriteFileContent.js.map +1 -0
  25. package/dist/cjs/defaultOptions.js +14 -0
  26. package/dist/cjs/defaultOptions.js.map +1 -0
  27. package/dist/cjs/helpers/resBodyMaker.js +33 -0
  28. package/dist/cjs/helpers/resBodyMaker.js.map +1 -0
  29. package/dist/cjs/setDatatoReqobj.js +24 -0
  30. package/dist/cjs/setDatatoReqobj.js.map +1 -0
  31. package/dist/cjs/setFileNameToBody.js +103 -0
  32. package/dist/cjs/setFileNameToBody.js.map +1 -0
  33. package/dist/defaultOptions.d.ts +2 -0
  34. package/dist/esm/EventHandlers.js +11 -0
  35. package/dist/esm/EventHandlers.js.map +1 -0
  36. package/dist/esm/ExtractContent.js +141 -0
  37. package/dist/esm/ExtractContent.js.map +1 -0
  38. package/dist/esm/FormFlux.Types.js +2 -0
  39. package/dist/esm/FormFlux.Types.js.map +1 -0
  40. package/dist/esm/FormFlux.js +475 -0
  41. package/dist/esm/FormFlux.js.map +1 -0
  42. package/dist/esm/FormFluxError.js +10 -0
  43. package/dist/esm/FormFluxError.js.map +1 -0
  44. package/dist/esm/SetBodyContentToReq.js +95 -0
  45. package/dist/esm/SetBodyContentToReq.js.map +1 -0
  46. package/dist/esm/SetFileContentToReqFile.js +59 -0
  47. package/dist/esm/SetFileContentToReqFile.js.map +1 -0
  48. package/dist/esm/WriteFileContent.js +134 -0
  49. package/dist/esm/WriteFileContent.js.map +1 -0
  50. package/dist/esm/defaultOptions.js +10 -0
  51. package/dist/esm/defaultOptions.js.map +1 -0
  52. package/dist/esm/helpers/resBodyMaker.js +29 -0
  53. package/dist/esm/helpers/resBodyMaker.js.map +1 -0
  54. package/dist/esm/setDatatoReqobj.js +20 -0
  55. package/dist/esm/setDatatoReqobj.js.map +1 -0
  56. package/dist/esm/setFileNameToBody.js +98 -0
  57. package/dist/esm/setFileNameToBody.js.map +1 -0
  58. package/dist/helpers/resBodyMaker.d.ts +1 -0
  59. package/dist/package.json +61 -0
  60. package/dist/setDatatoReqobj.d.ts +7 -0
  61. package/dist/setFileNameToBody.d.ts +10 -0
  62. package/package.json +13 -3
  63. package/script/babel-package.js +3 -0
  64. package/script/babel-preset.js +21 -0
@@ -0,0 +1,7 @@
1
+ import EventEmitter from "node:events";
2
+ declare class EventHandlers extends EventEmitter {
3
+ constructor();
4
+ emitMessage(eventName: string, message: string | object): void;
5
+ }
6
+ declare const _default: EventHandlers;
7
+ export default _default;
@@ -0,0 +1,11 @@
1
+ import { optionFields, options, optionSingle, reqObj } from "./FormFlux.Types";
2
+ declare class ExtractFileContent {
3
+ private obj;
4
+ private options;
5
+ private events;
6
+ private fieldArr;
7
+ private singleObj;
8
+ constructor(obj: reqObj, options: options | optionSingle, fieldArr: optionFields | null, singleObj: string | null);
9
+ extraction(): void;
10
+ }
11
+ export default ExtractFileContent;
@@ -0,0 +1,51 @@
1
+ import { Request } from "express";
2
+ import FormfluxError from "./FormFluxError";
3
+ export interface reqObj {
4
+ "originalReq": string;
5
+ "modifiedReq": Buffer;
6
+ "data": Array<string>;
7
+ "content": Array<Buffer>;
8
+ "contentBody": Array<string>;
9
+ "metaData": Array<string>;
10
+ "mimeType": Array<string>;
11
+ "fieldNameBody": Array<string>;
12
+ "fileName": Array<string>;
13
+ "modifiedFileName": Array<string>;
14
+ "fieldNameFile": Array<string>;
15
+ "filePath": Array<string>;
16
+ "filesize": Array<number>;
17
+ "streams": Array<any>;
18
+ }
19
+ export interface File {
20
+ mimetype: string;
21
+ originalname: string;
22
+ filesize: number;
23
+ fieldname: string;
24
+ }
25
+ export interface options {
26
+ attachFileToReqBody?: boolean;
27
+ maxFileCount?: number;
28
+ maxFileSize?: number;
29
+ maxFields?: number;
30
+ minFileCount?: number;
31
+ filename: (req: Request, file: File, cb: (error: FormfluxError | null, filename: string) => void) => void;
32
+ destination: (req: Request, file: File, cb: (error: FormfluxError | null, filepath: string) => void) => void;
33
+ fileFilter?: (req: Request, file: File, cb: (error: Error | null, bool: boolean) => void) => void;
34
+ }
35
+ export interface optionSingle {
36
+ attachFileToReqBody?: boolean;
37
+ maxFileCount?: number;
38
+ maxFileSize?: number;
39
+ maxFields?: number;
40
+ minFileCount?: number;
41
+ filename: (req: Request, file: File, cb: (error: FormfluxError | null, filename: string) => void) => void;
42
+ fileFilter?: (req: Request, file: File, cb: (error: Error | null, bool: boolean) => void) => void;
43
+ }
44
+ interface fieldObject {
45
+ name: string;
46
+ maxFileCount?: number;
47
+ maxFileSize?: number;
48
+ minFileCount?: number;
49
+ }
50
+ export type optionFields = [fieldObject, ...fieldObject[]];
51
+ export {};
@@ -0,0 +1,16 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+ import { options, optionFields, optionSingle } from "./FormFlux.Types";
3
+ declare class Formflux {
4
+ static diskStorage(options: options): {
5
+ any(): (req: Request, res: Response, next: NextFunction) => Promise<void>;
6
+ fields(optionFields: optionFields): (req: Request, res: Response, next: NextFunction) => Promise<void>;
7
+ single(field: string): (req: Request, res: Response, next: NextFunction) => Promise<void>;
8
+ };
9
+ static memoryStorage(options: optionSingle): {
10
+ any(): (req: Request, res: Response, next: NextFunction) => Promise<void>;
11
+ fields(optionFields: optionFields): (req: Request, res: Response, next: NextFunction) => Promise<void>;
12
+ single(field: string): (req: Request, res: Response, next: NextFunction) => Promise<void>;
13
+ };
14
+ bodyParser(): (req: Request, res: Response, next: NextFunction) => Promise<void>;
15
+ }
16
+ export default Formflux;
@@ -0,0 +1,5 @@
1
+ declare class FormfluxError extends Error {
2
+ private statusCode;
3
+ constructor(message: string, statusCode?: number);
4
+ }
5
+ export default FormfluxError;
@@ -0,0 +1,10 @@
1
+ import { reqObj } from "./FormFlux.Types";
2
+ import { Request } from "express";
3
+ declare class setContentToBody {
4
+ private obj;
5
+ constructor(obj: reqObj);
6
+ setBody(req: Request): void;
7
+ nestedData(req: Request, fieldName: string, data: any): void;
8
+ getNestedField(req: Request, obj: object, posArr: Array<any>, keySearch: any, data: any, i: number, prevObj: object): void;
9
+ }
10
+ export default setContentToBody;
@@ -0,0 +1,11 @@
1
+ import { Request } from "express";
2
+ import { reqObj } from "./FormFlux.Types";
3
+ declare class setFileContentToReq {
4
+ private obj;
5
+ private req;
6
+ private for;
7
+ private storage;
8
+ constructor(req: Request, obj: reqObj, forReason: "any" | "fields" | "single", storage: "memory" | "disk");
9
+ setFileNames(fileObj: any, field: string | null): void;
10
+ }
11
+ export default setFileContentToReq;
@@ -0,0 +1,17 @@
1
+ import { Request } from "express";
2
+ import { options, optionSingle, reqObj } from "./FormFlux.Types";
3
+ import FormfluxError from "./FormFluxError";
4
+ declare class writeFileContent {
5
+ private obj;
6
+ private options;
7
+ private req;
8
+ private for;
9
+ private storage;
10
+ constructor(req: Request, obj: reqObj, options: options | optionSingle, forReason: "any" | "fields" | "single", storage: "memory" | "disk");
11
+ writeContent(): void;
12
+ singleFile(count: number, metaData: string, content: Buffer, filesize: number, fieldname: string): void;
13
+ callBackFilename(error: FormfluxError | null, fileName: string): void;
14
+ callBackfilepath(error: FormfluxError | null, filepath: string): void;
15
+ callBackFilter(error: FormfluxError | null, bool: boolean): boolean;
16
+ }
17
+ export default writeFileContent;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _nodeEvents = _interopRequireDefault(require("node:events"));
6
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
7
+ class EventHandlers extends _nodeEvents.default {
8
+ constructor() {
9
+ super();
10
+ }
11
+ emitMessage(eventName, message) {
12
+ this.emit(eventName, message);
13
+ }
14
+ }
15
+ var _default = exports.default = new EventHandlers();
16
+ //# sourceMappingURL=EventHandlers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventHandlers.js","names":["_nodeEvents","_interopRequireDefault","require","e","__esModule","default","EventHandlers","EventEmitter","constructor","emitMessage","eventName","message","emit","_default","exports"],"sources":["../../src/EventHandlers.ts"],"sourcesContent":["import EventEmitter from \"node:events\";\n\n class EventHandlers extends EventEmitter {\n constructor() {\n super();\n }\n\n emitMessage(eventName: string, message: string | object) {\n this.emit(eventName, message);\n }\n}\n\nexport default new EventHandlers();"],"mappings":";;;;AAAA,IAAAA,WAAA,GAAAC,sBAAA,CAAAC,OAAA;AAAuC,SAAAD,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAEtC,MAAMG,aAAa,SAASC,mBAAY,CAAC;EACtCC,WAAWA,CAAA,EAAG;IACV,KAAK,CAAC,CAAC;EACX;EAEAC,WAAWA,CAACC,SAAiB,EAAEC,OAAwB,EAAE;IACrD,IAAI,CAACC,IAAI,CAACF,SAAS,EAAEC,OAAO,CAAC;EACjC;AACJ;AAAC,IAAAE,QAAA,GAAAC,OAAA,CAAAT,OAAA,GAEc,IAAIC,aAAa,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _FormFluxError = _interopRequireDefault(require("./FormFluxError"));
6
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
7
+ class ExtractFileContent {
8
+ constructor(obj, options, fieldArr, singleObj) {
9
+ this.obj = void 0;
10
+ this.options = void 0;
11
+ this.events = void 0;
12
+ this.fieldArr = void 0;
13
+ this.singleObj = void 0;
14
+ this.obj = obj;
15
+ this.options = options;
16
+ this.fieldArr = fieldArr;
17
+ this.singleObj = singleObj;
18
+ }
19
+ extraction() {
20
+ var _this$options, _this$options3, _this$fieldArr, _this$options6;
21
+ for (let val of this.obj.data) {
22
+ if (val.includes("\r\n\r\n") && val.includes("Content-Type")) {
23
+ const [meta, content] = val.split("\r\n\r\n");
24
+ this.obj.fieldNameFile.push(val.split(`name="`)[1].substring(0, val.split(`name="`)[1].indexOf(`"`)));
25
+ this.obj.content.push(Buffer.from(content, "binary"));
26
+ if (this.options && this.options["maxFileSize"] && Buffer.from(content, "binary").length > this.options["maxFileSize"]) throw new _FormFluxError.default("File size exceeded limit", 400);
27
+ this.obj.metaData.push(meta);
28
+ } else if (!val.includes("Content-Type")) {
29
+ this.obj.fieldNameBody.push(val.split(`name="`)[1].substring(0, val.split(`name="`)[1].indexOf(`"`)));
30
+ this.obj.contentBody.push(val == null ? void 0 : val.split("\r\n\r\n")[1].substring(0, val == null ? void 0 : val.split("\r\n\r\n")[1].indexOf("\r\n")));
31
+ }
32
+ }
33
+
34
+ // for single file checks
35
+ if (this.singleObj) {
36
+ let count = 0;
37
+ for (let val of this.obj.metaData) {
38
+ if (val.includes("filename") && val.split(`name="`)[1].substring(0, val.split(`name="`)[1].indexOf(`"`)) == this.singleObj) {
39
+ count++;
40
+ if (count > 1) throw new _FormFluxError.default("Too many files", 429);
41
+ } else if (val.includes("filename") && val.split(`name="`)[1].substring(0, val.split(`name="`)[1].indexOf(`"`)) != this.singleObj) throw new _FormFluxError.default("Unexpected field", 400);
42
+ }
43
+ }
44
+ // check maxfileCount
45
+ if (this.options && (_this$options = this.options) != null && _this$options.maxFileCount) {
46
+ var _this$options2;
47
+ if (this.options.minFileCount && this.options.minFileCount > this.options.maxFileCount) throw new _FormFluxError.default("minFileCount should be less than maxFileCount", 500);
48
+ if (this.obj.content.length > ((_this$options2 = this.options) == null ? void 0 : _this$options2.maxFileCount)) throw new _FormFluxError.default("Too many files", 429);
49
+ }
50
+
51
+ //check minfileCount
52
+ if (this.options && (_this$options3 = this.options) != null && _this$options3.minFileCount) {
53
+ var _this$options4, _this$options5;
54
+ if (this.options.maxFileCount < this.options.minFileCount) throw new _FormFluxError.default("minFileCount should be less than maxFileCount", 500);
55
+ if (this.obj.content.length < ((_this$options4 = this.options) == null ? void 0 : _this$options4.minFileCount)) throw new _FormFluxError.default(`At least ${(_this$options5 = this.options) == null ? void 0 : _this$options5.minFileCount} file(s) required`, 400);
56
+ }
57
+
58
+ //check each fields
59
+ if (this.fieldArr && ((_this$fieldArr = this.fieldArr) == null ? void 0 : _this$fieldArr.length) != 0) {
60
+ let fieldStart = 0;
61
+ let fieldEnd = this.fieldArr.length - 1;
62
+ let fieldObj = {};
63
+ let isCountField = false;
64
+ while (fieldStart <= fieldEnd) {
65
+ fieldObj[`${this.fieldArr[fieldStart].name}`] = [];
66
+ fieldObj[`${this.fieldArr[fieldStart].name}Check`] = false;
67
+ if (fieldStart == fieldEnd) break;
68
+ fieldObj[`${this.fieldArr[fieldEnd].name}`] = [];
69
+ fieldObj[`${this.fieldArr[fieldEnd].name}Check`] = false;
70
+ fieldStart++;
71
+ fieldEnd--;
72
+ }
73
+ if (this.obj.metaData.length != 0) {
74
+ let header;
75
+ for (let val of this.obj.metaData) {
76
+ let count = 0;
77
+ if (val.includes("filename")) {
78
+ for (let item of this.fieldArr) {
79
+ header = val.split(`name="`)[1];
80
+ if (header.substring(0, header.indexOf(`"`)) == item.name) {
81
+ fieldObj[item.name].push(1);
82
+ count++;
83
+
84
+ // check if min is greater then max count
85
+ if (item.minFileCount && item.maxFileCount && item.maxFileCount < item.minFileCount) throw new _FormFluxError.default("minFileCount should be less than maxFileCount", 500);
86
+ if (item.maxFileCount && fieldObj[item.name].length > item.maxFileCount) throw new _FormFluxError.default("Too may files", 429);
87
+
88
+ // set the minCountfield
89
+ if (item.minFileCount && !fieldObj[`${item.name}minCount`]) {
90
+ isCountField = true;
91
+ }
92
+
93
+ // each field filesize check
94
+ if (item.maxFileSize && !fieldObj[`${item.name}Check`]) {
95
+ let rawContent = this.obj.data.filter(x => x.includes(`name="${item.name}"`) && x.includes("Content-Type"));
96
+ rawContent.forEach(cont => {
97
+ if (Buffer.from(cont.split("\r\n\r\n")[1], "binary").length > item.maxFileSize) throw new _FormFluxError.default("File size exceeded limit", 400);
98
+ });
99
+ fieldObj[`${item.name}Check`] = true;
100
+ }
101
+ }
102
+ }
103
+ if (count <= 0) throw new _FormFluxError.default("Unexpected Field", 400); // invalid field
104
+ }
105
+ }
106
+ if (isCountField) {
107
+ let i = 0;
108
+ let filterKeyVals = Object.entries(fieldObj).filter(x => !x[0].includes("Check"));
109
+ for (let i = 0; i < filterKeyVals.length; i++) {
110
+ if (this.fieldArr[i].minFileCount && filterKeyVals[i][1]["length"] < this.fieldArr[i].minFileCount) throw new _FormFluxError.default(`At least ${this.fieldArr[i].minFileCount} file(s) required for ${this.fieldArr[i].name} field`, 400);
111
+ }
112
+ }
113
+ }
114
+ }
115
+ this.obj.data = null; //*******emptying*******
116
+
117
+ //********maxFields validation*******
118
+ if (this.options && (_this$options6 = this.options) != null && _this$options6.maxFields) {
119
+ let countFileFields = 0;
120
+ let countBodyFields = 0;
121
+ if (this.obj.fieldNameFile.length > 0) {
122
+ let obj = {};
123
+ for (let field of this.obj.fieldNameFile) {
124
+ if (!Object.keys(obj).includes(`${field}`)) {
125
+ obj[`${field}`] = 1;
126
+ countFileFields += 1;
127
+ } else continue;
128
+ }
129
+ if (countFileFields > this.options.maxFields) throw new _FormFluxError.default("Too many fields", 429);
130
+ }
131
+ if (this.obj.fieldNameBody.length > 0) {
132
+ let obj = {};
133
+ for (let field of this.obj.fieldNameBody) {
134
+ if (!Object.keys(obj).includes(`${field}`)) {
135
+ obj[`${field}`] = 1;
136
+ countBodyFields += 1;
137
+ } else continue;
138
+ }
139
+ if (countBodyFields > this.options.maxFields) throw new _FormFluxError.default("Too many fields", 429);
140
+ }
141
+ if (countBodyFields + countFileFields > this.options.maxFields) throw new _FormFluxError.default("Too many fields", 429);
142
+ }
143
+ }
144
+ }
145
+ var _default = exports.default = ExtractFileContent;
146
+ //# sourceMappingURL=ExtractContent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExtractContent.js","names":["_FormFluxError","_interopRequireDefault","require","e","__esModule","default","ExtractFileContent","constructor","obj","options","fieldArr","singleObj","events","extraction","_this$options","_this$options3","_this$fieldArr","_this$options6","val","data","includes","meta","content","split","fieldNameFile","push","substring","indexOf","Buffer","from","length","FormfluxError","metaData","fieldNameBody","contentBody","count","maxFileCount","_this$options2","minFileCount","_this$options4","_this$options5","fieldStart","fieldEnd","fieldObj","isCountField","name","header","item","maxFileSize","rawContent","filter","x","forEach","cont","i","filterKeyVals","Object","entries","maxFields","countFileFields","countBodyFields","field","keys","_default","exports"],"sources":["../../src/ExtractContent.ts"],"sourcesContent":["import EventEmitter from \"node:events\";\nimport { optionFields, options, optionSingle, reqObj } from \"./FormFlux.Types\";\nimport FormfluxError from \"./FormFluxError\";\n\nclass ExtractFileContent {\n private obj: reqObj;\n private options: options | optionSingle | null\n private events: EventEmitter;\n private fieldArr: optionFields | null;\n private singleObj: string | null;\n constructor(obj: reqObj, options: options | optionSingle, fieldArr: optionFields | null, singleObj: string | null) {\n\n this.obj = obj;\n this.options = options;\n this.fieldArr = fieldArr;\n this.singleObj = singleObj;\n }\n extraction(): void {\n for (let val of this.obj.data) {\n if (val.includes(\"\\r\\n\\r\\n\") && val.includes(\"Content-Type\")) {\n const [meta, content] = val.split(\"\\r\\n\\r\\n\");\n this.obj.fieldNameFile.push(val.split(`name=\"`)[1].substring(0, val.split(`name=\"`)[1].indexOf(`\"`)));\n this.obj.content.push(Buffer.from(content, \"binary\"));\n\n if (this.options && this.options[\"maxFileSize\"] && Buffer.from(content, \"binary\").length > this.options[\"maxFileSize\"])\n throw new FormfluxError(\"File size exceeded limit\", 400);\n this.obj.metaData.push(meta);\n } else if (!val.includes(\"Content-Type\")) {\n this.obj.fieldNameBody.push(val.split(`name=\"`)[1].substring(0, val.split(`name=\"`)[1].indexOf(`\"`)));\n this.obj.contentBody.push(val?.split(\"\\r\\n\\r\\n\")[1].substring(0, val?.split(\"\\r\\n\\r\\n\")[1].indexOf(\"\\r\\n\")));\n }\n }\n\n // for single file checks\n if (this.singleObj) {\n let count = 0;\n for (let val of this.obj.metaData) {\n if (\n val.includes(\"filename\") &&\n val.split(`name=\"`)[1].substring(0, val.split(`name=\"`)[1].indexOf(`\"`)) == this.singleObj\n ) {\n count++;\n if (count > 1)\n throw new FormfluxError(\"Too many files\", 429);\n }\n else if (val.includes(\"filename\") && val.split(`name=\"`)[1].substring(0, val.split(`name=\"`)[1].indexOf(`\"`)) != this.singleObj)\n throw new FormfluxError(\"Unexpected field\", 400);\n }\n }\n // check maxfileCount\n if (this.options && this.options?.maxFileCount) {\n if (this.options.minFileCount && this.options.minFileCount > this.options.maxFileCount)\n throw new FormfluxError(\"minFileCount should be less than maxFileCount\", 500);\n\n if (this.obj.content.length > this.options?.maxFileCount)\n throw new FormfluxError(\"Too many files\", 429);\n }\n\n //check minfileCount\n if (this.options && this.options?.minFileCount) {\n if (this.options.maxFileCount < this.options.minFileCount)\n throw new FormfluxError(\"minFileCount should be less than maxFileCount\", 500);\n\n if (this.obj.content.length < this.options?.minFileCount)\n throw new FormfluxError(`At least ${this.options?.minFileCount} file(s) required`, 400);\n }\n\n //check each fields\n if (this.fieldArr && this.fieldArr?.length != 0) {\n let fieldStart = 0;\n let fieldEnd = this.fieldArr.length - 1;\n let fieldObj = {};\n let isCountField: boolean = false;\n while (fieldStart <= fieldEnd) {\n fieldObj[`${this.fieldArr[fieldStart].name}`] = [];\n fieldObj[`${this.fieldArr[fieldStart].name}Check`] = false;\n if (fieldStart == fieldEnd) break;\n fieldObj[`${this.fieldArr[fieldEnd].name}`] = [];\n fieldObj[`${this.fieldArr[fieldEnd].name}Check`] = false;\n fieldStart++;\n fieldEnd--;\n }\n\n if (this.obj.metaData.length != 0) {\n let header: string;\n for (let val of this.obj.metaData) {\n let count: number = 0;\n if (val.includes(\"filename\")) {\n for (let item of this.fieldArr) {\n header = val.split(`name=\"`)[1];\n if (header.substring(0, header.indexOf(`\"`)) == item.name) {\n fieldObj[item.name].push(1);\n count++;\n\n // check if min is greater then max count\n if (item.minFileCount && item.maxFileCount && item.maxFileCount < item.minFileCount)\n throw new FormfluxError(\"minFileCount should be less than maxFileCount\", 500);\n\n if (item.maxFileCount && fieldObj[item.name].length > item.maxFileCount)\n throw new FormfluxError(\"Too may files\", 429);\n\n // set the minCountfield\n if (item.minFileCount && !fieldObj[`${item.name}minCount`]) {\n isCountField = true;\n }\n\n // each field filesize check\n if (item.maxFileSize && !fieldObj[`${item.name}Check`]) {\n let rawContent = this.obj.data.filter(x => x.includes(`name=\"${item.name}\"`) && x.includes(\"Content-Type\"));\n rawContent.forEach(cont => {\n\n if (Buffer.from(cont.split(\"\\r\\n\\r\\n\")[1], \"binary\").length > item.maxFileSize)\n throw new FormfluxError(\"File size exceeded limit\", 400);\n });\n fieldObj[`${item.name}Check`] = true;\n }\n }\n }\n if (count <= 0) throw new FormfluxError(\"Unexpected Field\", 400); // invalid field\n }\n }\n\n if (isCountField) {\n let i = 0;\n let filterKeyVals = Object.entries(fieldObj).filter(x => !x[0].includes(\"Check\"));\n for (let i = 0; i < filterKeyVals.length; i++) {\n if (this.fieldArr[i].minFileCount && filterKeyVals[i][1][\"length\"] < this.fieldArr[i].minFileCount)\n throw new FormfluxError(`At least ${this.fieldArr[i].minFileCount} file(s) required for ${this.fieldArr[i].name} field`, 400);\n }\n }\n }\n }\n this.obj.data = null;//*******emptying*******\n\n //********maxFields validation*******\n if (this.options && this.options?.maxFields) {\n let countFileFields = 0;\n let countBodyFields = 0;\n if (this.obj.fieldNameFile.length > 0) {\n\n let obj = {};\n for (let field of this.obj.fieldNameFile) {\n if (!Object.keys(obj).includes(`${field}`)) {\n obj[`${field}`] = 1;\n countFileFields += 1;\n }\n else continue;\n }\n if (countFileFields > this.options.maxFields)\n throw new FormfluxError(\"Too many fields\", 429);\n }\n if (this.obj.fieldNameBody.length > 0) {\n let obj = {};\n for (let field of this.obj.fieldNameBody) {\n if (!Object.keys(obj).includes(`${field}`)) {\n obj[`${field}`] = 1;\n countBodyFields += 1;\n }\n else continue;\n }\n if (countBodyFields > this.options.maxFields)\n throw new FormfluxError(\"Too many fields\", 429);\n }\n if (countBodyFields + countFileFields > this.options.maxFields)\n throw new FormfluxError(\"Too many fields\", 429);\n }\n }\n}\n\nexport default ExtractFileContent;"],"mappings":";;;;AAEA,IAAAA,cAAA,GAAAC,sBAAA,CAAAC,OAAA;AAA4C,SAAAD,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAE5C,MAAMG,kBAAkB,CAAC;EAMrBC,WAAWA,CAACC,GAAW,EAAEC,OAA+B,EAAEC,QAA6B,EAAEC,SAAwB,EAAE;IAAA,KAL3GH,GAAG;IAAA,KACHC,OAAO;IAAA,KACPG,MAAM;IAAA,KACNF,QAAQ;IAAA,KACRC,SAAS;IAGb,IAAI,CAACH,GAAG,GAAGA,GAAG;IACd,IAAI,CAACC,OAAO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,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=FormFlux.Types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FormFlux.Types.js","names":[],"sources":["../../src/FormFlux.Types.ts"],"sourcesContent":["import { Request } from \"express\";\nimport FormfluxError from \"./FormFluxError\";\n\nexport interface reqObj {\n \"originalReq\": string;\n \"modifiedReq\": Buffer;\n \"data\": Array<string>;\n \"content\": Array<Buffer>,\n \"contentBody\": Array<string>,\n \"metaData\": Array<string>,\n \"mimeType\": Array<string>,\n \"fieldNameBody\": Array<string>,\n \"fileName\": Array<string>,\n \"modifiedFileName\": Array<string>,\n \"fieldNameFile\": Array<string>,\n \"filePath\": Array<string>,\n \"filesize\": Array<number>,\n \"streams\": Array<any>\n}\nexport interface File {\n mimetype: string,\n originalname: string,\n filesize: number,\n fieldname: string\n}\n\nexport interface options {\n attachFileToReqBody?: boolean,\n maxFileCount?: number,\n maxFileSize?: number,\n maxFields?: number,\n minFileCount?: number\n filename: (\n req: Request,\n file: File,\n cb: (error: FormfluxError | null, filename: string) => void\n ) => void,\n destination: (\n req: Request,\n file: File,\n cb: (error: FormfluxError | null, filepath: string) => void\n ) => void,\n fileFilter?: (\n req: Request,\n file: File,\n cb: (error: Error | null, bool: boolean) => void\n ) => void\n\n}\n\nexport interface optionSingle {\n attachFileToReqBody?: boolean,\n maxFileCount?: number,\n maxFileSize?: number,\n maxFields?: number,\n minFileCount?: number\n filename: (\n req: Request,\n file: File,\n cb: (error: FormfluxError | null, filename: string) => void\n ) => void,\n fileFilter?: (\n req: Request,\n file: File,\n cb: (error: Error | null, bool: boolean) => void\n ) => void\n\n}\n\ninterface fieldObject {\n name: string,\n maxFileCount?: number,\n maxFileSize?: number,\n minFileCount?: number\n}\n\nexport type optionFields = [fieldObject, ...fieldObject[]];"],"mappings":"","ignoreList":[]}