| import { parentPort } from 'node:worker_threads'; | |
| import { | |
| decodeBuffer, | |
| encodeBuffer, | |
| } from './eu4_han_convert/src/codec.js'; | |
| const PROFILE = 'compatible'; | |
| const bom = Buffer.from([0xef, 0xbb, 0xbf]); | |
| function decodeUtf8Source(buffer) { | |
| const offset = buffer.subarray(0, bom.length).equals(bom) ? bom.length : 0; | |
| return new TextDecoder('utf-8', { fatal: true }).decode(buffer.subarray(offset)); | |
| } | |
| parentPort.on('message', ({ id, operation, input }) => { | |
| try { | |
| const buffer = Buffer.from(input); | |
| const output = operation === 'encode' | |
| ? encodeBuffer(decodeUtf8Source(buffer), { profile: PROFILE }) | |
| : Buffer.from(decodeBuffer(buffer), 'utf8'); | |
| parentPort.postMessage({ id, output }); | |
| } catch (error) { | |
| parentPort.postMessage({ id, error: error.message }); | |
| } | |
| }); | |