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: refactor internal libs #1083

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
refactor: refactor internal libs
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Jun 3, 2024
commit 85eb714127f27636da02c149d0bd617e3ad43951
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,20 +20,19 @@
const assert = require('assert')
const { suite, test } = require('mocha')

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

Check failure on line 23 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Multiple spaces found before '='. (no-multi-spaces)

suite('libs/universal-node-xml', () => {
suite('stringify', () => {
suite('libs/universal-node-xml/stringify', () => {
const dummyElem = Object.freeze({

Check failure on line 26 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 2 spaces but found 4. (indent)
type: 'element',

Check failure on line 27 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 4 spaces but found 6. (indent)
name: 'foo'

Check failure on line 28 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 4 spaces but found 6. (indent)
})

Check failure on line 29 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 2 spaces but found 4. (indent)
const dummyElemStringifiedRE = /<foo(:?\/>|><\/foo>)/

Check failure on line 30 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 2 spaces but found 4. (indent)

if (stringify.fails) {

Check failure on line 32 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 2 spaces but found 4. (indent)
test('call should fail/throw', () => {

Check failure on line 33 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 4 spaces but found 6. (indent)
assert.throws(

Check failure on line 34 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 6 spaces but found 8. (indent)
() => {

Check failure on line 35 in libs/universal-node-xml/stringify.test.js

View workflow job for this annotation

GitHub Actions / test standard

Expected indentation of 8 spaces but found 10. (indent)
stringify(dummyElem)
},
(err) => {
Expand All @@ -49,5 +48,4 @@
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
Loading