|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import { join, basename } from 'https://deno.land/[email protected]/path/mod.ts' |
| 4 | +import { errors } from '../util.js' |
| 5 | + |
| 6 | +const { INVALID, GONE, MISMATCH, MOD_ERR, SYNTAX } = errors |
| 7 | + |
| 8 | +// TODO: |
| 9 | +// - either depend on fetch-blob. |
| 10 | +// - push for https://github.com/denoland/deno/pull/10969 |
| 11 | +// - or extend the File class like i did in that PR |
| 12 | +/** @param {string} path */ |
| 13 | +async function fileFrom (path) { |
| 14 | + const e = Deno.readFileSync(path) |
| 15 | + const s = await Deno.stat(path) |
| 16 | + return new File([e], basename(path), { lastModified: Number(s.mtime) }) |
| 17 | +} |
| 18 | + |
| 19 | +export class Sink { |
| 20 | + /** |
| 21 | + * @param {Deno.File} fileHandle |
| 22 | + * @param {number} size |
| 23 | + */ |
| 24 | + constructor (fileHandle, size) { |
| 25 | + this.fileHandle = fileHandle |
| 26 | + this.size = size |
| 27 | + this.position = 0 |
| 28 | + } |
| 29 | + async abort() { |
| 30 | + await this.fileHandle.close() |
| 31 | + } |
| 32 | + async write (chunk) { |
| 33 | + if (typeof chunk === 'object') { |
| 34 | + if (chunk.type === 'write') { |
| 35 | + if (Number.isInteger(chunk.position) && chunk.position >= 0) { |
| 36 | + this.position = chunk.position |
| 37 | + } |
| 38 | + if (!('data' in chunk)) { |
| 39 | + await this.fileHandle.close() |
| 40 | + throw new DOMException(...SYNTAX('write requires a data argument')) |
| 41 | + } |
| 42 | + chunk = chunk.data |
| 43 | + } else if (chunk.type === 'seek') { |
| 44 | + if (Number.isInteger(chunk.position) && chunk.position >= 0) { |
| 45 | + if (this.size < chunk.position) { |
| 46 | + throw new DOMException(...INVALID) |
| 47 | + } |
| 48 | + this.position = chunk.position |
| 49 | + return |
| 50 | + } else { |
| 51 | + await this.fileHandle.close() |
| 52 | + throw new DOMException(...SYNTAX('seek requires a position argument')) |
| 53 | + } |
| 54 | + } else if (chunk.type === 'truncate') { |
| 55 | + if (Number.isInteger(chunk.size) && chunk.size >= 0) { |
| 56 | + await this.fileHandle.truncate(chunk.size) |
| 57 | + this.size = chunk.size |
| 58 | + if (this.position > this.size) { |
| 59 | + this.position = this.size |
| 60 | + } |
| 61 | + return |
| 62 | + } else { |
| 63 | + await this.fileHandle.close() |
| 64 | + throw new DOMException(...SYNTAX('truncate requires a size argument')) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (chunk instanceof ArrayBuffer) { |
| 70 | + chunk = new Uint8Array(chunk) |
| 71 | + } else if (typeof chunk === 'string') { |
| 72 | + chunk = new TextEncoder().encode(chunk) |
| 73 | + } else if (chunk instanceof Blob) { |
| 74 | + await this.fileHandle.seek(this.position, Deno.SeekMode.Start) |
| 75 | + for await (const data of chunk.stream()) { |
| 76 | + const written = await this.fileHandle.write(data) |
| 77 | + this.position += written |
| 78 | + this.size += written |
| 79 | + } |
| 80 | + return |
| 81 | + } |
| 82 | + await this.fileHandle.seek(this.position, Deno.SeekMode.Start) |
| 83 | + const written = await this.fileHandle.write(chunk) |
| 84 | + this.position += written |
| 85 | + this.size += written |
| 86 | + } |
| 87 | + |
| 88 | + async close () { |
| 89 | + await this.fileHandle.close() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export class FileHandle { |
| 94 | + #path |
| 95 | + |
| 96 | + /** |
| 97 | + * @param {string} path |
| 98 | + * @param {string} name |
| 99 | + */ |
| 100 | + constructor (path, name) { |
| 101 | + this.#path = path |
| 102 | + this.name = name |
| 103 | + this.kind = 'file' |
| 104 | + } |
| 105 | + |
| 106 | + async getFile () { |
| 107 | + await Deno.stat(this.#path).catch(err => { |
| 108 | + if (err.name === 'NotFound') throw new DOMException(...GONE) |
| 109 | + }) |
| 110 | + return fileFrom(this.#path) |
| 111 | + } |
| 112 | + |
| 113 | + isSameEntry (other) { |
| 114 | + return this.#path === this.#getPath.apply(other) |
| 115 | + } |
| 116 | + |
| 117 | + #getPath() { |
| 118 | + return this.#path |
| 119 | + } |
| 120 | + |
| 121 | + async createWritable () { |
| 122 | + const fileHandle = await Deno.open(this.#path, {write: true}).catch(err => { |
| 123 | + if (err.name === 'NotFound') throw new DOMException(...GONE) |
| 124 | + throw err |
| 125 | + }) |
| 126 | + const { size } = await fileHandle.stat() |
| 127 | + return new Sink(fileHandle, size) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export class FolderHandle { |
| 132 | + #path = '' |
| 133 | + |
| 134 | + /** @param {string} path */ |
| 135 | + constructor (path, name = '') { |
| 136 | + this.name = name |
| 137 | + this.kind = 'directory' |
| 138 | + this.#path = join(path) |
| 139 | + } |
| 140 | + |
| 141 | + isSameEntry (other) { |
| 142 | + return this.#path === this.#getPath.apply(other) |
| 143 | + } |
| 144 | + |
| 145 | + #getPath() { |
| 146 | + return this.#path |
| 147 | + } |
| 148 | + |
| 149 | + async * entries () { |
| 150 | + const dir = this.#path |
| 151 | + try { |
| 152 | + for await (const dirEntry of Deno.readDir(dir)) { |
| 153 | + const { name } = dirEntry |
| 154 | + const path = join(dir, name) |
| 155 | + const stat = await Deno.lstat(path) |
| 156 | + if (stat.isFile) { |
| 157 | + yield [name, new FileHandle(path, name)] |
| 158 | + } else if (stat.isDirectory) { |
| 159 | + yield [name, new FolderHandle(path, name)] |
| 160 | + } |
| 161 | + } |
| 162 | + } catch (err) { |
| 163 | + throw err.name === 'NotFound' ? new DOMException(...GONE) : err |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @param {string} name |
| 169 | + * @param {{create?: boolean}} opts |
| 170 | + */ |
| 171 | + async getDirectoryHandle (name, opts = {}) { |
| 172 | + const path = join(this.#path, name) |
| 173 | + const stat = await Deno.lstat(path).catch(err => { |
| 174 | + if (err.name !== 'NotFound') throw err |
| 175 | + }) |
| 176 | + const isDirectory = stat?.isDirectory |
| 177 | + if (stat && isDirectory) return new FolderHandle(path, name) |
| 178 | + if (stat && !isDirectory) throw new DOMException(...MISMATCH) |
| 179 | + if (!opts.create) throw new DOMException(...GONE) |
| 180 | + await Deno.mkdir(path) |
| 181 | + return new FolderHandle(path, name) |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @param {string} name |
| 186 | + * @param {{ create: any; }} opts |
| 187 | + */ |
| 188 | + async getFileHandle (name, opts) { |
| 189 | + const path = join(this.#path, name) |
| 190 | + const stat = await Deno.lstat(path).catch(err => { |
| 191 | + if (err.name !== 'NotFound') throw err |
| 192 | + }) |
| 193 | + |
| 194 | + const isFile = stat?.isFile |
| 195 | + if (stat && isFile) return new FileHandle(path, name) |
| 196 | + if (stat && !isFile) throw new DOMException(...MISMATCH) |
| 197 | + if (!opts.create) throw new DOMException(...GONE) |
| 198 | + const c = await Deno.open(path, { |
| 199 | + create: true, |
| 200 | + write: true, |
| 201 | + }) |
| 202 | + c.close() |
| 203 | + return new FileHandle(path, name) |
| 204 | + } |
| 205 | + |
| 206 | + queryPermission () { |
| 207 | + return 'granted' |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * @param {string} name |
| 212 | + * @param {{ recursive: boolean; }} opts |
| 213 | + */ |
| 214 | + async removeEntry (name, opts) { |
| 215 | + const path = join(this.#path, name) |
| 216 | + const stat = await Deno.lstat(path).catch(err => { |
| 217 | + if (err.name === 'NotFound') throw new DOMException(...GONE) |
| 218 | + throw err |
| 219 | + }) |
| 220 | + |
| 221 | + if (stat.isDirectory) { |
| 222 | + if (opts.recursive) { |
| 223 | + await Deno.remove(path, { recursive: true }).catch(err => { |
| 224 | + if (err.code === 'ENOTEMPTY') throw new DOMException(...MOD_ERR) |
| 225 | + throw err |
| 226 | + }) |
| 227 | + } else { |
| 228 | + await Deno.remove(path).catch(() => { |
| 229 | + throw new DOMException(...MOD_ERR) |
| 230 | + }) |
| 231 | + } |
| 232 | + } else { |
| 233 | + await Deno.remove(path) |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +export default path => new FolderHandle(join(Deno.cwd(), path)) |
0 commit comments