Skip to content

Commit

Permalink
by-hash initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
origin-yaropolk committed Apr 1, 2024
1 parent 051b4aa commit eb30758
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/deb/deb-builder.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from 'crypto';
import { createGzip } from 'zlib';

Check failure on line 2 in src/deb/deb-builder.mts

View workflow job for this annotation

GitHub Actions / build

Imports should be sorted alphabetically

import * as fs from 'fs';
Expand All @@ -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';

Check failure on line 12 in src/deb/deb-builder.mts

View workflow job for this annotation

GitHub Actions / build

Imports should be sorted alphabetically

type DebRepoDistribution = string;
type DebRepoComponent = string;
Expand All @@ -23,6 +25,7 @@ const ReleaseFileTemplate =
Label: Ubuntu/Debian
Architecture: $ARCH
Component: $COMPONENT
Acquire-By-Hash: yes
Codename: $DISTRIBUTION\n`;

function iterateComponents(repo: DebRepo, callback: (distribution: string, component: string, deb: Artifact[]) => void): void {
Expand Down Expand Up @@ -201,19 +204,20 @@ export class DebBuilder implements Deployer {
}

private async makeRelease(): Promise<{}> {
const compressFile = (filePath: string): Promise<void> => new Promise<void>(resolve => {
const compressFile = (filePath: string): Promise<string> => new Promise<string>(resolve => {
const inp = fs.createReadStream(filePath);
const out = fs.createWriteStream(`${filePath}.gz`);

const gzip = createGzip({ level: 9 });

inp.pipe(gzip).pipe(out)
.on('finish', () => {
resolve();
resolve(`${filePath}.gz`);
});
});

const compressPromises: Promise<void>[] = [];
const compressPromises: Promise<string>[] = [];
const byHashPromises: Promise<void>[] = [];

iterateComponents(this.config.repo, (distribution, component) => {
const componentRoot = path.join(this.distsPath, distribution, component);
Expand All @@ -234,12 +238,18 @@ export class DebBuilder implements Deployer {
}

fs.writeFileSync(targetPackagesFile, packagesContent);

byHashPromises.push(this.handleByHash(targetPackagesFile));
compressPromises.push(compressFile(targetPackagesFile));
});
});

await Promise.all(compressPromises);
const compressedPackages = await Promise.all(compressPromises);

compressedPackages.forEach(packagePath => {
byHashPromises.push(this.handleByHash(packagePath));
});

await Promise.all(byHashPromises);

const releasesPromises: Promise<void>[] = [];

Expand All @@ -254,4 +264,22 @@ export class DebBuilder implements Deployer {

return Promise.all(releasesPromises);
}

private async handleByHash(filePath: string): Promise<void> {
const byHashDir = path.join(path.dirname(filePath), 'by-hash', 'SHA512');
const byHashFileName = path.resolve(path.join(byHashDir, await this.sha512(filePath)));

createDir(byHashDir);
return fs.promises.copyFile(filePath, byHashFileName);
}

private async sha512(filePath: string): Promise<string>{

Check failure on line 276 in src/deb/deb-builder.mts

View workflow job for this annotation

GitHub Actions / build

Expected 'this' to be used by class async method 'sha512'

Check failure on line 276 in src/deb/deb-builder.mts

View workflow job for this annotation

GitHub Actions / build

Async method 'sha512' has no 'await' expression

Check failure on line 276 in src/deb/deb-builder.mts

View workflow job for this annotation

GitHub Actions / build

Missing space before opening brace
return new Promise((resolve, reject) => {
const hash = createHash('sha512');
createReadStream(filePath)
.once('end', () => resolve(hash.digest('hex')))
.once('error', () => reject)
.pipe(hash);
});
}
}

0 comments on commit eb30758

Please sign in to comment.