signal-desktop/ts/workers/heicConverterWorker.ts

40 lines
885 B
TypeScript
Raw Normal View History

2021-08-09 20:06:21 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import heicConvert from 'heic-convert';
import { parentPort } from 'worker_threads';
import type {
2021-08-09 20:06:21 +00:00
WrappedWorkerRequest,
WrappedWorkerResponse,
} from './heicConverterMain';
if (!parentPort) {
throw new Error('Must run as a worker thread');
}
const port = parentPort;
function respond(uuid: string, error: Error | undefined, response?: File) {
const wrappedResponse: WrappedWorkerResponse = {
uuid,
error: error?.stack,
2021-08-09 20:06:21 +00:00
response,
};
port.postMessage(wrappedResponse);
}
port.on('message', async ({ uuid, data }: WrappedWorkerRequest) => {
try {
const file = await heicConvert({
buffer: new Uint8Array(data),
format: 'JPEG',
quality: 0.75,
});
respond(uuid, undefined, file);
} catch (error) {
respond(uuid, error, undefined);
}
});