fix tests

This commit is contained in:
Metazla
2024-04-02 19:57:03 +02:00
parent 60898adca5
commit 38b78f4f92
32 changed files with 923 additions and 2396 deletions
+5 -5
View File
@@ -1,6 +1,6 @@
import {HashComputer} from "./HashComputer.js";
import {CID_ALGORITHM_NAMES, ComputeInterface, MultiHashData} from "./MultiHashData.js";
import {HashIndexManager} from "./HashIndexManager.js";
import {HashComputer} from "./HashComputer";
import {CID_ALGORITHM_NAMES, ComputeInterface, MultiHashData} from "./MultiHashData";
import {HashIndexManager} from "./HashIndexManager";
import {stat} from "fs/promises";
import path from "path";
@@ -8,8 +8,8 @@ export class ComputeHashIndexCache implements ComputeInterface {
hashIndexManager: HashIndexManager;
hashComputer: HashComputer;
constructor(indexFilePath: string, private targetHash: CID_ALGORITHM_NAMES[] = [CID_ALGORITHM_NAMES.sha1, CID_ALGORITHM_NAMES.sha256]) {
this.hashComputer = new HashComputer(this.targetHash);
constructor(indexFilePath: string,private targetHash: CID_ALGORITHM_NAMES[] = [CID_ALGORITHM_NAMES.sha1, CID_ALGORITHM_NAMES.sha256],workerPath?:string) {
this.hashComputer = new HashComputer(this.targetHash,workerPath);
this.hashIndexManager = new HashIndexManager(indexFilePath);
}
+5 -4
View File
@@ -1,10 +1,11 @@
import { FileIDComputer } from "./fileID/FileIDComputer.js";
import {CID_ALGORITHM_NAMES, ComputeInterface, MultiHashData} from "./MultiHashData.js";
import { FileIDComputer } from "./fileID/FileIDComputer";
import {CID_ALGORITHM_NAMES, ComputeInterface, MultiHashData} from "./MultiHashData";
export class HashComputer implements ComputeInterface{
private fileIDComputer = new FileIDComputer();
private fileIDComputer:FileIDComputer;
constructor(private targetHash: CID_ALGORITHM_NAMES[]) {
constructor(private targetHash: CID_ALGORITHM_NAMES[],workerPath?:string) {
this.fileIDComputer = new FileIDComputer(workerPath);
}
async computeMissingHash(filePath: string, metadata: MultiHashData): Promise<void> {
+2 -2
View File
@@ -2,10 +2,10 @@ import {createReadStream, promises as fs} from 'fs';
import {parse} from 'csv-parse';
import {parse as parseSync} from 'csv-parse/sync';
import {stringify} from 'csv-stringify/sync';
import {existsAsync} from "./ExistsAsync.js";
import {existsAsync} from "./ExistsAsync";
import path from "path";
import {clearInterval} from "node:timers";
import {CID_ALGORITHM_NAMES} from "./MultiHashData.js";
import {CID_ALGORITHM_NAMES} from "./MultiHashData";
interface IndexLine extends Partial<Record<CID_ALGORITHM_NAMES, string>> {
path: string;
+11 -3
View File
@@ -1,13 +1,21 @@
import {Piscina} from "piscina";
import {CID_ALGORITHM_NAMES} from "../MultiHashData.js";
import {CID_ALGORITHM_NAMES} from "../MultiHashData";
// Construct the URL for the current module
let distFolder = import.meta.dirname;
distFolder = distFolder.replace('src', 'dist');
distFolder = distFolder + "/worker.js";
const workerUrl = new URL(distFolder).href;
console.log("Worker URL: ", workerUrl);
export class FileIDComputer {
private piscina: Piscina;
constructor() {
constructor(workerPath?:string) {
this.piscina = new Piscina({
maxThreads: 4,
filename: new URL('./ShaComputeWorker.js', import.meta.url).href
//filename: new URL('./ShaComputeWorker.ts', import.meta.url).href
filename: workerPath || process.env.WORKER_URL || workerUrl
});
}
+1 -1
View File
@@ -2,7 +2,7 @@ import { createReadStream } from "fs";
import {BinaryLike, createHash} from 'crypto';
import { CID } from 'multiformats/cid';
import { create } from "multiformats/hashes/digest";
import {CID_ALGORITHM, CID_ALGORITHM_CODES, CID_ALGORITHM_NAMES} from "../MultiHashData.js";
import {CID_ALGORITHM, CID_ALGORITHM_CODES, CID_ALGORITHM_NAMES} from "../MultiHashData";
export interface SimpleHash {
update(data: BinaryLike),
+8 -8
View File
@@ -1,11 +1,11 @@
// Import and export directly to simplify access to library components
export * from './ComputeHashIndexCache';
export * from './ExistsAsync'; // Assuming 'async' is a function; adjust as necessary
export * from './fileID/FileIDComputer';
export * from './fileID/ShaComputeWorker'; // Assuming this exports a class or main function; adjust as needed
export * from './HashComputer';
export * from './HashIndexManager';
export * from './ComputeHashIndexCache.js';
export * from './ExistsAsync.js'; // Assuming 'async' is a function; adjust as necessary
export * from './fileID/FileIDComputer.js';
export * from './fileID/ShaComputeWorker.js'; // Assuming this exports a class or main function; adjust as needed
export * from './HashComputer.js';
export * from './HashIndexManager.js';
// For MultiHashData.ts, assuming interfaces and enums need to be exported; you might need to adjust based on actual content
export * from './MultiHashData';
export * from './MultiHashData.js';
// Assuming Properties.ts exports one or more interfaces; adjust as needed
export * from './Properties';
export * from './Properties.js';
-3
View File
@@ -1,3 +0,0 @@
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
+27
View File
@@ -0,0 +1,27 @@
import {HashComputer} from "../lib";
import {CID_ALGORITHM_NAMES} from "../lib";
import {describe, it, expect} from 'vitest';
import * as process from "process";
process.env.WORKER_URL = "./dist/worker.js";
describe('add', () => {
it('meta data compute', async () => {
let hashComputer = new HashComputer([
CID_ALGORITHM_NAMES.sha1,
CID_ALGORITHM_NAMES.sha256,
CID_ALGORITHM_NAMES.md5,
CID_ALGORITHM_NAMES.sha3_256
]);
let metadata = {};
await hashComputer.computeMissingHash('./src/tests/test.txt', metadata);
console.log(metadata);
expect(metadata).toEqual({
cid_sha1: 'baeircff3eqvxxaqqot2lt242stkdtk3gq4c2vra',
'cid_sha2-256': 'baejbeibku6ua2l6r5olnggx4pto2sii2s5jeeawgh4cbkhlbmp52gudbua',
cid_md5: 'bahkqdvibcatpcfl7ine2n625eb5ml5mzkfaa',
'cid_sha3-256': 'baelbmidner7x3dd7t2kldh6wiq4w6lsye7qrwo6uoqot2t52zkhqk56fnq'
});
});
});
+10
View File
@@ -0,0 +1,10 @@
this is a file that is just some data to be hashed
loreum ipsum dolor sit amet
consectetur adipiscing elit
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
ut enim ad minim veniam
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat
duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
excepteur sint occaecat cupidatat non proident
sunt in culpa qui officia deserunt mollit anim id est laborum