js-platform/idbfs — pull request #39 by humphd (2013-11-26 00:39:41+00:00)
Title: Make backends pluggable via FileSystem ctor, fixes #36.

This is still in progress, but it's now passing all tests, and probably worth looking at for feedback. I've rebased the new symlink changes in as well.

This patch does a few things, including:

1) Creates `src/providers` which is a set of storage providers (currently Memory and IndexedDB). Later we can drop a WebSQL one in there too without much effort. A provider is an abstraction over a database transaction. It has the following API:

```javascript
// See if your provider is supported
if(IDBFS.Providers.IndexedDB.isSupported()) {...}
// Create a provider
var provider = new IDBFS.Providers.IndexedDB();
// Open it before you can use it (provides a way to do authentication or other first-run operations
provider.open(function(err, needsFormatting) {
// needsFormatting means the db was just created, and has no fs.
});
// Now you can request read-only or read-write contexts to use
var contextRO = provider.getReadOnlyContext();
...
var contextRW = provider.getReadWriteContext();
// A context has async put, get, delete, and clear operations:
contextRO.get("key", callback);
...
contextRW.put("key", "value", callback);
contextRW.delete("key", callback);
conextRW.clear(callback);
```

2) There is now only 1 `FileSystem` object, and all storage/db operations are abstracted to a provider. You can explicitly pass a provider in when you make a `FileSystem` or let it use the default provider (i.e., `IDBFS.Providers.Default`, which is IndexedDB):

```javascript
// Use defaults (indexeddb)
var fs = new FileSystem();
// Use Memory provider
var fsRAM = new FileSystem({provider: new IDBFS.Providers.Memory()});
// Use IndexedDB provider explicitly
var fsIDB = new FileSystem({provider: new IDBFS.Providers.IndexedDB()});
// Use a custom one you create outside our library (e.g., DropBox)
var fsCustom = new FileSystem({provider: new CustomProvider()});
```

3) A new `FileSystemsManager` object has been created in `src/filesystems-manager.js`. The purpose of this object is to manage internal aspects of a file system, for example, file descriptors, queued operations, etc. This way we don't expose things to the public API on the prototype that shouldn't be touched by code outside fs.js.

4) Secondary functions have been moved off the prototype.

5) I've added a bunch of new tests for the providers.

I'll follow-up with proper docs so people know how to extend the code with new/external providers.