1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-02-13 19:42:36 +02:00

Plugins: Fix crash when path includes trailing slash

This commit is contained in:
Laurent Cozic 2020-11-13 22:03:10 +00:00
parent 8305eb4403
commit 918b768634
2 changed files with 15 additions and 0 deletions

View File

@ -44,6 +44,8 @@ describe('services_PluginService', function() {
const service = newPluginService();
await service.loadAndRunPlugins([`${testPluginDir}/simple`]);
expect(() => service.pluginById('simple')).not.toThrowError();
const allFolders = await Folder.all();
expect(allFolders.length).toBe(1);
expect(allFolders[0].title).toBe('my plugin folder');
@ -54,6 +56,12 @@ describe('services_PluginService', function() {
expect(allNotes[0].parent_id).toBe(allFolders[0].id);
}));
it('should load and run a simple plugin and handle trailing slash', asyncTest(async () => {
const service = newPluginService();
await service.loadAndRunPlugins([`${testPluginDir}/simple/`]);
expect(() => service.pluginById('simple')).not.toThrowError();
}));
it('should load and run a plugin that uses external packages', asyncTest(async () => {
const service = newPluginService();
await service.loadAndRunPlugins([`${testPluginDir}/withExternalModules`]);

View File

@ -4,6 +4,7 @@ import Global from './api/Global';
import BasePluginRunner from './BasePluginRunner';
import BaseService from '../BaseService';
import shim from '../../shim';
import { rtrimSlashes } from '../../path-utils';
const { filename, dirname } = require('../../path-utils');
const uslug = require('uslug');
@ -86,11 +87,15 @@ export default class PluginService extends BaseService {
}
public async loadPluginFromString(pluginId: string, baseDir: string, jsBundleString: string): Promise<Plugin> {
baseDir = rtrimSlashes(baseDir);
const r = await this.parsePluginJsBundle(jsBundleString);
return this.loadPlugin(pluginId, baseDir, r.manifestText, r.scriptText);
}
private async loadPluginFromPath(path: string): Promise<Plugin> {
path = rtrimSlashes(path);
const fsDriver = shim.fsDriver();
if (path.toLowerCase().endsWith('.js')) return this.loadPluginFromString(filename(path), dirname(path), await fsDriver.readFile(path));
@ -110,6 +115,8 @@ export default class PluginService extends BaseService {
}
private async loadPlugin(pluginId: string, baseDir: string, manifestText: string, scriptText: string): Promise<Plugin> {
baseDir = rtrimSlashes(baseDir);
const manifest = manifestFromObject(JSON.parse(manifestText));
// After transforming the plugin path to an ID, multiple plugins might end up with the same ID. For