diff --git a/package.json b/package.json index 94de166..a94a4e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "native-file-system-adapter", - "version": "1.0.0", + "version": "1.0.1", "description": "Native File System API", "main": "src/es6.js", "module": "./src/es6.js", diff --git a/src/FileSystemDirectoryHandle.js b/src/FileSystemDirectoryHandle.js index caeca06..d84de91 100644 --- a/src/FileSystemDirectoryHandle.js +++ b/src/FileSystemDirectoryHandle.js @@ -1,13 +1,15 @@ import FileSystemHandle from './FileSystemHandle.js' import FileSystemFileHandle from './FileSystemFileHandle.js' +const kAdapter = Symbol('adapter') + class FileSystemDirectoryHandle extends FileSystemHandle { /** @type {FileSystemDirectoryHandle} */ - #adapter + [kAdapter] constructor (adapter) { super(adapter) - this.#adapter = adapter + this[kAdapter] = adapter } /** @@ -19,19 +21,19 @@ class FileSystemDirectoryHandle extends FileSystemHandle { async getDirectoryHandle (name, options = {}) { if (name === '') throw new TypeError(`Name can't be an empty string.`) if (name === '.' || name === '..' || name.includes('/')) throw new TypeError(`Name contains invalid characters.`) - return new FileSystemDirectoryHandle(await this.#adapter.getDirectoryHandle(name, options)) + return new FileSystemDirectoryHandle(await this[kAdapter].getDirectoryHandle(name, options)) } /** @returns {AsyncGenerator<[string, FileSystemHandle], void, unknown>} */ async * entries () { - for await (const [_, entry] of this.#adapter.entries()) + for await (const [_, entry] of this[kAdapter].entries()) yield [entry.name, entry.kind === 'file' ? new FileSystemFileHandle(entry) : new FileSystemDirectoryHandle(entry)] } /** @deprecated use .entries() instead */ async * getEntries() { console.warn('deprecated, use .entries() instead') - for await (let entry of this.#adapter.entries()) + for await (let entry of this[kAdapter].entries()) yield entry.kind === 'file' ? new FileSystemFileHandle(entry) : new FileSystemDirectoryHandle(entry) } @@ -45,7 +47,7 @@ class FileSystemDirectoryHandle extends FileSystemHandle { if (name === '') throw new TypeError(`Name can't be an empty string.`) if (name === '.' || name === '..' || name.includes('/')) throw new TypeError(`Name contains invalid characters.`) options.create = !!options.create - return new FileSystemFileHandle(await this.#adapter.getFileHandle(name, options)) + return new FileSystemFileHandle(await this[kAdapter].getFileHandle(name, options)) } /** @@ -57,7 +59,7 @@ class FileSystemDirectoryHandle extends FileSystemHandle { if (name === '') throw new TypeError(`Name can't be an empty string.`) if (name === '.' || name === '..' || name.includes('/')) throw new TypeError(`Name contains invalid characters.`) options.recursive = !!options.recursive // cuz node's fs.rm require boolean - return this.#adapter.removeEntry(name, options) + return this[kAdapter].removeEntry(name, options) } [Symbol.asyncIterator]() { diff --git a/src/FileSystemFileHandle.js b/src/FileSystemFileHandle.js index c24e35a..47ac490 100644 --- a/src/FileSystemFileHandle.js +++ b/src/FileSystemFileHandle.js @@ -1,13 +1,15 @@ import FileSystemHandle from './FileSystemHandle.js' import FileSystemWritableFileStream from './FileSystemWritableFileStream.js' +const kAdapter = Symbol('adapter') + class FileSystemFileHandle extends FileSystemHandle { /** @type {FileSystemFileHandle} */ - #adapter + [kAdapter] constructor (adapter) { super(adapter) - this.#adapter = adapter + this[kAdapter] = adapter } /** @@ -17,7 +19,7 @@ class FileSystemFileHandle extends FileSystemHandle { */ async createWritable (options = {}) { return new FileSystemWritableFileStream( - await this.#adapter.createWritable(options) + await this[kAdapter].createWritable(options) ) } @@ -25,7 +27,7 @@ class FileSystemFileHandle extends FileSystemHandle { * @returns {Promise} */ getFile () { - return Promise.resolve(this.#adapter.getFile()) + return Promise.resolve(this[kAdapter].getFile()) } } diff --git a/src/FileSystemHandle.js b/src/FileSystemHandle.js index 8d8b060..3bb1516 100644 --- a/src/FileSystemHandle.js +++ b/src/FileSystemHandle.js @@ -1,8 +1,8 @@ -const wm = new WeakMap() +const kAdapter = Symbol('adapter') class FileSystemHandle { /** @type {FileSystemHandle} */ - #adapter + [kAdapter] /** @type {string} */ name @@ -13,12 +13,12 @@ class FileSystemHandle { constructor (adapter) { this.kind = adapter.kind this.name = adapter.name - this.#adapter = adapter + this[kAdapter] = adapter } async queryPermission (options = {}) { if (options.readable) return 'granted' - const handle = this.#adapter + const handle = this[kAdapter] return handle.queryPermission ? await handle.queryPermission(options) : handle.writable @@ -28,7 +28,7 @@ class FileSystemHandle { async requestPermission (options = {}) { if (options.readable) return 'granted' - const handle = this.#adapter + const handle = this[kAdapter] return handle.writable ? 'granted' : 'denied' } @@ -39,7 +39,7 @@ class FileSystemHandle { * @param {boolean} [options.recursive=false] */ async remove (options = {}) { - await this.#adapter.remove(options) + await this[kAdapter].remove(options) } /** @@ -48,7 +48,7 @@ class FileSystemHandle { async isSameEntry (other) { if (this === other) return true if (this.kind !== other.kind) return false - return this.#adapter.isSameEntry(other.#adapter) + return this[kAdapter].isSameEntry(other[kAdapter]) } } diff --git a/src/getOriginPrivateDirectory.js b/src/getOriginPrivateDirectory.js index 6096fe1..9c679d4 100644 --- a/src/getOriginPrivateDirectory.js +++ b/src/getOriginPrivateDirectory.js @@ -14,7 +14,7 @@ if (globalThis.DataTransferItem && !DataTransferItem.prototype.getAsFileSystemHa import('./FileSystemDirectoryHandle.js'), import('./FileSystemFileHandle.js') ]) - console.log('jo') + return entry.isFile ? new FileSystemFileHandle(new FileHandle(entry, false)) : new FileSystemDirectoryHandle(new FolderHandle(entry, false)) diff --git a/test/test.js b/test/test.js index 0f68379..97ad8d5 100644 --- a/test/test.js +++ b/test/test.js @@ -14,6 +14,11 @@ import { capture } from './util.js' +if (!globalThis.WritableStream) { + const m = await import('https://cdn.jsdelivr.net/npm/web-streams-polyfill@3/dist/ponyfill.es2018.mjs') + globalThis.ReadableStream = m.ReadableStream +} + /** @type {typeof window.Blob} */ const Blob = globalThis.Blob || await import('fetch-blob').then(m => m.Blob)