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 Chromium from './browser/chromium.js';
|
||||||
import Firefox from './browser/firefox.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';
|
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 basePath = isAbsolute(url) ? url : join(ranJsDir, url);
|
||||||
|
|
||||||
const closeHandlers = [];
|
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)({
|
const Window = await (browserType === 'firefox' ? Firefox : Chromium)({
|
||||||
dataPath,
|
dataPath,
|
||||||
|
@ -1,48 +1,20 @@
|
|||||||
import { basename, dirname, extname, join } from 'path';
|
import createLocalFulfill from './fulfill.js';
|
||||||
import { readFile } from 'fs/promises';
|
|
||||||
import { log } from '../logger.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 }) => {
|
export default async (CDP, { sessionId, url: givenPath, localUrl }) => {
|
||||||
const basePath = extname(givenPath) ? dirname(givenPath) : givenPath;
|
const localFulfill = createLocalFulfill(givenPath);
|
||||||
const indexFile = extname(givenPath) ? basename(givenPath) : 'index.html';
|
|
||||||
|
|
||||||
CDP.onMessage(async msg => {
|
CDP.onMessage(async msg => {
|
||||||
if (msg.method === 'Fetch.requestPaused') {
|
if (msg.method === 'Fetch.requestPaused') {
|
||||||
const { requestId, request } = msg.params;
|
const { requestId, request } = msg.params;
|
||||||
|
|
||||||
const url = new URL(request.url);
|
const { status, body, headers } = await localFulfill(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
|
|
||||||
});
|
|
||||||
|
|
||||||
return await CDP.sendMessage('Fetch.fulfillRequest', {
|
return await CDP.sendMessage('Fetch.fulfillRequest', {
|
||||||
requestId,
|
requestId,
|
||||||
responseCode: 200,
|
responseCode: status,
|
||||||
body: Buffer.from(body).toString('base64'), // CDP uses base64 encoding for request body
|
body: Buffer.from(body).toString('base64'), // CDP uses base64 encoding for request body
|
||||||
responseHeaders: [
|
responseHeaders: Object.keys(headers).map(x => ({ name: x, value: headers[x] }))
|
||||||
{
|
|
||||||
name: 'Content-Type',
|
|
||||||
value: mimeType(ext)
|
|
||||||
}
|
|
||||||
]
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { basename, dirname, extname, join } from 'path';
|
import { basename, dirname, extname, join } from 'path';
|
||||||
import { readFile } from 'fs/promises';
|
import { readFile } from 'fs/promises';
|
||||||
import { createServer } from 'http';
|
|
||||||
import { log } from '../logger.js';
|
|
||||||
import mimeType from '../mimeType.js';
|
import mimeType from '../mimeType.js';
|
||||||
|
|
||||||
const generatePath = (pathname, indexFile) => {
|
const generatePath = (pathname, indexFile) => {
|
||||||
@ -11,13 +10,13 @@ const generatePath = (pathname, indexFile) => {
|
|||||||
return pathname;
|
return pathname;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async ({ url: givenPath, localUrl }) => {
|
export default givenPath => {
|
||||||
const basePath = extname(givenPath) ? dirname(givenPath) : givenPath;
|
const basePath = extname(givenPath) ? dirname(givenPath) : givenPath;
|
||||||
const indexFile = extname(givenPath) ? basename(givenPath) : 'index.html';
|
const indexFile = extname(givenPath) ? basename(givenPath) : 'index.html';
|
||||||
|
|
||||||
const port = parseInt(localUrl.split(':').pop());
|
return async url => {
|
||||||
const server = createServer(async (req, res) => {
|
url = new URL(url);
|
||||||
const url = new URL(`http://localhost:${port}` + decodeURI(req.url));
|
|
||||||
const path = join(basePath, generatePath(url.pathname, indexFile));
|
const path = join(basePath, generatePath(url.pathname, indexFile));
|
||||||
const ext = extname(path).slice(1);
|
const ext = extname(path).slice(1);
|
||||||
|
|
||||||
@ -26,19 +25,19 @@ export default async ({ url: givenPath, localUrl }) => {
|
|||||||
const body = await readFile(path, 'utf8').catch(() => false);
|
const body = await readFile(path, 'utf8').catch(() => false);
|
||||||
if (!body) error = 404;
|
if (!body) error = 404;
|
||||||
|
|
||||||
if (error) {
|
if (error) return {
|
||||||
res.writeHead(error);
|
status: 404,
|
||||||
return res.end();
|
error: true,
|
||||||
}
|
body: '',
|
||||||
|
headers: {},
|
||||||
|
};
|
||||||
|
|
||||||
res.writeHead(200, {
|
return {
|
||||||
'Content-Type': mimeType(ext)
|
status: 200,
|
||||||
});
|
body,
|
||||||
|
headers: {
|
||||||
res.end(body, 'utf8');
|
'Content-Type': mimeType(ext)
|
||||||
}).listen(port, '127.0.0.1');
|
}
|
||||||
|
};
|
||||||
log('local setup');
|
};
|
||||||
|
|
||||||
return () => server.close();
|
|
||||||
};
|
};
|
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