Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,33 @@ THE SOFTWARE.

Parts of "lib/instrumentation/run-context" have been adapted from or influenced
by TypeScript code in `@opentelemetry/context-async-hooks`.

## load-source-map

- **path:** [lib/load-source-map.js](lib/load-source-map.js)
- **author:** Espen Hovlandsdal
- **project url:** https://github.com/rexxars/load-source-map
- **original file:** https://github.com/rexxars/load-source-map/blob/v2.0.0/lib/index.js
- **license:** MIT License (MIT), http://opensource.org/licenses/MIT

The MIT License (MIT)

Copyright (c) 2017 Espen Hovlandsdal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
84 changes: 84 additions & 0 deletions lib/load-source-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict'

// This is a copy of load-source-map@2.0.0. It is inlined in elastic-apm-node
// solely to update its `source-map` dependency to fix
// https://github.com/elastic/apm-agent-nodejs/issues/2589
// If/when there is a new release of load-source-map with
// https://github.com/rexxars/load-source-map/pull/6
// then we could move back to using load-source-map as a dependency.

var fs = require('fs')
var path = require('path')
var SourceMapConsumer = require('source-map').SourceMapConsumer

var INLINE_SOURCEMAP_REGEX = /^data:application\/json[^,]+base64,/
var SOURCEMAP_REGEX = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*(?:\*\/)[ \t]*$)/
var READ_FILE_OPTS = { encoding: 'utf8' }

module.exports = function readSourceMap (filename, cb) {
fs.readFile(filename, READ_FILE_OPTS, function (err, sourceFile) {
if (err) {
return cb(err)
}

// Look for a sourcemap URL
var sourceMapUrl = resolveSourceMapUrl(sourceFile, path.dirname(filename))
if (!sourceMapUrl) {
return cb()
}

// If it's an inline map, decode it and pass it through the same consumer factory
if (isInlineMap(sourceMapUrl)) {
return onMapRead(null, decodeInlineMap(sourceMapUrl))
}

// Load actual source map from given path
fs.readFile(sourceMapUrl, READ_FILE_OPTS, onMapRead)

function onMapRead (readErr, sourceMap) {
if (readErr) {
readErr.message = 'Error reading sourcemap for file "' + filename + '":\n' + readErr.message
return cb(readErr)
}

try {
(new SourceMapConsumer(sourceMap))
.then(function onConsumerReady (consumer) {
return cb(null, consumer)
}, onConsumerError)
} catch (parseErr) {
onConsumerError(parseErr)
}
}

function onConsumerError (parseErr) {
parseErr.message = 'Error parsing sourcemap for file "' + filename + '":\n' + parseErr.message
return cb(parseErr)
}
})
}

function resolveSourceMapUrl (sourceFile, sourcePath) {
var lines = sourceFile.split(/\r?\n/)
var sourceMapUrl = null
for (var i = lines.length - 1; i >= 0 && !sourceMapUrl; i--) {
sourceMapUrl = lines[i].match(SOURCEMAP_REGEX)
}

if (!sourceMapUrl) {
return null
}

return isInlineMap(sourceMapUrl[1])
? sourceMapUrl[1]
: path.resolve(sourcePath, sourceMapUrl[1])
}

function isInlineMap (url) {
return INLINE_SOURCEMAP_REGEX.test(url)
}

function decodeInlineMap (data) {
var rawData = data.slice(data.indexOf(',') + 1)
return Buffer.from(rawData, 'base64').toString()
}
2 changes: 1 addition & 1 deletion lib/stacktraces.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const asyncCache = require('async-cache')
const afterAllResults = require('after-all-results')
const errorCallsites = require('error-callsites')
const errorStackParser = require('error-stack-parser')
const loadSourceMap = require('load-source-map')
const loadSourceMap = require('./load-source-map')
const LRU = require('lru-cache')

const fileCache = asyncCache({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
"fast-safe-stringify": "^2.0.7",
"http-headers": "^3.0.2",
"is-native": "^1.0.1",
"load-source-map": "^2.0.0",
"lru-cache": "^6.0.0",
"measured-reporting": "^1.51.1",
"monitor-event-loop-delay": "^1.0.0",
Expand All @@ -108,6 +107,7 @@
"semver": "^6.3.0",
"set-cookie-serde": "^1.0.0",
"shallow-clone-shim": "^2.0.0",
"source-map": "^0.8.0-beta.0",
"sql-summary": "^1.0.1",
"traceparent": "^1.0.0",
"traverse": "^0.6.6",
Expand Down