script.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. 'use strict';
  2. const {Script} = require('vm');
  3. const {
  4. lookupCompiler,
  5. removeShebang
  6. } = require('./compiler');
  7. const {
  8. transformer
  9. } = require('./transformer');
  10. const objectDefineProperties = Object.defineProperties;
  11. const MODULE_PREFIX = '(function (exports, require, module, __filename, __dirname) { ';
  12. const STRICT_MODULE_PREFIX = MODULE_PREFIX + '"use strict"; ';
  13. const MODULE_SUFFIX = '\n});';
  14. /**
  15. * Class Script
  16. *
  17. * @public
  18. */
  19. class VMScript {
  20. /**
  21. * The script code with wrapping. If set will invalidate the cache.<br>
  22. * Writable only for backwards compatibility.
  23. *
  24. * @public
  25. * @readonly
  26. * @member {string} code
  27. * @memberOf VMScript#
  28. */
  29. /**
  30. * The filename used for this script.
  31. *
  32. * @public
  33. * @readonly
  34. * @since v3.9.0
  35. * @member {string} filename
  36. * @memberOf VMScript#
  37. */
  38. /**
  39. * The line offset use for stack traces.
  40. *
  41. * @public
  42. * @readonly
  43. * @since v3.9.0
  44. * @member {number} lineOffset
  45. * @memberOf VMScript#
  46. */
  47. /**
  48. * The column offset use for stack traces.
  49. *
  50. * @public
  51. * @readonly
  52. * @since v3.9.0
  53. * @member {number} columnOffset
  54. * @memberOf VMScript#
  55. */
  56. /**
  57. * The compiler to use to get the JavaScript code.
  58. *
  59. * @public
  60. * @readonly
  61. * @since v3.9.0
  62. * @member {(string|compileCallback)} compiler
  63. * @memberOf VMScript#
  64. */
  65. /**
  66. * The prefix for the script.
  67. *
  68. * @private
  69. * @member {string} _prefix
  70. * @memberOf VMScript#
  71. */
  72. /**
  73. * The suffix for the script.
  74. *
  75. * @private
  76. * @member {string} _suffix
  77. * @memberOf VMScript#
  78. */
  79. /**
  80. * The compiled vm.Script for the VM or if not compiled <code>null</code>.
  81. *
  82. * @private
  83. * @member {?vm.Script} _compiledVM
  84. * @memberOf VMScript#
  85. */
  86. /**
  87. * The compiled vm.Script for the NodeVM or if not compiled <code>null</code>.
  88. *
  89. * @private
  90. * @member {?vm.Script} _compiledNodeVM
  91. * @memberOf VMScript#
  92. */
  93. /**
  94. * The compiled vm.Script for the NodeVM in strict mode or if not compiled <code>null</code>.
  95. *
  96. * @private
  97. * @member {?vm.Script} _compiledNodeVMStrict
  98. * @memberOf VMScript#
  99. */
  100. /**
  101. * The resolved compiler to use to get the JavaScript code.
  102. *
  103. * @private
  104. * @readonly
  105. * @member {compileCallback} _compiler
  106. * @memberOf VMScript#
  107. */
  108. /**
  109. * The script to run without wrapping.
  110. *
  111. * @private
  112. * @member {string} _code
  113. * @memberOf VMScript#
  114. */
  115. /**
  116. * Whether or not the script contains async functions.
  117. *
  118. * @private
  119. * @member {boolean} _hasAsync
  120. * @memberOf VMScript#
  121. */
  122. /**
  123. * Create VMScript instance.
  124. *
  125. * @public
  126. * @param {string} code - Code to run.
  127. * @param {(string|Object)} [options] - Options map or filename.
  128. * @param {string} [options.filename="vm.js"] - Filename that shows up in any stack traces produced from this script.
  129. * @param {number} [options.lineOffset=0] - Passed to vm.Script options.
  130. * @param {number} [options.columnOffset=0] - Passed to vm.Script options.
  131. * @param {(string|compileCallback)} [options.compiler="javascript"] - The compiler to use.
  132. * @throws {VMError} If the compiler is unknown or if coffee-script was requested but the module not found.
  133. */
  134. constructor(code, options) {
  135. const sCode = `${code}`;
  136. let useFileName;
  137. let useOptions;
  138. if (arguments.length === 2) {
  139. if (typeof options === 'object') {
  140. useOptions = options || {__proto__: null};
  141. useFileName = useOptions.filename;
  142. } else {
  143. useOptions = {__proto__: null};
  144. useFileName = options;
  145. }
  146. } else if (arguments.length > 2) {
  147. // We do it this way so that there are no more arguments in the function.
  148. // eslint-disable-next-line prefer-rest-params
  149. useOptions = arguments[2] || {__proto__: null};
  150. useFileName = options || useOptions.filename;
  151. } else {
  152. useOptions = {__proto__: null};
  153. }
  154. const {
  155. compiler = 'javascript',
  156. lineOffset = 0,
  157. columnOffset = 0
  158. } = useOptions;
  159. // Throw if the compiler is unknown.
  160. const resolvedCompiler = lookupCompiler(compiler);
  161. objectDefineProperties(this, {
  162. __proto__: null,
  163. code: {
  164. __proto__: null,
  165. // Put this here so that it is enumerable, and looks like a property.
  166. get() {
  167. return this._prefix + this._code + this._suffix;
  168. },
  169. set(value) {
  170. const strNewCode = String(value);
  171. if (strNewCode === this._code && this._prefix === '' && this._suffix === '') return;
  172. this._code = strNewCode;
  173. this._prefix = '';
  174. this._suffix = '';
  175. this._compiledVM = null;
  176. this._compiledNodeVM = null;
  177. this._compiledCode = null;
  178. },
  179. enumerable: true
  180. },
  181. filename: {
  182. __proto__: null,
  183. value: useFileName || 'vm.js',
  184. enumerable: true
  185. },
  186. lineOffset: {
  187. __proto__: null,
  188. value: lineOffset,
  189. enumerable: true
  190. },
  191. columnOffset: {
  192. __proto__: null,
  193. value: columnOffset,
  194. enumerable: true
  195. },
  196. compiler: {
  197. __proto__: null,
  198. value: compiler,
  199. enumerable: true
  200. },
  201. _code: {
  202. __proto__: null,
  203. value: sCode,
  204. writable: true
  205. },
  206. _prefix: {
  207. __proto__: null,
  208. value: '',
  209. writable: true
  210. },
  211. _suffix: {
  212. __proto__: null,
  213. value: '',
  214. writable: true
  215. },
  216. _compiledVM: {
  217. __proto__: null,
  218. value: null,
  219. writable: true
  220. },
  221. _compiledNodeVM: {
  222. __proto__: null,
  223. value: null,
  224. writable: true
  225. },
  226. _compiledNodeVMStrict: {
  227. __proto__: null,
  228. value: null,
  229. writable: true
  230. },
  231. _compiledCode: {
  232. __proto__: null,
  233. value: null,
  234. writable: true
  235. },
  236. _hasAsync: {
  237. __proto__: null,
  238. value: false,
  239. writable: true
  240. },
  241. _compiler: {__proto__: null, value: resolvedCompiler}
  242. });
  243. }
  244. /**
  245. * Wraps the code.<br>
  246. * This will replace the old wrapping.<br>
  247. * Will invalidate the code cache.
  248. *
  249. * @public
  250. * @deprecated Since v3.9.0. Wrap your code before passing it into the VMScript object.
  251. * @param {string} prefix - String that will be appended before the script code.
  252. * @param {script} suffix - String that will be appended behind the script code.
  253. * @return {this} This for chaining.
  254. * @throws {TypeError} If prefix or suffix is a Symbol.
  255. */
  256. wrap(prefix, suffix) {
  257. const strPrefix = `${prefix}`;
  258. const strSuffix = `${suffix}`;
  259. if (this._prefix === strPrefix && this._suffix === strSuffix) return this;
  260. this._prefix = strPrefix;
  261. this._suffix = strSuffix;
  262. this._compiledVM = null;
  263. this._compiledNodeVM = null;
  264. this._compiledNodeVMStrict = null;
  265. return this;
  266. }
  267. /**
  268. * Compile this script. <br>
  269. * This is useful to detect syntax errors in the script.
  270. *
  271. * @public
  272. * @return {this} This for chaining.
  273. * @throws {SyntaxError} If there is a syntax error in the script.
  274. */
  275. compile() {
  276. this._compileVM();
  277. return this;
  278. }
  279. /**
  280. * Get the compiled code.
  281. *
  282. * @private
  283. * @return {string} The code.
  284. */
  285. getCompiledCode() {
  286. if (!this._compiledCode) {
  287. const comp = this._compiler(this._prefix + removeShebang(this._code) + this._suffix, this.filename);
  288. const res = transformer(null, comp, false, false, this.filename);
  289. this._compiledCode = res.code;
  290. this._hasAsync = res.hasAsync;
  291. }
  292. return this._compiledCode;
  293. }
  294. /**
  295. * Compiles this script to a vm.Script.
  296. *
  297. * @private
  298. * @param {string} prefix - JavaScript code that will be used as prefix.
  299. * @param {string} suffix - JavaScript code that will be used as suffix.
  300. * @return {vm.Script} The compiled vm.Script.
  301. * @throws {SyntaxError} If there is a syntax error in the script.
  302. */
  303. _compile(prefix, suffix) {
  304. return new Script(prefix + this.getCompiledCode() + suffix, {
  305. __proto__: null,
  306. filename: this.filename,
  307. displayErrors: false,
  308. lineOffset: this.lineOffset,
  309. columnOffset: this.columnOffset
  310. });
  311. }
  312. /**
  313. * Will return the cached version of the script intended for VM or compile it.
  314. *
  315. * @private
  316. * @return {vm.Script} The compiled script
  317. * @throws {SyntaxError} If there is a syntax error in the script.
  318. */
  319. _compileVM() {
  320. let script = this._compiledVM;
  321. if (!script) {
  322. this._compiledVM = script = this._compile('', '');
  323. }
  324. return script;
  325. }
  326. /**
  327. * Will return the cached version of the script intended for NodeVM or compile it.
  328. *
  329. * @private
  330. * @return {vm.Script} The compiled script
  331. * @throws {SyntaxError} If there is a syntax error in the script.
  332. */
  333. _compileNodeVM() {
  334. let script = this._compiledNodeVM;
  335. if (!script) {
  336. this._compiledNodeVM = script = this._compile(MODULE_PREFIX, MODULE_SUFFIX);
  337. }
  338. return script;
  339. }
  340. /**
  341. * Will return the cached version of the script intended for NodeVM in strict mode or compile it.
  342. *
  343. * @private
  344. * @return {vm.Script} The compiled script
  345. * @throws {SyntaxError} If there is a syntax error in the script.
  346. */
  347. _compileNodeVMStrict() {
  348. let script = this._compiledNodeVMStrict;
  349. if (!script) {
  350. this._compiledNodeVMStrict = script = this._compile(STRICT_MODULE_PREFIX, MODULE_SUFFIX);
  351. }
  352. return script;
  353. }
  354. }
  355. exports.MODULE_PREFIX = MODULE_PREFIX;
  356. exports.STRICT_MODULE_PREFIX = STRICT_MODULE_PREFIX;
  357. exports.MODULE_SUFFIX = MODULE_SUFFIX;
  358. exports.VMScript = VMScript;