Skip to content

Commit

Permalink
using new file streaming in 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Dec 4, 2024
1 parent 4c87203 commit 16e61ec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 66 deletions.
2 changes: 2 additions & 0 deletions examples/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ broker.createService({
},

aliases: {
"GET /:file": "file.get",

// File upload from HTML form
"POST /": "multipart:file.save",

Expand Down
16 changes: 7 additions & 9 deletions examples/formidable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ broker.createService({
},

aliases: {
"GET /:file": "file.get",

// File upload from AJAX or cURL
"PUT /": "stream:file.save",

Expand Down Expand Up @@ -91,15 +93,11 @@ broker.createService({
const ctx = req.$ctx;
const entries = Array.isArray(files.myfile) ? files.myfile : [files.myfile];
const result = await Promise.all(entries.map(entry => {
return ctx.call("file.save", fs.createReadStream(entry.path), {
meta: {
filename: entry.name,
$params: {
...req.params,
...fields,
}
}
});
return ctx.call("file.save", {
$filename: entry.name,
...req.params,
...fields,
}, { stream: fs.createReadStream(entry.path) });
}));

return this.sendResponse(req, res, Array.isArray(files.myfile) ? result : result[0]);
Expand Down
96 changes: 39 additions & 57 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3615,16 +3615,16 @@ describe("Test file uploading", () => {
actions: {
save(ctx) {
return new this.Promise((resolve) => {
if (ctx.params.pipe) {
if (ctx.stream && ctx.stream.pipe) {
const hash = crypto.createHash("sha256");

hash.on("readable", () => {
const data = hash.read();
if (data)
resolve({ hash: data.toString("base64"), meta: ctx.meta });
resolve({ hash: data.toString("base64"), params: ctx.params, meta: ctx.meta });
});

ctx.params.pipe(hash);
ctx.stream.pipe(hash);
} else {
resolve({ params: ctx.params, meta: ctx.meta });
}
Expand Down Expand Up @@ -3722,14 +3722,12 @@ describe("Test file uploading", () => {
.then(res => {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
$multipart: {},
$params: {},
encoding: "7bit",
fieldname: "myFile",
filename: "logo.png",
mimetype: "image/png"
} });
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: {
$encoding: "7bit",
$fieldname: "myFile",
$filename: "logo.png",
$mimetype: "image/png"
}, meta: {} });

expect(onFilesLimitFn).toHaveBeenCalledTimes(0);
});
Expand All @@ -3738,21 +3736,18 @@ describe("Test file uploading", () => {
it("should send data field with multipart", () => {
return request(server)
.post("/upload")
.attach("myFile", assetsDir + "logo.png")
.field("name", "moleculer")
.attach("myFile", assetsDir + "logo.png")
.then(res => {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
$multipart: {
name: "moleculer"
},
$params: {},
encoding: "7bit",
fieldname: "myFile",
filename: "logo.png",
mimetype: "image/png"
} });
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: {
name: "moleculer",
$encoding: "7bit",
$fieldname: "myFile",
$filename: "logo.png",
$mimetype: "image/png"
}, meta: {} });

expect(onFilesLimitFn).toHaveBeenCalledTimes(0);
});
Expand All @@ -3767,11 +3762,8 @@ describe("Test file uploading", () => {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
expect(res.body).toEqual([{
meta: {
$multipart: { name: "moleculer", more: "services" },
$params: { id: "f1234" }
},
params: {}
meta: {},
params: { id: "f1234", name: "moleculer", more: "services" }
}]);
});
});
Expand All @@ -3784,14 +3776,12 @@ describe("Test file uploading", () => {
.then(res => {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
$multipart: {},
$params: {},
encoding: "7bit",
fieldname: "myFile",
filename: "logo.png",
mimetype: "image/png"
} });
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: {
$encoding: "7bit",
$fieldname: "myFile",
$filename: "logo.png",
$mimetype: "image/png"
} , meta: {} });
expect(onFilesLimitFn).toHaveBeenCalledTimes(1);
expect(onFilesLimitFn).toHaveBeenCalledWith(expect.any(Busboy), expect.any(Alias), service);
});
Expand All @@ -3806,22 +3796,18 @@ describe("Test file uploading", () => {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
expect(res.body).toEqual([
{ hash: origHashes["logo.png"], meta: {
$multipart: {},
$params: {},
encoding: "7bit",
fieldname: "myFile",
filename: "logo.png",
mimetype: "image/png"
} },
{ hash: origHashes["lorem.txt"], meta: {
$multipart: {},
$params: {},
encoding: "7bit",
fieldname: "myText",
filename: "lorem.txt",
mimetype: "text/plain"
} }
{ hash: origHashes["logo.png"], params: {
$encoding: "7bit",
$fieldname: "myFile",
$filename: "logo.png",
$mimetype: "image/png"
}, meta: {} },
{ hash: origHashes["lorem.txt"], params: {
$encoding: "7bit",
$fieldname: "myText",
$filename: "lorem.txt",
$mimetype: "text/plain"
}, meta: {} }
]);
});
});
Expand All @@ -3835,9 +3821,7 @@ describe("Test file uploading", () => {
.then(res => {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
$params: {},
} });
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: {}, meta: {} });
});

});
Expand All @@ -3851,9 +3835,7 @@ describe("Test file uploading", () => {
.then(res => {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
$params: { id: "f1234" },
} });
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: { id: "f1234" }, meta: {} });
});
});

Expand Down

0 comments on commit 16e61ec

Please sign in to comment.