Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created April 24, 2017 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/8ae34e5bdd4e574b659c4072bd82e1b3 to your computer and use it in GitHub Desktop.
Save tmcw/8ae34e5bdd4e574b659c4072bd82e1b3 to your computer and use it in GitHub Desktop.
import { join } from 'path';
import fsp from 'fs-promise';
import { EventEmitter } from 'events';
/**
* The minimum requirements for a standard file based operation
*/
type minimalFileDescriptorType = {
name: string,
path: Array<string>,
encoding: ?string
};
/**
* The minimum requirements for a path operation
*/
type pathFileDescriptorType = {
name: ?string,
path: Array<string>
};
/**
* The minimum requirements for a write operation (data/buffer)
*/
type writeFileDescriptorType = {
name: string,
path: Array<string>,
content: string | Buffer,
encoding: ?string
};
module.exports = {
/**
* Service requires a configuration and must have dependencies pre-fulfilled
*/
init: (config: Object, services: Object): Promise<Object> =>
Promise.resolve().then(() => {
const { root } = config;
/**
* Local storage operations (local could be NFS mapped of course...)
* Every function has a Many alternative, e.g readMany
*/
const fns = {
/**
* Read a file
*/
read: (fD: minimalFileDescriptorType): Promise<Buffer | string> =>
fsp.readFile(join(...root, ...fD.path, fD.name), fD.encoding),
/**
* Read file stream
*/
readStream: (fD: minimalFileDescriptorType): EventEmitter =>
fsp.createReadStream(join(...root, ...fD.path, fD.name), fD.encoding),
/**
* Write data to a file
*/
write: (fD: writeFileDescriptorType): Promise =>
fsp.writeFile(
join(...root, ...fD.path, fD.name),
fD.content,
fD.encoding
),
/**
* Write file stream
*/
writeStream: (fD: minimalFileDescriptorType): EventEmitter =>
fsp.createWriteStream(
join(...root, ...fD.path, fD.name),
fD.encoding
),
/**
* Lists directory contents
*/
ls: (path: Array<string>): Promise =>
fsp.readdir(join(...root, ...path)),
/**
* Removes a file
*/
rm: (fD: minimalFileDescriptor): Promise =>
fsp.unlink(join(...root, ...fD.path, fD.name)),
/**
* Removes a directory. Think rf -rf
*/
rmdir: (path: Array<string>): Promise =>
fsp.remove(join(...root, ...path)),
/**
* Checks the existance of a dir or file
*/
stat: (fD: pathFileDescriptorType): Promise =>
fsp
.stat(join(...root, ...fD.path, fD.name || ''))
.then(() => true)
.catch(() => false),
/**
* Creates a path if it doesn't exist. Think mkdir -p
*/
mkdir: (path: Array<string>): Promise =>
fsp.mkdirs(join(...root, ...path)),
/**
* Generates a path representation with which another application can upload to
*/
getUploadUrl: (fD: minimalFileDescriptorType): Promise =>
Promise.resolve().then(() => {
return {
url: 'file://' + join(...root, ...fD.path, fD.name)
};
}),
/**
* Generates a path representation with which another application can download from
*/
getDownloadUrl: (fD: minimalFileDescriptorType): Promise =>
fns.getUploadUrl(fD)
};
// create Many version of the above
Object.keys(fns).forEach(key => {
fns[`${key}Many`] = (paths: Array<mixed>, ...args) =>
Promise.all(paths.map(path => fns[key](path, ...args)));
});
return fns;
})
};

Table of Contents

minimalFileDescriptorType

The minimum requirements for a standard file based operation

Type: {name: string, path: Array<string>, encoding: string?}

Properties

pathFileDescriptorType

The minimum requirements for a path operation

Type: {name: string?, path: Array<string>}

Properties

writeFileDescriptorType

The minimum requirements for a write operation (data/buffer)

Type: {name: string, path: Array<string>, content: (string | Buffer), encoding: string?}

Properties

init

Service requires a configuration and must have dependencies pre-fulfilled

Parameters

Returns Promise<Object>

fns

Local storage operations (local could be NFS mapped of course...) Every function has a Many alternative, e.g readMany

read

Read a file

Parameters

Returns Promise<(Buffer | string)>

readStream

Read file stream

Parameters

Returns EventEmitter

write

Write data to a file

Parameters

Returns Promise

writeStream

Write file stream

Parameters

Returns EventEmitter

ls

Lists directory contents

Parameters

Returns Promise

rm

Removes a file

Parameters

  • fD minimalFileDescriptor

Returns Promise

rmdir

Removes a directory. Think rf -rf

Parameters

Returns Promise

stat

Checks the existance of a dir or file

Parameters

Returns Promise

mkdir

Creates a path if it doesn't exist. Think mkdir -p

Parameters

Returns Promise

getUploadUrl

Generates a path representation with which another application can upload to

Parameters

Returns Promise

getDownloadUrl

Generates a path representation with which another application can download from

Parameters

Returns Promise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment