index.js 1.5 KB

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