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// Alternative for better tree-shaking on bundling, import only the needed symbols like so:
25//    const { Bom, Component } = require('@cyclonedx/cyclonedx-library/Models')
26//    const { ComponentType } = require('@cyclonedx/cyclonedx-library/Enums')
27
28const lFac = new CDX.Contrib.License.Factories.LicenseFactory()
29const purlFac = new CDX.Contrib.PackageUrl.Factories.PackageUrlFactory('generic')
30
31const bom = new CDX.Models.Bom()
32bom.metadata.component = new CDX.Models.Component(
33  CDX.Enums.ComponentType.Application,
34  'MyProject'
35)
36bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
37
38const componentA = new CDX.Models.Component(
39  CDX.Enums.ComponentType.Library,
40  'myComponentA',
41  {
42    group: 'acme',
43    version: '1.33.7'
44  }
45)
46componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
47componentA.purl = purlFac.makeFromComponent(componentA)
48
49bom.components.add(componentA)
50bom.metadata.component.dependencies.add(componentA.bomRef)
51
52const serializeSpec = CDX.Spec.Spec1dot7
53
54const jsonSerializer = new CDX.Serialize.JsonSerializer(
55  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
56const serializedJson = jsonSerializer.serialize(bom)
57console.log(serializedJson)
58const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
59jsonValidator.validate(serializedJson)
60  .then(validationErrors => {
61    if (validationErrors === null) {
62      console.info('JSON valid')
63    } else {
64      throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
65    }
66  })
67  .catch(err => {
68    if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
69      console.info('JSON validation skipped:', err)
70    } else {
71      throw err
72    }
73  })
74
75const xmlSerializer = new CDX.Serialize.XmlSerializer(
76  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
77const serializedXML = xmlSerializer.serialize(bom)
78console.log(serializedXML)
79const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
80xmlValidator.validate(serializedXML)
81  .then(validationErrors => {
82    if (validationErrors === null) {
83      console.info('XML valid')
84    } else {
85      throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
86    }
87  })
88  .catch(err => {
89    if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
90      console.info('XML validation skipped:', err)
91    } else {
92      throw err
93    }
94  })

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// Alternative for better tree-shaking on bundling, import only the needed symbols like so:
25//    import { Bom, Component } from '@cyclonedx/cyclonedx-library/Models'
26//    import { ComponentType } from '@cyclonedx/cyclonedx-library/Enums'
27
28const lFac = new CDX.Contrib.License.Factories.LicenseFactory()
29const purlFac = new CDX.Contrib.PackageUrl.Factories.PackageUrlFactory('generic')
30
31const bom = new CDX.Models.Bom()
32bom.metadata.component = new CDX.Models.Component(
33  CDX.Enums.ComponentType.Application,
34  'MyProject'
35)
36bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
37
38const componentA = new CDX.Models.Component(
39  CDX.Enums.ComponentType.Library,
40  'myComponentA',
41  {
42    group: 'acme',
43    version: '1.33.7'
44  }
45)
46componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
47componentA.purl = purlFac.makeFromComponent(componentA)
48
49bom.components.add(componentA)
50bom.metadata.component.dependencies.add(componentA.bomRef)
51
52const serializeSpec = CDX.Spec.Spec1dot7
53
54const jsonSerializer = new CDX.Serialize.JsonSerializer(
55  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
56const serializedJson = jsonSerializer.serialize(bom)
57console.log(serializedJson)
58const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
59try {
60  const validationErrors = await jsonValidator.validate(serializedJson)
61  if (validationErrors === null) {
62    console.info('JSON valid')
63  } else {
64    throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
65  }
66} catch (err) {
67  if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
68    console.info('JSON validation skipped:', err)
69  } else {
70    throw err
71  }
72}
73
74const xmlSerializer = new CDX.Serialize.XmlSerializer(
75  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
76const serializedXML = xmlSerializer.serialize(bom)
77console.log(serializedXML)
78const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
79try {
80  const validationErrors = await xmlValidator.validate(serializedXML)
81  if (validationErrors === null) {
82    console.info('XML valid')
83  } else {
84    throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
85  }
86} catch (err) {
87  if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
88    console.info('XML validation skipped:', err)
89  } else {
90    throw err
91  }
92}

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// Alternative for better tree-shaking on bundling, import only the needed symbols like so:
25//    import { Bom, Component } from '@cyclonedx/cyclonedx-library/Models'
26//    import { ComponentType } from '@cyclonedx/cyclonedx-library/Enums'
27
28const lFac = new CDX.Contrib.License.Factories.LicenseFactory()
29const purlFac = new CDX.Contrib.PackageUrl.Factories.PackageUrlFactory('generic')
30
31const bom = new CDX.Models.Bom()
32bom.metadata.component = new CDX.Models.Component(
33  CDX.Enums.ComponentType.Application,
34  'MyProject'
35)
36bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
37
38const componentA = new CDX.Models.Component(
39  CDX.Enums.ComponentType.Library,
40  'myComponentA',
41  {
42    group: 'acme',
43    version: '1.33.7'
44  }
45)
46componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
47componentA.purl = purlFac.makeFromComponent(componentA)
48
49bom.components.add(componentA)
50bom.metadata.component.dependencies.add(componentA.bomRef)
51
52const serializeSpec = CDX.Spec.Spec1dot7
53
54const jsonSerializer = new CDX.Serialize.JsonSerializer(
55  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
56const serializedJson = jsonSerializer.serialize(bom)
57console.log(serializedJson)
58const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
59jsonValidator.validate(serializedJson)
60  .then(validationErrors => {
61    if (validationErrors === null) {
62      console.info('JSON valid')
63    } else {
64      throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
65    }
66  })
67  .catch(err => {
68    if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
69      console.info('JSON validation skipped:', err)
70    } else {
71
72      throw err
73    }
74  })
75
76const xmlSerializer = new CDX.Serialize.XmlSerializer(
77  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
78const serializedXML = xmlSerializer.serialize(bom)
79console.log(serializedXML)
80const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
81xmlValidator.validate(serializedXML)
82  .then(validationErrors => {
83    if (validationErrors === null) {
84      console.info('XML valid')
85    } else {
86      throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
87    }
88  })
89  .catch(err => {
90    if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
91      console.info('XML validation skipped:', err)
92    } else {
93
94      throw err
95    }
96  })

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// Alternative for better tree-shaking on bundling, import only the needed symbols like so:
25//    import { Bom, Component } from '@cyclonedx/cyclonedx-library/Models'
26//    import { ComponentType } from '@cyclonedx/cyclonedx-library/Enums'
27
28const lFac = new CDX.Contrib.License.Factories.LicenseFactory()
29const purlFac = new CDX.Contrib.PackageUrl.Factories.PackageUrlFactory('generic')
30
31const bom = new CDX.Models.Bom()
32bom.metadata.component = new CDX.Models.Component(
33  CDX.Enums.ComponentType.Application,
34  'MyProject'
35)
36bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
37
38const componentA = new CDX.Models.Component(
39  CDX.Enums.ComponentType.Library,
40  'myComponentA',
41  {
42    group: 'acme',
43    version: '1.33.7'
44  }
45)
46componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
47componentA.purl = purlFac.makeFromComponent(componentA)
48
49bom.components.add(componentA)
50bom.metadata.component.dependencies.add(componentA.bomRef)
51
52const serializeSpec = CDX.Spec.Spec1dot7
53
54const jsonSerializer = new CDX.Serialize.JsonSerializer(
55  new CDX.Serialize.JSON.Normalize.Factory(serializeSpec))
56const serializedJson = jsonSerializer.serialize(bom)
57console.log(serializedJson)
58const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
59try {
60  /* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- intended */
61  const validationErrors = await jsonValidator.validate(serializedJson)
62  if (validationErrors === null) {
63    console.info('JSON valid')
64  } else {
65    throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
66  }
67} catch (err) {
68  if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
69    console.info('JSON validation skipped:', err)
70  } else {
71    throw err
72  }
73}
74
75const xmlSerializer = new CDX.Serialize.XmlSerializer(
76  new CDX.Serialize.XML.Normalize.Factory(serializeSpec))
77const serializedXML = xmlSerializer.serialize(bom)
78console.log(serializedXML)
79const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
80try {
81  /* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- intended */
82  const validationErrors = await xmlValidator.validate(serializedXML)
83  if (validationErrors === null) {
84    console.info('XML valid')
85  } else {
86    throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
87  }
88} catch (err) {
89  if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
90    console.info('XML validation skipped:', err)
91  } else {
92    throw err
93  }
94}

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.Contrib.License.Factories.LicenseFactory()
26const purlFac = new CDX.Contrib.PackageUrl.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.Spec1dot7
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.Contrib.License.Factories.LicenseFactory()
26const purlFac = new CDX.Contrib.PackageUrl.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.Spec1dot7
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)