Browse Source

fix: use direct promisify calls instead of variables

- drop synchronous calls on various places

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5031/head
mertmit 2 years ago
parent
commit
0cb8be57ae
  1. 3
      packages/nocodb/src/lib/Noco.ts
  2. 8
      packages/nocodb/src/lib/db/sql-client/lib/SqlClientFactory.ts
  3. 11
      packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts
  4. 19
      packages/nocodb/src/lib/meta/NcMetaMgr.ts
  5. 5
      packages/nocodb/src/lib/meta/api/projectApis.ts
  6. 3
      packages/nocodb/src/lib/plugins/backblaze/Backblaze.ts
  7. 3
      packages/nocodb/src/lib/plugins/gcs/Gcs.ts
  8. 3
      packages/nocodb/src/lib/plugins/linode/LinodeObjectStorage.ts
  9. 3
      packages/nocodb/src/lib/plugins/mino/Minio.ts
  10. 3
      packages/nocodb/src/lib/plugins/ovhCloud/OvhCloud.ts
  11. 3
      packages/nocodb/src/lib/plugins/s3/S3.ts
  12. 3
      packages/nocodb/src/lib/plugins/scaleway/ScalewayObjectStorage.ts
  13. 3
      packages/nocodb/src/lib/plugins/spaces/Spaces.ts
  14. 3
      packages/nocodb/src/lib/plugins/upcloud/UpoCloud.ts
  15. 3
      packages/nocodb/src/lib/plugins/vultr/Vultr.ts
  16. 14
      packages/nocodb/src/lib/utils/NcConfigFactory.ts
  17. 8
      packages/nocodb/src/lib/utils/common/NcConnectionMgr.ts
  18. 3
      packages/nocodb/src/lib/v1-legacy/NcProjectBuilder.ts
  19. 10
      packages/nocodb/src/lib/v1-legacy/plugins/adapters/storage/Local.ts
  20. 14
      packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts

3
packages/nocodb/src/lib/Noco.ts

@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/ban-types */
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import * as Sentry from '@sentry/node';
import bodyParser from 'body-parser';
@ -459,7 +460,7 @@ export default class Noco {
connectionConfig.meta.dbAlias,
'migrations'
);
if (!fs.existsSync(migrationFolder)) {
if (!await promisify(fs.exists)(migrationFolder)) {
await migrator.init({
folder: this.config?.toolDir,
env: this.env,

8
packages/nocodb/src/lib/db/sql-client/lib/SqlClientFactory.ts

@ -12,8 +12,6 @@ import VitessClient from './mysql/VitessClient';
import fs from 'fs';
import { promisify } from 'util';
const readFileAsync = promisify(fs.readFile);
export class SqlClientFactory {
static create(connectionConfig) {
connectionConfig.meta = connectionConfig.meta || {};
@ -51,17 +49,17 @@ export default class {
typeof connectionConfig.connection.ssl === 'object'
) {
if (connectionConfig.connection.ssl.caFilePath) {
connectionConfig.connection.ssl.ca = await readFileAsync(
connectionConfig.connection.ssl.ca = await promisify(fs.readFile)(
connectionConfig.connection.ssl.caFilePath
).toString();
}
if (connectionConfig.connection.ssl.keyFilePath) {
connectionConfig.connection.ssl.key = await readFileAsync(
connectionConfig.connection.ssl.key = await promisify(fs.readFile)(
connectionConfig.connection.ssl.keyFilePath
).toString();
}
if (connectionConfig.connection.ssl.certFilePath) {
connectionConfig.connection.ssl.cert = await readFileAsync(
connectionConfig.connection.ssl.cert = await promisify(fs.readFile)(
connectionConfig.connection.ssl.certFilePath
).toString();
}

11
packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts

@ -20,9 +20,6 @@ import Result from '../util/Result';
const randomID = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 20);
const log = new Debug('SqlMgr');
const writeFileAsync = promisify(fs.writeFile);
const readFileAsync = promisify(fs.readFile);
const ToolOps = {
DB_TABLE_LIST: 'tableList',
DB_VIEW_LIST: 'viewList',
@ -443,7 +440,7 @@ export default class SqlMgr {
console.log(args);
try {
fs.unlinkSync(path.join(this.currentProjectFolder, 'config.xc.json'));
await promisify(fs.unlink)(path.join(this.currentProjectFolder, 'config.xc.json'));
args.folder = args.folder || args.project.folder;
args.folder = path.dirname(args.folder);
@ -1102,13 +1099,13 @@ export default class SqlMgr {
public async projectChangeEnv(args) {
try {
const xcConfig = JSON.parse(
await readFileAsync(
await promisify(fs.readFile)(
path.join(this.currentProjectFolder, 'config.xc.json'),
'utf8'
)
);
xcConfig.workingEnv = args.env;
await writeFileAsync(
await promisify(fs.writeFile)(
path.join(this.currentProjectFolder, 'config.xc.json'),
JSON.stringify(xcConfig, null, 2)
);
@ -1361,7 +1358,7 @@ export default class SqlMgr {
break;
case ToolOps.WRITE_FILE:
console.log('Within WRITE_FILE handler', args);
result = await writeFileAsync(args.args.path, args.args.data);
result = await promisify(fs.writeFile)(args.args.path, args.args.data);
break;
case ToolOps.REST_API_CALL:

19
packages/nocodb/src/lib/meta/NcMetaMgr.ts

@ -47,9 +47,6 @@ const XC_PLUGIN_DET = 'XC_PLUGIN_DET';
const NOCO_RELEASE = 'NOCO_RELEASE';
const writeFileAsync = promisify(fs.writeFile);
const readFileAsync = promisify(fs.readFile);
export default class NcMetaMgr {
public projectConfigs = {};
public readonly pluginMgr: NcPluginMgr;
@ -440,9 +437,9 @@ export default class NcMetaMgr {
await this.xcMetaTablesReset(args);
for (const tn of META_TABLES[this.config.projectType.toLowerCase()]) {
if (fs.existsSync(path.join(metaFolder, `${tn}.json`))) {
if (await promisify(fs.exists)(path.join(metaFolder, `${tn}.json`))) {
const data = JSON.parse(
await readFileAsync(path.join(metaFolder, `${tn}.json`), 'utf8')
await promisify(fs.readFile)(path.join(metaFolder, `${tn}.json`), 'utf8')
);
for (const row of data) {
delete row.id;
@ -491,14 +488,14 @@ export default class NcMetaMgr {
},
});
// delete temporary upload file
fs.unlinkSync(file.path);
await promisify(fs.unlink)(file.path);
let projectId = this.getProjectId(args);
if (!projectConfigPath) {
throw new Error('Missing project config file');
}
const projectDetailsJSON: any = await readFileAsync(
const projectDetailsJSON: any = await promisify(fs.readFile)(
path.join(this.config.toolDir, 'uploads', projectConfigPath),
'utf8'
);
@ -617,11 +614,11 @@ export default class NcMetaMgr {
},
});
// delete temporary upload file
fs.unlinkSync(file.path);
await promisify(fs.unlink)(file.path);
if (projectConfigPath) {
// read project config and extract project id
let projectConfig: any = await readFileAsync(
let projectConfig: any = await promisify(fs.readFile)(
path.join(this.config?.toolDir, projectConfigPath),
'utf8'
);
@ -712,7 +709,7 @@ export default class NcMetaMgr {
for (const tn of META_TABLES[this.config.projectType.toLowerCase()]) {
// const metaData = await client.knex(tn).select();
const metaData = await this.xcMeta.metaList(projectId, dbAlias, tn);
await writeFileAsync(
await promisify(fs.writeFile)(
path.join(metaFolder, `${tn}.json`),
JSON.stringify(metaData, null, 2)
);
@ -723,7 +720,7 @@ export default class NcMetaMgr {
true
);
projectMetaData.key = this.config?.auth?.jwt?.secret;
await writeFileAsync(
await promisify(fs.writeFile)(
path.join(metaFolder, `nc_project.json`),
JSON.stringify(projectMetaData, null, 2)
);

5
packages/nocodb/src/lib/meta/api/projectApis.ts

@ -27,6 +27,7 @@ import { extractAndGenerateManyToManyRelations } from './metaDiffApis';
import { metaApiMetrics } from '../helpers/apiMetrics';
import { extractPropsAndSanitize } from '../helpers/extractProps';
import NcConfigFactory from '../../utils/NcConfigFactory';
import { promisify } from 'util';
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 4);
@ -121,8 +122,8 @@ async function projectCreate(req: Request<any, any>, res) {
'1234567890abcdefghijklmnopqrstuvwxyz',
14
);
if (!fs.existsSync(`${toolDir}/nc_minimal_dbs`)) {
fs.mkdirSync(`${toolDir}/nc_minimal_dbs`);
if (!await promisify(fs.exists)(`${toolDir}/nc_minimal_dbs`)) {
await promisify(fs.mkdir)(`${toolDir}/nc_minimal_dbs`);
}
const dbId = nanoidv2();
const projectTitle = DOMPurify.sanitize(projectBody.title);

3
packages/nocodb/src/lib/plugins/backblaze/Backblaze.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import AWS from 'aws-sdk';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -132,7 +133,7 @@ export default class Backblaze implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/gcs/Gcs.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import { Storage, StorageOptions } from '@google-cloud/storage';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -96,7 +97,7 @@ export default class Gcs implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/linode/LinodeObjectStorage.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import AWS from 'aws-sdk';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -122,7 +123,7 @@ export default class LinodeObjectStorage implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/mino/Minio.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import { Client as MinioClient } from 'minio';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -84,7 +85,7 @@ export default class Minio implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/ovhCloud/OvhCloud.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import AWS from 'aws-sdk';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -122,7 +123,7 @@ export default class OvhCloud implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/s3/S3.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import AWS from 'aws-sdk';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -125,7 +126,7 @@ export default class S3 implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/scaleway/ScalewayObjectStorage.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import AWS from 'aws-sdk';
import request from 'request';
@ -40,7 +41,7 @@ export default class ScalewayObjectStorage implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/spaces/Spaces.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import AWS from 'aws-sdk';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -130,7 +131,7 @@ export default class Spaces implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/upcloud/UpoCloud.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import AWS from 'aws-sdk';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -120,7 +121,7 @@ export default class UpoCloud implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

3
packages/nocodb/src/lib/plugins/vultr/Vultr.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import { promisify } from 'util';
import AWS from 'aws-sdk';
import { IStorageAdapterV2, XcFile } from 'nc-plugin';
import request from 'request';
@ -122,7 +123,7 @@ export default class Vultr implements IStorageAdapterV2 {
originalname: 'temp.txt',
size: '',
});
fs.unlinkSync(tempFile);
await promisify(fs.unlink)(tempFile);
return true;
} catch (e) {
throw e;

14
packages/nocodb/src/lib/utils/NcConfigFactory.ts

@ -19,8 +19,6 @@ const {
animals,
} = require('unique-names-generator');
const readFileAsync = promisify(fs.readFile);
const driverClientMapping = {
mysql: 'mysql2',
mariadb: 'mysql2',
@ -124,11 +122,11 @@ export default class NcConfigFactory implements NcConfig {
} else if (process.env.NC_DB_JSON_FILE) {
const filePath = process.env.NC_DB_JSON_FILE;
if (!fs.existsSync(filePath)) {
if (!await promisify(fs.exists)(filePath)) {
throw new Error(`NC_DB_JSON_FILE not found: ${filePath}`);
}
const fileContent = await readFileAsync(filePath, { encoding: 'utf8' });
const fileContent = await promisify(fs.readFile)(filePath, { encoding: 'utf8' });
ncConfig.meta.db = JSON.parse(fileContent);
}
@ -381,12 +379,12 @@ export default class NcConfigFactory implements NcConfig {
typeof dbConfig?.connection?.ssl === 'object'
) {
if (dbConfig.connection.ssl.caFilePath && !dbConfig.connection.ssl.ca) {
dbConfig.connection.ssl.ca = await readFileAsync(
dbConfig.connection.ssl.ca = await promisify(fs.readFile)(
dbConfig.connection.ssl.caFilePath
).toString();
}
if (dbConfig.connection.ssl.keyFilePath && !dbConfig.connection.ssl.key) {
dbConfig.connection.ssl.key = await readFileAsync(
dbConfig.connection.ssl.key = await promisify(fs.readFile)(
dbConfig.connection.ssl.keyFilePath
).toString();
}
@ -394,7 +392,7 @@ export default class NcConfigFactory implements NcConfig {
dbConfig.connection.ssl.certFilePath &&
!dbConfig.connection.ssl.cert
) {
dbConfig.connection.ssl.cert = await readFileAsync(
dbConfig.connection.ssl.cert = await promisify(fs.readFile)(
dbConfig.connection.ssl.certFilePath
).toString();
}
@ -642,7 +640,7 @@ export default class NcConfigFactory implements NcConfig {
public static async jdbcToXcUrl() {
if (process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE) {
const database_url = await readFileAsync(
const database_url = await promisify(fs.readFile)(
process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE,
'utf-8'
);

8
packages/nocodb/src/lib/utils/common/NcConnectionMgr.ts

@ -8,8 +8,6 @@ import { Knex } from 'knex';
import NcMetaIO from '../../meta/NcMetaIO';
import { defaultConnectionConfig } from '../NcConfigFactory';
const readFileAsync = promisify(fs.readFile);
export default class NcConnectionMgr {
private static connectionRefs: {
[projectId: string]: {
@ -76,7 +74,7 @@ export default class NcConnectionMgr {
connectionConfig.connection.ssl.caFilePath &&
!connectionConfig.connection.ssl.ca
) {
connectionConfig.connection.ssl.ca = await readFileAsync(
connectionConfig.connection.ssl.ca = await promisify(fs.readFile)(
connectionConfig.connection.ssl.caFilePath
).toString();
}
@ -84,7 +82,7 @@ export default class NcConnectionMgr {
connectionConfig.connection.ssl.keyFilePath &&
!connectionConfig.connection.ssl.key
) {
connectionConfig.connection.ssl.key = await readFileAsync(
connectionConfig.connection.ssl.key = await promisify(fs.readFile)(
connectionConfig.connection.ssl.keyFilePath
).toString();
}
@ -92,7 +90,7 @@ export default class NcConnectionMgr {
connectionConfig.connection.ssl.certFilePath &&
!connectionConfig.connection.ssl.cert
) {
connectionConfig.connection.ssl.cert = await readFileAsync(
connectionConfig.connection.ssl.cert = await promisify(fs.readFile)(
connectionConfig.connection.ssl.certFilePath
).toString();
}

3
packages/nocodb/src/lib/v1-legacy/NcProjectBuilder.ts

@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import axios from 'axios';
import { Router } from 'express';
@ -718,7 +719,7 @@ export default class NcProjectBuilder {
connectionConfig.meta.dbAlias,
'migrations'
);
if (!fs.existsSync(migrationFolder)) {
if (!await promisify(fs.exists)(migrationFolder)) {
await migrator.init({
folder: this.app.getToolDir(),
env: this.appConfig.workingEnv,

10
packages/nocodb/src/lib/v1-legacy/plugins/adapters/storage/Local.ts

@ -9,10 +9,6 @@ import NcConfigFactory from '../../../../utils/NcConfigFactory';
import axios from 'axios';
const writeFileAsync = promisify(fs.writeFile)
const readFileAsync = promisify(fs.readFile)
const unlinkAsync = promisify(fs.unlink)
export default class Local implements IStorageAdapterV2 {
constructor() {}
@ -20,9 +16,9 @@ export default class Local implements IStorageAdapterV2 {
const destPath = path.join(NcConfigFactory.getToolDir(), ...key.split('/'));
try {
await mkdirp(path.dirname(destPath));
const data = await readFileAsync(file.path)
await writeFileAsync(destPath, data);
await unlinkAsync(file.path);
const data = await promisify(fs.readFile)(file.path)
await promisify(fs.writeFile)(destPath, data);
await promisify(fs.unlink)(file.path);
// await fs.promises.rename(file.path, destPath);
} catch (e) {
throw e;

14
packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts

@ -44,10 +44,6 @@ 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<Noco> {
public readonly type = 'rest';
private controllers: {
@ -2608,7 +2604,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
'swagger'
);
await mkdirp(swaggerFilePath);
await writeFileAsync(
await promisify(fs.writeFile)(
path.join(swaggerFilePath, 'swagger.json'),
JSON.stringify(swaggerDoc)
);
@ -2653,7 +2649,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
scheme,
scheme === 'http' ? 'https' : 'http',
];
await globAsync(
await promisify(glob)(
path.join(
this.app.getToolDir(),
'nc',
@ -2664,7 +2660,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
)
)
.forEach(async (jsonFile) => {
const swaggerJson = JSON.parse(await readFileAsync(jsonFile, 'utf8'));
const swaggerJson = JSON.parse(await promisify(fs.readFile)(jsonFile, 'utf8'));
swaggerBaseDocument.tags.push(...swaggerJson.tags);
Object.assign(swaggerBaseDocument.paths, swaggerJson.paths);
Object.assign(
@ -2733,7 +2729,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
'swagger'
);
const swaggerJson = JSON.parse(
await readFileAsync(path.join(swaggerFilePath, 'swagger.json'), 'utf8')
await promisify(fs.readFile)(path.join(swaggerFilePath, 'swagger.json'), 'utf8')
);
/* remove tags, paths and keys */
@ -2799,7 +2795,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
}
}
await writeFileAsync(
await promisify(fs.writeFile)(
path.join(swaggerFilePath, 'swagger.json'),
JSON.stringify(swaggerJson)
);

Loading…
Cancel
Save