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 | 3x 3x 3x 3x 164x 164x 164x 164x 164x 164x 164x 122x 122x 122x 122x 78x 78x 44x 44x 44x 164x 200x 196x 192x 192x 192x 192x 192x 8x 8x 4x 4x 8x 8x 4x 4x 176x 164x 36x 32x 28x 22x 22x 22x 22x 14x 8x 8x 8x 164x 34x 30x 26x 26x 14x 14x 20x 10x 10x 4x 6x 164x 44x 40x 24x 20x 14x 14x 8x 8x 6x 6x 6x 16x 12x 12x 12x 12x 164x 20x 16x 16x 8x 8x 4x 12x 4x 8x 164x 24x 16x 8x 56x 24x 16x 8x 8x 164x 12x 36x 4x 164x | import { assertName } from '../node-to-fsa/util'; import { assertType } from '../crud/util'; import { FLAG } from '../consts/FLAG'; import { newExistsError, newFile404Error, newFolder404Error, newMissingError } from '../fsa-to-crud/util'; import type { FsPromisesApi } from '../node/types'; import type * as crud from '../crud/types'; import type { IDirent } from '../node/types/misc'; export interface NodeCrudOptions { readonly fs: FsPromisesApi; readonly dir: string; readonly separator?: string; } export class NodeCrud implements crud.CrudApi { protected readonly fs: FsPromisesApi; protected readonly dir: string; protected readonly separator: string; public constructor(protected readonly options: NodeCrudOptions) { this.separator = options.separator ?? '/'; let dir = options.dir; const last = dir[dir.length - 1]; if (last !== this.separator) dir = dir + this.separator; this.dir = dir; this.fs = options.fs; } protected async checkDir(collection: crud.CrudCollection): Promise<string> { const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : ''); const fs = this.fs; try { const stats = await fs.stat(dir); Iif (!stats.isDirectory()) throw newFolder404Error(collection); return dir; } catch (error) { if (error && typeof error === 'object') { switch (error.code) { case 'ENOENT': case 'ENOTDIR': throw newFolder404Error(collection); } } throw error; } } public readonly put = async ( collection: crud.CrudCollection, id: string, data: Uint8Array, options?: crud.CrudPutOptions, ): Promise<void> => { assertType(collection, 'put', 'crudfs'); assertName(id, 'put', 'crudfs'); const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : ''); const fs = this.fs; if (dir.length > 1) await fs.mkdir(dir, { recursive: true }); const filename = dir + id; switch (options?.throwIf) { case 'exists': { try { await fs.writeFile(filename, data, { flag: FLAG.O_CREAT | FLAG.O_EXCL }); } catch (error) { if (error && typeof error === 'object' && error.code === 'EEXIST') throw newExistsError(); throw error; } break; } case 'missing': { try { await fs.writeFile(filename, data, { flag: FLAG.O_RDWR }); } catch (error) { if (error && typeof error === 'object' && error.code === 'ENOENT') throw newMissingError(); throw error; } break; } default: { await fs.writeFile(filename, data); } } }; public readonly get = async (collection: crud.CrudCollection, id: string): Promise<Uint8Array> => { assertType(collection, 'get', 'crudfs'); assertName(id, 'get', 'crudfs'); const dir = await this.checkDir(collection); const filename = dir + id; const fs = this.fs; try { const buf = (await fs.readFile(filename)) as Buffer; return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength); } catch (error) { if (error && typeof error === 'object') { switch (error.code) { case 'ENOENT': throw newFile404Error(collection, id); } } throw error; } }; public readonly del = async (collection: crud.CrudCollection, id: string, silent?: boolean): Promise<void> => { assertType(collection, 'del', 'crudfs'); assertName(id, 'del', 'crudfs'); try { const dir = await this.checkDir(collection); const filename = dir + id; await this.fs.unlink(filename); } catch (error) { if (!!silent) return; if (error && typeof error === 'object') { switch (error.code) { case 'ENOENT': throw newFile404Error(collection, id); } } throw error; } }; public readonly info = async (collection: crud.CrudCollection, id?: string): Promise<crud.CrudResourceInfo> => { assertType(collection, 'info', 'crudfs'); if (id) { assertName(id, 'info', 'crudfs'); await this.checkDir(collection); try { const stats = await this.fs.stat(this.dir + collection.join(this.separator) + this.separator + id); Iif (!stats.isFile()) throw newFile404Error(collection, id); return { type: 'resource', id, size: <number>stats.size, modified: <number>stats.mtimeMs, }; } catch (error) { if (error && typeof error === 'object') { switch (error.code) { case 'ENOENT': throw newFile404Error(collection, id); } } throw error; } } else { await this.checkDir(collection); try { const stats = await this.fs.stat(this.dir + collection.join(this.separator)); Iif (!stats.isDirectory()) throw newFolder404Error(collection); return { type: 'collection', id: '', }; } catch (error) { Iif (error && typeof error === 'object') { switch (error.code) { case 'ENOENT': case 'ENOTDIR': throw newFolder404Error(collection); } } throw error; } } }; public readonly drop = async (collection: crud.CrudCollection, silent?: boolean): Promise<void> => { assertType(collection, 'drop', 'crudfs'); try { const dir = await this.checkDir(collection); const isRoot = dir === this.dir; if (isRoot) { const list = (await this.fs.readdir(dir)) as string[]; for (const entry of list) await this.fs.rmdir(dir + entry, { recursive: true }); } else { await this.fs.rmdir(dir, { recursive: true }); } } catch (error) { if (!silent) throw error; } }; public readonly scan = async function* ( collection: crud.CrudCollection, ): AsyncIterableIterator<crud.CrudCollectionEntry> { assertType(collection, 'scan', 'crudfs'); const dir = await this.checkDir(collection); const dirents = (await this.fs.readdir(dir, { withFileTypes: true })) as IDirent[]; for await (const entry of dirents) { if (entry.isFile()) { yield { type: 'resource', id: '' + entry.name, }; } else if (entry.isDirectory()) { yield { type: 'collection', id: '' + entry.name, }; } } }; public readonly list = async (collection: crud.CrudCollection): Promise<crud.CrudCollectionEntry[]> => { const entries: crud.CrudCollectionEntry[] = []; for await (const entry of this.scan(collection)) entries.push(entry); return entries; }; public readonly from = async (collection: crud.CrudCollection): Promise<crud.CrudApi> => { assertType(collection, 'from', 'crudfs'); const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : ''); const fs = this.fs; Iif (dir.length > 1) await fs.mkdir(dir, { recursive: true }); await this.checkDir(collection); return new NodeCrud({ dir, fs: this.fs, separator: this.separator, }); }; } |