|
| 1 | +<a name="0.11.0-beta1"></a> |
| 2 | +# 0.11.0-beta1 (2024-12-04) |
| 3 | + |
| 4 | +## Changes |
| 5 | + |
| 6 | +### Using 0.15 new streaming solution |
| 7 | + |
| 8 | +The [email protected] supports Moleculer v0.15.x including the new streaming solution. It means, it doesn't support 0.13 and 0.14 moleculer versions. |
| 9 | + |
| 10 | +Thanks for the new solution, the multipart fields and request parameters are sent via `ctx.params` instead of meta and the file stream is available in `ctx.stream` in action handlers. |
| 11 | + |
| 12 | +**Example** |
| 13 | + |
| 14 | +```js |
| 15 | +module.exports = { |
| 16 | + name: "file", |
| 17 | + actions: { |
| 18 | + save: { |
| 19 | + handler(ctx) { |
| 20 | + return new this.Promise((resolve, reject) => { |
| 21 | + const filePath = path.join(uploadDir, ctx.params.$filename); |
| 22 | + const f = fs.createWriteStream(filePath); |
| 23 | + f.on("close", () => { |
| 24 | + // File written successfully |
| 25 | + this.logger.info(`Uploaded file stored in '${filePath}'`); |
| 26 | + resolve({ filePath }); |
| 27 | + }); |
| 28 | + |
| 29 | + ctx.stream.on("error", err => { |
| 30 | + this.logger.info("File error received", err.message); |
| 31 | + reject(err); |
| 32 | + |
| 33 | + // Destroy the local file |
| 34 | + f.destroy(err); |
| 35 | + }); |
| 36 | + |
| 37 | + f.on("error", () => { |
| 38 | + // Remove the errored file. |
| 39 | + fs.unlinkSync(filePath); |
| 40 | + }); |
| 41 | + |
| 42 | + ctx.stream.pipe(f); |
| 43 | + }); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +}; |
| 48 | +``` |
| 49 | + |
| 50 | +Example content of `ctx.params`: |
| 51 | + |
| 52 | +```json |
| 53 | +{ |
| 54 | + // Multipart file properties |
| 55 | + $fieldname: "myfile", |
| 56 | + $filename: "avatar.png", |
| 57 | + $encoding: "7bit", |
| 58 | + $mimetype: "image/png", |
| 59 | + |
| 60 | + // Other multipart fields |
| 61 | + // e.g.: `<input type="text" name="name" id="name" value="Test User">` |
| 62 | + name: "Test User", |
| 63 | + |
| 64 | + // Request path parameter, e.g.: `/upload/single/1234` |
| 65 | + id: "1234" |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +----------------------------- |
1 | 71 | <a name="0.10.7"></a>
|
2 | 72 | # 0.10.7 (2023-11-12)
|
3 | 73 |
|
|
0 commit comments