Skip to content

Commit

Permalink
fix flow-bin verification on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mroch committed Sep 7, 2021
1 parent e5b6af7 commit 9885ab6
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/flowLSP/utils/getVerifiedFlowBinPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getFlowBinDirPrefixForPlatform(): null | string {
null;
}

async function getFlowBinRelativePath(flowBinModulePath: string): Promise<string> {
async function getFlowBinRelativePath(flowBinModulePath: string): Promise<{|flowBinDirName: string, flowBinName: string|}> {
const dirPrefix = getFlowBinDirPrefixForPlatform();
if (!dirPrefix) {
throw new Error(`Failed to determine correct binary for platform ${process.platform} and arch ${process.arch}`);
Expand All @@ -38,7 +38,7 @@ async function getFlowBinRelativePath(flowBinModulePath: string): Promise<string
if (!flowBinName) {
throw new Error(`Failed to find flow binary in ${flowBinDir}`);
}
return path.join(flowBinDirName, flowBinName);
return {flowBinDirName, flowBinName};
}

async function getShasums(flowBinModulePath: string, logger: Logger): Promise<Buffer> {
Expand All @@ -59,7 +59,8 @@ async function getShasums(flowBinModulePath: string, logger: Logger): Promise<Bu
}
}

function getShasum(shasums: string, flowBinRelativePath: string): string {
function getShasum(shasums: string, flowBinDirName: string, flowBinName: string): string {
const flowBinRelativePath = `${flowBinDirName}/${flowBinName}`;
const shasum_lines = shasums.split(/\r?\n/);
const shasum_line = shasum_lines.find(line => line.includes(flowBinRelativePath));
if (!shasum_line) {
Expand All @@ -74,8 +75,8 @@ export default async function(flowBinModulePath: string, logger: Logger): Promis
const shasums = await getShasums(flowBinModulePath, logger);

// successfully verified SHASUM256.txt, now we can use it to verify the flow binary
const flowBinRelativePath = await getFlowBinRelativePath(flowBinModulePath);
const flowBinPath = path.join(flowBinModulePath, flowBinRelativePath);
const {flowBinDirName, flowBinName} = await getFlowBinRelativePath(flowBinModulePath);
const flowBinPath = path.join(flowBinModulePath, flowBinDirName, flowBinName);
const hash = createHash('sha256');
const flowBinReadStream = createReadStream(flowBinPath);
const flowBinHashPromise = new Promise((resolve, reject) => {
Expand All @@ -84,7 +85,7 @@ export default async function(flowBinModulePath: string, logger: Logger): Promis
});
flowBinReadStream.pipe(hash);
const flowBinHash = await flowBinHashPromise;
const shasum = getShasum(shasums.toString(), flowBinRelativePath);
const shasum = getShasum(shasums.toString(), flowBinDirName, flowBinName);
if (flowBinHash !== shasum) {
throw new Error(
`Hash of ${flowBinPath} does not match hash from SHASUM256.txt:\n` +
Expand Down

0 comments on commit 9885ab6

Please sign in to comment.