'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
'use strict';
 
const { Session } = require('inspector');
const { promisify } = require('util');
 
class CoverageInstrumenter {
  constructor() {
    this.session = new Session();
 
    this.postSession = promisify(this.session.post.bind(this.session));
  }
 
  async startInstrumenting() {
    this.session.connect();
 
    await this.postSession('Profiler.enable');
 
    await this.postSession('Profiler.startPreciseCoverage', {
      callCount: true,
      detailed: true,
    });
  }
 
  async stopInstrumenting() {
    const {result} = await this.postSession(
      'Profiler.takePreciseCoverage',
    );
 
    await this.postSession('Profiler.stopPreciseCoverage');
 
    await this.postSession('Profiler.disable');
 
    // When using networked filesystems on Windows, v8 sometimes returns URLs
    // of the form file:////<host>/path. These URLs are not well understood
    // by NodeJS (see https://github.com/nodejs/node/issues/48530).
    // We circumvent this issue here by fixing these URLs.
    // FWIW, Python has special code to deal with URLs like this
    // https://github.com/python/cpython/blob/bef1c8761e3b0dfc5708747bb646ad8b669cbd67/Lib/nturl2path.py#L22C1-L22C1
    if (process.platform === 'win32') {
      const prefix = 'file:////';
      result.forEach(res => {
        if (res.url.startsWith(prefix)) {
          res.url = 'file://' + res.url.slice(prefix.length);
        }
      })
    }
 
    return result;
  }
}
 
module.exports.CoverageInstrumenter = CoverageInstrumenter;