mmmagic icon indicating copy to clipboard operation
mmmagic copied to clipboard

Support for worker threads

Open ncroese opened this issue 4 years ago • 1 comments

The library does not seem to work inside a worker thread. When running the code below the result is not logged:

const { Worker, isMainThread } = require("worker_threads");
if (isMainThread) {
    const worker = new Worker(__filename);
    worker.on("error", error => console.log(error));
    worker.on("exit", code => console.log(`Exit: ${code}`));
} else {
    const { Magic, MAGIC_MIME_TYPE } = require("mmmagic");
    const buffer = Buffer.from("plain text");
    const magic = new Magic(MAGIC_MIME_TYPE);
    magic.detect(buffer, (error, result) => {
        if (error) {
            throw error;
        }
        console.log(`Result: ${result}`);
    });
}

I'm assuming something breaks inside the library code because it's not compatible with worker threads yet, since running the same code on the main thread works just fine. Would you be willing to make the library compatible with worker threads? Sadly I don't have the knowledge to do this myself.

ncroese avatar Apr 02 '20 14:04 ncroese

Hi @ncroese, The logging issue you're facing is probably related to this Node bug related to worker threads: https://github.com/nodejs/node/issues/30491 This is not to say that your feature request is not valid, it definitely is, but the logging issue could be a separate one.

tnabil avatar Mar 09 '23 00:03 tnabil