Skip to content

Commit

Permalink
fix: minor cleanup for domain entries
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Aug 29, 2023
1 parent ca80f42 commit f71bc04
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 67 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
11 changes: 0 additions & 11 deletions src/main/domains/train-config/constants.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/domains/train-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrainConfigParseOutput> {
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) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/domains/train-config/type.ts
Original file line number Diff line number Diff line change
@@ -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
};
18 changes: 14 additions & 4 deletions src/main/domains/train-result/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrainResultOutput> {
let files : TarFile[] = [];

Expand All @@ -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;
});

Expand Down
5 changes: 0 additions & 5 deletions src/main/domains/train-result/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ export type TrainResultLoaderContext = {
paillierPrivateKey?: PrivateKey
};

export type ReadTrainResultConfigContext = {
files: TarFile[],
privateKey: string
};

export type TrainResultOutput = {
config: TrainConfig,
files: TarFile[]
Expand Down
4 changes: 2 additions & 2 deletions src/main/utils/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
25 changes: 3 additions & 22 deletions test/unit/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
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";

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');
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"src/**/*.ts",
"src/**/*.js",
"src/**/*.vue",
"test/**/*.spec.ts"
"test/**/*.ts"
]
}
1 change: 0 additions & 1 deletion vitron.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

module.exports = {
port: 9000,
framework: {
name: 'nuxt',
version: '3.0.0'
Expand Down

0 comments on commit f71bc04

Please sign in to comment.