Skip to content

Commit

Permalink
Simpler and safer code
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Dec 5, 2023
1 parent 2066b94 commit 1b967f8
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions util/flashcn.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {createContext, runInContext} from 'vm';

import {DOMParser} from '@xmldom/xmldom';

// eslint-disable-next-line max-len
Expand Down Expand Up @@ -37,26 +35,12 @@ function parseJsonP(jsonp) {
return JSON.parse(m[1]);
}

function parseJsVar(js, varName) {
const ctx = createContext(Object.create(null));
try {
runInContext(js, ctx);
}
catch (_) {
throw new Error('Failed to run the JS code');
}
const result = runInContext(`(function() {
try {
return '1:' + JSON.stringify(this[${JSON.stringify(varName)}]);
}
catch (err) {
return '0:' + err;
}
})()`, ctx);
if (result[0] === '1') {
return JSON.parse(result.substring(2));
}
throw new Error(`JS Error: ${result.substring(2)}`);
function parseJsonV(jsonv) {
const json = jsonv.substring(
jsonv.indexOf('{'),
jsonv.lastIndexOf('}') + 1
);
return JSON.parse(json);
}

function getSource(downloadUrl, version) {
Expand Down Expand Up @@ -180,7 +164,7 @@ async function listDebug() {
throw new Error(`Unexpected status: ${jsRes.status}`);
}
const js = await jsRes.text();
const {version, date} = parseJsVar(js, '__package_info');
const {version, date} = parseJsonV(js);
const dated = dateNorm(date);

const r = [];
Expand Down

0 comments on commit 1b967f8

Please sign in to comment.