2
2
3
3
const { createFromPrivKey } = require ( 'peer-id' )
4
4
const series = require ( 'async/series' )
5
- const Receptacle = require ( 'receptacle' )
6
5
7
6
const errcode = require ( 'err-code' )
8
7
const debug = require ( 'debug' )
@@ -13,20 +12,28 @@ const IpnsPublisher = require('./publisher')
13
12
const IpnsRepublisher = require ( './republisher' )
14
13
const IpnsResolver = require ( './resolver' )
15
14
const path = require ( './path' )
16
-
15
+ const { normalizePath } = require ( '../utils' )
16
+ const TLRU = require ( '../../utils/tlru' )
17
17
const defaultRecordTtl = 60 * 1000
18
18
19
19
class IPNS {
20
20
constructor ( routing , datastore , peerInfo , keychain , options ) {
21
21
this . publisher = new IpnsPublisher ( routing , datastore )
22
22
this . republisher = new IpnsRepublisher ( this . publisher , datastore , peerInfo , keychain , options )
23
23
this . resolver = new IpnsResolver ( routing )
24
- this . cache = new Receptacle ( { max : 1000 } ) // Create an LRU cache with max 1000 items
24
+ this . cache = new TLRU ( 1000 )
25
25
this . routing = routing
26
26
}
27
27
28
28
// Publish
29
- publish ( privKey , value , lifetime , callback ) {
29
+ publish ( privKey , value , lifetime = IpnsPublisher . defaultRecordLifetime , callback ) {
30
+ try {
31
+ value = normalizePath ( value )
32
+ } catch ( err ) {
33
+ log . error ( err )
34
+ return callback ( err )
35
+ }
36
+
30
37
series ( [
31
38
( cb ) => createFromPrivKey ( privKey . bytes , cb ) ,
32
39
( cb ) => this . publisher . publishWithEOL ( privKey , value , lifetime , cb )
@@ -38,12 +45,12 @@ class IPNS {
38
45
39
46
log ( `IPNS value ${ value } was published correctly` )
40
47
41
- // Add to cache
48
+ // // Add to cache
42
49
const id = results [ 0 ] . toB58String ( )
43
50
const ttEol = parseFloat ( lifetime )
44
51
const ttl = ( ttEol < defaultRecordTtl ) ? ttEol : defaultRecordTtl
45
52
46
- this . cache . set ( id , value , { ttl : ttl } )
53
+ this . cache . set ( id , value , ttl )
47
54
48
55
log ( `IPNS value ${ value } was cached correctly` )
49
56
@@ -96,7 +103,7 @@ class IPNS {
96
103
// Initialize keyspace
97
104
// sets the ipns record for the given key to point to an empty directory
98
105
initializeKeyspace ( privKey , value , callback ) {
99
- this . publisher . publish ( privKey , value , callback )
106
+ this . publish ( privKey , value , IpnsPublisher . defaultRecordLifetime , callback )
100
107
}
101
108
}
102
109
0 commit comments