Skip to content

Commit c51adff

Browse files
Jan Kumerjanybravo
Jan Kumer
authored andcommitted
Replace xml2js with fast-xml-parser
1 parent 9f9a014 commit c51adff

File tree

5 files changed

+96
-58
lines changed

5 files changed

+96
-58
lines changed

package-lock.json

+56-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
"@turf/area": "^6.0.1",
1010
"@turf/helpers": "^6.1.4",
1111
"@types/proj4": "^2.5.2",
12-
"@types/xml2js": "^0.4.4",
1312
"axios": "^0.21.1",
13+
"fast-xml-parser": "^4.4.1",
1414
"moment": "^2.24.0",
1515
"polygon-clipping": "^0.14.3",
1616
"proj4": "^2.9.0",
1717
"query-string": "^6.4.2",
18-
"terraformer-wkt-parser": "^1.2.1",
19-
"xml2js": "^0.4.19"
18+
"terraformer-wkt-parser": "^1.2.1"
2019
},
2120
"engines": {
2221
"node": ">=18"
@@ -30,7 +29,6 @@
3029
"@types/jest": "^25.1.3",
3130
"@types/node-fetch": "^2.5.7",
3231
"@types/service-worker-mock": "^2.0.4",
33-
"@types/xml2js": "^0.4.4",
3432
"@typescript-eslint/eslint-plugin": "^7.17.0",
3533
"@typescript-eslint/parser": "^7.17.0",
3634
"axios-mock-adapter": "^1.18.1",

src/layer/const.ts

+11
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,14 @@ export const PLANET_FALSE_COLOR_TEMPLATES = [
286286
export const EQUATOR_RADIUS = 6378137.0;
287287
export const DEGREE_TO_RADIAN = Math.PI / 180;
288288
export const RADIAN_TO_DEGREE = 180 / Math.PI;
289+
290+
export const XmlParserOptions = Object.freeze({
291+
attributesGroupName: '$',
292+
attributeNamePrefix: '',
293+
textNodeName: '_',
294+
ignoreAttributes: false,
295+
isArray: (name: string, jpath: string, isLeafNode: boolean, isAttribute: boolean) => {
296+
const isA = !isAttribute && !['Capabilities', 'WMS_Capabilities', 'WMT_MS_Capabilities'].includes(name);
297+
return isA;
298+
},
299+
});

src/layer/utils.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import axios, { AxiosRequestConfig } from 'axios';
22
import { stringify, parseUrl, stringifyUrl } from 'query-string';
3-
import { parseStringPromise } from 'xml2js';
4-
import { EQUATOR_RADIUS, OgcServiceTypes, SH_SERVICE_HOSTNAMES_V3, SH_SERVICE_ROOT_URL } from './const';
3+
54
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
65
import { CACHE_CONFIG_30MIN, CACHE_CONFIG_30MIN_MEMORY } from '../utils/cacheHandlers';
7-
import type { GetCapabilitiesWmtsXml } from './wmts.utils';
6+
import { XMLParser } from 'fast-xml-parser';
7+
import proj4 from 'proj4';
88
import { getAuthToken } from '../auth';
99
import { BBox } from '../bbox';
1010
import { CRS_EPSG3857 } from '../crs';
11-
import proj4 from 'proj4';
11+
import {
12+
EQUATOR_RADIUS,
13+
OgcServiceTypes,
14+
SH_SERVICE_HOSTNAMES_V3,
15+
SH_SERVICE_ROOT_URL,
16+
XmlParserOptions,
17+
} from './const';
18+
import { GetCapabilitiesWmtsXml } from './wmts.utils';
19+
20+
export const xmlParser = new XMLParser(XmlParserOptions);
1221

1322
interface Capabilities {
1423
Service: [];
@@ -67,7 +76,7 @@ export async function fetchGetCapabilitiesXml(
6776
};
6877
const url = createGetCapabilitiesXmlUrl(baseUrl, ogcServiceType);
6978
const res = await axios.get(url, axiosReqConfig);
70-
const parsedXml = await parseStringPromise(res.data);
79+
const parsedXml = xmlParser.parse(res.data);
7180
return parsedXml;
7281
}
7382

src/layer/wmts.utils.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,19 @@ export function toPixel(
163163
}
164164

165165
function parseXmlWmtsLayers(parsedXml: GetCapabilitiesWmtsXml): GetCapabilitiesXmlLayer[] {
166-
return parsedXml.Capabilities.Contents[0].Layer.map((l) => {
167-
return {
168-
Name: l['ows:Identifier'],
169-
Title: l['ows:Title'],
170-
Abstract: l['ows:Abstract'],
171-
Style: l.Style,
172-
ResourceUrl: getResourceUrl(l),
173-
};
174-
});
166+
try {
167+
return parsedXml.Capabilities.Contents[0].Layer.map((l) => {
168+
return {
169+
Name: l['ows:Identifier'],
170+
Title: l['ows:Title'],
171+
Abstract: l['ows:Abstract'],
172+
Style: l.Style,
173+
ResourceUrl: getResourceUrl(l),
174+
};
175+
});
176+
} catch (x) {
177+
console.error(x);
178+
}
175179
}
176180
export async function fetchLayersFromWmtsGetCapabilitiesXml(
177181
baseUrl: string,

0 commit comments

Comments
 (0)