-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63792fa
commit e6afb04
Showing
14 changed files
with
111 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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
22 changes: 22 additions & 0 deletions
22
src/context/offline-drive/contents/application/ContentsChunkReader.ts
This file contains 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,22 @@ | ||
import { Optional } from '../../../../shared/types/Optional'; | ||
import { ContentsRepository } from '../domain/ContentsRepository'; | ||
|
||
export class ContentsChunkReader { | ||
constructor(private readonly repository: ContentsRepository) {} | ||
|
||
async run( | ||
contentsPath: string, | ||
length: number, | ||
position: number | ||
): Promise<Optional<Buffer>> { | ||
const data = await this.repository.read(contentsPath); | ||
|
||
if (position >= data.length) { | ||
return Optional.empty(); | ||
} | ||
|
||
const chunk = data.slice(position, position + length); | ||
|
||
return Optional.of(chunk); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/context/offline-drive/contents/domain/ContentsRepository.ts
This file contains 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,5 @@ | ||
export interface ContentsRepository { | ||
read(path: string): Promise<Buffer>; | ||
|
||
forget(path: string): void; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/context/offline-drive/contents/infrastructure/cache/CachedFSContentsRepository.ts
This file contains 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,23 @@ | ||
import { ContentsRepository } from '../../domain/ContentsRepository'; | ||
import fs from 'fs/promises'; | ||
|
||
export class CachedFSContentsRepository implements ContentsRepository { | ||
private buffers: Map<string, Buffer> = new Map(); | ||
|
||
async read(path: string): Promise<Buffer> { | ||
const cached = this.buffers.get(path); | ||
|
||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const read = await fs.readFile(path); | ||
this.buffers.set(path, read); | ||
|
||
return read; | ||
} | ||
|
||
forget(_path: string): void { | ||
// | ||
} | ||
} |
This file contains 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,23 @@ | ||
export class Optional<T> { | ||
private constructor(private readonly value: T | undefined) {} | ||
|
||
static of<T>(value: T): Optional<T> { | ||
return new Optional(value); | ||
} | ||
|
||
static empty<T>(): Optional<T> { | ||
return new Optional<T>(undefined); | ||
} | ||
|
||
get(): T { | ||
if (!this.value) { | ||
throw new Error('Element not found'); | ||
} | ||
|
||
return this.value; | ||
} | ||
|
||
isPresent(): boolean { | ||
return this.value !== undefined; | ||
} | ||
} |