forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathIsolatedFileSystem.ts
More file actions
673 lines (595 loc) · 25.9 KB
/
Copy pathIsolatedFileSystem.ts
File metadata and controls
673 lines (595 loc) · 25.9 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import * as Common from '../../core/common/common.js';
import * as Host from '../../core/host/host.js';
import * as i18n from '../../core/i18n/i18n.js';
import * as Platform from '../../core/platform/platform.js';
import * as TextUtils from '../text_utils/text_utils.js';
import {Events, type IsolatedFileSystemManager} from './IsolatedFileSystemManager.js';
import {PlatformFileSystem, PlatformFileSystemType} from './PlatformFileSystem.js';
const UIStrings = {
/**
*@description Text in Isolated File System of the Workspace settings in Settings
*@example {folder does not exist} PH1
*/
fileSystemErrorS: 'File system error: {PH1}',
/**
*@description Error message when reading a remote blob
*/
blobCouldNotBeLoaded: 'Blob could not be loaded.',
/**
*@description Error message when reading a file.
*@example {c:\dir\file.js} PH1
*@example {Underlying error} PH2
*/
cantReadFileSS: 'Can\'t read file: {PH1}: {PH2}',
/**
*@description Text to show something is linked to another
*@example {example.url} PH1
*/
linkedToS: 'Linked to {PH1}',
} as const;
const str_ = i18n.i18n.registerUIStrings('models/persistence/IsolatedFileSystem.ts', UIStrings);
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
export class IsolatedFileSystem extends PlatformFileSystem {
private readonly manager: IsolatedFileSystemManager;
private readonly embedderPathInternal: Platform.DevToolsPath.RawPathString;
private readonly domFileSystem: FileSystem;
private readonly excludedFoldersSetting:
Common.Settings.Setting<{[path: Platform.DevToolsPath.UrlString]: Platform.DevToolsPath.EncodedPathString[]}>;
private excludedFoldersInternal: Set<Platform.DevToolsPath.EncodedPathString>;
private readonly excludedEmbedderFolders: Platform.DevToolsPath.RawPathString[] = [];
private readonly initialFilePathsInternal = new Set<Platform.DevToolsPath.EncodedPathString>();
private readonly initialGitFoldersInternal = new Set<Platform.DevToolsPath.EncodedPathString>();
private readonly fileLocks = new Map<Platform.DevToolsPath.EncodedPathString, Promise<unknown>>();
constructor(
manager: IsolatedFileSystemManager, path: Platform.DevToolsPath.UrlString,
embedderPath: Platform.DevToolsPath.RawPathString, domFileSystem: FileSystem, type: PlatformFileSystemType,
automatic: boolean) {
super(path, type, automatic);
this.manager = manager;
this.embedderPathInternal = embedderPath;
this.domFileSystem = domFileSystem;
this.excludedFoldersSetting =
Common.Settings.Settings.instance().createLocalSetting('workspace-excluded-folders', {});
this.excludedFoldersInternal = new Set(this.excludedFoldersSetting.get()[path] || []);
}
static async create(
manager: IsolatedFileSystemManager, path: Platform.DevToolsPath.UrlString,
embedderPath: Platform.DevToolsPath.RawPathString, type: PlatformFileSystemType, name: string, rootURL: string,
automatic: boolean): Promise<IsolatedFileSystem|null> {
const domFileSystem = Host.InspectorFrontendHost.InspectorFrontendHostInstance.isolatedFileSystem(name, rootURL);
if (!domFileSystem) {
return null;
}
const fileSystem = new IsolatedFileSystem(manager, path, embedderPath, domFileSystem, type, automatic);
return await fileSystem.initializeFilePaths().then(() => fileSystem).catch(error => {
console.error(error);
return null;
});
}
static errorMessage(error: DOMError): string {
return i18nString(UIStrings.fileSystemErrorS, {PH1: error.message});
}
private serializedFileOperation<T>(path: Platform.DevToolsPath.EncodedPathString, operation: () => Promise<T>):
Promise<T> {
const promise = Promise.resolve(this.fileLocks.get(path)).then(() => operation.call(null));
this.fileLocks.set(path, promise);
return promise;
}
override getMetadata(path: Platform.DevToolsPath.EncodedPathString): Promise<Metadata|null> {
const {promise, resolve} = Promise.withResolvers<Metadata|null>();
this.domFileSystem.root.getFile(
Common.ParsedURL.ParsedURL.encodedPathToRawPathString(path), undefined, fileEntryLoaded, errorHandler);
return promise;
function fileEntryLoaded(entry: FileEntry): void {
entry.getMetadata(resolve, errorHandler);
}
function errorHandler(error: DOMError): void {
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when getting file metadata \'' + path);
resolve(null);
}
}
override initialFilePaths(): Platform.DevToolsPath.EncodedPathString[] {
return [...this.initialFilePathsInternal];
}
override initialGitFolders(): Platform.DevToolsPath.EncodedPathString[] {
return [...this.initialGitFoldersInternal];
}
override embedderPath(): Platform.DevToolsPath.RawPathString {
return this.embedderPathInternal;
}
private initializeFilePaths(): Promise<void> {
return new Promise(fulfill => {
let pendingRequests = 1;
const boundInnerCallback = innerCallback.bind(this);
this.requestEntries(Platform.DevToolsPath.EmptyRawPathString, boundInnerCallback);
function innerCallback(this: IsolatedFileSystem, entries: FileEntry[]): void {
for (let i = 0; i < entries.length; ++i) {
const entry = entries[i];
if (!entry.isDirectory) {
if (this.isFileExcluded(Common.ParsedURL.ParsedURL.rawPathToEncodedPathString(
entry.fullPath as Platform.DevToolsPath.RawPathString))) {
continue;
}
this.initialFilePathsInternal.add(Common.ParsedURL.ParsedURL.rawPathToEncodedPathString(
Common.ParsedURL.ParsedURL.substr(entry.fullPath as Platform.DevToolsPath.RawPathString, 1)));
} else {
if (entry.fullPath.endsWith('/.git')) {
const lastSlash = entry.fullPath.lastIndexOf('/');
const parentFolder = Common.ParsedURL.ParsedURL.substr(
entry.fullPath as Platform.DevToolsPath.RawPathString, 1, lastSlash);
this.initialGitFoldersInternal.add(Common.ParsedURL.ParsedURL.rawPathToEncodedPathString(parentFolder));
}
if (this.isFileExcluded(Common.ParsedURL.ParsedURL.concatenate(
Common.ParsedURL.ParsedURL.rawPathToEncodedPathString(
entry.fullPath as Platform.DevToolsPath.RawPathString),
'/'))) {
const url = Common.ParsedURL.ParsedURL.concatenate(
this.path(),
Common.ParsedURL.ParsedURL.rawPathToEncodedPathString(
entry.fullPath as Platform.DevToolsPath.RawPathString));
this.excludedEmbedderFolders.push(
Common.ParsedURL.ParsedURL.urlToRawPathString(url, Host.Platform.isWin()));
continue;
}
++pendingRequests;
this.requestEntries(entry.fullPath as Platform.DevToolsPath.RawPathString, boundInnerCallback);
}
}
if ((--pendingRequests === 0)) {
fulfill();
}
}
});
}
private async createFoldersIfNotExist(folderPath: Platform.DevToolsPath.RawPathString): Promise<DirectoryEntry|null> {
// Fast-path. If parent directory already exists we return it immidiatly.
let dirEntry = await new Promise<DirectoryEntry|null>(
resolve => this.domFileSystem.root.getDirectory(folderPath, undefined, resolve, () => resolve(null)));
if (dirEntry) {
return dirEntry;
}
const paths = folderPath.split('/');
let activePath = '';
for (const path of paths) {
activePath = activePath + '/' + path;
dirEntry = await this.innerCreateFolderIfNeeded(activePath);
if (!dirEntry) {
return null;
}
}
return dirEntry;
}
private innerCreateFolderIfNeeded(path: string): Promise<DirectoryEntry|null> {
return new Promise(resolve => {
this.domFileSystem.root.getDirectory(path, {create: true}, dirEntry => resolve(dirEntry), error => {
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' trying to create directory \'' + path + '\'');
resolve(null);
});
});
}
override async createFile(
path: Platform.DevToolsPath.EncodedPathString,
name: Platform.DevToolsPath.RawPathString|null): Promise<Platform.DevToolsPath.EncodedPathString|null> {
const dirEntry = await this.createFoldersIfNotExist(Common.ParsedURL.ParsedURL.encodedPathToRawPathString(path));
if (!dirEntry) {
return null;
}
const fileEntry = await this.serializedFileOperation(
path, createFileCandidate.bind(this, name || 'NewFile' as Platform.DevToolsPath.RawPathString));
if (!fileEntry) {
return null;
}
return Common.ParsedURL.ParsedURL.rawPathToEncodedPathString(
Common.ParsedURL.ParsedURL.substr(fileEntry.fullPath as Platform.DevToolsPath.RawPathString, 1));
function createFileCandidate(
this: IsolatedFileSystem, name: Platform.DevToolsPath.RawPathString,
newFileIndex?: number): Promise<FileEntry|null> {
return new Promise(resolve => {
const nameCandidate = Common.ParsedURL.ParsedURL.concatenate(name, (newFileIndex || '').toString());
(dirEntry as DirectoryEntry).getFile(nameCandidate, {create: true, exclusive: true}, resolve, error => {
if (error.name === 'InvalidModificationError') {
resolve(createFileCandidate.call(this, name, (newFileIndex ? newFileIndex + 1 : 1)));
return;
}
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(
errorMessage + ' when testing if file exists \'' + (this.path() + '/' + path + '/' + nameCandidate) +
'\'');
resolve(null);
});
});
}
}
override deleteFile(path: Platform.DevToolsPath.EncodedPathString): Promise<boolean> {
const {promise, resolve} = Promise.withResolvers<boolean>();
this.domFileSystem.root.getFile(
Common.ParsedURL.ParsedURL.encodedPathToRawPathString(path), undefined, fileEntryLoaded.bind(this),
errorHandler.bind(this));
return promise;
function fileEntryLoaded(this: IsolatedFileSystem, fileEntry: FileEntry): void {
fileEntry.remove(fileEntryRemoved, errorHandler.bind(this));
}
function fileEntryRemoved(): void {
resolve(true);
}
/**
* TODO(jsbell): Update externs replacing DOMError with DOMException. https://crbug.com/496901
*/
function errorHandler(this: IsolatedFileSystem, error: DOMError): void {
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when deleting file \'' + (this.path() + '/' + path) + '\'');
resolve(false);
}
}
override deleteDirectoryRecursively(path: Platform.DevToolsPath.EncodedPathString): Promise<boolean> {
const {promise, resolve} = Promise.withResolvers<boolean>();
this.domFileSystem.root.getDirectory(
Common.ParsedURL.ParsedURL.encodedPathToRawPathString(path), undefined, dirEntryLoaded.bind(this),
errorHandler.bind(this));
return promise;
function dirEntryLoaded(this: IsolatedFileSystem, dirEntry: DirectoryEntry): void {
dirEntry.removeRecursively(dirEntryRemoved, errorHandler.bind(this));
}
function dirEntryRemoved(): void {
resolve(true);
}
/**
* TODO(jsbell): Update externs replacing DOMError with DOMException. https://crbug.com/496901
*/
function errorHandler(this: IsolatedFileSystem, error: DOMError): void {
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when deleting directory \'' + (this.path() + '/' + path) + '\'');
resolve(false);
}
}
override requestFileBlob(path: Platform.DevToolsPath.EncodedPathString): Promise<Blob|null> {
return new Promise(resolve => {
this.domFileSystem.root.getFile(Common.ParsedURL.ParsedURL.encodedPathToRawPathString(path), undefined, entry => {
entry.file(resolve, errorHandler.bind(this));
}, errorHandler.bind(this));
function errorHandler(this: IsolatedFileSystem, error: DOMError): void {
if (error.name === 'NotFoundError') {
resolve(null);
return;
}
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when getting content for file \'' + (this.path() + '/' + path) + '\'');
resolve(null);
}
});
}
override requestFileContent(path: Platform.DevToolsPath.EncodedPathString):
Promise<TextUtils.ContentData.ContentDataOrError> {
return this.serializedFileOperation(path, () => this.innerRequestFileContent(path));
}
private async innerRequestFileContent(path: Platform.DevToolsPath.EncodedPathString):
Promise<TextUtils.ContentData.ContentDataOrError> {
const blob = await this.requestFileBlob(path);
if (!blob) {
return {error: i18nString(UIStrings.blobCouldNotBeLoaded)};
}
const mimeType = mimeTypeForBlob(path, blob);
try {
if (Platform.MimeType.isTextType(mimeType)) {
return new TextUtils.ContentData.ContentData(await blob.text(), /* isBase64 */ false, mimeType);
}
return new TextUtils.ContentData.ContentData(await Common.Base64.encode(blob), /* isBase64 */ true, mimeType);
} catch (e) {
return {error: i18nString(UIStrings.cantReadFileSS, {PH1: path, PH2: e.message})};
}
}
override async setFileContent(path: Platform.DevToolsPath.EncodedPathString, content: string, isBase64: boolean):
Promise<void> {
Host.userMetrics.actionTaken(Host.UserMetrics.Action.FileSavedInWorkspace);
let resolve: (result: ProgressEvent<EventTarget>|undefined) => void;
const innerSetFileContent = (): Promise<ProgressEvent<EventTarget>|undefined> => {
const promise = new Promise<ProgressEvent<EventTarget>|undefined>(x => {
resolve = x;
});
this.domFileSystem.root.getFile(
Common.ParsedURL.ParsedURL.encodedPathToRawPathString(path), {create: true}, fileEntryLoaded.bind(this),
errorHandler.bind(this));
return promise;
};
void this.serializedFileOperation(path, innerSetFileContent);
function fileEntryLoaded(this: IsolatedFileSystem, entry: FileEntry): void {
entry.createWriter(fileWriterCreated.bind(this), errorHandler.bind(this));
}
async function fileWriterCreated(this: IsolatedFileSystem, fileWriter: FileWriter): Promise<void> {
fileWriter.onerror = errorHandler.bind(this);
fileWriter.onwriteend = fileWritten;
let blob: Blob;
if (isBase64) {
blob = await (await fetch(`data:application/octet-stream;base64,${content}`)).blob();
} else {
blob = new Blob([content], {type: 'text/plain'});
}
fileWriter.write(blob);
function fileWritten(): void {
fileWriter.onwriteend = resolve;
fileWriter.truncate(blob.size);
}
}
function errorHandler(this: IsolatedFileSystem, error: DOMError|ProgressEvent<EventTarget>): void {
// @ts-expect-error TODO(crbug.com/1172300) Properly type this after jsdoc to ts migration
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when setting content for file \'' + (this.path() + '/' + path) + '\'');
resolve(undefined);
}
}
override renameFile(
path: Platform.DevToolsPath.EncodedPathString, newName: Platform.DevToolsPath.RawPathString,
callback: (arg0: boolean, arg1?: string|undefined) => void): void {
newName = newName ? Common.ParsedURL.ParsedURL.trim(newName) : newName;
if (!newName || newName.indexOf('/') !== -1) {
callback(false);
return;
}
let fileEntry: FileEntry;
let dirEntry: DirectoryEntry;
this.domFileSystem.root.getFile(
Common.ParsedURL.ParsedURL.encodedPathToRawPathString(path), undefined, fileEntryLoaded.bind(this),
errorHandler.bind(this));
function fileEntryLoaded(this: IsolatedFileSystem, entry: FileEntry): void {
if (entry.name === newName) {
callback(false);
return;
}
fileEntry = entry;
fileEntry.getParent(dirEntryLoaded.bind(this), errorHandler.bind(this));
}
function dirEntryLoaded(this: IsolatedFileSystem, entry: DirectoryEntry): void {
dirEntry = entry;
dirEntry.getFile(newName, undefined, newFileEntryLoaded, newFileEntryLoadErrorHandler.bind(this));
}
function newFileEntryLoaded(_entry: FileEntry): void {
callback(false);
}
function newFileEntryLoadErrorHandler(this: IsolatedFileSystem, error: DOMError): void {
if (error.name !== 'NotFoundError') {
callback(false);
return;
}
fileEntry.moveTo(dirEntry, newName, fileRenamed, errorHandler.bind(this));
}
function fileRenamed(entry: Entry): void {
callback(true, entry.name);
}
function errorHandler(this: IsolatedFileSystem, error: DOMError): void {
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when renaming file \'' + (this.path() + '/' + path) + '\' to \'' + newName + '\'');
callback(false);
}
}
private readDirectory(dirEntry: DirectoryEntry, callback: (arg0: FileEntry[]) => void): void {
const dirReader = dirEntry.createReader();
let entries: FileEntry[] = [];
function innerCallback(results: Entry[]): void {
if (!results.length) {
callback(entries.sort());
} else {
entries = entries.concat(toArray(results));
dirReader.readEntries(innerCallback, errorHandler);
}
}
function toArray(list: Entry[]): FileEntry[] {
return Array.prototype.slice.call(list || [], 0);
}
dirReader.readEntries(innerCallback, errorHandler);
function errorHandler(error: DOMError): void {
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when reading directory \'' + dirEntry.fullPath + '\'');
callback([]);
}
}
private requestEntries(path: Platform.DevToolsPath.RawPathString, callback: (arg0: FileEntry[]) => void): void {
this.domFileSystem.root.getDirectory(path, undefined, innerCallback.bind(this), errorHandler);
function innerCallback(this: IsolatedFileSystem, dirEntry: DirectoryEntry): void {
this.readDirectory(dirEntry, callback);
}
function errorHandler(error: DOMError): void {
const errorMessage = IsolatedFileSystem.errorMessage(error);
console.error(errorMessage + ' when requesting entry \'' + path + '\'');
callback([]);
}
}
private saveExcludedFolders(): void {
const settingValue = this.excludedFoldersSetting.get();
settingValue[this.path()] = [...this.excludedFoldersInternal];
this.excludedFoldersSetting.set(settingValue);
}
override addExcludedFolder(path: Platform.DevToolsPath.EncodedPathString): void {
this.excludedFoldersInternal.add(path);
this.saveExcludedFolders();
this.manager.dispatchEventToListeners(Events.ExcludedFolderAdded, path);
}
override removeExcludedFolder(path: Platform.DevToolsPath.EncodedPathString): void {
this.excludedFoldersInternal.delete(path);
this.saveExcludedFolders();
this.manager.dispatchEventToListeners(Events.ExcludedFolderRemoved, path);
}
override fileSystemRemoved(): void {
const settingValue = this.excludedFoldersSetting.get();
delete settingValue[this.path()];
this.excludedFoldersSetting.set(settingValue);
}
override isFileExcluded(folderPath: Platform.DevToolsPath.EncodedPathString): boolean {
if (this.excludedFoldersInternal.has(folderPath)) {
return true;
}
const regex = (this.manager.workspaceFolderExcludePatternSetting()).asRegExp();
return Boolean(regex?.test(Common.ParsedURL.ParsedURL.encodedPathToRawPathString(folderPath)));
}
override excludedFolders(): Set<Platform.DevToolsPath.EncodedPathString> {
return this.excludedFoldersInternal;
}
override searchInPath(query: string, progress: Common.Progress.Progress): Promise<string[]> {
return new Promise(resolve => {
const requestId = this.manager.registerCallback(innerCallback);
Host.InspectorFrontendHost.InspectorFrontendHostInstance.searchInPath(
requestId, this.embedderPathInternal, query);
function innerCallback(files: Platform.DevToolsPath.RawPathString[]): void {
resolve(files.map(path => Common.ParsedURL.ParsedURL.rawPathToUrlString(path)));
progress.incrementWorked(1);
}
});
}
override indexContent(progress: Common.Progress.Progress): void {
progress.setTotalWork(1);
const requestId = this.manager.registerProgress(progress);
Host.InspectorFrontendHost.InspectorFrontendHostInstance.indexPath(
requestId, this.embedderPathInternal, JSON.stringify(this.excludedEmbedderFolders));
}
override mimeFromPath(path: Platform.DevToolsPath.UrlString): string {
return Common.ResourceType.ResourceType.mimeFromURL(path) || 'text/plain';
}
override canExcludeFolder(path: Platform.DevToolsPath.EncodedPathString): boolean {
return Boolean(path) && this.type() !== PlatformFileSystemType.OVERRIDES;
}
// path not typed as Branded Types as here we are interested in extention only
override contentType(path: string): Common.ResourceType.ResourceType {
const extension = Common.ParsedURL.ParsedURL.extractExtension(path);
if (STYLE_SHEET_EXTENSIONS.has(extension)) {
return Common.ResourceType.resourceTypes.Stylesheet;
}
if (DOCUMENT_EXTENSIONS.has(extension)) {
return Common.ResourceType.resourceTypes.Document;
}
if (IMAGE_EXTENSIONS.has(extension)) {
return Common.ResourceType.resourceTypes.Image;
}
if (SCRIPT_EXTENSIONS.has(extension)) {
return Common.ResourceType.resourceTypes.Script;
}
return BinaryExtensions.has(extension) ? Common.ResourceType.resourceTypes.Other :
Common.ResourceType.resourceTypes.Document;
}
override tooltipForURL(url: Platform.DevToolsPath.UrlString): string {
const path = Platform.StringUtilities.trimMiddle(
Common.ParsedURL.ParsedURL.urlToRawPathString(url, Host.Platform.isWin()), 150);
return i18nString(UIStrings.linkedToS, {PH1: path});
}
override supportsAutomapping(): boolean {
return this.type() !== PlatformFileSystemType.OVERRIDES;
}
}
/**
* @returns Tries to determine the mime type for this Blob:
* 1) If blob.type is non-empty, we return that.
* 2) If we know it from the extension, use that.
* 3) Check the list of known binary extensions and use application/octet-stream.
* 4) Use text/plain
*/
function mimeTypeForBlob(path: Platform.DevToolsPath.EncodedPathString, blob: Blob): string {
if (blob.type) {
return blob.type;
}
const extension = Common.ParsedURL.ParsedURL.extractExtension(path);
const maybeMime = Common.ResourceType.ResourceType.mimeFromExtension(extension);
if (maybeMime) {
return maybeMime;
}
return BinaryExtensions.has(extension) ? 'application/octet-stream' : 'text/plain';
}
const STYLE_SHEET_EXTENSIONS = new Set<string>(['css', 'scss', 'sass', 'less']);
const DOCUMENT_EXTENSIONS = new Set<string>(['htm', 'html', 'asp', 'aspx', 'phtml', 'jsp']);
const SCRIPT_EXTENSIONS = new Set<string>([
'asp', 'aspx', 'c', 'cc', 'cljs', 'coffee', 'cpp', 'cs', 'dart', 'java', 'js',
'jsp', 'jsx', 'h', 'm', 'mjs', 'mm', 'py', 'sh', 'ts', 'tsx', 'ls',
]);
const IMAGE_EXTENSIONS = new Set<string>(['jpeg', 'jpg', 'svg', 'gif', 'webp', 'png', 'ico', 'tiff', 'tif', 'bmp']);
export const BinaryExtensions = new Set<string>([
// Executable extensions, roughly taken from https://en.wikipedia.org/wiki/Comparison_of_executable_file_formats
'cmd',
'com',
'exe',
// Archive extensions, roughly taken from https://en.wikipedia.org/wiki/List_of_archive_formats
'a',
'ar',
'iso',
'tar',
'bz2',
'gz',
'lz',
'lzma',
'z',
'7z',
'apk',
'arc',
'cab',
'dmg',
'jar',
'pak',
'rar',
'zip',
// Audio file extensions, roughly taken from https://en.wikipedia.org/wiki/Audio_file_format#List_of_formats
'3gp',
'aac',
'aiff',
'flac',
'm4a',
'mmf',
'mp3',
'ogg',
'oga',
'raw',
'sln',
'wav',
'wma',
'webm',
// Video file extensions, roughly taken from https://en.wikipedia.org/wiki/Video_file_format
'mkv',
'flv',
'vob',
'ogv',
'gifv',
'avi',
'mov',
'qt',
'mp4',
'm4p',
'm4v',
'mpg',
'mpeg',
// Image file extensions
'jpeg',
'jpg',
'gif',
'webp',
'png',
'ico',
'tiff',
'tif',
'bmp',
]);