get-node
Version:
Download a specific version of Node.js
74 lines (49 loc) • 1.5 kB
JavaScript
import{createHash}from"node:crypto";
import{env}from"node:process";
import{text}from"node:stream/consumers";
import fetchNodeWebsite from"fetch-node-website";
export const checkChecksum=async({
version,
filepath,
response,
fetchOpts
})=>{
try{
const[expectedChecksum,actualChecksum]=await Promise.all([
getExpectedChecksum(version,filepath,fetchOpts),
getActualChecksum(response)]
);
if(actualChecksum!==expectedChecksum){
return`Could not download Node.js ${version}: checksum did not match`
}
}catch(error){
return`Could not download Node.js ${version} checksum: ${error.message}`
}
};
const getExpectedChecksum=async(version,filepath,fetchOpts)=>{
const checksumLines=await getChecksumLines(version,fetchOpts);
const[expectedChecksum]=checksumLines.
split("\n").
map(parseChecksumLine).
find(([,expectedFilepath])=>expectedFilepath===filepath);
return expectedChecksum
};
const getChecksumLines=async(version,fetchOpts)=>{
if(env.TEST_CHECKSUMS!==undefined){
return env.TEST_CHECKSUMS
}
const response=await fetchNodeWebsite(`v${version}/SHASUMS256.txt`,{
...fetchOpts,
progress:false
});
const checksumLines=await text(response);
return checksumLines
};
const parseChecksumLine=(checksumLine)=>
checksumLine.trim().split(CHECKSUM_LINE_DELIMITER);
const CHECKSUM_LINE_DELIMITER=/\s+/u;
const getActualChecksum=async(response)=>{
const hashStream=response.pipe(createHash("sha256",{encoding:"hex"}));
const actualChecksum=await text(hashStream);
return actualChecksum
};