Skip to content

Commit 3805fda

Browse files
authored
Merge pull request #15 from sonicbaume/obfvalidator-noderequire
Dynamic imports for Node packages in browser target
2 parents 365aa08 + aedc058 commit 3805fda

4 files changed

Lines changed: 30 additions & 10 deletions

File tree

src/processors/gridset/symbols.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* This module provides symbol resolution and metadata extraction.
1414
*/
1515

16-
import { getFs, getPath } from '../../utils/io';
16+
import { getFs, getNodeRequire, getPath } from '../../utils/io';
1717

1818
/**
1919
* Default Grid 3 installation paths by platform
@@ -127,8 +127,9 @@ let cachedAdmZip: typeof import('adm-zip') | null = null;
127127
function getAdmZip(): typeof import('adm-zip') {
128128
if (cachedAdmZip) return cachedAdmZip;
129129
try {
130+
const nodeRequire = getNodeRequire();
130131
// eslint-disable-next-line @typescript-eslint/no-var-requires
131-
const module = require('adm-zip') as typeof import('adm-zip') & {
132+
const module = nodeRequire('adm-zip') as typeof import('adm-zip') & {
132133
default?: typeof import('adm-zip');
133134
};
134135
const resolved = module.default || module;

src/utils/io.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let cachedRequire: NodeRequire | null | undefined = undefined;
88

99
type NodeRequire = (id: string) => any;
1010

11-
function getNodeRequire(): NodeRequire {
11+
export function getNodeRequire(): NodeRequire {
1212
if (cachedRequire === undefined) {
1313
if (typeof require === 'function') {
1414
cachedRequire = require;

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 =

src/validation/obfValidator.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import JSZip from 'jszip';
77
import { BaseValidator } from './baseValidator';
88
import { ValidationResult } from './validationTypes';
9-
import * as fs from 'fs';
10-
import * as path from 'path';
9+
import { getFs, getPath, readBinaryFromInput } from '../utils/io';
1110

1211
const OBF_FORMAT = 'open-board-0.1';
1312
const OBF_FORMAT_CURRENT_VERSION = 0.1;
@@ -25,9 +24,9 @@ export class ObfValidator extends BaseValidator {
2524
*/
2625
static async validateFile(filePath: string): Promise<ValidationResult> {
2726
const validator = new ObfValidator();
28-
const content = fs.readFileSync(filePath);
29-
const stats = fs.statSync(filePath);
30-
return validator.validate(content, path.basename(filePath), stats.size);
27+
const content = readBinaryFromInput(filePath);
28+
const stats = getFs().statSync(filePath);
29+
return validator.validate(content, getPath().basename(filePath), stats.size);
3130
}
3231

3332
/**

0 commit comments

Comments
 (0)