-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ node_modules | |
|
||
# Temporary / data folders | ||
.tmp | ||
.data | ||
data/ | ||
|
||
# Build directory | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ src/ | |
|
||
# Test directory | ||
test/ | ||
.data | ||
|
||
.eslintrc | ||
.babelrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,77 @@ | ||
export default () => { | ||
import path from 'path' | ||
import fs from 'fs' | ||
import pump from 'pump' | ||
|
||
// promisify | ||
const readFile = (file) => new Promise((resolve, reject) => { | ||
fs.readFile(file, (err, data) => { | ||
if (err) return reject(err) | ||
resolve(data) | ||
}) | ||
}) | ||
const writeFile = (file, data) => new Promise((resolve, reject) => { | ||
fs.writeFile(file, data, (err) => { | ||
if (err) return reject(err) | ||
resolve(data) | ||
}) | ||
}) | ||
const stat = (file) => new Promise((resolve, reject) => { | ||
fs.stat(file, (err, statObj) => { | ||
if (err) return reject(err) | ||
resolve(statObj) | ||
}) | ||
}) | ||
|
||
export default ({ | ||
directory = '.fs-tus-store', | ||
} = {}) => { | ||
const absoluteDir = path.resolve(directory) | ||
// Returns { uploadOffset[, uploadLength] } | ||
|
||
// TODO: convert key to base64 or validate it? | ||
const keyPath = (key) => path.join(absoluteDir, key) | ||
const keyInfoPath = (key) => `${keyPath(key)}.info` | ||
|
||
const getKeyInfo = (key) => readFile(keyInfoPath(key)) | ||
.then(data => JSON.parse(data)) | ||
|
||
const setKeyInfo = (key, infoObj) => Promise.resolve() | ||
.then(() => JSON.stringify(infoObj)) | ||
.then(data => writeFile(keyInfoPath(key), data)) | ||
|
||
const getKeyOffset = (key) => stat(keyPath(key)) | ||
.then(({ size }) => size) | ||
|
||
const touchKey = (key) => writeFile(keyPath(key), '') | ||
|
||
// TODO: fail if key already exists? | ||
const create = (key, { uploadLength, uploadMetadata } = {}) => ( | ||
setKeyInfo(key, { uploadLength, uploadMetadata }) | ||
.then(() => touchKey(key)) | ||
) | ||
|
||
const info = key => getKeyInfo(key) | ||
.then(infoObj => ( | ||
getKeyOffset(key) | ||
.then((uploadOffset) => ({ | ||
...infoObj, | ||
uploadOffset, | ||
})) | ||
)) | ||
|
||
const write = (key, rs) => new Promise((resolve, reject) => { | ||
const ws = fs.createWriteStream(keyPath(key), { | ||
flags: 'a', // append to file | ||
}) | ||
pump(rs, ws, (err) => { | ||
if (err) return reject(err) | ||
resolve() | ||
}) | ||
}) | ||
|
||
return { | ||
info, | ||
create, | ||
write, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,72 @@ | ||
import test from 'blue-tape' | ||
import init from '../src' | ||
import rimraf from 'rimraf' | ||
import mkdirp from 'mkdirp' | ||
import str from 'string-to-stream' | ||
|
||
test('todo', (t) => { | ||
t.ok(!!init) | ||
t.end() | ||
import initFsStore from '../src' | ||
|
||
const resetStore = () => { | ||
const directory = `${__dirname}/.data` | ||
rimraf.sync(directory) | ||
mkdirp.sync(directory) | ||
return initFsStore({ directory }) | ||
} | ||
|
||
const store = resetStore() | ||
|
||
test('info - unknown key', (t) => { | ||
store | ||
.info('unknown-key') | ||
.catch((err) => { | ||
t.ok(err instanceof Error) | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('info - foo- unknown key', (t) => { | ||
store | ||
.info('foo') | ||
.catch((err) => { | ||
t.ok(err instanceof Error) | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('create foo', () => ( | ||
store | ||
.create('foo', { uploadLength: 'bar'.length }) | ||
)) | ||
|
||
test('info foo', (t) => ( | ||
store | ||
.info('foo') | ||
.then(({ uploadOffset, uploadLength }) => { | ||
t.equal(uploadOffset, 0) | ||
t.equal(uploadLength, 3) | ||
}) | ||
)) | ||
|
||
test('write ba to foo', () => ( | ||
store | ||
.write('foo', str('ba')) | ||
)) | ||
|
||
test('info foo', (t) => ( | ||
store | ||
.info('foo') | ||
.then(({ uploadOffset, uploadLength }) => { | ||
t.equal(uploadOffset, 2) | ||
t.equal(uploadLength, 3) | ||
}) | ||
)) | ||
|
||
test('write r to foo', () => store.write('foo', str('r'))) | ||
|
||
test('info foo', (t) => ( | ||
store | ||
.info('foo') | ||
.then(({ uploadOffset, uploadLength }) => { | ||
t.equal(uploadOffset, 3) | ||
t.equal(uploadLength, 3) | ||
}) | ||
)) |