Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/processors/gridset/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* This module provides symbol resolution and metadata extraction.
*/

import { getFs, getPath } from '../../utils/io';
import { getFs, getNodeRequire, getPath } from '../../utils/io';

/**
* Default Grid 3 installation paths by platform
Expand Down Expand Up @@ -127,8 +127,9 @@ let cachedAdmZip: typeof import('adm-zip') | null = null;
function getAdmZip(): typeof import('adm-zip') {
if (cachedAdmZip) return cachedAdmZip;
try {
const nodeRequire = getNodeRequire();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const module = require('adm-zip') as typeof import('adm-zip') & {
const module = nodeRequire('adm-zip') as typeof import('adm-zip') & {
default?: typeof import('adm-zip');
};
const resolved = module.default || module;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let cachedRequire: NodeRequire | null | undefined = undefined;

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

function getNodeRequire(): NodeRequire {
export function getNodeRequire(): NodeRequire {
if (cachedRequire === undefined) {
if (typeof require === 'function') {
cachedRequire = require;
Expand Down
24 changes: 22 additions & 2 deletions src/validation/gridsetValidator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
/* eslint-disable @typescript-eslint/require-await */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import * as xml2js from 'xml2js';
import JSZip from 'jszip';
import { BaseValidator } from './baseValidator';
import { ValidationResult } from './validationTypes';
import { getFs, getPath } from '../utils/io';
import { getFs, getNodeRequire, getPath } from '../utils/io';

let cachedXml2js: typeof import('xml2js') | null = null;
function getXml2js(): typeof import('xml2js') {
if (cachedXml2js) return cachedXml2js;
try {
const nodeRequire = getNodeRequire();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const module = nodeRequire('xml2js') as typeof import('xml2js') & {
default?: typeof import('xml2js');
};
const resolved = module.default || module;
cachedXml2js = resolved;
return resolved;
} catch {
throw new Error('Validator requires Xml2js in this environment.');
}
}

/**
* Validator for Grid3/Smartbox Gridset files (.gridset, .gridsetx)
Expand Down Expand Up @@ -39,6 +55,7 @@ export class GridsetValidator extends BaseValidator {
// Try to parse as XML and check for gridset structure
try {
const contentStr = Buffer.isBuffer(content) ? content.toString('utf-8') : content;
const xml2js = getXml2js();
const parser = new xml2js.Parser();
const result = await parser.parseStringPromise(contentStr as string);
return result && (result.gridset || result.Gridset);
Expand Down Expand Up @@ -105,6 +122,7 @@ export class GridsetValidator extends BaseValidator {
let xmlObj: any = null;
await this.add_check('xml_parse', 'valid XML', async () => {
try {
const xml2js = getXml2js();
const parser = new xml2js.Parser();
const contentStr = content.toString('utf-8');
xmlObj = await parser.parseStringPromise(contentStr);
Expand Down Expand Up @@ -153,6 +171,7 @@ export class GridsetValidator extends BaseValidator {
} else {
try {
const gridsetXml = await gridsetEntry.async('string');
const xml2js = getXml2js();
const parser = new xml2js.Parser();
const xmlObj = await parser.parseStringPromise(gridsetXml);
const gridset = xmlObj.gridset || xmlObj.Gridset;
Expand All @@ -175,6 +194,7 @@ export class GridsetValidator extends BaseValidator {
} else {
try {
const settingsXml = await settingsEntry.async('string');
const xml2js = getXml2js();
const parser = new xml2js.Parser();
const xmlObj = await parser.parseStringPromise(settingsXml);
const settings =
Expand Down
9 changes: 4 additions & 5 deletions src/validation/obfValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import JSZip from 'jszip';
import { BaseValidator } from './baseValidator';
import { ValidationResult } from './validationTypes';
import * as fs from 'fs';
import * as path from 'path';
import { getFs, getPath, readBinaryFromInput } from '../utils/io';

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

/**
Expand Down