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