Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(util/hash): replace hasha sha512 use cases #23548

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/modules/manager/bazel/parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { lang, lexer, parser, query as q } from 'good-enough-parser';
import hasha from 'hasha';
import { logger } from '../../../logger';
import * as memCache from '../../../util/cache/memory';
import { hash } from '../../../util/hash';
import { supportedRulesRegex } from './rules';
import type { NestedFragment, RecordFragment } from './types';

Expand Down Expand Up @@ -294,8 +294,8 @@ const query = q.tree<Ctx>({
});

function getCacheKey(input: string): string {
const hash = hasha(input);
return `bazel-parser-${hash}`;
const hashedInput = hash(input);
return `bazel-parser-${hashedInput}`;
}

const starlark = lang.createLang('starlark');
Expand Down
5 changes: 1 addition & 4 deletions lib/modules/platform/comment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import hasha from 'hasha';
import { getCache } from '../../util/cache/repository';
import { hash } from '../../util/hash';
import type { EnsureCommentConfig, EnsureCommentRemovalConfig } from './types';
import { platform } from '.';

// use sha512: https://www.npmjs.com/package/hasha#algorithm
const hash = (content: string): string => hasha(content);

export async function ensureComment(
commentConfig: EnsureCommentConfig
): Promise<boolean> {
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import hasha from 'hasha';
import { hash } from '../../util/hash';

export function repoFingerprint(
repoId: number | string,
endpoint: string | undefined
): string {
const input = endpoint ? `${endpoint}::${repoId}` : `${repoId}`;
const fingerprint = hasha(input);
const fingerprint = hash(input);
return fingerprint;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/util/cache/repository/impl/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import is from '@sindresorhus/is';
import hasha from 'hasha';
import { GlobalConfig } from '../../../../config/global';
import { logger } from '../../../../logger';
import { compress, decompress } from '../../../compress';
import { hash } from '../../../hash';
import { safeStringify } from '../../../stringify';
import { CACHE_REVISION } from '../common';
import { RepoCacheRecord, RepoCacheV13 } from '../schema';
Expand Down Expand Up @@ -72,8 +72,8 @@ export abstract class RepoCacheBase implements RepoCache {

async save(): Promise<void> {
const jsonStr = safeStringify(this.data);
const hash = await hasha.async(jsonStr);
if (hash === this.oldHash) {
const hashedJsonStr = hash(jsonStr);
if (hashedJsonStr === this.oldHash) {
return;
}

Expand All @@ -88,7 +88,7 @@ export abstract class RepoCacheBase implements RepoCache {
repository,
fingerprint,
payload,
hash,
hash: hashedJsonStr,
});
}

Expand All @@ -101,6 +101,6 @@ export abstract class RepoCacheBase implements RepoCache {
return undefined;
}
const jsonStr = safeStringify(this.data);
return hasha(jsonStr) !== this.oldHash;
return hash(jsonStr) !== this.oldHash;
}
}
6 changes: 3 additions & 3 deletions lib/util/cache/repository/impl/local.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import hasha from 'hasha';
import { fs } from '../../../../../test/util';
import { GlobalConfig } from '../../../../config/global';
import { logger } from '../../../../logger';
import { compress } from '../../../compress';
import { hash } from '../../../hash';
import { CACHE_REVISION } from '../common';
import type { RepoCacheRecord } from '../schema';
import type { RepoCacheData } from '../types';
Expand All @@ -20,15 +20,15 @@ async function createCacheRecord(
const fingerprint = '0123456789abcdef';

const jsonStr = JSON.stringify(data);
const hash = hasha(jsonStr);
const hashedJsonStr = hash(jsonStr);
const payload = await compress(jsonStr);

return {
revision,
repository,
fingerprint,
payload,
hash,
hash: hashedJsonStr,
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/util/fingerprint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hasha from 'hasha';
import { hash } from './hash';
import { safeStringify } from './stringify';

export function fingerprint(input: unknown): string {
const stringifiedInput = safeStringify(input);
return stringifiedInput ? hasha(stringifiedInput) : '';
return stringifiedInput ? hash(stringifiedInput) : '';
}
12 changes: 5 additions & 7 deletions lib/util/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import merge from 'deepmerge';
import got, { Options, RequestError } from 'got';
import hasha from 'hasha';
import type { SetRequired } from 'type-fest';
import { infer as Infer, type ZodError, ZodType } from 'zod';
import { HOST_DISABLED } from '../../constants/error-messages';
Expand All @@ -9,6 +8,7 @@ import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as memCache from '../cache/memory';
import { clone } from '../clone';
import { hash } from '../hash';
import { type AsyncResult, Result } from '../result';
import { resolveBaseUrl } from '../url';
import { applyAuthorization, removeAuthorization } from './auth';
Expand Down Expand Up @@ -180,18 +180,16 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
}
options = applyAuthorization(options);

// use sha512: https://www.npmjs.com/package/hasha#algorithm
const memCacheKey =
options.memCache !== false &&
(options.method === 'get' || options.method === 'head')
? hasha([
'got-',
JSON.stringify({
? hash(
`got-${JSON.stringify({
url,
headers: options.headers,
method: options.method,
}),
])
})}`
)
: null;

let resPromise: Promise<HttpResponse<T>> | null = null;
Expand Down
9 changes: 6 additions & 3 deletions lib/workers/repository/updates/branch-name.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// TODO #7154
import cleanGitRef from 'clean-git-ref';
import hasha from 'hasha';
import slugify from 'slugify';
import type { RenovateConfig } from '../../../config/types';
import { logger } from '../../../logger';
import { hash } from '../../../util/hash';
import { regEx } from '../../../util/regex';
import * as template from '../../../util/template';

Expand Down Expand Up @@ -96,10 +96,13 @@ export function generateBranchName(update: RenovateConfig): void {
hashInput = template.compile(hashInput, update);
hashInput = template.compile(hashInput, update);

const hash = hasha(hashInput);
const hashedInput = hash(hashInput);

// TODO: types (#7154)
update.branchName = `${update.branchPrefix!}${hash.slice(0, hashLength)}`;
update.branchName = `${update.branchPrefix!}${hashedInput.slice(
0,
hashLength
)}`;
} else {
update.branchName = template.compile(update.branchName!, update);

Expand Down