Skip to content

Commit d702f07

Browse files
committed
fix: fix sourcemap errors with node v18
Recent node v18 turned on an implementation of `fetch()` by default. The '[email protected]' dep used for sourcemap handling incorrectly uses the presence of `fetch` to decide it is in a browser env. This change inlines the load-source-map dep so we can update it to use the *beta* [email protected] which includes a fix. Fixes: #2589
1 parent 763f5f3 commit d702f07

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

NOTICE.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,33 @@ THE SOFTWARE.
109109

110110
Parts of "lib/instrumentation/run-context" have been adapted from or influenced
111111
by TypeScript code in `@opentelemetry/context-async-hooks`.
112+
113+
## load-source-map
114+
115+
- **path:** [lib/load-source-map.js](lib/load-source-map.js)
116+
- **author:** Espen Hovlandsdal
117+
- **project url:** https://github.com/rexxars/load-source-map
118+
- **original file:** https://github.com/rexxars/load-source-map/blob/v2.0.0/lib/index.js
119+
- **license:** MIT License (MIT), http://opensource.org/licenses/MIT
120+
121+
The MIT License (MIT)
122+
123+
Copyright (c) 2017 Espen Hovlandsdal
124+
125+
Permission is hereby granted, free of charge, to any person obtaining a copy
126+
of this software and associated documentation files (the "Software"), to deal
127+
in the Software without restriction, including without limitation the rights
128+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
129+
copies of the Software, and to permit persons to whom the Software is
130+
furnished to do so, subject to the following conditions:
131+
132+
The above copyright notice and this permission notice shall be included in all
133+
copies or substantial portions of the Software.
134+
135+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
136+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
137+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
138+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
139+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
140+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
141+
SOFTWARE.

lib/load-source-map.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict'
2+
3+
// This is a copy of [email protected]. It is inlined in elastic-apm-node
4+
// solely to update its `source-map` dependency to fix
5+
// https://github.com/elastic/apm-agent-nodejs/issues/2589
6+
// If/when there is a new release of load-source-map with
7+
// https://github.com/rexxars/load-source-map/pull/6
8+
// then we could move back to using load-source-map as a dependency.
9+
10+
var fs = require('fs')
11+
var path = require('path')
12+
var SourceMapConsumer = require('source-map').SourceMapConsumer
13+
14+
var INLINE_SOURCEMAP_REGEX = /^data:application\/json[^,]+base64,/
15+
var SOURCEMAP_REGEX = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*(?:\*\/)[ \t]*$)/
16+
var READ_FILE_OPTS = { encoding: 'utf8' }
17+
18+
module.exports = function readSourceMap (filename, cb) {
19+
fs.readFile(filename, READ_FILE_OPTS, function (err, sourceFile) {
20+
if (err) {
21+
return cb(err)
22+
}
23+
24+
// Look for a sourcemap URL
25+
var sourceMapUrl = resolveSourceMapUrl(sourceFile, path.dirname(filename))
26+
if (!sourceMapUrl) {
27+
return cb()
28+
}
29+
30+
// If it's an inline map, decode it and pass it through the same consumer factory
31+
if (isInlineMap(sourceMapUrl)) {
32+
return onMapRead(null, decodeInlineMap(sourceMapUrl))
33+
}
34+
35+
// Load actual source map from given path
36+
fs.readFile(sourceMapUrl, READ_FILE_OPTS, onMapRead)
37+
38+
function onMapRead (readErr, sourceMap) {
39+
if (readErr) {
40+
readErr.message = 'Error reading sourcemap for file "' + filename + '":\n' + readErr.message
41+
return cb(readErr)
42+
}
43+
44+
try {
45+
(new SourceMapConsumer(sourceMap))
46+
.then(function onConsumerReady (consumer) {
47+
return cb(null, consumer)
48+
}, onConsumerError)
49+
} catch (parseErr) {
50+
onConsumerError(parseErr)
51+
}
52+
}
53+
54+
function onConsumerError (parseErr) {
55+
parseErr.message = 'Error parsing sourcemap for file "' + filename + '":\n' + parseErr.message
56+
return cb(parseErr)
57+
}
58+
})
59+
}
60+
61+
function resolveSourceMapUrl (sourceFile, sourcePath) {
62+
var lines = sourceFile.split(/\r?\n/)
63+
var sourceMapUrl = null
64+
for (var i = lines.length - 1; i >= 0 && !sourceMapUrl; i--) {
65+
sourceMapUrl = lines[i].match(SOURCEMAP_REGEX)
66+
}
67+
68+
if (!sourceMapUrl) {
69+
return null
70+
}
71+
72+
return isInlineMap(sourceMapUrl[1])
73+
? sourceMapUrl[1]
74+
: path.resolve(sourcePath, sourceMapUrl[1])
75+
}
76+
77+
function isInlineMap (url) {
78+
return INLINE_SOURCEMAP_REGEX.test(url)
79+
}
80+
81+
function decodeInlineMap (data) {
82+
var rawData = data.slice(data.indexOf(',') + 1)
83+
return Buffer.from(rawData, 'base64').toString()
84+
}

lib/stacktraces.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const asyncCache = require('async-cache')
1414
const afterAllResults = require('after-all-results')
1515
const errorCallsites = require('error-callsites')
1616
const errorStackParser = require('error-stack-parser')
17-
const loadSourceMap = require('load-source-map')
17+
const loadSourceMap = require('./load-source-map')
1818
const LRU = require('lru-cache')
1919

2020
const fileCache = asyncCache({

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
"fast-safe-stringify": "^2.0.7",
9595
"http-headers": "^3.0.2",
9696
"is-native": "^1.0.1",
97-
"load-source-map": "^2.0.0",
9897
"lru-cache": "^6.0.0",
9998
"measured-reporting": "^1.51.1",
10099
"monitor-event-loop-delay": "^1.0.0",
@@ -108,6 +107,7 @@
108107
"semver": "^6.3.0",
109108
"set-cookie-serde": "^1.0.0",
110109
"shallow-clone-shim": "^2.0.0",
110+
"source-map": "^0.8.0-beta.0",
111111
"sql-summary": "^1.0.1",
112112
"traceparent": "^1.0.0",
113113
"traverse": "^0.6.6",

0 commit comments

Comments
 (0)