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

chore: migrate to structuredClone #20885

Merged
merged 3 commits into from
Apr 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/config/massage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import is from '@sindresorhus/is';
import { clone } from '../util/clone';
import { getOptions } from './options';
import type { PackageRule, RenovateConfig, UpdateType } from './types';

Expand All @@ -17,7 +16,7 @@ export function massageConfig(config: RenovateConfig): RenovateConfig {
}
});
}
const massagedConfig = clone(config);
const massagedConfig = structuredClone(config);
for (const [key, val] of Object.entries(config)) {
if (allowedStrings.includes(key) && is.string(val)) {
massagedConfig[key] = [val];
Expand Down Expand Up @@ -56,7 +55,7 @@ export function massageConfig(config: RenovateConfig): RenovateConfig {
PackageRule
][]) {
if (updateTypes.includes(key)) {
let newRule = clone(rule);
let newRule = structuredClone(rule);
Object.keys(newRule).forEach((newKey) => {
if (!(newKey.startsWith(`match`) || newKey.startsWith('exclude'))) {
delete newRule[newKey];
Expand Down
3 changes: 1 addition & 2 deletions lib/config/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as memCache from '../../util/cache/memory';
import { clone } from '../../util/clone';
import { regEx } from '../../util/regex';
import * as massage from '../massage';
import * as migration from '../migration';
Expand Down Expand Up @@ -274,7 +273,7 @@ export async function resolveConfigPresets(
_ignorePresets?: string[],
existingPresets: string[] = []
): Promise<AllConfig> {
let ignorePresets = clone(_ignorePresets);
let ignorePresets = structuredClone(_ignorePresets);
if (!ignorePresets || ignorePresets.length === 0) {
ignorePresets = inputConfig.ignorePresets ?? [];
}
Expand Down
6 changes: 2 additions & 4 deletions lib/config/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { logger } from '../logger';
import { clone } from '../util/clone';
import * as options from './options';
import type { RenovateConfig } from './types';

Expand All @@ -11,9 +10,8 @@ export function mergeChildConfig<
if (!child) {
return parent as never;
}
const parentConfig = clone(parent);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const childConfig = clone(child!);
const parentConfig = structuredClone(parent);
const childConfig = structuredClone(child);
const config: Record<string, any> = { ...parentConfig, ...childConfig };
for (const option of options.getOptions()) {
if (
Expand Down
10 changes: 6 additions & 4 deletions lib/logger/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import is from '@sindresorhus/is';
import bunyan from 'bunyan';
import fs from 'fs-extra';
import { RequestError as HttpError } from 'got';
import { clone } from '../util/clone';
import { redactedFields, sanitize } from '../util/sanitize';
import type { BunyanRecord, BunyanStream } from './types';

Expand Down Expand Up @@ -65,7 +64,7 @@ export default function prepareError(err: Error): Record<string, unknown> {
// handle got error
if (err instanceof HttpError) {
const options: Record<string, unknown> = {
headers: clone(err.options.headers),
headers: structuredClone(err.options.headers),
url: err.options.url?.toString(),
hostType: err.options.context.hostType,
};
Expand All @@ -82,8 +81,11 @@ export default function prepareError(err: Error): Record<string, unknown> {
statusCode: err.response?.statusCode,
statusMessage: err.response?.statusMessage,
body:
err.name === 'TimeoutError' ? undefined : clone(err.response.body),
headers: clone(err.response.headers),
// istanbul ignore if: not easily testable
err.name === 'TimeoutError'
? undefined
: structuredClone(err.response.body),
headers: structuredClone(err.response.headers),
httpVersion: err.response.httpVersion,
retryCount: err.response.retryCount,
};
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/datasource/aws-machine-image/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ describe('modules/datasource/aws-machine-image/index', () => {
packageName:
'[{"Name":"owner-id","Values":["602401143452"]},{"Name":"name","Values":["with one matching image to return that image"]}]',
});
expect(res).toStrictEqual({
expect(res).toEqual({
releases: [
{
isDeprecated: false,
Expand All @@ -366,7 +366,7 @@ describe('modules/datasource/aws-machine-image/index', () => {
packageName:
'[{"Name":"owner-id","Values":["602401143452"]},{"Name":"name","Values":["with one deprecated matching image to return that image"]}]',
});
expect(res).toStrictEqual({
expect(res).toEqual({
releases: [
{
isDeprecated: true,
Expand All @@ -385,7 +385,7 @@ describe('modules/datasource/aws-machine-image/index', () => {
packageName:
'[{"Name":"owner-id","Values":["602401143452"]},{"Name":"name","Values":["with 3 matching image to return the newest image"]}]',
});
expect(res).toStrictEqual({
expect(res).toEqual({
releases: [
{
isDeprecated: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/datasource/aws-rds/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('modules/datasource/aws-rds/index', () => {
datasource: AwsRdsDataSource.id,
packageName: '[{"Name":"engine","Values":["mysql"]}]',
});
expect(res).toStrictEqual({
expect(res).toEqual({
releases: [
{
isDeprecated: true,
Expand All @@ -138,7 +138,7 @@ describe('modules/datasource/aws-rds/index', () => {
datasource: AwsRdsDataSource.id,
packageName: '[{"Name":"engine","Values":["mysql"]}]',
});
expect(res).toStrictEqual({
expect(res).toEqual({
releases: [
{
isDeprecated: false,
Expand Down
22 changes: 11 additions & 11 deletions lib/modules/datasource/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [
{
Expand Down Expand Up @@ -1881,7 +1881,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [
{
Expand Down Expand Up @@ -1935,7 +1935,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [],
sourceUrl: 'https://github.com/renovatebot/renovate',
Expand All @@ -1962,7 +1962,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [],
});
Expand All @@ -1986,7 +1986,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [],
});
Expand All @@ -2007,7 +2007,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [],
});
Expand Down Expand Up @@ -2048,7 +2048,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [
{
Expand Down Expand Up @@ -2093,7 +2093,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [
{
Expand Down Expand Up @@ -2123,7 +2123,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [],
});
Expand Down Expand Up @@ -2173,7 +2173,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'registry.company.com/node',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://registry.company.com',
releases: [],
});
Expand Down Expand Up @@ -2229,7 +2229,7 @@ describe('modules/datasource/docker/index', () => {
datasource: DockerDatasource.id,
packageName: 'ghcr.io/visualon/drone-git',
});
expect(res).toStrictEqual({
expect(res).toEqual({
registryUrl: 'https://ghcr.io',
sourceUrl: 'https://github.com/visualon/drone-git',
releases: [{ version: '1.0.0' }],
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/jenkins-plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class JenkinsPluginsDatasource extends Datasource {
return null;
}

const result = clone(plugin);
const result = structuredClone(plugin);
const versions = await this.getJenkinsPluginVersions();
const releases = versions[packageName];
result.releases = releases ? clone(releases) : [];
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/kubernetes-api/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('modules/datasource/kubernetes-api/index', () => {
packageName: 'CSIStorageCapacity',
});
expect(res).not.toBeNull();
expect(res).toStrictEqual({
expect(res).toEqual({
releases: [
{ version: 'storage.k8s.io/v1beta1' },
{ version: 'storage.k8s.io/v1' },
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/gradle/parser/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('modules/manager/gradle/parser/common', () => {
it('prependNestingDepth', () => {
ctx.tmpNestingDepth = ctx.varTokens = [token];
prependNestingDepth(ctx);
expect(ctx.varTokens).toStrictEqual([token, token]);
expect(ctx.varTokens).toEqual([token, token]);

coalesceVariable(ctx);
expect(ctx).toMatchObject({
Expand Down
3 changes: 1 addition & 2 deletions lib/modules/manager/gradle/parser/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { lexer, parser, query as q } from 'good-enough-parser';
import { clone } from '../../../../util/clone';
import { regEx } from '../../../../util/regex';
import type {
Ctx,
Expand Down Expand Up @@ -52,7 +51,7 @@ export function reduceNestingDepth(ctx: Ctx): Ctx {
}

export function prependNestingDepth(ctx: Ctx): Ctx {
ctx.varTokens = [...clone(ctx.tmpNestingDepth), ...ctx.varTokens];
ctx.varTokens = [...structuredClone(ctx.tmpNestingDepth), ...ctx.varTokens];
return ctx;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { updateLockedDependency } from '../..';
import { Fixtures } from '../../../../../../test/fixtures';
import * as httpMock from '../../../../../../test/http-mock';
import { clone } from '../../../../../util/clone';
import type { UpdateLockedConfig } from '../../../types';

const packageFileContent = Fixtures.get('package.json', './package-lock');
Expand Down Expand Up @@ -114,7 +113,7 @@ describe('modules/manager/npm/update/locked-dependency/index', () => {
});

it('fails to remediate if parent dep cannot support', async () => {
const acceptsModified = clone(acceptsJson);
const acceptsModified = structuredClone(acceptsJson);
acceptsModified.versions['2.0.0'] = {};
httpMock
.scope('https://registry.npmjs.org')
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('modules/platform/github/index', () => {
});

const repos = await github.getRepos();
expect(repos).toStrictEqual(['a/b', 'c/d']);
expect(repos).toEqual(['a/b', 'c/d']);
});

it('should return an array of repos when using GitHub App Installation Token', async () => {
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('modules/platform/github/index', () => {
});

const repos = await github.getRepos();
expect(repos).toStrictEqual(['a/b', 'c/d']);
expect(repos).toEqual(['a/b', 'c/d']);
});
});

Expand Down
5 changes: 5 additions & 0 deletions lib/util/clone.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { quickStringify } from './stringify';

/**
* Creates a deep clone of an object.
* @deprecated Use {@link structuredClone} instead.
* @param input The object to clone.
*/
export function clone<T>(input: T | null = null): T {
const stringifiedInput = quickStringify(input);
return stringifiedInput ? JSON.parse(stringifiedInput) : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DateTime, Settings } from 'luxon';
import * as memCache from '../../../cache/memory';
import { clone } from '../../../clone';
import type { GithubDatasourceItem, GithubGraphqlCacheRecord } from '../types';
import { GithubGraphqlMemoryCacheStrategy } from './memory-cache-strategy';

Expand Down Expand Up @@ -45,7 +44,7 @@ describe('util/github/graphql/cache-strategies/memory-cache-strategy', () => {
items,
createdAt: isoTs('2022-10-01 15:30'),
};
memCache.set('github-graphql-cache:foo:bar', clone(cacheRecord));
memCache.set('github-graphql-cache:foo:bar', structuredClone(cacheRecord));

// At this moment, cache is valid
let now = '2022-10-31 15:29:59';
Expand Down Expand Up @@ -85,7 +84,7 @@ describe('util/github/graphql/cache-strategies/memory-cache-strategy', () => {
items: oldItems,
createdAt: isoTs('2022-10-30 12:00'),
};
memCache.set('github-graphql-cache:foo:bar', clone(cacheRecord));
memCache.set('github-graphql-cache:foo:bar', structuredClone(cacheRecord));

const now = '2022-10-31 15:30';
mockTime(now);
Expand Down Expand Up @@ -121,7 +120,7 @@ describe('util/github/graphql/cache-strategies/memory-cache-strategy', () => {
items: oldItems,
createdAt: isoTs('2022-10-30 12:00'),
};
memCache.set('github-graphql-cache:foo:bar', clone(cacheRecord));
memCache.set('github-graphql-cache:foo:bar', structuredClone(cacheRecord));

const now = '2022-10-31 15:30';
mockTime(now);
Expand All @@ -147,7 +146,7 @@ describe('util/github/graphql/cache-strategies/memory-cache-strategy', () => {
items: oldItems,
createdAt: isoTs('2022-12-31 12:00'),
};
memCache.set('github-graphql-cache:foo:bar', clone(cacheRecord));
memCache.set('github-graphql-cache:foo:bar', structuredClone(cacheRecord));

const now = '2022-12-31 23:59';
mockTime(now);
Expand Down Expand Up @@ -191,7 +190,7 @@ describe('util/github/graphql/cache-strategies/memory-cache-strategy', () => {
items,
createdAt: isoTs('2022-10-30 12:00'),
};
memCache.set('github-graphql-cache:foo:bar', clone(cacheRecord));
memCache.set('github-graphql-cache:foo:bar', structuredClone(cacheRecord));

const now = '2022-10-31 15:30';
mockTime(now);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DateTime, Settings } from 'luxon';
import * as packageCache from '../../../cache/package';
import { clone } from '../../../clone';
import type { GithubDatasourceItem, GithubGraphqlCacheRecord } from '../types';
import { GithubGraphqlPackageCacheStrategy } from './package-cache-strategy';

Expand Down Expand Up @@ -31,7 +30,7 @@ describe('util/github/graphql/cache-strategies/package-cache-strategy', () => {
items: oldItems,
createdAt: isoTs('2022-10-15 12:00'),
};
cacheGet.mockResolvedValueOnce(clone(cacheRecord));
cacheGet.mockResolvedValueOnce(structuredClone(cacheRecord));

const now = '2022-10-30 12:00';
mockTime(now);
Expand Down
Loading