Files
Sebastian Poxhofer 16ef1715e9 refactor(linters): add rules for @sindresorhus/is helpers (#45229)
* feat(lint): add renovate/prefer-is-helpers and renovate/prefer-is-object rules

Flag ad-hoc type checks that have @sindresorhus/is equivalents:
- .filter(Boolean) -> .filter(isTruthy)
- typeof x === 'string' -> isString(x)
- x === null || x === undefined -> isNullOrUndefined(x) (and negation)

The typeof x === 'object' shape lives in a separate suggestion-only rule
(renovate/prefer-is-object, kept at warn) because isObject()/isPlainObject()
are not drop-in equivalents: null, arrays and functions behave differently.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor: fix renovate/prefer-is-helpers violations

Replace ad-hoc type checks with @sindresorhus/is helpers across lib/:
- 13x .filter(Boolean) -> .filter(isTruthy)
- 36x typeof x ==/!== 'string' -> isString(x) / !isString(x)
- 6x x === null || x === undefined -> isNullOrUndefined(x) (and negation)

Promote renovate/prefer-is-helpers to error now that the codebase is clean.
renovate/prefer-is-object stays at warn: its 15 remaining sites guard
parsed YAML/JSON or unknown values where isObject()/isPlainObject() are
not provably equivalent (null/array/function semantics).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor: enforce renovate/prefer-is-object at error level

Resolve the 15 remaining warnings and raise the rule from warn to error:

- Migrate 10 sites to isObject() where the guarded value provably cannot
  be a function (parsed YAML/JSON, typed exec command arguments, or
  test-controlled fixtures): helm-values, woodpecker, bun, pre-commit
  managers, exec utils, file package cache, and the github http spec.
  In the bun manager this also fixes a latent crash where a null
  `workspaces` value would throw on the `in` operator.
- Keep 5 sites as reasoned oxlint-disable comments where excluding
  functions is load-bearing: the byte-exact fingerprint canonicalizer
  (2), the circularity walker, the zod result guard, and the generic
  hasKey helper.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-13 14:22:03 +00:00

193 lines
4.8 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { createGunzip } from 'node:zlib';
import { isNullOrUndefined } from '@sindresorhus/is';
import upath from 'upath';
import { logger } from '../../../../logger/index.ts';
import * as fs from '../../../../util/fs/index.ts';
import { toSha256 } from '../../../../util/hash.ts';
import type { Http, HttpOptions } from '../../../../util/http/index.ts';
import { acquireLock } from '../../../../util/mutex.ts';
import type { ReleaseResult } from '../../types.ts';
import { datasource } from '../common.ts';
const cacheSubDir = datasource;
type RpmVersionValue = boolean | number | string | null | undefined;
export function formatRpmVersion(
ver: RpmVersionValue,
rel?: RpmVersionValue,
): string | null {
if (isNullOrUndefined(ver)) {
return null;
}
const version = String(ver);
if (isNullOrUndefined(rel)) {
return version;
}
return `${version}-${String(rel)}`;
}
export function buildReleaseResult(
versions: Iterable<string>,
): ReleaseResult | null {
const uniqueVersions = [...new Set(versions)];
if (uniqueVersions.length === 0) {
return null;
}
return {
releases: uniqueVersions.map((version) => ({ version })),
};
}
async function getFileCreationTime(
filePath: string,
): Promise<Date | undefined> {
const stats = await fs.statCacheFile(filePath);
return stats?.ctime;
}
async function checkIfModified(
url: string,
lastDownloadTimestamp: Date,
http: Http,
): Promise<boolean> {
const options: HttpOptions = {
headers: {
'If-Modified-Since': lastDownloadTimestamp.toUTCString(),
},
};
try {
const response = await http.head(url, options);
return response.statusCode !== 304;
} catch (err) {
logger.warn(
{
err,
lastDownloadTimestamp,
url,
},
'Could not determine if metadata file is modified since last download',
);
return true;
}
}
async function downloadGzipFile(
url: string,
compressedFile: string,
http: Http,
lastDownloadTimestamp?: Date,
): Promise<boolean> {
let needsToDownload = true;
if (lastDownloadTimestamp) {
needsToDownload = await checkIfModified(url, lastDownloadTimestamp, http);
}
if (!needsToDownload) {
logger.debug(`No need to download ${url}, file is up to date.`);
return false;
}
const readStream = http.stream(url);
const writeStream = fs.createCacheWriteStream(compressedFile);
await fs.pipeline(readStream, writeStream);
const compressedStats = await fs.statCacheFile(compressedFile);
if (!compressedStats || compressedStats.size === 0) {
logger.debug(`Empty response body from getting ${url}.`);
throw new Error(`Empty response body from getting ${url}.`);
}
return true;
}
async function extractGzipFile(
compressedFile: string,
extractedFile: string,
): Promise<void> {
await fs.pipeline(
fs.createCacheReadStream(compressedFile),
createGunzip(),
fs.createCacheWriteStream(extractedFile),
);
}
export async function getCachedGunzippedFile(
http: Http,
url: string,
extension: 'xml',
): Promise<string> {
const releaseLock = await acquireLock(
`gunzipped-file:${url}:${extension}`,
'datasource-rpm',
);
try {
const cacheDir = await fs.ensureCacheDir(cacheSubDir);
const urlHash = toSha256(url);
const extractedFile = upath.join(cacheDir, `${urlHash}.${extension}`);
let lastTimestamp = await getFileCreationTime(extractedFile);
const compressedFile = upath.join(
cacheDir,
`${randomUUID()}_${urlHash}.gz`,
);
const extractedTempFile = upath.join(
cacheDir,
`${randomUUID()}_${urlHash}.${extension}`,
);
try {
const wasUpdated = await downloadGzipFile(
url,
compressedFile,
http,
lastTimestamp,
);
if (wasUpdated || !lastTimestamp) {
try {
// Only replace the shared cache file after a successful extract.
await extractGzipFile(compressedFile, extractedTempFile);
await fs.renameCacheFile(extractedTempFile, extractedFile);
lastTimestamp = await getFileCreationTime(extractedFile);
} catch (err) {
logger.warn(
{
compressedFile,
err,
extension,
extractedFile,
url,
},
'Failed to extract RPM metadata file from compressed file',
);
}
}
if (!lastTimestamp) {
throw new Error('Missing metadata in extracted RPM metadata file!');
}
return extractedFile;
} finally {
if (await fs.cachePathExists(compressedFile)) {
await fs.rmCache(compressedFile);
}
if (await fs.cachePathExists(extractedTempFile)) {
await fs.rmCache(extractedTempFile);
}
}
} finally {
releaseLock();
}
}