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 42dd690
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion 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,11 +534,12 @@ 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'} )

Check failure on line 538 in services/service.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'startDevmodeContents' is never reassigned. Use 'const' instead

Check failure on line 538 in services/service.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `await·asyncReadFile(startDevmode,·{·encoding:·'utf-8'}·)` with `(await·asyncReadFile(startDevmode,·{·encoding:·'utf-8'·}).catch(`
.catch((err) => (console.warn(`reading ${startDevmode} failed: ${err.toString()}`), '')) as string;

Check failure on line 539 in services/service.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `.catch((err)·=>·(console.warn(`reading·${startDevmode}·failed:·${err.toString()}`),·''` with `(err)·=>·(console.warn(`reading·${startDevmode}·failed:·${err.toString()}`),·''),⏎··········`

Check failure on line 539 in services/service.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected use of comma operator

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

if (localChecksum !== bundledStartupChecksum && updateableChecksums.indexOf(localChecksum) !== -1) {
await copyScript(bundledStartup, startDevmode);
messages.push(`${startDevmode} updated!`);
Expand Down

0 comments on commit 42dd690

Please sign in to comment.