Repository: denoland/deno
File: ext/node/polyfills/worker_threads.ts (TypeScript)

Code before:
        if (metadata.execArgv) {
          process.execArgv = metadata.execArgv;
          for (let i = 0; i < metadata.execArgv.length; i++) {
            if (metadata.execArgv[i] === "--trace-warnings") {
              process.traceProcessWarnings = true;
            }
          }
        }

        // Replace process.stdin with a Readable that receives
        // data from the parent via WORKER_STDIN messages.
        if (metadata.hasStdin) {
          const workerStdin = new Readable({ read() {} });
          process.stdin = workerStdin;

          // Register an early listener to intercept stdin messages
          // before any user-registered handlers. Remove the listener
          // once stdin ends so the worker can exit cleanly.
          const stdinHandler = (ev) => {
            const msg = ev.data;
            if (isWorkerStdinMsg(msg)) {
              FunctionPrototypeCall(
                Readable.prototype.push,
                workerStdin,
                msg.data,
              );
              ev.stopImmediatePropagation();
            } else if (isWorkerStdinEndMsg(msg)) {
              FunctionPrototypeCall(
                Readable.prototype.push,
                workerStdin,
                null,
              );
              parentPort.removeEventListener("message", stdinHandler);
              ev.stopImmediatePropagation();
            }
          };
          parentPort.addEventListener("message", stdinHandler);
        }

        // Forward stdout writes to the parent so worker.stdout
        // is readable from the host side.
        const origStdoutWrite = FunctionPrototypeBind(
          process.stdout.write,
          process.stdout,
        );
        process.stdout.write = function (chunk, encoding, callback) {
          parentPort.postMessage({
            type: "WORKER_STDOUT",
            data: chunk,
          });

Review comment (suggestion):
`workerStdin.push(msg.data)` and `workerStdin.push(null)` should suffice. Readable isn't primordial

Code after:
          process.execArgv = metadata.execArgv;
          for (let i = 0; i < metadata.execArgv.length; i++) {
            if (metadata.execArgv[i] === "--trace-warnings") {
              process.traceProcessWarnings = true;
            }
          }
        }

        // Replace process.stdin with a Readable that receives
        // data from the parent via WORKER_STDIN messages.
        if (metadata.hasStdin) {
          const workerStdin = new Readable({ read() {} });
          process.stdin = workerStdin;

          // Register an early listener to intercept stdin messages
          // before any user-registered handlers. Remove the listener
          // once stdin ends so the worker can exit cleanly.
          const stdinHandler = (ev) => {
            const msg = ev.data;
            if (isWorkerStdinMsg(msg)) {
              // deno-lint-ignore prefer-primordials
              workerStdin.push(msg.data);
              ev.stopImmediatePropagation();
            } else if (isWorkerStdinEndMsg(msg)) {
              // deno-lint-ignore prefer-primordials
              workerStdin.push(null);
              parentPort.removeEventListener("message", stdinHandler);
              ev.stopImmediatePropagation();
            }
          };
          parentPort.addEventListener("message", stdinHandler);
        }

        // Forward stdout writes to the parent so worker.stdout
        // is readable from the host side.
        const origStdoutWrite = FunctionPrototypeBind(
          process.stdout.write,
          process.stdout,
        );
        process.stdout.write = function (chunk, encoding, callback) {
          parentPort.postMessage({
            type: "WORKER_STDOUT",
            data: chunk,
          });
          return FunctionPrototypeCall(
            origStdoutWrite,
            process.stdout,
            chunk,
            encoding,
            callback,
          );