forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (88 loc) · 2.31 KB
/
Copy pathindex.js
File metadata and controls
104 lines (88 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
const md5 = require("md5");
function originalToGeneratedId(sourceId: string) {
if (isGeneratedId(sourceId)) {
return sourceId;
}
const match = sourceId.match(/(.*)\/originalSource/);
return match ? match[1] : "";
}
const getMd5 = memoize((url: string) => md5(url));
function generatedToOriginalId(generatedId: string, url: string) {
return `${generatedId}/originalSource-${getMd5(url)}`;
}
function isOriginalId(id: string) {
return /\/originalSource/.test(id);
}
function isGeneratedId(id: string) {
return !isOriginalId(id);
}
/**
* Trims the query part or reference identifier of a URL string, if necessary.
*/
function trimUrlQuery(url: string): string {
const length = url.length;
const q1 = url.indexOf("?");
const q2 = url.indexOf("&");
const q3 = url.indexOf("#");
const q = Math.min(
q1 != -1 ? q1 : length,
q2 != -1 ? q2 : length,
q3 != -1 ? q3 : length
);
return url.slice(0, q);
}
// Map suffix to content type.
const contentMap = {
js: "text/javascript",
jsm: "text/javascript",
mjs: "text/javascript",
ts: "text/typescript",
tsx: "text/typescript-jsx",
jsx: "text/jsx",
vue: "text/vue",
coffee: "text/coffeescript",
elm: "text/elm",
cljc: "text/x-clojure",
cljs: "text/x-clojurescript"
};
/**
* Returns the content type for the specified URL. If no specific
* content type can be determined, "text/plain" is returned.
*
* @return String
* The content type.
*/
function getContentType(url: string): string {
url = trimUrlQuery(url);
const dot = url.lastIndexOf(".");
if (dot >= 0) {
const name = url.substring(dot + 1);
if (name in contentMap) {
return contentMap[name];
}
}
return "text/plain";
}
function memoize<T: Function, A, R>(func: T): A => R {
const map = new Map();
return (arg: A) => {
if (map.has(arg)) {
return ((map.get(arg): any): R);
}
const result: R = func(arg);
map.set(arg, result);
return result;
};
}
module.exports = {
originalToGeneratedId,
generatedToOriginalId,
isOriginalId,
isGeneratedId,
getContentType,
contentMapForTesting: contentMap
};