summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/utils/sources-tree/utils.js
blob: f893fa76a5ace444b3069f2d2cf21bca2ab89419 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* 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
import { parse } from "../../utils/url";

import type { TreeNode, TreeSource, TreeDirectory, ParentMap } from "./types";
import type { Source, Thread, URL } from "../../types";
import type { SourcesMapByThread } from "../../reducers/types";
import { isPretty } from "../source";
import { getURL, type ParsedURL } from "./getURL";
const IGNORED_URLS = ["debugger eval code", "XStringBundle"];

export type PathPart = {
  part: string,
  path: string,
  debuggeeHostIfRoot: ?string,
};
export function getPathParts(
  url: ParsedURL,
  thread: string,
  debuggeeHost: ?string
): Array<PathPart> {
  const parts = url.path.split("/");
  if (parts.length > 1 && parts[parts.length - 1] === "") {
    parts.pop();
    if (url.search) {
      parts.push(url.search);
    }
  } else {
    parts[parts.length - 1] += url.search;
  }

  parts[0] = url.group;
  if (thread) {
    parts.unshift(thread);
  }

  let path = "";
  return parts.map((part, index) => {
    if (index == 0 && thread) {
      path = thread;
    } else {
      path = `${path}/${part}`;
    }

    const debuggeeHostIfRoot = index === 1 ? debuggeeHost : null;

    return {
      part,
      path,
      debuggeeHostIfRoot,
    };
  });
}

export function nodeHasChildren(item: TreeNode): boolean {
  return item.type == "directory" && Array.isArray(item.contents);
}

export function isExactUrlMatch(pathPart: string, debuggeeUrl: URL): boolean {
  // compare to hostname with an optional 'www.' prefix
  const { host } = parse(debuggeeUrl);
  if (!host) {
    return false;
  }
  return (
    host === pathPart ||
    host.replace(/^www\./, "") === pathPart.replace(/^www\./, "")
  );
}

export function isPathDirectory(path: string): boolean {
  // Assume that all urls point to files except when they end with '/'
  // Or directory node has children

  if (path.endsWith("/")) {
    return true;
  }

  let separators = 0;
  for (let i = 0; i < path.length - 1; ++i) {
    if (path[i] === "/") {
      if (path[i + i] !== "/") {
        return false;
      }

      ++separators;
    }
  }

  switch (separators) {
    case 0: {
      return false;
    }
    case 1: {
      return !path.startsWith("/");
    }
    default: {
      return true;
    }
  }
}

export function isDirectory(item: TreeNode): boolean {
  return (
    (item.type === "directory" || isPathDirectory(item.path)) &&
    item.name != "(index)"
  );
}

export function getSourceFromNode(item: TreeNode): ?Source {
  const { contents } = item;
  if (!isDirectory(item) && !Array.isArray(contents)) {
    return contents;
  }
}

export function isSource(item: TreeNode): boolean {
  return item.type === "source";
}

export function getFileExtension(source: Source): string {
  const { path } = getURL(source);
  if (!path) {
    return "";
  }

  const lastIndex = path.lastIndexOf(".");
  return lastIndex !== -1 ? path.slice(lastIndex + 1) : "";
}

export function isNotJavaScript(source: Source): boolean {
  return ["css", "svg", "png"].includes(getFileExtension(source));
}

export function isInvalidUrl(url: ParsedURL, source: Source): boolean {
  return (
    !source.url ||
    !url.group ||
    isNotJavaScript(source) ||
    IGNORED_URLS.includes(url) ||
    isPretty(source)
  );
}

export function partIsFile(
  index: number,
  parts: Array<PathPart>,
  url: Object
): boolean {
  const isLastPart = index === parts.length - 1;
  return isLastPart && !isDirectory(url);
}

export function createDirectoryNode(
  name: string,
  path: string,
  contents: TreeNode[]
): TreeDirectory {
  return {
    type: "directory",
    name,
    path,
    contents,
  };
}

export function createSourceNode(
  name: string,
  path: string,
  contents: Source
): TreeSource {
  return {
    type: "source",
    name,
    path,
    contents,
  };
}

export function createParentMap(tree: TreeNode): ParentMap {
  const map = new WeakMap();

  function _traverse(subtree) {
    if (subtree.type === "directory") {
      for (const child of subtree.contents) {
        map.set(child, subtree);
        _traverse(child);
      }
    }
  }

  if (tree.type === "directory") {
    // Don't link each top-level path to the "root" node because the
    // user never sees the root
    tree.contents.forEach(_traverse);
  }

  return map;
}

export function getRelativePath(url: URL): string {
  const { pathname } = parse(url);
  if (!pathname) {
    return url;
  }
  const index = pathname.indexOf("/");

  return index !== -1 ? pathname.slice(index + 1) : "";
}

export function getPathWithoutThread(path: string): string {
  const pathParts = path.split(/(context\d+?\/)/).splice(2);
  if (pathParts && pathParts.length > 0) {
    return pathParts.join("");
  }
  return "";
}

export function findSource(
  { threads, sources }: { threads: Thread[], sources: SourcesMapByThread },
  itemPath: string,
  source: ?Source
): ?Source {
  const targetThread = threads.find(thread => itemPath.includes(thread.actor));
  if (targetThread && source) {
    const { actor } = targetThread;
    if (sources[actor]) {
      return sources[actor][source.id];
    }
  }
  return source;
}

// NOTE: we get the source from sources because item.contents is cached
export function getSource(
  item: TreeNode,
  { threads, sources }: { threads: Thread[], sources: SourcesMapByThread }
): ?Source {
  const source = getSourceFromNode(item);
  return findSource({ threads, sources }, item.path, source);
}

export function getChildren(item: $Shape<TreeDirectory>) {
  return nodeHasChildren(item) ? item.contents : [];
}

export function getAllSources({
  threads,
  sources,
}: {
  threads: Thread[],
  sources: SourcesMapByThread,
}): Source[] {
  const sourcesAll = [];
  threads.forEach(thread => {
    const { actor } = thread;

    for (const source in sources[actor]) {
      sourcesAll.push(sources[actor][source]);
    }
  });
  return sourcesAll;
}

export function getSourcesInsideGroup(
  item: TreeNode,
  { threads, sources }: { threads: Thread[], sources: SourcesMapByThread }
): Source[] {
  const sourcesInsideDirectory = [];

  const findAllSourcesInsideDirectory = (directoryToSearch: TreeDirectory) => {
    const childrenItems = getChildren(directoryToSearch);

    childrenItems.forEach((itemChild: TreeNode) => {
      if (itemChild.type === "directory") {
        findAllSourcesInsideDirectory(itemChild);
      } else {
        const source = getSource(itemChild, { threads, sources });
        if (source) {
          sourcesInsideDirectory.push(source);
        }
      }
    });
  };
  if (item.type === "directory") {
    findAllSourcesInsideDirectory(item);
  }
  return sourcesInsideDirectory;
}