From 17015a31559096e83100f01523a5d9cf005b0ddb Mon Sep 17 00:00:00 2001 From: streamich Date: Wed, 14 Jun 2023 21:10:45 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20implement=20.getFile()?= =?UTF-8?q?=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/node-to-fsa/NodeFileSystemFileHandle.ts | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/node-to-fsa/NodeFileSystemFileHandle.ts b/src/node-to-fsa/NodeFileSystemFileHandle.ts index fd6688c78..186f6f51f 100644 --- a/src/node-to-fsa/NodeFileSystemFileHandle.ts +++ b/src/node-to-fsa/NodeFileSystemFileHandle.ts @@ -1,6 +1,6 @@ import { NodeFileSystemHandle } from './NodeFileSystemHandle'; import { NodeFileSystemSyncAccessHandle } from './NodeFileSystemSyncAccessHandle'; -import { basename, ctx as createCtx } from './util'; +import { basename, ctx as createCtx, newNotAllowedError } from './util'; import type { NodeFsaContext, NodeFsaFs } from './types'; export class NodeFileSystemFileHandle extends NodeFileSystemHandle { @@ -13,10 +13,30 @@ export class NodeFileSystemFileHandle extends NodeFileSystemHandle { } /** + * Returns a {@link Promise} which resolves to a {@link File} object + * representing the state on disk of the entry represented by the handle. + * * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/getFile */ public async getFile(): Promise { - throw new Error('Not implemented'); + try { + const path = this.__path; + const promises = this.fs.promises; + const stats = await promises.stat(path); + const data = await promises.readFile(path); + const file = new File([data], this.name, {lastModified: stats.mtime.getTime()}); + return file; + } catch (error) { + if (error instanceof DOMException) throw error; + if (error && typeof error === 'object') { + switch (error.code) { + case 'EPERM': + case 'EACCES': + throw newNotAllowedError(); + } + } + throw error; + } } /**