Browse Source

feat: deprecate modules internally using remote.require in sandboxed renderer context (#15145)

Milan Burda 6 years ago
parent
commit
d561c5531b

+ 33 - 0
docs/api/breaking-changes.md

@@ -65,6 +65,39 @@ not present, then the native module will fail to load on Windows, with an error
 message like `Cannot find module`. See the [native module
 guide](/docs/tutorial/using-native-node-modules.md) for more.
 
+## `electron.screen` in renderer process
+
+```js
+// Deprecated
+require('electron').screen
+// Replace with
+require('electron').remote.screen
+```
+
+## `require` in sandboxed renderers
+
+```js
+// Deprecated
+require('child_process')
+// Replace with
+require('electron').remote.require('child_process')
+
+// Deprecated
+require('fs')
+// Replace with
+require('electron').remote.require('fs')
+
+// Deprecated
+require('os')
+// Replace with
+require('electron').remote.require('os')
+
+// Deprecated
+require('path')
+// Replace with
+require('electron').remote.require('path')
+```
+
 
 # Breaking API Changes (3.0)
 

+ 1 - 3
lib/browser/api/app.js

@@ -93,9 +93,7 @@ if (process.platform === 'linux') {
 }
 
 app.allowNTLMCredentialsForAllDomains = function (allow) {
-  if (!process.noDeprecation) {
-    deprecate.warn('app.allowNTLMCredentialsForAllDomains', 'session.allowNTLMCredentialsForDomains')
-  }
+  deprecate.warn('app.allowNTLMCredentialsForAllDomains', 'session.allowNTLMCredentialsForDomains')
   const domains = allow ? '*' : ''
   if (!this.isReady()) {
     this.commandLine.appendSwitch('auth-server-whitelist', domains)

+ 3 - 1
lib/common/api/deprecate.js

@@ -19,7 +19,9 @@ const deprecate = {
   setHandler: (handler) => { deprecationHandler = handler },
   getHandler: () => deprecationHandler,
   warn: (oldName, newName) => {
-    return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
+    if (!process.noDeprecation) {
+      deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
+    }
   },
   log: (message) => {
     if (typeof deprecationHandler === 'function') {

+ 4 - 0
lib/renderer/api/screen.js

@@ -1,4 +1,8 @@
 'use strict'
 
+const { deprecate } = require('electron')
+
+deprecate.warn(`electron.screen`, `electron.remote.screen`)
+
 const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
 module.exports = getRemoteForUsage('screen').screen

+ 4 - 0
lib/sandboxed_renderer/api/exports/child_process.js

@@ -1,4 +1,8 @@
 'use strict'
 
+const { deprecate } = require('electron')
+
+deprecate.warn(`require('child_process')`, `remote.require('child_process')`)
+
 const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
 module.exports = getRemoteForUsage('child_process').require('child_process')

+ 4 - 0
lib/sandboxed_renderer/api/exports/fs.js

@@ -1,4 +1,8 @@
 'use strict'
 
+const { deprecate } = require('electron')
+
+deprecate.warn(`require('fs')`, `remote.require('fs')`)
+
 const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
 module.exports = getRemoteForUsage('fs').require('fs')

+ 4 - 0
lib/sandboxed_renderer/api/exports/os.js

@@ -1,4 +1,8 @@
 'use strict'
 
+const { deprecate } = require('electron')
+
+deprecate.warn(`require('os')`, `remote.require('os')`)
+
 const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
 module.exports = getRemoteForUsage('os').require('os')

+ 4 - 0
lib/sandboxed_renderer/api/exports/path.js

@@ -1,4 +1,8 @@
 'use strict'
 
+const { deprecate } = require('electron')
+
+deprecate.warn(`require('path')`, `remote.require('path')`)
+
 const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
 module.exports = getRemoteForUsage('path').require('path')

+ 9 - 0
lib/sandboxed_renderer/init.js

@@ -90,6 +90,15 @@ Object.assign(preloadProcess, processProps)
 Object.assign(process, binding.process)
 Object.assign(process, processProps)
 
+Object.defineProperty(preloadProcess, 'noDeprecation', {
+  get () {
+    return process.noDeprecation
+  },
+  set (value) {
+    process.noDeprecation = value
+  }
+})
+
 process.on('exit', () => preloadProcess.emit('exit'))
 
 // This is the `require` function that will be visible to the preload script

+ 2 - 2
spec/api-browser-window-spec.js

@@ -11,8 +11,8 @@ const http = require('http')
 const { closeWindow } = require('./window-helpers')
 const { emittedOnce } = require('./events-helpers')
 const { resolveGetters } = require('./assert-helpers')
-const { ipcRenderer, remote, screen } = require('electron')
-const { app, ipcMain, BrowserWindow, BrowserView, protocol, session, webContents } = remote
+const { ipcRenderer, remote } = require('electron')
+const { app, ipcMain, BrowserWindow, BrowserView, protocol, session, screen, webContents } = remote
 
 const features = process.atomBinding('features')
 const { expect } = chai

+ 2 - 1
spec/api-desktop-capturer-spec.js

@@ -1,6 +1,7 @@
 const chai = require('chai')
 const dirtyChai = require('dirty-chai')
-const { desktopCapturer, remote, screen } = require('electron')
+const { desktopCapturer, remote } = require('electron')
+const { screen } = remote
 const features = process.atomBinding('features')
 
 const { expect } = chai

+ 1 - 1
spec/api-screen-spec.js

@@ -1,5 +1,5 @@
 const assert = require('assert')
-const { screen } = require('electron')
+const { screen } = require('electron').remote
 
 describe('screen module', () => {
   describe('screen.getCursorScreenPoint()', () => {