Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Support for OPFS #841

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions api-reports/2_12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,39 @@ FileReader[JO] val LOADING: Short
FileReaderSync[JC] def readAsArrayBuffer(blob: Blob): ArrayBuffer
FileReaderSync[JC] def readAsDataURL(blob: Blob): URL
FileReaderSync[JC] def readAsText(blob: Blob, encoding: String?): String
FileSystemCreateWritableOptions[JT] var keepExistingData: js.UndefOr[Boolean]
FileSystemDirectoryHandle[JT] def getDirectoryHandle(name: String, options: js.UndefOr[FileSystemGetDirectoryOptions]?): js.Promise[FileSystemDirectoryHandle]
FileSystemDirectoryHandle[JT] def getFileHandle(name: String, options: js.UndefOr[FileSystemGetFileOptions]?): js.Promise[FileSystemFileHandle]
FileSystemDirectoryHandle[JT] def isSameEntry(fileSystemHandle: FileSystemHandle): js.Promise[Boolean]
FileSystemDirectoryHandle[JT] val kind: FileSystemHandleKind
FileSystemDirectoryHandle[JT] def name: String
FileSystemDirectoryHandle[JT] def removeEntry(name: String, options: js.UndefOr[FileSystemRemoveOptions]?): js.Promise[Unit]
FileSystemDirectoryHandle[JT] def resolve(possibleDescendant: FileSystemHandle): js.Promise[js.Array[String]]
FileSystemFileHandle[JT] def createSyncAccessHandle(): js.Promise[FileSystemSyncAccessHandle]
FileSystemFileHandle[JT] def createWritable(options: js.UndefOr[FileSystemCreateWritableOptions]?): js.Promise[FileSystemWritableFileStream]
FileSystemFileHandle[JT] def getFile(): js.Promise[File]
FileSystemGetDirectoryOptions[JT] var create: js.UndefOr[Boolean]
FileSystemGetFileOptions[JT] var create: js.UndefOr[Boolean]
FileSystemHandle[JT] def isSameEntry(fileSystemHandle: FileSystemHandle): js.Promise[Boolean]
FileSystemHandle[JT] val kind: FileSystemHandleKind
FileSystemHandle[JT] def name: String
FileSystemHandleKind[JT]
FileSystemHandleKind[SO] val directory: FileSystemHandleKind
FileSystemHandleKind[SO] val file: FileSystemHandleKind
FileSystemReadWriteOptions[JT] var at: Double
FileSystemRemoveOptions[JT] var recursive: js.UndefOr[Boolean]
FileSystemSyncAccessHandle[JT] def close(): Unit
FileSystemSyncAccessHandle[JT] def flush(): Unit
FileSystemSyncAccessHandle[JT] def getSize(): Double
FileSystemSyncAccessHandle[JT] def read(buffer: BufferSource, options: js.UndefOr[FileSystemReadWriteOptions]?): Double
FileSystemSyncAccessHandle[JT] def truncate(newSize: Double): Unit
FileSystemSyncAccessHandle[JT] def write(buffer: BufferSource, options: js.UndefOr[FileSystemReadWriteOptions]?): Double
FileSystemWritableFileStream[JT] def seek(position: Double): js.Promise[Unit]
FileSystemWritableFileStream[JT] def truncate(size: Double): js.Promise[Unit]
FileSystemWritableFileStream[JT] def write(data: Blob): js.Promise[Unit]
FileSystemWritableFileStream[JT] def write(data: BufferSource): js.Promise[Unit]
FileSystemWritableFileStream[JT] def write(data: String): js.Promise[Unit]
FileSystemWritableFileStream[JT] def write(data: WriteParams): js.Promise[Unit]
FocusEvent[JC] def bubbles: Boolean
FocusEvent[JC] def cancelBubble: Boolean
FocusEvent[JC] def cancelable: Boolean
Expand Down Expand Up @@ -26374,6 +26407,7 @@ StorageEventInit[JT] var scoped: js.UndefOr[Boolean]
StorageEventInit[JT] var storageArea: js.UndefOr[Storage]
StorageEventInit[JT] var url: js.UndefOr[String]
StorageManager[JT] def estimate(): js.Promise[StorageEstimate]
StorageManager[JT] def getDirectory(): js.Promise[FileSystemDirectoryHandle]
StorageManager[JT] def persist(): js.Promise[Boolean]
StorageManager[JT] def persisted(): js.Promise[Boolean]
StyleMedia[JT] def matchMedium(mediaquery: String): Boolean
Expand Down Expand Up @@ -27574,6 +27608,14 @@ WorkerOptions[JT] var `type`: js.UndefOr[WorkerType]
WorkerType[JT]
WorkerType[SO] val classic: WorkerType
WorkerType[SO] val module: WorkerType
WriteCommandType[JT]
WriteCommandType[SO] val seek: WriteCommandType
WriteCommandType[SO] val truncate: WriteCommandType
WriteCommandType[SO] val write: WriteCommandType
WriteParams[JT] var data: js.UndefOr[BufferSource | Blob | String]
WriteParams[JT] var position: js.UndefOr[Double]
WriteParams[JT] var size: js.UndefOr[Double]
WriteParams[JT] var `type`: WriteCommandType
WriteableState[JT]
WriteableState[SO] val closed: WriteableState
WriteableState[SO] val closing: WriteableState
Expand Down
11 changes: 11 additions & 0 deletions dom/src/main/scala-2/org/scalajs/dom/FileSystemHandleKind.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
sealed trait FileSystemHandleKind extends js.Any

object FileSystemHandleKind {
val file: FileSystemHandleKind = "file".asInstanceOf[FileSystemHandleKind]
val directory: FileSystemHandleKind = "directory".asInstanceOf[FileSystemHandleKind]
}
12 changes: 12 additions & 0 deletions dom/src/main/scala-2/org/scalajs/dom/WriteCommandType.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
sealed trait WriteCommandType extends js.Any

object WriteCommandType {
val write: WriteCommandType = "write".asInstanceOf[WriteCommandType]
val seek: WriteCommandType = "seek".asInstanceOf[WriteCommandType]
val truncate: WriteCommandType = "truncate".asInstanceOf[WriteCommandType]
}
10 changes: 10 additions & 0 deletions dom/src/main/scala-3/org/scalajs/dom/FileSystemHandleKind.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.scalajs.dom

import scala.scalajs.js

opaque type FileSystemHandleKind <: String = String

object FileSystemHandleKind {
val file: FileSystemHandleKind = "file"
val directory: FileSystemHandleKind = "directory"
}
11 changes: 11 additions & 0 deletions dom/src/main/scala-3/org/scalajs/dom/WriteCommandType.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.scalajs.dom

import scala.scalajs.js

opaque type WriteCommandType <: String = String

object WriteCommandType {
val write: WriteCommandType = "write"
val seek: WriteCommandType = "seek"
val truncate: WriteCommandType = "truncate"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API
* and available under the Creative Commons Attribution-ShareAlike v2.5 or later.
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Everything else is under the MIT License http://opensource.org/licenses/MIT
*/
package org.scalajs.dom

import scala.scalajs.js

trait FileSystemCreateWritableOptions extends js.Object {

/** A Boolean. Default false. When set to true if the file exists, the existing file is first copied to the temporary
* file. Otherwise the temporary file starts out empty.
*/
var keepExistingData: js.UndefOr[Boolean] = js.undefined
}
67 changes: 67 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/FileSystemDirectoryHandle.scala
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type also defines methods that return async iterators, which don't appear to be supported at the moment in scala-js. It makes sense to me to tackle those methods in a separate piece of work. I've asked about potentially adding support over in the scala-js discord.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API
* and available under the Creative Commons Attribution-ShareAlike v2.5 or later.
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Everything else is under the MIT License http://opensource.org/licenses/MIT
*/
package org.scalajs.dom

import scala.scalajs.js

/** The FileSystemDirectoryHandle interface of the File System API provides a handle to a file system directory. */
@js.native
trait FileSystemDirectoryHandle extends FileSystemHandle {

/** The getDirectoryHandle() method of the FileSystemDirectoryHandle interface returns a FileSystemDirectoryHandle for
* a subdirectory with the specified name within the directory handle on which the method is called.
*
* @param name
* A string representing the FileSystemHandle.name of the file you wish to retrieve.
* @param options
* An optional object containing options for the retrieved subdirectory.
*
* @return
* A Promise which resolves with a FileSystemDirectoryHandle.
*/
def getDirectoryHandle(name: String,
options: js.UndefOr[FileSystemGetDirectoryOptions] = js.native): js.Promise[FileSystemDirectoryHandle] = js.native

/** The getFileHandle() method of the FileSystemDirectoryHandle interface returns a FileSystemFileHandle for a file
* with the specified name, within the directory the method is called.
*
* @param name
* A string representing the FileSystemHandle.name of the subdirectory you wish to retrieve.
* @param options
* An object.
*
* @return
* A Promise which resolves with a FileSystemFileHandle.
*/
def getFileHandle(name: String,
options: js.UndefOr[FileSystemGetFileOptions] = js.native): js.Promise[FileSystemFileHandle] = js.native

/** The removeEntry() method of the FileSystemDirectoryHandle interface attempts to remove an entry if the directory
* handle contains a file or directory called the name specified.
*
* @param name
* A string representing the FileSystemHandle.name of the entry you wish to remove.
* @param options
* An optional object containing options
*
* @return
* A Promise which resolves with undefined.
*/
def removeEntry(name: String, options: js.UndefOr[FileSystemRemoveOptions] = js.native): js.Promise[Unit] = js.native

/** The resolve() method of the FileSystemDirectoryHandle interface returns an Array of directory names from the
* parent handle to the specified child entry, with the name of the child entry as the last array item.
*
* @param possibleDescendant
* The FileSystemHandle from which to return the relative path.
*
* @return
* A Promise which resolves with an Array of strings, or null if possibleDescendant is not a descendant of this
* FileSystemDirectoryHandle.
*/
def resolve(possibleDescendant: FileSystemHandle): js.Promise[js.Array[String]] = js.native
}
61 changes: 61 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/FileSystemFileHandle.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API
* and available under the Creative Commons Attribution-ShareAlike v2.5 or later.
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Everything else is under the MIT License http://opensource.org/licenses/MIT
*/
package org.scalajs.dom

import scala.scalajs.js

/** The FileSystemFileHandle interface of the File System API represents a handle to a file system entry. The interface
* is accessed through the window.showOpenFilePicker() method.
*
* Note that read and write operations depend on file-access permissions that do not persist after a page refresh if no
* other tabs for that origin remain open. The queryPermission method of the FileSystemHandle interface can be used to
* verify permission state before accessing a file.
*/
@js.native
trait FileSystemFileHandle extends js.Object {

/** The getFile() method of the FileSystemFileHandle interface returns a Promise which resolves to a File object
* representing the state on disk of the entry represented by the handle.
*
* If the file on disk changes or is removed after this method is called, the returned File object will likely be no
* longer readable.
*
* @return
* A Promise which resolves to a File object.
*/
def getFile(): js.Promise[File] = js.native

/** The createSyncAccessHandle() method of the FileSystemFileHandle interface returns a Promise which resolves to a
* FileSystemSyncAccessHandle object that can be used to synchronously read from and write to a file. The synchronous
* nature of this method brings performance advantages, but it is only usable inside dedicated Web Workers for files
* within the origin private file system.
*
* Creating a FileSystemSyncAccessHandle takes an exclusive lock on the file associated with the file handle. This
* prevents the creation of further FileSystemSyncAccessHandles or FileSystemWritableFileStreams for the file until
* the existing access handle is closed.
*
* @return
* A Promise which resolves to a FileSystemSyncAccessHandle object.
*/
def createSyncAccessHandle(): js.Promise[FileSystemSyncAccessHandle] = js.native

/** The createWritable() method of the FileSystemFileHandle interface creates a FileSystemWritableFileStream that can
* be used to write to a file. The method returns a Promise which resolves to this created stream.
*
* Any changes made through the stream won't be reflected in the file represented by the file handle until the stream
* has been closed. This is typically implemented by writing data to a temporary file, and only replacing the file
* represented by file handle with the temporary file when the writable filestream is closed.
*
* @param options
* An object.
*
* @return
* A Promise which resolves to a FileSystemWritableFileStream object.
*/
def createWritable(
options: js.UndefOr[FileSystemCreateWritableOptions] = js.native): js.Promise[FileSystemWritableFileStream] = js.native
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API
* and available under the Creative Commons Attribution-ShareAlike v2.5 or later.
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Everything else is under the MIT License http://opensource.org/licenses/MIT
*/
package org.scalajs.dom

import scala.scalajs.js

trait FileSystemGetDirectoryOptions extends js.Object {

/** A boolean value, which defaults to false. When set to true if the directory is not found, one with the specified
* name will be created and returned.
*/
var create: js.UndefOr[Boolean] = js.undefined
}
17 changes: 17 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/FileSystemGetFileOptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API
* and available under the Creative Commons Attribution-ShareAlike v2.5 or later.
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Everything else is under the MIT License http://opensource.org/licenses/MIT
*/
package org.scalajs.dom

import scala.scalajs.js

trait FileSystemGetFileOptions extends js.Object {

/** A Boolean. Default false. When set to true if the file is not found, one with the specified name will be created
* and returned.
*/
var create: js.UndefOr[Boolean] = js.undefined
}
34 changes: 34 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/FileSystemHandle.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API
* and available under the Creative Commons Attribution-ShareAlike v2.5 or later.
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Everything else is under the MIT License http://opensource.org/licenses/MIT
*/
package org.scalajs.dom

import scala.scalajs.js

/** The FileSystemHandle interface of the File System API is an object which represents a file or directory entry.
* Multiple handles can represent the same entry. For the most part you do not work with FileSystemHandle directly but
* rather its child interfaces FileSystemFileHandle and FileSystemDirectoryHandle.
*/
@js.native
trait FileSystemHandle extends js.Object {

/** Returns the type of entry. This is 'file' if the associated entry is a file or 'directory'. */
val kind: FileSystemHandleKind = js.native

/** Returns the name of the associated entry. */
def name: String = js.native

/** The isSameEntry() method of the FileSystemHandle interface compares two handles to see if the associated entries
* (either a file or directory) match.
*
* @param fileSystemHandle
* The FileSystemHandle to match against the handle on which the method is invoked.
*
* @return
* A Promise that fulfills with a Boolean.
*/
def isSameEntry(fileSystemHandle: FileSystemHandle): js.Promise[Boolean] = js.native
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API
* and available under the Creative Commons Attribution-ShareAlike v2.5 or later.
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Everything else is under the MIT License http://opensource.org/licenses/MIT
*/
package org.scalajs.dom

import scala.scalajs.js

trait FileSystemReadWriteOptions extends js.Object {

/** A number representing the offset in bytes from the start of the file that the file should be read from or written
* at.
*/
var at: Double
}
15 changes: 15 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/FileSystemRemoveOptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API
* and available under the Creative Commons Attribution-ShareAlike v2.5 or later.
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Everything else is under the MIT License http://opensource.org/licenses/MIT
*/
package org.scalajs.dom

import scala.scalajs.js

trait FileSystemRemoveOptions extends js.Object {

/** A boolean value, which defaults to false. When set to true entries will be removed recursively. */
var recursive: js.UndefOr[Boolean] = js.undefined
}
Loading
Loading