From 3710da737909f7877a00ad431ad942084ecb808c Mon Sep 17 00:00:00 2001 From: Corey Date: Tue, 20 Jun 2023 06:07:10 -0400 Subject: [PATCH] refactor: Replace deprecated `substr` with `substring` (#8644) --- postinstall.js | 2 +- src/Adapters/Files/GridFSBucketAdapter.js | 71 ++++++------------- .../Postgres/PostgresStorageAdapter.js | 14 ++-- src/Config.js | 2 +- 4 files changed, 32 insertions(+), 57 deletions(-) diff --git a/postinstall.js b/postinstall.js index fef1fb31ff..fe1fc96bae 100644 --- a/postinstall.js +++ b/postinstall.js @@ -1,6 +1,6 @@ const pkg = require('./package.json'); -const version = parseFloat(process.version.substr(1)); +const version = parseFloat(process.version.substring(1)); const minimum = parseFloat(pkg.engines.node.match(/\d+/g).join('.')); module.exports = function () { diff --git a/src/Adapters/Files/GridFSBucketAdapter.js b/src/Adapters/Files/GridFSBucketAdapter.js index 451165789d..76a8f25d1b 100644 --- a/src/Adapters/Files/GridFSBucketAdapter.js +++ b/src/Adapters/Files/GridFSBucketAdapter.js @@ -28,7 +28,11 @@ export class GridFSBucketAdapter extends FilesAdapter { this._algorithm = 'aes-256-gcm'; this._encryptionKey = encryptionKey !== undefined - ? crypto.createHash('sha256').update(String(encryptionKey)).digest('base64').substr(0, 32) + ? crypto + .createHash('sha256') + .update(String(encryptionKey)) + .digest('base64') + .substring(0, 32) : null; const defaultMongoOptions = { useNewUrlParser: true, @@ -138,8 +142,8 @@ export class GridFSBucketAdapter extends FilesAdapter { } async rotateEncryptionKey(options = {}) { - var fileNames = []; - var oldKeyFileAdapter = {}; + let fileNames = []; + let oldKeyFileAdapter = {}; const bucket = await this._getBucket(); if (options.oldKey !== undefined) { oldKeyFileAdapter = new GridFSBucketAdapter( @@ -158,51 +162,22 @@ export class GridFSBucketAdapter extends FilesAdapter { fileNames.push(file.filename); }); } - return new Promise(resolve => { - var fileNamesNotRotated = fileNames; - var fileNamesRotated = []; - var fileNameTotal = fileNames.length; - var fileNameIndex = 0; - fileNames.forEach(fileName => { - oldKeyFileAdapter - .getFileData(fileName) - .then(plainTextData => { - //Overwrite file with data encrypted with new key - this.createFile(fileName, plainTextData) - .then(() => { - fileNamesRotated.push(fileName); - fileNamesNotRotated = fileNamesNotRotated.filter(function (value) { - return value !== fileName; - }); - fileNameIndex += 1; - if (fileNameIndex == fileNameTotal) { - resolve({ - rotated: fileNamesRotated, - notRotated: fileNamesNotRotated, - }); - } - }) - .catch(() => { - fileNameIndex += 1; - if (fileNameIndex == fileNameTotal) { - resolve({ - rotated: fileNamesRotated, - notRotated: fileNamesNotRotated, - }); - } - }); - }) - .catch(() => { - fileNameIndex += 1; - if (fileNameIndex == fileNameTotal) { - resolve({ - rotated: fileNamesRotated, - notRotated: fileNamesNotRotated, - }); - } - }); - }); - }); + let fileNamesNotRotated = fileNames; + const fileNamesRotated = []; + for (const fileName of fileNames) { + try { + const plainTextData = await oldKeyFileAdapter.getFileData(fileName); + // Overwrite file with data encrypted with new key + await this.createFile(fileName, plainTextData); + fileNamesRotated.push(fileName); + fileNamesNotRotated = fileNamesNotRotated.filter(function (value) { + return value !== fileName; + }); + } catch (err) { + continue; + } + } + return { rotated: fileNamesRotated, notRotated: fileNamesNotRotated }; } getFileLocation(config, filename) { diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 3e8e867799..3ad59ec77f 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -231,7 +231,7 @@ const transformAggregateField = fieldName => { if (fieldName === '$_updated_at') { return 'updatedAt'; } - return fieldName.substr(1); + return fieldName.substring(1); }; const validateKeys = object => { @@ -1921,14 +1921,14 @@ export class PostgresStorageAdapter implements StorageAdapter { }; } if (object[fieldName] && schema.fields[fieldName].type === 'Polygon') { - let coords = object[fieldName]; - coords = coords.substr(2, coords.length - 4).split('),('); - coords = coords.map(point => { + let coords = new String(object[fieldName]); + coords = coords.substring(2, coords.length - 2).split('),('); + const updatedCoords = coords.map(point => { return [parseFloat(point.split(',')[1]), parseFloat(point.split(',')[0])]; }); object[fieldName] = { __type: 'Polygon', - coordinates: coords, + coordinates: updatedCoords, }; } if (object[fieldName] && schema.fields[fieldName].type === 'File') { @@ -2634,7 +2634,7 @@ function literalizeRegexPart(s: string) { const result1: any = s.match(matcher1); if (result1 && result1.length > 1 && result1.index > -1) { // process regex that has a beginning and an end specified for the literal text - const prefix = s.substr(0, result1.index); + const prefix = s.substring(0, result1.index); const remaining = result1[1]; return literalizeRegexPart(prefix) + createLiteralRegex(remaining); @@ -2644,7 +2644,7 @@ function literalizeRegexPart(s: string) { const matcher2 = /\\Q((?!\\E).*)$/; const result2: any = s.match(matcher2); if (result2 && result2.length > 1 && result2.index > -1) { - const prefix = s.substr(0, result2.index); + const prefix = s.substring(0, result2.index); const remaining = result2[1]; return literalizeRegexPart(prefix) + createLiteralRegex(remaining); diff --git a/src/Config.js b/src/Config.js index 5e3a49bb35..8fe10a9a1c 100644 --- a/src/Config.js +++ b/src/Config.js @@ -25,7 +25,7 @@ function removeTrailingSlash(str) { return str; } if (str.endsWith('/')) { - str = str.substr(0, str.length - 1); + str = str.substring(0, str.length - 1); } return str; }