From d11ac0a944abf193431a05487b9a9fab4f26984e Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Sun, 29 Jan 2023 20:07:22 +0000 Subject: [PATCH] local: rewrite to share code --- src/index.js | 4 +-- src/lib/local/cdp.js | 38 ++++-------------------- src/lib/local/{server.js => fulfill.js} | 39 ++++++++++++------------- src/lib/local/http.js | 19 ++++++++++++ 4 files changed, 45 insertions(+), 55 deletions(-) rename src/lib/local/{server.js => fulfill.js} (55%) create mode 100644 src/lib/local/http.js diff --git a/src/index.js b/src/index.js index 2fdd9a9..0990db4 100644 --- a/src/index.js +++ b/src/index.js @@ -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, diff --git a/src/lib/local/cdp.js b/src/lib/local/cdp.js index ba6939d..8e7cddf 100644 --- a/src/lib/local/cdp.js +++ b/src/lib/local/cdp.js @@ -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] })) }); } }); diff --git a/src/lib/local/server.js b/src/lib/local/fulfill.js similarity index 55% rename from src/lib/local/server.js rename to src/lib/local/fulfill.js index 1d30e40..9b05ef2 100644 --- a/src/lib/local/server.js +++ b/src/lib/local/fulfill.js @@ -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) + } + }; + }; }; \ No newline at end of file diff --git a/src/lib/local/http.js b/src/lib/local/http.js new file mode 100644 index 0000000..97add21 --- /dev/null +++ b/src/lib/local/http.js @@ -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(); +}; \ No newline at end of file