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

const EE = require('events')
const fs = require('fs')
const log = require('./log-shim')

// This is an event emiiter but on/off
// only listen on a single internal event that gets
// emitted whenever a timer ends
class Timers extends EE {
  file = null

  #unfinished = new Map()
  #finished = {}
  #onTimeEnd = Symbol('onTimeEnd')
  #initialListener = null
  #initialTimer = null

  constructor ({ listener = null, start = 'npm' } = {}) {
    super()
    this.#initialListener = listener
    this.#initialTimer = start
    this.#init()
  }

  get unfinished () {
    return this.#unfinished
  }

  get finished () {
    return this.#finished
  }

  #init () {
    this.on()
    if (this.#initialListener) {
      this.on(this.#initialListener)
    }
    process.emit('time', this.#initialTimer)
    this.started = this.#unfinished.get(this.#initialTimer)
  }

  on (listener) {
    if (listener) {
      super.on(this.#onTimeEnd, listener)
    } else {
      process.on('time', this.#timeListener)
      process.on('timeEnd', this.#timeEndListener)
    }
  }

  off (listener) {
    if (listener) {
      super.off(this.#onTimeEnd, listener)
    } else {
      this.removeAllListeners(this.#onTimeEnd)
      process.off('time', this.#timeListener)
      process.off('timeEnd', this.#timeEndListener)
    }
  }

  time (name, fn) {
    process.emit('time', name)
    const end = () => process.emit('timeEnd', name)
    if (typeof fn === 'function') {
      const res = fn()
      return res && res.finally ? res.finally(end) : (end(), res)
    }
    return end
  }

  load ({ path } = {}) {
    if (path) {
      this.file = `${path}timing.json`
    }
  }

  writeFile (metadata) {
    if (!this.file) {
      return
    }

    try {
      const globalStart = this.started
      const globalEnd = this.#finished.npm || Date.now()
      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')
    } catch (e) {
      this.file = null
      log.warn('timing', `could not write timing file: ${e}`)
    }
  }

  #timeListener = (name) => {
    this.#unfinished.set(name, Date.now())
  }

  #timeEndListener = (name) => {
    if (this.#unfinished.has(name)) {
      const ms = Date.now() - this.#unfinished.get(name)
      this.#finished[name] = ms
      this.#unfinished.delete(name)
      this.emit(this.#onTimeEnd, name, ms)
    } else {
      log.silly('timing', "Tried to end timer that doesn't exist:", name)
    }
  }
}

module.exports = Timers

Directory Contents

Dirs: 2 × Files: 34

Name Size Perms Modified Actions
- drwxr-xr-x 2024-03-03 22:54:11
Edit Download
config DIR
- drwxr-xr-x 2024-03-03 22:54:11
Edit Download
173 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.02 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.80 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.16 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.56 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.85 KB lrwxr-xr-x 2023-12-06 18:07:50
Edit Download
1.46 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.43 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
13.53 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
6.74 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.50 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.55 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
629 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.93 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
802 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
233 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
7.03 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.68 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.04 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.44 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.09 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.23 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
252 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
412 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
9.56 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.00 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
876 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
4.97 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
807 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.90 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.73 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
4.19 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1023 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
558 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download

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