| 
									
										
										
										
											2021-08-09 16:06:21 -04:00
										 |  |  | // Copyright 2021 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import heicConvert from 'heic-convert'; | 
					
						
							|  |  |  | import { parentPort } from 'worker_threads'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 14:15:33 -05:00
										 |  |  | import type { | 
					
						
							| 
									
										
										
										
											2021-08-09 16:06:21 -04:00
										 |  |  |   WrappedWorkerRequest, | 
					
						
							|  |  |  |   WrappedWorkerResponse, | 
					
						
							|  |  |  | } from './heicConverterMain'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (!parentPort) { | 
					
						
							|  |  |  |   throw new Error('Must run as a worker thread'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const port = parentPort; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 13:26:40 -07:00
										 |  |  | function respond(uuid: string, error: Error | undefined, response?: Buffer) { | 
					
						
							| 
									
										
										
										
											2021-08-09 16:06:21 -04:00
										 |  |  |   const wrappedResponse: WrappedWorkerResponse = { | 
					
						
							|  |  |  |     uuid, | 
					
						
							| 
									
										
										
										
											2022-11-22 10:43:43 -08:00
										 |  |  |     error: error?.stack, | 
					
						
							| 
									
										
										
										
											2021-08-09 16:06:21 -04:00
										 |  |  |     response, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   port.postMessage(wrappedResponse); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | port.on('message', async ({ uuid, data }: WrappedWorkerRequest) => { | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2024-08-05 13:26:40 -07:00
										 |  |  |     const buf = await heicConvert({ | 
					
						
							| 
									
										
										
										
											2021-08-09 16:06:21 -04:00
										 |  |  |       buffer: new Uint8Array(data), | 
					
						
							|  |  |  |       format: 'JPEG', | 
					
						
							|  |  |  |       quality: 0.75, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 13:26:40 -07:00
										 |  |  |     respond(uuid, undefined, buf); | 
					
						
							| 
									
										
										
										
											2021-08-09 16:06:21 -04:00
										 |  |  |   } catch (error) { | 
					
						
							|  |  |  |     respond(uuid, error, undefined); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); |