add browser dist

This commit is contained in:
Metazla
2024-04-08 16:13:08 +02:00
parent 14ba90f521
commit 6123e10c07
67 changed files with 538 additions and 254 deletions
+58
View File
@@ -0,0 +1,58 @@
import {CID_ALGORITHM_CODES, CID_ALGORITHM_NAMES} from "../hash-compute/MultiHashData";
import {Readable} from "stream";
import {create} from "multiformats/hashes/digest";
import {CID} from 'multiformats/cid';
import {createHasher, codeTable} from "./CreateHasher";
import {SimpleHash} from "./SimpleHash";
/**
* Compute the CIDs of a file using specific algorithms
* @param stream The Readable stream of the file
* @param algorithms Array of algorithms ('sha256', 'sha1', etc.)
* @returns Array of CIDs (in the order of the algorithms)
*/
export async function computeCIDs({stream, algorithms}: {
stream: Readable | ReadableStream;
algorithms: CID_ALGORITHM_NAMES[]
}): Promise<string[]> {
const hashers = hasherDefiner(algorithms);
// Check if the environment is Node.js or Browser and handle the stream accordingly
if (stream instanceof Readable) {
// Node.js environment
for await (const chunk of stream) {
hashers.forEach(item => item.hasher.update(chunk as Buffer));
}
} else {
// Browser environment
const reader = stream.getReader();
while (true) {
const {done, value} = await reader.read();
if (done) break;
// Assuming the hasher can handle Uint8Array directly
hashers.forEach(item => item.hasher.update(Buffer.from(value)));
}
}
return cidFinalize(hashers);
}
function hasherDefiner(algorithms: CID_ALGORITHM_NAMES[]): {
hasher: SimpleHash,
code: CID_ALGORITHM_CODES
}[] {
const hashers = algorithms.filter(algo => Object.values(CID_ALGORITHM_NAMES).includes(algo))
.map(algo => ({
hasher: createHasher(algo),
code: codeTable[algo]
}));
return hashers;
}
async function cidFinalize(hashers: { hasher: SimpleHash, code: number }[]): Promise<string[]> {
return await Promise.all(hashers.map(async ({code, hasher}) => {
const hashBuffer = hasher.digest();
const digest = create(code, hashBuffer);
return CID.createV1(code, digest).toString();
}));
}
+18
View File
@@ -0,0 +1,18 @@
import crc32 from "crc-32";
import {SimpleHash} from "@root/file-id/SimpleHash";
export class Crc32Hash implements SimpleHash {
private _crc: number = undefined;
update(data: Buffer): Crc32Hash {
// Update the CRC-32 checksum with the new chunk of data
this._crc = crc32.buf(data, this._crc);
return this;
}
digest(): Buffer {
const buffer = Buffer.alloc(4); // Create a buffer of 4 bytes (32 bits)
buffer.writeInt32BE(this._crc, 0); // Write the unsigned integer to the buffer in big-endian format
return buffer;
}
}
+39
View File
@@ -0,0 +1,39 @@
import {CID_ALGORITHM_CODES, CID_ALGORITHM_NAMES} from "../hash-compute/MultiHashData";
import {Readable} from "stream";
import {ReadableStream} from "stream/web";
import {SimpleHash} from "./SimpleHash";
import {createHash} from "crypto";
import {Crc32Hash} from "./Crc32Hash";
export const codeTable = {
[CID_ALGORITHM_NAMES.crc32]: CID_ALGORITHM_CODES.crc32,
[CID_ALGORITHM_NAMES.md5]: CID_ALGORITHM_CODES.md5,
[CID_ALGORITHM_NAMES.sha1]: CID_ALGORITHM_CODES.sha1,
[CID_ALGORITHM_NAMES.sha256]: CID_ALGORITHM_CODES.sha256,
[CID_ALGORITHM_NAMES.sha3_256]: CID_ALGORITHM_CODES.sha3_256,
[CID_ALGORITHM_NAMES.sha3_384]: CID_ALGORITHM_CODES.sha3_384
}
/**
* Compute the CIDs of a file using specific algorithms
* @returns Array of CIDs (in the order of the algorithms)
* @param algo
*/
export function createHasher(algo: CID_ALGORITHM_NAMES): SimpleHash {
switch (algo) {
case CID_ALGORITHM_NAMES.sha256:
return createHash("sha256");
case CID_ALGORITHM_NAMES.sha1:
return createHash("sha1");
case CID_ALGORITHM_NAMES.md5:
return createHash("md5");
case CID_ALGORITHM_NAMES.sha3_256:
return createHash("sha3-256");
case CID_ALGORITHM_NAMES.sha3_384:
return createHash("sha3-384");
case CID_ALGORITHM_NAMES.crc32:
return new Crc32Hash();
default:
throw new Error(`Unsupported algorithm: ${algo}`);
}
}
@@ -0,0 +1,6 @@
import {CID_ALGORITHM_NAMES} from "@root/hash-compute/MultiHashData";
import * as Stream from "stream";
export interface FileIDComputerInterface{
computeCIDs(filePath: Stream, algorithms: CID_ALGORITHM_NAMES[]): Promise<string[]>;
}
+32
View File
@@ -0,0 +1,32 @@
import {Piscina} from "piscina";
import {CID_ALGORITHM_NAMES} from "../hash-compute/MultiHashData";
import {FileIDComputerInterface} from "@root/file-id/FileIDComputerInterface";
// 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 FileIDComputerWorker{
private piscina: Piscina;
constructor(workerPath?:string) {
this.piscina = new Piscina({
maxThreads: 4,
//filename: new URL('./ShaComputeWorker.ts', import.meta.url).href
filename: workerPath || process.env.WORKER_URL || workerUrl
});
}
/**
* Compute the CIDs of a file using specific algorithms
* @param filePath The path to the file
* @param algorithms Array of algorithms ('sha256', 'sha1')
* @returns Array of CIDs (in the order of the algorithms)
*/
public async computeCIDs(filePath: string, algorithms: CID_ALGORITHM_NAMES[]): Promise<string[]> {
return this.piscina.run({filePath, algorithms});
}
}
+17
View File
@@ -0,0 +1,17 @@
import {CID_ALGORITHM_NAMES} from "@root/hash-compute/MultiHashData";
import fs from "fs";
import {computeCIDs as computeCIDsInternal} from "./ComputeHash";
/**
* Compute the CIDs of a file using specific algorithms
* @param filePath The path to the file
* @param algorithms Array of algorithms ('sha256', 'sha1')
* @returns Array of CIDs (in the order of the algorithms)
*/
export default async function computeCIDs({filePath, algorithms}: {
filePath: string;
algorithms: CID_ALGORITHM_NAMES[]
}): Promise<string[]> {
const stream = fs.createReadStream(filePath);
return computeCIDsInternal({stream, algorithms});
}
+5
View File
@@ -0,0 +1,5 @@
export interface SimpleHash {
update(data: Buffer),
digest(): Buffer;
}