cojs / busboy Goto Github PK
View Code? Open in Web Editor NEWBusboy multipart parser as a yieldable
Busboy multipart parser as a yieldable
Hey – not sure where to file this – but I had a quick question. Suppose I am using a koa middleware that looks like this:
app.use(function* (next) {
var parts = parse(this), part;
while (part = yield parts) part.pipe(/* somewhere */);
// Here
});
I would like to get the part
stream into a buffer, which I've used a library like accum or concat-stream (https://github.com/jeffbski/accum, https://github.com/maxogden/concat-stream) for before. But it would be really great if I could end up at the // Here
comment, so I could stay in Koa's scope, etc.
Is there a way to do this?
async-csp would pretty much be a drop in replacement for the chan module which as far as I can tell doesn't seem compatible with async/await. I've ran a few tests and async-csp works great so far. Would a PR make sense ?
I found that fileSize works well if we set it large enough , but when we have a small fileSize limit , it not works , for instance if you set fileSize=1024 or fileSize=1 , there is no limit , you can upload file with no size limit , I test it and I see that I can upload 5MB files
I guess , When we set fileSie limit less than highWaterMark it occures
In node-v14.x, some times busboy receive close
event before the finish
event, this will cause socket hang up because of too early cleanup
.
I think it may be referenced with [commit 1428a92492].
Reference: nodejs/node@1428a92492
Idk if you want to add more features to this lib, but right now you'd need a lot of boilerplate code to handle arrays. It's a one-line fix if we do it here . Let me know what you think
I use multipart form to upload file and edit fields, when i upload a file i will get all field values, but i don't upload a file there will always lost a field.
pls check is it busboy bug or something else.
thanks.
LINECONTENT:nodejs.Error: Unsupported content type: multipart/mixed; boundary=409ed440-5bfe-4c7e-b317-1a0735da6e8a
at Busboy.parseHeaders (/data/lifekh-mp-nodejs-mobile-app-composition-sit/ROOT/node_modules/busboy/lib/main.js:75:9)
at new Busboy (/data/lifekh-mp-nodejs-mobile-app-composition-sit/ROOT/node_modules/busboy/lib/main.js:22:10)
at module.exports (/data/lifekh-mp-nodejs-mobile-app-composition-sit/ROOT/node_modules/co-busboy/index.js:31:16)
This happens only on travis-ci, I couldn't figure out exactly what's wrong, but after the request
module update started using [email protected] (bumped patch) my test suite failed on travis, but was ok on ubuntu/osx
What's weird is that this branch fails
https://github.com/kilianc/damn-bug/tree/travis_bug
But this doesn't
https://github.com/kilianc/damn-bug/tree/master
It seems related to setting the headers in request.... O_o
Hello,
In the doc, I did not found these relevant infos. I check the source code of this module as well. These events seems not to be registered or handled. How could I use co-busboy in Koa.js together with the limits check?
Best regards,
Kevin
maybe so we can do parse(this)
== parse(this.req)
koa special sauce
I plan to test a file upload using multipart/form-data in koa2, the below was my code:
var parts = parse(ctx.request);
// or var parts = parse(ctx);
var part;
while (part = await parts) {
var stream = fs.createWriteStream(path.join(os.tmpdir(), Math.random().toString()));
console.log('part===>', part);
part.pipe(stream);
console.log('uploading %s -> %s', part.filename, stream.path);
}
the below was the dump of ctx in log:
ctx===> { request:
{ method: 'POST',
url: '/upload',
header:
{ host: '127.0.0.1:3000',
connection: 'keep-alive',
'content-length': '2316',
'cache-control': 'max-age=0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
origin: 'http://127.0.0.1:3000',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryJl7OKIVqHwYSA5fz',
referer: 'http://127.0.0.1:3000/',
'accept-encoding': 'gzip, deflate',
'accept-language': 'zh-CN,zh;q=0.8' } },
response: { status: 404, message: 'Not Found', header: {} }
but I get the below error as below,my question is :how to get the uploading file name and the content of uploading file ?
[TypeError: part.pipe is not a function]
Need to create a queue of parts in case users yield too long between parts.
with an option to transform it into an array when more than one are present
I have a 1MB limit for files , When I upload a 200KB file with a field like following
------WebKitFormBoundaryOmz20xyMCkE27rN7
Content-Disposition: form-data; name="data";
Content-Type: application/json
{
"description": "description"
}
------WebKitFormBoundaryOmz20xyMCkE27rN7
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: image/jpeg
FILE_DATA
------WebKitFormBoundaryOmz20xyMCkE27rN7
everything is OK and works , But the following has Payload Too Large
------WebKitFormBoundaryOmz20xyMCkE27rN7
Content-Disposition: form-data; name="data"; filename="blob"
Content-Type: application/json
{
"description": "description"
}
------WebKitFormBoundaryOmz20xyMCkE27rN7
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: image/jpeg
FILE_DATA
------WebKitFormBoundaryOmz20xyMCkE27rN7
the diffrence is filename="blob"
when I set filename for json part I have Payload Too Large
error
Probably a follow-up to #22: no length
property.
https://github.com/andrewrk/node-multiparty provides part.byteCount
(where possible), could be nice to have it here. Guess, an issue for the original busboy?
the dependencies busboy 0.2.x
has a dependency dicer
that contains a HIGH level vulnerability CVE-2022-24434
Are you free to upgrade it?
Best Regards👍
var parse = require('co-busboy');
app.use(function* (next) {
if (!this.request.is('multipart/*')) return yield next;
var parts = parse(this, {
checkFile: function (fieldname, filestream, filename) {
if (path.extname(filename) !== '.dat') {
return new Error('invalid filename extension');
}
}
});
var part;
try {
while (part = yield parts) {
if (part.length) {
console.log('key: ' + part[0]);
console.log('value: ' + part[1]);
} else {
part.pipe(fs.createWriteStream('some file.txt'))
}
}
catch (e) {
}
});
when upload a image file, the 'part' is FileStream instance.but it is no length method.
and then can't catch checkFile error. my request url's status always 'pending'.
i don't know why, pls help
I'm looking for a practical and performant method to defer piping of streams until I've validated other parts of the multipart request.
Essentially, I want to limit the files that are stored (streamed to a location on disk, or to the cloud), and then pipe the matching file field streams once everything else is done.
//...
while (part = yield parts) {
//... part is a stream here.
}
//... I should have access to file streams here.
Does anyone know how to achieve this?
Right now co-busboy ignores files/fields with names like 'getOwnProperty'.
That's just wrong. There's no legal reason to ditch fields with valid names.
In theory, such names never happen. But if you think about a specific service for programmers, such names are perfectly normal. I had a problem with that thing and made PR to fix it.
PR #23 removes this technical restriction and fixes this behavior. Guess, the tests should be fixed too.
not a huge deal, but that's one thing in the spec im not a huge fan of, IMO users shouldn't have to worry about wether or not they should be delegating
when request protocol is illegal, it will emit Part terminated early due to unexpected end of multipart data
in node_modules/busboy/node_modules/dicer/lib/Dicer.js
For unknown reason if parts of the form were not piped to another stream in the same while-loop (like it was done here: https://github.com/koajs/examples/blob/master/upload/index.js), but instead they were collected to array for further working with them out of this while-loop, then it gets stuck after it went through all the variables of the form.
E.g.
var yieldedParts = [];
while (part = yield parts) {
yieldedParts.push(part);
console.log('yielded');
}
console.log('went out of while loop');
In this case it will show 'yielded' as many times as you have form variables, but never 'went out of while loop'
I am studying the koa example--multipart,there is my code:
var parts = parse(this.request,{
autoFields:true
});
var tmpdir = path.join(os.tmpdir(),uid());
yield fs.mkdir(tmpdir);
var files = [];
var file;
var part;
while(part = yield parts){
files.push(file = path.join(tmpdir,part.filename));
yield saveTo(part,file);
}
there is a error "Missing content type" when i test use:
<form method="post" action="http://localhost:3000" enctype="multipart/form-data">
<input type="file" name="test">
<input type="submit" value="test">
</form>
so I log the opations.headers,before new Busboy(options)
i found the headers log twice and they are different.
why they log twice and why they are differents?
thanks i am a fresher
If you POST an empty form, it'll keep waiting and it won't respond.
Gist with code sample and command:
https://gist.github.com/cesarandreu/9865520
function onFile(fieldname, file, filename, encoding, mimetype) {
if (checkFile) {
// var err = checkFile(fieldname, file, filename, encoding, mimetype)
// if (err) {
// // make sure request stream's data has been read
// var blackHoleStream = new BlackHoleStream();
// file.pipe(blackHoleStream);
// return onError(err)
// }
checkFile(fieldname, file, filename, encoding, mimetype, ## function (err) {
if (err) {
// make sure request stream's data has been read
var blackHoleStream = new BlackHoleStream();
file.pipe(blackHoleStream);
return onError(err)
}
})
}
parts = parse(this, {
"checkFile": function (fieldname, file, filename, encoding, mimetype,cb) {
file.on("limit", function () {
var err = new Error('=====file size ======')
err.code = 'Request_fileSize_limit'
err.status = 400;
cb(err);
//return err;
});
}, "limits": { "fileSize": 10000000 }
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.