From f71bc04645bd3c6b77c0a3c7ee67a8fa42dabefd Mon Sep 17 00:00:00 2001 From: tada5hi Date: Tue, 29 Aug 2023 10:57:57 +0200 Subject: [PATCH] fix: minor cleanup for domain entries --- package-lock.json | 9 ++++--- package.json | 2 +- src/main/domains/train-config/constants.ts | 11 -------- src/main/domains/train-config/index.ts | 4 +-- .../train-config/{read.ts => parse.ts} | 23 +++++++---------- src/main/domains/train-config/type.ts | 18 +++++++++++++ src/main/domains/train-result/read.ts | 18 ++++++++++--- src/main/domains/train-result/type.ts | 5 ---- src/main/utils/context-menu.ts | 4 +-- test/unit/config.spec.ts | 25 +++---------------- tsconfig.eslint.json | 2 +- vitron.config.js | 1 - 12 files changed, 55 insertions(+), 67 deletions(-) delete mode 100644 src/main/domains/train-config/constants.ts rename src/main/domains/train-config/{read.ts => parse.ts} (56%) create mode 100644 src/main/domains/train-config/type.ts diff --git a/package-lock.json b/package-lock.json index e12c5ea..394eb14 100644 --- a/package-lock.json +++ b/package-lock.json @@ -62,7 +62,7 @@ "resguard": "^1.4.3", "tar": "^5.0.11", "ts-jest": "^27.1.4", - "vitron": "^2.0.0-alpha.0", + "vitron": "^2.0.0-alpha.2", "vue": "^3.3.4" }, "engines": { @@ -21268,13 +21268,14 @@ } }, "node_modules/vitron": { - "version": "2.0.0-alpha.0", - "resolved": "https://registry.npmjs.org/vitron/-/vitron-2.0.0-alpha.0.tgz", - "integrity": "sha512-XsO528dKR1GgB00WLFaRc13VcbpAEz392JZVMzZv8UDfLnuv1P6TdHlk1Xc/Uv/7Vo90Q5LnIJpGM8n7kWG2fQ==", + "version": "2.0.0-alpha.2", + "resolved": "https://registry.npmjs.org/vitron/-/vitron-2.0.0-alpha.2.tgz", + "integrity": "sha512-Wq0KHk7SkBlCUXub7NW8yiiZkSZVuo6MynDOShkPF61aw3oMvXO3fyzyC42bBoEHy3pUlaV805KImx/1yiWuQw==", "dev": true, "dependencies": { "continu": "^1.3.2", "cross-spawn": "^7.0.3", + "get-port-please": "^3.0.1", "locter": "^1.2.1", "picocolors": "^1.0.0", "semver": "^7.5.4", diff --git a/package.json b/package.json index e81f008..21b8e00 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,7 @@ "resguard": "^1.4.3", "tar": "^5.0.11", "ts-jest": "^27.1.4", - "vitron": "^2.0.0-alpha.0", + "vitron": "^2.0.0-alpha.2", "vue": "^3.3.4" } } diff --git a/src/main/domains/train-config/constants.ts b/src/main/domains/train-config/constants.ts deleted file mode 100644 index 9ad9144..0000000 --- a/src/main/domains/train-config/constants.ts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright (c) 2023. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -export enum TrainConfigPath { - RESULT_CONFIG_FILE_NAME = 'train_config.json', - RESULT_PATH = 'pht_results', -} diff --git a/src/main/domains/train-config/index.ts b/src/main/domains/train-config/index.ts index 97d8943..1357b33 100644 --- a/src/main/domains/train-config/index.ts +++ b/src/main/domains/train-config/index.ts @@ -5,5 +5,5 @@ * view the LICENSE file that was distributed with this source code. */ -export * from './constants'; -export * from './read'; +export * from './parse'; +export * from './type'; diff --git a/src/main/domains/train-config/read.ts b/src/main/domains/train-config/parse.ts similarity index 56% rename from src/main/domains/train-config/read.ts rename to src/main/domains/train-config/parse.ts index 297be59..164d049 100644 --- a/src/main/domains/train-config/read.ts +++ b/src/main/domains/train-config/parse.ts @@ -5,26 +5,21 @@ * view the LICENSE file that was distributed with this source code. */ -import crypto from 'crypto'; +import crypto from 'node:crypto'; import type { TrainConfig } from '@personalhealthtrain/core'; -import type { ReadTrainResultConfigContext } from '../train-result/type'; -import { TrainConfigPath } from './constants'; - -export async function parseTrainConfig(context: ReadTrainResultConfigContext) : Promise<{ - config: TrainConfig, - key: Buffer -}> { - const configIndex = context.files.findIndex((file) => file.path === TrainConfigPath.RESULT_CONFIG_FILE_NAME); - if (configIndex === -1) { - throw new Error(`The ${TrainConfigPath.RESULT_CONFIG_FILE_NAME} does not exist in the compressed tar file.`); - } +import type { TrainConfigParseContext, TrainConfigParseOutput } from './type'; +export async function parseTrainConfig(context: TrainConfigParseContext) : Promise { let config : TrainConfig; try { - config = JSON.parse(context.files[configIndex].content.toString('utf-8')); + if (Buffer.isBuffer(context.content)) { + config = JSON.parse(context.content.toString('utf-8')); + } else { + config = JSON.parse(context.content); + } } catch (e) { - throw new Error(`The ${TrainConfigPath.RESULT_CONFIG_FILE_NAME} could not be parsed.`); + throw new Error('The train config could not be parsed.'); } if (!config.creator.encrypted_key) { diff --git a/src/main/domains/train-config/type.ts b/src/main/domains/train-config/type.ts new file mode 100644 index 0000000..ee18f21 --- /dev/null +++ b/src/main/domains/train-config/type.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import type { TrainConfig } from '@personalhealthtrain/core'; + +export type TrainConfigParseContext = { + content: Buffer | string, + privateKey: string +}; + +export type TrainConfigParseOutput = { + config: TrainConfig, + key: Buffer +}; diff --git a/src/main/domains/train-result/read.ts b/src/main/domains/train-result/read.ts index 0481c31..e0cebec 100644 --- a/src/main/domains/train-result/read.ts +++ b/src/main/domains/train-result/read.ts @@ -5,11 +5,16 @@ * view the LICENSE file that was distributed with this source code. */ +import { TrainContainerPath } from '@personalhealthtrain/core'; +import path from 'node:path'; import { decompressTarFile, decryptPaillierNumberInTarFiles, decryptSymmetric } from '../../core'; -import { TrainConfigPath, parseTrainConfig } from '../train-config'; +import { parseTrainConfig } from '../train-config'; import type { TarFile, TrainResultLoaderContext, TrainResultOutput } from './type'; import { TrainResultSourceType } from './constants'; +const CONFIG_FILE = path.basename(TrainContainerPath.CONFIG); +const RESULT_DIRECTORY = path.basename(TrainContainerPath.RESULTS); + export async function readTrainResult(context: TrainResultLoaderContext) : Promise { let files : TarFile[] = []; @@ -25,15 +30,20 @@ export async function readTrainResult(context: TrainResultLoaderContext) : Promi throw new Error('The result is empty.'); } + const configIndex = files.findIndex((file) => file.path === CONFIG_FILE); + if (configIndex === -1) { + throw new Error(`The ${CONFIG_FILE} does not exist in the compressed tar file.`); + } + const { config, key } = await parseTrainConfig({ - files, + content: files[configIndex].content, privateKey: context.rsaPrivateKey, }); let resultFiles : TarFile[] = files - .filter((file) => file.path.startsWith(TrainConfigPath.RESULT_PATH)) + .filter((file) => file.path.startsWith(RESULT_DIRECTORY)) .map((file) => { - file.path = file.path.replace(`${TrainConfigPath.RESULT_PATH}/`, ''); + file.path = file.path.replace(`${RESULT_DIRECTORY}/`, ''); return file; }); diff --git a/src/main/domains/train-result/type.ts b/src/main/domains/train-result/type.ts index ad507f9..469d16a 100644 --- a/src/main/domains/train-result/type.ts +++ b/src/main/domains/train-result/type.ts @@ -25,11 +25,6 @@ export type TrainResultLoaderContext = { paillierPrivateKey?: PrivateKey }; -export type ReadTrainResultConfigContext = { - files: TarFile[], - privateKey: string -}; - export type TrainResultOutput = { config: TrainConfig, files: TarFile[] diff --git a/src/main/utils/context-menu.ts b/src/main/utils/context-menu.ts index 678f7c8..37c81bd 100644 --- a/src/main/utils/context-menu.ts +++ b/src/main/utils/context-menu.ts @@ -8,12 +8,12 @@ import { Menu, Tray, app, nativeImage, } from 'electron'; -import * as path from 'path'; +import path from 'node:path'; let tray; export function buildContextMenu() { - const imagePath : string = path.join(__dirname, '..', 'assets', 'icons', '512x512.png'); + const imagePath : string = path.join(__dirname, '..', '..', 'assets', 'icons', '512x512.png'); const image = nativeImage.createFromPath(imagePath); tray = new Tray(image); tray.setTitle('PHT: Desktop-App'); diff --git a/test/unit/config.spec.ts b/test/unit/config.spec.ts index 59b0ce7..24f03c8 100644 --- a/test/unit/config.spec.ts +++ b/test/unit/config.spec.ts @@ -1,7 +1,6 @@ -import {decompressTarFile, decryptRSAPrivateKey} from "../../src/main/core"; -import * as path from "path"; -import {parseTrainConfig} from "../../src/main/domains/train-config"; -import fs from "fs"; +import { decryptRSAPrivateKey} from "../../src/main/core"; +import path from "node:path"; +import fs from "node:fs"; import {readTrainResult} from "../../src/main/domains/train-result"; import {TrainResultSourceType} from "../../src/main/domains/train-result"; @@ -9,24 +8,6 @@ describe('src/renderer/domains/train-config*.ts', () => { const resultFilePath = path.join(__dirname, '..', 'data', 'result.tar'); const privateKeyFilePath = path.join(__dirname, '..', 'data', 'private.pem'); - it('should read train-config of tar file', async () => { - const files = await decompressTarFile(resultFilePath); - - expect(files).toBeDefined(); - expect(files.length).toEqual(2); - - let privateKey = await fs.promises.readFile(privateKeyFilePath, { encoding: 'utf-8' }); - let privateKeyDecrypted = decryptRSAPrivateKey(privateKey, 'leuko'); - - const {config, key} = await parseTrainConfig({ - files, - privateKey: privateKeyDecrypted - }); - - expect(config).toBeDefined(); - expect(key).toBeDefined(); - }); - it('should load train result', async () => { let privateKey = await fs.promises.readFile(privateKeyFilePath, { encoding: 'utf-8' }); let privateKeyDecrypted = decryptRSAPrivateKey(privateKey, 'leuko'); diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json index 7db275a..c24d686 100644 --- a/tsconfig.eslint.json +++ b/tsconfig.eslint.json @@ -4,6 +4,6 @@ "src/**/*.ts", "src/**/*.js", "src/**/*.vue", - "test/**/*.spec.ts" + "test/**/*.ts" ] } diff --git a/vitron.config.js b/vitron.config.js index a3df27f..5af6e96 100644 --- a/vitron.config.js +++ b/vitron.config.js @@ -6,7 +6,6 @@ */ module.exports = { - port: 9000, framework: { name: 'nuxt', version: '3.0.0'