Skip to content
This repository was archived by the owner on Jun 3, 2020. It is now read-only.

Commit 0416a59

Browse files
committed
Various fixes to make the hyperdrive-daemon work
1 parent 5dcafde commit 0416a59

File tree

5 files changed

+64
-42
lines changed

5 files changed

+64
-42
lines changed

dat/daemon.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,28 @@ exports.setup = async function () {
6060
// fetch daemon metadata from disk
6161
var metadata
6262
try {
63-
metadata = await loadMetadata()
63+
metadata = await new Promise((resolve, reject) => {
64+
loadMetadata((err, metadata) => {
65+
if (err) reject(err)
66+
else resolve(metadata)
67+
})
68+
})
6469
} catch (e) {
6570
await createMetadata(`localhost:${DAEMON_PORT}`)
66-
metadata = await loadMetadata()
71+
metadata = await new Promise((resolve, reject) => {
72+
loadMetadata((err, metadata) => {
73+
if (err) reject(err)
74+
else resolve(metadata)
75+
})
76+
})
6777
}
6878

6979
// instantiate the daemon
7080
// TODO the daemon should be managed in an external promise
7181
await startDaemon({
7282
storage: DAEMON_STORAGE_PATH,
7383
port: DAEMON_PORT,
84+
bootstrap: [],
7485
metadata
7586
})
7687

@@ -91,11 +102,11 @@ exports.setup = async function () {
91102
exports.createDatArchiveSession = async function (opts) {
92103
const session = await client.drive.get(opts)
93104
const sessionId = session.id
94-
const key = datEncoding.toStr(opts.key)
105+
const key = datEncoding.toStr(session.opts.key)
95106
var datArchive = {
96107
key: datEncoding.toBuf(key),
97108
url: `dat://${key}`,
98-
writable: false, // TODO
109+
writable: true, // TODO
99110

100111
session: {
101112
async close () {
@@ -127,8 +138,13 @@ exports.createDatArchiveSession = async function (opts) {
127138
})
128139
client.drive.stat(sessionId, ...args)
129140
},
130-
readFile: (...args) => client.drive.readFile(sessionId, ...args),
131-
writeFile: (...args) => client.drive.writeFile(sessionId, ...args),
141+
// readFile: (...args) => client.drive.readFile(sessionId, ...args), TODO opts not accepted by daemon yet
142+
readFile: (path, opts, cb) => {
143+
client.drive.readFile(sessionId, path, cb ? cb : opts)
144+
},
145+
// writeFile: (...args) => client.drive.writeFile(sessionId, ...args), TODO encoding/opts not accepted by daemon yet
146+
writeFile: (path, content, encoding, cb) => client.drive.writeFile(sessionId, path, content, cb),
147+
132148
readdir: (...args) => client.drive.readdir(sessionId, ...args),
133149
// ready: makeArchiveProxyCbFn(key, version, 'ready'),
134150
// download: makeArchiveProxyCbFn(key, version, 'download'),

dat/library.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ exports.setup = async function setup ({rpcAPI, disallowedSavePaths}) {
112112
// daemonEvents.on('folder-sync-error', evt => archivesEvents.emit('folder-sync-error', evt))
113113

114114
// configure the bandwidth throttle
115-
settingsDb.getAll().then(({dat_bandwidth_limit_up, dat_bandwidth_limit_down}) => {
116-
daemon.setBandwidthThrottle({
117-
up: dat_bandwidth_limit_up,
118-
down: dat_bandwidth_limit_down
119-
})
120-
})
121-
settingsDb.on('set:dat_bandwidth_limit_up', up => daemon.setBandwidthThrottle({up}))
122-
settingsDb.on('set:dat_bandwidth_limit_down', down => daemon.setBandwidthThrottle({down}))
115+
// TODO
116+
// settingsDb.getAll().then(({dat_bandwidth_limit_up, dat_bandwidth_limit_down}) => {
117+
// daemon.setBandwidthThrottle({
118+
// up: dat_bandwidth_limit_up,
119+
// down: dat_bandwidth_limit_down
120+
// })
121+
// })
122+
// settingsDb.on('set:dat_bandwidth_limit_up', up => daemon.setBandwidthThrottle({up}))
123+
// settingsDb.on('set:dat_bandwidth_limit_down', down => daemon.setBandwidthThrottle({down}))
123124

124125
// start the GC manager
125126
datGC.setup()
@@ -178,9 +179,6 @@ const pullLatestArchiveMeta = exports.pullLatestArchiveMeta = async function pul
178179
try {
179180
var key = archive.key.toString('hex')
180181

181-
// ready() just in case (we need .blocks)
182-
await pify(archive.ready.bind(archive))()
183-
184182
// read the archive meta and size on disk
185183
var [manifest, oldMeta, size] = await Promise.all([
186184
archive.pda.readManifest().catch(_ => {}),
@@ -299,7 +297,6 @@ exports.forkArchive = async function forkArchive (srcArchiveUrl, manifest = {},
299297

300298
const loadArchive = exports.loadArchive = async function loadArchive (key, userSettings = null) {
301299
// validate key
302-
var secretKey
303300
if (key) {
304301
if (!Buffer.isBuffer(key)) {
305302
// existing dat
@@ -309,35 +306,32 @@ const loadArchive = exports.loadArchive = async function loadArchive (key, userS
309306
}
310307
key = datEncoding.toBuf(key)
311308
}
312-
} else {
313-
// new dat, generate keys
314-
var kp = signatures.keyPair()
315-
key = kp.publicKey
316-
secretKey = kp.secretKey
317309
}
318310

319311
// fallback to the promise, if possible
320-
var keyStr = datEncoding.toStr(key)
321-
if (keyStr in archiveLoadPromises) {
312+
var keyStr = key ? datEncoding.toStr(key) : null
313+
if (keyStr && keyStr in archiveLoadPromises) {
322314
return archiveLoadPromises[keyStr]
323315
}
324316

325317
// run and cache the promise
326-
var p = loadArchiveInner(key, secretKey, userSettings)
327-
archiveLoadPromises[keyStr] = p
318+
var p = loadArchiveInner(key, userSettings)
319+
if (key) archiveLoadPromises[keyStr] = p
328320
p.catch(err => {
329321
console.error('Failed to load archive', keyStr, err.toString())
330322
})
331323

332324
// when done, clear the promise
333-
const clear = () => delete archiveLoadPromises[keyStr]
334-
p.then(clear, clear)
325+
if (key) {
326+
const clear = () => delete archiveLoadPromises[keyStr]
327+
p.then(clear, clear)
328+
}
335329

336330
return p
337331
}
338332

339333
// main logic, separated out so we can capture the promise
340-
async function loadArchiveInner (key, secretKey, userSettings = null) {
334+
async function loadArchiveInner (key, userSettings = null) {
341335
// load the user settings as needed
342336
if (!userSettings) {
343337
try {
@@ -351,8 +345,9 @@ async function loadArchiveInner (key, secretKey, userSettings = null) {
351345
}
352346

353347
// ensure the folders exist
354-
var metaPath = archivesDb.getArchiveMetaPath(key)
355-
mkdirp.sync(metaPath)
348+
// TODO needed?
349+
// var metaPath = archivesDb.getArchiveMetaPath(key)
350+
// mkdirp.sync(metaPath)
356351

357352
// create the archive session with the daemon
358353
var archive = await daemon.createDatArchiveSession({key})
@@ -361,22 +356,22 @@ async function loadArchiveInner (key, secretKey, userSettings = null) {
361356
archive.session.publish()
362357

363358
// update db
364-
archivesDb.touch(key).catch(err => console.error('Failed to update lastAccessTime for archive', key, err))
359+
archivesDb.touch(archive.key).catch(err => console.error('Failed to update lastAccessTime for archive', archive.key, err))
365360
await pullLatestArchiveMeta(archive)
366361
datAssets.update(archive)
367-
368362
// configure subsystems
369363
folderSync.reconfigureArchive(archive, userSettings)
370364

371365
// wire up events
372366
archive.pullLatestArchiveMeta = _debounce(opts => pullLatestArchiveMeta(archive, opts), 1e3)
373-
archive.fileActStream = archive.pda.watch()
374-
archive.fileActStream.on('data', ([event, {path}]) => {
375-
if (event === 'changed') {
376-
archive.pullLatestArchiveMeta({updateMTime: true})
377-
datAssets.update(archive, [path])
378-
}
379-
})
367+
// TODO
368+
// archive.fileActStream = archive.pda.watch()
369+
// archive.fileActStream.on('data', ([event, {path}]) => {
370+
// if (event === 'changed') {
371+
// archive.pullLatestArchiveMeta({updateMTime: true})
372+
// datAssets.update(archive, [path])
373+
// }
374+
// })
380375
// TODO
381376
// archive.fileActStream = pda.watch(archive)
382377
// archive.fileActStream.on('data', ([event, {path}]) => {

dat/protocol.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,16 @@ exports.electronHandler = async function (request, respond) {
344344
})
345345
}
346346

347+
// TODO
348+
// replace this with createReadStream when that method is available
349+
var content = await checkoutFS.pda.readFile(entry.path)
350+
Object.assign(headers, {
351+
'Content-Type': mime.identify(entry.path)
352+
})
353+
respond({statusCode, headers, data: intoStream(content)})
354+
cleanup()
355+
return
356+
347357
// fetch the entry and stream the response
348358
fileReadStream = checkoutFS.createReadStream(entry.path, range)
349359
var dataStream = fileReadStream

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"hypercore-protocol": "^6.11.0",
5050
"hyperdrive": "^9.14.5",
5151
"hyperdrive-daemon": "^0.9.13",
52-
"hyperdrive-daemon-client": "^0.9.9",
52+
"hyperdrive-daemon-client": "^0.9.10",
5353
"hyperdrive-network-speed": "^2.1.0",
5454
"icojs": "^0.12.3",
5555
"identify-filetype": "^1.0.0",

users/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ async function validateUserUrl (url) {
416416
* @returns {void}
417417
*/
418418
function startWatch (user) {
419+
return // TODO
419420
/* dont await */crawler.watchSite(user.archive)
420421
watchThumb(user)
421422
watchAndSyncBookmarks(user)

0 commit comments

Comments
 (0)