Skip to content

Commit

Permalink
feat: Use klona library for deep clone (#22979)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jun 26, 2023
1 parent bdf5fb9 commit ef6f0c7
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`modules/datasource/cdnjs/index getReleases filters releases by asset pr
"registryUrl": "https://api.cdnjs.com/",
"releases": [
{
"newDigest": undefined,
"version": "0.7.5",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ exports[`modules/datasource/galaxy-collection/index getReleases returns only val
"version": "1.2.0",
},
],
"tags": undefined,
}
`;
2 changes: 1 addition & 1 deletion lib/modules/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export async function getPkgReleases(
logger.error({ config }, 'Datasource getReleases without packageName');
return null;
}
let res: ReleaseResult;
let res: ReleaseResult | null = null;
try {
res = clone(
await getRawReleases({
Expand Down
72 changes: 72 additions & 0 deletions lib/modules/datasource/npm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ exports[`modules/datasource/npm/index should fetch package info from custom regi
"registryUrl": "https://npm.mycustomregistry.com",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -28,10 +34,16 @@ exports[`modules/datasource/npm/index should fetch package info from npm 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -50,10 +62,16 @@ exports[`modules/datasource/npm/index should handle foobar 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -72,10 +90,16 @@ exports[`modules/datasource/npm/index should handle no time 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"version": "0.0.2",
},
],
Expand All @@ -93,10 +117,16 @@ exports[`modules/datasource/npm/index should not send an authorization header if
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -115,6 +145,9 @@ exports[`modules/datasource/npm/index should parse repo url (string) 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
Expand All @@ -132,6 +165,9 @@ exports[`modules/datasource/npm/index should parse repo url 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
Expand All @@ -149,10 +185,16 @@ exports[`modules/datasource/npm/index should replace any environment variable in
"registryUrl": "https://registry.from-env.com",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -176,10 +218,16 @@ Marking the latest version of an npm package as deprecated results in the entire
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"isDeprecated": true,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
Expand All @@ -206,10 +254,16 @@ exports[`modules/datasource/npm/index should send an authorization header if pro
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -228,10 +282,16 @@ exports[`modules/datasource/npm/index should use default registry if missing fro
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -250,10 +310,16 @@ exports[`modules/datasource/npm/index should use host rules by baseUrl if provid
"registryUrl": "https://npm.mycustomregistry.com/_packaging/mycustomregistry/npm/registry",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -272,10 +338,16 @@ exports[`modules/datasource/npm/index should use host rules by hostName if provi
"registryUrl": "https://npm.mycustomregistry.com",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand Down
27 changes: 19 additions & 8 deletions lib/util/clone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { clone } from './clone';

describe('util/clone', () => {
const obj: any = {
name: 'object',
type: 'object',
isObject: true,
};

it('returns null', () => {
const res = clone(null);
expect(res).toBeNull();
});

it('maintains same order', () => {
const obj: any = {
name: 'object',
type: 'object',
isObject: true,
};

const res = clone(obj);

expect(res).toMatchSnapshot(`{
name: 'object',
type: 'object',
Expand All @@ -22,8 +23,18 @@ describe('util/clone', () => {
});

it('assigns "[Circular]" to circular references', () => {
const obj: any = {
name: 'object',
type: 'object',
isObject: true,
};
obj.circular = obj;
const res = clone(obj);
expect(res.circular).toBe('[Circular]');

expect(clone(obj)).toMatchObject({
circular: '[Circular]',
isObject: true,
name: 'object',
type: 'object',
});
});
});
18 changes: 15 additions & 3 deletions lib/util/clone.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { klona } from 'klona/json';
import { logger } from '../logger';
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;
export function clone<T = unknown>(input: T): T {
try {
return klona(input);
} catch (err) {
logger.warn({ err }, 'error cloning object');
const str = quickStringify(input);
if (str) {
return JSON.parse(str);
}

// istanbul ignore next: not easily testable
throw err;
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
"json-dup-key-validator": "1.0.3",
"json-stringify-pretty-compact": "3.0.0",
"json5": "2.2.3",
"klona": "2.0.6",
"luxon": "3.3.0",
"markdown-it": "13.0.1",
"markdown-table": "2.0.0",
Expand Down

0 comments on commit ef6f0c7

Please sign in to comment.