From d111a2b0f018dcda84ad2647d040728888f04d62 Mon Sep 17 00:00:00 2001 From: Egor Kushnarev Date: Mon, 1 Apr 2024 10:42:18 +0300 Subject: [PATCH] by-hash initial commit --- src/deb/deb-builder.mts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/deb/deb-builder.mts b/src/deb/deb-builder.mts index 1464a4b..659d5ea 100644 --- a/src/deb/deb-builder.mts +++ b/src/deb/deb-builder.mts @@ -1,3 +1,4 @@ +import { createHash } from 'crypto'; import { createGzip } from 'zlib'; import * as fs from 'fs'; @@ -8,6 +9,7 @@ import * as tar from 'tar'; import type { Artifact, ArtifactProvider } from '../artifact-provider.mjs'; import { createDir, execToolToFile, removeDir } from '../fs.mjs'; import type { Deployer } from '../deployer.mjs'; +import { createReadStream } from 'fs'; type DebRepoDistribution = string; type DebRepoComponent = string; @@ -111,6 +113,10 @@ export class DebBuilder implements Deployer { await execToolToFile('apt-ftparchive', ['release', distributionPath], releaseFilePath, true); await execToolToFile('gpg', ['--no-tty', '--default-key', this.config.gpgKeyName, '-abs', '-o', releaseGpgFilePath, releaseFilePath]); await execToolToFile('gpg', ['--no-tty', '--default-key', this.config.gpgKeyName, '--clearsign', '-o', inReleaseFilePath, releaseFilePath]); + + await this.handleByHash(releaseFilePath); + await this.handleByHash(releaseGpgFilePath); + await this.handleByHash(inReleaseFilePath); } private async dpkgScanpackages(): Promise { @@ -236,6 +242,7 @@ export class DebBuilder implements Deployer { fs.writeFileSync(targetPackagesFile, packagesContent); compressPromises.push(compressFile(targetPackagesFile)); + compressPromises.push(this.handleByHash(targetPackagesFile)); }); }); @@ -254,4 +261,19 @@ export class DebBuilder implements Deployer { return Promise.all(releasesPromises); } + + private async handleByHash(filePath: string): Promise { + const byHashFileName = path.resolve(path.join(path.dirname(filePath), 'by-hash', 'SHA256', await this.sha256(filePath))); + return fs.promises.copyFile(filePath, byHashFileName); + } + + private async sha256(filePath: string): Promise{ + return new Promise((resolve, reject) => { + const hash = createHash('sha256'); + createReadStream(filePath) + .once('end', () => resolve(hash.digest('hex'))) + .once('error', () => reject) + .pipe(hash); + }); + } }