Skip to content

Commit d466dff

Browse files
committed
fix(storage): use standard fast-xml-parser import for Metro compatibility
Replace the try/catch require workaround with a standard ESM import of fast-xml-parser. Add react-native field to storage package.json for proper Metro bundler resolution. Remove the now-unnecessary __mocks__/fast-xml-parser.js and moduleNameMapper jest config.
1 parent 6af0f75 commit d466dff

4 files changed

Lines changed: 12 additions & 24 deletions

File tree

__mocks__/fast-xml-parser.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@
122122
"verbose": true,
123123
"transformIgnorePatterns": [
124124
"node_modules/(?!(fast-xml-parser)/)"
125-
],
126-
"moduleNameMapper": {
127-
"fast-xml-parser/src/xmlparser/XMLParser.js": "<rootDir>/__mocks__/fast-xml-parser.js"
128-
}
125+
]
129126
},
130127
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
131128
}

packages/storage/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"main": "./lib/index.js",
66
"module": "./lib-esm/index.js",
77
"typings": "./lib-esm/index.d.ts",
8+
"react-native": {
9+
"./lib/index": "./src/index.ts",
10+
"fast-xml-parser": "fast-xml-parser",
11+
"buffer": "buffer"
12+
},
813
"browser": {
914
"./lib-esm/AwsClients/S3/runtime/index.js": "./lib-esm/AwsClients/S3/runtime/index.browser.js",
1015
"./lib/AwsClients/S3/runtime/index.js": "./lib/AwsClients/S3/runtime/index.browser.js"
@@ -84,9 +89,6 @@
8489
"transformIgnorePatterns": [
8590
"node_modules/(?!(fast-xml-parser)/)"
8691
],
87-
"moduleNameMapper": {
88-
"fast-xml-parser/src/xmlparser/XMLParser.js": "<rootDir>/../../__mocks__/fast-xml-parser.js"
89-
},
9092
"moduleFileExtensions": [
9193
"ts",
9294
"tsx",

packages/storage/src/AwsClients/S3/runtime/xmlParser/pureJs.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
let XMLParser: any;
4+
import { XMLParser } from 'fast-xml-parser';
5+
56
/**
67
* Pure JS XML parser that can be used in Non-browser environments, like React Native and Node.js. This is the same
78
* XML parser implementation as used in AWS SDK S3 client. It depends on pure JavaScript XML parser library
89
* `fast-xml-parser`.
910
*
1011
* Ref: https://github.com/aws/aws-sdk-js-v3/blob/1e806ba3f4a83c9e3eb0b41a3a7092da93826b8f/clients/client-s3/src/protocols/Aws_restXml.ts#L12938-L12959
1112
*/
12-
13-
// Use require to avoid all ESM/Metro issues
14-
try {
15-
XMLParser = require('fast-xml-parser/src/xmlparser/XMLParser.js');
16-
} catch (e) {
17-
XMLParser = require('fast-xml-parser').XMLParser;
18-
}
19-
2013
export const parser = {
2114
parse: (xmlStr: string): any => {
22-
const parser = new XMLParser({
15+
const xmlParser = new XMLParser({
2316
attributeNamePrefix: '',
2417
htmlEntities: true,
2518
ignoreAttributes: false,
@@ -30,9 +23,9 @@ export const parser = {
3023
tagValueProcessor: (_, val) =>
3124
val.trim() === '' && val.includes('\n') ? '' : undefined,
3225
});
33-
parser.addEntity('#xD', '\r');
34-
parser.addEntity('#10', '\n');
35-
const parsedObj = parser.parse(xmlStr);
26+
xmlParser.addEntity('#xD', '\r');
27+
xmlParser.addEntity('#10', '\n');
28+
const parsedObj = xmlParser.parse(xmlStr);
3629
const textNodeName = '#text';
3730
const key = Object.keys(parsedObj)[0];
3831
const parsedObjToReturn = parsedObj[key];

0 commit comments

Comments
 (0)