local: initial add
This commit is contained in:
parent
84ee7dbfe8
commit
d89edc705f
@ -7,12 +7,12 @@ const presets = { // Presets from OpenAsar
|
|||||||
'memory': '--in-process-gpu --js-flags="--lite-mode --optimize_for_size --wasm_opt --wasm_lazy_compilation --wasm_lazy_validation --always_compact" --renderer-process-limit=2 --enable-features=QuickIntensiveWakeUpThrottlingAfterLoading' // Less (?) memory usage
|
'memory': '--in-process-gpu --js-flags="--lite-mode --optimize_for_size --wasm_opt --wasm_lazy_compilation --wasm_lazy_validation --always_compact" --renderer-process-limit=2 --enable-features=QuickIntensiveWakeUpThrottlingAfterLoading' // Less (?) memory usage
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async ({ browserName, browserPath, dataPath }, { url, windowSize }) => {
|
export default async ({ browserPath, dataPath }, { url, windowSize }, extra) => {
|
||||||
return await StartBrowser(browserPath, [
|
return await StartBrowser(browserPath, [
|
||||||
`--app=${url}`,
|
`--app=${url}`,
|
||||||
`--remote-debugging-pipe`,
|
`--remote-debugging-pipe`,
|
||||||
`--user-data-dir=${dataPath}`,
|
`--user-data-dir=${dataPath}`,
|
||||||
windowSize ? `--window-size=${windowSize.join(',')}` : '',
|
windowSize ? `--window-size=${windowSize.join(',')}` : '',
|
||||||
...`--new-window --no-first-run --disable-extensions --disable-default-apps --disable-breakpad --disable-crashpad --disable-background-networking --disable-domain-reliability --disable-component-update --disable-sync --disable-features=AutofillServerCommunication ${presets.perf}`.split(' ')
|
...`--new-window --no-first-run --disable-extensions --disable-default-apps --disable-breakpad --disable-crashpad --disable-background-networking --disable-domain-reliability --disable-component-update --disable-sync --disable-features=AutofillServerCommunication ${presets.perf}`.split(' ')
|
||||||
], 'stdio', { browserName });
|
], 'stdio', extra);
|
||||||
};
|
};
|
@ -4,7 +4,7 @@ import { join } from 'path';
|
|||||||
import StartBrowser from '../launcher/start.js';
|
import StartBrowser from '../launcher/start.js';
|
||||||
|
|
||||||
|
|
||||||
export default async ({ browserName, browserPath, dataPath }, { url, windowSize }) => {
|
export default async ({ browserPath, dataPath }, { url, windowSize }, extra) => {
|
||||||
await mkdir(dataPath, { recursive: true });
|
await mkdir(dataPath, { recursive: true });
|
||||||
await writeFile(join(dataPath, 'user.js'), `
|
await writeFile(join(dataPath, 'user.js'), `
|
||||||
user_pref("toolkit.legacyUserProfileCustomizations.stylesheets", true);
|
user_pref("toolkit.legacyUserProfileCustomizations.stylesheets", true);
|
||||||
@ -78,6 +78,6 @@ html:not([tabsintitlebar="true"]) .tab-icon-image {
|
|||||||
`-profile`, dataPath,
|
`-profile`, dataPath,
|
||||||
`-new-window`, url,
|
`-new-window`, url,
|
||||||
`-new-instance`,
|
`-new-instance`,
|
||||||
`-no-remote`
|
`-no-remote`,
|
||||||
], 'websocket', { browserName });
|
], 'websocket', extra);
|
||||||
};
|
};
|
20
src/index.js
20
src/index.js
@ -10,6 +10,8 @@ import { fileURLToPath } from 'url';
|
|||||||
import Chromium from './browser/chromium.js';
|
import Chromium from './browser/chromium.js';
|
||||||
import Firefox from './browser/firefox.js';
|
import Firefox from './browser/firefox.js';
|
||||||
|
|
||||||
|
import LocalServer from './lib/local/server.js';
|
||||||
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
@ -164,6 +166,9 @@ const getBrowserType = name => { // todo: not need this
|
|||||||
return 'chromium';
|
return 'chromium';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const portRange = [ 10000, 60000 ];
|
||||||
|
const generatePort = () => (Math.floor(Math.random() * (portRange[1] - portRange[0] + 1)) + portRange[0]);
|
||||||
|
|
||||||
const startBrowser = async (url, { windowSize, forceBrowser, forceEngine }) => {
|
const startBrowser = async (url, { windowSize, forceBrowser, forceEngine }) => {
|
||||||
const [ browserPath, browserName ] = await findBrowserPath(forceBrowser, forceEngine);
|
const [ browserPath, browserName ] = await findBrowserPath(forceBrowser, forceEngine);
|
||||||
const browserFriendlyName = getFriendlyName(browserName);
|
const browserFriendlyName = getFriendlyName(browserName);
|
||||||
@ -176,13 +181,24 @@ const startBrowser = async (url, { windowSize, forceBrowser, forceEngine }) => {
|
|||||||
log('found browser', browserName, `(${browserType} based)`, 'at path:', browserPath);
|
log('found browser', browserName, `(${browserType} based)`, 'at path:', browserPath);
|
||||||
log('data path:', dataPath);
|
log('data path:', dataPath);
|
||||||
|
|
||||||
|
const openingLocal = !url.includes('://');
|
||||||
|
const localUrl = browserType === 'firefox' ? `http://localhost:${generatePort()}` : 'https://gluon.local';
|
||||||
|
|
||||||
|
const closeHandlers = [];
|
||||||
|
if (openingLocal && browserType === 'firefox') closeHandlers.push(await LocalServer({ localUrl, url }));
|
||||||
|
|
||||||
const Window = await (browserType === 'firefox' ? Firefox : Chromium)({
|
const Window = await (browserType === 'firefox' ? Firefox : Chromium)({
|
||||||
browserName: browserFriendlyName,
|
|
||||||
dataPath,
|
dataPath,
|
||||||
browserPath
|
browserPath
|
||||||
}, {
|
}, {
|
||||||
url,
|
url: openingLocal ? localUrl : url,
|
||||||
windowSize
|
windowSize
|
||||||
|
}, {
|
||||||
|
browserName: browserFriendlyName,
|
||||||
|
url,
|
||||||
|
localUrl,
|
||||||
|
openingLocal,
|
||||||
|
closeHandlers
|
||||||
});
|
});
|
||||||
|
|
||||||
return Window;
|
return Window;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import IPCApi from '../lib/ipc.js';
|
import IPCApi from '../lib/ipc.js';
|
||||||
|
import LocalCDP from '../lib/local/cdp.js';
|
||||||
|
|
||||||
import IdleApi from '../api/idle.js';
|
import IdleApi from '../api/idle.js';
|
||||||
import ControlsApi from '../api/controls.js';
|
import ControlsApi from '../api/controls.js';
|
||||||
@ -44,6 +45,7 @@ export default async (CDP, proc, injectionType = 'browser', { browserName, openi
|
|||||||
let sessionId;
|
let sessionId;
|
||||||
if (injectionType === 'browser') sessionId = await acquireTarget(CDP, target => target.url !== 'about:blank');
|
if (injectionType === 'browser') sessionId = await acquireTarget(CDP, target => target.url !== 'about:blank');
|
||||||
|
|
||||||
|
if (openingLocal && browserEngine === 'chromium') await LocalCDP(CDP, { sessionId, localUrl, url });
|
||||||
|
|
||||||
await CDP.sendMessage('Runtime.enable', {}, sessionId); // enable runtime API
|
await CDP.sendMessage('Runtime.enable', {}, sessionId); // enable runtime API
|
||||||
|
|
||||||
@ -92,7 +94,6 @@ export default async (CDP, proc, injectionType = 'browser', { browserName, openi
|
|||||||
jsEngine: generateVersionInfo(browserEngine === 'chromium' ? 'v8' : 'spidermonkey', browserInfo.jsVersion)
|
jsEngine: generateVersionInfo(browserEngine === 'chromium' ? 'v8' : 'spidermonkey', browserInfo.jsVersion)
|
||||||
};
|
};
|
||||||
|
|
||||||
const closeHandlers = [];
|
|
||||||
const Window = {
|
const Window = {
|
||||||
page: {
|
page: {
|
||||||
eval: evalInWindow,
|
eval: evalInWindow,
|
||||||
|
45
src/lib/local/cdp.js
Normal file
45
src/lib/local/cdp.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { basename, dirname, extname, join } from 'path';
|
||||||
|
import { readFile } from 'fs/promises';
|
||||||
|
|
||||||
|
const generatePath = (pathname, indexFile) => {
|
||||||
|
if (pathname === '/') return indexFile;
|
||||||
|
if (extname(pathname) === '') return pathname + '.html';
|
||||||
|
|
||||||
|
return pathname;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async (CDP, { sessionId, url: givenPath, localUrl }) => {
|
||||||
|
const basePath = extname(givenPath) ? dirname(givenPath) : givenPath;
|
||||||
|
const indexFile = extname(givenPath) ? basename(givenPath) : 'index.html';
|
||||||
|
|
||||||
|
CDP.onMessage(async msg => {
|
||||||
|
if (msg.method === 'Fetch.requestPaused') {
|
||||||
|
const { requestId, request } = msg.params;
|
||||||
|
|
||||||
|
const url = new URL(request.url);
|
||||||
|
const path = join(basePath, generatePath(url.pathname, indexFile));
|
||||||
|
|
||||||
|
let error = false;
|
||||||
|
|
||||||
|
const body = await readFile(path, 'utf8').catch(() => false);
|
||||||
|
if (!body) error = 404;
|
||||||
|
|
||||||
|
return await CDP.sendMessage('Fetch.fulfillRequest', {
|
||||||
|
requestId,
|
||||||
|
responseCode: error || 200,
|
||||||
|
body: Buffer.from(error ? '' : body).toString('base64'), // CDP uses base64 encoding for request body
|
||||||
|
// need to add our own headers or not?
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await CDP.sendMessage('Fetch.enable', {
|
||||||
|
patterns: [ {
|
||||||
|
urlPattern: `${localUrl}*`
|
||||||
|
} ]
|
||||||
|
});
|
||||||
|
|
||||||
|
await CDP.sendMessage('Page.reload', {}, sessionId);
|
||||||
|
|
||||||
|
log('local setup');
|
||||||
|
};
|
45
src/lib/local/server.js
Normal file
45
src/lib/local/server.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { basename, dirname, extname, join } from 'path';
|
||||||
|
import { readFile } from 'fs/promises';
|
||||||
|
import { createServer } from 'http';
|
||||||
|
|
||||||
|
const generatePath = (pathname, indexFile) => {
|
||||||
|
if (pathname === '/') return indexFile;
|
||||||
|
if (extname(pathname) === '') return pathname + '.html';
|
||||||
|
|
||||||
|
return pathname;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async ({ url: givenPath, localUrl }) => {
|
||||||
|
const basePath = extname(givenPath) ? dirname(givenPath) : givenPath;
|
||||||
|
const indexFile = extname(givenPath) ? basename(givenPath) : 'index.html';
|
||||||
|
|
||||||
|
const port = parseInt(localUrl.split(':').pop());
|
||||||
|
const server = createServer(async (req, res) => {
|
||||||
|
const url = new URL(`http://localhost:${port}` + decodeURI(req.url));
|
||||||
|
const path = join(basePath, generatePath(url.pathname, indexFile));
|
||||||
|
|
||||||
|
console.log('SERVER', url, path);
|
||||||
|
|
||||||
|
let error = false;
|
||||||
|
|
||||||
|
const body = await readFile(path, 'utf8').catch(() => false);
|
||||||
|
if (!body) error = 404;
|
||||||
|
|
||||||
|
console.log('SERVER', error);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
res.writeHead(error);
|
||||||
|
return res.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
res.writeHead(200, {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
res.end(body, 'utf8');
|
||||||
|
}).listen(port, '127.0.0.1');
|
||||||
|
|
||||||
|
log('local setup');
|
||||||
|
|
||||||
|
return () => server.close();
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user