Skip to content

Commit

Permalink
fix: 插件无法加载
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Sep 7, 2024
1 parent 1a49164 commit eb42b8a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 206 deletions.
171 changes: 0 additions & 171 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"http2-express-bridge": "^1.0.7",
"jsonwebtoken": "^9.0.2",
"socket.io": "^4.7.5",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
},
"devDependencies": {
Expand Down
57 changes: 25 additions & 32 deletions src/plugin/PluginLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,49 @@ import { Plugin } from './Plugin';
import { Server } from '../server';

export class PluginLoader {
private plugins: Plugin[] = [];
private pluginInstances: Plugin[] = [];

async loadPlugins(server: Server, directory: string = './plugins'): Promise<Plugin[]> {
const files = fs.readdirSync(directory);
async loadPlugins(server: Server, pluginDir: string = './plugins'): Promise<Plugin[]> {
const pluginFiles = fs.readdirSync(pluginDir);
for (const file of pluginFiles) {
const fullPath = path.join(pluginDir, file);

for (const file of files) {
const filePath = path.join(directory, file);

if (file.endsWith('.ts') || file.endsWith('.js')) {
// 只加载 .ts 或 .js 文件
if (file.endsWith('.js')) {
try {
const module = await import(filePath);
const module = await import(path.resolve(fullPath));

for (const key in module) {
const Class = module[key];
for (const exportedKey in module) {
const ExportedClass = module[exportedKey];

if (this.isPluginClass(Class)) {
let instance: Plugin | null = null;
if (this.isConcretePluginClass(ExportedClass)) {
let pluginInstance: Plugin | null = null;

try {
instance = this.createInstance(Class);
} catch {
pluginInstance = new (ExportedClass as new () => Plugin)();
} catch (error) {
try {
instance = this.createInstance(Class, server);
} catch {
// Handle instantiation errors as needed
pluginInstance = new (ExportedClass as unknown as new (server: Server) => Plugin)(server);
} catch (error) {
console.error(`Failed to instantiate plugin from ${file} with a Server parameter:`, error);
}
}

if (instance) {
this.plugins.push(instance);
if (pluginInstance) {
this.pluginInstances.push(pluginInstance);
}
}
}
} catch {
// Handle module loading errors as needed
} catch (err) {
console.error(`Failed to load plugin from ${file}:`, err);
}
}
}

return this.plugins;
}

private isPluginClass(value: any): value is typeof Plugin {
return typeof value === 'function' && value.prototype instanceof Plugin && value !== Plugin;
return this.pluginInstances;
}

private createInstance(Class: any, server?: Server): Plugin {
if (server) {
return new Class(server);
}
return new Class();
private isConcretePluginClass(obj: any): obj is typeof Plugin {
return typeof obj === 'function' && obj.prototype instanceof Plugin && !Object.is(obj, Plugin);
}
}
}
7 changes: 5 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export class Server {
this.plugins = [];
this.pluginLoader = new PluginLoader();
this.pluginLoader.loadPlugins(this)
.then(plugins => this.plugins = plugins)
.then(plugins => {
this.plugins = plugins;
plugins.forEach(plugin => plugin.init());
})
.catch(error => console.error(error));

this.files = [];
Expand Down Expand Up @@ -398,7 +401,7 @@ export class Server {
}

res.status(302)
.setHeader('Location', `http://${cluster.endpoint}:${cluster.port}/download/${file.hash}?${Utilities.getSign(file.hash, cluster.clusterSecret)}`)
.header('Location', Utilities.getUrl(file, cluster))
.send();
cluster.pendingHits++;
cluster.pendingTraffic += file.size;
Expand Down

0 comments on commit eb42b8a

Please sign in to comment.