local: rewrite to share code
This commit is contained in:
parent
352c09d292
commit
d11ac0a944
@ -6,7 +6,7 @@ import { log } from './lib/logger.js';
|
||||
import Chromium from './browser/chromium.js';
|
||||
import Firefox from './browser/firefox.js';
|
||||
|
||||
import LocalServer from './lib/local/server.js';
|
||||
import LocalHTTP from './lib/local/http.js';
|
||||
|
||||
process.versions.gluon = '0.13.0-alpha.0';
|
||||
|
||||
@ -192,7 +192,7 @@ const startBrowser = async (url, { windowSize, forceBrowser, forceEngine }) => {
|
||||
const basePath = isAbsolute(url) ? url : join(ranJsDir, url);
|
||||
|
||||
const closeHandlers = [];
|
||||
if (openingLocal && browserType === 'firefox') closeHandlers.push(await LocalServer({ localUrl, url: basePath }));
|
||||
if (openingLocal && browserType === 'firefox') closeHandlers.push(await LocalHTTP({ localUrl, url: basePath }));
|
||||
|
||||
const Window = await (browserType === 'firefox' ? Firefox : Chromium)({
|
||||
dataPath,
|
||||
|
@ -1,48 +1,20 @@
|
||||
import { basename, dirname, extname, join } from 'path';
|
||||
import { readFile } from 'fs/promises';
|
||||
import createLocalFulfill from './fulfill.js';
|
||||
import { log } from '../logger.js';
|
||||
import mimeType from '../mimeType.js';
|
||||
|
||||
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';
|
||||
const localFulfill = createLocalFulfill(givenPath);
|
||||
|
||||
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));
|
||||
const ext = extname(path).slice(1);
|
||||
|
||||
let error = false;
|
||||
|
||||
const body = await readFile(path, 'utf8').catch(() => false);
|
||||
if (!body) error = 404;
|
||||
|
||||
if (error) return await CDP.sendMessage('Fetch.fulfillRequest', {
|
||||
requestId,
|
||||
responseCode: error,
|
||||
body: Buffer.from('').toString('base64') // CDP uses base64 encoding for request body
|
||||
});
|
||||
const { status, body, headers } = await localFulfill(request.url);
|
||||
|
||||
return await CDP.sendMessage('Fetch.fulfillRequest', {
|
||||
requestId,
|
||||
responseCode: 200,
|
||||
responseCode: status,
|
||||
body: Buffer.from(body).toString('base64'), // CDP uses base64 encoding for request body
|
||||
responseHeaders: [
|
||||
{
|
||||
name: 'Content-Type',
|
||||
value: mimeType(ext)
|
||||
}
|
||||
]
|
||||
responseHeaders: Object.keys(headers).map(x => ({ name: x, value: headers[x] }))
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { basename, dirname, extname, join } from 'path';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { createServer } from 'http';
|
||||
import { log } from '../logger.js';
|
||||
|
||||
import mimeType from '../mimeType.js';
|
||||
|
||||
const generatePath = (pathname, indexFile) => {
|
||||
@ -11,13 +10,13 @@ const generatePath = (pathname, indexFile) => {
|
||||
return pathname;
|
||||
};
|
||||
|
||||
export default async ({ url: givenPath, localUrl }) => {
|
||||
export default givenPath => {
|
||||
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));
|
||||
return async url => {
|
||||
url = new URL(url);
|
||||
|
||||
const path = join(basePath, generatePath(url.pathname, indexFile));
|
||||
const ext = extname(path).slice(1);
|
||||
|
||||
@ -26,19 +25,19 @@ export default async ({ url: givenPath, localUrl }) => {
|
||||
const body = await readFile(path, 'utf8').catch(() => false);
|
||||
if (!body) error = 404;
|
||||
|
||||
if (error) {
|
||||
res.writeHead(error);
|
||||
return res.end();
|
||||
}
|
||||
if (error) return {
|
||||
status: 404,
|
||||
error: true,
|
||||
body: '',
|
||||
headers: {},
|
||||
};
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': mimeType(ext)
|
||||
});
|
||||
|
||||
res.end(body, 'utf8');
|
||||
}).listen(port, '127.0.0.1');
|
||||
|
||||
log('local setup');
|
||||
|
||||
return () => server.close();
|
||||
return {
|
||||
status: 200,
|
||||
body,
|
||||
headers: {
|
||||
'Content-Type': mimeType(ext)
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
19
src/lib/local/http.js
Normal file
19
src/lib/local/http.js
Normal file
@ -0,0 +1,19 @@
|
||||
import { createServer } from 'http';
|
||||
import createLocalFulfill from './fulfill.js';
|
||||
import { log } from '../logger.js';
|
||||
|
||||
export default async ({ url: givenPath, localUrl }) => {
|
||||
const localFulfill = createLocalFulfill(givenPath);
|
||||
|
||||
const port = parseInt(localUrl.split(':').pop());
|
||||
const server = createServer(async (req, res) => {
|
||||
const { status, body, headers } = await localFulfill(localUrl + decodeURI(req.url));
|
||||
|
||||
res.writeHead(status, headers);
|
||||
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