Skip to content

Commit 4c87203

Browse files
committed
using new file streaming in 0.15
1 parent 3a66b16 commit 4c87203

File tree

6 files changed

+83
-96
lines changed

6 files changed

+83
-96
lines changed

.github/workflows/nodejs.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ jobs:
88

99
strategy:
1010
matrix:
11-
node-version: [18.x, 20.x]
11+
node-version: [18.x, 20.x, 22.x]
1212

1313
steps:
14-
- uses: actions/checkout@v3
14+
- uses: actions/checkout@v4
1515
- name: Use Node.js ${{ matrix.node-version }}
16-
uses: actions/setup-node@v3
16+
uses: actions/setup-node@v4
1717
with:
1818
node-version: ${{ matrix.node-version }}
19-
- uses: actions/cache@v3
19+
- uses: actions/cache@v4
2020
with:
2121
path: node_modules
2222
key: ${{ matrix.node-version }}-node-${{ hashFiles('**/package-lock.json') }}

examples/file.service.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ module.exports = {
4646

4747
save: {
4848
handler(ctx) {
49-
this.logger.info("Received upload $params:", ctx.meta.$params);
49+
this.logger.info("Received upload params:", ctx.params);
5050
return new this.Promise((resolve, reject) => {
5151
//reject(new Error("Disk out of space"));
52-
const filePath = path.join(uploadDir, ctx.meta.filename || this.randomName());
52+
const filePath = path.join(uploadDir, ctx.params.$filename || this.randomName());
5353
const f = fs.createWriteStream(filePath);
5454
f.on("close", () => {
5555
// File written successfully
5656
this.logger.info(`Uploaded file stored in '${filePath}'`);
57-
resolve({ filePath, meta: ctx.meta });
57+
resolve({ filePath, params: ctx.params });
5858
});
5959

60-
ctx.params.on("error", err => {
60+
ctx.stream.on("error", err => {
6161
this.logger.info("File error received", err.message);
6262
reject(err);
6363

@@ -70,7 +70,7 @@ module.exports = {
7070
fs.unlinkSync(filePath);
7171
});
7272

73-
ctx.params.pipe(f);
73+
ctx.stream.pipe(f);
7474
});
7575
}
7676
}

package-lock.json

+58-63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"jsonwebtoken": "^8.5.1",
5050
"mime-types": "^2.1.33",
5151
"mkdirp": "^1.0.4",
52-
"moleculer": "github:moleculerjs/moleculer#next",
52+
"moleculer": "next",
5353
"moleculer-repl": "^0.7.0",
5454
"nats": "^2.2.0",
5555
"nodemon": "^2.0.13",

src/alias.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Alias {
144144
*/
145145
multipartHandler(req, res) {
146146
const ctx = req.$ctx;
147-
ctx.meta.$multipart = {};
147+
const multipartParams = {};
148148
const promises = [];
149149

150150
let numOfFiles = 0;
@@ -161,21 +161,15 @@ class Alias {
161161
file.destroy(new PayloadTooLarge({ fieldname, filename, encoding, mimetype }));
162162
});
163163
numOfFiles++;
164-
promises.push(ctx.call(this.action, file, _.defaultsDeep({}, this.route.opts.callOptions, { meta: {
165-
fieldname: fieldname,
166-
filename: filename,
167-
encoding: encoding,
168-
mimetype: mimetype,
169-
$params: req.$params,
170-
} })).catch(err => {
164+
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 => {
171165
file.resume(); // Drain file stream to continue processing form
172166
busboy.emit("error", err);
173167
return err;
174168
}));
175169
});
176170
busboy.on("field", (field, value) => {
177171
hasField = true;
178-
ctx.meta.$multipart[field] = value;
172+
multipartParams[field] = value;
179173
});
180174

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

186180
// Call the action if no files but multipart fields
187181
if (numOfFiles == 0 && hasField) {
188-
promises.push(ctx.call(this.action, {}, _.defaultsDeep({}, this.route.opts.callOptions, { meta: {
189-
$params: req.$params,
190-
} })));
182+
promises.push(ctx.call(this.action, _.defaultsDeep({}, multipartParams, req.$params), _.defaultsDeep({}, this.route.opts.callOptions)));
191183
}
192184

193185
try {

0 commit comments

Comments
 (0)