Skip to content

Commit

Permalink
fix minor review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdev03 committed Aug 21, 2024
1 parent 33f717a commit b911784
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
12 changes: 3 additions & 9 deletions lib/modules/datasource/deb/checksum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import crypto from 'crypto';
import { createCacheReadStream } from '../../../util/fs';
import { hashStream } from '../../../util/hash';
import { escapeRegExp, regEx } from '../../../util/regex';

/**
Expand Down Expand Up @@ -35,12 +35,6 @@ export function parseChecksumsFromInRelease(
* @returns resolves to the SHA256 checksum
*/
export function computeFileChecksum(filePath: string): Promise<string> {
return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256');
const stream = createCacheReadStream(filePath);

stream.on('data', (data) => hash.update(data));
stream.on('end', () => resolve(hash.digest('hex')));
stream.on('error', (error) => reject(error));
});
const stream = createCacheReadStream(filePath);
return hashStream(stream, 'sha256');
}
15 changes: 6 additions & 9 deletions lib/modules/datasource/deb/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createHash } from 'crypto';
import readline from 'readline';
import { createUnzip } from 'zlib';
import { nanoid } from 'nanoid';
import upath from 'upath';
import { logger } from '../../../logger';
import { cache } from '../../../util/cache/package/decorator';
import * as fs from '../../../util/fs';
import { toSha256 } from '../../../util/hash';
import type { HttpOptions } from '../../../util/http/types';
import { joinUrlParts } from '../../../util/url';
import { Datasource } from '../datasource';
Expand Down Expand Up @@ -120,9 +120,7 @@ export class DebDatasource extends Datasource {
async downloadAndExtractPackage(
componentUrl: string,
): Promise<{ extractedFile: string; lastTimestamp: Date }> {
const packageUrlHash = createHash('sha256')
.update(componentUrl)
.digest('hex');
const packageUrlHash = toSha256(componentUrl);
const fullCacheDir = await fs.ensureCacheDir(DebDatasource.cacheSubDir);
const extractedFile = upath.join(fullCacheDir, `${packageUrlHash}.txt`);
let lastTimestamp = await this.getFileCreationTime(extractedFile);
Expand Down Expand Up @@ -225,21 +223,20 @@ export class DebDatasource extends Datasource {
'Downloading Debian package file',
);

const actualChecksum = await computeFileChecksum(compressedFile);

let inReleaseContent = '';

try {
inReleaseContent = await this.fetchInReleaseFile(baseReleaseUrl);
} catch (error) {
// This is expected to fail for Artifactory if GPG verification is not enabled
logger.debug(
{ url: baseReleaseUrl, error },
{ url: baseReleaseUrl, err: error },
'Could not fetch InRelease file',
);
}

if (inReleaseContent) {
const actualChecksum = await computeFileChecksum(compressedFile);
const expectedChecksum = parseChecksumsFromInRelease(
inReleaseContent,
// path to the Package.gz file
Expand Down Expand Up @@ -304,7 +301,7 @@ export class DebDatasource extends Datasource {
* @returns a list of packages with minimal Metadata.
*/
@cache({
namespace: `datasource-${DebDatasource.id}-package`,
namespace: `datasource-${DebDatasource.id}`,
key: (extractedFile: string, lastTimestamp: Date) =>
`${extractedFile}:${lastTimestamp.getTime()}`,
ttlMinutes: 24 * 60,
Expand Down Expand Up @@ -362,7 +359,7 @@ export class DebDatasource extends Datasource {
}

@cache({
namespace: `datasource-${DebDatasource.id}-package`,
namespace: `datasource-${DebDatasource.id}`,
key: (componentUrl: string) => componentUrl,
})
async getPackageIndex(
Expand Down
2 changes: 1 addition & 1 deletion lib/util/cache/package/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type PackageCacheNamespace =
| 'datasource-cpan'
| 'datasource-crate-metadata'
| 'datasource-crate'
| 'datasource-deb-package'
| 'datasource-deb'
| 'datasource-deno-details'
| 'datasource-deno-versions'
| 'datasource-deno'
Expand Down

0 comments on commit b911784

Please sign in to comment.