@datadog/native-iast-rewriter
Advanced tools
Comparing version 0.1.0-beta.1 to 0.1.0-beta.2
@@ -9,5 +9,5 @@ 'use strict' | ||
const pathSourceMapsCache = new Map() | ||
const rewrittenSourceMapsCache = new Map() | ||
function readAndCacheSourceMap (filename, filePath) { | ||
const fileContent = fs.readFileSync(filename).toString() | ||
function generateSourceMapFromFileContent (fileContent, filePath) { | ||
const fileLines = fileContent.trim().split('\n') | ||
@@ -27,3 +27,15 @@ const lastLine = fileLines[fileLines.length - 1] | ||
if (rawSourceMap) { | ||
const sm = new SourceMap(JSON.parse(rawSourceMap)) | ||
return new SourceMap(JSON.parse(rawSourceMap)) | ||
} | ||
} | ||
function cacheRewrittenSourceMap (filename, fileContent) { | ||
const sm = generateSourceMapFromFileContent(fileContent, getFilePathFromName(filename)) | ||
rewrittenSourceMapsCache.set(filename, sm) | ||
} | ||
function readAndCacheSourceMap (filename, filePath) { | ||
const fileContent = fs.readFileSync(filename).toString() | ||
const sm = generateSourceMapFromFileContent(fileContent, filePath) | ||
if (sm) { | ||
if (pathSourceMapsCache.size >= CACHE_MAX_SIZE) { | ||
@@ -38,8 +50,12 @@ pathSourceMapsCache.clear() | ||
function getFilePathFromName (filename) { | ||
const filenameParts = filename.split(path.sep) | ||
filenameParts.pop() | ||
return filenameParts.join(path.sep) | ||
} | ||
function getSourcePathAndLineFromSourceMaps (filename, line, column = 0) { | ||
try { | ||
let sourceMap = pathSourceMapsCache.get(filename) | ||
const filenameParts = filename.split(path.sep) | ||
filenameParts.pop() | ||
const filePath = filenameParts.join(path.sep) | ||
let sourceMap = rewrittenSourceMapsCache.get(filename) || pathSourceMapsCache.get(filename) | ||
const filePath = getFilePathFromName(filename) | ||
if (!sourceMap) { | ||
@@ -49,7 +65,7 @@ sourceMap = readAndCacheSourceMap(filename, filePath) | ||
if (sourceMap) { | ||
const { originalSource, originalLine, originalColumn } = sourceMap.findEntry(line, column) | ||
const { originalSource, originalLine, originalColumn } = sourceMap.findEntry(line - 1, column - 1) | ||
return { | ||
path: path.join(filePath, originalSource), | ||
line: originalLine, | ||
column: originalColumn | ||
line: originalLine + 1, | ||
column: originalColumn + 1 | ||
} | ||
@@ -64,3 +80,4 @@ } | ||
module.exports = { | ||
getSourcePathAndLineFromSourceMaps | ||
getSourcePathAndLineFromSourceMaps, | ||
cacheRewrittenSourceMap | ||
} |
21
main.js
@@ -8,2 +8,3 @@ /** | ||
const { getPrepareStackTrace } = require('./js/stack-trace/') | ||
const { cacheRewrittenSourceMap } = require('./js/source-map') | ||
@@ -16,5 +17,23 @@ class DummyRewriter { | ||
let NativeRewriter | ||
class CacheRewriter { | ||
constructor (config) { | ||
if (NativeRewriter) { | ||
this.nativeRewriter = new NativeRewriter(config) | ||
} else { | ||
this.nativeRewriter = new DummyRewriter() | ||
} | ||
} | ||
rewrite (code, file) { | ||
const content = this.nativeRewriter.rewrite(code, file) | ||
cacheRewrittenSourceMap(file, content) | ||
return content | ||
} | ||
} | ||
function getRewriter () { | ||
try { | ||
return require('node-gyp-build')(__dirname).Rewriter | ||
NativeRewriter = require('node-gyp-build')(__dirname).Rewriter | ||
return CacheRewriter | ||
} catch (e) { | ||
@@ -21,0 +40,0 @@ return DummyRewriter |
{ | ||
"name": "@datadog/native-iast-rewriter", | ||
"homepage": "https://github.com/DataDog/dd-native-iast-rewriter-js/blob/main/README.md", | ||
"version": "0.1.0-beta.1", | ||
"version": "0.1.0-beta.2", | ||
"description": "Datadog IAST instrumentation addon for NodeJS", | ||
@@ -6,0 +6,0 @@ "main": "main.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20989360
540