Browse Source

input feed id's as strings before encoding message as protobuf

Kieran Gibb 2 years ago
parent
commit
775110a1d1
No known key found for this signature in database
4 changed files with 15 additions and 15 deletions
  1. 7
    7
      index.js
  2. 2
    2
      messages.js
  3. 1
    1
      schema.proto
  4. 5
    5
      test/messages.test.js

+ 7
- 7
index.js View File

@@ -122,7 +122,7 @@ class KappaDrive extends EventEmitter {
122 122
     if (!filename) return callback(new errors.BadFileDescriptor(BAD_FILE_DESCRIPTOR))
123 123
     var drive = writeMode
124 124
       ? this.drive
125
-      : this._drives[this._fileDescriptors[fd].driveKey]
125
+      : this._drives[this._fileDescriptors[fd].driveKey.toString('hex')]
126 126
 
127 127
     // TODO: this is a hack - the file should already be open
128 128
     drive.open(filename, 'r', (err, newFd) => {
@@ -402,10 +402,10 @@ class KappaDrive extends EventEmitter {
402 402
         var values = msgs.map((msg) => State.decode(msg.value))
403 403
         if (!values || !values.length) return callback(null, this.drive)
404 404
         var winner = this._resolveFork(values, this.vectorClock)
405
-        var metadata = this.core.feed(winner.metadata.id)
405
+        var metadata = this.core.feed(Buffer.from(winner.metadata.id, 'hex'))
406 406
         if (!metadata) return callback(new Error('kappa-drive: invalid key for metadata'))
407 407
         this.log(`${self._id} [INDEX] metadata key: ${metadata.key.toString('hex')}`)
408
-        var content = this.core.feed(winner.content.id)
408
+        var content = this.core.feed(Buffer.from(winner.content.id, 'hex'))
409 409
         if (!content) return callback(new Error('kappa-drive: invalid key for content'))
410 410
         this.log(`${self._id} [INDEX] content key: ${content.key.toString('hex')}`)
411 411
         this._getDrive(metadata, content, callback)
@@ -487,7 +487,7 @@ class KappaDrive extends EventEmitter {
487 487
   _getDrive (metadata, content, opts = {}, callback) {
488 488
     if (typeof opts === 'function' && !callback) return this._getDrive(metadata, content, {}, opts)
489 489
 
490
-    if (this._drives[metadata.key]) return callback(null, this._drives[metadata.key])
490
+    if (this._drives[metadata.key.toString('hex')]) return callback(null, this._drives[metadata.key.toString('hex')])
491 491
     var store = corestore(this.storage, { defaultCore: metadata })
492 492
 
493 493
     var drive = hyperdrive(ram, metadata.key, {
@@ -501,7 +501,7 @@ class KappaDrive extends EventEmitter {
501 501
     })
502 502
 
503 503
     // TODO: drive = opts.version ? drive.checkout(opts.version) : drive
504
-    this._drives[metadata.key] = drive
504
+    this._drives[metadata.key.toString('hex')] = drive
505 505
 
506 506
     if (this._nonStateFeeds.indexOf(metadata.key.toString('hex')) < 0) this._nonStateFeeds.push(metadata.key.toString('hex'))
507 507
     if (this._nonStateFeeds.indexOf(content.key.toString('hex')) < 0) this._nonStateFeeds.push(content.key.toString('hex'))
@@ -528,11 +528,11 @@ class KappaDrive extends EventEmitter {
528 528
       version: this.drive.version,
529 529
       links,
530 530
       metadata: {
531
-        id: this.metadata.key,
531
+        id: this.metadata.key.toString('hex'),
532 532
         seq: this.metadata.length
533 533
       },
534 534
       content: {
535
-        id: this.content.key,
535
+        id: this.content.key.toString('hex'),
536 536
         seq: this.content.length
537 537
       },
538 538
       timestamp: Date.now(),

+ 2
- 2
messages.js View File

@@ -39,7 +39,7 @@ function defineState () {
39 39
 
40 40
   function defineFeed () {
41 41
     var enc = [
42
-      encodings.bytes,
42
+      encodings.string,
43 43
       encodings.varint
44 44
     ]
45 45
 
@@ -80,7 +80,7 @@ function defineState () {
80 80
       if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
81 81
       var oldOffset = offset
82 82
       var obj = {
83
-        id: null,
83
+        id: "",
84 84
         seq: 0
85 85
       }
86 86
       var found0 = false

+ 1
- 1
schema.proto View File

@@ -1,6 +1,6 @@
1 1
 message State {
2 2
   message Feed {
3
-    required bytes id = 1;
3
+    required string id = 1;
4 4
     required uint32 seq = 2;
5 5
   }
6 6
 

+ 5
- 5
test/messages.test.js View File

@@ -8,9 +8,9 @@ describe('schema.proto', (context) => {
8 8
   context('Feed', (assert, next) => {
9 9
     assert.ok(messages.State.Feed, 'has a schema for Feed')
10 10
     assert.throws(() => messages.State.Feed.encode({ dogSays: 'woof' }), 'Error: id is required', 'throws an error when missing id')
11
-    assert.throws(() => messages.State.Feed.encode({ id: crypto.randomBytes(32), dogSays: 'woof' }), 'Error: seq is required', 'throws an error when missing seq')
11
+    assert.throws(() => messages.State.Feed.encode({ id: crypto.randomBytes(32).toString('hex'), dogSays: 'woof' }), 'Error: seq is required', 'throws an error when missing seq')
12 12
 
13
-    var payload = messages.State.Feed.encode({ id: crypto.randomBytes(32), seq: 0 })
13
+    var payload = messages.State.Feed.encode({ id: crypto.randomBytes(32).toString('hex'), seq: 0 })
14 14
     assert.ok(payload instanceof Buffer, 'is a buffer')
15 15
 
16 16
     next()
@@ -28,14 +28,14 @@ describe('schema.proto', (context) => {
28 28
     var message = { filename: 'hello.txt', links: [] }
29 29
     assert.throws(() => messages.State.encode(message), 'Error: metadata is required', 'throws an error when missing metadata')
30 30
 
31
-    var message = { filename: 'hello.txt', links: [], metadata: { id: crypto.randomBytes(32), seq: 42 } }
31
+    var message = { filename: 'hello.txt', links: [], metadata: { id: crypto.randomBytes(32).toString('hex'), seq: 42 } }
32 32
     assert.throws(() => messages.State.encode(message), 'Error: content is required', 'throws an error when missing content')
33 33
 
34 34
     var message = {
35 35
       filename: 'hello.txt',
36 36
       links: [],
37
-      metadata: { id: crypto.randomBytes(32), seq: 0 },
38
-      content: { id: crypto.randomBytes(32), seq: 0 }
37
+      metadata: { id: crypto.randomBytes(32).toString('hex'), seq: 0 },
38
+      content: { id: crypto.randomBytes(32).toString('hex'), seq: 0 }
39 39
     }
40 40
 
41 41
     var payload = messages.State.encode(message)