mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-19 16:21:13 +01:00
From SEA to regular NodeJS
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import path from 'node:path';
|
||||
import { Worker } from 'node:worker_threads';
|
||||
import { PluginInfo } from './plugins';
|
||||
|
||||
@@ -24,10 +25,11 @@ export class PluginHandle {
|
||||
readonly pluginDir: string;
|
||||
readonly #worker: Worker;
|
||||
|
||||
constructor({ pluginDir, workerJsPath }: { pluginDir: string; workerJsPath: string }) {
|
||||
constructor(pluginDir: string) {
|
||||
this.pluginDir = pluginDir;
|
||||
|
||||
this.#worker = new Worker(workerJsPath, {
|
||||
const workerPath = path.join(__dirname, 'index.worker.cjs');
|
||||
this.#worker = new Worker(workerPath, {
|
||||
workerData: {
|
||||
pluginDir: this.pluginDir,
|
||||
},
|
||||
|
||||
@@ -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() })));
|
||||
|
||||
@@ -8,11 +8,11 @@ export interface PluginInfo {
|
||||
capabilities: ('import' | 'export' | 'filter')[];
|
||||
}
|
||||
|
||||
export function loadPlugins(workerJsPath: string): PluginHandle[] {
|
||||
export function loadPlugins(): PluginHandle[] {
|
||||
const pluginsDir = process.env.PLUGINS_DIR;
|
||||
if (!pluginsDir) throw new Error('PLUGINS_DIR is not set');
|
||||
console.log('Loading plugins from', pluginsDir);
|
||||
|
||||
const pluginDirs = fs.readdirSync(pluginsDir).map((p) => path.join(pluginsDir, p));
|
||||
return pluginDirs.map((pluginDir) => new PluginHandle({ pluginDir, workerJsPath }));
|
||||
return pluginDirs.map((pluginDir) => new PluginHandle(pluginDir));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user