Examples

For Node

JavaScript

JavaScript as CommonJS

 1/*!
 2This file is part of CycloneDX JavaScript Library.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8   http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16SPDX-License-Identifier: Apache-2.0
17Copyright (c) OWASP Foundation. All Rights Reserved.
18*/
19
20/** Example how to serialize a Bom to JSON / XML. */
21
22const CDX = require('@cyclonedx/cyclonedx-library')
23// full Library is available as `CDX`, now
24
25const lFac = new CDX.Factories.LicenseFactory()
26const purlFac = new CDX.Factories.PackageUrlFactory('generic')
27
28const bom = new CDX.Models.Bom()
29bom.metadata.component = new CDX.Models.Component(
30  CDX.Enums.ComponentType.Application,
31  'MyProject'
32)
33bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
34
35const componentA = new CDX.Models.Component(
36  CDX.Enums.ComponentType.Library,
37  'myComponentA',
38  {
39    group: 'acme',
40    version: '1.33.7'
41  }
42)
43componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
44componentA.purl = purlFac.makeFromComponent(componentA)
45
46bom.components.add(componentA)
47bom.metadata.component.dependencies.add(componentA.bomRef)
48
49const serializeSpec = CDX.Spec.Spec1dot6
50
51const jsonSerializer = new CDX.Serialize.JsonSerializer(
52  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
53const serializedJson = jsonSerializer.serialize(bom)
54console.log(serializedJson)
55const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
56jsonValidator.validate(serializedJson)
57  .then(validationErrors => {
58    if (validationErrors === null) {
59      console.info('JSON valid')
60    } else {
61      throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
62    }
63  })
64  .catch(err => {
65    if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
66      console.info('JSON validation skipped:', err)
67    } else {
68      throw err
69    }
70  })
71
72const xmlSerializer = new CDX.Serialize.XmlSerializer(
73  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
74const serializedXML = xmlSerializer.serialize(bom)
75console.log(serializedXML)
76const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
77xmlValidator.validate(serializedXML)
78  .then(validationErrors => {
79    if (validationErrors === null) {
80      console.info('XML valid')
81    } else {
82      throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
83    }
84  })
85  .catch(err => {
86    if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
87      console.info('XML validation skipped:', err)
88    } else {
89      throw err
90    }
91  })

JavaScript as ECMAScript module

 1/*!
 2This file is part of CycloneDX JavaScript Library.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8   http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16SPDX-License-Identifier: Apache-2.0
17Copyright (c) OWASP Foundation. All Rights Reserved.
18*/
19
20/** Example how to serialize a Bom to JSON / XML. */
21
22import * as CDX from '@cyclonedx/cyclonedx-library'
23// full Library is available as `CDX`, now
24
25const lFac = new CDX.Factories.LicenseFactory()
26const purlFac = new CDX.Factories.PackageUrlFactory('generic')
27
28const bom = new CDX.Models.Bom()
29bom.metadata.component = new CDX.Models.Component(
30  CDX.Enums.ComponentType.Application,
31  'MyProject'
32)
33bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
34
35const componentA = new CDX.Models.Component(
36  CDX.Enums.ComponentType.Library,
37  'myComponentA',
38  {
39    group: 'acme',
40    version: '1.33.7'
41  }
42)
43componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
44componentA.purl = purlFac.makeFromComponent(componentA)
45
46bom.components.add(componentA)
47bom.metadata.component.dependencies.add(componentA.bomRef)
48
49const serializeSpec = CDX.Spec.Spec1dot6
50
51const jsonSerializer = new CDX.Serialize.JsonSerializer(
52  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
53const serializedJson = jsonSerializer.serialize(bom)
54console.log(serializedJson)
55const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
56try {
57  const validationErrors = await jsonValidator.validate(serializedJson)
58  if (validationErrors === null) {
59    console.info('JSON valid')
60  } else {
61    throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
62  }
63} catch (err) {
64  if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
65    console.info('JSON validation skipped:', err)
66  } else {
67    throw err
68  }
69}
70
71const xmlSerializer = new CDX.Serialize.XmlSerializer(
72  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
73const serializedXML = xmlSerializer.serialize(bom)
74console.log(serializedXML)
75const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
76try {
77  const validationErrors = await xmlValidator.validate(serializedXML)
78  if (validationErrors === null) {
79    console.info('XML valid')
80  } else {
81    throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
82  }
83} catch (err) {
84  if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
85    console.info('XML validation skipped:', err)
86  } else {
87    throw err
88  }
89}

TypeScript

TypeScript for CommonJS

 1/*!
 2This file is part of CycloneDX JavaScript Library.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8   http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16SPDX-License-Identifier: Apache-2.0
17Copyright (c) OWASP Foundation. All Rights Reserved.
18*/
19
20/** Example how to serialize a Bom to JSON / XML. */
21
22import * as CDX from '@cyclonedx/cyclonedx-library'
23// full Library is available as `CDX`, now
24
25const lFac = new CDX.Factories.LicenseFactory()
26const purlFac = new CDX.Factories.PackageUrlFactory('generic')
27
28const bom = new CDX.Models.Bom()
29bom.metadata.component = new CDX.Models.Component(
30  CDX.Enums.ComponentType.Application,
31  'MyProject'
32)
33bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
34
35const componentA = new CDX.Models.Component(
36  CDX.Enums.ComponentType.Library,
37  'myComponentA',
38  {
39    group: 'acme',
40    version: '1.33.7'
41  }
42)
43componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
44componentA.purl = purlFac.makeFromComponent(componentA)
45
46bom.components.add(componentA)
47bom.metadata.component.dependencies.add(componentA.bomRef)
48
49const serializeSpec = CDX.Spec.Spec1dot6
50
51const jsonSerializer = new CDX.Serialize.JsonSerializer(
52  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
53const serializedJson = jsonSerializer.serialize(bom)
54console.log(serializedJson)
55const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
56jsonValidator.validate(serializedJson)
57  .then(validationErrors => {
58    if (validationErrors === null) {
59      console.info('JSON valid')
60    } else {
61      throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
62    }
63  })
64  .catch(err => {
65    if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
66      console.info('JSON validation skipped:', err)
67    } else {
68      throw err
69    }
70  })
71
72const xmlSerializer = new CDX.Serialize.XmlSerializer(
73  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
74const serializedXML = xmlSerializer.serialize(bom)
75console.log(serializedXML)
76const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
77xmlValidator.validate(serializedXML)
78  .then(validationErrors => {
79    if (validationErrors === null) {
80      console.info('XML valid')
81    } else {
82      throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
83    }
84  })
85  .catch(err => {
86    if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
87      console.info('XML validation skipped:', err)
88    } else {
89      throw err
90    }
91  })

TypeScript for ECMAScript module

 1/*!
 2This file is part of CycloneDX JavaScript Library.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8   http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16SPDX-License-Identifier: Apache-2.0
17Copyright (c) OWASP Foundation. All Rights Reserved.
18*/
19
20/** Example how to serialize a Bom to JSON / XML. */
21
22import * as CDX from '@cyclonedx/cyclonedx-library'
23// full Library is available as `CDX`, now
24
25const lFac = new CDX.Factories.LicenseFactory()
26const purlFac = new CDX.Factories.PackageUrlFactory('generic')
27
28const bom = new CDX.Models.Bom()
29bom.metadata.component = new CDX.Models.Component(
30  CDX.Enums.ComponentType.Application,
31  'MyProject'
32)
33bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
34
35const componentA = new CDX.Models.Component(
36  CDX.Enums.ComponentType.Library,
37  'myComponentA',
38  {
39    group: 'acme',
40    version: '1.33.7'
41  }
42)
43componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
44componentA.purl = purlFac.makeFromComponent(componentA)
45
46bom.components.add(componentA)
47bom.metadata.component.dependencies.add(componentA.bomRef)
48
49const serializeSpec = CDX.Spec.Spec1dot6
50
51const jsonSerializer = new CDX.Serialize.JsonSerializer(
52  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
53const serializedJson = jsonSerializer.serialize(bom)
54console.log(serializedJson)
55const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
56try {
57  const validationErrors = await jsonValidator.validate(serializedJson)
58  if (validationErrors === null) {
59    console.info('JSON valid')
60  } else {
61    throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
62  }
63} catch (err) {
64  if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
65    console.info('JSON validation skipped:', err)
66  } else {
67    throw err
68  }
69}
70
71const xmlSerializer = new CDX.Serialize.XmlSerializer(
72  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
73const serializedXML = xmlSerializer.serialize(bom)
74console.log(serializedXML)
75const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
76try {
77  const validationErrors = await xmlValidator.validate(serializedXML)
78  if (validationErrors === null) {
79    console.info('XML valid')
80  } else {
81    throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
82  }
83} catch (err) {
84  if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
85    console.info('XML validation skipped:', err)
86  } else {
87    throw err
88  }
89}

For Web

With Parcel

 1/*!
 2This file is part of CycloneDX JavaScript Library.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8   http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16SPDX-License-Identifier: Apache-2.0
17Copyright (c) OWASP Foundation. All Rights Reserved.
18*/
19
20/** Example how to serialize a Bom to JSON / XML. */
21
22const CDX = require('@cyclonedx/cyclonedx-library')
23// full Library is available as `CDX`, now
24
25const lFac = new CDX.Factories.LicenseFactory()
26const purlFac = new CDX.Factories.PackageUrlFactory('generic')
27
28const bom = new CDX.Models.Bom()
29bom.metadata.component = new CDX.Models.Component(
30  CDX.Enums.ComponentType.Application,
31  'MyProject'
32)
33bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
34
35const componentA = new CDX.Models.Component(
36  CDX.Enums.ComponentType.Library,
37  'myComponentA',
38  {
39    group: 'acme',
40    version: '1.33.7'
41  }
42)
43componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
44componentA.purl = purlFac.makeFromComponent(componentA)
45
46bom.components.add(componentA)
47bom.metadata.component.dependencies.add(componentA.bomRef)
48
49const serializeSpec = CDX.Spec.Spec1dot6
50
51const jsonSerializer = new CDX.Serialize.JsonSerializer(
52  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
53const serializedJson = jsonSerializer.serialize(bom)
54console.log(serializedJson)
55
56const xmlSerializer = new CDX.Serialize.XmlSerializer(
57  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
58const serializedXML = xmlSerializer.serialize(bom)
59console.log(serializedXML)

With webpack

 1/*!
 2This file is part of CycloneDX JavaScript Library.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8   http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16SPDX-License-Identifier: Apache-2.0
17Copyright (c) OWASP Foundation. All Rights Reserved.
18*/
19
20/** Example how to serialize a Bom to JSON / XML. */
21
22const CDX = require('@cyclonedx/cyclonedx-library')
23// full Library is available as `CDX`, now
24
25const lFac = new CDX.Factories.LicenseFactory()
26const purlFac = new CDX.Factories.PackageUrlFactory('generic')
27
28const bom = new CDX.Models.Bom()
29bom.metadata.component = new CDX.Models.Component(
30  CDX.Enums.ComponentType.Application,
31  'MyProject'
32)
33bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
34
35const componentA = new CDX.Models.Component(
36  CDX.Enums.ComponentType.Library,
37  'myComponentA',
38  {
39    group: 'acme',
40    version: '1.33.7'
41  }
42)
43componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
44componentA.purl = purlFac.makeFromComponent(componentA)
45
46bom.components.add(componentA)
47bom.metadata.component.dependencies.add(componentA.bomRef)
48
49const serializeSpec = CDX.Spec.Spec1dot6
50
51const jsonSerializer = new CDX.Serialize.JsonSerializer(
52  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
53const serializedJson = jsonSerializer.serialize(bom)
54console.log(serializedJson)
55
56const xmlSerializer = new CDX.Serialize.XmlSerializer(
57  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
58const serializedXML = xmlSerializer.serialize(bom)
59console.log(serializedXML)