Skip to content

Commit 0a93e79

Browse files
authored
feat: make clientKey optional (#42)
1 parent 9217c16 commit 0a93e79

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/client.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ export class Saturn {
4848
downloadTimeout: 0
4949
}, config)
5050

51-
if (!this.config.clientKey) {
52-
throw new Error('clientKey is required')
53-
}
54-
5551
this.logs = []
5652
this.nodes = []
5753
this.reportingLogs = process?.env?.NODE_ENV !== 'development'
@@ -74,17 +70,20 @@ export class Saturn {
7470
if (!opts.originFallback) {
7571
const [cid] = (cidPath ?? '').split('/')
7672
CID.parse(cid)
77-
const jwt = await getJWT(options, this.storage)
78-
options.jwt = jwt
79-
}
8073

81-
if (!isBrowserContext) {
82-
options.headers = {
83-
...(options.headers || {}),
84-
Authorization: 'Bearer ' + options.jwt
74+
if (options.clientKey) {
75+
options.jwt = await getJWT(options, this.storage)
8576
}
8677
}
8778

79+
options.headers = {
80+
...(options.headers || {})
81+
}
82+
83+
if (!isBrowserContext && options.jwt) {
84+
options.headers.Authorization = 'Bearer ' + options.jwt
85+
}
86+
8887
let nodes = options.nodes
8988
if (!nodes || nodes.length === 0) {
9089
const replacementNode = { url: options.cdnURL }
@@ -165,8 +164,10 @@ export class Saturn {
165164
if (!opts.originFallback) {
166165
const [cid] = (cidPath ?? '').split('/')
167166
CID.parse(cid)
168-
const jwt = await getJWT(this.config, this.storage)
169-
options.jwt = jwt
167+
168+
if (options.clientKey) {
169+
options.jwt = await getJWT(options, this.storage)
170+
}
170171
}
171172

172173
const node = options.nodes && options.nodes[0]
@@ -184,12 +185,14 @@ export class Saturn {
184185
controller.abort()
185186
}, options.connectTimeout)
186187

187-
if (!isBrowserContext) {
188-
options.headers = {
189-
...(options.headers || {}),
190-
Authorization: 'Bearer ' + options.jwt
191-
}
188+
options.headers = {
189+
...(options.headers || {})
192190
}
191+
192+
if (!isBrowserContext && options.jwt) {
193+
options.headers.Authorization = 'Bearer ' + options.jwt
194+
}
195+
193196
let res
194197
try {
195198
res = await fetch(parseUrl(url), { signal: controller.signal, ...options })
@@ -417,7 +420,7 @@ export class Saturn {
417420
url.searchParams.set('dag-scope', 'entity')
418421
}
419422

420-
if (isBrowserContext) {
423+
if (isBrowserContext && opts.jwt) {
421424
url.searchParams.set('jwt', opts.jwt)
422425
}
423426

src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @property {boolean} [originFallback] - Is this a fallback to the customer origin
2222
* @property {boolean} [raceNodes] - Does the fetch race multiple nodes on requests.
2323
* @property {string} [customerFallbackURL] - Customer Origin that is a fallback.
24+
* @property {string} [jwt] - JWT for L1 request.
2425
* @property {number} [connectTimeout=5000] - Connection timeout in milliseconds.
2526
* @property {number} [downloadTimeout=0] - Download timeout in milliseconds.
2627
* @property {AbortController} [controller]

0 commit comments

Comments
 (0)