summaryrefslogtreecommitdiff
path: root/lib/internal/process/worker_thread_only.js
blob: b6529e4344167972913571e67b6a1b96b7d299d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';

// This file contains process bootstrappers that can only be
// run in the worker thread.

const {
  createWorkerStdio
} = require('internal/worker/io');

const {
  codes: { ERR_WORKER_UNSUPPORTED_OPERATION }
} = require('internal/errors');

let workerStdio;
function lazyWorkerStdio() {
  if (!workerStdio) workerStdio = createWorkerStdio();
  return workerStdio;
}
function createStdioGetters() {
  return {
    getStdout() { return lazyWorkerStdio().stdout; },
    getStderr() { return lazyWorkerStdio().stderr; },
    getStdin() { return lazyWorkerStdio().stdin; }
  };
}

// The execution of this function itself should not cause any side effects.
function wrapProcessMethods(binding) {
  function umask(mask) {
    // process.umask() is a read-only operation in workers.
    if (mask !== undefined) {
      throw new ERR_WORKER_UNSUPPORTED_OPERATION('Setting process.umask()');
    }

    return binding.umask(mask);
  }

  return { umask };
}

function unavailable(name) {
  function unavailableInWorker() {
    throw new ERR_WORKER_UNSUPPORTED_OPERATION(name);
  }

  unavailableInWorker.disabled = true;
  return unavailableInWorker;
}

module.exports = {
  createStdioGetters,
  unavailable,
  wrapProcessMethods
};