Skip to content

Commit

Permalink
refactor: use native structured-clone
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 28, 2023
1 parent 1c1baa6 commit c20bba8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/config/configStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { AsyncOptionalCreatable, cloneJson } from '@salesforce/kit';
import { AsyncOptionalCreatable } from '@salesforce/kit';
import { entriesOf, isPlainObject } from '@salesforce/ts-types';
import { definiteEntriesOf, definiteValuesOf, isJsonMap, isString, JsonMap, Optional } from '@salesforce/ts-types';
import { Crypto } from '../crypto/crypto';
Expand Down Expand Up @@ -95,7 +95,7 @@ export abstract class BaseConfigStore<

if (this.hasEncryption() && decrypt) {
if (isJsonMap(rawValue)) {
return this.recursiveDecrypt(cloneJson(rawValue), key);
return this.recursiveDecrypt(structuredClone(rawValue), key);
} else if (this.isCryptoKey(key)) {
return this.decrypt(rawValue) as P[K] | ConfigValue;
}
Expand Down Expand Up @@ -219,7 +219,7 @@ export abstract class BaseConfigStore<
*/
public getContents(decrypt = false): Readonly<P> {
if (this.hasEncryption() && decrypt) {
return this.recursiveDecrypt(cloneJson(this.contents?.value ?? {})) as P;
return this.recursiveDecrypt(structuredClone(this.contents?.value ?? {})) as P;
}
return this.contents?.value ?? ({} as P);
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/authInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { resolve as pathResolve } from 'node:path';
import * as os from 'node:os';
import * as fs from 'node:fs';
import { Record as RecordType } from 'jsforce';
import { AsyncOptionalCreatable, cloneJson, env, isEmpty, parseJson, parseJsonMap } from '@salesforce/kit';
import { AsyncOptionalCreatable, env, isEmpty, parseJson, parseJsonMap } from '@salesforce/kit';
import {
AnyJson,
asString,
Expand Down Expand Up @@ -847,7 +847,7 @@ export class AuthInfo extends AsyncOptionalCreatable<AuthInfo.Options> {
let authConfig: AuthFields;

if (options) {
options = cloneJson(options);
options = structuredClone(options);

if (this.isTokenOptions(options)) {
authConfig = options;
Expand Down
2 changes: 1 addition & 1 deletion src/schema/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class SchemaValidator {

// AJV will modify the original json object. We need to make a clone of the
// json to keep this backwards compatible with JSEN functionality
const jsonClone: T = JSON.parse(JSON.stringify(json)) as T;
const jsonClone = structuredClone(json);

const valid = validate(jsonClone);

Expand Down
12 changes: 5 additions & 7 deletions test/unit/config/configTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import * as fs from 'node:fs';
import { stubMethod } from '@salesforce/ts-sinon';
import { ensureString, JsonMap } from '@salesforce/ts-types';
import { ensureString } from '@salesforce/ts-types';
import { expect } from 'chai';
import * as lockfileLib from 'proper-lockfile';
import { Config, ConfigPropertyMeta } from '../../../src/config/config';
Expand All @@ -23,8 +23,6 @@ import { shouldThrowSync, TestContext } from '../../../src/testSetup';
const configFileContentsString = '{"target-dev-hub": "configTest_devhub","target-org": "configTest_default"}';
const configFileContentsJson = { 'target-dev-hub': 'configTest_devhub', 'target-org': 'configTest_default' };

const clone = (obj: JsonMap) => JSON.parse(JSON.stringify(obj));

describe('Config', () => {
const $$ = new TestContext();

Expand Down Expand Up @@ -99,7 +97,7 @@ describe('Config', () => {
it('calls Config.write with updated file contents', async () => {
const writeStub = stubMethod($$.SANDBOX, fs.promises, 'writeFile');

const expectedFileContents = clone(configFileContentsJson);
const expectedFileContents = structuredClone(configFileContentsJson);
const newUsername = 'updated_val';
expectedFileContents['target-org'] = newUsername;

Expand All @@ -109,7 +107,7 @@ describe('Config', () => {
});

it('calls Config.write with deleted file contents', async () => {
const expectedFileContents = clone(configFileContentsJson);
const expectedFileContents = structuredClone(configFileContentsJson);
const newUsername = 'updated_val';
expectedFileContents['target-org'] = newUsername;

Expand Down Expand Up @@ -236,8 +234,8 @@ describe('Config', () => {
stubMethod($$.SANDBOX, fs.promises, 'stat').resolves({ mtimeNs: BigInt(new Date().valueOf() - 1_000 * 60 * 5) });
const writeStub = stubMethod($$.SANDBOX, fs.promises, 'writeFile');

const expectedFileContents = clone(configFileContentsJson);
delete expectedFileContents['target-org'];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { 'target-org': deleteThis, ...expectedFileContents } = structuredClone(configFileContentsJson);

const config = await Config.create({ isGlobal: false });
config.unset('target-org');
Expand Down
5 changes: 2 additions & 3 deletions test/unit/messagesTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import { EOL } from 'node:os';
import { cloneJson } from '@salesforce/kit';
import { assert, expect } from 'chai';
import { SinonStub } from 'sinon';
import { Messages } from '../../src/messages';
Expand All @@ -30,8 +29,8 @@ describe('Messages', () => {
msgMap.set('msg1', testMessages.msg1);
msgMap.set('msg1.actions', testMessages.msg2);
msgMap.set('msg2', testMessages.msg2);
msgMap.set('msg3', cloneJson(testMessages));
msgMap.get('msg3').msg3 = cloneJson(testMessages);
msgMap.set('msg3', structuredClone(testMessages));
msgMap.get('msg3').msg3 = structuredClone(testMessages);
msgMap.set('manyMsgs', testMessages.manyMsgs);

describe('getMessage', () => {
Expand Down
14 changes: 7 additions & 7 deletions test/unit/org/authInfoTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import * as pathImport from 'node:path';
import * as dns from 'node:dns';
import * as jwt from 'jsonwebtoken';
import { cloneJson, env, includes } from '@salesforce/kit';
import { env, includes } from '@salesforce/kit';
import { spyMethod, stubMethod } from '@salesforce/ts-sinon';
import { AnyJson, getJsonMap, JsonMap, toJsonMap } from '@salesforce/ts-types';
import { expect } from 'chai';
Expand Down Expand Up @@ -335,7 +335,7 @@ describe('AuthInfo', () => {
loginUrl: testOrg.loginUrl,
privateKey: testOrg.privateKey,
};
const jwtConfigClone = cloneJson(jwtConfig);
const jwtConfigClone = structuredClone(jwtConfig);
const authResponse = {
access_token: testOrg.accessToken,
instance_url: testOrg.instanceUrl,
Expand Down Expand Up @@ -469,7 +469,7 @@ describe('AuthInfo', () => {
loginUrl: testOrg.loginUrl,
privateKey: testOrg.privateKey,
};
const jwtConfigClone = cloneJson(jwtConfig);
const jwtConfigClone = structuredClone(jwtConfig);
const authResponse = {
access_token: testOrg.accessToken,
instance_url: testOrg.instanceUrl,
Expand Down Expand Up @@ -535,7 +535,7 @@ describe('AuthInfo', () => {
refreshToken: testOrg.refreshToken,
loginUrl: testOrg.loginUrl,
};
const refreshTokenConfigClone = cloneJson(refreshTokenConfig);
const refreshTokenConfigClone = structuredClone(refreshTokenConfig);
const authResponse = {
access_token: testOrg.accessToken,
instance_url: testOrg.instanceUrl,
Expand Down Expand Up @@ -590,7 +590,7 @@ describe('AuthInfo', () => {
loginUrl: testOrg.loginUrl,
username: testOrg.username,
};
const refreshTokenConfigClone = cloneJson(refreshTokenConfig);
const refreshTokenConfigClone = structuredClone(refreshTokenConfig);
const authResponse = {
access_token: testOrg.accessToken,
instance_url: testOrg.instanceUrl,
Expand Down Expand Up @@ -643,7 +643,7 @@ describe('AuthInfo', () => {
refreshToken: testOrg.refreshToken,
loginUrl: testOrg.loginUrl,
};
const refreshTokenConfigClone = cloneJson(refreshTokenConfig);
const refreshTokenConfigClone = structuredClone(refreshTokenConfig);
const authResponse = {
access_token: testOrg.accessToken,
instance_url: testOrg.instanceUrl,
Expand Down Expand Up @@ -786,7 +786,7 @@ describe('AuthInfo', () => {
authCode: testOrg.authcode,
loginUrl: testOrg.loginUrl,
};
const authCodeConfigClone = cloneJson(authCodeConfig);
const authCodeConfigClone = structuredClone(authCodeConfig);
const authResponse = {
access_token: testOrg.accessToken,
instance_url: testOrg.instanceUrl,
Expand Down

2 comments on commit c20bba8

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: c20bba8 Previous: f7da5be Ratio
Child logger creation 470268 ops/sec (±0.81%) 462124 ops/sec (±2.31%) 0.98
Logging a string on root logger 787763 ops/sec (±8.22%) 806365 ops/sec (±8.36%) 1.02
Logging an object on root logger 601125 ops/sec (±6.11%) 665727 ops/sec (±8.17%) 1.11
Logging an object with a message on root logger 8442 ops/sec (±205.69%) 4412 ops/sec (±223.46%) 0.52
Logging an object with a redacted prop on root logger 379533 ops/sec (±22.05%) 463173 ops/sec (±7.75%) 1.22
Logging a nested 3-level object on root logger 396344 ops/sec (±6.72%) 381806 ops/sec (±8.38%) 0.96

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: c20bba8 Previous: f7da5be Ratio
Child logger creation 318206 ops/sec (±0.96%) 346610 ops/sec (±0.36%) 1.09
Logging a string on root logger 790067 ops/sec (±5.83%) 817086 ops/sec (±6.81%) 1.03
Logging an object on root logger 588717 ops/sec (±7.51%) 664432 ops/sec (±7.14%) 1.13
Logging an object with a message on root logger 5018 ops/sec (±208.40%) 2325 ops/sec (±243.20%) 0.46
Logging an object with a redacted prop on root logger 458424 ops/sec (±17.42%) 425573 ops/sec (±21.04%) 0.93
Logging a nested 3-level object on root logger 336817 ops/sec (±5.57%) 345871 ops/sec (±5.47%) 1.03

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.