From df68e69724ffd90b79c8edd9013ba0bf34c97b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 23 Sep 2021 14:48:47 +0200 Subject: [PATCH] permission stuff --- src/FileSystemHandle.js | 34 ++++++++++++++++++++++++---------- src/adapters/node.js | 9 ++++----- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/FileSystemHandle.js b/src/FileSystemHandle.js index 01d1cfa..2bab58b 100644 --- a/src/FileSystemHandle.js +++ b/src/FileSystemHandle.js @@ -16,20 +16,34 @@ class FileSystemHandle { this[kAdapter] = adapter } - async queryPermission (options = {}) { - if (options.readable) return 'granted' + async queryPermission ({mode = 'read'} = {}) { const handle = this[kAdapter] - return handle.queryPermission ? - await handle.queryPermission(options) : - handle.writable - ? 'granted' - : 'denied' + if (handle.queryPermission) { + return handle.queryPermission({mode}) + } + + if (mode === 'read') { + return 'granted' + } else if (mode === 'readwrite') { + return handle.writable ? 'granted' : 'denied' + } else { + throw new TypeError(`Mode ${mode} must be 'read' or 'readwrite'`) + } } - async requestPermission (options = {}) { - if (options.readable) return 'granted' + async requestPermission ({mode = 'read'} = {}) { const handle = this[kAdapter] - return handle.writable ? 'granted' : 'denied' + if (handle.requestPermission) { + return handle.requestPermission({mode}) + } + + if (mode === 'read') { + return 'granted' + } else if (mode === 'readwrite') { + return handle.writable ? 'granted' : 'denied' + } else { + throw new TypeError(`Mode ${mode} must be 'read' or 'readwrite'`) + } } /** diff --git a/src/adapters/node.js b/src/adapters/node.js index b9fc716..d651fd7 100644 --- a/src/adapters/node.js +++ b/src/adapters/node.js @@ -1,10 +1,9 @@ -import fs from 'fs/promises' -import { errors } from '../util.js' -import { join } from 'path' +import fs from 'node:fs/promises' +import { join } from 'node:path' +import 'node-domexception' import Blob from 'fetch-blob' import { fileFrom } from 'fetch-blob/from.js' -import DOMException from 'node-domexception' - +import { errors } from '../util.js' // import mime from 'mime-types' const { INVALID, GONE, MISMATCH, MOD_ERR, SYNTAX } = errors