From 42dd6908778c9ff83dc5df51d1c82b09d0efbfad Mon Sep 17 00:00:00 2001 From: throwaway96 <68320646+throwaway96@users.noreply.github.com> Date: Fri, 24 Mar 2023 16:36:55 -0400 Subject: [PATCH] only read start-devmode.sh once 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. --- services/service.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/services/service.ts b/services/service.ts index c222541..1275287 100644 --- a/services/service.ts +++ b/services/service.ts @@ -99,6 +99,15 @@ async function hashFile(filePath: string, algorithm: string): Promise { 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. */ @@ -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'} ) .catch((err) => (console.warn(`reading ${startDevmode} failed: ${err.toString()}`), '')) as string; + const localChecksum = hashString(startDevmodeContents, 'sha256'); + if (localChecksum !== bundledStartupChecksum && updateableChecksums.indexOf(localChecksum) !== -1) { await copyScript(bundledStartup, startDevmode); messages.push(`${startDevmode} updated!`);