diff --git a/index.d.ts b/index.d.ts index 0ca49f5..458236d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -40,8 +40,12 @@ export interface LiteralInfo { value: string locations: Array } -export class Rewriter { +export class NonCacheRewriter { constructor(config?: RewriterConfig | undefined | null) rewrite(code: string, file: string): Result csiMethods(): Array } + +export const Rewriter: NonCacheRewriter + +export function cacheRewrittenSourceMap(filename: string, fileContent: string): void diff --git a/main.js b/main.js index 163cc3b..8fe02f3 100644 --- a/main.js +++ b/main.js @@ -19,7 +19,8 @@ class DummyRewriter { } let NativeRewriter -class CacheRewriter { + +class NonCacheRewriter { constructor (config) { if (NativeRewriter) { this.nativeRewriter = new NativeRewriter(config) @@ -30,18 +31,7 @@ class CacheRewriter { } rewrite (code, file) { - const response = this.nativeRewriter.rewrite(code, file) - - try { - const { metrics, content } = response - if (metrics?.status === 'modified') { - cacheRewrittenSourceMap(file, content) - } - } catch (e) { - this.logError(e) - } - - return response + return this.nativeRewriter.rewrite(code, file) } csiMethods () { @@ -65,12 +55,29 @@ class CacheRewriter { } } -function getRewriter () { +class CacheRewriter extends NonCacheRewriter { + rewrite (code, file) { + const response = super.rewrite(code, file) + + try { + const { metrics, content } = response + if (metrics?.status === 'modified') { + cacheRewrittenSourceMap(file, content) + } + } catch (e) { + this.logError(e) + } + + return response + } +} + +function getRewriter (withoutCache = false) { try { const iastRewriter = require('./wasm/wasm_iast_rewriter') NativeRewriter = iastRewriter.Rewriter - return CacheRewriter + return withoutCache ? NonCacheRewriter : CacheRewriter } catch (e) { return DummyRewriter } @@ -78,8 +85,10 @@ function getRewriter () { module.exports = { Rewriter: getRewriter(), + NonCacheRewriter: getRewriter(true), DummyRewriter, getPrepareStackTrace, getOriginalPathAndLineFromSourceMap, - kSymbolPrepareStackTrace + kSymbolPrepareStackTrace, + cacheRewrittenSourceMap } diff --git a/test/main.spec.js b/test/main.spec.js index c1717ef..3867233 100644 --- a/test/main.spec.js +++ b/test/main.spec.js @@ -30,33 +30,69 @@ describe('main', () => { cacheRewrittenSourceMap } }) - - rewriter = new main.Rewriter() }) - it('loads sourceMap when source file has been modified', () => { - status = 'modified' + describe('default Rewriter', () => { + beforeEach(() => { + rewriter = new main.Rewriter() + }) - const response = rewriter.rewrite('content', 'file') + it('loads sourceMap when source file has been modified', () => { + status = 'modified' - expect(response.metrics.status).to.eq('modified') - expect(cacheRewrittenSourceMap).to.be.calledOnceWith('file', 'content') - }) + const response = rewriter.rewrite('content', 'file') - it('does not load sourceMap when source file has not been modified', () => { - status = 'notmodified' + expect(response.metrics.status).to.eq('modified') + expect(cacheRewrittenSourceMap).to.be.calledOnceWith('file', 'content') + }) + + it('does not load sourceMap when source file has not been modified', () => { + status = 'notmodified' - const response = rewriter.rewrite('content', 'file') + const response = rewriter.rewrite('content', 'file') - expect(response.metrics.status).to.eq('notmodified') - expect(cacheRewrittenSourceMap).to.not.be.called + expect(response.metrics.status).to.eq('notmodified') + expect(cacheRewrittenSourceMap).to.not.be.called + }) + + it('should catch errors produced in cacheRewrittenSourceMap', () => { + status = 'modified' + + cacheRewrittenSourceMap.throws(() => new Error('Error reading sourceMap file')) + + expect(() => rewriter.rewrite('content', 'file')).to.not.throw() + }) }) - it('should catch errors produced in cacheRewrittenSourceMap', () => { - status = 'modified' + describe('NonCacheRewriter', () => { + beforeEach(() => { + rewriter = new main.NonCacheRewriter() + }) - cacheRewrittenSourceMap.throws(() => new Error('Error reading sourceMap file')) + it('does not load sourceMap when source file has been modified', () => { + status = 'modified' - expect(() => rewriter.rewrite('content', 'file')).to.not.throw() + const response = rewriter.rewrite('content', 'file') + + expect(response.metrics.status).to.eq('modified') + expect(cacheRewrittenSourceMap).to.not.be.called + }) + + it('does not load sourceMap when source file has not been modified', () => { + status = 'notmodified' + + const response = rewriter.rewrite('content', 'file') + + expect(response.metrics.status).to.eq('notmodified') + expect(cacheRewrittenSourceMap).to.not.be.called + }) + + it('should catch errors produced in cacheRewrittenSourceMap', () => { + status = 'modified' + + cacheRewrittenSourceMap.throws(() => new Error('Error reading sourceMap file')) + + expect(() => rewriter.rewrite('content', 'file')).to.not.throw() + }) }) })