fix: ensure we return an empty string when key file is missing

This commit is contained in:
Alexis Tyler
2021-04-22 13:14:53 +09:30
parent eb99cd2686
commit 3f2707cff1
6 changed files with 34 additions and 25 deletions

View File

@@ -3,11 +3,10 @@
* Written by: Alexis Tyler
*/
import fs from 'fs';
import btoa from 'btoa';
import { varState } from '../../states';
import { CoreContext, CoreResult } from '../../types';
import { ensurePermission } from '../../utils';
import { getKeyFile } from '../../utils/misc/get-key-file';
/**
* Get server's license info
@@ -28,8 +27,7 @@ export const getLicense = async function (context: CoreContext): Promise<CoreRes
// Get license data
const type = varState.data.regTy;
const state = varState.data.regState;
const file = await fs.promises.readFile(varState.data.regFile, 'binary');
const parsedFile = btoa(file).trim().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
const file = await getKeyFile();
return {
get text() {
@@ -39,7 +37,7 @@ export const getLicense = async function (context: CoreContext): Promise<CoreRes
return {
type,
state,
file: parsedFile
file
};
}
};

View File

@@ -0,0 +1,15 @@
import btoa from 'btoa';
import { promises } from 'fs';
import { varState } from '../../states';
// Get key file
export const getKeyFile = async function () {
// Bail if key is missing
if (varState.data.regFile.trim() === '') {
return '';
}
return promises.readFile(varState.data.regFile, 'binary').then(keyFile => {
return btoa(keyFile).trim().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}).catch(() => '');
};

View File

@@ -3,13 +3,12 @@
* Written by: Alexis Tyler
*/
import btoa from 'btoa';
import { promises } from 'fs';
import { dirname } from 'path';
import chokidar from 'chokidar';
import { coreLogger } from '../log';
import { varState } from '../states';
import { pubsub } from '../pubsub';
import { getKeyFile } from '../utils/misc/get-key-file';
export const keyFile = () => {
const watchers: chokidar.FSWatcher[] = [];
@@ -39,14 +38,7 @@ export const keyFile = () => {
}
// Get key file
const file = await promises.readFile(fullPath, 'binary').catch(() => '');
// If the file throws an error then bail
if (!file) {
return;
}
// Convert binary to base64 with no "+", "/" or "="
const parsedFile = btoa(file).trim().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
const keyFile = await getKeyFile();
// Publish event
// This will end up going to the graphql endpoint
@@ -57,7 +49,7 @@ export const keyFile = () => {
state: varState.data.regState,
keyFile: {
location: fullPath,
contents: parsedFile
contents: keyFile
}
}
}).catch(error => {

View File

@@ -3,10 +3,8 @@
* Written by: Alexis Tyler
*/
import btoa from 'btoa';
import { promises } from 'fs';
import { varState } from '../../../core/states';
import { ensurePermission } from '../../../core/utils';
import { ensurePermission, getKeyFile } from '../../../core/utils';
import { Context } from '../../schema/utils';
export default async (_: unknown, __: unknown, context: Context) => {
@@ -16,17 +14,13 @@ export default async (_: unknown, __: unknown, context: Context) => {
possession: 'any'
});
// Get key file
const file = await promises.readFile(varState.data.regFile, 'binary');
const parsedFile = btoa(file).trim().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
return {
guid: varState.data.regGuid,
type: varState.data.regTy,
state: varState.data.regState,
keyFile: {
location: varState.data.regFile,
contents: parsedFile
contents: await getKeyFile()
}
};
};

9
package-lock.json generated
View File

@@ -1136,6 +1136,15 @@
"@types/node": "*"
}
},
"@types/btoa": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/@types/btoa/-/btoa-1.2.3.tgz",
"integrity": "sha512-ANNCZICS/ofxhzUl8V1DniBJs+sFQ+Yg5am1ZwVEf/sxoKY/J2+h5Fuw3xUErlZ7eJLdgzukBjZwnsV6+/2Rmg==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/cli-table": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/@types/cli-table/-/cli-table-0.3.0.tgz",

View File

@@ -135,6 +135,7 @@
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@types/btoa": "^1.2.3",
"@types/cli-table": "^0.3.0",
"@types/dockerode": "^3.2.2",
"@types/lodash.get": "^4.4.6",
@@ -268,4 +269,4 @@
"uuid-apikey",
"xhr2"
]
}
}