| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const { Session } = require('inspector');
|
|---|
| 4 | const { promisify } = require('util');
|
|---|
| 5 |
|
|---|
| 6 | class CoverageInstrumenter {
|
|---|
| 7 | constructor() {
|
|---|
| 8 | this.session = new Session();
|
|---|
| 9 |
|
|---|
| 10 | this.postSession = promisify(this.session.post.bind(this.session));
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | async startInstrumenting() {
|
|---|
| 14 | this.session.connect();
|
|---|
| 15 |
|
|---|
| 16 | await this.postSession('Debugger.enable');
|
|---|
| 17 |
|
|---|
| 18 | await this.postSession('Profiler.enable');
|
|---|
| 19 |
|
|---|
| 20 | await this.postSession('Profiler.startPreciseCoverage', {
|
|---|
| 21 | callCount: true,
|
|---|
| 22 | detailed: true,
|
|---|
| 23 | });
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | async stopInstrumenting() {
|
|---|
| 27 | const {result} = await this.postSession(
|
|---|
| 28 | 'Profiler.takePreciseCoverage',
|
|---|
| 29 | );
|
|---|
| 30 |
|
|---|
| 31 | await this.postSession('Profiler.stopPreciseCoverage');
|
|---|
| 32 |
|
|---|
| 33 | await this.postSession('Profiler.disable');
|
|---|
| 34 |
|
|---|
| 35 | await this.postSession('Debugger.disable');
|
|---|
| 36 |
|
|---|
| 37 | // When using networked filesystems on Windows, v8 sometimes returns URLs
|
|---|
| 38 | // of the form file:////<host>/path. These URLs are not well understood
|
|---|
| 39 | // by NodeJS (see https://github.com/nodejs/node/issues/48530).
|
|---|
| 40 | // We circumvent this issue here by fixing these URLs.
|
|---|
| 41 | // FWIW, Python has special code to deal with URLs like this
|
|---|
| 42 | // https://github.com/python/cpython/blob/bef1c8761e3b0dfc5708747bb646ad8b669cbd67/Lib/nturl2path.py#L22C1-L22C1
|
|---|
| 43 | if (process.platform === 'win32') {
|
|---|
| 44 | const prefix = 'file:////';
|
|---|
| 45 | result.forEach(res => {
|
|---|
| 46 | if (res.url.startsWith(prefix)) {
|
|---|
| 47 | res.url = 'file://' + res.url.slice(prefix.length);
|
|---|
| 48 | }
|
|---|
| 49 | })
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | return result;
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | module.exports.CoverageInstrumenter = CoverageInstrumenter;
|
|---|