Skip to content

Commit

Permalink
refactor: refactor internal libs (#1083)
Browse files Browse the repository at this point in the history
refactoring internal libs, in preparation of possible additions in the
field

---------

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Jun 3, 2024
1 parent 038352e commit df53819
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,5 @@ declare interface ThrowError {
}

declare type Stringify = (element: SimpleXml.Element, options?: SerializerOptions) => string
export declare const stringify: Stringify | ThrowError

/*
declare type Parse = (xml: string) => SimpleXml.Element
export declare const parse: Parse | ThrowError
*/
declare const stringify: Stringify | ThrowError
export = stringify
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
* @type {[string, function():(Function|*)][]}
*/
const possibleStringifiers = [
['xmlbuilder2', () => require('./stringifiers/xmlbuilder2')]
['xmlbuilder2', () => require('./__stringifiers/xmlbuilder2')]
// ... add others here, pull-requests welcome!
]
/* eslint-enable jsdoc/valid-types */

module.exports.stringify = function () {
module.exports = function () {
throw new Error(
'No stringifier available.' +
' Please install any of the optional dependencies: ' +
possibleStringifiers.map(kv => kv[0]).join(', ')
)
}
module.exports.stringify.fails = true
module.exports.fails = true

for (const [, getStringifier] of possibleStringifiers) {
try {
const possibleStringifier = getStringifier()
if (typeof possibleStringifier === 'function') {
module.exports.stringify = possibleStringifier
module.exports = possibleStringifier
break
}
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,32 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
const assert = require('assert')
const { suite, test } = require('mocha')

const { stringify } = require('./')
const stringify = require('./stringify')

suite('libs/universal-node-xml', () => {
suite('stringify', () => {
const dummyElem = Object.freeze({
type: 'element',
name: 'foo'
})
const dummyElemStringifiedRE = /<foo(:?\/>|><\/foo>)/

if (stringify.fails) {
test('call should fail/throw', () => {
assert.throws(
() => {
stringify(dummyElem)
},
(err) => {
assert.ok(err instanceof Error)
assert.match(err.message, /no stringifier available/i)
return true
}
)
})
} else {
test('call should pass', () => {
const stringified = stringify(dummyElem)
assert.match(stringified, dummyElemStringifiedRE)
})
}
suite('libs/universal-node-xml/stringify', () => {
const dummyElem = Object.freeze({
type: 'element',
name: 'foo'
})
const dummyElemStringifiedRE = /<foo(:?\/>|><\/foo>)/

if (stringify.fails) {
test('call should fail/throw', () => {
assert.throws(
() => {
stringify(dummyElem)
},
(err) => {
assert.ok(err instanceof Error)
assert.match(err.message, /no stringifier available/i)
return true
}
)
})
} else {
test('call should pass', () => {
const stringified = stringify(dummyElem)
assert.match(stringified, dummyElemStringifiedRE)
})
}
})
2 changes: 1 addition & 1 deletion src/serialize/xmlSerializer.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import { stringify } from '../../libs/universal-node-xml'
import * as stringify from '../../libs/universal-node-xml/stringify'
import type { SerializerOptions } from './types'
import type { SimpleXml } from './xml/types'
import { XmlBaseSerializer } from './xmlBaseSerializer'
Expand Down

0 comments on commit df53819

Please sign in to comment.