tus / tus-node-server

Node.js tus server, standalone or integrable in any framework, with disk, S3, Azure, and GGC stores.
https://tus.io/
MIT License
824 stars 201 forks source link

@tus/server: Metadata on namingFunction + propagate error #558

Closed fenos closed 9 months ago

fenos commented 9 months ago

This small PR simply allows error propagation in the namingFunction.

Currently, if an error is thrown from the namingFunction the error is swallowed and a 500 error code is returned. This behavior is not ideal, in my use case I rely on the metadata in order to create a valid uploadID.

For this reason the metadata is validated in the naming function. If some of the fields don't pass the validation instead of returning a 400 status code, the application returns a non-ideal 500

fenos commented 9 months ago

@Murderlon i think it makes a lot of sense to also pass the parsed Metadata in the namingFunction as an optional second param for example:

function namingFunction(req, metadata) {
  if (!metadata) {
     throw new Error('must provide metadata')
  }

  // validate metadata
  return metadata.fileName
}
Murderlon commented 9 months ago

Sounds good 👍

We can add the metadata argument in this PR as well

fenos commented 9 months ago

Done 🎉

Murderlon commented 9 months ago

I'll do a release tomorrow.