question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi, I’m developing a Rest API with Nest.js. I successfully converted it to a monolithic lambda with aws-serverless-express with the next code:

const binaryMimeTypes: string[] = ['application/octet-stream'];

let cachedServer: Server;

async function bootstrapServer(): Promise<Server> {
  if (!cachedServer) {
    const expressApp = express();
    const nestApp = await NestFactory.create(
      AppModule,
      new ExpressAdapter(expressApp)
    );
    nestApp.use(eventContext());
    nestApp.enableCors();
    await nestApp.init();
    cachedServer = createServer(expressApp, undefined, binaryMimeTypes);
  }
  return cachedServer;
}

export const handler: Handler = async (
  event: APIGatewayEvent,
  context: Context
) => {
  cachedServer = await bootstrapServer();
  return proxy(cachedServer, event, context, 'PROMISE').promise;
};

For development, I use serverless-offline and serverless-webpack.

When I try to send an image with multipart/form-data to the /upload controller, it throws me an error regardless of the image type. ** With other file types (like .txt or .env) it works as expected. Before moving the app to lambda, it worked without any issues.

Controller:

 @Post('upload')
 @UseInterceptors(AnyFilesInterceptor())
 async upload(@UploadedFiles() files: any) {
    console.log('photos', files);
  }

Sending text file:

files [ { fieldname: 'file',
    originalname: 'expretiments.js',
    encoding: '7bit',
    mimetype: 'application/javascript',
    buffer:
     <Buffer 2f 2f 20 63 6f 6e 73 74 20 67 63 64 20 3d 20 28 61 2c 20 62 29 20 3d 3e 20 7b 0d 0a 2f 2f 20 09 77 68 69 6c 65 28 62 29 20 7b 0d 0
a 2f 2f 20 09 09 69 ... >,
    size: 8537 } ]

Error log (sending .png):

[Nest] 8276   - 09/20/2019, 12:18:42 PM   [ExceptionsHandler] Unexpected end of multipart data +149ms
Error: Unexpected end of multipart data
    at D:\web\_projects\new-book-2-wheel\server\node_modules\dicer\lib\Dicer.js:62:28
    at process._tickCallback (internal/process/next_tick.js:61:11)
ERROR: aws-serverless-express connection error
{ Error: write EOF
    at WriteWrap.afterWrite (net.js:788:14) errno: 'EOF', code: 'EOF', syscall: 'write' }

What I tried:

  • changing multer to express-form-data
  • playing around with binaryMimeTypes passed to createServer()
  • using aws-lambda-multipart-parser package

aws-serverless-express: 3.3.6 nodejs: v10.16.0 Current workaround: send images in base64 format

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

8reactions
shelooks16commented, Sep 24, 2019

Although the setup above works during development, in deployment it causes images being broken. I upload images to S3. Local upload is OK, when deployed - corrupted.

According to this post: https://stackoverflow.com/a/41770688, API gateway needs additional configuration to process binaries. To make it more or less automated, I simply installed serverless-apigw-binary and put */* wildcard.

serverless.yml:

plugins:
  - serverless-apigw-binary

custom:
  apigwBinary:
    types:
      - '*/*' 

handler:

const binaryMimeTypes: string[] = [];

if (
  event.body &&
  event.headers['Content-Type'].includes('multipart/form-data') &&
  process.env.NODE_ENV !== 'production' // added
) {
  event.body = (Buffer.from(event.body, 'binary') as unknown) as string;
}
2reactions
calflegalcommented, Apr 11, 2020

I am also experiencing the above issue, but with audio files: Unexpected end of multipart data

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: write EOF #7 - imagemin/imagemin-jpegoptim - GitHub
Hi, I'm getting the following error when using imagemin-jpegoptim: events.js:72 throw er; // Unhandled 'error' event ^ Error: write EOF at ...
Read more >
Error EOF when trying to resize images using Gulp on Windows
I get an error as follows when calling resize:images task in my gulpfile ... Unhandled 'error' event ^ Error: Error: write EOF at...
Read more >
What is an EOF error and why do i keep getting it - Sololearn
EOF stands for End Of File. As to why you encountered this error, we can't tell precisely without being given the code to...
Read more >
EOF and Errors (The GNU C Library)
This macro is an integer value that is returned by a number of narrow stream functions to indicate an end-of-file condition, or some...
Read more >
Correcting EOF error in python in Codechef - GeeksforGeeks
EOF stands for End Of File. Well, technically it is not an error, rather an exception. This exception is raised when one of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found