Skip to content

Commit 900b768

Browse files
authored
feat: prevent duplicate jwt fetches (#43)
1 parent 0a93e79 commit 900b768

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

package-lock.json

Lines changed: 55 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"idb": "^7.1.1",
3232
"ipfs-unixfs-exporter": "https://gitpkg.now.sh/filecoin-saturn/js-ipfs-unixfs/packages/ipfs-unixfs-exporter?build",
3333
"msw": "^1.3.2",
34-
"multiformats": "^12.1.1"
34+
"multiformats": "^12.1.1",
35+
"p-limit": "^5.0.0"
3536
},
3637
"devDependencies": {
3738
"eslint": "^8.24.0",
@@ -61,4 +62,4 @@
6162
"publishConfig": {
6263
"access": "public"
6364
}
64-
}
65+
}

src/client.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22

33
import { CID } from 'multiformats'
4+
import pLimit from 'p-limit'
45

56
import { extractVerifiedContent } from './utils/car.js'
67
import { asAsyncIterable, asyncIteratorToBuffer } from './utils/itr.js'
@@ -57,6 +58,7 @@ export class Saturn {
5758
}
5859
this.storage = this.config.storage || memoryStorage()
5960
this.loadNodesPromise = this.config.experimental ? this._loadNodes(this.config) : null
61+
this.authLimiter = pLimit(1)
6062
}
6163

6264
/**
@@ -72,7 +74,7 @@ export class Saturn {
7274
CID.parse(cid)
7375

7476
if (options.clientKey) {
75-
options.jwt = await getJWT(options, this.storage)
77+
options.jwt = await this.authLimiter(() => getJWT(options, this.storage))
7678
}
7779
}
7880

@@ -166,7 +168,7 @@ export class Saturn {
166168
CID.parse(cid)
167169

168170
if (options.clientKey) {
169-
options.jwt = await getJWT(options, this.storage)
171+
options.jwt = await this.authLimiter(() => getJWT(options, this.storage))
170172
}
171173
}
172174

0 commit comments

Comments
 (0)