fix: check for Node.js-created module when contextIsolation disabled (#41265)

fix: check for Node.js-created module when contextIsolation disabled
This commit is contained in:
Shelley Vohr
2024-02-09 02:54:59 +01:00
committed by GitHub
parent 432b89445f
commit 9bb821a818
8 changed files with 101 additions and 11 deletions

View File

@@ -141,7 +141,7 @@ describe('esm', () => {
const hostsUrl = pathToFileURL(process.platform === 'win32' ? 'C:\\Windows\\System32\\drivers\\etc\\hosts' : '/etc/hosts');
describe('without context isolation', () => {
it('should use blinks dynamic loader in the main world', async () => {
it('should use Blinks dynamic loader in the main world', async () => {
const [webContents] = await loadWindowWithPreload('', {
nodeIntegration: true,
sandbox: false,
@@ -156,13 +156,18 @@ describe('esm', () => {
}
expect(error).to.not.equal(null);
// This is a blink specific error message
// This is a Blink specific error message
expect(error?.message).to.include('Failed to fetch dynamically imported module');
});
it('should use import.meta callback handling from Node.js for Node.js modules', async () => {
const result = await runFixture(path.resolve(fixturePath, 'import-meta'));
expect(result.code).to.equal(0);
});
});
describe('with context isolation', () => {
it('should use nodes esm dynamic loader in the isolated context', async () => {
it('should use Node.js ESM dynamic loader in the isolated context', async () => {
const [, preloadError] = await loadWindowWithPreload(`await import(${JSON.stringify(hostsUrl)})`, {
nodeIntegration: true,
sandbox: false,
@@ -174,7 +179,7 @@ describe('esm', () => {
expect(preloadError!.toString()).to.include('Unknown file extension');
});
it('should use blinks dynamic loader in the main world', async () => {
it('should use Blinks dynamic loader in the main world', async () => {
const [webContents] = await loadWindowWithPreload('', {
nodeIntegration: true,
sandbox: false,

View File

@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
</body>
</html>

33
spec/fixtures/esm/import-meta/main.mjs vendored Normal file
View File

@@ -0,0 +1,33 @@
import { app, BrowserWindow } from 'electron'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path';
async function createWindow() {
const mainWindow = new BrowserWindow({
show: false,
webPreferences: {
preload: fileURLToPath(new URL('preload.mjs', import.meta.url)),
sandbox: false,
contextIsolation: false
}
})
await mainWindow.loadFile('index.html')
const importMetaPreload = await mainWindow.webContents.executeJavaScript('window.importMetaPath');
const expected = join(dirname(fileURLToPath(import.meta.url)), 'preload.mjs');
process.exit(importMetaPreload === expected ? 0 : 1);
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})

View File

@@ -0,0 +1,4 @@
{
"main": "main.mjs",
"type": "module"
}

View File

@@ -0,0 +1,3 @@
import { fileURLToPath } from 'node:url'
window.importMetaPath = fileURLToPath(import.meta.url)