Skip to content

Commit a3925e9

Browse files
authored
feat: function to cleanup allocated resources after usage (#161)
1 parent aac4e36 commit a3925e9

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ converter.applyCoverage([
3232
// output coverage information in a form that can
3333
// be consumed by Istanbul.
3434
console.info(JSON.stringify(converter.toIstanbul()))
35+
36+
// cleanup resources allocated in "load" (i.e. by the source-map dependency),
37+
// the converter may not be used anymore afterwards
38+
converter.destroy()
3539
```
3640

3741
## Ignoring Uncovered Lines

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ declare type Sources =
1515
}
1616
declare class V8ToIstanbul {
1717
load(): Promise<void>
18+
destroy(): void
1819
applyCoverage(blocks: ReadonlyArray<Profiler.FunctionCoverage>): void
1920
toIstanbul(): CoverageMapData
2021
}

lib/v8-to-istanbul.js

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ module.exports = class V8ToIstanbul {
8383
}
8484
}
8585

86+
destroy () {
87+
if (this.sourceMap) {
88+
this.sourceMap.destroy()
89+
this.sourceMap = undefined
90+
}
91+
}
92+
8693
_resolveSource (rawSourceMap, sourcePath) {
8794
if (sourcePath.startsWith('file://')) {
8895
return fileURLToPath(sourcePath)

test/v8-to-istanbul.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const sourcemap = require('source-map')
1010
const assert = require('assert')
1111

1212
require('tap').mochaGlobals()
13-
require('should')
13+
const should = require('should')
1414

1515
describe('V8ToIstanbul', async () => {
1616
describe('constructor', () => {
@@ -22,6 +22,8 @@ describe('V8ToIstanbul', async () => {
2222
v8ToIstanbul.covSources[0].source.lines.length.should.equal(48)
2323
v8ToIstanbul.covSources.length.should.equal(1)
2424
v8ToIstanbul.wrapperLength.should.equal(0) // common-js header.
25+
26+
v8ToIstanbul.destroy()
2527
})
2628

2729
it('handles ESM style paths', async () => {
@@ -33,6 +35,8 @@ describe('V8ToIstanbul', async () => {
3335
v8ToIstanbul.covSources[0].source.lines.length.should.equal(48)
3436
v8ToIstanbul.covSources.length.should.equal(1)
3537
v8ToIstanbul.wrapperLength.should.equal(0) // ESM header.
38+
39+
v8ToIstanbul.destroy()
3640
})
3741

3842
it('handles source maps with sourceRoot', async () => {
@@ -60,6 +64,8 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
6064
await v8ToIstanbul.load()
6165

6266
v8ToIstanbul.path.should.equal(absoluteSourceFilePath)
67+
68+
v8ToIstanbul.destroy()
6369
})
6470

6571
it('handles sourceContent', async () => {
@@ -91,6 +97,8 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
9197
// if the source is transpiled and since we didn't inline the source map into the transpiled source file
9298
// that means it was bale to access the content via the provided sources object
9399
v8ToIstanbul.sourceTranspiled.should.not.be.undefined()
100+
101+
v8ToIstanbul.destroy()
94102
})
95103

96104
it('should clamp line source column >= 0', async () => {
@@ -121,6 +129,8 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
121129
endOffset: matchedNewLineChar + 10
122130
}]
123131
}])
132+
133+
v8ToIstanbul.destroy()
124134
})
125135

126136
it('should exclude files when passing excludePath', async () => {
@@ -139,6 +149,8 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
139149
}]
140150
}])
141151
Object.keys(v8ToIstanbul.toIstanbul()).should.eql(['/src/index.ts', '/src/utils.ts'].map(path.normalize))
152+
153+
v8ToIstanbul.destroy()
142154
})
143155
})
144156

@@ -157,6 +169,7 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
157169
0
158170
)
159171
await v8ToIstanbul.load()
172+
v8ToIstanbul.destroy()
160173
})
161174

162175
it('should handle relative sourceRoots correctly', async () => {
@@ -166,6 +179,7 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
166179
)
167180
await v8ToIstanbul.load()
168181
assert(v8ToIstanbul.path.includes(path.normalize('v8-to-istanbul/test/fixtures/one-up/relative-source-root.js')))
182+
v8ToIstanbul.destroy()
169183
})
170184

171185
it('should handles source maps with multiple sources', async () => {
@@ -177,9 +191,25 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
177191

178192
v8ToIstanbul.covSources.length.should.equal(3)
179193
Object.keys(v8ToIstanbul.toIstanbul()).should.eql(['/webpack/bootstrap', '/src/index.ts', '/src/utils.ts'].map(path.normalize))
194+
195+
v8ToIstanbul.destroy()
180196
})
181197
})
182198

199+
it('destroy cleans up source map', async () => {
200+
const v8ToIstanbul = new V8ToIstanbul(
201+
pathToFileURL(require.resolve('./fixtures/scripts/empty.compiled.js')).href
202+
)
203+
await v8ToIstanbul.load()
204+
// assertion only to check test data and setup - source map must be loaded,
205+
// otherwise destroy would have no effect anyway
206+
assert(v8ToIstanbul.sourceMap !== undefined, 'Test fixture must load a source map')
207+
208+
v8ToIstanbul.destroy()
209+
210+
should.not.exist(v8ToIstanbul.sourceMap)
211+
})
212+
183213
// execute JavaScript files in fixtures directory; these
184214
// files contain the raw v8 output along with a set of
185215
// assertions. the original scripts can be found in the

0 commit comments

Comments
 (0)