Skip to content

Commit

Permalink
refactor: structuredClonePolyfill to helpers (#1250)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck authored Dec 28, 2024
1 parent 3d7f8e1 commit b93b1c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
import { readFileSync, writeSync } from 'fs'
import { extname, parse } from 'path'

export const structuredClonePolyfill: <T>(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" } })`
Expand Down
12 changes: 7 additions & 5 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -714,7 +720,3 @@ export class TreeBuilder {
}
}
}

const structuredClonePolyfill: <T>(value: T) => T = typeof structuredClone === 'function'
? structuredClone
: function (value) { return JSON.parse(JSON.stringify(value)) }

0 comments on commit b93b1c3

Please sign in to comment.