mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
feat: enable code cache for custom protocols (#40544)
This commit is contained in:
@@ -1090,6 +1090,33 @@ describe('protocol module', () => {
|
||||
}
|
||||
});
|
||||
|
||||
describe('protocol.registerSchemesAsPrivileged codeCache', function () {
|
||||
const temp = require('temp').track();
|
||||
const appPath = path.join(fixturesPath, 'apps', 'refresh-page');
|
||||
|
||||
let w: BrowserWindow;
|
||||
let codeCachePath: string;
|
||||
beforeEach(async () => {
|
||||
w = new BrowserWindow({ show: false });
|
||||
codeCachePath = temp.path();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await closeWindow(w);
|
||||
w = null as unknown as BrowserWindow;
|
||||
});
|
||||
|
||||
it('code cache in custom protocol is disabled by default', async () => {
|
||||
ChildProcess.spawnSync(process.execPath, [appPath, 'false', codeCachePath]);
|
||||
expect(fs.readdirSync(path.join(codeCachePath, 'js')).length).to.equal(2);
|
||||
});
|
||||
|
||||
it('codeCache:true enables codeCache in custom protocol', async () => {
|
||||
ChildProcess.spawnSync(process.execPath, [appPath, 'true', codeCachePath]);
|
||||
expect(fs.readdirSync(path.join(codeCachePath, 'js')).length).to.above(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('handle', () => {
|
||||
afterEach(closeAllWindows);
|
||||
|
||||
|
||||
9
spec/fixtures/apps/refresh-page/main.html
vendored
Normal file
9
spec/fixtures/apps/refresh-page/main.html
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<body>
|
||||
<!-- Use mocha which has a large enough js file -->
|
||||
<script src="mocha.js"></script>
|
||||
<script>
|
||||
mocha.setup('bdd');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
38
spec/fixtures/apps/refresh-page/main.js
vendored
Normal file
38
spec/fixtures/apps/refresh-page/main.js
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
const path = require('node:path');
|
||||
const { once } = require('node:events');
|
||||
const { pathToFileURL } = require('node:url');
|
||||
const { BrowserWindow, app, protocol, net, session } = require('electron');
|
||||
|
||||
if (process.argv.length < 4) {
|
||||
console.error('Must pass allow_code_cache code_cache_dir');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
protocol.registerSchemesAsPrivileged([
|
||||
{
|
||||
scheme: 'atom',
|
||||
privileges: {
|
||||
standard: true,
|
||||
codeCache: process.argv[2] === 'true'
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
app.once('ready', async () => {
|
||||
const codeCachePath = process.argv[3];
|
||||
session.defaultSession.setCodeCachePath(codeCachePath);
|
||||
|
||||
protocol.handle('atom', (request) => {
|
||||
let { pathname } = new URL(request.url);
|
||||
if (pathname === '/mocha.js') { pathname = path.resolve(__dirname, '../../../node_modules/mocha/mocha.js'); } else { pathname = path.join(__dirname, pathname); }
|
||||
return net.fetch(pathToFileURL(pathname).toString());
|
||||
});
|
||||
|
||||
const win = new BrowserWindow({ show: false });
|
||||
win.loadURL('atom://host/main.html');
|
||||
await once(win.webContents, 'did-finish-load');
|
||||
// Reload to generate code cache.
|
||||
win.reload();
|
||||
await once(win.webContents, 'did-finish-load');
|
||||
app.exit();
|
||||
});
|
||||
4
spec/fixtures/apps/refresh-page/package.json
vendored
Normal file
4
spec/fixtures/apps/refresh-page/package.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "electron-test-refresh-page",
|
||||
"main": "main.js"
|
||||
}
|
||||
Reference in New Issue
Block a user