PHP 8.2.30
Preview: timers.js Size: 2.07 KB
/proc/thread-self/root/proc/self/root/opt/alt/alt-nodejs20/root/usr/lib/node_modules/npm/lib/utils/timers.js

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

Directory Contents

Dirs: 0 × Files: 34

Name Size Perms Modified Actions
1.02 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
3.21 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
2.89 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
1.56 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
1.85 KB lrwxr-xr-x 2026-02-23 03:24:41
Edit Download
1.16 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
15.38 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
14.60 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
3.03 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
2.56 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
629 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
4.70 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
1.91 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
802 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
1.71 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
1.10 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
583 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
177 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
7.78 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
2.02 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
2.36 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
767 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
252 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
9.56 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
1.91 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
886 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
5.65 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
5.21 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
4.59 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
3.47 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
2.07 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download
1013 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
1023 B lrw-r--r-- 2026-02-23 03:24:41
Edit Download
11.92 KB lrw-r--r-- 2026-02-23 03:24:41
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).