@1n/file-hash
Version:
Get the hash-sum of a given file, with low memory usage, even on huge file
2 lines (1 loc) • 826 B
JavaScript
import{createHash as r}from"node:crypto";import{openSync as n,readSync as o,closeSync as t,createReadStream as e}from"node:fs";import{env as i}from"node:process";import{isString as f}from"@1k/shared";function u(r){if(r){if(f(r))return r;const n=r.algorithm;if(n&&f(n))return n}return"md5"}function d(e,i){const f=8192,d=r(u(i)),s=n(e,"r"),a=Buffer.alloc(f);try{let r;do{r=o(s,a,0,f,null),d.update(a.subarray(0,r))}while(r===f)}finally{t(s)}return d.digest("hex")}function s(n,o){return new Promise(((t,f)=>function(n,o,t,e){const f=i.DEBUG,u=f?Date.now():0,d=r(n||"md5");o.on("data",(r=>d.update(r))),o.on("error",e),o.on("end",f?()=>t(d.digest("hex"),Date.now()-u):()=>t(d.digest("hex")))}(u(o),e(n),t,f)))}function a(r,n){const o=n&&!0===n.async,t=u(n);return o?s(r,t):d(r,t)}export{a as hash,s as hashAsync,d as hashSync};