Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 108x 108x 108x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 44x 44x 44x 44x 44x 48x 48x 8x 8x 40x 40x 36x 36x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 1x 1x 3x 3x 35x 17x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 1x 1x 1x 1x 40x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 7x 7x 5x 5x 7x 4x 4x 1x 1x 1x 1x 1x 1x 1x 61x 61x 61x 61x 61x 61x 61x 61x 1x 1x 1x 1x 1x 1x 1x 1x 1x 48x 48x 48x 48x 29x 48x 10x 19x 8x 8x 8x 8x 8x 8x 9x 1x 1x 48x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 1x 1x 3x 3x 34x 26x 26x 26x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 8x 8x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 2x 2x 10x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 10x 2x 2x 2x 1x 1x 1x 1x 10x 10x 10x 10x 1x 1x 2x 2x 10x 1x 1x 100x 100x 100x 100x 100x 1x 1x 100x 1x | /*
* Copyright (c) 2025 Erik Fortune
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// File System Access API types are imported via the file-api-types module
import { Result, succeed, fail } from '@fgv/ts-utils';
import { FileTree } from '@fgv/ts-json-base';
import {
isFileHandle,
isDirectoryHandle,
FileSystemFileHandle,
FileSystemDirectoryHandle
} from '../file-api-types';
/**
* Interface for File objects that may have the webkitRelativePath property.
* This property is added by browsers when using directory upload (webkitdirectory).
* @internal
*/
interface IFileWithWebkitPath {
readonly webkitRelativePath?: string;
}
/**
* Helper function to safely get webkitRelativePath from a File object.
* @internal
*/
function getFileRelativePath(file: File): string {
return (file as unknown as IFileWithWebkitPath).webkitRelativePath || file.name;
}
/**
* Tree initializer for FileList objects (from File API).
* @public
*/
export interface IFileListTreeInitializer {
readonly fileList: FileList;
}
/**
* Tree initializer for File System Access API file handles.
* @public
*/
export interface IFileHandleTreeInitializer {
readonly prefix?: string;
readonly fileHandles: FileSystemFileHandle[];
}
/**
* Tree initializer for File System Access API directory handles.
* @public
*/
export interface IDirectoryHandleTreeInitializer {
readonly prefix?: string;
readonly dirHandles: FileSystemDirectoryHandle[];
readonly nonRecursive?: boolean; // Default: false (recursive by default)
}
/**
* Union type for all supported tree initializers.
* @public
*/
export type TreeInitializer =
| IFileListTreeInitializer
| IFileHandleTreeInitializer
| IDirectoryHandleTreeInitializer;
/**
* Interface for file metadata.
* @public
*/
export interface IFileMetadata {
path: string;
name: string;
size: number;
type: string;
lastModified: number;
}
/**
* Helper class to create FileTree instances from various file sources.
* Supports File API (FileList) and File System Access API handles.
* @public
*/
export class FileApiTreeAccessors<TCT extends string = string> {
/**
* Create FileTree from various file sources using TreeInitializer array.
* @param initializers - Array of TreeInitializer objects specifying file sources
* @param params - Optional `IFileTreeInitParams` for the file tree.
* @returns Promise resolving to a FileTree with all content pre-loaded
*/
public static async create<TCT extends string = string>(
initializers: TreeInitializer[],
params?: FileTree.IFileTreeInitParams<TCT>
): Promise<Result<FileTree.FileTree<TCT>>> {
try {
const allFiles: FileTree.IInMemoryFile<TCT>[] = [];
for (const initializer of initializers) {
const filesResult = await this._processInitializer<TCT>(initializer, params);
if (filesResult.isFailure()) {
return fail(filesResult.message);
}
allFiles.push(...filesResult.value);
}
return FileTree.inMemory<TCT>(allFiles, params);
/* c8 ignore next 5 - defense in depth */
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return fail(`Failed to process initializers: ${message}`);
}
}
/**
* Create FileTree from FileList (e.g., from input[type="file"]).
* @param fileList - FileList from a file input element
* @param params - Optional `IFileTreeInitParams` for the file tree.
* @returns Promise resolving to a FileTree with all content pre-loaded
*/
public static async fromFileList<TCT extends string = string>(
fileList: FileList,
params?: FileTree.IFileTreeInitParams<TCT>
): Promise<Result<FileTree.FileTree<TCT>>> {
const files: FileTree.IInMemoryFile<TCT>[] = [];
for (let i = 0; i < fileList.length; i++) {
const file = fileList[i];
const path = this._normalizePath(params?.prefix || '', file.name);
try {
const contents = await file.text();
const contentType = params?.inferContentType?.(path, file.type).orDefault();
files.push({
path,
contents,
contentType
});
} catch (error) {
/* c8 ignore next 1 - defense in depth */
const message = error instanceof Error ? error.message : String(error);
return fail(`Failed to read file ${file.name}: ${message}`);
}
}
return FileTree.inMemory<TCT>(files, params);
}
/**
* Create FileTree from directory upload with webkitRelativePath.
* @param fileList - FileList from a directory upload (input with webkitdirectory)
* @param params - Optional `IFileTreeInitParams` for the file tree.
* @returns Promise resolving to a FileTree with all content pre-loaded
*/
public static async fromDirectoryUpload<TCT extends string = string>(
fileList: FileList,
params?: FileTree.IFileTreeInitParams<TCT>
): Promise<Result<FileTree.FileTree<TCT>>> {
const files: FileTree.IInMemoryFile<TCT>[] = [];
for (let i = 0; i < fileList.length; i++) {
const file = fileList[i];
const relativePath = getFileRelativePath(file);
const path = this._normalizePath(params?.prefix || '', relativePath);
try {
const contents = await file.text();
const contentType = params?.inferContentType?.(path, file.type).orDefault();
files.push({
path,
contents,
contentType
});
} catch (error) {
/* c8 ignore next 1 - defense in depth */
const message = error instanceof Error ? error.message : String(error);
return fail(`Failed to read file ${relativePath}: ${message}`);
}
}
return FileTree.inMemory<TCT>(files, params);
}
/**
* Get the File object for a specific path from the original FileList.
* This is useful for accessing the original File API object for operations
* like getting file metadata, MIME type, etc.
* @param fileList - The original FileList
* @param targetPath - The path to find
* @returns Result containing the File object if found
*/
public static getOriginalFile(fileList: FileList, targetPath: string): Result<File> {
for (const file of Array.from(fileList)) {
const filePath = getFileRelativePath(file);
if (filePath === targetPath) {
return succeed(file);
}
}
return fail(`File not found: ${targetPath}`);
}
/**
* Extract file metadata from a File.
* @param fileList - The File to extract metadata from
* @returns The {@link IFileMetadata | file metadata}
*/
public static extractFileMetadata(file: File): IFileMetadata {
return {
path: getFileRelativePath(file),
name: file.name,
size: file.size,
type: file.type,
lastModified: file.lastModified
};
}
/**
* Process a single TreeInitializer and return the resulting files.
* @param initializer - The TreeInitializer to process
* @param params - Optional `IFileTreeInitParams` for the file tree.
* @returns Promise resolving to an array of `IInMemoryFile` objects
* @internal
*/
private static async _processInitializer<TCT extends string = string>(
initializer: TreeInitializer,
params?: FileTree.IFileTreeInitParams<TCT>
): Promise<Result<FileTree.IInMemoryFile<TCT>[]>> {
if ('fileList' in initializer) {
return this._processFileList<TCT>(initializer.fileList, params);
} else if ('fileHandles' in initializer) {
return this._processFileHandles<TCT>(initializer.fileHandles, params);
} else if ('dirHandles' in initializer) {
return this._processDirectoryHandles<TCT>(
initializer.dirHandles,
initializer.prefix ?? params?.prefix,
!initializer.nonRecursive,
params
);
} else {
return fail('Unknown initializer type');
}
}
/**
* Process a FileList and return IInMemoryFile array.
* @param fileList - The FileList to process
* @param params - Optional `IFileTreeInitParams` for the file tree.
* @returns Promise resolving to an array of `IInMemoryFile` objects
* @internal
*/
private static async _processFileList<TCT extends string = string>(
fileList: FileList,
params?: FileTree.IFileTreeInitParams<TCT>
): Promise<Result<FileTree.IInMemoryFile<TCT>[]>> {
const files: FileTree.IInMemoryFile<TCT>[] = [];
for (let i = 0; i < fileList.length; i++) {
const file = fileList[i];
const metadata = this.extractFileMetadata(file);
const relativePath = metadata.path;
const path = relativePath.startsWith('/') ? relativePath : `/${relativePath}`;
try {
const contents = await file.text();
const contentType = params?.inferContentType?.(path, metadata.type).orDefault();
files.push({
path,
contents,
contentType
});
} catch (error) {
/* c8 ignore next 1 - defense in depth */
const message = error instanceof Error ? error.message : String(error);
return fail(`Failed to read file ${relativePath}: ${message}`);
}
}
return succeed(files);
}
/**
* Process FileSystemFileHandles and return IInMemoryFile array.
* @internal
*/
private static async _processFileHandles<TCT extends string = string>(
handles: FileSystemFileHandle[],
params?: FileTree.IFileTreeInitParams<TCT>
): Promise<Result<FileTree.IInMemoryFile<TCT>[]>> {
const files: FileTree.IInMemoryFile<TCT>[] = [];
for (const handle of handles) {
try {
const file = await handle.getFile();
const contents = await file.text();
const metadata = this.extractFileMetadata(file);
/* c8 ignore next 1 - defense in depth */
const path = this._normalizePath(params?.prefix ?? '', handle.name);
const contentType = params?.inferContentType?.(path, metadata.type).orDefault();
files.push({
path,
contents,
contentType
});
} catch (error) {
/* c8 ignore next 1 - defense in depth */
const message = error instanceof Error ? error.message : String(error);
return fail(`Failed to read file handle ${handle.name}: ${message}`);
}
}
return succeed(files);
}
/**
* Process FileSystemDirectoryHandles and return IInMemoryFile array.
* @param handles - The FileSystemDirectoryHandle[] to process
* @param prefix - The prefix to use for the file paths
* @param recursive - Whether to process the directory recursively
* @param params - Optional `IFileTreeInitParams` for the file tree.
* @returns Promise resolving to an array of `IInMemoryFile` objects
* @internal
*/
private static async _processDirectoryHandles<TCT extends string = string>(
handles: FileSystemDirectoryHandle[],
prefix?: string,
recursive: boolean = true,
params?: FileTree.IFileTreeInitParams<TCT>
): Promise<Result<FileTree.IInMemoryFile<TCT>[]>> {
const allFiles: FileTree.IInMemoryFile<TCT>[] = [];
prefix = prefix ?? params?.prefix;
for (const handle of handles) {
const dirPrefix = this._normalizePath(prefix ?? '', handle.name);
const filesResult = await this._processDirectoryHandle<TCT>(handle, dirPrefix, recursive, params);
if (filesResult.isFailure()) {
return filesResult;
}
allFiles.push(...filesResult.value);
}
return succeed(allFiles);
}
/**
* Process a single FileSystemDirectoryHandle recursively.
* @param handle - The FileSystemDirectoryHandle to process
* @param basePath - The base path to use for the file paths
* @param recursive - Whether to process the directory recursively
* @param params - Optional `IFileTreeInitParams` for the file tree.
* @returns Promise resolving to an array of `IInMemoryFile` objects
* @internal
*/
private static async _processDirectoryHandle<TCT extends string = string>(
handle: FileSystemDirectoryHandle,
basePath: string,
recursive: boolean,
params?: FileTree.IFileTreeInitParams<TCT>
): Promise<Result<FileTree.IInMemoryFile<TCT>[]>> {
const files: FileTree.IInMemoryFile<TCT>[] = [];
try {
for await (const entry of handle.values()) {
if (isFileHandle(entry)) {
const file = await entry.getFile();
const contents = await file.text();
const path = this._normalizePath(basePath, entry.name);
const contentType = params?.inferContentType?.(path).orDefault();
files.push({
path,
contents,
contentType
});
} else if (isDirectoryHandle(entry) && recursive) {
const subDirPath = this._normalizePath(basePath, entry.name);
const subdirResult = await this._processDirectoryHandle<TCT>(entry, subDirPath, recursive, params);
if (subdirResult.isFailure()) {
return subdirResult;
}
files.push(...subdirResult.value);
}
}
return succeed(files);
} catch (error) {
/* c8 ignore next 1 - defense in depth */
const message = error instanceof Error ? error.message : String(error);
return fail(`Failed to process directory ${handle.name}: ${message}`);
}
}
private static _normalizePath(prefix: string, path: string): string {
// Remove leading slash from prefix if it exists to avoid double slashes
const cleanPrefix = prefix ? prefix.replace(/^\/+/, '') : '';
const combined = cleanPrefix ? `${cleanPrefix}/${path}` : path;
// Normalize multiple slashes and ensure it starts with / for FileTree compatibility
const normalized = combined.replace(/\/+/g, '/').replace(/\/$/, '');
/* c8 ignore next 1 - tested but coverage intermittently missed */
return normalized.startsWith('/') ? normalized : `/${normalized}`;
}
}
|