Files
meta-hash/dist/chunk-ASEKTFOG.cjs.map
T
2024-04-06 16:57:21 +02:00

1 line
5.4 KiB
Plaintext

{"version":3,"sources":["../src/lib/fileID/ShaComputeWorker.ts","../src/lib/MultiHashData.ts"],"names":["createReadStream","createHash","CID","create","crc32","CID_ALGORITHM_CODES","CID_ALGORITHM_NAMES","CID_ALGORITHM","Crc32Hash","data","buffer","computeCIDs","filePath","algorithms","hashFunctions","hashers","algo","stream","chunk","item","code","hasher","hashBuffer","digest"],"mappings":"AAAA,OAAQ,oBAAAA,MAAuB,KAC/B,OAAQ,cAAAC,MAAiB,SACzB,OAAQ,OAAAC,MAAU,mBAClB,OAAQ,UAAAC,MAAa,6BACrB,OAAOC,MAAW,SCaX,IAAKC,OACRA,IAAA,MAAQ,KAAR,QACAA,IAAA,IAAM,KAAN,MACAA,IAAA,KAAO,IAAP,OACAA,IAAA,OAAS,IAAT,SACAA,IAAA,SAAW,IAAX,WACAA,IAAA,SAAW,IAAX,WANQA,OAAA,IASAC,OACRA,EAAA,MAAQ,YACRA,EAAA,IAAM,UACNA,EAAA,KAAO,WACPA,EAAA,OAAS,eACTA,EAAA,SAAW,eACXA,EAAA,SAAW,eANHA,OAAA,IASAC,OACRA,EAAA,MAAQ,QACRA,EAAA,IAAM,MACNA,EAAA,KAAO,OACPA,EAAA,OAAS,WACTA,EAAA,SAAW,WACXA,EAAA,SAAW,WANHA,OAAA,IDtBZ,IAAMC,EAAN,KAAsC,CAC1B,KAAe,OAEvB,OAAOC,EAAyB,CAE5B,YAAK,KAAOL,EAAM,IAAIK,EAAM,KAAK,IAAI,EAC9B,IACX,CAEA,QAAiB,CACb,IAAMC,EAAS,OAAO,MAAM,CAAC,EAC7B,OAAAA,EAAO,aAAa,KAAK,KAAM,CAAC,EACzBA,CACX,CACJ,EASA,eAAOC,EAAmC,CAAC,SAAAC,EAAU,WAAAC,CAAU,EAGzC,CAClB,IAAMC,EACA,CACD,eAA6B,CAAC,QAAkC,OAAQb,EAAW,QAAQ,CAAC,EAC5F,SAA2B,CAAC,QAAgC,OAAQA,EAAW,MAAM,CAAC,EACtF,QAA0B,CAAC,SAA+B,OAAQA,EAAW,KAAK,CAAC,EACnF,eAA+B,CAAC,QAAoC,OAAQA,EAAW,UAAU,CAAC,EAClG,eAA+B,CAAC,QAAoC,OAAQA,EAAW,UAAU,CAAC,EAClG,UAA4B,CAAC,SAAiC,OAAQ,IAAIO,CAAW,CAE1F,EAEMO,EAAUF,EAAW,IAAIG,IAAS,CACpC,OAAQF,EAAcE,CAAI,EAAE,OAC5B,KAAMF,EAAcE,CAAI,EAAE,IAC9B,EAAE,EAEIC,EAASjB,EAAiBY,CAAQ,EACxC,cAAiBM,KAASD,EACtB,QAAWE,KAAQJ,EACfI,EAAK,OAAO,OAAOD,CAAe,EAU1C,OANa,MAAM,QAAQ,IAAIH,EAAQ,IAAI,MAAO,CAAC,KAAAK,EAAM,OAAAC,CAAM,IAAM,CACjE,IAAMC,EAAaD,EAAO,OAAO,EAC3BE,EAASpB,EAAOiB,EAAME,CAAU,EACtC,OAAOpB,EAAI,SAASkB,EAAMG,CAAM,EAAE,SAAS,CAC/C,CAAC,CAAC,CAGN","sourcesContent":["import {createReadStream} from \"fs\";\nimport {createHash} from 'crypto';\nimport {CID} from 'multiformats/cid';\nimport {create} from \"multiformats/hashes/digest\";\nimport crc32 from 'crc-32';\nimport {CID_ALGORITHM_CODES, CID_ALGORITHM_NAMES} from \"../MultiHashData\";\n\nexport interface SimpleHash {\n update(data: Buffer),\n\n digest(): Buffer;\n}\n\nclass Crc32Hash implements SimpleHash {\n private _crc: number = undefined;\n\n update(data: Buffer): Crc32Hash {\n // Update the CRC-32 checksum with the new chunk of data\n this._crc = crc32.buf(data, this._crc);\n return this;\n }\n\n digest(): Buffer {\n const buffer = Buffer.alloc(4); // Create a buffer of 4 bytes (32 bits)\n buffer.writeInt32BE(this._crc, 0); // Write the unsigned integer to the buffer in big-endian format\n return buffer;\n }\n}\n\n\n/**\n * Compute the CIDs of a file using specific algorithms\n * @param filePath The path to the file\n * @param algorithms Array of algorithms ('sha256', 'sha1')\n * @returns Array of CIDs (in the order of the algorithms)\n */\nexport default async function computeCIDs({filePath, algorithms}: {\n filePath: string;\n algorithms: CID_ALGORITHM_NAMES[]\n}): Promise<string[]> {\n const hashFunctions: Record<string, { code: number, hasher: SimpleHash }>\n = {\n [CID_ALGORITHM_NAMES.sha256]: {code: CID_ALGORITHM_CODES.sha256, hasher: createHash('sha256')},\n [CID_ALGORITHM_NAMES.sha1]: {code: CID_ALGORITHM_CODES.sha1, hasher: createHash('sha1')},\n [CID_ALGORITHM_NAMES.md5]: {code: CID_ALGORITHM_CODES.md5, hasher: createHash('md5')},\n [CID_ALGORITHM_NAMES.sha3_256]: {code: CID_ALGORITHM_CODES.sha3_256, hasher: createHash('sha3-256')},\n [CID_ALGORITHM_NAMES.sha3_384]: {code: CID_ALGORITHM_CODES.sha3_384, hasher: createHash('sha3-384')},\n [CID_ALGORITHM_NAMES.crc32]: {code: CID_ALGORITHM_CODES.crc32, hasher: new Crc32Hash()}\n\n };\n\n const hashers = algorithms.map(algo => ({\n hasher: hashFunctions[algo].hasher,\n code: hashFunctions[algo].code\n }));\n\n const stream = createReadStream(filePath);\n for await (const chunk of stream) {\n for (const item of hashers) {\n item.hasher.update(chunk as Buffer);\n }\n }\n\n const cids = await Promise.all(hashers.map(async ({code, hasher}) => {\n const hashBuffer = hasher.digest();\n const digest = create(code, hashBuffer);\n return CID.createV1(code, digest).toString();\n }));\n\n return cids;\n}\n","export interface MultiHashData {\n cid_crc32?: string; //0x0132 CRC32 (SFV)\n cid_md5?: string; // 0xd5 md5\n cid_sha1?: string; // 0x11 sha1\n \"cid_sha2-256\"?: string;//0x12 sha2-256\n \"cid_sha3-256\"?: string; // 0x16 sha3-256\n \"cid_sha3_384\"?: string; // 0x16 sha3-256\n}\n\nexport interface ComputeInterface {\n computeMissingHash(filePath: string, metadata: MultiHashData): Promise<void>;\n}\n\n/**\n * according to\n * https://ipfs.io/ipfs/QmXec1jjwzxWJoNbxQF5KffL8q6hFXm9QwUGaa3wKGk6dT/#title=Multicodecs&src=https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv\n */\nexport enum CID_ALGORITHM_CODES {\n crc32 = 0x0132,\n md5 = 0xd5,\n sha1 = 0x11,\n sha256 = 0x12,\n sha3_256 = 0x16,\n sha3_384 = 0x15,\n}\n\nexport enum CID_ALGORITHM_NAMES {\n crc32 = 'cid_crc32',\n md5 = 'cid_md5',\n sha1 = 'cid_sha1',\n sha256 = 'cid_sha2-256',\n sha3_256 = 'cid_sha3-256',\n sha3_384 = 'cid_sha3-384',\n}\n\nexport enum CID_ALGORITHM {\n crc32 = 'crc32',\n md5 = 'md5',\n sha1 = 'sha1',\n sha256 = 'sha2-256',\n sha3_256 = 'sha3-256',\n sha3_384 = 'sha3-384',\n}"]}