-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·59 lines (58 loc) · 1.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createReadStream, createWriteStream, unlink } from "node:fs";
import os from "node:os";
import { cwd } from "node:process";
import busboy from "busboy";
import { type Context, JetPlugin } from "jetpath";
import path from "node:path";
export const jetbusboy = new JetPlugin({
name: "busboyjet",
version: "1.0.0",
executor({}) {
return {
formData(ctx: Context) {
return new Promise((res, rej) => {
const data: Record<string, any> = {};
try {
const bb = busboy({ headers: ctx.request.headers });
bb.on("file", (name: string, file: any, info: any) => {
const oldPath = path.join(
os.tmpdir(),
info.filename + Date.now(),
);
info.location = oldPath;
info.saveTo = (name: string): Promise<string> => {
const newPath = path.resolve(cwd(), name);
return new Promise<string>((res, rej) => {
var readStream = createReadStream(oldPath);
var writeStream = createWriteStream(newPath);
readStream.on("error", (e) => {
rej(e);
});
writeStream.on("error", (e) => {
rej(e);
});
readStream.on("close", function () {
unlink(oldPath, (_e) => {});
res(name);
});
readStream.pipe(writeStream);
});
};
data[name] = info;
file.pipe(createWriteStream(oldPath));
});
bb.on("field", (name: string, val: any) => {
data[name] = val !== "undefined" ? val : undefined;
});
bb.on("close", () => {
res(data);
});
ctx.request.pipe(bb);
} catch (error) {
rej(error);
}
});
},
};
},
});