Skip to content

Commit 4c8aaf5

Browse files
committed
Dynamically import xml2js with node
1 parent 00afba1 commit 4c8aaf5

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/validation/gridsetValidator.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
/* eslint-disable @typescript-eslint/require-await */
22
/* eslint-disable @typescript-eslint/no-unsafe-argument */
33
/* eslint-disable @typescript-eslint/no-unsafe-return */
4-
import * as xml2js from 'xml2js';
54
import JSZip from 'jszip';
65
import { BaseValidator } from './baseValidator';
76
import { ValidationResult } from './validationTypes';
8-
import { getFs, getPath } from '../utils/io';
7+
import { getFs, getNodeRequire, getPath } from '../utils/io';
8+
9+
let cachedXml2js: typeof import('xml2js') | null = null;
10+
function getXml2js(): typeof import('xml2js') {
11+
if (cachedXml2js) return cachedXml2js;
12+
try {
13+
const nodeRequire = getNodeRequire();
14+
// eslint-disable-next-line @typescript-eslint/no-var-requires
15+
const module = nodeRequire('xml2js') as typeof import('xml2js') & {
16+
default?: typeof import('xml2js');
17+
};
18+
const resolved = module.default || module;
19+
cachedXml2js = resolved;
20+
return resolved;
21+
} catch {
22+
throw new Error('Validator requires Xml2js in this environment.');
23+
}
24+
}
925

1026
/**
1127
* Validator for Grid3/Smartbox Gridset files (.gridset, .gridsetx)
@@ -39,6 +55,7 @@ export class GridsetValidator extends BaseValidator {
3955
// Try to parse as XML and check for gridset structure
4056
try {
4157
const contentStr = Buffer.isBuffer(content) ? content.toString('utf-8') : content;
58+
const xml2js = getXml2js()
4259
const parser = new xml2js.Parser();
4360
const result = await parser.parseStringPromise(contentStr as string);
4461
return result && (result.gridset || result.Gridset);
@@ -105,6 +122,7 @@ export class GridsetValidator extends BaseValidator {
105122
let xmlObj: any = null;
106123
await this.add_check('xml_parse', 'valid XML', async () => {
107124
try {
125+
const xml2js = getXml2js()
108126
const parser = new xml2js.Parser();
109127
const contentStr = content.toString('utf-8');
110128
xmlObj = await parser.parseStringPromise(contentStr);
@@ -153,6 +171,7 @@ export class GridsetValidator extends BaseValidator {
153171
} else {
154172
try {
155173
const gridsetXml = await gridsetEntry.async('string');
174+
const xml2js = getXml2js()
156175
const parser = new xml2js.Parser();
157176
const xmlObj = await parser.parseStringPromise(gridsetXml);
158177
const gridset = xmlObj.gridset || xmlObj.Gridset;
@@ -175,6 +194,7 @@ export class GridsetValidator extends BaseValidator {
175194
} else {
176195
try {
177196
const settingsXml = await settingsEntry.async('string');
197+
const xml2js = getXml2js()
178198
const parser = new xml2js.Parser();
179199
const xmlObj = await parser.parseStringPromise(settingsXml);
180200
const settings =

0 commit comments

Comments
 (0)