@jsenv/sourcemap 1.0.10 → 1.1.0

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.
@@ -1,8 +1,8 @@
1
- import { DATA_URL } from "@jsenv/urls"
1
+ import { DATA_URL } from "@jsenv/urls";
2
2
 
3
- import { SOURCEMAP } from "../sourcemap_comment.js"
4
- import { createDetailedMessage } from "./detailed_message.js"
5
- import { remapCallSite } from "./remap_call_site.js"
3
+ import { SOURCEMAP } from "../sourcemap_comment.js";
4
+ import { createDetailedMessage } from "./detailed_message.js";
5
+ import { remapCallSite } from "./remap_call_site.js";
6
6
 
7
7
  export const remapStack = async ({
8
8
  stack,
@@ -15,17 +15,17 @@ export const remapStack = async ({
15
15
  const urlToSourcemapConsumer = memoizeByFirstArgStringValue(
16
16
  async (stackTraceFileUrl) => {
17
17
  if (stackTraceFileUrl.startsWith("node:")) {
18
- return null
18
+ return null;
19
19
  }
20
20
 
21
21
  try {
22
- let text
22
+ let text;
23
23
  try {
24
- const fileResponse = await fetchFile(stackTraceFileUrl)
25
- const { status } = fileResponse
24
+ const fileResponse = await fetchFile(stackTraceFileUrl);
25
+ const { status } = fileResponse;
26
26
  if (status !== 200) {
27
27
  if (status === 404) {
28
- onFailure(`stack trace file not found at ${stackTraceFileUrl}`)
28
+ onFailure(`stack trace file not found at ${stackTraceFileUrl}`);
29
29
  } else {
30
30
  onFailure(
31
31
  createDetailedMessage(
@@ -36,46 +36,46 @@ export const remapStack = async ({
36
36
  ["stack trace file"]: stackTraceFileUrl,
37
37
  },
38
38
  ),
39
- )
39
+ );
40
40
  }
41
- return null
41
+ return null;
42
42
  }
43
- text = await fileResponse.text()
43
+ text = await fileResponse.text();
44
44
  } catch (e) {
45
45
  onFailure(
46
46
  createDetailedMessage(`error while fetching stack trace file.`, {
47
47
  ["fetch error stack"]: readErrorStack(e),
48
48
  ["stack trace file"]: stackTraceFileUrl,
49
49
  }),
50
- )
50
+ );
51
51
 
52
- return null
52
+ return null;
53
53
  }
54
54
 
55
55
  const jsSourcemapComment = SOURCEMAP.readComment({
56
56
  contentType: "text/javascript",
57
57
  content: text,
58
- })
58
+ });
59
59
  if (!jsSourcemapComment) {
60
- return null
60
+ return null;
61
61
  }
62
- const jsSourcemapUrl = jsSourcemapComment.specifier
63
- let sourcemapUrl
64
- let sourcemapString
62
+ const jsSourcemapUrl = jsSourcemapComment.specifier;
63
+ let sourcemapUrl;
64
+ let sourcemapString;
65
65
  if (jsSourcemapUrl.startsWith("data:")) {
66
- sourcemapUrl = stackTraceFileUrl
67
- sourcemapString = DATA_URL.parse(jsSourcemapUrl, { as: "string" })
66
+ sourcemapUrl = stackTraceFileUrl;
67
+ sourcemapString = DATA_URL.parse(jsSourcemapUrl, { as: "string" });
68
68
  } else {
69
69
  sourcemapUrl = resolveFile(jsSourcemapUrl, stackTraceFileUrl, {
70
70
  type: "source-map",
71
- })
71
+ });
72
72
 
73
73
  try {
74
- const sourcemapResponse = await fetchFile(sourcemapUrl)
75
- const { status } = sourcemapResponse
74
+ const sourcemapResponse = await fetchFile(sourcemapUrl);
75
+ const { status } = sourcemapResponse;
76
76
  if (status !== 200) {
77
77
  if (status === 404) {
78
- onFailure(`sourcemap file not found at ${sourcemapUrl}`)
78
+ onFailure(`sourcemap file not found at ${sourcemapUrl}`);
79
79
  } else {
80
80
  onFailure(
81
81
  createDetailedMessage(
@@ -86,56 +86,56 @@ export const remapStack = async ({
86
86
  ["sourcemap url"]: sourcemapUrl,
87
87
  },
88
88
  ),
89
- )
89
+ );
90
90
  }
91
- return null
91
+ return null;
92
92
  }
93
- sourcemapString = await sourcemapResponse.text()
93
+ sourcemapString = await sourcemapResponse.text();
94
94
  } catch (e) {
95
95
  onFailure(
96
96
  createDetailedMessage(`error while fetching sourcemap.`, {
97
97
  ["fetch error stack"]: readErrorStack(e),
98
98
  ["sourcemap url"]: sourcemapUrl,
99
99
  }),
100
- )
101
- return null
100
+ );
101
+ return null;
102
102
  }
103
103
  }
104
104
 
105
- let sourceMap
105
+ let sourceMap;
106
106
  try {
107
- sourceMap = JSON.parse(sourcemapString)
107
+ sourceMap = JSON.parse(sourcemapString);
108
108
  } catch (e) {
109
109
  onFailure(
110
110
  createDetailedMessage(`error while parsing sourcemap.`, {
111
111
  ["parse error stack"]: readErrorStack(e),
112
112
  ["sourcemap url"]: sourcemapUrl,
113
113
  }),
114
- )
115
- return null
114
+ );
115
+ return null;
116
116
  }
117
117
 
118
- let { sourcesContent } = sourceMap
118
+ let { sourcesContent } = sourceMap;
119
119
 
120
120
  if (!sourcesContent) {
121
- sourcesContent = []
122
- sourceMap.sourcesContent = sourcesContent
121
+ sourcesContent = [];
122
+ sourceMap.sourcesContent = sourcesContent;
123
123
  }
124
124
 
125
- let firstSourceMapSourceFailure = null
125
+ let firstSourceMapSourceFailure = null;
126
126
 
127
127
  await Promise.all(
128
128
  sourceMap.sources.map(async (source, index) => {
129
- if (index in sourcesContent) return
129
+ if (index in sourcesContent) return;
130
130
 
131
131
  const sourcemapSourceUrl = resolveFile(source, sourcemapUrl, {
132
132
  type: "source",
133
- })
133
+ });
134
134
  try {
135
- const sourceResponse = await fetchFile(sourcemapSourceUrl)
136
- const { status } = sourceResponse
135
+ const sourceResponse = await fetchFile(sourcemapSourceUrl);
136
+ const { status } = sourceResponse;
137
137
  if (status !== 200) {
138
- if (firstSourceMapSourceFailure) return
138
+ if (firstSourceMapSourceFailure) return;
139
139
 
140
140
  if (status === 404) {
141
141
  firstSourceMapSourceFailure = createDetailedMessage(
@@ -144,8 +144,8 @@ export const remapStack = async ({
144
144
  ["sourcemap source url"]: sourcemapSourceUrl,
145
145
  ["sourcemap url"]: sourcemapUrl,
146
146
  },
147
- )
148
- return
147
+ );
148
+ return;
149
149
  }
150
150
  firstSourceMapSourceFailure = createDetailedMessage(
151
151
  `unexpected response for sourcemap source.`,
@@ -155,14 +155,14 @@ export const remapStack = async ({
155
155
  ["sourcemap source url"]: sourcemapSourceUrl,
156
156
  ["sourcemap url"]: sourcemapUrl,
157
157
  },
158
- )
159
- return
158
+ );
159
+ return;
160
160
  }
161
161
 
162
- const sourceString = await sourceResponse.text()
163
- sourcesContent[index] = sourceString
162
+ const sourceString = await sourceResponse.text();
163
+ sourcesContent[index] = sourceString;
164
164
  } catch (e) {
165
- if (firstSourceMapSourceFailure) return
165
+ if (firstSourceMapSourceFailure) return;
166
166
  firstSourceMapSourceFailure = createDetailedMessage(
167
167
  `error while fetching sourcemap source.`,
168
168
  {
@@ -170,17 +170,17 @@ export const remapStack = async ({
170
170
  ["sourcemap source url"]: sourcemapSourceUrl,
171
171
  ["sourcemap url"]: sourcemapUrl,
172
172
  },
173
- )
173
+ );
174
174
  }
175
175
  }),
176
- )
176
+ );
177
177
 
178
178
  if (firstSourceMapSourceFailure) {
179
- onFailure(firstSourceMapSourceFailure)
180
- return null
179
+ onFailure(firstSourceMapSourceFailure);
180
+ return null;
181
181
  }
182
182
 
183
- return new SourceMapConsumer(sourceMap)
183
+ return new SourceMapConsumer(sourceMap);
184
184
  } catch (e) {
185
185
  onFailure(
186
186
  createDetailedMessage(
@@ -190,11 +190,11 @@ export const remapStack = async ({
190
190
  ["stack trace file"]: stackTraceFileUrl,
191
191
  },
192
192
  ),
193
- )
194
- return null
193
+ );
194
+ return null;
195
195
  }
196
196
  },
197
- )
197
+ );
198
198
  const originalCallsites = await Promise.all(
199
199
  stack.map((callSite) =>
200
200
  remapCallSite(callSite, {
@@ -204,17 +204,17 @@ export const remapStack = async ({
204
204
  onFailure,
205
205
  }),
206
206
  ),
207
- )
208
- return originalCallsites
209
- }
207
+ );
208
+ return originalCallsites;
209
+ };
210
210
 
211
211
  const memoizeByFirstArgStringValue = (fn) => {
212
- const stringValueCache = {}
212
+ const stringValueCache = {};
213
213
  return (firstArgValue) => {
214
214
  if (firstArgValue in stringValueCache)
215
- return stringValueCache[firstArgValue]
216
- const value = fn(firstArgValue)
217
- stringValueCache[firstArgValue] = value
218
- return value
219
- }
220
- }
215
+ return stringValueCache[firstArgValue];
216
+ const value = fn(firstArgValue);
217
+ stringValueCache[firstArgValue] = value;
218
+ return value;
219
+ };
220
+ };
@@ -1,9 +1,9 @@
1
1
  export const stringifyStack = (stack, { error, indent }) => {
2
- const name = error.name || "Error"
3
- const message = error.message || ""
2
+ const name = error.name || "Error";
3
+ const message = error.message || "";
4
4
  const stackString = stack
5
5
  .map((callSite) => `\n${indent}at ${callSite}`)
6
- .join("")
6
+ .join("");
7
7
 
8
- return `${name}: ${message}${stackString}`
9
- }
8
+ return `${name}: ${message}${stackString}`;
9
+ };
@@ -1,52 +1,52 @@
1
- import MagicString from "magic-string"
1
+ import MagicString from "magic-string";
2
2
 
3
3
  export const createMagicSource = (content) => {
4
4
  if (content === undefined) {
5
- throw new Error("content missing")
5
+ throw new Error("content missing");
6
6
  }
7
- const mutations = []
7
+ const mutations = [];
8
8
  return {
9
9
  prepend: (string) => {
10
10
  mutations.push((magicString) => {
11
- magicString.prepend(string)
12
- })
11
+ magicString.prepend(string);
12
+ });
13
13
  },
14
14
  append: (string) => {
15
15
  mutations.push((magicString) => {
16
- magicString.append(string)
17
- })
16
+ magicString.append(string);
17
+ });
18
18
  },
19
19
  replace: ({ start, end, replacement }) => {
20
20
  mutations.push((magicString) => {
21
- magicString.overwrite(start, end, replacement)
22
- })
21
+ magicString.overwrite(start, end, replacement);
22
+ });
23
23
  },
24
24
  remove: ({ start, end }) => {
25
25
  mutations.push((magicString) => {
26
- magicString.remove(start, end)
27
- })
26
+ magicString.remove(start, end);
27
+ });
28
28
  },
29
29
  toContentAndSourcemap: ({ source } = {}) => {
30
30
  if (mutations.length === 0) {
31
31
  return {
32
32
  content,
33
33
  sourcemap: null,
34
- }
34
+ };
35
35
  }
36
- const magicString = new MagicString(content)
36
+ const magicString = new MagicString(content);
37
37
  mutations.forEach((mutation) => {
38
- mutation(magicString)
39
- })
40
- const code = magicString.toString()
38
+ mutation(magicString);
39
+ });
40
+ const code = magicString.toString();
41
41
  const map = magicString.generateMap({
42
42
  hires: true,
43
43
  includeContent: true,
44
44
  source,
45
- })
45
+ });
46
46
  return {
47
47
  content: code,
48
48
  sourcemap: map,
49
- }
49
+ };
50
50
  },
51
- }
52
- }
51
+ };
52
+ };
package/src/main.js CHANGED
@@ -1,10 +1,10 @@
1
- export { createMagicSource } from "./magic_source.js"
2
- export { composeTwoSourcemaps } from "./sourcemap_composition_v3.js"
1
+ export { createMagicSource } from "./magic_source.js";
2
+ export { composeTwoSourcemaps } from "./sourcemap_composition_v3.js";
3
3
 
4
- export { getOriginalPosition } from "./original_position.js"
5
- export { sourcemapConverter } from "./sourcemap_converter.js"
4
+ export { getOriginalPosition } from "./original_position.js";
5
+ export { sourcemapConverter } from "./sourcemap_converter.js";
6
6
  export {
7
7
  generateSourcemapFileUrl,
8
8
  generateSourcemapDataUrl,
9
- } from "./sourcemap_url_generator.js"
10
- export { SOURCEMAP } from "./sourcemap_comment.js"
9
+ } from "./sourcemap_url_generator.js";
10
+ export { SOURCEMAP } from "./sourcemap_comment.js";
@@ -1,14 +1,14 @@
1
- import { requireSourcemap } from "./require_sourcemap.js"
1
+ import { requireSourcemap } from "./require_sourcemap.js";
2
2
 
3
3
  // https://github.com/mozilla/source-map#sourcemapconsumerprototypeoriginalpositionforgeneratedposition
4
4
  export const getOriginalPosition = ({ sourcemap, line, column, bias }) => {
5
- const { SourceMapConsumer } = requireSourcemap()
5
+ const { SourceMapConsumer } = requireSourcemap();
6
6
 
7
- const sourceMapConsumer = new SourceMapConsumer(sourcemap)
7
+ const sourceMapConsumer = new SourceMapConsumer(sourcemap);
8
8
  const originalPosition = sourceMapConsumer.originalPositionFor({
9
9
  line,
10
10
  column,
11
11
  bias,
12
- })
13
- return originalPosition
14
- }
12
+ });
13
+ return originalPosition;
14
+ };
@@ -1,9 +1,9 @@
1
- import { createRequire } from "node:module"
1
+ import { createRequire } from "node:module";
2
2
 
3
- const require = createRequire(import.meta.url)
3
+ const require = createRequire(import.meta.url);
4
4
  // consider using https://github.com/7rulnik/source-map-js
5
5
 
6
6
  export const requireSourcemap = () => {
7
- const namespace = require("source-map-js")
8
- return namespace
9
- }
7
+ const namespace = require("source-map-js");
8
+ return namespace;
9
+ };
@@ -1,36 +1,36 @@
1
1
  export const SOURCEMAP = {
2
2
  enabledOnContentType: (contentType) => {
3
- return ["text/javascript", "text/css"].includes(contentType)
3
+ return ["text/javascript", "text/css"].includes(contentType);
4
4
  },
5
5
 
6
6
  readComment: ({ contentType, content }) => {
7
7
  const read = {
8
8
  "text/javascript": parseJavaScriptSourcemapComment,
9
9
  "text/css": parseCssSourcemapComment,
10
- }[contentType]
11
- return read ? read(content) : null
10
+ }[contentType];
11
+ return read ? read(content) : null;
12
12
  },
13
13
 
14
14
  writeComment: ({ contentType, content, specifier }) => {
15
15
  const write = {
16
16
  "text/javascript": setJavaScriptSourceMappingUrl,
17
17
  "text/css": setCssSourceMappingUrl,
18
- }[contentType]
19
- return write ? write(content, specifier) : content
18
+ }[contentType];
19
+ return write ? write(content, specifier) : content;
20
20
  },
21
- }
21
+ };
22
22
 
23
23
  const parseJavaScriptSourcemapComment = (javaScriptSource) => {
24
- let sourceMappingUrl
24
+ let sourceMappingUrl;
25
25
  replaceSourceMappingUrl(
26
26
  javaScriptSource,
27
27
  javascriptSourceMappingUrlCommentRegexp,
28
28
  (value) => {
29
- sourceMappingUrl = value
29
+ sourceMappingUrl = value;
30
30
  },
31
- )
31
+ );
32
32
  if (!sourceMappingUrl) {
33
- return null
33
+ return null;
34
34
  }
35
35
  return {
36
36
  type: "sourcemap_comment",
@@ -40,46 +40,46 @@ const parseJavaScriptSourcemapComment = (javaScriptSource) => {
40
40
  // ${"//#"} is to avoid static analysis to think there is a sourceMappingUrl for this file
41
41
  column: `${"//#"} sourceMappingURL=`.length + 1,
42
42
  specifier: sourceMappingUrl,
43
- }
44
- }
43
+ };
44
+ };
45
45
 
46
46
  const setJavaScriptSourceMappingUrl = (
47
47
  javaScriptSource,
48
48
  sourceMappingFileUrl,
49
49
  ) => {
50
- let replaced
50
+ let replaced;
51
51
  const sourceAfterReplace = replaceSourceMappingUrl(
52
52
  javaScriptSource,
53
53
  javascriptSourceMappingUrlCommentRegexp,
54
54
  () => {
55
- replaced = true
55
+ replaced = true;
56
56
  return sourceMappingFileUrl
57
57
  ? writeJavaScriptSourceMappingURL(sourceMappingFileUrl)
58
- : ""
58
+ : "";
59
59
  },
60
- )
60
+ );
61
61
  if (replaced) {
62
- return sourceAfterReplace
62
+ return sourceAfterReplace;
63
63
  }
64
64
 
65
65
  return sourceMappingFileUrl
66
66
  ? `${javaScriptSource}
67
67
  ${writeJavaScriptSourceMappingURL(sourceMappingFileUrl)}
68
68
  `
69
- : javaScriptSource
70
- }
69
+ : javaScriptSource;
70
+ };
71
71
 
72
72
  const parseCssSourcemapComment = (cssSource) => {
73
- let sourceMappingUrl
73
+ let sourceMappingUrl;
74
74
  replaceSourceMappingUrl(
75
75
  cssSource,
76
76
  cssSourceMappingUrlCommentRegExp,
77
77
  (value) => {
78
- sourceMappingUrl = value
78
+ sourceMappingUrl = value;
79
79
  },
80
- )
80
+ );
81
81
  if (!sourceMappingUrl) {
82
- return null
82
+ return null;
83
83
  }
84
84
  return {
85
85
  type: "sourcemap_comment",
@@ -89,55 +89,55 @@ const parseCssSourcemapComment = (cssSource) => {
89
89
  // ${"//*#"} is to avoid static analysis to think there is a sourceMappingUrl for this file
90
90
  column: `${"//*#"} sourceMappingURL=`.length + 1,
91
91
  specifier: sourceMappingUrl,
92
- }
93
- }
92
+ };
93
+ };
94
94
 
95
95
  const setCssSourceMappingUrl = (cssSource, sourceMappingFileUrl) => {
96
- let replaced
96
+ let replaced;
97
97
  const sourceAfterReplace = replaceSourceMappingUrl(
98
98
  cssSource,
99
99
  cssSourceMappingUrlCommentRegExp,
100
100
  () => {
101
- replaced = true
101
+ replaced = true;
102
102
  return sourceMappingFileUrl
103
103
  ? writeCssSourceMappingUrl(sourceMappingFileUrl)
104
- : ""
104
+ : "";
105
105
  },
106
- )
106
+ );
107
107
  if (replaced) {
108
- return sourceAfterReplace
108
+ return sourceAfterReplace;
109
109
  }
110
110
  return sourceMappingFileUrl
111
111
  ? `${cssSource}
112
112
  ${writeCssSourceMappingUrl(sourceMappingFileUrl)}
113
113
  `
114
- : cssSource
115
- }
114
+ : cssSource;
115
+ };
116
116
 
117
117
  const javascriptSourceMappingUrlCommentRegexp =
118
- /\/\/ ?# ?sourceMappingURL=([^\s'"]+)/g
118
+ /\/\/ ?# ?sourceMappingURL=([^\s'"]+)/g;
119
119
  const cssSourceMappingUrlCommentRegExp =
120
- /\/\*# ?sourceMappingURL=([^\s'"]+) \*\//g
120
+ /\/\*# ?sourceMappingURL=([^\s'"]+) \*\//g;
121
121
 
122
122
  // ${"//#"} is to avoid a parser thinking there is a sourceMappingUrl for this file
123
123
  const writeJavaScriptSourceMappingURL = (value) =>
124
- `${"//#"} sourceMappingURL=${value}`
125
- const writeCssSourceMappingUrl = (value) => `/*# sourceMappingURL=${value} */`
124
+ `${"//#"} sourceMappingURL=${value}`;
125
+ const writeCssSourceMappingUrl = (value) => `/*# sourceMappingURL=${value} */`;
126
126
 
127
127
  const replaceSourceMappingUrl = (source, regexp, callback) => {
128
- let lastSourceMappingUrl
129
- let matchSourceMappingUrl
128
+ let lastSourceMappingUrl;
129
+ let matchSourceMappingUrl;
130
130
  while ((matchSourceMappingUrl = regexp.exec(source))) {
131
- lastSourceMappingUrl = matchSourceMappingUrl
131
+ lastSourceMappingUrl = matchSourceMappingUrl;
132
132
  }
133
133
  if (lastSourceMappingUrl) {
134
- const index = lastSourceMappingUrl.index
135
- const before = source.slice(0, index)
136
- const after = source.slice(index)
134
+ const index = lastSourceMappingUrl.index;
135
+ const before = source.slice(0, index);
136
+ const after = source.slice(index);
137
137
  const mappedAfter = after.replace(regexp, (match, firstGroup) => {
138
- return callback(firstGroup)
139
- })
140
- return `${before}${mappedAfter}`
138
+ return callback(firstGroup);
139
+ });
140
+ return `${before}${mappedAfter}`;
141
141
  }
142
- return source
143
- }
142
+ return source;
143
+ };