diff --git a/src/controllers/plugins.controller.ts b/src/controllers/plugins.controller.ts index 273b6c219224208089807ea0fdd6fc5432086d0f..2f08a04ff5f60b73bb61ba14e904b673a6ec2f2b 100644 --- a/src/controllers/plugins.controller.ts +++ b/src/controllers/plugins.controller.ts @@ -194,7 +194,6 @@ export class PluginsController { */ // GET /download/:arch/:id - // eslint-disable-next-line @typescript-eslint/no-misused-promises this.router.get( '/download/:arch/:id', // eslint-disable-next-line @typescript-eslint/no-misused-promises @@ -322,7 +321,7 @@ export class PluginsController { // GET /versions/:id?arch=:arch // eslint-disable-next-line @typescript-eslint/no-misused-promises - this.router.get('/versions/', async (req: Request, res: Response) => { + this.router.get('/versions/:id', async (req: Request, res: Response) => { try { if (req.query.arch === undefined) { res.status(StatusCodes.BAD_REQUEST).send(); diff --git a/src/services/certificate_manager.ts b/src/services/certificate_manager.ts index 7c51dd4c47d78e6181f6b0209190fcfc8c1ad38d..c18cb13037675bfe4aaaa720420d49ffdd819a3b 100644 --- a/src/services/certificate_manager.ts +++ b/src/services/certificate_manager.ts @@ -23,7 +23,7 @@ import {X509Certificate} from 'crypto'; export class CertificateManagerService { constructor(private readonly fileManager: FileManagerService) {} - async getIssuer(path: string, file: string): Promise<{CN: string}> { + async getIssuer(path: string, file: string): Promise<Record<string, string>> { const certificate = await this.readCertificate(path, file); return this.parseDN(certificate.issuer); } @@ -39,12 +39,13 @@ export class CertificateManagerService { }); } - parseDN(dn: string): {CN: string} { - // eslint-disable-next-line - return dn.split('\n').reduce<any>((acc: any, pair: string, _, { }) => { - const [key, value] = pair.split('='); - acc[key] = value; - return acc; - }, {}); + parseDN(dn: string): Record<string, string> { + return dn + .split('\n') + .reduce((acc: Record<string, string>, pair: string) => { + const [key, value] = pair.split('='); + acc[key] = value; + return acc; + }, {}); } } diff --git a/src/services/plugins_manager.service.ts b/src/services/plugins_manager.service.ts index f81af17ce2c1a2b2b88780a505b68e96917dcf1b..39308dd991f89c470d51119abbca9c24b1a1895a 100644 --- a/src/services/plugins_manager.service.ts +++ b/src/services/plugins_manager.service.ts @@ -30,6 +30,10 @@ export class PluginsManager { private readonly fileManager: FileManagerService, private readonly certificateManager: CertificateManagerService ) { + if (process.env.DATA_DIRECTORY === undefined) { + return; + } + const watcher = this.fileManager.watchFile( // eslint-disable-next-line __dirname + '/../..' + process.env.DATA_DIRECTORY @@ -38,8 +42,10 @@ export class PluginsManager { return; } // eslint-disable-next-line - watcher.on('change', async () => { - await this.setPlugins(); + watcher.on('change', () => { + this.setPlugins().catch(e => { + console.log(e); + }); }); } @@ -117,7 +123,10 @@ export class PluginsManager { ); } - async addPlugin(path: string, arches: string[]): Promise<void> { + async getNewPlugin( + path: string, + arches: string[] + ): Promise<Plugins | undefined> { try { const plugin = await this.readManifest(path); const timestamp = (await this.fileManager.getStat(path)).mtime.toString(); @@ -128,7 +137,7 @@ export class PluginsManager { if (issuer === undefined) { return; } - this.plugins.push({ + return { name: plugin.name, version: plugin.version, description: plugin.description, @@ -136,10 +145,12 @@ export class PluginsManager { arches, timestamp, author: issuer.CN, - }); + }; } catch (e) { console.error(e); } + // eslint-disable-next-line + return; } private isPluginAvailable(id: string, arch: string): boolean { @@ -234,7 +245,7 @@ export class PluginsManager { } // eslint-disable-next-line dataDirectory = __dirname + '/../..' + dataDirectory; - this.plugins = []; + const plugins = []; const pluginsPath = await this.fileManager.listFiles(dataDirectory); if (pluginsPath === undefined || pluginsPath.length === 0) { return; @@ -247,7 +258,7 @@ export class PluginsManager { return; } for (const platformPath of platformPaths) { - await this.addPlugin( + const plugin = await this.getNewPlugin( dataDirectory + '/' + pluginPath + @@ -258,7 +269,12 @@ export class PluginsManager { '.jpl', [platformPath] ); + if (plugin === undefined) { + continue; + } + plugins.push(plugin); } } + this.plugins = plugins; } } diff --git a/tests/plugins.controller.test.ts b/tests/plugins.controller.test.ts index 4196e9029e4d47b8959bf0c9884e9a0f61c0f054..19f403a0a47960087d63811c4d746556790948d1 100644 --- a/tests/plugins.controller.test.ts +++ b/tests/plugins.controller.test.ts @@ -211,7 +211,7 @@ describe('Routes', function () { request(expressApp) .get("/versions/?arch=test") - .expect(StatusCodes.INTERNAL_SERVER_ERROR) + .expect(StatusCodes.NOT_FOUND) .then((response) => { done(); }); diff --git a/tests/plugins.manager.test.ts b/tests/plugins.manager.test.ts index 7151cffe9c358545750dfbf30a48d8f575fe7a3e..7ecac6a29b618252da0fd9b8051ccb5ca36c9fcc 100644 --- a/tests/plugins.manager.test.ts +++ b/tests/plugins.manager.test.ts @@ -149,7 +149,7 @@ describe('Plugins manager service tests', function () { // Get the modification timestamp of the file - await pluginsManagerService.addPlugin(pluginPath, ['arch1', 'arch2']); + const newPlugin = await pluginsManagerService.getNewPlugin(pluginPath, ['arch1', 'arch2']); const expectedPlugin = { name: 'test', @@ -161,7 +161,7 @@ describe('Plugins manager service tests', function () { author: 'author', }; - expect(pluginsManagerService['plugins']).toContainEqual(expectedPlugin); + expect(newPlugin).toEqual(expectedPlugin); }); @@ -183,7 +183,7 @@ describe('Plugins manager service tests', function () { // Get the modification timestamp of the file - await pluginsManagerService.addPlugin(pluginPath, ['test']); + await pluginsManagerService.getNewPlugin(pluginPath, ['test']); await pluginsManagerService.removePlugin('test'); const isAvailable = pluginsManagerService['isPluginAvailable']('test', 'test'); expect(isAvailable).toBeFalsy(); @@ -347,16 +347,16 @@ describe('Plugins manager service tests', function () { process.env.DATA_DIRECTORY = dataDirectory; fileManager.listFiles.onFirstCall().resolves([pluginsPath]).onSecondCall().resolves([platformPath]); - // Create a spy for the addPlugin method - const addPluginStub = Sinon.stub(pluginsManagerService, 'addPlugin'); + // Create a spy for the getNewPlugin method + const getNewPluginStub = Sinon.stub(pluginsManagerService, 'getNewPlugin'); await pluginsManagerService['setPlugins'](); - // Assert that the addPlugin method was called once - expect(addPluginStub.calledOnce).toBeTruthy(); + // Assert that the getNewPlugin method was called once + expect(getNewPluginStub.calledOnce).toBeTruthy(); - // Restore the original addPlugin method - addPluginStub.restore(); + // Restore the original getNewPlugin method + getNewPluginStub.restore(); });