@theia/filesystem 1.55.1 → 1.57.0-next.7

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.
Files changed (34) hide show
  1. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.d.ts +1 -2
  2. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.d.ts.map +1 -1
  3. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.js +0 -4
  4. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.js.map +1 -1
  5. package/lib/browser/filesystem-preferences.d.ts +1 -0
  6. package/lib/browser/filesystem-preferences.d.ts.map +1 -1
  7. package/lib/browser/filesystem-preferences.js +6 -0
  8. package/lib/browser/filesystem-preferences.js.map +1 -1
  9. package/lib/browser-only/browser-only-filesystem-frontend-module.js +7 -7
  10. package/lib/browser-only/browser-only-filesystem-frontend-module.js.map +1 -1
  11. package/lib/browser-only/opfs-filesystem-initialization.d.ts +11 -0
  12. package/lib/browser-only/opfs-filesystem-initialization.d.ts.map +1 -0
  13. package/lib/browser-only/opfs-filesystem-initialization.js +33 -0
  14. package/lib/browser-only/opfs-filesystem-initialization.js.map +1 -0
  15. package/lib/browser-only/opfs-filesystem-provider.d.ts +31 -0
  16. package/lib/browser-only/opfs-filesystem-provider.d.ts.map +1 -0
  17. package/lib/browser-only/opfs-filesystem-provider.js +323 -0
  18. package/lib/browser-only/opfs-filesystem-provider.js.map +1 -0
  19. package/package.json +4 -5
  20. package/src/browser/breadcrumbs/filepath-breadcrumbs-container.ts +1 -4
  21. package/src/browser/filesystem-preferences.ts +7 -0
  22. package/src/browser-only/browser-only-filesystem-frontend-module.ts +7 -7
  23. package/src/browser-only/opfs-filesystem-initialization.ts +36 -0
  24. package/src/browser-only/opfs-filesystem-provider.ts +347 -0
  25. package/lib/browser-only/browserfs-filesystem-initialization.d.ts +0 -13
  26. package/lib/browser-only/browserfs-filesystem-initialization.d.ts.map +0 -1
  27. package/lib/browser-only/browserfs-filesystem-initialization.js +0 -55
  28. package/lib/browser-only/browserfs-filesystem-initialization.js.map +0 -1
  29. package/lib/browser-only/browserfs-filesystem-provider.d.ts +0 -46
  30. package/lib/browser-only/browserfs-filesystem-provider.d.ts.map +0 -1
  31. package/lib/browser-only/browserfs-filesystem-provider.js +0 -440
  32. package/lib/browser-only/browserfs-filesystem-provider.js.map +0 -1
  33. package/src/browser-only/browserfs-filesystem-initialization.ts +0 -61
  34. package/src/browser-only/browserfs-filesystem-provider.ts +0 -462
@@ -1,462 +0,0 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2023 EclipseSource and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
- /*---------------------------------------------------------------------------------------------
17
- * Copyright (c) Microsoft Corporation. All rights reserved.
18
- * Licensed under the MIT License. See License.txt in the project root for license information.
19
- *--------------------------------------------------------------------------------------------*/
20
- // based on https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/platform/files/node/diskFileSystemProvider.ts
21
-
22
- /* eslint-disable no-null/no-null */
23
-
24
- import { inject, injectable } from '@theia/core/shared/inversify';
25
- import {
26
- FileChange, FileDeleteOptions, FileOpenOptions,
27
- FileOverwriteOptions, FileReadStreamOptions, FileSystemProviderCapabilities,
28
- FileSystemProviderError,
29
- FileSystemProviderErrorCode,
30
- FileSystemProviderWithFileReadWriteCapability,
31
- FileType, FileUpdateOptions, FileUpdateResult, FileWriteOptions, Stat, WatchOptions, createFileSystemProviderError
32
- } from '../common/files';
33
- import { Event, URI, Disposable, CancellationToken } from '@theia/core';
34
- import { TextDocumentContentChangeEvent } from '@theia/core/shared/vscode-languageserver-protocol';
35
- import { ReadableStreamEvents } from '@theia/core/lib/common/stream';
36
- import { BFSRequire } from 'browserfs';
37
- import type { FSModule } from 'browserfs/dist/node/core/FS';
38
- import type { FileSystem } from 'browserfs/dist/node/core/file_system';
39
- import MountableFileSystem from 'browserfs/dist/node/backend/MountableFileSystem';
40
- import { basename, dirname, normalize } from 'path';
41
- import Stats from 'browserfs/dist/node/core/node_fs_stats';
42
- import { retry } from '@theia/core/lib/common/promise-util';
43
- import { BrowserFSInitialization } from './browserfs-filesystem-initialization';
44
-
45
- // adapted from DiskFileSystemProvider
46
- @injectable()
47
- export class BrowserFSFileSystemProvider implements FileSystemProviderWithFileReadWriteCapability {
48
- capabilities: FileSystemProviderCapabilities = FileSystemProviderCapabilities.FileReadWrite;
49
- onDidChangeCapabilities: Event<void> = Event.None;
50
- onDidChangeFile: Event<readonly FileChange[]> = Event.None;
51
- onFileWatchError: Event<void> = Event.None;
52
- private mapHandleToPos: Map<number, number> = new Map();
53
- private writeHandles: Set<number> = new Set();
54
- private canFlush: boolean = true;
55
-
56
- private fs: FSModule;
57
- private mountableFS: MountableFileSystem;
58
- private initialized: Promise<true>;
59
-
60
- constructor(@inject(BrowserFSInitialization) readonly initialization: BrowserFSInitialization) {
61
- const init = async (): Promise<true> => {
62
- this.mountableFS = await initialization.createMountableFileSystem();
63
- this.fs = BFSRequire('fs');
64
- await initialization.initializeFS(this.fs, new Proxy(this, {
65
- get(target, prop, receiver): unknown {
66
- if (prop === 'initialized') {
67
- return Promise.resolve(true);
68
- }
69
- return Reflect.get(target, prop, receiver);
70
- }
71
- }));
72
- return true;
73
- };
74
- this.initialized = init();
75
- }
76
-
77
- async mount(mountPoint: string, fs: FileSystem): Promise<void> {
78
- await this.initialized;
79
- this.mountableFS.mount(mountPoint, fs);
80
- };
81
-
82
- watch(_resource: URI, _opts: WatchOptions): Disposable {
83
- return Disposable.NULL;
84
- }
85
- async stat(resource: URI): Promise<Stat> {
86
- await this.initialized;
87
- const path = this.toFilePath(resource);
88
-
89
- let stats: Stats;
90
- try {
91
- stats = await this.promisify(this.fs.stat)(path) as Stats;
92
- } catch (error) {
93
- throw this.toFileSystemProviderError(error);
94
- }
95
- if (stats === undefined) {
96
- throw new Error(`Could not read file stat for resource '${path}'`);
97
- }
98
- return {
99
- type: this.toType(stats, /* symbolicLink */undefined), // FIXME: missing symbolicLink
100
- ctime: stats.birthtime.getTime(), // intentionally not using ctime here, we want the creation time
101
- mtime: stats.mtime.getTime(),
102
- size: stats.size,
103
- // FIXME: missing mode, permissions
104
- };
105
-
106
- }
107
- async mkdir(resource: URI): Promise<void> {
108
- await this.initialized;
109
- try {
110
- await this.promisify(this.fs.mkdir)(this.toFilePath(resource));
111
- } catch (error) {
112
- throw this.toFileSystemProviderError(error);
113
- }
114
- }
115
- async readdir(resource: URI): Promise<[string, FileType][]> {
116
- await this.initialized;
117
- try {
118
-
119
- const children = await this.promisify(this.fs.readdir)(this.toFilePath(resource)) as string[];
120
- const result: [string, FileType][] = [];
121
- await Promise.all(children.map(async child => {
122
- try {
123
- const stat = await this.stat(resource.resolve(child));
124
- result.push([child, stat.type]);
125
- } catch (error) {
126
- console.trace(error); // ignore errors for individual entries that can arise from permission denied
127
- }
128
- }));
129
-
130
- return result;
131
- } catch (error) {
132
- throw this.toFileSystemProviderError(error);
133
- }
134
- }
135
- async delete(resource: URI, _opts: FileDeleteOptions): Promise<void> {
136
- await this.initialized;
137
- // FIXME use options
138
- try {
139
- await this.promisify(this.fs.unlink)(this.toFilePath(resource));
140
- } catch (error) {
141
- throw this.toFileSystemProviderError(error);
142
- }
143
- }
144
- async rename(from: URI, to: URI, opts: FileOverwriteOptions): Promise<void> {
145
- await this.initialized;
146
- const fromFilePath = this.toFilePath(from);
147
- const toFilePath = this.toFilePath(to);
148
- if (fromFilePath === toFilePath) {
149
- return; // simulate node.js behaviour here and do a no-op if paths match
150
- }
151
- try {
152
- // assume FS is path case sensitive - correct?
153
- const targetExists = await this.promisify(this.fs.exists)(toFilePath);
154
- if (targetExists) {
155
- throw Error(`File '${toFilePath}' already exists.`);
156
- }
157
- if (fromFilePath === toFilePath) {
158
- return Promise.resolve();
159
- }
160
-
161
- await this.promisify(this.fs.rename)(fromFilePath, toFilePath);
162
-
163
- const stat = await this.promisify(this.fs.lstat)(toFilePath) as Stats;
164
- if (stat.isDirectory() || stat.isSymbolicLink()) {
165
- return Promise.resolve(); // only for files
166
- }
167
- const fd = await this.promisify(open)(toFilePath, 'a');
168
- try {
169
- await this.promisify(this.fs.futimes)(fd, stat.atime, new Date());
170
- } catch (error) {
171
- // ignore
172
- }
173
-
174
- this.promisify(this.fs.close)(fd);
175
- } catch (error) {
176
- // rewrite some typical errors that can happen especially around symlinks
177
- // to something the user can better understand
178
- if (error.code === 'EINVAL' || error.code === 'EBUSY' || error.code === 'ENAMETOOLONG') {
179
- error = new Error(`Unable to move '${basename(fromFilePath)}' into '${basename(dirname(toFilePath))}' (${error.toString()}).`);
180
- }
181
-
182
- throw this.toFileSystemProviderError(error);
183
- }
184
- }
185
- async copy?(from: URI, to: URI, opts: FileOverwriteOptions): Promise<void> {
186
- await this.initialized;
187
- throw new Error('Method not implemented.');
188
- }
189
- async readFile(resource: URI): Promise<Uint8Array> {
190
- await this.initialized;
191
- try {
192
- const filePath = this.toFilePath(resource);
193
- return await this.promisify(this.fs.readFile)(filePath) as Uint8Array;
194
- } catch (error) {
195
- throw this.toFileSystemProviderError(error);
196
- }
197
- }
198
- async writeFile(resource: URI, content: Uint8Array, opts: FileWriteOptions): Promise<void> {
199
- await this.initialized;
200
- let handle: number | undefined = undefined;
201
- try {
202
- const filePath = this.toFilePath(resource);
203
-
204
- // Validate target unless { create: true, overwrite: true }
205
- if (!opts.create || !opts.overwrite) {
206
- const fileExists = await this.promisify(this.fs.exists)(filePath);
207
- if (fileExists) {
208
- if (!opts.overwrite) {
209
- throw createFileSystemProviderError('File already exists', FileSystemProviderErrorCode.FileExists);
210
- }
211
- } else {
212
- if (!opts.create) {
213
- throw createFileSystemProviderError('File does not exist', FileSystemProviderErrorCode.FileNotFound);
214
- }
215
- }
216
- }
217
-
218
- // Open
219
- handle = await this.open(resource, { create: true });
220
-
221
- // Write content at once
222
- await this.write(handle, 0, content, 0, content.byteLength);
223
- } catch (error) {
224
- throw this.toFileSystemProviderError(error);
225
- } finally {
226
- if (typeof handle === 'number') {
227
- await this.close(handle);
228
- }
229
- }
230
- }
231
- readFileStream?(resource: URI, opts: FileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array> {
232
- throw new Error('Method not implemented.');
233
- }
234
- async open(resource: URI, opts: FileOpenOptions): Promise<number> {
235
- await this.initialized;
236
- try {
237
- const filePath = this.toFilePath(resource);
238
-
239
- let flags: string | undefined = undefined;
240
- if (opts.create) {
241
- // we take opts.create as a hint that the file is opened for writing
242
- // as such we use 'w' to truncate an existing or create the
243
- // file otherwise. we do not allow reading.
244
- if (!flags) {
245
- flags = 'w';
246
- }
247
- } else {
248
- // otherwise we assume the file is opened for reading
249
- // as such we use 'r' to neither truncate, nor create
250
- // the file.
251
- flags = 'r';
252
- }
253
-
254
- const handle = await this.promisify(this.fs.open)(filePath, flags) as number;
255
-
256
- // remember this handle to track file position of the handle
257
- // we init the position to 0 since the file descriptor was
258
- // just created and the position was not moved so far (see
259
- // also http://man7.org/linux/man-pages/man2/open.2.html -
260
- // "The file offset is set to the beginning of the file.")
261
- this.mapHandleToPos.set(handle, 0);
262
-
263
- // remember that this handle was used for writing
264
- if (opts.create) {
265
- this.writeHandles.add(handle);
266
- }
267
-
268
- return handle;
269
- } catch (error) {
270
- throw this.toFileSystemProviderError(error);
271
- }
272
- }
273
- async close(fd: number): Promise<void> {
274
- await this.initialized;
275
- // remove this handle from map of positions
276
- this.mapHandleToPos.delete(fd);
277
-
278
- // if a handle is closed that was used for writing, ensure
279
- // to flush the contents to disk if possible.
280
- if (this.writeHandles.delete(fd) && this.canFlush) {
281
- try {
282
- await this.promisify(this.fs.fdatasync)(fd);
283
- } catch (error) {
284
- // In some exotic setups it is well possible that node fails to sync
285
- // In that case we disable flushing and log the error to our logger
286
- this.canFlush = false;
287
- console.error(error);
288
- }
289
- }
290
-
291
- await this.promisify(this.fs.close)(fd);
292
- }
293
- async read(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
294
- await this.initialized;
295
- const normalizedPos = this.normalizePos(fd, pos);
296
-
297
- let bytesRead: number | null = null;
298
- try {
299
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
300
- const result: { bytesRead: number, buffer: Uint8Array } | number = (await this.promisify(this.fs.read)(fd, data, offset, length, normalizedPos)) as any;
301
-
302
- if (typeof result === 'number') {
303
- bytesRead = result; // node.d.ts fail
304
- } else {
305
- bytesRead = result.bytesRead;
306
- }
307
-
308
- return bytesRead;
309
- } catch (error) {
310
- throw this.toFileSystemProviderError(error);
311
- } finally {
312
- this.updatePos(fd, normalizedPos, bytesRead);
313
- }
314
- }
315
- async write(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
316
- await this.initialized;
317
- // we know at this point that the file to write to is truncated and thus empty
318
- // if the write now fails, the file remains empty. as such we really try hard
319
- // to ensure the write succeeds by retrying up to three times.
320
- return retry(() => this.doWrite(fd, pos, data, offset, length), 100 /* ms delay */, 3 /* retries */);
321
-
322
- }
323
- private async doWrite(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
324
- await this.initialized;
325
- const normalizedPos = this.normalizePos(fd, pos);
326
-
327
- let bytesWritten: number | null = null;
328
- try {
329
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
330
- const result: { bytesWritten: number, buffer: Uint8Array } | number = (await this.promisify(this.fs.write)(fd, data, offset, length, normalizedPos)) as any;
331
-
332
- if (typeof result === 'number') {
333
- bytesWritten = result; // node.d.ts fail
334
- } else {
335
- bytesWritten = result.bytesWritten;
336
- }
337
-
338
- return bytesWritten;
339
- } catch (error) {
340
- throw this.toFileSystemProviderError(error);
341
- } finally {
342
- this.updatePos(fd, normalizedPos, bytesWritten);
343
- }
344
- }
345
- private normalizePos(fd: number, pos: number): number | null {
346
-
347
- // when calling fs.read/write we try to avoid passing in the "pos" argument and
348
- // rather prefer to pass in "null" because this avoids an extra seek(pos)
349
- // call that in some cases can even fail (e.g. when opening a file over FTP -
350
- // see https://github.com/microsoft/vscode/issues/73884).
351
- //
352
- // as such, we compare the passed in position argument with our last known
353
- // position for the file descriptor and use "null" if they match.
354
- if (pos === this.mapHandleToPos.get(fd)) {
355
- return null;
356
- }
357
-
358
- return pos;
359
- }
360
- private updatePos(fd: number, pos: number | null, bytesLength: number | null): void {
361
- const lastKnownPos = this.mapHandleToPos.get(fd);
362
- if (typeof lastKnownPos === 'number') {
363
-
364
- // pos !== null signals that previously a position was used that is
365
- // not null. node.js documentation explains, that in this case
366
- // the internal file pointer is not moving and as such we do not move
367
- // our position pointer.
368
- //
369
- // Docs: "If position is null, data will be read from the current file position,
370
- // and the file position will be updated. If position is an integer, the file position
371
- // will remain unchanged."
372
- if (typeof pos === 'number') {
373
- // do not modify the position
374
- } else if (typeof bytesLength === 'number') {
375
- this.mapHandleToPos.set(fd, lastKnownPos + bytesLength);
376
- } else {
377
- this.mapHandleToPos.delete(fd);
378
- }
379
- }
380
- }
381
- async access?(resource: URI, mode?: number | undefined): Promise<void> {
382
- await this.initialized;
383
- throw new Error('Method not implemented.');
384
- }
385
- async fsPath?(resource: URI): Promise<string> {
386
- await this.initialized;
387
- throw new Error('Method not implemented.');
388
- }
389
- async updateFile?(resource: URI, changes: TextDocumentContentChangeEvent[], opts: FileUpdateOptions): Promise<FileUpdateResult> {
390
- await this.initialized;
391
- throw new Error('Method not implemented.');
392
- }
393
-
394
- private toFilePath(resource: URI): string {
395
- return normalize(resource.path.toString());
396
- }
397
-
398
- private toType(entry: Stats, symbolicLink?: { dangling: boolean }): FileType {
399
- // Signal file type by checking for file / directory, except:
400
- // - symbolic links pointing to non-existing files are FileType.Unknown
401
- // - files that are neither file nor directory are FileType.Unknown
402
- let type: FileType;
403
- if (symbolicLink?.dangling) {
404
- type = FileType.Unknown;
405
- } else if (entry.isFile()) {
406
- type = FileType.File;
407
- } else if (entry.isDirectory()) {
408
- type = FileType.Directory;
409
- } else {
410
- type = FileType.Unknown;
411
- }
412
-
413
- // Always signal symbolic link as file type additionally
414
- if (symbolicLink) {
415
- type |= FileType.SymbolicLink;
416
- }
417
-
418
- return type;
419
- }
420
-
421
- // FIXME typing
422
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
423
- private promisify<T>(f: Function): (...args: any[]) => Promise<T> {
424
- // eslint-disable-next-line @typescript-eslint/tslint/config, @typescript-eslint/no-explicit-any
425
- return function (...args: any[]) {
426
- return new Promise((resolve, reject) => {
427
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
428
- f(...args, (err: Error, result: T) => err ? reject(err) : resolve(result));
429
- });
430
- };
431
- }
432
-
433
- private toFileSystemProviderError(error: NodeJS.ErrnoException): FileSystemProviderError {
434
- if (error instanceof FileSystemProviderError) {
435
- return error; // avoid double conversion
436
- }
437
-
438
- let code: FileSystemProviderErrorCode;
439
- switch (error.code) {
440
- case 'ENOENT':
441
- code = FileSystemProviderErrorCode.FileNotFound;
442
- break;
443
- case 'EISDIR':
444
- code = FileSystemProviderErrorCode.FileIsADirectory;
445
- break;
446
- case 'ENOTDIR':
447
- code = FileSystemProviderErrorCode.FileNotADirectory;
448
- break;
449
- case 'EEXIST':
450
- code = FileSystemProviderErrorCode.FileExists;
451
- break;
452
- case 'EPERM':
453
- case 'EACCES':
454
- code = FileSystemProviderErrorCode.NoPermissions;
455
- break;
456
- default:
457
- code = FileSystemProviderErrorCode.Unknown;
458
- }
459
-
460
- return createFileSystemProviderError(error, code);
461
- }
462
- }