From: Ardis Lu Date: Mon, 7 Sep 2026 06:07:08 +0000 (-0700) Subject: Refactor global `SERVER` singleton into dependency injection X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=915dae0fbb3f13446e4ffa49b3214fbb1b159c46;p=envpass Refactor global `SERVER` singleton into dependency injection --- diff --git a/src/getPrf.js b/src/getPrf.js index bb2de19..cefd1a5 100644 --- a/src/getPrf.js +++ b/src/getPrf.js @@ -10,16 +10,10 @@ import open from 'open'; * Load from disk the HTML to be served by the passkey bridge server. * @returns {Promise} The HTML file to serve. */ -async function getHTML() { +export async function getHTMLFromFile() { return await readFile(new URL('./getPrf.html', import.meta.url), { encoding: 'utf8' }); } -/** - * Wrapper around server operations. This wrapper object is required to mock the methods for - * testing purposes. - */ -export const SERVER = { getHTML }; - /** * 2 minutes in milliseconds. After this duration, the http server will close and the flow * must be restarted. @@ -28,6 +22,8 @@ export const WINDOW_EXPIRATION_DURATION = 2 * 60 * 1000; /** * @typedef {Object} GetPrfOptions + * @property {() => string} [getHTML] Function which returns the HTML to serve as the passkey page. The default value + * is a function that reads a local `getPrf.html` file from disk and returns it as a string. * @property {((url: string) => unknown)|null} [onListening] Function which will be called after the server is ready. * The function is passed a URL on `localhost` to complete the passkey flow. The default value is * {@link https://github.com/sindresorhus/open|`open`}, which will open the default web browser to the passkey page. @@ -59,6 +55,7 @@ export const WINDOW_EXPIRATION_DURATION = 2 * 60 * 1000; */ export async function getPrf(options = {}) { const { + getHTML = getHTMLFromFile, onListening = open, port = undefined, signal = undefined @@ -72,7 +69,7 @@ export async function getPrf(options = {}) { const { promise: prfPromise, resolve: prfResolve } = /** @type {PromiseWithResolvers}*/(Promise.withResolvers()); const { promise: abortPromise, resolve: abortResolve } = /** @type {PromiseWithResolvers}*/(Promise.withResolvers()); - const html = await SERVER.getHTML(); + const html = await getHTML(); // Generate hashes of