Skip to content

Commit

Permalink
refactor: Replace deprecated substr with substring (#8644)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 authored Jun 20, 2023
1 parent 9674d4a commit 3710da7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 57 deletions.
2 changes: 1 addition & 1 deletion postinstall.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
71 changes: 23 additions & 48 deletions src/Adapters/Files/GridFSBucketAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ const transformAggregateField = fieldName => {
if (fieldName === '$_updated_at') {
return 'updatedAt';
}
return fieldName.substr(1);
return fieldName.substring(1);
};

const validateKeys = object => {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 3710da7

Please sign in to comment.