Created
June 6, 2017 23:12
-
-
Save pfrazee/9dc43748f7cbfe34e545a77c2a5bab15 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import path from 'path' | |
import raf from 'random-access-file' | |
import multi from 'multi-random-access' | |
import pda from 'pauls-dat-api' | |
module.exports = function (metadataDir, contentDir) { | |
return { | |
metadata: function (name, opts) { | |
return raf(path.join(metadataDir, 'metadata', name)) | |
}, | |
content: function (name, opts, archive) { | |
if (!archive) archive = opts | |
if (name === 'data') return createStorage(archive, metadataDir, contentDir) | |
return raf(path.join(metadataDir, 'content', name)) | |
} | |
} | |
} | |
function createStorage (archive, metadataDir, contentDir) { | |
var historyStorage = raf(path.join(metadataDir, 'content', name)) | |
return multi({limit: 128}, locate) | |
function locate (offset, cb) { | |
archive.ready(async function (err) { | |
if (err) return cb(err) | |
// locate the metadata (stat) info | |
var entry | |
try { | |
entry = await pda.findEntryByContentBlock(archive, offset) | |
if (!entry) throw new Error('Could not locate data') | |
} catch (err) { | |
return cb(err) | |
} | |
// look up the latest version of this file | |
var latestSt = await pda.stat(archive, entry.name) | |
// fallback to history storage if not latest | |
if (latestSt.offset > entry.start) { | |
return cb({ | |
start: entry.start, | |
end: entry.end, | |
storage: historyStorage | |
}) | |
} | |
// use the content directory | |
cb(null, { | |
start: entry.bytesStart, | |
end: entry.bytesEnd, | |
storage: raf(path.join(contentDir, entry.name)) | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment