@travetto/asset
Advanced tools
Comparing version 4.0.7 to 4.0.8
{ | ||
"name": "@travetto/asset", | ||
"version": "4.0.7", | ||
"version": "4.0.8", | ||
"description": "Modular library for storing and retrieving binary assets", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -140,3 +140,2 @@ import fs from 'node:fs/promises'; | ||
static async hashUrl(url: string, byteLimit = -1): Promise<string> { | ||
const hasher = crypto.createHash('sha256').setEncoding('hex'); | ||
const str = await fetch(url); | ||
@@ -148,13 +147,24 @@ if (!str.ok) { | ||
let count = 0; | ||
const buffer: Buffer[] = []; | ||
for await (const chunk of body) { | ||
if (Buffer.isBuffer(chunk) || typeof chunk === 'string') { | ||
count += chunk?.length ?? 0; | ||
hasher.update(chunk); | ||
if (Buffer.isBuffer(chunk)) { | ||
buffer.push(chunk); | ||
count += chunk.length; | ||
} else if (typeof chunk === 'string') { | ||
buffer.push(Buffer.from(chunk)); | ||
count += chunk.length; | ||
} | ||
if (count > byteLimit && byteLimit > 0) { | ||
body.destroy(); | ||
} | ||
} | ||
if (count > byteLimit && byteLimit > 0) { | ||
body.destroy(); | ||
} | ||
} | ||
return hasher.end().read().toString(); | ||
const hasher = crypto.createHash('sha256').setEncoding('hex'); | ||
const finalData = Buffer.concat(buffer, byteLimit <= 0 ? undefined : byteLimit); | ||
return hasher.update(finalData).end().read().toString(); | ||
} | ||
} |
28256
371