| 1 |
'use strict' |
| 2 |
const { v4: uuidv4 } = require('uuid'); |
| 3 |
const archy = require('archy') |
| 4 |
const libCoverage = require('istanbul-lib-coverage') |
| 5 |
const {dirname, resolve} = require('path') |
| 6 |
const {promisify} = require('util') |
| 7 |
/* Shallow clone so we can promisify in-place */ |
| 8 |
const fs = { ...require('fs') } |
| 9 |
const {spawn} = require('child_process') |
| 10 |
const rimraf = promisify(require('rimraf')) |
| 11 |
const pMap = require('p-map') |
| 12 |
|
| 13 |
const _nodes = Symbol('nodes') |
| 14 |
const _label = Symbol('label') |
| 15 |
const _coverageMap = Symbol('coverageMap') |
| 16 |
const _processInfoDirectory = Symbol('processInfo.directory') |
| 17 |
// shared symbol for testing |
| 18 |
const _spawnArgs = Symbol.for('spawnArgs') |
| 19 |
|
| 20 |
;['writeFile', 'readFile', 'readdir'].forEach(fn => { |
| 21 |
fs[fn] = promisify(fs[fn]) |
| 22 |
}) |
| 23 |
|
| 24 |
// the enumerable fields |
| 25 |
const defaults = () => ({ |
| 26 |
parent: null, |
| 27 |
pid: process.pid, |
| 28 |
argv: process.argv, |
| 29 |
execArgv: process.execArgv, |
| 30 |
cwd: process.cwd(), |
| 31 |
time: Date.now(), |
| 32 |
ppid: process.ppid, |
| 33 |
coverageFilename: null, |
| 34 |
externalId: '', |
| 35 |
[_nodes]: [], |
| 36 |
[_label]: null, |
| 37 |
[_coverageMap]: null |
| 38 |
}) |
| 39 |
|
| 40 |
/* istanbul ignore next */ |
| 41 |
const fromEntries = Object.fromEntries || ( |
| 42 |
entries => entries.reduce((obj, [name, value]) => { |
| 43 |
obj[name] = value |
| 44 |
return obj |
| 45 |
}, {}) |
| 46 |
) |
| 47 |
|
| 48 |
class ProcessInfo { |
| 49 |
constructor (fields = {}) { |
| 50 |
Object.assign(this, defaults(), fields) |
| 51 |
|
| 52 |
if (!this.uuid) { |
| 53 |
this.uuid = uuidv4() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
get nodes () { |
| 58 |
return this[_nodes] |
| 59 |
} |
| 60 |
|
| 61 |
set nodes (n) { |
| 62 |
this[_nodes] = n |
| 63 |
} |
| 64 |
|
| 65 |
set directory (d) { |
| 66 |
this[_processInfoDirectory] = resolve(d) |
| 67 |
} |
| 68 |
|
| 69 |
get directory () { |
| 70 |
return this[_processInfoDirectory] |
| 71 |
} |
| 72 |
|
| 73 |
saveSync () { |
| 74 |
const f = resolve(this.directory, this.uuid + '.json') |
| 75 |
fs.writeFileSync(f, JSON.stringify(this), 'utf-8') |
| 76 |
} |
| 77 |
|
| 78 |
async save () { |
| 79 |
const f = resolve(this.directory, this.uuid + '.json') |
| 80 |
await fs.writeFile(f, JSON.stringify(this), 'utf-8') |
| 81 |
} |
| 82 |
|
| 83 |
async getCoverageMap (nyc) { |
| 84 |
if (this[_coverageMap]) { |
| 85 |
return this[_coverageMap] |
| 86 |
} |
| 87 |
|
| 88 |
const childMaps = await Promise.all(this.nodes.map(child => child.getCoverageMap(nyc))) |
| 89 |
|
| 90 |
this[_coverageMap] = await mapMerger(nyc, this.coverageFilename, childMaps) |
| 91 |
|
| 92 |
return this[_coverageMap] |
| 93 |
} |
| 94 |
|
| 95 |
get label () { |
| 96 |
if (this[_label]) { |
| 97 |
return this[_label] |
| 98 |
} |
| 99 |
|
| 100 |
const covInfo = this[_coverageMap] |
| 101 |
? '\n ' + this[_coverageMap].getCoverageSummary().lines.pct + ' % Lines' |
| 102 |
: '' |
| 103 |
|
| 104 |
return this[_label] = this.argv.join(' ') + covInfo |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
const mapMerger = async (nyc, filename, maps) => { |
| 109 |
const map = libCoverage.createCoverageMap({}) |
| 110 |
if (filename) { |
| 111 |
map.merge(await nyc.coverageFileLoad(filename)) |
| 112 |
} |
| 113 |
maps.forEach(otherMap => map.merge(otherMap)) |
| 114 |
return map |
| 115 |
} |
| 116 |
|
| 117 |
// Operations on the processinfo database as a whole, |
| 118 |
// and the root of the tree rendering operation. |
| 119 |
class ProcessDB { |
| 120 |
constructor (directory) { |
| 121 |
if (!directory) { |
| 122 |
const nycConfig = process.env.NYC_CONFIG; |
| 123 |
if (nycConfig) { |
| 124 |
directory = resolve(JSON.parse(nycConfig).tempDir, 'processinfo') |
| 125 |
} |
| 126 |
|
| 127 |
if (!directory) { |
| 128 |
throw new TypeError('must provide directory argument when outside of NYC') |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
Object.defineProperty(this, 'directory', { get: () => directory, enumerable: true }) |
| 133 |
this.nodes = [] |
| 134 |
this[_label] = null |
| 135 |
this[_coverageMap] = null |
| 136 |
} |
| 137 |
|
| 138 |
get label () { |
| 139 |
if (this[_label]) { |
| 140 |
return this[_label] |
| 141 |
} |
| 142 |
|
| 143 |
const covInfo = this[_coverageMap] |
| 144 |
? '\n ' + this[_coverageMap].getCoverageSummary().lines.pct + ' % Lines' |
| 145 |
: '' |
| 146 |
|
| 147 |
return this[_label] = 'nyc' + covInfo |
| 148 |
} |
| 149 |
|
| 150 |
async getCoverageMap (nyc) { |
| 151 |
if (this[_coverageMap]) { |
| 152 |
return this[_coverageMap] |
| 153 |
} |
| 154 |
|
| 155 |
const childMaps = await Promise.all(this.nodes.map(child => child.getCoverageMap(nyc))) |
| 156 |
this[_coverageMap] = await mapMerger(nyc, undefined, childMaps) |
| 157 |
return this[_coverageMap] |
| 158 |
} |
| 159 |
|
| 160 |
async renderTree (nyc) { |
| 161 |
await this.buildProcessTree() |
| 162 |
await this.getCoverageMap(nyc) |
| 163 |
|
| 164 |
return archy(this) |
| 165 |
} |
| 166 |
|
| 167 |
async buildProcessTree () { |
| 168 |
const infos = await this.readProcessInfos(this.directory) |
| 169 |
const index = await this.readIndex() |
| 170 |
for (const id in index.processes) { |
| 171 |
const node = infos[id] |
| 172 |
if (!node) { |
| 173 |
throw new Error(`Invalid entry in processinfo index: ${id}`) |
| 174 |
} |
| 175 |
const idx = index.processes[id] |
| 176 |
node.nodes = idx.children.map(id => infos[id]) |
| 177 |
.sort((a, b) => a.time - b.time) |
| 178 |
if (!node.parent) { |
| 179 |
this.nodes.push(node) |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
async _readJSON (file) { |
| 185 |
if (Array.isArray(file)) { |
| 186 |
const result = await pMap( |
| 187 |
file, |
| 188 |
f => this._readJSON(f), |
| 189 |
{ concurrency: 8 } |
| 190 |
) |
| 191 |
return result.filter(Boolean) |
| 192 |
} |
| 193 |
|
| 194 |
try { |
| 195 |
return JSON.parse(await fs.readFile(resolve(this.directory, file), 'utf-8')) |
| 196 |
} catch (error) { |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
async readProcessInfos () { |
| 201 |
const files = await fs.readdir(this.directory) |
| 202 |
const fileData = await this._readJSON(files.filter(f => f !== 'index.json')) |
| 203 |
|
| 204 |
return fromEntries(fileData.map(info => [ |
| 205 |
info.uuid, |
| 206 |
new ProcessInfo(info) |
| 207 |
])) |
| 208 |
} |
| 209 |
|
| 210 |
_createIndex (infos) { |
| 211 |
const infoMap = fromEntries(infos.map(info => [ |
| 212 |
info.uuid, |
| 213 |
Object.assign(info, {children: []}) |
| 214 |
])) |
| 215 |
|
| 216 |
// create all the parent-child links |
| 217 |
infos.forEach(info => { |
| 218 |
if (info.parent) { |
| 219 |
const parentInfo = infoMap[info.parent] |
| 220 |
if (parentInfo && !parentInfo.children.includes(info.uuid)) { |
| 221 |
parentInfo.children.push(info.uuid) |
| 222 |
} |
| 223 |
} |
| 224 |
}) |
| 225 |
|
| 226 |
// figure out which files were touched by each process. |
| 227 |
const files = infos.reduce((files, info) => { |
| 228 |
info.files.forEach(f => { |
| 229 |
files[f] = files[f] || [] |
| 230 |
if (!files[f].includes(info.uuid)) { |
| 231 |
files[f].push(info.uuid) |
| 232 |
} |
| 233 |
}) |
| 234 |
return files |
| 235 |
}, {}) |
| 236 |
|
| 237 |
const processes = fromEntries(infos.map(info => [ |
| 238 |
info.uuid, |
| 239 |
{ |
| 240 |
parent: info.parent, |
| 241 |
...(info.externalId ? { externalId: info.externalId } : {}), |
| 242 |
children: Array.from(info.children) |
| 243 |
} |
| 244 |
])) |
| 245 |
|
| 246 |
const eidList = new Set() |
| 247 |
const externalIds = fromEntries(infos.filter(info => info.externalId).map(info => { |
| 248 |
if (eidList.has(info.externalId)) { |
| 249 |
throw new Error( |
| 250 |
`External ID ${info.externalId} used by multiple processes`) |
| 251 |
} |
| 252 |
|
| 253 |
eidList.add(info.externalId) |
| 254 |
|
| 255 |
const children = Array.from(info.children) |
| 256 |
// flatten the descendant sets of all the externalId procs |
| 257 |
// push the next generation onto the list so we accumulate them all |
| 258 |
for (let i = 0; i < children.length; i++) { |
| 259 |
children.push(...processes[children[i]].children.filter(uuid => !children.includes(uuid))) |
| 260 |
} |
| 261 |
|
| 262 |
return [ |
| 263 |
info.externalId, |
| 264 |
{ |
| 265 |
root: info.uuid, |
| 266 |
children |
| 267 |
} |
| 268 |
] |
| 269 |
})) |
| 270 |
|
| 271 |
return { processes, files, externalIds } |
| 272 |
} |
| 273 |
|
| 274 |
async writeIndex () { |
| 275 |
const {directory} = this |
| 276 |
const files = await fs.readdir(directory) |
| 277 |
const infos = await this._readJSON(files.filter(f => f !== 'index.json')) |
| 278 |
const index = this._createIndex(infos) |
| 279 |
const indexFile = resolve(directory, 'index.json') |
| 280 |
await fs.writeFile(indexFile, JSON.stringify(index)) |
| 281 |
|
| 282 |
return index |
| 283 |
} |
| 284 |
|
| 285 |
async readIndex () { |
| 286 |
return await this._readJSON('index.json') || await this.writeIndex() |
| 287 |
} |
| 288 |
|
| 289 |
// delete all coverage and processinfo for a given process |
| 290 |
// Warning! Doing this makes the index out of date, so make sure |
| 291 |
// to update it when you're done! |
| 292 |
// Not multi-process safe, because it cannot be done atomically. |
| 293 |
async expunge (id) { |
| 294 |
const index = await this.readIndex() |
| 295 |
const entry = index.externalIds[id] |
| 296 |
if (!entry) { |
| 297 |
return |
| 298 |
} |
| 299 |
|
| 300 |
await pMap( |
| 301 |
[].concat( |
| 302 |
`${dirname(this.directory)}/${entry.root}.json`, |
| 303 |
`${this.directory}/${entry.root}.json`, |
| 304 |
...entry.children.map(c => [ |
| 305 |
`${dirname(this.directory)}/${c}.json`, |
| 306 |
`${this.directory}/${c}.json` |
| 307 |
]) |
| 308 |
), |
| 309 |
f => rimraf(f), |
| 310 |
{ concurrency: 8 } |
| 311 |
) |
| 312 |
} |
| 313 |
|
| 314 |
[_spawnArgs] (name, file, args, options) { |
| 315 |
if (!Array.isArray(args)) { |
| 316 |
options = args |
| 317 |
args = [] |
| 318 |
} |
| 319 |
if (!options) { |
| 320 |
options = {} |
| 321 |
} |
| 322 |
|
| 323 |
if (!process.env.NYC_CONFIG) { |
| 324 |
const nyc = options.nyc || 'nyc' |
| 325 |
const nycArgs = options.nycArgs || [] |
| 326 |
args = [...nycArgs, file, ...args] |
| 327 |
file = nyc |
| 328 |
} |
| 329 |
|
| 330 |
options.env = { |
| 331 |
...(options.env || process.env), |
| 332 |
NYC_PROCESSINFO_EXTERNAL_ID: name |
| 333 |
} |
| 334 |
|
| 335 |
return [name, file, args, options] |
| 336 |
} |
| 337 |
|
| 338 |
// spawn an externally named process |
| 339 |
async spawn (...spawnArgs) { |
| 340 |
const [name, file, args, options] = this[_spawnArgs](...spawnArgs) |
| 341 |
await this.expunge(name) |
| 342 |
return spawn(file, args, options) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
exports.ProcessDB = ProcessDB |
| 347 |
exports.ProcessInfo = ProcessInfo |