@jsenv/sourcemap 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/sourcemap",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,8 @@
12
12
  },
13
13
  "type": "module",
14
14
  "exports": {
15
- ".": "./src/main.js"
15
+ ".": "./src/main.js",
16
+ "./*": "./*"
16
17
  },
17
18
  "scripts": {
18
19
  "test": "node --conditions=development ./scripts/test.mjs"
@@ -1,6 +1,4 @@
1
- import { startsWithWindowsDriveLetter, windowsFilePathToUrl } from "@jsenv/urls"
2
-
3
- import { createDetailedMessage } from "./detailed_message.js"
1
+ import { remapSourcePosition } from "./remap_source_position.js"
4
2
 
5
3
  export const remapCallSite = async (
6
4
  callSite,
@@ -222,91 +220,3 @@ const remapEvalOrigin = async (
222
220
  // Make sure we still return useful information if we didn't find anything
223
221
  return origin
224
222
  }
225
-
226
- const remapSourcePosition = async ({
227
- source,
228
- line,
229
- column,
230
- resolveFile,
231
- urlToSourcemapConsumer,
232
- readErrorStack,
233
- onFailure,
234
- }) => {
235
- const position = { source, line, column }
236
-
237
- const url = sourceToUrl(source, { resolveFile })
238
- if (!url) return position
239
-
240
- const sourceMapConsumer = await urlToSourcemapConsumer(url)
241
-
242
- if (!sourceMapConsumer) return position
243
-
244
- try {
245
- const originalPosition = sourceMapConsumer.originalPositionFor(position)
246
-
247
- // Only return the original position if a matching line was found. If no
248
- // matching line is found then we return position instead, which will cause
249
- // the stack trace to print the path and line for the compiled file. It is
250
- // better to give a precise location in the compiled file than a vague
251
- // location in the original file.
252
- const originalSource = originalPosition.source
253
-
254
- if (originalSource === null) return position
255
- originalPosition.source = resolveFile(originalSource, url, {
256
- type: "file-original",
257
- })
258
-
259
- return originalPosition
260
- } catch (e) {
261
- onFailure(
262
- createDetailedMessage(`error while remapping position.`, {
263
- ["error stack"]: readErrorStack(e),
264
- ["source"]: source,
265
- ["line"]: line,
266
- ["column"]: column,
267
- }),
268
- )
269
- return position
270
- }
271
- }
272
-
273
- const sourceToUrl = (source, { resolveFile }) => {
274
- if (startsWithScheme(source)) {
275
- return source
276
- }
277
-
278
- // linux filesystem path
279
- if (source[0] === "/") {
280
- return resolveFile(source)
281
- }
282
-
283
- // be careful, due to babel or something like that we might receive paths like
284
- // C:/directory/file.js (without backslashes we would expect on windows)
285
- // In that case we consider C: is the signe we are on windows
286
- // And I avoid to rely on process.platform === "win32" because this file might be executed in chrome
287
- if (startsWithWindowsDriveLetter(source)) {
288
- return windowsFilePathToUrl(source)
289
- }
290
-
291
- // I don't think we will ever encounter relative file in the stack trace
292
- // but if it ever happens we are safe :)
293
- if (source.slice(0, 2) === "./" || source.slice(0, 3) === "../") {
294
- return resolveFile(source)
295
- }
296
-
297
- // we have received a "bare specifier" for the source
298
- // it happens for internal/process/task_queues.js for instance
299
- // if we do return resolveFile(source) it will be converted to
300
- // file:///C:/project-directory/internal/process/task_queues.js in node
301
- // and
302
- // http://domain.com/internal/process/task_queues.js
303
- // but the file will certainly be a 404
304
- // and if not it won't be the right file anyway
305
- // for now we assume "bare specifier" in the stack trace
306
- // are internal files that are pointless to try to remap
307
- return null
308
- }
309
-
310
- const startsWithScheme = (string) => {
311
- return /^[a-zA-Z]{2,}:/.test(string)
312
- }
@@ -0,0 +1,106 @@
1
+ import { createDetailedMessage } from "./detailed_message.js"
2
+
3
+ export const remapSourcePosition = async ({
4
+ source,
5
+ line,
6
+ column,
7
+ resolveFile,
8
+ urlToSourcemapConsumer,
9
+ readErrorStack,
10
+ onFailure,
11
+ }) => {
12
+ const position = { source, line, column }
13
+
14
+ const url = sourceToUrl(source, { resolveFile })
15
+ if (!url) return position
16
+
17
+ const sourceMapConsumer = await urlToSourcemapConsumer(url)
18
+
19
+ if (!sourceMapConsumer) return position
20
+
21
+ try {
22
+ const originalPosition = sourceMapConsumer.originalPositionFor(position)
23
+
24
+ // Only return the original position if a matching line was found. If no
25
+ // matching line is found then we return position instead, which will cause
26
+ // the stack trace to print the path and line for the compiled file. It is
27
+ // better to give a precise location in the compiled file than a vague
28
+ // location in the original file.
29
+ const originalSource = originalPosition.source
30
+
31
+ if (originalSource === null) return position
32
+ originalPosition.source = resolveFile(originalSource, url, {
33
+ type: "file-original",
34
+ })
35
+
36
+ return originalPosition
37
+ } catch (e) {
38
+ onFailure(
39
+ createDetailedMessage(`error while remapping position.`, {
40
+ ["error stack"]: readErrorStack(e),
41
+ ["source"]: source,
42
+ ["line"]: line,
43
+ ["column"]: column,
44
+ }),
45
+ )
46
+ return position
47
+ }
48
+ }
49
+
50
+ const sourceToUrl = (source, { resolveFile }) => {
51
+ if (startsWithScheme(source)) {
52
+ return source
53
+ }
54
+
55
+ // linux filesystem path
56
+ if (source[0] === "/") {
57
+ return resolveFile(source)
58
+ }
59
+
60
+ // be careful, due to babel or something like that we might receive paths like
61
+ // C:/directory/file.js (without backslashes we would expect on windows)
62
+ // In that case we consider C: is the signe we are on windows
63
+ // And I avoid to rely on process.platform === "win32" because this file might be executed in chrome
64
+ if (startsWithWindowsDriveLetter(source)) {
65
+ return windowsFilePathToUrl(source)
66
+ }
67
+
68
+ // I don't think we will ever encounter relative file in the stack trace
69
+ // but if it ever happens we are safe :)
70
+ if (source.slice(0, 2) === "./" || source.slice(0, 3) === "../") {
71
+ return resolveFile(source)
72
+ }
73
+
74
+ // we have received a "bare specifier" for the source
75
+ // it happens for internal/process/task_queues.js for instance
76
+ // if we do return resolveFile(source) it will be converted to
77
+ // file:///C:/project-directory/internal/process/task_queues.js in node
78
+ // and
79
+ // http://domain.com/internal/process/task_queues.js
80
+ // but the file will certainly be a 404
81
+ // and if not it won't be the right file anyway
82
+ // for now we assume "bare specifier" in the stack trace
83
+ // are internal files that are pointless to try to remap
84
+ return null
85
+ }
86
+
87
+ const startsWithScheme = (string) => {
88
+ return /^[a-zA-Z]{2,}:/.test(string)
89
+ }
90
+
91
+ const startsWithWindowsDriveLetter = (string) => {
92
+ const firstChar = string[0]
93
+ if (!/[a-zA-Z]/.test(firstChar)) return false
94
+
95
+ const secondChar = string[1]
96
+ if (secondChar !== ":") return false
97
+
98
+ return true
99
+ }
100
+
101
+ const windowsFilePathToUrl = (windowsFilePath) => {
102
+ return `file:///${replaceBackSlashesWithSlashes(windowsFilePath)}`
103
+ }
104
+
105
+ export const replaceBackSlashesWithSlashes = (string) =>
106
+ string.replace(/\\/g, "/")