跳转至

utilityProcess

utilityProcess 创建一个启用了 Node.js 和消息端口的子进程。它提供与 Node.js 中 [child_process.fork][] API 等效的功能,但使用 Chromium 的 Services API 来启动子进程。

进程:Main

方法

utilityProcess.fork(modulePath[, args][, options])

  • modulePath string - 子进程中作为入口点运行的脚本的路径。
  • args string[] (可选) - 字符串参数列表,将在子进程中作为 process.argv 可用。
  • options Object (可选)
  • env Object (可选) - 环境变量键值对。默认为 process.env。
  • execArgv string[] (可选) - 传递给可执行文件的字符串参数列表。
  • cwd string (可选) - 子进程的当前工作目录。
  • session Session (可选) - 设置进程用于网络请求的会话。默认情况下,来自 utility 进程的网络请求将使用系统网络上下文,该上下文不支持 HTTP 缓存。设置会话可启用 HTTP 缓存和其他特定于会话的网络功能。更多信息请参见 session。
  • partition string (可选) - 根据会话的分区字符串设置进程使用的会话。如果 partition 以 persist: 开头,进程将使用一个持久会话,该会话可供应用中具有相同 partition 的所有页面使用。如果没有 persist: 前缀,进程将使用内存会话。通过分配相同的 partition,多个进程可以共享同一个会话。如果设置了 session 选项,则忽略此选项。
  • stdio (string[] | string) (可选) - 允许配置子进程的 stdout 和 stderr 模式。默认为 inherit。 字符串值可以是 pipe、ignore、inherit 之一,有关这些值的更多详细信息,请参见 Node.js 的 stdio 文档。目前,此选项仅支持将 stdout 和 stderr 配置为 pipe、inherit 或 ignore。将 stdin 配置为除 ignore 以外的任何属性均不受支持,并会导致错误。 例如,受支持的值将按以下方式处理:
    • pipe:等效于 ['ignore', 'pipe', 'pipe']
    • ignore:等效于 ['ignore', 'ignore', 'ignore']
    • inherit:等效于 ['ignore', 'inherit', 'inherit'](默认值)
  • serviceName string (可选) - 进程的名称,将出现在 app.getAppMetrics 返回的 ProcessMetric 的 name 属性中, 以及 app 的 child-process-gone 事件 中。 默认为 Node Utility Process。
  • allowLoadingUnsignedLibraries boolean (可选) macOS - 使用此标志时,utility 进程将通过 macOS 上的 Electron Helper (Plugin).app 辅助可执行文件启动,该文件可以 使用 com.apple.security.cs.disable-library-validation 和 com.apple.security.cs.allow-unsigned-executable-memory 权限进行代码签名。这将允许 utility 进程 加载未签名的库。除非你确实需要此功能,否则最好保持禁用。 默认为 false。
  • disclaim boolean (可选) macOS - 使用此标志时,utility 进程将放弃对子进程的责任。这会导致操作系统出于安全策略(如透明度、同意和控制(TCC))的目的,将子 进程视为独立实体。当放弃责任时,父进程将不会被归因为子进程发起的任何 TCC 请求。这在启动运行第三方代码或其他不受信任代码的进程时非常有用。默认为 false。
  • respondToAuthRequestsFromMainProcess boolean (可选) - 使用此标志时,通过 net module 创建的所有 HTTP 401 和 407 网络 请求都允许通过 UtilityProcess 实例上的 login 事件进行响应(当提供 session 时),或者通过 主进程中的 app#login 事件进行响应(当使用默认系统网络上下文时)。此标志还将客户端证书选择路由到 主进程中的 app#select-client-certificate 事件;如果没有 此标志,来自 utility 进程的 net 请求将在没有客户端证书的情况下继续。如果没有此标志,身份验证 质询将由 ClientRequest 对象上的默认 login 事件处理。默认为 false。

返回 UtilityProcess

[!NOTE] utilityProcess.fork 只能在 App 上发出 ready 事件后调用。

类:UtilityProcess

UtilityProcess 的实例表示具有 Node.js 集成的由 Chromium 生成的子进程。

UtilityProcess 是一个 EventEmitter。

实例方法

child.postMessage(message, [transfer])

  • message any
  • transfer MessagePortMain[] (可选)

向子进程发送消息,可选地转移零个或多个 [MessagePortMain][] 对象的所有权。

例如:

// Main process
const { port1, port2 } = new MessageChannelMain()
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.postMessage({ message: 'hello' }, [port1])

// Child process
process.parentPort.once('message', (e) => {
  const [port] = e.ports
  // ...
})

child.kill()

返回 boolean

优雅地终止进程。在 POSIX 上,它发送 SIGTERM;在 Windows 上, 它终止进程。与 Node 的 child_process.kill() 类似, 这会发送信号,但不保证进程退出:处理 SIGTERM 的子进程决定何时退出,或者是否退出。在 POSIX 上,子进程 退出后由 Electron 回收。如果信号已发送,则返回 true, 否则返回 false。

实例属性

child.pid

一个 Integer | undefined,表示子进程的进程标识符(PID)。 在子进程成功生成之前,该值为 undefined。当子进程退出后,在发出 exit 事件之后,该值为 undefined。

const child = utilityProcess.fork(path.join(__dirname, 'test.js'))

console.log(child.pid) // undefined

child.on('spawn', () => {
  console.log(child.pid) // Integer
})

child.on('exit', () => {
  console.log(child.pid) // undefined
})

[!NOTE] 你可以使用 pid 来确定进程当前是否正在运行。

child.stdout

一个 NodeJS.ReadableStream | null,表示子进程的 stdout。 如果子进程生成时 options.stdio[1] 被设置为 'pipe' 以外的任何值,则此值为 null。 当子进程退出后,在发出 exit 事件之后,该值为 null。

// Main process
const { port1, port2 } = new MessageChannelMain()
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.stdout.on('data', (data) => {
  console.log(`Received chunk ${data}`)
})

child.stderr

一个 NodeJS.ReadableStream | null,表示子进程的 stderr。 如果子进程生成时 options.stdio[2] 被设置为 'pipe' 以外的任何值,则此值为 null。 当子进程退出后,在发出 exit 事件之后,该值为 null。

实例事件

事件:'spawn'

在子进程成功生成后发出一次。

事件:'error' 实验性

返回值:

  • type string - 错误类型。以下值之一:
  • FatalError
  • location string - 错误来源的源位置。
  • report string - [Node.js diagnostic report][].

当子进程由于来自 V8 的不可继续错误而需要终止时发出。

无论是否监听 error 事件,子进程终止后都会发出 exit 事件。

事件:'exit'

返回值:

  • code number - 包含从 POSIX 的 waitpid 或 Windows 的 GetExitCodeProcess 获取的进程退出代码。

在子进程结束后发出。

事件:'message'

返回值:

  • message any

当子进程使用 process.parentPort.postMessage() 发送消息时发出。

事件:'login'

返回值:

  • authenticationResponseDetails Object
  • url URL
  • pid number
  • isRequestForNavigation boolean - 指示该请求是否为导航请求。
  • firstAuthAttempt boolean - 指示这是否为第一次身份验证尝试。
  • responseHeaders Record\<string, string | string[]> (可选) - 响应中返回的标头。
  • authInfo Object
  • isProxy boolean
  • scheme string
  • host string
  • port Integer
  • realm string
  • callback Function
  • username string (可选)
  • password string (可选)

当 utility process 遇到 HTTP 401 或 407 身份验证质询时发出,如果该进程创建时同时使用了 respondToAuthRequestsFromMainProcess: true 和 session 选项。 应使用凭据调用 callback 以响应质询。不带参数调用 callback 将取消请求。

其行为与 app 上的 login 事件 相同,但作用域限定于单个 utility process 实例。

const { session, utilityProcess } = require('electron')

const ses = session.defaultSession
const child = utilityProcess.fork('./worker.js', [], {
  session: ses,
  respondToAuthRequestsFromMainProcess: true
})

child.on('login', (authenticationResponseDetails, authInfo, callback) => {
  callback('username', 'password')
})

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron