Skip to content

Commit 1a69a1a

Browse files
author
Jan Kumer
committed
Replace xml2js with fast-xml-parser
1 parent 6326a37 commit 1a69a1a

File tree

6 files changed

+93
-95
lines changed

6 files changed

+93
-95
lines changed

package-lock.json

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

package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
"module": "dist/sentinelHub.esm.js",
66
"browser": "dist/sentinelHub.umd.js",
77
"type": "module",
8-
"dependencies": {},
8+
"dependencies": {
9+
},
910
"peerDependencies": {
1011
"@turf/area": "^6.0.1",
1112
"@turf/helpers": "^6.1.4",
1213
"@types/proj4": "^2.5.2",
13-
"@types/xml2js": "^0.4.4",
1414
"axios": "^0.21.1",
15+
"fast-xml-parser": "^4.4.1",
1516
"moment": "^2.24.0",
1617
"polygon-clipping": "^0.14.3",
1718
"proj4": "^2.9.0",
1819
"query-string": "^6.4.2",
19-
"terraformer-wkt-parser": "^1.2.1",
20-
"xml2js": "^0.4.19"
20+
"terraformer-wkt-parser": "^1.2.1"
2121
},
2222
"devDependencies": {
2323
"@babel/core": "^7.8.3",
@@ -30,14 +30,13 @@
3030
"@types/service-worker-mock": "^2.0.4",
3131
"@typescript-eslint/eslint-plugin": "^7.17.0",
3232
"@typescript-eslint/parser": "^7.17.0",
33-
"@types/xml2js": "^0.4.4",
3433
"axios-mock-adapter": "^1.18.1",
3534
"babel-loader": "^8.0.6",
3635
"concurrently": "^5.1.0",
3736
"dotenv": "^8.2.0",
3837
"eslint": "^8.57.0",
39-
"eslint-plugin-prettier": "^5.2.1",
4038
"eslint-config-prettier": "^9.1.0",
39+
"eslint-plugin-prettier": "^5.2.1",
4140
"jest": "^25.1.0",
4241
"jest-canvas-mock": "^2.3.1",
4342
"node-fetch": "^2.6.0",

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ import { setDefaultRequestsConfig } from './utils/defaultReqsConfig';
112112
import { CacheTarget, CacheTargets, invalidateCaches } from './utils/Cache';
113113
import { wmsGetMapUrl as _wmsGetMapUrl } from './layer/wms';
114114
import { drawBlobOnCanvas, canvasToBlob } from './utils/canvas';
115+
import { XmlParserOptions } from './layer/const';
115116

116117
import { Effects, ColorRange } from './mapDataManipulation/const';
117118
import { TPDI, setTPDIServiceBaseURL } from './dataimport/TPDI';
@@ -209,6 +210,7 @@ export {
209210
setAuthToken,
210211
isAuthTokenSet,
211212
requestAuthToken,
213+
XmlParserOptions,
212214
// other:
213215
GetMapParams,
214216
OverrideGetMapParams,

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

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import axios, { AxiosRequestConfig } from 'axios';
2-
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';
5-
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
6-
import { CACHE_CONFIG_30MIN, CACHE_CONFIG_30MIN_MEMORY } from '../utils/cacheHandlers';
7-
import type { GetCapabilitiesWmtsXml } from './wmts.utils';
2+
import { XMLParser } from 'fast-xml-parser';
3+
import proj4 from 'proj4';
4+
import { parseUrl, stringify, stringifyUrl } from 'query-string';
85
import { getAuthToken } from '../auth';
96
import { BBox } from '../bbox';
107
import { CRS_EPSG3857 } from '../crs';
11-
import proj4 from 'proj4';
8+
import { CACHE_CONFIG_30MIN, CACHE_CONFIG_30MIN_MEMORY } from '../utils/cacheHandlers';
9+
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
10+
import {
11+
EQUATOR_RADIUS,
12+
OgcServiceTypes,
13+
SH_SERVICE_HOSTNAMES_V3,
14+
SH_SERVICE_ROOT_URL,
15+
XmlParserOptions,
16+
} from './const';
17+
import { GetCapabilitiesWmtsXml } from './wmts.utils';
18+
19+
export const xmlParser = new XMLParser(XmlParserOptions);
1220

1321
interface Capabilities {
1422
Service: [];
@@ -67,7 +75,7 @@ export async function fetchGetCapabilitiesXml(
6775
};
6876
const url = createGetCapabilitiesXmlUrl(baseUrl, ogcServiceType);
6977
const res = await axios.get(url, axiosReqConfig);
70-
const parsedXml = await parseStringPromise(res.data);
78+
const parsedXml = xmlParser.parse(res.data);
7179
return parsedXml;
7280
}
7381

src/layer/wmts.utils.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,20 @@ 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.log(parsedXml);
178+
console.error(x);
179+
}
175180
}
176181
export async function fetchLayersFromWmtsGetCapabilitiesXml(
177182
baseUrl: string,

0 commit comments

Comments
 (0)