From 1f2672d3178b56365ee951979cafa838578556f8 Mon Sep 17 00:00:00 2001 From: mertmit Date: Sat, 4 Feb 2023 13:57:01 +0300 Subject: [PATCH] fix: asynchronous RestApiBuilder Signed-off-by: mertmit --- .../src/lib/v1-legacy/rest/RestApiBuilder.ts | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts b/packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts index 8ebd95f37f..6415e67d21 100644 --- a/packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts +++ b/packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts @@ -1,5 +1,6 @@ import fs from 'fs'; import path from 'path'; +import { promisify } from 'util'; import autoBind from 'auto-bind'; import debug from 'debug'; @@ -43,6 +44,10 @@ import { MetaTable } from '../../utils/globals'; const log = debug('nc:api:rest'); const NC_CUSTOM_ROUTE_KEY = '__xc_custom'; +const globAsync = promisify(glob); +const writeFileAsync = promisify(fs.writeFile); +const readFileAsync = promisify(fs.readFile); + export class RestApiBuilder extends BaseApiBuilder { public readonly type = 'rest'; private controllers: { @@ -371,7 +376,7 @@ export class RestApiBuilder extends BaseApiBuilder { }); if (tables?.length) { - this.swaggerUpdate({ + await this.swaggerUpdate({ addApis: swaggerDoc, }); } else { @@ -995,7 +1000,7 @@ export class RestApiBuilder extends BaseApiBuilder { } if (args.tableNames && args.tableNames.length) { - this.swaggerUpdate({ + await this.swaggerUpdate({ addApis: swaggerDoc, }); } else { @@ -1099,7 +1104,7 @@ export class RestApiBuilder extends BaseApiBuilder { await this.xcTablesRowDelete(tn, extras); delete this.routers[tn]; - this.swaggerUpdate({ + await this.swaggerUpdate({ deleteTags: [tn], }); } catch (e) { @@ -1122,7 +1127,7 @@ export class RestApiBuilder extends BaseApiBuilder { delete this.routers[viewName]; - this.swaggerUpdate({ + await this.swaggerUpdate({ deleteTags: [viewName], }); } catch (e) { @@ -2367,7 +2372,7 @@ export class RestApiBuilder extends BaseApiBuilder { this.log(`onFunctionCreate : Update function and procedure routes`); this.procedureCtrl.functionsSet(functions); this.procedureCtrl.mapRoutes(this.routers.___procedure, this.customRoutes); - this.swaggerUpdate({ + await this.swaggerUpdate({ addApis: { paths: this.procedureCtrl.getSwaggerObj().paths }, }); } @@ -2387,7 +2392,7 @@ export class RestApiBuilder extends BaseApiBuilder { this.routers.___procedure.stack.length ); this.procedureCtrl.mapRoutes(this.routers.___procedure, this.customRoutes); - this.swaggerUpdate({ + await this.swaggerUpdate({ addApis: this.procedureCtrl.getSwaggerObj(), deleteTags: ['Procedures', 'Functions'], }); @@ -2422,7 +2427,7 @@ export class RestApiBuilder extends BaseApiBuilder { this.log(`onProcedureCreate : Update function and procedure routes`); this.procedureCtrl.proceduresSet(procedures); this.procedureCtrl.mapRoutes(this.routers.___procedure, this.customRoutes); - this.swaggerUpdate({ + await this.swaggerUpdate({ addApis: { paths: this.procedureCtrl.getSwaggerObj().paths }, }); } @@ -2442,7 +2447,7 @@ export class RestApiBuilder extends BaseApiBuilder { this.routers.___procedure.stack.length ); this.procedureCtrl.mapRoutes(this.routers.___procedure, this.customRoutes); - this.swaggerUpdate({ + await this.swaggerUpdate({ addApis: this.procedureCtrl.getSwaggerObj(), deleteTags: ['Procedures', 'Functions'], }); @@ -2602,8 +2607,8 @@ export class RestApiBuilder extends BaseApiBuilder { this.getDbAlias(), 'swagger' ); - mkdirp.sync(swaggerFilePath); - fs.writeFileSync( + await mkdirp(swaggerFilePath); + await writeFileAsync( path.join(swaggerFilePath, 'swagger.json'), JSON.stringify(swaggerDoc) ); @@ -2648,8 +2653,7 @@ export class RestApiBuilder extends BaseApiBuilder { scheme, scheme === 'http' ? 'https' : 'http', ]; - glob - .sync( + await globAsync( path.join( this.app.getToolDir(), 'nc', @@ -2659,8 +2663,8 @@ export class RestApiBuilder extends BaseApiBuilder { 'swagger.json' ) ) - .forEach((jsonFile) => { - const swaggerJson = JSON.parse(fs.readFileSync(jsonFile, 'utf8')); + .forEach(async (jsonFile) => { + const swaggerJson = JSON.parse(await readFileAsync(jsonFile, 'utf8')); swaggerBaseDocument.tags.push(...swaggerJson.tags); Object.assign(swaggerBaseDocument.paths, swaggerJson.paths); Object.assign( @@ -2709,7 +2713,7 @@ export class RestApiBuilder extends BaseApiBuilder { } // @ts-ignore - private swaggerUpdate(args: { + private async swaggerUpdate(args: { deleteApis?: any; addApis?: any; deleteTags?: string[]; @@ -2729,7 +2733,7 @@ export class RestApiBuilder extends BaseApiBuilder { 'swagger' ); const swaggerJson = JSON.parse( - fs.readFileSync(path.join(swaggerFilePath, 'swagger.json'), 'utf8') + await readFileAsync(path.join(swaggerFilePath, 'swagger.json'), 'utf8') ); /* remove tags, paths and keys */ @@ -2795,7 +2799,7 @@ export class RestApiBuilder extends BaseApiBuilder { } } - fs.writeFileSync( + await writeFileAsync( path.join(swaggerFilePath, 'swagger.json'), JSON.stringify(swaggerJson) );