Skip to content

Commit 903a72d

Browse files
authored
refactor: Lint project and remove deprecated code (#43)
1 parent 67b42f1 commit 903a72d

10 files changed

+390
-387
lines changed

.github/dependabot.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"
7+
commit-message:
8+
prefix: "refactor"
9+
- package-ecosystem: "npm"
10+
directory: "/"
11+
schedule:
12+
interval: "daily"
13+
commit-message:
14+
prefix: "refactor"

.github/workflows/ci.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ on:
88
- '**'
99
jobs:
1010
test:
11-
runs-on: ubuntu-latest
1211
strategy:
1312
matrix:
1413
node: [ '14', '16', '18' ]
14+
name: Node ${{ matrix.node }}
15+
runs-on: ubuntu-latest
1516
timeout-minutes: 30
1617
env:
1718
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
1819
COVERAGE_OPTION: ./node_modules/.bin/nyc
1920
steps:
20-
- uses: actions/checkout@v2
21+
- uses: actions/checkout@v3
2122
- name: Use Node.js
2223
uses: actions/setup-node@v3
2324
with:
2425
node-version: ${{ matrix.node }}
2526
cache: 'npm'
26-
- run: npm ci
27+
- name: Install dependencies
28+
run: npm ci
2729
- run: npm run coverage
2830
- run: bash <(curl -s https://codecov.io/bash)
31+
concurrency:
32+
group: ${{ github.workflow }}-${{ github.ref }}
33+
cancel-in-progress: true

.github/workflows/release-automated.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99
- name: Checkout repository
10-
uses: actions/checkout@v2
10+
uses: actions/checkout@v3
1111
with:
1212
persist-credentials: false
1313
- name: Setup Node

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ node_modules
3434

3535
# Optional REPL history
3636
.node_repl_history
37+
38+
# visual studio code
39+
.vscode
40+
41+
# cache folder
42+
.cache
43+
.eslintcache
44+
45+
# Mac DS_Store files
46+
.DS_Store

index.js

Lines changed: 51 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
// Stores files in local file system
55
// Requires write access to the server's file system.
66

7-
var fs = require('fs');
8-
var path = require('path');
9-
var pathSep = require('path').sep;
7+
const fs = require('fs');
8+
const path = require('path');
9+
const pathSep = require('path').sep;
1010
const crypto = require("crypto");
1111
const algorithm = 'aes-256-gcm';
1212

1313
function FileSystemAdapter(options) {
1414
options = options || {};
1515
this._encryptionKey = null;
1616

17-
if (options.encryptionKey !== undefined){
18-
this._encryptionKey = crypto.createHash('sha256').update(String(options.encryptionKey)).digest('base64').substr(0, 32);
17+
if (options.encryptionKey !== undefined) {
18+
this._encryptionKey = crypto.createHash('sha256').update(String(options.encryptionKey)).digest('base64').substring(0, 32);
1919
}
20-
let filesSubDirectory = options.filesSubDirectory || '';
20+
const filesSubDirectory = options.filesSubDirectory || '';
2121
this._filesDir = filesSubDirectory;
2222
this._mkdir(this._getApplicationDir());
2323
if (!this._applicationDirExist()) {
@@ -26,11 +26,11 @@ function FileSystemAdapter(options) {
2626
}
2727

2828
FileSystemAdapter.prototype.createFile = function(filename, data) {
29-
let filepath = this._getLocalFilePath(filename);
29+
const filepath = this._getLocalFilePath(filename);
3030
const stream = fs.createWriteStream(filepath);
3131
return new Promise((resolve, reject) => {
32-
try{
33-
if(this._encryptionKey !== null){
32+
try {
33+
if (this._encryptionKey !== null) {
3434
const iv = crypto.randomBytes(16);
3535
const cipher = crypto.createCipheriv(
3636
algorithm,
@@ -48,21 +48,21 @@ FileSystemAdapter.prototype.createFile = function(filename, data) {
4848
stream.on('finish', function() {
4949
resolve(data);
5050
});
51-
}else{
51+
} else {
5252
stream.write(data);
5353
stream.end();
5454
stream.on('finish', function() {
5555
resolve(data);
5656
});
57-
}
58-
}catch(err){
57+
}
58+
} catch(err) {
5959
return reject(err);
6060
}
6161
});
6262
}
6363

6464
FileSystemAdapter.prototype.deleteFile = function(filename) {
65-
let filepath = this._getLocalFilePath(filename);
65+
const filepath = this._getLocalFilePath(filename);
6666
const chunks = [];
6767
const stream = fs.createReadStream(filepath);
6868
return new Promise((resolve, reject) => {
@@ -86,7 +86,7 @@ FileSystemAdapter.prototype.deleteFile = function(filename) {
8686
}
8787

8888
FileSystemAdapter.prototype.getFileData = function(filename) {
89-
let filepath = this._getLocalFilePath(filename);
89+
const filepath = this._getLocalFilePath(filename);
9090
const stream = fs.createReadStream(filepath);
9191
stream.read();
9292
return new Promise((resolve, reject) => {
@@ -96,18 +96,18 @@ FileSystemAdapter.prototype.getFileData = function(filename) {
9696
});
9797
stream.on('end', () => {
9898
const data = Buffer.concat(chunks);
99-
if(this._encryptionKey !== null){
99+
if (this._encryptionKey !== null) {
100100
const authTagLocation = data.length - 16;
101101
const ivLocation = data.length - 32;
102102
const authTag = data.slice(authTagLocation);
103103
const iv = data.slice(ivLocation,authTagLocation);
104104
const encrypted = data.slice(0,ivLocation);
105-
try{
105+
try {
106106
const decipher = crypto.createDecipheriv(algorithm, this._encryptionKey, iv);
107107
decipher.setAuthTag(authTag);
108-
const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
108+
const decrypted = Buffer.concat([ decipher.update(encrypted), decipher.final() ]);
109109
return resolve(decrypted);
110-
}catch(err){
110+
} catch(err) {
111111
return reject(err);
112112
}
113113
}
@@ -119,55 +119,36 @@ FileSystemAdapter.prototype.getFileData = function(filename) {
119119
});
120120
}
121121

122-
FileSystemAdapter.prototype.rotateEncryptionKey = function(options = {}) {
122+
FileSystemAdapter.prototype.rotateEncryptionKey = async function(options = {}) {
123123
const applicationDir = this._getApplicationDir();
124-
var fileNames = [];
125-
var oldKeyFileAdapter = {};
124+
let fileNames = [];
125+
let oldKeyFileAdapter = {};
126126
if (options.oldKey !== undefined) {
127-
oldKeyFileAdapter = new FileSystemAdapter({filesSubDirectory: this._filesDir, encryptionKey: options.oldKey});
128-
}else{
129-
oldKeyFileAdapter = new FileSystemAdapter({filesSubDirectory: this._filesDir});
127+
oldKeyFileAdapter = new FileSystemAdapter({ filesSubDirectory: this._filesDir, encryptionKey: options.oldKey });
128+
} else {
129+
oldKeyFileAdapter = new FileSystemAdapter({ filesSubDirectory: this._filesDir });
130130
}
131-
if (options.fileNames !== undefined){
131+
if (options.fileNames !== undefined) {
132132
fileNames = options.fileNames;
133-
}else{
134-
fileNames = fs.readdirSync(applicationDir);
135-
fileNames = fileNames.filter(fileName => fileName.indexOf('.') !== 0);
133+
} else {
134+
fileNames = fs.readdirSync(applicationDir);
135+
fileNames = fileNames.filter(fileName => fileName.indexOf('.') !== 0);
136136
}
137-
return new Promise((resolve, _reject) => {
138-
var fileNamesNotRotated = fileNames;
139-
var fileNamesRotated = [];
140-
var fileNameTotal = fileNames.length;
141-
var fileNameIndex = 0;
142-
fileNames.forEach(fileName => {
143-
oldKeyFileAdapter
144-
.getFileData(fileName)
145-
.then(plainTextData => {
146-
//Overwrite file with data encrypted with new key
147-
this.createFile(fileName, plainTextData)
148-
.then(() => {
149-
fileNamesRotated.push(fileName);
150-
fileNamesNotRotated = fileNamesNotRotated.filter(function(value){ return value !== fileName;})
151-
fileNameIndex += 1;
152-
if (fileNameIndex == fileNameTotal){
153-
resolve({rotated: fileNamesRotated, notRotated: fileNamesNotRotated});
154-
}
155-
})
156-
.catch(() => {
157-
fileNameIndex += 1;
158-
if (fileNameIndex == fileNameTotal){
159-
resolve({rotated: fileNamesRotated, notRotated: fileNamesNotRotated});
160-
}
161-
})
162-
})
163-
.catch(() => {
164-
fileNameIndex += 1;
165-
if (fileNameIndex == fileNameTotal){
166-
resolve({rotated: fileNamesRotated, notRotated: fileNamesNotRotated});
167-
}
168-
});
169-
});
170-
});
137+
138+
let fileNamesNotRotated = fileNames;
139+
const fileNamesRotated = [];
140+
for (const fileName of fileNames) {
141+
try {
142+
const plainTextData = await oldKeyFileAdapter.getFileData(fileName)
143+
// Overwrite file with data encrypted with new key
144+
await this.createFile(fileName, plainTextData)
145+
fileNamesRotated.push(fileName);
146+
fileNamesNotRotated = fileNamesNotRotated.filter(function(value) { return value !== fileName; });
147+
} catch(err) {
148+
continue;
149+
}
150+
}
151+
return { rotated: fileNamesRotated, notRotated: fileNamesNotRotated };
171152
}
172153

173154
FileSystemAdapter.prototype.getFileLocation = function(config, filename) {
@@ -177,20 +158,20 @@ FileSystemAdapter.prototype.getFileLocation = function(config, filename) {
177158
/*
178159
Helpers
179160
--------------- */
180-
FileSystemAdapter.prototype._getApplicationDir = function() {
161+
FileSystemAdapter.prototype._getApplicationDir = function() {
181162
if (this._filesDir) {
182163
return path.join('files', this._filesDir);
183164
} else {
184165
return 'files';
185166
}
186-
}
167+
}
187168

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

192173
FileSystemAdapter.prototype._getLocalFilePath = function(filename) {
193-
let applicationDir = this._getApplicationDir();
174+
const applicationDir = this._getApplicationDir();
194175
if (!fs.existsSync(applicationDir)) {
195176
this._mkdir(applicationDir);
196177
}
@@ -199,20 +180,19 @@ FileSystemAdapter.prototype._getLocalFilePath = function(filename) {
199180

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

205186
while (dirs.length > 0) {
206-
var dir = dirs.shift();
187+
const dir = dirs.shift();
207188
if (dir === "") { // If directory starts with a /, the first path will be an empty string.
208189
root = pathSep;
209190
}
210191
if (!fs.existsSync(path.join(root, dir))) {
211192
try {
212193
fs.mkdirSync(path.join(root, dir));
213-
}
214-
catch (e) {
215-
if ( e.code == 'EACCES' ) {
194+
} catch (err) {
195+
if (err.code == 'EACCES') {
216196
throw new Error("PERMISSION ERROR: In order to use the FileSystemAdapter, write access to the server's file system is required.");
217197
}
218198
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"url": "https://github.com/parse-community/parse-server-fs-adapter"
99
},
1010
"scripts": {
11-
"test": "jasmine",
12-
"coverage": "nyc jasmine"
11+
"coverage": "nyc jasmine",
12+
"test": "jasmine"
1313
},
1414
"keywords": [
1515
"parse-server",

release.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function config() {
2626
// Get branch
2727
const branch = ref.split('/').pop();
2828
console.log(`Running on branch: ${branch}`);
29-
29+
3030
// Set changelog file
3131
//const changelogFile = `./changelogs/CHANGELOG_${branch}.md`;
3232
const changelogFile = `./CHANGELOG.md`;
@@ -108,7 +108,7 @@ async function readFile(filePath) {
108108

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

0 commit comments

Comments
 (0)