mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-12 18:29:53 -05:00
fix: Remove usage of vm2 (#26863)
* fix performance stuff -- run ci * Update cypress.config.ts * remove dep, add tests * clean up * m does not exist * vm2 is everywhere --------- Co-authored-by: Ryan Manuel <ryanm@cypress.io>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import fs from 'fs-extra'
|
||||
import { NodeVM } from 'vm2'
|
||||
import Debug from 'debug'
|
||||
import type { ProtocolManagerShape, AppCaptureProtocolInterface, CDPClient, ProtocolError } from '@packages/types'
|
||||
import Database from 'better-sqlite3'
|
||||
@@ -7,8 +6,7 @@ import path from 'path'
|
||||
import os from 'os'
|
||||
import { createGzip } from 'zlib'
|
||||
import fetch from 'cross-fetch'
|
||||
import { performance } from 'perf_hooks'
|
||||
import crypto from 'crypto'
|
||||
import Module from 'module'
|
||||
|
||||
const routes = require('./routes')
|
||||
const pkg = require('@packages/root')
|
||||
@@ -19,6 +17,24 @@ const debugVerbose = Debug('cypress-verbose:server:protocol')
|
||||
const CAPTURE_ERRORS = !process.env.CYPRESS_LOCAL_PROTOCOL_PATH
|
||||
const DELETE_DB = !process.env.CYPRESS_LOCAL_PROTOCOL_PATH
|
||||
|
||||
/**
|
||||
* requireScript, does just that, requires the passed in script as if it was a module.
|
||||
* @param script - string
|
||||
* @returns exports
|
||||
*/
|
||||
const requireScript = (script: string) => {
|
||||
const mod = new Module('id', module)
|
||||
|
||||
mod.filename = ''
|
||||
// _compile is a private method
|
||||
// @ts-expect-error
|
||||
mod._compile(script, mod.filename)
|
||||
|
||||
module.children.splice(module.children.indexOf(mod), 1)
|
||||
|
||||
return mod.exports
|
||||
}
|
||||
|
||||
export class ProtocolManager implements ProtocolManagerShape {
|
||||
private _runId?: string
|
||||
private _instanceId?: string
|
||||
@@ -39,21 +55,8 @@ export class ProtocolManager implements ProtocolManagerShape {
|
||||
const cypressProtocolDirectory = path.join(os.tmpdir(), 'cypress', 'protocol')
|
||||
|
||||
await fs.ensureDir(cypressProtocolDirectory)
|
||||
const vm = new NodeVM({
|
||||
console: 'inherit',
|
||||
sandbox: {
|
||||
Debug,
|
||||
performance: {
|
||||
now: performance.now,
|
||||
timeOrigin: performance.timeOrigin,
|
||||
},
|
||||
createHash: (text) => {
|
||||
return crypto.createHash('md5').update(text).digest('hex')
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const { AppCaptureProtocol } = vm.run(script)
|
||||
const { AppCaptureProtocol } = requireScript(script)
|
||||
|
||||
this._protocol = new AppCaptureProtocol()
|
||||
}
|
||||
|
||||
@@ -130,7 +130,6 @@
|
||||
"underscore.string": "3.3.6",
|
||||
"url-parse": "1.5.9",
|
||||
"uuid": "8.3.2",
|
||||
"vm2": "3.9.19",
|
||||
"webpack-virtual-modules": "0.5.0",
|
||||
"widest-line": "3.1.0"
|
||||
},
|
||||
@@ -224,4 +223,4 @@
|
||||
"fsevents": "^2",
|
||||
"registry-js": "1.15.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,6 @@
|
||||
import type { ProtocolManagerShape } from '@packages/types'
|
||||
|
||||
declare const Debug: (namespace) => import('debug').IDebugger
|
||||
declare const performance: {
|
||||
now(): number
|
||||
timeOrigin: number
|
||||
}
|
||||
declare const createHash: {
|
||||
(text: string): string
|
||||
}
|
||||
|
||||
export class AppCaptureProtocol implements ProtocolManagerShape {
|
||||
private Debug: typeof Debug
|
||||
private performance: typeof performance
|
||||
private createHash: typeof createHash
|
||||
|
||||
constructor () {
|
||||
this.Debug = Debug
|
||||
this.performance = performance
|
||||
this.createHash = createHash
|
||||
}
|
||||
|
||||
setupProtocol = (script, runId) => {
|
||||
return Promise.resolve()
|
||||
|
||||
@@ -38,11 +38,8 @@ describe('lib/cloud/protocol', () => {
|
||||
})
|
||||
|
||||
it('should be able to setup the protocol', () => {
|
||||
expect((protocol as any).Debug).not.to.be.undefined
|
||||
expect((protocol as any).performance).not.to.be.undefined
|
||||
expect((protocol as any).performance.now).not.to.be.undefined
|
||||
expect((protocol as any).performance.timeOrigin).not.to.be.undefined
|
||||
expect((protocol as any).createHash).not.to.be.undefined
|
||||
expect((protocol as any)).not.to.be.undefined
|
||||
expect((protocol as any).setupProtocol).not.to.be.undefined
|
||||
})
|
||||
|
||||
it('should be able to connect to the browser', async () => {
|
||||
|
||||
@@ -63,7 +63,6 @@ const getDependencyPathsToKeep = async (buildAppDir) => {
|
||||
'openbsd',
|
||||
'sunos',
|
||||
'win32'].map((platform) => path.join(unixBuildAppDir, `node_modules/default-gateway/${platform}.js`)),
|
||||
path.join(unixBuildAppDir, 'node_modules/vm2/lib/bridge.js'),
|
||||
])
|
||||
let esbuildResult
|
||||
let newEntryPointsFound = true
|
||||
|
||||
+1
-12
@@ -48,7 +48,6 @@
|
||||
"./node_modules/tcp-port-used/node_modules/debug/src/node.js",
|
||||
"./node_modules/trash/node_modules/make-dir/index.js",
|
||||
"./node_modules/utif/UTIF.js",
|
||||
"./node_modules/vm2/lib/bridge.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/comments.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/conversion.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/family.js",
|
||||
@@ -710,9 +709,6 @@
|
||||
"./node_modules/utf8-stream/node_modules/readable-stream/transform.js",
|
||||
"./node_modules/util-deprecate/node.js",
|
||||
"./node_modules/verror/lib/verror.js",
|
||||
"./node_modules/vm2/lib/nodevm.js",
|
||||
"./node_modules/vm2/lib/resolver-compat.js",
|
||||
"./node_modules/vm2/lib/vm.js",
|
||||
"./node_modules/ws/index.js",
|
||||
"./node_modules/ws/lib/buffer-util.js",
|
||||
"./node_modules/ws/lib/constants.js",
|
||||
@@ -3951,13 +3947,6 @@
|
||||
"./node_modules/value-or-promise/build/main/ValueOrPromise.js",
|
||||
"./node_modules/value-or-promise/build/main/index.js",
|
||||
"./node_modules/vary/index.js",
|
||||
"./node_modules/vm2/index.js",
|
||||
"./node_modules/vm2/lib/compiler.js",
|
||||
"./node_modules/vm2/lib/filesystem.js",
|
||||
"./node_modules/vm2/lib/main.js",
|
||||
"./node_modules/vm2/lib/resolver.js",
|
||||
"./node_modules/vm2/lib/script.js",
|
||||
"./node_modules/vm2/lib/transformer.js",
|
||||
"./node_modules/which/which.js",
|
||||
"./node_modules/widest-line/index.js",
|
||||
"./node_modules/win-version-info/index.js",
|
||||
@@ -4781,4 +4770,4 @@
|
||||
],
|
||||
"deferredHashFile": "yarn.lock",
|
||||
"deferredHash": "dd014fb58c3b5e133ecf1163035224c4ab5d0f5b1f715a479bd82dcdbab01df2"
|
||||
}
|
||||
}
|
||||
|
||||
+1
-12
@@ -48,7 +48,6 @@
|
||||
"./node_modules/tcp-port-used/node_modules/debug/src/node.js",
|
||||
"./node_modules/trash/node_modules/make-dir/index.js",
|
||||
"./node_modules/utif/UTIF.js",
|
||||
"./node_modules/vm2/lib/bridge.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/comments.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/conversion.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/family.js",
|
||||
@@ -709,9 +708,6 @@
|
||||
"./node_modules/utf8-stream/node_modules/readable-stream/transform.js",
|
||||
"./node_modules/util-deprecate/node.js",
|
||||
"./node_modules/verror/lib/verror.js",
|
||||
"./node_modules/vm2/lib/nodevm.js",
|
||||
"./node_modules/vm2/lib/resolver-compat.js",
|
||||
"./node_modules/vm2/lib/vm.js",
|
||||
"./node_modules/ws/index.js",
|
||||
"./node_modules/ws/lib/buffer-util.js",
|
||||
"./node_modules/ws/lib/constants.js",
|
||||
@@ -3950,13 +3946,6 @@
|
||||
"./node_modules/value-or-promise/build/main/ValueOrPromise.js",
|
||||
"./node_modules/value-or-promise/build/main/index.js",
|
||||
"./node_modules/vary/index.js",
|
||||
"./node_modules/vm2/index.js",
|
||||
"./node_modules/vm2/lib/compiler.js",
|
||||
"./node_modules/vm2/lib/filesystem.js",
|
||||
"./node_modules/vm2/lib/main.js",
|
||||
"./node_modules/vm2/lib/resolver.js",
|
||||
"./node_modules/vm2/lib/script.js",
|
||||
"./node_modules/vm2/lib/transformer.js",
|
||||
"./node_modules/which/which.js",
|
||||
"./node_modules/widest-line/index.js",
|
||||
"./node_modules/win-version-info/index.js",
|
||||
@@ -4780,4 +4769,4 @@
|
||||
],
|
||||
"deferredHashFile": "yarn.lock",
|
||||
"deferredHash": "dd014fb58c3b5e133ecf1163035224c4ab5d0f5b1f715a479bd82dcdbab01df2"
|
||||
}
|
||||
}
|
||||
|
||||
+1
-12
@@ -48,7 +48,6 @@
|
||||
"./node_modules/tcp-port-used/node_modules/debug/src/node.js",
|
||||
"./node_modules/trash/node_modules/make-dir/index.js",
|
||||
"./node_modules/utif/UTIF.js",
|
||||
"./node_modules/vm2/lib/bridge.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/comments.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/conversion.js",
|
||||
"./packages/config/node_modules/@babel/traverse/lib/path/family.js",
|
||||
@@ -713,9 +712,6 @@
|
||||
"./node_modules/utf8-stream/node_modules/readable-stream/transform.js",
|
||||
"./node_modules/util-deprecate/node.js",
|
||||
"./node_modules/verror/lib/verror.js",
|
||||
"./node_modules/vm2/lib/nodevm.js",
|
||||
"./node_modules/vm2/lib/resolver-compat.js",
|
||||
"./node_modules/vm2/lib/vm.js",
|
||||
"./node_modules/win-version-info/index.js",
|
||||
"./node_modules/ws/index.js",
|
||||
"./node_modules/ws/lib/buffer-util.js",
|
||||
@@ -3951,13 +3947,6 @@
|
||||
"./node_modules/value-or-promise/build/main/ValueOrPromise.js",
|
||||
"./node_modules/value-or-promise/build/main/index.js",
|
||||
"./node_modules/vary/index.js",
|
||||
"./node_modules/vm2/index.js",
|
||||
"./node_modules/vm2/lib/compiler.js",
|
||||
"./node_modules/vm2/lib/filesystem.js",
|
||||
"./node_modules/vm2/lib/main.js",
|
||||
"./node_modules/vm2/lib/resolver.js",
|
||||
"./node_modules/vm2/lib/script.js",
|
||||
"./node_modules/vm2/lib/transformer.js",
|
||||
"./node_modules/which/which.js",
|
||||
"./node_modules/widest-line/index.js",
|
||||
"./node_modules/wonka/dist/wonka.js",
|
||||
@@ -4780,4 +4769,4 @@
|
||||
],
|
||||
"deferredHashFile": "yarn.lock",
|
||||
"deferredHash": "b72479c20b36abc043663fd55b78282329103014ba45cc089c91ca77bdba1507"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30096,7 +30096,7 @@ vm-browserify@^1.0.1:
|
||||
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"
|
||||
integrity sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==
|
||||
|
||||
vm2@3.9.19, vm2@^3.9.3:
|
||||
vm2@^3.9.3:
|
||||
version "3.9.19"
|
||||
resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.9.19.tgz#be1e1d7a106122c6c492b4d51c2e8b93d3ed6a4a"
|
||||
integrity sha512-J637XF0DHDMV57R6JyVsTak7nIL8gy5KH4r1HiwWLf/4GBbb5MKL5y7LpmF4A8E2nR6XmzpmMFQ7V7ppPTmUQg==
|
||||
|
||||
Reference in New Issue
Block a user