diff --git a/package.json b/package.json index 4e58c0b..1d368f2 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "safe-stable-stringify": "^2.2.0", "ts-node": "^10.9.1", "typescript": "~5.5.0", + "vm2": "^3.10.0", "yargs": "^17.1.1" }, "devDependencies": { diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index bb7e055..c39f746 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -9,7 +9,7 @@ export { Program, CompilerOptions, Symbol } from "typescript"; export { ts }; -const vm = require("vm"); +const { VM } = require("vm2"); const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"\)|".*?")\.| /g; const REGEX_TSCONFIG_NAME = /^.*\.json$/; @@ -867,10 +867,8 @@ export class JsonSchemaGenerator { definition.default = initial.getText(); } else { try { - const sandbox = { sandboxvar: null as any }; - vm.runInNewContext("sandboxvar=" + initial.getText(), sandbox); - - const val = sandbox.sandboxvar; + const vm = new VM(); + const val = vm.run("sandboxvar=" + initial.getText()) as any; if ( val === null || typeof val === "string" || diff --git a/yarn.lock b/yarn.lock index c08d754..a7405b9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -118,6 +118,18 @@ acorn-walk@^8.1.1: resolved "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz" integrity sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA== +acorn-walk@^8.3.4: + version "8.3.4" + resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.3.4.tgz#794dd169c3977edf4ba4ea47583587c5866236b7" + integrity sha512-ueEepnujpqee2o5aIYnvHU6C0A42MNdsIDeqy5BydrkuC5R1ZuUFnm27EeFJGoEHJQgn3uleRvmTXaJgfXbt4g== + dependencies: + acorn "^8.11.0" + +acorn@^8.11.0, acorn@^8.14.1: + version "8.15.0" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.15.0.tgz#a360898bc415edaac46c8241f6383975b930b816" + integrity sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg== + acorn@^8.4.1: version "8.5.0" resolved "https://registry.npmjs.org/acorn/-/acorn-8.5.0.tgz" @@ -923,6 +935,14 @@ v8-compile-cache-lib@^3.0.1: resolved "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz" integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg== +vm2@^3.10.0: + version "3.10.0" + resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.10.0.tgz#bd241fbf37fed0b7d0050e40ad08d7be6ba33d57" + integrity sha512-3ggF4Bs0cw4M7Rxn19/Cv3nJi04xrgHwt4uLto+zkcZocaKwP/nKP9wPx6ggN2X0DSXxOOIc63BV1jvES19wXQ== + dependencies: + acorn "^8.14.1" + acorn-walk "^8.3.4" + which@2.0.2: version "2.0.2" resolved "https://registry.npmjs.org/which/-/which-2.0.2.tgz"