From b93b1c3b470551b5fbd39651475879d98aa518cb Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Sat, 28 Dec 2024 12:41:35 +0100 Subject: [PATCH] refactor: `structuredClonePolyfill` to helpers (#1250) Signed-off-by: Jan Kowalleck --- src/_helpers.ts | 4 ++++ src/builders.ts | 12 +++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/_helpers.ts b/src/_helpers.ts index bed426489..029a9237a 100644 --- a/src/_helpers.ts +++ b/src/_helpers.ts @@ -20,6 +20,10 @@ Copyright (c) OWASP Foundation. All Rights Reserved. import { readFileSync, writeSync } from 'fs' import { extname, parse } from 'path' +export const structuredClonePolyfill: (value: T) => T = typeof structuredClone === 'function' + ? structuredClone + : function (value) { return JSON.parse(JSON.stringify(value)) } + export function loadJsonFile (path: string): any { return JSON.parse(readFileSync(path, 'utf8')) // may be replaced by `require(f, { with: { type: "json" } })` diff --git a/src/builders.ts b/src/builders.ts index e64d38cb5..d71b1f770 100644 --- a/src/builders.ts +++ b/src/builders.ts @@ -23,7 +23,13 @@ import * as normalizePackageData from 'normalize-package-data' import * as path from 'path' import { join } from 'path' -import { getMimeForLicenseFile, isString, loadJsonFile, tryRemoveSecretsFromUrl } from './_helpers' +import { + getMimeForLicenseFile, + isString, + loadJsonFile, + structuredClonePolyfill, + tryRemoveSecretsFromUrl +} from './_helpers' import { makeNpmRunner, type runFunc } from './npmRunner' import { PropertyNames, PropertyValueBool } from './properties' import { versionCompare } from './versionCompare' @@ -714,7 +720,3 @@ export class TreeBuilder { } } } - -const structuredClonePolyfill: (value: T) => T = typeof structuredClone === 'function' - ? structuredClone - : function (value) { return JSON.parse(JSON.stringify(value)) }