Skip to content

Commit

Permalink
only read start-devmode.sh once
Browse files Browse the repository at this point in the history
This file was being read twice: once to hash it, and again for its
contents. The new function hashString() is now used to compute the hash
from the data already read.
  • Loading branch information
throwaway96 committed Jul 30, 2023
1 parent 1eb2cf2 commit 678ebb1
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ async function hashFile(filePath: string, algorithm: string): Promise<string> {
return hash.read();
}

/**
* Hashes a string with specified algorithm.
*
* Input should be UTF-8.
*/
function hashString(data: string, algorithm: string): string {
return createHash(algorithm).update(data, 'utf-8').digest('hex');
}

/**
* Elevates a package by name.
*/
Expand Down Expand Up @@ -525,10 +534,13 @@ function runService() {

// RootMyTV v1
if (await isFile(startDevmode)) {
const localChecksum = await hashFile(startDevmode, 'sha256');
// Warn and return empty string on read error
let startDevmodeContents = await asyncReadFile(startDevmode, { encoding: 'utf-8'} )
.catch((err) => (console.warn(`reading ${startDevmode} failed: ${err.toString()}`), '')) as string;
const startDevmodeContents = (await asyncReadFile(startDevmode, { encoding: 'utf-8' }).catch((err) => {
console.warn(`reading ${startDevmode} failed: ${err.toString()}`);
return '';
})) as string;

const localChecksum = hashString(startDevmodeContents, 'sha256');

if (localChecksum !== bundledStartupChecksum && updateableChecksums.indexOf(localChecksum) !== -1) {
await copyScript(bundledStartup, startDevmode);
Expand Down

0 comments on commit 678ebb1

Please sign in to comment.