napi-rs/test_module/__test__/napi4/uv_worker.js
Ouyang Yadong 00454eb577 refactor(napi): use get_uv_event_loop instead of uv_default_loop
Using the uv_loop_s from napi_env is safer as it's possible that
there are multiple Workers, and therefore multiple uv_loop_s.
2020-08-18 13:55:35 +08:00

16 lines
513 B
JavaScript

const { isMainThread, parentPort } = require('worker_threads')
const { join } = require('path')
const { readFileSync } = require('fs')
const bindings = require('../../index.node')
const filepath = join(__dirname, './example.txt')
if (!isMainThread) {
;(async () => {
const fileContent = await bindings.uvReadFile(filepath)
const success =
Buffer.isBuffer(fileContent) &&
readFileSync(filepath).toString('utf8') === fileContent.toString('utf8')
parentPort.postMessage(success)
})()
}