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 3a66b16 commit 4c87203
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 96 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: node_modules
key: ${{ matrix.node-version }}-node-${{ hashFiles('**/package-lock.json') }}
Expand Down
10 changes: 5 additions & 5 deletions examples/file.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ module.exports = {

save: {
handler(ctx) {
this.logger.info("Received upload $params:", ctx.meta.$params);
this.logger.info("Received upload params:", ctx.params);
return new this.Promise((resolve, reject) => {
//reject(new Error("Disk out of space"));
const filePath = path.join(uploadDir, ctx.meta.filename || this.randomName());
const filePath = path.join(uploadDir, ctx.params.$filename || this.randomName());
const f = fs.createWriteStream(filePath);
f.on("close", () => {
// File written successfully
this.logger.info(`Uploaded file stored in '${filePath}'`);
resolve({ filePath, meta: ctx.meta });
resolve({ filePath, params: ctx.params });
});

ctx.params.on("error", err => {
ctx.stream.on("error", err => {
this.logger.info("File error received", err.message);
reject(err);

Expand All @@ -70,7 +70,7 @@ module.exports = {
fs.unlinkSync(filePath);
});

ctx.params.pipe(f);
ctx.stream.pipe(f);
});
}
}
Expand Down
121 changes: 58 additions & 63 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"jsonwebtoken": "^8.5.1",
"mime-types": "^2.1.33",
"mkdirp": "^1.0.4",
"moleculer": "github:moleculerjs/moleculer#next",
"moleculer": "next",
"moleculer-repl": "^0.7.0",
"nats": "^2.2.0",
"nodemon": "^2.0.13",
Expand Down
16 changes: 4 additions & 12 deletions src/alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Alias {
*/
multipartHandler(req, res) {
const ctx = req.$ctx;
ctx.meta.$multipart = {};
const multipartParams = {};
const promises = [];

let numOfFiles = 0;
Expand All @@ -161,21 +161,15 @@ class Alias {
file.destroy(new PayloadTooLarge({ fieldname, filename, encoding, mimetype }));
});
numOfFiles++;
promises.push(ctx.call(this.action, file, _.defaultsDeep({}, this.route.opts.callOptions, { meta: {
fieldname: fieldname,
filename: filename,
encoding: encoding,
mimetype: mimetype,
$params: req.$params,
} })).catch(err => {
promises.push(ctx.call(this.action, _.defaultsDeep({ $fieldname: fieldname, $filename: filename, $encoding: encoding, $mimetype: mimetype }, multipartParams, req.$params), _.defaultsDeep({ stream: file }, this.route.opts.callOptions)).catch(err => {
file.resume(); // Drain file stream to continue processing form
busboy.emit("error", err);
return err;
}));
});
busboy.on("field", (field, value) => {
hasField = true;
ctx.meta.$multipart[field] = value;
multipartParams[field] = value;
});

busboy.on("finish", async () => {
Expand All @@ -185,9 +179,7 @@ class Alias {

// Call the action if no files but multipart fields
if (numOfFiles == 0 && hasField) {
promises.push(ctx.call(this.action, {}, _.defaultsDeep({}, this.route.opts.callOptions, { meta: {
$params: req.$params,
} })));
promises.push(ctx.call(this.action, _.defaultsDeep({}, multipartParams, req.$params), _.defaultsDeep({}, this.route.opts.callOptions)));
}

try {
Expand Down
Loading

0 comments on commit 4c87203

Please sign in to comment.