chore: tsify sandboxed init (#23719)

This commit is contained in:
Samuel Attard 2020-05-30 02:56:54 -07:00 committed by GitHub
parent 9f21d09dfd
commit 236c1334e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 19 deletions

View file

@ -167,10 +167,12 @@ auto_filenames = {
"lib/renderer/window-setup.ts",
"lib/sandboxed_renderer/api/exports/electron.ts",
"lib/sandboxed_renderer/api/module-list.ts",
"lib/sandboxed_renderer/init.js",
"lib/sandboxed_renderer/init.ts",
"package.json",
"tsconfig.electron.json",
"tsconfig.json",
"typings/internal-ambient.d.ts",
"typings/internal-electron.d.ts",
]
isolated_bundle_deps = [
@ -181,6 +183,8 @@ auto_filenames = {
"package.json",
"tsconfig.electron.json",
"tsconfig.json",
"typings/internal-ambient.d.ts",
"typings/internal-electron.d.ts",
]
browser_bundle_deps = [
@ -256,6 +260,8 @@ auto_filenames = {
"package.json",
"tsconfig.electron.json",
"tsconfig.json",
"typings/internal-ambient.d.ts",
"typings/internal-electron.d.ts",
]
renderer_bundle_deps = [
@ -298,6 +304,8 @@ auto_filenames = {
"package.json",
"tsconfig.electron.json",
"tsconfig.json",
"typings/internal-ambient.d.ts",
"typings/internal-electron.d.ts",
]
worker_bundle_deps = [
@ -329,5 +337,7 @@ auto_filenames = {
"package.json",
"tsconfig.electron.json",
"tsconfig.json",
"typings/internal-ambient.d.ts",
"typings/internal-electron.d.ts",
]
}

View file

@ -1,11 +1,11 @@
'use strict';
/* eslint no-eval: "off" */
/* global binding, Buffer */
const events = require('events');
import { electronBindingSetup } from '@electron/internal/common/electron-binding-setup';
import * as events from 'events';
const { EventEmitter } = events;
process.electronBinding = require('@electron/internal/common/electron-binding-setup').electronBindingSetup(binding.get, 'renderer');
process.electronBinding = electronBindingSetup(binding.get, 'renderer');
const v8Util = process.electronBinding('v8_util');
// Expose Buffer shim as a hidden value. This is used by C++ code to
@ -19,7 +19,7 @@ v8Util.setHiddenValue(global, 'ipc', new EventEmitter());
v8Util.setHiddenValue(global, 'ipc-internal', new EventEmitter());
// The process object created by webpack is not an event emitter, fix it so
// the API is more compatible with non-sandboxed renderers.
for (const prop of Object.keys(EventEmitter.prototype)) {
for (const prop of Object.keys(EventEmitter.prototype) as (keyof typeof process)[]) {
if (Object.prototype.hasOwnProperty.call(process, prop)) {
delete process[prop];
}
@ -53,7 +53,7 @@ const loadedModules = new Map([
// ElectronApiServiceImpl will look for the "ipcNative" hidden object when
// invoking the 'onMessage' callback.
v8Util.setHiddenValue(global, 'ipcNative', {
onMessage (internal, channel, ports, args, senderId) {
onMessage (internal: boolean, channel: string, ports: MessagePort[], args: any[], senderId: number) {
const sender = internal ? ipcRendererInternal : electron.ipcRenderer;
sender.emit(channel, { sender, senderId, ports }, ...args);
}
@ -62,16 +62,16 @@ v8Util.setHiddenValue(global, 'ipcNative', {
// ElectronSandboxedRendererClient will look for the "lifecycle" hidden object when
v8Util.setHiddenValue(global, 'lifecycle', {
onLoaded () {
process.emit('loaded');
(process as events.EventEmitter).emit('loaded');
},
onExit () {
process.emit('exit');
(process as events.EventEmitter).emit('exit');
},
onDocumentStart () {
process.emit('document-start');
(process as events.EventEmitter).emit('document-start');
},
onDocumentEnd () {
process.emit('document-end');
(process as events.EventEmitter).emit('document-end');
}
});
@ -80,7 +80,7 @@ webFrameInit();
// Pass different process object to the preload script(which should not have
// access to things like `process.electronBinding`).
const preloadProcess = new EventEmitter();
const preloadProcess: NodeJS.Process = new EventEmitter() as any;
Object.assign(preloadProcess, binding.process);
Object.assign(preloadProcess, processProps);
@ -97,13 +97,13 @@ Object.defineProperty(preloadProcess, 'noDeprecation', {
}
});
process.on('loaded', () => preloadProcess.emit('loaded'));
process.on('exit', () => preloadProcess.emit('exit'));
process.on('document-start', () => preloadProcess.emit('document-start'));
process.on('document-end', () => preloadProcess.emit('document-end'));
process.on('loaded', () => (preloadProcess as events.EventEmitter).emit('loaded'));
process.on('exit', () => (preloadProcess as events.EventEmitter).emit('exit'));
(process as events.EventEmitter).on('document-start', () => (preloadProcess as events.EventEmitter).emit('document-start'));
(process as events.EventEmitter).on('document-end', () => (preloadProcess as events.EventEmitter).emit('document-end'));
// This is the `require` function that will be visible to the preload script
function preloadRequire (module) {
function preloadRequire (module: string) {
if (loadedModules.has(module)) {
return loadedModules.get(module);
}
@ -156,7 +156,7 @@ if (process.isMainFrame) {
// - `process`: The `preloadProcess` object
// - `Buffer`: Shim of `Buffer` implementation
// - `global`: The window object, which is aliased to `global` by webpack.
function runPreloadScript (preloadSrc) {
function runPreloadScript (preloadSrc: string) {
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate, clearImmediate, exports) {
${preloadSrc}
})`;

View file

@ -13,6 +13,8 @@ const allDocs = fs.readdirSync(path.resolve(__dirname, '../docs/api'))
.map(doc => `docs/api/structures/${doc}`)
);
const typingFiles = fs.readdirSync(path.resolve(__dirname, '../typings')).map(child => `typings/${child}`);
const main = async () => {
const webpackTargets = [
{
@ -70,7 +72,7 @@ const main = async () => {
// Only care about our own files
.filter(line => !line.startsWith('node_modules'))
// All webpack builds depend on the tsconfig and package json files
.concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json'])
.concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json', ...typingFiles])
// Make the generated list easier to read
.sort();
await fs.remove(tmpDir);

View file

@ -1,4 +1,5 @@
declare var internalBinding: any;
declare var binding: { get: (name: string) => any; process: NodeJS.Process; createPreloadScript: (src: string) => Function };
declare const BUILDFLAG: (flag: boolean) => boolean;