question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

throws when trying to use `.authorized()`

See original GitHub issue

I’ve tried to reduce my code to this, hope it is understandable:

const hyperdb = require('hyperdb')
const findPeers = require('./lib/find-peers')

const onceAuthorized = (db, key, cb) => {
	const check = () => {
		db.authorized(key, (err, isAuthorized) => {
			if (err) cb(err)
			else if (isAuthorized === true) cb(null)
			else db.once('append', check) // todo: correct event?
		})
	}
	setImmediate(check)
}

const onReady = (db, name) => {
	findPeers(db.discoveryKey, (peer) => {
		const s = db.replicate({live: true})
		s.pipe(peer).pipe(s)
	})

	db.put('names/' + db.local.key.toString('hex'), name, (err) => {})

	const send = (content) => {
		let key = content.slice(12).trim()
		if (!validKey.test(key)) return null
		key = Buffer.from(key, 'hex')
		db.authorize(key, (err) => {})
	}

	// this crashes
	onceAuthorized(db, db.local.key, (err) => {})
}

const openChat = (dir, key, name) => {
	const db = hyperdb(dir, key, {
		valueEncoding: 'json'
	})

	db.ready((err) => {
		if (err) return null // todo
		onReady(db, name)
	})
}

On peer A, I’ve opened the hyperdb without a predefined key.

On peer B, I’ve opened the hyperdb with db.key from peer A as key. As I can tell from my logging, I have piped the replication streams together already (not sure why, the callback should have been called later).

The db.authorized(db.local.key, cb) call then throws with the following error:

/Users/j/web/hyper-chat-cli/node_modules/hyperdb/index.js:272
      var feedKey = self.feeds[head.clock[j]].key
                                              ^

TypeError: Cannot read property 'key' of undefined
    at /Users/j/web/hyper-chat-cli/node_modules/hyperdb/index.js:272:47
    at onhead (/Users/j/web/hyper-chat-cli/node_modules/hyperdb/index.js:309:10)
    at done (/Users/j/web/hyper-chat-cli/node_modules/hyperdb/index.js:800:5)
    at /Users/j/web/hyper-chat-cli/node_modules/hyperdb/index.js:467:5
    at /Users/j/web/hyper-chat-cli/node_modules/hypercore/index.js:134:15
    at apply (/Users/j/web/hyper-chat-cli/node_modules/thunky/index.js:44:12)
    at process._tickCallback (internal/process/next_tick.js:114:19)

Using Node v9.10.1, hyperdb@3.0.0-1.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jimpickcommented, Apr 7, 2018

Is this the right fix? (edit: nope)

diff --git a/index.js b/index.js
index d46943e..93262de 100644
--- a/index.js
+++ b/index.js
@@ -274,7 +274,7 @@ HyperDB.prototype.authorized = function (key, cb) {
     }

     for (var j = 0; j < max; j++) {
-      var feedKey = self.feeds[head.clock[j]].key
+      var feedKey = self.feeds[j].key
       if (feedKey.equals(key)) {
         return cb(null, true)
       }
0reactions
mafintoshcommented, Apr 12, 2018

Now released

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - .NET exceptions I can throw for Not Authorized or Not ...
If I were to look at some code that caught an UnauthorizedAccessViolation I would think that an unauthorized access attempt had been made...
Read more >
authorize() should now throw an exception · Issue #42 ...
I think that authorize name inflicts that an action is being done (eq "to authorize" - check and pass/hold) while can seems like...
Read more >
Authorization-code/callback throw exception after login
asked a question. Hi, I have created app of OIDC with web App with 2F and its implemented in local machine and working...
Read more >
throw - JavaScript - MDN Web Docs - Mozilla
Use the throw statement to throw an exception. When you throw an exception, expression specifies the value of the exception.
Read more >
Authorization - Laravel - The PHP Framework For Web Artisans
If the action is not authorized or if no user is currently authenticated, Laravel will automatically throw an Illuminate\Auth\Access\AuthorizationException ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found