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 | 4x 4x 4x 4x 4x 4x 66x 66x 66x 66x 66x 8x 8x 8x 8x 25x 9x 5x 5x 5x 5x 5x 14x 4x 4x 4x 2x 2x 2x 1x 17x 17x 17x 17x 17x 7x 7x 7x 10x 10x 10x 10x 10x 6x 5x 5x 5x 4x 35x 35x 35x 35x 35x 27x 27x 27x 8x 8x 8x 8x 8x 5x 4x 4x 4x 3x 6x 5x 5x 5x 5x 5x 5x 2x 3x 3x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x | import { CoreFileSystemHandle } from './CoreFileSystemHandle'; import { assertCanWrite, assertName, basename, ctx as createCtx, newNotAllowedError, newNotFoundError, newTypeMismatchError, } from './util'; import { CoreFileSystemFileHandle } from './CoreFileSystemFileHandle'; import type { CoreFsaContext, GetDirectoryHandleOptions, GetFileHandleOptions, IFileSystemDirectoryHandle, IFileSystemFileHandle, IFileSystemHandle, RemoveEntryOptions, } from './types'; import type { Superblock } from '../core/Superblock'; import { ERROR_CODE } from '../core/constants'; import { filenameToSteps } from '../core/util'; import { Buffer } from '../internal/buffer'; import { MODE, FLAGS } from '../node/constants'; /** * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle */ export class CoreFileSystemDirectoryHandle extends CoreFileSystemHandle implements IFileSystemDirectoryHandle { protected readonly ctx: CoreFsaContext; /** Directory path with trailing slash. */ public readonly __path: string; public constructor( protected readonly _core: Superblock, path: string, ctx: Partial<CoreFsaContext> = {}, ) { const fullCtx = createCtx(ctx); super('directory', basename(path, fullCtx.separator), fullCtx); this.ctx = fullCtx; 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 CoreFileSystemDirectoryHandle} object. * * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/keys */ public async *keys(): AsyncIterableIterator<string> { try { const link = this._core.getResolvedLinkOrThrow(this.__path); const children = link.children; for (const [name] of children) { if (name !== '.' && name !== '..') { yield name; } } } catch (error) { this._handleError(error, 'keys'); } } /** * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/entries */ public async *entries(): AsyncIterableIterator<[string, CoreFileSystemHandle]> { const { __path: path, _core, ctx } = this; try { const link = _core.getResolvedLinkOrThrow(path); const children = link.children; for (const [name, childLink] of children) { if (name !== '.' && name !== '..' && childLink) { const childPath = path + name; const node = childLink.getNode(); if (node.isDirectory()) { yield [name, new CoreFileSystemDirectoryHandle(_core, childPath, ctx)]; } else if (node.isFile()) { yield [name, new CoreFileSystemFileHandle(_core, childPath, ctx)]; } } } } catch (error) { this._handleError(error, 'entries'); } } /** * 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<CoreFileSystemHandle> { for await (const [, value] of this.entries()) yield value; } /** * Returns a {@link CoreFileSystemDirectoryHandle} 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 CoreFileSystemHandle} 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 link = this._core.getResolvedLink(filename); if (link) { const node = link.getNode(); Iif (!node.isDirectory()) throw newTypeMismatchError(); return new CoreFileSystemDirectoryHandle(this._core, filename, this.ctx); } else { throw new Error('ENOENT'); // Simulate error for consistency with catch block } } catch (error) { Iif (error instanceof DOMException) throw error; if (error && typeof error === 'object') { if (error.code === ERROR_CODE.ENOENT || error.message === 'ENOENT') { if (options?.create) { assertCanWrite(this.ctx.mode!); try { this._core.mkdir(filename, 0o755); return new CoreFileSystemDirectoryHandle(this._core, filename, this.ctx); } catch (createError) { Iif (createError && typeof createError === 'object' && createError.code === ERROR_CODE.EACCES) { throw newNotAllowedError(); } throw createError; } } throw newNotFoundError(); } Iif (error.code === ERROR_CODE.EACCES) { throw newNotAllowedError(); } } throw error; } } /** * Returns a {@link CoreFileSystemFileHandle} 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 CoreFileSystemHandle} 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 link = this._core.getResolvedLink(filename); if (link) { const node = link.getNode(); Iif (!node.isFile()) throw newTypeMismatchError(); return new CoreFileSystemFileHandle(this._core, filename, this.ctx); } else { throw new Error('ENOENT'); // Simulate error for consistency with catch block } } catch (error) { Iif (error instanceof DOMException) throw error; if (error && typeof error === 'object') { if (error.code === ERROR_CODE.ENOENT || error.message === 'ENOENT') { if (options?.create) { assertCanWrite(this.ctx.mode!); try { this._core.writeFile(filename, Buffer.alloc(0), FLAGS.w, MODE.FILE); return new CoreFileSystemFileHandle(this._core, filename, this.ctx); } catch (createError) { Iif (createError && typeof createError === 'object' && createError.code === ERROR_CODE.EACCES) { throw newNotAllowedError(); } throw createError; } } throw newNotFoundError(); } Iif (error.code === ERROR_CODE.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 CoreFileSystemHandle} 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; try { const link = this._core.getResolvedLinkOrThrow(filename); const node = link.getNode(); if (node.isFile()) { this._core.unlink(filename); } else if (node.isDirectory()) { this._core.rmdir(filename, recursive); } else E{ throw newTypeMismatchError(); } } catch (error) { Iif (error instanceof DOMException) throw error; Iif (error && typeof error === 'object') { switch (error.code) { case ERROR_CODE.ENOENT: { throw newNotFoundError(); } case ERROR_CODE.EACCES: throw newNotAllowedError(); case ERROR_CODE.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 CoreFileSystemHandle} from which * to return the relative path. */ public async resolve(possibleDescendant: IFileSystemHandle): Promise<string[] | null> { if ( possibleDescendant instanceof CoreFileSystemDirectoryHandle || possibleDescendant instanceof CoreFileSystemFileHandle ) { // First check if they are from the same core instance if ((possibleDescendant as any)._core !== this._core) return null; const path = this.__path; const childPath = possibleDescendant.__path; Iif (!childPath.startsWith(path)) return null; let relative = childPath.slice(path.length); Iif (relative === '') return []; const separator = this.ctx.separator!; Iif (relative[0] === separator) relative = relative.slice(1); return relative.split(separator); } return null; } private _handleError(error: any, method: string): never { Iif (error instanceof DOMException) throw error; Iif (error && typeof error === 'object') { switch (error.code) { case ERROR_CODE.ENOENT: throw newNotFoundError(); case ERROR_CODE.EACCES: throw newNotAllowedError(); } } throw error; } } |