Skip to content

Commit

Permalink
chore: migrate electron-serve to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillzyusko committed Jul 22, 2024
1 parent 39dbe50 commit 5db459c
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions desktop/electron-serve.js → desktop/electron-serve.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
/* eslint-disable @typescript-eslint/no-misused-promises */

/* eslint-disable rulesdir/no-negated-variables */

/* eslint-disable no-param-reassign */

/* eslint-disable @lwc/lwc/no-async-await */

/**
* This file is a modified version of the electron-serve package.
* We keep the same interface, but instead of file protocol we use buffer protocol (with support of JS self profiling).
*/
const {app, protocol, session} = require('electron');
const path = require('path');
const fs = require('fs');
import type {BrowserWindow, Protocol} from 'electron';
import {app, protocol, session} from 'electron';
import fs from 'fs';
import path from 'path';

type RegisterBufferProtocol = Protocol['registerBufferProtocol'];
type HandlerType = Parameters<RegisterBufferProtocol>[1];
type Optional<T> = T | null | undefined;

const FILE_NOT_FOUND = -6;

const getPath = async (filePath) => {
const getPath = async (filePath: string): Promise<Optional<string>> => {
try {
const result = await fs.promises.stat(filePath);

Expand All @@ -21,15 +30,25 @@ const getPath = async (filePath) => {
}

if (result.isDirectory()) {
// eslint-disable-next-line @typescript-eslint/return-await
return getPath(path.join(filePath, 'index.html'));
}
} catch {
return null;
}
};

export default function electronServe(options) {
options = {
type ServeOptions = {
directory: string;
isCorsEnabled?: boolean;
scheme?: string;
hostname?: string;
file?: string;
partition?: string;
};

export default function electronServe(options: ServeOptions) {
const mandatoryOptions = {
isCorsEnabled: true,
scheme: 'app',
hostname: '-',
Expand All @@ -43,16 +62,17 @@ export default function electronServe(options) {

options.directory = path.resolve(app.getAppPath(), options.directory);

const handler = async (request, callback) => {
const handler: HandlerType = async (request, callback) => {
const filePath = path.join(options.directory, decodeURIComponent(new URL(request.url).pathname));
const resolvedPath = (await getPath(filePath)) || path.join(options.directory, `${options.file}.html`);
const resolvedPath = (await getPath(filePath)) ?? path.join(options.directory, `${options.file}.html`);

try {
const data = await fs.promises.readFile(resolvedPath);
callback({
mimeType: 'text/html',
data: Buffer.from(data),
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Document-Policy': 'js-profiling',
},
});
Expand All @@ -63,7 +83,7 @@ export default function electronServe(options) {

protocol.registerSchemesAsPrivileged([
{
scheme: options.scheme,
scheme: mandatoryOptions.scheme,
privileges: {
standard: true,
secure: true,
Expand All @@ -75,12 +95,13 @@ export default function electronServe(options) {
]);

app.on('ready', () => {
const partitionSession = options.partition ? session.fromPartition(options.partition) : session.defaultSession;
const partitionSession = mandatoryOptions.partition ? session.fromPartition(mandatoryOptions.partition) : session.defaultSession;

partitionSession.protocol.registerBufferProtocol(options.scheme, handler);
partitionSession.protocol.registerBufferProtocol(mandatoryOptions.scheme, handler);
});

return async (window_, searchParameters) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
return async (window_: BrowserWindow, searchParameters?: URLSearchParams) => {
const queryString = searchParameters ? `?${new URLSearchParams(searchParameters).toString()}` : '';
await window_.loadURL(`${options.scheme}://${options.hostname}${queryString}`);
};
Expand Down

0 comments on commit 5db459c

Please sign in to comment.