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 | 10x 10x 10x 10x 5178x 5178x 5178x 5178x 17x 41x 29x 29x 29x 71x 71x 71x 71x 45x 12x 4648x 4633x 4633x 4633x 4183x 4180x 453x 450x 450x 450x 405x 404x 404x 45x 3281x 3265x 3265x 3265x 1752x 1005x 2260x 1513x 1513x 1513x 691x 689x 689x 822x 374x 371x 355x 355x 355x 355x 349x 295x 54x 54x 10x 10x 10x 6x 4x 5x 5x 5x 5x 4x 4x 3x 3x 3x | import { NodeFileSystemHandle } from './NodeFileSystemHandle'; import { assertCanWrite, assertName, basename, ctx as createCtx, newNotAllowedError, newNotFoundError, newTypeMismatchError, } from './util'; import { NodeFileSystemFileHandle } from './NodeFileSystemFileHandle'; import type { NodeFsaContext, NodeFsaFs } from './types'; import type Dirent from '../Dirent'; import type { GetDirectoryHandleOptions, GetFileHandleOptions, IFileSystemDirectoryHandle, IFileSystemFileHandle, RemoveEntryOptions, } from '../fsa/types'; /** * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle */ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle implements IFileSystemDirectoryHandle { protected readonly ctx: Partial<NodeFsaContext>; /** Directory path with trailing slash. */ public readonly __path: string; public constructor( protected readonly fs: NodeFsaFs, path: string, ctx: Partial<NodeFsaContext> = {}, ) { super('directory', basename(path, ctx.separator || '/')); this.ctx = createCtx(ctx); this.__path = path[path.length - 1] === this.ctx.separator ? path : path + this.ctx.separator; } /** * Returns a new array iterator containing the keys for each item in * {@link NodeFileSystemDirectoryHandle} object. * * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/keys */ public async *keys(): AsyncIterableIterator<string> { const list = await this.fs.promises.readdir(this.__path); for (const name of list) yield '' + name; } /** * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/entries */ public async *entries(): AsyncIterableIterator<[string, NodeFileSystemHandle]> { const { __path: path, fs, ctx } = this; const list = await fs.promises.readdir(path, { withFileTypes: true }); for (const d of list) { const dirent = d as Dirent; const name = dirent.name + ''; const newPath = path + name; if (dirent.isDirectory()) yield [name, new NodeFileSystemDirectoryHandle(fs, newPath, ctx)]; else if (dirent.isFile()) yield [name, new NodeFileSystemFileHandle(fs, newPath, ctx)]; } } /** * Returns a new array iterator containing the values for each index in the * {@link FileSystemDirectoryHandle} object. * * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/values */ public async *values(): AsyncIterableIterator<NodeFileSystemHandle> { for await (const [, value] of this.entries()) yield value; } /** * Returns a {@link NodeFileSystemDirectoryHandle} for a subdirectory with the specified * name within the directory handle on which the method is called. * * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getDirectoryHandle * @param name A string representing the {@link NodeFileSystemHandle} name of * the subdirectory you wish to retrieve. * @param options An optional object containing options for the retrieved * subdirectory. */ public async getDirectoryHandle( name: string, options?: GetDirectoryHandleOptions, ): Promise<IFileSystemDirectoryHandle> { assertName(name, 'getDirectoryHandle', 'FileSystemDirectoryHandle'); const filename = this.__path + name; try { const stats = await this.fs.promises.stat(filename); if (!stats.isDirectory()) throw newTypeMismatchError(); return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx); } catch (error) { if (error instanceof DOMException) throw error; if (error && typeof error === 'object') { switch (error.code) { case 'ENOENT': { if (options?.create) { assertCanWrite(this.ctx.mode!); await this.fs.promises.mkdir(filename); return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx); } throw newNotFoundError(); } case 'EPERM': case 'EACCES': throw newNotAllowedError(); } } throw error; } } /** * Returns a {@link FileSystemFileHandle} for a file with the specified name, * within the directory the method is called. * * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getFileHandle * @param name A string representing the {@link NodeFileSystemHandle} name of * the file you wish to retrieve. * @param options An optional object containing options for the retrieved file. */ public async getFileHandle(name: string, options?: GetFileHandleOptions): Promise<IFileSystemFileHandle> { assertName(name, 'getFileHandle', 'FileSystemDirectoryHandle'); const filename = this.__path + name; try { const stats = await this.fs.promises.stat(filename); if (!stats.isFile()) throw newTypeMismatchError(); return new NodeFileSystemFileHandle(this.fs, filename, this.ctx); } catch (error) { if (error instanceof DOMException) throw error; if (error && typeof error === 'object') { switch (error.code) { case 'ENOENT': { if (options?.create) { assertCanWrite(this.ctx.mode!); await this.fs.promises.writeFile(filename, ''); return new NodeFileSystemFileHandle(this.fs, filename, this.ctx); } throw newNotFoundError(); } case 'EPERM': case 'EACCES': throw newNotAllowedError(); } } throw error; } } /** * Attempts to remove an entry if the directory handle contains a file or * directory called the name specified. * * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/removeEntry * @param name A string representing the {@link FileSystemHandle} name of the * entry you wish to remove. * @param options An optional object containing options. */ public async removeEntry(name: string, { recursive = false }: RemoveEntryOptions = {}): Promise<void> { assertCanWrite(this.ctx.mode!); assertName(name, 'removeEntry', 'FileSystemDirectoryHandle'); const filename = this.__path + name; const promises = this.fs.promises; try { const stats = await promises.stat(filename); if (stats.isFile()) { await promises.unlink(filename); } else if (stats.isDirectory()) { await promises.rmdir(filename, { recursive }); } else Ethrow newTypeMismatchError(); } catch (error) { Iif (error instanceof DOMException) throw error; if (error && typeof error === 'object') { switch (error.code) { case 'ENOENT': { throw newNotFoundError(); } case 'EPERM': case 'EACCES': throw newNotAllowedError(); case 'ENOTEMPTY': throw new DOMException('The object can not be modified in this way.', 'InvalidModificationError'); } } throw error; } } /** * The `resolve()` method of the {@link FileSystemDirectoryHandle} interface * returns an {@link Array} of directory names from the parent handle to the specified * child entry, with the name of the child entry as the last array item. * * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/resolve * @param possibleDescendant The {@link NodeFileSystemFileHandle} from which * to return the relative path. */ public async resolve(possibleDescendant: NodeFileSystemHandle): Promise<string[] | null> { if ( possibleDescendant instanceof NodeFileSystemDirectoryHandle || possibleDescendant instanceof NodeFileSystemFileHandle ) { const path = this.__path; const childPath = possibleDescendant.__path; if (!childPath.startsWith(path)) return null; let relative = childPath.slice(path.length); if (relative === '') return []; const separator = this.ctx.separator!; Iif (relative[0] === separator) relative = relative.slice(1); return relative.split(separator); } return null; } } |