1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 07:16:36 +01:00
metamask-extension/development/build/task.js
Etienne Dusseault 9f6fa64d67
Add SES lockdown to extension webapp (#9729)
* Freezeglobals: remove Promise freezing, add lockdown

* background & UI: temp disable sentry

* add loose-envify, dedupe symbol-observable

* use loose envify

* add symbol-observable patch

* run freezeGlobals after sentry init

* use require instead of import

* add lockdown to contentscript

* add error code in message

* try increasing node env heap size to 2048

* change back circe CI option

* make freezeGlobals an exported function

* make freezeGlobals an exported function

* use freezeIntrinsics

* pass down env to child process

* fix unknown module

* fix tests

* change back to 2048

* fix import error

* attempt to fix memory error

* fix lint

* fix lint

* fix mem gain

* use lockdown in phishing detect

* fix lint

* move sentry init into freezeIntrinsics to run lockdown before other imports

* lint fix

* custom lockdown modules per context

* lint fix

* fix global test

* remove run in child process

* remove lavamoat-core, use ses, require lockdown directly

* revert childprocess

* patch package postinstall

* revert back child process

* add postinstall to ci

* revert node max space size to 1024

* put back loose-envify

* Disable sentry to see if e2e tetss pass

* use runLockdown, add as script in manifest

* remove global and require from runlockdown

* add more memory to tests

* upgrade resource class for prep-build & prep-build-test

* fix lint

* lint fix

* upgrade remote-redux-devtools

* skillfully re-add sentry

* lintfix

* fix lint

* put back beep

* remove envify, add loose-envify and patch-package in dev deps

* Replace patch with Yarn resolution (#9923)

Instead of patching `symbol-observable`, this ensures that all
versions of `symbol-observable` are resolved to the given range, even
if it contradicts the requested range.

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2020-11-24 11:26:43 +08:00

126 lines
3.1 KiB
JavaScript

const EventEmitter = require('events')
const spawn = require('cross-spawn')
const tasks = {}
const taskEvents = new EventEmitter()
module.exports = {
detectAndRunEntryTask,
tasks,
taskEvents,
createTask,
runTask,
composeSeries,
composeParallel,
runInChildProcess,
}
const { setupTaskDisplay } = require('./display')
function detectAndRunEntryTask() {
// get requested task name and execute
const taskName = process.argv[2]
if (!taskName) {
throw new Error(`MetaMask build: No task name specified`)
}
const skipStats = process.argv[3] === '--skip-stats'
runTask(taskName, { skipStats })
}
async function runTask(taskName, { skipStats } = {}) {
if (!(taskName in tasks)) {
throw new Error(`MetaMask build: Unrecognized task name "${taskName}"`)
}
if (!skipStats) {
setupTaskDisplay(taskEvents)
console.log(`running task "${taskName}"...`)
}
try {
await tasks[taskName]()
} catch (err) {
console.error(
`MetaMask build: Encountered an error while running task "${taskName}".`,
)
console.error(err)
process.exit(1)
}
taskEvents.emit('complete')
}
function createTask(taskName, taskFn) {
if (taskName in tasks) {
throw new Error(
`MetaMask build: task "${taskName}" already exists. Refusing to redefine`,
)
}
const task = instrumentForTaskStats(taskName, taskFn)
task.taskName = taskName
tasks[taskName] = task
return task
}
function runInChildProcess(task) {
const taskName = typeof task === 'string' ? task : task.taskName
if (!taskName) {
throw new Error(
`MetaMask build: runInChildProcess unable to identify task name`,
)
}
return instrumentForTaskStats(taskName, async () => {
const childProcess = spawn('yarn', ['build', taskName, '--skip-stats'], {
env: process.env,
})
// forward logs to main process
// skip the first stdout event (announcing the process command)
childProcess.stdout.once('data', () => {
childProcess.stdout.on('data', (data) =>
process.stdout.write(`${taskName}: ${data}`),
)
})
childProcess.stderr.on('data', (data) =>
process.stderr.write(`${taskName}: ${data}`),
)
// await end of process
await new Promise((resolve, reject) => {
childProcess.once('close', (errCode) => {
if (errCode !== 0) {
reject(
new Error(
`MetaMask build: runInChildProcess for task "${taskName}" encountered an error ${errCode}`,
),
)
return
}
resolve()
})
})
})
}
function instrumentForTaskStats(taskName, asyncFn) {
return async () => {
const start = Date.now()
taskEvents.emit('start', [taskName, start])
await asyncFn()
const end = Date.now()
taskEvents.emit('end', [taskName, start, end])
}
}
function composeSeries(...subtasks) {
return async () => {
const realTasks = subtasks
for (const subtask of realTasks) {
await subtask()
}
}
}
function composeParallel(...subtasks) {
return async () => {
const realTasks = subtasks
await Promise.all(realTasks.map((subtask) => subtask()))
}
}