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

refactor: pluggable optional deps funcs centralized #1084

Merged
merged 45 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
1596a82
refactor: convert to TS
jkowalleck Jun 4, 2024
6168be2
migrate pluggable xml validator
jkowalleck Jun 4, 2024
f683ad0
wip
jkowalleck Jun 4, 2024
0cabf9e
wip
jkowalleck Jun 4, 2024
f436e4b
wip
jkowalleck Jun 4, 2024
58be395
wip
jkowalleck Jun 4, 2024
01a9cbf
docs
jkowalleck Jun 4, 2024
e1cbc79
wip
jkowalleck Jun 5, 2024
15e1aa3
fix json schema 1.2
jkowalleck Jun 5, 2024
9135e7b
wip
jkowalleck Jun 5, 2024
a23c597
wip
jkowalleck Jun 5, 2024
7f60354
wip
jkowalleck Jun 5, 2024
ecbddce
docs
jkowalleck Jun 5, 2024
2ef6d30
refactor
jkowalleck Jun 5, 2024
aa0798e
tidy
jkowalleck Jun 5, 2024
d26a611
tidy
jkowalleck Jun 5, 2024
3d30e5e
tidy
jkowalleck Jun 5, 2024
ba8da8b
fix
jkowalleck Jun 5, 2024
8ea81d6
tests
jkowalleck Jun 5, 2024
a571fa6
tests
jkowalleck Jun 5, 2024
a725e8f
tests
jkowalleck Jun 5, 2024
640f3c7
ci
jkowalleck Jun 5, 2024
ab3972a
ci
jkowalleck Jun 5, 2024
a6b6feb
ci
jkowalleck Jun 5, 2024
e463121
dings
jkowalleck Jun 5, 2024
87116f9
tidy
jkowalleck Jun 5, 2024
e10e52c
ci
jkowalleck Jun 5, 2024
e0103c4
ci
jkowalleck Jun 5, 2024
441bdc9
ci
jkowalleck Jun 5, 2024
fed8af0
tests
jkowalleck Jun 5, 2024
6f0b285
tests
jkowalleck Jun 5, 2024
ee7c22e
tests
jkowalleck Jun 5, 2024
c08e1c5
fix
jkowalleck Jun 5, 2024
c1136d3
fix
jkowalleck Jun 5, 2024
2ed07ab
types
jkowalleck Jun 5, 2024
e867191
docs
jkowalleck Jun 5, 2024
7ba6c80
docs
jkowalleck Jun 5, 2024
4d8ead5
docs
jkowalleck Jun 5, 2024
6366556
docs
jkowalleck Jun 5, 2024
0d71a47
types
jkowalleck Jun 5, 2024
c3a7dec
tidy
jkowalleck Jun 5, 2024
ec95498
tidy
jkowalleck Jun 5, 2024
36cbb6d
tidy
jkowalleck Jun 6, 2024
528c0a2
tidy
jkowalleck Jun 6, 2024
359b5b5
tidy
jkowalleck Jun 6, 2024
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
3 changes: 1 addition & 2 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
module.exports = {
timeout: 10000,
spec: [
'tests',
'libs'
'tests'
],
recursive: true,
parallel: false, // if true, then some IDEs cannot run it
Expand Down
129 changes: 0 additions & 129 deletions libs/universal-node-xml/__stringifiers/xmlbuilder2.spec.js

This file was deleted.

52 changes: 0 additions & 52 deletions libs/universal-node-xml/stringify.js

This file was deleted.

53 changes: 53 additions & 0 deletions src/_optPlug.node/_wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*!
This file is part of CycloneDX JavaScript Library.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import { OptPlugError } from './errors'

type WillThrow = (() => never) & { fails: true }
type WillNotFailRightAway<T> = Omit<T, 'fails'>
type PossibleFunctionalities<Functionality> = Array<[string, () => Functionality | undefined]>

function makeWIllThrow (message: string): WillThrow {
const f: WillThrow = function (): never {
throw new OptPlugError(message)
}
f.fails = true
return Object.freeze(f)
}

export default function <Functionality extends WillNotFailRightAway<Functionality>> (
name: string,
pf: PossibleFunctionalities<Functionality>
): Functionality | WillThrow {
for (const [, getF] of pf) {
try {
const f = getF()
if (f !== undefined) {
return f
}
} catch {
/* pass */
}
}
return makeWIllThrow(
`No ${name} available.` +
' Please install any of the optional dependencies: ' +
pf.map(kv => kv[0]).join(' || ')
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import type { SerializerOptions } from '../../src/serialize/types'
import type { SimpleXml } from '../../src/serialize/xml/types'
export class OptPlugError extends Error {
readonly cause: any | undefined

declare interface ThrowError {
/** @throws {@link Error} */
(..._: any[]): never
fails: true
constructor (message: string, cause?: any) {
super(message)
this.cause = cause
}
}

declare type Stringify = (element: SimpleXml.Element, options?: SerializerOptions) => string
declare const stringify: Stringify | ThrowError
export = stringify
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,22 @@ SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

const { create } = require('xmlbuilder2')
const { getNS, makeIndent } = require('./_helpers')
import { create } from 'xmlbuilder2'
import type { XMLBuilder } from 'xmlbuilder2/lib/interfaces'

module.exports = typeof create === 'function'
? stringify
/* c8 ignore next */
: undefined
import type { SerializerOptions } from '../../../serialize/types'
import type { SimpleXml } from '../../../serialize/xml/types'
import type { Functionality } from '../'

/* eslint-disable jsdoc/valid-types */

/**
* @typedef {import('xmlbuilder2/lib/interfaces').XMLBuilder} XMLBuilder
*/

/**
* @typedef {import('../../../src/serialize/xml/types').SimpleXml.Element} Element
*/

/* eslint-enable jsdoc/valid-types */
if (typeof create !== 'function') {
throw new Error('`create` is not a function')
}

/**
* @param {Element} rootElement
* @param {string|number|undefined} [space]
* @return {string}
*/
function stringify (rootElement, { space } = {}) {
/** @internal */
export default (function (
rootElement: SimpleXml.Element,
{ space }: SerializerOptions = {}
): string {
const indent = makeIndent(space)
const doc = create({ encoding: 'UTF-8' })
addEle(doc, rootElement)
Expand All @@ -52,15 +42,16 @@ function stringify (rootElement, { space } = {}) {
prettyPrint: indent.length > 0,
indent
})
}
}) satisfies Functionality

/**
* @param {XMLBuilder} parent
* @param {Element} element
* @param {?string} [parentNS]
*/
function addEle (parent, element, parentNS = null) {
if (element.type !== 'element') { return }
function addEle (
parent: XMLBuilder,
element: SimpleXml.Element | SimpleXml.Comment,
parentNS: string | null = null
): void {
if (element.type !== 'element') {
return
}
const ns = getNS(element) ?? parentNS
const ele = parent.ele(ns, element.name, element.attributes)
if (element.children === undefined) {
Expand All @@ -73,3 +64,20 @@ function addEle (parent, element, parentNS = null) {
}
}
}

function getNS (element: SimpleXml.Element): string | null {
const ns = (element.namespace ?? element.attributes?.xmlns)?.toString() ?? ''
return ns.length > 0
? ns
: null
}

function makeIndent (space: string | number | any): string {
if (typeof space === 'number') {
return ' '.repeat(Math.max(0, space))
}
if (typeof space === 'string') {
return space
}
return ''
}
Loading
Loading