22
33const { createFromPrivKey } = require ( 'peer-id' )
44const series = require ( 'async/series' )
5- const Receptacle = require ( 'receptacle' )
65
76const errcode = require ( 'err-code' )
87const debug = require ( 'debug' )
@@ -13,20 +12,28 @@ const IpnsPublisher = require('./publisher')
1312const IpnsRepublisher = require ( './republisher' )
1413const IpnsResolver = require ( './resolver' )
1514const path = require ( './path' )
16-
15+ const { normalizePath } = require ( '../utils' )
16+ const TLRU = require ( '../../utils/tlru' )
1717const defaultRecordTtl = 60 * 1000
1818
1919class IPNS {
2020 constructor ( routing , datastore , peerInfo , keychain , options ) {
2121 this . publisher = new IpnsPublisher ( routing , datastore )
2222 this . republisher = new IpnsRepublisher ( this . publisher , datastore , peerInfo , keychain , options )
2323 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 )
2525 this . routing = routing
2626 }
2727
2828 // 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+
3037 series ( [
3138 ( cb ) => createFromPrivKey ( privKey . bytes , cb ) ,
3239 ( cb ) => this . publisher . publishWithEOL ( privKey , value , lifetime , cb )
@@ -38,12 +45,12 @@ class IPNS {
3845
3946 log ( `IPNS value ${ value } was published correctly` )
4047
41- // Add to cache
48+ // // Add to cache
4249 const id = results [ 0 ] . toB58String ( )
4350 const ttEol = parseFloat ( lifetime )
4451 const ttl = ( ttEol < defaultRecordTtl ) ? ttEol : defaultRecordTtl
4552
46- this . cache . set ( id , value , { ttl : ttl } )
53+ this . cache . set ( id , value , ttl )
4754
4855 log ( `IPNS value ${ value } was cached correctly` )
4956
@@ -77,9 +84,7 @@ class IPNS {
7784 const result = this . cache . get ( id )
7885
7986 if ( result ) {
80- return callback ( null , {
81- path : result
82- } )
87+ return callback ( null , result )
8388 }
8489 }
8590
@@ -91,18 +96,17 @@ class IPNS {
9196
9297 log ( `IPNS record from ${ name } was resolved correctly` )
9398
94- callback ( null , {
95- path : result
96- } )
99+ callback ( null , result )
97100 } )
98101 }
99102
100103 // Initialize keyspace
101104 // sets the ipns record for the given key to point to an empty directory
102105 initializeKeyspace ( privKey , value , callback ) {
103- this . publisher . publish ( privKey , value , callback )
106+ this . publish ( privKey , value , IpnsPublisher . defaultRecordLifetime , callback )
104107 }
105108}
106109
107- exports = module . exports = IPNS
108- exports . path = path
110+ IPNS . path = path
111+
112+ module . exports = IPNS
0 commit comments