Initial commit.

This commit is contained in:
Jonathan Cremin 2015-07-09 23:01:43 +01:00
commit b48a4e92e1
169 changed files with 7538 additions and 0 deletions

36
lib/hostr-file-stream.js Normal file
View file

@ -0,0 +1,36 @@
import fs from 'fs';
import path from 'path';
import createError from 'http-errors';
import { get as getFile } from './s3';
import debugname from 'debug';
const debug = debugname('hostr:file-stream');
export default function* hostrFileStream(localPath, remotePath) {
const localRead = fs.createReadStream(localPath);
return new Promise((resolve, reject) => {
localRead.once('error', () => {
debug('local error');
const remoteRead = getFile(remotePath);
remoteRead.once('readable', () => {
debug('remote readable');
const localWrite = fs.createWriteStream(localPath);
localWrite.once('finish', () => {
debug('local write end');
resolve(fs.createReadStream(localPath));
});
remoteRead.pipe(localWrite);
});
remoteRead.once('error', () => {
debug('remote error');
reject(createError(404));
});
});
localRead.once('readable', () => {
debug('local readable');
resolve(localRead);
});
});
}