diff --git a/filenames.gni b/filenames.gni index ee5185d01c3d..188473cc8957 100644 --- a/filenames.gni +++ b/filenames.gni @@ -67,7 +67,7 @@ filenames = { "lib/renderer/content-scripts-injector.js", "lib/renderer/init.js", "lib/renderer/inspector.js", - "lib/renderer/ipc-renderer-internal-utils.js", + "lib/renderer/ipc-renderer-internal-utils.ts", "lib/renderer/ipc-renderer-internal.js", "lib/renderer/remote.js", "lib/renderer/security-warnings.js", diff --git a/lib/renderer/ipc-renderer-internal-utils.js b/lib/renderer/ipc-renderer-internal-utils.js deleted file mode 100644 index e0c71aa4bf45..000000000000 --- a/lib/renderer/ipc-renderer-internal-utils.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict' - -const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal') -const errorUtils = require('@electron/internal/common/error-utils') - -let nextId = 0 - -exports.invoke = function (command, ...args) { - return new Promise((resolve, reject) => { - const requestId = ++nextId - ipcRenderer.once(`${command}_RESPONSE_${requestId}`, (event, error, result) => { - if (error) { - reject(errorUtils.deserialize(error)) - } else { - resolve(result) - } - }) - ipcRenderer.send(command, requestId, ...args) - }) -} - -exports.invokeSync = function (command, ...args) { - const [ error, result ] = ipcRenderer.sendSync(command, ...args) - - if (error) { - throw errorUtils.deserialize(error) - } else { - return result - } -} diff --git a/lib/renderer/ipc-renderer-internal-utils.ts b/lib/renderer/ipc-renderer-internal-utils.ts new file mode 100644 index 000000000000..e76c461f41a0 --- /dev/null +++ b/lib/renderer/ipc-renderer-internal-utils.ts @@ -0,0 +1,30 @@ +import * as ipcRenderer from '@electron/internal/renderer/ipc-renderer-internal' +import * as errorUtils from '@electron/internal/common/error-utils' + +let nextId = 0 + +export function invoke (command: string, ...args: any[]) { + return new Promise((resolve, reject) => { + const requestId = ++nextId + ipcRenderer.once(`${command}_RESPONSE_${requestId}`, ( + _event: Electron.Event, error: Electron.SerializedError, result: any + ) => { + if (error) { + reject(errorUtils.deserialize(error)) + } else { + resolve(result) + } + }) + ipcRenderer.send(command, requestId, ...args) + }) +} + +export function invokeSync (command: string, ...args: any[]): T { + const [ error, result ] = ipcRenderer.sendSync(command, ...args) + + if (error) { + throw errorUtils.deserialize(error) + } else { + return result + } +} diff --git a/package.json b/package.json index be2f33e1b0bc..ba3ef2ce4947 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,9 @@ "aliasify": { "replacements": { "@electron/internal/(.+)": "./lib/$1" + }, + "appliesTo": { + "includeExtensions": [".js", ".ts"] } }, "lint-staged": { diff --git a/typings/internal-electron.d.ts b/typings/internal-electron.d.ts index e89610c35c3b..ffd0c2083814 100644 --- a/typings/internal-electron.d.ts +++ b/typings/internal-electron.d.ts @@ -6,9 +6,24 @@ */ declare namespace Electron { + enum ProcessType { + browser = 'browser', + renderer = 'renderer', + worker = 'worker' + } + interface App { setVersion(version: string): void; setDesktopName(name: string): void; setAppPath(path: string | null): void; } + + interface SerializedError { + message: string; + stack?: string, + name: string, + from: Electron.ProcessType, + cause: SerializedError, + __ELECTRON_SERIALIZED_ERROR__: true + } }