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

Commit 62ab10c

Browse files
committed
Various fixes to make the hyperdrive-daemon work
1 parent 577a067 commit 62ab10c

5 files changed

Lines changed: 64 additions & 42 deletions

File tree

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
@@ -119,14 +119,15 @@ exports.setup = async function setup ({rpcAPI, disallowedSavePaths}) {
119119
// daemonEvents.on('folder-sync-error', evt => archivesEvents.emit('folder-sync-error', evt))
120120

121121
// configure the bandwidth throttle
122-
settingsDb.getAll().then(({dat_bandwidth_limit_up, dat_bandwidth_limit_down}) => {
123-
daemon.setBandwidthThrottle({
124-
up: dat_bandwidth_limit_up,
125-
down: dat_bandwidth_limit_down
126-
})
127-
})
128-
settingsDb.on('set:dat_bandwidth_limit_up', up => daemon.setBandwidthThrottle({up}))
129-
settingsDb.on('set:dat_bandwidth_limit_down', down => daemon.setBandwidthThrottle({down}))
122+
// TODO
123+
// settingsDb.getAll().then(({dat_bandwidth_limit_up, dat_bandwidth_limit_down}) => {
124+
// daemon.setBandwidthThrottle({
125+
// up: dat_bandwidth_limit_up,
126+
// down: dat_bandwidth_limit_down
127+
// })
128+
// })
129+
// settingsDb.on('set:dat_bandwidth_limit_up', up => daemon.setBandwidthThrottle({up}))
130+
// settingsDb.on('set:dat_bandwidth_limit_down', down => daemon.setBandwidthThrottle({down}))
130131

131132
// start the GC manager
132133
datGC.setup()
@@ -185,9 +186,6 @@ const pullLatestArchiveMeta = exports.pullLatestArchiveMeta = async function pul
185186
try {
186187
var key = archive.key.toString('hex')
187188

188-
// ready() just in case (we need .blocks)
189-
await pify(archive.ready.bind(archive))()
190-
191189
// trigger DNS update
192190
confirmDomain(key)
193191

@@ -309,7 +307,6 @@ exports.forkArchive = async function forkArchive (srcArchiveUrl, manifest = {},
309307

310308
const loadArchive = exports.loadArchive = async function loadArchive (key, userSettings = null) {
311309
// validate key
312-
var secretKey
313310
if (key) {
314311
if (!Buffer.isBuffer(key)) {
315312
// existing dat
@@ -319,35 +316,32 @@ const loadArchive = exports.loadArchive = async function loadArchive (key, userS
319316
}
320317
key = datEncoding.toBuf(key)
321318
}
322-
} else {
323-
// new dat, generate keys
324-
var kp = signatures.keyPair()
325-
key = kp.publicKey
326-
secretKey = kp.secretKey
327319
}
328320

329321
// fallback to the promise, if possible
330-
var keyStr = datEncoding.toStr(key)
331-
if (keyStr in archiveLoadPromises) {
322+
var keyStr = key ? datEncoding.toStr(key) : null
323+
if (keyStr && keyStr in archiveLoadPromises) {
332324
return archiveLoadPromises[keyStr]
333325
}
334326

335327
// run and cache the promise
336-
var p = loadArchiveInner(key, secretKey, userSettings)
337-
archiveLoadPromises[keyStr] = p
328+
var p = loadArchiveInner(key, userSettings)
329+
if (key) archiveLoadPromises[keyStr] = p
338330
p.catch(err => {
339331
console.error('Failed to load archive', keyStr, err.toString())
340332
})
341333

342334
// when done, clear the promise
343-
const clear = () => delete archiveLoadPromises[keyStr]
344-
p.then(clear, clear)
335+
if (key) {
336+
const clear = () => delete archiveLoadPromises[keyStr]
337+
p.then(clear, clear)
338+
}
345339

346340
return p
347341
}
348342

349343
// main logic, separated out so we can capture the promise
350-
async function loadArchiveInner (key, secretKey, userSettings = null) {
344+
async function loadArchiveInner (key, userSettings = null) {
351345
// load the user settings as needed
352346
if (!userSettings) {
353347
try {
@@ -361,8 +355,9 @@ async function loadArchiveInner (key, secretKey, userSettings = null) {
361355
}
362356

363357
// ensure the folders exist
364-
var metaPath = archivesDb.getArchiveMetaPath(key)
365-
mkdirp.sync(metaPath)
358+
// TODO needed?
359+
// var metaPath = archivesDb.getArchiveMetaPath(key)
360+
// mkdirp.sync(metaPath)
366361

367362
// create the archive session with the daemon
368363
var archive = await daemon.createDatArchiveSession({key})
@@ -375,22 +370,22 @@ async function loadArchiveInner (key, secretKey, userSettings = null) {
375370
archive.domain = dnsRecord ? dnsRecord.name : undefined
376371

377372
// update db
378-
archivesDb.touch(key).catch(err => console.error('Failed to update lastAccessTime for archive', key, err))
373+
archivesDb.touch(archive.key).catch(err => console.error('Failed to update lastAccessTime for archive', archive.key, err))
379374
await pullLatestArchiveMeta(archive)
380375
datAssets.update(archive)
381-
382376
// configure subsystems
383377
folderSync.reconfigureArchive(archive, userSettings)
384378

385379
// wire up events
386380
archive.pullLatestArchiveMeta = _debounce(opts => pullLatestArchiveMeta(archive, opts), 1e3)
387-
archive.fileActStream = archive.pda.watch()
388-
archive.fileActStream.on('data', ([event, {path}]) => {
389-
if (event === 'changed') {
390-
archive.pullLatestArchiveMeta({updateMTime: true})
391-
datAssets.update(archive, [path])
392-
}
393-
})
381+
// TODO
382+
// archive.fileActStream = archive.pda.watch()
383+
// archive.fileActStream.on('data', ([event, {path}]) => {
384+
// if (event === 'changed') {
385+
// archive.pullLatestArchiveMeta({updateMTime: true})
386+
// datAssets.update(archive, [path])
387+
// }
388+
// })
394389
// TODO
395390
// archive.fileActStream = pda.watch(archive)
396391
// archive.fileActStream.on('data', ([event, {path}]) => {

dat/protocol.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ ${html}`
321321
})
322322
}
323323

324+
// TODO
325+
// replace this with createReadStream when that method is available
326+
var content = await checkoutFS.pda.readFile(entry.path)
327+
Object.assign(headers, {
328+
'Content-Type': mime.identify(entry.path)
329+
})
330+
respond({statusCode, headers, data: intoStream(content)})
331+
cleanup()
332+
return
333+
324334
// fetch the entry and stream the response
325335
fileReadStream = checkoutFS.createReadStream(entry.path, range)
326336
var dataStream = fileReadStream

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"fs-reverse": "0.0.3",
5050
"function-queue": "0.0.12",
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
@@ -436,6 +436,7 @@ async function validateUserUrl (url) {
436436
* @returns {void}
437437
*/
438438
function startWatch (user) {
439+
return // TODO
439440
/* dont await */crawler.watchSite(user.archive)
440441
watchThumb(user)
441442
watchAndSyncBookmarks(user)

0 commit comments

Comments
 (0)