From 334f5a7507f9ce0bafdbdfb20ec4c41367bfbb9d Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Mon, 26 Apr 2021 18:32:31 +0200 Subject: [PATCH] refactor: remove legacy callbacks from bulk storage --- src/routes/bulk-add-route.ts | 2 +- src/routes/bulk-done-route.ts | 2 +- src/routes/virtual-plugin-route.ts | 5 +++-- src/storage/bulk-storage.ts | 17 ++++++++--------- test/routes/virtual-plugin-route.spec.ts | 6 +++--- test/storage/bulk-storage.spec.ts | 4 ++-- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/routes/bulk-add-route.ts b/src/routes/bulk-add-route.ts index 522bbbdb..cfba7d5d 100644 --- a/src/routes/bulk-add-route.ts +++ b/src/routes/bulk-add-route.ts @@ -32,7 +32,7 @@ export const bulkAddRouter = createRoute( async (request, app, params) => { const bulkMemory: BulkStorage = app.get('bulk'); - const bulk = await bulkMemory.read(params.UID); + const bulk = bulkMemory.read(params.UID); if (typeof bulk === 'undefined') { Logger.warn(`Bulk with ${params.UID} not found.`); diff --git a/src/routes/bulk-done-route.ts b/src/routes/bulk-done-route.ts index 5e8c1f55..62b13cd9 100644 --- a/src/routes/bulk-done-route.ts +++ b/src/routes/bulk-done-route.ts @@ -32,7 +32,7 @@ export const bulkDoneRouter = createRoute async (_request, app, params) => { const bulkMemory: BulkStorage = app.get('bulk'); - const bulk = await bulkMemory.read(params.UID); + const bulk = bulkMemory.read(params.UID); if (typeof bulk === 'undefined') { Logger.warn(`Bulk with ${params.UID} not found.`); diff --git a/src/routes/virtual-plugin-route.ts b/src/routes/virtual-plugin-route.ts index 1c31e710..41721825 100644 --- a/src/routes/virtual-plugin-route.ts +++ b/src/routes/virtual-plugin-route.ts @@ -39,14 +39,15 @@ export async function virtualPluginRoute(req: Request, plugin: SCPluginMetaData) } // send the request to the plugin (forward the body) and save the response const pluginResponse = await got.post( - plugin.route, + plugin.route.replace(/^\//gi, ''), { prefixUrl: plugin.address, json: req.body, timeout: configFile.backend.externalRequestTimeout, + responseType: 'json', }, ); - responseBody = JSON.parse(pluginResponse.body); + responseBody = pluginResponse.body as object; const responseValidation = validator.validate(responseBody, plugin.responseSchema); if (responseValidation.errors.length > 0) { throw new SCValidationErrorResponse(responseValidation.errors, isTestEnvironment); diff --git a/src/storage/bulk-storage.ts b/src/storage/bulk-storage.ts index 0b81be5b..c85ee5b8 100644 --- a/src/storage/bulk-storage.ts +++ b/src/storage/bulk-storage.ts @@ -17,7 +17,6 @@ import {SCBulkRequest, SCThingType} from '@openstapps/core'; import {Logger} from '@openstapps/logger'; import moment from 'moment'; import NodeCache from 'node-cache'; -import {promisify} from 'util'; import {v4} from 'uuid'; import {Database} from './database'; @@ -125,14 +124,14 @@ export class BulkStorage { * @param bulk the bulk process to save * @returns the bulk process that was saved */ - private async save(bulk: Bulk): Promise { + private save(bulk: Bulk): Bulk { const expirationInSeconds = moment(bulk.expiration) // tslint:disable-next-line: no-magic-numbers .diff(moment.now()) / 1000; Logger.info('Bulk expires in ', expirationInSeconds, 'seconds'); // save the item in the cache with it's expected expiration - await promisify(this.cache.set)(bulk.uid, bulk, expirationInSeconds); + this.cache.set(bulk.uid, bulk, expirationInSeconds); return bulk; } @@ -147,7 +146,7 @@ export class BulkStorage { bulk.source = bulkRequest.source; bulk.type = bulkRequest.type; - await this.save(bulk); + this.save(bulk); // tell the database that the bulk was created await this.database.bulkCreated(bulk); @@ -161,14 +160,14 @@ export class BulkStorage { * @returns a promise that contains the deleted bulk process */ public async delete(uid: string): Promise { - const bulk = await this.read(uid); + const bulk = this.read(uid); if (typeof bulk === 'undefined') { throw new Error(`Bulk that should be deleted was not found. UID was "${uid}"`); } // delete the bulk process from the cache - await promisify(this.cache.del)(uid); + this.cache.del(uid); // tell the database to handle the expiration of the bulk await this.database.bulkExpired(bulk); @@ -183,7 +182,7 @@ export class BulkStorage { */ public async markAsDone(bulk: Bulk): Promise { bulk.state = 'done'; - await this.save(bulk); + this.save(bulk); // tell the database that this is the new bulk await this.database.bulkUpdated(bulk); @@ -196,8 +195,8 @@ export class BulkStorage { * @param uid uid of the bulk process * @returns a promise that contains a bulk */ - public async read(uid: string): Promise { - return promisify(this.cache.get)(uid); + public read(uid: string): Bulk | undefined { + return this.cache.get(uid); } } diff --git a/test/routes/virtual-plugin-route.spec.ts b/test/routes/virtual-plugin-route.spec.ts index 7b959de1..9274ae76 100644 --- a/test/routes/virtual-plugin-route.spec.ts +++ b/test/routes/virtual-plugin-route.spec.ts @@ -21,7 +21,7 @@ import { import {expect, use} from 'chai'; import chaiAsPromised from 'chai-as-promised'; import {Request} from 'express'; -import got, { Options } from 'got'; +import got, {Options} from 'got'; import nock from 'nock'; import sinon from 'sinon'; import {mockReq} from 'sinon-express-mock'; @@ -79,9 +79,9 @@ describe('Virtual plugin routes', async function () { await virtualPluginRoute(req, plugin); - expect(gotStub.args[0][0]).to.equal(plugin.route); + expect(gotStub.args[0][0]).to.equal(plugin.route.substr(1)); expect(((gotStub.args[0] as any)[1] as Options).prefixUrl).to.equal(plugin.address); - expect(((gotStub.args[0] as any)[1] as Options).body).to.equal(req.body); + expect(((gotStub.args[0] as any)[1] as Options).json).to.equal(req.body); }); it('should provide data from the plugin when its route is called', async function () { diff --git a/test/storage/bulk-storage.spec.ts b/test/storage/bulk-storage.spec.ts index 7420f5d9..0d6501bd 100644 --- a/test/storage/bulk-storage.spec.ts +++ b/test/storage/bulk-storage.spec.ts @@ -81,14 +81,14 @@ describe('Bulk Storage', function () { }); it('should throw an error if the bulk for deletion cannot be read', async function () { - sandbox.stub(BulkStorage.prototype, 'read').callsFake(async () => Promise.resolve(undefined)); + sandbox.stub(BulkStorage.prototype, 'read').callsFake(() => undefined); const bulkStorage = new BulkStorage(database); return expect(bulkStorage.delete('123')).to.be.rejected; }); it('should delete a bulk', async function () { - const readStub = sandbox.stub(BulkStorage.prototype, 'read').callsFake(async () => Promise.resolve(bulk)); + const readStub = sandbox.stub(BulkStorage.prototype, 'read').callsFake(() => bulk); let caught: any; sandbox.stub(NodeCache.prototype, 'del').callsFake(() => caught = 123); // force call