Skip to content

Commit

Permalink
refactor: Lint project and remove deprecated code (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 authored Feb 15, 2024
1 parent 67b42f1 commit 903a72d
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 387 deletions.
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
commit-message:
prefix: "refactor"
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "daily"
commit-message:
prefix: "refactor"
11 changes: 8 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ on:
- '**'
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '14', '16', '18' ]
name: Node ${{ matrix.node }}
runs-on: ubuntu-latest
timeout-minutes: 30
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
COVERAGE_OPTION: ./node_modules/.bin/nyc
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: 'npm'
- run: npm ci
- name: Install dependencies
run: npm ci
- run: npm run coverage
- run: bash <(curl -s https://codecov.io/bash)
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
2 changes: 1 addition & 1 deletion .github/workflows/release-automated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
persist-credentials: false
- name: Setup Node
Expand Down
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ node_modules

# Optional REPL history
.node_repl_history

# visual studio code
.vscode

# cache folder
.cache
.eslintcache

# Mac DS_Store files
.DS_Store
122 changes: 51 additions & 71 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
// Stores files in local file system
// Requires write access to the server's file system.

var fs = require('fs');
var path = require('path');
var pathSep = require('path').sep;
const fs = require('fs');
const path = require('path');
const pathSep = require('path').sep;
const crypto = require("crypto");
const algorithm = 'aes-256-gcm';

function FileSystemAdapter(options) {
options = options || {};
this._encryptionKey = null;

if (options.encryptionKey !== undefined){
this._encryptionKey = crypto.createHash('sha256').update(String(options.encryptionKey)).digest('base64').substr(0, 32);
if (options.encryptionKey !== undefined) {
this._encryptionKey = crypto.createHash('sha256').update(String(options.encryptionKey)).digest('base64').substring(0, 32);
}
let filesSubDirectory = options.filesSubDirectory || '';
const filesSubDirectory = options.filesSubDirectory || '';
this._filesDir = filesSubDirectory;
this._mkdir(this._getApplicationDir());
if (!this._applicationDirExist()) {
Expand All @@ -26,11 +26,11 @@ function FileSystemAdapter(options) {
}

FileSystemAdapter.prototype.createFile = function(filename, data) {
let filepath = this._getLocalFilePath(filename);
const filepath = this._getLocalFilePath(filename);
const stream = fs.createWriteStream(filepath);
return new Promise((resolve, reject) => {
try{
if(this._encryptionKey !== null){
try {
if (this._encryptionKey !== null) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(
algorithm,
Expand All @@ -48,21 +48,21 @@ FileSystemAdapter.prototype.createFile = function(filename, data) {
stream.on('finish', function() {
resolve(data);
});
}else{
} else {
stream.write(data);
stream.end();
stream.on('finish', function() {
resolve(data);
});
}
}catch(err){
}
} catch(err) {
return reject(err);
}
});
}

FileSystemAdapter.prototype.deleteFile = function(filename) {
let filepath = this._getLocalFilePath(filename);
const filepath = this._getLocalFilePath(filename);
const chunks = [];
const stream = fs.createReadStream(filepath);
return new Promise((resolve, reject) => {
Expand All @@ -86,7 +86,7 @@ FileSystemAdapter.prototype.deleteFile = function(filename) {
}

FileSystemAdapter.prototype.getFileData = function(filename) {
let filepath = this._getLocalFilePath(filename);
const filepath = this._getLocalFilePath(filename);
const stream = fs.createReadStream(filepath);
stream.read();
return new Promise((resolve, reject) => {
Expand All @@ -96,18 +96,18 @@ FileSystemAdapter.prototype.getFileData = function(filename) {
});
stream.on('end', () => {
const data = Buffer.concat(chunks);
if(this._encryptionKey !== null){
if (this._encryptionKey !== null) {
const authTagLocation = data.length - 16;
const ivLocation = data.length - 32;
const authTag = data.slice(authTagLocation);
const iv = data.slice(ivLocation,authTagLocation);
const encrypted = data.slice(0,ivLocation);
try{
try {
const decipher = crypto.createDecipheriv(algorithm, this._encryptionKey, iv);
decipher.setAuthTag(authTag);
const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
const decrypted = Buffer.concat([ decipher.update(encrypted), decipher.final() ]);
return resolve(decrypted);
}catch(err){
} catch(err) {
return reject(err);
}
}
Expand All @@ -119,55 +119,36 @@ FileSystemAdapter.prototype.getFileData = function(filename) {
});
}

FileSystemAdapter.prototype.rotateEncryptionKey = function(options = {}) {
FileSystemAdapter.prototype.rotateEncryptionKey = async function(options = {}) {
const applicationDir = this._getApplicationDir();
var fileNames = [];
var oldKeyFileAdapter = {};
let fileNames = [];
let oldKeyFileAdapter = {};
if (options.oldKey !== undefined) {
oldKeyFileAdapter = new FileSystemAdapter({filesSubDirectory: this._filesDir, encryptionKey: options.oldKey});
}else{
oldKeyFileAdapter = new FileSystemAdapter({filesSubDirectory: this._filesDir});
oldKeyFileAdapter = new FileSystemAdapter({ filesSubDirectory: this._filesDir, encryptionKey: options.oldKey });
} else {
oldKeyFileAdapter = new FileSystemAdapter({ filesSubDirectory: this._filesDir });
}
if (options.fileNames !== undefined){
if (options.fileNames !== undefined) {
fileNames = options.fileNames;
}else{
fileNames = fs.readdirSync(applicationDir);
fileNames = fileNames.filter(fileName => fileName.indexOf('.') !== 0);
} else {
fileNames = fs.readdirSync(applicationDir);
fileNames = fileNames.filter(fileName => fileName.indexOf('.') !== 0);
}
return new Promise((resolve, _reject) => {
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 };
}

FileSystemAdapter.prototype.getFileLocation = function(config, filename) {
Expand All @@ -177,20 +158,20 @@ FileSystemAdapter.prototype.getFileLocation = function(config, filename) {
/*
Helpers
--------------- */
FileSystemAdapter.prototype._getApplicationDir = function() {
FileSystemAdapter.prototype._getApplicationDir = function() {
if (this._filesDir) {
return path.join('files', this._filesDir);
} else {
return 'files';
}
}
}

FileSystemAdapter.prototype._applicationDirExist = function() {
return fs.existsSync(this._getApplicationDir());
}

FileSystemAdapter.prototype._getLocalFilePath = function(filename) {
let applicationDir = this._getApplicationDir();
const applicationDir = this._getApplicationDir();
if (!fs.existsSync(applicationDir)) {
this._mkdir(applicationDir);
}
Expand All @@ -199,20 +180,19 @@ FileSystemAdapter.prototype._getLocalFilePath = function(filename) {

FileSystemAdapter.prototype._mkdir = function(dirPath) {
// snippet found on -> https://gist.github.com/danherbert-epam/3960169
let dirs = dirPath.split(pathSep);
var root = "";
const dirs = dirPath.split(pathSep);
let root = "";

while (dirs.length > 0) {
var dir = dirs.shift();
const dir = dirs.shift();
if (dir === "") { // If directory starts with a /, the first path will be an empty string.
root = pathSep;
}
if (!fs.existsSync(path.join(root, dir))) {
try {
fs.mkdirSync(path.join(root, dir));
}
catch (e) {
if ( e.code == 'EACCES' ) {
} catch (err) {
if (err.code == 'EACCES') {
throw new Error("PERMISSION ERROR: In order to use the FileSystemAdapter, write access to the server's file system is required.");
}
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"url": "https://github.com/parse-community/parse-server-fs-adapter"
},
"scripts": {
"test": "jasmine",
"coverage": "nyc jasmine"
"coverage": "nyc jasmine",
"test": "jasmine"
},
"keywords": [
"parse-server",
Expand Down
4 changes: 2 additions & 2 deletions release.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function config() {
// Get branch
const branch = ref.split('/').pop();
console.log(`Running on branch: ${branch}`);

// Set changelog file
//const changelogFile = `./changelogs/CHANGELOG_${branch}.md`;
const changelogFile = `./CHANGELOG.md`;
Expand Down Expand Up @@ -108,7 +108,7 @@ async function readFile(filePath) {

function getReleaseComment() {
const url = repositoryUrl + '/releases/tag/${nextRelease.gitTag}';
let comment = '🎉 This change has been released in version [${nextRelease.version}](' + url + ')';
const comment = '🎉 This change has been released in version [${nextRelease.version}](' + url + ')';
return comment;
}

Expand Down
Loading

0 comments on commit 903a72d

Please sign in to comment.