sails.jssails-skipper

Optional file uploads with Sails.js


Is it possible to have a Sails action accept an optional file upload without spitting out the following stacktrace a few seconds after the request?

Upstream (file upload: `image`) emitted an error: { Error: EMAXBUFFER: An upstream (`NOOP_image`) timed out before it was plugged into a receiver. It was still unused after waiting 4500ms. You can configure this timeout by changing the `maxTimeToBuffer` option.

Note that this error might be occurring due to an earlier file upload that is finally timing out after an unrelated server error.
    at Timeout.<anonymous> (/home/jarrod/workspace/cuckold/Cuckold-API/node_modules/skipper/lib/private/Upstream/Upstream.js:86:15)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
  code: 'EMAXBUFFER',
  message: 'EMAXBUFFER: An upstream (`NOOP_image`) timed out before it was plugged into a receiver. It was still unused after waiting 4500ms. You can configure this timeout by changing the `maxTimeToBuffer` option.\n\nNote that this error might be occurring due to an earlier file upload that is finally timing out after an unrelated server error.' }

For completeness I'm using a custom Skipper adapter to dump files in minio which I loosely based off the skipper-s3 adapter, but I also see the same symptoms using the default store-files-in-.tmp-directory adapter.

The below code functions correctly and accepts and stores a single file upload when it's provided in the HTTP request, but if the image field is omitted the above is printed to the console ~4.5 seconds after said request.

The most interesting parts of the action in question follow:

module.exports = {
  friendlyName: 'Create or update a news article',

  files: ['image'],

  inputs: {
    id: {
      type: 'number',
      description: 'ID of article to edit (omit for new articles)'
    },

    content: {
      type: 'string',
      description: 'Markdown-formatted content of news artcle',
      required: true
    },

    image: {
      type: 'ref'
    },
  },

  exits: {
    success: { }
  },

  fn: async function (inputs, exits) {
    let id = inputs.id;
    let article;

    console.log('About to grab upload(s)')
    let uploadedFiles = await (new Promise((resolve, reject) => {
      this.req.file('image').upload({/* ... */}, (err, uploadedFiles) => {
        console.log('Upload stuff callback', [err, uploadedFiles])
        if (err) {
          sails.log.error(`Unable to store uploads for news article`, err.stack)
          return reject(new Error('Unable to store uploads')); // Return a vague message to the client
        }
        resolve(uploadedFiles)
      });
    }));

    console.log('uploadedFiles', uploadedFiles)
    if (uploadedFiles && uploadedFiles.length) {
      uploadedFiles = uploadedFiles.map(f => f.fd)
    } else {
      uploadedFiles = null
    }

    // Do some database updating

    return exits.success({ article });
  }
};

If no upload was in the HTTP request then uploadedFiles is an empty array and the rest of the action code runs without issue, but I don't want logs full of this kind of cruft.


Solution

  • Turns out it's simple really, just pull in the sails-hook-uploads module from npm and replace the Promise-wrapped cruft in the original question with something like this

    let uploadedFiles = await sails.upload(inputs.image, {/* custom adapter config */});
    

    Appears to work as expected but doesn't log a warning that the upload wasn't intercepted in time if none is provided in the request.

    Led to this solution by the Ration example app, here.