-
-
Notifications
You must be signed in to change notification settings - Fork 610
Fix #4699: Read no more than the last 1 MB of log files to be shown #5457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thatguywiththekids
wants to merge
9
commits into
Heroic-Games-Launcher:main
Choose a base branch
from
thatguywiththekids:dont-crash-on-large-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8c2d3ca
Fix #4699 by only reading the last 1 MB of log files.
thatguywiththekids c63139e
Add tests for readLastBytes.
thatguywiththekids 70ae5aa
Fix lint error.
thatguywiththekids a75d3d0
Fix prettier complaints.
thatguywiththekids c0e87d2
Use the promise API already present.
thatguywiththekids 3257a95
Handle failed getLogContent promise, and differ between no file and o…
thatguywiththekids f225bb3
Separate try blocks for file reading and buffer decoding.
thatguywiththekids 73d03ae
Split readLastBytes into readLastBytes and decodeUTF8.
thatguywiththekids ca930a8
Explain what we are looking for in the byte.
thatguywiththekids File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { decodeUTF8 } from '../strings' | ||
|
|
||
| jest.mock('backend/logger', () => ({ | ||
| logError: jest.fn() | ||
| })) | ||
|
|
||
| describe('decodeUTF8', () => { | ||
| it('decodes a simple string', () => { | ||
| const buffer = Buffer.from('Hello World') | ||
| expect(decodeUTF8(buffer)).toBe('Hello World') | ||
| }) | ||
|
|
||
| it('skips leading continuation bytes', () => { | ||
| // '🚀' is 4 bytes: 0xF0 0x9F 0x9A 0x80 | ||
| // Test broken rocket + 'a' | ||
| const buffer = Buffer.from([0x9f, 0x9a, 0x80, 0x61]) | ||
| expect(decodeUTF8(buffer)).toBe('a') | ||
| }) | ||
|
|
||
| it('does not skip if first byte is not a continuation byte', () => { | ||
| const buffer = Buffer.from([0x61, 0x80, 0x80]) | ||
| expect(decodeUTF8(buffer)).toBe(buffer.toString('utf-8')) | ||
| }) | ||
|
|
||
| it('translates empty buffer to empty string', () => { | ||
| const buffer = Buffer.alloc(0) | ||
| expect(decodeUTF8(buffer)).toBe('') | ||
| }) | ||
| }) |
45 changes: 45 additions & 0 deletions
45
src/backend/utils/filesystem/__tests__/read_last_bytes.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import fs from 'fs' | ||
| import path from 'path' | ||
| import os from 'os' | ||
| import { readLastBytes } from '../read_last_bytes' | ||
| import { logError } from 'backend/logger' | ||
|
|
||
| jest.mock('backend/logger', () => ({ | ||
| logError: jest.fn() | ||
| })) | ||
|
|
||
| describe('readLastBytes', () => { | ||
| const testFile = path.join(os.tmpdir(), `heroic_test_log_${Date.now()}.log`) | ||
|
|
||
| afterEach(() => { | ||
| if (fs.existsSync(testFile)) { | ||
| try { | ||
| fs.unlinkSync(testFile) | ||
| } catch { | ||
| // Ignore if file was already removed | ||
| } | ||
| } | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it('reads the whole file if it is smaller than n', async () => { | ||
| const content = 'Hello World' | ||
| fs.writeFileSync(testFile, content) | ||
|
|
||
| const buffer = await readLastBytes(testFile, 100) | ||
| expect(buffer.toString()).toBe(content) | ||
| }) | ||
|
|
||
| it('reads only the last n bytes if the file is larger than n', async () => { | ||
| const content = '0123456789' | ||
| fs.writeFileSync(testFile, content) | ||
|
|
||
| const buffer = await readLastBytes(testFile, 5) | ||
| expect(buffer.toString()).toBe('56789') | ||
| }) | ||
|
|
||
| it('throws error for non-existent files', async () => { | ||
| await expect(readLastBytes('non_existent_file.log', 100)).rejects.toThrow() | ||
| expect(logError).toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { promises as fsp } from 'graceful-fs' | ||
| import { logError } from 'backend/logger' | ||
|
|
||
| /** | ||
| * Reads the last `n` bytes of a file as a Buffer. | ||
| * If the file is smaller than `n` bytes, the whole file is read. | ||
| */ | ||
| export async function readLastBytes(path: string, n: number): Promise<Buffer> { | ||
| let fileHandle: fsp.FileHandle | undefined | ||
| try { | ||
| fileHandle = await fsp.open(path, 'r') | ||
| const { size: fileSize } = await fileHandle.stat() | ||
| const bytesToRead = Math.min(fileSize, n) | ||
| const position = fileSize - bytesToRead | ||
|
|
||
| const buffer = Buffer.alloc(bytesToRead) | ||
| await fileHandle.read(buffer, 0, bytesToRead, position) | ||
| return buffer | ||
| } catch (error) { | ||
| logError(`Error reading last bytes of ${path}: ${error}`) | ||
| throw error | ||
| } finally { | ||
| if (fileHandle !== undefined) { | ||
| await fileHandle.close() | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { logError } from 'backend/logger' | ||
|
|
||
| /** | ||
| * Decodes a buffer to a UTF-8 string. | ||
| * Any leading continuation bytes are skipped to get a clean string start. | ||
| * | ||
| * @param buffer The buffer to decode | ||
| */ | ||
| export function decodeUTF8(buffer: Buffer): string { | ||
| try { | ||
| let skip = 0 | ||
| // If bit 7 is set and bit 6 is cleared, this is a continuation byte. | ||
| while (skip < 4 && skip < buffer.length && (buffer[skip] & 0xc0) === 0x80) { | ||
| skip++ | ||
| } | ||
| return buffer.subarray(skip).toString('utf-8') | ||
| } catch (error) { | ||
| logError(`Error decoding buffer as UTF-8: ${error}`) | ||
| throw error | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.