11const { Worker } = require ( 'internal/worker' ) ;
2+ const { deserialize, serialize } = require ( 'v8' ) ;
23
34
45/**
@@ -13,7 +14,7 @@ const commsChannel = new SharedArrayBuffer(2048);
1314const lock = new Int32Array ( commsChannel , 0 , 4 ) ;
1415/**
1516 * The request & response segment of the shared memory. TextEncoder/Decoder (needed to convert
16- * requests & responses into a format supported by the coms channel) reads and writes with
17+ * requests & responses into a format supported by the comms channel) reads and writes with
1718 * Uint8Array.
1819 */
1920const requestResponseData = new Uint8Array ( commsChannel , 4 , 2044 ) ;
@@ -27,12 +28,12 @@ Atomics.wait(lock, 0, 1); // ! Block this module until the worker is ready.
2728
2829function makeRequest ( type , data ) {
2930 requestResponseData . fill ( 0 ) ; // Erase any previous request/response (it's already been handled)
30- const request = JSON . stringify ( { data, type } ) ;
31- new TextEncoder ( ) . encodeInto ( request , requestResponseData ) ;
3231 Atomics . store ( lock , 0 , 1 ) ; // Send request to worker
32+ const request = serialize ( { data, type } ) ;
33+ requestResponseData . set ( request ) ;
3334 Atomics . notify ( lock , 0 ) ; // Notify worker of new request
3435 Atomics . wait ( lock , 0 , 1 ) ; // Sleep until worker responds
35- return new TextDecoder ( ) . decode ( requestResponseData ) ;
36+ return deserialize ( requestResponseData ) ;
3637}
3738
3839module . exports = {
0 commit comments