Skip to content

Commit 9534f56

Browse files
authored
feat: first pass at functional prototype without subprocess support (bcoe#5)
BREAKING CHANGE: dropped subprocess support for the time being, while we march towards an initial implementation.
1 parent 38dff30 commit 9534f56

File tree

8 files changed

+101
-170
lines changed

8 files changed

+101
-170
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
node_modules
3+
.nyc_output

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ The above example will collect coverage for `foo.js` using v8's profiler.
1414

1515
TODO:
1616

17-
- [ ] write logic for converting v8 coverage output to [Istanbul Coverage.json format](https://github.com/gotwarlost/istanbul/blob/master/coverage.json.md).
17+
- [x] write logic for converting v8 coverage output to [Istanbul Coverage.json format](https://github.com/gotwarlost/istanbul/blob/master/coverage.json.md).
18+
* https://github.com/bcoe/v8-to-istanbul
19+
1820
- [ ] talk to Node.js project about silencing messages:
1921

2022
> `Debugger listening on ws://127.0.0.1:56399/e850110a-c5df-41d8-8ef2-400f6829617f`.
2123
22-
- [ ] figure out why `detailed` mode does not appear to be working.
23-
- [ ] figure out a better way to determine that all processes in event loop
24+
- [x] figure out why `detailed` mode does not appear to be working.
25+
* this is fixed in v8, as long as you start with `--inspect-brk` you
26+
can collect coverage in detailed mode.
27+
- [x] figure out a better way to determine that all processes in event loop
2428
have terminated (except the inspector session).
25-
- [ ] process.exit() can't perform an async operation; how can we track coverage
29+
- [x] process.exit() can't perform an async operation; how can we track coverage
2630
for scripts that exit?
31+
* we can now listen for the `Runtime.executionContextDestroyed` event.
32+
- [ ]

bin/c8.js

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,58 @@
11
#!/usr/bin/env node
22

33
const argv = require('yargs').parse()
4+
const CRI = require('chrome-remote-interface')
5+
const getPort = require('get-port');
46
const foreground = require('foreground-child')
5-
const sw = require('spawn-wrap')
7+
const waitTillPortOpen = require('wait-till-port-open')
68

7-
if (argv._.length) {
8-
sw([require.resolve('./wrap')])
9-
foreground(process.argv.slice(2))
9+
getPort().then(async port => {
10+
foreground(
11+
['node', `--inspect-brk=${port}`].concat(process.argv.slice(2)),
12+
(done) => {
13+
console.info('actually got here')
14+
}
15+
)
16+
try {
17+
await waitTillPortOpen(port)
18+
const client = await CRI({port: port})
19+
20+
const {Debugger, Runtime, Profiler} = client
21+
await Runtime.runIfWaitingForDebugger()
22+
await Runtime.enable()
23+
await Profiler.enable()
24+
await Profiler.startPreciseCoverage({callCount: true, detailed: true})
25+
await Debugger.enable()
26+
await Debugger.paused()
27+
await Debugger.resume()
28+
29+
client.on('event', async (message) => {
30+
// console.info(message)
31+
if (message.method === 'Runtime.executionContextDestroyed') {
32+
await outputCoverage(Profiler)
33+
client.close()
34+
}
35+
})
36+
37+
} catch (err) {
38+
console.error(err)
39+
process.exit(1)
40+
}
41+
})
42+
43+
async function outputCoverage (Profiler) {
44+
const IGNORED_PATHS = [
45+
/\/bin\/wrap.js/,
46+
/\/node_modules\//,
47+
/node-spawn-wrap/
48+
]
49+
let {result} = await Profiler.takePreciseCoverage()
50+
result = result.filter(coverage => {
51+
for (var ignored, i = 0; (ignored = IGNORED_PATHS[i]) !== undefined; i++) {
52+
if (ignored.test(coverage.url)) return false
53+
}
54+
if (!/^\//.test(coverage.url)) return false
55+
else return true
56+
})
57+
console.log(JSON.stringify(result, null, 2))
1058
}

bin/wrap.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

index.js

Whitespace-only changes.

package-lock.json

Lines changed: 26 additions & 109 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"chrome-remote-interface": "^0.25.2",
2626
"foreground-child": "^1.5.6",
2727
"get-port": "^3.2.0",
28-
"spawn-wrap": "=1.3.8",
28+
"wait-till-port-open": "^1.0.0",
2929
"yargs": "^10.0.3"
3030
},
3131
"devDependencies": {

0 commit comments

Comments
 (0)