-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(debian): extract content-addressable storage logic to separa…
…te module
- Loading branch information
Showing
2 changed files
with
56 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as path from 'node:path'; | ||
import { PassThrough, Readable } from 'node:stream'; | ||
import { buffer } from 'node:stream/consumers'; | ||
import { createGzip } from 'node:zlib'; | ||
import { createHash } from 'node:crypto'; | ||
import { createWriteStream } from 'node:fs'; | ||
import { pipeline } from 'node:stream/promises'; | ||
|
||
import { createDir } from '../fs.mjs'; | ||
|
||
export interface ContentAddress { | ||
sha256: Uint8Array; | ||
} | ||
|
||
export interface ContentDescription { | ||
size: number; | ||
address: ContentAddress; | ||
} | ||
|
||
export async function storeGzippedStream(root: string, stream: NodeJS.ReadableStream): Promise<ContentDescription> { | ||
const gzip = createGzip({ level: 9 }); | ||
const gzippedStream = new PassThrough(); | ||
await pipeline(stream, gzip, gzippedStream); | ||
return storeStream(root, gzippedStream); | ||
} | ||
|
||
// https://wiki.debian.org/DebianRepository/Format#indices_acquisition_via_hashsums_.28by-hash.29 | ||
export async function storeStream(root: string, stream: NodeJS.ReadableStream): Promise<ContentDescription> { | ||
const contentBuffer = await buffer(stream); | ||
|
||
// compute a digest | ||
const sha256Hash = createHash('sha256'); | ||
await pipeline(Readable.from(contentBuffer), sha256Hash); | ||
const sha256 = sha256Hash.read(); | ||
|
||
// store in a file | ||
const sha256Dir = path.join(root, 'SHA256'); | ||
const fileName = path.join(sha256Dir, Buffer.from(sha256).toString('hex')); | ||
createDir(sha256Dir); | ||
await pipeline(Readable.from(contentBuffer), createWriteStream(fileName)); | ||
return { | ||
size: contentBuffer.length, | ||
address: { sha256 }, | ||
}; | ||
} |
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