Skip to content

Commit

Permalink
Replace xml2js with fast-xml-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Kumer authored and janybravo committed Aug 19, 2024
1 parent 9f9a014 commit c51adff
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 58 deletions.
96 changes: 56 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
"@turf/area": "^6.0.1",
"@turf/helpers": "^6.1.4",
"@types/proj4": "^2.5.2",
"@types/xml2js": "^0.4.4",
"axios": "^0.21.1",
"fast-xml-parser": "^4.4.1",
"moment": "^2.24.0",
"polygon-clipping": "^0.14.3",
"proj4": "^2.9.0",
"query-string": "^6.4.2",
"terraformer-wkt-parser": "^1.2.1",
"xml2js": "^0.4.19"
"terraformer-wkt-parser": "^1.2.1"
},
"engines": {
"node": ">=18"
Expand All @@ -30,7 +29,6 @@
"@types/jest": "^25.1.3",
"@types/node-fetch": "^2.5.7",
"@types/service-worker-mock": "^2.0.4",
"@types/xml2js": "^0.4.4",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"axios-mock-adapter": "^1.18.1",
Expand Down
11 changes: 11 additions & 0 deletions src/layer/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,14 @@ export const PLANET_FALSE_COLOR_TEMPLATES = [
export const EQUATOR_RADIUS = 6378137.0;
export const DEGREE_TO_RADIAN = Math.PI / 180;
export const RADIAN_TO_DEGREE = 180 / Math.PI;

export const XmlParserOptions = Object.freeze({
attributesGroupName: '$',
attributeNamePrefix: '',
textNodeName: '_',
ignoreAttributes: false,
isArray: (name: string, jpath: string, isLeafNode: boolean, isAttribute: boolean) => {
const isA = !isAttribute && !['Capabilities', 'WMS_Capabilities', 'WMT_MS_Capabilities'].includes(name);
return isA;
},
});
19 changes: 14 additions & 5 deletions src/layer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import axios, { AxiosRequestConfig } from 'axios';
import { stringify, parseUrl, stringifyUrl } from 'query-string';
import { parseStringPromise } from 'xml2js';
import { EQUATOR_RADIUS, OgcServiceTypes, SH_SERVICE_HOSTNAMES_V3, SH_SERVICE_ROOT_URL } from './const';

import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
import { CACHE_CONFIG_30MIN, CACHE_CONFIG_30MIN_MEMORY } from '../utils/cacheHandlers';
import type { GetCapabilitiesWmtsXml } from './wmts.utils';
import { XMLParser } from 'fast-xml-parser';
import proj4 from 'proj4';
import { getAuthToken } from '../auth';
import { BBox } from '../bbox';
import { CRS_EPSG3857 } from '../crs';
import proj4 from 'proj4';
import {
EQUATOR_RADIUS,
OgcServiceTypes,
SH_SERVICE_HOSTNAMES_V3,
SH_SERVICE_ROOT_URL,
XmlParserOptions,
} from './const';
import { GetCapabilitiesWmtsXml } from './wmts.utils';

export const xmlParser = new XMLParser(XmlParserOptions);

interface Capabilities {
Service: [];
Expand Down Expand Up @@ -67,7 +76,7 @@ export async function fetchGetCapabilitiesXml(
};
const url = createGetCapabilitiesXmlUrl(baseUrl, ogcServiceType);
const res = await axios.get(url, axiosReqConfig);
const parsedXml = await parseStringPromise(res.data);
const parsedXml = xmlParser.parse(res.data);
return parsedXml;
}

Expand Down
22 changes: 13 additions & 9 deletions src/layer/wmts.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,19 @@ export function toPixel(
}

function parseXmlWmtsLayers(parsedXml: GetCapabilitiesWmtsXml): GetCapabilitiesXmlLayer[] {
return parsedXml.Capabilities.Contents[0].Layer.map((l) => {
return {
Name: l['ows:Identifier'],
Title: l['ows:Title'],
Abstract: l['ows:Abstract'],
Style: l.Style,
ResourceUrl: getResourceUrl(l),
};
});
try {
return parsedXml.Capabilities.Contents[0].Layer.map((l) => {
return {
Name: l['ows:Identifier'],
Title: l['ows:Title'],
Abstract: l['ows:Abstract'],
Style: l.Style,
ResourceUrl: getResourceUrl(l),
};
});
} catch (x) {
console.error(x);
}
}
export async function fetchLayersFromWmtsGetCapabilitiesXml(
baseUrl: string,
Expand Down

0 comments on commit c51adff

Please sign in to comment.