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