Skip to content

Commit

Permalink
Refactor code; update package version to 3.2.13
Browse files Browse the repository at this point in the history
  • Loading branch information
FlameWolf committed May 6, 2024
1 parent f86044b commit 866c5c8
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CallbackStorage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FileInfo } from "busboy";
import { FileHandler, StorageOption } from "./index";

export declare class CallbackStorage implements StorageOption {
callback: FileHandler;
#callback: FileHandler;
constructor(callback: FileHandler);
process(name: string, stream: Readable, info: FileInfo): import("./index").File | Promise<import("./index").File>;
}
6 changes: 3 additions & 3 deletions CallbackStorage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";

class CallbackStorage {
callback;
#callback;
constructor(callback) {
this.callback = callback;
this.#callback = callback;
}
process(name, stream, info) {
return this.callback(name, stream, info);
return this.#callback(name, stream, info);
}
}

Expand Down
2 changes: 1 addition & 1 deletion DiscStorage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Readable } from "stream";
import { FileInfo } from "busboy";

export declare class DiscStorage implements StorageOption {
target: TargetType;
#target: TargetType;
constructor(target: TargetType);
process(name: string, stream: Readable, info: FileInfo): Promise<File>;
}
6 changes: 3 additions & 3 deletions DiscStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const os = require("os");
const fs = require("fs");

class DiscStorage {
target;
#target;
constructor(target) {
this.target = target;
this.#target = target;
}
process(name, stream, info) {
const target = this.target;
const target = this.#target;
const file = new FileInternal(name, info);
const saveLocation = typeof target === "function" ? target(file) : target;
const filePath = path.join(saveLocation?.directory || os.tmpdir(), saveLocation?.fileName || file.originalName);
Expand Down
5 changes: 5 additions & 0 deletions FieldParserNoSchema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FieldParser } from "./index";

export declare class FieldParserNoSchema implements FieldParser {
parseField(name: string, value: any): any;
}
9 changes: 9 additions & 0 deletions FieldParserNoSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

class FieldParserNoSchema {
parseField(name, value) {
return value;
}
}

exports.FieldParserNoSchema = FieldParserNoSchema;
7 changes: 7 additions & 0 deletions FieldParserWithSchema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Dictionary, FieldParser } from "./index";

export declare class FieldParserWithSchema implements FieldParser {
#props: Dictionary;
constructor(props: Dictionary);
parseField(name: string, value: any): any;
}
20 changes: 20 additions & 0 deletions FieldParserWithSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

class FieldParserWithSchema {
#props;
constructor(props) {
this.#props = props;
}
parseField(name, value) {
if (this.#props[name]?.type !== "string") {
try {
return JSON.parse(value);
} catch {
void 0;
}
}
return value;
}
}

exports.FieldParserWithSchema = FieldParserWithSchema;
9 changes: 6 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ export interface File {
data: Buffer | undefined;
error: Error | undefined;
}
export declare type FileHandler = (name: string, stream: Readable, info: FileInfo) => File | Promise<File>;
export type FileHandler = (name: string, stream: Readable, info: FileInfo) => File | Promise<File>;
export interface StorageOption {
process: FileHandler;
}
export interface FileSaveTarget {
directory?: string;
fileName?: string;
}
export declare type TargetType = FileSaveTarget | ((source: File) => FileSaveTarget);
export type TargetType = FileSaveTarget | ((source: File) => FileSaveTarget);
export interface FormDataParserPluginOptions extends FastifyPluginOptions {
limits?: Limits;
storage?: StorageOption;
}
export declare type FormDataParserPlugin = FastifyPluginAsync<FormDataParserPluginOptions> & Dictionary;
export type FormDataParserPlugin = FastifyPluginAsync<FormDataParserPluginOptions> & Dictionary;
export interface FieldParser {
parseField(name: string, value: any): any;
}
declare module "fastify" {
interface FastifyRequest {
__files__?: Array<File>;
Expand Down
13 changes: 4 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
"use strict";

const { StreamStorage } = require("./StreamStorage");
const { FieldParserNoSchema } = require("./FieldParserNoSchema");
const { FieldParserWithSchema } = require("./FieldParserWithSchema");
const busboy = require("busboy");
const { finished } = require("stream");

const tryParse = value => {
try {
return JSON.parse(value);
} catch {
return value;
}
};
const formDataParser = async (instance, options) => {
const { limits, storage = new StreamStorage() } = options;
instance.addContentTypeParser("multipart/form-data", (request, message, done) => {
const results = [];
const body = new Map();
const props = request.routeOptions.schema?.body?.properties;
const parseField = props ? (name, value) => (props[name]?.type === "string" ? value : tryParse(value)) : (name, value) => value;
const parser = props ? new FieldParserWithSchema(props) : new FieldParserNoSchema();
const bus = busboy({ headers: message.headers, limits, defParamCharset: "utf8" });
bus.on("file", (name, stream, info) => {
results.push(storage.process(name, stream, info));
Expand All @@ -33,7 +28,7 @@ const formDataParser = async (instance, options) => {
body.set(name, [fileProp, JSON.stringify(info)]);
});
bus.on("field", (name, value) => {
body.set(name, parseField(name, value));
body.set(name, parser.parseField(name, value));
});
finished(bus, (err = null) => {
Promise.all(results).then(files => {
Expand Down
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formzilla",
"version": "3.2.12",
"version": "3.2.13",
"description": "Fastify plugin for parsing multipart/form data",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,8 +32,8 @@
"@fastify/swagger"
],
"devDependencies": {
"@types/busboy": "^1.5.3",
"ava": "^6.1.2",
"@types/busboy": "^1.5.4",
"ava": "^6.1.3",
"form-data": "^4.0.0"
},
"ava": {
Expand Down

0 comments on commit 866c5c8

Please sign in to comment.