Passing my stream to a worker thread node.js

Viewed 614

I have to implement this custom method of Multer storage in order to encrypt file before saving it.

I have implemented my function encrypt, which will comunicate with a worker thread.

import { Worker, isMainThread } from 'worker_threads'
import path from 'path'
export const encrypt = ({ destination, filename, file, password }) =>
  new Promise((resolve, reject) => {
    if (isMainThread) {
      const workerPath = path.resolve(__dirname, './encryptWorker.js')
      const worker = new Worker(workerPath, {
        workerData: {
          destination,
          file,
          filename,
          password
        },
      })

      worker.on('message', resolve)
      worker.on('error', reject)
      worker.on('exit', (code) => {
        if (code !== 0) {
          reject(new Error(`Worker stopped with exit code ${code}`))
        }
      })
    }
  })

and the file params which is getting into the function contains basics information (name, length) plus the file.stream which is essential for its transformation.

The issue is coming when I am passing it to the Worker Thread: since it uses the structured clone algorithm to clone it and passing as worker data, it happens that this file.stream is getting undefined into the worker thread. I tried also to pass directly the file.stream params but, for the same reason above i am receiving DataCloneError issue, because I am passing a FileStream.

Any suggestion about how to solve this issue? I tried several approach to bypass it, one of them is writing the stream into an ArrayBuffer and then read it using another stream in the Worker Thread, but to be honest I would know if there is a better approach to resolve it.

Thanks

0 Answers
Related