Skip to content

Commit

Permalink
Degraded private field to Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed Jul 7, 2021
1 parent ac1693c commit cbd8dcd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 9 additions & 7 deletions src/FileSystemDirectoryHandle.js
Original file line number Diff line number Diff line change
@@ -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
}

/**
Expand All @@ -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)
}

Expand All @@ -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))
}

/**
Expand All @@ -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]() {
Expand Down
10 changes: 6 additions & 4 deletions src/FileSystemFileHandle.js
Original file line number Diff line number Diff line change
@@ -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
}

/**
Expand All @@ -17,15 +19,15 @@ class FileSystemFileHandle extends FileSystemHandle {
*/
async createWritable (options = {}) {
return new FileSystemWritableFileStream(
await this.#adapter.createWritable(options)
await this[kAdapter].createWritable(options)
)
}

/**
* @returns {Promise<File>}
*/
getFile () {
return Promise.resolve(this.#adapter.getFile())
return Promise.resolve(this[kAdapter].getFile())
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/FileSystemHandle.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const wm = new WeakMap()
const kAdapter = Symbol('adapter')

class FileSystemHandle {
/** @type {FileSystemHandle} */
#adapter
[kAdapter]

/** @type {string} */
name
Expand All @@ -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
Expand All @@ -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'
}

Expand All @@ -39,7 +39,7 @@ class FileSystemHandle {
* @param {boolean} [options.recursive=false]
*/
async remove (options = {}) {
await this.#adapter.remove(options)
await this[kAdapter].remove(options)
}

/**
Expand All @@ -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])
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/getOriginPrivateDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit cbd8dcd

Please sign in to comment.