-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to stream based API #399
base: master
Are you sure you want to change the base?
Conversation
Love this idea. It is much simpler, more modular, and would solve way more use cases than the current situation (#356 for example). I don't have time now to read all the code, but in theory this has my thumbs up. I will try to look through it more throughly this weekend. |
My only issue @LinusU is to fix it so that you expose |
Just released 🎉 |
@niftylettuce I'll look into the koa-issue shortly 👌 |
You are a rockstar. How do I donate to you? On Oct 1, 2016 10:02 AM, "Linus Unnebäck" notifications@github.com wrote:
|
Also @LinusU - is limits option still enforced? |
There are others who need it more than I, e.g. https://my.charitywater.org/donate/home
Yes they are 👌 |
To try it out, use the following command: |
👍🎉👍
|
It's working great for me! I have a fork of |
Solves many problems, thanks! But I don't understand why no encoding and mimetype properties in a file? I think it's critical. |
Oh nevermind, mime type is usually determined by file's extension, and file encoding would not help at anything. Correct if wrong. |
That is correct, as I have understood it the encoding is only how it's sent over the wire and doesn't matter at all for the end user |
lib/middleware.js
Outdated
var stream = fs.createReadStream(file.path) | ||
|
||
stream.on('end', function () { | ||
fs.unlink(file.path, function () {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we can unlink this file as soon as it has been opened, that way the file wills still be removed even if it isn't consumed. More info: http://stackoverflow.com/a/3181726/148072
It's not yet stable in Node, but WHATWG streams seem like they'd be a nice addition here eventually. That might be Multer 3.0, though! |
Hi @LinusU, I was testing on one of my (typescript) project, and it seem to throw some ESM error while running with the latest
This is my {
"ts-node": {
// See https://github.com/TypeStrong/ts-node/issues/922
// these options are overrides used only by ts-node
// same as our --compilerOptions flag and our TS_NODE_COMPILER_OPTIONS environment variable
"compilerOptions": {
"module": "commonjs"
}
},
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"outDir": "./lib",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"exclude": [
"test"
]
} And I'm using import multer from 'multer';
const MAX_FILE_SIZE = 5242880;
const MAX_FILE_UPLOAD_LIMIT = 5;
// Instantiate multer
const multerware = multer({
limits: {
fileSize: MAX_FILE_SIZE,
files: MAX_FILE_UPLOAD_LIMIT,
},
});
multerware.array('files'), If I add I switched back to I'm not familiar with the whole ESM thingy, any idea? |
@bwgjoseph I think that if you follow this answer it should help you get TypeScript up and running with ESM: |
So I used this config as mentioned in the SO post you linked "target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"outDir": "./lib",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true where I stick to $ npm run mocha
> feathers-ottoman-demo@0.0.0 mocha \bwgjoseph\feathers-ottoman-demo
> ts-mocha "test/**/*.ts" --recursive --exit
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for \bwgjoseph\feathers-ottoman-demo\test\app.test.ts
at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
at Loader.getFormat (internal/modules/esm/loader.js:102:42)
at Loader.getModuleJob (internal/modules/esm/loader.js:231:31)
at Loader.import (internal/modules/esm/loader.js:165:17)
at formattedImport (\bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\nodejs\esm-utils.js:7:14)
at Object.exports.requireOrImport (\bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\nodejs\esm-utils.js:48:32)
at Object.exports.loadFilesAsync (\bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\nodejs\esm-utils.js:88:20)
at singleRun (\bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\cli\run-helpers.js:125:3)
at Object.exports.handler (\bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\cli\run.js:374:5) Not exactly sure why it points to Say that I follow what the poster mentioned and set the config to "target": "ES2020",
"module": "ES2020",
"lib": ["ES2020"],
"outDir": "./lib",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node"
// and adding this to package.json
"type": "module" It wouldn't even run in $ npm run mocha
> feathers-ottoman-demo@0.0.0 mocha bwgjoseph\feathers-ottoman-demo
> ts-mocha "test/**/*.ts" --recursive --exit
bwgjoseph\feathers-ottoman-demo\test\app.test.ts:1
import assert from 'assert';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Module.m._compile (bwgjoseph\feathers-ottoman-demo\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .ts] (bwgjoseph\feathers-ottoman-demo\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.exports.requireOrImport (bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\nodejs\esm-utils.js:56:20)
at Object.exports.loadFilesAsync (bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\nodejs\esm-utils.js:88:20)
at singleRun (bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\cli\run-helpers.js:125:3)
at Object.exports.handler (bwgjoseph\feathers-ottoman-demo\node_modules\mocha\lib\cli\run.js:374:5) Can't quite figure out the cause of it. If you think this has nothing to do with the recent change, then I'll figure out something myself. Was thinking not sure if this was caused by changes in |
@bwgjoseph I think that your problem is generic to using any ESM module with your specific setup (which seems to be TypeScript + I haven't personally used Could you try to switch our your - ts-mocha "test/**/*.ts" --recursive --exit
+ node --loader ts-node/esm node_modules/.bin/mocha "test/**/*.ts" --recursive --exit You can find more information about this here: TypeStrong/ts-node#1007 As outlined in those step, you also need to:
I believe that you have already done at least the three first steps. The fourth is admittedly a bit weird when working with TypeScript, so the easiest way to get up and running is probably to pass the - ts-mocha "test/**/*.ts" --recursive --exit
+ node --loader ts-node/esm --experimental-specifier-resolution=node node_modules/.bin/mocha "test/**/*.ts" --recursive --exit There is also more support coming with the next version of TypeScript, which is currently in beta. See the release notes here: https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/. Potentially that will help making this smoother as well! For some background on why this change is happening, I would recommend some of these resources: |
Hey @LinusU, Thanks for helping me out, appreciate it. I still am not able to get it working properly even with the different trials and errors. I think I don't understand enough of all these, will do up more reading first. So I reverted back to And I also don't want to clutter this thread further more if this is not a Cheers! |
Maybe use this opportunity to update all packages to their new major versions? And maybe remove mkdirp and replace it with its native counterpart https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fs_fspromises_mkdir_path_options / https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsmkdirpath-options-callback Edit: e.g I did an almost working update (tests hang at the end) at https://github.com/mohe2015/multer/tree/explore-new-api-update-esm But this also includes switching to esm which may not be wanted. Also this was not tested at all except running the tests |
@mohe2015 I believe that this is already done in this branch? It's using ESM and the latest major version of all dependencies? |
Yeah seems like I messed something up. Still the conflicts probably need to be resolved at some point but I don't know if this would be needed now. |
When we release version 2.0.0 I will rename the |
Update fs-temp
Bump 😉 |
Replace unsupported busboy version with fastify fork
@LinusU How do you feel about just releasing the 2.0 and seeing how it goes? That way more people get to try it and any bugs that might exist in it will surface quicker. Also gives the community a chance to contribute if bugs are found. What do you think? In any case thanks for this project, it's helped me out a lot! 😁 |
As have been discussed in a few other issues, I thought it would be interested to see what the api would look like if Multer just gave you
.stream
on the uploaded files. This removes the concept of storage engines completely and you can use any other module that works with streams, instead of multer-specific modules.I would love some thoughts and feedback on this ❤️
ping @expressjs/multer-collaborators, @wesleytodd