@@ -8,6 +8,12 @@ const fs = require('bare-fs')
88const path = require ( 'bare-path' )
99const store = require ( './store' )
1010
11+ // peer protocol commands
12+ const CMD_MANIFEST = 1
13+ const CMD_GET = 2
14+ const CMD_PUT = 3
15+ const CMD_DEL = 4
16+
1117class Space {
1218 constructor ( opts , emit ) {
1319 this . id = opts . id
@@ -54,21 +60,20 @@ class Space {
5460 spaceId : this . id , peerId : id , peers : this . _peers . size
5561 } )
5662
57- // only initiator starts the sync to avoid both sides requesting at once
5863 if ( isInitiator ) this . _syncWithPeer ( id )
5964 }
6065
6166 async _onRequest ( req ) {
6267 switch ( req . command ) {
6368
64- case 1 : { // manifest
69+ case CMD_MANIFEST : {
6570 const manifest = await this . _buildManifest ( )
6671 console . log ( '[space] sending manifest:' , manifest . length , 'files' )
6772 req . reply ( Buffer . from ( JSON . stringify ( manifest ) ) )
6873 break
6974 }
7075
71- case 2 : { // get
76+ case CMD_GET : {
7277 const key = req . data . toString ( )
7378 const abs = path . join ( this . _folder , key )
7479 console . log ( '[space] peer GET' , key )
@@ -80,7 +85,7 @@ class Space {
8085 break
8186 }
8287
83- case 3 : { // put
88+ case CMD_PUT : {
8489 const { key, data : b64 } = JSON . parse ( req . data . toString ( ) )
8590 const buf = Buffer . from ( b64 , 'base64' )
8691 const abs = path . join ( this . _folder , key )
@@ -99,7 +104,7 @@ class Space {
99104 break
100105 }
101106
102- case 4 : { // del
107+ case CMD_DEL : {
103108 const key = req . data . toString ( )
104109 const abs = path . join ( this . _folder , key )
105110 console . log ( '[space] peer DEL' , key )
@@ -120,14 +125,23 @@ class Space {
120125 }
121126 }
122127
128+ // wrap bare-rpc request in a promise
129+ _request ( rpc , command , data ) {
130+ return new Promise ( ( resolve , reject ) => {
131+ const req = rpc . request ( command )
132+ req . on ( 'response' , ( res ) => resolve ( res . data ) )
133+ req . on ( 'error' , reject )
134+ req . send ( data )
135+ } )
136+ }
137+
123138 async _syncWithPeer ( peerId ) {
124139 console . log ( '[space] syncing with' , peerId . slice ( 0 , 8 ) )
125140 const rpc = this . _peers . get ( peerId )
126141 if ( ! rpc ) { console . log ( '[space] peer gone' ) ; return }
127142
128143 try {
129- // get their manifest
130- const raw = await rpc . request ( 1 ) . send ( Buffer . alloc ( 0 ) )
144+ const raw = await this . _request ( rpc , CMD_MANIFEST , Buffer . alloc ( 0 ) )
131145 const theirFiles = JSON . parse ( raw . toString ( ) )
132146 console . log ( '[space] peer has' , theirFiles . length , 'files' )
133147
@@ -138,7 +152,7 @@ class Space {
138152 const mine = myMap . get ( their . key )
139153 if ( ! mine || their . mtime > mine . mtime ) {
140154 console . log ( '[space] pulling' , their . key )
141- const data = await rpc . request ( 2 ) . send ( Buffer . from ( their . key ) )
155+ const data = await this . _request ( rpc , CMD_GET , Buffer . from ( their . key ) )
142156 if ( data && data . length > 0 ) {
143157 const abs = path . join ( this . _folder , their . key )
144158 await fs . promises . mkdir ( path . dirname ( abs ) , { recursive : true } )
@@ -196,7 +210,7 @@ class Space {
196210 const data = await fs . promises . readFile ( filename )
197211 for ( const [ id , rpc ] of this . _peers ) {
198212 try {
199- await rpc . request ( 3 ) . send ( Buffer . from (
213+ await this . _request ( rpc , CMD_PUT , Buffer . from (
200214 JSON . stringify ( { key, data : data . toString ( 'base64' ) } )
201215 ) )
202216 console . log ( '[space] pushed' , key , 'to' , id . slice ( 0 , 8 ) )
@@ -207,7 +221,7 @@ class Space {
207221 } else if ( type === 'delete' ) {
208222 for ( const [ id , rpc ] of this . _peers ) {
209223 try {
210- await rpc . request ( 4 ) . send ( Buffer . from ( key ) )
224+ await this . _request ( rpc , CMD_DEL , Buffer . from ( key ) )
211225 console . log ( '[space] del pushed to' , id . slice ( 0 , 8 ) )
212226 } catch ( err ) {
213227 console . log ( '[space] del failed to' , id . slice ( 0 , 8 ) , err . message )
0 commit comments