Files
crowd-funder-from-jason/src/services/platforms/ElectronPlatformService.ts
Matthew Raymer d9ce884513 fix: configure Vite for proper Node.js module handling in Electron
- Add vite-plugin-node-polyfills to provide Node.js built-in module polyfills
- Configure build target as 'node18' for Electron environment
- Switch to CommonJS format for Electron builds
- Add specific polyfills for sqlite3 dependencies (util, stream, buffer)
- Mark Node.js built-in modules as external in Electron builds

This fixes the "Module util has been externalized" error by properly handling
Node.js modules in the Electron environment, particularly for sqlite3 which
depends on Node.js built-in modules.
2025-05-26 14:06:21 +00:00

102 lines
3.1 KiB
TypeScript

import {
PlatformService,
PlatformCapabilities,
SQLiteOperations,
SQLiteConfig,
PreparedStatement,
SQLiteResult,
ImageResult,
} from "../PlatformService";
import { BaseSQLiteService } from "../sqlite/BaseSQLiteService";
import { app } from "electron";
import { dialog } from "electron";
import fs from "fs";
import path from "path";
import sqlite3 from "sqlite3";
import { open, Database } from "sqlite";
import { logger } from "../../utils/logger";
import { Settings } from "../../db/tables/settings";
import { Account } from "../../db/tables/accounts";
import { Contact } from "../../db/tables/contacts";
import { db } from "../../db";
import { MASTER_SETTINGS_KEY } from "../../db/tables/settings";
import { accountsDBPromise } from "../../db";
import { accessToken } from "../../libs/crypto";
import { getPlanFromCache as getPlanFromCacheImpl } from "../../libs/endorserServer";
import { PlanSummaryRecord } from "../../interfaces/records";
import { Axios } from "axios";
// Create Promise-based versions of fs functions
const readFileAsync = (filePath: string, encoding: BufferEncoding): Promise<string> => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, { encoding }, (err: NodeJS.ErrnoException | null, data: string) => {
if (err) reject(err);
else resolve(data);
});
});
};
const readFileBufferAsync = (filePath: string): Promise<Buffer> => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err: NodeJS.ErrnoException | null, data: Buffer) => {
if (err) reject(err);
else resolve(data);
});
});
};
const writeFileAsync = (filePath: string, data: string, encoding: BufferEncoding): Promise<void> => {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, data, { encoding }, (err: NodeJS.ErrnoException | null) => {
if (err) reject(err);
else resolve();
});
});
};
const unlinkAsync = (filePath: string): Promise<void> => {
return new Promise((resolve, reject) => {
fs.unlink(filePath, (err: NodeJS.ErrnoException | null) => {
if (err) reject(err);
else resolve();
});
});
};
const readdirAsync = (dirPath: string): Promise<string[]> => {
return new Promise((resolve, reject) => {
fs.readdir(dirPath, (err: NodeJS.ErrnoException | null, files: string[]) => {
if (err) reject(err);
else resolve(files);
});
});
};
const statAsync = (filePath: string): Promise<fs.Stats> => {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err: NodeJS.ErrnoException | null, stats: fs.Stats) => {
if (err) reject(err);
else resolve(stats);
});
});
};
interface SQLiteDatabase extends Database {
changes: number;
}
/**
* SQLite implementation for Electron using native sqlite3
*/
class ElectronSQLiteService extends BaseSQLiteService {
private db: SQLiteDatabase | null = null;
private config: SQLiteConfig | null = null;
// ... rest of the ElectronSQLiteService implementation ...
}
export class ElectronPlatformService implements PlatformService {
private sqliteService: ElectronSQLiteService | null = null;
// ... rest of the ElectronPlatformService implementation ...
}