Compare commits

..

No commits in common. "master" and "develop" have entirely different histories.

133 changed files with 12589 additions and 29194 deletions

View File

@ -27,10 +27,6 @@
"semi": [
"error",
"always"
],
"eqeqeq": [
"error",
"always"
]
}
}

1
.gitattributes vendored
View File

@ -1 +0,0 @@
* text=auto eol=lf

View File

@ -1,27 +0,0 @@
name: node-js-ci
on:
pull_request:
branches:
- master
push:
branches:
- master
jobs:
build:
runs-on: ${{matrix.os}}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node: ['14', '16']
name: Node ${{ matrix.node }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Test
uses: actions/setup-node@v2.4.1
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test

7
.gitignore vendored
View File

@ -1,13 +1,10 @@
node_modules
bower_components
.env
*~
dist/filer-issue225.js
.vscode
.idea
# Parcel build dirs
.cache
tests/dist
# nyc code coverage
.nyc_output
coverage

1
.npmrc
View File

@ -1 +0,0 @@
loglevel=silent

View File

@ -1,17 +0,0 @@
{
"hooks": {
"before:init": ["npm run test"],
"before:bump": ["npm run build"]
},
"git": {
"pushRepo": "git@github.com:filerjs/filer.git",
"tagName": "v${version}"
},
"npm": {
"publish": true
},
"github": {
"pushRepo": "git@github.com:filerjs/filer.git",
"release": true
}
}

View File

@ -1,32 +1,16 @@
sudo: false
language: node_js
sudo: true
language: node_js
node_js:
- "lts/*"
cache:
directories:
- "node_modules"
os:
- linux
- osx
# Setup headless Firefox and Chrome support
# Setup headless Chrome support
# https://docs.travis-ci.com/user/gui-and-headless-browsers/#Using-the-Chrome-addon-in-the-headless-mode
env:
- MOZ_HEADLESS=1
addons:
chrome: stable
firefox: latest
before_install:
- google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &
after_success:
- npm install -g codecov
- npm run coverage
- codecov
notifications:
email: false
irc: "irc.mozilla.org#filer"

View File

@ -5,5 +5,3 @@ Barry Tulchinsky <barry.tulchinsky@gmail.com> (@btulchinsky)
Kieran Sedgwick <kieran.sedgwick@gmail.com> (@sedge)
Yoav Gurevich <ygurevich@ymail.com>
Gideon Thomas <r.gideonthomas@gmail.com>
Abdirahman Guled <aguled2@myseneca.ca>
Ben Heidemann <ben@heidemann.co.uk>

View File

@ -15,61 +15,44 @@ To get a working build system do the following:
npm install
```
Next, make sure you have installed Chrome and Firefox, which are needed for
running headless versions of the tests with `npm test`.
You can now run the following `npm` scripts:
* `npm run lint` or `npm run eslint` will run `eslint` on the `src` and `tests` directories.
* `npm run lint:fix` or `npm run eslint:fix` will run `eslint` with `--fix` on the `src` and `tests` directories, automatically fixing minor issues.
* `npm run test:manual` will build the tests, and allow you to run them in a browser manually by loading http://localhost:1234.
* `npm run karma-mocha` will build Filer and the tests, and finally run the tests in a headless Chrome browser.
* `npm test` will run `lint` followed by `karma-chrome`, and is what we do on Travis.
* `npm run build` will bundle two versions of Filer: `dist/filer.js` (unminified) and `dist/filer.min.js` (minified) as well as source map files.
Once you've done some hacking and you'd like to have your work merged, you'll need to
make a pull request. If you're patch includes code, make sure to check that all the
unit tests pass, including any new tests you wrote. Finally, make sure you add yourself
to the `AUTHORS` file.
=======
## Tests
Tests are written using [Mocha](https://mochajs.org/) and [Chai](http://chaijs.com/api/bdd/).
There are a number of ways to run the tests. The preferred way is:
```
npm test
```
This will do a build, run the linting, start a server, and load the tests into
headless versions of Chrome and Firefox.
If you want more control over how tests are run, you can use other scripts:
* Linting is done via `npm run lint` or `npm run eslint`, both of which will run `eslint` on the `src` and `tests` directories. You can also use `npm run lint:fix` or `npm run eslint:fix`, which will run `eslint` with `--fix` on the `src` and `tests` directories, automatically fixing minor issues. Linting is run by default as part of `npm test`
* In headless versions of Chrome and Firefox using `npm test`. A report at the end will tell you what happened with each browser. Browser tests are preferred because they also test our providers (e.g., IndexedDB). They do take longer to run. You can also use `npm run karma-mocha-firefox` or `npm run karma-mocha-chrome` to run the tests in only one of the two headless browsers.
* In node.js using the Memory provider using `npm run test:node`. These run much faster, but don't run all tests (e.g., providers, watches).
* If you need to debug browser tests, or want to run them in a different browser, use `npm run test:manual`, which will start a server and you can point your browser to [http://localhost:1234](http://localhost:1234). Running the tests this way will also automatically watch your files, and hot-reload your code and tests, which is useful for debugging and trial/error testing.
* If you need to debug node.js test runs, you can do so using `npm run test:node-debug`. Then, open Chrome and browse to [chrome://inspect](chrome://inspect) and click on your tests in the inspector. The easiest way to get a breakpoint is to manually add a `debugger` keyword to your test code where you want the tests to stop.
> Tip: you can add `skip()` to any `it()` or `describe()` in Mocha to skip a test, or `only()` to have only that test run. For example: `describe.skip(...)` or `it.only(...)`.
* If you want to run migration tests separate from unit tests, use `npm run test:migrations`. Migration tests run at the end of a typical `npm test` run. If you need to create a new migration test, see [`tools/fs-image.js`](tools/fs-image.js) for details on how to generate a filesystem image, and [tests/filesystems/images/README.md](tests/filesystems/images/README.md) for more docs.
* If you want to manually generate coverage info for the tests, use `npm run coverage`. This is done automatically in Travis, so you shouldn't need to do it. You can see [https://codecov.io/gh/filerjs/filer](https://codecov.io/gh/filerjs/filer) for detailed reports.
Tests are writting using [Mocha](http://visionmedia.github.io/mocha/) and [Chai](http://chaijs.com/api/bdd/).
You can run the tests in your browser by running `npm test` and opening your browser to `http://localhost:1234`.
There are a number of configurable options for the test suite, which are set via query string params.
First, you can choose which filer source to use (i.e., src/, dist/filer-test.js, dist/filer.js or dist/filer.min.js). The default is to use what is in /dist/filer-test.js, and you can switch to other versions like so:
First, you can choose which filer source to use (i.e., src/, dist/filer-test.js, dist/filer.js or dist/filer.min.js).
The default is to use what is in /dist/filer-test.js, and you can switch to other versions like so:
* tests/index.html?filer-dist/filer.js
* tests/index.html?filer-dist/filer.min.js
* tests/index.html?filer-src/filer.js (from src)
Second, you can specify which provider to use for all non-provider specific tests (i.e., most of the tests).
The default provider is `Memory`, and you can switch it like so:
* tests/index.html?filer-provider=memory
* tests/index.html?filer-provider=indexeddb
* tests/index.html?filer-provider=websql
If you're writing tests, make sure you write them in the same style as existing tests, which are
provider agnostic. See [`tests/lib/test-utils.js`](tests/lib/test-utils.js) and how it gets used
in various tests as an example.
provider agnostic. See `tests/lib/test-utils.js` and how it gets used in various tests as
an example.
## Releases
## Communication
In order to perform a release, you'll need commit access to the main Filer repo,
as well as access to publish to Filer's npm module. To do a release:
1. Make sure you have a .env file, with your `GITHUB_TOKEN` included. See [`env.sample`](env.sample) for more info on how to create one.
1. Login to the `npm` registry if you haven't already using `npm login`
1. Run `npm run release`. Releases are done interactively using [release-it](https://www.npmjs.com/package/release-it), and our config is defined in [`.release-it.json`](.release-it.json).
If you'd like to talk to someone about the project, you can reach us on irc.mozilla.org in the #filer or #mofodev channel. Look for "ack" or "humph".

View File

@ -1,4 +1,4 @@
Copyright (c) 2013 - 2019 Alan Kligman and the Filer contributors
Copyright (c) 2013, Alan Kligman
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

372
README.md
View File

@ -1,26 +1,26 @@
[![NPM](https://nodei.co/npm/filer.png?downloads=true&stars=true)](https://nodei.co/npm/filer/)
[![Build Status](https://secure.travis-ci.org/filerjs/filer.png?branch=develop)](http://travis-ci.org/filerjs/filer) [![codecov](https://codecov.io/gh/filerjs/filer/branch/master/graph/badge.svg)](https://codecov.io/gh/filerjs/filer)
[![Build Status](https://secure.travis-ci.org/filerjs/filer.png?branch=develop)](http://travis-ci.org/filerjs/filer)
### Filer
Filer is a drop-in replacement for node's `fs` module, a POSIX-like file system
for browsers.
Filer is a POSIX-like file system interface for node.js and browser-based JavaScript.
### Compatibility
Filer uses [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
and is [known to work in the following browsers/versions](https://caniuse.com/#feat=indexeddb):
Filer is known to work in the following browsers/versions, with the specified [Storage Providers](#providers):
* node.js: v0.10.*+
* IE: 10+
* Edge: 12+
* Firefox: 10+
* Chrome: 23+
* Safari: 10+
* Opera: 15+
* iOS: 10+
* Android Browser: 4.4+
* IE: 10+ (IndexedDB)
* Firefox: 26+ (IndexedDB)
* Chrome: 31+ (IndexedDB, WebSQL)
* Safari: 7.0+ (WebSQL)
* Opera: 19+ (IndexedDB, WebSQL)
* iOS: 3.2+ (WebSQL)
* Android Browser: 2.1-4.4 (WebSQL), 4.4+ (IndexedDB)
NOTE: if you're interested in maximum compatibility, use the `Fallback` provider instead of `Default`.
See the section on [Storage Providers](#providers).
### Contributing
@ -30,15 +30,16 @@ Want to join the fun? We'd love to have you! See [CONTRIBUTING](https://github.c
Filer can be obtained in a number of ways:
1. Via npm: `npm install filer`
1. Via unpkg: `<script src="https://unpkg.com/filer"></script>` or specify a version directly, for example: [https://unpkg.com/filer@1.0.1/dist/filer.min.js](https://unpkg.com/filer@1.0.1/dist/filer.min.js)
1. npm - `npm install filer`
2. bower - `bower install filer`
3. download pre-built versions: [filer.js](https://raw.github.com/filerjs/filer/develop/dist/filer.js), [filer.min.js](https://raw.github.com/filerjs/filer/develop/dist/filer.min.js)
### Loading and Usage
Filer is built as a UMD module and can therefore be loaded as a CommonJS or AMD module, or used via the global.
```javascript
// Option 1: Filer loaded via require()
// Option 1: Filer loaded via require() in node/browserify
var Filer = require('filer');
// Option 2: Filer loaded via RequireJS
@ -54,84 +55,13 @@ requirejs(['filer'], function(Filer) {...}
var Filer = window.Filer;
```
### Webpack Plugin
Filer can be used as a drop-in replacement for the node.js [fs](http://nodejs.org/api/fs.html) and
[path](http://nodejs.org/api/path.html) modules. For convenience, filer provides a webpack plugin which
will shim the desired node.js functionality. This plugin can be used by inserting the following into
your webpack config:
```javascript
// webpack.config.js
var { FilerWebpackPlugin } = require('filer/webpack');
module.exports = {
plugins: [
new FilerWebpackPlugin(),
],
}
```
---
**NOTE**
Previously it was recommended to access the `FilerWebpackPlugin` class by importing the main filer module. This was depracated due [this issue](https://github.com/filerjs/filer/issues/790). For anyone using ***filer version 1.4.0 or earlier***, please import the plugin class like this:
```javascript
var FilerWebpackPlugin = require('filer/src/webpack-plugin');
```
---
You can then import the node.js [fs](http://nodejs.org/api/fs.html) and [path](http://nodejs.org/api/path.html)
modules as normal and `FilerWebpackPlugin` will ensure that webpack will resolve references to these modules to
the appropriate filer shims. You will then be able to use these modules as normal (with the exception of the
synchronous fs methods e.g. `mkdirSync()`).
```javascript
import fs from 'fs';
import path from 'path';
```
The filer webpack plugin will, by default, shim the [fs](http://nodejs.org/api/fs.html) and
[path](http://nodejs.org/api/path.html) modules. However, it's behaviour can be customised by passing an
options object.
```javascript
// webpack.config.js
module.exports = {
plugins: [
new FilerWebpackPlugin({
// Options
}),
],
}
```
The following options can be passed to the filer webpack plugin:
| Option | Type | Optional | Default | Description |
|---------------|---------|----------|--------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| filerDir | string | yes | '\<rootDir\>/node_modules/filer' | The directory in which filer is installed. |
| shimsDir | string | yes | '\<rootDir\>/node_modules/filer/shims' | The directory in which the shims are installed. |
| fsProviderDir | string | yes | '\<rootDir\>/node_modules/filer/shims/providers' | The directory in which the shims are located. This option is required when using a custom provider. |
| shimFs | boolean | yes | true | Should the fs module be shimmed. |
| shimPath | boolean | yes | true | Should the path module be shimmed. |
| fsProvider | string | yes | 'default' | The file system provider to use. Should be one of 'default', 'indexeddb', 'memory', 'custom'. The 'default' option is equivalent to 'indexeddb'. |
NOTE: '\<rootDir\>' will be resolved to the current working directory.
Though filer also exposes the Buffer object, it is left up to the user to shim this as appropriate. This is because filer offers
no custom implementation. Currently, filer uses the [node-libs-browser](https://github.com/webpack/node-libs-browser) Buffer implementation
internally, though any faithful implementation of the [node.js Buffer object](http://nodejs.org/api/buffer.html) should play nicely
with filer.
### Getting Started
Filer is as close to the node.js [fs module](http://nodejs.org/api/fs.html) as possible,
with the following differences:
* No synchronous versions of methods (e.g., `mkdir()` but not `mkdirSync()`).
* No permissions (e.g., no `chown()`, `chmod()`, etc.).
* No support for stream-based operations (e.g., `fs.ReadStream`, `fs.WriteStream`).
Filer has other features lacking in node.js (e.g., swappable backend
@ -148,32 +78,19 @@ they are invoked. Ensure proper ordering by chaining operations in callbacks.
To create a new file system or open an existing one, create a new `FileSystem`
instance. By default, a new [IndexedDB](https://developer.mozilla.org/en/docs/IndexedDB)
database is created for each file system. The file system can also use other
backend storage providers, for example `Memory`. See the section on [Storage Providers](#providers).
backend storage providers, for example [WebSQL](http://en.wikipedia.org/wiki/Web_SQL_Database)
or even RAM (i.e., for temporary storage). See the section on [Storage Providers](#providers).
<a name="overviewExample"></a>
```js
const { fs, path } = require('filer');
fs.mkdir('/docs', (err) => {
if (err) {
return console.error('Unable to create /docs dir', err);
}
const filename = path.join('/docs', 'first.txt');
const data = 'Hello World!\n';
fs.writeFile(filename, data, (err) => {
if (err) {
return console.error('Unable to write /docs/first.txt', err);
}
fs.stat(filename, (err, stats) => {
if (err) {
return console.error('Unable to stat /docs/first.txt', err);
}
console.log('Stats for /docs/first.txt:', stats);
```javascript
var fs = new Filer.FileSystem();
fs.open('/myfile', 'w+', function(err, fd) {
if (err) throw err;
fs.close(fd, function(err) {
if (err) throw err;
fs.stat('/myfile', function(err, stats) {
if (err) throw err;
console.log('stats: ' + JSON.stringify(stats));
});
});
});
@ -182,7 +99,7 @@ fs.mkdir('/docs', (err) => {
For a complete list of `FileSystem` methods and examples, see the [FileSystem Instance Methods](#FileSystemMethods)
section below.
Filer also includes node's `path` and `Buffer` modules. See the [Filer.Path](#FilerPath) and [Filer.Buffer](#FilerBuffer) sections below.
Filer also supports node's Path module. See the [Filer.Path](#FilerPath) section below.
In addition, common shell operations (e.g., rm, touch, cat, etc.) are supported via the
`FileSystemShell` object, which can be obtained from, and used with a `FileSystem`.
@ -195,28 +112,25 @@ you omit the callback, errors will be thrown as exceptions). The first callback
reserved for passing errors. It will be `null` if no errors occurred and should always be checked.
#### Support for Promises
The Promise based API mimics the way node [implements](https://nodejs.org/api/fs.html#fs_fs_promises_api) them. Both `Shell` and `FileSystem` now have a `promises` property, which gives access to Promise based versions of methods in addition to the regular callback style methods. Method names are identical to their callback counterparts with the difference that instead of receiving a final argument as a callback, they return a Promise that is resolved or rejected based on the success of method execution.
The Promise based API mimics the way Node [implements](https://nodejs.org/api/fs.html#fs_fs_promises_api) them. Both `Shell` and `FileSystem` now have a `promises` object attached alongside the regular callback style methods. Method names are identical to their callback counterparts with the difference that instead of receiving a final argument as a callback, they return a Promise that is resolved or rejected based on the success of method execution.
> Please note that `exists` method will always throw, since it was [deprecated](https://nodejs.org/api/fs.html#fs_fs_exists_path_callback) by Node.
See example below:
```javascript
const fs = new Filer.FileSystem().promises;
fs.writeFile('/myfile', 'some data')
fs.open('/myfile', 'w+')
.then(fd => fs.close(fd))
.then(() => fs.stat('/myfile'))
.then(stats => { console.log(`stats: ${JSON.stringify(stats)}`); })
.catch(err => { console.error(err); });
.catch(err => { console.error(err); })
```
> The callback style usage could be found [here](#overviewExample).
#### Filer.FileSystem(options, callback) constructor
In most cases, using `Filer.fs` will be sufficient, and provide a working filesystem.
However, if you need more control over the filesystem, you can also use the `FileSystem`
constructor, invoked to open an existing file system or create a new one.
`Filer.FileSystem()` It accepts two arguments: an `options` object, and an optional
`callback` function. The `options` object can specify a number of optional arguments,
including:
File system constructor, invoked to open an existing file system or create a new one.
Accepts two arguments: an `options` object, and an optional `callback`. The `options`
object can specify a number of optional arguments, including:
* `name`: the name of the file system, defaults to `'"local'`
* `flags`: an Array of one or more flags to use when creating/opening the file system:
@ -251,17 +165,15 @@ it becomes ready.
#### Filer.FileSystem.providers - Storage Providers<a name="providers"></a>
Filer can be configured to use a number of different storage providers. The provider object encapsulates all aspects of data access, making it possible to swap in different backend storage options. There are currently 2 providers to choose from:
Filer can be configured to use a number of different storage providers. The provider object encapsulates all aspects
of data access, making it possible to swap in different backend storage options. There are currently 4 different
providers to choose from:
* `FileSystem.providers.IndexedDB()` - uses IndexedDB
if necessary
* `FileSystem.providers.WebSQL()` - uses WebSQL
* `FileSystem.providers.Fallback()` - attempts to use IndexedDB if possible, falling-back to WebSQL if necessary
* `FileSystem.providers.Memory()` - uses memory (not suitable for data that needs to survive the current session)
**NOTE**: previous versions of Filer also supported `FileSystem.providers.WebSQL()` and
`FileSystem.providers.Fallback()`, which could be used in browsers that supported
WebSQL but not IndexedDB. [WebSQL has been deprecated](https://www.w3.org/TR/webdatabase/),
and this functionality was removed in `v1.0.0`. If for some reason you still need it, use [`v0.0.44`](https://github.com/filerjs/filer/releases/tag/v0.0.44).
You can choose your provider when creating a `FileSystem`:
```javascript
@ -271,20 +183,28 @@ var providers = FileSystem.providers;
// Example 1: Use the default provider (currently IndexedDB)
var fs1 = new FileSystem();
// Example 2: Use the Memory provider
var fs2 = new FileSystem({ provider: new providers.Memory() });
// Example 2: Explicitly use IndexedDB
var fs2 = new FileSystem({ provider: new providers.IndexedDB() });
// Example 3: Use one of IndexedDB or WebSQL, whichever is supported
var fs3 = new FileSystem({ provider: new providers.Fallback() });
```
Every provider has an `isSupported()` method, which returns `true` if the browser supports this provider:
```javascript
if( Filer.FileSystem.providers.IndexedDB.isSupported() ) {
// IndexedDB provider will work in current environment...
if( Filer.FileSystem.providers.WebSQL.isSupported() ) {
// WebSQL provider will work in current environment...
}
```
You can also write your own provider if you need a different backend. See the code in `src/providers` for details.
A number of other providers have been written, including:
* node.js fs provider: https://github.com/humphd/filer-fs
* node.js Amazon S3 provider: https://github.com/alicoding/filer-s3
#### Filer.Buffer<a name="FilerBuffer"></a>
When reading and writing data, Filer follows node.js and uses [`Buffer`](http://nodejs.org/api/buffer.html).
@ -292,41 +212,16 @@ When in a node.js environment, native `Buffer`s can be used, or Filer.Buffer, wh
to node's `Buffer`. In a browser, you can use also use `Filer.Buffer`.
NOTE: a `Filer.Buffer` in a browser is really an augmented `Uint8Array` (i.e., the node `Buffer` api
methods are added to the instance). See https://github.com/feross/buffer for more details.
NOTE: `Filer.Buffer` currently includes the older, deprecated [constructor functions](https://nodejs.org/api/buffer.html#buffer_new_buffer_array), but these will be removed
at some point. You are encouraged to switch to use the newer class methods `Buffer.from()`
and `Buffer.alloc()`. See the [node.js Buffer docs](https://nodejs.org/api/buffer.html).
```js
/* Deprecated - see https://nodejs.org/api/buffer.html#buffer_new_buffer_array */
new Buffer(array)
new Buffer(arrayBuffer[, byteOffset[, length]])
new Buffer(buffer)
new Buffer(string[, encoding])
new Buffer(size)
/* Use Instead */
Buffer.from(array)
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(buffer)
Buffer.from(string[, encoding])
Buffer.alloc(size)
Buffer.allocUnsafe(size)
```
methods are added to the instance). See https://github.com/feross/buffer for more details. Additionally, unlike native `Buffer`, `Filer.Buffer`'s constructor can accept `ArrayBuffer` objects, which will be interpreted as `Uint8Array`s.
#### Filer.Path<a name="FilerPath"></a>
The node.js [path module](http://nodejs.org/api/path.html) is available via `Filer.path` or
`Filer.Path` (both are supported for historical reasons, and to match node). The Filer `path`
module is identical to the node.js version (see [https://github.com/browserify/path-browserify](https://github.com/browserify/path-browserify)), with the following differences:
* The CWD always defaults to `/`
* No support for Windows style paths (assume you are on a POSIX system)
* Additional utility methods (see below)
The node.js [path module](http://nodejs.org/api/path.html) is available via the `Filer.Path` object. It is
identical to the node.js version with the following differences:
* No notion of a current working directory in `resolve` (the root dir is used instead)
```javascript
var path = Filer.path;
var path = Filer.Path;
var dir = path.dirname('/foo/bar/baz/asdf/quux');
// dir is now '/foo/bar/baz/asdf'
@ -341,37 +236,16 @@ var newpath = path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
```
For more info see the docs in the [path module](http://nodejs.org/api/path.html) for a particular method:
* `path.normalize(p)` - NOTE: Filer.Path.normalize does *not* add a trailing slash
* `path.normalize(p)`
* `path.join([path1], [path2], [...])`
* `path.resolve([from ...], to)`
* `path.relative(from, to)`
* `path.dirname(p)`
* `path.basename(p, [ext])` - NOTE: Filer.Path.basename will return `'/'` vs. `''`
* `path.basename(p, [ext])`
* `path.extname(p)`
* `path.sep`
* `path.delimiter`
Filer.Path also includes the following extra methods:
* `isNull(p)` returns `true` or `false` if the path contains a null character (`'\u0000'`)
* `addTrailing(p)` returns the path `p` with a single trailing slash added
* `removeTrailing(p)` returns the path `p` with trailing slash(es) removed
[As with node.js](https://nodejs.org/api/fs.html#fs_file_paths), all methods below that
accept a `path` argument as a `String` can also take a [`file://` URL](https://nodejs.org/api/fs.html#fs_url_object_support)
or a `Buffer`. For example, all of the following cases will work the same way with Filer:
```js
// 1. path as a String
fs.writeFile('/dir/file.txt', 'data', function(err) {...});
// 2. path as a URL
fs.writeFile(new URL('file:///dir/file.txt'), 'data', function(err) {...});
// 3. path as a Buffer
fs.writeFile(Buffer.from('/dir/file.txt'), 'data', function(err) {...});
```
#### Filer.Errors<a name="Errors"></a>
The error objects used internally by Filer are also exposed via the `Filer.Errors` object. As much as possible
@ -414,11 +288,7 @@ Once a `FileSystem` is created, it has the following methods. NOTE: code example
a `FileSystem` instance named `fs` has been created like so:
```javascript
// 1. Using Filer.fs for a default filesystem
const { fs } = require('filer');
// 2. Or via the FileSystem constructor with specified options
const fs = new Filer.FileSystem(options, callback);
var fs = new Filer.FileSystem();
```
* [fs.rename(oldPath, newPath, callback)](#rename)
@ -436,8 +306,6 @@ const fs = new Filer.FileSystem(options, callback);
* [fs.mknod(path, mode, callback)](#mknod)
* [fs.rmdir(path, callback)](#rmdir)
* [fs.mkdir(path, [mode], callback)](#mkdir)
* [fs.access(path, [mode], callback)](#access)
* [fs.mkdtemp(path, [options], callback)](#mkdtemp)
* [fs.readdir(path, callback)](#readdir)
* [fs.close(fd, callback)](#close)
* [fs.open(path, flags, [mode], callback)](#open)
@ -487,7 +355,7 @@ Example:
```javascript
// Create a file, shrink it, expand it.
var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw error;
@ -518,7 +386,7 @@ Example:
```javascript
// Create a file, shrink it, expand it.
var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw error;
@ -554,12 +422,9 @@ Callback gets `(error, stats)`, where `stats` is an object with the following pr
name: <string> // the entry's name (basename)
size: <number> // file size in bytes
nlinks: <number> // number of links
atime: <date> // last access time as JS Date Object
mtime: <date> // last modified time as JS Date Object
ctime: <date> // creation time as JS Date Object
atimeMs: <number> // last access time as Unix Timestamp
mtimeMs: <number> // last modified time as Unix Timestamp
ctimeMs: <number> // creation time as Unix Timestamp
atime: <number> // last access time
mtime: <number> // last modified time
ctime: <number> // creation time
type: <string> // file type (FILE, DIRECTORY, SYMLINK),
gid: <number> // group name
uid: <number> // owner name
@ -696,12 +561,10 @@ Create a symbolic link to the file at `dstPath` containing the path `srcPath`. A
Symbolic links are files that point to other paths.
NOTE: Filer allows for, but ignores the optional `type` parameter used in node.js.
The `srcPath` may be a relative path, which will be resolved relative to `dstPath`
Example:
```javascript
// Absolute path
fs.symlink('/logs/august.log', '/logs/current', function(err) {
if(err) throw err;
fs.readFile('/logs/current', 'utf8', function(err, data) {
@ -709,21 +572,11 @@ fs.symlink('/logs/august.log', '/logs/current', function(err) {
var currentLog = data;
});
});
// Relative path
fs.symlink('../file', '/dir/symlink', function(err) {
if(err) throw err;
// The /dir/symlink file is now a symlink to /file
});
```
#### fs.readlink(path, callback)<a name="readlink"></a>
Reads the contents of a symbolic link. Asynchronous [readlink(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/readlink.html).
Callback gets `(error, linkContents)`, where `linkContents` is a string
containing the symbolic link's link path. If the original `srcPath` given
to `symlink()` was a relative path, it will be fully resolved relative
to `dstPath` when returned by `readlink()`.
Reads the contents of a symbolic link. Asynchronous [readlink(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/readlink.html). Callback gets `(error, linkContents)`, where `linkContents` is a string containing the symbolic link's link path.
Example:
@ -820,47 +673,7 @@ fs.mkdir('/home', function(err) {
});
```
#### fs.access(path, [mode], callback)<a name="access"></a>
Tests a user's permissions for the file or directory supplied in `path` argument. Asynchronous [access(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/access.html). Callback gets no additional arguments. The `mode` argument can be one of the following (constants are available on `fs.constants` and `fs`):
* `F_OK`: Test for existence of file.
* `R_OK`: Test whether the file exists and grants read permission.
* `W_OK`: Test whether the file exists and grants write permission.
* `X_OK`: Test whether the file exists and grants execute permission.
NOTE: you can also create a mask consisting of the bitwise OR of two or more values (e.g. `fs.constants.W_OK | fs.constants.R_OK`).
Example:
```javascript
// Check if the file exists in the current directory.
fs.access(file, fs.F_OK, function(err) {
console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
});
```
#### fs.mkdtemp(prefix, options, callback)<a name="mkdtemp"></a>
Makes a temporary directory with prefix supplied in `path` argument. Method will append six random characters directly to the prefix. Asynchronous. Callback gets `(error, path)`, where path is the path to the created directory.
NOTE: Filer allows for, but ignores the optional `options` argument used in node.js.
Example:
```javascript
// Create tmp directory with prefix foo
fs.mkdtemp("/foo-", function (error, path) {
// A new folder foo-xxxxxx will be created. Path contains a path to created folder.
});
fs.mkdtemp("/myDir/tmp", function (error, path) {
// Will create a new folder tmpxxxxxx inside myDir directory.
// Will throw error if myDir does not exist
});
```
#### fs.readdir(path, [options], callback)<a name="readdir"></a>
#### fs.readdir(path, callback)<a name="readdir"></a>
Reads the contents of a directory. Asynchronous [readdir(3)](http://pubs.opengroup.org/onlinepubs/009695399/functions/readdir.html).
Callback gets `(error, files)`, where `files` is an array containing the names of each directory entry (i.e., file, directory, link) in the directory, excluding `.` and `..`.
@ -881,12 +694,6 @@ fs.readdir('/docs', function(err, files) {
});
```
Optionally accepts an options parameter, which can be either an encoding (e.g. "utf8") or an object with optional properties `encoding` and `withFileTypes`.
The `encoding` property is a `string` which will determine the character encoding to use for the names of each directory entry. The `withFileTypes` property is a `boolean` which defaults to `false`. If `true`, this method will return an array of [fs.Dirent](https://nodejs.org/api/fs.html#fs_class_fs_dirent) objects.
The `name` property on the [fs.Dirent](https://nodejs.org/api/fs.html#fs_class_fs_dirent) objects will be encoded using the specified character encoding.
#### fs.close(fd, callback)<a name="close"></a>
Closes a file descriptor. Asynchronous [close(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/close.html).
@ -934,7 +741,7 @@ fs.open('/myfile', 'w', function(err, fd) {
#### fs.utimes(path, atime, mtime, callback)<a name="utimes"></a>
Changes the file timestamps for the file given at path `path`. Asynchronous [utimes(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/utimes.html). Callback gets no additional arguments. Both `atime` (access time) and `mtime` (modified time) arguments should be a JavaScript Date or Number.
Changes the file timestamps for the file given at path `path`. Asynchronous [utimes(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/utimes.html). Callback gets no additional arguments. Both `atime` (access time) and `mtime` (modified time) arguments should be a JavaScript Date.
Example:
@ -948,7 +755,7 @@ fs.utimes('/myfile.txt', now, now, function(err) {
#### fs.futimes(fd, atime, mtime, callback)<a name="futimes"></a>
Changes the file timestamps for the open file represented by the file descriptor `fd`. Asynchronous [utimes(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/utimes.html). Callback gets no additional arguments. Both `atime` (access time) and `mtime` (modified time) arguments should be a JavaScript Date or Number.
Changes the file timestamps for the open file represented by the file descriptor `fd`. Asynchronous [utimes(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/utimes.html). Callback gets no additional arguments. Both `atime` (access time) and `mtime` (modified time) arguments should be a JavaScript Date.
Example:
@ -1052,22 +859,7 @@ fs.open('/myfile.txt', function(err, fd) {
#### fs.fsync(fd, callback)<a name="fsync"></a>
Synchronize the data and metadata for the file referred to by `fd` to disk.
Asynchronous [fsync(2)](http://man7.org/linux/man-pages/man2/fsync.2.html).
The callback gets `(error)`.
```js
fs.open('/myfile', 'r', function(error, fd) {
if(err) throw err;
// Use fd, then sync
fs.fsync(fd, function(error) {
if(err) throw err;
fs.close(fd, done);
});
});
```
NOTE: Not yet implemented, see https://github.com/filerjs/filer/issues/87
#### fs.write(fd, buffer, offset, length, position, callback)<a name="write"></a>
@ -1079,7 +871,7 @@ Example:
```javascript
// Create a file with the following bytes.
var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw error;
@ -1116,22 +908,22 @@ Example:
```javascript
fs.open('/myfile', 'r', function(err, fd) {
if(err) throw err;
if(err) throw error;
// Determine size of file
fs.fstat(fd, function(err, stats) {
if(err) throw err;
if(err) throw error;
// Create a buffer large enough to hold the file's contents
var nbytes = expected = stats.size;
var buffer = Filer.Buffer.alloc(nbytes);
var buffer = new Filer.Buffer(nbytes);
var read = 0;
function readBytes(offset, position, length) {
length = length || buffer.length - read;
fs.read(fd, buffer, offset, length, position, function(err, nbytes) {
if(err) throw err;
if(err) throw error;
// nbytes is now the number of bytes read, between 0 and buffer.length.
// See if we still have more bytes to read.
@ -1182,7 +974,7 @@ fs.writeFile('/myfile.txt', "...data...", function (err) {
});
// Write binary file
var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
fs.writeFile('/myfile', buffer, function (err) {
if (err) throw err;
});
@ -1205,8 +997,8 @@ fs.appendFile('/myfile.txt', "Data...", function (err) {
// '/myfile.txt' would now read out 'More...Data...'
// Append binary file
var data = Filer.Buffer.from([1, 2, 3, 4]);
var more = Filer.Buffer.from([5, 6, 7, 8]);
var data = new Filer.Buffer([1, 2, 3, 4]);
var more = new Filer.Buffer([5, 6, 7, 8]);
fs.writeFile('/myfile', data, function (err) {
if (err) throw err;

5
config/environment.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = (function() {
var habitat = require('habitat');
habitat.load();
return new habitat();
})();

1800
dist/buffer.js vendored Normal file

File diff suppressed because it is too large Load Diff

2
dist/buffer.min.js vendored Normal file

File diff suppressed because one or more lines are too long

14641
dist/filer.js vendored

File diff suppressed because it is too large Load Diff

1
dist/filer.js.map vendored

File diff suppressed because one or more lines are too long

2
dist/filer.map vendored

File diff suppressed because one or more lines are too long

287
dist/filer.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/filer.min.map vendored

File diff suppressed because one or more lines are too long

243
dist/path.js vendored Normal file
View File

@ -0,0 +1,243 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Path = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
// Based on https://github.com/joyent/node/blob/41e53e557992a7d552a8e23de035f9463da25c99/lib/path.js
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last === '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
var splitPathRe =
/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/;
var splitPath = function(filename) {
var result = splitPathRe.exec(filename);
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
};
// path.resolve([from ...], to)
function resolve() {
var resolvedPath = '',
resolvedAbsolute = false;
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
// XXXfiler: we don't have process.cwd() so we use '/' as a fallback
var path = (i >= 0) ? arguments[i] : '/';
// Skip empty and invalid entries
if (typeof path !== 'string' || !path) {
continue;
}
resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path.charAt(0) === '/';
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
return !!p;
}), !resolvedAbsolute).join('/');
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
}
// path.normalize(path)
function normalize(path) {
var isAbsolute = path.charAt(0) === '/',
trailingSlash = path.substr(-1) === '/';
// Normalize the path
path = normalizeArray(path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
/*
if (path && trailingSlash) {
path += '/';
}
*/
return (isAbsolute ? '/' : '') + path;
}
function join() {
var paths = Array.prototype.slice.call(arguments, 0);
return normalize(paths.filter(function(p, index) {
return p && typeof p === 'string';
}).join('/'));
}
// path.relative(from, to)
function relative(from, to) {
from = resolve(from).substr(1);
to = resolve(to).substr(1);
function trim(arr) {
var start = 0;
for (; start < arr.length; start++) {
if (arr[start] !== '') break;
}
var end = arr.length - 1;
for (; end >= 0; end--) {
if (arr[end] !== '') break;
}
if (start > end) return [];
return arr.slice(start, end - start + 1);
}
var fromParts = trim(from.split('/'));
var toParts = trim(to.split('/'));
var length = Math.min(fromParts.length, toParts.length);
var samePartsLength = length;
for (var i = 0; i < length; i++) {
if (fromParts[i] !== toParts[i]) {
samePartsLength = i;
break;
}
}
var outputParts = [];
for (var i = samePartsLength; i < fromParts.length; i++) {
outputParts.push('..');
}
outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('/');
}
function dirname(path) {
var result = splitPath(path),
root = result[0],
dir = result[1];
if (!root && !dir) {
// No dirname whatsoever
return '.';
}
if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
}
return root + dir;
}
function basename(path, ext) {
var f = splitPath(path)[2];
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
// XXXfiler: node.js just does `return f`
return f === "" ? "/" : f;
}
function extname(path) {
return splitPath(path)[3];
}
function isAbsolute(path) {
if(path.charAt(0) === '/') {
return true;
}
return false;
}
function isNull(path) {
if (('' + path).indexOf('\u0000') !== -1) {
return true;
}
return false;
}
// Make sure we don't double-add a trailing slash (e.g., '/' -> '//')
function addTrailing(path) {
return path.replace(/\/*$/, '/');
}
// Deal with multiple slashes at the end, one, or none
// and make sure we don't return the empty string.
function removeTrailing(path) {
path = path.replace(/\/*$/, '');
return path === '' ? '/' : path;
}
// XXXfiler: we don't support path.exists() or path.existsSync(), which
// are deprecated, and need a FileSystem instance to work. Use fs.stat().
module.exports = {
normalize: normalize,
resolve: resolve,
join: join,
relative: relative,
sep: '/',
delimiter: ':',
dirname: dirname,
basename: basename,
extname: extname,
isAbsolute: isAbsolute,
isNull: isNull,
// Non-node but useful...
addTrailing: addTrailing,
removeTrailing: removeTrailing
};
},{}]},{},[1])(1)
});

2
dist/path.min.js vendored Normal file
View File

@ -0,0 +1,2 @@
/*! filer 0.0.44 2018-06-25 */
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.Path=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c||a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){function d(a,b){for(var c=0,d=a.length-1;d>=0;d--){var e=a[d];"."===e?a.splice(d,1):".."===e?(a.splice(d,1),c++):c&&(a.splice(d,1),c--)}if(b)for(;c--;c)a.unshift("..");return a}function e(){for(var a="",b=!1,c=arguments.length-1;c>=-1&&!b;c--){var e=c>=0?arguments[c]:"/";"string"==typeof e&&e&&(a=e+"/"+a,b="/"===e.charAt(0))}return a=d(a.split("/").filter(function(a){return!!a}),!b).join("/"),(b?"/":"")+a||"."}function f(a){var b="/"===a.charAt(0);a.substr(-1);return a=d(a.split("/").filter(function(a){return!!a}),!b).join("/"),a||b||(a="."),(b?"/":"")+a}function g(){return f(Array.prototype.slice.call(arguments,0).filter(function(a,b){return a&&"string"==typeof a}).join("/"))}function h(a,b){function c(a){for(var b=0;b<a.length&&""===a[b];b++);for(var c=a.length-1;c>=0&&""===a[c];c--);return b>c?[]:a.slice(b,c-b+1)}a=e(a).substr(1),b=e(b).substr(1);for(var d=c(a.split("/")),f=c(b.split("/")),g=Math.min(d.length,f.length),h=g,i=0;i<g;i++)if(d[i]!==f[i]){h=i;break}for(var j=[],i=h;i<d.length;i++)j.push("..");return j=j.concat(f.slice(h)),j.join("/")}function i(a){var b=q(a),c=b[0],d=b[1];return c||d?(d&&(d=d.substr(0,d.length-1)),c+d):"."}function j(a,b){var c=q(a)[2];return b&&c.substr(-1*b.length)===b&&(c=c.substr(0,c.length-b.length)),""===c?"/":c}function k(a){return q(a)[3]}function l(a){return"/"===a.charAt(0)}function m(a){return-1!==(""+a).indexOf("\0")}function n(a){return a.replace(/\/*$/,"/")}function o(a){return a=a.replace(/\/*$/,""),""===a?"/":a}var p=/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/,q=function(a){var b=p.exec(a);return[b[1]||"",b[2]||"",b[3]||"",b[4]||""]};b.exports={normalize:f,resolve:e,join:g,relative:h,sep:"/",delimiter:":",dirname:i,basename:j,extname:k,isAbsolute:l,isNull:m,addTrailing:n,removeTrailing:o}},{}]},{},[1])(1)});

View File

@ -1,9 +1,14 @@
###
# Dev ENVIRONMENT file
#
# Copy to .env to use defaults when releasing via `npm release`
# Copy to .env to use defaults
###
# GitHub Personal Access Token (to push releases)
# https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
GITHUB_TOKEN=
# GIT (upstream) url to publish to
export FILER_UPSTREAM_URI="git@github.com:js-platform/filer.git"
# GIT (upstream) branch to publish to
export FILER_UPSTREAM_BRANCH="develop"
# Remote name for upstream repo
export FILER_UPSTREAM_REMOTE_NAME="origin"

385
gruntfile.js Normal file
View File

@ -0,0 +1,385 @@
var semver = require('semver'),
fs = require('fs'),
currentVersion = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version,
env = require('./config/environment');
// Globals
var PROMPT_CONFIRM_CONFIG = 'confirmation',
GIT_BRANCH = env.get('FILER_UPSTREAM_BRANCH'),
GIT_REMOTE = env.get('FILER_UPSTREAM_REMOTE_NAME'),
GIT_FULL_REMOTE = env.get('FILER_UPSTREAM_URI') + ' ' + GIT_BRANCH;
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: ['dist/filer-test.js', 'dist/filer-issue225.js', 'dist/filer-perf.js'],
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= pkg.version %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
develop: {
src: 'dist/filer.js',
dest: 'dist/filer.min.js'
},
path: {
src: 'dist/path.js',
dest: 'dist/path.min.js'
},
buffer: {
src: 'dist/buffer.js',
dest: 'dist/buffer.min.js'
}
},
jshint: {
// Don't bother with src/path.js
all: [
'gruntfile.js',
'config/environment.js',
'src/constants.js',
'src/errors.js',
'src/fs.js',
'src/index.js',
'src/shared.js',
'src/shell.js',
'src/fswatcher.js',
'src/environment.js',
'src/providers/**/*.js',
'src/adapters/**/*.js',
'src/directory-entry.js',
'src/open-file-description.js',
'src/super-node.js',
'src/node.js',
'src/stats.js',
'src/filesystem/**/*.js'
]
},
browserify: {
filerDist: {
src: "./src/index.js",
dest: "./dist/filer.js",
options: {
browserifyOptions: {
commondir: false,
builtins: ["buffer", "path", "url", "punycode", "querystring"],
insertGlobalVars: {
// This ensures that process won't be defined, since
// browserify will do so automatically if any globals
// are requested by us or detected by browserify.
process: function() {
return undefined;
}
},
standalone: 'Filer'
},
exclude: ["./node_modules/request/index.js"]
}
},
filerPerf: {
src: "./perf/index.js",
dest: "./dist/filer-perf.js",
options: {
browserifyOptions: {
commondir: false,
builtins: ["buffer", "path", "url", "punycode", "querystring"],
insertGlobalVars: {
// This ensures that process won't be defined, since
// browserify will do so automatically if any globals
// are requested by us or detected by browserify.
process: function() {
return undefined;
}
},
standalone: 'Filer'
}
}
},
filerTest: {
src: "./tests/index.js",
dest: "./dist/filer-test.js",
options: {
browserifyOptions: {
commondir: false,
builtins: ["buffer", "path", "url", "punycode", "querystring"],
insertGlobalVars: {
// This ensures that process won't be defined, since
// browserify will do so automatically if any globals
// are requested by us or detected by browserify.
process: function() {
return undefined;
}
}
}
}
},
// See tests/bugs/issue225.js
filerIssue225: {
src: "./src/index.js",
dest: "./dist/filer-issue225.js",
options: {
browserifyOptions: {
commondir: false,
builtins: ["buffer", "path", "url", "punycode", "querystring"],
insertGlobalVars: {
// This ensures that process won't be defined, since
// browserify will do so automatically if any globals
// are requested by us or detected by browserify.
process: function() {
return undefined;
}
},
standalone: 'Filer'
}
}
},
// For low-cost access to filer's `Path` and `buffer` modules
filerPath: {
src: "./src/path.js",
dest: "./dist/path.js",
options: {
browserifyOptions: {
standalone: 'Path'
}
}
},
filerBuffer: {
src: "./src/buffer.js",
dest: "./dist/buffer.js",
options: {
browserifyOptions: {
standalone: 'FilerBuffer'
}
}
}
},
shell: {
mocha: {
// Run all tests (e.g., tests require()'ed in tests/index.js) and also tests/bugs/issue225.js
// separately, since it can't be included in a browserify build.
command: '"./node_modules/.bin/mocha" --reporter list tests/index.js && "./node_modules/.bin/mocha" --reporter list tests/bugs/issue225.js'
}
},
bump: {
options: {
files: ['package.json', 'bower.json'],
commit: true,
commitMessage: 'v%VERSION%',
commitFiles: [
'package.json',
'bower.json',
'./dist/filer.js',
'./dist/filer.min.js',
'./dist/buffer.js',
'./dist/buffer.min.js',
'./dist/path.js',
'./dist/path.min.js'
],
createTag: true,
tagName: 'v%VERSION%',
tagMessage: 'v%VERSION%',
push: true,
pushTo: GIT_FULL_REMOTE
}
},
'npm-checkBranch': {
options: {
branch: GIT_BRANCH
}
},
'npm-publish': {
options: {
abortIfDirty: false
}
},
prompt: {
confirm: {
options: {
questions: [
{
config: PROMPT_CONFIRM_CONFIG,
type: 'confirm',
message: 'Bump version from ' + (currentVersion).cyan +
' to ' + semver.inc(currentVersion, "patch").yellow + '?',
default: false
}
],
then: function(results) {
if (!results[PROMPT_CONFIRM_CONFIG]) {
return grunt.fatal('User aborted...');
}
}
}
}
},
gitcheckout: {
publish: {
options: {
branch: 'gh-pages',
force: true
}
},
revert: {
options: {
branch: GIT_BRANCH
}
}
},
gitpush: {
publish: {
options: {
remote: GIT_REMOTE,
branch: 'gh-pages',
force: true
},
}
},
gitcommit: {
publish: {
options: {
noStatus: true
}
}
},
gitadd: {
publish: {
files: {
src: ['./dist/filer-test.js', './dist/filer-perf.js']
}
}
},
gitstash: {
publish: {
},
pop: {
options: {
command: "pop"
}
}
},
gitrm: {
publish: {
options: {
force: true
},
files: {
src: ['./dist/filer-test.js', './dist/filer-perf.js']
}
}
},
connect: {
serverForBrowser: {
options: {
port: 1234,
base: './',
keepalive: true
}
}
},
usebanner: {
publish: {
options: {
position: "top"
},
files: {
src: ['./dist/filer-test.js', './dist/filer-perf.js']
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-bump');
grunt.loadNpmTasks('grunt-npm');
grunt.loadNpmTasks('grunt-git');
grunt.loadNpmTasks('grunt-prompt');
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-banner');
grunt.registerTask('develop', [
'browserify:filerDist',
'browserify:filerPath',
'browserify:filerBuffer',
'uglify:develop',
'uglify:path',
'uglify:buffer'
]);
grunt.registerTask('build-tests', ['clean', 'browserify:filerTest', 'browserify:filerPerf', 'browserify:filerIssue225']);
grunt.registerTask('release', ['test', 'develop']);
grunt.registerTask('publish', 'Publish filer as a new version to NPM, bower and github.', function(patchLevel) {
var allLevels = ['patch', 'minor', 'major'];
// No level specified defaults to 'patch'
patchLevel = (patchLevel || 'patch').toLowerCase();
// Fail out if the patch level isn't recognized
if (allLevels.filter(function(el) { return el == patchLevel; }).length === 0) {
return grunt.fatal('Patch level not recognized! "Patch", "minor" or "major" only.');
}
// Set prompt message
var promptOpts = grunt.config('prompt.confirm.options');
promptOpts.questions[0].message = 'Bump version from ' + (currentVersion).cyan +
' to ' + semver.inc(currentVersion, patchLevel).yellow + '?';
grunt.config('prompt.confirm.options', promptOpts);
// Store the new version in the gh-pages commit message
var ghPagesMessage = 'Tests for Filer v' + semver.inc(currentVersion, patchLevel);
grunt.config('gitcommit.publish.options.message', ghPagesMessage);
// Store the new version as a banner in the test file
// NOTE: This is a hack intended to ensure that this build process
// succeeds even if no changes were made to the tests
// before publishing a new version. Otherwise, the automatic
// commit + push to github pages would break a normal build
var bannerMsg = "/* Test file for filerjs v" + semver.inc(currentVersion, patchLevel) + "*/";
grunt.config('usebanner.publish.options.banner', bannerMsg);
grunt.task.run([
'prompt:confirm',
'checkBranch',
'release',
'bump:' + patchLevel,
'build-tests',
'usebanner:publish',
'gitadd:publish',
'gitstash:publish',
'gitcheckout:publish',
'gitrm:publish',
'gitstash:pop',
'gitcommit:publish',
'gitpush:publish',
'gitcheckout:revert',
'npm-publish'
]);
});
grunt.registerTask('test-node', ['jshint', 'browserify:filerIssue225', 'shell:mocha']);
grunt.registerTask('test-browser', ['jshint', 'build-tests', 'connect:serverForBrowser']);
grunt.registerTask('test', ['test-node']);
grunt.registerTask('default', ['test']);
};

View File

@ -1,28 +1,18 @@
module.exports = function(config) {
config.set({
browsers: ['ChromeHeadless'],
singleRun: true,
basePath: '',
files: [
'node_modules/regenerator-runtime/runtime.js',
'tests/dist/index.js'
],
files: ['tests/dist/index.js'],
frameworks: ['mocha', 'chai'],
reporters: ['mocha', 'summary'],
reporters: ['mocha'],
client: {
captureConsole: true,
mocha: {
ui: 'bdd',
timeout: 5000,
slow: 250
}
},
summaryReporter: {
// 'failed', 'skipped' or 'all'
show: 'failed',
// Limit the spec label to this length
specLength: 50,
// Show an 'all' column as a summary
overviewColumn: true
}
});
};

97
lib/nodash.js Normal file
View File

@ -0,0 +1,97 @@
// Cherry-picked bits of underscore.js, lodash.js
/**
* Lo-Dash 2.4.0 <http://lodash.com/>
* Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
* Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Available under MIT license <http://lodash.com/license>
*/
var ArrayProto = Array.prototype;
var nativeForEach = ArrayProto.forEach;
var nativeIndexOf = ArrayProto.indexOf;
var nativeSome = ArrayProto.some;
var ObjProto = Object.prototype;
var hasOwnProperty = ObjProto.hasOwnProperty;
var nativeKeys = Object.keys;
var breaker = {};
function has(obj, key) {
return hasOwnProperty.call(obj, key);
}
var keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var keys = [];
for (var key in obj) if (has(obj, key)) keys.push(key);
return keys;
};
function size(obj) {
if (obj == null) return 0;
return (obj.length === +obj.length) ? obj.length : keys(obj).length;
}
function identity(value) {
return value;
}
function each(obj, iterator, context) {
var i, length;
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (i = 0, length = obj.length; i < length; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
var keys = keys(obj);
for (i = 0, length = keys.length; i < length; i++) {
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
}
};
function any(obj, iterator, context) {
iterator || (iterator = identity);
var result = false;
if (obj == null) return result;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
each(obj, function(value, index, list) {
if (result || (result = iterator.call(context, value, index, list))) return breaker;
});
return !!result;
};
function contains(obj, target) {
if (obj == null) return false;
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
return any(obj, function(value) {
return value === target;
});
};
function Wrapped(value) {
this.value = value;
}
Wrapped.prototype.has = function(key) {
return has(this.value, key);
};
Wrapped.prototype.contains = function(target) {
return contains(this.value, target);
};
Wrapped.prototype.size = function() {
return size(this.value);
};
function nodash(value) {
// don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
return (value && typeof value == 'object' && !Array.isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
? value
: new Wrapped(value);
}
module.exports = nodash;

16148
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,12 +5,13 @@
"fs",
"node",
"file",
"filesystem",
"system",
"browser",
"indexeddb",
"idb"
"idb",
"websql"
],
"version": "1.4.1",
"version": "0.0.44",
"author": "Alan K <ack@modeswitch.org> (http://blog.modeswitch.org)",
"homepage": "http://filerjs.github.io/filer",
"bugs": "https://github.com/filerjs/filer/issues",
@ -20,76 +21,34 @@
"eslint:fix": "npm run lint:fix",
"lint": "eslint src tests",
"lint:fix": "eslint --fix src tests",
"test:node": "mocha --timeout 5000 tests",
"pretest:node-debug": "echo \"Open Chrome to chrome://inspect to debug tests...\"",
"test:node-debug": "mocha --timeout 5000 --inspect-brk tests",
"test:manual": "parcel tests/index.html --out-dir tests/dist",
"test:migrations": "mocha tests/filesystems/migrations",
"pretest": "npm run lint",
"test": "npm run karma-mocha",
"posttest": "npm run test:migrations",
"prebuild": "parcel build --global Filer src/index.js --no-minify --out-file filer.js",
"build": "parcel build --global Filer src/index.js --out-file filer.min.js --detailed-report",
"build-tests": "parcel build tests/index.js --no-source-maps --out-dir tests/dist",
"prekarma-mocha-firefox": "npm run build-tests",
"karma-mocha-firefox": "karma start karma.conf.js --browsers FirefoxHeadless",
"prekarma-mocha-chrome": "npm run build-tests",
"karma-mocha-chrome": "karma start karma.conf.js --browsers ChromeHeadless",
"prekarma-mocha": "npm run build-tests",
"karma-mocha": "karma start karma.conf.js --browsers ChromeHeadless,FirefoxHeadless",
"coverage": "nyc mocha tests/index.js",
"release": "run.env release-it"
"prekarma-mocha": "parcel build tests/index.js --no-source-maps --out-dir tests/dist",
"karma-mocha": "karma start karma.conf.js"
},
"repository": {
"type": "git",
"url": "https://github.com/filerjs/filer.git"
},
"dependencies": {
"es6-promisify": "^7.0.0",
"minimatch": "^3.0.4",
"schema-utils": "^3.1.1"
"base64-arraybuffer": "^0.1.5",
"es6-promisify": "^6.0.0",
"minimatch": "^3.0.4"
},
"devDependencies": {
"regenerator-runtime": "^0.13.9",
"chai": "^4.3.4",
"chai-datetime": "^1.8.0",
"eslint": "^7.32.0",
"fake-indexeddb": "^3.1.7",
"karma": "^6.3.8",
"chai": "^4.1.2",
"eslint": "^5.0.1",
"karma": "^2.0.4",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-firefox-launcher": "^2.1.2",
"karma-mocha": "^2.0.1",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"karma-summary-reporter": "^3.0.0",
"meow": "^10.0.1",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"parcel-bundler": "^1.12.5",
"pretty-bytes": "^5.6.0",
"release-it": "^14.11.6",
"run.env": "^1.1.0",
"unused-filename": "^3.0.1",
"walk": "^2.3.15"
"mocha": "^5.2.0",
"parcel-bundler": "^1.9.6"
},
"main": "./src/index.js",
"browser": "./dist/filer.min.js",
"files": [
"src",
"lib",
"dist",
"shims",
"webpack"
],
"nyc": {
"exclude": [
"tests/**/*.js",
"lib/**/*.js",
"src/providers/**/*.js"
],
"reporter": [
"lcov",
"text"
]
}
"browser": "./dist/filer.min.js"
}

View File

@ -1,65 +0,0 @@
const { FileSystem } = require('../src/index');
let Provider;
try {
Provider = require('fsProvider');
}
catch (err) {
Provider = require('./providers/default');
}
const provider = new Provider();
let onFsReady;
let onFsError;
let fsReady = new Promise((resolve, reject) => {
onFsReady = resolve;
onFsError = reject;
});
var fsInstance = new FileSystem({ provider }, (err) => {
if (err) {
onFsError(err);
} else {
onFsReady(true);
}
});
function proxyHasProp(target, prop) {
return prop in target;
}
const fsPromises = new Proxy(fsInstance.promises, {
get(target, prop) {
if (!proxyHasProp(target, prop)) {
return;
}
return async (...args) => {
await fsReady;
return await target[prop](...args);
};
},
});
const fs = new Proxy(fsInstance, {
get(target, prop) {
if (!proxyHasProp(target, prop)) {
return;
}
if (prop === 'promises') {
return fsPromises;
}
return (...args) => {
(async () => {
await fsReady;
target[prop](...args);
})();
};
},
});
module.exports = fs;

View File

@ -1,3 +0,0 @@
const { path } = require('../src/index');
module.exports = path;

View File

@ -1,2 +0,0 @@
const { Default } = require('../../src/providers/index');
module.exports = Default;

View File

@ -1,2 +0,0 @@
const IndexedDB = require('../../src/providers/indexeddb');
module.exports = IndexedDB;

View File

@ -1,2 +0,0 @@
const Memory = require('../../src/providers/memory');
module.exports = Memory;

23
src/buffer.js Normal file
View File

@ -0,0 +1,23 @@
function FilerBuffer (subject, encoding, nonZero) {
// Automatically turn ArrayBuffer into Uint8Array so that underlying
// Buffer code doesn't just throw away and ignore ArrayBuffer data.
if (subject instanceof ArrayBuffer) {
subject = new Uint8Array(subject);
}
return new Buffer(subject, encoding, nonZero);
}
// Inherit prototype from Buffer
FilerBuffer.prototype = Object.create(Buffer.prototype);
FilerBuffer.prototype.constructor = FilerBuffer;
// Also copy static methods onto FilerBuffer ctor
Object.keys(Buffer).forEach(function (p) {
if (Buffer.hasOwnProperty(p)) {
FilerBuffer[p] = Buffer[p];
}
});
module.exports = FilerBuffer;

View File

@ -24,6 +24,9 @@ module.exports = {
NODE_TYPE_SYMBOLIC_LINK: 'SYMLINK',
NODE_TYPE_META: 'META',
S_IFREG: 0x8000,
S_IFDIR: 0x4000,
S_IFLNK: 0xA000,
DEFAULT_DIR_PERMISSIONS: 0x1ED, // 755
DEFAULT_FILE_PERMISSIONS: 0x1A4, // 644

View File

@ -1,12 +0,0 @@
'use strict';
const Stats = require('./stats.js');
function Dirent(path, fileNode, devName) {
this.constructor = Dirent;
Stats.call(this, path, fileNode, devName);
}
Dirent.prototype = Stats.prototype;
module.exports = Dirent;

13
src/encoding.js Normal file
View File

@ -0,0 +1,13 @@
// Adapt encodings to work with Buffer or Uint8Array, they expect the latter
function decode(buf) {
return buf.toString('utf8');
}
function encode(string) {
return new Buffer(string, 'utf8');
}
module.exports = {
encode: encode,
decode: decode
};

View File

@ -7,7 +7,7 @@ var errors = {};
//'0:OK:success',
//'1:EOF:end of file',
//'2:EADDRINFO:getaddrinfo error',
'3:EACCES:permission denied',
//'3:EACCES:permission denied',
//'4:EAGAIN:resource temporarily unavailable',
//'5:EADDRINUSE:address already in use',
//'6:EADDRNOTAVAIL:address not available',

File diff suppressed because it is too large Load Diff

View File

@ -1,43 +1,40 @@
'use strict';
var _ = require('../../lib/nodash.js');
var { promisify } = require('es6-promisify');
const { promisify } = require('es6-promisify');
var isNullPath = require('../path.js').isNull;
var nop = require('../shared.js').nop;
const Path = require('../path.js');
var Constants = require('../constants.js');
var FILE_SYSTEM_NAME = Constants.FILE_SYSTEM_NAME;
var FS_FORMAT = Constants.FS_FORMAT;
var FS_READY = Constants.FS_READY;
var FS_PENDING = Constants.FS_PENDING;
var FS_ERROR = Constants.FS_ERROR;
var FS_NODUPEIDCHECK = Constants.FS_NODUPEIDCHECK;
const providers = require('../providers/index.js');
var providers = require('../providers/index.js');
const Shell = require('../shell/shell.js');
const Intercom = require('../../lib/intercom.js');
const FSWatcher = require('../fs-watcher.js');
const Errors = require('../errors.js');
const {
nop,
guid: defaultGuidFn
} = require('../shared.js');
var Shell = require('../shell/shell.js');
var Intercom = require('../../lib/intercom.js');
var FSWatcher = require('../fs-watcher.js');
var Errors = require('../errors.js');
var defaultGuidFn = require('../shared.js').guid;
const {
fsConstants,
FILE_SYSTEM_NAME,
FS_FORMAT,
FS_READY,
FS_PENDING,
FS_ERROR,
FS_NODUPEIDCHECK,
STDIN,
STDOUT,
STDERR
} = require('../constants.js');
var STDIN = Constants.STDIN;
var STDOUT = Constants.STDOUT;
var STDERR = Constants.STDERR;
var FIRST_DESCRIPTOR = Constants.FIRST_DESCRIPTOR;
// The core fs operations live on impl
const impl = require('./implementation.js');
var impl = require('./implementation.js');
// node.js supports a calling pattern that leaves off a callback.
function maybeCallback(callback) {
if (typeof callback === 'function') {
if(typeof callback === 'function') {
return callback;
}
return function (err) {
if (err) {
return function(err) {
if(err) {
throw err;
}
};
@ -45,66 +42,11 @@ function maybeCallback(callback) {
// Default callback that logs an error if passed in
function defaultCallback(err) {
if (err) {
if(err) {
/* eslint no-console: 0 */
console.error('Filer error: ', err);
}
}
// Get a path (String) from a file:// URL. Support URL() like objects
// https://github.com/nodejs/node/blob/968e901aff38a343b1de4addebf79fd8fa991c59/lib/internal/url.js#L1381
function toPathIfFileURL(fileURLOrPath) {
if (!(fileURLOrPath &&
fileURLOrPath.protocol &&
fileURLOrPath.pathname)) {
return fileURLOrPath;
}
if (fileURLOrPath.protocol !== 'file:') {
throw new Errors.EINVAL('only file: URLs are supported for paths', fileURLOrPath);
}
const pathname = fileURLOrPath.pathname;
for (let n = 0; n < pathname.length; n++) {
if (pathname[n] === '%') {
const third = pathname.codePointAt(n + 2) | 0x20;
if (pathname[n + 1] === '2' && third === 102) {
throw new Errors.EINVAL('file: URLs must not include encoded / characters', fileURLOrPath);
}
}
}
return decodeURIComponent(pathname);
}
// Allow Buffers for paths. Assumes we want UTF8.
function toPathIfBuffer(bufferOrPath) {
return Buffer.isBuffer(bufferOrPath) ? bufferOrPath.toString() : bufferOrPath;
}
function validatePath(path, allowRelative) {
if (!path) {
return new Errors.EINVAL('Path must be a string', path);
} else if (Path.isNull(path)) {
return new Errors.EINVAL('Path must be a string without null bytes.', path);
} else if (!allowRelative && !Path.isAbsolute(path)) {
return new Errors.EINVAL('Path must be absolute.', path);
}
}
function processPathArg(args, idx, allowRelative) {
let path = args[idx];
path = toPathIfFileURL(path);
path = toPathIfBuffer(path);
// Some methods specifically allow for rel paths (eg symlink with srcPath)
let err = validatePath(path, allowRelative);
if (err) {
throw err;
}
// Overwrite path arg with converted and validated path
args[idx] = path;
}
/**
* FileSystem
@ -138,14 +80,14 @@ function FileSystem(options, callback) {
options = options || {};
callback = callback || defaultCallback;
const flags = options.flags || [];
const guid = options.guid ? options.guid : defaultGuidFn;
const provider = options.provider || new providers.Default(options.name || FILE_SYSTEM_NAME);
var flags = options.flags;
var guid = options.guid ? options.guid : defaultGuidFn;
var provider = options.provider || new providers.Default(options.name || FILE_SYSTEM_NAME);
// If we're given a provider, match its name unless we get an explicit name
const name = options.name || provider.name;
const forceFormatting = flags.includes(FS_FORMAT);
var name = options.name || provider.name;
var forceFormatting = _(flags).contains(FS_FORMAT);
const fs = this;
var fs = this;
fs.readyState = FS_PENDING;
fs.name = name;
fs.error = null;
@ -155,24 +97,35 @@ function FileSystem(options, callback) {
fs.stderr = STDERR;
// Expose Node's fs.constants to users
fs.constants = fsConstants;
// Node also forwards the access mode flags onto fs
fs.F_OK = fsConstants.F_OK;
fs.R_OK = fsConstants.R_OK;
fs.W_OK = fsConstants.W_OK;
fs.X_OK = fsConstants.X_OK;
fs.constants = Constants.fsConstants;
// Expose Shell constructor
this.Shell = Shell.bind(undefined, this);
// Safely expose the operation queue
let queue = [];
this.queueOrRun = function (operation) {
let error;
// Safely expose the list of open files and file
// descriptor management functions
var openFiles = {};
var nextDescriptor = FIRST_DESCRIPTOR;
Object.defineProperty(this, 'openFiles', {
get: function() { return openFiles; }
});
this.allocDescriptor = function(openFileDescription) {
var fd = nextDescriptor ++;
openFiles[fd] = openFileDescription;
return fd;
};
this.releaseDescriptor = function(fd) {
delete openFiles[fd];
};
if (FS_READY === fs.readyState) {
// Safely expose the operation queue
var queue = [];
this.queueOrRun = function(operation) {
var error;
if(FS_READY == fs.readyState) {
operation.call(fs);
} else if (FS_ERROR === fs.readyState) {
} else if(FS_ERROR == fs.readyState) {
error = new Errors.EFILESYSTEMERROR('unknown error');
} else {
queue.push(operation);
@ -181,25 +134,25 @@ function FileSystem(options, callback) {
return error;
};
function runQueued() {
queue.forEach(function (operation) {
queue.forEach(function(operation) {
operation.call(this);
}.bind(fs));
queue = null;
}
// We support the optional `options` arg from node, but ignore it
this.watch = function (filename, options, listener) {
if (Path.isNull(filename)) {
this.watch = function(filename, options, listener) {
if(isNullPath(filename)) {
throw new Error('Path must be a string without null bytes.');
}
if (typeof options === 'function') {
if(typeof options === 'function') {
listener = options;
options = {};
}
options = options || {};
listener = listener || nop;
const watcher = new FSWatcher();
var watcher = new FSWatcher();
watcher.start(filename, false, options.recursive);
watcher.on('change', listener);
@ -208,24 +161,24 @@ function FileSystem(options, callback) {
// Deal with various approaches to node ID creation
function wrappedGuidFn(context) {
return function (callback) {
return function(callback) {
// Skip the duplicate ID check if asked to
if (flags.includes(FS_NODUPEIDCHECK)) {
if(_(flags).contains(FS_NODUPEIDCHECK)) {
callback(null, guid());
return;
}
// Otherwise (default) make sure this id is unused first
function guidWithCheck(callback) {
const id = guid();
context.getObject(id, function (err, value) {
if (err) {
var id = guid();
context.getObject(id, function(err, value) {
if(err) {
callback(err);
return;
}
// If this id is unused, use it, otherwise find another
if (!value) {
if(!value) {
callback(null, id);
} else {
guidWithCheck(callback);
@ -239,28 +192,27 @@ function FileSystem(options, callback) {
// Let other instances (in this or other windows) know about
// any changes to this fs instance.
function broadcastChanges(changes) {
if (!changes.length) {
if(!changes.length) {
return;
}
const intercom = Intercom.getInstance();
changes.forEach(function (change) {
var intercom = Intercom.getInstance();
changes.forEach(function(change) {
intercom.emit(change.event, change.path);
});
}
// Open file system storage provider
provider.open(function (err) {
provider.open(function(err) {
function complete(error) {
function wrappedContext(methodName) {
let context = provider[methodName]();
context.name = name;
var context = provider[methodName]();
context.flags = flags;
context.changes = [];
context.guid = wrappedGuidFn(context);
// When the context is finished, let the fs deal with any change events
context.close = function () {
let changes = context.changes;
context.close = function() {
var changes = context.changes;
broadcastChanges(changes);
changes.length = 0;
};
@ -273,15 +225,15 @@ function FileSystem(options, callback) {
// for paths updated during the lifetime of the context). From this
// point forward we won't call open again, so it's safe to drop it.
fs.provider = {
openReadWriteContext: function () {
openReadWriteContext: function() {
return wrappedContext('getReadWriteContext');
},
openReadOnlyContext: function () {
openReadOnlyContext: function() {
return wrappedContext('getReadOnlyContext');
}
};
if (error) {
if(error) {
fs.readyState = FS_ERROR;
} else {
fs.readyState = FS_READY;
@ -290,18 +242,18 @@ function FileSystem(options, callback) {
callback(error, fs);
}
if (err) {
if(err) {
return complete(err);
}
const context = provider.getReadWriteContext();
var context = provider.getReadWriteContext();
context.guid = wrappedGuidFn(context);
// Mount the filesystem, formatting if necessary
if (forceFormatting) {
if(forceFormatting) {
// Wipe the storage provider, then write root block
context.clear(function (err) {
if (err) {
context.clear(function(err) {
if(err) {
return complete(err);
}
impl.ensureRootDirectory(context, complete);
@ -312,91 +264,63 @@ function FileSystem(options, callback) {
}
});
FileSystem.prototype.promises = {};
/**
* Public API for FileSystem. All node.js methods that are exposed on fs.promises
* include `promise: true`. We also include our own extra methods, but skip the
* fd versions to match node.js, which puts these on a `FileHandle` object.
* Any method that deals with path argument(s) also includes the position of
* those args in one of `absPathArgs: [...]` or `relPathArgs: [...]`, so they
* can be processed and validated before being passed on to the method.
*/
* Public API for FileSystem
*/
[
{ name: 'appendFile', promises: true, absPathArgs: [0] },
{ name: 'access', promises: true, absPathArgs: [0] },
{ name: 'chown', promises: true, absPathArgs: [0] },
{ name: 'chmod', promises: true, absPathArgs: [0] },
{ name: 'close' },
// copyFile - https://github.com/filerjs/filer/issues/436
{ name: 'exists', absPathArgs: [0] },
{ name: 'fchown' },
{ name: 'fchmod' },
// fdatasync - https://github.com/filerjs/filer/issues/653
{ name: 'fgetxattr' },
{ name: 'fremovexattr' },
{ name: 'fsetxattr' },
{ name: 'fstat' },
{ name: 'fsync' },
{ name: 'ftruncate' },
{ name: 'futimes' },
{ name: 'getxattr', promises: true, absPathArgs: [0] },
// lchown - https://github.com/filerjs/filer/issues/620
// lchmod - https://github.com/filerjs/filer/issues/619
{ name: 'link', promises: true, absPathArgs: [0, 1] },
{ name: 'lseek' },
{ name: 'lstat', promises: true },
{ name: 'mkdir', promises: true, absPathArgs: [0] },
{ name: 'mkdtemp', promises: true },
{ name: 'mknod', promises: true, absPathArgs: [0] },
{ name: 'open', promises: true, absPathArgs: [0] },
{ name: 'readdir', promises: true, absPathArgs: [0] },
{ name: 'read' },
{ name: 'readFile', promises: true, absPathArgs: [0] },
{ name: 'readlink', promises: true, absPathArgs: [0] },
// realpath - https://github.com/filerjs/filer/issues/85
{ name: 'removexattr', promises: true, absPathArgs: [0] },
{ name: 'rename', promises: true, absPathArgs: [0, 1] },
{ name: 'rmdir', promises: true, absPathArgs: [0] },
{ name: 'setxattr', promises: true, absPathArgs: [0] },
{ name: 'stat', promises: true, absPathArgs: [0] },
{ name: 'symlink', promises: true, relPathArgs: [0], absPathArgs: [1] },
{ name: 'truncate', promises: true, absPathArgs: [0] },
// unwatchFile - https://github.com/filerjs/filer/pull/553
{ name: 'unlink', promises: true, absPathArgs: [0] },
{ name: 'utimes', promises: true, absPathArgs: [0] },
// watch - implemented above in `this.watch`
// watchFile - https://github.com/filerjs/filer/issues/654
{ name: 'writeFile', promises: true, absPathArgs: [0] },
{ name: 'write' }
].forEach(function (method) {
const methodName = method.name;
const shouldPromisify = method.promises === true;
FileSystem.prototype[methodName] = function () {
const fs = this;
const args = Array.prototype.slice.call(arguments, 0);
const lastArgIndex = args.length - 1;
'open',
'chmod',
'fchmod',
'chown',
'fchown',
'close',
'mknod',
'mkdir',
'rmdir',
'stat',
'fstat',
'link',
'unlink',
'read',
'readFile',
'write',
'writeFile',
'appendFile',
'exists',
'lseek',
'readdir',
'rename',
'readlink',
'symlink',
'lstat',
'truncate',
'ftruncate',
'utimes',
'futimes',
'setxattr',
'getxattr',
'fsetxattr',
'fgetxattr',
'removexattr',
'fremovexattr'
].forEach(function(methodName) {
FileSystem.prototype[methodName] = function() {
var fs = this;
var args = Array.prototype.slice.call(arguments, 0);
var lastArgIndex = args.length - 1;
// We may or may not get a callback, and since node.js supports
// fire-and-forget style fs operations, we have to dance a bit here.
const missingCallback = typeof args[lastArgIndex] !== 'function';
const callback = maybeCallback(args[lastArgIndex]);
var missingCallback = typeof args[lastArgIndex] !== 'function';
var callback = maybeCallback(args[lastArgIndex]);
// Deal with path arguments, validating and normalizing Buffer and file:// URLs
if (method.absPathArgs) {
method.absPathArgs.forEach(pathArg => processPathArg(args, pathArg, false));
}
if (method.relPathArgs) {
method.relPathArgs.forEach(pathArg => processPathArg(args, pathArg, true));
}
const error = fs.queueOrRun(function () {
const context = fs.provider.openReadWriteContext();
var error = fs.queueOrRun(function() {
var context = fs.provider.openReadWriteContext();
// Fail early if the filesystem is in an error state (e.g.,
// provider failed to open.
if (FS_ERROR === fs.readyState) {
const err = new Errors.EFILESYSTEMERROR('filesystem unavailable, operation canceled');
if(FS_ERROR === fs.readyState) {
var err = new Errors.EFILESYSTEMERROR('filesystem unavailable, operation canceled');
return callback.call(fs, err);
}
@ -407,7 +331,7 @@ function FileSystem(options, callback) {
}
// Either add or replace the callback with our wrapper complete()
if (missingCallback) {
if(missingCallback) {
args.push(complete);
} else {
args[lastArgIndex] = complete;
@ -416,18 +340,15 @@ function FileSystem(options, callback) {
// Forward this call to the impl's version, using the following
// call signature, with complete() as the callback/last-arg now:
// fn(fs, context, arg0, arg1, ... , complete);
const fnArgs = [context].concat(args);
var fnArgs = [fs, context].concat(args);
impl[methodName].apply(null, fnArgs);
});
if (error) {
if(error) {
callback(error);
}
};
// Add to fs.promises if appropriate
if (shouldPromisify) {
FileSystem.prototype.promises[methodName] = promisify(FileSystem.prototype[methodName].bind(fs));
}
FileSystem.prototype.promises[methodName] = promisify(FileSystem.prototype[methodName].bind(fs));
});
}

View File

@ -1,8 +1,6 @@
'using strict';
const EventEmitter = require('../lib/eventemitter.js');
const Path = require('./path.js');
const Intercom = require('../lib/intercom.js');
var EventEmitter = require('../lib/eventemitter.js');
var Path = require('./path.js');
var Intercom = require('../lib/intercom.js');
/**
* FSWatcher based on node.js' FSWatcher
@ -10,10 +8,10 @@ const Intercom = require('../lib/intercom.js');
*/
function FSWatcher() {
EventEmitter.call(this);
const self = this;
let recursive = false;
let recursivePathPrefix;
let filename;
var self = this;
var recursive = false;
var recursivePathPrefix;
var filename;
function onchange(path) {
// Watch for exact filename, or parent path when recursive is true.
@ -48,12 +46,12 @@ function FSWatcher() {
recursivePathPrefix = filename === '/' ? '/' : filename + '/';
}
const intercom = Intercom.getInstance();
var intercom = Intercom.getInstance();
intercom.on('change', onchange);
};
self.close = function() {
const intercom = Intercom.getInstance();
var intercom = Intercom.getInstance();
intercom.off('change', onchange);
self.removeAllListeners('change');
};

View File

@ -1,34 +1,7 @@
let fs = null;
let Filer = null;
module.exports = Filer = {
module.exports = {
FileSystem: require('./filesystem/interface.js'),
Buffer: Buffer,
// We previously called this Path, but node calls it path. Do both
Buffer: require('./buffer.js'),
Path: require('./path.js'),
path: require('./path.js'),
Errors: require('./errors.js'),
Shell: require('./shell/shell.js'),
/**
* @deprecated Importing filer from your webpack config is not recommended.
*
* The filer `FilerWebpackPlugin` class is exposed directly.
*
* ```
* const { FilerWebpackPlugin } = require('filer/webpack');
* ```
*/
FilerWebpackPlugin: require('./webpack-plugin'),
Shell: require('./shell/shell.js')
};
// Add a getter for the `fs` instance, which returns
// a Filer FileSystem instance, using the default provider/flags.
Object.defineProperty(Filer, 'fs', {
enumerable: true,
get() {
if(!fs) {
fs = new Filer.FileSystem();
}
return fs;
}
});

View File

@ -1,135 +1,84 @@
const {
NODE_TYPE_FILE,
NODE_TYPE_DIRECTORY,
NODE_TYPE_SYMBOLIC_LINK,
DEFAULT_FILE_PERMISSIONS,
DEFAULT_DIR_PERMISSIONS
} = require('./constants');
const {
S_IFREG,
S_IFDIR,
S_IFLNK
} = require('./constants').fsConstants;
var NODE_TYPE_FILE = require('./constants.js').NODE_TYPE_FILE;
var NODE_TYPE_DIRECTORY = require('./constants.js').NODE_TYPE_DIRECTORY;
var NODE_TYPE_SYMBOLIC_LINK = require('./constants.js').NODE_TYPE_SYMBOLIC_LINK;
/**
* Make sure the options object has an id on property,
* either from caller or one we generate using supplied guid fn.
*/
function ensureID(options, prop, callback) {
if(options[prop]) {
return callback();
}
var S_IFREG = require('./constants.js').S_IFREG;
var S_IFDIR = require('./constants.js').S_IFDIR;
var S_IFLNK = require('./constants.js').S_IFLNK;
options.guid(function(err, id) {
if(err) {
return callback(err);
}
options[prop] = id;
callback();
});
}
var DEFAULT_FILE_PERMISSIONS = require('./constants.js').DEFAULT_FILE_PERMISSIONS;
var DEFAULT_DIR_PERMISSIONS = require('./constants.js').DEFAULT_DIR_PERMISSIONS;
/**
* Generate a POSIX mode (integer) for the node type and permissions.
* Use default permissions if we aren't passed any.
*/
function generateMode(nodeType, modePermissions) {
switch(nodeType) {
function getMode(type, mode) {
switch(type) {
case NODE_TYPE_DIRECTORY:
return (modePermissions || DEFAULT_DIR_PERMISSIONS) | S_IFDIR;
return (mode || DEFAULT_DIR_PERMISSIONS) | S_IFDIR;
case NODE_TYPE_SYMBOLIC_LINK:
return (modePermissions || DEFAULT_FILE_PERMISSIONS) | S_IFLNK;
return (mode || DEFAULT_FILE_PERMISSIONS) | S_IFLNK;
/* jshint -W086 */
case NODE_TYPE_FILE:
// falls through
default:
return (modePermissions || DEFAULT_FILE_PERMISSIONS) | S_IFREG;
return (mode || DEFAULT_FILE_PERMISSIONS) | S_IFREG;
}
}
/**
* Common properties for the layout of a Node
*/
class Node {
constructor(options) {
var now = Date.now();
function Node(options) {
var now = Date.now();
this.id = options.id;
this.data = options.data; // id for data object
this.size = options.size || 0; // size (bytes for files, entries for directories)
this.atime = options.atime || now; // access time (will mirror ctime after creation)
this.ctime = options.ctime || now; // creation/change time
this.mtime = options.mtime || now; // modified time
this.flags = options.flags || []; // file flags
this.xattrs = options.xattrs || {}; // extended attributes
this.nlinks = options.nlinks || 0; // links count
this.id = options.id;
this.type = options.type || NODE_TYPE_FILE; // node type (file, directory, etc)
this.size = options.size || 0; // size (bytes for files, entries for directories)
this.atime = options.atime || now; // access time (will mirror ctime after creation)
this.ctime = options.ctime || now; // creation/change time
this.mtime = options.mtime || now; // modified time
this.flags = options.flags || []; // file flags
this.xattrs = options.xattrs || {}; // extended attributes
this.nlinks = options.nlinks || 0; // links count
this.data = options.data; // id for data object
this.version = options.version || 1;
// Historically, Filer's node layout has referred to the
// node type as `mode`, and done so using a String. In
// a POSIX filesystem, the mode is a number that combines
// both node type and permission bits. Internal we use `type`,
// but store it in the database as `mode` for backward
// compatibility.
if(typeof options.type === 'string') {
this.type = options.type;
} else if(typeof options.mode === 'string') {
this.type = options.mode;
} else {
this.type = NODE_TYPE_FILE;
}
// permissions and flags
this.mode = options.mode || (getMode(this.type));
this.uid = options.uid || 0x0; // owner name
this.gid = options.gid || 0x0; // group name
}
// Extra mode permissions and ownership info
this.permissions = options.permissions || generateMode(this.type);
this.uid = options.uid || 0x0; // owner name
this.gid = options.gid || 0x0; // group name
}
/**
* Serialize a Node to JSON. Everything is as expected except
* that we use `mode` for `type` to maintain backward compatibility.
*/
toJSON() {
return {
id: this.id,
data: this.data,
size: this.size,
atime: this.atime,
ctime: this.ctime,
mtime: this.ctime,
flags: this.flags,
xattrs: this.xattrs,
nlinks: this.nlinks,
// Use `mode` for `type` to keep backward compatibility
mode: this.type,
permissions: this.permissions,
uid: this.uid,
gid: this.gid
};
}
// Return complete POSIX `mode` for node type + permissions. See:
// http://man7.org/linux/man-pages/man2/chmod.2.html
get mode() {
return generateMode(this.type, this.permissions);
}
// When setting the `mode` we assume permissions bits only (not changing type)
set mode(value) {
this.permissions = value;
// Make sure the options object has an id on property,
// either from caller or one we generate using supplied guid fn.
function ensureID(options, prop, callback) {
if(options[prop]) {
callback(null);
} else {
options.guid(function(err, id) {
options[prop] = id;
callback(err);
});
}
}
module.exports.create = function create(options, callback) {
Node.create = function(options, callback) {
// We expect both options.id and options.data to be provided/generated.
ensureID(options, 'id', function(err) {
if(err) {
return callback(err);
callback(err);
return;
}
ensureID(options, 'data', function(err) {
if(err) {
return callback(err);
callback(err);
return;
}
callback(null, new Node(options));
});
});
};
// Update the node's mode (permissions), taking file type bits into account.
Node.setMode = function(mode, node) {
node.mode = getMode(node.type, mode);
};
module.exports = Node;

View File

@ -1,5 +1,4 @@
const Errors = require('./errors.js');
const Node = require('./node');
var Errors = require('./errors.js');
function OpenFileDescription(path, id, flags, position) {
this.path = path;
@ -23,7 +22,7 @@ OpenFileDescription.prototype.getNode = function(context, callback) {
return callback(new Errors.EBADF('file descriptor refers to unknown node', path));
}
Node.create(node, callback);
callback(null, node);
}
context.getObject(id, check_if_node_exists);

View File

@ -1,44 +0,0 @@
const { FIRST_DESCRIPTOR } = require('./constants');
const openFiles = {};
/**
* Start at FIRST_DESCRIPTOR and go until we find
* an empty file descriptor, then return it.
*/
const getEmptyDescriptor = () => {
let fd = FIRST_DESCRIPTOR;
while(getOpenFileDescription(fd)) {
fd++;
}
return fd;
};
/**
* Look up the open file description object for a given
* file descriptor.
*/
const getOpenFileDescription = ofd => openFiles[ofd];
/**
* Allocate a new file descriptor for the given
* open file description.
*/
const allocDescriptor = openFileDescription => {
const ofd = getEmptyDescriptor();
openFiles[ofd] = openFileDescription;
return ofd;
};
/**
* Release the given existing file descriptor created
* with allocDescriptor().
*/
const releaseDescriptor = ofd => delete openFiles[ofd];
module.exports = {
allocDescriptor,
releaseDescriptor,
getOpenFileDescription
};

View File

@ -1,49 +1,238 @@
/**
* Patch process to add process.cwd(), always giving the root dir.
* NOTE: this line needs to happen *before* we require in `path`.
*/
process.cwd = () => '/';
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* https://github.com/browserify/path-browserify via Parcel.
* We use is as a base for our own Filer.Path, and patch/add
* a few things we need for the browser environment.
*/
const nodePath = require('path');
const filerPath = Object.assign({}, nodePath);
// Based on https://github.com/joyent/node/blob/41e53e557992a7d552a8e23de035f9463da25c99/lib/path.js
/**
* Patch path.basename() to return / vs. ''
*/
filerPath.basename = (path, ext) => {
const basename = nodePath.basename(path, ext);
return basename === '' ? '/' : basename;
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last === '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
var splitPathRe =
/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^./]*)?)$/;
var splitPath = function(filename) {
var result = splitPathRe.exec(filename);
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
};
/**
* Patch path.normalize() to not add a trailing /
*/
filerPath.normalize = (path) => {
path = nodePath.normalize(path);
return path === '/' ? path : filerPath.removeTrailing(path);
};
// path.resolve([from ...], to)
function resolve() {
var resolvedPath = '',
resolvedAbsolute = false;
/**
* Add new utility method isNull() to path: check for null paths.
*/
filerPath.isNull = path => ('' + path).indexOf('\u0000') !== -1;
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
// XXXfiler: we don't have process.cwd() so we use '/' as a fallback
var path = (i >= 0) ? arguments[i] : '/';
/**
* Add new utility method addTrailing() to add trailing / without doubling to //.
*/
filerPath.addTrailing = path => path.replace(/\/*$/, '/');
// Skip empty and invalid entries
if (typeof path !== 'string' || !path) {
continue;
}
/**
* Add new utility method removeTrailing() to remove trailing /, dealing with multiple
*/
filerPath.removeTrailing = path => {
resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path.charAt(0) === '/';
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
return !!p;
}), !resolvedAbsolute).join('/');
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
}
// path.normalize(path)
function normalize(path) {
var isAbsolute = path.charAt(0) === '/';
// Normalize the path
path = normalizeArray(path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
/*
if (path && trailingSlash) {
path += '/';
}
*/
return (isAbsolute ? '/' : '') + path;
}
function join() {
var paths = Array.prototype.slice.call(arguments, 0);
return normalize(paths.filter(function(p) {
return p && typeof p === 'string';
}).join('/'));
}
// path.relative(from, to)
function relative(from, to) {
from = resolve(from).substr(1);
to = resolve(to).substr(1);
function trim(arr) {
var start = 0;
for (; start < arr.length; start++) {
if (arr[start] !== '') break;
}
var end = arr.length - 1;
for (; end >= 0; end--) {
if (arr[end] !== '') break;
}
if (start > end) return [];
return arr.slice(start, end - start + 1);
}
var fromParts = trim(from.split('/'));
var toParts = trim(to.split('/'));
var length = Math.min(fromParts.length, toParts.length);
var samePartsLength = length;
for (var i = 0; i < length; i++) {
if (fromParts[i] !== toParts[i]) {
samePartsLength = i;
break;
}
}
var outputParts = [];
for (i = samePartsLength; i < fromParts.length; i++) {
outputParts.push('..');
}
outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('/');
}
function dirname(path) {
var result = splitPath(path),
root = result[0],
dir = result[1];
if (!root && !dir) {
// No dirname whatsoever
return '.';
}
if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
}
return root + dir;
}
function basename(path, ext) {
var f = splitPath(path)[2];
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
// XXXfiler: node.js just does `return f`
return f === '' ? '/' : f;
}
function extname(path) {
return splitPath(path)[3];
}
function isAbsolute(path) {
if(path.charAt(0) === '/') {
return true;
}
return false;
}
function isNull(path) {
if (('' + path).indexOf('\u0000') !== -1) {
return true;
}
return false;
}
// Make sure we don't double-add a trailing slash (e.g., '/' -> '//')
function addTrailing(path) {
return path.replace(/\/*$/, '/');
}
// Deal with multiple slashes at the end, one, or none
// and make sure we don't return the empty string.
function removeTrailing(path) {
path = path.replace(/\/*$/, '');
return path === '' ? '/' : path;
};
}
module.exports = filerPath;
// XXXfiler: we don't support path.exists() or path.existsSync(), which
// are deprecated, and need a FileSystem instance to work. Use fs.stat().
module.exports = {
normalize: normalize,
resolve: resolve,
join: join,
relative: relative,
sep: '/',
delimiter: ':',
dirname: dirname,
basename: basename,
extname: extname,
isAbsolute: isAbsolute,
isNull: isNull,
// Non-node but useful...
addTrailing: addTrailing,
removeTrailing: removeTrailing
};

View File

@ -1,8 +1,35 @@
const IndexedDB = require('./indexeddb.js');
const Memory = require('./memory.js');
var IndexedDB = require('./indexeddb.js');
var WebSQL = require('./websql.js');
var Memory = require('./memory.js');
module.exports = {
IndexedDB: IndexedDB,
WebSQL: WebSQL,
Memory: Memory,
/**
* Convenience Provider references
*/
// The default provider to use when none is specified
Default: IndexedDB,
Memory: Memory
// The Fallback provider does automatic fallback checks
Fallback: (function() {
if(IndexedDB.isSupported()) {
return IndexedDB;
}
if(WebSQL.isSupported()) {
return WebSQL;
}
function NotSupported() {
throw '[Filer Error] Your browser doesn\'t support IndexedDB or WebSQL.';
}
NotSupported.isSupported = function() {
return false;
};
return NotSupported;
}())
};

View File

@ -2,6 +2,12 @@ var FILE_SYSTEM_NAME = require('../constants.js').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = require('../constants.js').FILE_STORE_NAME;
var IDB_RW = require('../constants.js').IDB_RW;
var IDB_RO = require('../constants.js').IDB_RO;
var FilerBuffer = require('../buffer.js');
var indexedDB = global.indexedDB ||
global.mozIndexedDB ||
global.webkitIndexedDB ||
global.msIndexedDB;
function IndexedDBContext(db, mode) {
this.db = db;
@ -58,7 +64,7 @@ IndexedDBContext.prototype.getBuffer = function(key, callback) {
if(err) {
return callback(err);
}
callback(null, Buffer.from(arrayBuffer));
callback(null, new FilerBuffer(arrayBuffer));
});
};
@ -109,10 +115,6 @@ function IndexedDB(name) {
this.db = null;
}
IndexedDB.isSupported = function() {
var indexedDB = global.indexedDB ||
global.mozIndexedDB ||
global.webkitIndexedDB ||
global.msIndexedDB;
return !!indexedDB;
};
@ -125,11 +127,6 @@ IndexedDB.prototype.open = function(callback) {
}
try {
var indexedDB = global.indexedDB ||
global.mozIndexedDB ||
global.webkitIndexedDB ||
global.msIndexedDB;
// NOTE: we're not using versioned databases.
var openRequest = indexedDB.open(that.name);

View File

@ -9,7 +9,7 @@ var asyncCallback = require('../../lib/async.js').setImmediate;
var createDB = (function() {
var pool = {};
return function getOrCreate(name) {
if(!Object.prototype.hasOwnProperty.call(pool, name)) {
if(!pool.hasOwnProperty(name)) {
pool[name] = {};
}
return pool[name];

171
src/providers/websql.js Normal file
View File

@ -0,0 +1,171 @@
var FILE_SYSTEM_NAME = require('../constants.js').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = require('../constants.js').FILE_STORE_NAME;
var WSQL_VERSION = require('../constants.js').WSQL_VERSION;
var WSQL_SIZE = require('../constants.js').WSQL_SIZE;
var WSQL_DESC = require('../constants.js').WSQL_DESC;
var Errors = require('../errors.js');
var FilerBuffer = require('../buffer.js');
var base64ArrayBuffer = require('base64-arraybuffer');
function WebSQLContext(db, isReadOnly) {
var that = this;
this.getTransaction = function(callback) {
if(that.transaction) {
callback(that.transaction);
return;
}
// Either do readTransaction() (read-only) or transaction() (read/write)
db[isReadOnly ? 'readTransaction' : 'transaction'](function(transaction) {
that.transaction = transaction;
callback(transaction);
});
};
}
WebSQLContext.prototype.clear = function(callback) {
function onError(transaction, error) {
callback(error);
}
function onSuccess() {
callback(null);
}
this.getTransaction(function(transaction) {
transaction.executeSql('DELETE FROM ' + FILE_STORE_NAME + ';',
[], onSuccess, onError);
});
};
function _get(getTransaction, key, callback) {
function onSuccess(transaction, result) {
// If the key isn't found, return null
var value = result.rows.length === 0 ? null : result.rows.item(0).data;
callback(null, value);
}
function onError(transaction, error) {
callback(error);
}
getTransaction(function(transaction) {
transaction.executeSql('SELECT data FROM ' + FILE_STORE_NAME + ' WHERE id = ? LIMIT 1;',
[key], onSuccess, onError);
});
}
WebSQLContext.prototype.getObject = function(key, callback) {
_get(this.getTransaction, key, function(err, result) {
if(err) {
return callback(err);
}
try {
if(result) {
result = JSON.parse(result);
}
} catch(e) {
return callback(e);
}
callback(null, result);
});
};
WebSQLContext.prototype.getBuffer = function(key, callback) {
_get(this.getTransaction, key, function(err, result) {
if(err) {
return callback(err);
}
// Deal with zero-length ArrayBuffers, which will be encoded as ''
if(result || result === '') {
var arrayBuffer = base64ArrayBuffer.decode(result);
result = new FilerBuffer(arrayBuffer);
}
callback(null, result);
});
};
function _put(getTransaction, key, value, callback) {
function onSuccess() {
callback(null);
}
function onError(transaction, error) {
callback(error);
}
getTransaction(function(transaction) {
transaction.executeSql('INSERT OR REPLACE INTO ' + FILE_STORE_NAME + ' (id, data) VALUES (?, ?);',
[key, value], onSuccess, onError);
});
}
WebSQLContext.prototype.putObject = function(key, value, callback) {
var json = JSON.stringify(value);
_put(this.getTransaction, key, json, callback);
};
WebSQLContext.prototype.putBuffer = function(key, uint8BackedBuffer, callback) {
var base64 = base64ArrayBuffer.encode(uint8BackedBuffer.buffer);
_put(this.getTransaction, key, base64, callback);
};
WebSQLContext.prototype.delete = function(key, callback) {
function onSuccess() {
callback(null);
}
function onError(transaction, error) {
callback(error);
}
this.getTransaction(function(transaction) {
transaction.executeSql('DELETE FROM ' + FILE_STORE_NAME + ' WHERE id = ?;',
[key], onSuccess, onError);
});
};
function WebSQL(name) {
this.name = name || FILE_SYSTEM_NAME;
this.db = null;
}
WebSQL.isSupported = function() {
return !!global.openDatabase;
};
WebSQL.prototype.open = function(callback) {
var that = this;
// Bail if we already have a db open
if(that.db) {
return callback();
}
var db = global.openDatabase(that.name, WSQL_VERSION, WSQL_DESC, WSQL_SIZE);
if(!db) {
callback('[WebSQL] Unable to open database.');
return;
}
function onError(transaction, error) {
if (error.code === 5) {
callback(new Errors.EINVAL('WebSQL cannot be accessed. If private browsing is enabled, disable it.'));
}
callback(error);
}
function onSuccess() {
that.db = db;
callback();
}
// Create the table and index we'll need to store the fs data.
db.transaction(function(transaction) {
function createIndex(transaction) {
transaction.executeSql('CREATE INDEX IF NOT EXISTS idx_' + FILE_STORE_NAME + '_id' +
' on ' + FILE_STORE_NAME + ' (id);',
[], onSuccess, onError);
}
transaction.executeSql('CREATE TABLE IF NOT EXISTS ' + FILE_STORE_NAME + ' (id unique, data TEXT);',
[], createIndex, onError);
});
};
WebSQL.prototype.getReadOnlyContext = function() {
return new WebSQLContext(this.db, true);
};
WebSQL.prototype.getReadWriteContext = function() {
return new WebSQLContext(this.db, false);
};
module.exports = WebSQL;

View File

@ -1,27 +1,26 @@
function generateRandom(template) {
return template.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
function guid() {
return generateRandom('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx').toUpperCase();
}
/**
* Generate a string of n random characters. Defaults to n=6.
*/
function randomChars(n) {
n = n || 6;
var template = 'x'.repeat(n);
return generateRandom(template);
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
}).toUpperCase();
}
function nop() {}
/**
* Convert a Uint8Array to a regular array
*/
function u8toArray(u8) {
var array = [];
var len = u8.length;
for(var i = 0; i < len; i++) {
array[i] = u8[i];
}
return array;
}
module.exports = {
guid: guid,
nop: nop,
randomChars: randomChars
u8toArray: u8toArray,
nop: nop
};

View File

@ -1,5 +1,4 @@
'use strict';
const defaults = require('../constants.js').ENVIRONMENT;
var defaults = require('../constants.js').ENVIRONMENT;
module.exports = function Environment(env) {
env = env || {};

View File

@ -387,8 +387,7 @@ Shell.prototype.mkdirp = function(path, callback) {
callback(new Errors.EINVAL('Missing path argument'));
return;
}
path = Path.resolve(sh.pwd(), path);
if (path === '/') {
else if (path === '/') {
callback();
return;
}
@ -412,7 +411,7 @@ Shell.prototype.mkdirp = function(path, callback) {
var parent = Path.dirname(path);
if(parent === '/') {
fs.mkdir(path, function (err) {
if (err && err.code !== 'EEXIST') {
if (err && err.code != 'EEXIST') {
callback(err);
return;
}
@ -424,7 +423,7 @@ Shell.prototype.mkdirp = function(path, callback) {
_mkdirp(parent, function (err) {
if (err) return callback(err);
fs.mkdir(path, function (err) {
if (err && err.code !== 'EEXIST') {
if (err && err.code != 'EEXIST') {
callback(err);
return;
}

View File

@ -1,11 +1,5 @@
'use strict';
const Constants = require('./constants.js');
const Path = require('./path.js');
function dateFromMs(ms) {
return new Date(Number(ms));
}
var Constants = require('./constants.js');
var Path = require('./path.js');
function Stats(path, fileNode, devName) {
this.dev = devName;
@ -13,14 +7,9 @@ function Stats(path, fileNode, devName) {
this.type = fileNode.type;
this.size = fileNode.size;
this.nlinks = fileNode.nlinks;
// Date objects
this.atime = dateFromMs(fileNode.atime);
this.mtime = dateFromMs(fileNode.mtime);
this.ctime = dateFromMs(fileNode.ctime);
// Unix timestamp MS Numbers
this.atimeMs = fileNode.atime;
this.mtimeMs = fileNode.mtime;
this.ctimeMs = fileNode.ctime;
this.atime = fileNode.atime;
this.mtime = fileNode.mtime;
this.ctime = fileNode.ctime;
this.version = fileNode.version;
this.mode = fileNode.mode;
this.uid = fileNode.uid;

View File

@ -1,81 +0,0 @@
var path = require('path');
var utils = require('./utils');
const PLUGIN_NAME = 'filer-webpack-plugin';
const OPTIONS_SCHEMA = require('./schema');
const OPTIONS_PROCESSORS = require('./processors');
module.exports = class FilerWebpackPlugin {
constructor(options = {}) {
utils.validateOptions(options, OPTIONS_SCHEMA);
this.options = utils.processOptions(options, OPTIONS_PROCESSORS);
}
apply(compiler) {
compiler.hooks.normalModuleFactory.tap(
PLUGIN_NAME,
(factory) => {
factory.hooks.resolve.tap(
PLUGIN_NAME,
(resolveData) => {
// Resolve fsProvider if required
if (
resolveData.request === 'fsProvider'
&& resolveData.context === this.options.shimsDir
) {
return this.resolveFsProvider(resolveData);
}
// Ignore filer files (these should resolve modules normally)
if (resolveData.context.startsWith(this.options.filerDir)) return;
// Apply fs, path and buffer shims if required
switch (resolveData.request) {
case 'fs':
if (!this.options.shimFs) return;
return this.applyFsShim(resolveData);
case 'path':
if (!this.options.shimPath) return;
return this.applyPathShim(resolveData);
default:
return;
}
}
);
},
);
}
resolveFsProvider(resolveData) {
switch (this.options.fsProvider) {
case 'default':
resolveData.request = path.join(this.options.fsProviderDir, 'default.js');
break;
case 'indexeddb':
resolveData.request = path.join(this.options.fsProviderDir, 'indexeddb.js');
break;
case 'memory':
resolveData.request = path.join(this.options.fsProviderDir, 'memory.js');
break;
case 'custom':
resolveData.request = path.join(this.options.fsProviderDir, 'custom.js');
break;
default:
throw new Error([
'Invalid option for fsProvider.',
'fsProvider must be one of \'default\', \'indexeddb\', \'memory\' or \'custom\'.',
'If using a custom fsProvider, you must also provide the fsProviderDir option.'
].join(' '));
}
}
applyFsShim(resolveData) {
resolveData.request = path.join(this.options.shimsDir, 'fs.js');
}
applyPathShim(resolveData) {
resolveData.request = path.join(this.options.shimsDir, 'path.js');
}
};

View File

@ -1,34 +0,0 @@
var path = require('path');
const ROOT_DIR_TAG = '<rootDir>';
const CWD = process.cwd();
module.exports = {
filerDir: {
process: function(value) {
if (!value) {
return path.join(CWD, 'node_modules', 'filer');
}
return path.resolve(value.replace(ROOT_DIR_TAG, CWD));
},
},
shimsDir: {
process: function(value) {
if (!value) {
return path.join(CWD, 'node_modules', 'filer', 'shims');
}
return path.resolve(value.replace(ROOT_DIR_TAG, CWD));
}
},
fsProviderDir: {
process: function(value) {
if (!value) {
return path.join(CWD, 'node_modules', 'filer', 'shims', 'providers');
}
return path.resolve(value.replace(ROOT_DIR_TAG, CWD));
},
},
shimFs: { default: true },
shimPath: { default: true},
fsProvider: { default: 'default'},
};

View File

@ -1,23 +0,0 @@
module.exports = {
type: 'object',
properties: {
filerDir: {
type: 'string',
},
shimsDir: {
type: 'string',
},
shimFs: {
type: 'boolean',
},
shimPath: {
type: 'boolean',
},
fsProvider: {
type: 'string',
},
fsProviderDir: {
type: 'string',
},
}
};

View File

@ -1,26 +0,0 @@
var { validate } = require('schema-utils');
function validateOptions(options, schema) {
validate(schema, options);
}
function processOptions(options, processors) {
const processedOptions = {};
for (const [property, processor] of Object.entries(processors)) {
processedOptions[property] = options[property];
if (processedOptions[property] === undefined) {
processedOptions[property] = processor.default;
}
if (processor.process) {
processedOptions[property] = processor.process(processedOptions[property]);
}
}
return processedOptions;
}
module.exports = {
validateOptions,
processOptions,
};

View File

@ -7,7 +7,7 @@ describe('Filer.Buffer should accept initialized ArrayBuffers, issue 249', funct
afterEach(util.cleanup);
it('should accept an ArrayBuffer with a specified size', function(done) {
var buffer = Buffer.from(new ArrayBuffer(5));
var buffer = new Filer.Buffer(new ArrayBuffer(5));
expect(buffer.length).to.equal(5);
done();
});
@ -18,7 +18,7 @@ describe('Filer.Buffer static methods are in tact, issue 249', function() {
afterEach(util.cleanup);
it('should proxy Buffer.isBuffer', function(done) {
expect(Filer.Buffer.isBuffer(Buffer.from([]))).to.equal(true);
expect(Filer.Buffer.isBuffer(new Filer.Buffer([]))).to.equal(true);
expect(Filer.Buffer.isBuffer('')).to.equal(false);
done();
});
@ -35,7 +35,7 @@ describe('Filer.Buffer static methods are in tact, issue 249', function() {
});
it('should proxy Buffer.concat', function(done) {
expect(Filer.Buffer.concat([Buffer.alloc(1), Buffer.alloc(2)]).length).to.equal(3);
expect(Filer.Buffer.concat([new Filer.Buffer(1), new Filer.Buffer(2)]).length).to.equal(3);
done();
});
});

View File

@ -5,15 +5,23 @@ describe('undefined and relative paths, issue270', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should fail with EINVAL when called on an undefined path', function() {
it('should fail with EINVAL when called on an undefined path', function(done) {
var fs = util.fs();
var fn = () => fs.writeFile(undefined, 'data');
expect(fn).to.throw();
fs.writeFile(undefined, 'data', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
it('should fail with EINVAL when called on a relative path', function() {
it('should fail with EINVAL when called on a relative path', function(done) {
var fs = util.fs();
var fn = () => fs.writeFile('relpath/file.txt', 'data');
expect(fn).to.throw();
fs.writeFile('relpath/file.txt', 'data', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
});

View File

@ -1,12 +0,0 @@
'use strict';
const util = require('../lib/test-utils.js');
describe('unexpected failures when calling fs functions (e.g. writeFile) with empty options object, issue 773', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should call fs.writeFile with an empty options object', function(done) {
const fs = util.fs();
fs.writeFile('/a', 'trololol', {}, done);
});
});

View File

@ -1,115 +0,0 @@
'use strict';
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.readdir fails when passing options, issue775', function () {
beforeEach(util.setup);
afterEach(util.cleanup);
function setup(fs, dir, cb) {
fs.mkdir(dir, undefined, (err) => {
if (err) {
cb(err);
}
else {
fs.writeFile(dir + '/file', '', (err) => {
if (err) {
cb(err);
}
else {
fs.mkdir(dir + '/folder', (err) => {
if (err) {
cb(err);
}
else {
fs.symlink(dir + '/file', dir + '/symlink', (err) => {
if (err) {
cb(err);
}
else {
cb();
}
});
}
});
}
});
}
});
}
it('should create a directory, add a file, folder and symbolic link then call fs.readdir with buffer encoding', function (done) {
var fs = util.fs();
setup(fs, '/test_dir', (err) => {
if (err) {
done(err);
}
fs.readdir('/test_dir', 'buffer', (err, data) => {
if (err) {
done(err);
}
else {
expect(data).to.have.length(3);
expect(data[0].toString()).to.equal('file');
expect(data[1].toString()).to.equal('folder');
expect(data[2].toString()).to.equal('symlink');
done();
}
});
});
});
it('should create a directory, add a file, folder and symbolic link then call fs.readdir with withFileTypes and encoding options', function (done) {
var fs = util.fs();
setup(fs, '/test_dir', (err) => {
if (err) {
done(err);
}
fs.readdir('/test_dir', { encoding: 'base64', withFileTypes: true }, (err, data) => {
if (err) {
done(err);
}
else {
expect(data).to.have.length(3);
expect(Buffer.from(data[0].name, 'base64').toString()).to.equal('file');
expect(Buffer.from(data[1].name, 'base64').toString()).to.equal('folder');
expect(Buffer.from(data[2].name, 'base64').toString()).to.equal('symlink');
expect(data[0].isFile()).to.be.true;
expect(data[1].isDirectory()).to.be.true;
expect(data[2].isSymbolicLink()).to.be.true;
done();
}
});
});
});
it('should create a directory then call fs.readdir without options', function (done) {
var fs = util.fs();
setup(fs, '/test_dir', (err) => {
if (err) {
done(err);
}
else {
fs.readdir('/test_dir', (err, data) => {
if (err) {
done(err);
}
else {
expect(data).to.have.length(3);
expect(data[0]).to.equal('file');
expect(data[1]).to.equal('folder');
expect(data[2]).to.equal('symlink');
done();
}
});
}
});
});
});

View File

@ -1,12 +0,0 @@
'use strict';
const util = require('../lib/test-utils.js');
describe('fs.mkdir does not recursively create parent directories when called with { recursive: true }, issue776', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it.skip('should not throw when calling fs.mkdir with recursive flag set', function(done) {
const fs = util.fs();
fs.mkdir('/test_dir/a/b', { recursive: true }, done);
});
});

View File

@ -1,9 +0,0 @@
The following images are JSON filesystem images created with
`tools/fs-image.js`, and named with the filesystem dir used
as the source of the image, and the version of Filer used to
generate them:
1. `tiny-fs.0.43.json` was created from `tests/filesystem/tiny-fs/` with https://github.com/filerjs/filer/blob/d66114e20c7f0235698d9933dbd90217ba86fa4e/dist/filer.min.js
If you need to create a new image, use `tools/get-filer-version.js` to
get a specific version of Filer, then `tools/fs-image.js` to generate an image.

View File

@ -1 +0,0 @@
{"00000000-0000-0000-0000-000000000000":{"id":"00000000-0000-0000-0000-000000000000","mode":"META","atime":1545098425757,"ctime":1545098425757,"mtime":1545098425757,"rnode":"A63180BA-9BCE-4245-86E8-8613726587CA"},"A63180BA-9BCE-4245-86E8-8613726587CA":{"id":"A63180BA-9BCE-4245-86E8-8613726587CA","mode":"DIRECTORY","size":0,"atime":1545098425770,"ctime":1545098425770,"mtime":1545098425770,"flags":[],"xattrs":{},"nlinks":1,"version":0,"nblocks":1,"data":"3A41160B-1F39-4162-85BD-473F16ABF35F"},"3A41160B-1F39-4162-85BD-473F16ABF35F":{"dir":{"id":"E1634C88-337D-4D1E-9155-81B099BC7E7C","type":"DIRECTORY"},"file.txt":{"id":"DD338D5C-34FF-4AF7-AFBB-C01E8214125F","type":"FILE"}},"E1634C88-337D-4D1E-9155-81B099BC7E7C":{"id":"E1634C88-337D-4D1E-9155-81B099BC7E7C","mode":"DIRECTORY","size":0,"atime":1545098425778,"ctime":1545098425778,"mtime":1545098425778,"flags":[],"xattrs":{},"nlinks":1,"version":0,"nblocks":1,"data":"C8BB5894-9B0C-4CDC-B014-3944FB40DA77"},"C8BB5894-9B0C-4CDC-B014-3944FB40DA77":{"file2.txt":{"id":"EEB56FEC-DABE-48BF-88A5-B9F37723A0BC","type":"FILE"}},"DD338D5C-34FF-4AF7-AFBB-C01E8214125F":{"id":"DD338D5C-34FF-4AF7-AFBB-C01E8214125F","mode":"FILE","size":16,"atime":1545098327877.7542,"ctime":1545098313562.6824,"mtime":1545098313562.6824,"flags":[],"xattrs":{},"nlinks":1,"version":1,"nblocks":1,"data":"BDEE2339-6992-4D8F-AFAA-81813C30592C"},"BDEE2339-6992-4D8F-AFAA-81813C30592C":{"type":"Buffer","data":[84,104,105,115,32,105,115,32,97,32,102,105,108,101,46,10]},"EEB56FEC-DABE-48BF-88A5-B9F37723A0BC":{"id":"EEB56FEC-DABE-48BF-88A5-B9F37723A0BC","mode":"FILE","size":23,"atime":1545098327877.7542,"ctime":1545098313562.6824,"mtime":1545098313562.6824,"flags":[],"xattrs":{},"nlinks":1,"version":1,"nblocks":1,"data":"62262B53-32BC-43F6-983B-4127302C2483"},"62262B53-32BC-43F6-983B-4127302C2483":{"type":"Buffer","data":[84,104,105,115,32,105,115,32,97,32,115,101,99,111,110,100,32,102,105,108,101,46,10]}}

View File

@ -1,91 +0,0 @@
const expect = require('chai').expect;
const Filer = require('../../../src');
const SerializableMemoryProvider = require('../../lib/serializable-memory-provider');
const nodeFs = require('fs');
const nodePath = require('path');
describe('Migration tests from Filer 0.43 to current', () => {
let filerFs;
before(done => {
// Let the provider parse the JSON
const imagePath = nodePath.resolve(__dirname, '../images/tiny-fs.0.43.json');
nodeFs.readFile(imagePath, 'utf8', (err, data) => {
if(err) throw err;
new Filer.FileSystem({
provider: new SerializableMemoryProvider('0.43', data)
}, (err, fs) => {
if(err) throw err;
filerFs = fs;
done();
});
});
});
it('should have a root directory', done => {
filerFs.stat('/', (err, stats) => {
if(err) throw err;
expect(stats).to.be.an('object');
expect(stats.isDirectory()).to.be.true;
done();
});
});
it('should have expected entries in root dir', done => {
filerFs.readdir('/', (err, entries) => {
if(err) throw err;
expect(entries).to.be.an('array');
expect(entries.length).to.equal(2);
expect(entries).to.contain('file.txt');
expect(entries).to.contain('dir');
done();
});
});
it('should have correct contents for /file.txt (read as String)', done => {
const fileTxtPath = nodePath.resolve(__dirname, '../tiny-fs/file.txt');
nodeFs.readFile(fileTxtPath, 'utf8', (err, nodeData) => {
if(err) throw err;
filerFs.readFile('/file.txt', 'utf8', (err, filerData) => {
if(err) throw err;
expect(nodeData).to.equal(filerData);
done();
});
});
});
it('should have expected entries in /dir', done => {
filerFs.readdir('/dir', (err, entries) => {
if(err) throw err;
expect(entries).to.be.an('array');
expect(entries.length).to.equal(1);
expect(entries).to.contain('file2.txt');
done();
});
});
it('should have correct contents for /dir/file2.txt (read as Buffer)', done => {
const file2TxtPath = nodePath.resolve(__dirname, '../tiny-fs/dir/file2.txt');
nodeFs.readFile(file2TxtPath, null, (err, nodeData) => {
if(err) throw err;
filerFs.readFile('/dir/file2.txt', null, (err, filerData) => {
if(err) throw err;
expect(nodeData).to.deep.equal(filerData);
done();
});
});
});
});

View File

@ -1 +0,0 @@
This is a second file.

View File

@ -1 +0,0 @@
This is a file.

View File

@ -12,7 +12,7 @@
<script src="../node_modules/mocha/mocha.js"></script>
<script>
mocha.setup('bdd').timeout(10000).slow(250);
mocha.setup('bdd').timeout(5000).slow(250);
window.onload = function() {
mocha.checkLeaks();

View File

@ -3,27 +3,17 @@
* get them running by default.
*/
// Shims
require('./spec/shims/fs.spec');
require('./spec/shims/path.spec');
// Webpack Plugin
require('./spec/webpack-plugin/webpack-plugin.spec');
// Filer
require('./spec/filer.spec');
require('./spec/filer.buffer.spec.js');
// Filer.FileSystem.*
require('./spec/filer.filesystem.spec');
require('./spec/fs.spec');
require('./spec/fs.access.spec');
require('./spec/fs.stat.spec');
require('./spec/fs.lstat.spec');
require('./spec/fs.exists.spec');
require('./spec/fs.mknod.spec');
require('./spec/fs.mkdir.spec');
require('./spec/fs.mkdtemp.spec');
require('./spec/fs.readdir.spec');
require('./spec/fs.rmdir.spec');
require('./spec/fs.open.spec');
@ -32,7 +22,6 @@ require('./spec/fs.writeFile-readFile.spec');
require('./spec/fs.appendFile.spec');
require('./spec/fs.read.spec');
require('./spec/fs.close.spec');
require('./spec/fs.fsync.spec');
require('./spec/fs.link.spec');
require('./spec/fs.unlink.spec');
require('./spec/fs.rename.spec');
@ -40,26 +29,24 @@ require('./spec/fs.lseek.spec');
require('./spec/fs.symlink.spec');
require('./spec/fs.readlink.spec');
require('./spec/fs.truncate.spec');
require('./spec/fs.ftruncate.spec');
require('./spec/fs.utimes.spec');
require('./spec/fs.xattr.spec');
require('./spec/fs.stats.spec');
require('./spec/path-resolution.spec');
require('./spec/trailing-slashes.spec');
require('./spec/times.spec');
require('./spec/time-flags.spec');
require('./spec/fs.watch.spec');
require('./spec/fs.unwatchFile.spec');
require('./spec/errors.spec');
require('./spec/fs.shell.spec');
require('./spec/fs.chmod.spec');
require('./spec/fs.chown.spec');
require('./spec/fs.copyFile.spec');
// Filer.FileSystem.providers.*
require('./spec/providers/providers.spec');
require('./spec/providers/providers.indexeddb.spec');
require('./spec/providers/providers.websql.spec');
require('./spec/providers/providers.memory.spec');
require('./spec/providers/serializable-memory-provider.spec');
// Filer.FileSystemShell.*
require('./spec/shell/cd.spec');
@ -91,9 +78,3 @@ require('./bugs/issue267.js');
require('./bugs/issue270.js');
require('./bugs/rename-dir-trailing-slash.js');
require('./bugs/issue357.js');
require('./bugs/issue773.js');
require('./bugs/issue775.js');
require('./bugs/issue776.js');
// Sample code from README
require('./spec/readme.example.spec');

View File

@ -1,8 +1,11 @@
'use strict';
var Filer = require('../../src');
const Filer = require('../../src');
var indexedDB = global.indexedDB ||
global.mozIndexedDB ||
global.webkitIndexedDB ||
global.msIndexedDB;
let needsCleanup = [];
var needsCleanup = [];
if(global.addEventListener) {
global.addEventListener('beforeunload', function() {
needsCleanup.forEach(function(f) { f(); });
@ -10,8 +13,8 @@ if(global.addEventListener) {
}
function IndexedDBTestProvider(name) {
let _done = false;
let that = this;
var _done = false;
var that = this;
function cleanup(callback) {
callback = callback || function(){};
@ -33,12 +36,7 @@ function IndexedDBTestProvider(name) {
that.provider.db.close();
}
const indexedDB = global.indexedDB ||
global.mozIndexedDB ||
global.webkitIndexedDB ||
global.msIndexedDB;
let request = indexedDB.deleteDatabase(name);
var request = indexedDB.deleteDatabase(name);
request.onsuccess = finished;
request.onerror = finished;
} catch(e) {

View File

@ -1,42 +0,0 @@
const MemoryProvider = require('../../src/providers/memory');
const { parseBJSON } = require('../lib/test-utils');
class SerializableMemoryProvider extends MemoryProvider {
constructor(name, jsonImage) {
super(name);
this.unparsedJSONImage = jsonImage;
}
/**
* In addition to the usual setup of a Memory provider,
* also parse and overwrite the internal database.
*/
open(callback) {
super.open(err => {
if(err) {
return callback(err);
}
// If we don't have an image to import, leave db as is
if(!this.unparsedJSONImage) {
return callback();
}
// Try to import the fs image from JSON
try {
this.db = parseBJSON(this.unparsedJSONImage);
this.unparsedJSONImage = null;
callback();
} catch(e) {
callback(new Error(`unable to parse JSON filesystem image: ${e.message}`));
}
});
}
export() {
return JSON.stringify(this.db);
}
}
module.exports = SerializableMemoryProvider;

View File

@ -1,5 +1,6 @@
var Filer = require('../../src');
var IndexedDBTestProvider = require('./indexeddb.js');
var WebSQLTestProvider = require('./websql.js');
var MemoryTestProvider = require('./memory.js');
var Url = require('url');
@ -15,8 +16,13 @@ function uniqueName() {
function findBestProvider() {
var providers = Filer.FileSystem.providers;
return providers.IndexedDB.isSupported() ?
IndexedDBTestProvider : MemoryTestProvider;
if(providers.IndexedDB.isSupported()) {
return IndexedDBTestProvider;
}
if(providers.WebSQL.isSupported()) {
return WebSQLTestProvider;
}
return MemoryTestProvider;
}
function getUrlParams() {
@ -43,24 +49,6 @@ function getProviderType() {
return queryString['filer-provider'] || defaultProvider;
}
// Run fn() in an environment with indexedDB available
// either as-is, or shimmed, removing when done.
function shimIndexedDB(fn) {
var addShim = !Filer.FileSystem.providers.IndexedDB.isSupported();
if(addShim) {
global.indexedDB = require('fake-indexeddb');
}
var result = fn();
if(addShim) {
delete global.indexedDB;
}
return result;
}
function setup(callback) {
// In browser we support specifying the provider via the query string
// (e.g., ?filer-provider=IndexedDB). If not specified, we use
@ -73,6 +61,9 @@ function setup(callback) {
case 'indexeddb':
_provider = new IndexedDBTestProvider(name);
break;
case 'websql':
_provider = new WebSQLTestProvider(name);
break;
case 'memory':
_provider = new MemoryTestProvider(name);
break;
@ -104,14 +95,14 @@ function setup(callback) {
function fs() {
if(!_fs) {
throw new Error('TestUtil: call setup() before fs()');
throw 'TestUtil: call setup() before fs()';
}
return _fs;
}
function provider() {
if(!_provider) {
throw new Error('TestUtil: call setup() before provider()');
throw 'TestUtil: call setup() before provider()';
}
return _provider;
}
@ -149,34 +140,6 @@ function typedArrayEqual(a, b) {
return true;
}
/**
* Parse JSON with serialized Buffers
*/
const parseBJSON = json =>
JSON.parse(json, (key, value) =>
value && value.type === 'Buffer' ?
Buffer.from(value.data) :
value
);
function createMockFn(implementation = undefined) {
const calls = [];
const mockFn = function(...args) {
calls.push({
args,
});
if (typeof implementation === 'function') {
return implementation(...args);
}
};
Object.defineProperty(mockFn, 'calls', {
get() {
return calls;
}
});
return mockFn;
}
module.exports = {
uniqueName: uniqueName,
setup: setup,
@ -185,11 +148,9 @@ module.exports = {
provider: provider,
providers: {
IndexedDB: IndexedDBTestProvider,
WebSQL: WebSQLTestProvider,
Memory: MemoryTestProvider
},
cleanup: cleanup,
typedArrayEqual: typedArrayEqual,
parseBJSON,
shimIndexedDB,
createMockFn
typedArrayEqual: typedArrayEqual
};

49
tests/lib/websql.js Normal file
View File

@ -0,0 +1,49 @@
var Filer = require('../../src');
var needsCleanup = [];
if(global.addEventListener) {
global.addEventListener('beforeunload', function() {
needsCleanup.forEach(function(f) { f(); });
});
}
function WebSQLTestProvider(name) {
var _done = false;
var that = this;
function cleanup(callback) {
callback = callback || function(){};
if(!that.provider || _done) {
return callback();
}
// Provider is there, but db was never touched
if(!that.provider.db) {
return callback();
}
var context = that.provider.getReadWriteContext();
context.clear(function() {
that.provider = null;
_done = true;
callback();
});
}
function init() {
if(that.provider) {
return;
}
that.provider = new Filer.FileSystem.providers.WebSQL(name);
needsCleanup.push(cleanup);
}
this.init = init;
this.cleanup = cleanup;
}
WebSQLTestProvider.isSupported = function() {
return Filer.FileSystem.providers.WebSQL.isSupported();
};
module.exports = WebSQLTestProvider;

View File

@ -1,7 +1,5 @@
'use strict';
const Filer = require('../../src');
const expect = require('chai').expect;
var Filer = require('../../src');
var expect = require('chai').expect;
describe('Filer.Errors', function() {
it('has expected errors', function() {
@ -137,7 +135,7 @@ describe('Filer.Errors', function() {
});
it('should include all expected properties by default', function() {
const err = new Filer.Errors.ENOENT();
var err = new Filer.Errors.ENOENT();
expect(err.name).to.equal('ENOENT');
expect(err.code).to.equal('ENOENT');
expect(err.errno).to.equal(34);
@ -145,7 +143,7 @@ describe('Filer.Errors', function() {
});
it('should include extra properties when provided', function() {
const err = new Filer.Errors.ENOENT('This is the message', '/this/is/the/path');
var err = new Filer.Errors.ENOENT('This is the message', '/this/is/the/path');
expect(err.name).to.equal('ENOENT');
expect(err.code).to.equal('ENOENT');
expect(err.errno).to.equal(34);
@ -154,29 +152,29 @@ describe('Filer.Errors', function() {
});
it('should include default message and path info when provided', function() {
const err = new Filer.Errors.ENOENT(null, '/this/is/the/path');
var err = new Filer.Errors.ENOENT(null, '/this/is/the/path');
expect(err.message).to.equal('no such file or directory');
expect(err.path).to.equal('/this/is/the/path');
});
it('should include just the message when no path provided', function() {
const err = new Filer.Errors.ENOENT();
var err = new Filer.Errors.ENOENT();
expect(err.message).to.equal('no such file or directory');
expect(err.path).not.to.exist;
});
it('should not include path in toString() when not provided', function() {
const err = new Filer.Errors.ENOENT('This is the message');
var err = new Filer.Errors.ENOENT('This is the message');
expect(err.toString()).to.equal('ENOENT: This is the message');
});
it('should include path in toString() when provided', function() {
const err = new Filer.Errors.ENOENT(null, '/this/is/the/path');
var err = new Filer.Errors.ENOENT(null, '/this/is/the/path');
expect(err.toString()).to.equal('ENOENT: no such file or directory, \'/this/is/the/path\'');
});
it('should include message and path info when provided', function() {
const err = new Filer.Errors.ENOENT('This is the message', '/this/is/the/path');
var err = new Filer.Errors.ENOENT('This is the message', '/this/is/the/path');
expect(err.message).to.equal('This is the message');
expect(err.path).to.equal('/this/is/the/path');
});

View File

@ -1,51 +0,0 @@
'use strict';
const Filer = require('../../src');
const expect = require('chai').expect;
describe('Filer.Buffer', function() {
it('should support .from()', function() {
expect(Filer.Buffer.from).to.be.a('function');
});
it('should support .alloc()', function() {
expect(Filer.Buffer.alloc).to.be.a('function');
});
it('should support .isBuffer()', function() {
const buf = Buffer.alloc(0);
expect(Buffer.isBuffer(buf)).to.be.true;
});
describe('Deprecation checks - constructor vs. class method init', function() {
it('should allow new Buffer(array)', function() {
const arr = [1, 2, 3];
const buf1 = new Buffer(arr);
const buf2 = new Buffer.from(arr);
expect(buf1).to.deep.equal(buf2);
});
it('should allow new Buffer(ArrayBuffer)', function() {
const arrayBuffer = (new Uint8Array([1, 2, 3])).buffer;
const buf1 = new Buffer(arrayBuffer);
const buf2 = Buffer.from(arrayBuffer);
expect(buf1).to.deep.equal(buf2);
});
it('should allow new Buffer(ArrayBuffer)', function() {
const buffer = new Buffer.from([1, 2, 3]);
const buf1 = new Buffer(buffer);
const buf2 = Buffer.from(buffer);
expect(buf1).to.deep.equal(buf2);
});
it('should allow new Buffer(string)', function() {
const s = 'Hello World';
const buf1 = new Buffer(s);
const buf2 = Buffer.from(s);
expect(buf1).to.deep.equal(buf2);
});
});
});

View File

@ -1,17 +1,16 @@
'use strict';
const Filer = require('../../src');
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var Filer = require('../../src');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('Filer.FileSystem', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should properly mount new or existing filesystem', function(done) {
const provider = util.provider().provider;
var provider = util.provider().provider;
// 1) Should be able to open a new filesystem, and get empty root
const fs1 = new Filer.FileSystem({provider: provider}, function() {
var fs1 = new Filer.FileSystem({provider: provider}, function() {
fs1.readdir('/', function(err, entries) {
expect(err).not.to.exist;
expect(entries).to.be.an('array');
@ -21,7 +20,7 @@ describe('Filer.FileSystem', function() {
if(err) throw err;
// 2) Should be able to open an existing filesystem
const fs2 = new Filer.FileSystem({provider: provider}, function() {
var fs2 = new Filer.FileSystem({provider: provider}, function() {
fs2.readdir('/', function(err, entries) {
expect(err).not.to.exist;
expect(entries).to.be.an('array');
@ -30,7 +29,7 @@ describe('Filer.FileSystem', function() {
// 3) FORMAT flag should wipe an existing filesystem
const fs3 = new Filer.FileSystem({provider: provider, flags: ['FORMAT']}, function() {
var fs3 = new Filer.FileSystem({provider: provider, flags: ['FORMAT']}, function() {
fs3.readdir('/', function(err, entries) {
expect(err).not.to.exist;
expect(entries).to.be.an('array');

View File

@ -1,7 +1,5 @@
'use strict';
const Filer = require('../../src');
const util = require('../lib/test-utils');
const expect = require('chai').expect;
var Filer = require('../../src');
var expect = require('chai').expect;
describe('Filer', function() {
it('is defined', function() {
@ -12,73 +10,7 @@ describe('Filer', function() {
expect(typeof Filer.FileSystem).to.equal('function');
});
it('has Buffer constructor', function() {
expect(typeof Filer.Buffer).to.equal('function');
});
it('has Path and path objects', function() {
expect(typeof Filer.Path).to.equal('object');
expect(typeof Filer.path).to.equal('object');
expect(Filer.Path).to.equal(Filer.path);
});
it('has Errors object', function() {
expect(typeof Filer.Errors).to.equal('object');
});
it('has an fs object that returns a Filer.FileSystem', function() {
// Depends on IndexedDB being available, since we can't
// configure our own test provider. Shim for coverage.
util.shimIndexedDB(function() {
expect(typeof Filer.fs).to.equal('object');
const fs1 = Filer.fs;
const fs2 = Filer.fs;
expect(fs1).to.be.an.instanceof(Filer.FileSystem);
expect(fs2).to.be.an.instanceof(Filer.FileSystem);
expect(fs1).to.equal(fs2);
});
});
it('has Shell constructor', function() {
expect(typeof Filer.Shell).to.equal('function');
});
it('must honor the \'FORMAT\' flag', function(done) {
const name = 'local-test';
// Because we need to use a bunch of Filer filesystems
// in this test, we can't use the usual test infrastructure
// to create/manage the fs instance. Pick the best one
// based on the testing environment (browser vs. node)
const providers = Filer.FileSystem.providers;
let Provider;
if(providers.IndexedDB.isSupported()) {
Provider = providers.IndexedDB;
} else {
Provider = providers.Memory;
}
let fs = new Filer.FileSystem({name, provider: new Provider(name)});
let fs2 = new Filer.FileSystem({name, provider: new Provider(name)});
fs.mkdir('/test', function(err){
if(err) throw err;
fs2.readdir('/', function(err, list) {
if(err) throw err;
expect(list).to.exist;
expect(list).to.have.length(1);
fs2 = new Filer.FileSystem({name, provider: new Provider(name), flags:['FORMAT']});
fs2.readdir('/', function(err, list2) {
expect(err).to.not.exist;
expect(list2).to.exist;
expect(list2).to.have.length(0);
done();
});
});
});
});
});

View File

@ -1,151 +0,0 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
describe('fs.access', function () {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should expose access mode flags on fs and fs.constants', function() {
const fs = util.fs();
// F_OK
expect(fs.F_OK).to.equal(0);
expect(fs.constants.F_OK).to.equal(0);
// R_OK
expect(fs.R_OK).to.equal(4);
expect(fs.constants.R_OK).to.equal(4);
// W_OK
expect(fs.W_OK).to.equal(2);
expect(fs.constants.W_OK).to.equal(2);
// X_OK
expect(fs.X_OK).to.equal(1);
expect(fs.constants.X_OK).to.equal(1);
});
it('should be a function', function () {
const fs = util.fs();
expect(typeof fs.access).to.equal('function');
});
it('should return an error if file does not exist', function (done) {
const fs = util.fs();
fs.access('/tmp', fs.constants.F_OK, function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
done();
});
});
it('should return no error if file does exist and mode = F_OK', function (done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;
fs.access('/myfile', fs.constants.F_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});
it('should return no error if file does exist and mode = R_OK', function (done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;
fs.access('/myfile', fs.constants.R_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});
it('should return no error if file does exist and mode = W_OK', function (done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;
fs.access('/myfile', fs.constants.W_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});
it('should return an error if file is not executable and mode = X_OK', function (done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;
fs.chmod('/myfile', '644', function(error){
if (error) throw error;
fs.access('/myfile', fs.constants.X_OK, function (error) {
expect(error).to.exist;
expect(error.code).to.equal('EACCES');
done();
});
});
});
});
it('should return no error if file does exist and mode = X_OK', function (done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;
fs.chmod('/myfile', 0o777, function(error) {
if (error) throw error;
fs.access('/myfile', fs.constants.X_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});
});
it('should return no error if file does exist and no mode is passed', function (done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;
fs.access('/myfile', function (error) {
expect(error).not.to.exist;
done();
});
});
});
it('should return no error if file does exist and mode = R_OK | W_OK', function (done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;
fs.access('/myfile', fs.constants.R_OK | fs.constants.W_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});
});

View File

@ -1,15 +1,11 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.appendFile', function() {
const contents = 'This is a file.';
beforeEach(function(done) {
util.setup(function() {
const fs = util.fs();
fs.writeFile('/myfile', contents, function(error) {
var fs = util.fs();
fs.writeFile('/myfile', 'This is a file.', { encoding: 'utf8' }, function(error) {
if(error) throw error;
done();
});
@ -18,13 +14,14 @@ describe('fs.appendFile', function() {
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
var fs = util.fs();
expect(fs.appendFile).to.be.a('function');
});
it('should append a utf8 file without specifying utf8 in appendFile', function(done) {
const fs = util.fs();
const more = ' Appended.';
var fs = util.fs();
var contents = 'This is a file.';
var more = ' Appended.';
fs.appendFile('/myfile', more, function(error) {
if(error) throw error;
@ -38,8 +35,9 @@ describe('fs.appendFile', function() {
});
it('should append a utf8 file with "utf8" option to appendFile', function(done) {
const fs = util.fs();
const more = ' Appended.';
var fs = util.fs();
var contents = 'This is a file.';
var more = ' Appended.';
fs.appendFile('/myfile', more, 'utf8', function(error) {
if(error) throw error;
@ -53,8 +51,9 @@ describe('fs.appendFile', function() {
});
it('should append a utf8 file with {encoding: "utf8"} option to appendFile', function(done) {
const fs = util.fs();
const more = ' Appended.';
var fs = util.fs();
var contents = 'This is a file.';
var more = ' Appended.';
fs.appendFile('/myfile', more, { encoding: 'utf8' }, function(error) {
if(error) throw error;
@ -68,12 +67,12 @@ describe('fs.appendFile', function() {
});
it('should append a binary file', function(done) {
const fs = util.fs();
var fs = util.fs();
// String and utf8 binary encoded versions of the same thing: 'This is a file.'
const binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
const binary2 = Buffer.from([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
const binary3 = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46,
var binary = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
var binary2 = new Buffer([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
var binary3 = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46,
32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
fs.writeFile('/mybinaryfile', binary, function(error) {
@ -92,9 +91,9 @@ describe('fs.appendFile', function() {
});
it('should follow symbolic links', function(done) {
const fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
var fs = util.fs();
var contents = 'This is a file.';
var more = ' Appended.';
fs.symlink('/myfile', '/myFileLink', function (error) {
if (error) throw error;
@ -112,7 +111,8 @@ describe('fs.appendFile', function() {
});
it('should work when file does not exist, and create the file', function(done) {
const fs = util.fs();
var fs = util.fs();
var contents = 'This is a file.';
fs.appendFile('/newfile', contents, { encoding: 'utf8' }, function(error) {
expect(error).not.to.exist;
@ -124,101 +124,4 @@ describe('fs.appendFile', function() {
});
});
});
it('should accept numbers and append them to the file', function(done) {
const fs = util.fs();
const more = 10000;
fs.appendFile('/myfile', more, 'utf8', function(error) {
if(error) throw error;
fs.readFile('/myfile', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents + more);
done();
});
});
});
});
describe('fs.promises.appendFile', function() {
beforeEach(function(done) {
util.setup(function() {
const fs = util.fs();
return fs.promises.writeFile('/myfile', 'This is a file.', { encoding: 'utf8' })
.then(done)
.catch(done);
});
});
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
expect(fs.promises.appendFile).to.be.a('function');
});
it('should append a utf8 file without specifying utf8 in appendFile', function() {
const fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
return fs.promises.appendFile('/myfile', more)
.then(() => fs.promises.readFile('/myfile', 'utf8'))
.then(data => expect(data).to.equal(contents + more));
});
it('should append a utf8 file with "utf8" option to appendFile', function() {
const fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
return fs.promises.appendFile('/myfile', more, 'utf8')
.then(() => fs.promises.readFile('/myfile', 'utf8'))
.then(data => expect(data).to.equal(contents + more));
});
it('should append a utf8 file with {encoding: "utf8"} option to appendFile', function() {
const fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
return fs.promises.appendFile('/myfile', more, { encoding: 'utf8' })
.then(() => fs.promises.readFile('/myfile', { encoding: 'utf8' }))
.then(data => expect(data).to.equal(contents + more));
});
it('should append a binary file', function() {
const fs = util.fs();
// String and utf8 binary encoded versions of the same thing: 'This is a file.'
const binary = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
const binary2 = new Buffer([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
const binary3 = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46,
32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
return fs.promises.writeFile('/mybinaryfile', binary)
.then(() => fs.promises.appendFile('/mybinaryfile', binary2))
.then(() => fs.promises.readFile('/mybinaryfile', 'ascii'))
.then(data => expect(data).to.deep.equal(binary3));
});
it('should follow symbolic links', function() {
const fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
return fs.promises.symlink('/myfile', '/myFileLink')
.then(() => fs.promises.appendFile('/myFileLink', more, 'utf8'))
.then(() => fs.promises.readFile('/myFileLink', 'utf8'))
.then(data => expect(data).to.equal(contents + more));
});
it('should work when file does not exist, and create the file', function() {
const fs = util.fs();
const contents = 'This is a file.';
return fs.promises.appendFile('/newfile', contents, { encoding: 'utf8' })
.then(() => fs.promises.readFile('/newfile', 'utf8'))
.then(data => expect(data).to.equal(contents));
});
});

View File

@ -1,20 +1,18 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.chmod, fs.fchmod', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be functions', function() {
const fs = util.fs();
var fs = util.fs();
expect(typeof fs.chmod).to.equal('function');
expect(typeof fs.fchmod).to.equal('function');
});
it('should automatically set mode=755 for a directory', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.mkdir('/dir', function(err) {
if(err) throw err;
@ -28,7 +26,7 @@ describe('fs.chmod, fs.fchmod', function() {
});
it('should automatically set mode=644 for a file', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.open('/file', 'w', function(err, fd) {
if(err) throw err;
@ -41,92 +39,8 @@ describe('fs.chmod, fs.fchmod', function() {
});
});
it('should be an error when the path is invalid', function(done){
const fs = util.fs();
fs.chmod('/invalid_path', 0o444, function(err){
expect(err).to.exist;
expect(err.code).to.equal('ENOENT');
done();
});
});
it('should error if mode value is a non-numeric string', function(done) {
const fs = util.fs();
fs.mkdir('/dir', function(err) {
if(err) throw err;
fs.chmod('/dir', 'mode', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
});
it('should error if mode value is null', function(done) {
const fs = util.fs();
fs.mkdir('/dir', function(err) {
if(err) throw err;
fs.chmod('/dir', 'null', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
});
it('should error if mode value is non-integer number', function(done) {
const fs = util.fs();
fs.mkdir('/dir', function(err) {
if(err) throw err;
fs.chmod('/dir', 3.14, function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
});
it('should error if mode value is non-integer number', function(done) {
const fs = util.fs();
fs.mkdir('/dir', function(err) {
if(err) throw err;
fs.chmod('/dir', 3.14, function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
});
it('should allow octal strings for mode value', function(done) {
const fs = util.fs();
fs.mkdir('/dir', function(err) {
if(err) throw err;
fs.chmod('/dir', '777', function(err) {
if(err) throw err;
fs.stat('/dir/', function(err, stats) {
if(err) throw err;
expect(stats.mode & 0o777).to.equal(0o777);
done();
});
});
});
});
it('should allow for updating mode of a given file', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.open('/file', 'w', function(err, fd) {
if(err) throw err;
@ -157,22 +71,3 @@ describe('fs.chmod, fs.fchmod', function() {
});
});
});
describe('fsPromise.chmod', function() {
beforeEach(util.setup);
afterEach(util.setup);
it('should be a function', function() {
const fsPromise = util.fs().promises;
expect(typeof fsPromise.chmod).to.equal('function');
});
it('should allow for updating mode of a given file', function() {
const fsPromise = util.fs().promises;
return fsPromise.open('/file', 'w')
.then(() => fsPromise.chmod('/file', 0o444))
.then(() => fsPromise.stat('/file'))
.then(stats => expect(stats.mode & 0o444).to.equal(0o444));
});
});

View File

@ -27,62 +27,6 @@ describe('fs.chown, fs.fchown', function() {
});
});
it('fchown should expect an interger value for uid', function(done) {
var fs = util.fs();
fs.open('/file', 'w', function(err, fd) {
if(err) throw err;
fs.fchown(fd, '1001', 1001, function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
fs.close(fd, done);
});
});
});
it('chown should expect an interger value for uid', function(done) {
var fs = util.fs();
fs.writeFile('/file', 'w', function(err) {
if(err) throw err;
fs.chown('/file', '1001', 1001, function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
});
it('fchown should expect an interger value for gid', function(done) {
var fs = util.fs();
fs.open('/file', 'w', function(err, fd) {
if(err) throw err;
fs.fchown(fd, 1001, '1001', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
fs.close(fd, done);
});
});
});
it('chown should expect an interger value for gid', function(done) {
var fs = util.fs();
fs.writeFile('/file', 'w', function(err) {
if(err) throw err;
fs.chown('/file', 1001, '1001', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
});
it('should allow updating gid and uid for a file', function(done) {
var fs = util.fs();
@ -118,27 +62,3 @@ describe('fs.chown, fs.fchown', function() {
});
});
});
describe('fs.promises.chown', function(){
beforeEach(util.setup);
afterEach(util.setup);
it('should be a function', function(){
var fsPromises = util.fs().promises;
expect(fsPromises.chown).to.be.a('function');
});
it('should allow updating uid and gid for a file', function() {
var fsPromises = util.fs().promises;
return fsPromises
.writeFile('/file', 'data')
.then(() => fsPromises.chown('/file', 500, 500))
.then(() => fsPromises.stat('/file'))
.then((stats) => {
expect(stats.uid).to.equal(500);
expect(stats.gid).to.equal(500);
});
});
});

View File

@ -1,31 +1,29 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var Filer = require('../../src');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.close', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
var fs = util.fs();
expect(typeof fs.close).to.equal('function');
});
it('should release the file descriptor', function(done) {
const buffer = Buffer.alloc(0);
const fs = util.fs();
var buffer = new Filer.Buffer(0);
var fs = util.fs();
fs.open('/myfile', 'w+', function(error, result) {
if(error) throw error;
const fd = result;
var fd = result;
fs.close(fd, function(error) {
if(error) throw error;
fs.read(fd, buffer, 0, buffer.length, undefined, function(error, result) {
expect(error).to.exist;
expect(error.code).to.equal('EBADF');
expect(result).not.to.exist;
done();
});

View File

@ -1,64 +0,0 @@
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
const { COPYFILE_EXCL } = require('../../src/constants').fsConstants;
// Waiting on implementation to land https://github.com/filerjs/filer/issues/436
describe.skip('fs.copyFile', function() {
const file = {
path: '/srcfile',
contents: 'This is a src file.'
};
beforeEach(function(done){
util.setup(function() {
var fs = util.fs();
fs.writeFile(file.path, file.contents, done);
});
});
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
expect(fs.copyFile).to.be.a('function');
});
it('should return an error if the src path does not exist', function(done){
var fs = util.fs();
fs.copyFile(null, '/dest.txt', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
done();
});
});
it('should copy file successfully', function(done) {
var fs = util.fs();
const destPath = '/destfile';
fs.copyFile(file.path, destPath, function(error) {
if(error) throw error;
fs.readFile(destPath, function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(file.contents);
done();
});
});
});
it('should return an error if flag=COPYFILE_EXCL and the destination file exists', function (done) {
var fs = util.fs();
const destPath = '/destfile';
fs.writeFile(destPath, 'data', function(error) {
if(error) throw error;
fs.copyFile(file.path, destPath, COPYFILE_EXCL, function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
done();
});
});
});
});

View File

@ -1,18 +1,17 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.exists', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
var fs = util.fs();
expect(typeof fs.exists).to.equal('function');
});
it('should return false if path does not exist', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.exists('/tmp', function(result) {
expect(result).to.be.false;
@ -21,7 +20,7 @@ describe('fs.exists', function() {
});
it('should return true if path exists', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw err;
@ -38,7 +37,7 @@ describe('fs.exists', function() {
});
it('should follow symbolic links and return true for the resulting path', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.open('/myfile', 'w', function(error, fd) {
if(error) throw error;
@ -57,29 +56,4 @@ describe('fs.exists', function() {
});
});
});
it('should follow symbolic links and return false if for the resulting path does not exist', function(done) {
const fs = util.fs();
fs.open('/myfile', 'w', function(error, fd) {
if(error) throw error;
fs.close(fd, function(error) {
if(error) throw error;
fs.symlink('/myfile', '/myfilelink', function(error) {
if(error) throw error;
fs.unlink('/myfile', function(err) {
if(err) throw err;
fs.exists('/myfilelink', function(result) {
expect(result).to.be.false;
done();
});
});
});
});
});
});
});

View File

@ -1,47 +0,0 @@
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.fsync', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
expect(fs.fsync).to.be.a('function');
});
it('should return error when fd is not a number', function(done) {
var fs = util.fs();
fs.fsync('1', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
it('should return error when fd is invalid', function(done) {
var fs = util.fs();
fs.fsync(1, function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EBADF');
done();
});
});
it('should not error if the fd is valid', function(done) {
var fs = util.fs();
fs.writeFile('/myfile', 'contents', function(error) {
if(error) throw error;
fs.open('/myfile', 'r', function(error, fd) {
if(error) throw error;
fs.fsync(fd, function(error) {
expect(error).not.to.exist;
fs.close(fd, done);
});
});
});
});
});

View File

@ -1,213 +0,0 @@
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
describe('fs.ftruncate', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
let fs = util.fs();
expect(fs.ftruncate).to.be.a('function');
});
it('should return an error if length is not an integer', function(done) {
let fs = util.fs();
let contents = 'This is a file.';
fs.writeFile('/myfile', contents, function(error) {
if(error) throw error;
fs.open('/myfile', 'w', function(err, fd) {
fs.ftruncate(fd, 'notAnInteger', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
// Close file descriptor when done
fs.close(fd);
});
});
});
it('should return an error if length is negative', function(done) {
let fs = util.fs();
let contents = 'This is a file.';
fs.writeFile('/myfile', contents, function(error) {
if(error) throw error;
fs.open('/myfile', 'w', function(err, fd) {
fs.ftruncate(fd, -1, function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
// Close file descriptor when done
fs.close(fd);
});
});
});
it('should return an error if file descriptor is negative', function(done) {
let fs = util.fs();
// File descriptor should be a non-negative number
fs.ftruncate(-1, 0, function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EBADF');
done();
});
});
it('should truncate a file', function(done) {
let fs = util.fs();
let buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
let truncated = Buffer.from([1, 2]);
fs.open('/myfile', 'w', function(error, result) {
if(error) throw error;
let fd = result;
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(buffer.length);
// Truncate file to first two bytes
fs.ftruncate(fd, 2, function(error) {
expect(error).not.to.exist;
});
fs.close(fd, function(error) {
if(error) throw error;
fs.readFile('/myfile', function(error, result) {
if(error) throw error;
expect(result).to.deep.equal(truncated);
done();
});
});
});
});
});
it('should truncate a valid descriptor', function(done) {
var fs = util.fs();
var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(error, result) {
if(error) throw error;
var fd = result;
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(buffer.length);
fs.ftruncate(fd, 0, function(error) {
expect(error).not.to.exist;
fs.fstat(fd, function(error, result) {
if(error) throw error;
expect(result.size).to.equal(0);
done();
});
});
});
});
});
it('should pad a file with zeros when the length is greater than the file size', function(done) {
let fs = util.fs();
let buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
let truncated = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 0]);
fs.open('/myfile', 'w', function(error, result) {
if(error) throw error;
let fd = result;
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(buffer.length);
fs.ftruncate(fd, 9, function(error) {
expect(error).not.to.exist;
});
fs.close(fd, function(error) {
if(error) throw error;
fs.readFile('/myfile', function(error, result) {
if(error) throw error;
expect(result).to.deep.equal(truncated);
done();
});
});
});
});
});
it('should assume a length of 0 if passed undefined', function(done) {
var fs = util.fs();
var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(error, result) {
if(error) throw error;
var fd = result;
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(buffer.length);
// Pass undefined to see that it defaults to 0
fs.ftruncate(fd, undefined, function(error) {
expect(error).not.to.exist;
fs.stat('/myfile', function(error, result) {
if(error) throw error;
expect(result.size).to.equal(0);
done();
});
});
// Close file descriptor
fs.close(fd);
});
});
});
it('should update the file size', function(done) {
var fs = util.fs();
var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(error, result) {
if(error) throw error;
var fd = result;
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(buffer.length);
fs.ftruncate(fd, 0, function(error) {
expect(error).not.to.exist;
fs.stat('/myfile', function(error, result) {
if(error) throw error;
expect(result.size).to.equal(0);
done();
});
});
// Close file descriptor
fs.close(fd);
});
});
});
});

View File

@ -80,6 +80,7 @@ describe('fs.link', function() {
fs.lstat('/myfileLink', function (error, result) {
if (error) throw error;
var _linkstats = result;
fs.lstat('/myotherfile', function (error, result) {
expect(error).not.to.exist;
@ -109,83 +110,4 @@ describe('fs.link', function() {
});
});
});
});
describe('fs.promises.link', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function(){
var fsPromises = util.fs().promises;
expect(fsPromises.link).to.be.a('function');
});
it('should return a promise', function() {
var fsPromise = util.fs().promises;
var p = fsPromise
.writeFile('/myfile', '')
.then(() => fsPromise.link('/myfile', '/myotherfile'));
expect(p).to.be.a('promise');
return p;
});
});
describe('fs.promises.link', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
expect(fs.promises.link).to.be.a('function');
});
it('should create a link to an existing file', function() {
var fs = util.fs().promises;
return fs.writeFile('/myfile', 'data')
.then(() => fs.link('/myfile', '/myotherfile'))
.then(() => fs.stat('/myfile'))
.then(stats => {
var _oldstats = stats;
return fs.stat('/myotherfile')
.then(stats => {
expect(stats.nlinks).to.equal(2);
expect(stats.dev).to.equal(_oldstats.dev);
expect(stats.node).to.equal(_oldstats.node);
expect(stats.size).to.equal(_oldstats.size);
expect(stats.type).to.equal(_oldstats.type);
});
});
});
it('should create hard link to identical data node', function() {
var fs = util.fs().promises;
var contents = 'Hello World!';
return fs.writeFile('/file', contents)
.then(() => fs.link('/file', '/hlink'))
.then(() => fs.readFile('/file', 'utf8'))
.then(fileData => {
return fs.readFile('/hlink', 'utf8')
.then(hlinkData => expect(fileData).to.equal(hlinkData));
});
});
it('should not allow links to a directory', function() {
var fs = util.fs().promises;
return fs.mkdir('/mydir')
.then(() => fs.link('/mydir', '/mydirlink'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EPERM');
});
});
});

View File

@ -1,3 +1,4 @@
var Filer = require('../../src');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
@ -49,8 +50,9 @@ describe('fs.lseek', function() {
it('should set the current position if whence is SET', function(done) {
var fs = util.fs();
var offset = 3;
var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer = Buffer.alloc(buffer.length + offset);
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer = new Filer.Buffer(buffer.length + offset);
result_buffer.fill(0);
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
@ -75,9 +77,9 @@ describe('fs.lseek', function() {
if(error) throw error;
expect(result.size).to.equal(offset + buffer.length);
var expected = Buffer.from([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8]);
var expected = new Filer.Buffer([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8]);
expect(result_buffer).to.deep.equal(expected);
fs.close(fd, done);
done();
});
});
});
@ -89,8 +91,9 @@ describe('fs.lseek', function() {
it('should update the current position if whence is CUR', function(done) {
var fs = util.fs();
var offset = -2;
var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer = Buffer.alloc(2 * buffer.length + offset);
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer = new Filer.Buffer(2 * buffer.length + offset);
result_buffer.fill(0);
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
@ -115,9 +118,9 @@ describe('fs.lseek', function() {
if(error) throw error;
expect(result.size).to.equal(offset + 2 * buffer.length);
var expected = Buffer.from([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8]);
var expected = new Filer.Buffer([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8]);
expect(result_buffer).to.deep.equal(expected);
fs.close(fd, done);
done();
});
});
});
@ -129,7 +132,7 @@ describe('fs.lseek', function() {
it('should update the current position if whence is END', function(done) {
var fs = util.fs();
var offset = 5;
var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer;
fs.open('/myfile', 'w+', function(error, result) {
@ -156,19 +159,15 @@ describe('fs.lseek', function() {
if(error) throw error;
expect(result.size).to.equal(offset + 2 * buffer.length);
result_buffer = Buffer.alloc(result.size);
result_buffer = new Filer.Buffer(result.size);
result_buffer.fill(0);
fs.read(fd2, result_buffer, 0, result_buffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(result_buffer.length);
var expected = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8]);
var expected = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8]);
expect(result_buffer).to.deep.equal(expected);
fs.close(fd1, function(error) {
if(error) throw error;
fs.close(fd2, done);
});
done();
});
});
});

View File

@ -1,19 +1,18 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.lstat', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
var fs = util.fs();
expect(typeof fs.lstat).to.equal('function');
});
it('should return an error if path does not exist', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.lstat('/tmp', function(error, result) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
@ -23,74 +22,28 @@ describe('fs.lstat', function() {
});
it('should return a stat object if path is not a symbolic link', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.lstat('/', function(error, result) {
expect(error).not.to.exist;
expect(result).to.exist;
expect(result.isDirectory()).to.be.true;
expect(result.type).to.equal('DIRECTORY');
done();
});
});
it('should return a stat object if path is a symbolic link', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.symlink('/', '/mylink', function(error) {
if(error) throw error;
fs.lstat('/mylink', function(error, result) {
expect(error).not.to.exist;
expect(result).to.exist;
expect(result.isSymbolicLink()).to.be.true;
expect(result.type).to.equal('SYMLINK');
done();
});
});
});
it('should have a mode (full node) when stat\'d with lstat', function(done) {
var fs = util.fs();
fs.writeFile('/file', 'data', function(error) {
if(error) throw error;
fs.symlink('/file', '/symlink', function(error) {
if(error) throw error;
fs.lstat('/symlink', function(error, stats) {
if(error) throw error;
// We should build and return a full node object, complete with
// calculated mode, which should be a SYMLINK and the default file permissions.
expect(stats.mode).to.equal(fs.constants.S_IFLNK | 0o644);
done();
});
});
});
});
});
describe('fs.promises.lstat', () => {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should return an error if path does not exist', () => {
const fsPromises = util.fs().promises;
return fsPromises.lstat('/tmp')
.catch( error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
});
it('should return a stat object if path is not a symbolic link', () => {
const fsPromises = util.fs().promises;
return fsPromises.lstat('/')
.then(result => {
expect(result).to.exist;
expect(result.isDirectory()).to.be.true;
});
});
});

View File

@ -1,95 +1,48 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.mkdir', function () {
describe('fs.mkdir', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function () {
const fs = util.fs();
it('should be a function', function() {
var fs = util.fs();
expect(fs.mkdir).to.be.a('function');
});
it('should return an error if part of the parent path does not exist', function (done) {
const fs = util.fs();
it('should return an error if part of the parent path does not exist', function(done) {
var fs = util.fs();
fs.mkdir('/tmp/mydir', function (error) {
fs.mkdir('/tmp/mydir', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
done();
});
});
it('should return an error if the path already exists', function (done) {
const fs = util.fs();
it('should return an error if the path already exists', function(done) {
var fs = util.fs();
fs.mkdir('/', function (error) {
fs.mkdir('/', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EEXIST');
done();
});
});
it('should make a new directory', function (done) {
const fs = util.fs();
it('should make a new directory', function(done) {
var fs = util.fs();
fs.mkdir('/tmp', function (error) {
fs.mkdir('/tmp', function(error) {
expect(error).not.to.exist;
if (error) throw error;
if(error) throw error;
fs.stat('/tmp', function (error, stats) {
fs.stat('/tmp', function(error, stats) {
expect(error).not.to.exist;
expect(stats).to.exist;
expect(stats.isDirectory()).to.be.true;
expect(stats.type).to.equal('DIRECTORY');
done();
});
});
});
});
describe('fs.promises.mkdir', function () {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function () {
const fs = util.fs();
expect(fs.promises.mkdir).to.be.a('function');
});
it('should return an error if part of the parent path does not exist', function () {
const fs = util.fs();
return fs.promises.mkdir('/tmp/mydir')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
});
it('should return an error if the path already exists', function () {
const fs = util.fs();
return fs.promises.mkdir('/')
.catch(error =>{
expect(error).to.exist;
expect(error.code).to.equal('EEXIST');
});
});
it('should make a new directory', function () {
const fs = util.fs();
return fs.promises.mkdir('/tmp')
.then(() => fs.promises.stat('/tmp'))
.then(stats => {
expect(stats).to.exist;
expect(stats.isDirectory()).to.be.true;
});
});
it('should return a promise', function () {
const fs = util.fs();
expect(fs.promises.mkdir('/tmp')).to.be.a('promise');
});
});

View File

@ -1,54 +0,0 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
describe('fs.mkdtemp', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
expect(fs.mkdtemp).to.be.a('function');
});
it('should craete temp dir with specified prefix', function(done) {
const fs = util.fs();
fs.mkdtemp('/foo', function(error, path) {
expect(error).not.to.exist;
expect(path).to.match(/foo-\w{6}/);
done();
});
});
it('should craete temp dir inside existing directory', function(done) {
const fs = util.fs();
fs.mkdir('/myDir', (error) => {
expect(error).not.to.exist;
fs.mkdtemp('/myDir/foo', function(error, path) {
expect(error).not.to.exist;
expect(path).to.match(/foo.{6}/);
done();
});
});
});
it('should not create temp dir without prefix', function(done) {
const fs = util.fs();
fs.mkdtemp('', function(error, path) {
expect(error).to.exist;
expect(path).not.to.exist;
done();
});
});
it('should not create temp dir inside non existing dir', function(done) {
const fs = util.fs();
fs.mkdtemp('/doesNotExists/foo', function(error, path) {
expect(error).to.exist;
expect(error.code).to.be.equal('ENOENT');
expect(path).to.exist;
done();
});
});
});

View File

@ -1,93 +1,82 @@
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.mknod', function(){
describe('fs.mknod', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function(done){
it('should be a function', function(done) {
var fs = util.fs();
expect(fs.mknod).to.be.a('function');
done();
});
it('should return an error if part of the parent path does not exist', function(done){
it('should return an error if part of the parent path does not exist', function(done) {
var fs = util.fs();
fs.mknod('/dir/mydir', 'DIRECTORY', function(error){
fs.mknod('/dir/mydir', 'DIRECTORY', function(error) {
expect(error.code).to.equal('ENOENT');
done();
});
});
it('should return an error if path already exists', function(done){
it('should return an error if path already exists', function(done) {
var fs = util.fs();
fs.mknod('/', 'DIRECTORY', function(error){
fs.mknod('/', 'DIRECTORY', function(error) {
expect(error.code).to.equal('EEXIST');
done();
});
});
it('should return an error if the parent node is not a directory', function(done){
it('should return an error if the parent node is not a directory', function(done) {
var fs = util.fs();
fs.mknod('/file', 'FILE', function(error){
if (error) throw error;
fs.mknod('/file', 'FILE' , function(error) {
if(error) throw error;
fs.mknod('/file/myfile', 'FILE', function(error){
fs.mknod('/file/myfile', 'FILE', function(error) {
expect(error.code).to.equal('ENOTDIR');
done();
});
});
});
it('should return an error if the mode provided is not DIRECTORY or FILE', function(done){
it('should return an error if the mode provided is not DIRECTORY or FILE', function(done) {
var fs = util.fs();
fs.mknod('/symlink', 'SYMLINK', function(error){
fs.mknod('/symlink', 'SYMLINK', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
it('should make a new directory', function(done){
it('should make a new directory', function(done) {
var fs = util.fs();
fs.mknod('/dir', 'DIRECTORY', function(error){
if (error) throw error;
fs.mknod('/dir', 'DIRECTORY', function(error) {
if(error) throw error;
fs.stat('/dir', function(error, stats){
fs.stat('/dir', function(error, stats) {
expect(error).not.to.exist;
expect(stats.isDirectory()).to.be.true;
expect(stats.type).to.equal('DIRECTORY');
done();
});
});
});
it('should make a new file', function(done){
it('should make a new file', function(done) {
var fs = util.fs();
fs.mknod('/file', 'FILE', function(error){
if (error) throw error;
fs.stat('/file', function(error, result){
fs.mknod('/file', 'FILE' , function(error) {
if(error) throw error;
fs.stat('/file', function(error, result) {
expect(error).not.to.exist;
expect(result.isFile()).to.be.true;
expect(result.type).to.equal('FILE');
done();
});
});
});
describe('Promise test mknod', function(){
it('should return an error if part of the parent path does not exist', function(){
var fs = util.fs().promises;
return fs.mknod('/dir/mydir', 'DIRECTORY')
.catch(error => {
expect(error.code).to.equal('ENOENT');
});
});
});
});

View File

@ -1,6 +1,6 @@
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
var { FIRST_DESCRIPTOR } = require('../../src/constants.js');
var constants = require('../../src/constants.js');
describe('fs.open', function() {
beforeEach(util.setup);
@ -47,23 +47,6 @@ describe('fs.open', function() {
});
});
it('should return an error when flagged for write and the path exists', function(done) {
var fs = util.fs();
fs.mkdir('/tmp', function(error) {
if(error) throw error;
fs.writeFile('/tmp/file', 'data', function(error) {
if(error) throw error;
fs.open('/tmp/file', 'wx', function(error, result) {
expect(error).to.exist;
expect(error.code).to.equal('EEXIST');
expect(result).not.to.exist;
done();
});
});
});
});
it('should return an error when flagged for append and the path is a directory', function(done) {
var fs = util.fs();
@ -80,101 +63,45 @@ describe('fs.open', function() {
it('should return a unique file descriptor', function(done) {
var fs = util.fs();
var fd1;
fs.open('/file1', 'w+', function(error, fd) {
if(error) throw error;
expect(error).not.to.exist;
expect(fd).to.be.a('number');
fs.open('/file2', 'w+', function(error, fd1) {
fs.open('/file2', 'w+', function(error, fd) {
if(error) throw error;
expect(error).not.to.exist;
expect(fd1).to.be.a('number');
expect(fd1).not.to.equal(fd);
fs.close(fd, function(error) {
if(error) throw error;
fs.close(fd1, done);
});
expect(fd).to.be.a('number');
expect(fd).not.to.equal(fd1);
done();
});
});
});
it('should return the argument value of the file descriptor index greater than or equal to the value set by the first useable file descriptor constant', function(done) {
it('should return the argument value of the file descriptor index matching the value set by the first useable file descriptor constant', function(done) {
var fs = util.fs();
var firstFD = constants.FIRST_DESCRIPTOR;
fs.open('/file1', 'w+', function(error, fd) {
if(error) throw error;
expect(fd).to.equal(FIRST_DESCRIPTOR);
fs.close(fd, done);
});
});
it('should reuse file descriptors after closing', function(done) {
var fs = util.fs();
fs.open('/file1', 'w+', function(error, fd) {
if(error) throw error;
expect(fd).to.equal(FIRST_DESCRIPTOR);
fs.close(fd, function(error) {
if(error) throw error;
fs.open('/file1', 'w+', function(error, fd) {
if(error) throw error;
expect(fd).to.equal(FIRST_DESCRIPTOR);
fs.close(fd, done);
});
});
expect(fd).to.equal(firstFD);
done();
});
});
it('should create a new file when flagged for write', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w', function(error, fd) {
fs.open('/myfile', 'w', function(error) {
if(error) throw error;
fs.stat('/myfile', function(error, result) {
expect(error).not.to.exist;
expect(result).to.exist;
expect(result.isFile()).to.be.true;
fs.close(fd, done);
});
});
});
it('should create a new file, when flagged for write, and set the mode to the passed value', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w', 0o777, function(error, fd) {
if(error) throw error;
fs.stat('/myfile', function(error, result) {
expect(error).not.to.exist;
expect(result).to.exist;
expect(result.mode).to.exist;
expect(result.mode & 0o777).to.equal(0o777);
fs.close(fd, done);
});
});
});
it('should create a new file, but no mode is passed, so the default value of 644 should be seen', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w', function(error, fd) {
if(error) throw error;
fs.stat('/myfile', function(error, result) {
expect(error).not.to.exist;
expect(result).to.exist;
expect(result.mode).to.exist;
expect(result.mode & 0o644).to.equal(0o644);
fs.close(fd, done);
expect(result.type).to.equal('FILE');
done();
});
});
});
@ -202,20 +129,11 @@ describe('fs.open', function() {
expect(error.code).to.equal('EBADF');
expect(result).not.to.exist;
fs.close(fd, done);
fs.close(fd);
done();
});
});
});
});
});
it('should error when flag is invalid', function(done) {
var fs = util.fs();
fs.open('/myfile', 'abcd', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
done();
});
});
});

View File

@ -1,24 +1,24 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var Filer = require('../../src');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.read', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
var fs = util.fs();
expect(fs.read).to.be.a('function');
});
it('should read data from a file', function(done) {
const fs = util.fs();
const wbuffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
const rbuffer = Buffer.alloc(wbuffer.length);
var fs = util.fs();
var wbuffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
var rbuffer = new Filer.Buffer(wbuffer.length);
rbuffer.fill(0);
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.write(fd, wbuffer, 0, wbuffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(wbuffer.length);
@ -27,17 +27,18 @@ describe('fs.read', function() {
expect(error).not.to.exist;
expect(result).to.equal(rbuffer.length);
expect(wbuffer).to.deep.equal(rbuffer);
fs.close(fd, done);
done();
});
});
});
});
it('should update the current file position', function(done) {
const fs = util.fs();
const wbuffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
const rbuffer = Buffer.alloc(wbuffer.length);
let _result = 0;
var fs = util.fs();
var wbuffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
var rbuffer = new Filer.Buffer(wbuffer.length);
rbuffer.fill(0);
var _result = 0;
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
@ -56,7 +57,7 @@ describe('fs.read', function() {
expect(error).not.to.exist;
expect(_result).to.equal(rbuffer.length);
expect(wbuffer).to.deep.equal(rbuffer);
fs.close(fd, done);
done();
});
});
});
@ -64,9 +65,11 @@ describe('fs.read', function() {
});
it('should fail to read a directory', function(done) {
const fs = util.fs();
const buf = Buffer.alloc(20);
const buf2 = Buffer.alloc(20);
var fs = util.fs();
var buf = new Filer.Buffer(20);
var buf2 = new Filer.Buffer(20);
buf.fill(0);
buf2.fill(0);
fs.mkdir('/mydir', function(error) {
if(error) throw error;
@ -79,23 +82,9 @@ describe('fs.read', function() {
expect(error.code).to.equal('EISDIR');
expect(result).to.equal(0);
expect(buf).to.deep.equal(buf2);
fs.close(fd, done);
done();
});
});
});
});
it('should fail to read a file that does not exist', function(done) {
const fs = util.fs();
const fd = 0;
const rbuffer = Buffer.alloc(8);
fs.read(fd, rbuffer, 0, rbuffer.length, 0, function(error, result) {
expect(error).to.exist;
expect(result).not.to.exist;
expect(error.code).to.equal('EBADF');
done();
});
});
});

View File

@ -1,19 +1,17 @@
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.readdir', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
var fs = util.fs();
expect(fs.readdir).to.be.a('function');
});
it('should return an error if the path does not exist', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.readdir('/tmp/mydir', function(error, files) {
expect(error).to.exist;
@ -24,7 +22,7 @@ describe('fs.readdir', function() {
});
it('should return a list of files from an existing directory', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.mkdir('/tmp', function(error) {
if(error) throw error;
@ -40,7 +38,7 @@ describe('fs.readdir', function() {
});
it('should follow symbolic links', function(done) {
const fs = util.fs();
var fs = util.fs();
fs.mkdir('/tmp', function(error) {
if(error) throw error;
@ -56,41 +54,4 @@ describe('fs.readdir', function() {
});
});
});
it('(promise) should be a function', function() {
const fsPromises = util.fs().promises;
expect(fsPromises.readdir).to.be.a('function');
});
it('should return an error if the path is a file', function() {
const fsPromises = util.fs().promises;
return fsPromises.writeFile('/myfile', 'contents')
.then(() => fsPromises.readdir('/myfile'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');
});
});
it('(promise) should return a list of files from an existing directory', function() {
const fsPromises = util.fs().promises;
return fsPromises.mkdir('/tmp')
.then(() => {
return fsPromises.stat('/');
})
.then(stats => {
expect(stats).to.exist;
expect(stats.isDirectory()).to.be.true;
})
.then(() => {
return fsPromises.readdir('/');
})
.then(files => {
expect(files).to.exist;
expect(files.length).to.equal(1);
expect(files[0]).to.equal('tmp');
});
});
});

View File

@ -1,91 +1,46 @@
'use strict';
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
describe('fs.readlink', function () {
describe('fs.readlink', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function () {
const fs = util.fs();
it('should be a function', function() {
var fs = util.fs();
expect(fs.readlink).to.be.a('function');
});
it('should return an error if part of the parent destination path does not exist', function (done) {
const fs = util.fs();
it('should return an error if part of the parent destination path does not exist', function(done) {
var fs = util.fs();
fs.readlink('/tmp/mydir', function (error) {
fs.readlink('/tmp/mydir', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
done();
});
});
it('should return an error if the path is not a symbolic link', function (done) {
const fs = util.fs();
it('should return an error if the path is not a symbolic link', function(done) {
var fs = util.fs();
fs.readlink('/', function (error) {
fs.readlink('/', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
done();
});
});
it('should return an error if the path is not a symbolic link', function (done) {
const fs = util.fs();
it('should return the contents of a symbolic link', function(done) {
var fs = util.fs();
fs.mkdir('/tmp', function (error) {
if (error) throw error;
fs.symlink('/', '/myfile', function(error) {
if(error) throw error;
fs.readlink('/tmp', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
});
it('should return the contents of a symbolic link', function (done) {
const fs = util.fs();
fs.symlink('/', '/myfile', function (error) {
if (error) throw error;
fs.readlink('/myfile', function (error, result) {
fs.readlink('/myfile', function(error, result) {
expect(error).not.to.exist;
expect(result).to.equal('/');
done();
});
});
});
it('should allow relative paths, but resolve to the dstpath', function (done) {
const fs = util.fs();
const contents = 'contents';
fs.mkdir('/dir', function (error) {
if (error) throw error;
fs.writeFile('/file', contents, function (error) {
if (error) throw error;
fs.symlink('../file', '/dir/symlink', function (error) {
if (error) throw error;
fs.readlink('/dir/symlink', function (error, result) {
expect(error).not.to.exist;
expect(result).to.equal('../file');
fs.readFile('/dir/symlink', 'utf8', function (error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents);
done();
});
});
});
});
});
});
});

View File

@ -10,23 +10,6 @@ describe('fs.rename', function() {
expect(fs.rename).to.be.a('function');
});
it.skip('should be able to rename an existing file to the same filename', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.close(fd, function(error) {
if(error) throw error;
fs.rename('/myfile', '/myfile', function(error) {
expect(error).not.to.exist;
done();
});
});
});
});
it('should rename an existing file', function(done) {
var complete1 = false;
var complete2 = false;
@ -87,56 +70,6 @@ describe('fs.rename', function() {
});
});
it('should rename an existing directory (using promises)', () => {
var fsPromises = util.fs().promises;
return fsPromises.mkdir('/mydir')
.then(() => fsPromises.rename('/mydir', '/myotherdir'))
.then(() => { fsPromises.stat('/mydir')
.catch((error) => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
})
.then(() => { fsPromises.stat('/myotherdir')
.then(result => {
expect(result.nlinks).to.equal(1);
});
})
.catch((error) => {
if (error) throw error;
});
});
it('should rename an existing directory into another sub directory', () => {
var fsPromises = util.fs().promises;
return fsPromises.mkdir('/mydir')
.then(() => fsPromises.mkdir('/mydir/subdir'))
.then(() => fsPromises.mkdir('/anotherdir'))
.then(() => fsPromises.rename('/mydir', '/anotherdir/originaldir'))
.then(() => { fsPromises.stat('/mydir')
.catch((error) => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
})
.then(() => { fsPromises.stat('/anotherdir/mydir')
.catch((error) => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
})
.then(() => { fsPromises.stat('/anotherdir/originaldir/subdir')
.then(result => {
expect(result.nlinks).to.equal(1);
});
})
.catch((error) => {
if (error) throw error;
});
});
it('should rename an existing directory if the new path points to an existing directory', function(done) {
var fs = util.fs();

View File

@ -97,83 +97,4 @@ describe('fs.rmdir', function() {
});
});
});
it('(promise) should be a function', function() {
var fsPromises = util.fs().promises;
expect(fsPromises.rmdir).to.be.a('function');
});
it('(promise) should return an error if the path does not exist', function() {
var fsPromises = util.fs().promises;
return fsPromises.rmdir('/tmp/mydir')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
});
it('(promise) should return an error if attempting to remove the root directory', function() {
var fsPromises = util.fs().promises;
return fsPromises.rmdir('/')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EBUSY');
});
});
});
describe('fs.promises.rmdir', function(){
beforeEach(util.setup);
afterEach(util.cleanup);
it('should return an error if the directory is not empty', function() {
var fs = util.fs();
var fsPromises = fs.promises;
return fsPromises.mkdir('/tmp')
.then(() => fsPromises.mkdir('/tmp/mydir'))
.then(() => fsPromises.rmdir('/'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EBUSY');
});
});
it('should return an error if the path is not a directory', function() {
var fs = util.fs();
var fsPromises = fs.promises;
return fsPromises.mkdir('/tmp')
.then(() => fsPromises.writeFile('/tmp/myfile','Hello World'))
.then(() => fsPromises.rmdir('/tmp/myfile'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');
});
});
});
describe('fsPromises.rmdir', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should return an error if the path does not exist', function() {
var fs = util.fs().promises;
return fs.rmdir('/tmp/mydir')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
});
it('should return an error if attempting to remove the root directory', function() {
var fs = util.fs().promises;
return fs.rmdir('/')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EBUSY');
});
});
});

View File

@ -1,31 +1,33 @@
'use strict';
const Filer = require('../../src');
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
var Filer = require('../../src');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.Shell', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('is a function', function() {
const fs = util.fs();
it('is a function', function(done) {
var fs = util.fs();
expect(typeof fs.Shell).to.equal('function');
done();
});
it('should return a FileSystemShell instance', function() {
const fs = util.fs();
const sh = new fs.Shell();
it('should return a FileSystemShell instance', function(done) {
var fs = util.fs();
var sh = new fs.Shell();
expect(sh.prototype).to.deep.equal((new Filer.Shell(fs)).prototype);
done();
});
it('should reflect changes to the prototype', function(){
const fs = util.fs();
const sh = new fs.Shell();
it('should reflect changes to the prototype', function(done){
var fs = util.fs();
var sh = new fs.Shell();
Filer.Shell.prototype.test = 'foo';
expect(sh.test).to.equal('foo');
done();
});
});

Some files were not shown because too many files have changed in this diff Show More