From SEA to regular NodeJS

This commit is contained in:
Gregory Schier
2024-07-21 22:14:17 -07:00
parent 63ba00d1a7
commit f1433b59d4
10 changed files with 47 additions and 141 deletions

View File

@@ -1,14 +1,9 @@
import { existsSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { getAsset } from 'node:sea';
import { PluginHandle } from './PluginHandle';
import { loadPlugins, PluginInfo } from './plugins';
export class PluginManager {
#handles: PluginHandle[] | null = null;
static #instance: PluginManager | null = null;
static #workerPath = path.join(tmpdir(), `index.${Math.random()}.worker.js`);
public static instance(): PluginManager {
if (PluginManager.#instance == null) {
@@ -19,22 +14,10 @@ export class PluginManager {
}
async plugins(): Promise<PluginHandle[]> {
await this.#ensureWorkerForSea();
this.#handles = this.#handles ?? loadPlugins(PluginManager.#workerPath);
this.#handles = this.#handles ?? loadPlugins();
return this.#handles;
}
/**
* Copy worker JS asset to filesystem if we're in single-executable-application (SEA)
* @private
*/
async #ensureWorkerForSea() {
if (existsSync(PluginManager.#workerPath)) return;
console.log('Writing worker file to', PluginManager.#workerPath);
writeFileSync(PluginManager.#workerPath, getAsset('worker', 'utf8'));
}
async #pluginsWithInfo(): Promise<{ plugin: PluginHandle; info: PluginInfo }[]> {
const plugins = await this.plugins();
return Promise.all(plugins.map(async (plugin) => ({ plugin, info: await plugin.getInfo() })));