The origin non-public file system


Browser help #

The origin non-public file system is supported by trendy browsers and is standardized by the Internet Hypertext Utility Know-how Working Group (WHATWG) within the File System Residing Commonplace.

Browser help

  • Chrome 86, Supported 86
  • Firefox 111, Supported 111
  • Edge 86, Supported 86
  • Safari 15.2, Supported 15.2


Motivation #

Once you consider recordsdata in your pc, you in all probability take into consideration a file hierarchy: recordsdata organized in folders which you can discover along with your working system’s file explorer. For instance, on Home windows, for a consumer referred to as Tom, their To Do listing may reside in C:UsersTomDocumentsToDo.txt. On this instance, ToDo.txt is the file title, and Customers, Tom, and Paperwork are folder names. C: on Home windows represents the foundation listing of the drive.

Conventional method of working with recordsdata on the net #

To edit the To Do listing in an online software, that is the normal move:

  1. The consumer uploads the file to a server or opens it on the shopper with &LTinput sort="file">.
  2. The consumer makes their modifications, after which downloads the ensuing file with an injected &LTa obtain="ToDo.txt> that you simply programmatically click on() by way of JavaScript.
  3. For opening folders, you employ a particular attribute in &LTinput sort="file" webkitdirectory>, which, regardless of its proprietary title, has virtually common browser help.

Trendy method of working with recordsdata on the net #

This move just isn’t consultant of how customers consider enhancing recordsdata, and means customers find yourself with downloaded copies of their enter recordsdata. Due to this fact, the File System Entry API launched three picker strategies—showOpenFilePicker(), showSaveFilePicker(), and showDirectoryPicker()—that do precisely what their title suggests. They permit a move as follows:

  1. Open ToDo.txt with showOpenFilePicker(), and get a FileSystemFileHandle object.
  2. From the FileSystemFileHandle object, get a File by calling the file deal with’s getFile() methodology.
  3. Modify the file, then name requestPermission({mode: 'readwrite'}) on the deal with.
  4. If the consumer accepts the permission request, save the modifications again to the unique file.
  5. Alternatively, name showSaveFilePicker() and let the consumer choose a brand new file. (If the consumer picks a beforehand opened file, its contents will probably be overwritten.) For repeat saves, you possibly can hold the file deal with round, so you do not have to point out the file save dialog once more.

Restrictions of working with recordsdata on the net #

Information and folders which might be accessible by way of these strategies reside in what could be referred to as the user-visible file system. Information saved from the net, and executable recordsdata particularly, are marked with the mark of the net, so there’s an extra warning the working system can present earlier than a probably harmful file will get executed. As an extra safety function, recordsdata obtained from the net are additionally protected by Secure Searching, which, for the sake of simplicity and within the context of this text, you possibly can consider as a cloud-based virus scan. Once you write knowledge to a file utilizing the File System Entry API, writes should not in-place, however use a brief file. The file itself just isn’t modified until it passes all these safety checks. As you possibly can think about, this work makes file operations comparatively sluggish, regardless of enhancements utilized the place attainable, for instance, on macOS. Nonetheless each write() name is self-contained, so underneath the hood it opens the file, seeks to the given offset, and at last writes knowledge.

Information as the muse of processing #

On the identical time, recordsdata are a wonderful strategy to file knowledge. For instance, SQLite shops total databases in a single file. One other instance are mipmaps utilized in picture processing. Mipmaps are pre-calculated, optimized sequences of photos, every of which is a progressively decrease decision illustration of the earlier, which makes many operations like zooming quicker. So how can net purposes get the advantages of recordsdata, however with out the efficiency prices of conventional web-based file processing? The reply is the origin non-public file system.

The user-visible versus the origin non-public file system #

In contrast to the user-visible file system browsed by way of the working system’s file explorer, with recordsdata and folders you possibly can learn, write, transfer, and rename, the origin non-public file system just isn’t meant to be seen by customers. Information and folders within the origin non-public file system, because the title suggests, are non-public, and extra concretely, non-public to the origin of a website. Uncover the origin of a web page by typing location.origin within the DevTools Console. For instance, the origin of the web page is (that’s, the half /articles is not a part of the origin). You’ll be able to learn extra concerning the idea of origins in Understanding “same-site” and “same-origin”. All pages that share the identical origin can see the identical origin non-public file system knowledge, so can see the identical particulars because the earlier instance. Every origin has its personal impartial origin non-public file system, which suggests the origin non-public file system of is totally distinct from the one in every of, say, On Home windows, the foundation listing of the user-visible file system is C:. The equal for the origin non-public file system is an initially empty root listing per origin accessed by calling the asynchronous methodology For a comparability of the user-visible file system and the origin non-public file system, see the next diagram. The diagram reveals that aside from the foundation listing, the whole lot else is conceptually the identical, with a hierarchy of recordsdata and folders to arrange and prepare as wanted in your knowledge and storage wants.

Diagram of the user-visible file system and the origin private file system with two exemplary file hierarchies. The entry point for the user-visible file system is a symbolic harddisk, the entry point for the origin private file system is calling of the method ''.

Specifics of the origin non-public file system #

Similar to different storage mechanisms within the browser (for instance, localStorage or IndexedDB), the origin non-public file system is topic to browser quota restrictions. When a consumer clears all shopping knowledge or all website knowledge, the origin non-public file system will probably be deleted, too. Name and within the ensuing response object see the utilization entry to see how a lot storage your app already consumes, which is damaged down by storage mechanism within the usageDetails object, the place you need to have a look at the fileSystem entry particularly. For the reason that origin non-public file system just isn’t seen to the consumer, there are not any permissions prompts and no Secure Searching checks.

Gaining access to the foundation listing #

To get entry to the foundation listing, run the command beneath. You find yourself with an empty listing deal with, extra particularly, a FileSystemDirectoryHandle.

const opfsRoot = await;
// A FileSystemDirectoryHandle whose sort is "listing"
// and whose title is "".

Foremost thread or Internet Employee #

There are two methods of utilizing the origin non-public file system: on the principal thread or in a Internet Employee. Internet Employees can’t block the principle thread, which suggests on this context APIs could be synchronous, a sample typically disallowed on the principle thread. Synchronous APIs could be quicker as they keep away from having to cope with guarantees, and file operations are usually synchronous in languages like C that may be compiled to WebAssembly.

// That is synchronous C code.
FILE *f;
f = fopen("instance.txt", "w+");
fputs("Some textn", f);

When you want the quickest attainable file operations and/otherwise you cope with WebAssembly, skip all the way down to Utilizing the origin non-public file system in a Internet Employee. Else, you possibly can learn on.

Utilizing the origin non-public file system on the principle thread #

Creating new recordsdata and folders #

After getting a root folder, create recordsdata and folders utilizing the getFileHandle() and the getDirectoryHandle() strategies respectively. By passing {create: true}, the file or folder will probably be created if it would not exist. Construct up a hierarchy of recordsdata by calling these features utilizing a newly created listing as the start line.

const fileHandle = await opfsRoot
.getFileHandle('my first file', {create: true});
const directoryHandle = await opfsRoot
.getDirectoryHandle('my first folder', {create: true});
const nestedFileHandle = await directoryHandle
.getFileHandle('my first nested file', {create: true});
const nestedDirectoryHandle = await directoryHandle
.getDirectoryHandle('my first nested folder', {create: true});

The resulting file hierarchy from the earlier code sample.

Accessing current recordsdata and folders #

If you recognize their title, entry beforehand created recordsdata and folders by calling the getFileHandle() or the getDirectoryHandle() strategies, passing within the title of the file or folder.

const existingFileHandle = await opfsRoot.getFileHandle('my first file');
const existingDirectoryHandle = await opfsRoot
.getDirectoryHandle('my first folder);

Getting the file related to a file deal with for studying #

A FileSystemFileHandle represents a file on the file system. To acquire the related File, use the getFile() methodology. A File object is a selected type of Blob, and can be utilized in any context {that a} Blob can. Particularly, FileReader, URL.createObjectURL(), createImageBitmap(), and XMLHttpRequest.ship() settle for each Blobs and Information. If you’ll, acquiring a File from a FileSystemFileHandle “frees” the info, so you possibly can entry it and make it obtainable to the user-visible file system.

const file = await fileHandle.getFile();
console.log(await file.textual content());

Writing to a file by streaming #

Stream knowledge right into a file by calling createWritable() which creates a FileSystemWritableFileStream to that you simply then write() the contents. On the finish, that you must shut() the stream.

const contents = 'Some textual content';
// Get a writable stream.
const writable = await fileHandle.createWritable();
// Write the contents of the file to the stream.
await writable.write(contents);
// Shut the stream, which persists the contents.
await writable.shut();

Deleting recordsdata and folders #

Delete recordsdata and folders by calling their file or listing deal with’s specific take away() methodology. To delete a folder together with all subfolders, move the {recursive: true} possibility.

await fileHandle.take away();
await directoryHandle.take away({recursive: true});

In its place, if you recognize the title of the to-be-deleted file or folder in a listing, use the removeEntry() methodology.

directoryHandle.removeEntry('my first nested file');

Transferring and renaming recordsdata and folders #

Rename and transfer recordsdata and folders utilizing the transfer() methodology. Transferring and renaming can occur collectively or in isolation.

// Rename a file.
await fileHandle.transfer('my first renamed file');
// Transfer a file to a different listing.
await fileHandle.transfer(nestedDirectoryHandle);
// Transfer a file to a different listing and rename it.
await fileHandle
.transfer(nestedDirectoryHandle, 'my first renamed and now nested file');

Resolving the trail of a file or folder #

To study the place a given file or folder is positioned in relation to a reference listing, use the resolve() methodology, passing it a FileSystemHandle because the argument. To acquire the complete path of a file or folder within the origin non-public file system, use the foundation listing because the reference listing obtained by way of

const relativePath = await opfsRoot.resolve(nestedDirectoryHandle);
// `relativePath` is `['my first folder', 'my first nested folder']`.

Checking if two file or folder handles level to the identical file or folder #

Generally you will have two handles and do not know in the event that they level on the identical file or folder. To test whether or not that is the case, use the isSameEntry() methodology.

// Returns `false`.

Itemizing the contents of a folder #

FileSystemDirectoryHandle is an asynchronous iterator that you simply iterate over with a for await…of loop. As an asynchronous iterator, it additionally helps the entries(), the values(), and the keys() strategies, from which you’ll select relying on what info you want:

for await (let [name, handle] of directoryHandle) {}
for await (let [name, handle] of directoryHandle.entries()) {}
for await (let deal with of directoryHandle.values()) {}
for await (let title of directoryHandle.keys()) {}

Recursively itemizing the contents of a folder and all subfolders #

Coping with asynchronous loops and features paired with recursion is simple to get incorrect. The perform beneath can function a place to begin for itemizing the contents of a folder and all its subfolders, together with all recordsdata and their sizes. You’ll be able to simplify the perform in the event you do not want the file sizes by, the place it says directoryEntryPromises.push, not pushing the deal with.getFile() promise, however the deal with immediately.

  const getDirectoryEntriesRecursive = async (
relativePath = '.',
) => {
const fileHandles = [];
const directoryHandles = [];
const entries = {};
// Get an iterator of the recordsdata and folders within the listing.
const directoryIterator = directoryHandle.values();
const directoryEntryPromises = [];
for await (const deal with of directoryIterator) {
const nestedPath = `${relativePath}/${deal with.title}`;
if (deal with.variety === 'file') {
fileHandles.push({ deal with, nestedPath });
deal with.getFile().then((file) => {
return {
title: deal with.title,
variety: deal with.variety,
dimension: file.dimension,
sort: file.sort,
lastModified: file.lastModified,
relativePath: nestedPath,
deal with
} else if (deal with.variety === 'listing') {
directoryHandles.push({ deal with, nestedPath });
(async () => {
return {
title: deal with.title,
variety: deal with.variety,
relativePath: nestedPath,
await getDirectoryEntriesRecursive(deal with, nestedPath),
deal with,
const directoryEntries = await Promise.all(directoryEntryPromises);
directoryEntries.forEach((directoryEntry) => {
entries[] = directoryEntry;
return entries;

Utilizing the origin non-public file system in a Internet Employee #

As outlined earlier than, Internet Employees cannot block the principle thread, which is why on this context synchronous strategies are allowed.

Getting a synchronous entry deal with #

The entry level to the quickest attainable file operations is a FileSystemSyncAccessHandle, obtained from a daily FileSystemFileHandle by calling createSyncAccessHandle().

const fileHandle = await opfsRoot
.getFileHandle('my highspeed file.txt', {create: true});
const syncAccessHandle = await fileHandle.createSyncAccessHandle();

Synchronous in-place file strategies #

After getting a synchronous entry deal with, you get entry to quick in-place file strategies which might be all synchronous.

  • getSize(): Returns the dimensions of the file in bytes.
  • write(): Writes the content material of a buffer into the, optionally at a given offset, and returns the variety of written bytes. Checking the returned variety of written bytes permits callers to detect and deal with errors and partial writes.
  • learn(): Reads the contents of the file right into a buffer, optionally at a given offset.
  • truncate(): Resizes the file to the given dimension.
  • flush(): Ensures that the contents of the file include all of the modifications completed by way of write().
  • shut(): Closes the entry deal with.

Right here is an instance that makes use of all of the strategies talked about above.

const opfsRoot = await;
const fileHandle = await opfsRoot.getFileHandle('quick', {create: true});
const accessHandle = await fileHandle.createSyncAccessHandle();

const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

// Initialize this variable for the dimensions of the file.
let dimension;
// The present dimension of the file, initially `0`.
dimension = accessHandle.getSize();
// Encode content material to jot down to the file.
const content material = textEncoder.encode('Some textual content');
// Write the content material initially of the file.
accessHandle.write(content material, {at: dimension});
// Flush the modifications.
// The present dimension of the file, now `9` (the size of "Some textual content").
dimension = accessHandle.getSize();

// Encode extra content material to jot down to the file.
const moreContent = textEncoder.encode('Extra content material');
// Write the content material on the finish of the file.
accessHandle.write(moreContent, {at: dimension});
// Flush the modifications.
// The present dimension of the file, now `21` (the size of
// "Some textMore content material").
dimension = accessHandle.getSize();

// Put together an information view of the size of the file.
const dataView = new DataView(new ArrayBuffer(dimension));

// Learn the whole file into the info view.
// Logs `"Some textMore content material"`.

// Learn beginning at offset 9 into the info view.
accessHandle.learn(dataView, {at: 9});
// Logs `"Extra content material"`.

// Truncate the file after 4 bytes.

Copying a file from the origin non-public file system to the user-visible file system #

As talked about above, shifting recordsdata from the origin non-public file system to the user-visible file system is not attainable, however you possibly can copy recordsdata. Since showSaveFilePicker() is just uncovered on the principle thread, however not within the Employee thread, you should definitely run the code there.

// On the principle thread, not within the Employee. This assumes
// `fileHandle` is the `FileSystemFileHandle` you obtained
// the `FileSystemSyncAccessHandle` from within the Employee
// thread. Make sure you shut the file within the Employee thread first.
const fileHandle = await opfsRoot.getFileHandle('quick');
attempt {
// Receive a file deal with to a brand new file within the user-visible file system
// with the identical title because the file within the origin non-public file system.
const saveHandle = await showSaveFilePicker( ''
const writable = await saveHandle.createWritable();
await writable.write(await fileHandle.getFile());
await writable.shut();
} catch (err) {
console.error(err.title, err.message);

Debugging the origin non-public file system #

Till built-in DevTools help is added (see crbug/1284595), use the OPFS Explorer Chrome extension to debug the origin non-public file system. The screenshot above from the part Creating new recordsdata and folders is taken straight from the extension by the best way.

The OPFS Explorer Chrome DevTools extension in the Chrome Web Store.

After putting in the extension, open the Chrome DevTools, choose the OPFS Explorer tab, and also you’re then prepared to examine the file hierarchy. Save recordsdata from the origin non-public file system to the user-visible file system by clicking the file title and delete recordsdata and folders by clicking the trash can icon.

Demo #

See the origin non-public file system in motion (in the event you set up the OPFS Explorer extension) in a demo that makes use of it as a backend for a SQLite database compiled to WebAssembly. Make sure you take a look at the supply code on Glitch. Be aware how the embedded model beneath doesn’t use the origin non-public file system backend (as a result of the iframe is cross-origin), however if you open the demo in a separate tab, it does.

Conclusions #

The origin non-public file system, as specified by the WHATWG, has formed the best way we use and work together with recordsdata on the net. It has enabled new use circumstances that had been not possible to realize with the user-visible file system. All main browser distributors—Apple, Mozilla, and Google—are on-board and share a joint imaginative and prescient. The event of the origin non-public file system may be very a lot a collaborative effort, and suggestions from builders and customers is important to its progress. As we proceed to refine and enhance the usual, suggestions on the whatwg/fs repository within the type of Points or Pull Requests is welcome.

Acknowledgements #

This text was reviewed by Austin Sully, Etienne Noël, and Rachel Andrew. Hero picture by Christina Rumpf on Unsplash.


Please enter your comment!
Please enter your name here