马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const EE = require('node:events')
const fs = require('node:fs')
const { log, time } = require('proc-log')
 
const INITIAL_TIMER = 'npm'
 
class Timers extends EE {
  #file
  #timing
 
  #unfinished = new Map()
  #finished = {}
 
  constructor () {
    super()
    this.on()
    time.start(INITIAL_TIMER)
    this.started = this.#unfinished.get(INITIAL_TIMER)
  }
 
  on () {
    process.on('time', this.#timeHandler)
  }
 
  off () {
    process.off('time', this.#timeHandler)
  }
 
  load ({ path, timing } = {}) {
    this.#timing = timing
    this.#file = `${path}timing.json`
  }
 
  finish (metadata) {
    time.end(INITIAL_TIMER)
 
    for (const [name, timer] of this.#unfinished) {
      log.silly('unfinished npm timer', name, timer)
    }
 
    if (!this.#timing) {
      // Not in timing mode, nothing else to do here
      return
    }
 
    try {
      this.#writeFile(metadata)
      log.info('timing', `Timing info written to: ${this.#file}`)
    } catch (e) {
      log.warn('timing', `could not write timing file: ${e}`)
    }
  }
 
  #writeFile (metadata) {
    const globalStart = this.started
    const globalEnd = this.#finished[INITIAL_TIMER]
    const content = {
      metadata,
      timers: this.#finished,
      // add any unfinished timers with their relative start/end
      unfinishedTimers: [...this.#unfinished.entries()].reduce((acc, [name, start]) => {
        acc[name] = [start - globalStart, globalEnd - globalStart]
        return acc
      }, {}),
    }
    fs.writeFileSync(this.#file, JSON.stringify(content) + '\n')
  }
 
  #timeHandler = (level, name) => {
    const now = Date.now()
    switch (level) {
      case time.KEYS.start:
        this.#unfinished.set(name, now)
        break
      case time.KEYS.end: {
        if (this.#unfinished.has(name)) {
          const ms = now - this.#unfinished.get(name)
          this.#finished[name] = ms
          this.#unfinished.delete(name)
          log.timing(name, `Completed in ${ms}ms`)
        } else {
          log.silly('timing', `Tried to end timer that doesn't exist: ${name}`)
        }
      }
    }
  }
}
 
module.exports = Timers