source: trip-planner-front/node_modules/source-map-resolve/index.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 9.5 KB
Line 
1var atob = require("atob")
2var urlLib = require("url")
3var pathLib = require("path")
4var decodeUriComponentLib = require("decode-uri-component")
5
6
7
8function resolveUrl(/* ...urls */) {
9 return Array.prototype.reduce.call(arguments, function(resolved, nextUrl) {
10 return urlLib.resolve(resolved, nextUrl)
11 })
12}
13
14function convertWindowsPath(aPath) {
15 return pathLib.sep === "\\" ? aPath.replace(/\\/g, "/").replace(/^[a-z]:\/?/i, "/") : aPath
16}
17
18function customDecodeUriComponent(string) {
19 // `decodeUriComponentLib` turns `+` into ` `, but that's not wanted.
20 return decodeUriComponentLib(string.replace(/\+/g, "%2B"))
21}
22
23function callbackAsync(callback, error, result) {
24 setImmediate(function() { callback(error, result) })
25}
26
27function parseMapToJSON(string, data) {
28 try {
29 return JSON.parse(string.replace(/^\)\]\}'/, ""))
30 } catch (error) {
31 error.sourceMapData = data
32 throw error
33 }
34}
35
36function readSync(read, url, data) {
37 var readUrl = customDecodeUriComponent(url)
38 try {
39 return String(read(readUrl))
40 } catch (error) {
41 error.sourceMapData = data
42 throw error
43 }
44}
45
46
47
48var innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/
49
50var sourceMappingURLRegex = RegExp(
51 "(?:" +
52 "/\\*" +
53 "(?:\\s*\r?\n(?://)?)?" +
54 "(?:" + innerRegex.source + ")" +
55 "\\s*" +
56 "\\*/" +
57 "|" +
58 "//(?:" + innerRegex.source + ")" +
59 ")" +
60 "\\s*"
61)
62
63function getSourceMappingUrl(code) {
64 var match = code.match(sourceMappingURLRegex)
65 return match ? match[1] || match[2] || "" : null
66}
67
68
69
70function resolveSourceMap(code, codeUrl, read, callback) {
71 var mapData
72 try {
73 mapData = resolveSourceMapHelper(code, codeUrl)
74 } catch (error) {
75 return callbackAsync(callback, error)
76 }
77 if (!mapData || mapData.map) {
78 return callbackAsync(callback, null, mapData)
79 }
80 var readUrl = customDecodeUriComponent(mapData.url)
81 read(readUrl, function(error, result) {
82 if (error) {
83 error.sourceMapData = mapData
84 return callback(error)
85 }
86 mapData.map = String(result)
87 try {
88 mapData.map = parseMapToJSON(mapData.map, mapData)
89 } catch (error) {
90 return callback(error)
91 }
92 callback(null, mapData)
93 })
94}
95
96function resolveSourceMapSync(code, codeUrl, read) {
97 var mapData = resolveSourceMapHelper(code, codeUrl)
98 if (!mapData || mapData.map) {
99 return mapData
100 }
101 mapData.map = readSync(read, mapData.url, mapData)
102 mapData.map = parseMapToJSON(mapData.map, mapData)
103 return mapData
104}
105
106var dataUriRegex = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/
107
108/**
109 * The media type for JSON text is application/json.
110 *
111 * {@link https://tools.ietf.org/html/rfc8259#section-11 | IANA Considerations }
112 *
113 * `text/json` is non-standard media type
114 */
115var jsonMimeTypeRegex = /^(?:application|text)\/json$/
116
117/**
118 * JSON text exchanged between systems that are not part of a closed ecosystem
119 * MUST be encoded using UTF-8.
120 *
121 * {@link https://tools.ietf.org/html/rfc8259#section-8.1 | Character Encoding}
122 */
123var jsonCharacterEncoding = "utf-8"
124
125function base64ToBuf(b64) {
126 var binStr = atob(b64)
127 var len = binStr.length
128 var arr = new Uint8Array(len)
129 for (var i = 0; i < len; i++) {
130 arr[i] = binStr.charCodeAt(i)
131 }
132 return arr
133}
134
135function decodeBase64String(b64) {
136 if (typeof TextDecoder === "undefined" || typeof Uint8Array === "undefined") {
137 return atob(b64)
138 }
139 var buf = base64ToBuf(b64);
140 // Note: `decoder.decode` method will throw a `DOMException` with the
141 // `"EncodingError"` value when an coding error is found.
142 var decoder = new TextDecoder(jsonCharacterEncoding, {fatal: true})
143 return decoder.decode(buf);
144}
145
146function resolveSourceMapHelper(code, codeUrl) {
147 codeUrl = convertWindowsPath(codeUrl)
148
149 var url = getSourceMappingUrl(code)
150 if (!url) {
151 return null
152 }
153
154 var dataUri = url.match(dataUriRegex)
155 if (dataUri) {
156 var mimeType = dataUri[1] || "text/plain"
157 var lastParameter = dataUri[2] || ""
158 var encoded = dataUri[3] || ""
159 var data = {
160 sourceMappingURL: url,
161 url: null,
162 sourcesRelativeTo: codeUrl,
163 map: encoded
164 }
165 if (!jsonMimeTypeRegex.test(mimeType)) {
166 var error = new Error("Unuseful data uri mime type: " + mimeType)
167 error.sourceMapData = data
168 throw error
169 }
170 try {
171 data.map = parseMapToJSON(
172 lastParameter === ";base64" ? decodeBase64String(encoded) : decodeURIComponent(encoded),
173 data
174 )
175 } catch (error) {
176 error.sourceMapData = data
177 throw error
178 }
179 return data
180 }
181
182 var mapUrl = resolveUrl(codeUrl, url)
183 return {
184 sourceMappingURL: url,
185 url: mapUrl,
186 sourcesRelativeTo: mapUrl,
187 map: null
188 }
189}
190
191
192
193function resolveSources(map, mapUrl, read, options, callback) {
194 if (typeof options === "function") {
195 callback = options
196 options = {}
197 }
198 var pending = map.sources ? map.sources.length : 0
199 var result = {
200 sourcesResolved: [],
201 sourcesContent: []
202 }
203
204 if (pending === 0) {
205 callbackAsync(callback, null, result)
206 return
207 }
208
209 var done = function() {
210 pending--
211 if (pending === 0) {
212 callback(null, result)
213 }
214 }
215
216 resolveSourcesHelper(map, mapUrl, options, function(fullUrl, sourceContent, index) {
217 result.sourcesResolved[index] = fullUrl
218 if (typeof sourceContent === "string") {
219 result.sourcesContent[index] = sourceContent
220 callbackAsync(done, null)
221 } else {
222 var readUrl = customDecodeUriComponent(fullUrl)
223 read(readUrl, function(error, source) {
224 result.sourcesContent[index] = error ? error : String(source)
225 done()
226 })
227 }
228 })
229}
230
231function resolveSourcesSync(map, mapUrl, read, options) {
232 var result = {
233 sourcesResolved: [],
234 sourcesContent: []
235 }
236
237 if (!map.sources || map.sources.length === 0) {
238 return result
239 }
240
241 resolveSourcesHelper(map, mapUrl, options, function(fullUrl, sourceContent, index) {
242 result.sourcesResolved[index] = fullUrl
243 if (read !== null) {
244 if (typeof sourceContent === "string") {
245 result.sourcesContent[index] = sourceContent
246 } else {
247 var readUrl = customDecodeUriComponent(fullUrl)
248 try {
249 result.sourcesContent[index] = String(read(readUrl))
250 } catch (error) {
251 result.sourcesContent[index] = error
252 }
253 }
254 }
255 })
256
257 return result
258}
259
260var endingSlash = /\/?$/
261
262function resolveSourcesHelper(map, mapUrl, options, fn) {
263 options = options || {}
264 mapUrl = convertWindowsPath(mapUrl)
265 var fullUrl
266 var sourceContent
267 var sourceRoot
268 for (var index = 0, len = map.sources.length; index < len; index++) {
269 sourceRoot = null
270 if (typeof options.sourceRoot === "string") {
271 sourceRoot = options.sourceRoot
272 } else if (typeof map.sourceRoot === "string" && options.sourceRoot !== false) {
273 sourceRoot = map.sourceRoot
274 }
275 // If the sourceRoot is the empty string, it is equivalent to not setting
276 // the property at all.
277 if (sourceRoot === null || sourceRoot === '') {
278 fullUrl = resolveUrl(mapUrl, map.sources[index])
279 } else {
280 // Make sure that the sourceRoot ends with a slash, so that `/scripts/subdir` becomes
281 // `/scripts/subdir/<source>`, not `/scripts/<source>`. Pointing to a file as source root
282 // does not make sense.
283 fullUrl = resolveUrl(mapUrl, sourceRoot.replace(endingSlash, "/"), map.sources[index])
284 }
285 sourceContent = (map.sourcesContent || [])[index]
286 fn(fullUrl, sourceContent, index)
287 }
288}
289
290
291
292function resolve(code, codeUrl, read, options, callback) {
293 if (typeof options === "function") {
294 callback = options
295 options = {}
296 }
297 if (code === null) {
298 var mapUrl = codeUrl
299 var data = {
300 sourceMappingURL: null,
301 url: mapUrl,
302 sourcesRelativeTo: mapUrl,
303 map: null
304 }
305 var readUrl = customDecodeUriComponent(mapUrl)
306 read(readUrl, function(error, result) {
307 if (error) {
308 error.sourceMapData = data
309 return callback(error)
310 }
311 data.map = String(result)
312 try {
313 data.map = parseMapToJSON(data.map, data)
314 } catch (error) {
315 return callback(error)
316 }
317 _resolveSources(data)
318 })
319 } else {
320 resolveSourceMap(code, codeUrl, read, function(error, mapData) {
321 if (error) {
322 return callback(error)
323 }
324 if (!mapData) {
325 return callback(null, null)
326 }
327 _resolveSources(mapData)
328 })
329 }
330
331 function _resolveSources(mapData) {
332 resolveSources(mapData.map, mapData.sourcesRelativeTo, read, options, function(error, result) {
333 if (error) {
334 return callback(error)
335 }
336 mapData.sourcesResolved = result.sourcesResolved
337 mapData.sourcesContent = result.sourcesContent
338 callback(null, mapData)
339 })
340 }
341}
342
343function resolveSync(code, codeUrl, read, options) {
344 var mapData
345 if (code === null) {
346 var mapUrl = codeUrl
347 mapData = {
348 sourceMappingURL: null,
349 url: mapUrl,
350 sourcesRelativeTo: mapUrl,
351 map: null
352 }
353 mapData.map = readSync(read, mapUrl, mapData)
354 mapData.map = parseMapToJSON(mapData.map, mapData)
355 } else {
356 mapData = resolveSourceMapSync(code, codeUrl, read)
357 if (!mapData) {
358 return null
359 }
360 }
361 var result = resolveSourcesSync(mapData.map, mapData.sourcesRelativeTo, read, options)
362 mapData.sourcesResolved = result.sourcesResolved
363 mapData.sourcesContent = result.sourcesContent
364 return mapData
365}
366
367
368
369module.exports = {
370 resolveSourceMap: resolveSourceMap,
371 resolveSourceMapSync: resolveSourceMapSync,
372 resolveSources: resolveSources,
373 resolveSourcesSync: resolveSourcesSync,
374 resolve: resolve,
375 resolveSync: resolveSync,
376 parseMapToJSON: parseMapToJSON
377}
Note: See TracBrowser for help on using the repository browser.