-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_based_usage.ts
More file actions
64 lines (53 loc) · 2.51 KB
/
file_based_usage.ts
File metadata and controls
64 lines (53 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// REPLACE ALL REFERENCES TO "_generic_hasher" WITH THE DESIRED ALGORITHM IN THE ALGORITHMS DIRECTORY.
import { existsSync, rmSync } from 'node:fs';
import { loadConfig, postprocessFile, preprocessFile, processFile } from '../src/index';
import { makeHasher, ALGORITHM_ID } from './example_algorithm/_generic_hasher';
import { dirname, join, parse, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const CONFIG_PATH = join(__dirname, 'example_algorithm', 'config.toml');
const INPUT_PATH = join(__dirname, 'example_algorithm', 'input_10.csv');
const OUTPUT_PATH = join(__dirname, 'output', 'output_10.csv');
const VALIDATION_ERRORS_PATH = join(__dirname, 'output', 'validation_errors.csv');
if (existsSync(resolve(__dirname, "output"))) rmSync(join(__dirname, "output"), { recursive: true, force: true});
// 1. load configuration from file
const configLoadResult = loadConfig({
configPath: CONFIG_PATH,
algorithmId: ALGORITHM_ID,
validateConfig: true,
embeddedSalt: { source: "STRING", value: "SOME_SALT_VALUE" }
});
if (!configLoadResult.success)
throw new Error(`ERROR: Unable to load configuration file >> ${configLoadResult.error}`);
// 2. validate the input file against all configured validation rules.
const preprocessResult = await preprocessFile({
config: configLoadResult.config,
inputFilePath: INPUT_PATH,
errorFileOutputPath: VALIDATION_ERRORS_PATH,
});
if (!preprocessResult.isValid)
throw new Error('ERROR: Validation errors found in input file, review error file output.');
// 3. process the input file according to the configuration.
const processFileResult = await processFile({
config: configLoadResult.config,
inputFilePath: INPUT_PATH,
outputPath: OUTPUT_PATH,
hasherFactory: makeHasher,
});
if (!processFileResult.outputFilePath)
throw new Error(`ERROR: Unable to process input file`);
// 4. print the result, save the result, etc.
console.dir(processFileResult, { depth: 3 });
// 5. postprocess the output file according to the configuration.
const postprocessResult = await postprocessFile({
config: configLoadResult.config,
inputPath: processFileResult.outputFilePath,
outputPath: join(parse(processFileResult.outputFilePath).dir, "ENCRYPTED.gpg"),
options: {
signer: "" // OPTIONAL: Signing key
}
});
if (!postprocessResult.success) {
const errors = postprocessResult.steps.filter(step => !step.success);
throw new Error(`ERROR: Postprocessing failed, one or more steps failed >> ${JSON.stringify(errors)}`);
}