3
3
const { BlockstoreAdapter } = require ( 'interface-blockstore' )
4
4
const merge = require ( 'it-merge' )
5
5
const pushable = require ( 'it-pushable' )
6
+ const filter = require ( 'it-filter' )
6
7
7
8
/**
8
9
* @typedef {import('interface-blockstore').Blockstore } Blockstore
@@ -55,6 +56,10 @@ class BlockStorage extends BlockstoreAdapter {
55
56
* @param {AbortOptions } [options]
56
57
*/
57
58
async put ( cid , block , options = { } ) {
59
+ if ( await this . has ( cid ) ) {
60
+ return
61
+ }
62
+
58
63
if ( this . bitswap . isStarted ( ) ) {
59
64
await this . bitswap . put ( cid , block , options )
60
65
} else {
@@ -69,10 +74,12 @@ class BlockStorage extends BlockstoreAdapter {
69
74
* @param {AbortOptions } [options]
70
75
*/
71
76
async * putMany ( blocks , options = { } ) {
77
+ const missingBlocks = filter ( blocks , async ( { key } ) => { return ! ( await this . has ( key ) ) } )
78
+
72
79
if ( this . bitswap . isStarted ( ) ) {
73
- yield * this . bitswap . putMany ( blocks , options )
80
+ yield * this . bitswap . putMany ( missingBlocks , options )
74
81
} else {
75
- yield * this . child . putMany ( blocks , options )
82
+ yield * this . child . putMany ( missingBlocks , options )
76
83
}
77
84
}
78
85
0 commit comments