|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { Observable, of } from 'rxjs'; |
| 9 | +import { catchError } from 'rxjs/operators'; |
| 10 | +import { Path, PathFragment } from '../path'; |
| 11 | +import { FileBuffer, HostCapabilities, ReadonlyHost, Stats } from './interface'; |
| 12 | + |
| 13 | +/** |
| 14 | + * A Host that filters out errors. The only exception is `read()` which will still error out if |
| 15 | + * the delegate returned an error (e.g. NodeJS will error out if the file doesn't exist). |
| 16 | + */ |
| 17 | +export class SafeReadonlyHost<StatsT extends object = {}> implements ReadonlyHost<StatsT> { |
| 18 | + constructor(private _delegate: ReadonlyHost<StatsT>) {} |
| 19 | + |
| 20 | + get capabilities(): HostCapabilities { |
| 21 | + return this._delegate.capabilities; |
| 22 | + } |
| 23 | + |
| 24 | + read(path: Path): Observable<FileBuffer> { |
| 25 | + return this._delegate.read(path); |
| 26 | + } |
| 27 | + |
| 28 | + list(path: Path): Observable<PathFragment[]> { |
| 29 | + return this._delegate.list(path).pipe( |
| 30 | + catchError(() => of([])), |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + exists(path: Path): Observable<boolean> { |
| 35 | + return this._delegate.exists(path); |
| 36 | + } |
| 37 | + isDirectory(path: Path): Observable<boolean> { |
| 38 | + return this._delegate.isDirectory(path).pipe( |
| 39 | + catchError(() => of(false)), |
| 40 | + ); |
| 41 | + } |
| 42 | + isFile(path: Path): Observable<boolean> { |
| 43 | + return this._delegate.isFile(path).pipe( |
| 44 | + catchError(() => of(false)), |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + // Some hosts may not support stats. |
| 49 | + stat(path: Path): Observable<Stats<StatsT> | null> | null { |
| 50 | + const maybeStat = this._delegate.stat(path); |
| 51 | + |
| 52 | + return maybeStat && maybeStat.pipe( |
| 53 | + catchError(() => of(null)), |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
0 commit comments