node.jswebsocketkuzzle

Why receiving a zip file from kuzzle with the js sdk does not work?


I want to send a zip from my kuzzle backend so i wrote that in my backend:

// Generate zip file
    const zip = new JSZip()
    zip.file(filename, zipData)
    const finalZipFile = await zip.generateAsync({
      type:"nodebuffer",
      compression: "DEFLATE",
      compressionOptions: {
        level: 6
      }
    })
req.response.configure({
      // Tell Kuzzle that this result will contain a raw payload
      format: 'raw', 
      headers: {
        // Set HTTP response headers
        'Content-Length': finalZipFile.length.toString(),
        'Content-Type': 'application/zip',
        'Content-Disposition': `attachment; filename="${filenameBase + ".zip"}"`,
        'Cache-Control': 'no-cache'
      }
    });
    return finalZipFile;

But when i make a request using the nodejs sdk:

const result = await kuzzle.query({
      controller: "hahaha",
      action: "hahaha"
    })
    console.log(result)

i get an error message:

/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/kuzzle-sdk/src/KuzzleError.js:32
        super(apiError.message);
                       ^

TypeError: Cannot read properties of undefined (reading 'message')
    at new KuzzleError (/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/kuzzle-sdk/src/KuzzleError.js:32:24)
    at WebSocket.client.onmessage (/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/kuzzle-sdk/src/protocols/WebSocket.js:159:35)
    at WebSocket.onMessage (/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (node:events:520:28)
    at Receiver.receiverOnMessage (/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/ws/lib/websocket.js:1137:20)
    at Receiver.emit (node:events:520:28)
    at Receiver.dataMessage (/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/ws/lib/receiver.js:528:14)
    at Receiver.getData (/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/ws/lib/receiver.js:446:17)
    at Receiver.startLoop (/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/ws/lib/receiver.js:148:22)
    at Receiver._write (/Users/doriancruveiller/Desktop/kuzzle-plugin-test/node_modules/ws/lib/receiver.js:83:10)

But the weird thing is that it works just fine if i do the same request with wscat. Maybe a bug in the nodeJS sdk ?


Solution

  • Most likely you just hit the server.maxRequestSize limit of the network layer.

    You should increase this limit to send bigger files to Kuzzle.

    Also, consider using S3 based storage with Pre-signed URLsinstead of storing file directly on Kuzzle because this will not works properly in a cluster environment.