From 493e7bf774e274be33257afb2b742c022e9f5664 Mon Sep 17 00:00:00 2001 From: LoneRifle Date: Sat, 15 Jul 2023 11:16:40 +0800 Subject: [PATCH 1/2] Limit imports from `./types` to type where applicable --- src/c14n-canonicalization.ts | 2 +- src/enveloped-signature.ts | 2 +- src/exclusive-canonicalization.ts | 2 +- src/hash-algorithms.ts | 2 +- src/signature-algorithms.ts | 2 +- src/signed-xml.ts | 2 +- src/utils.ts | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/c14n-canonicalization.ts b/src/c14n-canonicalization.ts index 7625217a..03406145 100644 --- a/src/c14n-canonicalization.ts +++ b/src/c14n-canonicalization.ts @@ -1,4 +1,4 @@ -import { +import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions, NamespacePrefix, diff --git a/src/enveloped-signature.ts b/src/enveloped-signature.ts index ab898bd4..80a0a553 100644 --- a/src/enveloped-signature.ts +++ b/src/enveloped-signature.ts @@ -1,6 +1,6 @@ import * as xpath from "xpath"; -import { +import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions, CanonicalizationOrTransformAlgorithmType, diff --git a/src/exclusive-canonicalization.ts b/src/exclusive-canonicalization.ts index 06eea343..4f76c83a 100644 --- a/src/exclusive-canonicalization.ts +++ b/src/exclusive-canonicalization.ts @@ -1,4 +1,4 @@ -import { +import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions, NamespacePrefix, diff --git a/src/hash-algorithms.ts b/src/hash-algorithms.ts index 56f6c896..eeeb8b27 100644 --- a/src/hash-algorithms.ts +++ b/src/hash-algorithms.ts @@ -1,5 +1,5 @@ import * as crypto from "crypto"; -import { HashAlgorithm } from "./types"; +import type { HashAlgorithm } from "./types"; export class Sha1 implements HashAlgorithm { getHash = function (xml) { diff --git a/src/signature-algorithms.ts b/src/signature-algorithms.ts index 71a23ed8..46abe0eb 100644 --- a/src/signature-algorithms.ts +++ b/src/signature-algorithms.ts @@ -1,5 +1,5 @@ import * as crypto from "crypto"; -import { SignatureAlgorithm, createOptionalCallbackFunction } from "./types"; +import { type SignatureAlgorithm, createOptionalCallbackFunction } from "./types"; export class RsaSha1 implements SignatureAlgorithm { getSignature = createOptionalCallbackFunction( diff --git a/src/signed-xml.ts b/src/signed-xml.ts index b2c0ca0f..072201ff 100644 --- a/src/signed-xml.ts +++ b/src/signed-xml.ts @@ -1,4 +1,4 @@ -import { +import type { CanonicalizationAlgorithmType, CanonicalizationOrTransformationAlgorithm, ComputeSignatureOptions, diff --git a/src/utils.ts b/src/utils.ts index e58a95d3..c66a4ac1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import * as xpath from "xpath"; -import { NamespacePrefix } from "./types"; +import type { NamespacePrefix } from "./types"; export function isArrayHasLength(array: unknown): array is unknown[] { return Array.isArray(array) && array.length > 0; From c9ba5b614df5ea766f9afc2b6b9ab65c5af457db Mon Sep 17 00:00:00 2001 From: LoneRifle Date: Sat, 15 Jul 2023 11:21:47 +0800 Subject: [PATCH 2/2] findAncestorNs: replace type coercion with type guard --- src/utils.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index c66a4ac1..2bb860a8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -207,6 +207,10 @@ function findNSPrefix(subset) { return subset.prefix || ""; } +function isElementSubset(docSubset: Node[]): docSubset is Element[] { + return docSubset.every((node) => xpath.isElement(node)) +} + /** * Extract ancestor namespaces in order to import it to root of document subset * which is being canonicalized for non-exclusive c14n. @@ -222,20 +226,17 @@ export function findAncestorNs( namespaceResolver?: XPathNSResolver ) { const docSubset = xpath.selectWithResolver(docSubsetXpath, doc, namespaceResolver); - let elementSubset: Element[] = []; if (!isArrayHasLength(docSubset)) { return []; } - if (!docSubset.every((node) => xpath.isElement(node))) { + if (!isElementSubset(docSubset)) { throw new Error("Document subset must be list of elements"); - } else { - elementSubset = docSubset as Element[]; } // Remove duplicate on ancestor namespace - const ancestorNs = collectAncestorNamespaces(elementSubset[0]); + const ancestorNs = collectAncestorNamespaces(docSubset[0]); const ancestorNsWithoutDuplicate: NamespacePrefix[] = []; for (let i = 0; i < ancestorNs.length; i++) { let notOnTheList = true;